Skip to content

Commit 5a3f23d

Browse files
committed
Btrfs: add extra flushing for renames and truncates
Renames and truncates are both common ways to replace old data with new data. The filesystem can make an effort to make sure the new data is on disk before actually replacing the old data. This is especially important for rename, which many application use as though it were atomic for both the data and the metadata involved. The current btrfs code will happily replace a file that is fully on disk with one that was just created and still has pending IO. If we crash after transaction commit but before the IO is done, we'll end up replacing a good file with a zero length file. The solution used here is to create a list of inodes that need special ordering and force them to disk before the commit is done. This is similar to the ext3 style data=ordering, except it is only done on selected files. Btrfs is able to get away with this because it does not wait on commits very often, even for fsync (which use a sub-commit). For renames, we order the file when it wasn't already on disk and when it is replacing an existing file. Larger files are sent to filemap_flush right away (before the transaction handle is opened). For truncates, we order if the file goes from non-zero size down to zero size. This is a little different, because at the time of the truncate the file has no dirty bytes to order. But, we flag the inode so that it is added to the ordered list on close (via release method). We also immediately add it to the ordered list of the current transaction so that we can try to flush down any writes the application sneaks in before commit. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 1a81af4 commit 5a3f23d

8 files changed

Lines changed: 288 additions & 7 deletions

File tree

