1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
5 #include <linux/blkdev.h>
6 #include <linux/sched/mm.h>
7 #include <linux/atomic.h>
8 #include <linux/vmalloc.h>
12 #include "rcu-string.h"
14 #include "block-group.h"
15 #include "transaction.h"
16 #include "dev-replace.h"
17 #include "space-info.h"
20 #include "accessors.h"
23 /* Maximum number of zones to report per blkdev_report_zones() call */
24 #define BTRFS_REPORT_NR_ZONES 4096
25 /* Invalid allocation pointer value for missing devices */
26 #define WP_MISSING_DEV ((u64)-1)
27 /* Pseudo write pointer value for conventional zone */
28 #define WP_CONVENTIONAL ((u64)-2)
31 * Location of the first zone of superblock logging zone pairs.
33 * - primary superblock: 0B (zone 0)
34 * - first copy: 512G (zone starting at that offset)
35 * - second copy: 4T (zone starting at that offset)
37 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
38 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
39 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
41 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
42 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
44 /* Number of superblock log zones */
45 #define BTRFS_NR_SB_LOG_ZONES 2
48 * Minimum of active zones we need:
50 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
51 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
52 * - 1 zone for tree-log dedicated block group
53 * - 1 zone for relocation
55 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
58 * Minimum / maximum supported zone size. Currently, SMR disks have a zone
59 * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
60 * We do not expect the zone size to become larger than 8GiB or smaller than
61 * 4MiB in the near future.
63 #define BTRFS_MAX_ZONE_SIZE SZ_8G
64 #define BTRFS_MIN_ZONE_SIZE SZ_4M
66 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
68 static void wait_eb_writebacks(struct btrfs_block_group *block_group);
69 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written);
71 static inline bool sb_zone_is_full(const struct blk_zone *zone)
73 return (zone->cond == BLK_ZONE_COND_FULL) ||
74 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
77 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
79 struct blk_zone *zones = data;
81 memcpy(&zones[idx], zone, sizeof(*zone));
86 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
89 bool empty[BTRFS_NR_SB_LOG_ZONES];
90 bool full[BTRFS_NR_SB_LOG_ZONES];
94 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
95 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
96 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
97 full[i] = sb_zone_is_full(&zones[i]);
101 * Possible states of log buffer zones
103 * Empty[0] In use[0] Full[0]
109 * *: Special case, no superblock is written
110 * 0: Use write pointer of zones[0]
111 * 1: Use write pointer of zones[1]
112 * C: Compare super blocks from zones[0] and zones[1], use the latest
113 * one determined by generation
117 if (empty[0] && empty[1]) {
118 /* Special case to distinguish no superblock to read */
119 *wp_ret = zones[0].start << SECTOR_SHIFT;
121 } else if (full[0] && full[1]) {
122 /* Compare two super blocks */
123 struct address_space *mapping = bdev->bd_inode->i_mapping;
124 struct page *page[BTRFS_NR_SB_LOG_ZONES];
125 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
128 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
129 u64 zone_end = (zones[i].start + zones[i].capacity) << SECTOR_SHIFT;
130 u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) -
131 BTRFS_SUPER_INFO_SIZE;
133 page[i] = read_cache_page_gfp(mapping,
134 bytenr >> PAGE_SHIFT, GFP_NOFS);
135 if (IS_ERR(page[i])) {
137 btrfs_release_disk_super(super[0]);
138 return PTR_ERR(page[i]);
140 super[i] = page_address(page[i]);
143 if (btrfs_super_generation(super[0]) >
144 btrfs_super_generation(super[1]))
145 sector = zones[1].start;
147 sector = zones[0].start;
149 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
150 btrfs_release_disk_super(super[i]);
151 } else if (!full[0] && (empty[1] || full[1])) {
152 sector = zones[0].wp;
153 } else if (full[0]) {
154 sector = zones[1].wp;
158 *wp_ret = sector << SECTOR_SHIFT;
163 * Get the first zone number of the superblock mirror
165 static inline u32 sb_zone_number(int shift, int mirror)
169 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
171 case 0: zone = 0; break;
172 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
173 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
176 ASSERT(zone <= U32_MAX);
181 static inline sector_t zone_start_sector(u32 zone_number,
182 struct block_device *bdev)
184 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
187 static inline u64 zone_start_physical(u32 zone_number,
188 struct btrfs_zoned_device_info *zone_info)
190 return (u64)zone_number << zone_info->zone_size_shift;
194 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
195 * device into static sized chunks and fake a conventional zone on each of
198 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
199 struct blk_zone *zones, unsigned int nr_zones)
201 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
202 sector_t bdev_size = bdev_nr_sectors(device->bdev);
205 pos >>= SECTOR_SHIFT;
206 for (i = 0; i < nr_zones; i++) {
207 zones[i].start = i * zone_sectors + pos;
208 zones[i].len = zone_sectors;
209 zones[i].capacity = zone_sectors;
210 zones[i].wp = zones[i].start + zone_sectors;
211 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
212 zones[i].cond = BLK_ZONE_COND_NOT_WP;
214 if (zones[i].wp >= bdev_size) {
223 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
224 struct blk_zone *zones, unsigned int *nr_zones)
226 struct btrfs_zoned_device_info *zinfo = device->zone_info;
232 if (!bdev_is_zoned(device->bdev)) {
233 ret = emulate_report_zones(device, pos, zones, *nr_zones);
239 if (zinfo->zone_cache) {
243 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
244 zno = pos >> zinfo->zone_size_shift;
246 * We cannot report zones beyond the zone end. So, it is OK to
247 * cap *nr_zones to at the end.
249 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
251 for (i = 0; i < *nr_zones; i++) {
252 struct blk_zone *zone_info;
254 zone_info = &zinfo->zone_cache[zno + i];
259 if (i == *nr_zones) {
260 /* Cache hit on all the zones */
261 memcpy(zones, zinfo->zone_cache + zno,
262 sizeof(*zinfo->zone_cache) * *nr_zones);
267 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
268 copy_zone_info_cb, zones);
270 btrfs_err_in_rcu(device->fs_info,
271 "zoned: failed to read zone %llu on %s (devid %llu)",
272 pos, rcu_str_deref(device->name),
281 if (zinfo->zone_cache) {
282 u32 zno = pos >> zinfo->zone_size_shift;
284 memcpy(zinfo->zone_cache + zno, zones,
285 sizeof(*zinfo->zone_cache) * *nr_zones);
291 /* The emulated zone size is determined from the size of device extent */
292 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
294 struct btrfs_path *path;
295 struct btrfs_root *root = fs_info->dev_root;
296 struct btrfs_key key;
297 struct extent_buffer *leaf;
298 struct btrfs_dev_extent *dext;
302 key.type = BTRFS_DEV_EXTENT_KEY;
305 path = btrfs_alloc_path();
309 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
313 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
314 ret = btrfs_next_leaf(root, path);
317 /* No dev extents at all? Not good */
324 leaf = path->nodes[0];
325 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
326 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
330 btrfs_free_path(path);
335 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
337 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
338 struct btrfs_device *device;
341 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
342 if (!btrfs_fs_incompat(fs_info, ZONED))
345 mutex_lock(&fs_devices->device_list_mutex);
346 list_for_each_entry(device, &fs_devices->devices, dev_list) {
347 /* We can skip reading of zone info for missing devices */
351 ret = btrfs_get_dev_zone_info(device, true);
355 mutex_unlock(&fs_devices->device_list_mutex);
360 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
362 struct btrfs_fs_info *fs_info = device->fs_info;
363 struct btrfs_zoned_device_info *zone_info = NULL;
364 struct block_device *bdev = device->bdev;
365 unsigned int max_active_zones;
366 unsigned int nactive;
369 struct blk_zone *zones = NULL;
370 unsigned int i, nreported = 0, nr_zones;
371 sector_t zone_sectors;
372 char *model, *emulated;
376 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
379 if (!btrfs_fs_incompat(fs_info, ZONED))
382 if (device->zone_info)
385 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
389 device->zone_info = zone_info;
391 if (!bdev_is_zoned(bdev)) {
392 if (!fs_info->zone_size) {
393 ret = calculate_emulated_zone_size(fs_info);
398 ASSERT(fs_info->zone_size);
399 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
401 zone_sectors = bdev_zone_sectors(bdev);
404 ASSERT(is_power_of_two_u64(zone_sectors));
405 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
407 /* We reject devices with a zone size larger than 8GB */
408 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
409 btrfs_err_in_rcu(fs_info,
410 "zoned: %s: zone size %llu larger than supported maximum %llu",
411 rcu_str_deref(device->name),
412 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
415 } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
416 btrfs_err_in_rcu(fs_info,
417 "zoned: %s: zone size %llu smaller than supported minimum %u",
418 rcu_str_deref(device->name),
419 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
424 nr_sectors = bdev_nr_sectors(bdev);
425 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
426 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
427 if (!IS_ALIGNED(nr_sectors, zone_sectors))
428 zone_info->nr_zones++;
430 max_active_zones = bdev_max_active_zones(bdev);
431 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
432 btrfs_err_in_rcu(fs_info,
433 "zoned: %s: max active zones %u is too small, need at least %u active zones",
434 rcu_str_deref(device->name), max_active_zones,
435 BTRFS_MIN_ACTIVE_ZONES);
439 zone_info->max_active_zones = max_active_zones;
441 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
442 if (!zone_info->seq_zones) {
447 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
448 if (!zone_info->empty_zones) {
453 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
454 if (!zone_info->active_zones) {
459 zones = kvcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
466 * Enable zone cache only for a zoned device. On a non-zoned device, we
467 * fill the zone info with emulated CONVENTIONAL zones, so no need to
470 if (populate_cache && bdev_is_zoned(device->bdev)) {
471 zone_info->zone_cache = vcalloc(zone_info->nr_zones,
472 sizeof(struct blk_zone));
473 if (!zone_info->zone_cache) {
474 btrfs_err_in_rcu(device->fs_info,
475 "zoned: failed to allocate zone cache for %s",
476 rcu_str_deref(device->name));
484 while (sector < nr_sectors) {
485 nr_zones = BTRFS_REPORT_NR_ZONES;
486 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
491 for (i = 0; i < nr_zones; i++) {
492 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
493 __set_bit(nreported, zone_info->seq_zones);
494 switch (zones[i].cond) {
495 case BLK_ZONE_COND_EMPTY:
496 __set_bit(nreported, zone_info->empty_zones);
498 case BLK_ZONE_COND_IMP_OPEN:
499 case BLK_ZONE_COND_EXP_OPEN:
500 case BLK_ZONE_COND_CLOSED:
501 __set_bit(nreported, zone_info->active_zones);
507 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
510 if (nreported != zone_info->nr_zones) {
511 btrfs_err_in_rcu(device->fs_info,
512 "inconsistent number of zones on %s (%u/%u)",
513 rcu_str_deref(device->name), nreported,
514 zone_info->nr_zones);
519 if (max_active_zones) {
520 if (nactive > max_active_zones) {
521 btrfs_err_in_rcu(device->fs_info,
522 "zoned: %u active zones on %s exceeds max_active_zones %u",
523 nactive, rcu_str_deref(device->name),
528 atomic_set(&zone_info->active_zones_left,
529 max_active_zones - nactive);
530 set_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags);
533 /* Validate superblock log */
534 nr_zones = BTRFS_NR_SB_LOG_ZONES;
535 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
538 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
540 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
541 if (sb_zone + 1 >= zone_info->nr_zones)
544 ret = btrfs_get_dev_zones(device,
545 zone_start_physical(sb_zone, zone_info),
546 &zone_info->sb_zones[sb_pos],
551 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
552 btrfs_err_in_rcu(device->fs_info,
553 "zoned: failed to read super block log zone info at devid %llu zone %u",
554 device->devid, sb_zone);
560 * If zones[0] is conventional, always use the beginning of the
561 * zone to record superblock. No need to validate in that case.
563 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
564 BLK_ZONE_TYPE_CONVENTIONAL)
567 ret = sb_write_pointer(device->bdev,
568 &zone_info->sb_zones[sb_pos], &sb_wp);
569 if (ret != -ENOENT && ret) {
570 btrfs_err_in_rcu(device->fs_info,
571 "zoned: super block log zone corrupted devid %llu zone %u",
572 device->devid, sb_zone);
581 if (bdev_is_zoned(bdev)) {
582 model = "host-managed zoned";
586 emulated = "emulated ";
589 btrfs_info_in_rcu(fs_info,
590 "%s block device %s, %u %szones of %llu bytes",
591 model, rcu_str_deref(device->name), zone_info->nr_zones,
592 emulated, zone_info->zone_size);
598 btrfs_destroy_dev_zone_info(device);
602 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
604 struct btrfs_zoned_device_info *zone_info = device->zone_info;
609 bitmap_free(zone_info->active_zones);
610 bitmap_free(zone_info->seq_zones);
611 bitmap_free(zone_info->empty_zones);
612 vfree(zone_info->zone_cache);
614 device->zone_info = NULL;
617 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
619 struct btrfs_zoned_device_info *zone_info;
621 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
625 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
626 if (!zone_info->seq_zones)
629 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
630 zone_info->nr_zones);
632 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
633 if (!zone_info->empty_zones)
636 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
637 zone_info->nr_zones);
639 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
640 if (!zone_info->active_zones)
643 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
644 zone_info->nr_zones);
645 zone_info->zone_cache = NULL;
650 bitmap_free(zone_info->seq_zones);
651 bitmap_free(zone_info->empty_zones);
652 bitmap_free(zone_info->active_zones);
657 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
658 struct blk_zone *zone)
660 unsigned int nr_zones = 1;
663 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
664 if (ret != 0 || !nr_zones)
665 return ret ? ret : -EIO;
670 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
672 struct btrfs_device *device;
674 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
675 if (device->bdev && bdev_is_zoned(device->bdev)) {
677 "zoned: mode not enabled but zoned device found: %pg",
686 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
688 struct queue_limits *lim = &fs_info->limits;
689 struct btrfs_device *device;
694 * Host-Managed devices can't be used without the ZONED flag. With the
695 * ZONED all devices can be used, using zone emulation if required.
697 if (!btrfs_fs_incompat(fs_info, ZONED))
698 return btrfs_check_for_zoned_device(fs_info);
700 blk_set_stacking_limits(lim);
702 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
703 struct btrfs_zoned_device_info *zone_info = device->zone_info;
709 zone_size = zone_info->zone_size;
710 } else if (zone_info->zone_size != zone_size) {
712 "zoned: unequal block device zone sizes: have %llu found %llu",
713 zone_info->zone_size, zone_size);
718 * With the zoned emulation, we can have non-zoned device on the
719 * zoned mode. In this case, we don't have a valid max zone
722 if (bdev_is_zoned(device->bdev)) {
723 blk_stack_limits(lim,
724 &bdev_get_queue(device->bdev)->limits,
730 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
731 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
732 * check the alignment here.
734 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
736 "zoned: zone size %llu not aligned to stripe %u",
737 zone_size, BTRFS_STRIPE_LEN);
741 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
742 btrfs_err(fs_info, "zoned: mixed block groups not supported");
746 fs_info->zone_size = zone_size;
748 * Also limit max_zone_append_size by max_segments * PAGE_SIZE.
749 * Technically, we can have multiple pages per segment. But, since
750 * we add the pages one by one to a bio, and cannot increase the
751 * metadata reservation even if it increases the number of extents, it
752 * is safe to stick with the limit.
754 fs_info->max_zone_append_size = ALIGN_DOWN(
755 min3((u64)lim->max_zone_append_sectors << SECTOR_SHIFT,
756 (u64)lim->max_sectors << SECTOR_SHIFT,
757 (u64)lim->max_segments << PAGE_SHIFT),
758 fs_info->sectorsize);
759 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
760 if (fs_info->max_zone_append_size < fs_info->max_extent_size)
761 fs_info->max_extent_size = fs_info->max_zone_append_size;
764 * Check mount options here, because we might change fs_info->zoned
765 * from fs_info->zone_size.
767 ret = btrfs_check_mountopts_zoned(fs_info, &fs_info->mount_opt);
771 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
775 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info, unsigned long *mount_opt)
777 if (!btrfs_is_zoned(info))
781 * Space cache writing is not COWed. Disable that to avoid write errors
782 * in sequential zones.
784 if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
785 btrfs_err(info, "zoned: space cache v1 is not supported");
789 if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) {
790 btrfs_err(info, "zoned: NODATACOW not supported");
794 if (btrfs_raw_test_opt(*mount_opt, DISCARD_ASYNC)) {
796 "zoned: async discard ignored and disabled for zoned mode");
797 btrfs_clear_opt(*mount_opt, DISCARD_ASYNC);
803 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
804 int rw, u64 *bytenr_ret)
809 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
810 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
814 ret = sb_write_pointer(bdev, zones, &wp);
815 if (ret != -ENOENT && ret < 0)
819 struct blk_zone *reset = NULL;
821 if (wp == zones[0].start << SECTOR_SHIFT)
823 else if (wp == zones[1].start << SECTOR_SHIFT)
826 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
827 ASSERT(sb_zone_is_full(reset));
829 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
830 reset->start, reset->len,
835 reset->cond = BLK_ZONE_COND_EMPTY;
836 reset->wp = reset->start;
838 } else if (ret != -ENOENT) {
840 * For READ, we want the previous one. Move write pointer to
841 * the end of a zone, if it is at the head of a zone.
845 if (wp == zones[0].start << SECTOR_SHIFT)
846 zone_end = zones[1].start + zones[1].capacity;
847 else if (wp == zones[1].start << SECTOR_SHIFT)
848 zone_end = zones[0].start + zones[0].capacity;
850 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
851 BTRFS_SUPER_INFO_SIZE);
853 wp -= BTRFS_SUPER_INFO_SIZE;
861 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
864 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
865 sector_t zone_sectors;
868 u8 zone_sectors_shift;
872 if (!bdev_is_zoned(bdev)) {
873 *bytenr_ret = btrfs_sb_offset(mirror);
877 ASSERT(rw == READ || rw == WRITE);
879 zone_sectors = bdev_zone_sectors(bdev);
880 if (!is_power_of_2(zone_sectors))
882 zone_sectors_shift = ilog2(zone_sectors);
883 nr_sectors = bdev_nr_sectors(bdev);
884 nr_zones = nr_sectors >> zone_sectors_shift;
886 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
887 if (sb_zone + 1 >= nr_zones)
890 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
891 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
895 if (ret != BTRFS_NR_SB_LOG_ZONES)
898 return sb_log_location(bdev, zones, rw, bytenr_ret);
901 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
904 struct btrfs_zoned_device_info *zinfo = device->zone_info;
908 * For a zoned filesystem on a non-zoned block device, use the same
909 * super block locations as regular filesystem. Doing so, the super
910 * block can always be retrieved and the zoned flag of the volume
911 * detected from the super block information.
913 if (!bdev_is_zoned(device->bdev)) {
914 *bytenr_ret = btrfs_sb_offset(mirror);
918 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
919 if (zone_num + 1 >= zinfo->nr_zones)
922 return sb_log_location(device->bdev,
923 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
927 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
935 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
936 if (zone_num + 1 >= zinfo->nr_zones)
939 if (!test_bit(zone_num, zinfo->seq_zones))
945 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
947 struct btrfs_zoned_device_info *zinfo = device->zone_info;
948 struct blk_zone *zone;
951 if (!is_sb_log_zone(zinfo, mirror))
954 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
955 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
956 /* Advance the next zone */
957 if (zone->cond == BLK_ZONE_COND_FULL) {
962 if (zone->cond == BLK_ZONE_COND_EMPTY)
963 zone->cond = BLK_ZONE_COND_IMP_OPEN;
965 zone->wp += SUPER_INFO_SECTORS;
967 if (sb_zone_is_full(zone)) {
969 * No room left to write new superblock. Since
970 * superblock is written with REQ_SYNC, it is safe to
971 * finish the zone now.
973 * If the write pointer is exactly at the capacity,
974 * explicit ZONE_FINISH is not necessary.
976 if (zone->wp != zone->start + zone->capacity) {
979 ret = blkdev_zone_mgmt(device->bdev,
980 REQ_OP_ZONE_FINISH, zone->start,
981 zone->len, GFP_NOFS);
986 zone->wp = zone->start + zone->len;
987 zone->cond = BLK_ZONE_COND_FULL;
992 /* All the zones are FULL. Should not reach here. */
997 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
999 sector_t zone_sectors;
1000 sector_t nr_sectors;
1001 u8 zone_sectors_shift;
1005 zone_sectors = bdev_zone_sectors(bdev);
1006 zone_sectors_shift = ilog2(zone_sectors);
1007 nr_sectors = bdev_nr_sectors(bdev);
1008 nr_zones = nr_sectors >> zone_sectors_shift;
1010 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1011 if (sb_zone + 1 >= nr_zones)
1014 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1015 zone_start_sector(sb_zone, bdev),
1016 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1020 * Find allocatable zones within a given region.
1022 * @device: the device to allocate a region on
1023 * @hole_start: the position of the hole to allocate the region
1024 * @num_bytes: size of wanted region
1025 * @hole_end: the end of the hole
1026 * @return: position of allocatable zones
1028 * Allocatable region should not contain any superblock locations.
1030 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1031 u64 hole_end, u64 num_bytes)
1033 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1034 const u8 shift = zinfo->zone_size_shift;
1035 u64 nzones = num_bytes >> shift;
1036 u64 pos = hole_start;
1041 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1042 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1044 while (pos < hole_end) {
1045 begin = pos >> shift;
1046 end = begin + nzones;
1048 if (end > zinfo->nr_zones)
1051 /* Check if zones in the region are all empty */
1052 if (btrfs_dev_is_sequential(device, pos) &&
1053 !bitmap_test_range_all_set(zinfo->empty_zones, begin, nzones)) {
1054 pos += zinfo->zone_size;
1059 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1063 sb_zone = sb_zone_number(shift, i);
1064 if (!(end <= sb_zone ||
1065 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1067 pos = zone_start_physical(
1068 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1072 /* We also need to exclude regular superblock positions */
1073 sb_pos = btrfs_sb_offset(i);
1074 if (!(pos + num_bytes <= sb_pos ||
1075 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1077 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1089 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1091 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1092 unsigned int zno = (pos >> zone_info->zone_size_shift);
1094 /* We can use any number of zones */
1095 if (zone_info->max_active_zones == 0)
1098 if (!test_bit(zno, zone_info->active_zones)) {
1099 /* Active zone left? */
1100 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1102 if (test_and_set_bit(zno, zone_info->active_zones)) {
1103 /* Someone already set the bit */
1104 atomic_inc(&zone_info->active_zones_left);
1111 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1113 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1114 unsigned int zno = (pos >> zone_info->zone_size_shift);
1116 /* We can use any number of zones */
1117 if (zone_info->max_active_zones == 0)
1120 if (test_and_clear_bit(zno, zone_info->active_zones))
1121 atomic_inc(&zone_info->active_zones_left);
1124 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1125 u64 length, u64 *bytes)
1130 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1131 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1138 btrfs_dev_set_zone_empty(device, physical);
1139 btrfs_dev_clear_active_zone(device, physical);
1140 physical += device->zone_info->zone_size;
1141 length -= device->zone_info->zone_size;
1147 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1149 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1150 const u8 shift = zinfo->zone_size_shift;
1151 unsigned long begin = start >> shift;
1152 unsigned long nbits = size >> shift;
1156 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1157 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1159 if (begin + nbits > zinfo->nr_zones)
1162 /* All the zones are conventional */
1163 if (bitmap_test_range_all_zero(zinfo->seq_zones, begin, nbits))
1166 /* All the zones are sequential and empty */
1167 if (bitmap_test_range_all_set(zinfo->seq_zones, begin, nbits) &&
1168 bitmap_test_range_all_set(zinfo->empty_zones, begin, nbits))
1171 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1174 if (!btrfs_dev_is_sequential(device, pos) ||
1175 btrfs_dev_is_empty_zone(device, pos))
1178 /* Free regions should be empty */
1181 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1182 rcu_str_deref(device->name), device->devid, pos >> shift);
1185 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1195 * Calculate an allocation pointer from the extent allocation information
1196 * for a block group consist of conventional zones. It is pointed to the
1197 * end of the highest addressed extent in the block group as an allocation
1200 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1201 u64 *offset_ret, bool new)
1203 struct btrfs_fs_info *fs_info = cache->fs_info;
1204 struct btrfs_root *root;
1205 struct btrfs_path *path;
1206 struct btrfs_key key;
1207 struct btrfs_key found_key;
1212 * Avoid tree lookups for a new block group, there's no use for it.
1213 * It must always be 0.
1215 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1216 * For new a block group, this function is called from
1217 * btrfs_make_block_group() which is already taking the chunk mutex.
1218 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1219 * buffer locks to avoid deadlock.
1226 path = btrfs_alloc_path();
1230 key.objectid = cache->start + cache->length;
1234 root = btrfs_extent_root(fs_info, key.objectid);
1235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1236 /* We should not find the exact match */
1242 ret = btrfs_previous_extent_item(root, path, cache->start);
1251 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1253 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1254 length = found_key.offset;
1256 length = fs_info->nodesize;
1258 if (!(found_key.objectid >= cache->start &&
1259 found_key.objectid + length <= cache->start + cache->length)) {
1263 *offset_ret = found_key.objectid + length - cache->start;
1267 btrfs_free_path(path);
1277 static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
1278 struct zone_info *info, unsigned long *active,
1279 struct btrfs_chunk_map *map)
1281 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1282 struct btrfs_device *device = map->stripes[zone_idx].dev;
1283 int dev_replace_is_ongoing = 0;
1284 unsigned int nofs_flag;
1285 struct blk_zone zone;
1288 info->physical = map->stripes[zone_idx].physical;
1290 if (!device->bdev) {
1291 info->alloc_offset = WP_MISSING_DEV;
1295 /* Consider a zone as active if we can allow any number of active zones. */
1296 if (!device->zone_info->max_active_zones)
1297 __set_bit(zone_idx, active);
1299 if (!btrfs_dev_is_sequential(device, info->physical)) {
1300 info->alloc_offset = WP_CONVENTIONAL;
1304 /* This zone will be used for allocation, so mark this zone non-empty. */
1305 btrfs_dev_clear_zone_empty(device, info->physical);
1307 down_read(&dev_replace->rwsem);
1308 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1309 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1310 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, info->physical);
1311 up_read(&dev_replace->rwsem);
1314 * The group is mapped to a sequential zone. Get the zone write pointer
1315 * to determine the allocation offset within the zone.
1317 WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size));
1318 nofs_flag = memalloc_nofs_save();
1319 ret = btrfs_get_dev_zone(device, info->physical, &zone);
1320 memalloc_nofs_restore(nofs_flag);
1322 if (ret != -EIO && ret != -EOPNOTSUPP)
1324 info->alloc_offset = WP_MISSING_DEV;
1328 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1329 btrfs_err_in_rcu(fs_info,
1330 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1331 zone.start << SECTOR_SHIFT, rcu_str_deref(device->name),
1336 info->capacity = (zone.capacity << SECTOR_SHIFT);
1338 switch (zone.cond) {
1339 case BLK_ZONE_COND_OFFLINE:
1340 case BLK_ZONE_COND_READONLY:
1342 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1343 (info->physical >> device->zone_info->zone_size_shift),
1344 rcu_str_deref(device->name), device->devid);
1345 info->alloc_offset = WP_MISSING_DEV;
1347 case BLK_ZONE_COND_EMPTY:
1348 info->alloc_offset = 0;
1350 case BLK_ZONE_COND_FULL:
1351 info->alloc_offset = info->capacity;
1354 /* Partially used zone. */
1355 info->alloc_offset = ((zone.wp - zone.start) << SECTOR_SHIFT);
1356 __set_bit(zone_idx, active);
1363 static int btrfs_load_block_group_single(struct btrfs_block_group *bg,
1364 struct zone_info *info,
1365 unsigned long *active)
1367 if (info->alloc_offset == WP_MISSING_DEV) {
1368 btrfs_err(bg->fs_info,
1369 "zoned: cannot recover write pointer for zone %llu",
1374 bg->alloc_offset = info->alloc_offset;
1375 bg->zone_capacity = info->capacity;
1376 if (test_bit(0, active))
1377 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1381 static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
1382 struct btrfs_chunk_map *map,
1383 struct zone_info *zone_info,
1384 unsigned long *active)
1386 struct btrfs_fs_info *fs_info = bg->fs_info;
1388 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1389 btrfs_err(fs_info, "zoned: data DUP profile needs raid-stripe-tree");
1393 if (zone_info[0].alloc_offset == WP_MISSING_DEV) {
1394 btrfs_err(bg->fs_info,
1395 "zoned: cannot recover write pointer for zone %llu",
1396 zone_info[0].physical);
1399 if (zone_info[1].alloc_offset == WP_MISSING_DEV) {
1400 btrfs_err(bg->fs_info,
1401 "zoned: cannot recover write pointer for zone %llu",
1402 zone_info[1].physical);
1405 if (zone_info[0].alloc_offset != zone_info[1].alloc_offset) {
1406 btrfs_err(bg->fs_info,
1407 "zoned: write pointer offset mismatch of zones in DUP profile");
1411 if (test_bit(0, active) != test_bit(1, active)) {
1412 if (!btrfs_zone_activate(bg))
1414 } else if (test_bit(0, active)) {
1415 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1418 bg->alloc_offset = zone_info[0].alloc_offset;
1419 bg->zone_capacity = min(zone_info[0].capacity, zone_info[1].capacity);
1423 static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
1424 struct btrfs_chunk_map *map,
1425 struct zone_info *zone_info,
1426 unsigned long *active)
1428 struct btrfs_fs_info *fs_info = bg->fs_info;
1431 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1432 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1433 btrfs_bg_type_to_raid_name(map->type));
1437 for (i = 0; i < map->num_stripes; i++) {
1438 if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1439 zone_info[i].alloc_offset == WP_CONVENTIONAL)
1442 if ((zone_info[0].alloc_offset != zone_info[i].alloc_offset) &&
1443 !btrfs_test_opt(fs_info, DEGRADED)) {
1445 "zoned: write pointer offset mismatch of zones in %s profile",
1446 btrfs_bg_type_to_raid_name(map->type));
1449 if (test_bit(0, active) != test_bit(i, active)) {
1450 if (!btrfs_test_opt(fs_info, DEGRADED) &&
1451 !btrfs_zone_activate(bg)) {
1455 if (test_bit(0, active))
1456 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1458 /* In case a device is missing we have a cap of 0, so don't use it. */
1459 bg->zone_capacity = min_not_zero(zone_info[0].capacity,
1460 zone_info[1].capacity);
1463 if (zone_info[0].alloc_offset != WP_MISSING_DEV)
1464 bg->alloc_offset = zone_info[0].alloc_offset;
1466 bg->alloc_offset = zone_info[i - 1].alloc_offset;
1471 static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
1472 struct btrfs_chunk_map *map,
1473 struct zone_info *zone_info,
1474 unsigned long *active)
1476 struct btrfs_fs_info *fs_info = bg->fs_info;
1478 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1479 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1480 btrfs_bg_type_to_raid_name(map->type));
1484 for (int i = 0; i < map->num_stripes; i++) {
1485 if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1486 zone_info[i].alloc_offset == WP_CONVENTIONAL)
1489 if (test_bit(0, active) != test_bit(i, active)) {
1490 if (!btrfs_zone_activate(bg))
1493 if (test_bit(0, active))
1494 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1496 bg->zone_capacity += zone_info[i].capacity;
1497 bg->alloc_offset += zone_info[i].alloc_offset;
1503 static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
1504 struct btrfs_chunk_map *map,
1505 struct zone_info *zone_info,
1506 unsigned long *active)
1508 struct btrfs_fs_info *fs_info = bg->fs_info;
1510 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1511 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1512 btrfs_bg_type_to_raid_name(map->type));
1516 for (int i = 0; i < map->num_stripes; i++) {
1517 if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1518 zone_info[i].alloc_offset == WP_CONVENTIONAL)
1521 if (test_bit(0, active) != test_bit(i, active)) {
1522 if (!btrfs_zone_activate(bg))
1525 if (test_bit(0, active))
1526 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1529 if ((i % map->sub_stripes) == 0) {
1530 bg->zone_capacity += zone_info[i].capacity;
1531 bg->alloc_offset += zone_info[i].alloc_offset;
1538 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1540 struct btrfs_fs_info *fs_info = cache->fs_info;
1541 struct btrfs_chunk_map *map;
1542 u64 logical = cache->start;
1543 u64 length = cache->length;
1544 struct zone_info *zone_info = NULL;
1547 unsigned long *active = NULL;
1549 u32 num_sequential = 0, num_conventional = 0;
1551 if (!btrfs_is_zoned(fs_info))
1555 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1557 "zoned: block group %llu len %llu unaligned to zone size %llu",
1558 logical, length, fs_info->zone_size);
1562 map = btrfs_find_chunk_map(fs_info, logical, length);
1566 cache->physical_map = btrfs_clone_chunk_map(map, GFP_NOFS);
1567 if (!cache->physical_map) {
1572 zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS);
1578 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1584 for (i = 0; i < map->num_stripes; i++) {
1585 ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map);
1589 if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1595 if (num_sequential > 0)
1596 set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1598 if (num_conventional > 0) {
1599 /* Zone capacity is always zone size in emulation */
1600 cache->zone_capacity = cache->length;
1601 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1604 "zoned: failed to determine allocation offset of bg %llu",
1607 } else if (map->num_stripes == num_conventional) {
1608 cache->alloc_offset = last_alloc;
1609 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1614 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1615 case 0: /* single */
1616 ret = btrfs_load_block_group_single(cache, &zone_info[0], active);
1618 case BTRFS_BLOCK_GROUP_DUP:
1619 ret = btrfs_load_block_group_dup(cache, map, zone_info, active);
1621 case BTRFS_BLOCK_GROUP_RAID1:
1622 case BTRFS_BLOCK_GROUP_RAID1C3:
1623 case BTRFS_BLOCK_GROUP_RAID1C4:
1624 ret = btrfs_load_block_group_raid1(cache, map, zone_info, active);
1626 case BTRFS_BLOCK_GROUP_RAID0:
1627 ret = btrfs_load_block_group_raid0(cache, map, zone_info, active);
1629 case BTRFS_BLOCK_GROUP_RAID10:
1630 ret = btrfs_load_block_group_raid10(cache, map, zone_info, active);
1632 case BTRFS_BLOCK_GROUP_RAID5:
1633 case BTRFS_BLOCK_GROUP_RAID6:
1635 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1636 btrfs_bg_type_to_raid_name(map->type));
1642 if (cache->alloc_offset > cache->zone_capacity) {
1644 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1645 cache->alloc_offset, cache->zone_capacity,
1650 /* An extent is allocated after the write pointer */
1651 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1653 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1654 logical, last_alloc, cache->alloc_offset);
1659 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1660 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1661 btrfs_get_block_group(cache);
1662 spin_lock(&fs_info->zone_active_bgs_lock);
1663 list_add_tail(&cache->active_bg_list,
1664 &fs_info->zone_active_bgs);
1665 spin_unlock(&fs_info->zone_active_bgs_lock);
1668 btrfs_free_chunk_map(cache->physical_map);
1669 cache->physical_map = NULL;
1671 bitmap_free(active);
1673 btrfs_free_chunk_map(map);
1678 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1682 if (!btrfs_is_zoned(cache->fs_info))
1685 WARN_ON(cache->bytes_super != 0);
1686 unusable = (cache->alloc_offset - cache->used) +
1687 (cache->length - cache->zone_capacity);
1688 free = cache->zone_capacity - cache->alloc_offset;
1690 /* We only need ->free_space in ALLOC_SEQ block groups */
1691 cache->cached = BTRFS_CACHE_FINISHED;
1692 cache->free_space_ctl->free_space = free;
1693 cache->zone_unusable = unusable;
1696 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
1698 u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
1699 struct btrfs_inode *inode = bbio->inode;
1700 struct btrfs_fs_info *fs_info = bbio->fs_info;
1701 struct btrfs_block_group *cache;
1704 if (!btrfs_is_zoned(fs_info))
1707 if (!inode || !is_data_inode(&inode->vfs_inode))
1710 if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE)
1714 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1715 * extent layout the relocation code has.
1716 * Furthermore we have set aside own block-group from which only the
1717 * relocation "process" can allocate and make sure only one process at a
1718 * time can add pages to an extent that gets relocated, so it's safe to
1719 * use regular REQ_OP_WRITE for this special case.
1721 if (btrfs_is_data_reloc_root(inode->root))
1724 cache = btrfs_lookup_block_group(fs_info, start);
1729 ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1730 btrfs_put_block_group(cache);
1735 void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
1737 const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
1738 struct btrfs_ordered_sum *sum = bbio->sums;
1740 if (physical < bbio->orig_physical)
1741 sum->logical -= bbio->orig_physical - physical;
1743 sum->logical += physical - bbio->orig_physical;
1746 static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered,
1749 struct extent_map_tree *em_tree = &BTRFS_I(ordered->inode)->extent_tree;
1750 struct extent_map *em;
1752 ordered->disk_bytenr = logical;
1754 write_lock(&em_tree->lock);
1755 em = search_extent_mapping(em_tree, ordered->file_offset,
1756 ordered->num_bytes);
1757 em->block_start = logical;
1758 free_extent_map(em);
1759 write_unlock(&em_tree->lock);
1762 static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
1763 u64 logical, u64 len)
1765 struct btrfs_ordered_extent *new;
1767 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
1768 split_extent_map(BTRFS_I(ordered->inode), ordered->file_offset,
1769 ordered->num_bytes, len, logical))
1772 new = btrfs_split_ordered_extent(ordered, len);
1775 new->disk_bytenr = logical;
1776 btrfs_finish_one_ordered(new);
1780 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
1782 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1783 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1784 struct btrfs_ordered_sum *sum;
1788 * Write to pre-allocated region is for the data relocation, and so
1789 * it should use WRITE operation. No split/rewrite are necessary.
1791 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
1794 ASSERT(!list_empty(&ordered->list));
1795 /* The ordered->list can be empty in the above pre-alloc case. */
1796 sum = list_first_entry(&ordered->list, struct btrfs_ordered_sum, list);
1797 logical = sum->logical;
1800 while (len < ordered->disk_num_bytes) {
1801 sum = list_next_entry(sum, list);
1802 if (sum->logical == logical + len) {
1806 if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
1807 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
1808 btrfs_err(fs_info, "failed to split ordered extent");
1811 logical = sum->logical;
1815 if (ordered->disk_bytenr != logical)
1816 btrfs_rewrite_logical_zoned(ordered, logical);
1820 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
1821 * were allocated by btrfs_alloc_dummy_sum only to record the logical
1822 * addresses and don't contain actual checksums. We thus must free them
1823 * here so that we don't attempt to log the csums later.
1825 if ((inode->flags & BTRFS_INODE_NODATASUM) ||
1826 test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) {
1827 while ((sum = list_first_entry_or_null(&ordered->list,
1828 typeof(*sum), list))) {
1829 list_del(&sum->list);
1835 static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
1836 struct btrfs_block_group **active_bg)
1838 const struct writeback_control *wbc = ctx->wbc;
1839 struct btrfs_block_group *block_group = ctx->zoned_bg;
1840 struct btrfs_fs_info *fs_info = block_group->fs_info;
1842 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
1845 if (fs_info->treelog_bg == block_group->start) {
1846 if (!btrfs_zone_activate(block_group)) {
1847 int ret_fin = btrfs_zone_finish_one_bg(fs_info);
1849 if (ret_fin != 1 || !btrfs_zone_activate(block_group))
1852 } else if (*active_bg != block_group) {
1853 struct btrfs_block_group *tgt = *active_bg;
1855 /* zoned_meta_io_lock protects fs_info->active_{meta,system}_bg. */
1856 lockdep_assert_held(&fs_info->zoned_meta_io_lock);
1860 * If there is an unsent IO left in the allocated area,
1861 * we cannot wait for them as it may cause a deadlock.
1863 if (tgt->meta_write_pointer < tgt->start + tgt->alloc_offset) {
1864 if (wbc->sync_mode == WB_SYNC_NONE ||
1865 (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync))
1869 /* Pivot active metadata/system block group. */
1870 btrfs_zoned_meta_io_unlock(fs_info);
1871 wait_eb_writebacks(tgt);
1872 do_zone_finish(tgt, true);
1873 btrfs_zoned_meta_io_lock(fs_info);
1874 if (*active_bg == tgt) {
1875 btrfs_put_block_group(tgt);
1879 if (!btrfs_zone_activate(block_group))
1881 if (*active_bg != block_group) {
1882 ASSERT(*active_bg == NULL);
1883 *active_bg = block_group;
1884 btrfs_get_block_group(block_group);
1892 * Check if @ctx->eb is aligned to the write pointer.
1895 * 0: @ctx->eb is at the write pointer. You can write it.
1896 * -EAGAIN: There is a hole. The caller should handle the case.
1897 * -EBUSY: There is a hole, but the caller can just bail out.
1899 int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1900 struct btrfs_eb_write_context *ctx)
1902 const struct writeback_control *wbc = ctx->wbc;
1903 const struct extent_buffer *eb = ctx->eb;
1904 struct btrfs_block_group *block_group = ctx->zoned_bg;
1906 if (!btrfs_is_zoned(fs_info))
1910 if (block_group->start > eb->start ||
1911 block_group->start + block_group->length <= eb->start) {
1912 btrfs_put_block_group(block_group);
1914 ctx->zoned_bg = NULL;
1919 block_group = btrfs_lookup_block_group(fs_info, eb->start);
1922 ctx->zoned_bg = block_group;
1925 if (block_group->meta_write_pointer == eb->start) {
1926 struct btrfs_block_group **tgt;
1928 if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
1931 if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)
1932 tgt = &fs_info->active_system_bg;
1934 tgt = &fs_info->active_meta_bg;
1935 if (check_bg_is_active(ctx, tgt))
1940 * Since we may release fs_info->zoned_meta_io_lock, someone can already
1941 * start writing this eb. In that case, we can just bail out.
1943 if (block_group->meta_write_pointer > eb->start)
1946 /* If for_sync, this hole will be filled with trasnsaction commit. */
1947 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
1952 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1954 if (!btrfs_dev_is_sequential(device, physical))
1957 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1958 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1961 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1962 struct blk_zone *zone)
1964 struct btrfs_io_context *bioc = NULL;
1965 u64 mapped_length = PAGE_SIZE;
1966 unsigned int nofs_flag;
1970 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1971 &mapped_length, &bioc, NULL, NULL);
1972 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1977 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1982 nofs_flag = memalloc_nofs_save();
1983 nmirrors = (int)bioc->num_stripes;
1984 for (i = 0; i < nmirrors; i++) {
1985 u64 physical = bioc->stripes[i].physical;
1986 struct btrfs_device *dev = bioc->stripes[i].dev;
1988 /* Missing device */
1992 ret = btrfs_get_dev_zone(dev, physical, zone);
1993 /* Failing device */
1994 if (ret == -EIO || ret == -EOPNOTSUPP)
1998 memalloc_nofs_restore(nofs_flag);
2000 btrfs_put_bioc(bioc);
2005 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
2006 * filling zeros between @physical_pos to a write pointer of dev-replace
2009 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
2010 u64 physical_start, u64 physical_pos)
2012 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
2013 struct blk_zone zone;
2018 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
2021 ret = read_zone_info(fs_info, logical, &zone);
2025 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
2027 if (physical_pos == wp)
2030 if (physical_pos > wp)
2033 length = wp - physical_pos;
2034 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
2038 * Activate block group and underlying device zones
2040 * @block_group: the block group to activate
2042 * Return: true on success, false otherwise
2044 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
2046 struct btrfs_fs_info *fs_info = block_group->fs_info;
2047 struct btrfs_chunk_map *map;
2048 struct btrfs_device *device;
2050 const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
2054 if (!btrfs_is_zoned(block_group->fs_info))
2057 map = block_group->physical_map;
2059 spin_lock(&fs_info->zone_active_bgs_lock);
2060 spin_lock(&block_group->lock);
2061 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2067 if (btrfs_zoned_bg_is_full(block_group)) {
2072 for (i = 0; i < map->num_stripes; i++) {
2073 struct btrfs_zoned_device_info *zinfo;
2076 device = map->stripes[i].dev;
2077 physical = map->stripes[i].physical;
2078 zinfo = device->zone_info;
2080 if (zinfo->max_active_zones == 0)
2084 reserved = zinfo->reserved_active_zones;
2086 * For the data block group, leave active zones for one
2087 * metadata block group and one system block group.
2089 if (atomic_read(&zinfo->active_zones_left) <= reserved) {
2094 if (!btrfs_dev_set_active_zone(device, physical)) {
2095 /* Cannot activate the zone */
2100 zinfo->reserved_active_zones--;
2103 /* Successfully activated all the zones */
2104 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2105 spin_unlock(&block_group->lock);
2107 /* For the active block group list */
2108 btrfs_get_block_group(block_group);
2109 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
2110 spin_unlock(&fs_info->zone_active_bgs_lock);
2115 spin_unlock(&block_group->lock);
2116 spin_unlock(&fs_info->zone_active_bgs_lock);
2120 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
2122 struct btrfs_fs_info *fs_info = block_group->fs_info;
2123 const u64 end = block_group->start + block_group->length;
2124 struct radix_tree_iter iter;
2125 struct extent_buffer *eb;
2129 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
2130 block_group->start >> fs_info->sectorsize_bits) {
2131 eb = radix_tree_deref_slot(slot);
2134 if (radix_tree_deref_retry(eb)) {
2135 slot = radix_tree_iter_retry(&iter);
2139 if (eb->start < block_group->start)
2141 if (eb->start >= end)
2144 slot = radix_tree_iter_resume(slot, &iter);
2146 wait_on_extent_buffer_writeback(eb);
2152 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
2154 struct btrfs_fs_info *fs_info = block_group->fs_info;
2155 struct btrfs_chunk_map *map;
2156 const bool is_metadata = (block_group->flags &
2157 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
2161 spin_lock(&block_group->lock);
2162 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2163 spin_unlock(&block_group->lock);
2167 /* Check if we have unwritten allocated space */
2169 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
2170 spin_unlock(&block_group->lock);
2175 * If we are sure that the block group is full (= no more room left for
2176 * new allocation) and the IO for the last usable block is completed, we
2177 * don't need to wait for the other IOs. This holds because we ensure
2178 * the sequential IO submissions using the ZONE_APPEND command for data
2179 * and block_group->meta_write_pointer for metadata.
2181 if (!fully_written) {
2182 if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2183 spin_unlock(&block_group->lock);
2186 spin_unlock(&block_group->lock);
2188 ret = btrfs_inc_block_group_ro(block_group, false);
2192 /* Ensure all writes in this block group finish */
2193 btrfs_wait_block_group_reservations(block_group);
2194 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
2195 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
2196 block_group->length);
2197 /* Wait for extent buffers to be written. */
2199 wait_eb_writebacks(block_group);
2201 spin_lock(&block_group->lock);
2204 * Bail out if someone already deactivated the block group, or
2205 * allocated space is left in the block group.
2207 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2208 &block_group->runtime_flags)) {
2209 spin_unlock(&block_group->lock);
2210 btrfs_dec_block_group_ro(block_group);
2214 if (block_group->reserved ||
2215 test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2216 &block_group->runtime_flags)) {
2217 spin_unlock(&block_group->lock);
2218 btrfs_dec_block_group_ro(block_group);
2223 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2224 block_group->alloc_offset = block_group->zone_capacity;
2225 if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
2226 block_group->meta_write_pointer = block_group->start +
2227 block_group->zone_capacity;
2228 block_group->free_space_ctl->free_space = 0;
2229 btrfs_clear_treelog_bg(block_group);
2230 btrfs_clear_data_reloc_bg(block_group);
2231 spin_unlock(&block_group->lock);
2233 map = block_group->physical_map;
2234 for (i = 0; i < map->num_stripes; i++) {
2235 struct btrfs_device *device = map->stripes[i].dev;
2236 const u64 physical = map->stripes[i].physical;
2237 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2239 if (zinfo->max_active_zones == 0)
2242 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2243 physical >> SECTOR_SHIFT,
2244 zinfo->zone_size >> SECTOR_SHIFT,
2250 if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA))
2251 zinfo->reserved_active_zones++;
2252 btrfs_dev_clear_active_zone(device, physical);
2256 btrfs_dec_block_group_ro(block_group);
2258 spin_lock(&fs_info->zone_active_bgs_lock);
2259 ASSERT(!list_empty(&block_group->active_bg_list));
2260 list_del_init(&block_group->active_bg_list);
2261 spin_unlock(&fs_info->zone_active_bgs_lock);
2263 /* For active_bg_list */
2264 btrfs_put_block_group(block_group);
2266 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2271 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2273 if (!btrfs_is_zoned(block_group->fs_info))
2276 return do_zone_finish(block_group, false);
2279 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2281 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2282 struct btrfs_device *device;
2285 if (!btrfs_is_zoned(fs_info))
2288 /* Check if there is a device with active zones left */
2289 mutex_lock(&fs_info->chunk_mutex);
2290 spin_lock(&fs_info->zone_active_bgs_lock);
2291 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2292 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2298 if (!zinfo->max_active_zones) {
2303 if (flags & BTRFS_BLOCK_GROUP_DATA)
2304 reserved = zinfo->reserved_active_zones;
2306 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
2307 case 0: /* single */
2308 ret = (atomic_read(&zinfo->active_zones_left) >= (1 + reserved));
2310 case BTRFS_BLOCK_GROUP_DUP:
2311 ret = (atomic_read(&zinfo->active_zones_left) >= (2 + reserved));
2317 spin_unlock(&fs_info->zone_active_bgs_lock);
2318 mutex_unlock(&fs_info->chunk_mutex);
2321 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2326 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2328 struct btrfs_block_group *block_group;
2329 u64 min_alloc_bytes;
2331 if (!btrfs_is_zoned(fs_info))
2334 block_group = btrfs_lookup_block_group(fs_info, logical);
2335 ASSERT(block_group);
2337 /* No MIXED_BG on zoned btrfs. */
2338 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2339 min_alloc_bytes = fs_info->sectorsize;
2341 min_alloc_bytes = fs_info->nodesize;
2343 /* Bail out if we can allocate more data from this block group. */
2344 if (logical + length + min_alloc_bytes <=
2345 block_group->start + block_group->zone_capacity)
2348 do_zone_finish(block_group, true);
2351 btrfs_put_block_group(block_group);
2354 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2356 struct btrfs_block_group *bg =
2357 container_of(work, struct btrfs_block_group, zone_finish_work);
2359 wait_on_extent_buffer_writeback(bg->last_eb);
2360 free_extent_buffer(bg->last_eb);
2361 btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2362 btrfs_put_block_group(bg);
2365 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2366 struct extent_buffer *eb)
2368 if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
2369 eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2372 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2373 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2379 btrfs_get_block_group(bg);
2380 atomic_inc(&eb->refs);
2382 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2383 queue_work(system_unbound_wq, &bg->zone_finish_work);
2386 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2388 struct btrfs_fs_info *fs_info = bg->fs_info;
2390 spin_lock(&fs_info->relocation_bg_lock);
2391 if (fs_info->data_reloc_bg == bg->start)
2392 fs_info->data_reloc_bg = 0;
2393 spin_unlock(&fs_info->relocation_bg_lock);
2396 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2398 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2399 struct btrfs_device *device;
2401 if (!btrfs_is_zoned(fs_info))
2404 mutex_lock(&fs_devices->device_list_mutex);
2405 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2406 if (device->zone_info) {
2407 vfree(device->zone_info->zone_cache);
2408 device->zone_info->zone_cache = NULL;
2411 mutex_unlock(&fs_devices->device_list_mutex);
2414 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2416 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2417 struct btrfs_device *device;
2422 ASSERT(btrfs_is_zoned(fs_info));
2424 if (fs_info->bg_reclaim_threshold == 0)
2427 mutex_lock(&fs_devices->device_list_mutex);
2428 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2432 total += device->disk_total_bytes;
2433 used += device->bytes_used;
2435 mutex_unlock(&fs_devices->device_list_mutex);
2437 factor = div64_u64(used * 100, total);
2438 return factor >= fs_info->bg_reclaim_threshold;
2441 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2444 struct btrfs_block_group *block_group;
2446 if (!btrfs_is_zoned(fs_info))
2449 block_group = btrfs_lookup_block_group(fs_info, logical);
2450 /* It should be called on a previous data relocation block group. */
2451 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2453 spin_lock(&block_group->lock);
2454 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2457 /* All relocation extents are written. */
2458 if (block_group->start + block_group->alloc_offset == logical + length) {
2460 * Now, release this block group for further allocations and
2463 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2464 &block_group->runtime_flags);
2468 spin_unlock(&block_group->lock);
2469 btrfs_put_block_group(block_group);
2472 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2474 struct btrfs_block_group *block_group;
2475 struct btrfs_block_group *min_bg = NULL;
2476 u64 min_avail = U64_MAX;
2479 spin_lock(&fs_info->zone_active_bgs_lock);
2480 list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2484 spin_lock(&block_group->lock);
2485 if (block_group->reserved || block_group->alloc_offset == 0 ||
2486 (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM) ||
2487 test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2488 spin_unlock(&block_group->lock);
2492 avail = block_group->zone_capacity - block_group->alloc_offset;
2493 if (min_avail > avail) {
2495 btrfs_put_block_group(min_bg);
2496 min_bg = block_group;
2498 btrfs_get_block_group(min_bg);
2500 spin_unlock(&block_group->lock);
2502 spin_unlock(&fs_info->zone_active_bgs_lock);
2507 ret = btrfs_zone_finish(min_bg);
2508 btrfs_put_block_group(min_bg);
2510 return ret < 0 ? ret : 1;
2513 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2514 struct btrfs_space_info *space_info,
2517 struct btrfs_block_group *bg;
2520 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2525 bool need_finish = false;
2527 down_read(&space_info->groups_sem);
2528 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2529 list_for_each_entry(bg, &space_info->block_groups[index],
2531 if (!spin_trylock(&bg->lock))
2533 if (btrfs_zoned_bg_is_full(bg) ||
2534 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2535 &bg->runtime_flags)) {
2536 spin_unlock(&bg->lock);
2539 spin_unlock(&bg->lock);
2541 if (btrfs_zone_activate(bg)) {
2542 up_read(&space_info->groups_sem);
2549 up_read(&space_info->groups_sem);
2551 if (!do_finish || !need_finish)
2554 ret = btrfs_zone_finish_one_bg(fs_info);
2565 * Reserve zones for one metadata block group, one tree-log block group, and one
2566 * system block group.
2568 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
2570 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2571 struct btrfs_block_group *block_group;
2572 struct btrfs_device *device;
2573 /* Reserve zones for normal SINGLE metadata and tree-log block group. */
2574 unsigned int metadata_reserve = 2;
2575 /* Reserve a zone for SINGLE system block group. */
2576 unsigned int system_reserve = 1;
2578 if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
2582 * This function is called from the mount context. So, there is no
2583 * parallel process touching the bits. No need for read_seqretry().
2585 if (fs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
2586 metadata_reserve = 4;
2587 if (fs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
2590 /* Apply the reservation on all the devices. */
2591 mutex_lock(&fs_devices->device_list_mutex);
2592 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2596 device->zone_info->reserved_active_zones =
2597 metadata_reserve + system_reserve;
2599 mutex_unlock(&fs_devices->device_list_mutex);
2601 /* Release reservation for currently active block groups. */
2602 spin_lock(&fs_info->zone_active_bgs_lock);
2603 list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
2604 struct btrfs_chunk_map *map = block_group->physical_map;
2606 if (!(block_group->flags &
2607 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
2610 for (int i = 0; i < map->num_stripes; i++)
2611 map->stripes[i].dev->zone_info->reserved_active_zones--;
2613 spin_unlock(&fs_info->zone_active_bgs_lock);