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 switch (bdev_zoned_model(bdev)) {
583 model = "host-managed zoned";
587 model = "host-aware zoned";
592 emulated = "emulated ";
596 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
597 bdev_zoned_model(bdev),
598 rcu_str_deref(device->name));
600 goto out_free_zone_info;
603 btrfs_info_in_rcu(fs_info,
604 "%s block device %s, %u %szones of %llu bytes",
605 model, rcu_str_deref(device->name), zone_info->nr_zones,
606 emulated, zone_info->zone_size);
613 btrfs_destroy_dev_zone_info(device);
618 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
620 struct btrfs_zoned_device_info *zone_info = device->zone_info;
625 bitmap_free(zone_info->active_zones);
626 bitmap_free(zone_info->seq_zones);
627 bitmap_free(zone_info->empty_zones);
628 vfree(zone_info->zone_cache);
630 device->zone_info = NULL;
633 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
635 struct btrfs_zoned_device_info *zone_info;
637 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
641 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
642 if (!zone_info->seq_zones)
645 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
646 zone_info->nr_zones);
648 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
649 if (!zone_info->empty_zones)
652 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
653 zone_info->nr_zones);
655 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
656 if (!zone_info->active_zones)
659 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
660 zone_info->nr_zones);
661 zone_info->zone_cache = NULL;
666 bitmap_free(zone_info->seq_zones);
667 bitmap_free(zone_info->empty_zones);
668 bitmap_free(zone_info->active_zones);
673 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
674 struct blk_zone *zone)
676 unsigned int nr_zones = 1;
679 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
680 if (ret != 0 || !nr_zones)
681 return ret ? ret : -EIO;
686 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
688 struct btrfs_device *device;
690 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
692 bdev_zoned_model(device->bdev) == BLK_ZONED_HM) {
694 "zoned: mode not enabled but zoned device found: %pg",
703 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
705 struct queue_limits *lim = &fs_info->limits;
706 struct btrfs_device *device;
711 * Host-Managed devices can't be used without the ZONED flag. With the
712 * ZONED all devices can be used, using zone emulation if required.
714 if (!btrfs_fs_incompat(fs_info, ZONED))
715 return btrfs_check_for_zoned_device(fs_info);
717 blk_set_stacking_limits(lim);
719 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
720 struct btrfs_zoned_device_info *zone_info = device->zone_info;
726 zone_size = zone_info->zone_size;
727 } else if (zone_info->zone_size != zone_size) {
729 "zoned: unequal block device zone sizes: have %llu found %llu",
730 zone_info->zone_size, zone_size);
735 * With the zoned emulation, we can have non-zoned device on the
736 * zoned mode. In this case, we don't have a valid max zone
739 if (bdev_is_zoned(device->bdev)) {
740 blk_stack_limits(lim,
741 &bdev_get_queue(device->bdev)->limits,
747 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
748 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
749 * check the alignment here.
751 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
753 "zoned: zone size %llu not aligned to stripe %u",
754 zone_size, BTRFS_STRIPE_LEN);
758 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
759 btrfs_err(fs_info, "zoned: mixed block groups not supported");
763 fs_info->zone_size = zone_size;
765 * Also limit max_zone_append_size by max_segments * PAGE_SIZE.
766 * Technically, we can have multiple pages per segment. But, since
767 * we add the pages one by one to a bio, and cannot increase the
768 * metadata reservation even if it increases the number of extents, it
769 * is safe to stick with the limit.
771 fs_info->max_zone_append_size = ALIGN_DOWN(
772 min3((u64)lim->max_zone_append_sectors << SECTOR_SHIFT,
773 (u64)lim->max_sectors << SECTOR_SHIFT,
774 (u64)lim->max_segments << PAGE_SHIFT),
775 fs_info->sectorsize);
776 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
777 if (fs_info->max_zone_append_size < fs_info->max_extent_size)
778 fs_info->max_extent_size = fs_info->max_zone_append_size;
781 * Check mount options here, because we might change fs_info->zoned
782 * from fs_info->zone_size.
784 ret = btrfs_check_mountopts_zoned(fs_info);
788 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
792 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
794 if (!btrfs_is_zoned(info))
798 * Space cache writing is not COWed. Disable that to avoid write errors
799 * in sequential zones.
801 if (btrfs_test_opt(info, SPACE_CACHE)) {
802 btrfs_err(info, "zoned: space cache v1 is not supported");
806 if (btrfs_test_opt(info, NODATACOW)) {
807 btrfs_err(info, "zoned: NODATACOW not supported");
811 btrfs_clear_and_info(info, DISCARD_ASYNC,
812 "zoned: async discard ignored and disabled for zoned mode");
817 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
818 int rw, u64 *bytenr_ret)
823 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
824 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
828 ret = sb_write_pointer(bdev, zones, &wp);
829 if (ret != -ENOENT && ret < 0)
833 struct blk_zone *reset = NULL;
835 if (wp == zones[0].start << SECTOR_SHIFT)
837 else if (wp == zones[1].start << SECTOR_SHIFT)
840 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
841 ASSERT(sb_zone_is_full(reset));
843 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
844 reset->start, reset->len,
849 reset->cond = BLK_ZONE_COND_EMPTY;
850 reset->wp = reset->start;
852 } else if (ret != -ENOENT) {
854 * For READ, we want the previous one. Move write pointer to
855 * the end of a zone, if it is at the head of a zone.
859 if (wp == zones[0].start << SECTOR_SHIFT)
860 zone_end = zones[1].start + zones[1].capacity;
861 else if (wp == zones[1].start << SECTOR_SHIFT)
862 zone_end = zones[0].start + zones[0].capacity;
864 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
865 BTRFS_SUPER_INFO_SIZE);
867 wp -= BTRFS_SUPER_INFO_SIZE;
875 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
878 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
879 sector_t zone_sectors;
882 u8 zone_sectors_shift;
886 if (!bdev_is_zoned(bdev)) {
887 *bytenr_ret = btrfs_sb_offset(mirror);
891 ASSERT(rw == READ || rw == WRITE);
893 zone_sectors = bdev_zone_sectors(bdev);
894 if (!is_power_of_2(zone_sectors))
896 zone_sectors_shift = ilog2(zone_sectors);
897 nr_sectors = bdev_nr_sectors(bdev);
898 nr_zones = nr_sectors >> zone_sectors_shift;
900 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
901 if (sb_zone + 1 >= nr_zones)
904 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
905 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
909 if (ret != BTRFS_NR_SB_LOG_ZONES)
912 return sb_log_location(bdev, zones, rw, bytenr_ret);
915 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
918 struct btrfs_zoned_device_info *zinfo = device->zone_info;
922 * For a zoned filesystem on a non-zoned block device, use the same
923 * super block locations as regular filesystem. Doing so, the super
924 * block can always be retrieved and the zoned flag of the volume
925 * detected from the super block information.
927 if (!bdev_is_zoned(device->bdev)) {
928 *bytenr_ret = btrfs_sb_offset(mirror);
932 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
933 if (zone_num + 1 >= zinfo->nr_zones)
936 return sb_log_location(device->bdev,
937 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
941 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
949 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
950 if (zone_num + 1 >= zinfo->nr_zones)
953 if (!test_bit(zone_num, zinfo->seq_zones))
959 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
961 struct btrfs_zoned_device_info *zinfo = device->zone_info;
962 struct blk_zone *zone;
965 if (!is_sb_log_zone(zinfo, mirror))
968 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
969 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
970 /* Advance the next zone */
971 if (zone->cond == BLK_ZONE_COND_FULL) {
976 if (zone->cond == BLK_ZONE_COND_EMPTY)
977 zone->cond = BLK_ZONE_COND_IMP_OPEN;
979 zone->wp += SUPER_INFO_SECTORS;
981 if (sb_zone_is_full(zone)) {
983 * No room left to write new superblock. Since
984 * superblock is written with REQ_SYNC, it is safe to
985 * finish the zone now.
987 * If the write pointer is exactly at the capacity,
988 * explicit ZONE_FINISH is not necessary.
990 if (zone->wp != zone->start + zone->capacity) {
993 ret = blkdev_zone_mgmt(device->bdev,
994 REQ_OP_ZONE_FINISH, zone->start,
995 zone->len, GFP_NOFS);
1000 zone->wp = zone->start + zone->len;
1001 zone->cond = BLK_ZONE_COND_FULL;
1006 /* All the zones are FULL. Should not reach here. */
1011 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
1013 sector_t zone_sectors;
1014 sector_t nr_sectors;
1015 u8 zone_sectors_shift;
1019 zone_sectors = bdev_zone_sectors(bdev);
1020 zone_sectors_shift = ilog2(zone_sectors);
1021 nr_sectors = bdev_nr_sectors(bdev);
1022 nr_zones = nr_sectors >> zone_sectors_shift;
1024 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1025 if (sb_zone + 1 >= nr_zones)
1028 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1029 zone_start_sector(sb_zone, bdev),
1030 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1034 * Find allocatable zones within a given region.
1036 * @device: the device to allocate a region on
1037 * @hole_start: the position of the hole to allocate the region
1038 * @num_bytes: size of wanted region
1039 * @hole_end: the end of the hole
1040 * @return: position of allocatable zones
1042 * Allocatable region should not contain any superblock locations.
1044 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1045 u64 hole_end, u64 num_bytes)
1047 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1048 const u8 shift = zinfo->zone_size_shift;
1049 u64 nzones = num_bytes >> shift;
1050 u64 pos = hole_start;
1055 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1056 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1058 while (pos < hole_end) {
1059 begin = pos >> shift;
1060 end = begin + nzones;
1062 if (end > zinfo->nr_zones)
1065 /* Check if zones in the region are all empty */
1066 if (btrfs_dev_is_sequential(device, pos) &&
1067 !bitmap_test_range_all_set(zinfo->empty_zones, begin, nzones)) {
1068 pos += zinfo->zone_size;
1073 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1077 sb_zone = sb_zone_number(shift, i);
1078 if (!(end <= sb_zone ||
1079 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1081 pos = zone_start_physical(
1082 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1086 /* We also need to exclude regular superblock positions */
1087 sb_pos = btrfs_sb_offset(i);
1088 if (!(pos + num_bytes <= sb_pos ||
1089 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1091 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1103 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1105 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1106 unsigned int zno = (pos >> zone_info->zone_size_shift);
1108 /* We can use any number of zones */
1109 if (zone_info->max_active_zones == 0)
1112 if (!test_bit(zno, zone_info->active_zones)) {
1113 /* Active zone left? */
1114 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1116 if (test_and_set_bit(zno, zone_info->active_zones)) {
1117 /* Someone already set the bit */
1118 atomic_inc(&zone_info->active_zones_left);
1125 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1127 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1128 unsigned int zno = (pos >> zone_info->zone_size_shift);
1130 /* We can use any number of zones */
1131 if (zone_info->max_active_zones == 0)
1134 if (test_and_clear_bit(zno, zone_info->active_zones))
1135 atomic_inc(&zone_info->active_zones_left);
1138 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1139 u64 length, u64 *bytes)
1144 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1145 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1152 btrfs_dev_set_zone_empty(device, physical);
1153 btrfs_dev_clear_active_zone(device, physical);
1154 physical += device->zone_info->zone_size;
1155 length -= device->zone_info->zone_size;
1161 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1163 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1164 const u8 shift = zinfo->zone_size_shift;
1165 unsigned long begin = start >> shift;
1166 unsigned long nbits = size >> shift;
1170 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1171 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1173 if (begin + nbits > zinfo->nr_zones)
1176 /* All the zones are conventional */
1177 if (bitmap_test_range_all_zero(zinfo->seq_zones, begin, nbits))
1180 /* All the zones are sequential and empty */
1181 if (bitmap_test_range_all_set(zinfo->seq_zones, begin, nbits) &&
1182 bitmap_test_range_all_set(zinfo->empty_zones, begin, nbits))
1185 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1188 if (!btrfs_dev_is_sequential(device, pos) ||
1189 btrfs_dev_is_empty_zone(device, pos))
1192 /* Free regions should be empty */
1195 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1196 rcu_str_deref(device->name), device->devid, pos >> shift);
1199 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1209 * Calculate an allocation pointer from the extent allocation information
1210 * for a block group consist of conventional zones. It is pointed to the
1211 * end of the highest addressed extent in the block group as an allocation
1214 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1215 u64 *offset_ret, bool new)
1217 struct btrfs_fs_info *fs_info = cache->fs_info;
1218 struct btrfs_root *root;
1219 struct btrfs_path *path;
1220 struct btrfs_key key;
1221 struct btrfs_key found_key;
1226 * Avoid tree lookups for a new block group, there's no use for it.
1227 * It must always be 0.
1229 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1230 * For new a block group, this function is called from
1231 * btrfs_make_block_group() which is already taking the chunk mutex.
1232 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1233 * buffer locks to avoid deadlock.
1240 path = btrfs_alloc_path();
1244 key.objectid = cache->start + cache->length;
1248 root = btrfs_extent_root(fs_info, key.objectid);
1249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1250 /* We should not find the exact match */
1256 ret = btrfs_previous_extent_item(root, path, cache->start);
1265 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1267 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1268 length = found_key.offset;
1270 length = fs_info->nodesize;
1272 if (!(found_key.objectid >= cache->start &&
1273 found_key.objectid + length <= cache->start + cache->length)) {
1277 *offset_ret = found_key.objectid + length - cache->start;
1281 btrfs_free_path(path);
1291 static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
1292 struct zone_info *info, unsigned long *active,
1293 struct map_lookup *map)
1295 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1296 struct btrfs_device *device = map->stripes[zone_idx].dev;
1297 int dev_replace_is_ongoing = 0;
1298 unsigned int nofs_flag;
1299 struct blk_zone zone;
1302 info->physical = map->stripes[zone_idx].physical;
1304 if (!device->bdev) {
1305 info->alloc_offset = WP_MISSING_DEV;
1309 /* Consider a zone as active if we can allow any number of active zones. */
1310 if (!device->zone_info->max_active_zones)
1311 __set_bit(zone_idx, active);
1313 if (!btrfs_dev_is_sequential(device, info->physical)) {
1314 info->alloc_offset = WP_CONVENTIONAL;
1318 /* This zone will be used for allocation, so mark this zone non-empty. */
1319 btrfs_dev_clear_zone_empty(device, info->physical);
1321 down_read(&dev_replace->rwsem);
1322 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1323 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1324 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, info->physical);
1325 up_read(&dev_replace->rwsem);
1328 * The group is mapped to a sequential zone. Get the zone write pointer
1329 * to determine the allocation offset within the zone.
1331 WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size));
1332 nofs_flag = memalloc_nofs_save();
1333 ret = btrfs_get_dev_zone(device, info->physical, &zone);
1334 memalloc_nofs_restore(nofs_flag);
1336 if (ret != -EIO && ret != -EOPNOTSUPP)
1338 info->alloc_offset = WP_MISSING_DEV;
1342 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1343 btrfs_err_in_rcu(fs_info,
1344 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1345 zone.start << SECTOR_SHIFT, rcu_str_deref(device->name),
1350 info->capacity = (zone.capacity << SECTOR_SHIFT);
1352 switch (zone.cond) {
1353 case BLK_ZONE_COND_OFFLINE:
1354 case BLK_ZONE_COND_READONLY:
1356 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1357 (info->physical >> device->zone_info->zone_size_shift),
1358 rcu_str_deref(device->name), device->devid);
1359 info->alloc_offset = WP_MISSING_DEV;
1361 case BLK_ZONE_COND_EMPTY:
1362 info->alloc_offset = 0;
1364 case BLK_ZONE_COND_FULL:
1365 info->alloc_offset = info->capacity;
1368 /* Partially used zone. */
1369 info->alloc_offset = ((zone.wp - zone.start) << SECTOR_SHIFT);
1370 __set_bit(zone_idx, active);
1377 static int btrfs_load_block_group_single(struct btrfs_block_group *bg,
1378 struct zone_info *info,
1379 unsigned long *active)
1381 if (info->alloc_offset == WP_MISSING_DEV) {
1382 btrfs_err(bg->fs_info,
1383 "zoned: cannot recover write pointer for zone %llu",
1388 bg->alloc_offset = info->alloc_offset;
1389 bg->zone_capacity = info->capacity;
1390 if (test_bit(0, active))
1391 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1395 static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
1396 struct map_lookup *map,
1397 struct zone_info *zone_info,
1398 unsigned long *active)
1400 struct btrfs_fs_info *fs_info = bg->fs_info;
1402 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1403 btrfs_err(fs_info, "zoned: data DUP profile needs raid-stripe-tree");
1407 if (zone_info[0].alloc_offset == WP_MISSING_DEV) {
1408 btrfs_err(bg->fs_info,
1409 "zoned: cannot recover write pointer for zone %llu",
1410 zone_info[0].physical);
1413 if (zone_info[1].alloc_offset == WP_MISSING_DEV) {
1414 btrfs_err(bg->fs_info,
1415 "zoned: cannot recover write pointer for zone %llu",
1416 zone_info[1].physical);
1419 if (zone_info[0].alloc_offset != zone_info[1].alloc_offset) {
1420 btrfs_err(bg->fs_info,
1421 "zoned: write pointer offset mismatch of zones in DUP profile");
1425 if (test_bit(0, active) != test_bit(1, active)) {
1426 if (!btrfs_zone_activate(bg))
1428 } else if (test_bit(0, active)) {
1429 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1432 bg->alloc_offset = zone_info[0].alloc_offset;
1433 bg->zone_capacity = min(zone_info[0].capacity, zone_info[1].capacity);
1437 static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
1438 struct map_lookup *map,
1439 struct zone_info *zone_info,
1440 unsigned long *active)
1442 struct btrfs_fs_info *fs_info = bg->fs_info;
1445 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1446 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1447 btrfs_bg_type_to_raid_name(map->type));
1451 for (i = 0; i < map->num_stripes; i++) {
1452 if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1453 zone_info[i].alloc_offset == WP_CONVENTIONAL)
1456 if ((zone_info[0].alloc_offset != zone_info[i].alloc_offset) &&
1457 !btrfs_test_opt(fs_info, DEGRADED)) {
1459 "zoned: write pointer offset mismatch of zones in %s profile",
1460 btrfs_bg_type_to_raid_name(map->type));
1463 if (test_bit(0, active) != test_bit(i, active)) {
1464 if (!btrfs_test_opt(fs_info, DEGRADED) &&
1465 !btrfs_zone_activate(bg)) {
1469 if (test_bit(0, active))
1470 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1472 /* In case a device is missing we have a cap of 0, so don't use it. */
1473 bg->zone_capacity = min_not_zero(zone_info[0].capacity,
1474 zone_info[1].capacity);
1477 if (zone_info[0].alloc_offset != WP_MISSING_DEV)
1478 bg->alloc_offset = zone_info[0].alloc_offset;
1480 bg->alloc_offset = zone_info[i - 1].alloc_offset;
1485 static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
1486 struct map_lookup *map,
1487 struct zone_info *zone_info,
1488 unsigned long *active)
1490 struct btrfs_fs_info *fs_info = bg->fs_info;
1492 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1493 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1494 btrfs_bg_type_to_raid_name(map->type));
1498 for (int i = 0; i < map->num_stripes; i++) {
1499 if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1500 zone_info[i].alloc_offset == WP_CONVENTIONAL)
1503 if (test_bit(0, active) != test_bit(i, active)) {
1504 if (!btrfs_zone_activate(bg))
1507 if (test_bit(0, active))
1508 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1510 bg->zone_capacity += zone_info[i].capacity;
1511 bg->alloc_offset += zone_info[i].alloc_offset;
1517 static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
1518 struct map_lookup *map,
1519 struct zone_info *zone_info,
1520 unsigned long *active)
1522 struct btrfs_fs_info *fs_info = bg->fs_info;
1524 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1525 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1526 btrfs_bg_type_to_raid_name(map->type));
1530 for (int i = 0; i < map->num_stripes; i++) {
1531 if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1532 zone_info[i].alloc_offset == WP_CONVENTIONAL)
1535 if (test_bit(0, active) != test_bit(i, active)) {
1536 if (!btrfs_zone_activate(bg))
1539 if (test_bit(0, active))
1540 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1543 if ((i % map->sub_stripes) == 0) {
1544 bg->zone_capacity += zone_info[i].capacity;
1545 bg->alloc_offset += zone_info[i].alloc_offset;
1552 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1554 struct btrfs_fs_info *fs_info = cache->fs_info;
1555 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1556 struct extent_map *em;
1557 struct map_lookup *map;
1558 u64 logical = cache->start;
1559 u64 length = cache->length;
1560 struct zone_info *zone_info = NULL;
1563 unsigned long *active = NULL;
1565 u32 num_sequential = 0, num_conventional = 0;
1567 if (!btrfs_is_zoned(fs_info))
1571 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1573 "zoned: block group %llu len %llu unaligned to zone size %llu",
1574 logical, length, fs_info->zone_size);
1578 /* Get the chunk mapping */
1579 read_lock(&em_tree->lock);
1580 em = lookup_extent_mapping(em_tree, logical, length);
1581 read_unlock(&em_tree->lock);
1586 map = em->map_lookup;
1588 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1589 if (!cache->physical_map) {
1594 zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS);
1600 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1606 for (i = 0; i < map->num_stripes; i++) {
1607 ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map);
1611 if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1617 if (num_sequential > 0)
1618 set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1620 if (num_conventional > 0) {
1621 /* Zone capacity is always zone size in emulation */
1622 cache->zone_capacity = cache->length;
1623 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1626 "zoned: failed to determine allocation offset of bg %llu",
1629 } else if (map->num_stripes == num_conventional) {
1630 cache->alloc_offset = last_alloc;
1631 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1636 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1637 case 0: /* single */
1638 ret = btrfs_load_block_group_single(cache, &zone_info[0], active);
1640 case BTRFS_BLOCK_GROUP_DUP:
1641 ret = btrfs_load_block_group_dup(cache, map, zone_info, active);
1643 case BTRFS_BLOCK_GROUP_RAID1:
1644 case BTRFS_BLOCK_GROUP_RAID1C3:
1645 case BTRFS_BLOCK_GROUP_RAID1C4:
1646 ret = btrfs_load_block_group_raid1(cache, map, zone_info, active);
1648 case BTRFS_BLOCK_GROUP_RAID0:
1649 ret = btrfs_load_block_group_raid0(cache, map, zone_info, active);
1651 case BTRFS_BLOCK_GROUP_RAID10:
1652 ret = btrfs_load_block_group_raid10(cache, map, zone_info, active);
1654 case BTRFS_BLOCK_GROUP_RAID5:
1655 case BTRFS_BLOCK_GROUP_RAID6:
1657 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1658 btrfs_bg_type_to_raid_name(map->type));
1664 if (cache->alloc_offset > fs_info->zone_size) {
1666 "zoned: invalid write pointer %llu in block group %llu",
1667 cache->alloc_offset, cache->start);
1671 if (cache->alloc_offset > cache->zone_capacity) {
1673 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1674 cache->alloc_offset, cache->zone_capacity,
1679 /* An extent is allocated after the write pointer */
1680 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1682 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1683 logical, last_alloc, cache->alloc_offset);
1688 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1689 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1690 btrfs_get_block_group(cache);
1691 spin_lock(&fs_info->zone_active_bgs_lock);
1692 list_add_tail(&cache->active_bg_list,
1693 &fs_info->zone_active_bgs);
1694 spin_unlock(&fs_info->zone_active_bgs_lock);
1697 kfree(cache->physical_map);
1698 cache->physical_map = NULL;
1700 bitmap_free(active);
1702 free_extent_map(em);
1707 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1711 if (!btrfs_is_zoned(cache->fs_info))
1714 WARN_ON(cache->bytes_super != 0);
1715 unusable = (cache->alloc_offset - cache->used) +
1716 (cache->length - cache->zone_capacity);
1717 free = cache->zone_capacity - cache->alloc_offset;
1719 /* We only need ->free_space in ALLOC_SEQ block groups */
1720 cache->cached = BTRFS_CACHE_FINISHED;
1721 cache->free_space_ctl->free_space = free;
1722 cache->zone_unusable = unusable;
1725 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1726 struct extent_buffer *eb)
1728 if (!btrfs_is_zoned(eb->fs_info) ||
1729 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN))
1732 ASSERT(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1734 memzero_extent_buffer(eb, 0, eb->len);
1735 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1736 set_extent_buffer_dirty(eb);
1737 set_extent_bit(&trans->dirty_pages, eb->start, eb->start + eb->len - 1,
1738 EXTENT_DIRTY, NULL);
1741 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
1743 u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
1744 struct btrfs_inode *inode = bbio->inode;
1745 struct btrfs_fs_info *fs_info = bbio->fs_info;
1746 struct btrfs_block_group *cache;
1749 if (!btrfs_is_zoned(fs_info))
1752 if (!inode || !is_data_inode(&inode->vfs_inode))
1755 if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE)
1759 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1760 * extent layout the relocation code has.
1761 * Furthermore we have set aside own block-group from which only the
1762 * relocation "process" can allocate and make sure only one process at a
1763 * time can add pages to an extent that gets relocated, so it's safe to
1764 * use regular REQ_OP_WRITE for this special case.
1766 if (btrfs_is_data_reloc_root(inode->root))
1769 cache = btrfs_lookup_block_group(fs_info, start);
1774 ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1775 btrfs_put_block_group(cache);
1780 void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
1782 const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
1783 struct btrfs_ordered_sum *sum = bbio->sums;
1785 if (physical < bbio->orig_physical)
1786 sum->logical -= bbio->orig_physical - physical;
1788 sum->logical += physical - bbio->orig_physical;
1791 static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered,
1794 struct extent_map_tree *em_tree = &BTRFS_I(ordered->inode)->extent_tree;
1795 struct extent_map *em;
1797 ordered->disk_bytenr = logical;
1799 write_lock(&em_tree->lock);
1800 em = search_extent_mapping(em_tree, ordered->file_offset,
1801 ordered->num_bytes);
1802 em->block_start = logical;
1803 free_extent_map(em);
1804 write_unlock(&em_tree->lock);
1807 static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
1808 u64 logical, u64 len)
1810 struct btrfs_ordered_extent *new;
1812 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
1813 split_extent_map(BTRFS_I(ordered->inode), ordered->file_offset,
1814 ordered->num_bytes, len, logical))
1817 new = btrfs_split_ordered_extent(ordered, len);
1820 new->disk_bytenr = logical;
1821 btrfs_finish_one_ordered(new);
1825 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
1827 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1828 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1829 struct btrfs_ordered_sum *sum;
1833 * Write to pre-allocated region is for the data relocation, and so
1834 * it should use WRITE operation. No split/rewrite are necessary.
1836 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
1839 ASSERT(!list_empty(&ordered->list));
1840 /* The ordered->list can be empty in the above pre-alloc case. */
1841 sum = list_first_entry(&ordered->list, struct btrfs_ordered_sum, list);
1842 logical = sum->logical;
1845 while (len < ordered->disk_num_bytes) {
1846 sum = list_next_entry(sum, list);
1847 if (sum->logical == logical + len) {
1851 if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
1852 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
1853 btrfs_err(fs_info, "failed to split ordered extent");
1856 logical = sum->logical;
1860 if (ordered->disk_bytenr != logical)
1861 btrfs_rewrite_logical_zoned(ordered, logical);
1865 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
1866 * were allocated by btrfs_alloc_dummy_sum only to record the logical
1867 * addresses and don't contain actual checksums. We thus must free them
1868 * here so that we don't attempt to log the csums later.
1870 if ((inode->flags & BTRFS_INODE_NODATASUM) ||
1871 test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) {
1872 while ((sum = list_first_entry_or_null(&ordered->list,
1873 typeof(*sum), list))) {
1874 list_del(&sum->list);
1880 static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
1881 struct btrfs_block_group **active_bg)
1883 const struct writeback_control *wbc = ctx->wbc;
1884 struct btrfs_block_group *block_group = ctx->zoned_bg;
1885 struct btrfs_fs_info *fs_info = block_group->fs_info;
1887 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
1890 if (fs_info->treelog_bg == block_group->start) {
1891 if (!btrfs_zone_activate(block_group)) {
1892 int ret_fin = btrfs_zone_finish_one_bg(fs_info);
1894 if (ret_fin != 1 || !btrfs_zone_activate(block_group))
1897 } else if (*active_bg != block_group) {
1898 struct btrfs_block_group *tgt = *active_bg;
1900 /* zoned_meta_io_lock protects fs_info->active_{meta,system}_bg. */
1901 lockdep_assert_held(&fs_info->zoned_meta_io_lock);
1905 * If there is an unsent IO left in the allocated area,
1906 * we cannot wait for them as it may cause a deadlock.
1908 if (tgt->meta_write_pointer < tgt->start + tgt->alloc_offset) {
1909 if (wbc->sync_mode == WB_SYNC_NONE ||
1910 (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync))
1914 /* Pivot active metadata/system block group. */
1915 btrfs_zoned_meta_io_unlock(fs_info);
1916 wait_eb_writebacks(tgt);
1917 do_zone_finish(tgt, true);
1918 btrfs_zoned_meta_io_lock(fs_info);
1919 if (*active_bg == tgt) {
1920 btrfs_put_block_group(tgt);
1924 if (!btrfs_zone_activate(block_group))
1926 if (*active_bg != block_group) {
1927 ASSERT(*active_bg == NULL);
1928 *active_bg = block_group;
1929 btrfs_get_block_group(block_group);
1937 * Check if @ctx->eb is aligned to the write pointer.
1940 * 0: @ctx->eb is at the write pointer. You can write it.
1941 * -EAGAIN: There is a hole. The caller should handle the case.
1942 * -EBUSY: There is a hole, but the caller can just bail out.
1944 int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1945 struct btrfs_eb_write_context *ctx)
1947 const struct writeback_control *wbc = ctx->wbc;
1948 const struct extent_buffer *eb = ctx->eb;
1949 struct btrfs_block_group *block_group = ctx->zoned_bg;
1951 if (!btrfs_is_zoned(fs_info))
1955 if (block_group->start > eb->start ||
1956 block_group->start + block_group->length <= eb->start) {
1957 btrfs_put_block_group(block_group);
1959 ctx->zoned_bg = NULL;
1964 block_group = btrfs_lookup_block_group(fs_info, eb->start);
1967 ctx->zoned_bg = block_group;
1970 if (block_group->meta_write_pointer == eb->start) {
1971 struct btrfs_block_group **tgt;
1973 if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
1976 if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)
1977 tgt = &fs_info->active_system_bg;
1979 tgt = &fs_info->active_meta_bg;
1980 if (check_bg_is_active(ctx, tgt))
1985 * Since we may release fs_info->zoned_meta_io_lock, someone can already
1986 * start writing this eb. In that case, we can just bail out.
1988 if (block_group->meta_write_pointer > eb->start)
1991 /* If for_sync, this hole will be filled with trasnsaction commit. */
1992 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
1997 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1999 if (!btrfs_dev_is_sequential(device, physical))
2002 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
2003 length >> SECTOR_SHIFT, GFP_NOFS, 0);
2006 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
2007 struct blk_zone *zone)
2009 struct btrfs_io_context *bioc = NULL;
2010 u64 mapped_length = PAGE_SIZE;
2011 unsigned int nofs_flag;
2015 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
2016 &mapped_length, &bioc, NULL, NULL);
2017 if (ret || !bioc || mapped_length < PAGE_SIZE) {
2022 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
2027 nofs_flag = memalloc_nofs_save();
2028 nmirrors = (int)bioc->num_stripes;
2029 for (i = 0; i < nmirrors; i++) {
2030 u64 physical = bioc->stripes[i].physical;
2031 struct btrfs_device *dev = bioc->stripes[i].dev;
2033 /* Missing device */
2037 ret = btrfs_get_dev_zone(dev, physical, zone);
2038 /* Failing device */
2039 if (ret == -EIO || ret == -EOPNOTSUPP)
2043 memalloc_nofs_restore(nofs_flag);
2045 btrfs_put_bioc(bioc);
2050 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
2051 * filling zeros between @physical_pos to a write pointer of dev-replace
2054 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
2055 u64 physical_start, u64 physical_pos)
2057 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
2058 struct blk_zone zone;
2063 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
2066 ret = read_zone_info(fs_info, logical, &zone);
2070 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
2072 if (physical_pos == wp)
2075 if (physical_pos > wp)
2078 length = wp - physical_pos;
2079 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
2083 * Activate block group and underlying device zones
2085 * @block_group: the block group to activate
2087 * Return: true on success, false otherwise
2089 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
2091 struct btrfs_fs_info *fs_info = block_group->fs_info;
2092 struct map_lookup *map;
2093 struct btrfs_device *device;
2095 const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
2099 if (!btrfs_is_zoned(block_group->fs_info))
2102 map = block_group->physical_map;
2104 spin_lock(&block_group->lock);
2105 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2111 if (btrfs_zoned_bg_is_full(block_group)) {
2116 spin_lock(&fs_info->zone_active_bgs_lock);
2117 for (i = 0; i < map->num_stripes; i++) {
2118 struct btrfs_zoned_device_info *zinfo;
2121 device = map->stripes[i].dev;
2122 physical = map->stripes[i].physical;
2123 zinfo = device->zone_info;
2125 if (zinfo->max_active_zones == 0)
2129 reserved = zinfo->reserved_active_zones;
2131 * For the data block group, leave active zones for one
2132 * metadata block group and one system block group.
2134 if (atomic_read(&zinfo->active_zones_left) <= reserved) {
2136 spin_unlock(&fs_info->zone_active_bgs_lock);
2140 if (!btrfs_dev_set_active_zone(device, physical)) {
2141 /* Cannot activate the zone */
2143 spin_unlock(&fs_info->zone_active_bgs_lock);
2147 zinfo->reserved_active_zones--;
2149 spin_unlock(&fs_info->zone_active_bgs_lock);
2151 /* Successfully activated all the zones */
2152 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2153 spin_unlock(&block_group->lock);
2155 /* For the active block group list */
2156 btrfs_get_block_group(block_group);
2158 spin_lock(&fs_info->zone_active_bgs_lock);
2159 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
2160 spin_unlock(&fs_info->zone_active_bgs_lock);
2165 spin_unlock(&block_group->lock);
2169 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
2171 struct btrfs_fs_info *fs_info = block_group->fs_info;
2172 const u64 end = block_group->start + block_group->length;
2173 struct radix_tree_iter iter;
2174 struct extent_buffer *eb;
2178 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
2179 block_group->start >> fs_info->sectorsize_bits) {
2180 eb = radix_tree_deref_slot(slot);
2183 if (radix_tree_deref_retry(eb)) {
2184 slot = radix_tree_iter_retry(&iter);
2188 if (eb->start < block_group->start)
2190 if (eb->start >= end)
2193 slot = radix_tree_iter_resume(slot, &iter);
2195 wait_on_extent_buffer_writeback(eb);
2201 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
2203 struct btrfs_fs_info *fs_info = block_group->fs_info;
2204 struct map_lookup *map;
2205 const bool is_metadata = (block_group->flags &
2206 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
2210 spin_lock(&block_group->lock);
2211 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2212 spin_unlock(&block_group->lock);
2216 /* Check if we have unwritten allocated space */
2218 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
2219 spin_unlock(&block_group->lock);
2224 * If we are sure that the block group is full (= no more room left for
2225 * new allocation) and the IO for the last usable block is completed, we
2226 * don't need to wait for the other IOs. This holds because we ensure
2227 * the sequential IO submissions using the ZONE_APPEND command for data
2228 * and block_group->meta_write_pointer for metadata.
2230 if (!fully_written) {
2231 if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2232 spin_unlock(&block_group->lock);
2235 spin_unlock(&block_group->lock);
2237 ret = btrfs_inc_block_group_ro(block_group, false);
2241 /* Ensure all writes in this block group finish */
2242 btrfs_wait_block_group_reservations(block_group);
2243 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
2244 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
2245 block_group->length);
2246 /* Wait for extent buffers to be written. */
2248 wait_eb_writebacks(block_group);
2250 spin_lock(&block_group->lock);
2253 * Bail out if someone already deactivated the block group, or
2254 * allocated space is left in the block group.
2256 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2257 &block_group->runtime_flags)) {
2258 spin_unlock(&block_group->lock);
2259 btrfs_dec_block_group_ro(block_group);
2263 if (block_group->reserved ||
2264 test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2265 &block_group->runtime_flags)) {
2266 spin_unlock(&block_group->lock);
2267 btrfs_dec_block_group_ro(block_group);
2272 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2273 block_group->alloc_offset = block_group->zone_capacity;
2274 if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
2275 block_group->meta_write_pointer = block_group->start +
2276 block_group->zone_capacity;
2277 block_group->free_space_ctl->free_space = 0;
2278 btrfs_clear_treelog_bg(block_group);
2279 btrfs_clear_data_reloc_bg(block_group);
2280 spin_unlock(&block_group->lock);
2282 map = block_group->physical_map;
2283 for (i = 0; i < map->num_stripes; i++) {
2284 struct btrfs_device *device = map->stripes[i].dev;
2285 const u64 physical = map->stripes[i].physical;
2286 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2288 if (zinfo->max_active_zones == 0)
2291 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2292 physical >> SECTOR_SHIFT,
2293 zinfo->zone_size >> SECTOR_SHIFT,
2299 if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA))
2300 zinfo->reserved_active_zones++;
2301 btrfs_dev_clear_active_zone(device, physical);
2305 btrfs_dec_block_group_ro(block_group);
2307 spin_lock(&fs_info->zone_active_bgs_lock);
2308 ASSERT(!list_empty(&block_group->active_bg_list));
2309 list_del_init(&block_group->active_bg_list);
2310 spin_unlock(&fs_info->zone_active_bgs_lock);
2312 /* For active_bg_list */
2313 btrfs_put_block_group(block_group);
2315 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2320 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2322 if (!btrfs_is_zoned(block_group->fs_info))
2325 return do_zone_finish(block_group, false);
2328 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2330 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2331 struct btrfs_device *device;
2334 if (!btrfs_is_zoned(fs_info))
2337 /* Check if there is a device with active zones left */
2338 mutex_lock(&fs_info->chunk_mutex);
2339 spin_lock(&fs_info->zone_active_bgs_lock);
2340 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2341 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2347 if (!zinfo->max_active_zones) {
2352 if (flags & BTRFS_BLOCK_GROUP_DATA)
2353 reserved = zinfo->reserved_active_zones;
2355 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
2356 case 0: /* single */
2357 ret = (atomic_read(&zinfo->active_zones_left) >= (1 + reserved));
2359 case BTRFS_BLOCK_GROUP_DUP:
2360 ret = (atomic_read(&zinfo->active_zones_left) >= (2 + reserved));
2366 spin_unlock(&fs_info->zone_active_bgs_lock);
2367 mutex_unlock(&fs_info->chunk_mutex);
2370 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2375 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2377 struct btrfs_block_group *block_group;
2378 u64 min_alloc_bytes;
2380 if (!btrfs_is_zoned(fs_info))
2383 block_group = btrfs_lookup_block_group(fs_info, logical);
2384 ASSERT(block_group);
2386 /* No MIXED_BG on zoned btrfs. */
2387 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2388 min_alloc_bytes = fs_info->sectorsize;
2390 min_alloc_bytes = fs_info->nodesize;
2392 /* Bail out if we can allocate more data from this block group. */
2393 if (logical + length + min_alloc_bytes <=
2394 block_group->start + block_group->zone_capacity)
2397 do_zone_finish(block_group, true);
2400 btrfs_put_block_group(block_group);
2403 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2405 struct btrfs_block_group *bg =
2406 container_of(work, struct btrfs_block_group, zone_finish_work);
2408 wait_on_extent_buffer_writeback(bg->last_eb);
2409 free_extent_buffer(bg->last_eb);
2410 btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2411 btrfs_put_block_group(bg);
2414 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2415 struct extent_buffer *eb)
2417 if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
2418 eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2421 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2422 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2428 btrfs_get_block_group(bg);
2429 atomic_inc(&eb->refs);
2431 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2432 queue_work(system_unbound_wq, &bg->zone_finish_work);
2435 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2437 struct btrfs_fs_info *fs_info = bg->fs_info;
2439 spin_lock(&fs_info->relocation_bg_lock);
2440 if (fs_info->data_reloc_bg == bg->start)
2441 fs_info->data_reloc_bg = 0;
2442 spin_unlock(&fs_info->relocation_bg_lock);
2445 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2447 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2448 struct btrfs_device *device;
2450 if (!btrfs_is_zoned(fs_info))
2453 mutex_lock(&fs_devices->device_list_mutex);
2454 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2455 if (device->zone_info) {
2456 vfree(device->zone_info->zone_cache);
2457 device->zone_info->zone_cache = NULL;
2460 mutex_unlock(&fs_devices->device_list_mutex);
2463 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2465 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2466 struct btrfs_device *device;
2471 ASSERT(btrfs_is_zoned(fs_info));
2473 if (fs_info->bg_reclaim_threshold == 0)
2476 mutex_lock(&fs_devices->device_list_mutex);
2477 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2481 total += device->disk_total_bytes;
2482 used += device->bytes_used;
2484 mutex_unlock(&fs_devices->device_list_mutex);
2486 factor = div64_u64(used * 100, total);
2487 return factor >= fs_info->bg_reclaim_threshold;
2490 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2493 struct btrfs_block_group *block_group;
2495 if (!btrfs_is_zoned(fs_info))
2498 block_group = btrfs_lookup_block_group(fs_info, logical);
2499 /* It should be called on a previous data relocation block group. */
2500 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2502 spin_lock(&block_group->lock);
2503 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2506 /* All relocation extents are written. */
2507 if (block_group->start + block_group->alloc_offset == logical + length) {
2509 * Now, release this block group for further allocations and
2512 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2513 &block_group->runtime_flags);
2517 spin_unlock(&block_group->lock);
2518 btrfs_put_block_group(block_group);
2521 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2523 struct btrfs_block_group *block_group;
2524 struct btrfs_block_group *min_bg = NULL;
2525 u64 min_avail = U64_MAX;
2528 spin_lock(&fs_info->zone_active_bgs_lock);
2529 list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2533 spin_lock(&block_group->lock);
2534 if (block_group->reserved || block_group->alloc_offset == 0 ||
2535 (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM) ||
2536 test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2537 spin_unlock(&block_group->lock);
2541 avail = block_group->zone_capacity - block_group->alloc_offset;
2542 if (min_avail > avail) {
2544 btrfs_put_block_group(min_bg);
2545 min_bg = block_group;
2547 btrfs_get_block_group(min_bg);
2549 spin_unlock(&block_group->lock);
2551 spin_unlock(&fs_info->zone_active_bgs_lock);
2556 ret = btrfs_zone_finish(min_bg);
2557 btrfs_put_block_group(min_bg);
2559 return ret < 0 ? ret : 1;
2562 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2563 struct btrfs_space_info *space_info,
2566 struct btrfs_block_group *bg;
2569 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2574 bool need_finish = false;
2576 down_read(&space_info->groups_sem);
2577 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2578 list_for_each_entry(bg, &space_info->block_groups[index],
2580 if (!spin_trylock(&bg->lock))
2582 if (btrfs_zoned_bg_is_full(bg) ||
2583 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2584 &bg->runtime_flags)) {
2585 spin_unlock(&bg->lock);
2588 spin_unlock(&bg->lock);
2590 if (btrfs_zone_activate(bg)) {
2591 up_read(&space_info->groups_sem);
2598 up_read(&space_info->groups_sem);
2600 if (!do_finish || !need_finish)
2603 ret = btrfs_zone_finish_one_bg(fs_info);
2614 * Reserve zones for one metadata block group, one tree-log block group, and one
2615 * system block group.
2617 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
2619 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2620 struct btrfs_block_group *block_group;
2621 struct btrfs_device *device;
2622 /* Reserve zones for normal SINGLE metadata and tree-log block group. */
2623 unsigned int metadata_reserve = 2;
2624 /* Reserve a zone for SINGLE system block group. */
2625 unsigned int system_reserve = 1;
2627 if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
2631 * This function is called from the mount context. So, there is no
2632 * parallel process touching the bits. No need for read_seqretry().
2634 if (fs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
2635 metadata_reserve = 4;
2636 if (fs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
2639 /* Apply the reservation on all the devices. */
2640 mutex_lock(&fs_devices->device_list_mutex);
2641 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2645 device->zone_info->reserved_active_zones =
2646 metadata_reserve + system_reserve;
2648 mutex_unlock(&fs_devices->device_list_mutex);
2650 /* Release reservation for currently active block groups. */
2651 spin_lock(&fs_info->zone_active_bgs_lock);
2652 list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
2653 struct map_lookup *map = block_group->physical_map;
2655 if (!(block_group->flags &
2656 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
2659 for (int i = 0; i < map->num_stripes; i++)
2660 map->stripes[i].dev->zone_info->reserved_active_zones--;
2662 spin_unlock(&fs_info->zone_active_bgs_lock);