‎fs/btrfs/btrfs_inode.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ struct btrfs_inode {
6666
*/
6767
struct list_head delalloc_inodes;
6868

69+
/*
70+
* list for tracking inodes that must be sent to disk before a
71+
* rename or truncate commit
72+
*/
73+
struct list_head ordered_operations;
74+
6975
/* the space_info for where this inode's data allocations are done */
7076
struct btrfs_space_info *space_info;
7177

@@ -122,6 +128,18 @@ struct btrfs_inode {
122128
*/
123129
u64 last_unlink_trans;
124130

131+
/*
132+
* ordered_data_close is set by truncate when a file that used
133+
* to have good data has been truncated to zero. When it is set
134+
* the btrfs file release call will add this inode to the
135+
* ordered operations list so that we make sure to flush out any
136+
* new data the application may have written before commit.
137+
*
138+
* yes, its silly to have a single bitflag, but we might grow more
139+
* of these.
140+
*/
141+
unsigned ordered_data_close:1;
142+
125143
struct inode vfs_inode;
126144
};
127145

‎fs/btrfs/ctree.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ struct btrfs_ordered_sum;
4545

4646
#define BTRFS_MAX_LEVEL 8
4747

48+
/*
49+
* files bigger than this get some pre-flushing when they are added
50+
* to the ordered operations list. That way we limit the total
51+
* work done by the commit
52+
*/
53+
#define BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT (8 * 1024 * 1024)
54+
4855
/* holds pointers to all of the tree roots */
4956
#define BTRFS_ROOT_TREE_OBJECTID 1ULL
5057

@@ -727,6 +734,15 @@ struct btrfs_fs_info {
727734
struct mutex volume_mutex;
728735
struct mutex tree_reloc_mutex;
729736

737+
/*
738+
* this protects the ordered operations list only while we are
739+
* processing all of the entries on it. This way we make
740+
* sure the commit code doesn't find the list temporarily empty
741+
* because another function happens to be doing non-waiting preflush
742+
* before jumping into the main commit.
743+
*/
744+
struct mutex ordered_operations_mutex;
745+
730746
struct list_head trans_list;
731747
struct list_head hashers;
732748
struct list_head dead_roots;
@@ -741,9 +757,28 @@ struct btrfs_fs_info {
741757
* ordered extents
742758
*/
743759
spinlock_t ordered_extent_lock;
760+
761+
/*
762+
* all of the data=ordered extents pending writeback
763+
* these can span multiple transactions and basically include
764+
* every dirty data page that isn't from nodatacow
765+
*/
744766
struct list_head ordered_extents;
767+
768+
/*
769+
* all of the inodes that have delalloc bytes. It is possible for
770+
* this list to be empty even when there is still dirty data=ordered
771+
* extents waiting to finish IO.
772+
*/
745773
struct list_head delalloc_inodes;
746774

775+
/*
776+
* special rename and truncate targets that must be on disk before
777+
* we're allowed to commit. This is basically the ext3 style
778+
* data=ordered list.
779+
*/
780+
struct list_head ordered_operations;
781+
747782
/*
748783
* there is a pool of worker threads for checksumming during writes
749784
* and a pool for checksumming after reads. This is because readers

‎fs/btrfs/disk-io.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
15721572
INIT_LIST_HEAD(&fs_info->dead_roots);
15731573
INIT_LIST_HEAD(&fs_info->hashers);
15741574
INIT_LIST_HEAD(&fs_info->delalloc_inodes);
1575+
INIT_LIST_HEAD(&fs_info->ordered_operations);
15751576
spin_lock_init(&fs_info->delalloc_lock);
15761577
spin_lock_init(&fs_info->new_trans_lock);
15771578
spin_lock_init(&fs_info->ref_cache_lock);
@@ -1643,6 +1644,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
16431644
insert_inode_hash(fs_info->btree_inode);
16441645

16451646
mutex_init(&fs_info->trans_mutex);
1647+
mutex_init(&fs_info->ordered_operations_mutex);
16461648
mutex_init(&fs_info->tree_log_mutex);
16471649
mutex_init(&fs_info->drop_mutex);
16481650
mutex_init(&fs_info->pinned_mutex);

‎fs/btrfs/file.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,20 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
11611161
page_cache_release(pinned[1]);
11621162
*ppos = pos;
11631163

1164+
/*
1165+
* we want to make sure fsync finds this change
1166+
* but we haven't joined a transaction running right now.
1167+
*
1168+
* Later on, someone is sure to update the inode and get the
1169+
* real transid recorded.
1170+
*
1171+
* We set last_trans now to the fs_info generation + 1,
1172+
* this will either be one more than the running transaction
1173+
* or the generation used for the next transaction if there isn't
1174+
* one running right now.
1175+
*/
1176+
BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1177+
11641178
if (num_written > 0 && will_write) {
11651179
struct btrfs_trans_handle *trans;
11661180

@@ -1194,6 +1208,18 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
11941208

11951209
int btrfs_release_file(struct inode *inode, struct file *filp)
11961210
{
1211+
/*
1212+
* ordered_data_close is set by settattr when we are about to truncate
1213+
* a file from a non-zero size to a zero size. This tries to
1214+
* flush down new bytes that may have been written if the
1215+
* application were using truncate to replace a file in place.
1216+
*/
1217+
if (BTRFS_I(inode)->ordered_data_close) {
1218+
BTRFS_I(inode)->ordered_data_close = 0;
1219+
btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1220+
if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1221+
filemap_flush(inode->i_mapping);
1222+
}
11971223
if (filp->private_data)
11981224
btrfs_ioctl_trans_end(filp);
11991225
return 0;

‎fs/btrfs/inode.c‎

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,11 +2907,21 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
29072907
if (err)
29082908
return err;
29092909

2910-
if (S_ISREG(inode->i_mode) &&
2911-
attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
2912-
err = btrfs_cont_expand(inode, attr->ia_size);
2913-
if (err)
2914-
return err;
2910+
if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
2911+
if (attr->ia_size > inode->i_size) {
2912+
err = btrfs_cont_expand(inode, attr->ia_size);
2913+
if (err)
2914+
return err;
2915+
} else if (inode->i_size > 0 &&
2916+
attr->ia_size == 0) {
2917+
2918+
/* we're truncating a file that used to have good
2919+
* data down to zero. Make sure it gets into
2920+
* the ordered flush list so that any new writes
2921+
* get down to disk quickly.
2922+
*/
2923+
BTRFS_I(inode)->ordered_data_close = 1;
2924+
}
29152925
}
29162926

29172927
err = inode_setattr(inode, attr);
@@ -3050,6 +3060,7 @@ static noinline void init_btrfs_i(struct inode *inode)
30503060
extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
30513061
inode->i_mapping, GFP_NOFS);
30523062
INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3063+
INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
30533064
btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
30543065
mutex_init(&BTRFS_I(inode)->extent_mutex);
30553066
mutex_init(&BTRFS_I(inode)->log_mutex);
@@ -4419,6 +4430,8 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
44194430
}
44204431
ClearPageChecked(page);
44214432
set_page_dirty(page);
4433+
4434+
BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
44224435
unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
44234436

44244437
out_unlock:
@@ -4444,6 +4457,27 @@ static void btrfs_truncate(struct inode *inode)
44444457
btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
44454458

44464459
trans = btrfs_start_transaction(root, 1);
4460+
4461+
/*
4462+
* setattr is responsible for setting the ordered_data_close flag,
4463+
* but that is only tested during the last file release. That
4464+
* could happen well after the next commit, leaving a great big
4465+
* window where new writes may get lost if someone chooses to write
4466+
* to this file after truncating to zero
4467+
*
4468+
* The inode doesn't have any dirty data here, and so if we commit
4469+
* this is a noop. If someone immediately starts writing to the inode
4470+
* it is very likely we'll catch some of their writes in this
4471+
* transaction, and the commit will find this file on the ordered
4472+
* data list with good things to send down.
4473+
*
4474+
* This is a best effort solution, there is still a window where
4475+
* using truncate to replace the contents of the file will
4476+
* end up with a zero length file after a crash.
4477+
*/
4478+
if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
4479+
btrfs_add_ordered_operation(trans, root, inode);
4480+
44474481
btrfs_set_trans_block_group(trans, inode);
44484482
btrfs_i_size_write(inode, inode->i_size);
44494483

@@ -4520,12 +4554,15 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
45204554
ei->i_acl = BTRFS_ACL_NOT_CACHED;
45214555
ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
45224556
INIT_LIST_HEAD(&ei->i_orphan);
4557+
INIT_LIST_HEAD(&ei->ordered_operations);
45234558
return &ei->vfs_inode;
45244559
}
45254560

45264561
void btrfs_destroy_inode(struct inode *inode)
45274562
{
45284563
struct btrfs_ordered_extent *ordered;
4564+
struct btrfs_root *root = BTRFS_I(inode)->root;
4565+
45294566
WARN_ON(!list_empty(&inode->i_dentry));
45304567
WARN_ON(inode->i_data.nrpages);
45314568

@@ -4536,13 +4573,24 @@ void btrfs_destroy_inode(struct inode *inode)
45364573
BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
45374574
posix_acl_release(BTRFS_I(inode)->i_default_acl);
45384575

4539-
spin_lock(&BTRFS_I(inode)->root->list_lock);
4576+
/*
4577+
* Make sure we're properly removed from the ordered operation
4578+
* lists.
4579+
*/
4580+
smp_mb();
4581+
if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
4582+
spin_lock(&root->fs_info->ordered_extent_lock);
4583+
list_del_init(&BTRFS_I(inode)->ordered_operations);
4584+
spin_unlock(&root->fs_info->ordered_extent_lock);
4585+
}
4586+
4587+
spin_lock(&root->list_lock);
45404588
if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
45414589
printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
45424590
" list\n", inode->i_ino);
45434591
dump_stack();
45444592
}
4545-
spin_unlock(&BTRFS_I(inode)->root->list_lock);
4593+
spin_unlock(&root->list_lock);
45464594

45474595
while (1) {
45484596
ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
@@ -4667,8 +4715,27 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
46674715
if (ret)
46684716
goto out_unlock;
46694717

4718+
/*
4719+
* we're using rename to replace one file with another.
4720+
* and the replacement file is large. Start IO on it now so
4721+
* we don't add too much work to the end of the transaction
4722+
*/
4723+
if (new_inode && old_inode && S_ISREG(old_inode->i_mode) &&
4724+
new_inode->i_size &&
4725+
old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
4726+
filemap_flush(old_inode->i_mapping);
4727+
46704728
trans = btrfs_start_transaction(root, 1);
46714729

4730+
/*
4731+
* make sure the inode gets flushed if it is replacing
4732+
* something.
4733+
*/
4734+
if (new_inode && new_inode->i_size &&
4735+
old_inode && S_ISREG(old_inode->i_mode)) {
4736+
btrfs_add_ordered_operation(trans, root, old_inode);
4737+
}
4738+
46724739
/*
46734740
* this is an ugly little race, but the rename is required to make
46744741
* sure that if we crash, the inode is either at the old name

0 commit comments

Comments
 (0)