Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'for-6.14-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux

Pull btrfs updates from David Sterba:
"User visible changes, features:

- rebuilding of the free space tree at mount time is done in more
transactions, fix potential hangs when the transaction thread is
blocked due to large amount of block groups

- more read IO balancing strategies (experimental config), add two
new ways how to select a device for read if the profiles allow that
(all RAID1*), the current default selects the device by pid which
is good on average but less performant for single reader workloads

- select preferred device for all reads (namely for testing)
- round-robin, balance reads across devices relevant for the
requested IO range

- add encoded write ioctl support to io_uring (read was added in
6.12), basis for writing send stream using that instead of
syscalls, non-blocking mode is not yet implemented

- support FS_IOC_READ_VERITY_METADATA, applications can use the
metadata to do their own verification

- pass inode's i_write_hint to bios, for parity with other
filesystems, ioctls F_GET_RW_HINT/F_SET_RW_HINT

Core:

- in zoned mode: allow to directly reclaim a block group by simply
resetting it, then it can be reused and another block group does
not need to be allocated

- super block validation now also does more comprehensive sys array
validation, adding it to the points where superblock is validated
(post-read, pre-write)

- subpage mode fixes:
- fix double accounting of blocks due to some races
- improved or fixed error handling in a few cases (compression,
delalloc)

- raid stripe tree:
- fix various cases with extent range splitting or deleting
- implement hole punching to extent range
- reduce number of stripe tree lookups during bio submission
- more self-tests

- updated self-tests (delayed refs)

- error handling improvements

- cleanups, refactoring
- remove rest of backref caching infrastructure from relocation,
not needed anymore
- error message updates
- remove unnecessary calls when extent buffer was marked dirty
- unused parameter removal
- code moved to new files

Other code changes: add rb_find_add_cached() to the rb-tree API"

* tag 'for-6.14-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: (127 commits)
btrfs: selftests: add a selftest for deleting two out of three extents
btrfs: selftests: add test for punching a hole into 3 RAID stripe-extents
btrfs: selftests: add selftest for punching holes into the RAID stripe extents
btrfs: selftests: test RAID stripe-tree deletion spanning two items
btrfs: selftests: don't split RAID extents in half
btrfs: selftests: check for correct return value of failed lookup
btrfs: don't use btrfs_set_item_key_safe on RAID stripe-extents
btrfs: implement hole punching for RAID stripe extents
btrfs: fix deletion of a range spanning parts two RAID stripe extents
btrfs: fix tail delete of RAID stripe-extents
btrfs: fix front delete range calculation for RAID stripe extents
btrfs: assert RAID stripe-extent length is always greater than 0
btrfs: don't try to delete RAID stripe-extents if we don't need to
btrfs: selftests: correct RAID stripe-tree feature flag setting
btrfs: add io_uring interface for encoded writes
btrfs: remove the unused locked_folio parameter from btrfs_cleanup_ordered_extents()
btrfs: add extra error messages for delalloc range related errors
btrfs: subpage: dump the involved bitmap when ASSERT() failed
btrfs: subpage: fix the bitmap dump of the locked flags
btrfs: do proper folio cleanup when run_delalloc_nocow() failed
...

