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 #include "accessors.h"
22 /* Maximum number of zones to report per blkdev_report_zones() call */
23 #define BTRFS_REPORT_NR_ZONES 4096
24 /* Invalid allocation pointer value for missing devices */
25 #define WP_MISSING_DEV ((u64)-1)
26 /* Pseudo write pointer value for conventional zone */
27 #define WP_CONVENTIONAL ((u64)-2)
30 * Location of the first zone of superblock logging zone pairs.
32 * - primary superblock: 0B (zone 0)
33 * - first copy: 512G (zone starting at that offset)
34 * - second copy: 4T (zone starting at that offset)
36 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
37 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
38 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
40 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
41 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
43 /* Number of superblock log zones */
44 #define BTRFS_NR_SB_LOG_ZONES 2
47 * Minimum of active zones we need:
49 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
50 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
51 * - 1 zone for tree-log dedicated block group
52 * - 1 zone for relocation
54 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
57 * Minimum / maximum supported zone size. Currently, SMR disks have a zone
58 * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
59 * We do not expect the zone size to become larger than 8GiB or smaller than
60 * 4MiB in the near future.
62 #define BTRFS_MAX_ZONE_SIZE SZ_8G
63 #define BTRFS_MIN_ZONE_SIZE SZ_4M
65 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
67 static inline bool sb_zone_is_full(const struct blk_zone *zone)
69 return (zone->cond == BLK_ZONE_COND_FULL) ||
70 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
73 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
75 struct blk_zone *zones = data;
77 memcpy(&zones[idx], zone, sizeof(*zone));
82 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
85 bool empty[BTRFS_NR_SB_LOG_ZONES];
86 bool full[BTRFS_NR_SB_LOG_ZONES];
90 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
91 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
92 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
93 full[i] = sb_zone_is_full(&zones[i]);
97 * Possible states of log buffer zones
99 * Empty[0] In use[0] Full[0]
105 * *: Special case, no superblock is written
106 * 0: Use write pointer of zones[0]
107 * 1: Use write pointer of zones[1]
108 * C: Compare super blocks from zones[0] and zones[1], use the latest
109 * one determined by generation
113 if (empty[0] && empty[1]) {
114 /* Special case to distinguish no superblock to read */
115 *wp_ret = zones[0].start << SECTOR_SHIFT;
117 } else if (full[0] && full[1]) {
118 /* Compare two super blocks */
119 struct address_space *mapping = bdev->bd_inode->i_mapping;
120 struct page *page[BTRFS_NR_SB_LOG_ZONES];
121 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
124 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
127 bytenr = ((zones[i].start + zones[i].len)
128 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
130 page[i] = read_cache_page_gfp(mapping,
131 bytenr >> PAGE_SHIFT, GFP_NOFS);
132 if (IS_ERR(page[i])) {
134 btrfs_release_disk_super(super[0]);
135 return PTR_ERR(page[i]);
137 super[i] = page_address(page[i]);
140 if (btrfs_super_generation(super[0]) >
141 btrfs_super_generation(super[1]))
142 sector = zones[1].start;
144 sector = zones[0].start;
146 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
147 btrfs_release_disk_super(super[i]);
148 } else if (!full[0] && (empty[1] || full[1])) {
149 sector = zones[0].wp;
150 } else if (full[0]) {
151 sector = zones[1].wp;
155 *wp_ret = sector << SECTOR_SHIFT;
160 * Get the first zone number of the superblock mirror
162 static inline u32 sb_zone_number(int shift, int mirror)
166 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
168 case 0: zone = 0; break;
169 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
170 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
173 ASSERT(zone <= U32_MAX);
178 static inline sector_t zone_start_sector(u32 zone_number,
179 struct block_device *bdev)
181 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
184 static inline u64 zone_start_physical(u32 zone_number,
185 struct btrfs_zoned_device_info *zone_info)
187 return (u64)zone_number << zone_info->zone_size_shift;
191 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
192 * device into static sized chunks and fake a conventional zone on each of
195 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
196 struct blk_zone *zones, unsigned int nr_zones)
198 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
199 sector_t bdev_size = bdev_nr_sectors(device->bdev);
202 pos >>= SECTOR_SHIFT;
203 for (i = 0; i < nr_zones; i++) {
204 zones[i].start = i * zone_sectors + pos;
205 zones[i].len = zone_sectors;
206 zones[i].capacity = zone_sectors;
207 zones[i].wp = zones[i].start + zone_sectors;
208 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
209 zones[i].cond = BLK_ZONE_COND_NOT_WP;
211 if (zones[i].wp >= bdev_size) {
220 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
221 struct blk_zone *zones, unsigned int *nr_zones)
223 struct btrfs_zoned_device_info *zinfo = device->zone_info;
229 if (!bdev_is_zoned(device->bdev)) {
230 ret = emulate_report_zones(device, pos, zones, *nr_zones);
236 if (zinfo->zone_cache) {
240 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
241 zno = pos >> zinfo->zone_size_shift;
243 * We cannot report zones beyond the zone end. So, it is OK to
244 * cap *nr_zones to at the end.
246 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
248 for (i = 0; i < *nr_zones; i++) {
249 struct blk_zone *zone_info;
251 zone_info = &zinfo->zone_cache[zno + i];
256 if (i == *nr_zones) {
257 /* Cache hit on all the zones */
258 memcpy(zones, zinfo->zone_cache + zno,
259 sizeof(*zinfo->zone_cache) * *nr_zones);
264 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
265 copy_zone_info_cb, zones);
267 btrfs_err_in_rcu(device->fs_info,
268 "zoned: failed to read zone %llu on %s (devid %llu)",
269 pos, rcu_str_deref(device->name),
278 if (zinfo->zone_cache) {
279 u32 zno = pos >> zinfo->zone_size_shift;
281 memcpy(zinfo->zone_cache + zno, zones,
282 sizeof(*zinfo->zone_cache) * *nr_zones);
288 /* The emulated zone size is determined from the size of device extent */
289 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
291 struct btrfs_path *path;
292 struct btrfs_root *root = fs_info->dev_root;
293 struct btrfs_key key;
294 struct extent_buffer *leaf;
295 struct btrfs_dev_extent *dext;
299 key.type = BTRFS_DEV_EXTENT_KEY;
302 path = btrfs_alloc_path();
306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
310 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
311 ret = btrfs_next_leaf(root, path);
314 /* No dev extents at all? Not good */
321 leaf = path->nodes[0];
322 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
323 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
327 btrfs_free_path(path);
332 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
334 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
335 struct btrfs_device *device;
338 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
339 if (!btrfs_fs_incompat(fs_info, ZONED))
342 mutex_lock(&fs_devices->device_list_mutex);
343 list_for_each_entry(device, &fs_devices->devices, dev_list) {
344 /* We can skip reading of zone info for missing devices */
348 ret = btrfs_get_dev_zone_info(device, true);
352 mutex_unlock(&fs_devices->device_list_mutex);
357 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
359 struct btrfs_fs_info *fs_info = device->fs_info;
360 struct btrfs_zoned_device_info *zone_info = NULL;
361 struct block_device *bdev = device->bdev;
362 unsigned int max_active_zones;
363 unsigned int nactive;
366 struct blk_zone *zones = NULL;
367 unsigned int i, nreported = 0, nr_zones;
368 sector_t zone_sectors;
369 char *model, *emulated;
373 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
376 if (!btrfs_fs_incompat(fs_info, ZONED))
379 if (device->zone_info)
382 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
386 device->zone_info = zone_info;
388 if (!bdev_is_zoned(bdev)) {
389 if (!fs_info->zone_size) {
390 ret = calculate_emulated_zone_size(fs_info);
395 ASSERT(fs_info->zone_size);
396 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
398 zone_sectors = bdev_zone_sectors(bdev);
401 ASSERT(is_power_of_two_u64(zone_sectors));
402 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
404 /* We reject devices with a zone size larger than 8GB */
405 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
406 btrfs_err_in_rcu(fs_info,
407 "zoned: %s: zone size %llu larger than supported maximum %llu",
408 rcu_str_deref(device->name),
409 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
412 } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
413 btrfs_err_in_rcu(fs_info,
414 "zoned: %s: zone size %llu smaller than supported minimum %u",
415 rcu_str_deref(device->name),
416 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
421 nr_sectors = bdev_nr_sectors(bdev);
422 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
423 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
424 if (!IS_ALIGNED(nr_sectors, zone_sectors))
425 zone_info->nr_zones++;
427 max_active_zones = bdev_max_active_zones(bdev);
428 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
429 btrfs_err_in_rcu(fs_info,
430 "zoned: %s: max active zones %u is too small, need at least %u active zones",
431 rcu_str_deref(device->name), max_active_zones,
432 BTRFS_MIN_ACTIVE_ZONES);
436 zone_info->max_active_zones = max_active_zones;
438 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
439 if (!zone_info->seq_zones) {
444 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
445 if (!zone_info->empty_zones) {
450 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
451 if (!zone_info->active_zones) {
456 zones = kvcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
463 * Enable zone cache only for a zoned device. On a non-zoned device, we
464 * fill the zone info with emulated CONVENTIONAL zones, so no need to
467 if (populate_cache && bdev_is_zoned(device->bdev)) {
468 zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
469 zone_info->nr_zones);
470 if (!zone_info->zone_cache) {
471 btrfs_err_in_rcu(device->fs_info,
472 "zoned: failed to allocate zone cache for %s",
473 rcu_str_deref(device->name));
481 while (sector < nr_sectors) {
482 nr_zones = BTRFS_REPORT_NR_ZONES;
483 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
488 for (i = 0; i < nr_zones; i++) {
489 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
490 __set_bit(nreported, zone_info->seq_zones);
491 switch (zones[i].cond) {
492 case BLK_ZONE_COND_EMPTY:
493 __set_bit(nreported, zone_info->empty_zones);
495 case BLK_ZONE_COND_IMP_OPEN:
496 case BLK_ZONE_COND_EXP_OPEN:
497 case BLK_ZONE_COND_CLOSED:
498 __set_bit(nreported, zone_info->active_zones);
504 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
507 if (nreported != zone_info->nr_zones) {
508 btrfs_err_in_rcu(device->fs_info,
509 "inconsistent number of zones on %s (%u/%u)",
510 rcu_str_deref(device->name), nreported,
511 zone_info->nr_zones);
516 if (max_active_zones) {
517 if (nactive > max_active_zones) {
518 btrfs_err_in_rcu(device->fs_info,
519 "zoned: %u active zones on %s exceeds max_active_zones %u",
520 nactive, rcu_str_deref(device->name),
525 atomic_set(&zone_info->active_zones_left,
526 max_active_zones - nactive);
527 /* Overcommit does not work well with active zone tacking. */
528 set_bit(BTRFS_FS_NO_OVERCOMMIT, &fs_info->flags);
531 /* Validate superblock log */
532 nr_zones = BTRFS_NR_SB_LOG_ZONES;
533 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
536 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
538 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
539 if (sb_zone + 1 >= zone_info->nr_zones)
542 ret = btrfs_get_dev_zones(device,
543 zone_start_physical(sb_zone, zone_info),
544 &zone_info->sb_zones[sb_pos],
549 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
550 btrfs_err_in_rcu(device->fs_info,
551 "zoned: failed to read super block log zone info at devid %llu zone %u",
552 device->devid, sb_zone);
558 * If zones[0] is conventional, always use the beginning of the
559 * zone to record superblock. No need to validate in that case.
561 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
562 BLK_ZONE_TYPE_CONVENTIONAL)
565 ret = sb_write_pointer(device->bdev,
566 &zone_info->sb_zones[sb_pos], &sb_wp);
567 if (ret != -ENOENT && ret) {
568 btrfs_err_in_rcu(device->fs_info,
569 "zoned: super block log zone corrupted devid %llu zone %u",
570 device->devid, sb_zone);
579 switch (bdev_zoned_model(bdev)) {
581 model = "host-managed zoned";
585 model = "host-aware zoned";
590 emulated = "emulated ";
594 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
595 bdev_zoned_model(bdev),
596 rcu_str_deref(device->name));
598 goto out_free_zone_info;
601 btrfs_info_in_rcu(fs_info,
602 "%s block device %s, %u %szones of %llu bytes",
603 model, rcu_str_deref(device->name), zone_info->nr_zones,
604 emulated, zone_info->zone_size);
611 btrfs_destroy_dev_zone_info(device);
616 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
618 struct btrfs_zoned_device_info *zone_info = device->zone_info;
623 bitmap_free(zone_info->active_zones);
624 bitmap_free(zone_info->seq_zones);
625 bitmap_free(zone_info->empty_zones);
626 vfree(zone_info->zone_cache);
628 device->zone_info = NULL;
631 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
633 struct btrfs_zoned_device_info *zone_info;
635 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
639 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
640 if (!zone_info->seq_zones)
643 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
644 zone_info->nr_zones);
646 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
647 if (!zone_info->empty_zones)
650 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
651 zone_info->nr_zones);
653 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
654 if (!zone_info->active_zones)
657 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
658 zone_info->nr_zones);
659 zone_info->zone_cache = NULL;
664 bitmap_free(zone_info->seq_zones);
665 bitmap_free(zone_info->empty_zones);
666 bitmap_free(zone_info->active_zones);
671 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
672 struct blk_zone *zone)
674 unsigned int nr_zones = 1;
677 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
678 if (ret != 0 || !nr_zones)
679 return ret ? ret : -EIO;
684 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
686 struct btrfs_device *device;
688 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
690 bdev_zoned_model(device->bdev) == BLK_ZONED_HM) {
692 "zoned: mode not enabled but zoned device found: %pg",
701 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
703 struct queue_limits *lim = &fs_info->limits;
704 struct btrfs_device *device;
709 * Host-Managed devices can't be used without the ZONED flag. With the
710 * ZONED all devices can be used, using zone emulation if required.
712 if (!btrfs_fs_incompat(fs_info, ZONED))
713 return btrfs_check_for_zoned_device(fs_info);
715 blk_set_stacking_limits(lim);
717 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
718 struct btrfs_zoned_device_info *zone_info = device->zone_info;
724 zone_size = zone_info->zone_size;
725 } else if (zone_info->zone_size != zone_size) {
727 "zoned: unequal block device zone sizes: have %llu found %llu",
728 zone_info->zone_size, zone_size);
733 * With the zoned emulation, we can have non-zoned device on the
734 * zoned mode. In this case, we don't have a valid max zone
737 if (bdev_is_zoned(device->bdev)) {
738 blk_stack_limits(lim,
739 &bdev_get_queue(device->bdev)->limits,
745 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
746 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
747 * check the alignment here.
749 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
751 "zoned: zone size %llu not aligned to stripe %u",
752 zone_size, BTRFS_STRIPE_LEN);
756 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
757 btrfs_err(fs_info, "zoned: mixed block groups not supported");
761 fs_info->zone_size = zone_size;
763 * Also limit max_zone_append_size by max_segments * PAGE_SIZE.
764 * Technically, we can have multiple pages per segment. But, since
765 * we add the pages one by one to a bio, and cannot increase the
766 * metadata reservation even if it increases the number of extents, it
767 * is safe to stick with the limit.
769 fs_info->max_zone_append_size = ALIGN_DOWN(
770 min3((u64)lim->max_zone_append_sectors << SECTOR_SHIFT,
771 (u64)lim->max_sectors << SECTOR_SHIFT,
772 (u64)lim->max_segments << PAGE_SHIFT),
773 fs_info->sectorsize);
774 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
775 if (fs_info->max_zone_append_size < fs_info->max_extent_size)
776 fs_info->max_extent_size = fs_info->max_zone_append_size;
779 * Check mount options here, because we might change fs_info->zoned
780 * from fs_info->zone_size.
782 ret = btrfs_check_mountopts_zoned(fs_info);
786 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
790 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
792 if (!btrfs_is_zoned(info))
796 * Space cache writing is not COWed. Disable that to avoid write errors
797 * in sequential zones.
799 if (btrfs_test_opt(info, SPACE_CACHE)) {
800 btrfs_err(info, "zoned: space cache v1 is not supported");
804 if (btrfs_test_opt(info, NODATACOW)) {
805 btrfs_err(info, "zoned: NODATACOW not supported");
812 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
813 int rw, u64 *bytenr_ret)
818 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
819 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
823 ret = sb_write_pointer(bdev, zones, &wp);
824 if (ret != -ENOENT && ret < 0)
828 struct blk_zone *reset = NULL;
830 if (wp == zones[0].start << SECTOR_SHIFT)
832 else if (wp == zones[1].start << SECTOR_SHIFT)
835 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
836 ASSERT(sb_zone_is_full(reset));
838 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
839 reset->start, reset->len,
844 reset->cond = BLK_ZONE_COND_EMPTY;
845 reset->wp = reset->start;
847 } else if (ret != -ENOENT) {
849 * For READ, we want the previous one. Move write pointer to
850 * the end of a zone, if it is at the head of a zone.
854 if (wp == zones[0].start << SECTOR_SHIFT)
855 zone_end = zones[1].start + zones[1].capacity;
856 else if (wp == zones[1].start << SECTOR_SHIFT)
857 zone_end = zones[0].start + zones[0].capacity;
859 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
860 BTRFS_SUPER_INFO_SIZE);
862 wp -= BTRFS_SUPER_INFO_SIZE;
870 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
873 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
874 sector_t zone_sectors;
877 u8 zone_sectors_shift;
881 if (!bdev_is_zoned(bdev)) {
882 *bytenr_ret = btrfs_sb_offset(mirror);
886 ASSERT(rw == READ || rw == WRITE);
888 zone_sectors = bdev_zone_sectors(bdev);
889 if (!is_power_of_2(zone_sectors))
891 zone_sectors_shift = ilog2(zone_sectors);
892 nr_sectors = bdev_nr_sectors(bdev);
893 nr_zones = nr_sectors >> zone_sectors_shift;
895 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
896 if (sb_zone + 1 >= nr_zones)
899 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
900 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
904 if (ret != BTRFS_NR_SB_LOG_ZONES)
907 return sb_log_location(bdev, zones, rw, bytenr_ret);
910 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
913 struct btrfs_zoned_device_info *zinfo = device->zone_info;
917 * For a zoned filesystem on a non-zoned block device, use the same
918 * super block locations as regular filesystem. Doing so, the super
919 * block can always be retrieved and the zoned flag of the volume
920 * detected from the super block information.
922 if (!bdev_is_zoned(device->bdev)) {
923 *bytenr_ret = btrfs_sb_offset(mirror);
927 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
928 if (zone_num + 1 >= zinfo->nr_zones)
931 return sb_log_location(device->bdev,
932 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
936 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
944 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
945 if (zone_num + 1 >= zinfo->nr_zones)
948 if (!test_bit(zone_num, zinfo->seq_zones))
954 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
956 struct btrfs_zoned_device_info *zinfo = device->zone_info;
957 struct blk_zone *zone;
960 if (!is_sb_log_zone(zinfo, mirror))
963 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
964 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
965 /* Advance the next zone */
966 if (zone->cond == BLK_ZONE_COND_FULL) {
971 if (zone->cond == BLK_ZONE_COND_EMPTY)
972 zone->cond = BLK_ZONE_COND_IMP_OPEN;
974 zone->wp += SUPER_INFO_SECTORS;
976 if (sb_zone_is_full(zone)) {
978 * No room left to write new superblock. Since
979 * superblock is written with REQ_SYNC, it is safe to
980 * finish the zone now.
982 * If the write pointer is exactly at the capacity,
983 * explicit ZONE_FINISH is not necessary.
985 if (zone->wp != zone->start + zone->capacity) {
988 ret = blkdev_zone_mgmt(device->bdev,
989 REQ_OP_ZONE_FINISH, zone->start,
990 zone->len, GFP_NOFS);
995 zone->wp = zone->start + zone->len;
996 zone->cond = BLK_ZONE_COND_FULL;
1001 /* All the zones are FULL. Should not reach here. */
1006 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
1008 sector_t zone_sectors;
1009 sector_t nr_sectors;
1010 u8 zone_sectors_shift;
1014 zone_sectors = bdev_zone_sectors(bdev);
1015 zone_sectors_shift = ilog2(zone_sectors);
1016 nr_sectors = bdev_nr_sectors(bdev);
1017 nr_zones = nr_sectors >> zone_sectors_shift;
1019 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1020 if (sb_zone + 1 >= nr_zones)
1023 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1024 zone_start_sector(sb_zone, bdev),
1025 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1029 * Find allocatable zones within a given region.
1031 * @device: the device to allocate a region on
1032 * @hole_start: the position of the hole to allocate the region
1033 * @num_bytes: size of wanted region
1034 * @hole_end: the end of the hole
1035 * @return: position of allocatable zones
1037 * Allocatable region should not contain any superblock locations.
1039 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1040 u64 hole_end, u64 num_bytes)
1042 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1043 const u8 shift = zinfo->zone_size_shift;
1044 u64 nzones = num_bytes >> shift;
1045 u64 pos = hole_start;
1050 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1051 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1053 while (pos < hole_end) {
1054 begin = pos >> shift;
1055 end = begin + nzones;
1057 if (end > zinfo->nr_zones)
1060 /* Check if zones in the region are all empty */
1061 if (btrfs_dev_is_sequential(device, pos) &&
1062 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1063 pos += zinfo->zone_size;
1068 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1072 sb_zone = sb_zone_number(shift, i);
1073 if (!(end <= sb_zone ||
1074 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1076 pos = zone_start_physical(
1077 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1081 /* We also need to exclude regular superblock positions */
1082 sb_pos = btrfs_sb_offset(i);
1083 if (!(pos + num_bytes <= sb_pos ||
1084 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1086 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1098 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1100 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1101 unsigned int zno = (pos >> zone_info->zone_size_shift);
1103 /* We can use any number of zones */
1104 if (zone_info->max_active_zones == 0)
1107 if (!test_bit(zno, zone_info->active_zones)) {
1108 /* Active zone left? */
1109 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1111 if (test_and_set_bit(zno, zone_info->active_zones)) {
1112 /* Someone already set the bit */
1113 atomic_inc(&zone_info->active_zones_left);
1120 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1122 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1123 unsigned int zno = (pos >> zone_info->zone_size_shift);
1125 /* We can use any number of zones */
1126 if (zone_info->max_active_zones == 0)
1129 if (test_and_clear_bit(zno, zone_info->active_zones))
1130 atomic_inc(&zone_info->active_zones_left);
1133 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1134 u64 length, u64 *bytes)
1139 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1140 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1147 btrfs_dev_set_zone_empty(device, physical);
1148 btrfs_dev_clear_active_zone(device, physical);
1149 physical += device->zone_info->zone_size;
1150 length -= device->zone_info->zone_size;
1156 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1158 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1159 const u8 shift = zinfo->zone_size_shift;
1160 unsigned long begin = start >> shift;
1161 unsigned long end = (start + size) >> shift;
1165 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1166 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1168 if (end > zinfo->nr_zones)
1171 /* All the zones are conventional */
1172 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1175 /* All the zones are sequential and empty */
1176 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1177 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1180 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1183 if (!btrfs_dev_is_sequential(device, pos) ||
1184 btrfs_dev_is_empty_zone(device, pos))
1187 /* Free regions should be empty */
1190 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1191 rcu_str_deref(device->name), device->devid, pos >> shift);
1194 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1204 * Calculate an allocation pointer from the extent allocation information
1205 * for a block group consist of conventional zones. It is pointed to the
1206 * end of the highest addressed extent in the block group as an allocation
1209 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1210 u64 *offset_ret, bool new)
1212 struct btrfs_fs_info *fs_info = cache->fs_info;
1213 struct btrfs_root *root;
1214 struct btrfs_path *path;
1215 struct btrfs_key key;
1216 struct btrfs_key found_key;
1221 * Avoid tree lookups for a new block group, there's no use for it.
1222 * It must always be 0.
1224 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1225 * For new a block group, this function is called from
1226 * btrfs_make_block_group() which is already taking the chunk mutex.
1227 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1228 * buffer locks to avoid deadlock.
1235 path = btrfs_alloc_path();
1239 key.objectid = cache->start + cache->length;
1243 root = btrfs_extent_root(fs_info, key.objectid);
1244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1245 /* We should not find the exact match */
1251 ret = btrfs_previous_extent_item(root, path, cache->start);
1260 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1262 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1263 length = found_key.offset;
1265 length = fs_info->nodesize;
1267 if (!(found_key.objectid >= cache->start &&
1268 found_key.objectid + length <= cache->start + cache->length)) {
1272 *offset_ret = found_key.objectid + length - cache->start;
1276 btrfs_free_path(path);
1280 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1282 struct btrfs_fs_info *fs_info = cache->fs_info;
1283 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1284 struct extent_map *em;
1285 struct map_lookup *map;
1286 struct btrfs_device *device;
1287 u64 logical = cache->start;
1288 u64 length = cache->length;
1291 unsigned int nofs_flag;
1292 u64 *alloc_offsets = NULL;
1294 u64 *physical = NULL;
1295 unsigned long *active = NULL;
1297 u32 num_sequential = 0, num_conventional = 0;
1299 if (!btrfs_is_zoned(fs_info))
1303 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1305 "zoned: block group %llu len %llu unaligned to zone size %llu",
1306 logical, length, fs_info->zone_size);
1310 /* Get the chunk mapping */
1311 read_lock(&em_tree->lock);
1312 em = lookup_extent_mapping(em_tree, logical, length);
1313 read_unlock(&em_tree->lock);
1318 map = em->map_lookup;
1320 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1321 if (!cache->physical_map) {
1326 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1327 if (!alloc_offsets) {
1332 caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1338 physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1344 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1350 for (i = 0; i < map->num_stripes; i++) {
1352 struct blk_zone zone;
1353 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1354 int dev_replace_is_ongoing = 0;
1356 device = map->stripes[i].dev;
1357 physical[i] = map->stripes[i].physical;
1359 if (device->bdev == NULL) {
1360 alloc_offsets[i] = WP_MISSING_DEV;
1364 is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1371 * Consider a zone as active if we can allow any number of
1374 if (!device->zone_info->max_active_zones)
1375 __set_bit(i, active);
1377 if (!is_sequential) {
1378 alloc_offsets[i] = WP_CONVENTIONAL;
1383 * This zone will be used for allocation, so mark this zone
1386 btrfs_dev_clear_zone_empty(device, physical[i]);
1388 down_read(&dev_replace->rwsem);
1389 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1390 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1391 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1392 up_read(&dev_replace->rwsem);
1395 * The group is mapped to a sequential zone. Get the zone write
1396 * pointer to determine the allocation offset within the zone.
1398 WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1399 nofs_flag = memalloc_nofs_save();
1400 ret = btrfs_get_dev_zone(device, physical[i], &zone);
1401 memalloc_nofs_restore(nofs_flag);
1402 if (ret == -EIO || ret == -EOPNOTSUPP) {
1404 alloc_offsets[i] = WP_MISSING_DEV;
1410 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1411 btrfs_err_in_rcu(fs_info,
1412 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1413 zone.start << SECTOR_SHIFT,
1414 rcu_str_deref(device->name), device->devid);
1419 caps[i] = (zone.capacity << SECTOR_SHIFT);
1421 switch (zone.cond) {
1422 case BLK_ZONE_COND_OFFLINE:
1423 case BLK_ZONE_COND_READONLY:
1425 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1426 physical[i] >> device->zone_info->zone_size_shift,
1427 rcu_str_deref(device->name), device->devid);
1428 alloc_offsets[i] = WP_MISSING_DEV;
1430 case BLK_ZONE_COND_EMPTY:
1431 alloc_offsets[i] = 0;
1433 case BLK_ZONE_COND_FULL:
1434 alloc_offsets[i] = caps[i];
1437 /* Partially used zone */
1439 ((zone.wp - zone.start) << SECTOR_SHIFT);
1440 __set_bit(i, active);
1445 if (num_sequential > 0)
1446 set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1448 if (num_conventional > 0) {
1449 /* Zone capacity is always zone size in emulation */
1450 cache->zone_capacity = cache->length;
1451 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1454 "zoned: failed to determine allocation offset of bg %llu",
1457 } else if (map->num_stripes == num_conventional) {
1458 cache->alloc_offset = last_alloc;
1459 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1464 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1465 case 0: /* single */
1466 if (alloc_offsets[0] == WP_MISSING_DEV) {
1468 "zoned: cannot recover write pointer for zone %llu",
1473 cache->alloc_offset = alloc_offsets[0];
1474 cache->zone_capacity = caps[0];
1475 if (test_bit(0, active))
1476 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1478 case BTRFS_BLOCK_GROUP_DUP:
1479 if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1480 btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1484 if (alloc_offsets[0] == WP_MISSING_DEV) {
1486 "zoned: cannot recover write pointer for zone %llu",
1491 if (alloc_offsets[1] == WP_MISSING_DEV) {
1493 "zoned: cannot recover write pointer for zone %llu",
1498 if (alloc_offsets[0] != alloc_offsets[1]) {
1500 "zoned: write pointer offset mismatch of zones in DUP profile");
1504 if (test_bit(0, active) != test_bit(1, active)) {
1505 if (!btrfs_zone_activate(cache)) {
1510 if (test_bit(0, active))
1511 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1512 &cache->runtime_flags);
1514 cache->alloc_offset = alloc_offsets[0];
1515 cache->zone_capacity = min(caps[0], caps[1]);
1517 case BTRFS_BLOCK_GROUP_RAID1:
1518 case BTRFS_BLOCK_GROUP_RAID0:
1519 case BTRFS_BLOCK_GROUP_RAID10:
1520 case BTRFS_BLOCK_GROUP_RAID5:
1521 case BTRFS_BLOCK_GROUP_RAID6:
1522 /* non-single profiles are not supported yet */
1524 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1525 btrfs_bg_type_to_raid_name(map->type));
1531 if (cache->alloc_offset > fs_info->zone_size) {
1533 "zoned: invalid write pointer %llu in block group %llu",
1534 cache->alloc_offset, cache->start);
1538 if (cache->alloc_offset > cache->zone_capacity) {
1540 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1541 cache->alloc_offset, cache->zone_capacity,
1546 /* An extent is allocated after the write pointer */
1547 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1549 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1550 logical, last_alloc, cache->alloc_offset);
1555 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1556 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1557 btrfs_get_block_group(cache);
1558 spin_lock(&fs_info->zone_active_bgs_lock);
1559 list_add_tail(&cache->active_bg_list,
1560 &fs_info->zone_active_bgs);
1561 spin_unlock(&fs_info->zone_active_bgs_lock);
1564 kfree(cache->physical_map);
1565 cache->physical_map = NULL;
1567 bitmap_free(active);
1570 kfree(alloc_offsets);
1571 free_extent_map(em);
1576 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1580 if (!btrfs_is_zoned(cache->fs_info))
1583 WARN_ON(cache->bytes_super != 0);
1584 unusable = (cache->alloc_offset - cache->used) +
1585 (cache->length - cache->zone_capacity);
1586 free = cache->zone_capacity - cache->alloc_offset;
1588 /* We only need ->free_space in ALLOC_SEQ block groups */
1589 cache->cached = BTRFS_CACHE_FINISHED;
1590 cache->free_space_ctl->free_space = free;
1591 cache->zone_unusable = unusable;
1594 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1595 struct extent_buffer *eb)
1597 struct btrfs_fs_info *fs_info = eb->fs_info;
1599 if (!btrfs_is_zoned(fs_info) ||
1600 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1601 !list_empty(&eb->release_list))
1604 set_extent_buffer_dirty(eb);
1605 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1606 eb->start + eb->len - 1, EXTENT_DIRTY);
1607 memzero_extent_buffer(eb, 0, eb->len);
1608 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1610 spin_lock(&trans->releasing_ebs_lock);
1611 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1612 spin_unlock(&trans->releasing_ebs_lock);
1613 atomic_inc(&eb->refs);
1616 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1618 spin_lock(&trans->releasing_ebs_lock);
1619 while (!list_empty(&trans->releasing_ebs)) {
1620 struct extent_buffer *eb;
1622 eb = list_first_entry(&trans->releasing_ebs,
1623 struct extent_buffer, release_list);
1624 list_del_init(&eb->release_list);
1625 free_extent_buffer(eb);
1627 spin_unlock(&trans->releasing_ebs_lock);
1630 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
1632 u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
1633 struct btrfs_inode *inode = bbio->inode;
1634 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1635 struct btrfs_block_group *cache;
1638 if (!btrfs_is_zoned(fs_info))
1641 if (!is_data_inode(&inode->vfs_inode))
1644 if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE)
1648 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1649 * extent layout the relocation code has.
1650 * Furthermore we have set aside own block-group from which only the
1651 * relocation "process" can allocate and make sure only one process at a
1652 * time can add pages to an extent that gets relocated, so it's safe to
1653 * use regular REQ_OP_WRITE for this special case.
1655 if (btrfs_is_data_reloc_root(inode->root))
1658 cache = btrfs_lookup_block_group(fs_info, start);
1663 ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1664 btrfs_put_block_group(cache);
1669 void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
1671 const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
1672 struct btrfs_ordered_extent *ordered;
1674 ordered = btrfs_lookup_ordered_extent(bbio->inode, bbio->file_offset);
1675 if (WARN_ON(!ordered))
1678 ordered->physical = physical;
1679 btrfs_put_ordered_extent(ordered);
1682 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1684 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1685 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1686 struct extent_map_tree *em_tree;
1687 struct extent_map *em;
1688 struct btrfs_ordered_sum *sum;
1689 u64 orig_logical = ordered->disk_bytenr;
1690 struct map_lookup *map;
1691 u64 physical = ordered->physical;
1692 u64 chunk_start_phys;
1695 em = btrfs_get_chunk_map(fs_info, orig_logical, 1);
1698 map = em->map_lookup;
1699 chunk_start_phys = map->stripes[0].physical;
1701 if (WARN_ON_ONCE(map->num_stripes > 1) ||
1702 WARN_ON_ONCE((map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) ||
1703 WARN_ON_ONCE(physical < chunk_start_phys) ||
1704 WARN_ON_ONCE(physical > chunk_start_phys + em->orig_block_len)) {
1705 free_extent_map(em);
1708 logical = em->start + (physical - map->stripes[0].physical);
1709 free_extent_map(em);
1711 if (orig_logical == logical)
1714 ordered->disk_bytenr = logical;
1716 em_tree = &inode->extent_tree;
1717 write_lock(&em_tree->lock);
1718 em = search_extent_mapping(em_tree, ordered->file_offset,
1719 ordered->num_bytes);
1720 em->block_start = logical;
1721 free_extent_map(em);
1722 write_unlock(&em_tree->lock);
1724 list_for_each_entry(sum, &ordered->list, list) {
1725 if (logical < orig_logical)
1726 sum->bytenr -= orig_logical - logical;
1728 sum->bytenr += logical - orig_logical;
1732 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1733 struct extent_buffer *eb,
1734 struct btrfs_block_group **cache_ret)
1736 struct btrfs_block_group *cache;
1739 if (!btrfs_is_zoned(fs_info))
1742 cache = btrfs_lookup_block_group(fs_info, eb->start);
1746 if (cache->meta_write_pointer != eb->start) {
1747 btrfs_put_block_group(cache);
1751 cache->meta_write_pointer = eb->start + eb->len;
1759 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1760 struct extent_buffer *eb)
1762 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1765 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1766 cache->meta_write_pointer = eb->start;
1769 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1771 if (!btrfs_dev_is_sequential(device, physical))
1774 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1775 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1778 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1779 struct blk_zone *zone)
1781 struct btrfs_io_context *bioc = NULL;
1782 u64 mapped_length = PAGE_SIZE;
1783 unsigned int nofs_flag;
1787 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1788 &mapped_length, &bioc);
1789 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1794 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1799 nofs_flag = memalloc_nofs_save();
1800 nmirrors = (int)bioc->num_stripes;
1801 for (i = 0; i < nmirrors; i++) {
1802 u64 physical = bioc->stripes[i].physical;
1803 struct btrfs_device *dev = bioc->stripes[i].dev;
1805 /* Missing device */
1809 ret = btrfs_get_dev_zone(dev, physical, zone);
1810 /* Failing device */
1811 if (ret == -EIO || ret == -EOPNOTSUPP)
1815 memalloc_nofs_restore(nofs_flag);
1817 btrfs_put_bioc(bioc);
1822 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1823 * filling zeros between @physical_pos to a write pointer of dev-replace
1826 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1827 u64 physical_start, u64 physical_pos)
1829 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1830 struct blk_zone zone;
1835 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1838 ret = read_zone_info(fs_info, logical, &zone);
1842 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1844 if (physical_pos == wp)
1847 if (physical_pos > wp)
1850 length = wp - physical_pos;
1851 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1855 * Activate block group and underlying device zones
1857 * @block_group: the block group to activate
1859 * Return: true on success, false otherwise
1861 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1863 struct btrfs_fs_info *fs_info = block_group->fs_info;
1864 struct btrfs_space_info *space_info = block_group->space_info;
1865 struct map_lookup *map;
1866 struct btrfs_device *device;
1871 if (!btrfs_is_zoned(block_group->fs_info))
1874 map = block_group->physical_map;
1876 spin_lock(&space_info->lock);
1877 spin_lock(&block_group->lock);
1878 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1884 if (btrfs_zoned_bg_is_full(block_group)) {
1889 for (i = 0; i < map->num_stripes; i++) {
1890 device = map->stripes[i].dev;
1891 physical = map->stripes[i].physical;
1893 if (device->zone_info->max_active_zones == 0)
1896 if (!btrfs_dev_set_active_zone(device, physical)) {
1897 /* Cannot activate the zone */
1903 /* Successfully activated all the zones */
1904 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
1905 space_info->active_total_bytes += block_group->length;
1906 spin_unlock(&block_group->lock);
1907 btrfs_try_granting_tickets(fs_info, space_info);
1908 spin_unlock(&space_info->lock);
1910 /* For the active block group list */
1911 btrfs_get_block_group(block_group);
1913 spin_lock(&fs_info->zone_active_bgs_lock);
1914 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1915 spin_unlock(&fs_info->zone_active_bgs_lock);
1920 spin_unlock(&block_group->lock);
1921 spin_unlock(&space_info->lock);
1925 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
1927 struct btrfs_fs_info *fs_info = block_group->fs_info;
1928 const u64 end = block_group->start + block_group->length;
1929 struct radix_tree_iter iter;
1930 struct extent_buffer *eb;
1934 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
1935 block_group->start >> fs_info->sectorsize_bits) {
1936 eb = radix_tree_deref_slot(slot);
1939 if (radix_tree_deref_retry(eb)) {
1940 slot = radix_tree_iter_retry(&iter);
1944 if (eb->start < block_group->start)
1946 if (eb->start >= end)
1949 slot = radix_tree_iter_resume(slot, &iter);
1951 wait_on_extent_buffer_writeback(eb);
1957 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
1959 struct btrfs_fs_info *fs_info = block_group->fs_info;
1960 struct map_lookup *map;
1961 const bool is_metadata = (block_group->flags &
1962 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
1966 spin_lock(&block_group->lock);
1967 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1968 spin_unlock(&block_group->lock);
1972 /* Check if we have unwritten allocated space */
1974 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
1975 spin_unlock(&block_group->lock);
1980 * If we are sure that the block group is full (= no more room left for
1981 * new allocation) and the IO for the last usable block is completed, we
1982 * don't need to wait for the other IOs. This holds because we ensure
1983 * the sequential IO submissions using the ZONE_APPEND command for data
1984 * and block_group->meta_write_pointer for metadata.
1986 if (!fully_written) {
1987 spin_unlock(&block_group->lock);
1989 ret = btrfs_inc_block_group_ro(block_group, false);
1993 /* Ensure all writes in this block group finish */
1994 btrfs_wait_block_group_reservations(block_group);
1995 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
1996 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
1997 block_group->length);
1998 /* Wait for extent buffers to be written. */
2000 wait_eb_writebacks(block_group);
2002 spin_lock(&block_group->lock);
2005 * Bail out if someone already deactivated the block group, or
2006 * allocated space is left in the block group.
2008 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2009 &block_group->runtime_flags)) {
2010 spin_unlock(&block_group->lock);
2011 btrfs_dec_block_group_ro(block_group);
2015 if (block_group->reserved) {
2016 spin_unlock(&block_group->lock);
2017 btrfs_dec_block_group_ro(block_group);
2022 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2023 block_group->alloc_offset = block_group->zone_capacity;
2024 block_group->free_space_ctl->free_space = 0;
2025 btrfs_clear_treelog_bg(block_group);
2026 btrfs_clear_data_reloc_bg(block_group);
2027 spin_unlock(&block_group->lock);
2029 map = block_group->physical_map;
2030 for (i = 0; i < map->num_stripes; i++) {
2031 struct btrfs_device *device = map->stripes[i].dev;
2032 const u64 physical = map->stripes[i].physical;
2034 if (device->zone_info->max_active_zones == 0)
2037 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2038 physical >> SECTOR_SHIFT,
2039 device->zone_info->zone_size >> SECTOR_SHIFT,
2045 btrfs_dev_clear_active_zone(device, physical);
2049 btrfs_dec_block_group_ro(block_group);
2051 spin_lock(&fs_info->zone_active_bgs_lock);
2052 ASSERT(!list_empty(&block_group->active_bg_list));
2053 list_del_init(&block_group->active_bg_list);
2054 spin_unlock(&fs_info->zone_active_bgs_lock);
2056 /* For active_bg_list */
2057 btrfs_put_block_group(block_group);
2059 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2064 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2066 if (!btrfs_is_zoned(block_group->fs_info))
2069 return do_zone_finish(block_group, false);
2072 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2074 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2075 struct btrfs_device *device;
2078 if (!btrfs_is_zoned(fs_info))
2081 /* Check if there is a device with active zones left */
2082 mutex_lock(&fs_info->chunk_mutex);
2083 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2084 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2089 if (!zinfo->max_active_zones ||
2090 atomic_read(&zinfo->active_zones_left)) {
2095 mutex_unlock(&fs_info->chunk_mutex);
2098 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2103 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2105 struct btrfs_block_group *block_group;
2106 u64 min_alloc_bytes;
2108 if (!btrfs_is_zoned(fs_info))
2111 block_group = btrfs_lookup_block_group(fs_info, logical);
2112 ASSERT(block_group);
2114 /* No MIXED_BG on zoned btrfs. */
2115 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2116 min_alloc_bytes = fs_info->sectorsize;
2118 min_alloc_bytes = fs_info->nodesize;
2120 /* Bail out if we can allocate more data from this block group. */
2121 if (logical + length + min_alloc_bytes <=
2122 block_group->start + block_group->zone_capacity)
2125 do_zone_finish(block_group, true);
2128 btrfs_put_block_group(block_group);
2131 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2133 struct btrfs_block_group *bg =
2134 container_of(work, struct btrfs_block_group, zone_finish_work);
2136 wait_on_extent_buffer_writeback(bg->last_eb);
2137 free_extent_buffer(bg->last_eb);
2138 btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2139 btrfs_put_block_group(bg);
2142 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2143 struct extent_buffer *eb)
2145 if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
2146 eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2149 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2150 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2156 btrfs_get_block_group(bg);
2157 atomic_inc(&eb->refs);
2159 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2160 queue_work(system_unbound_wq, &bg->zone_finish_work);
2163 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2165 struct btrfs_fs_info *fs_info = bg->fs_info;
2167 spin_lock(&fs_info->relocation_bg_lock);
2168 if (fs_info->data_reloc_bg == bg->start)
2169 fs_info->data_reloc_bg = 0;
2170 spin_unlock(&fs_info->relocation_bg_lock);
2173 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2175 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2176 struct btrfs_device *device;
2178 if (!btrfs_is_zoned(fs_info))
2181 mutex_lock(&fs_devices->device_list_mutex);
2182 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2183 if (device->zone_info) {
2184 vfree(device->zone_info->zone_cache);
2185 device->zone_info->zone_cache = NULL;
2188 mutex_unlock(&fs_devices->device_list_mutex);
2191 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2193 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2194 struct btrfs_device *device;
2199 ASSERT(btrfs_is_zoned(fs_info));
2201 if (fs_info->bg_reclaim_threshold == 0)
2204 mutex_lock(&fs_devices->device_list_mutex);
2205 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2209 total += device->disk_total_bytes;
2210 used += device->bytes_used;
2212 mutex_unlock(&fs_devices->device_list_mutex);
2214 factor = div64_u64(used * 100, total);
2215 return factor >= fs_info->bg_reclaim_threshold;
2218 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2221 struct btrfs_block_group *block_group;
2223 if (!btrfs_is_zoned(fs_info))
2226 block_group = btrfs_lookup_block_group(fs_info, logical);
2227 /* It should be called on a previous data relocation block group. */
2228 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2230 spin_lock(&block_group->lock);
2231 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2234 /* All relocation extents are written. */
2235 if (block_group->start + block_group->alloc_offset == logical + length) {
2236 /* Now, release this block group for further allocations. */
2237 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2238 &block_group->runtime_flags);
2242 spin_unlock(&block_group->lock);
2243 btrfs_put_block_group(block_group);
2246 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2248 struct btrfs_block_group *block_group;
2249 struct btrfs_block_group *min_bg = NULL;
2250 u64 min_avail = U64_MAX;
2253 spin_lock(&fs_info->zone_active_bgs_lock);
2254 list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2258 spin_lock(&block_group->lock);
2259 if (block_group->reserved ||
2260 (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)) {
2261 spin_unlock(&block_group->lock);
2265 avail = block_group->zone_capacity - block_group->alloc_offset;
2266 if (min_avail > avail) {
2268 btrfs_put_block_group(min_bg);
2269 min_bg = block_group;
2271 btrfs_get_block_group(min_bg);
2273 spin_unlock(&block_group->lock);
2275 spin_unlock(&fs_info->zone_active_bgs_lock);
2280 ret = btrfs_zone_finish(min_bg);
2281 btrfs_put_block_group(min_bg);
2283 return ret < 0 ? ret : 1;
2286 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2287 struct btrfs_space_info *space_info,
2290 struct btrfs_block_group *bg;
2293 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2296 /* No more block groups to activate */
2297 if (space_info->active_total_bytes == space_info->total_bytes)
2302 bool need_finish = false;
2304 down_read(&space_info->groups_sem);
2305 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2306 list_for_each_entry(bg, &space_info->block_groups[index],
2308 if (!spin_trylock(&bg->lock))
2310 if (btrfs_zoned_bg_is_full(bg) ||
2311 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2312 &bg->runtime_flags)) {
2313 spin_unlock(&bg->lock);
2316 spin_unlock(&bg->lock);
2318 if (btrfs_zone_activate(bg)) {
2319 up_read(&space_info->groups_sem);
2326 up_read(&space_info->groups_sem);
2328 if (!do_finish || !need_finish)
2331 ret = btrfs_zone_finish_one_bg(fs_info);