1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
5 #include <linux/blkdev.h>
6 #include <linux/sched/mm.h>
7 #include <linux/atomic.h>
8 #include <linux/vmalloc.h>
12 #include "rcu-string.h"
14 #include "block-group.h"
15 #include "transaction.h"
16 #include "dev-replace.h"
17 #include "space-info.h"
19 /* Maximum number of zones to report per blkdev_report_zones() call */
20 #define BTRFS_REPORT_NR_ZONES 4096
21 /* Invalid allocation pointer value for missing devices */
22 #define WP_MISSING_DEV ((u64)-1)
23 /* Pseudo write pointer value for conventional zone */
24 #define WP_CONVENTIONAL ((u64)-2)
27 * Location of the first zone of superblock logging zone pairs.
29 * - primary superblock: 0B (zone 0)
30 * - first copy: 512G (zone starting at that offset)
31 * - second copy: 4T (zone starting at that offset)
33 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
34 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
35 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
37 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
38 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
40 /* Number of superblock log zones */
41 #define BTRFS_NR_SB_LOG_ZONES 2
44 * Minimum of active zones we need:
46 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
47 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
48 * - 1 zone for tree-log dedicated block group
49 * - 1 zone for relocation
51 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
54 * Minimum / maximum supported zone size. Currently, SMR disks have a zone
55 * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
56 * We do not expect the zone size to become larger than 8GiB or smaller than
57 * 4MiB in the near future.
59 #define BTRFS_MAX_ZONE_SIZE SZ_8G
60 #define BTRFS_MIN_ZONE_SIZE SZ_4M
62 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
64 static inline bool sb_zone_is_full(const struct blk_zone *zone)
66 return (zone->cond == BLK_ZONE_COND_FULL) ||
67 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
70 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
72 struct blk_zone *zones = data;
74 memcpy(&zones[idx], zone, sizeof(*zone));
79 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
82 bool empty[BTRFS_NR_SB_LOG_ZONES];
83 bool full[BTRFS_NR_SB_LOG_ZONES];
87 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
88 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
89 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
90 full[i] = sb_zone_is_full(&zones[i]);
94 * Possible states of log buffer zones
96 * Empty[0] In use[0] Full[0]
102 * *: Special case, no superblock is written
103 * 0: Use write pointer of zones[0]
104 * 1: Use write pointer of zones[1]
105 * C: Compare super blocks from zones[0] and zones[1], use the latest
106 * one determined by generation
110 if (empty[0] && empty[1]) {
111 /* Special case to distinguish no superblock to read */
112 *wp_ret = zones[0].start << SECTOR_SHIFT;
114 } else if (full[0] && full[1]) {
115 /* Compare two super blocks */
116 struct address_space *mapping = bdev->bd_inode->i_mapping;
117 struct page *page[BTRFS_NR_SB_LOG_ZONES];
118 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
121 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
124 bytenr = ((zones[i].start + zones[i].len)
125 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
127 page[i] = read_cache_page_gfp(mapping,
128 bytenr >> PAGE_SHIFT, GFP_NOFS);
129 if (IS_ERR(page[i])) {
131 btrfs_release_disk_super(super[0]);
132 return PTR_ERR(page[i]);
134 super[i] = page_address(page[i]);
137 if (super[0]->generation > super[1]->generation)
138 sector = zones[1].start;
140 sector = zones[0].start;
142 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
143 btrfs_release_disk_super(super[i]);
144 } else if (!full[0] && (empty[1] || full[1])) {
145 sector = zones[0].wp;
146 } else if (full[0]) {
147 sector = zones[1].wp;
151 *wp_ret = sector << SECTOR_SHIFT;
156 * Get the first zone number of the superblock mirror
158 static inline u32 sb_zone_number(int shift, int mirror)
162 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
164 case 0: zone = 0; break;
165 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
166 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
169 ASSERT(zone <= U32_MAX);
174 static inline sector_t zone_start_sector(u32 zone_number,
175 struct block_device *bdev)
177 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
180 static inline u64 zone_start_physical(u32 zone_number,
181 struct btrfs_zoned_device_info *zone_info)
183 return (u64)zone_number << zone_info->zone_size_shift;
187 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
188 * device into static sized chunks and fake a conventional zone on each of
191 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
192 struct blk_zone *zones, unsigned int nr_zones)
194 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
195 sector_t bdev_size = bdev_nr_sectors(device->bdev);
198 pos >>= SECTOR_SHIFT;
199 for (i = 0; i < nr_zones; i++) {
200 zones[i].start = i * zone_sectors + pos;
201 zones[i].len = zone_sectors;
202 zones[i].capacity = zone_sectors;
203 zones[i].wp = zones[i].start + zone_sectors;
204 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
205 zones[i].cond = BLK_ZONE_COND_NOT_WP;
207 if (zones[i].wp >= bdev_size) {
216 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
217 struct blk_zone *zones, unsigned int *nr_zones)
219 struct btrfs_zoned_device_info *zinfo = device->zone_info;
226 if (!bdev_is_zoned(device->bdev)) {
227 ret = emulate_report_zones(device, pos, zones, *nr_zones);
233 if (zinfo->zone_cache) {
236 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
237 zno = pos >> zinfo->zone_size_shift;
239 * We cannot report zones beyond the zone end. So, it is OK to
240 * cap *nr_zones to at the end.
242 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
244 for (i = 0; i < *nr_zones; i++) {
245 struct blk_zone *zone_info;
247 zone_info = &zinfo->zone_cache[zno + i];
252 if (i == *nr_zones) {
253 /* Cache hit on all the zones */
254 memcpy(zones, zinfo->zone_cache + zno,
255 sizeof(*zinfo->zone_cache) * *nr_zones);
260 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
261 copy_zone_info_cb, zones);
263 btrfs_err_in_rcu(device->fs_info,
264 "zoned: failed to read zone %llu on %s (devid %llu)",
265 pos, rcu_str_deref(device->name),
274 if (zinfo->zone_cache)
275 memcpy(zinfo->zone_cache + zno, zones,
276 sizeof(*zinfo->zone_cache) * *nr_zones);
281 /* The emulated zone size is determined from the size of device extent */
282 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
284 struct btrfs_path *path;
285 struct btrfs_root *root = fs_info->dev_root;
286 struct btrfs_key key;
287 struct extent_buffer *leaf;
288 struct btrfs_dev_extent *dext;
292 key.type = BTRFS_DEV_EXTENT_KEY;
295 path = btrfs_alloc_path();
299 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
303 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
304 ret = btrfs_next_leaf(root, path);
307 /* No dev extents at all? Not good */
314 leaf = path->nodes[0];
315 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
316 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
320 btrfs_free_path(path);
325 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
327 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
328 struct btrfs_device *device;
331 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
332 if (!btrfs_fs_incompat(fs_info, ZONED))
335 mutex_lock(&fs_devices->device_list_mutex);
336 list_for_each_entry(device, &fs_devices->devices, dev_list) {
337 /* We can skip reading of zone info for missing devices */
341 ret = btrfs_get_dev_zone_info(device, true);
345 mutex_unlock(&fs_devices->device_list_mutex);
350 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
352 struct btrfs_fs_info *fs_info = device->fs_info;
353 struct btrfs_zoned_device_info *zone_info = NULL;
354 struct block_device *bdev = device->bdev;
355 unsigned int max_active_zones;
356 unsigned int nactive;
359 struct blk_zone *zones = NULL;
360 unsigned int i, nreported = 0, nr_zones;
361 sector_t zone_sectors;
362 char *model, *emulated;
366 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
369 if (!btrfs_fs_incompat(fs_info, ZONED))
372 if (device->zone_info)
375 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
379 device->zone_info = zone_info;
381 if (!bdev_is_zoned(bdev)) {
382 if (!fs_info->zone_size) {
383 ret = calculate_emulated_zone_size(fs_info);
388 ASSERT(fs_info->zone_size);
389 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
391 zone_sectors = bdev_zone_sectors(bdev);
394 /* Check if it's power of 2 (see is_power_of_2) */
395 ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
396 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
398 /* We reject devices with a zone size larger than 8GB */
399 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
400 btrfs_err_in_rcu(fs_info,
401 "zoned: %s: zone size %llu larger than supported maximum %llu",
402 rcu_str_deref(device->name),
403 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
406 } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
407 btrfs_err_in_rcu(fs_info,
408 "zoned: %s: zone size %llu smaller than supported minimum %u",
409 rcu_str_deref(device->name),
410 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
415 nr_sectors = bdev_nr_sectors(bdev);
416 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
417 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
419 * We limit max_zone_append_size also by max_segments *
420 * PAGE_SIZE. Technically, we can have multiple pages per segment. But,
421 * since btrfs adds the pages one by one to a bio, and btrfs cannot
422 * increase the metadata reservation even if it increases the number of
423 * extents, it is safe to stick with the limit.
425 * With the zoned emulation, we can have non-zoned device on the zoned
426 * mode. In this case, we don't have a valid max zone append size. So,
427 * use max_segments * PAGE_SIZE as the pseudo max_zone_append_size.
429 if (bdev_is_zoned(bdev)) {
430 zone_info->max_zone_append_size = min_t(u64,
431 (u64)bdev_max_zone_append_sectors(bdev) << SECTOR_SHIFT,
432 (u64)bdev_max_segments(bdev) << PAGE_SHIFT);
434 zone_info->max_zone_append_size =
435 (u64)bdev_max_segments(bdev) << PAGE_SHIFT;
437 if (!IS_ALIGNED(nr_sectors, zone_sectors))
438 zone_info->nr_zones++;
440 max_active_zones = bdev_max_active_zones(bdev);
441 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
442 btrfs_err_in_rcu(fs_info,
443 "zoned: %s: max active zones %u is too small, need at least %u active zones",
444 rcu_str_deref(device->name), max_active_zones,
445 BTRFS_MIN_ACTIVE_ZONES);
449 zone_info->max_active_zones = max_active_zones;
451 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
452 if (!zone_info->seq_zones) {
457 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
458 if (!zone_info->empty_zones) {
463 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
464 if (!zone_info->active_zones) {
469 zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
476 * Enable zone cache only for a zoned device. On a non-zoned device, we
477 * fill the zone info with emulated CONVENTIONAL zones, so no need to
480 if (populate_cache && bdev_is_zoned(device->bdev)) {
481 zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
482 zone_info->nr_zones);
483 if (!zone_info->zone_cache) {
484 btrfs_err_in_rcu(device->fs_info,
485 "zoned: failed to allocate zone cache for %s",
486 rcu_str_deref(device->name));
494 while (sector < nr_sectors) {
495 nr_zones = BTRFS_REPORT_NR_ZONES;
496 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
501 for (i = 0; i < nr_zones; i++) {
502 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
503 __set_bit(nreported, zone_info->seq_zones);
504 switch (zones[i].cond) {
505 case BLK_ZONE_COND_EMPTY:
506 __set_bit(nreported, zone_info->empty_zones);
508 case BLK_ZONE_COND_IMP_OPEN:
509 case BLK_ZONE_COND_EXP_OPEN:
510 case BLK_ZONE_COND_CLOSED:
511 __set_bit(nreported, zone_info->active_zones);
517 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
520 if (nreported != zone_info->nr_zones) {
521 btrfs_err_in_rcu(device->fs_info,
522 "inconsistent number of zones on %s (%u/%u)",
523 rcu_str_deref(device->name), nreported,
524 zone_info->nr_zones);
529 if (max_active_zones) {
530 if (nactive > max_active_zones) {
531 btrfs_err_in_rcu(device->fs_info,
532 "zoned: %u active zones on %s exceeds max_active_zones %u",
533 nactive, rcu_str_deref(device->name),
538 atomic_set(&zone_info->active_zones_left,
539 max_active_zones - nactive);
542 /* Validate superblock log */
543 nr_zones = BTRFS_NR_SB_LOG_ZONES;
544 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
547 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
549 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
550 if (sb_zone + 1 >= zone_info->nr_zones)
553 ret = btrfs_get_dev_zones(device,
554 zone_start_physical(sb_zone, zone_info),
555 &zone_info->sb_zones[sb_pos],
560 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
561 btrfs_err_in_rcu(device->fs_info,
562 "zoned: failed to read super block log zone info at devid %llu zone %u",
563 device->devid, sb_zone);
569 * If zones[0] is conventional, always use the beginning of the
570 * zone to record superblock. No need to validate in that case.
572 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
573 BLK_ZONE_TYPE_CONVENTIONAL)
576 ret = sb_write_pointer(device->bdev,
577 &zone_info->sb_zones[sb_pos], &sb_wp);
578 if (ret != -ENOENT && ret) {
579 btrfs_err_in_rcu(device->fs_info,
580 "zoned: super block log zone corrupted devid %llu zone %u",
581 device->devid, sb_zone);
590 switch (bdev_zoned_model(bdev)) {
592 model = "host-managed zoned";
596 model = "host-aware zoned";
601 emulated = "emulated ";
605 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
606 bdev_zoned_model(bdev),
607 rcu_str_deref(device->name));
609 goto out_free_zone_info;
612 btrfs_info_in_rcu(fs_info,
613 "%s block device %s, %u %szones of %llu bytes",
614 model, rcu_str_deref(device->name), zone_info->nr_zones,
615 emulated, zone_info->zone_size);
622 btrfs_destroy_dev_zone_info(device);
627 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
629 struct btrfs_zoned_device_info *zone_info = device->zone_info;
634 bitmap_free(zone_info->active_zones);
635 bitmap_free(zone_info->seq_zones);
636 bitmap_free(zone_info->empty_zones);
637 vfree(zone_info->zone_cache);
639 device->zone_info = NULL;
642 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
644 struct btrfs_zoned_device_info *zone_info;
646 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
650 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
651 if (!zone_info->seq_zones)
654 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
655 zone_info->nr_zones);
657 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
658 if (!zone_info->empty_zones)
661 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
662 zone_info->nr_zones);
664 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
665 if (!zone_info->active_zones)
668 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
669 zone_info->nr_zones);
670 zone_info->zone_cache = NULL;
675 bitmap_free(zone_info->seq_zones);
676 bitmap_free(zone_info->empty_zones);
677 bitmap_free(zone_info->active_zones);
682 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
683 struct blk_zone *zone)
685 unsigned int nr_zones = 1;
688 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
689 if (ret != 0 || !nr_zones)
690 return ret ? ret : -EIO;
695 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
697 struct btrfs_device *device;
699 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
701 bdev_zoned_model(device->bdev) == BLK_ZONED_HM) {
703 "zoned: mode not enabled but zoned device found: %pg",
712 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
714 struct btrfs_device *device;
716 u64 max_zone_append_size = 0;
720 * Host-Managed devices can't be used without the ZONED flag. With the
721 * ZONED all devices can be used, using zone emulation if required.
723 if (!btrfs_fs_incompat(fs_info, ZONED))
724 return btrfs_check_for_zoned_device(fs_info);
726 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
727 struct btrfs_zoned_device_info *zone_info = device->zone_info;
733 zone_size = zone_info->zone_size;
734 } else if (zone_info->zone_size != zone_size) {
736 "zoned: unequal block device zone sizes: have %llu found %llu",
737 zone_info->zone_size, zone_size);
740 if (!max_zone_append_size ||
741 (zone_info->max_zone_append_size &&
742 zone_info->max_zone_append_size < max_zone_append_size))
743 max_zone_append_size = zone_info->max_zone_append_size;
747 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
748 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
749 * check the alignment here.
751 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
753 "zoned: zone size %llu not aligned to stripe %u",
754 zone_size, BTRFS_STRIPE_LEN);
758 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
759 btrfs_err(fs_info, "zoned: mixed block groups not supported");
763 fs_info->zone_size = zone_size;
764 fs_info->max_zone_append_size = ALIGN_DOWN(max_zone_append_size,
765 fs_info->sectorsize);
766 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
767 if (fs_info->max_zone_append_size < fs_info->max_extent_size)
768 fs_info->max_extent_size = fs_info->max_zone_append_size;
771 * Check mount options here, because we might change fs_info->zoned
772 * from fs_info->zone_size.
774 ret = btrfs_check_mountopts_zoned(fs_info);
778 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
782 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
784 if (!btrfs_is_zoned(info))
788 * Space cache writing is not COWed. Disable that to avoid write errors
789 * in sequential zones.
791 if (btrfs_test_opt(info, SPACE_CACHE)) {
792 btrfs_err(info, "zoned: space cache v1 is not supported");
796 if (btrfs_test_opt(info, NODATACOW)) {
797 btrfs_err(info, "zoned: NODATACOW not supported");
804 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
805 int rw, u64 *bytenr_ret)
810 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
811 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
815 ret = sb_write_pointer(bdev, zones, &wp);
816 if (ret != -ENOENT && ret < 0)
820 struct blk_zone *reset = NULL;
822 if (wp == zones[0].start << SECTOR_SHIFT)
824 else if (wp == zones[1].start << SECTOR_SHIFT)
827 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
828 ASSERT(sb_zone_is_full(reset));
830 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
831 reset->start, reset->len,
836 reset->cond = BLK_ZONE_COND_EMPTY;
837 reset->wp = reset->start;
839 } else if (ret != -ENOENT) {
841 * For READ, we want the previous one. Move write pointer to
842 * the end of a zone, if it is at the head of a zone.
846 if (wp == zones[0].start << SECTOR_SHIFT)
847 zone_end = zones[1].start + zones[1].capacity;
848 else if (wp == zones[1].start << SECTOR_SHIFT)
849 zone_end = zones[0].start + zones[0].capacity;
851 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
852 BTRFS_SUPER_INFO_SIZE);
854 wp -= BTRFS_SUPER_INFO_SIZE;
862 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
865 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
866 sector_t zone_sectors;
869 u8 zone_sectors_shift;
873 if (!bdev_is_zoned(bdev)) {
874 *bytenr_ret = btrfs_sb_offset(mirror);
878 ASSERT(rw == READ || rw == WRITE);
880 zone_sectors = bdev_zone_sectors(bdev);
881 if (!is_power_of_2(zone_sectors))
883 zone_sectors_shift = ilog2(zone_sectors);
884 nr_sectors = bdev_nr_sectors(bdev);
885 nr_zones = nr_sectors >> zone_sectors_shift;
887 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
888 if (sb_zone + 1 >= nr_zones)
891 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
892 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
896 if (ret != BTRFS_NR_SB_LOG_ZONES)
899 return sb_log_location(bdev, zones, rw, bytenr_ret);
902 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
905 struct btrfs_zoned_device_info *zinfo = device->zone_info;
909 * For a zoned filesystem on a non-zoned block device, use the same
910 * super block locations as regular filesystem. Doing so, the super
911 * block can always be retrieved and the zoned flag of the volume
912 * detected from the super block information.
914 if (!bdev_is_zoned(device->bdev)) {
915 *bytenr_ret = btrfs_sb_offset(mirror);
919 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
920 if (zone_num + 1 >= zinfo->nr_zones)
923 return sb_log_location(device->bdev,
924 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
928 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
936 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
937 if (zone_num + 1 >= zinfo->nr_zones)
940 if (!test_bit(zone_num, zinfo->seq_zones))
946 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
948 struct btrfs_zoned_device_info *zinfo = device->zone_info;
949 struct blk_zone *zone;
952 if (!is_sb_log_zone(zinfo, mirror))
955 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
956 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
957 /* Advance the next zone */
958 if (zone->cond == BLK_ZONE_COND_FULL) {
963 if (zone->cond == BLK_ZONE_COND_EMPTY)
964 zone->cond = BLK_ZONE_COND_IMP_OPEN;
966 zone->wp += SUPER_INFO_SECTORS;
968 if (sb_zone_is_full(zone)) {
970 * No room left to write new superblock. Since
971 * superblock is written with REQ_SYNC, it is safe to
972 * finish the zone now.
974 * If the write pointer is exactly at the capacity,
975 * explicit ZONE_FINISH is not necessary.
977 if (zone->wp != zone->start + zone->capacity) {
980 ret = blkdev_zone_mgmt(device->bdev,
981 REQ_OP_ZONE_FINISH, zone->start,
982 zone->len, GFP_NOFS);
987 zone->wp = zone->start + zone->len;
988 zone->cond = BLK_ZONE_COND_FULL;
993 /* All the zones are FULL. Should not reach here. */
998 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
1000 sector_t zone_sectors;
1001 sector_t nr_sectors;
1002 u8 zone_sectors_shift;
1006 zone_sectors = bdev_zone_sectors(bdev);
1007 zone_sectors_shift = ilog2(zone_sectors);
1008 nr_sectors = bdev_nr_sectors(bdev);
1009 nr_zones = nr_sectors >> zone_sectors_shift;
1011 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1012 if (sb_zone + 1 >= nr_zones)
1015 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1016 zone_start_sector(sb_zone, bdev),
1017 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1021 * btrfs_find_allocatable_zones - find allocatable zones within a given region
1023 * @device: the device to allocate a region on
1024 * @hole_start: the position of the hole to allocate the region
1025 * @num_bytes: size of wanted region
1026 * @hole_end: the end of the hole
1027 * @return: position of allocatable zones
1029 * Allocatable region should not contain any superblock locations.
1031 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1032 u64 hole_end, u64 num_bytes)
1034 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1035 const u8 shift = zinfo->zone_size_shift;
1036 u64 nzones = num_bytes >> shift;
1037 u64 pos = hole_start;
1042 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1043 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1045 while (pos < hole_end) {
1046 begin = pos >> shift;
1047 end = begin + nzones;
1049 if (end > zinfo->nr_zones)
1052 /* Check if zones in the region are all empty */
1053 if (btrfs_dev_is_sequential(device, pos) &&
1054 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1055 pos += zinfo->zone_size;
1060 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1064 sb_zone = sb_zone_number(shift, i);
1065 if (!(end <= sb_zone ||
1066 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1068 pos = zone_start_physical(
1069 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1073 /* We also need to exclude regular superblock positions */
1074 sb_pos = btrfs_sb_offset(i);
1075 if (!(pos + num_bytes <= sb_pos ||
1076 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1078 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1090 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1092 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1093 unsigned int zno = (pos >> zone_info->zone_size_shift);
1095 /* We can use any number of zones */
1096 if (zone_info->max_active_zones == 0)
1099 if (!test_bit(zno, zone_info->active_zones)) {
1100 /* Active zone left? */
1101 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1103 if (test_and_set_bit(zno, zone_info->active_zones)) {
1104 /* Someone already set the bit */
1105 atomic_inc(&zone_info->active_zones_left);
1112 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1114 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1115 unsigned int zno = (pos >> zone_info->zone_size_shift);
1117 /* We can use any number of zones */
1118 if (zone_info->max_active_zones == 0)
1121 if (test_and_clear_bit(zno, zone_info->active_zones))
1122 atomic_inc(&zone_info->active_zones_left);
1125 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1126 u64 length, u64 *bytes)
1131 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1132 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1139 btrfs_dev_set_zone_empty(device, physical);
1140 btrfs_dev_clear_active_zone(device, physical);
1141 physical += device->zone_info->zone_size;
1142 length -= device->zone_info->zone_size;
1148 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1150 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1151 const u8 shift = zinfo->zone_size_shift;
1152 unsigned long begin = start >> shift;
1153 unsigned long end = (start + size) >> shift;
1157 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1158 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1160 if (end > zinfo->nr_zones)
1163 /* All the zones are conventional */
1164 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1167 /* All the zones are sequential and empty */
1168 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1169 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1172 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1175 if (!btrfs_dev_is_sequential(device, pos) ||
1176 btrfs_dev_is_empty_zone(device, pos))
1179 /* Free regions should be empty */
1182 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1183 rcu_str_deref(device->name), device->devid, pos >> shift);
1186 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1196 * Calculate an allocation pointer from the extent allocation information
1197 * for a block group consist of conventional zones. It is pointed to the
1198 * end of the highest addressed extent in the block group as an allocation
1201 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1202 u64 *offset_ret, bool new)
1204 struct btrfs_fs_info *fs_info = cache->fs_info;
1205 struct btrfs_root *root;
1206 struct btrfs_path *path;
1207 struct btrfs_key key;
1208 struct btrfs_key found_key;
1213 * Avoid tree lookups for a new block group, there's no use for it.
1214 * It must always be 0.
1216 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1217 * For new a block group, this function is called from
1218 * btrfs_make_block_group() which is already taking the chunk mutex.
1219 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1220 * buffer locks to avoid deadlock.
1227 path = btrfs_alloc_path();
1231 key.objectid = cache->start + cache->length;
1235 root = btrfs_extent_root(fs_info, key.objectid);
1236 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1237 /* We should not find the exact match */
1243 ret = btrfs_previous_extent_item(root, path, cache->start);
1252 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1254 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1255 length = found_key.offset;
1257 length = fs_info->nodesize;
1259 if (!(found_key.objectid >= cache->start &&
1260 found_key.objectid + length <= cache->start + cache->length)) {
1264 *offset_ret = found_key.objectid + length - cache->start;
1268 btrfs_free_path(path);
1272 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1274 struct btrfs_fs_info *fs_info = cache->fs_info;
1275 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1276 struct extent_map *em;
1277 struct map_lookup *map;
1278 struct btrfs_device *device;
1279 u64 logical = cache->start;
1280 u64 length = cache->length;
1283 unsigned int nofs_flag;
1284 u64 *alloc_offsets = NULL;
1286 u64 *physical = NULL;
1287 unsigned long *active = NULL;
1289 u32 num_sequential = 0, num_conventional = 0;
1291 if (!btrfs_is_zoned(fs_info))
1295 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1297 "zoned: block group %llu len %llu unaligned to zone size %llu",
1298 logical, length, fs_info->zone_size);
1302 /* Get the chunk mapping */
1303 read_lock(&em_tree->lock);
1304 em = lookup_extent_mapping(em_tree, logical, length);
1305 read_unlock(&em_tree->lock);
1310 map = em->map_lookup;
1312 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1313 if (!cache->physical_map) {
1318 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1319 if (!alloc_offsets) {
1324 caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1330 physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1336 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1342 for (i = 0; i < map->num_stripes; i++) {
1344 struct blk_zone zone;
1345 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1346 int dev_replace_is_ongoing = 0;
1348 device = map->stripes[i].dev;
1349 physical[i] = map->stripes[i].physical;
1351 if (device->bdev == NULL) {
1352 alloc_offsets[i] = WP_MISSING_DEV;
1356 is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1363 * Consider a zone as active if we can allow any number of
1366 if (!device->zone_info->max_active_zones)
1367 __set_bit(i, active);
1369 if (!is_sequential) {
1370 alloc_offsets[i] = WP_CONVENTIONAL;
1375 * This zone will be used for allocation, so mark this zone
1378 btrfs_dev_clear_zone_empty(device, physical[i]);
1380 down_read(&dev_replace->rwsem);
1381 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1382 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1383 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1384 up_read(&dev_replace->rwsem);
1387 * The group is mapped to a sequential zone. Get the zone write
1388 * pointer to determine the allocation offset within the zone.
1390 WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1391 nofs_flag = memalloc_nofs_save();
1392 ret = btrfs_get_dev_zone(device, physical[i], &zone);
1393 memalloc_nofs_restore(nofs_flag);
1394 if (ret == -EIO || ret == -EOPNOTSUPP) {
1396 alloc_offsets[i] = WP_MISSING_DEV;
1402 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1403 btrfs_err_in_rcu(fs_info,
1404 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1405 zone.start << SECTOR_SHIFT,
1406 rcu_str_deref(device->name), device->devid);
1411 caps[i] = (zone.capacity << SECTOR_SHIFT);
1413 switch (zone.cond) {
1414 case BLK_ZONE_COND_OFFLINE:
1415 case BLK_ZONE_COND_READONLY:
1417 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1418 physical[i] >> device->zone_info->zone_size_shift,
1419 rcu_str_deref(device->name), device->devid);
1420 alloc_offsets[i] = WP_MISSING_DEV;
1422 case BLK_ZONE_COND_EMPTY:
1423 alloc_offsets[i] = 0;
1425 case BLK_ZONE_COND_FULL:
1426 alloc_offsets[i] = caps[i];
1429 /* Partially used zone */
1431 ((zone.wp - zone.start) << SECTOR_SHIFT);
1432 __set_bit(i, active);
1437 if (num_sequential > 0)
1438 cache->seq_zone = true;
1440 if (num_conventional > 0) {
1441 /* Zone capacity is always zone size in emulation */
1442 cache->zone_capacity = cache->length;
1443 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1446 "zoned: failed to determine allocation offset of bg %llu",
1449 } else if (map->num_stripes == num_conventional) {
1450 cache->alloc_offset = last_alloc;
1451 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1456 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1457 case 0: /* single */
1458 if (alloc_offsets[0] == WP_MISSING_DEV) {
1460 "zoned: cannot recover write pointer for zone %llu",
1465 cache->alloc_offset = alloc_offsets[0];
1466 cache->zone_capacity = caps[0];
1467 if (test_bit(0, active))
1468 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1470 case BTRFS_BLOCK_GROUP_DUP:
1471 if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1472 btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1476 if (alloc_offsets[0] == WP_MISSING_DEV) {
1478 "zoned: cannot recover write pointer for zone %llu",
1483 if (alloc_offsets[1] == WP_MISSING_DEV) {
1485 "zoned: cannot recover write pointer for zone %llu",
1490 if (alloc_offsets[0] != alloc_offsets[1]) {
1492 "zoned: write pointer offset mismatch of zones in DUP profile");
1496 if (test_bit(0, active) != test_bit(1, active)) {
1497 if (!btrfs_zone_activate(cache)) {
1502 if (test_bit(0, active))
1503 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1504 &cache->runtime_flags);
1506 cache->alloc_offset = alloc_offsets[0];
1507 cache->zone_capacity = min(caps[0], caps[1]);
1509 case BTRFS_BLOCK_GROUP_RAID1:
1510 case BTRFS_BLOCK_GROUP_RAID0:
1511 case BTRFS_BLOCK_GROUP_RAID10:
1512 case BTRFS_BLOCK_GROUP_RAID5:
1513 case BTRFS_BLOCK_GROUP_RAID6:
1514 /* non-single profiles are not supported yet */
1516 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1517 btrfs_bg_type_to_raid_name(map->type));
1523 if (cache->alloc_offset > fs_info->zone_size) {
1525 "zoned: invalid write pointer %llu in block group %llu",
1526 cache->alloc_offset, cache->start);
1530 if (cache->alloc_offset > cache->zone_capacity) {
1532 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1533 cache->alloc_offset, cache->zone_capacity,
1538 /* An extent is allocated after the write pointer */
1539 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1541 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1542 logical, last_alloc, cache->alloc_offset);
1547 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1548 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1549 btrfs_get_block_group(cache);
1550 spin_lock(&fs_info->zone_active_bgs_lock);
1551 list_add_tail(&cache->active_bg_list,
1552 &fs_info->zone_active_bgs);
1553 spin_unlock(&fs_info->zone_active_bgs_lock);
1556 kfree(cache->physical_map);
1557 cache->physical_map = NULL;
1559 bitmap_free(active);
1562 kfree(alloc_offsets);
1563 free_extent_map(em);
1568 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1572 if (!btrfs_is_zoned(cache->fs_info))
1575 WARN_ON(cache->bytes_super != 0);
1576 unusable = (cache->alloc_offset - cache->used) +
1577 (cache->length - cache->zone_capacity);
1578 free = cache->zone_capacity - cache->alloc_offset;
1580 /* We only need ->free_space in ALLOC_SEQ block groups */
1581 cache->cached = BTRFS_CACHE_FINISHED;
1582 cache->free_space_ctl->free_space = free;
1583 cache->zone_unusable = unusable;
1586 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1587 struct extent_buffer *eb)
1589 struct btrfs_fs_info *fs_info = eb->fs_info;
1591 if (!btrfs_is_zoned(fs_info) ||
1592 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1593 !list_empty(&eb->release_list))
1596 set_extent_buffer_dirty(eb);
1597 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1598 eb->start + eb->len - 1, EXTENT_DIRTY);
1599 memzero_extent_buffer(eb, 0, eb->len);
1600 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1602 spin_lock(&trans->releasing_ebs_lock);
1603 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1604 spin_unlock(&trans->releasing_ebs_lock);
1605 atomic_inc(&eb->refs);
1608 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1610 spin_lock(&trans->releasing_ebs_lock);
1611 while (!list_empty(&trans->releasing_ebs)) {
1612 struct extent_buffer *eb;
1614 eb = list_first_entry(&trans->releasing_ebs,
1615 struct extent_buffer, release_list);
1616 list_del_init(&eb->release_list);
1617 free_extent_buffer(eb);
1619 spin_unlock(&trans->releasing_ebs_lock);
1622 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1624 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1625 struct btrfs_block_group *cache;
1628 if (!btrfs_is_zoned(fs_info))
1631 if (!is_data_inode(&inode->vfs_inode))
1635 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1636 * extent layout the relocation code has.
1637 * Furthermore we have set aside own block-group from which only the
1638 * relocation "process" can allocate and make sure only one process at a
1639 * time can add pages to an extent that gets relocated, so it's safe to
1640 * use regular REQ_OP_WRITE for this special case.
1642 if (btrfs_is_data_reloc_root(inode->root))
1645 cache = btrfs_lookup_block_group(fs_info, start);
1650 ret = cache->seq_zone;
1651 btrfs_put_block_group(cache);
1656 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1659 struct btrfs_ordered_extent *ordered;
1660 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1662 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1665 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1666 if (WARN_ON(!ordered))
1669 ordered->physical = physical;
1670 ordered->bdev = bio->bi_bdev;
1672 btrfs_put_ordered_extent(ordered);
1675 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1677 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1678 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1679 struct extent_map_tree *em_tree;
1680 struct extent_map *em;
1681 struct btrfs_ordered_sum *sum;
1682 u64 orig_logical = ordered->disk_bytenr;
1683 u64 *logical = NULL;
1686 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1687 ASSERT(!bdev_is_partition(ordered->bdev));
1688 if (WARN_ON(!ordered->bdev))
1691 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1692 ordered->physical, &logical, &nr,
1698 if (orig_logical == *logical)
1701 ordered->disk_bytenr = *logical;
1703 em_tree = &inode->extent_tree;
1704 write_lock(&em_tree->lock);
1705 em = search_extent_mapping(em_tree, ordered->file_offset,
1706 ordered->num_bytes);
1707 em->block_start = *logical;
1708 free_extent_map(em);
1709 write_unlock(&em_tree->lock);
1711 list_for_each_entry(sum, &ordered->list, list) {
1712 if (*logical < orig_logical)
1713 sum->bytenr -= orig_logical - *logical;
1715 sum->bytenr += *logical - orig_logical;
1722 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1723 struct extent_buffer *eb,
1724 struct btrfs_block_group **cache_ret)
1726 struct btrfs_block_group *cache;
1729 if (!btrfs_is_zoned(fs_info))
1732 cache = btrfs_lookup_block_group(fs_info, eb->start);
1736 if (cache->meta_write_pointer != eb->start) {
1737 btrfs_put_block_group(cache);
1741 cache->meta_write_pointer = eb->start + eb->len;
1749 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1750 struct extent_buffer *eb)
1752 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1755 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1756 cache->meta_write_pointer = eb->start;
1759 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1761 if (!btrfs_dev_is_sequential(device, physical))
1764 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1765 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1768 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1769 struct blk_zone *zone)
1771 struct btrfs_io_context *bioc = NULL;
1772 u64 mapped_length = PAGE_SIZE;
1773 unsigned int nofs_flag;
1777 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1778 &mapped_length, &bioc);
1779 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1784 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1789 nofs_flag = memalloc_nofs_save();
1790 nmirrors = (int)bioc->num_stripes;
1791 for (i = 0; i < nmirrors; i++) {
1792 u64 physical = bioc->stripes[i].physical;
1793 struct btrfs_device *dev = bioc->stripes[i].dev;
1795 /* Missing device */
1799 ret = btrfs_get_dev_zone(dev, physical, zone);
1800 /* Failing device */
1801 if (ret == -EIO || ret == -EOPNOTSUPP)
1805 memalloc_nofs_restore(nofs_flag);
1807 btrfs_put_bioc(bioc);
1812 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1813 * filling zeros between @physical_pos to a write pointer of dev-replace
1816 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1817 u64 physical_start, u64 physical_pos)
1819 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1820 struct blk_zone zone;
1825 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1828 ret = read_zone_info(fs_info, logical, &zone);
1832 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1834 if (physical_pos == wp)
1837 if (physical_pos > wp)
1840 length = wp - physical_pos;
1841 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1844 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1845 u64 logical, u64 length)
1847 struct btrfs_device *device;
1848 struct extent_map *em;
1849 struct map_lookup *map;
1851 em = btrfs_get_chunk_map(fs_info, logical, length);
1853 return ERR_CAST(em);
1855 map = em->map_lookup;
1856 /* We only support single profile for now */
1857 device = map->stripes[0].dev;
1859 free_extent_map(em);
1865 * Activate block group and underlying device zones
1867 * @block_group: the block group to activate
1869 * Return: true on success, false otherwise
1871 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1873 struct btrfs_fs_info *fs_info = block_group->fs_info;
1874 struct btrfs_space_info *space_info = block_group->space_info;
1875 struct map_lookup *map;
1876 struct btrfs_device *device;
1881 if (!btrfs_is_zoned(block_group->fs_info))
1884 map = block_group->physical_map;
1886 spin_lock(&space_info->lock);
1887 spin_lock(&block_group->lock);
1888 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1894 if (btrfs_zoned_bg_is_full(block_group)) {
1899 for (i = 0; i < map->num_stripes; i++) {
1900 device = map->stripes[i].dev;
1901 physical = map->stripes[i].physical;
1903 if (device->zone_info->max_active_zones == 0)
1906 if (!btrfs_dev_set_active_zone(device, physical)) {
1907 /* Cannot activate the zone */
1913 /* Successfully activated all the zones */
1914 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
1915 space_info->active_total_bytes += block_group->length;
1916 spin_unlock(&block_group->lock);
1917 btrfs_try_granting_tickets(fs_info, space_info);
1918 spin_unlock(&space_info->lock);
1920 /* For the active block group list */
1921 btrfs_get_block_group(block_group);
1923 spin_lock(&fs_info->zone_active_bgs_lock);
1924 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1925 spin_unlock(&fs_info->zone_active_bgs_lock);
1930 spin_unlock(&block_group->lock);
1931 spin_unlock(&space_info->lock);
1935 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
1937 struct btrfs_fs_info *fs_info = block_group->fs_info;
1938 const u64 end = block_group->start + block_group->length;
1939 struct radix_tree_iter iter;
1940 struct extent_buffer *eb;
1944 radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
1945 block_group->start >> fs_info->sectorsize_bits) {
1946 eb = radix_tree_deref_slot(slot);
1949 if (radix_tree_deref_retry(eb)) {
1950 slot = radix_tree_iter_retry(&iter);
1954 if (eb->start < block_group->start)
1956 if (eb->start >= end)
1959 slot = radix_tree_iter_resume(slot, &iter);
1961 wait_on_extent_buffer_writeback(eb);
1967 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
1969 struct btrfs_fs_info *fs_info = block_group->fs_info;
1970 struct map_lookup *map;
1971 const bool is_metadata = (block_group->flags &
1972 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
1976 spin_lock(&block_group->lock);
1977 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1978 spin_unlock(&block_group->lock);
1982 /* Check if we have unwritten allocated space */
1984 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
1985 spin_unlock(&block_group->lock);
1990 * If we are sure that the block group is full (= no more room left for
1991 * new allocation) and the IO for the last usable block is completed, we
1992 * don't need to wait for the other IOs. This holds because we ensure
1993 * the sequential IO submissions using the ZONE_APPEND command for data
1994 * and block_group->meta_write_pointer for metadata.
1996 if (!fully_written) {
1997 spin_unlock(&block_group->lock);
1999 ret = btrfs_inc_block_group_ro(block_group, false);
2003 /* Ensure all writes in this block group finish */
2004 btrfs_wait_block_group_reservations(block_group);
2005 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
2006 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
2007 block_group->length);
2008 /* Wait for extent buffers to be written. */
2010 wait_eb_writebacks(block_group);
2012 spin_lock(&block_group->lock);
2015 * Bail out if someone already deactivated the block group, or
2016 * allocated space is left in the block group.
2018 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2019 &block_group->runtime_flags)) {
2020 spin_unlock(&block_group->lock);
2021 btrfs_dec_block_group_ro(block_group);
2025 if (block_group->reserved) {
2026 spin_unlock(&block_group->lock);
2027 btrfs_dec_block_group_ro(block_group);
2032 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2033 block_group->alloc_offset = block_group->zone_capacity;
2034 block_group->free_space_ctl->free_space = 0;
2035 btrfs_clear_treelog_bg(block_group);
2036 btrfs_clear_data_reloc_bg(block_group);
2037 spin_unlock(&block_group->lock);
2039 map = block_group->physical_map;
2040 for (i = 0; i < map->num_stripes; i++) {
2041 struct btrfs_device *device = map->stripes[i].dev;
2042 const u64 physical = map->stripes[i].physical;
2044 if (device->zone_info->max_active_zones == 0)
2047 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2048 physical >> SECTOR_SHIFT,
2049 device->zone_info->zone_size >> SECTOR_SHIFT,
2055 btrfs_dev_clear_active_zone(device, physical);
2059 btrfs_dec_block_group_ro(block_group);
2061 spin_lock(&fs_info->zone_active_bgs_lock);
2062 ASSERT(!list_empty(&block_group->active_bg_list));
2063 list_del_init(&block_group->active_bg_list);
2064 spin_unlock(&fs_info->zone_active_bgs_lock);
2066 /* For active_bg_list */
2067 btrfs_put_block_group(block_group);
2069 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2074 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2076 if (!btrfs_is_zoned(block_group->fs_info))
2079 return do_zone_finish(block_group, false);
2082 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2084 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2085 struct btrfs_device *device;
2088 if (!btrfs_is_zoned(fs_info))
2091 /* Check if there is a device with active zones left */
2092 mutex_lock(&fs_info->chunk_mutex);
2093 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2094 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2099 if (!zinfo->max_active_zones ||
2100 atomic_read(&zinfo->active_zones_left)) {
2105 mutex_unlock(&fs_info->chunk_mutex);
2108 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2113 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2115 struct btrfs_block_group *block_group;
2116 u64 min_alloc_bytes;
2118 if (!btrfs_is_zoned(fs_info))
2121 block_group = btrfs_lookup_block_group(fs_info, logical);
2122 ASSERT(block_group);
2124 /* No MIXED_BG on zoned btrfs. */
2125 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2126 min_alloc_bytes = fs_info->sectorsize;
2128 min_alloc_bytes = fs_info->nodesize;
2130 /* Bail out if we can allocate more data from this block group. */
2131 if (logical + length + min_alloc_bytes <=
2132 block_group->start + block_group->zone_capacity)
2135 do_zone_finish(block_group, true);
2138 btrfs_put_block_group(block_group);
2141 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2143 struct btrfs_block_group *bg =
2144 container_of(work, struct btrfs_block_group, zone_finish_work);
2146 wait_on_extent_buffer_writeback(bg->last_eb);
2147 free_extent_buffer(bg->last_eb);
2148 btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2149 btrfs_put_block_group(bg);
2152 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2153 struct extent_buffer *eb)
2155 if (!bg->seq_zone || eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2158 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2159 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2165 btrfs_get_block_group(bg);
2166 atomic_inc(&eb->refs);
2168 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2169 queue_work(system_unbound_wq, &bg->zone_finish_work);
2172 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2174 struct btrfs_fs_info *fs_info = bg->fs_info;
2176 spin_lock(&fs_info->relocation_bg_lock);
2177 if (fs_info->data_reloc_bg == bg->start)
2178 fs_info->data_reloc_bg = 0;
2179 spin_unlock(&fs_info->relocation_bg_lock);
2182 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2184 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2185 struct btrfs_device *device;
2187 if (!btrfs_is_zoned(fs_info))
2190 mutex_lock(&fs_devices->device_list_mutex);
2191 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2192 if (device->zone_info) {
2193 vfree(device->zone_info->zone_cache);
2194 device->zone_info->zone_cache = NULL;
2197 mutex_unlock(&fs_devices->device_list_mutex);
2200 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2202 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2203 struct btrfs_device *device;
2208 ASSERT(btrfs_is_zoned(fs_info));
2210 if (fs_info->bg_reclaim_threshold == 0)
2213 mutex_lock(&fs_devices->device_list_mutex);
2214 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2218 total += device->disk_total_bytes;
2219 used += device->bytes_used;
2221 mutex_unlock(&fs_devices->device_list_mutex);
2223 factor = div64_u64(used * 100, total);
2224 return factor >= fs_info->bg_reclaim_threshold;
2227 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2230 struct btrfs_block_group *block_group;
2232 if (!btrfs_is_zoned(fs_info))
2235 block_group = btrfs_lookup_block_group(fs_info, logical);
2236 /* It should be called on a previous data relocation block group. */
2237 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2239 spin_lock(&block_group->lock);
2240 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2243 /* All relocation extents are written. */
2244 if (block_group->start + block_group->alloc_offset == logical + length) {
2245 /* Now, release this block group for further allocations. */
2246 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2247 &block_group->runtime_flags);
2251 spin_unlock(&block_group->lock);
2252 btrfs_put_block_group(block_group);
2255 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2257 struct btrfs_block_group *block_group;
2258 struct btrfs_block_group *min_bg = NULL;
2259 u64 min_avail = U64_MAX;
2262 spin_lock(&fs_info->zone_active_bgs_lock);
2263 list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2267 spin_lock(&block_group->lock);
2268 if (block_group->reserved ||
2269 (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)) {
2270 spin_unlock(&block_group->lock);
2274 avail = block_group->zone_capacity - block_group->alloc_offset;
2275 if (min_avail > avail) {
2277 btrfs_put_block_group(min_bg);
2278 min_bg = block_group;
2280 btrfs_get_block_group(min_bg);
2282 spin_unlock(&block_group->lock);
2284 spin_unlock(&fs_info->zone_active_bgs_lock);
2289 ret = btrfs_zone_finish(min_bg);
2290 btrfs_put_block_group(min_bg);
2292 return ret < 0 ? ret : 1;
2295 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2296 struct btrfs_space_info *space_info,
2299 struct btrfs_block_group *bg;
2302 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2305 /* No more block groups to activate */
2306 if (space_info->active_total_bytes == space_info->total_bytes)
2311 bool need_finish = false;
2313 down_read(&space_info->groups_sem);
2314 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2315 list_for_each_entry(bg, &space_info->block_groups[index],
2317 if (!spin_trylock(&bg->lock))
2319 if (btrfs_zoned_bg_is_full(bg) ||
2320 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2321 &bg->runtime_flags)) {
2322 spin_unlock(&bg->lock);
2325 spin_unlock(&bg->lock);
2327 if (btrfs_zone_activate(bg)) {
2328 up_read(&space_info->groups_sem);
2335 up_read(&space_info->groups_sem);
2337 if (!do_finish || !need_finish)
2340 ret = btrfs_zone_finish_one_bg(fs_info);