Skip to content

Commit 7ea394f

Browse files
Yan Zhengchrismason-xx
authored andcommitted
Btrfs: Fix nodatacow for the new data=ordered mode
Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 00e4e6b commit 7ea394f

7 files changed

Lines changed: 74 additions & 34 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,8 @@ static inline struct dentry *fdentry(struct file *file) {
14031403
}
14041404

14051405
/* extent-tree.c */
1406-
int btrfs_cross_ref_exists(struct btrfs_root *root,
1406+
int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1407+
struct btrfs_root *root,
14071408
struct btrfs_key *key, u64 bytenr);
14081409
int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
14091410
struct btrfs_root *root);

‎fs/btrfs/extent-tree.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,10 +893,10 @@ static int get_reference_status(struct btrfs_root *root, u64 bytenr,
893893
return ret;
894894
}
895895

896-
int btrfs_cross_ref_exists(struct btrfs_root *root,
896+
int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
897+
struct btrfs_root *root,
897898
struct btrfs_key *key, u64 bytenr)
898899
{
899-
struct btrfs_trans_handle *trans;
900900
struct btrfs_root *old_root;
901901
struct btrfs_path *path = NULL;
902902
struct extent_buffer *eb;
@@ -908,6 +908,7 @@ int btrfs_cross_ref_exists(struct btrfs_root *root,
908908
int level;
909909
int ret;
910910

911+
BUG_ON(trans == NULL);
911912
BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
912913
ret = get_reference_status(root, bytenr, 0, key->objectid,
913914
&min_generation, &ref_count);
@@ -917,7 +918,6 @@ int btrfs_cross_ref_exists(struct btrfs_root *root,
917918
if (ref_count != 1)
918919
return 1;
919920

920-
trans = btrfs_start_transaction(root, 0);
921921
old_root = root->dirty_root->root;
922922
ref_generation = old_root->root_key.offset;
923923

@@ -973,7 +973,6 @@ int btrfs_cross_ref_exists(struct btrfs_root *root,
973973
out:
974974
if (path)
975975
btrfs_free_path(path);
976-
btrfs_end_transaction(trans, root);
977976
return ret;
978977
}
979978

@@ -3320,7 +3319,7 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
33203319
mutex_unlock(&root->fs_info->alloc_mutex);
33213320

33223321
btrfs_start_delalloc_inodes(root);
3323-
btrfs_wait_ordered_extents(tree_root);
3322+
btrfs_wait_ordered_extents(tree_root, 0);
33243323

33253324
mutex_lock(&root->fs_info->alloc_mutex);
33263325

@@ -3407,7 +3406,7 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
34073406
btrfs_clean_old_snapshots(tree_root);
34083407

34093408
btrfs_start_delalloc_inodes(root);
3410-
btrfs_wait_ordered_extents(tree_root);
3409+
btrfs_wait_ordered_extents(tree_root, 0);
34113410

34123411
trans = btrfs_start_transaction(tree_root, 1);
34133412
btrfs_commit_transaction(trans, tree_root);

‎fs/btrfs/inode.c‎

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
166166

167167
cur_alloc_size = ins.offset;
168168
ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
169-
ins.offset);
169+
ins.offset, 0);
170170
BUG_ON(ret);
171171
if (num_bytes < cur_alloc_size) {
172172
printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
@@ -187,31 +187,32 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
187187
u64 extent_start;
188188
u64 extent_end;
189189
u64 bytenr;
190-
u64 cow_end;
191190
u64 loops = 0;
192191
u64 total_fs_bytes;
193192
struct btrfs_root *root = BTRFS_I(inode)->root;
194193
struct btrfs_block_group_cache *block_group;
194+
struct btrfs_trans_handle *trans;
195195
struct extent_buffer *leaf;
196196
int found_type;
197197
struct btrfs_path *path;
198198
struct btrfs_file_extent_item *item;
199199
int ret;
200-
int err;
200+
int err = 0;
201201
struct btrfs_key found_key;
202202

203203
total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
204204
path = btrfs_alloc_path();
205205
BUG_ON(!path);
206+
trans = btrfs_join_transaction(root, 1);
207+
BUG_ON(!trans);
206208
again:
207209
ret = btrfs_lookup_file_extent(NULL, root, path,
208210
inode->i_ino, start, 0);
209211
if (ret < 0) {
210-
btrfs_free_path(path);
211-
return ret;
212+
err = ret;
213+
goto out;
212214
}
213215

214-
cow_end = end;
215216
if (ret != 0) {
216217
if (path->slots[0] == 0)
217218
goto not_found;
@@ -244,12 +245,11 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
244245
if (start < extent_start || start >= extent_end)
245246
goto not_found;
246247

247-
cow_end = min(end, extent_end - 1);
248248
bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249249
if (bytenr == 0)
250250
goto not_found;
251251

252-
if (btrfs_cross_ref_exists(root, &found_key, bytenr))
252+
if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
253253
goto not_found;
254254
/*
255255
* we may be called by the resizer, make sure we're inside
@@ -260,24 +260,32 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
260260
if (!block_group || block_group->ro)
261261
goto not_found;
262262

263+
bytenr += btrfs_file_extent_offset(leaf, item);
264+
extent_num_bytes = min(end + 1, extent_end) - start;
265+
ret = btrfs_add_ordered_extent(inode, start, bytenr,
266+
extent_num_bytes, 1);
267+
if (ret) {
268+
err = ret;
269+
goto out;
270+
}
271+
272+
btrfs_release_path(root, path);
263273
start = extent_end;
274+
if (start <= end) {
275+
loops++;
276+
goto again;
277+
}
264278
} else {
265-
goto not_found;
266-
}
267-
loop:
268-
if (start > end) {
279+
not_found:
280+
btrfs_end_transaction(trans, root);
269281
btrfs_free_path(path);
270-
return 0;
282+
return cow_file_range(inode, start, end);
271283
}
272-
btrfs_release_path(root, path);
273-
loops++;
274-
goto again;
275-
276-
not_found:
277-
btrfs_release_path(root, path);
278-
cow_file_range(inode, start, end);
279-
start = end + 1;
280-
goto loop;
284+
out:
285+
WARN_ON(err);
286+
btrfs_end_transaction(trans, root);
287+
btrfs_free_path(path);
288+
return err;
281289
}
282290

283291
static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
@@ -385,6 +393,11 @@ int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
385393
goto mapit;
386394
}
387395

396+
if (btrfs_test_opt(root, NODATASUM) ||
397+
btrfs_test_flag(inode, NODATASUM)) {
398+
goto mapit;
399+
}
400+
388401
return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
389402
inode, rw, bio, mirror_num,
390403
__btrfs_submit_bio_hook);
@@ -527,6 +540,8 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
527540

528541
ordered_extent = btrfs_lookup_ordered_extent(inode, start);
529542
BUG_ON(!ordered_extent);
543+
if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
544+
goto nocow;
530545

531546
lock_extent(io_tree, ordered_extent->file_offset,
532547
ordered_extent->file_offset + ordered_extent->len - 1,
@@ -567,6 +582,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
567582
unlock_extent(io_tree, ordered_extent->file_offset,
568583
ordered_extent->file_offset + ordered_extent->len - 1,
569584
GFP_NOFS);
585+
nocow:
570586
add_pending_csums(trans, inode, ordered_extent->file_offset,
571587
&ordered_extent->list);
572588

‎fs/btrfs/ioctl.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/bit_spinlock.h>
3737
#include <linux/version.h>
3838
#include <linux/xattr.h>
39+
#include <linux/vmalloc.h>
3940
#include "ctree.h"
4041
#include "disk-io.h"
4142
#include "transaction.h"

‎fs/btrfs/ordered-data.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
152152
* inserted.
153153
*/
154154
int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
155-
u64 start, u64 len)
155+
u64 start, u64 len, int nocow)
156156
{
157157
struct btrfs_ordered_inode_tree *tree;
158158
struct rb_node *node;
@@ -168,6 +168,8 @@ int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
168168
entry->start = start;
169169
entry->len = len;
170170
entry->inode = inode;
171+
if (nocow)
172+
set_bit(BTRFS_ORDERED_NOCOW, &entry->flags);
171173

172174
/* one ref for the tree */
173175
atomic_set(&entry->refs, 1);
@@ -303,21 +305,28 @@ int btrfs_remove_ordered_extent(struct inode *inode,
303305
return 0;
304306
}
305307

306-
int btrfs_wait_ordered_extents(struct btrfs_root *root)
308+
int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only)
307309
{
308310
struct list_head splice;
309311
struct list_head *cur;
312+
struct list_head *tmp;
310313
struct btrfs_ordered_extent *ordered;
311314
struct inode *inode;
312315

313316
INIT_LIST_HEAD(&splice);
314317

315318
spin_lock(&root->fs_info->ordered_extent_lock);
316319
list_splice_init(&root->fs_info->ordered_extents, &splice);
317-
while(!list_empty(&splice)) {
320+
list_for_each_safe(cur, tmp, &splice) {
318321
cur = splice.next;
319322
ordered = list_entry(cur, struct btrfs_ordered_extent,
320323
root_extent_list);
324+
if (nocow_only &&
325+
!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
326+
cond_resched_lock(&root->fs_info->ordered_extent_lock);
327+
continue;
328+
}
329+
321330
list_del_init(&ordered->root_extent_list);
322331
atomic_inc(&ordered->refs);
323332
inode = ordered->inode;
@@ -338,6 +347,7 @@ int btrfs_wait_ordered_extents(struct btrfs_root *root)
338347

339348
spin_lock(&root->fs_info->ordered_extent_lock);
340349
}
350+
list_splice_init(&splice, &root->fs_info->ordered_extents);
341351
spin_unlock(&root->fs_info->ordered_extent_lock);
342352
return 0;
343353
}

‎fs/btrfs/ordered-data.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ struct btrfs_ordered_sum {
6464

6565
#define BTRFS_ORDERED_COMPLETE 1 /* set when removed from the tree */
6666

67+
#define BTRFS_ORDERED_NOCOW 2 /* set when we want to write in place */
68+
6769
struct btrfs_ordered_extent {
6870
/* logical offset in the file */
6971
u64 file_offset;
@@ -125,7 +127,7 @@ int btrfs_remove_ordered_extent(struct inode *inode,
125127
int btrfs_dec_test_ordered_pending(struct inode *inode,
126128
u64 file_offset, u64 io_size);
127129
int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
128-
u64 start, u64 len);
130+
u64 start, u64 len, int nocow);
129131
int btrfs_add_ordered_sum(struct inode *inode,
130132
struct btrfs_ordered_extent *entry,
131133
struct btrfs_ordered_sum *sum);
@@ -143,5 +145,5 @@ int btrfs_wait_on_page_writeback_range(struct address_space *mapping,
143145
pgoff_t start, pgoff_t end);
144146
int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start,
145147
loff_t end, int sync_mode);
146-
int btrfs_wait_ordered_extents(struct btrfs_root *root);
148+
int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only);
147149
#endif

‎fs/btrfs/transaction.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
438438

439439
free_extent_buffer(root->commit_root);
440440
root->commit_root = NULL;
441+
root->dirty_root = NULL;
441442

442443
spin_lock(&root->list_lock);
443444
list_del_init(&dirty->root->dead_list);
@@ -461,6 +462,7 @@ static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
461462
sizeof(struct btrfs_disk_key));
462463
root->root_item.drop_level = 0;
463464
root->commit_root = NULL;
465+
root->dirty_root = NULL;
464466
root->root_key.offset = root->fs_info->generation;
465467
btrfs_set_root_bytenr(&root->root_item,
466468
root->node->start);
@@ -762,7 +764,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
762764
}
763765

764766
do {
767+
int snap_pending = 0;
765768
joined = cur_trans->num_joined;
769+
if (!list_empty(&trans->transaction->pending_snapshots))
770+
snap_pending = 1;
771+
766772
WARN_ON(cur_trans != trans->transaction);
767773
prepare_to_wait(&cur_trans->writer_wait, &wait,
768774
TASK_UNINTERRUPTIBLE);
@@ -774,6 +780,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
774780

775781
mutex_unlock(&root->fs_info->trans_mutex);
776782

783+
if (snap_pending) {
784+
ret = btrfs_wait_ordered_extents(root, 1);
785+
BUG_ON(ret);
786+
}
787+
777788
schedule_timeout(timeout);
778789

779790
mutex_lock(&root->fs_info->trans_mutex);

0 commit comments

Comments
 (0)