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"
21 /* Maximum number of zones to report per blkdev_report_zones() call */
22 #define BTRFS_REPORT_NR_ZONES 4096
23 /* Invalid allocation pointer value for missing devices */
24 #define WP_MISSING_DEV ((u64)-1)
25 /* Pseudo write pointer value for conventional zone */
26 #define WP_CONVENTIONAL ((u64)-2)
29 * Location of the first zone of superblock logging zone pairs.
31 * - primary superblock: 0B (zone 0)
32 * - first copy: 512G (zone starting at that offset)
33 * - second copy: 4T (zone starting at that offset)
35 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
36 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
37 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
39 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
40 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
42 /* Number of superblock log zones */
43 #define BTRFS_NR_SB_LOG_ZONES 2
46 * Minimum of active zones we need:
48 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
49 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
50 * - 1 zone for tree-log dedicated block group
51 * - 1 zone for relocation
53 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
56 * Minimum / maximum supported zone size. Currently, SMR disks have a zone
57 * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
58 * We do not expect the zone size to become larger than 8GiB or smaller than
59 * 4MiB in the near future.
61 #define BTRFS_MAX_ZONE_SIZE SZ_8G
62 #define BTRFS_MIN_ZONE_SIZE SZ_4M
64 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
66 static inline bool sb_zone_is_full(const struct blk_zone *zone)
68 return (zone->cond == BLK_ZONE_COND_FULL) ||
69 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
72 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
74 struct blk_zone *zones = data;
76 memcpy(&zones[idx], zone, sizeof(*zone));
81 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
84 bool empty[BTRFS_NR_SB_LOG_ZONES];
85 bool full[BTRFS_NR_SB_LOG_ZONES];
89 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
90 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
91 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
92 full[i] = sb_zone_is_full(&zones[i]);
96 * Possible states of log buffer zones
98 * Empty[0] In use[0] Full[0]
104 * *: Special case, no superblock is written
105 * 0: Use write pointer of zones[0]
106 * 1: Use write pointer of zones[1]
107 * C: Compare super blocks from zones[0] and zones[1], use the latest
108 * one determined by generation
112 if (empty[0] && empty[1]) {
113 /* Special case to distinguish no superblock to read */
114 *wp_ret = zones[0].start << SECTOR_SHIFT;
116 } else if (full[0] && full[1]) {
117 /* Compare two super blocks */
118 struct address_space *mapping = bdev->bd_inode->i_mapping;
119 struct page *page[BTRFS_NR_SB_LOG_ZONES];
120 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
123 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
126 bytenr = ((zones[i].start + zones[i].len)
127 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
129 page[i] = read_cache_page_gfp(mapping,
130 bytenr >> PAGE_SHIFT, GFP_NOFS);
131 if (IS_ERR(page[i])) {
133 btrfs_release_disk_super(super[0]);
134 return PTR_ERR(page[i]);
136 super[i] = page_address(page[i]);
139 if (btrfs_super_generation(super[0]) >
140 btrfs_super_generation(super[1]))
141 sector = zones[1].start;
143 sector = zones[0].start;
145 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
146 btrfs_release_disk_super(super[i]);
147 } else if (!full[0] && (empty[1] || full[1])) {
148 sector = zones[0].wp;
149 } else if (full[0]) {
150 sector = zones[1].wp;
154 *wp_ret = sector << SECTOR_SHIFT;
159 * Get the first zone number of the superblock mirror
161 static inline u32 sb_zone_number(int shift, int mirror)
165 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
167 case 0: zone = 0; break;
168 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
169 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
172 ASSERT(zone <= U32_MAX);
177 static inline sector_t zone_start_sector(u32 zone_number,
178 struct block_device *bdev)
180 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
183 static inline u64 zone_start_physical(u32 zone_number,
184 struct btrfs_zoned_device_info *zone_info)
186 return (u64)zone_number << zone_info->zone_size_shift;
190 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
191 * device into static sized chunks and fake a conventional zone on each of
194 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
195 struct blk_zone *zones, unsigned int nr_zones)
197 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
198 sector_t bdev_size = bdev_nr_sectors(device->bdev);
201 pos >>= SECTOR_SHIFT;
202 for (i = 0; i < nr_zones; i++) {
203 zones[i].start = i * zone_sectors + pos;
204 zones[i].len = zone_sectors;
205 zones[i].capacity = zone_sectors;
206 zones[i].wp = zones[i].start + zone_sectors;
207 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
208 zones[i].cond = BLK_ZONE_COND_NOT_WP;
210 if (zones[i].wp >= bdev_size) {
219 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
220 struct blk_zone *zones, unsigned int *nr_zones)
222 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) {
239 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
240 zno = pos >> zinfo->zone_size_shift;
242 * We cannot report zones beyond the zone end. So, it is OK to
243 * cap *nr_zones to at the end.
245 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
247 for (i = 0; i < *nr_zones; i++) {
248 struct blk_zone *zone_info;
250 zone_info = &zinfo->zone_cache[zno + i];
255 if (i == *nr_zones) {
256 /* Cache hit on all the zones */
257 memcpy(zones, zinfo->zone_cache + zno,
258 sizeof(*zinfo->zone_cache) * *nr_zones);
263 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
264 copy_zone_info_cb, zones);
266 btrfs_err_in_rcu(device->fs_info,
267 "zoned: failed to read zone %llu on %s (devid %llu)",
268 pos, rcu_str_deref(device->name),
277 if (zinfo->zone_cache)
278 memcpy(zinfo->zone_cache + zno, zones,
279 sizeof(*zinfo->zone_cache) * *nr_zones);
284 /* The emulated zone size is determined from the size of device extent */
285 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
287 struct btrfs_path *path;
288 struct btrfs_root *root = fs_info->dev_root;
289 struct btrfs_key key;
290 struct extent_buffer *leaf;
291 struct btrfs_dev_extent *dext;
295 key.type = BTRFS_DEV_EXTENT_KEY;
298 path = btrfs_alloc_path();
302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
306 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
307 ret = btrfs_next_leaf(root, path);
310 /* No dev extents at all? Not good */
317 leaf = path->nodes[0];
318 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
319 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
323 btrfs_free_path(path);
328 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
330 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
331 struct btrfs_device *device;
334 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
335 if (!btrfs_fs_incompat(fs_info, ZONED))
338 mutex_lock(&fs_devices->device_list_mutex);
339 list_for_each_entry(device, &fs_devices->devices, dev_list) {
340 /* We can skip reading of zone info for missing devices */
344 ret = btrfs_get_dev_zone_info(device, true);
348 mutex_unlock(&fs_devices->device_list_mutex);
353 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
355 struct btrfs_fs_info *fs_info = device->fs_info;
356 struct btrfs_zoned_device_info *zone_info = NULL;
357 struct block_device *bdev = device->bdev;
358 unsigned int max_active_zones;
359 unsigned int nactive;
362 struct blk_zone *zones = NULL;
363 unsigned int i, nreported = 0, nr_zones;
364 sector_t zone_sectors;
365 char *model, *emulated;
369 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
372 if (!btrfs_fs_incompat(fs_info, ZONED))
375 if (device->zone_info)
378 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
382 device->zone_info = zone_info;
384 if (!bdev_is_zoned(bdev)) {
385 if (!fs_info->zone_size) {
386 ret = calculate_emulated_zone_size(fs_info);
391 ASSERT(fs_info->zone_size);
392 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
394 zone_sectors = bdev_zone_sectors(bdev);
397 ASSERT(is_power_of_two_u64(zone_sectors));
398 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
400 /* We reject devices with a zone size larger than 8GB */
401 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
402 btrfs_err_in_rcu(fs_info,
403 "zoned: %s: zone size %llu larger than supported maximum %llu",
404 rcu_str_deref(device->name),
405 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
408 } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
409 btrfs_err_in_rcu(fs_info,
410 "zoned: %s: zone size %llu smaller than supported minimum %u",
411 rcu_str_deref(device->name),
412 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
417 nr_sectors = bdev_nr_sectors(bdev);
418 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
419 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
421 * We limit max_zone_append_size also by max_segments *
422 * PAGE_SIZE. Technically, we can have multiple pages per segment. But,
423 * since btrfs adds the pages one by one to a bio, and btrfs cannot
424 * increase the metadata reservation even if it increases the number of
425 * extents, it is safe to stick with the limit.
427 * With the zoned emulation, we can have non-zoned device on the zoned
428 * mode. In this case, we don't have a valid max zone append size. So,
429 * use max_segments * PAGE_SIZE as the pseudo max_zone_append_size.
431 if (bdev_is_zoned(bdev)) {
432 zone_info->max_zone_append_size = min_t(u64,
433 (u64)bdev_max_zone_append_sectors(bdev) << SECTOR_SHIFT,
434 (u64)bdev_max_segments(bdev) << PAGE_SHIFT);
436 zone_info->max_zone_append_size =
437 (u64)bdev_max_segments(bdev) << PAGE_SHIFT;
439 if (!IS_ALIGNED(nr_sectors, zone_sectors))
440 zone_info->nr_zones++;
442 max_active_zones = bdev_max_active_zones(bdev);
443 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
444 btrfs_err_in_rcu(fs_info,
445 "zoned: %s: max active zones %u is too small, need at least %u active zones",
446 rcu_str_deref(device->name), max_active_zones,
447 BTRFS_MIN_ACTIVE_ZONES);
451 zone_info->max_active_zones = max_active_zones;
453 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
454 if (!zone_info->seq_zones) {
459 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
460 if (!zone_info->empty_zones) {
465 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
466 if (!zone_info->active_zones) {
471 zones = kvcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
478 * Enable zone cache only for a zoned device. On a non-zoned device, we
479 * fill the zone info with emulated CONVENTIONAL zones, so no need to
482 if (populate_cache && bdev_is_zoned(device->bdev)) {
483 zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
484 zone_info->nr_zones);
485 if (!zone_info->zone_cache) {
486 btrfs_err_in_rcu(device->fs_info,
487 "zoned: failed to allocate zone cache for %s",
488 rcu_str_deref(device->name));
496 while (sector < nr_sectors) {
497 nr_zones = BTRFS_REPORT_NR_ZONES;
498 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
503 for (i = 0; i < nr_zones; i++) {
504 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
505 __set_bit(nreported, zone_info->seq_zones);
506 switch (zones[i].cond) {
507 case BLK_ZONE_COND_EMPTY:
508 __set_bit(nreported, zone_info->empty_zones);
510 case BLK_ZONE_COND_IMP_OPEN:
511 case BLK_ZONE_COND_EXP_OPEN:
512 case BLK_ZONE_COND_CLOSED:
513 __set_bit(nreported, zone_info->active_zones);
519 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
522 if (nreported != zone_info->nr_zones) {
523 btrfs_err_in_rcu(device->fs_info,
524 "inconsistent number of zones on %s (%u/%u)",
525 rcu_str_deref(device->name), nreported,
526 zone_info->nr_zones);
531 if (max_active_zones) {
532 if (nactive > max_active_zones) {
533 btrfs_err_in_rcu(device->fs_info,
534 "zoned: %u active zones on %s exceeds max_active_zones %u",
535 nactive, rcu_str_deref(device->name),
540 atomic_set(&zone_info->active_zones_left,
541 max_active_zones - nactive);
544 /* Validate superblock log */
545 nr_zones = BTRFS_NR_SB_LOG_ZONES;
546 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
549 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
551 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
552 if (sb_zone + 1 >= zone_info->nr_zones)
555 ret = btrfs_get_dev_zones(device,
556 zone_start_physical(sb_zone, zone_info),
557 &zone_info->sb_zones[sb_pos],
562 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
563 btrfs_err_in_rcu(device->fs_info,
564 "zoned: failed to read super block log zone info at devid %llu zone %u",
565 device->devid, sb_zone);
571 * If zones[0] is conventional, always use the beginning of the
572 * zone to record superblock. No need to validate in that case.
574 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
575 BLK_ZONE_TYPE_CONVENTIONAL)
578 ret = sb_write_pointer(device->bdev,
579 &zone_info->sb_zones[sb_pos], &sb_wp);
580 if (ret != -ENOENT && ret) {
581 btrfs_err_in_rcu(device->fs_info,
582 "zoned: super block log zone corrupted devid %llu zone %u",
583 device->devid, sb_zone);
592 switch (bdev_zoned_model(bdev)) {
594 model = "host-managed zoned";
598 model = "host-aware zoned";
603 emulated = "emulated ";
607 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
608 bdev_zoned_model(bdev),
609 rcu_str_deref(device->name));
611 goto out_free_zone_info;
614 btrfs_info_in_rcu(fs_info,
615 "%s block device %s, %u %szones of %llu bytes",
616 model, rcu_str_deref(device->name), zone_info->nr_zones,
617 emulated, zone_info->zone_size);
624 btrfs_destroy_dev_zone_info(device);
629 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
631 struct btrfs_zoned_device_info *zone_info = device->zone_info;
636 bitmap_free(zone_info->active_zones);
637 bitmap_free(zone_info->seq_zones);
638 bitmap_free(zone_info->empty_zones);
639 vfree(zone_info->zone_cache);
641 device->zone_info = NULL;
644 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
646 struct btrfs_zoned_device_info *zone_info;
648 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
652 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
653 if (!zone_info->seq_zones)
656 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
657 zone_info->nr_zones);
659 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
660 if (!zone_info->empty_zones)
663 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
664 zone_info->nr_zones);
666 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
667 if (!zone_info->active_zones)
670 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
671 zone_info->nr_zones);
672 zone_info->zone_cache = NULL;
677 bitmap_free(zone_info->seq_zones);
678 bitmap_free(zone_info->empty_zones);
679 bitmap_free(zone_info->active_zones);
684 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
685 struct blk_zone *zone)
687 unsigned int nr_zones = 1;
690 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
691 if (ret != 0 || !nr_zones)
692 return ret ? ret : -EIO;
697 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
699 struct btrfs_device *device;
701 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
703 bdev_zoned_model(device->bdev) == BLK_ZONED_HM) {
705 "zoned: mode not enabled but zoned device found: %pg",
714 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
716 struct btrfs_device *device;
718 u64 max_zone_append_size = 0;
722 * Host-Managed devices can't be used without the ZONED flag. With the
723 * ZONED all devices can be used, using zone emulation if required.
725 if (!btrfs_fs_incompat(fs_info, ZONED))
726 return btrfs_check_for_zoned_device(fs_info);
728 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
729 struct btrfs_zoned_device_info *zone_info = device->zone_info;
735 zone_size = zone_info->zone_size;
736 } else if (zone_info->zone_size != zone_size) {
738 "zoned: unequal block device zone sizes: have %llu found %llu",
739 zone_info->zone_size, zone_size);
742 if (!max_zone_append_size ||
743 (zone_info->max_zone_append_size &&
744 zone_info->max_zone_append_size < max_zone_append_size))
745 max_zone_append_size = zone_info->max_zone_append_size;
749 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
750 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
751 * check the alignment here.
753 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
755 "zoned: zone size %llu not aligned to stripe %u",
756 zone_size, BTRFS_STRIPE_LEN);
760 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
761 btrfs_err(fs_info, "zoned: mixed block groups not supported");
765 fs_info->zone_size = zone_size;
766 fs_info->max_zone_append_size = ALIGN_DOWN(max_zone_append_size,
767 fs_info->sectorsize);
768 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
769 if (fs_info->max_zone_append_size < fs_info->max_extent_size)
770 fs_info->max_extent_size = fs_info->max_zone_append_size;
773 * Check mount options here, because we might change fs_info->zoned
774 * from fs_info->zone_size.
776 ret = btrfs_check_mountopts_zoned(fs_info);
780 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
784 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
786 if (!btrfs_is_zoned(info))
790 * Space cache writing is not COWed. Disable that to avoid write errors
791 * in sequential zones.
793 if (btrfs_test_opt(info, SPACE_CACHE)) {
794 btrfs_err(info, "zoned: space cache v1 is not supported");
798 if (btrfs_test_opt(info, NODATACOW)) {
799 btrfs_err(info, "zoned: NODATACOW not supported");
806 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
807 int rw, u64 *bytenr_ret)
812 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
813 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
817 ret = sb_write_pointer(bdev, zones, &wp);
818 if (ret != -ENOENT && ret < 0)
822 struct blk_zone *reset = NULL;
824 if (wp == zones[0].start << SECTOR_SHIFT)
826 else if (wp == zones[1].start << SECTOR_SHIFT)
829 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
830 ASSERT(sb_zone_is_full(reset));
832 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
833 reset->start, reset->len,
838 reset->cond = BLK_ZONE_COND_EMPTY;
839 reset->wp = reset->start;
841 } else if (ret != -ENOENT) {
843 * For READ, we want the previous one. Move write pointer to
844 * the end of a zone, if it is at the head of a zone.
848 if (wp == zones[0].start << SECTOR_SHIFT)
849 zone_end = zones[1].start + zones[1].capacity;
850 else if (wp == zones[1].start << SECTOR_SHIFT)
851 zone_end = zones[0].start + zones[0].capacity;
853 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
854 BTRFS_SUPER_INFO_SIZE);
856 wp -= BTRFS_SUPER_INFO_SIZE;
864 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
867 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
868 sector_t zone_sectors;
871 u8 zone_sectors_shift;
875 if (!bdev_is_zoned(bdev)) {
876 *bytenr_ret = btrfs_sb_offset(mirror);
880 ASSERT(rw == READ || rw == WRITE);
882 zone_sectors = bdev_zone_sectors(bdev);
883 if (!is_power_of_2(zone_sectors))
885 zone_sectors_shift = ilog2(zone_sectors);
886 nr_sectors = bdev_nr_sectors(bdev);
887 nr_zones = nr_sectors >> zone_sectors_shift;
889 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
890 if (sb_zone + 1 >= nr_zones)
893 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
894 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
898 if (ret != BTRFS_NR_SB_LOG_ZONES)
901 return sb_log_location(bdev, zones, rw, bytenr_ret);
904 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
907 struct btrfs_zoned_device_info *zinfo = device->zone_info;
911 * For a zoned filesystem on a non-zoned block device, use the same
912 * super block locations as regular filesystem. Doing so, the super
913 * block can always be retrieved and the zoned flag of the volume
914 * detected from the super block information.
916 if (!bdev_is_zoned(device->bdev)) {
917 *bytenr_ret = btrfs_sb_offset(mirror);
921 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
922 if (zone_num + 1 >= zinfo->nr_zones)
925 return sb_log_location(device->bdev,
926 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
930 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
938 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
939 if (zone_num + 1 >= zinfo->nr_zones)
942 if (!test_bit(zone_num, zinfo->seq_zones))
948 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
950 struct btrfs_zoned_device_info *zinfo = device->zone_info;
951 struct blk_zone *zone;
954 if (!is_sb_log_zone(zinfo, mirror))
957 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
958 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
959 /* Advance the next zone */
960 if (zone->cond == BLK_ZONE_COND_FULL) {
965 if (zone->cond == BLK_ZONE_COND_EMPTY)
966 zone->cond = BLK_ZONE_COND_IMP_OPEN;
968 zone->wp += SUPER_INFO_SECTORS;
970 if (sb_zone_is_full(zone)) {
972 * No room left to write new superblock. Since
973 * superblock is written with REQ_SYNC, it is safe to
974 * finish the zone now.
976 * If the write pointer is exactly at the capacity,
977 * explicit ZONE_FINISH is not necessary.
979 if (zone->wp != zone->start + zone->capacity) {
982 ret = blkdev_zone_mgmt(device->bdev,
983 REQ_OP_ZONE_FINISH, zone->start,
984 zone->len, GFP_NOFS);
989 zone->wp = zone->start + zone->len;
990 zone->cond = BLK_ZONE_COND_FULL;
995 /* All the zones are FULL. Should not reach here. */
1000 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
1002 sector_t zone_sectors;
1003 sector_t nr_sectors;
1004 u8 zone_sectors_shift;
1008 zone_sectors = bdev_zone_sectors(bdev);
1009 zone_sectors_shift = ilog2(zone_sectors);
1010 nr_sectors = bdev_nr_sectors(bdev);
1011 nr_zones = nr_sectors >> zone_sectors_shift;
1013 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1014 if (sb_zone + 1 >= nr_zones)
1017 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1018 zone_start_sector(sb_zone, bdev),
1019 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1023 * Find allocatable zones within a given region.
1025 * @device: the device to allocate a region on
1026 * @hole_start: the position of the hole to allocate the region
1027 * @num_bytes: size of wanted region
1028 * @hole_end: the end of the hole
1029 * @return: position of allocatable zones
1031 * Allocatable region should not contain any superblock locations.
1033 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1034 u64 hole_end, u64 num_bytes)
1036 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1037 const u8 shift = zinfo->zone_size_shift;
1038 u64 nzones = num_bytes >> shift;
1039 u64 pos = hole_start;
1044 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1045 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1047 while (pos < hole_end) {
1048 begin = pos >> shift;
1049 end = begin + nzones;
1051 if (end > zinfo->nr_zones)
1054 /* Check if zones in the region are all empty */
1055 if (btrfs_dev_is_sequential(device, pos) &&
1056 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1057 pos += zinfo->zone_size;
1062 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1066 sb_zone = sb_zone_number(shift, i);
1067 if (!(end <= sb_zone ||
1068 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1070 pos = zone_start_physical(
1071 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1075 /* We also need to exclude regular superblock positions */
1076 sb_pos = btrfs_sb_offset(i);
1077 if (!(pos + num_bytes <= sb_pos ||
1078 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1080 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1092 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1094 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1095 unsigned int zno = (pos >> zone_info->zone_size_shift);
1097 /* We can use any number of zones */
1098 if (zone_info->max_active_zones == 0)
1101 if (!test_bit(zno, zone_info->active_zones)) {
1102 /* Active zone left? */
1103 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1105 if (test_and_set_bit(zno, zone_info->active_zones)) {
1106 /* Someone already set the bit */
1107 atomic_inc(&zone_info->active_zones_left);
1114 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1116 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1117 unsigned int zno = (pos >> zone_info->zone_size_shift);
1119 /* We can use any number of zones */
1120 if (zone_info->max_active_zones == 0)
1123 if (test_and_clear_bit(zno, zone_info->active_zones))
1124 atomic_inc(&zone_info->active_zones_left);
1127 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1128 u64 length, u64 *bytes)
1133 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1134 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1141 btrfs_dev_set_zone_empty(device, physical);
1142 btrfs_dev_clear_active_zone(device, physical);
1143 physical += device->zone_info->zone_size;
1144 length -= device->zone_info->zone_size;
1150 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1152 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1153 const u8 shift = zinfo->zone_size_shift;
1154 unsigned long begin = start >> shift;
1155 unsigned long end = (start + size) >> shift;
1159 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1160 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1162 if (end > zinfo->nr_zones)
1165 /* All the zones are conventional */
1166 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1169 /* All the zones are sequential and empty */
1170 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1171 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1174 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1177 if (!btrfs_dev_is_sequential(device, pos) ||
1178 btrfs_dev_is_empty_zone(device, pos))
1181 /* Free regions should be empty */
1184 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1185 rcu_str_deref(device->name), device->devid, pos >> shift);
1188 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1198 * Calculate an allocation pointer from the extent allocation information
1199 * for a block group consist of conventional zones. It is pointed to the
1200 * end of the highest addressed extent in the block group as an allocation
1203 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1204 u64 *offset_ret, bool new)
1206 struct btrfs_fs_info *fs_info = cache->fs_info;
1207 struct btrfs_root *root;
1208 struct btrfs_path *path;
1209 struct btrfs_key key;
1210 struct btrfs_key found_key;
1215 * Avoid tree lookups for a new block group, there's no use for it.
1216 * It must always be 0.
1218 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1219 * For new a block group, this function is called from
1220 * btrfs_make_block_group() which is already taking the chunk mutex.
1221 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1222 * buffer locks to avoid deadlock.
1229 path = btrfs_alloc_path();
1233 key.objectid = cache->start + cache->length;
1237 root = btrfs_extent_root(fs_info, key.objectid);
1238 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1239 /* We should not find the exact match */
1245 ret = btrfs_previous_extent_item(root, path, cache->start);
1254 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1256 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1257 length = found_key.offset;
1259 length = fs_info->nodesize;
1261 if (!(found_key.objectid >= cache->start &&
1262 found_key.objectid + length <= cache->start + cache->length)) {
1266 *offset_ret = found_key.objectid + length - cache->start;
1270 btrfs_free_path(path);
1274 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1276 struct btrfs_fs_info *fs_info = cache->fs_info;
1277 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1278 struct extent_map *em;
1279 struct map_lookup *map;
1280 struct btrfs_device *device;
1281 u64 logical = cache->start;
1282 u64 length = cache->length;
1285 unsigned int nofs_flag;
1286 u64 *alloc_offsets = NULL;
1288 u64 *physical = NULL;
1289 unsigned long *active = NULL;
1291 u32 num_sequential = 0, num_conventional = 0;
1293 if (!btrfs_is_zoned(fs_info))
1297 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1299 "zoned: block group %llu len %llu unaligned to zone size %llu",
1300 logical, length, fs_info->zone_size);
1304 /* Get the chunk mapping */
1305 read_lock(&em_tree->lock);
1306 em = lookup_extent_mapping(em_tree, logical, length);
1307 read_unlock(&em_tree->lock);
1312 map = em->map_lookup;
1314 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1315 if (!cache->physical_map) {
1320 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1321 if (!alloc_offsets) {
1326 caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1332 physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1338 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1344 for (i = 0; i < map->num_stripes; i++) {
1346 struct blk_zone zone;
1347 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1348 int dev_replace_is_ongoing = 0;
1350 device = map->stripes[i].dev;
1351 physical[i] = map->stripes[i].physical;
1353 if (device->bdev == NULL) {
1354 alloc_offsets[i] = WP_MISSING_DEV;
1358 is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1365 * Consider a zone as active if we can allow any number of
1368 if (!device->zone_info->max_active_zones)
1369 __set_bit(i, active);
1371 if (!is_sequential) {
1372 alloc_offsets[i] = WP_CONVENTIONAL;
1377 * This zone will be used for allocation, so mark this zone
1380 btrfs_dev_clear_zone_empty(device, physical[i]);
1382 down_read(&dev_replace->rwsem);
1383 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1384 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1385 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1386 up_read(&dev_replace->rwsem);
1389 * The group is mapped to a sequential zone. Get the zone write
1390 * pointer to determine the allocation offset within the zone.
1392 WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1393 nofs_flag = memalloc_nofs_save();
1394 ret = btrfs_get_dev_zone(device, physical[i], &zone);
1395 memalloc_nofs_restore(nofs_flag);
1396 if (ret == -EIO || ret == -EOPNOTSUPP) {
1398 alloc_offsets[i] = WP_MISSING_DEV;
1404 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1405 btrfs_err_in_rcu(fs_info,
1406 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1407 zone.start << SECTOR_SHIFT,
1408 rcu_str_deref(device->name), device->devid);
1413 caps[i] = (zone.capacity << SECTOR_SHIFT);
1415 switch (zone.cond) {
1416 case BLK_ZONE_COND_OFFLINE:
1417 case BLK_ZONE_COND_READONLY:
1419 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1420 physical[i] >> device->zone_info->zone_size_shift,
1421 rcu_str_deref(device->name), device->devid);
1422 alloc_offsets[i] = WP_MISSING_DEV;
1424 case BLK_ZONE_COND_EMPTY:
1425 alloc_offsets[i] = 0;
1427 case BLK_ZONE_COND_FULL:
1428 alloc_offsets[i] = caps[i];
1431 /* Partially used zone */
1433 ((zone.wp - zone.start) << SECTOR_SHIFT);
1434 __set_bit(i, active);
1439 if (num_sequential > 0)
1440 set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1442 if (num_conventional > 0) {
1443 /* Zone capacity is always zone size in emulation */
1444 cache->zone_capacity = cache->length;
1445 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1448 "zoned: failed to determine allocation offset of bg %llu",
1451 } else if (map->num_stripes == num_conventional) {
1452 cache->alloc_offset = last_alloc;
1453 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1458 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1459 case 0: /* single */
1460 if (alloc_offsets[0] == WP_MISSING_DEV) {
1462 "zoned: cannot recover write pointer for zone %llu",
1467 cache->alloc_offset = alloc_offsets[0];
1468 cache->zone_capacity = caps[0];
1469 if (test_bit(0, active))
1470 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1472 case BTRFS_BLOCK_GROUP_DUP:
1473 if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1474 btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1478 if (alloc_offsets[0] == WP_MISSING_DEV) {
1480 "zoned: cannot recover write pointer for zone %llu",
1485 if (alloc_offsets[1] == WP_MISSING_DEV) {
1487 "zoned: cannot recover write pointer for zone %llu",
1492 if (alloc_offsets[0] != alloc_offsets[1]) {
1494 "zoned: write pointer offset mismatch of zones in DUP profile");
1498 if (test_bit(0, active) != test_bit(1, active)) {
1499 if (!btrfs_zone_activate(cache)) {
1504 if (test_bit(0, active))
1505 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1506 &cache->runtime_flags);
1508 cache->alloc_offset = alloc_offsets[0];
1509 cache->zone_capacity = min(caps[0], caps[1]);
1511 case BTRFS_BLOCK_GROUP_RAID1:
1512 case BTRFS_BLOCK_GROUP_RAID0:
1513 case BTRFS_BLOCK_GROUP_RAID10:
1514 case BTRFS_BLOCK_GROUP_RAID5:
1515 case BTRFS_BLOCK_GROUP_RAID6:
1516 /* non-single profiles are not supported yet */
1518 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1519 btrfs_bg_type_to_raid_name(map->type));
1525 if (cache->alloc_offset > fs_info->zone_size) {
1527 "zoned: invalid write pointer %llu in block group %llu",
1528 cache->alloc_offset, cache->start);
1532 if (cache->alloc_offset > cache->zone_capacity) {
1534 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1535 cache->alloc_offset, cache->zone_capacity,
1540 /* An extent is allocated after the write pointer */
1541 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1543 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1544 logical, last_alloc, cache->alloc_offset);
1549 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1550 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1551 btrfs_get_block_group(cache);
1552 spin_lock(&fs_info->zone_active_bgs_lock);
1553 list_add_tail(&cache->active_bg_list,
1554 &fs_info->zone_active_bgs);
1555 spin_unlock(&fs_info->zone_active_bgs_lock);
1558 kfree(cache->physical_map);
1559 cache->physical_map = NULL;
1561 bitmap_free(active);
1564 kfree(alloc_offsets);
1565 free_extent_map(em);
1570 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1574 if (!btrfs_is_zoned(cache->fs_info))
1577 WARN_ON(cache->bytes_super != 0);
1578 unusable = (cache->alloc_offset - cache->used) +
1579 (cache->length - cache->zone_capacity);
1580 free = cache->zone_capacity - cache->alloc_offset;
1582 /* We only need ->free_space in ALLOC_SEQ block groups */
1583 cache->cached = BTRFS_CACHE_FINISHED;
1584 cache->free_space_ctl->free_space = free;
1585 cache->zone_unusable = unusable;
1588 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1589 struct extent_buffer *eb)
1591 struct btrfs_fs_info *fs_info = eb->fs_info;
1593 if (!btrfs_is_zoned(fs_info) ||
1594 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1595 !list_empty(&eb->release_list))
1598 set_extent_buffer_dirty(eb);
1599 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1600 eb->start + eb->len - 1, EXTENT_DIRTY);
1601 memzero_extent_buffer(eb, 0, eb->len);
1602 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1604 spin_lock(&trans->releasing_ebs_lock);
1605 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1606 spin_unlock(&trans->releasing_ebs_lock);
1607 atomic_inc(&eb->refs);
1610 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1612 spin_lock(&trans->releasing_ebs_lock);
1613 while (!list_empty(&trans->releasing_ebs)) {
1614 struct extent_buffer *eb;
1616 eb = list_first_entry(&trans->releasing_ebs,
1617 struct extent_buffer, release_list);
1618 list_del_init(&eb->release_list);
1619 free_extent_buffer(eb);
1621 spin_unlock(&trans->releasing_ebs_lock);
1624 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1626 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1627 struct btrfs_block_group *cache;
1630 if (!btrfs_is_zoned(fs_info))
1633 if (!is_data_inode(&inode->vfs_inode))
1637 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1638 * extent layout the relocation code has.
1639 * Furthermore we have set aside own block-group from which only the
1640 * relocation "process" can allocate and make sure only one process at a
1641 * time can add pages to an extent that gets relocated, so it's safe to
1642 * use regular REQ_OP_WRITE for this special case.
1644 if (btrfs_is_data_reloc_root(inode->root))
1647 cache = btrfs_lookup_block_group(fs_info, start);
1652 ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1653 btrfs_put_block_group(cache);
1658 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1661 struct btrfs_ordered_extent *ordered;
1662 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1664 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1667 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1668 if (WARN_ON(!ordered))
1671 ordered->physical = physical;
1672 ordered->bdev = bio->bi_bdev;
1674 btrfs_put_ordered_extent(ordered);
1677 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1679 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1680 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1681 struct extent_map_tree *em_tree;
1682 struct extent_map *em;
1683 struct btrfs_ordered_sum *sum;
1684 u64 orig_logical = ordered->disk_bytenr;
1685 u64 *logical = NULL;
1688 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1689 ASSERT(!bdev_is_partition(ordered->bdev));
1690 if (WARN_ON(!ordered->bdev))
1693 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1694 ordered->physical, &logical, &nr,
1700 if (orig_logical == *logical)
1703 ordered->disk_bytenr = *logical;
1705 em_tree = &inode->extent_tree;
1706 write_lock(&em_tree->lock);
1707 em = search_extent_mapping(em_tree, ordered->file_offset,
1708 ordered->num_bytes);
1709 em->block_start = *logical;
1710 free_extent_map(em);
1711 write_unlock(&em_tree->lock);
1713 list_for_each_entry(sum, &ordered->list, list) {
1714 if (*logical < orig_logical)
1715 sum->bytenr -= orig_logical - *logical;
1717 sum->bytenr += *logical - orig_logical;
1724 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1725 struct extent_buffer *eb,
1726 struct btrfs_block_group **cache_ret)
1728 struct btrfs_block_group *cache;
1731 if (!btrfs_is_zoned(fs_info))
1734 cache = btrfs_lookup_block_group(fs_info, eb->start);
1738 if (cache->meta_write_pointer != eb->start) {
1739 btrfs_put_block_group(cache);
1743 cache->meta_write_pointer = eb->start + eb->len;
1751 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1752 struct extent_buffer *eb)
1754 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1757 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1758 cache->meta_write_pointer = eb->start;
1761 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1763 if (!btrfs_dev_is_sequential(device, physical))
1766 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1767 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1770 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1771 struct blk_zone *zone)
1773 struct btrfs_io_context *bioc = NULL;
1774 u64 mapped_length = PAGE_SIZE;
1775 unsigned int nofs_flag;
1779 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1780 &mapped_length, &bioc);
1781 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1786 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1791 nofs_flag = memalloc_nofs_save();
1792 nmirrors = (int)bioc->num_stripes;
1793 for (i = 0; i < nmirrors; i++) {
1794 u64 physical = bioc->stripes[i].physical;
1795 struct btrfs_device *dev = bioc->stripes[i].dev;
1797 /* Missing device */
1801 ret = btrfs_get_dev_zone(dev, physical, zone);
1802 /* Failing device */
1803 if (ret == -EIO || ret == -EOPNOTSUPP)
1807 memalloc_nofs_restore(nofs_flag);
1809 btrfs_put_bioc(bioc);
1814 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1815 * filling zeros between @physical_pos to a write pointer of dev-replace
1818 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1819 u64 physical_start, u64 physical_pos)
1821 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1822 struct blk_zone zone;
1827 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1830 ret = read_zone_info(fs_info, logical, &zone);
1834 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1836 if (physical_pos == wp)
1839 if (physical_pos > wp)
1842 length = wp - physical_pos;
1843 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1846 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1847 u64 logical, u64 length)
1849 struct btrfs_device *device;
1850 struct extent_map *em;
1851 struct map_lookup *map;
1853 em = btrfs_get_chunk_map(fs_info, logical, length);
1855 return ERR_CAST(em);
1857 map = em->map_lookup;
1858 /* We only support single profile for now */
1859 device = map->stripes[0].dev;
1861 free_extent_map(em);
1867 * Activate block group and underlying device zones
1869 * @block_group: the block group to activate
1871 * Return: true on success, false otherwise
1873 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1875 struct btrfs_fs_info *fs_info = block_group->fs_info;
1876 struct btrfs_space_info *space_info = block_group->space_info;
1877 struct map_lookup *map;
1878 struct btrfs_device *device;
1883 if (!btrfs_is_zoned(block_group->fs_info))
1886 map = block_group->physical_map;
1888 spin_lock(&space_info->lock);
1889 spin_lock(&block_group->lock);
1890 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1896 if (btrfs_zoned_bg_is_full(block_group)) {
1901 for (i = 0; i < map->num_stripes; i++) {
1902 device = map->stripes[i].dev;
1903 physical = map->stripes[i].physical;
1905 if (device->zone_info->max_active_zones == 0)
1908 if (!btrfs_dev_set_active_zone(device, physical)) {
1909 /* Cannot activate the zone */
1915 /* Successfully activated all the zones */
1916 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
1917 space_info->active_total_bytes += block_group->length;
1918 spin_unlock(&block_group->lock);
1919 btrfs_try_granting_tickets(fs_info, space_info);
1920 spin_unlock(&space_info->lock);
1922 /* For the active block group list */
1923 btrfs_get_block_group(block_group);
1925 spin_lock(&fs_info->zone_active_bgs_lock);
1926 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1927 spin_unlock(&fs_info->zone_active_bgs_lock);
1932 spin_unlock(&block_group->lock);
1933 spin_unlock(&space_info->lock);
1937 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
1939 struct btrfs_fs_info *fs_info = block_group->fs_info;
1940 const u64 end = block_group->start + block_group->length;
1941 struct radix_tree_iter iter;
1942 struct extent_buffer *eb;
1946 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
1947 block_group->start >> fs_info->sectorsize_bits) {
1948 eb = radix_tree_deref_slot(slot);
1951 if (radix_tree_deref_retry(eb)) {
1952 slot = radix_tree_iter_retry(&iter);
1956 if (eb->start < block_group->start)
1958 if (eb->start >= end)
1961 slot = radix_tree_iter_resume(slot, &iter);
1963 wait_on_extent_buffer_writeback(eb);
1969 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
1971 struct btrfs_fs_info *fs_info = block_group->fs_info;
1972 struct map_lookup *map;
1973 const bool is_metadata = (block_group->flags &
1974 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
1978 spin_lock(&block_group->lock);
1979 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1980 spin_unlock(&block_group->lock);
1984 /* Check if we have unwritten allocated space */
1986 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
1987 spin_unlock(&block_group->lock);
1992 * If we are sure that the block group is full (= no more room left for
1993 * new allocation) and the IO for the last usable block is completed, we
1994 * don't need to wait for the other IOs. This holds because we ensure
1995 * the sequential IO submissions using the ZONE_APPEND command for data
1996 * and block_group->meta_write_pointer for metadata.
1998 if (!fully_written) {
1999 spin_unlock(&block_group->lock);
2001 ret = btrfs_inc_block_group_ro(block_group, false);
2005 /* Ensure all writes in this block group finish */
2006 btrfs_wait_block_group_reservations(block_group);
2007 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
2008 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
2009 block_group->length);
2010 /* Wait for extent buffers to be written. */
2012 wait_eb_writebacks(block_group);
2014 spin_lock(&block_group->lock);
2017 * Bail out if someone already deactivated the block group, or
2018 * allocated space is left in the block group.
2020 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2021 &block_group->runtime_flags)) {
2022 spin_unlock(&block_group->lock);
2023 btrfs_dec_block_group_ro(block_group);
2027 if (block_group->reserved) {
2028 spin_unlock(&block_group->lock);
2029 btrfs_dec_block_group_ro(block_group);
2034 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2035 block_group->alloc_offset = block_group->zone_capacity;
2036 block_group->free_space_ctl->free_space = 0;
2037 btrfs_clear_treelog_bg(block_group);
2038 btrfs_clear_data_reloc_bg(block_group);
2039 spin_unlock(&block_group->lock);
2041 map = block_group->physical_map;
2042 for (i = 0; i < map->num_stripes; i++) {
2043 struct btrfs_device *device = map->stripes[i].dev;
2044 const u64 physical = map->stripes[i].physical;
2046 if (device->zone_info->max_active_zones == 0)
2049 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2050 physical >> SECTOR_SHIFT,
2051 device->zone_info->zone_size >> SECTOR_SHIFT,
2057 btrfs_dev_clear_active_zone(device, physical);
2061 btrfs_dec_block_group_ro(block_group);
2063 spin_lock(&fs_info->zone_active_bgs_lock);
2064 ASSERT(!list_empty(&block_group->active_bg_list));
2065 list_del_init(&block_group->active_bg_list);
2066 spin_unlock(&fs_info->zone_active_bgs_lock);
2068 /* For active_bg_list */
2069 btrfs_put_block_group(block_group);
2071 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2076 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2078 if (!btrfs_is_zoned(block_group->fs_info))
2081 return do_zone_finish(block_group, false);
2084 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2086 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2087 struct btrfs_device *device;
2090 if (!btrfs_is_zoned(fs_info))
2093 /* Check if there is a device with active zones left */
2094 mutex_lock(&fs_info->chunk_mutex);
2095 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2096 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2101 if (!zinfo->max_active_zones ||
2102 atomic_read(&zinfo->active_zones_left)) {
2107 mutex_unlock(&fs_info->chunk_mutex);
2110 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2115 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2117 struct btrfs_block_group *block_group;
2118 u64 min_alloc_bytes;
2120 if (!btrfs_is_zoned(fs_info))
2123 block_group = btrfs_lookup_block_group(fs_info, logical);
2124 ASSERT(block_group);
2126 /* No MIXED_BG on zoned btrfs. */
2127 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2128 min_alloc_bytes = fs_info->sectorsize;
2130 min_alloc_bytes = fs_info->nodesize;
2132 /* Bail out if we can allocate more data from this block group. */
2133 if (logical + length + min_alloc_bytes <=
2134 block_group->start + block_group->zone_capacity)
2137 do_zone_finish(block_group, true);
2140 btrfs_put_block_group(block_group);
2143 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2145 struct btrfs_block_group *bg =
2146 container_of(work, struct btrfs_block_group, zone_finish_work);
2148 wait_on_extent_buffer_writeback(bg->last_eb);
2149 free_extent_buffer(bg->last_eb);
2150 btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2151 btrfs_put_block_group(bg);
2154 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2155 struct extent_buffer *eb)
2157 if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
2158 eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2161 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2162 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2168 btrfs_get_block_group(bg);
2169 atomic_inc(&eb->refs);
2171 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2172 queue_work(system_unbound_wq, &bg->zone_finish_work);
2175 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2177 struct btrfs_fs_info *fs_info = bg->fs_info;
2179 spin_lock(&fs_info->relocation_bg_lock);
2180 if (fs_info->data_reloc_bg == bg->start)
2181 fs_info->data_reloc_bg = 0;
2182 spin_unlock(&fs_info->relocation_bg_lock);
2185 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2187 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2188 struct btrfs_device *device;
2190 if (!btrfs_is_zoned(fs_info))
2193 mutex_lock(&fs_devices->device_list_mutex);
2194 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2195 if (device->zone_info) {
2196 vfree(device->zone_info->zone_cache);
2197 device->zone_info->zone_cache = NULL;
2200 mutex_unlock(&fs_devices->device_list_mutex);
2203 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2205 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2206 struct btrfs_device *device;
2211 ASSERT(btrfs_is_zoned(fs_info));
2213 if (fs_info->bg_reclaim_threshold == 0)
2216 mutex_lock(&fs_devices->device_list_mutex);
2217 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2221 total += device->disk_total_bytes;
2222 used += device->bytes_used;
2224 mutex_unlock(&fs_devices->device_list_mutex);
2226 factor = div64_u64(used * 100, total);
2227 return factor >= fs_info->bg_reclaim_threshold;
2230 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2233 struct btrfs_block_group *block_group;
2235 if (!btrfs_is_zoned(fs_info))
2238 block_group = btrfs_lookup_block_group(fs_info, logical);
2239 /* It should be called on a previous data relocation block group. */
2240 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2242 spin_lock(&block_group->lock);
2243 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2246 /* All relocation extents are written. */
2247 if (block_group->start + block_group->alloc_offset == logical + length) {
2248 /* Now, release this block group for further allocations. */
2249 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2250 &block_group->runtime_flags);
2254 spin_unlock(&block_group->lock);
2255 btrfs_put_block_group(block_group);
2258 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2260 struct btrfs_block_group *block_group;
2261 struct btrfs_block_group *min_bg = NULL;
2262 u64 min_avail = U64_MAX;
2265 spin_lock(&fs_info->zone_active_bgs_lock);
2266 list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2270 spin_lock(&block_group->lock);
2271 if (block_group->reserved ||
2272 (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)) {
2273 spin_unlock(&block_group->lock);
2277 avail = block_group->zone_capacity - block_group->alloc_offset;
2278 if (min_avail > avail) {
2280 btrfs_put_block_group(min_bg);
2281 min_bg = block_group;
2283 btrfs_get_block_group(min_bg);
2285 spin_unlock(&block_group->lock);
2287 spin_unlock(&fs_info->zone_active_bgs_lock);
2292 ret = btrfs_zone_finish(min_bg);
2293 btrfs_put_block_group(min_bg);
2295 return ret < 0 ? ret : 1;
2298 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2299 struct btrfs_space_info *space_info,
2302 struct btrfs_block_group *bg;
2305 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2308 /* No more block groups to activate */
2309 if (space_info->active_total_bytes == space_info->total_bytes)
2314 bool need_finish = false;
2316 down_read(&space_info->groups_sem);
2317 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2318 list_for_each_entry(bg, &space_info->block_groups[index],
2320 if (!spin_trylock(&bg->lock))
2322 if (btrfs_zoned_bg_is_full(bg) ||
2323 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2324 &bg->runtime_flags)) {
2325 spin_unlock(&bg->lock);
2328 spin_unlock(&bg->lock);
2330 if (btrfs_zone_activate(bg)) {
2331 up_read(&space_info->groups_sem);
2338 up_read(&space_info->groups_sem);
2340 if (!do_finish || !need_finish)
2343 ret = btrfs_zone_finish_one_bg(fs_info);