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>
10 #include "rcu-string.h"
12 #include "block-group.h"
13 #include "transaction.h"
14 #include "dev-replace.h"
16 /* Maximum number of zones to report per blkdev_report_zones() call */
17 #define BTRFS_REPORT_NR_ZONES 4096
18 /* Invalid allocation pointer value for missing devices */
19 #define WP_MISSING_DEV ((u64)-1)
20 /* Pseudo write pointer value for conventional zone */
21 #define WP_CONVENTIONAL ((u64)-2)
23 /* Number of superblock log zones */
24 #define BTRFS_NR_SB_LOG_ZONES 2
26 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
28 struct blk_zone *zones = data;
30 memcpy(&zones[idx], zone, sizeof(*zone));
35 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
38 bool empty[BTRFS_NR_SB_LOG_ZONES];
39 bool full[BTRFS_NR_SB_LOG_ZONES];
42 ASSERT(zones[0].type != BLK_ZONE_TYPE_CONVENTIONAL &&
43 zones[1].type != BLK_ZONE_TYPE_CONVENTIONAL);
45 empty[0] = (zones[0].cond == BLK_ZONE_COND_EMPTY);
46 empty[1] = (zones[1].cond == BLK_ZONE_COND_EMPTY);
47 full[0] = (zones[0].cond == BLK_ZONE_COND_FULL);
48 full[1] = (zones[1].cond == BLK_ZONE_COND_FULL);
51 * Possible states of log buffer zones
53 * Empty[0] In use[0] Full[0]
59 * *: Special case, no superblock is written
60 * 0: Use write pointer of zones[0]
61 * 1: Use write pointer of zones[1]
62 * C: Compare super blcoks from zones[0] and zones[1], use the latest
63 * one determined by generation
67 if (empty[0] && empty[1]) {
68 /* Special case to distinguish no superblock to read */
69 *wp_ret = zones[0].start << SECTOR_SHIFT;
71 } else if (full[0] && full[1]) {
72 /* Compare two super blocks */
73 struct address_space *mapping = bdev->bd_inode->i_mapping;
74 struct page *page[BTRFS_NR_SB_LOG_ZONES];
75 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
78 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
81 bytenr = ((zones[i].start + zones[i].len)
82 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
84 page[i] = read_cache_page_gfp(mapping,
85 bytenr >> PAGE_SHIFT, GFP_NOFS);
86 if (IS_ERR(page[i])) {
88 btrfs_release_disk_super(super[0]);
89 return PTR_ERR(page[i]);
91 super[i] = page_address(page[i]);
94 if (super[0]->generation > super[1]->generation)
95 sector = zones[1].start;
97 sector = zones[0].start;
99 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
100 btrfs_release_disk_super(super[i]);
101 } else if (!full[0] && (empty[1] || full[1])) {
102 sector = zones[0].wp;
103 } else if (full[0]) {
104 sector = zones[1].wp;
108 *wp_ret = sector << SECTOR_SHIFT;
113 * The following zones are reserved as the circular buffer on ZONED btrfs.
114 * - The primary superblock: zones 0 and 1
115 * - The first copy: zones 16 and 17
116 * - The second copy: zones 1024 or zone at 256GB which is minimum, and
119 static inline u32 sb_zone_number(int shift, int mirror)
121 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
126 case 2: return min_t(u64, btrfs_sb_offset(mirror) >> shift, 1024);
133 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
134 * device into static sized chunks and fake a conventional zone on each of
137 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
138 struct blk_zone *zones, unsigned int nr_zones)
140 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
141 sector_t bdev_size = bdev_nr_sectors(device->bdev);
144 pos >>= SECTOR_SHIFT;
145 for (i = 0; i < nr_zones; i++) {
146 zones[i].start = i * zone_sectors + pos;
147 zones[i].len = zone_sectors;
148 zones[i].capacity = zone_sectors;
149 zones[i].wp = zones[i].start + zone_sectors;
150 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
151 zones[i].cond = BLK_ZONE_COND_NOT_WP;
153 if (zones[i].wp >= bdev_size) {
162 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
163 struct blk_zone *zones, unsigned int *nr_zones)
170 if (!bdev_is_zoned(device->bdev)) {
171 ret = emulate_report_zones(device, pos, zones, *nr_zones);
176 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
177 copy_zone_info_cb, zones);
179 btrfs_err_in_rcu(device->fs_info,
180 "zoned: failed to read zone %llu on %s (devid %llu)",
181 pos, rcu_str_deref(device->name),
192 /* The emulated zone size is determined from the size of device extent */
193 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
195 struct btrfs_path *path;
196 struct btrfs_root *root = fs_info->dev_root;
197 struct btrfs_key key;
198 struct extent_buffer *leaf;
199 struct btrfs_dev_extent *dext;
203 key.type = BTRFS_DEV_EXTENT_KEY;
206 path = btrfs_alloc_path();
210 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
214 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
215 ret = btrfs_next_item(root, path);
218 /* No dev extents at all? Not good */
225 leaf = path->nodes[0];
226 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
227 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
231 btrfs_free_path(path);
236 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
238 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
239 struct btrfs_device *device;
242 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
243 if (!btrfs_fs_incompat(fs_info, ZONED))
246 mutex_lock(&fs_devices->device_list_mutex);
247 list_for_each_entry(device, &fs_devices->devices, dev_list) {
248 /* We can skip reading of zone info for missing devices */
252 ret = btrfs_get_dev_zone_info(device);
256 mutex_unlock(&fs_devices->device_list_mutex);
261 int btrfs_get_dev_zone_info(struct btrfs_device *device)
263 struct btrfs_fs_info *fs_info = device->fs_info;
264 struct btrfs_zoned_device_info *zone_info = NULL;
265 struct block_device *bdev = device->bdev;
266 struct request_queue *queue = bdev_get_queue(bdev);
269 struct blk_zone *zones = NULL;
270 unsigned int i, nreported = 0, nr_zones;
271 unsigned int zone_sectors;
272 char *model, *emulated;
276 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
279 if (!btrfs_fs_incompat(fs_info, ZONED))
282 if (device->zone_info)
285 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
289 if (!bdev_is_zoned(bdev)) {
290 if (!fs_info->zone_size) {
291 ret = calculate_emulated_zone_size(fs_info);
296 ASSERT(fs_info->zone_size);
297 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
299 zone_sectors = bdev_zone_sectors(bdev);
302 nr_sectors = bdev_nr_sectors(bdev);
303 /* Check if it's power of 2 (see is_power_of_2) */
304 ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
305 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
306 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
307 zone_info->max_zone_append_size =
308 (u64)queue_max_zone_append_sectors(queue) << SECTOR_SHIFT;
309 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
310 if (!IS_ALIGNED(nr_sectors, zone_sectors))
311 zone_info->nr_zones++;
313 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
314 if (!zone_info->seq_zones) {
319 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
320 if (!zone_info->empty_zones) {
325 zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
332 while (sector < nr_sectors) {
333 nr_zones = BTRFS_REPORT_NR_ZONES;
334 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
339 for (i = 0; i < nr_zones; i++) {
340 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
341 __set_bit(nreported, zone_info->seq_zones);
342 if (zones[i].cond == BLK_ZONE_COND_EMPTY)
343 __set_bit(nreported, zone_info->empty_zones);
346 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
349 if (nreported != zone_info->nr_zones) {
350 btrfs_err_in_rcu(device->fs_info,
351 "inconsistent number of zones on %s (%u/%u)",
352 rcu_str_deref(device->name), nreported,
353 zone_info->nr_zones);
358 /* Validate superblock log */
359 nr_zones = BTRFS_NR_SB_LOG_ZONES;
360 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
363 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
365 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
366 if (sb_zone + 1 >= zone_info->nr_zones)
369 sector = sb_zone << (zone_info->zone_size_shift - SECTOR_SHIFT);
370 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT,
371 &zone_info->sb_zones[sb_pos],
376 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
377 btrfs_err_in_rcu(device->fs_info,
378 "zoned: failed to read super block log zone info at devid %llu zone %u",
379 device->devid, sb_zone);
385 * If zones[0] is conventional, always use the beggining of the
386 * zone to record superblock. No need to validate in that case.
388 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
389 BLK_ZONE_TYPE_CONVENTIONAL)
392 ret = sb_write_pointer(device->bdev,
393 &zone_info->sb_zones[sb_pos], &sb_wp);
394 if (ret != -ENOENT && ret) {
395 btrfs_err_in_rcu(device->fs_info,
396 "zoned: super block log zone corrupted devid %llu zone %u",
397 device->devid, sb_zone);
406 device->zone_info = zone_info;
408 switch (bdev_zoned_model(bdev)) {
410 model = "host-managed zoned";
414 model = "host-aware zoned";
419 emulated = "emulated ";
423 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
424 bdev_zoned_model(bdev),
425 rcu_str_deref(device->name));
427 goto out_free_zone_info;
430 btrfs_info_in_rcu(fs_info,
431 "%s block device %s, %u %szones of %llu bytes",
432 model, rcu_str_deref(device->name), zone_info->nr_zones,
433 emulated, zone_info->zone_size);
440 bitmap_free(zone_info->empty_zones);
441 bitmap_free(zone_info->seq_zones);
443 device->zone_info = NULL;
448 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
450 struct btrfs_zoned_device_info *zone_info = device->zone_info;
455 bitmap_free(zone_info->seq_zones);
456 bitmap_free(zone_info->empty_zones);
458 device->zone_info = NULL;
461 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
462 struct blk_zone *zone)
464 unsigned int nr_zones = 1;
467 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
468 if (ret != 0 || !nr_zones)
469 return ret ? ret : -EIO;
474 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
476 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
477 struct btrfs_device *device;
478 u64 zoned_devices = 0;
481 u64 max_zone_append_size = 0;
482 const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
485 /* Count zoned devices */
486 list_for_each_entry(device, &fs_devices->devices, dev_list) {
487 enum blk_zoned_model model;
492 model = bdev_zoned_model(device->bdev);
494 * A Host-Managed zoned device must be used as a zoned device.
495 * A Host-Aware zoned device and a non-zoned devices can be
496 * treated as a zoned device, if ZONED flag is enabled in the
499 if (model == BLK_ZONED_HM ||
500 (model == BLK_ZONED_HA && incompat_zoned) ||
501 (model == BLK_ZONED_NONE && incompat_zoned)) {
502 struct btrfs_zoned_device_info *zone_info =
505 zone_info = device->zone_info;
508 zone_size = zone_info->zone_size;
509 } else if (zone_info->zone_size != zone_size) {
511 "zoned: unequal block device zone sizes: have %llu found %llu",
512 device->zone_info->zone_size,
517 if (!max_zone_append_size ||
518 (zone_info->max_zone_append_size &&
519 zone_info->max_zone_append_size < max_zone_append_size))
520 max_zone_append_size =
521 zone_info->max_zone_append_size;
526 if (!zoned_devices && !incompat_zoned)
529 if (!zoned_devices && incompat_zoned) {
530 /* No zoned block device found on ZONED filesystem */
532 "zoned: no zoned devices found on a zoned filesystem");
537 if (zoned_devices && !incompat_zoned) {
539 "zoned: mode not enabled but zoned device found");
544 if (zoned_devices != nr_devices) {
546 "zoned: cannot mix zoned and regular devices");
552 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
553 * __btrfs_alloc_chunk(). Since we want stripe_len == zone_size,
554 * check the alignment here.
556 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
558 "zoned: zone size %llu not aligned to stripe %u",
559 zone_size, BTRFS_STRIPE_LEN);
564 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
565 btrfs_err(fs_info, "zoned: mixed block groups not supported");
570 fs_info->zone_size = zone_size;
571 fs_info->max_zone_append_size = max_zone_append_size;
572 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
575 * Check mount options here, because we might change fs_info->zoned
576 * from fs_info->zone_size.
578 ret = btrfs_check_mountopts_zoned(fs_info);
582 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
587 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
589 if (!btrfs_is_zoned(info))
593 * Space cache writing is not COWed. Disable that to avoid write errors
594 * in sequential zones.
596 if (btrfs_test_opt(info, SPACE_CACHE)) {
597 btrfs_err(info, "zoned: space cache v1 is not supported");
601 if (btrfs_test_opt(info, NODATACOW)) {
602 btrfs_err(info, "zoned: NODATACOW not supported");
609 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
610 int rw, u64 *bytenr_ret)
615 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
616 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
620 ret = sb_write_pointer(bdev, zones, &wp);
621 if (ret != -ENOENT && ret < 0)
625 struct blk_zone *reset = NULL;
627 if (wp == zones[0].start << SECTOR_SHIFT)
629 else if (wp == zones[1].start << SECTOR_SHIFT)
632 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
633 ASSERT(reset->cond == BLK_ZONE_COND_FULL);
635 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
636 reset->start, reset->len,
641 reset->cond = BLK_ZONE_COND_EMPTY;
642 reset->wp = reset->start;
644 } else if (ret != -ENOENT) {
645 /* For READ, we want the precious one */
646 if (wp == zones[0].start << SECTOR_SHIFT)
647 wp = (zones[1].start + zones[1].len) << SECTOR_SHIFT;
648 wp -= BTRFS_SUPER_INFO_SIZE;
656 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
659 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
660 unsigned int zone_sectors;
663 u8 zone_sectors_shift;
667 if (!bdev_is_zoned(bdev)) {
668 *bytenr_ret = btrfs_sb_offset(mirror);
672 ASSERT(rw == READ || rw == WRITE);
674 zone_sectors = bdev_zone_sectors(bdev);
675 if (!is_power_of_2(zone_sectors))
677 zone_sectors_shift = ilog2(zone_sectors);
678 nr_sectors = bdev_nr_sectors(bdev);
679 nr_zones = nr_sectors >> zone_sectors_shift;
681 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
682 if (sb_zone + 1 >= nr_zones)
685 ret = blkdev_report_zones(bdev, sb_zone << zone_sectors_shift,
686 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
690 if (ret != BTRFS_NR_SB_LOG_ZONES)
693 return sb_log_location(bdev, zones, rw, bytenr_ret);
696 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
699 struct btrfs_zoned_device_info *zinfo = device->zone_info;
703 * For a zoned filesystem on a non-zoned block device, use the same
704 * super block locations as regular filesystem. Doing so, the super
705 * block can always be retrieved and the zoned flag of the volume
706 * detected from the super block information.
708 if (!bdev_is_zoned(device->bdev)) {
709 *bytenr_ret = btrfs_sb_offset(mirror);
713 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
714 if (zone_num + 1 >= zinfo->nr_zones)
717 return sb_log_location(device->bdev,
718 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
722 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
730 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
731 if (zone_num + 1 >= zinfo->nr_zones)
734 if (!test_bit(zone_num, zinfo->seq_zones))
740 void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
742 struct btrfs_zoned_device_info *zinfo = device->zone_info;
743 struct blk_zone *zone;
745 if (!is_sb_log_zone(zinfo, mirror))
748 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
749 if (zone->cond != BLK_ZONE_COND_FULL) {
750 if (zone->cond == BLK_ZONE_COND_EMPTY)
751 zone->cond = BLK_ZONE_COND_IMP_OPEN;
753 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
755 if (zone->wp == zone->start + zone->len)
756 zone->cond = BLK_ZONE_COND_FULL;
762 ASSERT(zone->cond != BLK_ZONE_COND_FULL);
763 if (zone->cond == BLK_ZONE_COND_EMPTY)
764 zone->cond = BLK_ZONE_COND_IMP_OPEN;
766 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
768 if (zone->wp == zone->start + zone->len)
769 zone->cond = BLK_ZONE_COND_FULL;
772 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
774 sector_t zone_sectors;
776 u8 zone_sectors_shift;
780 zone_sectors = bdev_zone_sectors(bdev);
781 zone_sectors_shift = ilog2(zone_sectors);
782 nr_sectors = bdev_nr_sectors(bdev);
783 nr_zones = nr_sectors >> zone_sectors_shift;
785 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
786 if (sb_zone + 1 >= nr_zones)
789 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
790 sb_zone << zone_sectors_shift,
791 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
795 * btrfs_find_allocatable_zones - find allocatable zones within a given region
797 * @device: the device to allocate a region on
798 * @hole_start: the position of the hole to allocate the region
799 * @num_bytes: size of wanted region
800 * @hole_end: the end of the hole
801 * @return: position of allocatable zones
803 * Allocatable region should not contain any superblock locations.
805 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
806 u64 hole_end, u64 num_bytes)
808 struct btrfs_zoned_device_info *zinfo = device->zone_info;
809 const u8 shift = zinfo->zone_size_shift;
810 u64 nzones = num_bytes >> shift;
811 u64 pos = hole_start;
816 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
817 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
819 while (pos < hole_end) {
820 begin = pos >> shift;
821 end = begin + nzones;
823 if (end > zinfo->nr_zones)
826 /* Check if zones in the region are all empty */
827 if (btrfs_dev_is_sequential(device, pos) &&
828 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
829 pos += zinfo->zone_size;
834 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
838 sb_zone = sb_zone_number(shift, i);
839 if (!(end <= sb_zone ||
840 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
842 pos = ((u64)sb_zone + BTRFS_NR_SB_LOG_ZONES) << shift;
846 /* We also need to exclude regular superblock positions */
847 sb_pos = btrfs_sb_offset(i);
848 if (!(pos + num_bytes <= sb_pos ||
849 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
851 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
863 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
864 u64 length, u64 *bytes)
869 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
870 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
877 btrfs_dev_set_zone_empty(device, physical);
878 physical += device->zone_info->zone_size;
879 length -= device->zone_info->zone_size;
885 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
887 struct btrfs_zoned_device_info *zinfo = device->zone_info;
888 const u8 shift = zinfo->zone_size_shift;
889 unsigned long begin = start >> shift;
890 unsigned long end = (start + size) >> shift;
894 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
895 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
897 if (end > zinfo->nr_zones)
900 /* All the zones are conventional */
901 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
904 /* All the zones are sequential and empty */
905 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
906 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
909 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
912 if (!btrfs_dev_is_sequential(device, pos) ||
913 btrfs_dev_is_empty_zone(device, pos))
916 /* Free regions should be empty */
919 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
920 rcu_str_deref(device->name), device->devid, pos >> shift);
923 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
933 * Calculate an allocation pointer from the extent allocation information
934 * for a block group consist of conventional zones. It is pointed to the
935 * end of the highest addressed extent in the block group as an allocation
938 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
941 struct btrfs_fs_info *fs_info = cache->fs_info;
942 struct btrfs_root *root = fs_info->extent_root;
943 struct btrfs_path *path;
944 struct btrfs_key key;
945 struct btrfs_key found_key;
949 path = btrfs_alloc_path();
953 key.objectid = cache->start + cache->length;
957 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
958 /* We should not find the exact match */
964 ret = btrfs_previous_extent_item(root, path, cache->start);
973 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
975 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
976 length = found_key.offset;
978 length = fs_info->nodesize;
980 if (!(found_key.objectid >= cache->start &&
981 found_key.objectid + length <= cache->start + cache->length)) {
985 *offset_ret = found_key.objectid + length - cache->start;
989 btrfs_free_path(path);
993 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
995 struct btrfs_fs_info *fs_info = cache->fs_info;
996 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
997 struct extent_map *em;
998 struct map_lookup *map;
999 struct btrfs_device *device;
1000 u64 logical = cache->start;
1001 u64 length = cache->length;
1005 unsigned int nofs_flag;
1006 u64 *alloc_offsets = NULL;
1008 u32 num_sequential = 0, num_conventional = 0;
1010 if (!btrfs_is_zoned(fs_info))
1014 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1016 "zoned: block group %llu len %llu unaligned to zone size %llu",
1017 logical, length, fs_info->zone_size);
1021 /* Get the chunk mapping */
1022 read_lock(&em_tree->lock);
1023 em = lookup_extent_mapping(em_tree, logical, length);
1024 read_unlock(&em_tree->lock);
1029 map = em->map_lookup;
1031 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1032 if (!alloc_offsets) {
1033 free_extent_map(em);
1037 for (i = 0; i < map->num_stripes; i++) {
1039 struct blk_zone zone;
1040 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1041 int dev_replace_is_ongoing = 0;
1043 device = map->stripes[i].dev;
1044 physical = map->stripes[i].physical;
1046 if (device->bdev == NULL) {
1047 alloc_offsets[i] = WP_MISSING_DEV;
1051 is_sequential = btrfs_dev_is_sequential(device, physical);
1057 if (!is_sequential) {
1058 alloc_offsets[i] = WP_CONVENTIONAL;
1063 * This zone will be used for allocation, so mark this zone
1066 btrfs_dev_clear_zone_empty(device, physical);
1068 down_read(&dev_replace->rwsem);
1069 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1070 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1071 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical);
1072 up_read(&dev_replace->rwsem);
1075 * The group is mapped to a sequential zone. Get the zone write
1076 * pointer to determine the allocation offset within the zone.
1078 WARN_ON(!IS_ALIGNED(physical, fs_info->zone_size));
1079 nofs_flag = memalloc_nofs_save();
1080 ret = btrfs_get_dev_zone(device, physical, &zone);
1081 memalloc_nofs_restore(nofs_flag);
1082 if (ret == -EIO || ret == -EOPNOTSUPP) {
1084 alloc_offsets[i] = WP_MISSING_DEV;
1090 switch (zone.cond) {
1091 case BLK_ZONE_COND_OFFLINE:
1092 case BLK_ZONE_COND_READONLY:
1094 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1095 physical >> device->zone_info->zone_size_shift,
1096 rcu_str_deref(device->name), device->devid);
1097 alloc_offsets[i] = WP_MISSING_DEV;
1099 case BLK_ZONE_COND_EMPTY:
1100 alloc_offsets[i] = 0;
1102 case BLK_ZONE_COND_FULL:
1103 alloc_offsets[i] = fs_info->zone_size;
1106 /* Partially used zone */
1108 ((zone.wp - zone.start) << SECTOR_SHIFT);
1113 if (num_sequential > 0)
1114 cache->seq_zone = true;
1116 if (num_conventional > 0) {
1118 * Avoid calling calculate_alloc_pointer() for new BG. It
1119 * is no use for new BG. It must be always 0.
1121 * Also, we have a lock chain of extent buffer lock ->
1122 * chunk mutex. For new BG, this function is called from
1123 * btrfs_make_block_group() which is already taking the
1124 * chunk mutex. Thus, we cannot call
1125 * calculate_alloc_pointer() which takes extent buffer
1126 * locks to avoid deadlock.
1129 cache->alloc_offset = 0;
1132 ret = calculate_alloc_pointer(cache, &last_alloc);
1133 if (ret || map->num_stripes == num_conventional) {
1135 cache->alloc_offset = last_alloc;
1138 "zoned: failed to determine allocation offset of bg %llu",
1144 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1145 case 0: /* single */
1146 cache->alloc_offset = alloc_offsets[0];
1148 case BTRFS_BLOCK_GROUP_DUP:
1149 case BTRFS_BLOCK_GROUP_RAID1:
1150 case BTRFS_BLOCK_GROUP_RAID0:
1151 case BTRFS_BLOCK_GROUP_RAID10:
1152 case BTRFS_BLOCK_GROUP_RAID5:
1153 case BTRFS_BLOCK_GROUP_RAID6:
1154 /* non-single profiles are not supported yet */
1156 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1157 btrfs_bg_type_to_raid_name(map->type));
1163 /* An extent is allocated after the write pointer */
1164 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1166 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1167 logical, last_alloc, cache->alloc_offset);
1172 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1174 kfree(alloc_offsets);
1175 free_extent_map(em);
1180 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1184 if (!btrfs_is_zoned(cache->fs_info))
1187 WARN_ON(cache->bytes_super != 0);
1188 unusable = cache->alloc_offset - cache->used;
1189 free = cache->length - cache->alloc_offset;
1191 /* We only need ->free_space in ALLOC_SEQ block groups */
1192 cache->last_byte_to_unpin = (u64)-1;
1193 cache->cached = BTRFS_CACHE_FINISHED;
1194 cache->free_space_ctl->free_space = free;
1195 cache->zone_unusable = unusable;
1197 /* Should not have any excluded extents. Just in case, though */
1198 btrfs_free_excluded_extents(cache);
1201 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1202 struct extent_buffer *eb)
1204 struct btrfs_fs_info *fs_info = eb->fs_info;
1206 if (!btrfs_is_zoned(fs_info) ||
1207 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1208 !list_empty(&eb->release_list))
1211 set_extent_buffer_dirty(eb);
1212 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1213 eb->start + eb->len - 1, EXTENT_DIRTY);
1214 memzero_extent_buffer(eb, 0, eb->len);
1215 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1217 spin_lock(&trans->releasing_ebs_lock);
1218 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1219 spin_unlock(&trans->releasing_ebs_lock);
1220 atomic_inc(&eb->refs);
1223 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1225 spin_lock(&trans->releasing_ebs_lock);
1226 while (!list_empty(&trans->releasing_ebs)) {
1227 struct extent_buffer *eb;
1229 eb = list_first_entry(&trans->releasing_ebs,
1230 struct extent_buffer, release_list);
1231 list_del_init(&eb->release_list);
1232 free_extent_buffer(eb);
1234 spin_unlock(&trans->releasing_ebs_lock);
1237 bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em)
1239 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1240 struct btrfs_block_group *cache;
1243 if (!btrfs_is_zoned(fs_info))
1246 if (!fs_info->max_zone_append_size)
1249 if (!is_data_inode(&inode->vfs_inode))
1252 cache = btrfs_lookup_block_group(fs_info, em->block_start);
1257 ret = cache->seq_zone;
1258 btrfs_put_block_group(cache);
1263 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1266 struct btrfs_ordered_extent *ordered;
1267 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1269 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1272 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1273 if (WARN_ON(!ordered))
1276 ordered->physical = physical;
1277 ordered->disk = bio->bi_disk;
1278 ordered->partno = bio->bi_partno;
1280 btrfs_put_ordered_extent(ordered);
1283 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1285 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1286 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1287 struct extent_map_tree *em_tree;
1288 struct extent_map *em;
1289 struct btrfs_ordered_sum *sum;
1290 struct block_device *bdev;
1291 u64 orig_logical = ordered->disk_bytenr;
1292 u64 *logical = NULL;
1295 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1296 ASSERT(ordered->partno == 0);
1297 bdev = bdgrab(ordered->disk->part0);
1301 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, bdev,
1302 ordered->physical, &logical, &nr,
1308 if (orig_logical == *logical)
1311 ordered->disk_bytenr = *logical;
1313 em_tree = &inode->extent_tree;
1314 write_lock(&em_tree->lock);
1315 em = search_extent_mapping(em_tree, ordered->file_offset,
1316 ordered->num_bytes);
1317 em->block_start = *logical;
1318 free_extent_map(em);
1319 write_unlock(&em_tree->lock);
1321 list_for_each_entry(sum, &ordered->list, list) {
1322 if (*logical < orig_logical)
1323 sum->bytenr -= orig_logical - *logical;
1325 sum->bytenr += *logical - orig_logical;
1333 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1334 struct extent_buffer *eb,
1335 struct btrfs_block_group **cache_ret)
1337 struct btrfs_block_group *cache;
1340 if (!btrfs_is_zoned(fs_info))
1345 if (cache && (eb->start < cache->start ||
1346 cache->start + cache->length <= eb->start)) {
1347 btrfs_put_block_group(cache);
1353 cache = btrfs_lookup_block_group(fs_info, eb->start);
1356 if (cache->meta_write_pointer != eb->start) {
1357 btrfs_put_block_group(cache);
1361 cache->meta_write_pointer = eb->start + eb->len;
1370 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1371 struct extent_buffer *eb)
1373 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1376 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1377 cache->meta_write_pointer = eb->start;