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"
15 /* Maximum number of zones to report per blkdev_report_zones() call */
16 #define BTRFS_REPORT_NR_ZONES 4096
17 /* Invalid allocation pointer value for missing devices */
18 #define WP_MISSING_DEV ((u64)-1)
19 /* Pseudo write pointer value for conventional zone */
20 #define WP_CONVENTIONAL ((u64)-2)
22 /* Number of superblock log zones */
23 #define BTRFS_NR_SB_LOG_ZONES 2
25 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
27 struct blk_zone *zones = data;
29 memcpy(&zones[idx], zone, sizeof(*zone));
34 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
37 bool empty[BTRFS_NR_SB_LOG_ZONES];
38 bool full[BTRFS_NR_SB_LOG_ZONES];
41 ASSERT(zones[0].type != BLK_ZONE_TYPE_CONVENTIONAL &&
42 zones[1].type != BLK_ZONE_TYPE_CONVENTIONAL);
44 empty[0] = (zones[0].cond == BLK_ZONE_COND_EMPTY);
45 empty[1] = (zones[1].cond == BLK_ZONE_COND_EMPTY);
46 full[0] = (zones[0].cond == BLK_ZONE_COND_FULL);
47 full[1] = (zones[1].cond == BLK_ZONE_COND_FULL);
50 * Possible states of log buffer zones
52 * Empty[0] In use[0] Full[0]
58 * *: Special case, no superblock is written
59 * 0: Use write pointer of zones[0]
60 * 1: Use write pointer of zones[1]
61 * C: Compare super blcoks from zones[0] and zones[1], use the latest
62 * one determined by generation
66 if (empty[0] && empty[1]) {
67 /* Special case to distinguish no superblock to read */
68 *wp_ret = zones[0].start << SECTOR_SHIFT;
70 } else if (full[0] && full[1]) {
71 /* Compare two super blocks */
72 struct address_space *mapping = bdev->bd_inode->i_mapping;
73 struct page *page[BTRFS_NR_SB_LOG_ZONES];
74 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
77 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
80 bytenr = ((zones[i].start + zones[i].len)
81 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
83 page[i] = read_cache_page_gfp(mapping,
84 bytenr >> PAGE_SHIFT, GFP_NOFS);
85 if (IS_ERR(page[i])) {
87 btrfs_release_disk_super(super[0]);
88 return PTR_ERR(page[i]);
90 super[i] = page_address(page[i]);
93 if (super[0]->generation > super[1]->generation)
94 sector = zones[1].start;
96 sector = zones[0].start;
98 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
99 btrfs_release_disk_super(super[i]);
100 } else if (!full[0] && (empty[1] || full[1])) {
101 sector = zones[0].wp;
102 } else if (full[0]) {
103 sector = zones[1].wp;
107 *wp_ret = sector << SECTOR_SHIFT;
112 * The following zones are reserved as the circular buffer on ZONED btrfs.
113 * - The primary superblock: zones 0 and 1
114 * - The first copy: zones 16 and 17
115 * - The second copy: zones 1024 or zone at 256GB which is minimum, and
118 static inline u32 sb_zone_number(int shift, int mirror)
120 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
125 case 2: return min_t(u64, btrfs_sb_offset(mirror) >> shift, 1024);
132 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
133 * device into static sized chunks and fake a conventional zone on each of
136 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
137 struct blk_zone *zones, unsigned int nr_zones)
139 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
140 sector_t bdev_size = bdev_nr_sectors(device->bdev);
143 pos >>= SECTOR_SHIFT;
144 for (i = 0; i < nr_zones; i++) {
145 zones[i].start = i * zone_sectors + pos;
146 zones[i].len = zone_sectors;
147 zones[i].capacity = zone_sectors;
148 zones[i].wp = zones[i].start + zone_sectors;
149 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
150 zones[i].cond = BLK_ZONE_COND_NOT_WP;
152 if (zones[i].wp >= bdev_size) {
161 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
162 struct blk_zone *zones, unsigned int *nr_zones)
169 if (!bdev_is_zoned(device->bdev)) {
170 ret = emulate_report_zones(device, pos, zones, *nr_zones);
175 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
176 copy_zone_info_cb, zones);
178 btrfs_err_in_rcu(device->fs_info,
179 "zoned: failed to read zone %llu on %s (devid %llu)",
180 pos, rcu_str_deref(device->name),
191 /* The emulated zone size is determined from the size of device extent */
192 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
194 struct btrfs_path *path;
195 struct btrfs_root *root = fs_info->dev_root;
196 struct btrfs_key key;
197 struct extent_buffer *leaf;
198 struct btrfs_dev_extent *dext;
202 key.type = BTRFS_DEV_EXTENT_KEY;
205 path = btrfs_alloc_path();
209 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
213 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
214 ret = btrfs_next_item(root, path);
217 /* No dev extents at all? Not good */
224 leaf = path->nodes[0];
225 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
226 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
230 btrfs_free_path(path);
235 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
237 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
238 struct btrfs_device *device;
241 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
242 if (!btrfs_fs_incompat(fs_info, ZONED))
245 mutex_lock(&fs_devices->device_list_mutex);
246 list_for_each_entry(device, &fs_devices->devices, dev_list) {
247 /* We can skip reading of zone info for missing devices */
251 ret = btrfs_get_dev_zone_info(device);
255 mutex_unlock(&fs_devices->device_list_mutex);
260 int btrfs_get_dev_zone_info(struct btrfs_device *device)
262 struct btrfs_fs_info *fs_info = device->fs_info;
263 struct btrfs_zoned_device_info *zone_info = NULL;
264 struct block_device *bdev = device->bdev;
265 struct request_queue *queue = bdev_get_queue(bdev);
268 struct blk_zone *zones = NULL;
269 unsigned int i, nreported = 0, nr_zones;
270 unsigned int zone_sectors;
271 char *model, *emulated;
275 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
278 if (!btrfs_fs_incompat(fs_info, ZONED))
281 if (device->zone_info)
284 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
288 if (!bdev_is_zoned(bdev)) {
289 if (!fs_info->zone_size) {
290 ret = calculate_emulated_zone_size(fs_info);
295 ASSERT(fs_info->zone_size);
296 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
298 zone_sectors = bdev_zone_sectors(bdev);
301 nr_sectors = bdev_nr_sectors(bdev);
302 /* Check if it's power of 2 (see is_power_of_2) */
303 ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
304 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
305 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
306 zone_info->max_zone_append_size =
307 (u64)queue_max_zone_append_sectors(queue) << SECTOR_SHIFT;
308 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
309 if (!IS_ALIGNED(nr_sectors, zone_sectors))
310 zone_info->nr_zones++;
312 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
313 if (!zone_info->seq_zones) {
318 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
319 if (!zone_info->empty_zones) {
324 zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
331 while (sector < nr_sectors) {
332 nr_zones = BTRFS_REPORT_NR_ZONES;
333 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
338 for (i = 0; i < nr_zones; i++) {
339 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
340 __set_bit(nreported, zone_info->seq_zones);
341 if (zones[i].cond == BLK_ZONE_COND_EMPTY)
342 __set_bit(nreported, zone_info->empty_zones);
345 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
348 if (nreported != zone_info->nr_zones) {
349 btrfs_err_in_rcu(device->fs_info,
350 "inconsistent number of zones on %s (%u/%u)",
351 rcu_str_deref(device->name), nreported,
352 zone_info->nr_zones);
357 /* Validate superblock log */
358 nr_zones = BTRFS_NR_SB_LOG_ZONES;
359 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
362 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
364 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
365 if (sb_zone + 1 >= zone_info->nr_zones)
368 sector = sb_zone << (zone_info->zone_size_shift - SECTOR_SHIFT);
369 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT,
370 &zone_info->sb_zones[sb_pos],
375 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
376 btrfs_err_in_rcu(device->fs_info,
377 "zoned: failed to read super block log zone info at devid %llu zone %u",
378 device->devid, sb_zone);
384 * If zones[0] is conventional, always use the beggining of the
385 * zone to record superblock. No need to validate in that case.
387 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
388 BLK_ZONE_TYPE_CONVENTIONAL)
391 ret = sb_write_pointer(device->bdev,
392 &zone_info->sb_zones[sb_pos], &sb_wp);
393 if (ret != -ENOENT && ret) {
394 btrfs_err_in_rcu(device->fs_info,
395 "zoned: super block log zone corrupted devid %llu zone %u",
396 device->devid, sb_zone);
405 device->zone_info = zone_info;
407 switch (bdev_zoned_model(bdev)) {
409 model = "host-managed zoned";
413 model = "host-aware zoned";
418 emulated = "emulated ";
422 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
423 bdev_zoned_model(bdev),
424 rcu_str_deref(device->name));
426 goto out_free_zone_info;
429 btrfs_info_in_rcu(fs_info,
430 "%s block device %s, %u %szones of %llu bytes",
431 model, rcu_str_deref(device->name), zone_info->nr_zones,
432 emulated, zone_info->zone_size);
439 bitmap_free(zone_info->empty_zones);
440 bitmap_free(zone_info->seq_zones);
442 device->zone_info = NULL;
447 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
449 struct btrfs_zoned_device_info *zone_info = device->zone_info;
454 bitmap_free(zone_info->seq_zones);
455 bitmap_free(zone_info->empty_zones);
457 device->zone_info = NULL;
460 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
461 struct blk_zone *zone)
463 unsigned int nr_zones = 1;
466 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
467 if (ret != 0 || !nr_zones)
468 return ret ? ret : -EIO;
473 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
475 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
476 struct btrfs_device *device;
477 u64 zoned_devices = 0;
480 u64 max_zone_append_size = 0;
481 const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
484 /* Count zoned devices */
485 list_for_each_entry(device, &fs_devices->devices, dev_list) {
486 enum blk_zoned_model model;
491 model = bdev_zoned_model(device->bdev);
493 * A Host-Managed zoned device must be used as a zoned device.
494 * A Host-Aware zoned device and a non-zoned devices can be
495 * treated as a zoned device, if ZONED flag is enabled in the
498 if (model == BLK_ZONED_HM ||
499 (model == BLK_ZONED_HA && incompat_zoned) ||
500 (model == BLK_ZONED_NONE && incompat_zoned)) {
501 struct btrfs_zoned_device_info *zone_info =
504 zone_info = device->zone_info;
507 zone_size = zone_info->zone_size;
508 } else if (zone_info->zone_size != zone_size) {
510 "zoned: unequal block device zone sizes: have %llu found %llu",
511 device->zone_info->zone_size,
516 if (!max_zone_append_size ||
517 (zone_info->max_zone_append_size &&
518 zone_info->max_zone_append_size < max_zone_append_size))
519 max_zone_append_size =
520 zone_info->max_zone_append_size;
525 if (!zoned_devices && !incompat_zoned)
528 if (!zoned_devices && incompat_zoned) {
529 /* No zoned block device found on ZONED filesystem */
531 "zoned: no zoned devices found on a zoned filesystem");
536 if (zoned_devices && !incompat_zoned) {
538 "zoned: mode not enabled but zoned device found");
543 if (zoned_devices != nr_devices) {
545 "zoned: cannot mix zoned and regular devices");
551 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
552 * __btrfs_alloc_chunk(). Since we want stripe_len == zone_size,
553 * check the alignment here.
555 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
557 "zoned: zone size %llu not aligned to stripe %u",
558 zone_size, BTRFS_STRIPE_LEN);
563 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
564 btrfs_err(fs_info, "zoned: mixed block groups not supported");
569 fs_info->zone_size = zone_size;
570 fs_info->max_zone_append_size = max_zone_append_size;
571 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
574 * Check mount options here, because we might change fs_info->zoned
575 * from fs_info->zone_size.
577 ret = btrfs_check_mountopts_zoned(fs_info);
581 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
586 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
588 if (!btrfs_is_zoned(info))
592 * Space cache writing is not COWed. Disable that to avoid write errors
593 * in sequential zones.
595 if (btrfs_test_opt(info, SPACE_CACHE)) {
596 btrfs_err(info, "zoned: space cache v1 is not supported");
600 if (btrfs_test_opt(info, NODATACOW)) {
601 btrfs_err(info, "zoned: NODATACOW not supported");
608 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
609 int rw, u64 *bytenr_ret)
614 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
615 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
619 ret = sb_write_pointer(bdev, zones, &wp);
620 if (ret != -ENOENT && ret < 0)
624 struct blk_zone *reset = NULL;
626 if (wp == zones[0].start << SECTOR_SHIFT)
628 else if (wp == zones[1].start << SECTOR_SHIFT)
631 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
632 ASSERT(reset->cond == BLK_ZONE_COND_FULL);
634 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
635 reset->start, reset->len,
640 reset->cond = BLK_ZONE_COND_EMPTY;
641 reset->wp = reset->start;
643 } else if (ret != -ENOENT) {
644 /* For READ, we want the precious one */
645 if (wp == zones[0].start << SECTOR_SHIFT)
646 wp = (zones[1].start + zones[1].len) << SECTOR_SHIFT;
647 wp -= BTRFS_SUPER_INFO_SIZE;
655 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
658 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
659 unsigned int zone_sectors;
662 u8 zone_sectors_shift;
666 if (!bdev_is_zoned(bdev)) {
667 *bytenr_ret = btrfs_sb_offset(mirror);
671 ASSERT(rw == READ || rw == WRITE);
673 zone_sectors = bdev_zone_sectors(bdev);
674 if (!is_power_of_2(zone_sectors))
676 zone_sectors_shift = ilog2(zone_sectors);
677 nr_sectors = bdev_nr_sectors(bdev);
678 nr_zones = nr_sectors >> zone_sectors_shift;
680 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
681 if (sb_zone + 1 >= nr_zones)
684 ret = blkdev_report_zones(bdev, sb_zone << zone_sectors_shift,
685 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
689 if (ret != BTRFS_NR_SB_LOG_ZONES)
692 return sb_log_location(bdev, zones, rw, bytenr_ret);
695 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
698 struct btrfs_zoned_device_info *zinfo = device->zone_info;
702 * For a zoned filesystem on a non-zoned block device, use the same
703 * super block locations as regular filesystem. Doing so, the super
704 * block can always be retrieved and the zoned flag of the volume
705 * detected from the super block information.
707 if (!bdev_is_zoned(device->bdev)) {
708 *bytenr_ret = btrfs_sb_offset(mirror);
712 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
713 if (zone_num + 1 >= zinfo->nr_zones)
716 return sb_log_location(device->bdev,
717 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
721 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
729 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
730 if (zone_num + 1 >= zinfo->nr_zones)
733 if (!test_bit(zone_num, zinfo->seq_zones))
739 void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
741 struct btrfs_zoned_device_info *zinfo = device->zone_info;
742 struct blk_zone *zone;
744 if (!is_sb_log_zone(zinfo, mirror))
747 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
748 if (zone->cond != BLK_ZONE_COND_FULL) {
749 if (zone->cond == BLK_ZONE_COND_EMPTY)
750 zone->cond = BLK_ZONE_COND_IMP_OPEN;
752 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
754 if (zone->wp == zone->start + zone->len)
755 zone->cond = BLK_ZONE_COND_FULL;
761 ASSERT(zone->cond != BLK_ZONE_COND_FULL);
762 if (zone->cond == BLK_ZONE_COND_EMPTY)
763 zone->cond = BLK_ZONE_COND_IMP_OPEN;
765 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
767 if (zone->wp == zone->start + zone->len)
768 zone->cond = BLK_ZONE_COND_FULL;
771 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
773 sector_t zone_sectors;
775 u8 zone_sectors_shift;
779 zone_sectors = bdev_zone_sectors(bdev);
780 zone_sectors_shift = ilog2(zone_sectors);
781 nr_sectors = bdev_nr_sectors(bdev);
782 nr_zones = nr_sectors >> zone_sectors_shift;
784 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
785 if (sb_zone + 1 >= nr_zones)
788 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
789 sb_zone << zone_sectors_shift,
790 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
794 * btrfs_find_allocatable_zones - find allocatable zones within a given region
796 * @device: the device to allocate a region on
797 * @hole_start: the position of the hole to allocate the region
798 * @num_bytes: size of wanted region
799 * @hole_end: the end of the hole
800 * @return: position of allocatable zones
802 * Allocatable region should not contain any superblock locations.
804 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
805 u64 hole_end, u64 num_bytes)
807 struct btrfs_zoned_device_info *zinfo = device->zone_info;
808 const u8 shift = zinfo->zone_size_shift;
809 u64 nzones = num_bytes >> shift;
810 u64 pos = hole_start;
815 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
816 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
818 while (pos < hole_end) {
819 begin = pos >> shift;
820 end = begin + nzones;
822 if (end > zinfo->nr_zones)
825 /* Check if zones in the region are all empty */
826 if (btrfs_dev_is_sequential(device, pos) &&
827 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
828 pos += zinfo->zone_size;
833 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
837 sb_zone = sb_zone_number(shift, i);
838 if (!(end <= sb_zone ||
839 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
841 pos = ((u64)sb_zone + BTRFS_NR_SB_LOG_ZONES) << shift;
845 /* We also need to exclude regular superblock positions */
846 sb_pos = btrfs_sb_offset(i);
847 if (!(pos + num_bytes <= sb_pos ||
848 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
850 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
862 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
863 u64 length, u64 *bytes)
868 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
869 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
876 btrfs_dev_set_zone_empty(device, physical);
877 physical += device->zone_info->zone_size;
878 length -= device->zone_info->zone_size;
884 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
886 struct btrfs_zoned_device_info *zinfo = device->zone_info;
887 const u8 shift = zinfo->zone_size_shift;
888 unsigned long begin = start >> shift;
889 unsigned long end = (start + size) >> shift;
893 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
894 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
896 if (end > zinfo->nr_zones)
899 /* All the zones are conventional */
900 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
903 /* All the zones are sequential and empty */
904 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
905 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
908 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
911 if (!btrfs_dev_is_sequential(device, pos) ||
912 btrfs_dev_is_empty_zone(device, pos))
915 /* Free regions should be empty */
918 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
919 rcu_str_deref(device->name), device->devid, pos >> shift);
922 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
932 * Calculate an allocation pointer from the extent allocation information
933 * for a block group consist of conventional zones. It is pointed to the
934 * end of the highest addressed extent in the block group as an allocation
937 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
940 struct btrfs_fs_info *fs_info = cache->fs_info;
941 struct btrfs_root *root = fs_info->extent_root;
942 struct btrfs_path *path;
943 struct btrfs_key key;
944 struct btrfs_key found_key;
948 path = btrfs_alloc_path();
952 key.objectid = cache->start + cache->length;
956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
957 /* We should not find the exact match */
963 ret = btrfs_previous_extent_item(root, path, cache->start);
972 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
974 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
975 length = found_key.offset;
977 length = fs_info->nodesize;
979 if (!(found_key.objectid >= cache->start &&
980 found_key.objectid + length <= cache->start + cache->length)) {
984 *offset_ret = found_key.objectid + length - cache->start;
988 btrfs_free_path(path);
992 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
994 struct btrfs_fs_info *fs_info = cache->fs_info;
995 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
996 struct extent_map *em;
997 struct map_lookup *map;
998 struct btrfs_device *device;
999 u64 logical = cache->start;
1000 u64 length = cache->length;
1004 unsigned int nofs_flag;
1005 u64 *alloc_offsets = NULL;
1007 u32 num_sequential = 0, num_conventional = 0;
1009 if (!btrfs_is_zoned(fs_info))
1013 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1015 "zoned: block group %llu len %llu unaligned to zone size %llu",
1016 logical, length, fs_info->zone_size);
1020 /* Get the chunk mapping */
1021 read_lock(&em_tree->lock);
1022 em = lookup_extent_mapping(em_tree, logical, length);
1023 read_unlock(&em_tree->lock);
1028 map = em->map_lookup;
1030 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1031 if (!alloc_offsets) {
1032 free_extent_map(em);
1036 for (i = 0; i < map->num_stripes; i++) {
1038 struct blk_zone zone;
1040 device = map->stripes[i].dev;
1041 physical = map->stripes[i].physical;
1043 if (device->bdev == NULL) {
1044 alloc_offsets[i] = WP_MISSING_DEV;
1048 is_sequential = btrfs_dev_is_sequential(device, physical);
1054 if (!is_sequential) {
1055 alloc_offsets[i] = WP_CONVENTIONAL;
1060 * This zone will be used for allocation, so mark this zone
1063 btrfs_dev_clear_zone_empty(device, physical);
1066 * The group is mapped to a sequential zone. Get the zone write
1067 * pointer to determine the allocation offset within the zone.
1069 WARN_ON(!IS_ALIGNED(physical, fs_info->zone_size));
1070 nofs_flag = memalloc_nofs_save();
1071 ret = btrfs_get_dev_zone(device, physical, &zone);
1072 memalloc_nofs_restore(nofs_flag);
1073 if (ret == -EIO || ret == -EOPNOTSUPP) {
1075 alloc_offsets[i] = WP_MISSING_DEV;
1081 switch (zone.cond) {
1082 case BLK_ZONE_COND_OFFLINE:
1083 case BLK_ZONE_COND_READONLY:
1085 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1086 physical >> device->zone_info->zone_size_shift,
1087 rcu_str_deref(device->name), device->devid);
1088 alloc_offsets[i] = WP_MISSING_DEV;
1090 case BLK_ZONE_COND_EMPTY:
1091 alloc_offsets[i] = 0;
1093 case BLK_ZONE_COND_FULL:
1094 alloc_offsets[i] = fs_info->zone_size;
1097 /* Partially used zone */
1099 ((zone.wp - zone.start) << SECTOR_SHIFT);
1104 if (num_sequential > 0)
1105 cache->seq_zone = true;
1107 if (num_conventional > 0) {
1109 * Avoid calling calculate_alloc_pointer() for new BG. It
1110 * is no use for new BG. It must be always 0.
1112 * Also, we have a lock chain of extent buffer lock ->
1113 * chunk mutex. For new BG, this function is called from
1114 * btrfs_make_block_group() which is already taking the
1115 * chunk mutex. Thus, we cannot call
1116 * calculate_alloc_pointer() which takes extent buffer
1117 * locks to avoid deadlock.
1120 cache->alloc_offset = 0;
1123 ret = calculate_alloc_pointer(cache, &last_alloc);
1124 if (ret || map->num_stripes == num_conventional) {
1126 cache->alloc_offset = last_alloc;
1129 "zoned: failed to determine allocation offset of bg %llu",
1135 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1136 case 0: /* single */
1137 cache->alloc_offset = alloc_offsets[0];
1139 case BTRFS_BLOCK_GROUP_DUP:
1140 case BTRFS_BLOCK_GROUP_RAID1:
1141 case BTRFS_BLOCK_GROUP_RAID0:
1142 case BTRFS_BLOCK_GROUP_RAID10:
1143 case BTRFS_BLOCK_GROUP_RAID5:
1144 case BTRFS_BLOCK_GROUP_RAID6:
1145 /* non-single profiles are not supported yet */
1147 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1148 btrfs_bg_type_to_raid_name(map->type));
1154 /* An extent is allocated after the write pointer */
1155 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1157 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1158 logical, last_alloc, cache->alloc_offset);
1163 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1165 kfree(alloc_offsets);
1166 free_extent_map(em);
1171 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1175 if (!btrfs_is_zoned(cache->fs_info))
1178 WARN_ON(cache->bytes_super != 0);
1179 unusable = cache->alloc_offset - cache->used;
1180 free = cache->length - cache->alloc_offset;
1182 /* We only need ->free_space in ALLOC_SEQ block groups */
1183 cache->last_byte_to_unpin = (u64)-1;
1184 cache->cached = BTRFS_CACHE_FINISHED;
1185 cache->free_space_ctl->free_space = free;
1186 cache->zone_unusable = unusable;
1188 /* Should not have any excluded extents. Just in case, though */
1189 btrfs_free_excluded_extents(cache);
1192 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1193 struct extent_buffer *eb)
1195 struct btrfs_fs_info *fs_info = eb->fs_info;
1197 if (!btrfs_is_zoned(fs_info) ||
1198 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1199 !list_empty(&eb->release_list))
1202 set_extent_buffer_dirty(eb);
1203 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1204 eb->start + eb->len - 1, EXTENT_DIRTY);
1205 memzero_extent_buffer(eb, 0, eb->len);
1206 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1208 spin_lock(&trans->releasing_ebs_lock);
1209 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1210 spin_unlock(&trans->releasing_ebs_lock);
1211 atomic_inc(&eb->refs);
1214 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1216 spin_lock(&trans->releasing_ebs_lock);
1217 while (!list_empty(&trans->releasing_ebs)) {
1218 struct extent_buffer *eb;
1220 eb = list_first_entry(&trans->releasing_ebs,
1221 struct extent_buffer, release_list);
1222 list_del_init(&eb->release_list);
1223 free_extent_buffer(eb);
1225 spin_unlock(&trans->releasing_ebs_lock);
1228 bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em)
1230 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1231 struct btrfs_block_group *cache;
1234 if (!btrfs_is_zoned(fs_info))
1237 if (!fs_info->max_zone_append_size)
1240 if (!is_data_inode(&inode->vfs_inode))
1243 cache = btrfs_lookup_block_group(fs_info, em->block_start);
1248 ret = cache->seq_zone;
1249 btrfs_put_block_group(cache);
1254 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1257 struct btrfs_ordered_extent *ordered;
1258 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1260 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1263 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1264 if (WARN_ON(!ordered))
1267 ordered->physical = physical;
1268 ordered->disk = bio->bi_disk;
1269 ordered->partno = bio->bi_partno;
1271 btrfs_put_ordered_extent(ordered);
1274 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1276 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1277 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1278 struct extent_map_tree *em_tree;
1279 struct extent_map *em;
1280 struct btrfs_ordered_sum *sum;
1281 struct block_device *bdev;
1282 u64 orig_logical = ordered->disk_bytenr;
1283 u64 *logical = NULL;
1286 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1287 ASSERT(ordered->partno == 0);
1288 bdev = bdgrab(ordered->disk->part0);
1292 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, bdev,
1293 ordered->physical, &logical, &nr,
1299 if (orig_logical == *logical)
1302 ordered->disk_bytenr = *logical;
1304 em_tree = &inode->extent_tree;
1305 write_lock(&em_tree->lock);
1306 em = search_extent_mapping(em_tree, ordered->file_offset,
1307 ordered->num_bytes);
1308 em->block_start = *logical;
1309 free_extent_map(em);
1310 write_unlock(&em_tree->lock);
1312 list_for_each_entry(sum, &ordered->list, list) {
1313 if (*logical < orig_logical)
1314 sum->bytenr -= orig_logical - *logical;
1316 sum->bytenr += *logical - orig_logical;
1324 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1325 struct extent_buffer *eb,
1326 struct btrfs_block_group **cache_ret)
1328 struct btrfs_block_group *cache;
1331 if (!btrfs_is_zoned(fs_info))
1336 if (cache && (eb->start < cache->start ||
1337 cache->start + cache->length <= eb->start)) {
1338 btrfs_put_block_group(cache);
1344 cache = btrfs_lookup_block_group(fs_info, eb->start);
1347 if (cache->meta_write_pointer != eb->start) {
1348 btrfs_put_block_group(cache);
1352 cache->meta_write_pointer = eb->start + eb->len;
1361 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1362 struct extent_buffer *eb)
1364 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1367 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1368 cache->meta_write_pointer = eb->start;