+3751 -1343
+1 -1
fs/btrfs/Makefile
··· 44 44 tests/extent-buffer-tests.o tests/btrfs-tests.o \ 45 45 tests/extent-io-tests.o tests/inode-tests.o tests/qgroup-tests.o \ 46 46 tests/free-space-tree-tests.o tests/extent-map-tests.o \ 47 - tests/raid-stripe-tree-tests.o 47 + tests/raid-stripe-tree-tests.o tests/delayed-refs-tests.o
+3 -3
fs/btrfs/async-thread.c
··· 18 18 }; 19 19 20 20 #define NO_THRESHOLD (-1) 21 - #define DFT_THRESHOLD (32) 21 + #define DEFAULT_THRESHOLD (32) 22 22 23 23 struct btrfs_workqueue { 24 24 struct workqueue_struct *normal_wq; ··· 94 94 95 95 ret->limit_active = limit_active; 96 96 if (thresh == 0) 97 - thresh = DFT_THRESHOLD; 97 + thresh = DEFAULT_THRESHOLD; 98 98 /* For low threshold, disabling threshold is a better choice */ 99 - if (thresh < DFT_THRESHOLD) { 99 + if (thresh < DEFAULT_THRESHOLD) { 100 100 ret->current_active = limit_active; 101 101 ret->thresh = NO_THRESHOLD; 102 102 } else {
+63 -109
fs/btrfs/backref.c
··· 250 250 return 0; 251 251 } 252 252 253 + static int prelim_ref_rb_add_cmp(const struct rb_node *new, 254 + const struct rb_node *exist) 255 + { 256 + const struct prelim_ref *ref_new = 257 + rb_entry(new, struct prelim_ref, rbnode); 258 + const struct prelim_ref *ref_exist = 259 + rb_entry(exist, struct prelim_ref, rbnode); 260 + 261 + /* 262 + * prelim_ref_compare() expects the first parameter as the existing one, 263 + * different from the rb_find_add_cached() order. 264 + */ 265 + return prelim_ref_compare(ref_exist, ref_new); 266 + } 267 + 253 268 static void update_share_count(struct share_check *sc, int oldcount, 254 269 int newcount, const struct prelim_ref *newref) 255 270 { ··· 293 278 struct share_check *sc) 294 279 { 295 280 struct rb_root_cached *root; 296 - struct rb_node **p; 297 - struct rb_node *parent = NULL; 298 - struct prelim_ref *ref; 299 - int result; 300 - bool leftmost = true; 281 + struct rb_node *exist; 301 282 302 283 root = &preftree->root; 303 - p = &root->rb_root.rb_node; 284 + exist = rb_find_add_cached(&newref->rbnode, root, prelim_ref_rb_add_cmp); 285 + if (exist) { 286 + struct prelim_ref *ref = rb_entry(exist, struct prelim_ref, rbnode); 287 + /* Identical refs, merge them and free @newref */ 288 + struct extent_inode_elem *eie = ref->inode_list; 304 289 305 - while (*p) { 306 - parent = *p; 307 - ref = rb_entry(parent, struct prelim_ref, rbnode); 308 - result = prelim_ref_compare(ref, newref); 309 - if (result < 0) { 310 - p = &(*p)->rb_left; 311 - } else if (result > 0) { 312 - p = &(*p)->rb_right; 313 - leftmost = false; 314 - } else { 315 - /* Identical refs, merge them and free @newref */ 316 - struct extent_inode_elem *eie = ref->inode_list; 290 + while (eie && eie->next) 291 + eie = eie->next; 317 292 318 - while (eie && eie->next) 319 - eie = eie->next; 320 - 321 - if (!eie) 322 - ref->inode_list = newref->inode_list; 323 - else 324 - eie->next = newref->inode_list; 325 - trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 326 - preftree->count); 327 - /* 328 - * A delayed ref can have newref->count < 0. 329 - * The ref->count is updated to follow any 330 - * BTRFS_[ADD|DROP]_DELAYED_REF actions. 331 - */ 332 - update_share_count(sc, ref->count, 333 - ref->count + newref->count, newref); 334 - ref->count += newref->count; 335 - free_pref(newref); 336 - return; 337 - } 293 + if (!eie) 294 + ref->inode_list = newref->inode_list; 295 + else 296 + eie->next = newref->inode_list; 297 + trace_btrfs_prelim_ref_merge(fs_info, ref, newref, 298 + preftree->count); 299 + /* 300 + * A delayed ref can have newref->count < 0. 301 + * The ref->count is updated to follow any 302 + * BTRFS_[ADD|DROP]_DELAYED_REF actions. 303 + */ 304 + update_share_count(sc, ref->count, 305 + ref->count + newref->count, newref); 306 + ref->count += newref->count; 307 + free_pref(newref); 308 + return; 338 309 } 339 310 340 311 update_share_count(sc, 0, newref->count, newref); 341 312 preftree->count++; 342 313 trace_btrfs_prelim_ref_insert(fs_info, newref, NULL, preftree->count); 343 - rb_link_node(&newref->rbnode, parent, p); 344 - rb_insert_color_cached(&newref->rbnode, root, leftmost); 345 314 } 346 315 347 316 /* ··· 3021 3022 cache->rb_root = RB_ROOT; 3022 3023 for (i = 0; i < BTRFS_MAX_LEVEL; i++) 3023 3024 INIT_LIST_HEAD(&cache->pending[i]); 3024 - INIT_LIST_HEAD(&cache->changed); 3025 - INIT_LIST_HEAD(&cache->detached); 3026 - INIT_LIST_HEAD(&cache->leaves); 3027 3025 INIT_LIST_HEAD(&cache->pending_edge); 3028 3026 INIT_LIST_HEAD(&cache->useless_node); 3029 3027 cache->fs_info = fs_info; ··· 3128 3132 void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 3129 3133 struct btrfs_backref_node *node) 3130 3134 { 3131 - struct btrfs_backref_node *upper; 3132 3135 struct btrfs_backref_edge *edge; 3133 3136 3134 3137 if (!node) 3135 3138 return; 3136 3139 3137 - BUG_ON(!node->lowest && !node->detached); 3138 3140 while (!list_empty(&node->upper)) { 3139 3141 edge = list_entry(node->upper.next, struct btrfs_backref_edge, 3140 3142 list[LOWER]); 3141 - upper = edge->node[UPPER]; 3142 3143 list_del(&edge->list[LOWER]); 3143 3144 list_del(&edge->list[UPPER]); 3144 3145 btrfs_backref_free_edge(cache, edge); 3145 - 3146 - /* 3147 - * Add the node to leaf node list if no other child block 3148 - * cached. 3149 - */ 3150 - if (list_empty(&upper->lower)) { 3151 - list_add_tail(&upper->lower, &cache->leaves); 3152 - upper->lowest = 1; 3153 - } 3154 3146 } 3155 3147 3156 3148 btrfs_backref_drop_node(cache, node); ··· 3150 3166 void btrfs_backref_release_cache(struct btrfs_backref_cache *cache) 3151 3167 { 3152 3168 struct btrfs_backref_node *node; 3153 - int i; 3154 3169 3155 - while (!list_empty(&cache->detached)) { 3156 - node = list_entry(cache->detached.next, 3157 - struct btrfs_backref_node, list); 3170 + while ((node = rb_entry_safe(rb_first(&cache->rb_root), 3171 + struct btrfs_backref_node, rb_node))) 3158 3172 btrfs_backref_cleanup_node(cache, node); 3159 - } 3160 3173 3161 - while (!list_empty(&cache->leaves)) { 3162 - node = list_entry(cache->leaves.next, 3163 - struct btrfs_backref_node, lower); 3164 - btrfs_backref_cleanup_node(cache, node); 3165 - } 3166 - 3167 - for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 3168 - while (!list_empty(&cache->pending[i])) { 3169 - node = list_first_entry(&cache->pending[i], 3170 - struct btrfs_backref_node, 3171 - list); 3172 - btrfs_backref_cleanup_node(cache, node); 3173 - } 3174 - } 3175 3174 ASSERT(list_empty(&cache->pending_edge)); 3176 3175 ASSERT(list_empty(&cache->useless_node)); 3177 - ASSERT(list_empty(&cache->changed)); 3178 - ASSERT(list_empty(&cache->detached)); 3179 - ASSERT(RB_EMPTY_ROOT(&cache->rb_root)); 3180 3176 ASSERT(!cache->nr_nodes); 3181 3177 ASSERT(!cache->nr_edges); 3182 3178 } ··· 3280 3316 root = btrfs_get_fs_root(fs_info, ref_key->offset, false); 3281 3317 if (IS_ERR(root)) 3282 3318 return PTR_ERR(root); 3283 - if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 3284 - cur->cowonly = 1; 3319 + 3320 + /* We shouldn't be using backref cache for non-shareable roots. */ 3321 + if (unlikely(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))) { 3322 + btrfs_put_root(root); 3323 + return -EUCLEAN; 3324 + } 3285 3325 3286 3326 if (btrfs_root_level(&root->root_item) == cur->level) { 3287 3327 /* Tree root */ ··· 3371 3403 goto out; 3372 3404 } 3373 3405 upper->owner = btrfs_header_owner(eb); 3374 - if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 3375 - upper->cowonly = 1; 3406 + 3407 + /* We shouldn't be using backref cache for non shareable roots. */ 3408 + if (unlikely(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))) { 3409 + btrfs_put_root(root); 3410 + btrfs_backref_free_edge(cache, edge); 3411 + btrfs_backref_free_node(cache, upper); 3412 + ret = -EUCLEAN; 3413 + goto out; 3414 + } 3376 3415 3377 3416 /* 3378 3417 * If we know the block isn't shared we can avoid ··· 3570 3595 3571 3596 ASSERT(start->checked); 3572 3597 3573 - /* Insert this node to cache if it's not COW-only */ 3574 - if (!start->cowonly) { 3575 - rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, 3576 - &start->rb_node); 3577 - if (rb_node) 3578 - btrfs_backref_panic(cache->fs_info, start->bytenr, 3579 - -EEXIST); 3580 - list_add_tail(&start->lower, &cache->leaves); 3581 - } 3598 + rb_node = rb_simple_insert(&cache->rb_root, start->bytenr, &start->rb_node); 3599 + if (rb_node) 3600 + btrfs_backref_panic(cache->fs_info, start->bytenr, -EEXIST); 3582 3601 3583 3602 /* 3584 3603 * Use breadth first search to iterate all related edges. ··· 3611 3642 * parents have already been linked. 3612 3643 */ 3613 3644 if (!RB_EMPTY_NODE(&upper->rb_node)) { 3614 - if (upper->lowest) { 3615 - list_del_init(&upper->lower); 3616 - upper->lowest = 0; 3617 - } 3618 - 3619 3645 list_add_tail(&edge->list[UPPER], &upper->lower); 3620 3646 continue; 3621 3647 } ··· 3621 3657 return -EUCLEAN; 3622 3658 } 3623 3659 3624 - /* Sanity check, COW-only node has non-COW-only parent */ 3625 - if (start->cowonly != upper->cowonly) { 3626 - ASSERT(0); 3660 + rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 3661 + &upper->rb_node); 3662 + if (unlikely(rb_node)) { 3663 + btrfs_backref_panic(cache->fs_info, upper->bytenr, -EEXIST); 3627 3664 return -EUCLEAN; 3628 - } 3629 - 3630 - /* Only cache non-COW-only (subvolume trees) tree blocks */ 3631 - if (!upper->cowonly) { 3632 - rb_node = rb_simple_insert(&cache->rb_root, upper->bytenr, 3633 - &upper->rb_node); 3634 - if (rb_node) { 3635 - btrfs_backref_panic(cache->fs_info, 3636 - upper->bytenr, -EEXIST); 3637 - return -EUCLEAN; 3638 - } 3639 3665 } 3640 3666 3641 3667 list_add_tail(&edge->list[UPPER], &upper->lower);
+6 -10
fs/btrfs/backref.h
··· 318 318 u64 bytenr; 319 319 }; /* Use rb_simple_node for search/insert */ 320 320 321 + /* 322 + * This is a sanity check, whenever we COW a block we will update 323 + * new_bytenr with it's current location, and we will check this in 324 + * various places to validate that the cache makes sense, it shouldn't 325 + * be used for anything else. 326 + */ 321 327 u64 new_bytenr; 322 328 /* Objectid of tree block owner, can be not uptodate */ 323 329 u64 owner; ··· 341 335 struct extent_buffer *eb; 342 336 /* Level of the tree block */ 343 337 unsigned int level:8; 344 - /* Is the block in a non-shareable tree */ 345 - unsigned int cowonly:1; 346 - /* 1 if no child node is in the cache */ 347 - unsigned int lowest:1; 348 338 /* Is the extent buffer locked */ 349 339 unsigned int locked:1; 350 340 /* Has the block been processed */ ··· 393 391 * level blocks may not reflect the new location 394 392 */ 395 393 struct list_head pending[BTRFS_MAX_LEVEL]; 396 - /* List of backref nodes with no child node */ 397 - struct list_head leaves; 398 - /* List of blocks that have been COWed in current transaction */ 399 - struct list_head changed; 400 - /* List of detached backref node. */ 401 - struct list_head detached; 402 394 403 395 u64 last_trans; 404 396
+9 -2
fs/btrfs/bio.c
··· 453 453 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev), 454 454 dev->devid, bio->bi_iter.bi_size); 455 455 456 + /* 457 + * Track reads if tracking is enabled; ignore I/O operations before the 458 + * filesystem is fully initialized. 459 + */ 460 + if (dev->fs_devices->collect_fs_stats && bio_op(bio) == REQ_OP_READ && dev->fs_info) 461 + percpu_counter_add(&dev->fs_info->stats_read_blocks, 462 + bio->bi_iter.bi_size >> dev->fs_info->sectorsize_bits); 463 + 456 464 if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT) 457 465 blkcg_punt_bio_submit(bio); 458 466 else ··· 733 725 bio->bi_opf |= REQ_OP_ZONE_APPEND; 734 726 } 735 727 736 - if (is_data_bbio(bbio) && bioc && 737 - btrfs_need_stripe_tree_update(bioc->fs_info, bioc->map_type)) { 728 + if (is_data_bbio(bbio) && bioc && bioc->use_rst) { 738 729 /* 739 730 * No locking for the list update, as we only add to 740 731 * the list in the I/O submission path, and list
+28 -36
fs/btrfs/block-group.c
··· 173 173 } 174 174 } 175 175 176 + static int btrfs_bg_start_cmp(const struct rb_node *new, 177 + const struct rb_node *exist) 178 + { 179 + const struct btrfs_block_group *new_bg = 180 + rb_entry(new, struct btrfs_block_group, cache_node); 181 + const struct btrfs_block_group *exist_bg = 182 + rb_entry(exist, struct btrfs_block_group, cache_node); 183 + 184 + if (new_bg->start < exist_bg->start) 185 + return -1; 186 + if (new_bg->start > exist_bg->start) 187 + return 1; 188 + return 0; 189 + } 190 + 176 191 /* 177 192 * This adds the block group to the fs_info rb tree for the block group cache 178 193 */ 179 194 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, 180 195 struct btrfs_block_group *block_group) 181 196 { 182 - struct rb_node **p; 183 - struct rb_node *parent = NULL; 184 - struct btrfs_block_group *cache; 185 - bool leftmost = true; 197 + struct rb_node *exist; 198 + int ret = 0; 186 199 187 200 ASSERT(block_group->length != 0); 188 201 189 202 write_lock(&info->block_group_cache_lock); 190 - p = &info->block_group_cache_tree.rb_root.rb_node; 191 203 192 - while (*p) { 193 - parent = *p; 194 - cache = rb_entry(parent, struct btrfs_block_group, cache_node); 195 - if (block_group->start < cache->start) { 196 - p = &(*p)->rb_left; 197 - } else if (block_group->start > cache->start) { 198 - p = &(*p)->rb_right; 199 - leftmost = false; 200 - } else { 201 - write_unlock(&info->block_group_cache_lock); 202 - return -EEXIST; 203 - } 204 - } 205 - 206 - rb_link_node(&block_group->cache_node, parent, p); 207 - rb_insert_color_cached(&block_group->cache_node, 208 - &info->block_group_cache_tree, leftmost); 209 - 204 + exist = rb_find_add_cached(&block_group->cache_node, 205 + &info->block_group_cache_tree, btrfs_bg_start_cmp); 206 + if (exist) 207 + ret = -EEXIST; 210 208 write_unlock(&info->block_group_cache_lock); 211 209 212 - return 0; 210 + return ret; 213 211 } 214 212 215 213 /* ··· 1221 1223 block_group->space_info->total_bytes -= block_group->length; 1222 1224 block_group->space_info->bytes_readonly -= 1223 1225 (block_group->length - block_group->zone_unusable); 1224 - btrfs_space_info_update_bytes_zone_unusable(fs_info, block_group->space_info, 1226 + btrfs_space_info_update_bytes_zone_unusable(block_group->space_info, 1225 1227 -block_group->zone_unusable); 1226 1228 block_group->space_info->disk_total -= block_group->length * factor; 1227 1229 ··· 1394 1396 if (btrfs_is_zoned(cache->fs_info)) { 1395 1397 /* Migrate zone_unusable bytes to readonly */ 1396 1398 sinfo->bytes_readonly += cache->zone_unusable; 1397 - btrfs_space_info_update_bytes_zone_unusable(cache->fs_info, sinfo, 1398 - -cache->zone_unusable); 1399 + btrfs_space_info_update_bytes_zone_unusable(sinfo, -cache->zone_unusable); 1399 1400 cache->zone_unusable = 0; 1400 1401 } 1401 1402 cache->ro++; ··· 1642 1645 spin_lock(&space_info->lock); 1643 1646 spin_lock(&block_group->lock); 1644 1647 1645 - btrfs_space_info_update_bytes_pinned(fs_info, space_info, 1646 - -block_group->pinned); 1648 + btrfs_space_info_update_bytes_pinned(space_info, -block_group->pinned); 1647 1649 space_info->bytes_readonly += block_group->pinned; 1648 1650 block_group->pinned = 0; 1649 1651 ··· 2668 2672 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); 2669 2673 2670 2674 btrfs_set_dev_extent_length(leaf, extent, num_bytes); 2671 - btrfs_mark_buffer_dirty(trans, leaf); 2672 2675 out: 2673 2676 btrfs_free_path(path); 2674 2677 return ret; ··· 3055 3060 (cache->alloc_offset - cache->used - cache->pinned - 3056 3061 cache->reserved) + 3057 3062 (cache->length - cache->zone_capacity); 3058 - btrfs_space_info_update_bytes_zone_unusable(cache->fs_info, sinfo, 3059 - cache->zone_unusable); 3063 + btrfs_space_info_update_bytes_zone_unusable(sinfo, cache->zone_unusable); 3060 3064 sinfo->bytes_readonly -= cache->zone_unusable; 3061 3065 } 3062 3066 num_bytes = cache->length - cache->reserved - ··· 3117 3123 cache->global_root_id); 3118 3124 btrfs_set_stack_block_group_flags(&bgi, cache->flags); 3119 3125 write_extent_buffer(leaf, &bgi, bi, sizeof(bgi)); 3120 - btrfs_mark_buffer_dirty(trans, leaf); 3121 3126 fail: 3122 3127 btrfs_release_path(path); 3123 3128 /* ··· 3692 3699 old_val -= num_bytes; 3693 3700 cache->used = old_val; 3694 3701 cache->pinned += num_bytes; 3695 - btrfs_space_info_update_bytes_pinned(info, space_info, num_bytes); 3702 + btrfs_space_info_update_bytes_pinned(space_info, num_bytes); 3696 3703 space_info->bytes_used -= num_bytes; 3697 3704 space_info->disk_used -= num_bytes * factor; 3698 3705 if (READ_ONCE(space_info->periodic_reclaim)) ··· 3774 3781 space_info->bytes_reserved += num_bytes; 3775 3782 trace_btrfs_space_reservation(cache->fs_info, "space_info", 3776 3783 space_info->flags, num_bytes, 1); 3777 - btrfs_space_info_update_bytes_may_use(cache->fs_info, 3778 - space_info, -ram_bytes); 3784 + btrfs_space_info_update_bytes_may_use(space_info, -ram_bytes); 3779 3785 if (delalloc) 3780 3786 cache->delalloc_bytes += num_bytes; 3781 3787
+3 -7
fs/btrfs/block-rsv.c
··· 150 150 spin_unlock(&dest->lock); 151 151 } 152 152 if (num_bytes) 153 - btrfs_space_info_free_bytes_may_use(fs_info, 154 - space_info, 155 - num_bytes); 153 + btrfs_space_info_free_bytes_may_use(space_info, num_bytes); 156 154 } 157 155 if (qgroup_to_release_ret) 158 156 *qgroup_to_release_ret = qgroup_to_release; ··· 381 383 382 384 if (block_rsv->reserved < block_rsv->size) { 383 385 num_bytes = block_rsv->size - block_rsv->reserved; 384 - btrfs_space_info_update_bytes_may_use(fs_info, sinfo, 385 - num_bytes); 386 + btrfs_space_info_update_bytes_may_use(sinfo, num_bytes); 386 387 block_rsv->reserved = block_rsv->size; 387 388 } else if (block_rsv->reserved > block_rsv->size) { 388 389 num_bytes = block_rsv->reserved - block_rsv->size; 389 - btrfs_space_info_update_bytes_may_use(fs_info, sinfo, 390 - -num_bytes); 390 + btrfs_space_info_update_bytes_may_use(sinfo, -num_bytes); 391 391 block_rsv->reserved = block_rsv->size; 392 392 btrfs_try_granting_tickets(fs_info, sinfo); 393 393 }
+1 -1
fs/btrfs/btrfs_inode.h
··· 526 526 u32 bio_offset, struct bio_vec *bv); 527 527 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, 528 528 struct btrfs_file_extent *file_extent, 529 - bool nowait, bool strict); 529 + bool nowait); 530 530 531 531 void btrfs_del_delalloc_inode(struct btrfs_inode *inode); 532 532 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
+1 -67
fs/btrfs/ctree.c
··· 37 37 static int balance_node_right(struct btrfs_trans_handle *trans, 38 38 struct extent_buffer *dst_buf, 39 39 struct extent_buffer *src_buf); 40 - 41 - static const struct btrfs_csums { 42 - u16 size; 43 - const char name[10]; 44 - const char driver[12]; 45 - } btrfs_csums[] = { 46 - [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" }, 47 - [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" }, 48 - [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" }, 49 - [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b", 50 - .driver = "blake2b-256" }, 51 - }; 52 - 53 40 /* 54 41 * The leaf data grows from end-to-front in the node. this returns the address 55 42 * of the start of the last item, which is the stop of the leaf data stack. ··· 135 148 nr_items * sizeof(struct btrfs_item)); 136 149 } 137 150 138 - /* This exists for btrfs-progs usages. */ 139 - u16 btrfs_csum_type_size(u16 type) 140 - { 141 - return btrfs_csums[type].size; 142 - } 143 - 144 - int btrfs_super_csum_size(const struct btrfs_super_block *s) 145 - { 146 - u16 t = btrfs_super_csum_type(s); 147 - /* 148 - * csum type is validated at mount time 149 - */ 150 - return btrfs_csum_type_size(t); 151 - } 152 - 153 - const char *btrfs_super_csum_name(u16 csum_type) 154 - { 155 - /* csum type is validated at mount time */ 156 - return btrfs_csums[csum_type].name; 157 - } 158 - 159 - /* 160 - * Return driver name if defined, otherwise the name that's also a valid driver 161 - * name 162 - */ 163 - const char *btrfs_super_csum_driver(u16 csum_type) 164 - { 165 - /* csum type is validated at mount time */ 166 - return btrfs_csums[csum_type].driver[0] ? 167 - btrfs_csums[csum_type].driver : 168 - btrfs_csums[csum_type].name; 169 - } 170 - 171 - size_t __attribute_const__ btrfs_get_num_csums(void) 172 - { 173 - return ARRAY_SIZE(btrfs_csums); 174 - } 175 - 176 151 struct btrfs_path *btrfs_alloc_path(void) 177 152 { 178 153 might_sleep(); ··· 172 223 free_extent_buffer(p->nodes[i]); 173 224 p->nodes[i] = NULL; 174 225 } 175 - } 176 - 177 - /* 178 - * We want the transaction abort to print stack trace only for errors where the 179 - * cause could be a bug, eg. due to ENOSPC, and not for common errors that are 180 - * caused by external factors. 181 - */ 182 - bool __cold abort_should_print_stack(int error) 183 - { 184 - switch (error) { 185 - case -EIO: 186 - case -EROFS: 187 - case -ENOMEM: 188 - return false; 189 - } 190 - return true; 191 226 } 192 227 193 228 /* ··· 3833 3900 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3834 3901 3835 3902 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 3903 + key.type != BTRFS_RAID_STRIPE_KEY && 3836 3904 key.type != BTRFS_EXTENT_CSUM_KEY); 3837 3905 3838 3906 if (btrfs_leaf_free_space(leaf) >= ins_len)
-29
fs/btrfs/ctree.h
··· 7 7 #define BTRFS_CTREE_H 8 8 9 9 #include "linux/cleanup.h" 10 - #include <linux/pagemap.h> 11 10 #include <linux/spinlock.h> 12 11 #include <linux/rbtree.h> 13 12 #include <linux/mutex.h> ··· 505 506 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item); 506 507 } 507 508 508 - #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \ 509 - ((bytes) >> (fs_info)->sectorsize_bits) 510 - 511 - static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping) 512 - { 513 - return mapping_gfp_constraint(mapping, ~__GFP_FS); 514 - } 515 - 516 - void btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, u64 start, u64 end); 517 - int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, 518 - u64 num_bytes, u64 *actual_bytes); 519 - int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range); 520 - 521 - /* ctree.c */ 522 509 int __init btrfs_ctree_init(void); 523 510 void __cold btrfs_ctree_exit(void); 524 511 ··· 740 755 { 741 756 return root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID; 742 757 } 743 - 744 - u16 btrfs_csum_type_size(u16 type); 745 - int btrfs_super_csum_size(const struct btrfs_super_block *s); 746 - const char *btrfs_super_csum_name(u16 csum_type); 747 - const char *btrfs_super_csum_driver(u16 csum_type); 748 - size_t __attribute_const__ btrfs_get_num_csums(void); 749 - 750 - /* 751 - * We use folio flag owner_2 to indicate there is an ordered extent with 752 - * unfinished IO. 753 - */ 754 - #define folio_test_ordered(folio) folio_test_owner_2(folio) 755 - #define folio_set_ordered(folio) folio_set_owner_2(folio) 756 - #define folio_clear_ordered(folio) folio_clear_owner_2(folio) 757 758 758 759 #endif
+1 -1
fs/btrfs/delalloc-space.c
··· 176 176 ASSERT(IS_ALIGNED(len, fs_info->sectorsize)); 177 177 178 178 data_sinfo = fs_info->data_sinfo; 179 - btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len); 179 + btrfs_space_info_free_bytes_may_use(data_sinfo, len); 180 180 } 181 181 182 182 /*
+21 -28
fs/btrfs/delayed-inode.c
··· 366 366 return NULL; 367 367 } 368 368 369 + static int btrfs_delayed_item_cmp(const struct rb_node *new, 370 + const struct rb_node *exist) 371 + { 372 + const struct btrfs_delayed_item *new_item = 373 + rb_entry(new, struct btrfs_delayed_item, rb_node); 374 + const struct btrfs_delayed_item *exist_item = 375 + rb_entry(exist, struct btrfs_delayed_item, rb_node); 376 + 377 + if (new_item->index < exist_item->index) 378 + return -1; 379 + if (new_item->index > exist_item->index) 380 + return 1; 381 + return 0; 382 + } 383 + 369 384 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node, 370 385 struct btrfs_delayed_item *ins) 371 386 { 372 - struct rb_node **p, *node; 373 - struct rb_node *parent_node = NULL; 374 387 struct rb_root_cached *root; 375 - struct btrfs_delayed_item *item; 376 - bool leftmost = true; 388 + struct rb_node *exist; 377 389 378 390 if (ins->type == BTRFS_DELAYED_INSERTION_ITEM) 379 391 root = &delayed_node->ins_root; 380 392 else 381 393 root = &delayed_node->del_root; 382 394 383 - p = &root->rb_root.rb_node; 384 - node = &ins->rb_node; 385 - 386 - while (*p) { 387 - parent_node = *p; 388 - item = rb_entry(parent_node, struct btrfs_delayed_item, 389 - rb_node); 390 - 391 - if (item->index < ins->index) { 392 - p = &(*p)->rb_right; 393 - leftmost = false; 394 - } else if (item->index > ins->index) { 395 - p = &(*p)->rb_left; 396 - } else { 397 - return -EEXIST; 398 - } 399 - } 400 - 401 - rb_link_node(node, parent_node, p); 402 - rb_insert_color_cached(node, root, leftmost); 395 + exist = rb_find_add_cached(&ins->rb_node, root, btrfs_delayed_item_cmp); 396 + if (exist) 397 + return -EEXIST; 403 398 404 399 if (ins->type == BTRFS_DELAYED_INSERTION_ITEM && 405 400 ins->index >= delayed_node->index_cnt) ··· 1033 1038 struct btrfs_inode_item); 1034 1039 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item, 1035 1040 sizeof(struct btrfs_inode_item)); 1036 - btrfs_mark_buffer_dirty(trans, leaf); 1037 1041 1038 1042 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) 1039 1043 goto out; ··· 1555 1561 return ret; 1556 1562 } 1557 1563 1558 - static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info, 1559 - struct btrfs_delayed_node *node, 1564 + static int btrfs_delete_delayed_insertion_item(struct btrfs_delayed_node *node, 1560 1565 u64 index) 1561 1566 { 1562 1567 struct btrfs_delayed_item *item; ··· 1613 1620 if (IS_ERR(node)) 1614 1621 return PTR_ERR(node); 1615 1622 1616 - ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node, index); 1623 + ret = btrfs_delete_delayed_insertion_item(node, index); 1617 1624 if (!ret) 1618 1625 goto end; 1619 1626
+56 -33
fs/btrfs/delayed-ref.c
··· 93 93 u64 num_bytes; 94 94 u64 reserved_bytes; 95 95 96 + if (btrfs_is_testing(fs_info)) 97 + return; 98 + 96 99 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, trans->delayed_ref_updates); 97 100 num_bytes += btrfs_calc_delayed_ref_csum_bytes(fs_info, 98 101 trans->delayed_ref_csum_deletions); ··· 257 254 spin_unlock(&block_rsv->lock); 258 255 259 256 if (to_free > 0) 260 - btrfs_space_info_free_bytes_may_use(fs_info, space_info, to_free); 257 + btrfs_space_info_free_bytes_may_use(space_info, to_free); 261 258 262 259 if (refilled_bytes > 0) 263 260 trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", 0, ··· 268 265 /* 269 266 * compare two delayed data backrefs with same bytenr and type 270 267 */ 271 - static int comp_data_refs(struct btrfs_delayed_ref_node *ref1, 272 - struct btrfs_delayed_ref_node *ref2) 268 + static int comp_data_refs(const struct btrfs_delayed_ref_node *ref1, 269 + const struct btrfs_delayed_ref_node *ref2) 273 270 { 274 271 if (ref1->data_ref.objectid < ref2->data_ref.objectid) 275 272 return -1; ··· 282 279 return 0; 283 280 } 284 281 285 - static int comp_refs(struct btrfs_delayed_ref_node *ref1, 286 - struct btrfs_delayed_ref_node *ref2, 282 + static int comp_refs(const struct btrfs_delayed_ref_node *ref1, 283 + const struct btrfs_delayed_ref_node *ref2, 287 284 bool check_seq) 288 285 { 289 286 int ret = 0; ··· 317 314 return 0; 318 315 } 319 316 317 + static int cmp_refs_node(const struct rb_node *new, const struct rb_node *exist) 318 + { 319 + const struct btrfs_delayed_ref_node *new_node = 320 + rb_entry(new, struct btrfs_delayed_ref_node, ref_node); 321 + const struct btrfs_delayed_ref_node *exist_node = 322 + rb_entry(exist, struct btrfs_delayed_ref_node, ref_node); 323 + 324 + return comp_refs(new_node, exist_node, true); 325 + } 326 + 320 327 static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root, 321 328 struct btrfs_delayed_ref_node *ins) 322 329 { 323 - struct rb_node **p = &root->rb_root.rb_node; 324 330 struct rb_node *node = &ins->ref_node; 325 - struct rb_node *parent_node = NULL; 326 - struct btrfs_delayed_ref_node *entry; 327 - bool leftmost = true; 331 + struct rb_node *exist; 328 332 329 - while (*p) { 330 - int comp; 331 - 332 - parent_node = *p; 333 - entry = rb_entry(parent_node, struct btrfs_delayed_ref_node, 334 - ref_node); 335 - comp = comp_refs(ins, entry, true); 336 - if (comp < 0) { 337 - p = &(*p)->rb_left; 338 - } else if (comp > 0) { 339 - p = &(*p)->rb_right; 340 - leftmost = false; 341 - } else { 342 - return entry; 343 - } 344 - } 345 - 346 - rb_link_node(node, parent_node, p); 347 - rb_insert_color_cached(node, root, leftmost); 333 + exist = rb_find_add_cached(node, root, cmp_refs_node); 334 + if (exist) 335 + return rb_entry(exist, struct btrfs_delayed_ref_node, ref_node); 348 336 return NULL; 349 337 } 350 338 ··· 547 553 delayed_refs->num_heads--; 548 554 if (!head->processing) 549 555 delayed_refs->num_heads_ready--; 556 + } 557 + 558 + struct btrfs_delayed_ref_node *btrfs_select_delayed_ref(struct btrfs_delayed_ref_head *head) 559 + { 560 + struct btrfs_delayed_ref_node *ref; 561 + 562 + lockdep_assert_held(&head->mutex); 563 + lockdep_assert_held(&head->lock); 564 + 565 + if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) 566 + return NULL; 567 + 568 + /* 569 + * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. 570 + * This is to prevent a ref count from going down to zero, which deletes 571 + * the extent item from the extent tree, when there still are references 572 + * to add, which would fail because they would not find the extent item. 573 + */ 574 + if (!list_empty(&head->ref_add_list)) 575 + return list_first_entry(&head->ref_add_list, 576 + struct btrfs_delayed_ref_node, add_list); 577 + 578 + ref = rb_entry(rb_first_cached(&head->ref_tree), 579 + struct btrfs_delayed_ref_node, ref_node); 580 + ASSERT(list_empty(&ref->add_list)); 581 + return ref; 550 582 } 551 583 552 584 /* ··· 1254 1234 { 1255 1235 struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs; 1256 1236 struct btrfs_fs_info *fs_info = trans->fs_info; 1237 + bool testing = btrfs_is_testing(fs_info); 1257 1238 1258 1239 spin_lock(&delayed_refs->lock); 1259 1240 while (true) { ··· 1284 1263 spin_unlock(&delayed_refs->lock); 1285 1264 mutex_unlock(&head->mutex); 1286 1265 1287 - if (pin_bytes) { 1266 + if (!testing && pin_bytes) { 1288 1267 struct btrfs_block_group *bg; 1289 1268 1290 1269 bg = btrfs_lookup_block_group(fs_info, head->bytenr); ··· 1302 1281 spin_lock(&bg->space_info->lock); 1303 1282 spin_lock(&bg->lock); 1304 1283 bg->pinned += head->num_bytes; 1305 - btrfs_space_info_update_bytes_pinned(fs_info, 1306 - bg->space_info, 1284 + btrfs_space_info_update_bytes_pinned(bg->space_info, 1307 1285 head->num_bytes); 1308 1286 bg->reserved -= head->num_bytes; 1309 1287 bg->space_info->bytes_reserved -= head->num_bytes; ··· 1315 1295 btrfs_error_unpin_extent_range(fs_info, head->bytenr, 1316 1296 head->bytenr + head->num_bytes - 1); 1317 1297 } 1318 - btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 1298 + if (!testing) 1299 + btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); 1319 1300 btrfs_put_delayed_ref_head(head); 1320 1301 cond_resched(); 1321 1302 spin_lock(&delayed_refs->lock); 1322 1303 } 1323 - btrfs_qgroup_destroy_extent_records(trans); 1304 + 1305 + if (!testing) 1306 + btrfs_qgroup_destroy_extent_records(trans); 1324 1307 1325 1308 spin_unlock(&delayed_refs->lock); 1326 1309 }
+1
fs/btrfs/delayed-ref.h
··· 402 402 struct btrfs_delayed_ref_root *delayed_refs); 403 403 void btrfs_unselect_ref_head(struct btrfs_delayed_ref_root *delayed_refs, 404 404 struct btrfs_delayed_ref_head *head); 405 + struct btrfs_delayed_ref_node *btrfs_select_delayed_ref(struct btrfs_delayed_ref_head *head); 405 406 406 407 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq); 407 408
-3
fs/btrfs/dev-replace.c
··· 440 440 dev_replace->cursor_right); 441 441 dev_replace->item_needs_writeback = 0; 442 442 up_write(&dev_replace->rwsem); 443 - 444 - btrfs_mark_buffer_dirty(trans, eb); 445 - 446 443 out: 447 444 btrfs_free_path(path); 448 445
-2
fs/btrfs/dir-item.c
··· 92 92 93 93 write_extent_buffer(leaf, name, name_ptr, name_len); 94 94 write_extent_buffer(leaf, data, data_ptr, data_len); 95 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 96 95 97 96 return ret; 98 97 } ··· 151 152 name_ptr = (unsigned long)(dir_item + 1); 152 153 153 154 write_extent_buffer(leaf, name->name, name_ptr, name->len); 154 - btrfs_mark_buffer_dirty(trans, leaf); 155 155 156 156 second_insert: 157 157 /* FIXME, use some real flag for selecting the extra index */
+1 -2
fs/btrfs/direct-io.c
··· 248 248 len = min(len, em->len - (start - em->start)); 249 249 block_start = extent_map_block_start(em) + (start - em->start); 250 250 251 - if (can_nocow_extent(inode, start, &len, 252 - &file_extent, false, false) == 1) { 251 + if (can_nocow_extent(inode, start, &len, &file_extent, false) == 1) { 253 252 bg = btrfs_inc_nocow_writers(fs_info, block_start); 254 253 if (bg) 255 254 can_nocow = true;
+74 -1
fs/btrfs/disk-io.c
··· 226 226 227 227 while (1) { 228 228 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 229 - ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check); 229 + ret = read_extent_buffer_pages(eb, mirror_num, check); 230 230 if (!ret) 231 231 break; 232 232 ··· 1258 1258 { 1259 1259 struct percpu_counter *em_counter = &fs_info->evictable_extent_maps; 1260 1260 1261 + percpu_counter_destroy(&fs_info->stats_read_blocks); 1261 1262 percpu_counter_destroy(&fs_info->dirty_metadata_bytes); 1262 1263 percpu_counter_destroy(&fs_info->delalloc_bytes); 1263 1264 percpu_counter_destroy(&fs_info->ordered_bytes); ··· 2328 2327 return ret; 2329 2328 } 2330 2329 2330 + static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info, 2331 + const struct btrfs_super_block *sb) 2332 + { 2333 + unsigned int cur = 0; /* Offset inside the sys chunk array */ 2334 + /* 2335 + * At sb read time, fs_info is not fully initialized. Thus we have 2336 + * to use super block sectorsize, which should have been validated. 2337 + */ 2338 + const u32 sectorsize = btrfs_super_sectorsize(sb); 2339 + u32 sys_array_size = btrfs_super_sys_array_size(sb); 2340 + 2341 + if (sys_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 2342 + btrfs_err(fs_info, "system chunk array too big %u > %u", 2343 + sys_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE); 2344 + return -EUCLEAN; 2345 + } 2346 + 2347 + while (cur < sys_array_size) { 2348 + struct btrfs_disk_key *disk_key; 2349 + struct btrfs_chunk *chunk; 2350 + struct btrfs_key key; 2351 + u64 type; 2352 + u16 num_stripes; 2353 + u32 len; 2354 + int ret; 2355 + 2356 + disk_key = (struct btrfs_disk_key *)(sb->sys_chunk_array + cur); 2357 + len = sizeof(*disk_key); 2358 + 2359 + if (cur + len > sys_array_size) 2360 + goto short_read; 2361 + cur += len; 2362 + 2363 + btrfs_disk_key_to_cpu(&key, disk_key); 2364 + if (key.type != BTRFS_CHUNK_ITEM_KEY) { 2365 + btrfs_err(fs_info, 2366 + "unexpected item type %u in sys_array at offset %u", 2367 + key.type, cur); 2368 + return -EUCLEAN; 2369 + } 2370 + chunk = (struct btrfs_chunk *)(sb->sys_chunk_array + cur); 2371 + num_stripes = btrfs_stack_chunk_num_stripes(chunk); 2372 + if (cur + btrfs_chunk_item_size(num_stripes) > sys_array_size) 2373 + goto short_read; 2374 + type = btrfs_stack_chunk_type(chunk); 2375 + if (!(type & BTRFS_BLOCK_GROUP_SYSTEM)) { 2376 + btrfs_err(fs_info, 2377 + "invalid chunk type %llu in sys_array at offset %u", 2378 + type, cur); 2379 + return -EUCLEAN; 2380 + } 2381 + ret = btrfs_check_chunk_valid(fs_info, NULL, chunk, key.offset, 2382 + sectorsize); 2383 + if (ret < 0) 2384 + return ret; 2385 + cur += btrfs_chunk_item_size(num_stripes); 2386 + } 2387 + return 0; 2388 + short_read: 2389 + btrfs_err(fs_info, 2390 + "super block sys chunk array short read, cur=%u sys_array_size=%u", 2391 + cur, sys_array_size); 2392 + return -EUCLEAN; 2393 + } 2394 + 2331 2395 /* 2332 2396 * Real super block validation 2333 2397 * NOTE: super csum type and incompat features will not be checked here. ··· 2560 2494 btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET); 2561 2495 ret = -EINVAL; 2562 2496 } 2497 + 2498 + ret = validate_sys_chunk_array(fs_info, sb); 2563 2499 2564 2500 /* 2565 2501 * Obvious sys_chunk_array corruptions, it must hold at least one key ··· 2921 2853 return ret; 2922 2854 2923 2855 ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL); 2856 + if (ret) 2857 + return ret; 2858 + 2859 + ret = percpu_counter_init(&fs_info->stats_read_blocks, 0, GFP_KERNEL); 2924 2860 if (ret) 2925 2861 return ret; 2926 2862 ··· 3393 3321 fs_info->sectors_per_page = (PAGE_SIZE >> fs_info->sectorsize_bits); 3394 3322 fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size; 3395 3323 fs_info->stripesize = stripesize; 3324 + fs_info->fs_devices->fs_info = fs_info; 3396 3325 3397 3326 /* 3398 3327 * Handle the space caching options appropriately now that we have the
-3
fs/btrfs/disk-io.h
··· 96 96 /* 97 97 * This function is used to grab the root, and avoid it is freed when we 98 98 * access it. But it doesn't ensure that the tree is not dropped. 99 - * 100 - * If you want to ensure the whole tree is safe, you should use 101 - * fs_info->subvol_srcu 102 99 */ 103 100 static inline struct btrfs_root *btrfs_grab_root(struct btrfs_root *root) 104 101 {
+100 -102
fs/btrfs/extent-tree.c
··· 570 570 btrfs_set_extent_data_ref_count(leaf, ref, num_refs); 571 571 } 572 572 } 573 - btrfs_mark_buffer_dirty(trans, leaf); 574 573 ret = 0; 575 574 fail: 576 575 btrfs_release_path(path); ··· 617 618 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); 618 619 else if (key.type == BTRFS_SHARED_DATA_REF_KEY) 619 620 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); 620 - btrfs_mark_buffer_dirty(trans, leaf); 621 621 } 622 622 return ret; 623 623 } ··· 1048 1050 } else { 1049 1051 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); 1050 1052 } 1051 - btrfs_mark_buffer_dirty(trans, leaf); 1052 1053 } 1053 1054 1054 1055 static int lookup_extent_backref(struct btrfs_trans_handle *trans, ··· 1192 1195 item_size -= size; 1193 1196 btrfs_truncate_item(trans, path, item_size, 1); 1194 1197 } 1195 - btrfs_mark_buffer_dirty(trans, leaf); 1196 1198 return 0; 1197 1199 } 1198 1200 ··· 1256 1260 { 1257 1261 int j, ret = 0; 1258 1262 u64 bytes_left, end; 1259 - u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT); 1263 + u64 aligned_start = ALIGN(start, SECTOR_SIZE); 1260 1264 1261 1265 /* Adjust the range to be aligned to 512B sectors if necessary. */ 1262 1266 if (start != aligned_start) { 1263 1267 len -= aligned_start - start; 1264 - len = round_down(len, 1 << SECTOR_SHIFT); 1268 + len = round_down(len, SECTOR_SIZE); 1265 1269 start = aligned_start; 1266 1270 } 1267 1271 ··· 1523 1527 if (extent_op) 1524 1528 __run_delayed_extent_op(extent_op, leaf, item); 1525 1529 1526 - btrfs_mark_buffer_dirty(trans, leaf); 1527 1530 btrfs_release_path(path); 1528 1531 1529 1532 /* now insert the actual backref */ ··· 1706 1711 1707 1712 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1708 1713 __run_delayed_extent_op(extent_op, leaf, ei); 1709 - 1710 - btrfs_mark_buffer_dirty(trans, leaf); 1711 1714 out: 1712 1715 btrfs_free_path(path); 1713 1716 return ret; ··· 1794 1801 node->bytenr, node->num_bytes, node->type, 1795 1802 node->action, node->ref_mod, ret); 1796 1803 return ret; 1797 - } 1798 - 1799 - static inline struct btrfs_delayed_ref_node * 1800 - select_delayed_ref(struct btrfs_delayed_ref_head *head) 1801 - { 1802 - struct btrfs_delayed_ref_node *ref; 1803 - 1804 - if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) 1805 - return NULL; 1806 - 1807 - /* 1808 - * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. 1809 - * This is to prevent a ref count from going down to zero, which deletes 1810 - * the extent item from the extent tree, when there still are references 1811 - * to add, which would fail because they would not find the extent item. 1812 - */ 1813 - if (!list_empty(&head->ref_add_list)) 1814 - return list_first_entry(&head->ref_add_list, 1815 - struct btrfs_delayed_ref_node, add_list); 1816 - 1817 - ref = rb_entry(rb_first_cached(&head->ref_tree), 1818 - struct btrfs_delayed_ref_node, ref_node); 1819 - ASSERT(list_empty(&ref->add_list)); 1820 - return ref; 1821 1804 } 1822 1805 1823 1806 static struct btrfs_delayed_extent_op *cleanup_extent_op( ··· 1928 1959 lockdep_assert_held(&locked_ref->mutex); 1929 1960 lockdep_assert_held(&locked_ref->lock); 1930 1961 1931 - while ((ref = select_delayed_ref(locked_ref))) { 1962 + while ((ref = btrfs_select_delayed_ref(locked_ref))) { 1932 1963 if (ref->seq && 1933 1964 btrfs_check_delayed_seq(fs_info, ref->seq)) { 1934 1965 spin_unlock(&locked_ref->lock); ··· 2199 2230 return ret; 2200 2231 } 2201 2232 2202 - static noinline int check_delayed_ref(struct btrfs_root *root, 2233 + static noinline int check_delayed_ref(struct btrfs_inode *inode, 2203 2234 struct btrfs_path *path, 2204 - u64 objectid, u64 offset, u64 bytenr) 2235 + u64 offset, u64 bytenr) 2205 2236 { 2237 + struct btrfs_root *root = inode->root; 2206 2238 struct btrfs_delayed_ref_head *head; 2207 2239 struct btrfs_delayed_ref_node *ref; 2208 2240 struct btrfs_delayed_ref_root *delayed_refs; ··· 2277 2307 * then we have a cross reference. 2278 2308 */ 2279 2309 if (ref->ref_root != btrfs_root_id(root) || 2280 - ref_owner != objectid || ref_offset != offset) { 2310 + ref_owner != btrfs_ino(inode) || ref_offset != offset) { 2281 2311 ret = 1; 2282 2312 break; 2283 2313 } ··· 2288 2318 return ret; 2289 2319 } 2290 2320 2291 - static noinline int check_committed_ref(struct btrfs_root *root, 2321 + /* 2322 + * Check if there are references for a data extent other than the one belonging 2323 + * to the given inode and offset. 2324 + * 2325 + * @inode: The only inode we expect to find associated with the data extent. 2326 + * @path: A path to use for searching the extent tree. 2327 + * @offset: The only offset we expect to find associated with the data extent. 2328 + * @bytenr: The logical address of the data extent. 2329 + * 2330 + * When the extent does not have any other references other than the one we 2331 + * expect to find, we always return a value of 0 with the path having a locked 2332 + * leaf that contains the extent's extent item - this is necessary to ensure 2333 + * we don't race with a task running delayed references, and our caller must 2334 + * have such a path when calling check_delayed_ref() - it must lock a delayed 2335 + * ref head while holding the leaf locked. In case the extent item is not found 2336 + * in the extent tree, we return -ENOENT with the path having the leaf (locked) 2337 + * where the extent item should be, in order to prevent races with another task 2338 + * running delayed references, so that we don't miss any reference when calling 2339 + * check_delayed_ref(). 2340 + * 2341 + * Note: this may return false positives, and this is because we want to be 2342 + * quick here as we're called in write paths (when flushing delalloc and 2343 + * in the direct IO write path). For example we can have an extent with 2344 + * a single reference but that reference is not inlined, or we may have 2345 + * many references in the extent tree but we also have delayed references 2346 + * that cancel all the reference except the one for our inode and offset, 2347 + * but it would be expensive to do such checks and complex due to all 2348 + * locking to avoid races between the checks and flushing delayed refs, 2349 + * plus non-inline references may be located on leaves other than the one 2350 + * that contains the extent item in the extent tree. The important thing 2351 + * here is to not return false negatives and that the false positives are 2352 + * not very common. 2353 + * 2354 + * Returns: 0 if there are no cross references and with the path having a locked 2355 + * leaf from the extent tree that contains the extent's extent item. 2356 + * 2357 + * 1 if there are cross references (false positives can happen). 2358 + * 2359 + * < 0 in case of an error. In case of -ENOENT the leaf in the extent 2360 + * tree where the extent item should be located at is read locked and 2361 + * accessible in the given path. 2362 + */ 2363 + static noinline int check_committed_ref(struct btrfs_inode *inode, 2292 2364 struct btrfs_path *path, 2293 - u64 objectid, u64 offset, u64 bytenr, 2294 - bool strict) 2365 + u64 offset, u64 bytenr) 2295 2366 { 2367 + struct btrfs_root *root = inode->root; 2296 2368 struct btrfs_fs_info *fs_info = root->fs_info; 2297 2369 struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); 2298 2370 struct extent_buffer *leaf; ··· 2353 2341 2354 2342 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2355 2343 if (ret < 0) 2356 - goto out; 2344 + return ret; 2357 2345 if (ret == 0) { 2358 2346 /* 2359 2347 * Key with offset -1 found, there would have to exist an extent 2360 2348 * item with such offset, but this is out of the valid range. 2361 2349 */ 2362 - ret = -EUCLEAN; 2363 - goto out; 2350 + return -EUCLEAN; 2364 2351 } 2365 2352 2366 - ret = -ENOENT; 2367 2353 if (path->slots[0] == 0) 2368 - goto out; 2354 + return -ENOENT; 2369 2355 2370 2356 path->slots[0]--; 2371 2357 leaf = path->nodes[0]; 2372 2358 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 2373 2359 2374 2360 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) 2375 - goto out; 2361 + return -ENOENT; 2376 2362 2377 - ret = 1; 2378 2363 item_size = btrfs_item_size(leaf, path->slots[0]); 2379 2364 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 2380 2365 expected_size = sizeof(*ei) + btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY); 2381 2366 2382 2367 /* No inline refs; we need to bail before checking for owner ref. */ 2383 2368 if (item_size == sizeof(*ei)) 2384 - goto out; 2369 + return 1; 2385 2370 2386 2371 /* Check for an owner ref; skip over it to the real inline refs. */ 2387 2372 iref = (struct btrfs_extent_inline_ref *)(ei + 1); ··· 2386 2377 if (btrfs_fs_incompat(fs_info, SIMPLE_QUOTA) && type == BTRFS_EXTENT_OWNER_REF_KEY) { 2387 2378 expected_size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY); 2388 2379 iref = (struct btrfs_extent_inline_ref *)(iref + 1); 2380 + type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); 2389 2381 } 2390 2382 2391 2383 /* If extent item has more than 1 inline ref then it's shared */ 2392 2384 if (item_size != expected_size) 2393 - goto out; 2394 - 2395 - /* 2396 - * If extent created before last snapshot => it's shared unless the 2397 - * snapshot has been deleted. Use the heuristic if strict is false. 2398 - */ 2399 - if (!strict && 2400 - (btrfs_extent_generation(leaf, ei) <= 2401 - btrfs_root_last_snapshot(&root->root_item))) 2402 - goto out; 2385 + return 1; 2403 2386 2404 2387 /* If this extent has SHARED_DATA_REF then it's shared */ 2405 - type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); 2406 2388 if (type != BTRFS_EXTENT_DATA_REF_KEY) 2407 - goto out; 2389 + return 1; 2408 2390 2409 2391 ref = (struct btrfs_extent_data_ref *)(&iref->offset); 2410 2392 if (btrfs_extent_refs(leaf, ei) != 2411 2393 btrfs_extent_data_ref_count(leaf, ref) || 2412 2394 btrfs_extent_data_ref_root(leaf, ref) != btrfs_root_id(root) || 2413 - btrfs_extent_data_ref_objectid(leaf, ref) != objectid || 2395 + btrfs_extent_data_ref_objectid(leaf, ref) != btrfs_ino(inode) || 2414 2396 btrfs_extent_data_ref_offset(leaf, ref) != offset) 2415 - goto out; 2397 + return 1; 2416 2398 2417 - ret = 0; 2418 - out: 2419 - return ret; 2399 + return 0; 2420 2400 } 2421 2401 2422 - int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, 2423 - u64 bytenr, bool strict, struct btrfs_path *path) 2402 + int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset, 2403 + u64 bytenr, struct btrfs_path *path) 2424 2404 { 2425 2405 int ret; 2426 2406 2427 2407 do { 2428 - ret = check_committed_ref(root, path, objectid, 2429 - offset, bytenr, strict); 2408 + ret = check_committed_ref(inode, path, offset, bytenr); 2430 2409 if (ret && ret != -ENOENT) 2431 2410 goto out; 2432 2411 2433 - ret = check_delayed_ref(root, path, objectid, offset, bytenr); 2412 + /* 2413 + * The path must have a locked leaf from the extent tree where 2414 + * the extent item for our extent is located, in case it exists, 2415 + * or where it should be located in case it doesn't exist yet 2416 + * because it's new and its delayed ref was not yet flushed. 2417 + * We need to lock the delayed ref head at check_delayed_ref(), 2418 + * if one exists, while holding the leaf locked in order to not 2419 + * race with delayed ref flushing, missing references and 2420 + * incorrectly reporting that the extent is not shared. 2421 + */ 2422 + if (IS_ENABLED(CONFIG_BTRFS_ASSERT)) { 2423 + struct extent_buffer *leaf = path->nodes[0]; 2424 + 2425 + ASSERT(leaf != NULL); 2426 + btrfs_assert_tree_read_locked(leaf); 2427 + 2428 + if (ret != -ENOENT) { 2429 + struct btrfs_key key; 2430 + 2431 + btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 2432 + ASSERT(key.objectid == bytenr); 2433 + ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY); 2434 + } 2435 + } 2436 + 2437 + ret = check_delayed_ref(inode, path, offset, bytenr); 2434 2438 } while (ret == -EAGAIN && !path->nowait); 2435 2439 2436 2440 out: 2437 2441 btrfs_release_path(path); 2438 - if (btrfs_is_data_reloc_root(root)) 2442 + if (btrfs_is_data_reloc_root(inode->root)) 2439 2443 WARN_ON(ret > 0); 2440 2444 return ret; 2441 2445 } ··· 2593 2571 struct btrfs_block_group *cache, 2594 2572 u64 bytenr, u64 num_bytes, int reserved) 2595 2573 { 2596 - struct btrfs_fs_info *fs_info = cache->fs_info; 2597 - 2598 2574 spin_lock(&cache->space_info->lock); 2599 2575 spin_lock(&cache->lock); 2600 2576 cache->pinned += num_bytes; 2601 - btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, 2602 - num_bytes); 2577 + btrfs_space_info_update_bytes_pinned(cache->space_info, num_bytes); 2603 2578 if (reserved) { 2604 2579 cache->reserved -= num_bytes; 2605 2580 cache->space_info->bytes_reserved -= num_bytes; ··· 2743 2724 { 2744 2725 struct btrfs_block_group *cache = NULL; 2745 2726 struct btrfs_space_info *space_info; 2746 - struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; 2747 2727 struct btrfs_free_cluster *cluster = NULL; 2748 - u64 len; 2749 2728 u64 total_unpinned = 0; 2750 2729 u64 empty_cluster = 0; 2751 2730 bool readonly; 2752 2731 int ret = 0; 2753 2732 2754 2733 while (start <= end) { 2734 + u64 len; 2735 + 2755 2736 readonly = false; 2756 2737 if (!cache || 2757 2738 start >= cache->start + cache->length) { ··· 2797 2778 spin_lock(&space_info->lock); 2798 2779 spin_lock(&cache->lock); 2799 2780 cache->pinned -= len; 2800 - btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); 2781 + btrfs_space_info_update_bytes_pinned(space_info, -len); 2801 2782 space_info->max_extent_size = 0; 2802 2783 if (cache->ro) { 2803 2784 space_info->bytes_readonly += len; 2804 2785 readonly = true; 2805 2786 } else if (btrfs_is_zoned(fs_info)) { 2806 2787 /* Need reset before reusing in a zoned block group */ 2807 - btrfs_space_info_update_bytes_zone_unusable(fs_info, space_info, 2808 - len); 2788 + btrfs_space_info_update_bytes_zone_unusable(space_info, len); 2809 2789 readonly = true; 2810 2790 } 2811 2791 spin_unlock(&cache->lock); 2812 - if (!readonly && return_free_space && 2813 - global_rsv->space_info == space_info) { 2814 - spin_lock(&global_rsv->lock); 2815 - if (!global_rsv->full) { 2816 - u64 to_add = min(len, global_rsv->size - 2817 - global_rsv->reserved); 2818 - 2819 - global_rsv->reserved += to_add; 2820 - btrfs_space_info_update_bytes_may_use(fs_info, 2821 - space_info, to_add); 2822 - if (global_rsv->reserved >= global_rsv->size) 2823 - global_rsv->full = 1; 2824 - len -= to_add; 2825 - } 2826 - spin_unlock(&global_rsv->lock); 2827 - } 2828 - /* Add to any tickets we may have */ 2829 - if (!readonly && return_free_space && len) 2830 - btrfs_try_granting_tickets(fs_info, space_info); 2792 + if (!readonly && return_free_space) 2793 + btrfs_return_free_space(space_info, len); 2831 2794 spin_unlock(&space_info->lock); 2832 2795 } 2833 2796 ··· 3260 3259 } 3261 3260 } else { 3262 3261 btrfs_set_extent_refs(leaf, ei, refs); 3263 - btrfs_mark_buffer_dirty(trans, leaf); 3264 3262 } 3265 3263 if (found_extent) { 3266 3264 ret = remove_extent_backref(trans, extent_root, path, ··· 4827 4827 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); 4828 4828 } 4829 4829 4830 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 4831 4830 btrfs_free_path(path); 4832 4831 4833 4832 return alloc_reserved_extent(trans, ins->objectid, ins->offset); ··· 4901 4902 btrfs_set_extent_inline_ref_offset(leaf, iref, node->ref_root); 4902 4903 } 4903 4904 4904 - btrfs_mark_buffer_dirty(trans, leaf); 4905 4905 btrfs_free_path(path); 4906 4906 4907 4907 return alloc_reserved_extent(trans, node->bytenr, fs_info->nodesize);
+5 -2
fs/btrfs/extent-tree.h
··· 116 116 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, 117 117 const struct extent_buffer *eb); 118 118 int btrfs_exclude_logged_extents(struct extent_buffer *eb); 119 - int btrfs_cross_ref_exist(struct btrfs_root *root, 120 - u64 objectid, u64 offset, u64 bytenr, bool strict, 119 + int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset, u64 bytenr, 121 120 struct btrfs_path *path); 122 121 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, 123 122 struct btrfs_root *root, ··· 162 163 struct btrfs_root *root, 163 164 struct extent_buffer *node, 164 165 struct extent_buffer *parent); 166 + void btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, u64 start, u64 end); 167 + int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, 168 + u64 num_bytes, u64 *actual_bytes); 169 + int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range); 165 170 166 171 #endif
+150 -100
fs/btrfs/extent_io.c
··· 198 198 u64 end, unsigned long page_ops) 199 199 { 200 200 struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host); 201 - pgoff_t start_index = start >> PAGE_SHIFT; 201 + pgoff_t index = start >> PAGE_SHIFT; 202 202 pgoff_t end_index = end >> PAGE_SHIFT; 203 - pgoff_t index = start_index; 204 203 struct folio_batch fbatch; 205 204 int i; 206 205 ··· 220 221 } 221 222 } 222 223 223 - static noinline void __unlock_for_delalloc(const struct inode *inode, 224 + static noinline void unlock_delalloc_folio(const struct inode *inode, 224 225 const struct folio *locked_folio, 225 226 u64 start, u64 end) 226 227 { ··· 241 242 { 242 243 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 243 244 struct address_space *mapping = inode->i_mapping; 244 - pgoff_t start_index = start >> PAGE_SHIFT; 245 + pgoff_t index = start >> PAGE_SHIFT; 245 246 pgoff_t end_index = end >> PAGE_SHIFT; 246 - pgoff_t index = start_index; 247 247 u64 processed_end = start; 248 248 struct folio_batch fbatch; 249 249 ··· 286 288 out: 287 289 folio_batch_release(&fbatch); 288 290 if (processed_end > start) 289 - __unlock_for_delalloc(inode, locked_folio, start, 290 - processed_end); 291 + unlock_delalloc_folio(inode, locked_folio, start, processed_end); 291 292 return -EAGAIN; 292 293 } 293 294 ··· 387 390 388 391 unlock_extent(tree, delalloc_start, delalloc_end, &cached_state); 389 392 if (!ret) { 390 - __unlock_for_delalloc(inode, locked_folio, delalloc_start, 393 + unlock_delalloc_folio(inode, locked_folio, delalloc_start, 391 394 delalloc_end); 392 395 cond_resched(); 393 396 goto again; ··· 707 710 bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, fs_info, 708 711 bio_ctrl->end_io_func, NULL); 709 712 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; 713 + bbio->bio.bi_write_hint = inode->vfs_inode.i_write_hint; 710 714 bbio->inode = inode; 711 715 bbio->file_offset = file_offset; 712 716 bio_ctrl->bbio = bbio; ··· 860 862 return ret; 861 863 } 862 864 863 - int set_page_extent_mapped(struct page *page) 864 - { 865 - return set_folio_extent_mapped(page_folio(page)); 866 - } 867 - 868 865 int set_folio_extent_mapped(struct folio *folio) 869 866 { 870 867 struct btrfs_fs_info *fs_info; ··· 894 901 folio_detach_private(folio); 895 902 } 896 903 897 - static struct extent_map *__get_extent_map(struct inode *inode, 898 - struct folio *folio, u64 start, 899 - u64 len, struct extent_map **em_cached) 904 + static struct extent_map *get_extent_map(struct btrfs_inode *inode, 905 + struct folio *folio, u64 start, 906 + u64 len, struct extent_map **em_cached) 900 907 { 901 908 struct extent_map *em; 902 909 struct extent_state *cached_state = NULL; ··· 915 922 *em_cached = NULL; 916 923 } 917 924 918 - btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), start, start + len - 1, &cached_state); 919 - em = btrfs_get_extent(BTRFS_I(inode), folio, start, len); 925 + btrfs_lock_and_flush_ordered_range(inode, start, start + len - 1, &cached_state); 926 + em = btrfs_get_extent(inode, folio, start, len); 920 927 if (!IS_ERR(em)) { 921 928 BUG_ON(*em_cached); 922 929 refcount_inc(&em->refs); 923 930 *em_cached = em; 924 931 } 925 - unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len - 1, &cached_state); 932 + unlock_extent(&inode->io_tree, start, start + len - 1, &cached_state); 926 933 927 934 return em; 928 935 } ··· 978 985 end_folio_read(folio, true, cur, iosize); 979 986 break; 980 987 } 981 - em = __get_extent_map(inode, folio, cur, end - cur + 1, 982 - em_cached); 988 + em = get_extent_map(BTRFS_I(inode), folio, cur, end - cur + 1, em_cached); 983 989 if (IS_ERR(em)) { 984 990 end_folio_read(folio, false, cur, end + 1 - cur); 985 991 return PTR_ERR(em); ··· 1134 1142 } 1135 1143 1136 1144 /* 1137 - * helper for extent_writepage(), doing all of the delayed allocation setup. 1145 + * Do all of the delayed allocation setup. 1138 1146 * 1139 - * This returns 1 if btrfs_run_delalloc_range function did all the work required 1140 - * to write the page (copy into inline extent). In this case the IO has 1141 - * been started and the page is already unlocked. 1147 + * Return >0 if all the dirty blocks are submitted async (compression) or inlined. 1148 + * The @folio should no longer be touched (treat it as already unlocked). 1142 1149 * 1143 - * This returns 0 if all went well (page still locked) 1144 - * This returns < 0 if there were errors (page still locked) 1150 + * Return 0 if there is still dirty block that needs to be submitted through 1151 + * extent_writepage_io(). 1152 + * bio_ctrl->submit_bitmap will indicate which blocks of the folio should be 1153 + * submitted, and @folio is still kept locked. 1154 + * 1155 + * Return <0 if there is any error hit. 1156 + * Any allocated ordered extent range covering this folio will be marked 1157 + * finished (IOERR), and @folio is still kept locked. 1145 1158 */ 1146 1159 static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, 1147 1160 struct folio *folio, ··· 1164 1167 * last delalloc end. 1165 1168 */ 1166 1169 u64 last_delalloc_end = 0; 1170 + /* 1171 + * The range end (exclusive) of the last successfully finished delalloc 1172 + * range. 1173 + * Any range covered by ordered extent must either be manually marked 1174 + * finished (error handling), or has IO submitted (and finish the 1175 + * ordered extent normally). 1176 + * 1177 + * This records the end of ordered extent cleanup if we hit an error. 1178 + */ 1179 + u64 last_finished_delalloc_end = page_start; 1167 1180 u64 delalloc_start = page_start; 1168 1181 u64 delalloc_end = page_end; 1169 1182 u64 delalloc_to_write = 0; ··· 1242 1235 found_len = last_delalloc_end + 1 - found_start; 1243 1236 1244 1237 if (ret >= 0) { 1238 + /* 1239 + * Some delalloc range may be created by previous folios. 1240 + * Thus we still need to clean up this range during error 1241 + * handling. 1242 + */ 1243 + last_finished_delalloc_end = found_start; 1245 1244 /* No errors hit so far, run the current delalloc range. */ 1246 1245 ret = btrfs_run_delalloc_range(inode, folio, 1247 1246 found_start, 1248 1247 found_start + found_len - 1, 1249 1248 wbc); 1249 + if (ret >= 0) 1250 + last_finished_delalloc_end = found_start + found_len; 1251 + if (unlikely(ret < 0)) 1252 + btrfs_err_rl(fs_info, 1253 + "failed to run delalloc range, root=%lld ino=%llu folio=%llu submit_bitmap=%*pbl start=%llu len=%u: %d", 1254 + btrfs_root_id(inode->root), 1255 + btrfs_ino(inode), 1256 + folio_pos(folio), 1257 + fs_info->sectors_per_page, 1258 + &bio_ctrl->submit_bitmap, 1259 + found_start, found_len, ret); 1250 1260 } else { 1251 1261 /* 1252 1262 * We've hit an error during previous delalloc range, ··· 1271 1247 */ 1272 1248 unlock_extent(&inode->io_tree, found_start, 1273 1249 found_start + found_len - 1, NULL); 1274 - __unlock_for_delalloc(&inode->vfs_inode, folio, 1250 + unlock_delalloc_folio(&inode->vfs_inode, folio, 1275 1251 found_start, 1276 1252 found_start + found_len - 1); 1277 1253 } ··· 1298 1274 1299 1275 delalloc_start = found_start + found_len; 1300 1276 } 1301 - if (ret < 0) 1277 + /* 1278 + * It's possible we had some ordered extents created before we hit 1279 + * an error, cleanup non-async successfully created delalloc ranges. 1280 + */ 1281 + if (unlikely(ret < 0)) { 1282 + unsigned int bitmap_size = min( 1283 + (last_finished_delalloc_end - page_start) >> 1284 + fs_info->sectorsize_bits, 1285 + fs_info->sectors_per_page); 1286 + 1287 + for_each_set_bit(bit, &bio_ctrl->submit_bitmap, bitmap_size) 1288 + btrfs_mark_ordered_io_finished(inode, folio, 1289 + page_start + (bit << fs_info->sectorsize_bits), 1290 + fs_info->sectorsize, false); 1302 1291 return ret; 1292 + } 1303 1293 out: 1304 1294 if (last_delalloc_end) 1305 1295 delalloc_end = last_delalloc_end; ··· 1373 1335 1374 1336 em = btrfs_get_extent(inode, NULL, filepos, sectorsize); 1375 1337 if (IS_ERR(em)) 1376 - return PTR_ERR_OR_ZERO(em); 1338 + return PTR_ERR(em); 1377 1339 1378 1340 extent_offset = filepos - em->start; 1379 1341 em_end = extent_map_end(em); ··· 1429 1391 struct btrfs_fs_info *fs_info = inode->root->fs_info; 1430 1392 unsigned long range_bitmap = 0; 1431 1393 bool submitted_io = false; 1394 + bool error = false; 1432 1395 const u64 folio_start = folio_pos(folio); 1433 1396 u64 cur; 1434 1397 int bit; ··· 1472 1433 break; 1473 1434 } 1474 1435 ret = submit_one_sector(inode, folio, cur, bio_ctrl, i_size); 1475 - if (ret < 0) 1476 - goto out; 1436 + if (unlikely(ret < 0)) { 1437 + /* 1438 + * bio_ctrl may contain a bio crossing several folios. 1439 + * Submit it immediately so that the bio has a chance 1440 + * to finish normally, other than marked as error. 1441 + */ 1442 + submit_one_bio(bio_ctrl); 1443 + /* 1444 + * Failed to grab the extent map which should be very rare. 1445 + * Since there is no bio submitted to finish the ordered 1446 + * extent, we have to manually finish this sector. 1447 + */ 1448 + btrfs_mark_ordered_io_finished(inode, folio, cur, 1449 + fs_info->sectorsize, false); 1450 + error = true; 1451 + continue; 1452 + } 1477 1453 submitted_io = true; 1478 1454 } 1479 - out: 1455 + 1480 1456 /* 1481 1457 * If we didn't submitted any sector (>= i_size), folio dirty get 1482 1458 * cleared but PAGECACHE_TAG_DIRTY is not cleared (only cleared ··· 1499 1445 * 1500 1446 * Here we set writeback and clear for the range. If the full folio 1501 1447 * is no longer dirty then we clear the PAGECACHE_TAG_DIRTY tag. 1448 + * 1449 + * If we hit any error, the corresponding sector will still be dirty 1450 + * thus no need to clear PAGECACHE_TAG_DIRTY. 1502 1451 */ 1503 - if (!submitted_io) { 1452 + if (!submitted_io && !error) { 1504 1453 btrfs_folio_set_writeback(fs_info, folio, start, len); 1505 1454 btrfs_folio_clear_writeback(fs_info, folio, start, len); 1506 1455 } ··· 1521 1464 */ 1522 1465 static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl) 1523 1466 { 1524 - struct inode *inode = folio->mapping->host; 1525 - struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 1526 - const u64 page_start = folio_pos(folio); 1467 + struct btrfs_inode *inode = BTRFS_I(folio->mapping->host); 1468 + struct btrfs_fs_info *fs_info = inode->root->fs_info; 1527 1469 int ret; 1528 1470 size_t pg_offset; 1529 - loff_t i_size = i_size_read(inode); 1471 + loff_t i_size = i_size_read(&inode->vfs_inode); 1530 1472 unsigned long end_index = i_size >> PAGE_SHIFT; 1531 1473 1532 - trace_extent_writepage(folio, inode, bio_ctrl->wbc); 1474 + trace_extent_writepage(folio, &inode->vfs_inode, bio_ctrl->wbc); 1533 1475 1534 1476 WARN_ON(!folio_test_locked(folio)); 1535 1477 ··· 1552 1496 if (ret < 0) 1553 1497 goto done; 1554 1498 1555 - ret = writepage_delalloc(BTRFS_I(inode), folio, bio_ctrl); 1499 + ret = writepage_delalloc(inode, folio, bio_ctrl); 1556 1500 if (ret == 1) 1557 1501 return 0; 1558 1502 if (ret) 1559 1503 goto done; 1560 1504 1561 - ret = extent_writepage_io(BTRFS_I(inode), folio, folio_pos(folio), 1505 + ret = extent_writepage_io(inode, folio, folio_pos(folio), 1562 1506 PAGE_SIZE, bio_ctrl, i_size); 1563 1507 if (ret == 1) 1564 1508 return 0; 1509 + if (ret < 0) 1510 + btrfs_err_rl(fs_info, 1511 + "failed to submit blocks, root=%lld inode=%llu folio=%llu submit_bitmap=%*pbl: %d", 1512 + btrfs_root_id(inode->root), btrfs_ino(inode), 1513 + folio_pos(folio), fs_info->sectors_per_page, 1514 + &bio_ctrl->submit_bitmap, ret); 1565 1515 1566 1516 bio_ctrl->wbc->nr_to_write--; 1567 1517 1568 1518 done: 1569 - if (ret) { 1570 - btrfs_mark_ordered_io_finished(BTRFS_I(inode), folio, 1571 - page_start, PAGE_SIZE, !ret); 1519 + if (ret < 0) 1572 1520 mapping_set_error(folio->mapping, ret); 1573 - } 1574 - 1575 1521 /* 1576 1522 * Only unlock ranges that are submitted. As there can be some async 1577 1523 * submitted ranges inside the folio. ··· 1581 1523 btrfs_folio_end_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap); 1582 1524 ASSERT(ret <= 0); 1583 1525 return ret; 1584 - } 1585 - 1586 - void wait_on_extent_buffer_writeback(struct extent_buffer *eb) 1587 - { 1588 - wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, 1589 - TASK_UNINTERRUPTIBLE); 1590 1526 } 1591 1527 1592 1528 /* ··· 1723 1671 { 1724 1672 struct extent_buffer *eb = bbio->private; 1725 1673 struct btrfs_fs_info *fs_info = eb->fs_info; 1726 - bool uptodate = !bbio->bio.bi_status; 1727 1674 struct folio_iter fi; 1728 1675 u32 bio_offset = 0; 1729 1676 1730 - if (!uptodate) 1677 + if (bbio->bio.bi_status != BLK_STS_OK) 1731 1678 set_btree_ioerr(eb); 1732 1679 1733 1680 bio_for_each_folio_all(fi, &bbio->bio) { ··· 2343 2292 if (ret == 1) 2344 2293 goto next_page; 2345 2294 2346 - if (ret) { 2347 - btrfs_mark_ordered_io_finished(BTRFS_I(inode), folio, 2348 - cur, cur_len, !ret); 2295 + if (ret) 2349 2296 mapping_set_error(mapping, ret); 2350 - } 2351 2297 btrfs_folio_end_lock(fs_info, folio, cur, cur_len); 2352 2298 if (ret < 0) 2353 2299 found_error = true; ··· 2543 2495 return try_release_extent_state(io_tree, folio); 2544 2496 } 2545 2497 2546 - static void __free_extent_buffer(struct extent_buffer *eb) 2547 - { 2548 - kmem_cache_free(extent_buffer_cache, eb); 2549 - } 2550 - 2551 2498 static int extent_buffer_under_io(const struct extent_buffer *eb) 2552 2499 { 2553 2500 return (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || ··· 2623 2580 spin_unlock(&folio->mapping->i_private_lock); 2624 2581 } 2625 2582 2626 - /* Release all pages attached to the extent buffer */ 2627 - static void btrfs_release_extent_buffer_pages(const struct extent_buffer *eb) 2583 + /* Release all folios attached to the extent buffer */ 2584 + static void btrfs_release_extent_buffer_folios(const struct extent_buffer *eb) 2628 2585 { 2629 2586 ASSERT(!extent_buffer_under_io(eb)); 2630 2587 ··· 2646 2603 */ 2647 2604 static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) 2648 2605 { 2649 - btrfs_release_extent_buffer_pages(eb); 2606 + btrfs_release_extent_buffer_folios(eb); 2650 2607 btrfs_leak_debug_del_eb(eb); 2651 - __free_extent_buffer(eb); 2608 + kmem_cache_free(extent_buffer_cache, eb); 2652 2609 } 2653 2610 2654 2611 static struct extent_buffer * ··· 2746 2703 folio_put(eb->folios[i]); 2747 2704 } 2748 2705 } 2749 - __free_extent_buffer(eb); 2706 + kmem_cache_free(extent_buffer_cache, eb); 2750 2707 return NULL; 2751 2708 } 2752 2709 ··· 2873 2830 } 2874 2831 #endif 2875 2832 2876 - static struct extent_buffer *grab_extent_buffer( 2877 - struct btrfs_fs_info *fs_info, struct page *page) 2833 + static struct extent_buffer *grab_extent_buffer(struct btrfs_fs_info *fs_info, 2834 + struct folio *folio) 2878 2835 { 2879 - struct folio *folio = page_folio(page); 2880 2836 struct extent_buffer *exists; 2881 2837 2882 - lockdep_assert_held(&page->mapping->i_private_lock); 2838 + lockdep_assert_held(&folio->mapping->i_private_lock); 2883 2839 2884 2840 /* 2885 2841 * For subpage case, we completely rely on radix tree to ensure we ··· 2893 2851 return NULL; 2894 2852 2895 2853 /* 2896 - * We could have already allocated an eb for this page and attached one 2854 + * We could have already allocated an eb for this folio and attached one 2897 2855 * so lets see if we can get a ref on the existing eb, and if we can we 2898 2856 * know it's good and we can just return that one, else we know we can 2899 2857 * just overwrite folio private. ··· 2902 2860 if (atomic_inc_not_zero(&exists->refs)) 2903 2861 return exists; 2904 2862 2905 - WARN_ON(PageDirty(page)); 2863 + WARN_ON(folio_test_dirty(folio)); 2906 2864 folio_detach_private(folio); 2907 2865 return NULL; 2908 2866 } 2909 2867 2910 - static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) 2868 + /* 2869 + * Validate alignment constraints of eb at logical address @start. 2870 + */ 2871 + static bool check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) 2911 2872 { 2912 2873 if (!IS_ALIGNED(start, fs_info->sectorsize)) { 2913 2874 btrfs_err(fs_info, "bad tree block start %llu", start); 2914 - return -EINVAL; 2875 + return true; 2915 2876 } 2916 2877 2917 2878 if (fs_info->nodesize < PAGE_SIZE && ··· 2922 2877 btrfs_err(fs_info, 2923 2878 "tree block crosses page boundary, start %llu nodesize %u", 2924 2879 start, fs_info->nodesize); 2925 - return -EINVAL; 2880 + return true; 2926 2881 } 2927 2882 if (fs_info->nodesize >= PAGE_SIZE && 2928 2883 !PAGE_ALIGNED(start)) { 2929 2884 btrfs_err(fs_info, 2930 2885 "tree block is not page aligned, start %llu nodesize %u", 2931 2886 start, fs_info->nodesize); 2932 - return -EINVAL; 2887 + return true; 2933 2888 } 2934 2889 if (!IS_ALIGNED(start, fs_info->nodesize) && 2935 2890 !test_and_set_bit(BTRFS_FS_UNALIGNED_TREE_BLOCK, &fs_info->flags)) { ··· 2937 2892 "tree block not nodesize aligned, start %llu nodesize %u, can be resolved by a full metadata balance", 2938 2893 start, fs_info->nodesize); 2939 2894 } 2940 - return 0; 2895 + return false; 2941 2896 } 2942 - 2943 2897 2944 2898 /* 2945 2899 * Return 0 if eb->folios[i] is attached to btree inode successfully. ··· 2995 2951 } else if (existing_folio) { 2996 2952 struct extent_buffer *existing_eb; 2997 2953 2998 - existing_eb = grab_extent_buffer(fs_info, 2999 - folio_page(existing_folio, 0)); 2954 + existing_eb = grab_extent_buffer(fs_info, existing_folio); 3000 2955 if (existing_eb) { 3001 2956 /* The extent buffer still exists, we can use it directly. */ 3002 2957 *found_eb_ret = existing_eb; ··· 3192 3149 * live buffer and won't free them prematurely. 3193 3150 */ 3194 3151 for (int i = 0; i < num_folios; i++) 3195 - unlock_page(folio_page(eb->folios[i], 0)); 3152 + folio_unlock(eb->folios[i]); 3196 3153 return eb; 3197 3154 3198 3155 out: ··· 3216 3173 for (int i = 0; i < attached; i++) { 3217 3174 ASSERT(eb->folios[i]); 3218 3175 detach_extent_buffer_folio(eb, eb->folios[i]); 3219 - unlock_page(folio_page(eb->folios[i], 0)); 3176 + folio_unlock(eb->folios[i]); 3220 3177 folio_put(eb->folios[i]); 3221 3178 eb->folios[i] = NULL; 3222 3179 } ··· 3238 3195 struct extent_buffer *eb = 3239 3196 container_of(head, struct extent_buffer, rcu_head); 3240 3197 3241 - __free_extent_buffer(eb); 3198 + kmem_cache_free(extent_buffer_cache, eb); 3242 3199 } 3243 3200 3244 3201 static int release_extent_buffer(struct extent_buffer *eb) ··· 3262 3219 } 3263 3220 3264 3221 btrfs_leak_debug_del_eb(eb); 3265 - /* Should be safe to release our pages at this point */ 3266 - btrfs_release_extent_buffer_pages(eb); 3222 + /* Should be safe to release folios at this point. */ 3223 + btrfs_release_extent_buffer_folios(eb); 3267 3224 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 3268 3225 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { 3269 - __free_extent_buffer(eb); 3226 + kmem_cache_free(extent_buffer_cache, eb); 3270 3227 return 1; 3271 3228 } 3272 3229 #endif ··· 3425 3382 * the above race. 3426 3383 */ 3427 3384 if (subpage) 3428 - lock_page(folio_page(eb->folios[0], 0)); 3385 + folio_lock(eb->folios[0]); 3429 3386 for (int i = 0; i < num_folios; i++) 3430 3387 btrfs_folio_set_dirty(eb->fs_info, eb->folios[i], 3431 3388 eb->start, eb->len); 3432 3389 if (subpage) 3433 - unlock_page(folio_page(eb->folios[0], 0)); 3390 + folio_unlock(eb->folios[0]); 3434 3391 percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes, 3435 3392 eb->len, 3436 3393 eb->fs_info->dirty_metadata_batch); ··· 3540 3497 bio_put(&bbio->bio); 3541 3498 } 3542 3499 3543 - int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, 3544 - const struct btrfs_tree_parent_check *check) 3500 + int read_extent_buffer_pages_nowait(struct extent_buffer *eb, int mirror_num, 3501 + const struct btrfs_tree_parent_check *check) 3545 3502 { 3546 3503 struct btrfs_bio *bbio; 3547 3504 bool ret; ··· 3559 3516 3560 3517 /* Someone else is already reading the buffer, just wait for it. */ 3561 3518 if (test_and_set_bit(EXTENT_BUFFER_READING, &eb->bflags)) 3562 - goto done; 3519 + return 0; 3563 3520 3564 3521 /* 3565 3522 * Between the initial test_bit(EXTENT_BUFFER_UPTODATE) and the above ··· 3599 3556 } 3600 3557 } 3601 3558 btrfs_submit_bbio(bbio, mirror_num); 3559 + return 0; 3560 + } 3602 3561 3603 - done: 3604 - if (wait == WAIT_COMPLETE) { 3605 - wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE); 3606 - if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 3607 - return -EIO; 3608 - } 3562 + int read_extent_buffer_pages(struct extent_buffer *eb, int mirror_num, 3563 + const struct btrfs_tree_parent_check *check) 3564 + { 3565 + int ret; 3609 3566 3567 + ret = read_extent_buffer_pages_nowait(eb, mirror_num, check); 3568 + if (ret < 0) 3569 + return ret; 3570 + 3571 + wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE); 3572 + if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 3573 + return -EIO; 3610 3574 return 0; 3611 3575 } 3612 3576 ··· 4344 4294 return; 4345 4295 } 4346 4296 4347 - ret = read_extent_buffer_pages(eb, WAIT_NONE, 0, &check); 4297 + ret = read_extent_buffer_pages_nowait(eb, 0, &check); 4348 4298 if (ret < 0) 4349 4299 free_extent_buffer_stale(eb); 4350 4300 else
+10 -6
fs/btrfs/extent_io.h
··· 248 248 struct writeback_control *wbc); 249 249 void btrfs_readahead(struct readahead_control *rac); 250 250 int set_folio_extent_mapped(struct folio *folio); 251 - int set_page_extent_mapped(struct page *page); 252 251 void clear_folio_extent_mapped(struct folio *folio); 253 252 254 253 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, ··· 261 262 u64 start); 262 263 void free_extent_buffer(struct extent_buffer *eb); 263 264 void free_extent_buffer_stale(struct extent_buffer *eb); 264 - #define WAIT_NONE 0 265 - #define WAIT_COMPLETE 1 266 - #define WAIT_PAGE_LOCK 2 267 - int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, 265 + int read_extent_buffer_pages(struct extent_buffer *eb, int mirror_num, 268 266 const struct btrfs_tree_parent_check *parent_check); 269 - void wait_on_extent_buffer_writeback(struct extent_buffer *eb); 267 + int read_extent_buffer_pages_nowait(struct extent_buffer *eb, int mirror_num, 268 + const struct btrfs_tree_parent_check *parent_check); 269 + 270 + static inline void wait_on_extent_buffer_writeback(struct extent_buffer *eb) 271 + { 272 + wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, 273 + TASK_UNINTERRUPTIBLE); 274 + } 275 + 270 276 void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, 271 277 u64 bytenr, u64 owner_root, u64 gen, int level); 272 278 void btrfs_readahead_node_child(struct extent_buffer *node, int slot);
-3
fs/btrfs/file-item.c
··· 190 190 btrfs_set_file_extent_compression(leaf, item, 0); 191 191 btrfs_set_file_extent_encryption(leaf, item, 0); 192 192 btrfs_set_file_extent_other_encoding(leaf, item, 0); 193 - 194 - btrfs_mark_buffer_dirty(trans, leaf); 195 193 out: 196 194 btrfs_free_path(path); 197 195 return ret; ··· 1257 1259 ins_size /= csum_size; 1258 1260 total_bytes += ins_size * fs_info->sectorsize; 1259 1261 1260 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 1261 1262 if (total_bytes < sums->len) { 1262 1263 btrfs_release_path(path); 1263 1264 cond_resched();
+41 -65
fs/btrfs/file.c
··· 36 36 #include "ioctl.h" 37 37 #include "file.h" 38 38 #include "super.h" 39 - 40 - /* 41 - * Helper to fault in page and copy. This should go away and be replaced with 42 - * calls into generic code. 43 - */ 44 - static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes, 45 - struct folio *folio, struct iov_iter *i) 46 - { 47 - size_t copied = 0; 48 - size_t total_copied = 0; 49 - int offset = offset_in_page(pos); 50 - 51 - while (write_bytes > 0) { 52 - size_t count = min_t(size_t, PAGE_SIZE - offset, write_bytes); 53 - /* 54 - * Copy data from userspace to the current page 55 - */ 56 - copied = copy_folio_from_iter_atomic(folio, offset, count, i); 57 - 58 - /* Flush processor's dcache for this page */ 59 - flush_dcache_folio(folio); 60 - 61 - /* 62 - * if we get a partial write, we can end up with 63 - * partially up to date page. These add 64 - * a lot of complexity, so make sure they don't 65 - * happen by forcing this copy to be retried. 66 - * 67 - * The rest of the btrfs_file_write code will fall 68 - * back to page at a time copies after we return 0. 69 - */ 70 - if (unlikely(copied < count)) { 71 - if (!folio_test_uptodate(folio)) { 72 - iov_iter_revert(i, copied); 73 - copied = 0; 74 - } 75 - if (!copied) 76 - break; 77 - } 78 - 79 - write_bytes -= copied; 80 - total_copied += copied; 81 - offset += copied; 82 - } 83 - return total_copied; 84 - } 39 + #include "print-tree.h" 85 40 86 41 /* 87 42 * Unlock folio after btrfs_file_write() is done with it. ··· 61 106 } 62 107 63 108 /* 64 - * After btrfs_copy_from_user(), update the following things for delalloc: 109 + * After copy_folio_from_iter_atomic(), update the following things for delalloc: 65 110 * - Mark newly dirtied folio as DELALLOC in the io tree. 66 111 * Used to advise which range is to be written back. 67 112 * - Mark modified folio as Uptodate/Dirty and not needing COW fixup ··· 179 224 if (args->drop_cache) 180 225 btrfs_drop_extent_map_range(inode, args->start, args->end - 1, false); 181 226 182 - if (args->start >= inode->disk_i_size && !args->replace_extent) 227 + if (data_race(args->start >= inode->disk_i_size) && !args->replace_extent) 183 228 modify_tree = 0; 184 229 185 230 update_refs = (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID); ··· 200 245 next_slot: 201 246 leaf = path->nodes[0]; 202 247 if (path->slots[0] >= btrfs_header_nritems(leaf)) { 203 - BUG_ON(del_nr > 0); 248 + if (WARN_ON(del_nr > 0)) { 249 + btrfs_print_leaf(leaf); 250 + ret = -EINVAL; 251 + break; 252 + } 204 253 ret = btrfs_next_leaf(root, path); 205 254 if (ret < 0) 206 255 break; ··· 280 321 * | -------- extent -------- | 281 322 */ 282 323 if (args->start > key.offset && args->end < extent_end) { 283 - BUG_ON(del_nr > 0); 324 + if (WARN_ON(del_nr > 0)) { 325 + btrfs_print_leaf(leaf); 326 + ret = -EINVAL; 327 + break; 328 + } 284 329 if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 285 330 ret = -EOPNOTSUPP; 286 331 break; ··· 314 351 btrfs_set_file_extent_offset(leaf, fi, extent_offset); 315 352 btrfs_set_file_extent_num_bytes(leaf, fi, 316 353 extent_end - args->start); 317 - btrfs_mark_buffer_dirty(trans, leaf); 318 354 319 355 if (update_refs && disk_bytenr > 0) { 320 356 struct btrfs_ref ref = { ··· 359 397 btrfs_set_file_extent_offset(leaf, fi, extent_offset); 360 398 btrfs_set_file_extent_num_bytes(leaf, fi, 361 399 extent_end - args->end); 362 - btrfs_mark_buffer_dirty(trans, leaf); 363 400 if (update_refs && disk_bytenr > 0) 364 401 args->bytes_found += args->end - key.offset; 365 402 break; ··· 370 409 * | -------- extent -------- | 371 410 */ 372 411 if (args->start > key.offset && args->end >= extent_end) { 373 - BUG_ON(del_nr > 0); 412 + if (WARN_ON(del_nr > 0)) { 413 + btrfs_print_leaf(leaf); 414 + ret = -EINVAL; 415 + break; 416 + } 374 417 if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 375 418 ret = -EOPNOTSUPP; 376 419 break; ··· 382 417 383 418 btrfs_set_file_extent_num_bytes(leaf, fi, 384 419 args->start - key.offset); 385 - btrfs_mark_buffer_dirty(trans, leaf); 386 420 if (update_refs && disk_bytenr > 0) 387 421 args->bytes_found += extent_end - args->start; 388 422 if (args->end == extent_end) ··· 401 437 del_slot = path->slots[0]; 402 438 del_nr = 1; 403 439 } else { 404 - BUG_ON(del_slot + del_nr != path->slots[0]); 440 + if (WARN_ON(del_slot + del_nr != path->slots[0])) { 441 + btrfs_print_leaf(leaf); 442 + ret = -EINVAL; 443 + break; 444 + } 405 445 del_nr++; 406 446 } 407 447 ··· 636 668 trans->transid); 637 669 btrfs_set_file_extent_num_bytes(leaf, fi, 638 670 end - other_start); 639 - btrfs_mark_buffer_dirty(trans, leaf); 640 671 goto out; 641 672 } 642 673 } ··· 664 697 other_end - start); 665 698 btrfs_set_file_extent_offset(leaf, fi, 666 699 start - orig_offset); 667 - btrfs_mark_buffer_dirty(trans, leaf); 668 700 goto out; 669 701 } 670 702 } ··· 697 731 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset); 698 732 btrfs_set_file_extent_num_bytes(leaf, fi, 699 733 extent_end - split); 700 - btrfs_mark_buffer_dirty(trans, leaf); 701 734 702 735 ref.action = BTRFS_ADD_DELAYED_REF; 703 736 ref.bytenr = bytenr; ··· 775 810 btrfs_set_file_extent_type(leaf, fi, 776 811 BTRFS_FILE_EXTENT_REG); 777 812 btrfs_set_file_extent_generation(leaf, fi, trans->transid); 778 - btrfs_mark_buffer_dirty(trans, leaf); 779 813 } else { 780 814 fi = btrfs_item_ptr(leaf, del_slot - 1, 781 815 struct btrfs_file_extent_item); ··· 783 819 btrfs_set_file_extent_generation(leaf, fi, trans->transid); 784 820 btrfs_set_file_extent_num_bytes(leaf, fi, 785 821 extent_end - key.offset); 786 - btrfs_mark_buffer_dirty(trans, leaf); 787 822 788 823 ret = btrfs_del_items(trans, root, path, del_slot, del_nr); 789 824 if (ret < 0) { ··· 1015 1052 &cached_state); 1016 1053 } 1017 1054 ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes, 1018 - NULL, nowait, false); 1055 + NULL, nowait); 1019 1056 if (ret <= 0) 1020 1057 btrfs_drew_write_unlock(&root->snapshot_lock); 1021 1058 else ··· 1215 1252 break; 1216 1253 } 1217 1254 1218 - copied = btrfs_copy_from_user(pos, write_bytes, folio, i); 1255 + copied = copy_folio_from_iter_atomic(folio, 1256 + offset_in_folio(folio, pos), write_bytes, i); 1257 + flush_dcache_folio(folio); 1258 + 1259 + /* 1260 + * If we get a partial write, we can end up with partially 1261 + * uptodate page. Although if sector size < page size we can 1262 + * handle it, but if it's not sector aligned it can cause 1263 + * a lot of complexity, so make sure they don't happen by 1264 + * forcing retry this copy. 1265 + */ 1266 + if (unlikely(copied < write_bytes)) { 1267 + if (!folio_test_uptodate(folio)) { 1268 + iov_iter_revert(i, copied); 1269 + copied = 0; 1270 + } 1271 + } 1219 1272 1220 1273 num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes); 1221 1274 dirty_sectors = round_up(copied + sector_offset, ··· 2008 2029 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); 2009 2030 btrfs_set_file_extent_offset(leaf, fi, 0); 2010 2031 btrfs_set_file_extent_generation(leaf, fi, trans->transid); 2011 - btrfs_mark_buffer_dirty(trans, leaf); 2012 2032 goto out; 2013 2033 } 2014 2034 ··· 2024 2046 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); 2025 2047 btrfs_set_file_extent_offset(leaf, fi, 0); 2026 2048 btrfs_set_file_extent_generation(leaf, fi, trans->transid); 2027 - btrfs_mark_buffer_dirty(trans, leaf); 2028 2049 goto out; 2029 2050 } 2030 2051 btrfs_release_path(path); ··· 2171 2194 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len); 2172 2195 if (extent_info->is_new_extent) 2173 2196 btrfs_set_file_extent_generation(leaf, extent, trans->transid); 2174 - btrfs_mark_buffer_dirty(trans, leaf); 2175 2197 btrfs_release_path(path); 2176 2198 2177 2199 ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,
+2 -5
fs/btrfs/free-space-cache.c
··· 12 12 #include <linux/error-injection.h> 13 13 #include <linux/sched/mm.h> 14 14 #include <linux/string_choices.h> 15 - #include "ctree.h" 15 + #include "extent-tree.h" 16 16 #include "fs.h" 17 17 #include "messages.h" 18 18 #include "misc.h" ··· 198 198 btrfs_set_inode_nlink(leaf, inode_item, 1); 199 199 btrfs_set_inode_transid(leaf, inode_item, trans->transid); 200 200 btrfs_set_inode_block_group(leaf, inode_item, offset); 201 - btrfs_mark_buffer_dirty(trans, leaf); 202 201 btrfs_release_path(path); 203 202 204 203 key.objectid = BTRFS_FREE_SPACE_OBJECTID; ··· 215 216 struct btrfs_free_space_header); 216 217 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header)); 217 218 btrfs_set_free_space_key(leaf, header, &disk_key); 218 - btrfs_mark_buffer_dirty(trans, leaf); 219 219 btrfs_release_path(path); 220 220 221 221 return 0; ··· 461 463 return -ENOMEM; 462 464 } 463 465 464 - ret = set_page_extent_mapped(page); 466 + ret = set_folio_extent_mapped(page_folio(page)); 465 467 if (ret < 0) { 466 468 unlock_page(page); 467 469 put_page(page); ··· 1187 1189 btrfs_set_free_space_entries(leaf, header, entries); 1188 1190 btrfs_set_free_space_bitmaps(leaf, header, bitmaps); 1189 1191 btrfs_set_free_space_generation(leaf, header, trans->transid); 1190 - btrfs_mark_buffer_dirty(trans, leaf); 1191 1192 btrfs_release_path(path); 1192 1193 1193 1194 return 0;
+6 -5
fs/btrfs/free-space-tree.c
··· 89 89 struct btrfs_free_space_info); 90 90 btrfs_set_free_space_extent_count(leaf, info, 0); 91 91 btrfs_set_free_space_flags(leaf, info, 0); 92 - btrfs_mark_buffer_dirty(trans, leaf); 93 92 94 93 ret = 0; 95 94 out: ··· 286 287 flags |= BTRFS_FREE_SPACE_USING_BITMAPS; 287 288 btrfs_set_free_space_flags(leaf, info, flags); 288 289 expected_extent_count = btrfs_free_space_extent_count(leaf, info); 289 - btrfs_mark_buffer_dirty(trans, leaf); 290 290 btrfs_release_path(path); 291 291 292 292 if (extent_count != expected_extent_count) { ··· 322 324 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 323 325 write_extent_buffer(leaf, bitmap_cursor, ptr, 324 326 data_size); 325 - btrfs_mark_buffer_dirty(trans, leaf); 326 327 btrfs_release_path(path); 327 328 328 329 i += extent_size; ··· 427 430 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS; 428 431 btrfs_set_free_space_flags(leaf, info, flags); 429 432 expected_extent_count = btrfs_free_space_extent_count(leaf, info); 430 - btrfs_mark_buffer_dirty(trans, leaf); 431 433 btrfs_release_path(path); 432 434 433 435 nrbits = block_group->length >> block_group->fs_info->sectorsize_bits; ··· 491 495 492 496 extent_count += new_extents; 493 497 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count); 494 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 495 498 btrfs_release_path(path); 496 499 497 500 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) && ··· 1344 1349 btrfs_abort_transaction(trans, ret); 1345 1350 btrfs_end_transaction(trans); 1346 1351 return ret; 1352 + } 1353 + if (btrfs_should_end_transaction(trans)) { 1354 + btrfs_end_transaction(trans); 1355 + trans = btrfs_start_transaction(free_space_root, 1); 1356 + if (IS_ERR(trans)) 1357 + return PTR_ERR(trans); 1347 1358 } 1348 1359 node = rb_next(node); 1349 1360 }
+130
fs/btrfs/fs.c
··· 4 4 #include "ctree.h" 5 5 #include "fs.h" 6 6 #include "accessors.h" 7 + #include "volumes.h" 8 + 9 + static const struct btrfs_csums { 10 + u16 size; 11 + const char name[10]; 12 + const char driver[12]; 13 + } btrfs_csums[] = { 14 + [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" }, 15 + [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" }, 16 + [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" }, 17 + [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b", 18 + .driver = "blake2b-256" }, 19 + }; 20 + 21 + /* This exists for btrfs-progs usages. */ 22 + u16 btrfs_csum_type_size(u16 type) 23 + { 24 + return btrfs_csums[type].size; 25 + } 26 + 27 + int btrfs_super_csum_size(const struct btrfs_super_block *s) 28 + { 29 + u16 t = btrfs_super_csum_type(s); 30 + 31 + /* csum type is validated at mount time. */ 32 + return btrfs_csum_type_size(t); 33 + } 34 + 35 + const char *btrfs_super_csum_name(u16 csum_type) 36 + { 37 + /* csum type is validated at mount time. */ 38 + return btrfs_csums[csum_type].name; 39 + } 40 + 41 + /* 42 + * Return driver name if defined, otherwise the name that's also a valid driver 43 + * name. 44 + */ 45 + const char *btrfs_super_csum_driver(u16 csum_type) 46 + { 47 + /* csum type is validated at mount time */ 48 + return btrfs_csums[csum_type].driver[0] ? 49 + btrfs_csums[csum_type].driver : 50 + btrfs_csums[csum_type].name; 51 + } 52 + 53 + size_t __attribute_const__ btrfs_get_num_csums(void) 54 + { 55 + return ARRAY_SIZE(btrfs_csums); 56 + } 57 + 58 + /* 59 + * Start exclusive operation @type, return true on success. 60 + */ 61 + bool btrfs_exclop_start(struct btrfs_fs_info *fs_info, 62 + enum btrfs_exclusive_operation type) 63 + { 64 + bool ret = false; 65 + 66 + spin_lock(&fs_info->super_lock); 67 + if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) { 68 + fs_info->exclusive_operation = type; 69 + ret = true; 70 + } 71 + spin_unlock(&fs_info->super_lock); 72 + 73 + return ret; 74 + } 75 + 76 + /* 77 + * Conditionally allow to enter the exclusive operation in case it's compatible 78 + * with the running one. This must be paired with btrfs_exclop_start_unlock() 79 + * and btrfs_exclop_finish(). 80 + * 81 + * Compatibility: 82 + * - the same type is already running 83 + * - when trying to add a device and balance has been paused 84 + * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller 85 + * must check the condition first that would allow none -> @type 86 + */ 87 + bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info, 88 + enum btrfs_exclusive_operation type) 89 + { 90 + spin_lock(&fs_info->super_lock); 91 + if (fs_info->exclusive_operation == type || 92 + (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED && 93 + type == BTRFS_EXCLOP_DEV_ADD)) 94 + return true; 95 + 96 + spin_unlock(&fs_info->super_lock); 97 + return false; 98 + } 99 + 100 + void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info) 101 + { 102 + spin_unlock(&fs_info->super_lock); 103 + } 104 + 105 + void btrfs_exclop_finish(struct btrfs_fs_info *fs_info) 106 + { 107 + spin_lock(&fs_info->super_lock); 108 + WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE); 109 + spin_unlock(&fs_info->super_lock); 110 + sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation"); 111 + } 112 + 113 + void btrfs_exclop_balance(struct btrfs_fs_info *fs_info, 114 + enum btrfs_exclusive_operation op) 115 + { 116 + switch (op) { 117 + case BTRFS_EXCLOP_BALANCE_PAUSED: 118 + spin_lock(&fs_info->super_lock); 119 + ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE || 120 + fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD || 121 + fs_info->exclusive_operation == BTRFS_EXCLOP_NONE || 122 + fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); 123 + fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED; 124 + spin_unlock(&fs_info->super_lock); 125 + break; 126 + case BTRFS_EXCLOP_BALANCE: 127 + spin_lock(&fs_info->super_lock); 128 + ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); 129 + fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; 130 + spin_unlock(&fs_info->super_lock); 131 + break; 132 + default: 133 + btrfs_warn(fs_info, 134 + "invalid exclop balance operation %d requested", op); 135 + } 136 + } 7 137 8 138 void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag, 9 139 const char *name)
+30 -1
fs/btrfs/fs.h
··· 14 14 #include <linux/lockdep.h> 15 15 #include <linux/spinlock.h> 16 16 #include <linux/mutex.h> 17 - #include <linux/rwlock_types.h> 18 17 #include <linux/rwsem.h> 19 18 #include <linux/semaphore.h> 20 19 #include <linux/list.h> 20 + #include <linux/pagemap.h> 21 21 #include <linux/radix-tree.h> 22 22 #include <linux/workqueue.h> 23 23 #include <linux/wait.h> ··· 627 627 struct kobject *qgroups_kobj; 628 628 struct kobject *discard_kobj; 629 629 630 + /* Track the number of blocks (sectors) read by the filesystem. */ 631 + struct percpu_counter stats_read_blocks; 632 + 630 633 /* Used to keep from writing metadata until there is a nice batch */ 631 634 struct percpu_counter dirty_metadata_bytes; 632 635 struct percpu_counter delalloc_bytes; ··· 890 887 #define inode_to_fs_info(_inode) (BTRFS_I(_Generic((_inode), \ 891 888 struct inode *: (_inode)))->root->fs_info) 892 889 890 + static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping) 891 + { 892 + return mapping_gfp_constraint(mapping, ~__GFP_FS); 893 + } 894 + 893 895 static inline u64 btrfs_get_fs_generation(const struct btrfs_fs_info *fs_info) 894 896 { 895 897 return READ_ONCE(fs_info->generation); ··· 961 953 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \ 962 954 sizeof(struct btrfs_item)) 963 955 956 + #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) ((bytes) >> (fs_info)->sectorsize_bits) 957 + 964 958 static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info) 965 959 { 966 960 return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && fs_info->zone_size > 0; ··· 991 981 enum btrfs_exclusive_operation op); 992 982 993 983 int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args); 984 + 985 + u16 btrfs_csum_type_size(u16 type); 986 + int btrfs_super_csum_size(const struct btrfs_super_block *s); 987 + const char *btrfs_super_csum_name(u16 csum_type); 988 + const char *btrfs_super_csum_driver(u16 csum_type); 989 + size_t __attribute_const__ btrfs_get_num_csums(void); 990 + 991 + static inline bool btrfs_is_empty_uuid(const u8 *uuid) 992 + { 993 + return uuid_is_null((const uuid_t *)uuid); 994 + } 994 995 995 996 /* Compatibility and incompatibility defines */ 996 997 void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag, ··· 1078 1057 #define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info) \ 1079 1058 (unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, \ 1080 1059 &(fs_info)->fs_state))) 1060 + 1061 + /* 1062 + * We use folio flag owner_2 to indicate there is an ordered extent with 1063 + * unfinished IO. 1064 + */ 1065 + #define folio_test_ordered(folio) folio_test_owner_2(folio) 1066 + #define folio_set_ordered(folio) folio_set_owner_2(folio) 1067 + #define folio_clear_ordered(folio) folio_clear_owner_2(folio) 1081 1068 1082 1069 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 1083 1070
-5
fs/btrfs/inode-item.c
··· 298 298 299 299 ptr = (unsigned long)&extref->name; 300 300 write_extent_buffer(path->nodes[0], name->name, ptr, name->len); 301 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 302 - 303 301 out: 304 302 btrfs_free_path(path); 305 303 return ret; ··· 361 363 ptr = (unsigned long)(ref + 1); 362 364 } 363 365 write_extent_buffer(path->nodes[0], name->name, ptr, name->len); 364 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 365 - 366 366 out: 367 367 btrfs_free_path(path); 368 368 ··· 586 590 num_dec = (orig_num_bytes - extent_num_bytes); 587 591 if (extent_start != 0) 588 592 control->sub_bytes += num_dec; 589 - btrfs_mark_buffer_dirty(trans, leaf); 590 593 } else { 591 594 extent_num_bytes = 592 595 btrfs_file_extent_disk_num_bytes(leaf, fi);
+194 -131
fs/btrfs/inode.c
··· 393 393 * extent (btrfs_finish_ordered_io()). 394 394 */ 395 395 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, 396 - struct folio *locked_folio, 397 396 u64 offset, u64 bytes) 398 397 { 399 398 unsigned long index = offset >> PAGE_SHIFT; 400 399 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT; 401 - u64 page_start = 0, page_end = 0; 402 400 struct folio *folio; 403 401 404 - if (locked_folio) { 405 - page_start = folio_pos(locked_folio); 406 - page_end = page_start + folio_size(locked_folio) - 1; 407 - } 408 - 409 402 while (index <= end_index) { 410 - /* 411 - * For locked page, we will call btrfs_mark_ordered_io_finished 412 - * through btrfs_mark_ordered_io_finished() on it 413 - * in run_delalloc_range() for the error handling, which will 414 - * clear page Ordered and run the ordered extent accounting. 415 - * 416 - * Here we can't just clear the Ordered bit, or 417 - * btrfs_mark_ordered_io_finished() would skip the accounting 418 - * for the page range, and the ordered extent will never finish. 419 - */ 420 - if (locked_folio && index == (page_start >> PAGE_SHIFT)) { 421 - index++; 422 - continue; 423 - } 424 403 folio = filemap_get_folio(inode->vfs_inode.i_mapping, index); 425 404 index++; 426 405 if (IS_ERR(folio)) ··· 413 434 btrfs_folio_clamp_clear_ordered(inode->root->fs_info, folio, 414 435 offset, bytes); 415 436 folio_put(folio); 416 - } 417 - 418 - if (locked_folio) { 419 - /* The locked page covers the full range, nothing needs to be done */ 420 - if (bytes + offset <= page_start + folio_size(locked_folio)) 421 - return; 422 - /* 423 - * In case this page belongs to the delalloc range being 424 - * instantiated then skip it, since the first page of a range is 425 - * going to be properly cleaned up by the caller of 426 - * run_delalloc_range 427 - */ 428 - if (page_start >= offset && page_end <= (offset + bytes - 1)) { 429 - bytes = offset + bytes - folio_pos(locked_folio) - 430 - folio_size(locked_folio); 431 - offset = folio_pos(locked_folio) + folio_size(locked_folio); 432 - } 433 437 } 434 438 435 439 return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false); ··· 526 564 kunmap_local(kaddr); 527 565 folio_put(folio); 528 566 } 529 - btrfs_mark_buffer_dirty(trans, leaf); 530 567 btrfs_release_path(path); 531 568 532 569 /* ··· 1090 1129 &wbc, false); 1091 1130 wbc_detach_inode(&wbc); 1092 1131 if (ret < 0) { 1093 - btrfs_cleanup_ordered_extents(inode, locked_folio, 1094 - start, end - start + 1); 1095 - if (locked_folio) { 1096 - const u64 page_start = folio_pos(locked_folio); 1097 - 1098 - folio_start_writeback(locked_folio); 1099 - folio_end_writeback(locked_folio); 1100 - btrfs_mark_ordered_io_finished(inode, locked_folio, 1101 - page_start, PAGE_SIZE, 1102 - !ret); 1103 - mapping_set_error(locked_folio->mapping, ret); 1104 - folio_unlock(locked_folio); 1105 - } 1132 + btrfs_cleanup_ordered_extents(inode, start, end - start + 1); 1133 + if (locked_folio) 1134 + btrfs_folio_end_lock(inode->root->fs_info, locked_folio, 1135 + start, async_extent->ram_size); 1136 + btrfs_err_rl(inode->root->fs_info, 1137 + "%s failed, root=%llu inode=%llu start=%llu len=%llu: %d", 1138 + __func__, btrfs_root_id(inode->root), 1139 + btrfs_ino(inode), start, async_extent->ram_size, ret); 1106 1140 } 1107 1141 } 1108 1142 ··· 1329 1373 alloc_hint = btrfs_get_extent_allocation_hint(inode, start, num_bytes); 1330 1374 1331 1375 /* 1376 + * We're not doing compressed IO, don't unlock the first page (which 1377 + * the caller expects to stay locked), don't clear any dirty bits and 1378 + * don't set any writeback bits. 1379 + * 1380 + * Do set the Ordered (Private2) bit so we know this page was properly 1381 + * setup for writepage. 1382 + */ 1383 + page_ops = (keep_locked ? 0 : PAGE_UNLOCK); 1384 + page_ops |= PAGE_SET_ORDERED; 1385 + 1386 + /* 1332 1387 * Relocation relies on the relocated extents to have exactly the same 1333 1388 * size as the original extents. Normally writeback for relocation data 1334 1389 * extents follows a NOCOW path because relocation preallocates the ··· 1398 1431 file_extent.offset = 0; 1399 1432 file_extent.compression = BTRFS_COMPRESS_NONE; 1400 1433 1434 + /* 1435 + * Locked range will be released either during error clean up or 1436 + * after the whole range is finished. 1437 + */ 1401 1438 lock_extent(&inode->io_tree, start, start + cur_alloc_size - 1, 1402 1439 &cached); 1403 1440 ··· 1447 1476 1448 1477 btrfs_dec_block_group_reservations(fs_info, ins.objectid); 1449 1478 1450 - /* 1451 - * We're not doing compressed IO, don't unlock the first page 1452 - * (which the caller expects to stay locked), don't clear any 1453 - * dirty bits and don't set any writeback bits 1454 - * 1455 - * Do set the Ordered flag so we know this page was 1456 - * properly setup for writepage. 1457 - */ 1458 - page_ops = (keep_locked ? 0 : PAGE_UNLOCK); 1459 - page_ops |= PAGE_SET_ORDERED; 1460 - 1461 - extent_clear_unlock_delalloc(inode, start, start + cur_alloc_size - 1, 1462 - locked_folio, &cached, 1463 - EXTENT_LOCKED | EXTENT_DELALLOC, 1464 - page_ops); 1465 1479 if (num_bytes < cur_alloc_size) 1466 1480 num_bytes = 0; 1467 1481 else ··· 1463 1507 if (ret) 1464 1508 goto out_unlock; 1465 1509 } 1510 + extent_clear_unlock_delalloc(inode, orig_start, end, locked_folio, &cached, 1511 + EXTENT_LOCKED | EXTENT_DELALLOC, page_ops); 1466 1512 done: 1467 1513 if (done_offset) 1468 1514 *done_offset = end; ··· 1485 1527 * We process each region below. 1486 1528 */ 1487 1529 1488 - clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | 1489 - EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; 1490 - page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; 1491 - 1492 1530 /* 1493 1531 * For the range (1). We have already instantiated the ordered extents 1494 1532 * for this region. They are cleaned up by 1495 1533 * btrfs_cleanup_ordered_extents() in e.g, 1496 - * btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are 1497 - * already cleared in the above loop. And, EXTENT_DELALLOC_NEW | 1498 - * EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup 1499 - * function. 1534 + * btrfs_run_delalloc_range(). 1535 + * EXTENT_DELALLOC_NEW | EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV 1536 + * are also handled by the cleanup function. 1500 1537 * 1501 - * However, in case of @keep_locked, we still need to unlock the pages 1502 - * (except @locked_folio) to ensure all the pages are unlocked. 1538 + * So here we only clear EXTENT_LOCKED and EXTENT_DELALLOC flag, and 1539 + * finish the writeback of the involved folios, which will be never submitted. 1503 1540 */ 1504 - if (keep_locked && orig_start < start) { 1541 + if (orig_start < start) { 1542 + clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC; 1543 + page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; 1544 + 1505 1545 if (!locked_folio) 1506 1546 mapping_set_error(inode->vfs_inode.i_mapping, ret); 1507 1547 extent_clear_unlock_delalloc(inode, orig_start, start - 1, 1508 - locked_folio, NULL, 0, page_ops); 1548 + locked_folio, NULL, clear_bits, page_ops); 1509 1549 } 1510 1550 1511 - /* 1512 - * At this point we're unlocked, we want to make sure we're only 1513 - * clearing these flags under the extent lock, so lock the rest of the 1514 - * range and clear everything up. 1515 - */ 1516 - lock_extent(&inode->io_tree, start, end, NULL); 1551 + clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW | 1552 + EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV; 1553 + page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK; 1517 1554 1518 1555 /* 1519 1556 * For the range (2). If we reserved an extent for our delalloc range ··· 1542 1589 btrfs_qgroup_free_data(inode, NULL, start + cur_alloc_size, 1543 1590 end - start - cur_alloc_size + 1, NULL); 1544 1591 } 1592 + btrfs_err_rl(fs_info, 1593 + "%s failed, root=%llu inode=%llu start=%llu len=%llu: %d", 1594 + __func__, btrfs_root_id(inode->root), 1595 + btrfs_ino(inode), orig_start, end + 1 - orig_start, ret); 1545 1596 return ret; 1546 1597 } 1547 1598 ··· 1766 1809 bytes = range_bytes; 1767 1810 1768 1811 spin_lock(&sinfo->lock); 1769 - btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes); 1812 + btrfs_space_info_update_bytes_may_use(sinfo, bytes); 1770 1813 spin_unlock(&sinfo->lock); 1771 1814 1772 1815 if (count > 0) ··· 1794 1837 /* End file offset (inclusive) of the range we want to NOCOW. */ 1795 1838 u64 end; 1796 1839 bool writeback_path; 1797 - bool strict; 1798 1840 /* 1799 1841 * Free the path passed to can_nocow_file_extent() once it's not needed 1800 1842 * anymore. ··· 1848 1892 * for its subvolume was created, then this implies the extent is shared, 1849 1893 * hence we must COW. 1850 1894 */ 1851 - if (!args->strict && 1852 - btrfs_file_extent_generation(leaf, fi) <= 1895 + if (btrfs_file_extent_generation(leaf, fi) <= 1853 1896 btrfs_root_last_snapshot(&root->root_item)) 1854 1897 goto out; 1855 1898 ··· 1877 1922 */ 1878 1923 btrfs_release_path(path); 1879 1924 1880 - ret = btrfs_cross_ref_exist(root, btrfs_ino(inode), 1881 - key->offset - args->file_extent.offset, 1882 - args->file_extent.disk_bytenr, args->strict, path); 1925 + ret = btrfs_cross_ref_exist(inode, key->offset - args->file_extent.offset, 1926 + args->file_extent.disk_bytenr, path); 1883 1927 WARN_ON_ONCE(ret > 0 && is_freespace_inode); 1884 1928 if (ret != 0) 1885 1929 goto out; ··· 1925 1971 } 1926 1972 1927 1973 /* 1974 + * Cleanup the dirty folios which will never be submitted due to error. 1975 + * 1976 + * When running a delalloc range, we may need to split the ranges (due to 1977 + * fragmentation or NOCOW). If we hit an error in the later part, we will error 1978 + * out and previously successfully executed range will never be submitted, thus 1979 + * we have to cleanup those folios by clearing their dirty flag, starting and 1980 + * finishing the writeback. 1981 + */ 1982 + static void cleanup_dirty_folios(struct btrfs_inode *inode, 1983 + struct folio *locked_folio, 1984 + u64 start, u64 end, int error) 1985 + { 1986 + struct btrfs_fs_info *fs_info = inode->root->fs_info; 1987 + struct address_space *mapping = inode->vfs_inode.i_mapping; 1988 + pgoff_t start_index = start >> PAGE_SHIFT; 1989 + pgoff_t end_index = end >> PAGE_SHIFT; 1990 + u32 len; 1991 + 1992 + ASSERT(end + 1 - start < U32_MAX); 1993 + ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && 1994 + IS_ALIGNED(end + 1, fs_info->sectorsize)); 1995 + len = end + 1 - start; 1996 + 1997 + /* 1998 + * Handle the locked folio first. 1999 + * The btrfs_folio_clamp_*() helpers can handle range out of the folio case. 2000 + */ 2001 + btrfs_folio_clamp_finish_io(fs_info, locked_folio, start, len); 2002 + 2003 + for (pgoff_t index = start_index; index <= end_index; index++) { 2004 + struct folio *folio; 2005 + 2006 + /* Already handled at the beginning. */ 2007 + if (index == locked_folio->index) 2008 + continue; 2009 + folio = __filemap_get_folio(mapping, index, FGP_LOCK, GFP_NOFS); 2010 + /* Cache already dropped, no need to do any cleanup. */ 2011 + if (IS_ERR(folio)) 2012 + continue; 2013 + btrfs_folio_clamp_finish_io(fs_info, locked_folio, start, len); 2014 + folio_unlock(folio); 2015 + folio_put(folio); 2016 + } 2017 + mapping_set_error(mapping, error); 2018 + } 2019 + 2020 + /* 1928 2021 * when nowcow writeback call back. This checks for snapshots or COW copies 1929 2022 * of the extents that exist in the file, and COWs the file as required. 1930 2023 * ··· 1986 1985 struct btrfs_root *root = inode->root; 1987 1986 struct btrfs_path *path; 1988 1987 u64 cow_start = (u64)-1; 1988 + /* 1989 + * If not 0, represents the inclusive end of the last fallback_to_cow() 1990 + * range. Only for error handling. 1991 + */ 1992 + u64 cow_end = 0; 1989 1993 u64 cur_offset = start; 1990 1994 int ret; 1991 1995 bool check_prev = true; ··· 2151 2145 found_key.offset - 1); 2152 2146 cow_start = (u64)-1; 2153 2147 if (ret) { 2148 + cow_end = found_key.offset - 1; 2154 2149 btrfs_dec_nocow_writers(nocow_bg); 2155 2150 goto error; 2156 2151 } ··· 2225 2218 cow_start = cur_offset; 2226 2219 2227 2220 if (cow_start != (u64)-1) { 2228 - cur_offset = end; 2229 2221 ret = fallback_to_cow(inode, locked_folio, cow_start, end); 2230 2222 cow_start = (u64)-1; 2231 - if (ret) 2223 + if (ret) { 2224 + cow_end = end; 2232 2225 goto error; 2226 + } 2233 2227 } 2234 2228 2235 2229 btrfs_free_path(path); ··· 2238 2230 2239 2231 error: 2240 2232 /* 2241 - * If an error happened while a COW region is outstanding, cur_offset 2242 - * needs to be reset to cow_start to ensure the COW region is unlocked 2243 - * as well. 2233 + * There are several error cases: 2234 + * 2235 + * 1) Failed without falling back to COW 2236 + * start cur_offset end 2237 + * |/////////////| | 2238 + * 2239 + * For range [start, cur_offset) the folios are already unlocked (except 2240 + * @locked_folio), EXTENT_DELALLOC already removed. 2241 + * Only need to clear the dirty flag as they will never be submitted. 2242 + * Ordered extent and extent maps are handled by 2243 + * btrfs_mark_ordered_io_finished() inside run_delalloc_range(). 2244 + * 2245 + * 2) Failed with error from fallback_to_cow() 2246 + * start cur_offset cow_end end 2247 + * |/////////////|-----------| | 2248 + * 2249 + * For range [start, cur_offset) it's the same as case 1). 2250 + * But for range [cur_offset, cow_end), the folios have dirty flag 2251 + * cleared and unlocked, EXTENT_DEALLLOC cleared by cow_file_range(). 2252 + * 2253 + * Thus we should not call extent_clear_unlock_delalloc() on range 2254 + * [cur_offset, cow_end), as the folios are already unlocked. 2255 + * 2256 + * So clear the folio dirty flags for [start, cur_offset) first. 2244 2257 */ 2245 - if (cow_start != (u64)-1) 2246 - cur_offset = cow_start; 2258 + if (cur_offset > start) 2259 + cleanup_dirty_folios(inode, locked_folio, start, cur_offset - 1, ret); 2260 + 2261 + /* 2262 + * If an error happened while a COW region is outstanding, cur_offset 2263 + * needs to be reset to @cow_end + 1 to skip the COW range, as 2264 + * cow_file_range() will do the proper cleanup at error. 2265 + */ 2266 + if (cow_end) 2267 + cur_offset = cow_end + 1; 2247 2268 2248 2269 /* 2249 2270 * We need to lock the extent here because we're clearing DELALLOC and ··· 2292 2255 btrfs_qgroup_free_data(inode, NULL, cur_offset, end - cur_offset + 1, NULL); 2293 2256 } 2294 2257 btrfs_free_path(path); 2258 + btrfs_err_rl(fs_info, 2259 + "%s failed, root=%llu inode=%llu start=%llu len=%llu: %d", 2260 + __func__, btrfs_root_id(inode->root), 2261 + btrfs_ino(inode), start, end + 1 - start, ret); 2295 2262 return ret; 2296 2263 } 2297 2264 ··· 2346 2305 2347 2306 out: 2348 2307 if (ret < 0) 2349 - btrfs_cleanup_ordered_extents(inode, locked_folio, start, 2350 - end - start + 1); 2308 + btrfs_cleanup_ordered_extents(inode, start, end - start + 1); 2351 2309 return ret; 2352 2310 } 2353 2311 ··· 2961 2921 btrfs_item_ptr_offset(leaf, path->slots[0]), 2962 2922 sizeof(struct btrfs_file_extent_item)); 2963 2923 2964 - btrfs_mark_buffer_dirty(trans, leaf); 2965 2924 btrfs_release_path(path); 2966 2925 2967 2926 /* ··· 4124 4085 struct btrfs_inode_item); 4125 4086 4126 4087 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); 4127 - btrfs_mark_buffer_dirty(trans, leaf); 4128 4088 btrfs_set_inode_last_trans(trans, inode); 4129 4089 ret = 0; 4130 4090 failed: ··· 6418 6380 } 6419 6381 } 6420 6382 6421 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 6422 6383 /* 6423 6384 * We don't need the path anymore, plus inheriting properties, adding 6424 6385 * ACLs, security xattrs, orphan item or adding the link, will result in ··· 7048 7011 * @orig_start: (optional) Return the original file offset of the file extent 7049 7012 * @orig_len: (optional) Return the original on-disk length of the file extent 7050 7013 * @ram_bytes: (optional) Return the ram_bytes of the file extent 7051 - * @strict: if true, omit optimizations that might force us into unnecessary 7052 - * cow. e.g., don't trust generation number. 7053 7014 * 7054 7015 * Return: 7055 7016 * >0 and update @len if we can do nocow write ··· 7059 7024 */ 7060 7025 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, 7061 7026 struct btrfs_file_extent *file_extent, 7062 - bool nowait, bool strict) 7027 + bool nowait) 7063 7028 { 7064 7029 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); 7065 7030 struct can_nocow_file_extent_args nocow_args = { 0 }; ··· 7112 7077 7113 7078 nocow_args.start = offset; 7114 7079 nocow_args.end = offset + *len - 1; 7115 - nocow_args.strict = strict; 7116 7080 nocow_args.free_path = true; 7117 7081 7118 7082 ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args); ··· 8061 8027 /* src is a subvolume */ 8062 8028 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { 8063 8029 ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry); 8030 + if (ret) { 8031 + btrfs_abort_transaction(trans, ret); 8032 + goto out_fail; 8033 + } 8064 8034 } else { /* src is an inode */ 8065 8035 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), 8066 8036 BTRFS_I(old_dentry->d_inode), 8067 8037 old_name, &old_rename_ctx); 8068 - if (!ret) 8069 - ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); 8070 - } 8071 - if (ret) { 8072 - btrfs_abort_transaction(trans, ret); 8073 - goto out_fail; 8038 + if (ret) { 8039 + btrfs_abort_transaction(trans, ret); 8040 + goto out_fail; 8041 + } 8042 + ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); 8043 + if (ret) { 8044 + btrfs_abort_transaction(trans, ret); 8045 + goto out_fail; 8046 + } 8074 8047 } 8075 8048 8076 8049 /* dest is a subvolume */ 8077 8050 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { 8078 8051 ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry); 8052 + if (ret) { 8053 + btrfs_abort_transaction(trans, ret); 8054 + goto out_fail; 8055 + } 8079 8056 } else { /* dest is an inode */ 8080 8057 ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir), 8081 8058 BTRFS_I(new_dentry->d_inode), 8082 8059 new_name, &new_rename_ctx); 8083 - if (!ret) 8084 - ret = btrfs_update_inode(trans, BTRFS_I(new_inode)); 8085 - } 8086 - if (ret) { 8087 - btrfs_abort_transaction(trans, ret); 8088 - goto out_fail; 8060 + if (ret) { 8061 + btrfs_abort_transaction(trans, ret); 8062 + goto out_fail; 8063 + } 8064 + ret = btrfs_update_inode(trans, BTRFS_I(new_inode)); 8065 + if (ret) { 8066 + btrfs_abort_transaction(trans, ret); 8067 + goto out_fail; 8068 + } 8089 8069 } 8090 8070 8091 8071 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode), ··· 8335 8287 8336 8288 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { 8337 8289 ret = btrfs_unlink_subvol(trans, BTRFS_I(old_dir), old_dentry); 8290 + if (ret) { 8291 + btrfs_abort_transaction(trans, ret); 8292 + goto out_fail; 8293 + } 8338 8294 } else { 8339 8295 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir), 8340 8296 BTRFS_I(d_inode(old_dentry)), 8341 8297 &old_fname.disk_name, &rename_ctx); 8342 - if (!ret) 8343 - ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); 8344 - } 8345 - if (ret) { 8346 - btrfs_abort_transaction(trans, ret); 8347 - goto out_fail; 8298 + if (ret) { 8299 + btrfs_abort_transaction(trans, ret); 8300 + goto out_fail; 8301 + } 8302 + ret = btrfs_update_inode(trans, BTRFS_I(old_inode)); 8303 + if (ret) { 8304 + btrfs_abort_transaction(trans, ret); 8305 + goto out_fail; 8306 + } 8348 8307 } 8349 8308 8350 8309 if (new_inode) { ··· 8359 8304 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) == 8360 8305 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 8361 8306 ret = btrfs_unlink_subvol(trans, BTRFS_I(new_dir), new_dentry); 8307 + if (ret) { 8308 + btrfs_abort_transaction(trans, ret); 8309 + goto out_fail; 8310 + } 8362 8311 BUG_ON(new_inode->i_nlink == 0); 8363 8312 } else { 8364 8313 ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir), 8365 8314 BTRFS_I(d_inode(new_dentry)), 8366 8315 &new_fname.disk_name); 8316 + if (ret) { 8317 + btrfs_abort_transaction(trans, ret); 8318 + goto out_fail; 8319 + } 8367 8320 } 8368 - if (!ret && new_inode->i_nlink == 0) 8321 + if (new_inode->i_nlink == 0) { 8369 8322 ret = btrfs_orphan_add(trans, 8370 8323 BTRFS_I(d_inode(new_dentry))); 8371 - if (ret) { 8372 - btrfs_abort_transaction(trans, ret); 8373 - goto out_fail; 8324 + if (ret) { 8325 + btrfs_abort_transaction(trans, ret); 8326 + goto out_fail; 8327 + } 8374 8328 } 8375 8329 } 8376 8330 ··· 8719 8655 8720 8656 ptr = btrfs_file_extent_inline_start(ei); 8721 8657 write_extent_buffer(leaf, symname, ptr, name_len); 8722 - btrfs_mark_buffer_dirty(trans, leaf); 8723 8658 btrfs_free_path(path); 8724 8659 8725 8660 d_instantiate_new(dentry, inode);
+130 -92
fs/btrfs/ioctl.c
··· 403 403 return ret; 404 404 } 405 405 406 - /* 407 - * Start exclusive operation @type, return true on success 408 - */ 409 - bool btrfs_exclop_start(struct btrfs_fs_info *fs_info, 410 - enum btrfs_exclusive_operation type) 411 - { 412 - bool ret = false; 413 - 414 - spin_lock(&fs_info->super_lock); 415 - if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) { 416 - fs_info->exclusive_operation = type; 417 - ret = true; 418 - } 419 - spin_unlock(&fs_info->super_lock); 420 - 421 - return ret; 422 - } 423 - 424 - /* 425 - * Conditionally allow to enter the exclusive operation in case it's compatible 426 - * with the running one. This must be paired with btrfs_exclop_start_unlock and 427 - * btrfs_exclop_finish. 428 - * 429 - * Compatibility: 430 - * - the same type is already running 431 - * - when trying to add a device and balance has been paused 432 - * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller 433 - * must check the condition first that would allow none -> @type 434 - */ 435 - bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info, 436 - enum btrfs_exclusive_operation type) 437 - { 438 - spin_lock(&fs_info->super_lock); 439 - if (fs_info->exclusive_operation == type || 440 - (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED && 441 - type == BTRFS_EXCLOP_DEV_ADD)) 442 - return true; 443 - 444 - spin_unlock(&fs_info->super_lock); 445 - return false; 446 - } 447 - 448 - void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info) 449 - { 450 - spin_unlock(&fs_info->super_lock); 451 - } 452 - 453 - void btrfs_exclop_finish(struct btrfs_fs_info *fs_info) 454 - { 455 - spin_lock(&fs_info->super_lock); 456 - WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE); 457 - spin_unlock(&fs_info->super_lock); 458 - sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation"); 459 - } 460 - 461 - void btrfs_exclop_balance(struct btrfs_fs_info *fs_info, 462 - enum btrfs_exclusive_operation op) 463 - { 464 - switch (op) { 465 - case BTRFS_EXCLOP_BALANCE_PAUSED: 466 - spin_lock(&fs_info->super_lock); 467 - ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE || 468 - fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD || 469 - fs_info->exclusive_operation == BTRFS_EXCLOP_NONE || 470 - fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); 471 - fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED; 472 - spin_unlock(&fs_info->super_lock); 473 - break; 474 - case BTRFS_EXCLOP_BALANCE: 475 - spin_lock(&fs_info->super_lock); 476 - ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); 477 - fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; 478 - spin_unlock(&fs_info->super_lock); 479 - break; 480 - default: 481 - btrfs_warn(fs_info, 482 - "invalid exclop balance operation %d requested", op); 483 - } 484 - } 485 - 486 406 static int btrfs_ioctl_getversion(struct inode *inode, int __user *arg) 487 407 { 488 408 return put_user(inode->i_generation, arg); ··· 469 549 return -EFAULT; 470 550 471 551 return ret; 472 - } 473 - 474 - int __pure btrfs_is_empty_uuid(const u8 *uuid) 475 - { 476 - int i; 477 - 478 - for (i = 0; i < BTRFS_UUID_SIZE; i++) { 479 - if (uuid[i]) 480 - return 0; 481 - } 482 - return 1; 483 552 } 484 553 485 554 /* ··· 2916 3007 2917 3008 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 2918 3009 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 2919 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 2920 3010 btrfs_release_path(path); 2921 3011 2922 3012 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); ··· 4936 5028 return ret; 4937 5029 } 4938 5030 5031 + static int btrfs_uring_encoded_write(struct io_uring_cmd *cmd, unsigned int issue_flags) 5032 + { 5033 + loff_t pos; 5034 + struct kiocb kiocb; 5035 + struct file *file; 5036 + ssize_t ret; 5037 + void __user *sqe_addr; 5038 + struct btrfs_uring_encoded_data *data = io_uring_cmd_get_async_data(cmd)->op_data; 5039 + 5040 + if (!capable(CAP_SYS_ADMIN)) { 5041 + ret = -EPERM; 5042 + goto out_acct; 5043 + } 5044 + 5045 + file = cmd->file; 5046 + sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr)); 5047 + 5048 + if (!(file->f_mode & FMODE_WRITE)) { 5049 + ret = -EBADF; 5050 + goto out_acct; 5051 + } 5052 + 5053 + if (!data) { 5054 + data = kzalloc(sizeof(*data), GFP_NOFS); 5055 + if (!data) { 5056 + ret = -ENOMEM; 5057 + goto out_acct; 5058 + } 5059 + 5060 + io_uring_cmd_get_async_data(cmd)->op_data = data; 5061 + 5062 + if (issue_flags & IO_URING_F_COMPAT) { 5063 + #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 5064 + struct btrfs_ioctl_encoded_io_args_32 args32; 5065 + 5066 + if (copy_from_user(&args32, sqe_addr, sizeof(args32))) { 5067 + ret = -EFAULT; 5068 + goto out_acct; 5069 + } 5070 + data->args.iov = compat_ptr(args32.iov); 5071 + data->args.iovcnt = args32.iovcnt; 5072 + data->args.offset = args32.offset; 5073 + data->args.flags = args32.flags; 5074 + data->args.len = args32.len; 5075 + data->args.unencoded_len = args32.unencoded_len; 5076 + data->args.unencoded_offset = args32.unencoded_offset; 5077 + data->args.compression = args32.compression; 5078 + data->args.encryption = args32.encryption; 5079 + memcpy(data->args.reserved, args32.reserved, 5080 + sizeof(data->args.reserved)); 5081 + #else 5082 + ret = -ENOTTY; 5083 + goto out_acct; 5084 + #endif 5085 + } else { 5086 + if (copy_from_user(&data->args, sqe_addr, sizeof(data->args))) { 5087 + ret = -EFAULT; 5088 + goto out_acct; 5089 + } 5090 + } 5091 + 5092 + ret = -EINVAL; 5093 + if (data->args.flags != 0) 5094 + goto out_acct; 5095 + if (memchr_inv(data->args.reserved, 0, sizeof(data->args.reserved))) 5096 + goto out_acct; 5097 + if (data->args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE && 5098 + data->args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE) 5099 + goto out_acct; 5100 + if (data->args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES || 5101 + data->args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES) 5102 + goto out_acct; 5103 + if (data->args.unencoded_offset > data->args.unencoded_len) 5104 + goto out_acct; 5105 + if (data->args.len > data->args.unencoded_len - data->args.unencoded_offset) 5106 + goto out_acct; 5107 + 5108 + data->iov = data->iovstack; 5109 + ret = import_iovec(ITER_SOURCE, data->args.iov, data->args.iovcnt, 5110 + ARRAY_SIZE(data->iovstack), &data->iov, 5111 + &data->iter); 5112 + if (ret < 0) 5113 + goto out_acct; 5114 + 5115 + if (iov_iter_count(&data->iter) == 0) { 5116 + ret = 0; 5117 + goto out_iov; 5118 + } 5119 + } 5120 + 5121 + if (issue_flags & IO_URING_F_NONBLOCK) { 5122 + ret = -EAGAIN; 5123 + goto out_acct; 5124 + } 5125 + 5126 + pos = data->args.offset; 5127 + ret = rw_verify_area(WRITE, file, &pos, data->args.len); 5128 + if (ret < 0) 5129 + goto out_iov; 5130 + 5131 + init_sync_kiocb(&kiocb, file); 5132 + ret = kiocb_set_rw_flags(&kiocb, 0, WRITE); 5133 + if (ret) 5134 + goto out_iov; 5135 + kiocb.ki_pos = pos; 5136 + 5137 + file_start_write(file); 5138 + 5139 + ret = btrfs_do_write_iter(&kiocb, &data->iter, &data->args); 5140 + if (ret > 0) 5141 + fsnotify_modify(file); 5142 + 5143 + file_end_write(file); 5144 + out_iov: 5145 + kfree(data->iov); 5146 + out_acct: 5147 + if (ret > 0) 5148 + add_wchar(current, ret); 5149 + inc_syscw(current); 5150 + return ret; 5151 + } 5152 + 4939 5153 int btrfs_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) 4940 5154 { 4941 5155 switch (cmd->cmd_op) { ··· 5066 5036 case BTRFS_IOC_ENCODED_READ_32: 5067 5037 #endif 5068 5038 return btrfs_uring_encoded_read(cmd, issue_flags); 5039 + 5040 + case BTRFS_IOC_ENCODED_WRITE: 5041 + #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 5042 + case BTRFS_IOC_ENCODED_WRITE_32: 5043 + #endif 5044 + return btrfs_uring_encoded_write(cmd, issue_flags); 5069 5045 } 5070 5046 5071 5047 return -EINVAL; ··· 5344 5308 return fsverity_ioctl_enable(file, (const void __user *)argp); 5345 5309 case FS_IOC_MEASURE_VERITY: 5346 5310 return fsverity_ioctl_measure(file, argp); 5311 + case FS_IOC_READ_VERITY_METADATA: 5312 + return fsverity_ioctl_read_metadata(file, argp); 5347 5313 case BTRFS_IOC_ENCODED_READ: 5348 5314 return btrfs_ioctl_encoded_read(file, argp, false); 5349 5315 case BTRFS_IOC_ENCODED_WRITE:
-1
fs/btrfs/ioctl.h
··· 19 19 struct dentry *dentry, struct fileattr *fa); 20 20 int btrfs_ioctl_get_supported_features(void __user *arg); 21 21 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode); 22 - int __pure btrfs_is_empty_uuid(const u8 *uuid); 23 22 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 24 23 struct btrfs_ioctl_balance_args *bargs); 25 24 int btrfs_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags);
+5
fs/btrfs/locking.h
··· 199 199 { 200 200 lockdep_assert_held_write(&eb->lock); 201 201 } 202 + static inline void btrfs_assert_tree_read_locked(struct extent_buffer *eb) 203 + { 204 + lockdep_assert_held_read(&eb->lock); 205 + } 202 206 #else 203 207 static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { } 208 + static inline void btrfs_assert_tree_read_locked(struct extent_buffer *eb) { } 204 209 #endif 205 210 206 211 void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
+16 -23
fs/btrfs/qgroup.c
··· 673 673 key.offset = dst; 674 674 675 675 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0); 676 - 677 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 678 - 679 676 btrfs_free_path(path); 680 677 return ret; 681 678 } ··· 749 752 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0); 750 753 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0); 751 754 752 - btrfs_mark_buffer_dirty(trans, leaf); 753 - 754 755 btrfs_release_path(path); 755 756 756 757 key.type = BTRFS_QGROUP_LIMIT_KEY; ··· 765 770 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0); 766 771 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0); 767 772 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0); 768 - 769 - btrfs_mark_buffer_dirty(trans, leaf); 770 773 771 774 ret = 0; 772 775 out: ··· 852 859 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl); 853 860 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer); 854 861 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl); 855 - 856 - btrfs_mark_buffer_dirty(trans, l); 857 - 858 862 out: 859 863 btrfs_free_path(path); 860 864 return ret; ··· 895 905 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr); 896 906 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl); 897 907 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr); 898 - 899 - btrfs_mark_buffer_dirty(trans, l); 900 - 901 908 out: 902 909 btrfs_free_path(path); 903 910 return ret; ··· 934 947 btrfs_set_qgroup_status_generation(l, ptr, trans->transid); 935 948 btrfs_set_qgroup_status_rescan(l, ptr, 936 949 fs_info->qgroup_rescan_progress.objectid); 937 - 938 - btrfs_mark_buffer_dirty(trans, l); 939 - 940 950 out: 941 951 btrfs_free_path(path); 942 952 return ret; ··· 1113 1129 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags & 1114 1130 BTRFS_QGROUP_STATUS_FLAGS_MASK); 1115 1131 btrfs_set_qgroup_status_rescan(leaf, ptr, 0); 1116 - 1117 - btrfs_mark_buffer_dirty(trans, leaf); 1118 1132 1119 1133 key.objectid = 0; 1120 1134 key.type = BTRFS_ROOT_REF_KEY; ··· 1820 1838 * Thus its reserved space should all be zero, no matter if qgroup 1821 1839 * is consistent or the mode. 1822 1840 */ 1823 - WARN_ON(qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] || 1824 - qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] || 1825 - qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]); 1841 + if (qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] || 1842 + qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] || 1843 + qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]) { 1844 + WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 1845 + btrfs_warn_rl(fs_info, 1846 + "to be deleted qgroup %u/%llu has non-zero numbers, data %llu meta prealloc %llu meta pertrans %llu", 1847 + btrfs_qgroup_level(qgroup->qgroupid), 1848 + btrfs_qgroup_subvolid(qgroup->qgroupid), 1849 + qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA], 1850 + qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC], 1851 + qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]); 1852 + 1853 + } 1826 1854 /* 1827 1855 * The same for rfer/excl numbers, but that's only if our qgroup is 1828 1856 * consistent and if it's in regular qgroup mode. ··· 1841 1849 */ 1842 1850 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL && 1843 1851 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)) { 1844 - if (WARN_ON(qgroup->rfer || qgroup->excl || 1845 - qgroup->rfer_cmpr || qgroup->excl_cmpr)) { 1852 + if (qgroup->rfer || qgroup->excl || 1853 + qgroup->rfer_cmpr || qgroup->excl_cmpr) { 1854 + WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); 1846 1855 btrfs_warn_rl(fs_info, 1847 1856 "to be deleted qgroup %u/%llu has non-zero numbers, rfer %llu rfer_cmpr %llu excl %llu excl_cmpr %llu", 1848 1857 btrfs_qgroup_level(qgroup->qgroupid),
+130 -16
fs/btrfs/raid-stripe-tree.c
··· 13 13 #include "volumes.h" 14 14 #include "print-tree.h" 15 15 16 - static void btrfs_partially_delete_raid_extent(struct btrfs_trans_handle *trans, 16 + static int btrfs_partially_delete_raid_extent(struct btrfs_trans_handle *trans, 17 17 struct btrfs_path *path, 18 18 const struct btrfs_key *oldkey, 19 19 u64 newlen, u64 frontpad) 20 20 { 21 - struct btrfs_stripe_extent *extent; 21 + struct btrfs_root *stripe_root = trans->fs_info->stripe_root; 22 + struct btrfs_stripe_extent *extent, *newitem; 22 23 struct extent_buffer *leaf; 23 24 int slot; 24 25 size_t item_size; ··· 28 27 .type = BTRFS_RAID_STRIPE_KEY, 29 28 .offset = newlen, 30 29 }; 30 + int ret; 31 31 32 + ASSERT(newlen > 0); 32 33 ASSERT(oldkey->type == BTRFS_RAID_STRIPE_KEY); 33 34 34 35 leaf = path->nodes[0]; 35 36 slot = path->slots[0]; 36 37 item_size = btrfs_item_size(leaf, slot); 38 + 39 + newitem = kzalloc(item_size, GFP_NOFS); 40 + if (!newitem) 41 + return -ENOMEM; 42 + 37 43 extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent); 38 44 39 45 for (int i = 0; i < btrfs_num_raid_stripes(item_size); i++) { 40 46 struct btrfs_raid_stride *stride = &extent->strides[i]; 41 47 u64 phys; 42 48 43 - phys = btrfs_raid_stride_physical(leaf, stride); 44 - btrfs_set_raid_stride_physical(leaf, stride, phys + frontpad); 49 + phys = btrfs_raid_stride_physical(leaf, stride) + frontpad; 50 + btrfs_set_stack_raid_stride_physical(&newitem->strides[i], phys); 45 51 } 46 52 47 - btrfs_set_item_key_safe(trans, path, &newkey); 53 + ret = btrfs_del_item(trans, stripe_root, path); 54 + if (ret) 55 + goto out; 56 + 57 + btrfs_release_path(path); 58 + ret = btrfs_insert_item(trans, stripe_root, &newkey, newitem, item_size); 59 + 60 + out: 61 + kfree(newitem); 62 + return ret; 48 63 } 49 64 50 65 int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length) ··· 76 59 int slot; 77 60 int ret; 78 61 79 - if (!stripe_root) 62 + if (!btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE) || !stripe_root) 80 63 return 0; 64 + 65 + if (!btrfs_is_testing(fs_info)) { 66 + struct btrfs_chunk_map *map; 67 + bool use_rst; 68 + 69 + map = btrfs_find_chunk_map(fs_info, start, length); 70 + if (!map) 71 + return -EINVAL; 72 + use_rst = btrfs_need_stripe_tree_update(fs_info, map->type); 73 + btrfs_free_chunk_map(map); 74 + if (!use_rst) 75 + return 0; 76 + } 81 77 82 78 path = btrfs_alloc_path(); 83 79 if (!path) ··· 115 85 found_end = found_start + key.offset; 116 86 ret = 0; 117 87 88 + /* 89 + * The stripe extent starts before the range we want to delete, 90 + * but the range spans more than one stripe extent: 91 + * 92 + * |--- RAID Stripe Extent ---||--- RAID Stripe Extent ---| 93 + * |--- keep ---|--- drop ---| 94 + * 95 + * This means we have to get the previous item, truncate its 96 + * length and then restart the search. 97 + */ 98 + if (found_start > start) { 99 + if (slot == 0) { 100 + ret = btrfs_previous_item(stripe_root, path, start, 101 + BTRFS_RAID_STRIPE_KEY); 102 + if (ret) { 103 + if (ret > 0) 104 + ret = -ENOENT; 105 + break; 106 + } 107 + } else { 108 + path->slots[0]--; 109 + } 110 + 111 + leaf = path->nodes[0]; 112 + slot = path->slots[0]; 113 + btrfs_item_key_to_cpu(leaf, &key, slot); 114 + found_start = key.objectid; 115 + found_end = found_start + key.offset; 116 + ASSERT(found_start <= start); 117 + } 118 + 118 119 if (key.type != BTRFS_RAID_STRIPE_KEY) 119 120 break; 120 121 ··· 157 96 found_start, found_end); 158 97 159 98 /* 99 + * The stripe extent starts before the range we want to delete 100 + * and ends after the range we want to delete, i.e. we're 101 + * punching a hole in the stripe extent: 102 + * 103 + * |--- RAID Stripe Extent ---| 104 + * | keep |--- drop ---| keep | 105 + * 106 + * This means we need to a) truncate the existing item and b) 107 + * create a second item for the remaining range. 108 + */ 109 + if (found_start < start && found_end > end) { 110 + size_t item_size; 111 + u64 diff_start = start - found_start; 112 + u64 diff_end = found_end - end; 113 + struct btrfs_stripe_extent *extent; 114 + struct btrfs_key newkey = { 115 + .objectid = end, 116 + .type = BTRFS_RAID_STRIPE_KEY, 117 + .offset = diff_end, 118 + }; 119 + 120 + /* The "right" item. */ 121 + ret = btrfs_duplicate_item(trans, stripe_root, path, &newkey); 122 + if (ret) 123 + break; 124 + 125 + item_size = btrfs_item_size(leaf, path->slots[0]); 126 + extent = btrfs_item_ptr(leaf, path->slots[0], 127 + struct btrfs_stripe_extent); 128 + 129 + for (int i = 0; i < btrfs_num_raid_stripes(item_size); i++) { 130 + struct btrfs_raid_stride *stride = &extent->strides[i]; 131 + u64 phys; 132 + 133 + phys = btrfs_raid_stride_physical(leaf, stride); 134 + phys += diff_start + length; 135 + btrfs_set_raid_stride_physical(leaf, stride, phys); 136 + } 137 + 138 + /* The "left" item. */ 139 + path->slots[0]--; 140 + btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 141 + btrfs_partially_delete_raid_extent(trans, path, &key, 142 + diff_start, 0); 143 + break; 144 + } 145 + 146 + /* 160 147 * The stripe extent starts before the range we want to delete: 161 148 * 162 149 * |--- RAID Stripe Extent ---| ··· 214 105 * length to the new size and then re-insert the item. 215 106 */ 216 107 if (found_start < start) { 217 - u64 diff = start - found_start; 108 + u64 diff_start = start - found_start; 218 109 219 110 btrfs_partially_delete_raid_extent(trans, path, &key, 220 - diff, 0); 221 - break; 111 + diff_start, 0); 112 + 113 + start += (key.offset - diff_start); 114 + length -= (key.offset - diff_start); 115 + if (length == 0) 116 + break; 117 + 118 + btrfs_release_path(path); 119 + continue; 222 120 } 223 121 224 122 /* ··· 238 122 * length to the new size and then re-insert the item. 239 123 */ 240 124 if (found_end > end) { 241 - u64 diff = found_end - end; 125 + u64 diff_end = found_end - end; 242 126 243 127 btrfs_partially_delete_raid_extent(trans, path, &key, 244 - diff, diff); 128 + key.offset - length, 129 + length); 130 + ASSERT(key.offset - diff_end == length); 245 131 break; 246 132 } 247 133 134 + /* Finally we can delete the whole item, no more special cases. */ 248 135 ret = btrfs_del_item(trans, stripe_root, path); 249 136 if (ret) 250 137 break; ··· 288 169 289 170 write_extent_buffer(leaf, stripe_extent, btrfs_item_ptr_offset(leaf, slot), 290 171 item_size); 291 - btrfs_mark_buffer_dirty(trans, leaf); 292 172 btrfs_free_path(path); 293 173 294 174 return ret; ··· 317 199 for (int i = 0; i < num_stripes; i++) { 318 200 u64 devid = bioc->stripes[i].dev->devid; 319 201 u64 physical = bioc->stripes[i].physical; 320 - u64 length = bioc->stripes[i].length; 321 202 struct btrfs_raid_stride *raid_stride = &stripe_extent->strides[i]; 322 - 323 - if (length == 0) 324 - length = bioc->size; 325 203 326 204 btrfs_set_stack_raid_stride_devid(raid_stride, devid); 327 205 btrfs_set_stack_raid_stride_physical(raid_stride, physical);
+149 -220
fs/btrfs/relocation.c
··· 342 342 if (cur == node) 343 343 ret = true; 344 344 345 - /* The node is the lowest node */ 346 - if (cur->lowest) { 347 - list_del_init(&cur->lower); 348 - cur->lowest = 0; 349 - } 350 - 351 345 /* Cleanup the lower edges */ 352 346 while (!list_empty(&cur->lower)) { 353 347 struct btrfs_backref_edge *edge; ··· 367 373 * cache to avoid unnecessary backref lookup. 368 374 */ 369 375 if (cur->level > 0) { 370 - list_add(&cur->list, &cache->detached); 371 376 cur->detached = 1; 372 377 } else { 373 378 rb_erase(&cur->rb_node, &cache->rb_root); ··· 419 426 goto out; 420 427 } 421 428 422 - node->lowest = 1; 423 429 cur = node; 424 430 425 431 /* Breadth-first search to build backref cache */ ··· 459 467 ASSERT(list_empty(&cache->useless_node) && 460 468 list_empty(&cache->pending_edge)); 461 469 return node; 462 - } 463 - 464 - /* 465 - * helper to add backref node for the newly created snapshot. 466 - * the backref node is created by cloning backref node that 467 - * corresponds to root of source tree 468 - */ 469 - static int clone_backref_node(struct btrfs_trans_handle *trans, 470 - struct reloc_control *rc, 471 - const struct btrfs_root *src, 472 - struct btrfs_root *dest) 473 - { 474 - struct btrfs_root *reloc_root = src->reloc_root; 475 - struct btrfs_backref_cache *cache = &rc->backref_cache; 476 - struct btrfs_backref_node *node = NULL; 477 - struct btrfs_backref_node *new_node; 478 - struct btrfs_backref_edge *edge; 479 - struct btrfs_backref_edge *new_edge; 480 - struct rb_node *rb_node; 481 - 482 - rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start); 483 - if (rb_node) { 484 - node = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 485 - if (node->detached) 486 - node = NULL; 487 - else 488 - BUG_ON(node->new_bytenr != reloc_root->node->start); 489 - } 490 - 491 - if (!node) { 492 - rb_node = rb_simple_search(&cache->rb_root, 493 - reloc_root->commit_root->start); 494 - if (rb_node) { 495 - node = rb_entry(rb_node, struct btrfs_backref_node, 496 - rb_node); 497 - BUG_ON(node->detached); 498 - } 499 - } 500 - 501 - if (!node) 502 - return 0; 503 - 504 - new_node = btrfs_backref_alloc_node(cache, dest->node->start, 505 - node->level); 506 - if (!new_node) 507 - return -ENOMEM; 508 - 509 - new_node->lowest = node->lowest; 510 - new_node->checked = 1; 511 - new_node->root = btrfs_grab_root(dest); 512 - ASSERT(new_node->root); 513 - 514 - if (!node->lowest) { 515 - list_for_each_entry(edge, &node->lower, list[UPPER]) { 516 - new_edge = btrfs_backref_alloc_edge(cache); 517 - if (!new_edge) 518 - goto fail; 519 - 520 - btrfs_backref_link_edge(new_edge, edge->node[LOWER], 521 - new_node, LINK_UPPER); 522 - } 523 - } else { 524 - list_add_tail(&new_node->lower, &cache->leaves); 525 - } 526 - 527 - rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr, 528 - &new_node->rb_node); 529 - if (rb_node) 530 - btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST); 531 - 532 - if (!new_node->lowest) { 533 - list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) { 534 - list_add_tail(&new_edge->list[LOWER], 535 - &new_edge->node[LOWER]->upper); 536 - } 537 - } 538 - return 0; 539 - fail: 540 - while (!list_empty(&new_node->lower)) { 541 - new_edge = list_entry(new_node->lower.next, 542 - struct btrfs_backref_edge, list[UPPER]); 543 - list_del(&new_edge->list[UPPER]); 544 - btrfs_backref_free_edge(cache, new_edge); 545 - } 546 - btrfs_backref_free_node(cache, new_node); 547 - return -ENOMEM; 548 470 } 549 471 550 472 /* ··· 856 950 u32 i; 857 951 int ret = 0; 858 952 int first = 1; 859 - int dirty = 0; 860 953 861 954 if (rc->stage != UPDATE_DATA_PTRS) 862 955 return 0; ··· 935 1030 } 936 1031 937 1032 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); 938 - dirty = 1; 939 1033 940 1034 key.offset -= btrfs_file_extent_offset(leaf, fi); 941 1035 ref.action = BTRFS_ADD_DELAYED_REF; ··· 965 1061 break; 966 1062 } 967 1063 } 968 - if (dirty) 969 - btrfs_mark_buffer_dirty(trans, leaf); 970 1064 if (inode) 971 1065 btrfs_add_delayed_iput(inode); 972 1066 return ret; ··· 1157 1255 */ 1158 1256 btrfs_set_node_blockptr(parent, slot, new_bytenr); 1159 1257 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen); 1160 - btrfs_mark_buffer_dirty(trans, parent); 1161 1258 1162 1259 btrfs_set_node_blockptr(path->nodes[level], 1163 1260 path->slots[level], old_bytenr); 1164 1261 btrfs_set_node_ptr_generation(path->nodes[level], 1165 1262 path->slots[level], old_ptr_gen); 1166 - btrfs_mark_buffer_dirty(trans, path->nodes[level]); 1167 1263 1168 1264 ref.action = BTRFS_ADD_DELAYED_REF; 1169 1265 ref.bytenr = old_bytenr; ··· 1958 2058 int index = 0; 1959 2059 int ret; 1960 2060 1961 - next = node; 1962 - while (1) { 1963 - cond_resched(); 1964 - next = walk_up_backref(next, edges, &index); 1965 - root = next->root; 2061 + next = walk_up_backref(node, edges, &index); 2062 + root = next->root; 1966 2063 1967 - /* 1968 - * If there is no root, then our references for this block are 1969 - * incomplete, as we should be able to walk all the way up to a 1970 - * block that is owned by a root. 1971 - * 1972 - * This path is only for SHAREABLE roots, so if we come upon a 1973 - * non-SHAREABLE root then we have backrefs that resolve 1974 - * improperly. 1975 - * 1976 - * Both of these cases indicate file system corruption, or a bug 1977 - * in the backref walking code. 1978 - */ 1979 - if (!root) { 1980 - ASSERT(0); 1981 - btrfs_err(trans->fs_info, 1982 - "bytenr %llu doesn't have a backref path ending in a root", 1983 - node->bytenr); 1984 - return ERR_PTR(-EUCLEAN); 1985 - } 1986 - if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { 1987 - ASSERT(0); 1988 - btrfs_err(trans->fs_info, 1989 - "bytenr %llu has multiple refs with one ending in a non-shareable root", 1990 - node->bytenr); 1991 - return ERR_PTR(-EUCLEAN); 1992 - } 2064 + /* 2065 + * If there is no root, then our references for this block are 2066 + * incomplete, as we should be able to walk all the way up to a block 2067 + * that is owned by a root. 2068 + * 2069 + * This path is only for SHAREABLE roots, so if we come upon a 2070 + * non-SHAREABLE root then we have backrefs that resolve improperly. 2071 + * 2072 + * Both of these cases indicate file system corruption, or a bug in the 2073 + * backref walking code. 2074 + */ 2075 + if (unlikely(!root)) { 2076 + btrfs_err(trans->fs_info, 2077 + "bytenr %llu doesn't have a backref path ending in a root", 2078 + node->bytenr); 2079 + return ERR_PTR(-EUCLEAN); 2080 + } 2081 + if (unlikely(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))) { 2082 + btrfs_err(trans->fs_info, 2083 + "bytenr %llu has multiple refs with one ending in a non-shareable root", 2084 + node->bytenr); 2085 + return ERR_PTR(-EUCLEAN); 2086 + } 1993 2087 1994 - if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) { 1995 - ret = record_reloc_root_in_trans(trans, root); 1996 - if (ret) 1997 - return ERR_PTR(ret); 1998 - break; 1999 - } 2000 - 2001 - ret = btrfs_record_root_in_trans(trans, root); 2088 + if (btrfs_root_id(root) == BTRFS_TREE_RELOC_OBJECTID) { 2089 + ret = record_reloc_root_in_trans(trans, root); 2002 2090 if (ret) 2003 2091 return ERR_PTR(ret); 2004 - root = root->reloc_root; 2005 - 2006 - /* 2007 - * We could have raced with another thread which failed, so 2008 - * root->reloc_root may not be set, return ENOENT in this case. 2009 - */ 2010 - if (!root) 2011 - return ERR_PTR(-ENOENT); 2012 - 2013 - if (next->new_bytenr != root->node->start) { 2014 - /* 2015 - * We just created the reloc root, so we shouldn't have 2016 - * ->new_bytenr set and this shouldn't be in the changed 2017 - * list. If it is then we have multiple roots pointing 2018 - * at the same bytenr which indicates corruption, or 2019 - * we've made a mistake in the backref walking code. 2020 - */ 2021 - ASSERT(next->new_bytenr == 0); 2022 - ASSERT(list_empty(&next->list)); 2023 - if (next->new_bytenr || !list_empty(&next->list)) { 2024 - btrfs_err(trans->fs_info, 2025 - "bytenr %llu possibly has multiple roots pointing at the same bytenr %llu", 2026 - node->bytenr, next->bytenr); 2027 - return ERR_PTR(-EUCLEAN); 2028 - } 2029 - 2030 - next->new_bytenr = root->node->start; 2031 - btrfs_put_root(next->root); 2032 - next->root = btrfs_grab_root(root); 2033 - ASSERT(next->root); 2034 - list_add_tail(&next->list, 2035 - &rc->backref_cache.changed); 2036 - mark_block_processed(rc, next); 2037 - break; 2038 - } 2039 - 2040 - WARN_ON(1); 2041 - root = NULL; 2042 - next = walk_down_backref(edges, &index); 2043 - if (!next || next->level <= node->level) 2044 - break; 2092 + goto found; 2045 2093 } 2046 - if (!root) { 2047 - /* 2048 - * This can happen if there's fs corruption or if there's a bug 2049 - * in the backref lookup code. 2050 - */ 2051 - ASSERT(0); 2094 + 2095 + ret = btrfs_record_root_in_trans(trans, root); 2096 + if (ret) 2097 + return ERR_PTR(ret); 2098 + root = root->reloc_root; 2099 + 2100 + /* 2101 + * We could have raced with another thread which failed, so 2102 + * root->reloc_root may not be set, return ENOENT in this case. 2103 + */ 2104 + if (!root) 2052 2105 return ERR_PTR(-ENOENT); 2106 + 2107 + if (next->new_bytenr) { 2108 + /* 2109 + * We just created the reloc root, so we shouldn't have 2110 + * ->new_bytenr set yet. If it is then we have multiple roots 2111 + * pointing at the same bytenr which indicates corruption, or 2112 + * we've made a mistake in the backref walking code. 2113 + */ 2114 + ASSERT(next->new_bytenr == 0); 2115 + btrfs_err(trans->fs_info, 2116 + "bytenr %llu possibly has multiple roots pointing at the same bytenr %llu", 2117 + node->bytenr, next->bytenr); 2118 + return ERR_PTR(-EUCLEAN); 2053 2119 } 2054 2120 2121 + next->new_bytenr = root->node->start; 2122 + btrfs_put_root(next->root); 2123 + next->root = btrfs_grab_root(root); 2124 + ASSERT(next->root); 2125 + mark_block_processed(rc, next); 2126 + found: 2055 2127 next = node; 2056 2128 /* setup backref node path for btrfs_reloc_cow_block */ 2057 2129 while (1) { ··· 2119 2247 return num_bytes; 2120 2248 } 2121 2249 2122 - static int reserve_metadata_space(struct btrfs_trans_handle *trans, 2123 - struct reloc_control *rc, 2124 - struct btrfs_backref_node *node) 2250 + static int refill_metadata_space(struct btrfs_trans_handle *trans, 2251 + struct reloc_control *rc, u64 num_bytes) 2125 2252 { 2126 - struct btrfs_root *root = rc->extent_root; 2127 - struct btrfs_fs_info *fs_info = root->fs_info; 2128 - u64 num_bytes; 2253 + struct btrfs_fs_info *fs_info = trans->fs_info; 2129 2254 int ret; 2130 - u64 tmp; 2131 - 2132 - num_bytes = calcu_metadata_size(rc, node) * 2; 2133 2255 2134 2256 trans->block_rsv = rc->block_rsv; 2135 2257 rc->reserved_bytes += num_bytes; ··· 2136 2270 ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, num_bytes, 2137 2271 BTRFS_RESERVE_FLUSH_LIMIT); 2138 2272 if (ret) { 2139 - tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES; 2273 + u64 tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES; 2274 + 2140 2275 while (tmp <= rc->reserved_bytes) 2141 2276 tmp <<= 1; 2142 2277 /* ··· 2153 2286 } 2154 2287 2155 2288 return 0; 2289 + } 2290 + 2291 + static int reserve_metadata_space(struct btrfs_trans_handle *trans, 2292 + struct reloc_control *rc, 2293 + struct btrfs_backref_node *node) 2294 + { 2295 + u64 num_bytes; 2296 + 2297 + num_bytes = calcu_metadata_size(rc, node) * 2; 2298 + return refill_metadata_space(trans, rc, num_bytes); 2156 2299 } 2157 2300 2158 2301 /* ··· 2319 2442 2320 2443 if (!ret && node->pending) { 2321 2444 btrfs_backref_drop_node_buffer(node); 2322 - list_move_tail(&node->list, &rc->backref_cache.changed); 2445 + list_del_init(&node->list); 2323 2446 node->pending = 0; 2324 2447 } 2325 2448 ··· 2482 2605 /* 2483 2606 * This block was the root block of a root, and this is 2484 2607 * the first time we're processing the block and thus it 2485 - * should not have had the ->new_bytenr modified and 2486 - * should have not been included on the changed list. 2608 + * should not have had the ->new_bytenr modified. 2487 2609 * 2488 2610 * However in the case of corruption we could have 2489 2611 * multiple refs pointing to the same block improperly, ··· 2492 2616 * normal user in the case of corruption. 2493 2617 */ 2494 2618 ASSERT(node->new_bytenr == 0); 2495 - ASSERT(list_empty(&node->list)); 2496 - if (node->new_bytenr || !list_empty(&node->list)) { 2619 + if (node->new_bytenr) { 2497 2620 btrfs_err(root->fs_info, 2498 2621 "bytenr %llu has improper references to it", 2499 2622 node->bytenr); ··· 2515 2640 btrfs_put_root(node->root); 2516 2641 node->root = btrfs_grab_root(root); 2517 2642 ASSERT(node->root); 2518 - list_add_tail(&node->list, &rc->backref_cache.changed); 2519 2643 } else { 2520 - path->lowest_level = node->level; 2521 - if (root == root->fs_info->chunk_root) 2522 - btrfs_reserve_chunk_metadata(trans, false); 2523 - ret = btrfs_search_slot(trans, root, key, path, 0, 1); 2524 - btrfs_release_path(path); 2525 - if (root == root->fs_info->chunk_root) 2526 - btrfs_trans_release_chunk_metadata(trans); 2527 - if (ret > 0) 2528 - ret = 0; 2644 + btrfs_err(root->fs_info, 2645 + "bytenr %llu resolved to a non-shareable root", 2646 + node->bytenr); 2647 + ret = -EUCLEAN; 2648 + goto out; 2529 2649 } 2530 2650 if (!ret) 2531 2651 update_processed_blocks(rc, node); ··· 2528 2658 ret = do_relocation(trans, rc, node, key, path, 1); 2529 2659 } 2530 2660 out: 2531 - if (ret || node->level == 0 || node->cowonly) 2661 + if (ret || node->level == 0) 2532 2662 btrfs_backref_cleanup_node(&rc->backref_cache, node); 2663 + return ret; 2664 + } 2665 + 2666 + static int relocate_cowonly_block(struct btrfs_trans_handle *trans, 2667 + struct reloc_control *rc, struct tree_block *block, 2668 + struct btrfs_path *path) 2669 + { 2670 + struct btrfs_fs_info *fs_info = trans->fs_info; 2671 + struct btrfs_root *root; 2672 + u64 num_bytes; 2673 + int nr_levels; 2674 + int ret; 2675 + 2676 + root = btrfs_get_fs_root(fs_info, block->owner, true); 2677 + if (IS_ERR(root)) 2678 + return PTR_ERR(root); 2679 + 2680 + nr_levels = max(btrfs_header_level(root->node) - block->level, 0) + 1; 2681 + 2682 + num_bytes = fs_info->nodesize * nr_levels; 2683 + ret = refill_metadata_space(trans, rc, num_bytes); 2684 + if (ret) { 2685 + btrfs_put_root(root); 2686 + return ret; 2687 + } 2688 + path->lowest_level = block->level; 2689 + if (root == root->fs_info->chunk_root) 2690 + btrfs_reserve_chunk_metadata(trans, false); 2691 + 2692 + ret = btrfs_search_slot(trans, root, &block->key, path, 0, 1); 2693 + path->lowest_level = 0; 2694 + btrfs_release_path(path); 2695 + 2696 + if (root == root->fs_info->chunk_root) 2697 + btrfs_trans_release_chunk_metadata(trans); 2698 + if (ret > 0) 2699 + ret = 0; 2700 + btrfs_put_root(root); 2701 + 2533 2702 return ret; 2534 2703 } 2535 2704 ··· 2611 2702 2612 2703 /* Do tree relocation */ 2613 2704 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { 2705 + /* 2706 + * For COWonly blocks, or the data reloc tree, we only need to 2707 + * COW down to the block, there's no need to generate a backref 2708 + * tree. 2709 + */ 2710 + if (block->owner && 2711 + (!is_fstree(block->owner) || 2712 + block->owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) { 2713 + ret = relocate_cowonly_block(trans, rc, block, path); 2714 + if (ret) 2715 + break; 2716 + continue; 2717 + } 2718 + 2614 2719 node = build_backref_tree(trans, rc, &block->key, 2615 2720 block->level, block->bytenr); 2616 2721 if (IS_ERR(node)) { ··· 2870 2947 2871 2948 /* 2872 2949 * We could have lost folio private when we dropped the lock to read the 2873 - * folio above, make sure we set_page_extent_mapped here so we have any 2950 + * folio above, make sure we set_folio_extent_mapped() here so we have any 2874 2951 * of the subpage blocksize stuff we need in place. 2875 2952 */ 2876 2953 ret = set_folio_extent_mapped(folio); ··· 3722 3799 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); 3723 3800 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS | 3724 3801 BTRFS_INODE_PREALLOC); 3725 - btrfs_mark_buffer_dirty(trans, leaf); 3726 3802 out: 3727 3803 btrfs_free_path(path); 3728 3804 return ret; ··· 4327 4405 WARN_ON(!first_cow && level == 0); 4328 4406 4329 4407 node = rc->backref_cache.path[level]; 4330 - BUG_ON(node->bytenr != buf->start && 4331 - node->new_bytenr != buf->start); 4408 + 4409 + /* 4410 + * If node->bytenr != buf->start and node->new_bytenr != 4411 + * buf->start then we've got the wrong backref node for what we 4412 + * expected to see here and the cache is incorrect. 4413 + */ 4414 + if (unlikely(node->bytenr != buf->start && node->new_bytenr != buf->start)) { 4415 + btrfs_err(fs_info, 4416 + "bytenr %llu was found but our backref cache was expecting %llu or %llu", 4417 + buf->start, node->bytenr, node->new_bytenr); 4418 + return -EUCLEAN; 4419 + } 4332 4420 4333 4421 btrfs_backref_drop_node_buffer(node); 4334 4422 atomic_inc(&cow->refs); ··· 4438 4506 return ret; 4439 4507 } 4440 4508 new_root->reloc_root = btrfs_grab_root(reloc_root); 4441 - 4442 - if (rc->create_reloc_tree) 4443 - ret = clone_backref_node(trans, rc, root, reloc_root); 4444 - return ret; 4509 + return 0; 4445 4510 } 4446 4511 4447 4512 /*
-2
fs/btrfs/root-tree.c
··· 197 197 btrfs_set_root_generation_v2(item, btrfs_root_generation(item)); 198 198 199 199 write_extent_buffer(l, item, ptr, sizeof(*item)); 200 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 201 200 out: 202 201 btrfs_free_path(path); 203 202 return ret; ··· 446 447 btrfs_set_root_ref_name_len(leaf, ref, name->len); 447 448 ptr = (unsigned long)(ref + 1); 448 449 write_extent_buffer(leaf, name->name, ptr, name->len); 449 - btrfs_mark_buffer_dirty(trans, leaf); 450 450 451 451 if (key.type == BTRFS_ROOT_BACKREF_KEY) { 452 452 btrfs_release_path(path);
+1 -2
fs/btrfs/send.c
··· 7259 7259 enum btrfs_compare_tree_result result, 7260 7260 struct send_ctx *sctx) 7261 7261 { 7262 - int ret = 0; 7262 + int ret; 7263 7263 7264 7264 /* 7265 7265 * We can not hold the commit root semaphore here. This is because in ··· 7319 7319 return 0; 7320 7320 } 7321 7321 result = BTRFS_COMPARE_TREE_CHANGED; 7322 - ret = 0; 7323 7322 } 7324 7323 7325 7324 sctx->left_path = left_path;
+59 -10
fs/btrfs/space-info.c
··· 14 14 #include "fs.h" 15 15 #include "accessors.h" 16 16 #include "extent-tree.h" 17 + #include "zoned.h" 17 18 18 19 /* 19 20 * HOW DOES SPACE RESERVATION WORK ··· 127 126 * to reclaim space, but we want to hold this until the end because COW can 128 127 * churn a lot and we can avoid making some extent tree modifications if we 129 128 * are able to delay for as long as possible. 129 + * 130 + * RESET_ZONES 131 + * This state works only for the zoned mode. On the zoned mode, we cannot 132 + * reuse once allocated then freed region until we reset the zone, due to 133 + * the sequential write zone requirement. The RESET_ZONES state resets the 134 + * zones of an unused block group and let us reuse the space. The reusing 135 + * is faster than removing the block group and allocating another block 136 + * group on the zones. 130 137 * 131 138 * ALLOC_CHUNK 132 139 * We will skip this the first time through space reservation, because of ··· 325 316 found->bytes_used += block_group->used; 326 317 found->disk_used += block_group->used * factor; 327 318 found->bytes_readonly += block_group->bytes_super; 328 - btrfs_space_info_update_bytes_zone_unusable(info, found, block_group->zone_unusable); 319 + btrfs_space_info_update_bytes_zone_unusable(found, block_group->zone_unusable); 329 320 if (block_group->length > 0) 330 321 found->full = 0; 331 322 btrfs_try_granting_tickets(info, found); ··· 498 489 if ((used + ticket->bytes <= space_info->total_bytes) || 499 490 btrfs_can_overcommit(fs_info, space_info, ticket->bytes, 500 491 flush)) { 501 - btrfs_space_info_update_bytes_may_use(fs_info, 502 - space_info, 503 - ticket->bytes); 492 + btrfs_space_info_update_bytes_may_use(space_info, ticket->bytes); 504 493 remove_ticket(space_info, ticket); 505 494 ticket->bytes = 0; 506 495 space_info->tickets_id++; ··· 841 834 */ 842 835 ret = btrfs_commit_current_transaction(root); 843 836 break; 837 + case RESET_ZONES: 838 + ret = btrfs_reset_unused_block_groups(space_info, num_bytes); 839 + break; 844 840 default: 845 841 ret = -ENOSPC; 846 842 break; ··· 1096 1086 enum btrfs_flush_state flush_state; 1097 1087 int commit_cycles = 0; 1098 1088 u64 last_tickets_id; 1089 + enum btrfs_flush_state final_state; 1099 1090 1100 1091 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); 1101 1092 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); 1093 + if (btrfs_is_zoned(fs_info)) 1094 + final_state = RESET_ZONES; 1095 + else 1096 + final_state = COMMIT_TRANS; 1102 1097 1103 1098 spin_lock(&space_info->lock); 1104 1099 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info); ··· 1156 1141 if (flush_state == ALLOC_CHUNK_FORCE && !commit_cycles) 1157 1142 flush_state++; 1158 1143 1159 - if (flush_state > COMMIT_TRANS) { 1144 + if (flush_state > final_state) { 1160 1145 commit_cycles++; 1161 1146 if (commit_cycles > 2) { 1162 1147 if (maybe_fail_all_tickets(fs_info, space_info)) { ··· 1170 1155 } 1171 1156 } 1172 1157 spin_unlock(&space_info->lock); 1173 - } while (flush_state <= COMMIT_TRANS); 1158 + } while (flush_state <= final_state); 1174 1159 } 1175 1160 1176 1161 /* ··· 1301 1286 * This is where we reclaim all of the pinned space generated by running the 1302 1287 * iputs 1303 1288 * 1289 + * RESET_ZONES 1290 + * This state works only for the zoned mode. We scan the unused block group 1291 + * list and reset the zones and reuse the block group. 1292 + * 1304 1293 * ALLOC_CHUNK_FORCE 1305 1294 * For data we start with alloc chunk force, however we could have been full 1306 1295 * before, and then the transaction commit could have freed new block groups, ··· 1314 1295 FLUSH_DELALLOC_FULL, 1315 1296 RUN_DELAYED_IPUTS, 1316 1297 COMMIT_TRANS, 1298 + RESET_ZONES, 1317 1299 ALLOC_CHUNK_FORCE, 1318 1300 }; 1319 1301 ··· 1406 1386 static const enum btrfs_flush_state priority_flush_states[] = { 1407 1387 FLUSH_DELAYED_ITEMS_NR, 1408 1388 FLUSH_DELAYED_ITEMS, 1389 + RESET_ZONES, 1409 1390 ALLOC_CHUNK, 1410 1391 }; 1411 1392 ··· 1420 1399 FLUSH_DELALLOC_FULL, 1421 1400 ALLOC_CHUNK, 1422 1401 COMMIT_TRANS, 1402 + RESET_ZONES, 1423 1403 }; 1424 1404 1425 1405 static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, ··· 1712 1690 if (!pending_tickets && 1713 1691 ((used + orig_bytes <= space_info->total_bytes) || 1714 1692 btrfs_can_overcommit(fs_info, space_info, orig_bytes, flush))) { 1715 - btrfs_space_info_update_bytes_may_use(fs_info, space_info, 1716 - orig_bytes); 1693 + btrfs_space_info_update_bytes_may_use(space_info, orig_bytes); 1717 1694 ret = 0; 1718 1695 } 1719 1696 ··· 1724 1703 if (ret && unlikely(flush == BTRFS_RESERVE_FLUSH_EMERGENCY)) { 1725 1704 used = btrfs_space_info_used(space_info, false); 1726 1705 if (used + orig_bytes <= space_info->total_bytes) { 1727 - btrfs_space_info_update_bytes_may_use(fs_info, space_info, 1728 - orig_bytes); 1706 + btrfs_space_info_update_bytes_may_use(space_info, orig_bytes); 1729 1707 ret = 0; 1730 1708 } 1731 1709 } ··· 2101 2081 for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) 2102 2082 do_reclaim_sweep(space_info, raid); 2103 2083 } 2084 + } 2085 + 2086 + void btrfs_return_free_space(struct btrfs_space_info *space_info, u64 len) 2087 + { 2088 + struct btrfs_fs_info *fs_info = space_info->fs_info; 2089 + struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; 2090 + 2091 + lockdep_assert_held(&space_info->lock); 2092 + 2093 + /* Prioritize the global reservation to receive the freed space. */ 2094 + if (global_rsv->space_info != space_info) 2095 + goto grant; 2096 + 2097 + spin_lock(&global_rsv->lock); 2098 + if (!global_rsv->full) { 2099 + u64 to_add = min(len, global_rsv->size - global_rsv->reserved); 2100 + 2101 + global_rsv->reserved += to_add; 2102 + btrfs_space_info_update_bytes_may_use(space_info, to_add); 2103 + if (global_rsv->reserved >= global_rsv->size) 2104 + global_rsv->full = 1; 2105 + len -= to_add; 2106 + } 2107 + spin_unlock(&global_rsv->lock); 2108 + 2109 + grant: 2110 + /* Add to any tickets we may have. */ 2111 + if (len) 2112 + btrfs_try_granting_tickets(fs_info, space_info); 2104 2113 }
+10 -5
fs/btrfs/space-info.h
··· 79 79 BTRFS_RESERVE_FLUSH_EMERGENCY, 80 80 }; 81 81 82 + /* 83 + * Please be aware that the order of enum values will be the order of the reclaim 84 + * process in btrfs_async_reclaim_metadata_space(). 85 + */ 82 86 enum btrfs_flush_state { 83 87 FLUSH_DELAYED_ITEMS_NR = 1, 84 88 FLUSH_DELAYED_ITEMS = 2, ··· 95 91 ALLOC_CHUNK_FORCE = 9, 96 92 RUN_DELAYED_IPUTS = 10, 97 93 COMMIT_TRANS = 11, 94 + RESET_ZONES = 12, 98 95 }; 99 96 100 97 struct btrfs_space_info { ··· 234 229 */ 235 230 #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \ 236 231 static inline void \ 237 - btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \ 238 - struct btrfs_space_info *sinfo, \ 232 + btrfs_space_info_update_##name(struct btrfs_space_info *sinfo, \ 239 233 s64 bytes) \ 240 234 { \ 235 + struct btrfs_fs_info *fs_info = sinfo->fs_info; \ 241 236 const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \ 242 237 lockdep_assert_held(&sinfo->lock); \ 243 238 trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \ ··· 280 275 enum btrfs_reserve_flush_enum flush); 281 276 282 277 static inline void btrfs_space_info_free_bytes_may_use( 283 - struct btrfs_fs_info *fs_info, 284 278 struct btrfs_space_info *space_info, 285 279 u64 num_bytes) 286 280 { 287 281 spin_lock(&space_info->lock); 288 - btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes); 289 - btrfs_try_granting_tickets(fs_info, space_info); 282 + btrfs_space_info_update_bytes_may_use(space_info, -num_bytes); 283 + btrfs_try_granting_tickets(space_info->fs_info, space_info); 290 284 spin_unlock(&space_info->lock); 291 285 } 292 286 int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes, ··· 299 295 bool btrfs_should_periodic_reclaim(struct btrfs_space_info *space_info); 300 296 int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info); 301 297 void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info); 298 + void btrfs_return_free_space(struct btrfs_space_info *space_info, u64 len); 302 299 303 300 #endif /* BTRFS_SPACE_INFO_H */
+34 -13
fs/btrfs/subpage.c
··· 635 635 IMPLEMENT_BTRFS_PAGE_OPS(checked, folio_set_checked, folio_clear_checked, 636 636 folio_test_checked); 637 637 638 + #define GET_SUBPAGE_BITMAP(subpage, fs_info, name, dst) \ 639 + { \ 640 + const int sectors_per_page = fs_info->sectors_per_page; \ 641 + \ 642 + ASSERT(sectors_per_page < BITS_PER_LONG); \ 643 + *dst = bitmap_read(subpage->bitmaps, \ 644 + sectors_per_page * btrfs_bitmap_nr_##name, \ 645 + sectors_per_page); \ 646 + } 647 + 648 + #define SUBPAGE_DUMP_BITMAP(fs_info, folio, name, start, len) \ 649 + { \ 650 + const struct btrfs_subpage *subpage = folio_get_private(folio); \ 651 + unsigned long bitmap; \ 652 + \ 653 + GET_SUBPAGE_BITMAP(subpage, fs_info, name, &bitmap); \ 654 + btrfs_warn(fs_info, \ 655 + "dumpping bitmap start=%llu len=%u folio=%llu " #name "_bitmap=%*pbl", \ 656 + start, len, folio_pos(folio), \ 657 + fs_info->sectors_per_page, &bitmap); \ 658 + } 659 + 638 660 /* 639 661 * Make sure not only the page dirty bit is cleared, but also subpage dirty bit 640 662 * is cleared. ··· 682 660 subpage = folio_get_private(folio); 683 661 ASSERT(subpage); 684 662 spin_lock_irqsave(&subpage->lock, flags); 663 + if (unlikely(!bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits))) { 664 + SUBPAGE_DUMP_BITMAP(fs_info, folio, dirty, start, len); 665 + ASSERT(bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits)); 666 + } 685 667 ASSERT(bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits)); 686 668 spin_unlock_irqrestore(&subpage->lock, flags); 687 669 } ··· 715 689 nbits = len >> fs_info->sectorsize_bits; 716 690 spin_lock_irqsave(&subpage->lock, flags); 717 691 /* Target range should not yet be locked. */ 718 - ASSERT(bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits)); 692 + if (unlikely(!bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits))) { 693 + SUBPAGE_DUMP_BITMAP(fs_info, folio, locked, start, len); 694 + ASSERT(bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits)); 695 + } 719 696 bitmap_set(subpage->bitmaps, start_bit, nbits); 720 697 ret = atomic_add_return(nbits, &subpage->nr_locked); 721 698 ASSERT(ret <= fs_info->sectors_per_page); 722 699 spin_unlock_irqrestore(&subpage->lock, flags); 723 - } 724 - 725 - #define GET_SUBPAGE_BITMAP(subpage, fs_info, name, dst) \ 726 - { \ 727 - const int sectors_per_page = fs_info->sectors_per_page; \ 728 - \ 729 - ASSERT(sectors_per_page < BITS_PER_LONG); \ 730 - *dst = bitmap_read(subpage->bitmaps, \ 731 - sectors_per_page * btrfs_bitmap_nr_##name, \ 732 - sectors_per_page); \ 733 700 } 734 701 735 702 void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info, ··· 735 716 unsigned long writeback_bitmap; 736 717 unsigned long ordered_bitmap; 737 718 unsigned long checked_bitmap; 719 + unsigned long locked_bitmap; 738 720 unsigned long flags; 739 721 740 722 ASSERT(folio_test_private(folio) && folio_get_private(folio)); ··· 748 728 GET_SUBPAGE_BITMAP(subpage, fs_info, writeback, &writeback_bitmap); 749 729 GET_SUBPAGE_BITMAP(subpage, fs_info, ordered, &ordered_bitmap); 750 730 GET_SUBPAGE_BITMAP(subpage, fs_info, checked, &checked_bitmap); 751 - GET_SUBPAGE_BITMAP(subpage, fs_info, locked, &checked_bitmap); 731 + GET_SUBPAGE_BITMAP(subpage, fs_info, locked, &locked_bitmap); 752 732 spin_unlock_irqrestore(&subpage->lock, flags); 753 733 754 734 dump_page(folio_page(folio, 0), "btrfs subpage dump"); 755 735 btrfs_warn(fs_info, 756 - "start=%llu len=%u page=%llu, bitmaps uptodate=%*pbl dirty=%*pbl writeback=%*pbl ordered=%*pbl checked=%*pbl", 736 + "start=%llu len=%u page=%llu, bitmaps uptodate=%*pbl dirty=%*pbl locked=%*pbl writeback=%*pbl ordered=%*pbl checked=%*pbl", 757 737 start, len, folio_pos(folio), 758 738 sectors_per_page, &uptodate_bitmap, 759 739 sectors_per_page, &dirty_bitmap, 740 + sectors_per_page, &locked_bitmap, 760 741 sectors_per_page, &writeback_bitmap, 761 742 sectors_per_page, &ordered_bitmap, 762 743 sectors_per_page, &checked_bitmap);
+13
fs/btrfs/subpage.h
··· 137 137 DECLARE_BTRFS_SUBPAGE_OPS(ordered); 138 138 DECLARE_BTRFS_SUBPAGE_OPS(checked); 139 139 140 + /* 141 + * Helper for error cleanup, where a folio will have its dirty flag cleared, 142 + * with writeback started and finished. 143 + */ 144 + static inline void btrfs_folio_clamp_finish_io(struct btrfs_fs_info *fs_info, 145 + struct folio *locked_folio, 146 + u64 start, u32 len) 147 + { 148 + btrfs_folio_clamp_clear_dirty(fs_info, locked_folio, start, len); 149 + btrfs_folio_clamp_set_writeback(fs_info, locked_folio, start, len); 150 + btrfs_folio_clamp_clear_writeback(fs_info, locked_folio, start, len); 151 + } 152 + 140 153 bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info, 141 154 struct folio *folio, u64 start, u32 len); 142 155
+19 -1
fs/btrfs/super.c
··· 971 971 972 972 err = open_ctree(sb, fs_devices); 973 973 if (err) { 974 - btrfs_err(fs_info, "open_ctree failed"); 974 + btrfs_err(fs_info, "open_ctree failed: %d", err); 975 975 return err; 976 976 } 977 977 ··· 2446 2446 static int __init btrfs_print_mod_info(void) 2447 2447 { 2448 2448 static const char options[] = "" 2449 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 2450 + ", experimental=on" 2451 + #endif 2449 2452 #ifdef CONFIG_BTRFS_DEBUG 2450 2453 ", debug=on" 2451 2454 #endif ··· 2469 2466 ", fsverity=no" 2470 2467 #endif 2471 2468 ; 2469 + 2470 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 2471 + if (btrfs_get_mod_read_policy() == NULL) 2472 + pr_info("Btrfs loaded%s\n", options); 2473 + else 2474 + pr_info("Btrfs loaded%s, read_policy=%s\n", 2475 + options, btrfs_get_mod_read_policy()); 2476 + #else 2472 2477 pr_info("Btrfs loaded%s\n", options); 2478 + #endif 2479 + 2473 2480 return 0; 2474 2481 } 2475 2482 ··· 2537 2524 }, { 2538 2525 .init_func = extent_map_init, 2539 2526 .exit_func = extent_map_exit, 2527 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 2528 + }, { 2529 + .init_func = btrfs_read_policy_init, 2530 + .exit_func = NULL, 2531 + #endif 2540 2532 }, { 2541 2533 .init_func = ordered_data_init, 2542 2534 .exit_func = ordered_data_exit,
+155 -19
fs/btrfs/sysfs.c
··· 1305 1305 } 1306 1306 BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show); 1307 1307 1308 - static const char * const btrfs_read_policy_name[] = { "pid" }; 1308 + static const char *btrfs_read_policy_name[] = { 1309 + "pid", 1310 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1311 + "round-robin", 1312 + "devid", 1313 + #endif 1314 + }; 1315 + 1316 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1317 + 1318 + /* Global module configuration parameters. */ 1319 + static char *read_policy; 1320 + char *btrfs_get_mod_read_policy(void) 1321 + { 1322 + return read_policy; 1323 + } 1324 + 1325 + /* Set perms to 0, disable /sys/module/btrfs/parameter/read_policy interface. */ 1326 + module_param(read_policy, charp, 0); 1327 + MODULE_PARM_DESC(read_policy, 1328 + "Global read policy: pid (default), round-robin[:<min_contig_read>], devid[:<devid>]"); 1329 + #endif 1330 + 1331 + int btrfs_read_policy_to_enum(const char *str, s64 *value_ret) 1332 + { 1333 + char param[32] = { 0 }; 1334 + char __maybe_unused *value_str; 1335 + 1336 + if (!str || strlen(str) == 0) 1337 + return 0; 1338 + 1339 + strncpy(param, str, sizeof(param) - 1); 1340 + 1341 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1342 + /* Separate value from input in policy:value format. */ 1343 + value_str = strchr(param, ':'); 1344 + if (value_str) { 1345 + int ret; 1346 + 1347 + *value_str = 0; 1348 + value_str++; 1349 + if (!value_ret) 1350 + return -EINVAL; 1351 + ret = kstrtos64(value_str, 10, value_ret); 1352 + if (ret) 1353 + return -EINVAL; 1354 + if (*value_ret < 0) 1355 + return -ERANGE; 1356 + } 1357 + #endif 1358 + 1359 + return sysfs_match_string(btrfs_read_policy_name, param); 1360 + } 1361 + 1362 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1363 + int __init btrfs_read_policy_init(void) 1364 + { 1365 + s64 value; 1366 + 1367 + if (btrfs_read_policy_to_enum(read_policy, &value) == -EINVAL) { 1368 + btrfs_err(NULL, "invalid read policy or value %s", read_policy); 1369 + return -EINVAL; 1370 + } 1371 + 1372 + return 0; 1373 + } 1374 + #endif 1309 1375 1310 1376 static ssize_t btrfs_read_policy_show(struct kobject *kobj, 1311 1377 struct kobj_attribute *a, char *buf) ··· 1382 1316 int i; 1383 1317 1384 1318 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { 1385 - if (policy == i) 1386 - ret += sysfs_emit_at(buf, ret, "%s[%s]", 1387 - (ret == 0 ? "" : " "), 1388 - btrfs_read_policy_name[i]); 1389 - else 1390 - ret += sysfs_emit_at(buf, ret, "%s%s", 1391 - (ret == 0 ? "" : " "), 1392 - btrfs_read_policy_name[i]); 1319 + if (ret != 0) 1320 + ret += sysfs_emit_at(buf, ret, " "); 1321 + 1322 + if (i == policy) 1323 + ret += sysfs_emit_at(buf, ret, "["); 1324 + 1325 + ret += sysfs_emit_at(buf, ret, "%s", btrfs_read_policy_name[i]); 1326 + 1327 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1328 + if (i == BTRFS_READ_POLICY_RR) 1329 + ret += sysfs_emit_at(buf, ret, ":%u", 1330 + READ_ONCE(fs_devices->rr_min_contig_read)); 1331 + 1332 + if (i == BTRFS_READ_POLICY_DEVID) 1333 + ret += sysfs_emit_at(buf, ret, ":%llu", 1334 + READ_ONCE(fs_devices->read_devid)); 1335 + #endif 1336 + if (i == policy) 1337 + ret += sysfs_emit_at(buf, ret, "]"); 1393 1338 } 1394 1339 1395 1340 ret += sysfs_emit_at(buf, ret, "\n"); ··· 1413 1336 const char *buf, size_t len) 1414 1337 { 1415 1338 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); 1416 - int i; 1339 + int index; 1340 + s64 value = -1; 1417 1341 1418 - for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { 1419 - if (sysfs_streq(buf, btrfs_read_policy_name[i])) { 1420 - if (i != READ_ONCE(fs_devices->read_policy)) { 1421 - WRITE_ONCE(fs_devices->read_policy, i); 1422 - btrfs_info(fs_devices->fs_info, 1423 - "read policy set to '%s'", 1424 - btrfs_read_policy_name[i]); 1342 + index = btrfs_read_policy_to_enum(buf, &value); 1343 + if (index < 0) 1344 + return -EINVAL; 1345 + 1346 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1347 + /* If moving from RR then disable collecting fs stats. */ 1348 + if (fs_devices->read_policy == BTRFS_READ_POLICY_RR && index != BTRFS_READ_POLICY_RR) 1349 + fs_devices->collect_fs_stats = false; 1350 + 1351 + if (index == BTRFS_READ_POLICY_RR) { 1352 + if (value != -1) { 1353 + const u32 sectorsize = fs_devices->fs_info->sectorsize; 1354 + 1355 + if (!IS_ALIGNED(value, sectorsize)) { 1356 + u64 temp_value = round_up(value, sectorsize); 1357 + 1358 + btrfs_debug(fs_devices->fs_info, 1359 + "read_policy: min contig read %lld should be multiple of sectorsize %u, rounded to %llu", 1360 + value, sectorsize, temp_value); 1361 + value = temp_value; 1425 1362 } 1426 - return len; 1363 + } else { 1364 + value = BTRFS_DEFAULT_RR_MIN_CONTIG_READ; 1427 1365 } 1366 + 1367 + if (index != READ_ONCE(fs_devices->read_policy) || 1368 + value != READ_ONCE(fs_devices->rr_min_contig_read)) { 1369 + WRITE_ONCE(fs_devices->read_policy, index); 1370 + WRITE_ONCE(fs_devices->rr_min_contig_read, value); 1371 + 1372 + btrfs_info(fs_devices->fs_info, "read policy set to '%s:%lld'", 1373 + btrfs_read_policy_name[index], value); 1374 + } 1375 + 1376 + fs_devices->collect_fs_stats = true; 1377 + 1378 + return len; 1428 1379 } 1429 1380 1430 - return -EINVAL; 1381 + if (index == BTRFS_READ_POLICY_DEVID) { 1382 + if (value != -1) { 1383 + BTRFS_DEV_LOOKUP_ARGS(args); 1384 + 1385 + /* Validate input devid. */ 1386 + args.devid = value; 1387 + if (btrfs_find_device(fs_devices, &args) == NULL) 1388 + return -EINVAL; 1389 + } else { 1390 + /* Set default devid to the devid of the latest device. */ 1391 + value = fs_devices->latest_dev->devid; 1392 + } 1393 + 1394 + if (index != READ_ONCE(fs_devices->read_policy) || 1395 + value != READ_ONCE(fs_devices->read_devid)) { 1396 + WRITE_ONCE(fs_devices->read_policy, index); 1397 + WRITE_ONCE(fs_devices->read_devid, value); 1398 + 1399 + btrfs_info(fs_devices->fs_info, "read policy set to '%s:%llu'", 1400 + btrfs_read_policy_name[index], value); 1401 + } 1402 + 1403 + return len; 1404 + } 1405 + #endif 1406 + if (index != READ_ONCE(fs_devices->read_policy)) { 1407 + WRITE_ONCE(fs_devices->read_policy, index); 1408 + btrfs_info(fs_devices->fs_info, "read policy set to '%s'", 1409 + btrfs_read_policy_name[index]); 1410 + } 1411 + 1412 + return len; 1431 1413 } 1432 1414 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store); 1433 1415
+6
fs/btrfs/sysfs.h
··· 47 47 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info); 48 48 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, 49 49 struct btrfs_qgroup *qgroup); 50 + int btrfs_read_policy_to_enum(const char *str, s64 *value); 51 + 52 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 53 + int __init btrfs_read_policy_init(void); 54 + char *btrfs_get_mod_read_policy(void); 55 + #endif 50 56 51 57 #endif
+18
fs/btrfs/tests/btrfs-tests.c
··· 30 30 [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map", 31 31 [TEST_ALLOC_CHUNK_MAP] = "cannot allocate chunk map", 32 32 [TEST_ALLOC_IO_CONTEXT] = "cannot allocate io context", 33 + [TEST_ALLOC_TRANSACTION] = "cannot allocate transaction", 33 34 }; 34 35 35 36 static const struct super_operations btrfs_test_super_ops = { ··· 143 142 fs_info->nodesize = nodesize; 144 143 fs_info->sectorsize = sectorsize; 145 144 fs_info->sectorsize_bits = ilog2(sectorsize); 145 + 146 + /* CRC32C csum size. */ 147 + fs_info->csum_size = 4; 148 + fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / 149 + fs_info->csum_size; 146 150 set_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); 147 151 148 152 test_mnt->mnt_sb->s_fs_info = fs_info; ··· 253 247 kfree(cache); 254 248 } 255 249 250 + void btrfs_init_dummy_transaction(struct btrfs_transaction *trans, struct btrfs_fs_info *fs_info) 251 + { 252 + memset(trans, 0, sizeof(*trans)); 253 + trans->fs_info = fs_info; 254 + xa_init(&trans->delayed_refs.head_refs); 255 + xa_init(&trans->delayed_refs.dirty_extents); 256 + spin_lock_init(&trans->delayed_refs.lock); 257 + } 258 + 256 259 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans, 257 260 struct btrfs_fs_info *fs_info) 258 261 { ··· 308 293 if (ret) 309 294 goto out; 310 295 ret = btrfs_test_raid_stripe_tree(sectorsize, nodesize); 296 + if (ret) 297 + goto out; 298 + ret = btrfs_test_delayed_refs(sectorsize, nodesize); 311 299 if (ret) 312 300 goto out; 313 301 }
+6
fs/btrfs/tests/btrfs-tests.h
··· 6 6 #ifndef BTRFS_TESTS_H 7 7 #define BTRFS_TESTS_H 8 8 9 + #include <linux/types.h> 10 + 9 11 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 10 12 int btrfs_run_sanity_tests(void); 11 13 ··· 27 25 TEST_ALLOC_EXTENT_MAP, 28 26 TEST_ALLOC_CHUNK_MAP, 29 27 TEST_ALLOC_IO_CONTEXT, 28 + TEST_ALLOC_TRANSACTION, 30 29 }; 31 30 32 31 extern const char *test_error[]; 33 32 34 33 struct btrfs_root; 35 34 struct btrfs_trans_handle; 35 + struct btrfs_transaction; 36 36 37 37 int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize); 38 38 int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize); ··· 44 40 int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize); 45 41 int btrfs_test_raid_stripe_tree(u32 sectorsize, u32 nodesize); 46 42 int btrfs_test_extent_map(void); 43 + int btrfs_test_delayed_refs(u32 sectorsize, u32 nodesize); 47 44 struct inode *btrfs_new_test_inode(void); 48 45 struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize); 49 46 void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info); ··· 54 49 void btrfs_free_dummy_block_group(struct btrfs_block_group *cache); 55 50 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans, 56 51 struct btrfs_fs_info *fs_info); 52 + void btrfs_init_dummy_transaction(struct btrfs_transaction *trans, struct btrfs_fs_info *fs_info); 57 53 struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info); 58 54 #else 59 55 static inline int btrfs_run_sanity_tests(void)
+1015
fs/btrfs/tests/delayed-refs-tests.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/sizes.h> 3 + #include "btrfs-tests.h" 4 + #include "../transaction.h" 5 + #include "../delayed-ref.h" 6 + #include "../extent-tree.h" 7 + 8 + #define FAKE_ROOT_OBJECTID 256 9 + #define FAKE_BYTENR 0 10 + #define FAKE_LEVEL 1 11 + #define FAKE_INO 256 12 + #define FAKE_FILE_OFFSET 0 13 + #define FAKE_PARENT SZ_1M 14 + 15 + struct ref_head_check { 16 + u64 bytenr; 17 + u64 num_bytes; 18 + int ref_mod; 19 + int total_ref_mod; 20 + int must_insert; 21 + }; 22 + 23 + struct ref_node_check { 24 + u64 bytenr; 25 + u64 num_bytes; 26 + int ref_mod; 27 + enum btrfs_delayed_ref_action action; 28 + u8 type; 29 + u64 parent; 30 + u64 root; 31 + u64 owner; 32 + u64 offset; 33 + }; 34 + 35 + static enum btrfs_ref_type ref_type_from_disk_ref_type(u8 type) 36 + { 37 + if ((type == BTRFS_TREE_BLOCK_REF_KEY) || 38 + (type == BTRFS_SHARED_BLOCK_REF_KEY)) 39 + return BTRFS_REF_METADATA; 40 + return BTRFS_REF_DATA; 41 + } 42 + 43 + static void delete_delayed_ref_head(struct btrfs_trans_handle *trans, 44 + struct btrfs_delayed_ref_head *head) 45 + { 46 + struct btrfs_fs_info *fs_info = trans->fs_info; 47 + struct btrfs_delayed_ref_root *delayed_refs = 48 + &trans->transaction->delayed_refs; 49 + 50 + spin_lock(&delayed_refs->lock); 51 + spin_lock(&head->lock); 52 + btrfs_delete_ref_head(fs_info, delayed_refs, head); 53 + spin_unlock(&head->lock); 54 + spin_unlock(&delayed_refs->lock); 55 + 56 + btrfs_delayed_ref_unlock(head); 57 + btrfs_put_delayed_ref_head(head); 58 + } 59 + 60 + static void delete_delayed_ref_node(struct btrfs_delayed_ref_head *head, 61 + struct btrfs_delayed_ref_node *node) 62 + { 63 + rb_erase_cached(&node->ref_node, &head->ref_tree); 64 + RB_CLEAR_NODE(&node->ref_node); 65 + if (!list_empty(&node->add_list)) 66 + list_del_init(&node->add_list); 67 + btrfs_put_delayed_ref(node); 68 + } 69 + 70 + static int validate_ref_head(struct btrfs_delayed_ref_head *head, 71 + struct ref_head_check *check) 72 + { 73 + if (head->bytenr != check->bytenr) { 74 + test_err("invalid bytenr have: %llu want: %llu", head->bytenr, 75 + check->bytenr); 76 + return -EINVAL; 77 + } 78 + 79 + if (head->num_bytes != check->num_bytes) { 80 + test_err("invalid num_bytes have: %llu want: %llu", 81 + head->num_bytes, check->num_bytes); 82 + return -EINVAL; 83 + } 84 + 85 + if (head->ref_mod != check->ref_mod) { 86 + test_err("invalid ref_mod have: %d want: %d", head->ref_mod, 87 + check->ref_mod); 88 + return -EINVAL; 89 + } 90 + 91 + if (head->total_ref_mod != check->total_ref_mod) { 92 + test_err("invalid total_ref_mod have: %d want: %d", 93 + head->total_ref_mod, check->total_ref_mod); 94 + return -EINVAL; 95 + } 96 + 97 + if (head->must_insert_reserved != check->must_insert) { 98 + test_err("invalid must_insert have: %d want: %d", 99 + head->must_insert_reserved, check->must_insert); 100 + return -EINVAL; 101 + } 102 + 103 + return 0; 104 + } 105 + 106 + static int validate_ref_node(struct btrfs_delayed_ref_node *node, 107 + struct ref_node_check *check) 108 + { 109 + if (node->bytenr != check->bytenr) { 110 + test_err("invalid bytenr have: %llu want: %llu", node->bytenr, 111 + check->bytenr); 112 + return -EINVAL; 113 + } 114 + 115 + if (node->num_bytes != check->num_bytes) { 116 + test_err("invalid num_bytes have: %llu want: %llu", 117 + node->num_bytes, check->num_bytes); 118 + return -EINVAL; 119 + } 120 + 121 + if (node->ref_mod != check->ref_mod) { 122 + test_err("invalid ref_mod have: %d want: %d", node->ref_mod, 123 + check->ref_mod); 124 + return -EINVAL; 125 + } 126 + 127 + if (node->action != check->action) { 128 + test_err("invalid action have: %d want: %d", node->action, 129 + check->action); 130 + return -EINVAL; 131 + } 132 + 133 + if (node->parent != check->parent) { 134 + test_err("invalid parent have: %llu want: %llu", node->parent, 135 + check->parent); 136 + return -EINVAL; 137 + } 138 + 139 + if (node->ref_root != check->root) { 140 + test_err("invalid root have: %llu want: %llu", node->ref_root, 141 + check->root); 142 + return -EINVAL; 143 + } 144 + 145 + if (node->type != check->type) { 146 + test_err("invalid type have: %d want: %d", node->type, 147 + check->type); 148 + return -EINVAL; 149 + } 150 + 151 + if (btrfs_delayed_ref_owner(node) != check->owner) { 152 + test_err("invalid owner have: %llu want: %llu", 153 + btrfs_delayed_ref_owner(node), check->owner); 154 + return -EINVAL; 155 + } 156 + 157 + if (btrfs_delayed_ref_offset(node) != check->offset) { 158 + test_err("invalid offset have: %llu want: %llu", 159 + btrfs_delayed_ref_offset(node), check->offset); 160 + return -EINVAL; 161 + } 162 + 163 + return 0; 164 + } 165 + 166 + static int simple_test(struct btrfs_trans_handle *trans, 167 + struct ref_head_check *head_check, 168 + struct ref_node_check *node_check) 169 + { 170 + struct btrfs_delayed_ref_root *delayed_refs = 171 + &trans->transaction->delayed_refs; 172 + struct btrfs_fs_info *fs_info = trans->fs_info; 173 + struct btrfs_delayed_ref_head *head; 174 + struct btrfs_delayed_ref_node *node; 175 + struct btrfs_ref ref = { 176 + .type = ref_type_from_disk_ref_type(node_check->type), 177 + .action = node_check->action, 178 + .parent = node_check->parent, 179 + .ref_root = node_check->root, 180 + .bytenr = node_check->bytenr, 181 + .num_bytes = fs_info->nodesize, 182 + }; 183 + int ret; 184 + 185 + if (ref.type == BTRFS_REF_METADATA) 186 + btrfs_init_tree_ref(&ref, node_check->owner, node_check->root, 187 + false); 188 + else 189 + btrfs_init_data_ref(&ref, node_check->owner, node_check->offset, 190 + node_check->root, true); 191 + 192 + if (ref.type == BTRFS_REF_METADATA) 193 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 194 + else 195 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 196 + if (ret) { 197 + test_err("failed ref action %d", ret); 198 + return ret; 199 + } 200 + 201 + head = btrfs_select_ref_head(fs_info, delayed_refs); 202 + if (IS_ERR_OR_NULL(head)) { 203 + if (IS_ERR(head)) 204 + test_err("failed to select delayed ref head: %ld", 205 + PTR_ERR(head)); 206 + else 207 + test_err("failed to find delayed ref head"); 208 + return -EINVAL; 209 + } 210 + 211 + ret = -EINVAL; 212 + if (validate_ref_head(head, head_check)) 213 + goto out; 214 + 215 + spin_lock(&head->lock); 216 + node = btrfs_select_delayed_ref(head); 217 + spin_unlock(&head->lock); 218 + if (!node) { 219 + test_err("failed to select delayed ref"); 220 + goto out; 221 + } 222 + 223 + if (validate_ref_node(node, node_check)) 224 + goto out; 225 + ret = 0; 226 + out: 227 + btrfs_unselect_ref_head(delayed_refs, head); 228 + btrfs_destroy_delayed_refs(trans->transaction); 229 + return ret; 230 + } 231 + 232 + /* 233 + * These are simple tests, make sure that our btrfs_ref's get turned into the 234 + * appropriate btrfs_delayed_ref_node based on their settings and action. 235 + */ 236 + static int simple_tests(struct btrfs_trans_handle *trans) 237 + { 238 + struct btrfs_fs_info *fs_info = trans->fs_info; 239 + struct ref_head_check head_check = { 240 + .bytenr = FAKE_BYTENR, 241 + .num_bytes = fs_info->nodesize, 242 + .ref_mod = 1, 243 + .total_ref_mod = 1, 244 + }; 245 + struct ref_node_check node_check = { 246 + .bytenr = FAKE_BYTENR, 247 + .num_bytes = fs_info->nodesize, 248 + .ref_mod = 1, 249 + .action = BTRFS_ADD_DELAYED_REF, 250 + .type = BTRFS_TREE_BLOCK_REF_KEY, 251 + .parent = 0, 252 + .root = FAKE_ROOT_OBJECTID, 253 + .owner = FAKE_LEVEL, 254 + .offset = 0, 255 + }; 256 + 257 + if (simple_test(trans, &head_check, &node_check)) { 258 + test_err("single add tree block failed"); 259 + return -EINVAL; 260 + } 261 + 262 + node_check.type = BTRFS_EXTENT_DATA_REF_KEY; 263 + node_check.owner = FAKE_INO; 264 + node_check.offset = FAKE_FILE_OFFSET; 265 + 266 + if (simple_test(trans, &head_check, &node_check)) { 267 + test_err("single add extent data failed"); 268 + return -EINVAL; 269 + } 270 + 271 + node_check.parent = FAKE_PARENT; 272 + node_check.type = BTRFS_SHARED_BLOCK_REF_KEY; 273 + node_check.owner = FAKE_LEVEL; 274 + node_check.offset = 0; 275 + 276 + if (simple_test(trans, &head_check, &node_check)) { 277 + test_err("single add shared block failed"); 278 + return -EINVAL; 279 + } 280 + 281 + node_check.type = BTRFS_SHARED_DATA_REF_KEY; 282 + node_check.owner = FAKE_INO; 283 + node_check.offset = FAKE_FILE_OFFSET; 284 + 285 + if (simple_test(trans, &head_check, &node_check)) { 286 + test_err("single add shared data failed"); 287 + return -EINVAL; 288 + } 289 + 290 + head_check.ref_mod = -1; 291 + head_check.total_ref_mod = -1; 292 + node_check.action = BTRFS_DROP_DELAYED_REF; 293 + node_check.type = BTRFS_TREE_BLOCK_REF_KEY; 294 + node_check.owner = FAKE_LEVEL; 295 + node_check.offset = 0; 296 + node_check.parent = 0; 297 + 298 + if (simple_test(trans, &head_check, &node_check)) { 299 + test_err("single drop tree block failed"); 300 + return -EINVAL; 301 + } 302 + 303 + node_check.type = BTRFS_EXTENT_DATA_REF_KEY; 304 + node_check.owner = FAKE_INO; 305 + node_check.offset = FAKE_FILE_OFFSET; 306 + 307 + if (simple_test(trans, &head_check, &node_check)) { 308 + test_err("single drop extent data failed"); 309 + return -EINVAL; 310 + } 311 + 312 + node_check.parent = FAKE_PARENT; 313 + node_check.type = BTRFS_SHARED_BLOCK_REF_KEY; 314 + node_check.owner = FAKE_LEVEL; 315 + node_check.offset = 0; 316 + if (simple_test(trans, &head_check, &node_check)) { 317 + test_err("single drop shared block failed"); 318 + return -EINVAL; 319 + } 320 + 321 + node_check.type = BTRFS_SHARED_DATA_REF_KEY; 322 + node_check.owner = FAKE_INO; 323 + node_check.offset = FAKE_FILE_OFFSET; 324 + if (simple_test(trans, &head_check, &node_check)) { 325 + test_err("single drop shared data failed"); 326 + return -EINVAL; 327 + } 328 + 329 + return 0; 330 + } 331 + 332 + /* 333 + * Merge tests, validate that we do delayed ref merging properly, the ref counts 334 + * all end up properly, and delayed refs are deleted once they're no longer 335 + * needed. 336 + */ 337 + static int merge_tests(struct btrfs_trans_handle *trans, 338 + enum btrfs_ref_type type) 339 + { 340 + struct btrfs_fs_info *fs_info = trans->fs_info; 341 + struct btrfs_delayed_ref_head *head = NULL; 342 + struct btrfs_delayed_ref_node *node; 343 + struct btrfs_ref ref = { 344 + .type = type, 345 + .action = BTRFS_ADD_DELAYED_REF, 346 + .parent = 0, 347 + .ref_root = FAKE_ROOT_OBJECTID, 348 + .bytenr = FAKE_BYTENR, 349 + .num_bytes = fs_info->nodesize, 350 + }; 351 + struct ref_head_check head_check = { 352 + .bytenr = FAKE_BYTENR, 353 + .num_bytes = fs_info->nodesize, 354 + .ref_mod = 0, 355 + .total_ref_mod = 0, 356 + }; 357 + struct ref_node_check node_check = { 358 + .bytenr = FAKE_BYTENR, 359 + .num_bytes = fs_info->nodesize, 360 + .ref_mod = 2, 361 + .action = BTRFS_ADD_DELAYED_REF, 362 + .parent = 0, 363 + .root = FAKE_ROOT_OBJECTID, 364 + }; 365 + int ret; 366 + 367 + /* 368 + * First add a ref and then drop it, make sure we get a head ref with a 369 + * 0 total ref mod and no nodes. 370 + */ 371 + if (type == BTRFS_REF_METADATA) { 372 + node_check.type = BTRFS_TREE_BLOCK_REF_KEY; 373 + node_check.owner = FAKE_LEVEL; 374 + btrfs_init_tree_ref(&ref, FAKE_LEVEL, FAKE_ROOT_OBJECTID, false); 375 + } else { 376 + node_check.type = BTRFS_EXTENT_DATA_REF_KEY; 377 + node_check.owner = FAKE_INO; 378 + node_check.offset = FAKE_FILE_OFFSET; 379 + btrfs_init_data_ref(&ref, FAKE_INO, FAKE_FILE_OFFSET, 380 + FAKE_ROOT_OBJECTID, true); 381 + } 382 + 383 + if (type == BTRFS_REF_METADATA) 384 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 385 + else 386 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 387 + if (ret) { 388 + test_err("failed ref action %d", ret); 389 + return ret; 390 + } 391 + 392 + ref.action = BTRFS_DROP_DELAYED_REF; 393 + if (type == BTRFS_REF_METADATA) 394 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 395 + else 396 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 397 + if (ret) { 398 + test_err("failed ref action %d", ret); 399 + goto out; 400 + } 401 + 402 + head = btrfs_select_ref_head(fs_info, &trans->transaction->delayed_refs); 403 + if (IS_ERR_OR_NULL(head)) { 404 + if (IS_ERR(head)) 405 + test_err("failed to select delayed ref head: %ld", 406 + PTR_ERR(head)); 407 + else 408 + test_err("failed to find delayed ref head"); 409 + goto out; 410 + } 411 + 412 + ret = -EINVAL; 413 + if (validate_ref_head(head, &head_check)) { 414 + test_err("single add and drop failed"); 415 + goto out; 416 + } 417 + 418 + spin_lock(&head->lock); 419 + node = btrfs_select_delayed_ref(head); 420 + spin_unlock(&head->lock); 421 + if (node) { 422 + test_err("found node when none should exist"); 423 + goto out; 424 + } 425 + 426 + delete_delayed_ref_head(trans, head); 427 + head = NULL; 428 + 429 + /* 430 + * Add a ref, then add another ref, make sure we get a head ref with a 431 + * 2 total ref mod and 1 node. 432 + */ 433 + ref.action = BTRFS_ADD_DELAYED_REF; 434 + if (type == BTRFS_REF_METADATA) 435 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 436 + else 437 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 438 + if (ret) { 439 + test_err("failed ref action %d", ret); 440 + goto out; 441 + } 442 + 443 + if (type == BTRFS_REF_METADATA) 444 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 445 + else 446 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 447 + if (ret) { 448 + test_err("failed ref action %d", ret); 449 + goto out; 450 + } 451 + 452 + head = btrfs_select_ref_head(fs_info, &trans->transaction->delayed_refs); 453 + if (IS_ERR_OR_NULL(head)) { 454 + if (IS_ERR(head)) 455 + test_err("failed to select delayed ref head: %ld", 456 + PTR_ERR(head)); 457 + else 458 + test_err("failed to find delayed ref head"); 459 + goto out; 460 + } 461 + 462 + head_check.ref_mod = 2; 463 + head_check.total_ref_mod = 2; 464 + ret = -EINVAL; 465 + if (validate_ref_head(head, &head_check)) { 466 + test_err("double add failed"); 467 + goto out; 468 + } 469 + 470 + spin_lock(&head->lock); 471 + node = btrfs_select_delayed_ref(head); 472 + spin_unlock(&head->lock); 473 + if (!node) { 474 + test_err("failed to select delayed ref"); 475 + goto out; 476 + } 477 + 478 + if (validate_ref_node(node, &node_check)) { 479 + test_err("node check failed"); 480 + goto out; 481 + } 482 + 483 + delete_delayed_ref_node(head, node); 484 + 485 + spin_lock(&head->lock); 486 + node = btrfs_select_delayed_ref(head); 487 + spin_unlock(&head->lock); 488 + if (node) { 489 + test_err("found node when none should exist"); 490 + goto out; 491 + } 492 + delete_delayed_ref_head(trans, head); 493 + head = NULL; 494 + 495 + /* Add two drop refs, make sure they are merged properly. */ 496 + ref.action = BTRFS_DROP_DELAYED_REF; 497 + if (type == BTRFS_REF_METADATA) 498 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 499 + else 500 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 501 + if (ret) { 502 + test_err("failed ref action %d", ret); 503 + goto out; 504 + } 505 + 506 + if (type == BTRFS_REF_METADATA) 507 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 508 + else 509 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 510 + if (ret) { 511 + test_err("failed ref action %d", ret); 512 + goto out; 513 + } 514 + 515 + head = btrfs_select_ref_head(fs_info, &trans->transaction->delayed_refs); 516 + if (IS_ERR_OR_NULL(head)) { 517 + if (IS_ERR(head)) 518 + test_err("failed to select delayed ref head: %ld", 519 + PTR_ERR(head)); 520 + else 521 + test_err("failed to find delayed ref head"); 522 + goto out; 523 + } 524 + 525 + head_check.ref_mod = -2; 526 + head_check.total_ref_mod = -2; 527 + ret = -EINVAL; 528 + if (validate_ref_head(head, &head_check)) { 529 + test_err("double drop failed"); 530 + goto out; 531 + } 532 + 533 + node_check.action = BTRFS_DROP_DELAYED_REF; 534 + spin_lock(&head->lock); 535 + node = btrfs_select_delayed_ref(head); 536 + spin_unlock(&head->lock); 537 + if (!node) { 538 + test_err("failed to select delayed ref"); 539 + goto out; 540 + } 541 + 542 + if (validate_ref_node(node, &node_check)) { 543 + test_err("node check failed"); 544 + goto out; 545 + } 546 + 547 + delete_delayed_ref_node(head, node); 548 + 549 + spin_lock(&head->lock); 550 + node = btrfs_select_delayed_ref(head); 551 + spin_unlock(&head->lock); 552 + if (node) { 553 + test_err("found node when none should exist"); 554 + goto out; 555 + } 556 + delete_delayed_ref_head(trans, head); 557 + head = NULL; 558 + 559 + /* Add multiple refs, then drop until we go negative again. */ 560 + ref.action = BTRFS_ADD_DELAYED_REF; 561 + for (int i = 0; i < 10; i++) { 562 + if (type == BTRFS_REF_METADATA) 563 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 564 + else 565 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 566 + if (ret) { 567 + test_err("failed ref action %d", ret); 568 + goto out; 569 + } 570 + } 571 + 572 + ref.action = BTRFS_DROP_DELAYED_REF; 573 + for (int i = 0; i < 12; i++) { 574 + if (type == BTRFS_REF_METADATA) 575 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 576 + else 577 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 578 + if (ret) { 579 + test_err("failed ref action %d", ret); 580 + goto out; 581 + } 582 + } 583 + 584 + head = btrfs_select_ref_head(fs_info, &trans->transaction->delayed_refs); 585 + if (IS_ERR_OR_NULL(head)) { 586 + if (IS_ERR(head)) 587 + test_err("failed to select delayed ref head: %ld", 588 + PTR_ERR(head)); 589 + else 590 + test_err("failed to find delayed ref head"); 591 + ret = -EINVAL; 592 + goto out; 593 + } 594 + 595 + head_check.ref_mod = -2; 596 + head_check.total_ref_mod = -2; 597 + ret = -EINVAL; 598 + if (validate_ref_head(head, &head_check)) { 599 + test_err("double drop failed"); 600 + goto out; 601 + } 602 + 603 + spin_lock(&head->lock); 604 + node = btrfs_select_delayed_ref(head); 605 + spin_unlock(&head->lock); 606 + if (!node) { 607 + test_err("failed to select delayed ref"); 608 + goto out; 609 + } 610 + 611 + if (validate_ref_node(node, &node_check)) { 612 + test_err("node check failed"); 613 + goto out; 614 + } 615 + 616 + delete_delayed_ref_node(head, node); 617 + 618 + spin_lock(&head->lock); 619 + node = btrfs_select_delayed_ref(head); 620 + spin_unlock(&head->lock); 621 + if (node) { 622 + test_err("found node when none should exist"); 623 + goto out; 624 + } 625 + 626 + delete_delayed_ref_head(trans, head); 627 + head = NULL; 628 + 629 + /* Drop multiple refs, then add until we go positive again. */ 630 + ref.action = BTRFS_DROP_DELAYED_REF; 631 + for (int i = 0; i < 10; i++) { 632 + if (type == BTRFS_REF_METADATA) 633 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 634 + else 635 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 636 + if (ret) { 637 + test_err("failed ref action %d", ret); 638 + goto out; 639 + } 640 + } 641 + 642 + ref.action = BTRFS_ADD_DELAYED_REF; 643 + for (int i = 0; i < 12; i++) { 644 + if (type == BTRFS_REF_METADATA) 645 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 646 + else 647 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 648 + if (ret) { 649 + test_err("failed ref action %d", ret); 650 + goto out; 651 + } 652 + } 653 + 654 + head = btrfs_select_ref_head(fs_info, &trans->transaction->delayed_refs); 655 + if (IS_ERR_OR_NULL(head)) { 656 + if (IS_ERR(head)) 657 + test_err("failed to select delayed ref head: %ld", 658 + PTR_ERR(head)); 659 + else 660 + test_err("failed to find delayed ref head"); 661 + ret = -EINVAL; 662 + goto out; 663 + } 664 + 665 + head_check.ref_mod = 2; 666 + head_check.total_ref_mod = 2; 667 + ret = -EINVAL; 668 + if (validate_ref_head(head, &head_check)) { 669 + test_err("add and drop to positive failed"); 670 + goto out; 671 + } 672 + 673 + node_check.action = BTRFS_ADD_DELAYED_REF; 674 + spin_lock(&head->lock); 675 + node = btrfs_select_delayed_ref(head); 676 + spin_unlock(&head->lock); 677 + if (!node) { 678 + test_err("failed to select delayed ref"); 679 + goto out; 680 + } 681 + 682 + if (validate_ref_node(node, &node_check)) { 683 + test_err("node check failed"); 684 + goto out; 685 + } 686 + 687 + delete_delayed_ref_node(head, node); 688 + 689 + spin_lock(&head->lock); 690 + node = btrfs_select_delayed_ref(head); 691 + spin_unlock(&head->lock); 692 + if (node) { 693 + test_err("found node when none should exist"); 694 + goto out; 695 + } 696 + delete_delayed_ref_head(trans, head); 697 + head = NULL; 698 + 699 + /* 700 + * Add a bunch of refs with different roots and parents, then drop them 701 + * all, make sure everything is properly merged. 702 + */ 703 + ref.action = BTRFS_ADD_DELAYED_REF; 704 + for (int i = 0; i < 50; i++) { 705 + if (!(i % 2)) { 706 + ref.parent = 0; 707 + ref.ref_root = FAKE_ROOT_OBJECTID + i; 708 + } else { 709 + ref.parent = FAKE_PARENT + (i * fs_info->nodesize); 710 + } 711 + if (type == BTRFS_REF_METADATA) 712 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 713 + else 714 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 715 + if (ret) { 716 + test_err("failed ref action %d", ret); 717 + goto out; 718 + } 719 + } 720 + 721 + ref.action = BTRFS_DROP_DELAYED_REF; 722 + for (int i = 0; i < 50; i++) { 723 + if (!(i % 2)) { 724 + ref.parent = 0; 725 + ref.ref_root = FAKE_ROOT_OBJECTID + i; 726 + } else { 727 + ref.parent = FAKE_PARENT + (i * fs_info->nodesize); 728 + } 729 + if (type == BTRFS_REF_METADATA) 730 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 731 + else 732 + ret = btrfs_add_delayed_data_ref(trans, &ref, 0); 733 + if (ret) { 734 + test_err("failed ref action %d", ret); 735 + goto out; 736 + } 737 + } 738 + 739 + head = btrfs_select_ref_head(fs_info, &trans->transaction->delayed_refs); 740 + if (IS_ERR_OR_NULL(head)) { 741 + if (IS_ERR(head)) 742 + test_err("failed to select delayed ref head: %ld", 743 + PTR_ERR(head)); 744 + else 745 + test_err("failed to find delayed ref head"); 746 + ret = -EINVAL; 747 + goto out; 748 + } 749 + 750 + head_check.ref_mod = 0; 751 + head_check.total_ref_mod = 0; 752 + ret = -EINVAL; 753 + if (validate_ref_head(head, &head_check)) { 754 + test_err("add and drop multiple failed"); 755 + goto out; 756 + } 757 + 758 + spin_lock(&head->lock); 759 + node = btrfs_select_delayed_ref(head); 760 + spin_unlock(&head->lock); 761 + if (node) { 762 + test_err("found node when none should exist"); 763 + goto out; 764 + } 765 + ret = 0; 766 + out: 767 + if (!IS_ERR_OR_NULL(head)) 768 + btrfs_unselect_ref_head(&trans->transaction->delayed_refs, head); 769 + btrfs_destroy_delayed_refs(trans->transaction); 770 + return ret; 771 + } 772 + 773 + /* 774 + * Basic test to validate we always get the add operations first followed by any 775 + * delete operations. 776 + */ 777 + static int select_delayed_refs_test(struct btrfs_trans_handle *trans) 778 + { 779 + struct btrfs_delayed_ref_root *delayed_refs = 780 + &trans->transaction->delayed_refs; 781 + struct btrfs_fs_info *fs_info = trans->fs_info; 782 + struct btrfs_delayed_ref_head *head = NULL; 783 + struct btrfs_delayed_ref_node *node; 784 + struct btrfs_ref ref = { 785 + .type = BTRFS_REF_METADATA, 786 + .action = BTRFS_DROP_DELAYED_REF, 787 + .parent = 0, 788 + .ref_root = FAKE_ROOT_OBJECTID, 789 + .bytenr = FAKE_BYTENR, 790 + .num_bytes = fs_info->nodesize, 791 + }; 792 + struct ref_head_check head_check = { 793 + .bytenr = FAKE_BYTENR, 794 + .num_bytes = fs_info->nodesize, 795 + .ref_mod = 0, 796 + .total_ref_mod = 0, 797 + }; 798 + struct ref_node_check node_check = { 799 + .bytenr = FAKE_BYTENR, 800 + .num_bytes = fs_info->nodesize, 801 + .ref_mod = 1, 802 + .action = BTRFS_ADD_DELAYED_REF, 803 + .type = BTRFS_TREE_BLOCK_REF_KEY, 804 + .parent = 0, 805 + .owner = FAKE_LEVEL, 806 + .offset = 0, 807 + }; 808 + int ret; 809 + 810 + /* Add the drop first. */ 811 + btrfs_init_tree_ref(&ref, FAKE_LEVEL, FAKE_ROOT_OBJECTID, false); 812 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 813 + if (ret) { 814 + test_err("failed ref action %d", ret); 815 + return ret; 816 + } 817 + 818 + /* 819 + * Now add the add, and make it a different root so it's logically later 820 + * in the rb tree. 821 + */ 822 + ref.action = BTRFS_ADD_DELAYED_REF; 823 + ref.ref_root = FAKE_ROOT_OBJECTID + 1; 824 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 825 + if (ret) { 826 + test_err("failed ref action %d", ret); 827 + goto out; 828 + } 829 + 830 + head = btrfs_select_ref_head(fs_info, delayed_refs); 831 + if (IS_ERR_OR_NULL(head)) { 832 + if (IS_ERR(head)) 833 + test_err("failed to select delayed ref head: %ld", 834 + PTR_ERR(head)); 835 + else 836 + test_err("failed to find delayed ref head"); 837 + ret = -EINVAL; 838 + head = NULL; 839 + goto out; 840 + } 841 + 842 + ret = -EINVAL; 843 + if (validate_ref_head(head, &head_check)) { 844 + test_err("head check failed"); 845 + goto out; 846 + } 847 + 848 + spin_lock(&head->lock); 849 + node = btrfs_select_delayed_ref(head); 850 + spin_unlock(&head->lock); 851 + if (!node) { 852 + test_err("failed to select delayed ref"); 853 + goto out; 854 + } 855 + 856 + node_check.root = FAKE_ROOT_OBJECTID + 1; 857 + if (validate_ref_node(node, &node_check)) { 858 + test_err("node check failed"); 859 + goto out; 860 + } 861 + delete_delayed_ref_node(head, node); 862 + 863 + spin_lock(&head->lock); 864 + node = btrfs_select_delayed_ref(head); 865 + spin_unlock(&head->lock); 866 + if (!node) { 867 + test_err("failed to select delayed ref"); 868 + goto out; 869 + } 870 + 871 + node_check.action = BTRFS_DROP_DELAYED_REF; 872 + node_check.root = FAKE_ROOT_OBJECTID; 873 + if (validate_ref_node(node, &node_check)) { 874 + test_err("node check failed"); 875 + goto out; 876 + } 877 + delete_delayed_ref_node(head, node); 878 + delete_delayed_ref_head(trans, head); 879 + head = NULL; 880 + 881 + /* 882 + * Now we're going to do the same thing, but we're going to have an add 883 + * that gets deleted because of a merge, and make sure we still have 884 + * another add in place. 885 + */ 886 + ref.action = BTRFS_DROP_DELAYED_REF; 887 + ref.ref_root = FAKE_ROOT_OBJECTID; 888 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 889 + if (ret) { 890 + test_err("failed ref action %d", ret); 891 + goto out; 892 + } 893 + 894 + ref.action = BTRFS_ADD_DELAYED_REF; 895 + ref.ref_root = FAKE_ROOT_OBJECTID + 1; 896 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 897 + if (ret) { 898 + test_err("failed ref action %d", ret); 899 + goto out; 900 + } 901 + 902 + ref.action = BTRFS_DROP_DELAYED_REF; 903 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 904 + if (ret) { 905 + test_err("failed ref action %d", ret); 906 + goto out; 907 + } 908 + 909 + ref.action = BTRFS_ADD_DELAYED_REF; 910 + ref.ref_root = FAKE_ROOT_OBJECTID + 2; 911 + ret = btrfs_add_delayed_tree_ref(trans, &ref, NULL); 912 + if (ret) { 913 + test_err("failed ref action %d", ret); 914 + goto out; 915 + } 916 + 917 + head = btrfs_select_ref_head(fs_info, delayed_refs); 918 + if (IS_ERR_OR_NULL(head)) { 919 + if (IS_ERR(head)) 920 + test_err("failed to select delayed ref head: %ld", 921 + PTR_ERR(head)); 922 + else 923 + test_err("failed to find delayed ref head"); 924 + ret = -EINVAL; 925 + head = NULL; 926 + goto out; 927 + } 928 + 929 + ret = -EINVAL; 930 + if (validate_ref_head(head, &head_check)) { 931 + test_err("head check failed"); 932 + goto out; 933 + } 934 + 935 + spin_lock(&head->lock); 936 + node = btrfs_select_delayed_ref(head); 937 + spin_unlock(&head->lock); 938 + if (!node) { 939 + test_err("failed to select delayed ref"); 940 + goto out; 941 + } 942 + 943 + node_check.action = BTRFS_ADD_DELAYED_REF; 944 + node_check.root = FAKE_ROOT_OBJECTID + 2; 945 + if (validate_ref_node(node, &node_check)) { 946 + test_err("node check failed"); 947 + goto out; 948 + } 949 + delete_delayed_ref_node(head, node); 950 + 951 + spin_lock(&head->lock); 952 + node = btrfs_select_delayed_ref(head); 953 + spin_unlock(&head->lock); 954 + if (!node) { 955 + test_err("failed to select delayed ref"); 956 + goto out; 957 + } 958 + 959 + node_check.action = BTRFS_DROP_DELAYED_REF; 960 + node_check.root = FAKE_ROOT_OBJECTID; 961 + if (validate_ref_node(node, &node_check)) { 962 + test_err("node check failed"); 963 + goto out; 964 + } 965 + delete_delayed_ref_node(head, node); 966 + ret = 0; 967 + out: 968 + if (head) 969 + btrfs_unselect_ref_head(delayed_refs, head); 970 + btrfs_destroy_delayed_refs(trans->transaction); 971 + return ret; 972 + } 973 + 974 + int btrfs_test_delayed_refs(u32 sectorsize, u32 nodesize) 975 + { 976 + struct btrfs_transaction *transaction; 977 + struct btrfs_trans_handle trans; 978 + struct btrfs_fs_info *fs_info; 979 + int ret; 980 + 981 + test_msg("running delayed refs tests"); 982 + 983 + fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); 984 + if (!fs_info) { 985 + test_std_err(TEST_ALLOC_FS_INFO); 986 + return -ENOMEM; 987 + } 988 + transaction = kmalloc(sizeof(*transaction), GFP_KERNEL); 989 + if (!transaction) { 990 + test_std_err(TEST_ALLOC_TRANSACTION); 991 + ret = -ENOMEM; 992 + goto out_free_fs_info; 993 + } 994 + btrfs_init_dummy_trans(&trans, fs_info); 995 + btrfs_init_dummy_transaction(transaction, fs_info); 996 + trans.transaction = transaction; 997 + 998 + ret = simple_tests(&trans); 999 + if (!ret) { 1000 + test_msg("running delayed refs merg tests on metadata refs"); 1001 + ret = merge_tests(&trans, BTRFS_REF_METADATA); 1002 + } 1003 + 1004 + if (!ret) { 1005 + test_msg("running delayed refs merg tests on data refs"); 1006 + ret = merge_tests(&trans, BTRFS_REF_DATA); 1007 + } 1008 + 1009 + if (!ret) 1010 + ret = select_delayed_refs_test(&trans); 1011 + 1012 + out_free_fs_info: 1013 + btrfs_free_dummy_fs_info(fs_info); 1014 + return ret; 1015 + }
+642 -19
fs/btrfs/tests/raid-stripe-tree-tests.c
··· 14 14 #define RST_TEST_NUM_DEVICES (2) 15 15 #define RST_TEST_RAID1_TYPE (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_RAID1) 16 16 17 + #define SZ_48K (SZ_32K + SZ_16K) 18 + 17 19 typedef int (*test_func_t)(struct btrfs_trans_handle *trans); 18 20 19 21 static struct btrfs_device *btrfs_device_by_devid(struct btrfs_fs_devices *fs_devices, ··· 29 27 } 30 28 31 29 return NULL; 30 + } 31 + 32 + /* 33 + * Test creating a range of three extents and then punch a hole in the middle, 34 + * deleting all of the middle extents and partially deleting the "book ends". 35 + */ 36 + static int test_punch_hole_3extents(struct btrfs_trans_handle *trans) 37 + { 38 + struct btrfs_fs_info *fs_info = trans->fs_info; 39 + struct btrfs_io_context *bioc; 40 + struct btrfs_io_stripe io_stripe = { 0 }; 41 + u64 map_type = RST_TEST_RAID1_TYPE; 42 + u64 logical1 = SZ_1M; 43 + u64 len1 = SZ_1M; 44 + u64 logical2 = logical1 + len1; 45 + u64 len2 = SZ_1M; 46 + u64 logical3 = logical2 + len2; 47 + u64 len3 = SZ_1M; 48 + u64 hole_start = logical1 + SZ_256K; 49 + u64 hole_len = SZ_2M; 50 + int ret; 51 + 52 + bioc = alloc_btrfs_io_context(fs_info, logical1, RST_TEST_NUM_DEVICES); 53 + if (!bioc) { 54 + test_std_err(TEST_ALLOC_IO_CONTEXT); 55 + ret = -ENOMEM; 56 + goto out; 57 + } 58 + 59 + io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0); 60 + 61 + /* Prepare for the test, 1st create 3 x 1M extents. */ 62 + bioc->map_type = map_type; 63 + bioc->size = len1; 64 + 65 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 66 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 67 + 68 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 69 + if (!stripe->dev) { 70 + test_err("cannot find device with devid %d", i); 71 + ret = -EINVAL; 72 + goto out; 73 + } 74 + 75 + stripe->physical = logical1 + i * SZ_1G; 76 + } 77 + 78 + ret = btrfs_insert_one_raid_extent(trans, bioc); 79 + if (ret) { 80 + test_err("inserting RAID extent failed: %d", ret); 81 + goto out; 82 + } 83 + 84 + bioc->logical = logical2; 85 + bioc->size = len2; 86 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 87 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 88 + 89 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 90 + if (!stripe->dev) { 91 + test_err("cannot find device with devid %d", i); 92 + ret = -EINVAL; 93 + goto out; 94 + } 95 + 96 + stripe->physical = logical2 + i * SZ_1G; 97 + } 98 + 99 + ret = btrfs_insert_one_raid_extent(trans, bioc); 100 + if (ret) { 101 + test_err("inserting RAID extent failed: %d", ret); 102 + goto out; 103 + } 104 + 105 + bioc->logical = logical3; 106 + bioc->size = len3; 107 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 108 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 109 + 110 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 111 + if (!stripe->dev) { 112 + test_err("cannot find device with devid %d", i); 113 + ret = -EINVAL; 114 + goto out; 115 + } 116 + 117 + stripe->physical = logical3 + i * SZ_1G; 118 + } 119 + 120 + ret = btrfs_insert_one_raid_extent(trans, bioc); 121 + if (ret) { 122 + test_err("inserting RAID extent failed: %d", ret); 123 + goto out; 124 + } 125 + 126 + /* 127 + * Delete a range starting at logical1 + 256K and 2M in length. Extent 128 + * 1 is truncated to 256k length, extent 2 is completely dropped and 129 + * extent 3 is moved 256K to the right. 130 + */ 131 + ret = btrfs_delete_raid_extent(trans, hole_start, hole_len); 132 + if (ret) { 133 + test_err("deleting RAID extent [%llu, %llu] failed", 134 + hole_start, hole_start + hole_len); 135 + goto out; 136 + } 137 + 138 + /* Get the first extent and check its size. */ 139 + ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len1, map_type, 140 + 0, &io_stripe); 141 + if (ret) { 142 + test_err("lookup of RAID extent [%llu, %llu] failed", 143 + logical1, logical1 + len1); 144 + goto out; 145 + } 146 + 147 + if (io_stripe.physical != logical1) { 148 + test_err("invalid physical address, expected %llu, got %llu", 149 + logical1, io_stripe.physical); 150 + ret = -EINVAL; 151 + goto out; 152 + } 153 + 154 + if (len1 != SZ_256K) { 155 + test_err("invalid stripe length, expected %llu, got %llu", 156 + (u64)SZ_256K, len1); 157 + ret = -EINVAL; 158 + goto out; 159 + } 160 + 161 + /* Get the second extent and check it's absent. */ 162 + ret = btrfs_get_raid_extent_offset(fs_info, logical2, &len2, map_type, 163 + 0, &io_stripe); 164 + if (ret != -ENODATA) { 165 + test_err("lookup of RAID extent [%llu, %llu] succeeded should fail", 166 + logical2, logical2 + len2); 167 + ret = -EINVAL; 168 + goto out; 169 + } 170 + 171 + /* Get the third extent and check its size. */ 172 + logical3 += SZ_256K; 173 + ret = btrfs_get_raid_extent_offset(fs_info, logical3, &len3, map_type, 174 + 0, &io_stripe); 175 + if (ret) { 176 + test_err("lookup of RAID extent [%llu, %llu] failed", 177 + logical3, logical3 + len3); 178 + goto out; 179 + } 180 + 181 + if (io_stripe.physical != logical3) { 182 + test_err("invalid physical address, expected %llu, got %llu", 183 + logical3 + SZ_256K, io_stripe.physical); 184 + ret = -EINVAL; 185 + goto out; 186 + } 187 + 188 + if (len3 != SZ_1M - SZ_256K) { 189 + test_err("invalid stripe length, expected %llu, got %llu", 190 + (u64)SZ_1M - SZ_256K, len3); 191 + ret = -EINVAL; 192 + goto out; 193 + } 194 + 195 + ret = btrfs_delete_raid_extent(trans, logical1, len1); 196 + if (ret) { 197 + test_err("deleting RAID extent [%llu, %llu] failed", 198 + logical1, logical1 + len1); 199 + goto out; 200 + } 201 + 202 + ret = btrfs_delete_raid_extent(trans, logical3, len3); 203 + if (ret) { 204 + test_err("deleting RAID extent [%llu, %llu] failed", 205 + logical1, logical1 + len1); 206 + goto out; 207 + } 208 + 209 + out: 210 + btrfs_put_bioc(bioc); 211 + return ret; 212 + } 213 + 214 + static int test_delete_two_extents(struct btrfs_trans_handle *trans) 215 + { 216 + struct btrfs_fs_info *fs_info = trans->fs_info; 217 + struct btrfs_io_context *bioc; 218 + struct btrfs_io_stripe io_stripe = { 0 }; 219 + u64 map_type = RST_TEST_RAID1_TYPE; 220 + u64 logical1 = SZ_1M; 221 + u64 len1 = SZ_1M; 222 + u64 logical2 = logical1 + len1; 223 + u64 len2 = SZ_1M; 224 + u64 logical3 = logical2 + len2; 225 + u64 len3 = SZ_1M; 226 + int ret; 227 + 228 + bioc = alloc_btrfs_io_context(fs_info, logical1, RST_TEST_NUM_DEVICES); 229 + if (!bioc) { 230 + test_std_err(TEST_ALLOC_IO_CONTEXT); 231 + ret = -ENOMEM; 232 + goto out; 233 + } 234 + 235 + io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0); 236 + 237 + /* Prepare for the test, 1st create 3 x 1M extents. */ 238 + bioc->map_type = map_type; 239 + bioc->size = len1; 240 + 241 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 242 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 243 + 244 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 245 + if (!stripe->dev) { 246 + test_err("cannot find device with devid %d", i); 247 + ret = -EINVAL; 248 + goto out; 249 + } 250 + 251 + stripe->physical = logical1 + i * SZ_1G; 252 + } 253 + 254 + ret = btrfs_insert_one_raid_extent(trans, bioc); 255 + if (ret) { 256 + test_err("inserting RAID extent failed: %d", ret); 257 + goto out; 258 + } 259 + 260 + bioc->logical = logical2; 261 + bioc->size = len2; 262 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 263 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 264 + 265 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 266 + if (!stripe->dev) { 267 + test_err("cannot find device with devid %d", i); 268 + ret = -EINVAL; 269 + goto out; 270 + } 271 + 272 + stripe->physical = logical2 + i * SZ_1G; 273 + } 274 + 275 + ret = btrfs_insert_one_raid_extent(trans, bioc); 276 + if (ret) { 277 + test_err("inserting RAID extent failed: %d", ret); 278 + goto out; 279 + } 280 + 281 + bioc->logical = logical3; 282 + bioc->size = len3; 283 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 284 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 285 + 286 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 287 + if (!stripe->dev) { 288 + test_err("cannot find device with devid %d", i); 289 + ret = -EINVAL; 290 + goto out; 291 + } 292 + 293 + stripe->physical = logical3 + i * SZ_1G; 294 + } 295 + 296 + ret = btrfs_insert_one_raid_extent(trans, bioc); 297 + if (ret) { 298 + test_err("inserting RAID extent failed: %d", ret); 299 + goto out; 300 + } 301 + 302 + /* 303 + * Delete a range starting at logical1 and 2M in length. Extents 1 304 + * and 2 are dropped and extent 3 is kept as is. 305 + */ 306 + ret = btrfs_delete_raid_extent(trans, logical1, len1 + len2); 307 + if (ret) { 308 + test_err("deleting RAID extent [%llu, %llu] failed", 309 + logical1, logical1 + len1 + len2); 310 + goto out; 311 + } 312 + 313 + ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len1, map_type, 314 + 0, &io_stripe); 315 + if (ret != -ENODATA) { 316 + test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail", 317 + logical1, len1); 318 + goto out; 319 + } 320 + 321 + ret = btrfs_get_raid_extent_offset(fs_info, logical2, &len2, map_type, 322 + 0, &io_stripe); 323 + if (ret != -ENODATA) { 324 + test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail", 325 + logical2, len2); 326 + goto out; 327 + } 328 + 329 + ret = btrfs_get_raid_extent_offset(fs_info, logical3, &len3, map_type, 330 + 0, &io_stripe); 331 + if (ret) { 332 + test_err("lookup of RAID extent [%llu, %llu] failed", 333 + logical3, len3); 334 + goto out; 335 + } 336 + 337 + if (io_stripe.physical != logical3) { 338 + test_err("invalid physical address, expected %llu, got %llu", 339 + logical3, io_stripe.physical); 340 + ret = -EINVAL; 341 + goto out; 342 + } 343 + 344 + if (len3 != SZ_1M) { 345 + test_err("invalid stripe length, expected %llu, got %llu", 346 + (u64)SZ_1M, len3); 347 + ret = -EINVAL; 348 + goto out; 349 + } 350 + 351 + ret = btrfs_delete_raid_extent(trans, logical3, len3); 352 + out: 353 + btrfs_put_bioc(bioc); 354 + return ret; 355 + } 356 + 357 + /* Test punching a hole into a single RAID stripe-extent. */ 358 + static int test_punch_hole(struct btrfs_trans_handle *trans) 359 + { 360 + struct btrfs_fs_info *fs_info = trans->fs_info; 361 + struct btrfs_io_context *bioc; 362 + struct btrfs_io_stripe io_stripe = { 0 }; 363 + u64 map_type = RST_TEST_RAID1_TYPE; 364 + u64 logical1 = SZ_1M; 365 + u64 hole_start = logical1 + SZ_32K; 366 + u64 hole_len = SZ_64K; 367 + u64 logical2 = hole_start + hole_len; 368 + u64 len = SZ_1M; 369 + u64 len1 = SZ_32K; 370 + u64 len2 = len - len1 - hole_len; 371 + int ret; 372 + 373 + bioc = alloc_btrfs_io_context(fs_info, logical1, RST_TEST_NUM_DEVICES); 374 + if (!bioc) { 375 + test_std_err(TEST_ALLOC_IO_CONTEXT); 376 + ret = -ENOMEM; 377 + goto out; 378 + } 379 + 380 + io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0); 381 + bioc->map_type = map_type; 382 + bioc->size = len; 383 + 384 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 385 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 386 + 387 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 388 + if (!stripe->dev) { 389 + test_err("cannot find device with devid %d", i); 390 + ret = -EINVAL; 391 + goto out; 392 + } 393 + 394 + stripe->physical = logical1 + i * SZ_1G; 395 + } 396 + 397 + ret = btrfs_insert_one_raid_extent(trans, bioc); 398 + if (ret) { 399 + test_err("inserting RAID extent failed: %d", ret); 400 + goto out; 401 + } 402 + 403 + ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len, map_type, 0, 404 + &io_stripe); 405 + if (ret) { 406 + test_err("lookup of RAID extent [%llu, %llu] failed", logical1, 407 + logical1 + len); 408 + goto out; 409 + } 410 + 411 + if (io_stripe.physical != logical1) { 412 + test_err("invalid physical address, expected %llu got %llu", 413 + logical1, io_stripe.physical); 414 + ret = -EINVAL; 415 + goto out; 416 + } 417 + 418 + if (len != SZ_1M) { 419 + test_err("invalid stripe length, expected %llu got %llu", 420 + (u64)SZ_1M, len); 421 + ret = -EINVAL; 422 + goto out; 423 + } 424 + 425 + ret = btrfs_delete_raid_extent(trans, hole_start, hole_len); 426 + if (ret) { 427 + test_err("deleting RAID extent [%llu, %llu] failed", 428 + hole_start, hole_start + hole_len); 429 + goto out; 430 + } 431 + 432 + ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len1, map_type, 433 + 0, &io_stripe); 434 + if (ret) { 435 + test_err("lookup of RAID extent [%llu, %llu] failed", 436 + logical1, logical1 + len1); 437 + goto out; 438 + } 439 + 440 + if (io_stripe.physical != logical1) { 441 + test_err("invalid physical address, expected %llu, got %llu", 442 + logical1, io_stripe.physical); 443 + ret = -EINVAL; 444 + goto out; 445 + } 446 + 447 + if (len1 != SZ_32K) { 448 + test_err("invalid stripe length, expected %llu, got %llu", 449 + (u64)SZ_32K, len1); 450 + ret = -EINVAL; 451 + goto out; 452 + } 453 + 454 + ret = btrfs_get_raid_extent_offset(fs_info, logical2, &len2, map_type, 455 + 0, &io_stripe); 456 + if (ret) { 457 + test_err("lookup of RAID extent [%llu, %llu] failed", logical2, 458 + logical2 + len2); 459 + goto out; 460 + } 461 + 462 + if (io_stripe.physical != logical2) { 463 + test_err("invalid physical address, expected %llu, got %llu", 464 + logical2, io_stripe.physical); 465 + ret = -EINVAL; 466 + goto out; 467 + } 468 + 469 + if (len2 != len - len1 - hole_len) { 470 + test_err("invalid length, expected %llu, got %llu", 471 + len - len1 - hole_len, len2); 472 + ret = -EINVAL; 473 + goto out; 474 + } 475 + 476 + /* Check for the absence of the hole. */ 477 + ret = btrfs_get_raid_extent_offset(fs_info, hole_start, &hole_len, 478 + map_type, 0, &io_stripe); 479 + if (ret != -ENODATA) { 480 + ret = -EINVAL; 481 + test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail", 482 + hole_start, hole_start + SZ_64K); 483 + goto out; 484 + } 485 + 486 + ret = btrfs_delete_raid_extent(trans, logical1, len1); 487 + if (ret) 488 + goto out; 489 + 490 + ret = btrfs_delete_raid_extent(trans, logical2, len2); 491 + out: 492 + btrfs_put_bioc(bioc); 493 + return ret; 494 + } 495 + 496 + /* 497 + * Test a 1M RST write that spans two adjacent RST items on disk and then 498 + * delete a portion starting in the first item and spanning into the second 499 + * item. This is similar to test_front_delete(), but spanning multiple items. 500 + */ 501 + static int test_front_delete_prev_item(struct btrfs_trans_handle *trans) 502 + { 503 + struct btrfs_fs_info *fs_info = trans->fs_info; 504 + struct btrfs_io_context *bioc; 505 + struct btrfs_io_stripe io_stripe = { 0 }; 506 + u64 map_type = RST_TEST_RAID1_TYPE; 507 + u64 logical1 = SZ_1M; 508 + u64 logical2 = SZ_2M; 509 + u64 len = SZ_1M; 510 + int ret; 511 + 512 + bioc = alloc_btrfs_io_context(fs_info, logical1, RST_TEST_NUM_DEVICES); 513 + if (!bioc) { 514 + test_std_err(TEST_ALLOC_IO_CONTEXT); 515 + ret = -ENOMEM; 516 + goto out; 517 + } 518 + 519 + io_stripe.dev = btrfs_device_by_devid(fs_info->fs_devices, 0); 520 + bioc->map_type = map_type; 521 + bioc->size = len; 522 + 523 + /* Insert RAID extent 1. */ 524 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 525 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 526 + 527 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 528 + if (!stripe->dev) { 529 + test_err("cannot find device with devid %d", i); 530 + ret = -EINVAL; 531 + goto out; 532 + } 533 + 534 + stripe->physical = logical1 + i * SZ_1G; 535 + } 536 + 537 + ret = btrfs_insert_one_raid_extent(trans, bioc); 538 + if (ret) { 539 + test_err("inserting RAID extent failed: %d", ret); 540 + goto out; 541 + } 542 + 543 + bioc->logical = logical2; 544 + /* Insert RAID extent 2, directly adjacent to it. */ 545 + for (int i = 0; i < RST_TEST_NUM_DEVICES; i++) { 546 + struct btrfs_io_stripe *stripe = &bioc->stripes[i]; 547 + 548 + stripe->dev = btrfs_device_by_devid(fs_info->fs_devices, i); 549 + if (!stripe->dev) { 550 + test_err("cannot find device with devid %d", i); 551 + ret = -EINVAL; 552 + goto out; 553 + } 554 + 555 + stripe->physical = logical2 + i * SZ_1G; 556 + } 557 + 558 + ret = btrfs_insert_one_raid_extent(trans, bioc); 559 + if (ret) { 560 + test_err("inserting RAID extent failed: %d", ret); 561 + goto out; 562 + } 563 + 564 + ret = btrfs_delete_raid_extent(trans, logical1 + SZ_512K, SZ_1M); 565 + if (ret) { 566 + test_err("deleting RAID extent [%llu, %llu] failed", 567 + logical1 + SZ_512K, (u64)SZ_1M); 568 + goto out; 569 + } 570 + 571 + /* Verify item 1 is truncated to 512K. */ 572 + ret = btrfs_get_raid_extent_offset(fs_info, logical1, &len, map_type, 0, 573 + &io_stripe); 574 + if (ret) { 575 + test_err("lookup of RAID extent [%llu, %llu] failed", logical1, 576 + logical1 + len); 577 + goto out; 578 + } 579 + 580 + if (io_stripe.physical != logical1) { 581 + test_err("invalid physical address, expected %llu got %llu", 582 + logical1, io_stripe.physical); 583 + ret = -EINVAL; 584 + goto out; 585 + } 586 + 587 + if (len != SZ_512K) { 588 + test_err("invalid stripe length, expected %llu got %llu", 589 + (u64)SZ_512K, len); 590 + ret = -EINVAL; 591 + goto out; 592 + } 593 + 594 + /* Verify item 2's start is moved by 512K. */ 595 + ret = btrfs_get_raid_extent_offset(fs_info, logical2 + SZ_512K, &len, 596 + map_type, 0, &io_stripe); 597 + if (ret) { 598 + test_err("lookup of RAID extent [%llu, %llu] failed", 599 + logical2 + SZ_512K, logical2 + len); 600 + goto out; 601 + } 602 + 603 + if (io_stripe.physical != logical2 + SZ_512K) { 604 + test_err("invalid physical address, expected %llu got %llu", 605 + logical2 + SZ_512K, io_stripe.physical); 606 + ret = -EINVAL; 607 + goto out; 608 + } 609 + 610 + if (len != SZ_512K) { 611 + test_err("invalid stripe length, expected %llu got %llu", 612 + (u64)SZ_512K, len); 613 + ret = -EINVAL; 614 + goto out; 615 + } 616 + 617 + /* Verify there's a hole at [1M+512K, 2M+512K] . */ 618 + len = SZ_1M; 619 + ret = btrfs_get_raid_extent_offset(fs_info, logical1 + SZ_512K, &len, 620 + map_type, 0, &io_stripe); 621 + if (ret != -ENODATA) { 622 + test_err("lookup of RAID [%llu, %llu] succeeded, should fail", 623 + logical1 + SZ_512K, logical1 + SZ_512K + len); 624 + goto out; 625 + } 626 + 627 + /* Clean up after us. */ 628 + ret = btrfs_delete_raid_extent(trans, logical1, SZ_512K); 629 + if (ret) 630 + goto out; 631 + 632 + ret = btrfs_delete_raid_extent(trans, logical2 + SZ_512K, SZ_512K); 633 + 634 + out: 635 + btrfs_put_bioc(bioc); 636 + return ret; 32 637 } 33 638 34 639 /* ··· 703 94 goto out; 704 95 } 705 96 706 - ret = btrfs_delete_raid_extent(trans, logical, SZ_32K); 97 + ret = btrfs_delete_raid_extent(trans, logical, SZ_16K); 707 98 if (ret) { 708 99 test_err("deleting RAID extent [%llu, %llu] failed", logical, 709 - logical + SZ_32K); 100 + logical + SZ_16K); 710 101 goto out; 711 102 } 712 103 713 - len = SZ_32K; 714 - ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_32K, &len, 104 + len -= SZ_16K; 105 + ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_16K, &len, 715 106 map_type, 0, &io_stripe); 716 107 if (ret) { 717 108 test_err("lookup of RAID extent [%llu, %llu] failed", 718 - logical + SZ_32K, logical + SZ_32K + len); 109 + logical + SZ_16K, logical + SZ_64K); 719 110 goto out; 720 111 } 721 112 722 - if (io_stripe.physical != logical + SZ_32K) { 113 + if (io_stripe.physical != logical + SZ_16K) { 723 114 test_err("invalid physical address, expected %llu, got %llu", 724 - logical + SZ_32K, io_stripe.physical); 115 + logical + SZ_16K, io_stripe.physical); 725 116 ret = -EINVAL; 726 117 goto out; 727 118 } 728 119 729 - if (len != SZ_32K) { 120 + if (len != SZ_48K) { 730 121 test_err("invalid stripe length, expected %llu, got %llu", 731 - (u64)SZ_32K, len); 122 + (u64)SZ_48K, len); 732 123 ret = -EINVAL; 733 124 goto out; 734 125 } 735 126 736 127 ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe); 737 - if (!ret) { 128 + if (ret != -ENODATA) { 738 129 ret = -EINVAL; 739 130 test_err("lookup of RAID extent [%llu, %llu] succeeded, should fail", 740 - logical, logical + SZ_32K); 131 + logical, logical + SZ_16K); 741 132 goto out; 742 133 } 743 134 744 - ret = btrfs_delete_raid_extent(trans, logical + SZ_32K, SZ_32K); 135 + ret = btrfs_delete_raid_extent(trans, logical + SZ_16K, SZ_48K); 745 136 out: 746 137 btrfs_put_bioc(bioc); 747 138 return ret; ··· 818 209 goto out; 819 210 } 820 211 821 - ret = btrfs_delete_raid_extent(trans, logical + SZ_32K, SZ_32K); 212 + ret = btrfs_delete_raid_extent(trans, logical + SZ_48K, SZ_16K); 822 213 if (ret) { 823 214 test_err("deleting RAID extent [%llu, %llu] failed", 824 - logical + SZ_32K, logical + SZ_64K); 215 + logical + SZ_48K, logical + SZ_64K); 825 216 goto out; 826 217 } 827 218 828 - len = SZ_32K; 219 + len = SZ_48K; 829 220 ret = btrfs_get_raid_extent_offset(fs_info, logical, &len, map_type, 0, &io_stripe); 830 221 if (ret) { 831 222 test_err("lookup of RAID extent [%llu, %llu] failed", logical, ··· 840 231 goto out; 841 232 } 842 233 843 - if (len != SZ_32K) { 234 + if (len != SZ_48K) { 844 235 test_err("invalid stripe length, expected %llu, got %llu", 845 - (u64)SZ_32K, len); 236 + (u64)SZ_48K, len); 237 + ret = -EINVAL; 238 + goto out; 239 + } 240 + 241 + len = SZ_16K; 242 + ret = btrfs_get_raid_extent_offset(fs_info, logical + SZ_48K, &len, 243 + map_type, 0, &io_stripe); 244 + if (ret != -ENODATA) { 245 + test_err("lookup of RAID extent [%llu, %llu] succeeded should fail", 246 + logical + SZ_48K, logical + SZ_64K); 846 247 ret = -EINVAL; 847 248 goto out; 848 249 } ··· 1075 456 test_create_update_delete, 1076 457 test_tail_delete, 1077 458 test_front_delete, 459 + test_front_delete_prev_item, 460 + test_punch_hole, 461 + test_punch_hole_3extents, 462 + test_delete_two_extents, 1078 463 }; 1079 464 1080 465 static int run_test(test_func_t test, u32 sectorsize, u32 nodesize) ··· 1101 478 ret = PTR_ERR(root); 1102 479 goto out; 1103 480 } 1104 - btrfs_set_super_compat_ro_flags(root->fs_info->super_copy, 1105 - BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE); 481 + btrfs_set_super_incompat_flags(root->fs_info->super_copy, 482 + BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE); 1106 483 root->root_key.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID; 1107 484 root->root_key.type = BTRFS_ROOT_ITEM_KEY; 1108 485 root->root_key.offset = 0;
+1 -2
fs/btrfs/transaction.c
··· 795 795 if (num_bytes) 796 796 btrfs_block_rsv_release(fs_info, trans_rsv, num_bytes, NULL); 797 797 if (delayed_refs_bytes) 798 - btrfs_space_info_free_bytes_may_use(fs_info, trans_rsv->space_info, 799 - delayed_refs_bytes); 798 + btrfs_space_info_free_bytes_may_use(trans_rsv->space_info, delayed_refs_bytes); 800 799 reserve_fail: 801 800 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved); 802 801 return ERR_PTR(ret);
+16 -2
fs/btrfs/transaction.h
··· 227 227 delayed_refs->qgroup_to_skip = 0; 228 228 } 229 229 230 - bool __cold abort_should_print_stack(int error); 230 + /* 231 + * We want the transaction abort to print stack trace only for errors where the 232 + * cause could be a bug, eg. due to ENOSPC, and not for common errors that are 233 + * caused by external factors. 234 + */ 235 + static inline bool btrfs_abort_should_print_stack(int error) 236 + { 237 + switch (error) { 238 + case -EIO: 239 + case -EROFS: 240 + case -ENOMEM: 241 + return false; 242 + } 243 + return true; 244 + } 231 245 232 246 /* 233 247 * Call btrfs_abort_transaction as early as possible when an error condition is ··· 254 240 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \ 255 241 &((trans)->fs_info->fs_state))) { \ 256 242 __first = true; \ 257 - if (WARN(abort_should_print_stack(error), \ 243 + if (WARN(btrfs_abort_should_print_stack(error), \ 258 244 KERN_ERR \ 259 245 "BTRFS: Transaction aborted (error %d)\n", \ 260 246 (error))) { \
+54 -42
fs/btrfs/tree-checker.c
··· 764 764 return 0; 765 765 } 766 766 767 - __printf(4, 5) 767 + __printf(5, 6) 768 768 __cold 769 - static void chunk_err(const struct extent_buffer *leaf, 769 + static void chunk_err(const struct btrfs_fs_info *fs_info, 770 + const struct extent_buffer *leaf, 770 771 const struct btrfs_chunk *chunk, u64 logical, 771 772 const char *fmt, ...) 772 773 { 773 - const struct btrfs_fs_info *fs_info = leaf->fs_info; 774 - bool is_sb; 774 + bool is_sb = !leaf; 775 775 struct va_format vaf; 776 776 va_list args; 777 777 int i; 778 778 int slot = -1; 779 - 780 - /* Only superblock eb is able to have such small offset */ 781 - is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET); 782 779 783 780 if (!is_sb) { 784 781 /* ··· 809 812 /* 810 813 * The common chunk check which could also work on super block sys chunk array. 811 814 * 815 + * If @leaf is NULL, then @chunk must be an on-stack chunk item. 816 + * (For superblock sys_chunk array, and fs_info->sectorsize is unreliable) 817 + * 812 818 * Return -EUCLEAN if anything is corrupted. 813 819 * Return 0 if everything is OK. 814 820 */ 815 - int btrfs_check_chunk_valid(struct extent_buffer *leaf, 816 - struct btrfs_chunk *chunk, u64 logical) 821 + int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info, 822 + const struct extent_buffer *leaf, 823 + const struct btrfs_chunk *chunk, u64 logical, 824 + u32 sectorsize) 817 825 { 818 - struct btrfs_fs_info *fs_info = leaf->fs_info; 819 826 u64 length; 820 827 u64 chunk_end; 821 828 u64 stripe_len; ··· 827 826 u16 sub_stripes; 828 827 u64 type; 829 828 u64 features; 829 + u32 chunk_sector_size; 830 830 bool mixed = false; 831 831 int raid_index; 832 832 int nparity; 833 833 int ncopies; 834 834 835 - length = btrfs_chunk_length(leaf, chunk); 836 - stripe_len = btrfs_chunk_stripe_len(leaf, chunk); 837 - num_stripes = btrfs_chunk_num_stripes(leaf, chunk); 838 - sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); 839 - type = btrfs_chunk_type(leaf, chunk); 835 + if (leaf) { 836 + length = btrfs_chunk_length(leaf, chunk); 837 + stripe_len = btrfs_chunk_stripe_len(leaf, chunk); 838 + num_stripes = btrfs_chunk_num_stripes(leaf, chunk); 839 + sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); 840 + type = btrfs_chunk_type(leaf, chunk); 841 + chunk_sector_size = btrfs_chunk_sector_size(leaf, chunk); 842 + } else { 843 + length = btrfs_stack_chunk_length(chunk); 844 + stripe_len = btrfs_stack_chunk_stripe_len(chunk); 845 + num_stripes = btrfs_stack_chunk_num_stripes(chunk); 846 + sub_stripes = btrfs_stack_chunk_sub_stripes(chunk); 847 + type = btrfs_stack_chunk_type(chunk); 848 + chunk_sector_size = btrfs_stack_chunk_sector_size(chunk); 849 + } 840 850 raid_index = btrfs_bg_flags_to_raid_index(type); 841 851 ncopies = btrfs_raid_array[raid_index].ncopies; 842 852 nparity = btrfs_raid_array[raid_index].nparity; 843 853 844 854 if (unlikely(!num_stripes)) { 845 - chunk_err(leaf, chunk, logical, 855 + chunk_err(fs_info, leaf, chunk, logical, 846 856 "invalid chunk num_stripes, have %u", num_stripes); 847 857 return -EUCLEAN; 848 858 } 849 859 if (unlikely(num_stripes < ncopies)) { 850 - chunk_err(leaf, chunk, logical, 860 + chunk_err(fs_info, leaf, chunk, logical, 851 861 "invalid chunk num_stripes < ncopies, have %u < %d", 852 862 num_stripes, ncopies); 853 863 return -EUCLEAN; 854 864 } 855 865 if (unlikely(nparity && num_stripes == nparity)) { 856 - chunk_err(leaf, chunk, logical, 866 + chunk_err(fs_info, leaf, chunk, logical, 857 867 "invalid chunk num_stripes == nparity, have %u == %d", 858 868 num_stripes, nparity); 859 869 return -EUCLEAN; 860 870 } 861 - if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) { 862 - chunk_err(leaf, chunk, logical, 871 + if (unlikely(!IS_ALIGNED(logical, sectorsize))) { 872 + chunk_err(fs_info, leaf, chunk, logical, 863 873 "invalid chunk logical, have %llu should aligned to %u", 864 - logical, fs_info->sectorsize); 874 + logical, sectorsize); 865 875 return -EUCLEAN; 866 876 } 867 - if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) { 868 - chunk_err(leaf, chunk, logical, 877 + if (unlikely(chunk_sector_size != sectorsize)) { 878 + chunk_err(fs_info, leaf, chunk, logical, 869 879 "invalid chunk sectorsize, have %u expect %u", 870 - btrfs_chunk_sector_size(leaf, chunk), 871 - fs_info->sectorsize); 880 + chunk_sector_size, sectorsize); 872 881 return -EUCLEAN; 873 882 } 874 - if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) { 875 - chunk_err(leaf, chunk, logical, 883 + if (unlikely(!length || !IS_ALIGNED(length, sectorsize))) { 884 + chunk_err(fs_info, leaf, chunk, logical, 876 885 "invalid chunk length, have %llu", length); 877 886 return -EUCLEAN; 878 887 } 879 888 if (unlikely(check_add_overflow(logical, length, &chunk_end))) { 880 - chunk_err(leaf, chunk, logical, 889 + chunk_err(fs_info, leaf, chunk, logical, 881 890 "invalid chunk logical start and length, have logical start %llu length %llu", 882 891 logical, length); 883 892 return -EUCLEAN; 884 893 } 885 894 if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) { 886 - chunk_err(leaf, chunk, logical, 895 + chunk_err(fs_info, leaf, chunk, logical, 887 896 "invalid chunk stripe length: %llu", 888 897 stripe_len); 889 898 return -EUCLEAN; ··· 907 896 * Thus it should be a good way to catch obvious bitflips. 908 897 */ 909 898 if (unlikely(length >= btrfs_stripe_nr_to_offset(U32_MAX))) { 910 - chunk_err(leaf, chunk, logical, 899 + chunk_err(fs_info, leaf, chunk, logical, 911 900 "chunk length too large: have %llu limit %llu", 912 901 length, btrfs_stripe_nr_to_offset(U32_MAX)); 913 902 return -EUCLEAN; 914 903 } 915 904 if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK | 916 905 BTRFS_BLOCK_GROUP_PROFILE_MASK))) { 917 - chunk_err(leaf, chunk, logical, 906 + chunk_err(fs_info, leaf, chunk, logical, 918 907 "unrecognized chunk type: 0x%llx", 919 908 ~(BTRFS_BLOCK_GROUP_TYPE_MASK | 920 - BTRFS_BLOCK_GROUP_PROFILE_MASK) & 921 - btrfs_chunk_type(leaf, chunk)); 909 + BTRFS_BLOCK_GROUP_PROFILE_MASK) & type); 922 910 return -EUCLEAN; 923 911 } 924 912 925 913 if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && 926 914 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) { 927 - chunk_err(leaf, chunk, logical, 915 + chunk_err(fs_info, leaf, chunk, logical, 928 916 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set", 929 917 type & BTRFS_BLOCK_GROUP_PROFILE_MASK); 930 918 return -EUCLEAN; 931 919 } 932 920 if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) { 933 - chunk_err(leaf, chunk, logical, 921 + chunk_err(fs_info, leaf, chunk, logical, 934 922 "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx", 935 923 type, BTRFS_BLOCK_GROUP_TYPE_MASK); 936 924 return -EUCLEAN; ··· 938 928 if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) && 939 929 (type & (BTRFS_BLOCK_GROUP_METADATA | 940 930 BTRFS_BLOCK_GROUP_DATA)))) { 941 - chunk_err(leaf, chunk, logical, 931 + chunk_err(fs_info, leaf, chunk, logical, 942 932 "system chunk with data or metadata type: 0x%llx", 943 933 type); 944 934 return -EUCLEAN; ··· 951 941 if (!mixed) { 952 942 if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) && 953 943 (type & BTRFS_BLOCK_GROUP_DATA))) { 954 - chunk_err(leaf, chunk, logical, 944 + chunk_err(fs_info, leaf, chunk, logical, 955 945 "mixed chunk type in non-mixed mode: 0x%llx", type); 956 946 return -EUCLEAN; 957 947 } ··· 973 963 num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) || 974 964 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && 975 965 num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) { 976 - chunk_err(leaf, chunk, logical, 966 + chunk_err(fs_info, leaf, chunk, logical, 977 967 "invalid num_stripes:sub_stripes %u:%u for profile %llu", 978 968 num_stripes, sub_stripes, 979 969 type & BTRFS_BLOCK_GROUP_PROFILE_MASK); ··· 993 983 struct btrfs_chunk *chunk, 994 984 struct btrfs_key *key, int slot) 995 985 { 986 + struct btrfs_fs_info *fs_info = leaf->fs_info; 996 987 int num_stripes; 997 988 998 989 if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) { 999 - chunk_err(leaf, chunk, key->offset, 990 + chunk_err(fs_info, leaf, chunk, key->offset, 1000 991 "invalid chunk item size: have %u expect [%zu, %u)", 1001 992 btrfs_item_size(leaf, slot), 1002 993 sizeof(struct btrfs_chunk), 1003 - BTRFS_LEAF_DATA_SIZE(leaf->fs_info)); 994 + BTRFS_LEAF_DATA_SIZE(fs_info)); 1004 995 return -EUCLEAN; 1005 996 } 1006 997 ··· 1012 1001 1013 1002 if (unlikely(btrfs_chunk_item_size(num_stripes) != 1014 1003 btrfs_item_size(leaf, slot))) { 1015 - chunk_err(leaf, chunk, key->offset, 1004 + chunk_err(fs_info, leaf, chunk, key->offset, 1016 1005 "invalid chunk item size: have %u expect %lu", 1017 1006 btrfs_item_size(leaf, slot), 1018 1007 btrfs_chunk_item_size(num_stripes)); 1019 1008 return -EUCLEAN; 1020 1009 } 1021 1010 out: 1022 - return btrfs_check_chunk_valid(leaf, chunk, key->offset); 1011 + return btrfs_check_chunk_valid(fs_info, leaf, chunk, key->offset, 1012 + fs_info->sectorsize); 1023 1013 } 1024 1014 1025 1015 __printf(3, 4)
+5 -2
fs/btrfs/tree-checker.h
··· 10 10 #include <uapi/linux/btrfs_tree.h> 11 11 12 12 struct extent_buffer; 13 + struct btrfs_fs_info; 13 14 struct btrfs_chunk; 14 15 struct btrfs_key; 15 16 ··· 67 66 int btrfs_check_leaf(struct extent_buffer *leaf); 68 67 int btrfs_check_node(struct extent_buffer *node); 69 68 70 - int btrfs_check_chunk_valid(struct extent_buffer *leaf, 71 - struct btrfs_chunk *chunk, u64 logical); 69 + int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info, 70 + const struct extent_buffer *leaf, 71 + const struct btrfs_chunk *chunk, u64 logical, 72 + u32 sectorsize); 72 73 int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner); 73 74 int btrfs_verify_level_key(struct extent_buffer *eb, 74 75 const struct btrfs_tree_parent_check *check);
-4
fs/btrfs/tree-log.c
··· 590 590 } 591 591 } 592 592 no_copy: 593 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 594 593 btrfs_release_path(path); 595 594 return 0; 596 595 } ··· 3587 3588 last_offset = max(last_offset, curr_end); 3588 3589 } 3589 3590 btrfs_set_dir_log_end(path->nodes[0], item, last_offset); 3590 - btrfs_mark_buffer_dirty(trans, path->nodes[0]); 3591 3591 btrfs_release_path(path); 3592 3592 return 0; 3593 3593 } ··· 4564 4566 dst_index++; 4565 4567 } 4566 4568 4567 - btrfs_mark_buffer_dirty(trans, dst_path->nodes[0]); 4568 4569 btrfs_release_path(dst_path); 4569 4570 out: 4570 4571 kfree(ins_data); ··· 4773 4776 write_extent_buffer(leaf, &fi, 4774 4777 btrfs_item_ptr_offset(leaf, path->slots[0]), 4775 4778 sizeof(fi)); 4776 - btrfs_mark_buffer_dirty(trans, leaf); 4777 4779 4778 4780 btrfs_release_path(path); 4779 4781
-2
fs/btrfs/uuid-tree.c
··· 140 140 ret = 0; 141 141 subid_le = cpu_to_le64(subid_cpu); 142 142 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le)); 143 - btrfs_mark_buffer_dirty(trans, eb); 144 - 145 143 out: 146 144 btrfs_free_path(path); 147 145 return ret;
+139 -101
fs/btrfs/volumes.c
··· 13 13 #include <linux/list_sort.h> 14 14 #include <linux/namei.h> 15 15 #include "misc.h" 16 - #include "ctree.h" 17 16 #include "disk-io.h" 17 + #include "extent-tree.h" 18 18 #include "transaction.h" 19 19 #include "volumes.h" 20 20 #include "raid56.h" ··· 48 48 u64 raid56_full_stripe_start; 49 49 int max_errors; 50 50 enum btrfs_map_op op; 51 + bool use_rst; 51 52 }; 52 53 53 54 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { ··· 1303 1302 struct btrfs_device *device; 1304 1303 struct btrfs_device *latest_dev = NULL; 1305 1304 struct btrfs_device *tmp_device; 1305 + s64 __maybe_unused value = 0; 1306 1306 int ret = 0; 1307 1307 1308 1308 list_for_each_entry_safe(device, tmp_device, &fs_devices->devices, ··· 1333 1331 fs_devices->latest_dev = latest_dev; 1334 1332 fs_devices->total_rw_bytes = 0; 1335 1333 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; 1334 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 1335 + fs_devices->rr_min_contig_read = BTRFS_DEFAULT_RR_MIN_CONTIG_READ; 1336 + fs_devices->read_devid = latest_dev->devid; 1337 + fs_devices->read_policy = btrfs_read_policy_to_enum(btrfs_get_mod_read_policy(), 1338 + &value); 1339 + if (fs_devices->read_policy == BTRFS_READ_POLICY_RR) 1340 + fs_devices->collect_fs_stats = true; 1341 + 1342 + if (value) { 1343 + if (fs_devices->read_policy == BTRFS_READ_POLICY_RR) 1344 + fs_devices->rr_min_contig_read = value; 1345 + if (fs_devices->read_policy == BTRFS_READ_POLICY_DEVID) 1346 + fs_devices->read_devid = value; 1347 + } 1348 + #else 1336 1349 fs_devices->read_policy = BTRFS_READ_POLICY_PID; 1350 + #endif 1337 1351 1338 1352 return 0; 1339 1353 } ··· 2067 2049 ptr = btrfs_device_fsid(dev_item); 2068 2050 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, 2069 2051 ptr, BTRFS_FSID_SIZE); 2070 - btrfs_mark_buffer_dirty(trans, leaf); 2071 2052 2072 2053 ret = 0; 2073 2054 out: ··· 2762 2745 device = btrfs_find_device(fs_info->fs_devices, &args); 2763 2746 BUG_ON(!device); /* Logic error */ 2764 2747 2765 - if (device->fs_devices->seeding) { 2748 + if (device->fs_devices->seeding) 2766 2749 btrfs_set_device_generation(leaf, dev_item, 2767 2750 device->generation); 2768 - btrfs_mark_buffer_dirty(trans, leaf); 2769 - } 2770 2751 2771 2752 path->slots[0]++; 2772 2753 goto next_slot; ··· 3057 3042 btrfs_device_get_disk_total_bytes(device)); 3058 3043 btrfs_set_device_bytes_used(leaf, dev_item, 3059 3044 btrfs_device_get_bytes_used(device)); 3060 - btrfs_mark_buffer_dirty(trans, leaf); 3061 - 3062 3045 out: 3063 3046 btrfs_free_path(path); 3064 3047 return ret; ··· 3765 3752 btrfs_set_balance_meta(leaf, item, &disk_bargs); 3766 3753 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); 3767 3754 btrfs_set_balance_sys(leaf, item, &disk_bargs); 3768 - 3769 3755 btrfs_set_balance_flags(leaf, item, bctl->flags); 3770 - 3771 - btrfs_mark_buffer_dirty(trans, leaf); 3772 3756 out: 3773 3757 btrfs_free_path(path); 3774 3758 err = btrfs_commit_transaction(trans); ··· 5527 5517 btrfs_free_chunk_map(map); 5528 5518 } 5529 5519 5520 + static int btrfs_chunk_map_cmp(const struct rb_node *new, 5521 + const struct rb_node *exist) 5522 + { 5523 + const struct btrfs_chunk_map *new_map = 5524 + rb_entry(new, struct btrfs_chunk_map, rb_node); 5525 + const struct btrfs_chunk_map *exist_map = 5526 + rb_entry(exist, struct btrfs_chunk_map, rb_node); 5527 + 5528 + if (new_map->start == exist_map->start) 5529 + return 0; 5530 + if (new_map->start < exist_map->start) 5531 + return -1; 5532 + return 1; 5533 + } 5534 + 5530 5535 EXPORT_FOR_TESTS 5531 5536 int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map) 5532 5537 { 5533 - struct rb_node **p; 5534 - struct rb_node *parent = NULL; 5535 - bool leftmost = true; 5538 + struct rb_node *exist; 5536 5539 5537 5540 write_lock(&fs_info->mapping_tree_lock); 5538 - p = &fs_info->mapping_tree.rb_root.rb_node; 5539 - while (*p) { 5540 - struct btrfs_chunk_map *entry; 5541 + exist = rb_find_add_cached(&map->rb_node, &fs_info->mapping_tree, 5542 + btrfs_chunk_map_cmp); 5541 5543 5542 - parent = *p; 5543 - entry = rb_entry(parent, struct btrfs_chunk_map, rb_node); 5544 - 5545 - if (map->start < entry->start) { 5546 - p = &(*p)->rb_left; 5547 - } else if (map->start > entry->start) { 5548 - p = &(*p)->rb_right; 5549 - leftmost = false; 5550 - } else { 5551 - write_unlock(&fs_info->mapping_tree_lock); 5552 - return -EEXIST; 5553 - } 5544 + if (exist) { 5545 + write_unlock(&fs_info->mapping_tree_lock); 5546 + return -EEXIST; 5554 5547 } 5555 - rb_link_node(&map->rb_node, parent, p); 5556 - rb_insert_color_cached(&map->rb_node, &fs_info->mapping_tree, leftmost); 5557 5548 chunk_map_device_set_bits(map, CHUNK_ALLOCATED); 5558 5549 chunk_map_device_clear_bits(map, CHUNK_TRIMMED); 5559 5550 write_unlock(&fs_info->mapping_tree_lock); ··· 5974 5963 return len; 5975 5964 } 5976 5965 5966 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 5967 + static int btrfs_read_preferred(struct btrfs_chunk_map *map, int first, int num_stripes) 5968 + { 5969 + for (int index = first; index < first + num_stripes; index++) { 5970 + const struct btrfs_device *device = map->stripes[index].dev; 5971 + 5972 + if (device->devid == READ_ONCE(device->fs_devices->read_devid)) 5973 + return index; 5974 + } 5975 + 5976 + /* If no read-preferred device is set use the first stripe. */ 5977 + return first; 5978 + } 5979 + 5980 + struct stripe_mirror { 5981 + u64 devid; 5982 + int num; 5983 + }; 5984 + 5985 + static int btrfs_cmp_devid(const void *a, const void *b) 5986 + { 5987 + const struct stripe_mirror *s1 = (const struct stripe_mirror *)a; 5988 + const struct stripe_mirror *s2 = (const struct stripe_mirror *)b; 5989 + 5990 + if (s1->devid < s2->devid) 5991 + return -1; 5992 + if (s1->devid > s2->devid) 5993 + return 1; 5994 + return 0; 5995 + } 5996 + 5997 + /* 5998 + * Select a stripe for reading using the round-robin algorithm. 5999 + * 6000 + * 1. Compute the read cycle as the total sectors read divided by the minimum 6001 + * sectors per device. 6002 + * 2. Determine the stripe number for the current read by taking the modulus 6003 + * of the read cycle with the total number of stripes: 6004 + * 6005 + * stripe index = (total sectors / min sectors per dev) % num stripes 6006 + * 6007 + * The calculated stripe index is then used to select the corresponding device 6008 + * from the list of devices, which is ordered by devid. 6009 + */ 6010 + static int btrfs_read_rr(const struct btrfs_chunk_map *map, int first, int num_stripes) 6011 + { 6012 + struct stripe_mirror stripes[BTRFS_RAID1_MAX_MIRRORS] = { 0 }; 6013 + struct btrfs_device *device = map->stripes[first].dev; 6014 + struct btrfs_fs_info *fs_info = device->fs_devices->fs_info; 6015 + unsigned int read_cycle; 6016 + unsigned int total_reads; 6017 + unsigned int min_reads_per_dev; 6018 + 6019 + total_reads = percpu_counter_sum(&fs_info->stats_read_blocks); 6020 + min_reads_per_dev = READ_ONCE(fs_info->fs_devices->rr_min_contig_read) >> 6021 + fs_info->sectorsize_bits; 6022 + 6023 + for (int index = 0, i = first; i < first + num_stripes; i++) { 6024 + stripes[index].devid = map->stripes[i].dev->devid; 6025 + stripes[index].num = i; 6026 + index++; 6027 + } 6028 + sort(stripes, num_stripes, sizeof(struct stripe_mirror), 6029 + btrfs_cmp_devid, NULL); 6030 + 6031 + read_cycle = total_reads / min_reads_per_dev; 6032 + return stripes[read_cycle % num_stripes].num; 6033 + } 6034 + #endif 6035 + 5977 6036 static int find_live_mirror(struct btrfs_fs_info *fs_info, 5978 6037 struct btrfs_chunk_map *map, int first, 5979 6038 int dev_replace_is_ongoing) ··· 6073 5992 case BTRFS_READ_POLICY_PID: 6074 5993 preferred_mirror = first + (current->pid % num_stripes); 6075 5994 break; 5995 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 5996 + case BTRFS_READ_POLICY_RR: 5997 + preferred_mirror = btrfs_read_rr(map, first, num_stripes); 5998 + break; 5999 + case BTRFS_READ_POLICY_DEVID: 6000 + preferred_mirror = btrfs_read_preferred(map, first, num_stripes); 6001 + break; 6002 + #endif 6076 6003 } 6077 6004 6078 6005 if (dev_replace_is_ongoing && ··· 6439 6350 { 6440 6351 dst->dev = map->stripes[io_geom->stripe_index].dev; 6441 6352 6442 - if (io_geom->op == BTRFS_MAP_READ && 6443 - btrfs_need_stripe_tree_update(fs_info, map->type)) 6353 + if (io_geom->op == BTRFS_MAP_READ && io_geom->use_rst) 6444 6354 return btrfs_get_raid_extent_offset(fs_info, logical, length, 6445 6355 map->type, 6446 6356 io_geom->stripe_index, dst); ··· 6454 6366 const struct btrfs_io_stripe *smap, 6455 6367 const struct btrfs_chunk_map *map, 6456 6368 int num_alloc_stripes, 6457 - enum btrfs_map_op op, int mirror_num) 6369 + struct btrfs_io_geometry *io_geom) 6458 6370 { 6459 6371 if (!smap) 6460 6372 return false; ··· 6462 6374 if (num_alloc_stripes != 1) 6463 6375 return false; 6464 6376 6465 - if (btrfs_need_stripe_tree_update(fs_info, map->type) && op != BTRFS_MAP_READ) 6377 + if (io_geom->use_rst && io_geom->op != BTRFS_MAP_READ) 6466 6378 return false; 6467 6379 6468 - if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1) 6380 + if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && io_geom->mirror_num > 1) 6469 6381 return false; 6470 6382 6471 6383 return true; ··· 6671 6583 io_geom.raid56_full_stripe_start = (u64)-1; 6672 6584 max_len = btrfs_max_io_len(map, map_offset, &io_geom); 6673 6585 *length = min_t(u64, map->chunk_len - map_offset, max_len); 6586 + io_geom.use_rst = btrfs_need_stripe_tree_update(fs_info, map->type); 6674 6587 6675 6588 if (dev_replace->replace_task != current) 6676 6589 down_read(&dev_replace->rwsem); ··· 6740 6651 * physical block information on the stack instead of allocating an 6741 6652 * I/O context structure. 6742 6653 */ 6743 - if (is_single_device_io(fs_info, smap, map, num_alloc_stripes, op, 6744 - io_geom.mirror_num)) { 6654 + if (is_single_device_io(fs_info, smap, map, num_alloc_stripes, &io_geom)) { 6745 6655 ret = set_io_stripe(fs_info, logical, length, smap, map, &io_geom); 6746 6656 if (mirror_num_ret) 6747 6657 *mirror_num_ret = io_geom.mirror_num; ··· 6754 6666 goto out; 6755 6667 } 6756 6668 bioc->map_type = map->type; 6669 + bioc->use_rst = io_geom.use_rst; 6757 6670 6758 6671 /* 6759 6672 * For RAID56 full map, we need to make sure the stripes[] follows the ··· 7095 7006 warn_32bit_meta_chunk(fs_info, logical, length, type); 7096 7007 #endif 7097 7008 7098 - /* 7099 - * Only need to verify chunk item if we're reading from sys chunk array, 7100 - * as chunk item in tree block is already verified by tree-checker. 7101 - */ 7102 - if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { 7103 - ret = btrfs_check_chunk_valid(leaf, chunk, logical); 7104 - if (ret) 7105 - return ret; 7106 - } 7107 - 7108 7009 map = btrfs_find_chunk_map(fs_info, logical, 1); 7109 7010 7110 7011 /* already mapped? */ ··· 7352 7273 { 7353 7274 struct btrfs_super_block *super_copy = fs_info->super_copy; 7354 7275 struct extent_buffer *sb; 7355 - struct btrfs_disk_key *disk_key; 7356 - struct btrfs_chunk *chunk; 7357 7276 u8 *array_ptr; 7358 7277 unsigned long sb_array_offset; 7359 7278 int ret = 0; 7360 - u32 num_stripes; 7361 7279 u32 array_size; 7362 - u32 len = 0; 7363 7280 u32 cur_offset; 7364 - u64 type; 7365 7281 struct btrfs_key key; 7366 7282 7367 7283 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); ··· 7379 7305 cur_offset = 0; 7380 7306 7381 7307 while (cur_offset < array_size) { 7382 - disk_key = (struct btrfs_disk_key *)array_ptr; 7383 - len = sizeof(*disk_key); 7384 - if (cur_offset + len > array_size) 7385 - goto out_short_read; 7308 + struct btrfs_chunk *chunk; 7309 + struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)array_ptr; 7310 + u32 len = sizeof(*disk_key); 7311 + 7312 + /* 7313 + * The sys_chunk_array has been already verified at super block 7314 + * read time. Only do ASSERT()s for basic checks. 7315 + */ 7316 + ASSERT(cur_offset + len <= array_size); 7386 7317 7387 7318 btrfs_disk_key_to_cpu(&key, disk_key); 7388 7319 ··· 7395 7316 sb_array_offset += len; 7396 7317 cur_offset += len; 7397 7318 7398 - if (key.type != BTRFS_CHUNK_ITEM_KEY) { 7399 - btrfs_err(fs_info, 7400 - "unexpected item type %u in sys_array at offset %u", 7401 - (u32)key.type, cur_offset); 7402 - ret = -EIO; 7403 - break; 7404 - } 7319 + ASSERT(key.type == BTRFS_CHUNK_ITEM_KEY); 7405 7320 7406 7321 chunk = (struct btrfs_chunk *)sb_array_offset; 7407 - /* 7408 - * At least one btrfs_chunk with one stripe must be present, 7409 - * exact stripe count check comes afterwards 7410 - */ 7411 - len = btrfs_chunk_item_size(1); 7412 - if (cur_offset + len > array_size) 7413 - goto out_short_read; 7322 + ASSERT(btrfs_chunk_type(sb, chunk) & BTRFS_BLOCK_GROUP_SYSTEM); 7414 7323 7415 - num_stripes = btrfs_chunk_num_stripes(sb, chunk); 7416 - if (!num_stripes) { 7417 - btrfs_err(fs_info, 7418 - "invalid number of stripes %u in sys_array at offset %u", 7419 - num_stripes, cur_offset); 7420 - ret = -EIO; 7421 - break; 7422 - } 7324 + len = btrfs_chunk_item_size(btrfs_chunk_num_stripes(sb, chunk)); 7423 7325 7424 - type = btrfs_chunk_type(sb, chunk); 7425 - if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { 7426 - btrfs_err(fs_info, 7427 - "invalid chunk type %llu in sys_array at offset %u", 7428 - type, cur_offset); 7429 - ret = -EIO; 7430 - break; 7431 - } 7432 - 7433 - len = btrfs_chunk_item_size(num_stripes); 7434 - if (cur_offset + len > array_size) 7435 - goto out_short_read; 7326 + ASSERT(cur_offset + len <= array_size); 7436 7327 7437 7328 ret = read_one_chunk(&key, sb, chunk); 7438 7329 if (ret) ··· 7415 7366 clear_extent_buffer_uptodate(sb); 7416 7367 free_extent_buffer_stale(sb); 7417 7368 return ret; 7418 - 7419 - out_short_read: 7420 - btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u", 7421 - len, cur_offset); 7422 - clear_extent_buffer_uptodate(sb); 7423 - free_extent_buffer_stale(sb); 7424 - return -EIO; 7425 7369 } 7426 7370 7427 7371 /* ··· 7614 7572 struct btrfs_device *device; 7615 7573 int ret = 0; 7616 7574 7617 - fs_devices->fs_info = fs_info; 7618 - 7619 7575 mutex_lock(&fs_devices->device_list_mutex); 7620 7576 list_for_each_entry(device, &fs_devices->devices, dev_list) 7621 7577 device->fs_info = fs_info; ··· 7789 7749 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) 7790 7750 btrfs_set_dev_stats_value(eb, ptr, i, 7791 7751 btrfs_dev_stat_read(device, i)); 7792 - btrfs_mark_buffer_dirty(trans, eb); 7793 - 7794 7752 out: 7795 7753 btrfs_free_path(path); 7796 7754 return ret;
+21
fs/btrfs/volumes.h
··· 296 296 BTRFS_CHUNK_ALLOC_ZONED, 297 297 }; 298 298 299 + #define BTRFS_DEFAULT_RR_MIN_CONTIG_READ (SZ_256K) 300 + /* Keep in sync with raid_attr table, current maximum is RAID1C4. */ 301 + #define BTRFS_RAID1_MAX_MIRRORS (4) 299 302 /* 300 303 * Read policies for mirrored block group profiles, read picks the stripe based 301 304 * on these policies. ··· 306 303 enum btrfs_read_policy { 307 304 /* Use process PID to choose the stripe */ 308 305 BTRFS_READ_POLICY_PID, 306 + #ifdef CONFIG_BTRFS_EXPERIMENTAL 307 + /* Balancing RAID1 reads across all striped devices (round-robin). */ 308 + BTRFS_READ_POLICY_RR, 309 + /* Read from a specific device. */ 310 + BTRFS_READ_POLICY_DEVID, 311 + #endif 309 312 BTRFS_NR_READ_POLICY, 310 313 }; 311 314 ··· 426 417 bool seeding; 427 418 /* The mount needs to use a randomly generated fsid. */ 428 419 bool temp_fsid; 420 + /* Enable/disable the filesystem stats tracking. */ 421 + bool collect_fs_stats; 429 422 430 423 struct btrfs_fs_info *fs_info; 431 424 /* sysfs kobjects */ ··· 442 431 enum btrfs_read_policy read_policy; 443 432 444 433 #ifdef CONFIG_BTRFS_EXPERIMENTAL 434 + /* 435 + * Minimum contiguous reads before switching to next device, the unit 436 + * is one block/sectorsize. 437 + */ 438 + u32 rr_min_contig_read; 439 + 440 + /* Device to be used for reading in case of RAID1. */ 441 + u64 read_devid; 442 + 445 443 /* Checksum mode - offload it or do it synchronously. */ 446 444 enum btrfs_offload_csum_mode offload_csum_mode; 447 445 #endif ··· 505 485 struct bio *orig_bio; 506 486 atomic_t error; 507 487 u16 max_errors; 488 + bool use_rst; 508 489 509 490 u64 logical; 510 491 u64 size;
-1
fs/btrfs/xattr.c
··· 204 204 btrfs_set_dir_data_len(leaf, di, size); 205 205 data_ptr = ((unsigned long)(di + 1)) + name_len; 206 206 write_extent_buffer(leaf, value, data_ptr, size); 207 - btrfs_mark_buffer_dirty(trans, leaf); 208 207 } else { 209 208 /* 210 209 * Insert, and we had space for the xattr, so path->slots[0] is
+124
fs/btrfs/zoned.c
··· 2652 2652 } 2653 2653 spin_unlock(&fs_info->zone_active_bgs_lock); 2654 2654 } 2655 + 2656 + /* 2657 + * Reset the zones of unused block groups from @space_info->bytes_zone_unusable. 2658 + * 2659 + * @space_info: the space to work on 2660 + * @num_bytes: targeting reclaim bytes 2661 + * 2662 + * This one resets the zones of a block group, so we can reuse the region 2663 + * without removing the block group. On the other hand, btrfs_delete_unused_bgs() 2664 + * just removes a block group and frees up the underlying zones. So, we still 2665 + * need to allocate a new block group to reuse the zones. 2666 + * 2667 + * Resetting is faster than deleting/recreating a block group. It is similar 2668 + * to freeing the logical space on the regular mode. However, we cannot change 2669 + * the block group's profile with this operation. 2670 + */ 2671 + int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes) 2672 + { 2673 + struct btrfs_fs_info *fs_info = space_info->fs_info; 2674 + const sector_t zone_size_sectors = fs_info->zone_size >> SECTOR_SHIFT; 2675 + 2676 + if (!btrfs_is_zoned(fs_info)) 2677 + return 0; 2678 + 2679 + while (num_bytes > 0) { 2680 + struct btrfs_chunk_map *map; 2681 + struct btrfs_block_group *bg = NULL; 2682 + bool found = false; 2683 + u64 reclaimed = 0; 2684 + 2685 + /* 2686 + * Here, we choose a fully zone_unusable block group. It's 2687 + * technically possible to reset a partly zone_unusable block 2688 + * group, which still has some free space left. However, 2689 + * handling that needs to cope with the allocation side, which 2690 + * makes the logic more complex. So, let's handle the easy case 2691 + * for now. 2692 + */ 2693 + spin_lock(&fs_info->unused_bgs_lock); 2694 + list_for_each_entry(bg, &fs_info->unused_bgs, bg_list) { 2695 + if ((bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != space_info->flags) 2696 + continue; 2697 + 2698 + /* 2699 + * Use trylock to avoid locking order violation. In 2700 + * btrfs_reclaim_bgs_work(), the lock order is 2701 + * &bg->lock -> &fs_info->unused_bgs_lock. We skip a 2702 + * block group if we cannot take its lock. 2703 + */ 2704 + if (!spin_trylock(&bg->lock)) 2705 + continue; 2706 + if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length) { 2707 + spin_unlock(&bg->lock); 2708 + continue; 2709 + } 2710 + spin_unlock(&bg->lock); 2711 + found = true; 2712 + break; 2713 + } 2714 + if (!found) { 2715 + spin_unlock(&fs_info->unused_bgs_lock); 2716 + return 0; 2717 + } 2718 + 2719 + list_del_init(&bg->bg_list); 2720 + btrfs_put_block_group(bg); 2721 + spin_unlock(&fs_info->unused_bgs_lock); 2722 + 2723 + /* 2724 + * Since the block group is fully zone_unusable and we cannot 2725 + * allocate from this block group anymore, we don't need to set 2726 + * this block group read-only. 2727 + */ 2728 + 2729 + down_read(&fs_info->dev_replace.rwsem); 2730 + map = bg->physical_map; 2731 + for (int i = 0; i < map->num_stripes; i++) { 2732 + struct btrfs_io_stripe *stripe = &map->stripes[i]; 2733 + unsigned int nofs_flags; 2734 + int ret; 2735 + 2736 + nofs_flags = memalloc_nofs_save(); 2737 + ret = blkdev_zone_mgmt(stripe->dev->bdev, REQ_OP_ZONE_RESET, 2738 + stripe->physical >> SECTOR_SHIFT, 2739 + zone_size_sectors); 2740 + memalloc_nofs_restore(nofs_flags); 2741 + 2742 + if (ret) { 2743 + up_read(&fs_info->dev_replace.rwsem); 2744 + return ret; 2745 + } 2746 + } 2747 + up_read(&fs_info->dev_replace.rwsem); 2748 + 2749 + spin_lock(&space_info->lock); 2750 + spin_lock(&bg->lock); 2751 + ASSERT(!btrfs_is_block_group_used(bg)); 2752 + if (bg->ro) { 2753 + spin_unlock(&bg->lock); 2754 + spin_unlock(&space_info->lock); 2755 + continue; 2756 + } 2757 + 2758 + reclaimed = bg->alloc_offset; 2759 + bg->zone_unusable = bg->length - bg->zone_capacity; 2760 + bg->alloc_offset = 0; 2761 + /* 2762 + * This holds because we currently reset fully used then freed 2763 + * block group. 2764 + */ 2765 + ASSERT(reclaimed == bg->zone_capacity); 2766 + bg->free_space_ctl->free_space += reclaimed; 2767 + space_info->bytes_zone_unusable -= reclaimed; 2768 + spin_unlock(&bg->lock); 2769 + btrfs_return_free_space(space_info, reclaimed); 2770 + spin_unlock(&space_info->lock); 2771 + 2772 + if (num_bytes <= reclaimed) 2773 + break; 2774 + num_bytes -= reclaimed; 2775 + } 2776 + 2777 + return 0; 2778 + }
+7
fs/btrfs/zoned.h
··· 96 96 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info, 97 97 struct btrfs_space_info *space_info, bool do_finish); 98 98 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info); 99 + int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes); 99 100 #else /* CONFIG_BLK_DEV_ZONED */ 100 101 101 102 static inline int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info) ··· 265 264 } 266 265 267 266 static inline void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) { } 267 + 268 + static inline int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, 269 + u64 num_bytes) 270 + { 271 + return 0; 272 + } 268 273 269 274 #endif 270 275
+37
include/linux/rbtree.h
··· 211 211 } 212 212 213 213 /** 214 + * rb_find_add_cached() - find equivalent @node in @tree, or add @node 215 + * @node: node to look-for / insert 216 + * @tree: tree to search / modify 217 + * @cmp: operator defining the node order 218 + * 219 + * Returns the rb_node matching @node, or NULL when no match is found and @node 220 + * is inserted. 221 + */ 222 + static __always_inline struct rb_node * 223 + rb_find_add_cached(struct rb_node *node, struct rb_root_cached *tree, 224 + int (*cmp)(const struct rb_node *new, const struct rb_node *exist)) 225 + { 226 + bool leftmost = true; 227 + struct rb_node **link = &tree->rb_root.rb_node; 228 + struct rb_node *parent = NULL; 229 + int c; 230 + 231 + while (*link) { 232 + parent = *link; 233 + c = cmp(node, parent); 234 + 235 + if (c < 0) { 236 + link = &parent->rb_left; 237 + } else if (c > 0) { 238 + link = &parent->rb_right; 239 + leftmost = false; 240 + } else { 241 + return parent; 242 + } 243 + } 244 + 245 + rb_link_node(node, parent, link); 246 + rb_insert_color_cached(node, tree, leftmost); 247 + return NULL; 248 + } 249 + 250 + /** 214 251 * rb_find_add() - find equivalent @node in @tree, or add @node 215 252 * @node: node to look-for / insert 216 253 * @tree: tree to search / modify
+2 -1
include/trace/events/btrfs.h
··· 100 100 EM( ALLOC_CHUNK, "ALLOC_CHUNK") \ 101 101 EM( ALLOC_CHUNK_FORCE, "ALLOC_CHUNK_FORCE") \ 102 102 EM( RUN_DELAYED_IPUTS, "RUN_DELAYED_IPUTS") \ 103 - EMe(COMMIT_TRANS, "COMMIT_TRANS") 103 + EM( COMMIT_TRANS, "COMMIT_TRANS") \ 104 + EMe(RESET_ZONES, "RESET_ZONES") 104 105 105 106 /* 106 107 * First define the enums in the above macros to be exported to userspace via