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"
19 /* Maximum number of zones to report per blkdev_report_zones() call */
20 #define BTRFS_REPORT_NR_ZONES 4096
21 /* Invalid allocation pointer value for missing devices */
22 #define WP_MISSING_DEV ((u64)-1)
23 /* Pseudo write pointer value for conventional zone */
24 #define WP_CONVENTIONAL ((u64)-2)
27 * Location of the first zone of superblock logging zone pairs.
29 * - primary superblock: 0B (zone 0)
30 * - first copy: 512G (zone starting at that offset)
31 * - second copy: 4T (zone starting at that offset)
33 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
34 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
35 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
37 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
38 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
40 /* Number of superblock log zones */
41 #define BTRFS_NR_SB_LOG_ZONES 2
44 * Minimum of active zones we need:
46 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
47 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
48 * - 1 zone for tree-log dedicated block group
49 * - 1 zone for relocation
51 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
54 * Minimum / maximum supported zone size. Currently, SMR disks have a zone
55 * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
56 * We do not expect the zone size to become larger than 8GiB or smaller than
57 * 4MiB in the near future.
59 #define BTRFS_MAX_ZONE_SIZE SZ_8G
60 #define BTRFS_MIN_ZONE_SIZE SZ_4M
62 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
64 static inline bool sb_zone_is_full(const struct blk_zone *zone)
66 return (zone->cond == BLK_ZONE_COND_FULL) ||
67 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
70 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
72 struct blk_zone *zones = data;
74 memcpy(&zones[idx], zone, sizeof(*zone));
79 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
82 bool empty[BTRFS_NR_SB_LOG_ZONES];
83 bool full[BTRFS_NR_SB_LOG_ZONES];
87 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
88 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
89 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
90 full[i] = sb_zone_is_full(&zones[i]);
94 * Possible states of log buffer zones
96 * Empty[0] In use[0] Full[0]
102 * *: Special case, no superblock is written
103 * 0: Use write pointer of zones[0]
104 * 1: Use write pointer of zones[1]
105 * C: Compare super blocks from zones[0] and zones[1], use the latest
106 * one determined by generation
110 if (empty[0] && empty[1]) {
111 /* Special case to distinguish no superblock to read */
112 *wp_ret = zones[0].start << SECTOR_SHIFT;
114 } else if (full[0] && full[1]) {
115 /* Compare two super blocks */
116 struct address_space *mapping = bdev->bd_inode->i_mapping;
117 struct page *page[BTRFS_NR_SB_LOG_ZONES];
118 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
121 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
124 bytenr = ((zones[i].start + zones[i].len)
125 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
127 page[i] = read_cache_page_gfp(mapping,
128 bytenr >> PAGE_SHIFT, GFP_NOFS);
129 if (IS_ERR(page[i])) {
131 btrfs_release_disk_super(super[0]);
132 return PTR_ERR(page[i]);
134 super[i] = page_address(page[i]);
137 if (btrfs_super_generation(super[0]) >
138 btrfs_super_generation(super[1]))
139 sector = zones[1].start;
141 sector = zones[0].start;
143 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
144 btrfs_release_disk_super(super[i]);
145 } else if (!full[0] && (empty[1] || full[1])) {
146 sector = zones[0].wp;
147 } else if (full[0]) {
148 sector = zones[1].wp;
152 *wp_ret = sector << SECTOR_SHIFT;
157 * Get the first zone number of the superblock mirror
159 static inline u32 sb_zone_number(int shift, int mirror)
163 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
165 case 0: zone = 0; break;
166 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
167 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
170 ASSERT(zone <= U32_MAX);
175 static inline sector_t zone_start_sector(u32 zone_number,
176 struct block_device *bdev)
178 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
181 static inline u64 zone_start_physical(u32 zone_number,
182 struct btrfs_zoned_device_info *zone_info)
184 return (u64)zone_number << zone_info->zone_size_shift;
188 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
189 * device into static sized chunks and fake a conventional zone on each of
192 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
193 struct blk_zone *zones, unsigned int nr_zones)
195 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
196 sector_t bdev_size = bdev_nr_sectors(device->bdev);
199 pos >>= SECTOR_SHIFT;
200 for (i = 0; i < nr_zones; i++) {
201 zones[i].start = i * zone_sectors + pos;
202 zones[i].len = zone_sectors;
203 zones[i].capacity = zone_sectors;
204 zones[i].wp = zones[i].start + zone_sectors;
205 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
206 zones[i].cond = BLK_ZONE_COND_NOT_WP;
208 if (zones[i].wp >= bdev_size) {
217 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
218 struct blk_zone *zones, unsigned int *nr_zones)
220 struct btrfs_zoned_device_info *zinfo = device->zone_info;
227 if (!bdev_is_zoned(device->bdev)) {
228 ret = emulate_report_zones(device, pos, zones, *nr_zones);
234 if (zinfo->zone_cache) {
237 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
238 zno = pos >> zinfo->zone_size_shift;
240 * We cannot report zones beyond the zone end. So, it is OK to
241 * cap *nr_zones to at the end.
243 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
245 for (i = 0; i < *nr_zones; i++) {
246 struct blk_zone *zone_info;
248 zone_info = &zinfo->zone_cache[zno + i];
253 if (i == *nr_zones) {
254 /* Cache hit on all the zones */
255 memcpy(zones, zinfo->zone_cache + zno,
256 sizeof(*zinfo->zone_cache) * *nr_zones);
261 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
262 copy_zone_info_cb, zones);
264 btrfs_err_in_rcu(device->fs_info,
265 "zoned: failed to read zone %llu on %s (devid %llu)",
266 pos, rcu_str_deref(device->name),
275 if (zinfo->zone_cache)
276 memcpy(zinfo->zone_cache + zno, zones,
277 sizeof(*zinfo->zone_cache) * *nr_zones);
282 /* The emulated zone size is determined from the size of device extent */
283 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
285 struct btrfs_path *path;
286 struct btrfs_root *root = fs_info->dev_root;
287 struct btrfs_key key;
288 struct extent_buffer *leaf;
289 struct btrfs_dev_extent *dext;
293 key.type = BTRFS_DEV_EXTENT_KEY;
296 path = btrfs_alloc_path();
300 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
304 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
305 ret = btrfs_next_leaf(root, path);
308 /* No dev extents at all? Not good */
315 leaf = path->nodes[0];
316 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
317 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
321 btrfs_free_path(path);
326 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
328 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
329 struct btrfs_device *device;
332 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
333 if (!btrfs_fs_incompat(fs_info, ZONED))
336 mutex_lock(&fs_devices->device_list_mutex);
337 list_for_each_entry(device, &fs_devices->devices, dev_list) {
338 /* We can skip reading of zone info for missing devices */
342 ret = btrfs_get_dev_zone_info(device, true);
346 mutex_unlock(&fs_devices->device_list_mutex);
351 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
353 struct btrfs_fs_info *fs_info = device->fs_info;
354 struct btrfs_zoned_device_info *zone_info = NULL;
355 struct block_device *bdev = device->bdev;
356 unsigned int max_active_zones;
357 unsigned int nactive;
360 struct blk_zone *zones = NULL;
361 unsigned int i, nreported = 0, nr_zones;
362 sector_t zone_sectors;
363 char *model, *emulated;
367 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
370 if (!btrfs_fs_incompat(fs_info, ZONED))
373 if (device->zone_info)
376 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
380 device->zone_info = zone_info;
382 if (!bdev_is_zoned(bdev)) {
383 if (!fs_info->zone_size) {
384 ret = calculate_emulated_zone_size(fs_info);
389 ASSERT(fs_info->zone_size);
390 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
392 zone_sectors = bdev_zone_sectors(bdev);
395 /* Check if it's power of 2 (see is_power_of_2) */
396 ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
397 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
399 /* We reject devices with a zone size larger than 8GB */
400 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
401 btrfs_err_in_rcu(fs_info,
402 "zoned: %s: zone size %llu larger than supported maximum %llu",
403 rcu_str_deref(device->name),
404 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
407 } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
408 btrfs_err_in_rcu(fs_info,
409 "zoned: %s: zone size %llu smaller than supported minimum %u",
410 rcu_str_deref(device->name),
411 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
416 nr_sectors = bdev_nr_sectors(bdev);
417 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
418 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
420 * We limit max_zone_append_size also by max_segments *
421 * PAGE_SIZE. Technically, we can have multiple pages per segment. But,
422 * since btrfs adds the pages one by one to a bio, and btrfs cannot
423 * increase the metadata reservation even if it increases the number of
424 * extents, it is safe to stick with the limit.
426 * With the zoned emulation, we can have non-zoned device on the zoned
427 * mode. In this case, we don't have a valid max zone append size. So,
428 * use max_segments * PAGE_SIZE as the pseudo max_zone_append_size.
430 if (bdev_is_zoned(bdev)) {
431 zone_info->max_zone_append_size = min_t(u64,
432 (u64)bdev_max_zone_append_sectors(bdev) << SECTOR_SHIFT,
433 (u64)bdev_max_segments(bdev) << PAGE_SHIFT);
435 zone_info->max_zone_append_size =
436 (u64)bdev_max_segments(bdev) << PAGE_SHIFT;
438 if (!IS_ALIGNED(nr_sectors, zone_sectors))
439 zone_info->nr_zones++;
441 max_active_zones = bdev_max_active_zones(bdev);
442 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
443 btrfs_err_in_rcu(fs_info,
444 "zoned: %s: max active zones %u is too small, need at least %u active zones",
445 rcu_str_deref(device->name), max_active_zones,
446 BTRFS_MIN_ACTIVE_ZONES);
450 zone_info->max_active_zones = max_active_zones;
452 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
453 if (!zone_info->seq_zones) {
458 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
459 if (!zone_info->empty_zones) {
464 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
465 if (!zone_info->active_zones) {
470 zones = kvcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
477 * Enable zone cache only for a zoned device. On a non-zoned device, we
478 * fill the zone info with emulated CONVENTIONAL zones, so no need to
481 if (populate_cache && bdev_is_zoned(device->bdev)) {
482 zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
483 zone_info->nr_zones);
484 if (!zone_info->zone_cache) {
485 btrfs_err_in_rcu(device->fs_info,
486 "zoned: failed to allocate zone cache for %s",
487 rcu_str_deref(device->name));
495 while (sector < nr_sectors) {
496 nr_zones = BTRFS_REPORT_NR_ZONES;
497 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
502 for (i = 0; i < nr_zones; i++) {
503 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
504 __set_bit(nreported, zone_info->seq_zones);
505 switch (zones[i].cond) {
506 case BLK_ZONE_COND_EMPTY:
507 __set_bit(nreported, zone_info->empty_zones);
509 case BLK_ZONE_COND_IMP_OPEN:
510 case BLK_ZONE_COND_EXP_OPEN:
511 case BLK_ZONE_COND_CLOSED:
512 __set_bit(nreported, zone_info->active_zones);
518 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
521 if (nreported != zone_info->nr_zones) {
522 btrfs_err_in_rcu(device->fs_info,
523 "inconsistent number of zones on %s (%u/%u)",
524 rcu_str_deref(device->name), nreported,
525 zone_info->nr_zones);
530 if (max_active_zones) {
531 if (nactive > max_active_zones) {
532 btrfs_err_in_rcu(device->fs_info,
533 "zoned: %u active zones on %s exceeds max_active_zones %u",
534 nactive, rcu_str_deref(device->name),
539 atomic_set(&zone_info->active_zones_left,
540 max_active_zones - nactive);
541 /* Overcommit does not work well with active zone tacking. */
542 set_bit(BTRFS_FS_NO_OVERCOMMIT, &fs_info->flags);
545 /* Validate superblock log */
546 nr_zones = BTRFS_NR_SB_LOG_ZONES;
547 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
550 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
552 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
553 if (sb_zone + 1 >= zone_info->nr_zones)
556 ret = btrfs_get_dev_zones(device,
557 zone_start_physical(sb_zone, zone_info),
558 &zone_info->sb_zones[sb_pos],
563 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
564 btrfs_err_in_rcu(device->fs_info,
565 "zoned: failed to read super block log zone info at devid %llu zone %u",
566 device->devid, sb_zone);
572 * If zones[0] is conventional, always use the beginning of the
573 * zone to record superblock. No need to validate in that case.
575 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
576 BLK_ZONE_TYPE_CONVENTIONAL)
579 ret = sb_write_pointer(device->bdev,
580 &zone_info->sb_zones[sb_pos], &sb_wp);
581 if (ret != -ENOENT && ret) {
582 btrfs_err_in_rcu(device->fs_info,
583 "zoned: super block log zone corrupted devid %llu zone %u",
584 device->devid, sb_zone);
593 switch (bdev_zoned_model(bdev)) {
595 model = "host-managed zoned";
599 model = "host-aware zoned";
604 emulated = "emulated ";
608 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
609 bdev_zoned_model(bdev),
610 rcu_str_deref(device->name));
612 goto out_free_zone_info;
615 btrfs_info_in_rcu(fs_info,
616 "%s block device %s, %u %szones of %llu bytes",
617 model, rcu_str_deref(device->name), zone_info->nr_zones,
618 emulated, zone_info->zone_size);
625 btrfs_destroy_dev_zone_info(device);
630 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
632 struct btrfs_zoned_device_info *zone_info = device->zone_info;
637 bitmap_free(zone_info->active_zones);
638 bitmap_free(zone_info->seq_zones);
639 bitmap_free(zone_info->empty_zones);
640 vfree(zone_info->zone_cache);
642 device->zone_info = NULL;
645 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
647 struct btrfs_zoned_device_info *zone_info;
649 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
653 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
654 if (!zone_info->seq_zones)
657 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
658 zone_info->nr_zones);
660 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
661 if (!zone_info->empty_zones)
664 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
665 zone_info->nr_zones);
667 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
668 if (!zone_info->active_zones)
671 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
672 zone_info->nr_zones);
673 zone_info->zone_cache = NULL;
678 bitmap_free(zone_info->seq_zones);
679 bitmap_free(zone_info->empty_zones);
680 bitmap_free(zone_info->active_zones);
685 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
686 struct blk_zone *zone)
688 unsigned int nr_zones = 1;
691 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
692 if (ret != 0 || !nr_zones)
693 return ret ? ret : -EIO;
698 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
700 struct btrfs_device *device;
702 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
704 bdev_zoned_model(device->bdev) == BLK_ZONED_HM) {
706 "zoned: mode not enabled but zoned device found: %pg",
715 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
717 struct btrfs_device *device;
719 u64 max_zone_append_size = 0;
723 * Host-Managed devices can't be used without the ZONED flag. With the
724 * ZONED all devices can be used, using zone emulation if required.
726 if (!btrfs_fs_incompat(fs_info, ZONED))
727 return btrfs_check_for_zoned_device(fs_info);
729 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
730 struct btrfs_zoned_device_info *zone_info = device->zone_info;
736 zone_size = zone_info->zone_size;
737 } else if (zone_info->zone_size != zone_size) {
739 "zoned: unequal block device zone sizes: have %llu found %llu",
740 zone_info->zone_size, zone_size);
743 if (!max_zone_append_size ||
744 (zone_info->max_zone_append_size &&
745 zone_info->max_zone_append_size < max_zone_append_size))
746 max_zone_append_size = zone_info->max_zone_append_size;
750 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
751 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
752 * check the alignment here.
754 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
756 "zoned: zone size %llu not aligned to stripe %u",
757 zone_size, BTRFS_STRIPE_LEN);
761 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
762 btrfs_err(fs_info, "zoned: mixed block groups not supported");
766 fs_info->zone_size = zone_size;
767 fs_info->max_zone_append_size = ALIGN_DOWN(max_zone_append_size,
768 fs_info->sectorsize);
769 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
770 if (fs_info->max_zone_append_size < fs_info->max_extent_size)
771 fs_info->max_extent_size = fs_info->max_zone_append_size;
774 * Check mount options here, because we might change fs_info->zoned
775 * from fs_info->zone_size.
777 ret = btrfs_check_mountopts_zoned(fs_info);
781 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
785 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
787 if (!btrfs_is_zoned(info))
791 * Space cache writing is not COWed. Disable that to avoid write errors
792 * in sequential zones.
794 if (btrfs_test_opt(info, SPACE_CACHE)) {
795 btrfs_err(info, "zoned: space cache v1 is not supported");
799 if (btrfs_test_opt(info, NODATACOW)) {
800 btrfs_err(info, "zoned: NODATACOW not supported");
807 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
808 int rw, u64 *bytenr_ret)
813 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
814 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
818 ret = sb_write_pointer(bdev, zones, &wp);
819 if (ret != -ENOENT && ret < 0)
823 struct blk_zone *reset = NULL;
825 if (wp == zones[0].start << SECTOR_SHIFT)
827 else if (wp == zones[1].start << SECTOR_SHIFT)
830 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
831 ASSERT(sb_zone_is_full(reset));
833 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
834 reset->start, reset->len,
839 reset->cond = BLK_ZONE_COND_EMPTY;
840 reset->wp = reset->start;
842 } else if (ret != -ENOENT) {
844 * For READ, we want the previous one. Move write pointer to
845 * the end of a zone, if it is at the head of a zone.
849 if (wp == zones[0].start << SECTOR_SHIFT)
850 zone_end = zones[1].start + zones[1].capacity;
851 else if (wp == zones[1].start << SECTOR_SHIFT)
852 zone_end = zones[0].start + zones[0].capacity;
854 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
855 BTRFS_SUPER_INFO_SIZE);
857 wp -= BTRFS_SUPER_INFO_SIZE;
865 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
868 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
869 sector_t zone_sectors;
872 u8 zone_sectors_shift;
876 if (!bdev_is_zoned(bdev)) {
877 *bytenr_ret = btrfs_sb_offset(mirror);
881 ASSERT(rw == READ || rw == WRITE);
883 zone_sectors = bdev_zone_sectors(bdev);
884 if (!is_power_of_2(zone_sectors))
886 zone_sectors_shift = ilog2(zone_sectors);
887 nr_sectors = bdev_nr_sectors(bdev);
888 nr_zones = nr_sectors >> zone_sectors_shift;
890 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
891 if (sb_zone + 1 >= nr_zones)
894 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
895 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
899 if (ret != BTRFS_NR_SB_LOG_ZONES)
902 return sb_log_location(bdev, zones, rw, bytenr_ret);
905 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
908 struct btrfs_zoned_device_info *zinfo = device->zone_info;
912 * For a zoned filesystem on a non-zoned block device, use the same
913 * super block locations as regular filesystem. Doing so, the super
914 * block can always be retrieved and the zoned flag of the volume
915 * detected from the super block information.
917 if (!bdev_is_zoned(device->bdev)) {
918 *bytenr_ret = btrfs_sb_offset(mirror);
922 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
923 if (zone_num + 1 >= zinfo->nr_zones)
926 return sb_log_location(device->bdev,
927 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
931 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
939 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
940 if (zone_num + 1 >= zinfo->nr_zones)
943 if (!test_bit(zone_num, zinfo->seq_zones))
949 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
951 struct btrfs_zoned_device_info *zinfo = device->zone_info;
952 struct blk_zone *zone;
955 if (!is_sb_log_zone(zinfo, mirror))
958 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
959 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
960 /* Advance the next zone */
961 if (zone->cond == BLK_ZONE_COND_FULL) {
966 if (zone->cond == BLK_ZONE_COND_EMPTY)
967 zone->cond = BLK_ZONE_COND_IMP_OPEN;
969 zone->wp += SUPER_INFO_SECTORS;
971 if (sb_zone_is_full(zone)) {
973 * No room left to write new superblock. Since
974 * superblock is written with REQ_SYNC, it is safe to
975 * finish the zone now.
977 * If the write pointer is exactly at the capacity,
978 * explicit ZONE_FINISH is not necessary.
980 if (zone->wp != zone->start + zone->capacity) {
983 ret = blkdev_zone_mgmt(device->bdev,
984 REQ_OP_ZONE_FINISH, zone->start,
985 zone->len, GFP_NOFS);
990 zone->wp = zone->start + zone->len;
991 zone->cond = BLK_ZONE_COND_FULL;
996 /* All the zones are FULL. Should not reach here. */
1001 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
1003 sector_t zone_sectors;
1004 sector_t nr_sectors;
1005 u8 zone_sectors_shift;
1009 zone_sectors = bdev_zone_sectors(bdev);
1010 zone_sectors_shift = ilog2(zone_sectors);
1011 nr_sectors = bdev_nr_sectors(bdev);
1012 nr_zones = nr_sectors >> zone_sectors_shift;
1014 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1015 if (sb_zone + 1 >= nr_zones)
1018 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1019 zone_start_sector(sb_zone, bdev),
1020 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1024 * btrfs_find_allocatable_zones - find allocatable zones within a given region
1026 * @device: the device to allocate a region on
1027 * @hole_start: the position of the hole to allocate the region
1028 * @num_bytes: size of wanted region
1029 * @hole_end: the end of the hole
1030 * @return: position of allocatable zones
1032 * Allocatable region should not contain any superblock locations.
1034 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1035 u64 hole_end, u64 num_bytes)
1037 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1038 const u8 shift = zinfo->zone_size_shift;
1039 u64 nzones = num_bytes >> shift;
1040 u64 pos = hole_start;
1045 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1046 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1048 while (pos < hole_end) {
1049 begin = pos >> shift;
1050 end = begin + nzones;
1052 if (end > zinfo->nr_zones)
1055 /* Check if zones in the region are all empty */
1056 if (btrfs_dev_is_sequential(device, pos) &&
1057 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1058 pos += zinfo->zone_size;
1063 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1067 sb_zone = sb_zone_number(shift, i);
1068 if (!(end <= sb_zone ||
1069 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1071 pos = zone_start_physical(
1072 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1076 /* We also need to exclude regular superblock positions */
1077 sb_pos = btrfs_sb_offset(i);
1078 if (!(pos + num_bytes <= sb_pos ||
1079 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1081 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1093 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1095 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1096 unsigned int zno = (pos >> zone_info->zone_size_shift);
1098 /* We can use any number of zones */
1099 if (zone_info->max_active_zones == 0)
1102 if (!test_bit(zno, zone_info->active_zones)) {
1103 /* Active zone left? */
1104 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1106 if (test_and_set_bit(zno, zone_info->active_zones)) {
1107 /* Someone already set the bit */
1108 atomic_inc(&zone_info->active_zones_left);
1115 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1117 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1118 unsigned int zno = (pos >> zone_info->zone_size_shift);
1120 /* We can use any number of zones */
1121 if (zone_info->max_active_zones == 0)
1124 if (test_and_clear_bit(zno, zone_info->active_zones))
1125 atomic_inc(&zone_info->active_zones_left);
1128 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1129 u64 length, u64 *bytes)
1134 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1135 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1142 btrfs_dev_set_zone_empty(device, physical);
1143 btrfs_dev_clear_active_zone(device, physical);
1144 physical += device->zone_info->zone_size;
1145 length -= device->zone_info->zone_size;
1151 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1153 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1154 const u8 shift = zinfo->zone_size_shift;
1155 unsigned long begin = start >> shift;
1156 unsigned long end = (start + size) >> shift;
1160 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1161 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1163 if (end > zinfo->nr_zones)
1166 /* All the zones are conventional */
1167 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1170 /* All the zones are sequential and empty */
1171 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1172 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1175 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1178 if (!btrfs_dev_is_sequential(device, pos) ||
1179 btrfs_dev_is_empty_zone(device, pos))
1182 /* Free regions should be empty */
1185 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1186 rcu_str_deref(device->name), device->devid, pos >> shift);
1189 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1199 * Calculate an allocation pointer from the extent allocation information
1200 * for a block group consist of conventional zones. It is pointed to the
1201 * end of the highest addressed extent in the block group as an allocation
1204 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1205 u64 *offset_ret, bool new)
1207 struct btrfs_fs_info *fs_info = cache->fs_info;
1208 struct btrfs_root *root;
1209 struct btrfs_path *path;
1210 struct btrfs_key key;
1211 struct btrfs_key found_key;
1216 * Avoid tree lookups for a new block group, there's no use for it.
1217 * It must always be 0.
1219 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1220 * For new a block group, this function is called from
1221 * btrfs_make_block_group() which is already taking the chunk mutex.
1222 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1223 * buffer locks to avoid deadlock.
1230 path = btrfs_alloc_path();
1234 key.objectid = cache->start + cache->length;
1238 root = btrfs_extent_root(fs_info, key.objectid);
1239 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1240 /* We should not find the exact match */
1246 ret = btrfs_previous_extent_item(root, path, cache->start);
1255 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1257 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1258 length = found_key.offset;
1260 length = fs_info->nodesize;
1262 if (!(found_key.objectid >= cache->start &&
1263 found_key.objectid + length <= cache->start + cache->length)) {
1267 *offset_ret = found_key.objectid + length - cache->start;
1271 btrfs_free_path(path);
1275 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1277 struct btrfs_fs_info *fs_info = cache->fs_info;
1278 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1279 struct extent_map *em;
1280 struct map_lookup *map;
1281 struct btrfs_device *device;
1282 u64 logical = cache->start;
1283 u64 length = cache->length;
1286 unsigned int nofs_flag;
1287 u64 *alloc_offsets = NULL;
1289 u64 *physical = NULL;
1290 unsigned long *active = NULL;
1292 u32 num_sequential = 0, num_conventional = 0;
1294 if (!btrfs_is_zoned(fs_info))
1298 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1300 "zoned: block group %llu len %llu unaligned to zone size %llu",
1301 logical, length, fs_info->zone_size);
1305 /* Get the chunk mapping */
1306 read_lock(&em_tree->lock);
1307 em = lookup_extent_mapping(em_tree, logical, length);
1308 read_unlock(&em_tree->lock);
1313 map = em->map_lookup;
1315 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1316 if (!cache->physical_map) {
1321 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1322 if (!alloc_offsets) {
1327 caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1333 physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1339 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1345 for (i = 0; i < map->num_stripes; i++) {
1347 struct blk_zone zone;
1348 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1349 int dev_replace_is_ongoing = 0;
1351 device = map->stripes[i].dev;
1352 physical[i] = map->stripes[i].physical;
1354 if (device->bdev == NULL) {
1355 alloc_offsets[i] = WP_MISSING_DEV;
1359 is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1366 * Consider a zone as active if we can allow any number of
1369 if (!device->zone_info->max_active_zones)
1370 __set_bit(i, active);
1372 if (!is_sequential) {
1373 alloc_offsets[i] = WP_CONVENTIONAL;
1378 * This zone will be used for allocation, so mark this zone
1381 btrfs_dev_clear_zone_empty(device, physical[i]);
1383 down_read(&dev_replace->rwsem);
1384 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1385 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1386 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1387 up_read(&dev_replace->rwsem);
1390 * The group is mapped to a sequential zone. Get the zone write
1391 * pointer to determine the allocation offset within the zone.
1393 WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1394 nofs_flag = memalloc_nofs_save();
1395 ret = btrfs_get_dev_zone(device, physical[i], &zone);
1396 memalloc_nofs_restore(nofs_flag);
1397 if (ret == -EIO || ret == -EOPNOTSUPP) {
1399 alloc_offsets[i] = WP_MISSING_DEV;
1405 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1406 btrfs_err_in_rcu(fs_info,
1407 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1408 zone.start << SECTOR_SHIFT,
1409 rcu_str_deref(device->name), device->devid);
1414 caps[i] = (zone.capacity << SECTOR_SHIFT);
1416 switch (zone.cond) {
1417 case BLK_ZONE_COND_OFFLINE:
1418 case BLK_ZONE_COND_READONLY:
1420 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1421 physical[i] >> device->zone_info->zone_size_shift,
1422 rcu_str_deref(device->name), device->devid);
1423 alloc_offsets[i] = WP_MISSING_DEV;
1425 case BLK_ZONE_COND_EMPTY:
1426 alloc_offsets[i] = 0;
1428 case BLK_ZONE_COND_FULL:
1429 alloc_offsets[i] = caps[i];
1432 /* Partially used zone */
1434 ((zone.wp - zone.start) << SECTOR_SHIFT);
1435 __set_bit(i, active);
1440 if (num_sequential > 0)
1441 cache->seq_zone = true;
1443 if (num_conventional > 0) {
1444 /* Zone capacity is always zone size in emulation */
1445 cache->zone_capacity = cache->length;
1446 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1449 "zoned: failed to determine allocation offset of bg %llu",
1452 } else if (map->num_stripes == num_conventional) {
1453 cache->alloc_offset = last_alloc;
1454 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1459 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1460 case 0: /* single */
1461 if (alloc_offsets[0] == WP_MISSING_DEV) {
1463 "zoned: cannot recover write pointer for zone %llu",
1468 cache->alloc_offset = alloc_offsets[0];
1469 cache->zone_capacity = caps[0];
1470 if (test_bit(0, active))
1471 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1473 case BTRFS_BLOCK_GROUP_DUP:
1474 if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1475 btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1479 if (alloc_offsets[0] == WP_MISSING_DEV) {
1481 "zoned: cannot recover write pointer for zone %llu",
1486 if (alloc_offsets[1] == WP_MISSING_DEV) {
1488 "zoned: cannot recover write pointer for zone %llu",
1493 if (alloc_offsets[0] != alloc_offsets[1]) {
1495 "zoned: write pointer offset mismatch of zones in DUP profile");
1499 if (test_bit(0, active) != test_bit(1, active)) {
1500 if (!btrfs_zone_activate(cache)) {
1505 if (test_bit(0, active))
1506 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1507 &cache->runtime_flags);
1509 cache->alloc_offset = alloc_offsets[0];
1510 cache->zone_capacity = min(caps[0], caps[1]);
1512 case BTRFS_BLOCK_GROUP_RAID1:
1513 case BTRFS_BLOCK_GROUP_RAID0:
1514 case BTRFS_BLOCK_GROUP_RAID10:
1515 case BTRFS_BLOCK_GROUP_RAID5:
1516 case BTRFS_BLOCK_GROUP_RAID6:
1517 /* non-single profiles are not supported yet */
1519 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1520 btrfs_bg_type_to_raid_name(map->type));
1526 if (cache->alloc_offset > fs_info->zone_size) {
1528 "zoned: invalid write pointer %llu in block group %llu",
1529 cache->alloc_offset, cache->start);
1533 if (cache->alloc_offset > cache->zone_capacity) {
1535 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1536 cache->alloc_offset, cache->zone_capacity,
1541 /* An extent is allocated after the write pointer */
1542 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1544 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1545 logical, last_alloc, cache->alloc_offset);
1550 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1551 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1552 btrfs_get_block_group(cache);
1553 spin_lock(&fs_info->zone_active_bgs_lock);
1554 list_add_tail(&cache->active_bg_list,
1555 &fs_info->zone_active_bgs);
1556 spin_unlock(&fs_info->zone_active_bgs_lock);
1559 kfree(cache->physical_map);
1560 cache->physical_map = NULL;
1562 bitmap_free(active);
1565 kfree(alloc_offsets);
1566 free_extent_map(em);
1571 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1575 if (!btrfs_is_zoned(cache->fs_info))
1578 WARN_ON(cache->bytes_super != 0);
1579 unusable = (cache->alloc_offset - cache->used) +
1580 (cache->length - cache->zone_capacity);
1581 free = cache->zone_capacity - cache->alloc_offset;
1583 /* We only need ->free_space in ALLOC_SEQ block groups */
1584 cache->cached = BTRFS_CACHE_FINISHED;
1585 cache->free_space_ctl->free_space = free;
1586 cache->zone_unusable = unusable;
1589 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1590 struct extent_buffer *eb)
1592 struct btrfs_fs_info *fs_info = eb->fs_info;
1594 if (!btrfs_is_zoned(fs_info) ||
1595 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1596 !list_empty(&eb->release_list))
1599 set_extent_buffer_dirty(eb);
1600 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1601 eb->start + eb->len - 1, EXTENT_DIRTY);
1602 memzero_extent_buffer(eb, 0, eb->len);
1603 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1605 spin_lock(&trans->releasing_ebs_lock);
1606 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1607 spin_unlock(&trans->releasing_ebs_lock);
1608 atomic_inc(&eb->refs);
1611 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1613 spin_lock(&trans->releasing_ebs_lock);
1614 while (!list_empty(&trans->releasing_ebs)) {
1615 struct extent_buffer *eb;
1617 eb = list_first_entry(&trans->releasing_ebs,
1618 struct extent_buffer, release_list);
1619 list_del_init(&eb->release_list);
1620 free_extent_buffer(eb);
1622 spin_unlock(&trans->releasing_ebs_lock);
1625 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1627 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1628 struct btrfs_block_group *cache;
1631 if (!btrfs_is_zoned(fs_info))
1634 if (!is_data_inode(&inode->vfs_inode))
1638 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1639 * extent layout the relocation code has.
1640 * Furthermore we have set aside own block-group from which only the
1641 * relocation "process" can allocate and make sure only one process at a
1642 * time can add pages to an extent that gets relocated, so it's safe to
1643 * use regular REQ_OP_WRITE for this special case.
1645 if (btrfs_is_data_reloc_root(inode->root))
1648 cache = btrfs_lookup_block_group(fs_info, start);
1653 ret = cache->seq_zone;
1654 btrfs_put_block_group(cache);
1659 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1662 struct btrfs_ordered_extent *ordered;
1663 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1665 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1668 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1669 if (WARN_ON(!ordered))
1672 ordered->physical = physical;
1673 ordered->bdev = bio->bi_bdev;
1675 btrfs_put_ordered_extent(ordered);
1678 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1680 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1681 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1682 struct extent_map_tree *em_tree;
1683 struct extent_map *em;
1684 struct btrfs_ordered_sum *sum;
1685 u64 orig_logical = ordered->disk_bytenr;
1686 u64 *logical = NULL;
1689 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1690 ASSERT(!bdev_is_partition(ordered->bdev));
1691 if (WARN_ON(!ordered->bdev))
1694 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1695 ordered->physical, &logical, &nr,
1701 if (orig_logical == *logical)
1704 ordered->disk_bytenr = *logical;
1706 em_tree = &inode->extent_tree;
1707 write_lock(&em_tree->lock);
1708 em = search_extent_mapping(em_tree, ordered->file_offset,
1709 ordered->num_bytes);
1710 em->block_start = *logical;
1711 free_extent_map(em);
1712 write_unlock(&em_tree->lock);
1714 list_for_each_entry(sum, &ordered->list, list) {
1715 if (*logical < orig_logical)
1716 sum->bytenr -= orig_logical - *logical;
1718 sum->bytenr += *logical - orig_logical;
1725 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1726 struct extent_buffer *eb,
1727 struct btrfs_block_group **cache_ret)
1729 struct btrfs_block_group *cache;
1732 if (!btrfs_is_zoned(fs_info))
1735 cache = btrfs_lookup_block_group(fs_info, eb->start);
1739 if (cache->meta_write_pointer != eb->start) {
1740 btrfs_put_block_group(cache);
1744 cache->meta_write_pointer = eb->start + eb->len;
1752 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1753 struct extent_buffer *eb)
1755 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1758 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1759 cache->meta_write_pointer = eb->start;
1762 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1764 if (!btrfs_dev_is_sequential(device, physical))
1767 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1768 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1771 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1772 struct blk_zone *zone)
1774 struct btrfs_io_context *bioc = NULL;
1775 u64 mapped_length = PAGE_SIZE;
1776 unsigned int nofs_flag;
1780 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1781 &mapped_length, &bioc);
1782 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1787 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1792 nofs_flag = memalloc_nofs_save();
1793 nmirrors = (int)bioc->num_stripes;
1794 for (i = 0; i < nmirrors; i++) {
1795 u64 physical = bioc->stripes[i].physical;
1796 struct btrfs_device *dev = bioc->stripes[i].dev;
1798 /* Missing device */
1802 ret = btrfs_get_dev_zone(dev, physical, zone);
1803 /* Failing device */
1804 if (ret == -EIO || ret == -EOPNOTSUPP)
1808 memalloc_nofs_restore(nofs_flag);
1810 btrfs_put_bioc(bioc);
1815 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1816 * filling zeros between @physical_pos to a write pointer of dev-replace
1819 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1820 u64 physical_start, u64 physical_pos)
1822 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1823 struct blk_zone zone;
1828 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1831 ret = read_zone_info(fs_info, logical, &zone);
1835 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1837 if (physical_pos == wp)
1840 if (physical_pos > wp)
1843 length = wp - physical_pos;
1844 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1847 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1848 u64 logical, u64 length)
1850 struct btrfs_device *device;
1851 struct extent_map *em;
1852 struct map_lookup *map;
1854 em = btrfs_get_chunk_map(fs_info, logical, length);
1856 return ERR_CAST(em);
1858 map = em->map_lookup;
1859 /* We only support single profile for now */
1860 device = map->stripes[0].dev;
1862 free_extent_map(em);
1868 * Activate block group and underlying device zones
1870 * @block_group: the block group to activate
1872 * Return: true on success, false otherwise
1874 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1876 struct btrfs_fs_info *fs_info = block_group->fs_info;
1877 struct btrfs_space_info *space_info = block_group->space_info;
1878 struct map_lookup *map;
1879 struct btrfs_device *device;
1884 if (!btrfs_is_zoned(block_group->fs_info))
1887 map = block_group->physical_map;
1889 spin_lock(&space_info->lock);
1890 spin_lock(&block_group->lock);
1891 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1897 if (btrfs_zoned_bg_is_full(block_group)) {
1902 for (i = 0; i < map->num_stripes; i++) {
1903 device = map->stripes[i].dev;
1904 physical = map->stripes[i].physical;
1906 if (device->zone_info->max_active_zones == 0)
1909 if (!btrfs_dev_set_active_zone(device, physical)) {
1910 /* Cannot activate the zone */
1916 /* Successfully activated all the zones */
1917 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
1918 space_info->active_total_bytes += block_group->length;
1919 spin_unlock(&block_group->lock);
1920 btrfs_try_granting_tickets(fs_info, space_info);
1921 spin_unlock(&space_info->lock);
1923 /* For the active block group list */
1924 btrfs_get_block_group(block_group);
1926 spin_lock(&fs_info->zone_active_bgs_lock);
1927 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1928 spin_unlock(&fs_info->zone_active_bgs_lock);
1933 spin_unlock(&block_group->lock);
1934 spin_unlock(&space_info->lock);
1938 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
1940 struct btrfs_fs_info *fs_info = block_group->fs_info;
1941 const u64 end = block_group->start + block_group->length;
1942 struct radix_tree_iter iter;
1943 struct extent_buffer *eb;
1947 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
1948 block_group->start >> fs_info->sectorsize_bits) {
1949 eb = radix_tree_deref_slot(slot);
1952 if (radix_tree_deref_retry(eb)) {
1953 slot = radix_tree_iter_retry(&iter);
1957 if (eb->start < block_group->start)
1959 if (eb->start >= end)
1962 slot = radix_tree_iter_resume(slot, &iter);
1964 wait_on_extent_buffer_writeback(eb);
1970 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
1972 struct btrfs_fs_info *fs_info = block_group->fs_info;
1973 struct map_lookup *map;
1974 const bool is_metadata = (block_group->flags &
1975 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
1979 spin_lock(&block_group->lock);
1980 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1981 spin_unlock(&block_group->lock);
1985 /* Check if we have unwritten allocated space */
1987 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
1988 spin_unlock(&block_group->lock);
1993 * If we are sure that the block group is full (= no more room left for
1994 * new allocation) and the IO for the last usable block is completed, we
1995 * don't need to wait for the other IOs. This holds because we ensure
1996 * the sequential IO submissions using the ZONE_APPEND command for data
1997 * and block_group->meta_write_pointer for metadata.
1999 if (!fully_written) {
2000 spin_unlock(&block_group->lock);
2002 ret = btrfs_inc_block_group_ro(block_group, false);
2006 /* Ensure all writes in this block group finish */
2007 btrfs_wait_block_group_reservations(block_group);
2008 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
2009 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
2010 block_group->length);
2011 /* Wait for extent buffers to be written. */
2013 wait_eb_writebacks(block_group);
2015 spin_lock(&block_group->lock);
2018 * Bail out if someone already deactivated the block group, or
2019 * allocated space is left in the block group.
2021 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2022 &block_group->runtime_flags)) {
2023 spin_unlock(&block_group->lock);
2024 btrfs_dec_block_group_ro(block_group);
2028 if (block_group->reserved) {
2029 spin_unlock(&block_group->lock);
2030 btrfs_dec_block_group_ro(block_group);
2035 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2036 block_group->alloc_offset = block_group->zone_capacity;
2037 block_group->free_space_ctl->free_space = 0;
2038 btrfs_clear_treelog_bg(block_group);
2039 btrfs_clear_data_reloc_bg(block_group);
2040 spin_unlock(&block_group->lock);
2042 map = block_group->physical_map;
2043 for (i = 0; i < map->num_stripes; i++) {
2044 struct btrfs_device *device = map->stripes[i].dev;
2045 const u64 physical = map->stripes[i].physical;
2047 if (device->zone_info->max_active_zones == 0)
2050 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2051 physical >> SECTOR_SHIFT,
2052 device->zone_info->zone_size >> SECTOR_SHIFT,
2058 btrfs_dev_clear_active_zone(device, physical);
2062 btrfs_dec_block_group_ro(block_group);
2064 spin_lock(&fs_info->zone_active_bgs_lock);
2065 ASSERT(!list_empty(&block_group->active_bg_list));
2066 list_del_init(&block_group->active_bg_list);
2067 spin_unlock(&fs_info->zone_active_bgs_lock);
2069 /* For active_bg_list */
2070 btrfs_put_block_group(block_group);
2072 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2077 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2079 if (!btrfs_is_zoned(block_group->fs_info))
2082 return do_zone_finish(block_group, false);
2085 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2087 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2088 struct btrfs_device *device;
2091 if (!btrfs_is_zoned(fs_info))
2094 /* Check if there is a device with active zones left */
2095 mutex_lock(&fs_info->chunk_mutex);
2096 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2097 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2102 if (!zinfo->max_active_zones) {
2107 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
2108 case 0: /* single */
2109 ret = (atomic_read(&zinfo->active_zones_left) >= 1);
2111 case BTRFS_BLOCK_GROUP_DUP:
2112 ret = (atomic_read(&zinfo->active_zones_left) >= 2);
2118 mutex_unlock(&fs_info->chunk_mutex);
2121 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2126 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2128 struct btrfs_block_group *block_group;
2129 u64 min_alloc_bytes;
2131 if (!btrfs_is_zoned(fs_info))
2134 block_group = btrfs_lookup_block_group(fs_info, logical);
2135 ASSERT(block_group);
2137 /* No MIXED_BG on zoned btrfs. */
2138 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2139 min_alloc_bytes = fs_info->sectorsize;
2141 min_alloc_bytes = fs_info->nodesize;
2143 /* Bail out if we can allocate more data from this block group. */
2144 if (logical + length + min_alloc_bytes <=
2145 block_group->start + block_group->zone_capacity)
2148 do_zone_finish(block_group, true);
2151 btrfs_put_block_group(block_group);
2154 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2156 struct btrfs_block_group *bg =
2157 container_of(work, struct btrfs_block_group, zone_finish_work);
2159 wait_on_extent_buffer_writeback(bg->last_eb);
2160 free_extent_buffer(bg->last_eb);
2161 btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2162 btrfs_put_block_group(bg);
2165 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2166 struct extent_buffer *eb)
2168 if (!bg->seq_zone || eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2171 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2172 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2178 btrfs_get_block_group(bg);
2179 atomic_inc(&eb->refs);
2181 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2182 queue_work(system_unbound_wq, &bg->zone_finish_work);
2185 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2187 struct btrfs_fs_info *fs_info = bg->fs_info;
2189 spin_lock(&fs_info->relocation_bg_lock);
2190 if (fs_info->data_reloc_bg == bg->start)
2191 fs_info->data_reloc_bg = 0;
2192 spin_unlock(&fs_info->relocation_bg_lock);
2195 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2197 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2198 struct btrfs_device *device;
2200 if (!btrfs_is_zoned(fs_info))
2203 mutex_lock(&fs_devices->device_list_mutex);
2204 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2205 if (device->zone_info) {
2206 vfree(device->zone_info->zone_cache);
2207 device->zone_info->zone_cache = NULL;
2210 mutex_unlock(&fs_devices->device_list_mutex);
2213 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2215 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2216 struct btrfs_device *device;
2221 ASSERT(btrfs_is_zoned(fs_info));
2223 if (fs_info->bg_reclaim_threshold == 0)
2226 mutex_lock(&fs_devices->device_list_mutex);
2227 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2231 total += device->disk_total_bytes;
2232 used += device->bytes_used;
2234 mutex_unlock(&fs_devices->device_list_mutex);
2236 factor = div64_u64(used * 100, total);
2237 return factor >= fs_info->bg_reclaim_threshold;
2240 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2243 struct btrfs_block_group *block_group;
2245 if (!btrfs_is_zoned(fs_info))
2248 block_group = btrfs_lookup_block_group(fs_info, logical);
2249 /* It should be called on a previous data relocation block group. */
2250 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2252 spin_lock(&block_group->lock);
2253 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2256 /* All relocation extents are written. */
2257 if (block_group->start + block_group->alloc_offset == logical + length) {
2258 /* Now, release this block group for further allocations. */
2259 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2260 &block_group->runtime_flags);
2264 spin_unlock(&block_group->lock);
2265 btrfs_put_block_group(block_group);
2268 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2270 struct btrfs_block_group *block_group;
2271 struct btrfs_block_group *min_bg = NULL;
2272 u64 min_avail = U64_MAX;
2275 spin_lock(&fs_info->zone_active_bgs_lock);
2276 list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2280 spin_lock(&block_group->lock);
2281 if (block_group->reserved ||
2282 (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)) {
2283 spin_unlock(&block_group->lock);
2287 avail = block_group->zone_capacity - block_group->alloc_offset;
2288 if (min_avail > avail) {
2290 btrfs_put_block_group(min_bg);
2291 min_bg = block_group;
2293 btrfs_get_block_group(min_bg);
2295 spin_unlock(&block_group->lock);
2297 spin_unlock(&fs_info->zone_active_bgs_lock);
2302 ret = btrfs_zone_finish(min_bg);
2303 btrfs_put_block_group(min_bg);
2305 return ret < 0 ? ret : 1;
2308 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2309 struct btrfs_space_info *space_info,
2312 struct btrfs_block_group *bg;
2315 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2318 /* No more block groups to activate */
2319 if (space_info->active_total_bytes == space_info->total_bytes)
2324 bool need_finish = false;
2326 down_read(&space_info->groups_sem);
2327 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2328 list_for_each_entry(bg, &space_info->block_groups[index],
2330 if (!spin_trylock(&bg->lock))
2332 if (btrfs_zoned_bg_is_full(bg) ||
2333 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2334 &bg->runtime_flags)) {
2335 spin_unlock(&bg->lock);
2338 spin_unlock(&bg->lock);
2340 if (btrfs_zone_activate(bg)) {
2341 up_read(&space_info->groups_sem);
2348 up_read(&space_info->groups_sem);
2350 if (!do_finish || !need_finish)
2353 ret = btrfs_zone_finish_one_bg(fs_info);