Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/bitops.h>
0004 #include <linux/slab.h>
0005 #include <linux/blkdev.h>
0006 #include <linux/sched/mm.h>
0007 #include <linux/atomic.h>
0008 #include <linux/vmalloc.h>
0009 #include "ctree.h"
0010 #include "volumes.h"
0011 #include "zoned.h"
0012 #include "rcu-string.h"
0013 #include "disk-io.h"
0014 #include "block-group.h"
0015 #include "transaction.h"
0016 #include "dev-replace.h"
0017 #include "space-info.h"
0018 
0019 /* Maximum number of zones to report per blkdev_report_zones() call */
0020 #define BTRFS_REPORT_NR_ZONES   4096
0021 /* Invalid allocation pointer value for missing devices */
0022 #define WP_MISSING_DEV ((u64)-1)
0023 /* Pseudo write pointer value for conventional zone */
0024 #define WP_CONVENTIONAL ((u64)-2)
0025 
0026 /*
0027  * Location of the first zone of superblock logging zone pairs.
0028  *
0029  * - primary superblock:    0B (zone 0)
0030  * - first copy:          512G (zone starting at that offset)
0031  * - second copy:           4T (zone starting at that offset)
0032  */
0033 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
0034 #define BTRFS_SB_LOG_FIRST_OFFSET   (512ULL * SZ_1G)
0035 #define BTRFS_SB_LOG_SECOND_OFFSET  (4096ULL * SZ_1G)
0036 
0037 #define BTRFS_SB_LOG_FIRST_SHIFT    const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
0038 #define BTRFS_SB_LOG_SECOND_SHIFT   const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
0039 
0040 /* Number of superblock log zones */
0041 #define BTRFS_NR_SB_LOG_ZONES 2
0042 
0043 /*
0044  * Minimum of active zones we need:
0045  *
0046  * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
0047  * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
0048  * - 1 zone for tree-log dedicated block group
0049  * - 1 zone for relocation
0050  */
0051 #define BTRFS_MIN_ACTIVE_ZONES      (BTRFS_SUPER_MIRROR_MAX + 5)
0052 
0053 /*
0054  * Minimum / maximum supported zone size. Currently, SMR disks have a zone
0055  * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
0056  * We do not expect the zone size to become larger than 8GiB or smaller than
0057  * 4MiB in the near future.
0058  */
0059 #define BTRFS_MAX_ZONE_SIZE     SZ_8G
0060 #define BTRFS_MIN_ZONE_SIZE     SZ_4M
0061 
0062 #define SUPER_INFO_SECTORS  ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
0063 
0064 static inline bool sb_zone_is_full(const struct blk_zone *zone)
0065 {
0066     return (zone->cond == BLK_ZONE_COND_FULL) ||
0067         (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
0068 }
0069 
0070 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
0071 {
0072     struct blk_zone *zones = data;
0073 
0074     memcpy(&zones[idx], zone, sizeof(*zone));
0075 
0076     return 0;
0077 }
0078 
0079 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
0080                 u64 *wp_ret)
0081 {
0082     bool empty[BTRFS_NR_SB_LOG_ZONES];
0083     bool full[BTRFS_NR_SB_LOG_ZONES];
0084     sector_t sector;
0085     int i;
0086 
0087     for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
0088         ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
0089         empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
0090         full[i] = sb_zone_is_full(&zones[i]);
0091     }
0092 
0093     /*
0094      * Possible states of log buffer zones
0095      *
0096      *           Empty[0]  In use[0]  Full[0]
0097      * Empty[1]         *          0        1
0098      * In use[1]        x          x        1
0099      * Full[1]          0          0        C
0100      *
0101      * Log position:
0102      *   *: Special case, no superblock is written
0103      *   0: Use write pointer of zones[0]
0104      *   1: Use write pointer of zones[1]
0105      *   C: Compare super blocks from zones[0] and zones[1], use the latest
0106      *      one determined by generation
0107      *   x: Invalid state
0108      */
0109 
0110     if (empty[0] && empty[1]) {
0111         /* Special case to distinguish no superblock to read */
0112         *wp_ret = zones[0].start << SECTOR_SHIFT;
0113         return -ENOENT;
0114     } else if (full[0] && full[1]) {
0115         /* Compare two super blocks */
0116         struct address_space *mapping = bdev->bd_inode->i_mapping;
0117         struct page *page[BTRFS_NR_SB_LOG_ZONES];
0118         struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
0119         int i;
0120 
0121         for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
0122             u64 bytenr;
0123 
0124             bytenr = ((zones[i].start + zones[i].len)
0125                    << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
0126 
0127             page[i] = read_cache_page_gfp(mapping,
0128                     bytenr >> PAGE_SHIFT, GFP_NOFS);
0129             if (IS_ERR(page[i])) {
0130                 if (i == 1)
0131                     btrfs_release_disk_super(super[0]);
0132                 return PTR_ERR(page[i]);
0133             }
0134             super[i] = page_address(page[i]);
0135         }
0136 
0137         if (super[0]->generation > super[1]->generation)
0138             sector = zones[1].start;
0139         else
0140             sector = zones[0].start;
0141 
0142         for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
0143             btrfs_release_disk_super(super[i]);
0144     } else if (!full[0] && (empty[1] || full[1])) {
0145         sector = zones[0].wp;
0146     } else if (full[0]) {
0147         sector = zones[1].wp;
0148     } else {
0149         return -EUCLEAN;
0150     }
0151     *wp_ret = sector << SECTOR_SHIFT;
0152     return 0;
0153 }
0154 
0155 /*
0156  * Get the first zone number of the superblock mirror
0157  */
0158 static inline u32 sb_zone_number(int shift, int mirror)
0159 {
0160     u64 zone;
0161 
0162     ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
0163     switch (mirror) {
0164     case 0: zone = 0; break;
0165     case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
0166     case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
0167     }
0168 
0169     ASSERT(zone <= U32_MAX);
0170 
0171     return (u32)zone;
0172 }
0173 
0174 static inline sector_t zone_start_sector(u32 zone_number,
0175                      struct block_device *bdev)
0176 {
0177     return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
0178 }
0179 
0180 static inline u64 zone_start_physical(u32 zone_number,
0181                       struct btrfs_zoned_device_info *zone_info)
0182 {
0183     return (u64)zone_number << zone_info->zone_size_shift;
0184 }
0185 
0186 /*
0187  * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
0188  * device into static sized chunks and fake a conventional zone on each of
0189  * them.
0190  */
0191 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
0192                 struct blk_zone *zones, unsigned int nr_zones)
0193 {
0194     const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
0195     sector_t bdev_size = bdev_nr_sectors(device->bdev);
0196     unsigned int i;
0197 
0198     pos >>= SECTOR_SHIFT;
0199     for (i = 0; i < nr_zones; i++) {
0200         zones[i].start = i * zone_sectors + pos;
0201         zones[i].len = zone_sectors;
0202         zones[i].capacity = zone_sectors;
0203         zones[i].wp = zones[i].start + zone_sectors;
0204         zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
0205         zones[i].cond = BLK_ZONE_COND_NOT_WP;
0206 
0207         if (zones[i].wp >= bdev_size) {
0208             i++;
0209             break;
0210         }
0211     }
0212 
0213     return i;
0214 }
0215 
0216 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
0217                    struct blk_zone *zones, unsigned int *nr_zones)
0218 {
0219     struct btrfs_zoned_device_info *zinfo = device->zone_info;
0220     u32 zno;
0221     int ret;
0222 
0223     if (!*nr_zones)
0224         return 0;
0225 
0226     if (!bdev_is_zoned(device->bdev)) {
0227         ret = emulate_report_zones(device, pos, zones, *nr_zones);
0228         *nr_zones = ret;
0229         return 0;
0230     }
0231 
0232     /* Check cache */
0233     if (zinfo->zone_cache) {
0234         unsigned int i;
0235 
0236         ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
0237         zno = pos >> zinfo->zone_size_shift;
0238         /*
0239          * We cannot report zones beyond the zone end. So, it is OK to
0240          * cap *nr_zones to at the end.
0241          */
0242         *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
0243 
0244         for (i = 0; i < *nr_zones; i++) {
0245             struct blk_zone *zone_info;
0246 
0247             zone_info = &zinfo->zone_cache[zno + i];
0248             if (!zone_info->len)
0249                 break;
0250         }
0251 
0252         if (i == *nr_zones) {
0253             /* Cache hit on all the zones */
0254             memcpy(zones, zinfo->zone_cache + zno,
0255                    sizeof(*zinfo->zone_cache) * *nr_zones);
0256             return 0;
0257         }
0258     }
0259 
0260     ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
0261                   copy_zone_info_cb, zones);
0262     if (ret < 0) {
0263         btrfs_err_in_rcu(device->fs_info,
0264                  "zoned: failed to read zone %llu on %s (devid %llu)",
0265                  pos, rcu_str_deref(device->name),
0266                  device->devid);
0267         return ret;
0268     }
0269     *nr_zones = ret;
0270     if (!ret)
0271         return -EIO;
0272 
0273     /* Populate cache */
0274     if (zinfo->zone_cache)
0275         memcpy(zinfo->zone_cache + zno, zones,
0276                sizeof(*zinfo->zone_cache) * *nr_zones);
0277 
0278     return 0;
0279 }
0280 
0281 /* The emulated zone size is determined from the size of device extent */
0282 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
0283 {
0284     struct btrfs_path *path;
0285     struct btrfs_root *root = fs_info->dev_root;
0286     struct btrfs_key key;
0287     struct extent_buffer *leaf;
0288     struct btrfs_dev_extent *dext;
0289     int ret = 0;
0290 
0291     key.objectid = 1;
0292     key.type = BTRFS_DEV_EXTENT_KEY;
0293     key.offset = 0;
0294 
0295     path = btrfs_alloc_path();
0296     if (!path)
0297         return -ENOMEM;
0298 
0299     ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0300     if (ret < 0)
0301         goto out;
0302 
0303     if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
0304         ret = btrfs_next_leaf(root, path);
0305         if (ret < 0)
0306             goto out;
0307         /* No dev extents at all? Not good */
0308         if (ret > 0) {
0309             ret = -EUCLEAN;
0310             goto out;
0311         }
0312     }
0313 
0314     leaf = path->nodes[0];
0315     dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
0316     fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
0317     ret = 0;
0318 
0319 out:
0320     btrfs_free_path(path);
0321 
0322     return ret;
0323 }
0324 
0325 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
0326 {
0327     struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
0328     struct btrfs_device *device;
0329     int ret = 0;
0330 
0331     /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
0332     if (!btrfs_fs_incompat(fs_info, ZONED))
0333         return 0;
0334 
0335     mutex_lock(&fs_devices->device_list_mutex);
0336     list_for_each_entry(device, &fs_devices->devices, dev_list) {
0337         /* We can skip reading of zone info for missing devices */
0338         if (!device->bdev)
0339             continue;
0340 
0341         ret = btrfs_get_dev_zone_info(device, true);
0342         if (ret)
0343             break;
0344     }
0345     mutex_unlock(&fs_devices->device_list_mutex);
0346 
0347     return ret;
0348 }
0349 
0350 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
0351 {
0352     struct btrfs_fs_info *fs_info = device->fs_info;
0353     struct btrfs_zoned_device_info *zone_info = NULL;
0354     struct block_device *bdev = device->bdev;
0355     unsigned int max_active_zones;
0356     unsigned int nactive;
0357     sector_t nr_sectors;
0358     sector_t sector = 0;
0359     struct blk_zone *zones = NULL;
0360     unsigned int i, nreported = 0, nr_zones;
0361     sector_t zone_sectors;
0362     char *model, *emulated;
0363     int ret;
0364 
0365     /*
0366      * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
0367      * yet be set.
0368      */
0369     if (!btrfs_fs_incompat(fs_info, ZONED))
0370         return 0;
0371 
0372     if (device->zone_info)
0373         return 0;
0374 
0375     zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
0376     if (!zone_info)
0377         return -ENOMEM;
0378 
0379     device->zone_info = zone_info;
0380 
0381     if (!bdev_is_zoned(bdev)) {
0382         if (!fs_info->zone_size) {
0383             ret = calculate_emulated_zone_size(fs_info);
0384             if (ret)
0385                 goto out;
0386         }
0387 
0388         ASSERT(fs_info->zone_size);
0389         zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
0390     } else {
0391         zone_sectors = bdev_zone_sectors(bdev);
0392     }
0393 
0394     /* Check if it's power of 2 (see is_power_of_2) */
0395     ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
0396     zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
0397 
0398     /* We reject devices with a zone size larger than 8GB */
0399     if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
0400         btrfs_err_in_rcu(fs_info,
0401         "zoned: %s: zone size %llu larger than supported maximum %llu",
0402                  rcu_str_deref(device->name),
0403                  zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
0404         ret = -EINVAL;
0405         goto out;
0406     } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
0407         btrfs_err_in_rcu(fs_info,
0408         "zoned: %s: zone size %llu smaller than supported minimum %u",
0409                  rcu_str_deref(device->name),
0410                  zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
0411         ret = -EINVAL;
0412         goto out;
0413     }
0414 
0415     nr_sectors = bdev_nr_sectors(bdev);
0416     zone_info->zone_size_shift = ilog2(zone_info->zone_size);
0417     zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
0418     /*
0419      * We limit max_zone_append_size also by max_segments *
0420      * PAGE_SIZE. Technically, we can have multiple pages per segment. But,
0421      * since btrfs adds the pages one by one to a bio, and btrfs cannot
0422      * increase the metadata reservation even if it increases the number of
0423      * extents, it is safe to stick with the limit.
0424      *
0425      * With the zoned emulation, we can have non-zoned device on the zoned
0426      * mode. In this case, we don't have a valid max zone append size. So,
0427      * use max_segments * PAGE_SIZE as the pseudo max_zone_append_size.
0428      */
0429     if (bdev_is_zoned(bdev)) {
0430         zone_info->max_zone_append_size = min_t(u64,
0431             (u64)bdev_max_zone_append_sectors(bdev) << SECTOR_SHIFT,
0432             (u64)bdev_max_segments(bdev) << PAGE_SHIFT);
0433     } else {
0434         zone_info->max_zone_append_size =
0435             (u64)bdev_max_segments(bdev) << PAGE_SHIFT;
0436     }
0437     if (!IS_ALIGNED(nr_sectors, zone_sectors))
0438         zone_info->nr_zones++;
0439 
0440     max_active_zones = bdev_max_active_zones(bdev);
0441     if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
0442         btrfs_err_in_rcu(fs_info,
0443 "zoned: %s: max active zones %u is too small, need at least %u active zones",
0444                  rcu_str_deref(device->name), max_active_zones,
0445                  BTRFS_MIN_ACTIVE_ZONES);
0446         ret = -EINVAL;
0447         goto out;
0448     }
0449     zone_info->max_active_zones = max_active_zones;
0450 
0451     zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
0452     if (!zone_info->seq_zones) {
0453         ret = -ENOMEM;
0454         goto out;
0455     }
0456 
0457     zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
0458     if (!zone_info->empty_zones) {
0459         ret = -ENOMEM;
0460         goto out;
0461     }
0462 
0463     zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
0464     if (!zone_info->active_zones) {
0465         ret = -ENOMEM;
0466         goto out;
0467     }
0468 
0469     zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
0470     if (!zones) {
0471         ret = -ENOMEM;
0472         goto out;
0473     }
0474 
0475     /*
0476      * Enable zone cache only for a zoned device. On a non-zoned device, we
0477      * fill the zone info with emulated CONVENTIONAL zones, so no need to
0478      * use the cache.
0479      */
0480     if (populate_cache && bdev_is_zoned(device->bdev)) {
0481         zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
0482                         zone_info->nr_zones);
0483         if (!zone_info->zone_cache) {
0484             btrfs_err_in_rcu(device->fs_info,
0485                 "zoned: failed to allocate zone cache for %s",
0486                 rcu_str_deref(device->name));
0487             ret = -ENOMEM;
0488             goto out;
0489         }
0490     }
0491 
0492     /* Get zones type */
0493     nactive = 0;
0494     while (sector < nr_sectors) {
0495         nr_zones = BTRFS_REPORT_NR_ZONES;
0496         ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
0497                       &nr_zones);
0498         if (ret)
0499             goto out;
0500 
0501         for (i = 0; i < nr_zones; i++) {
0502             if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
0503                 __set_bit(nreported, zone_info->seq_zones);
0504             switch (zones[i].cond) {
0505             case BLK_ZONE_COND_EMPTY:
0506                 __set_bit(nreported, zone_info->empty_zones);
0507                 break;
0508             case BLK_ZONE_COND_IMP_OPEN:
0509             case BLK_ZONE_COND_EXP_OPEN:
0510             case BLK_ZONE_COND_CLOSED:
0511                 __set_bit(nreported, zone_info->active_zones);
0512                 nactive++;
0513                 break;
0514             }
0515             nreported++;
0516         }
0517         sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
0518     }
0519 
0520     if (nreported != zone_info->nr_zones) {
0521         btrfs_err_in_rcu(device->fs_info,
0522                  "inconsistent number of zones on %s (%u/%u)",
0523                  rcu_str_deref(device->name), nreported,
0524                  zone_info->nr_zones);
0525         ret = -EIO;
0526         goto out;
0527     }
0528 
0529     if (max_active_zones) {
0530         if (nactive > max_active_zones) {
0531             btrfs_err_in_rcu(device->fs_info,
0532             "zoned: %u active zones on %s exceeds max_active_zones %u",
0533                      nactive, rcu_str_deref(device->name),
0534                      max_active_zones);
0535             ret = -EIO;
0536             goto out;
0537         }
0538         atomic_set(&zone_info->active_zones_left,
0539                max_active_zones - nactive);
0540     }
0541 
0542     /* Validate superblock log */
0543     nr_zones = BTRFS_NR_SB_LOG_ZONES;
0544     for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
0545         u32 sb_zone;
0546         u64 sb_wp;
0547         int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
0548 
0549         sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
0550         if (sb_zone + 1 >= zone_info->nr_zones)
0551             continue;
0552 
0553         ret = btrfs_get_dev_zones(device,
0554                       zone_start_physical(sb_zone, zone_info),
0555                       &zone_info->sb_zones[sb_pos],
0556                       &nr_zones);
0557         if (ret)
0558             goto out;
0559 
0560         if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
0561             btrfs_err_in_rcu(device->fs_info,
0562     "zoned: failed to read super block log zone info at devid %llu zone %u",
0563                      device->devid, sb_zone);
0564             ret = -EUCLEAN;
0565             goto out;
0566         }
0567 
0568         /*
0569          * If zones[0] is conventional, always use the beginning of the
0570          * zone to record superblock. No need to validate in that case.
0571          */
0572         if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
0573             BLK_ZONE_TYPE_CONVENTIONAL)
0574             continue;
0575 
0576         ret = sb_write_pointer(device->bdev,
0577                        &zone_info->sb_zones[sb_pos], &sb_wp);
0578         if (ret != -ENOENT && ret) {
0579             btrfs_err_in_rcu(device->fs_info,
0580             "zoned: super block log zone corrupted devid %llu zone %u",
0581                      device->devid, sb_zone);
0582             ret = -EUCLEAN;
0583             goto out;
0584         }
0585     }
0586 
0587 
0588     kfree(zones);
0589 
0590     switch (bdev_zoned_model(bdev)) {
0591     case BLK_ZONED_HM:
0592         model = "host-managed zoned";
0593         emulated = "";
0594         break;
0595     case BLK_ZONED_HA:
0596         model = "host-aware zoned";
0597         emulated = "";
0598         break;
0599     case BLK_ZONED_NONE:
0600         model = "regular";
0601         emulated = "emulated ";
0602         break;
0603     default:
0604         /* Just in case */
0605         btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
0606                  bdev_zoned_model(bdev),
0607                  rcu_str_deref(device->name));
0608         ret = -EOPNOTSUPP;
0609         goto out_free_zone_info;
0610     }
0611 
0612     btrfs_info_in_rcu(fs_info,
0613         "%s block device %s, %u %szones of %llu bytes",
0614         model, rcu_str_deref(device->name), zone_info->nr_zones,
0615         emulated, zone_info->zone_size);
0616 
0617     return 0;
0618 
0619 out:
0620     kfree(zones);
0621 out_free_zone_info:
0622     btrfs_destroy_dev_zone_info(device);
0623 
0624     return ret;
0625 }
0626 
0627 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
0628 {
0629     struct btrfs_zoned_device_info *zone_info = device->zone_info;
0630 
0631     if (!zone_info)
0632         return;
0633 
0634     bitmap_free(zone_info->active_zones);
0635     bitmap_free(zone_info->seq_zones);
0636     bitmap_free(zone_info->empty_zones);
0637     vfree(zone_info->zone_cache);
0638     kfree(zone_info);
0639     device->zone_info = NULL;
0640 }
0641 
0642 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
0643                struct blk_zone *zone)
0644 {
0645     unsigned int nr_zones = 1;
0646     int ret;
0647 
0648     ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
0649     if (ret != 0 || !nr_zones)
0650         return ret ? ret : -EIO;
0651 
0652     return 0;
0653 }
0654 
0655 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
0656 {
0657     struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
0658     struct btrfs_device *device;
0659     u64 zoned_devices = 0;
0660     u64 nr_devices = 0;
0661     u64 zone_size = 0;
0662     u64 max_zone_append_size = 0;
0663     const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
0664     int ret = 0;
0665 
0666     /* Count zoned devices */
0667     list_for_each_entry(device, &fs_devices->devices, dev_list) {
0668         enum blk_zoned_model model;
0669 
0670         if (!device->bdev)
0671             continue;
0672 
0673         model = bdev_zoned_model(device->bdev);
0674         /*
0675          * A Host-Managed zoned device must be used as a zoned device.
0676          * A Host-Aware zoned device and a non-zoned devices can be
0677          * treated as a zoned device, if ZONED flag is enabled in the
0678          * superblock.
0679          */
0680         if (model == BLK_ZONED_HM ||
0681             (model == BLK_ZONED_HA && incompat_zoned) ||
0682             (model == BLK_ZONED_NONE && incompat_zoned)) {
0683             struct btrfs_zoned_device_info *zone_info;
0684 
0685             zone_info = device->zone_info;
0686             zoned_devices++;
0687             if (!zone_size) {
0688                 zone_size = zone_info->zone_size;
0689             } else if (zone_info->zone_size != zone_size) {
0690                 btrfs_err(fs_info,
0691         "zoned: unequal block device zone sizes: have %llu found %llu",
0692                       device->zone_info->zone_size,
0693                       zone_size);
0694                 ret = -EINVAL;
0695                 goto out;
0696             }
0697             if (!max_zone_append_size ||
0698                 (zone_info->max_zone_append_size &&
0699                  zone_info->max_zone_append_size < max_zone_append_size))
0700                 max_zone_append_size =
0701                     zone_info->max_zone_append_size;
0702         }
0703         nr_devices++;
0704     }
0705 
0706     if (!zoned_devices && !incompat_zoned)
0707         goto out;
0708 
0709     if (!zoned_devices && incompat_zoned) {
0710         /* No zoned block device found on ZONED filesystem */
0711         btrfs_err(fs_info,
0712               "zoned: no zoned devices found on a zoned filesystem");
0713         ret = -EINVAL;
0714         goto out;
0715     }
0716 
0717     if (zoned_devices && !incompat_zoned) {
0718         btrfs_err(fs_info,
0719               "zoned: mode not enabled but zoned device found");
0720         ret = -EINVAL;
0721         goto out;
0722     }
0723 
0724     if (zoned_devices != nr_devices) {
0725         btrfs_err(fs_info,
0726               "zoned: cannot mix zoned and regular devices");
0727         ret = -EINVAL;
0728         goto out;
0729     }
0730 
0731     /*
0732      * stripe_size is always aligned to BTRFS_STRIPE_LEN in
0733      * btrfs_create_chunk(). Since we want stripe_len == zone_size,
0734      * check the alignment here.
0735      */
0736     if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
0737         btrfs_err(fs_info,
0738               "zoned: zone size %llu not aligned to stripe %u",
0739               zone_size, BTRFS_STRIPE_LEN);
0740         ret = -EINVAL;
0741         goto out;
0742     }
0743 
0744     if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
0745         btrfs_err(fs_info, "zoned: mixed block groups not supported");
0746         ret = -EINVAL;
0747         goto out;
0748     }
0749 
0750     fs_info->zone_size = zone_size;
0751     fs_info->max_zone_append_size = ALIGN_DOWN(max_zone_append_size,
0752                            fs_info->sectorsize);
0753     fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
0754     if (fs_info->max_zone_append_size < fs_info->max_extent_size)
0755         fs_info->max_extent_size = fs_info->max_zone_append_size;
0756 
0757     /*
0758      * Check mount options here, because we might change fs_info->zoned
0759      * from fs_info->zone_size.
0760      */
0761     ret = btrfs_check_mountopts_zoned(fs_info);
0762     if (ret)
0763         goto out;
0764 
0765     btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
0766 out:
0767     return ret;
0768 }
0769 
0770 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
0771 {
0772     if (!btrfs_is_zoned(info))
0773         return 0;
0774 
0775     /*
0776      * Space cache writing is not COWed. Disable that to avoid write errors
0777      * in sequential zones.
0778      */
0779     if (btrfs_test_opt(info, SPACE_CACHE)) {
0780         btrfs_err(info, "zoned: space cache v1 is not supported");
0781         return -EINVAL;
0782     }
0783 
0784     if (btrfs_test_opt(info, NODATACOW)) {
0785         btrfs_err(info, "zoned: NODATACOW not supported");
0786         return -EINVAL;
0787     }
0788 
0789     return 0;
0790 }
0791 
0792 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
0793                int rw, u64 *bytenr_ret)
0794 {
0795     u64 wp;
0796     int ret;
0797 
0798     if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
0799         *bytenr_ret = zones[0].start << SECTOR_SHIFT;
0800         return 0;
0801     }
0802 
0803     ret = sb_write_pointer(bdev, zones, &wp);
0804     if (ret != -ENOENT && ret < 0)
0805         return ret;
0806 
0807     if (rw == WRITE) {
0808         struct blk_zone *reset = NULL;
0809 
0810         if (wp == zones[0].start << SECTOR_SHIFT)
0811             reset = &zones[0];
0812         else if (wp == zones[1].start << SECTOR_SHIFT)
0813             reset = &zones[1];
0814 
0815         if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
0816             ASSERT(sb_zone_is_full(reset));
0817 
0818             ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
0819                            reset->start, reset->len,
0820                            GFP_NOFS);
0821             if (ret)
0822                 return ret;
0823 
0824             reset->cond = BLK_ZONE_COND_EMPTY;
0825             reset->wp = reset->start;
0826         }
0827     } else if (ret != -ENOENT) {
0828         /*
0829          * For READ, we want the previous one. Move write pointer to
0830          * the end of a zone, if it is at the head of a zone.
0831          */
0832         u64 zone_end = 0;
0833 
0834         if (wp == zones[0].start << SECTOR_SHIFT)
0835             zone_end = zones[1].start + zones[1].capacity;
0836         else if (wp == zones[1].start << SECTOR_SHIFT)
0837             zone_end = zones[0].start + zones[0].capacity;
0838         if (zone_end)
0839             wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
0840                     BTRFS_SUPER_INFO_SIZE);
0841 
0842         wp -= BTRFS_SUPER_INFO_SIZE;
0843     }
0844 
0845     *bytenr_ret = wp;
0846     return 0;
0847 
0848 }
0849 
0850 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
0851                    u64 *bytenr_ret)
0852 {
0853     struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
0854     sector_t zone_sectors;
0855     u32 sb_zone;
0856     int ret;
0857     u8 zone_sectors_shift;
0858     sector_t nr_sectors;
0859     u32 nr_zones;
0860 
0861     if (!bdev_is_zoned(bdev)) {
0862         *bytenr_ret = btrfs_sb_offset(mirror);
0863         return 0;
0864     }
0865 
0866     ASSERT(rw == READ || rw == WRITE);
0867 
0868     zone_sectors = bdev_zone_sectors(bdev);
0869     if (!is_power_of_2(zone_sectors))
0870         return -EINVAL;
0871     zone_sectors_shift = ilog2(zone_sectors);
0872     nr_sectors = bdev_nr_sectors(bdev);
0873     nr_zones = nr_sectors >> zone_sectors_shift;
0874 
0875     sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
0876     if (sb_zone + 1 >= nr_zones)
0877         return -ENOENT;
0878 
0879     ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
0880                   BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
0881                   zones);
0882     if (ret < 0)
0883         return ret;
0884     if (ret != BTRFS_NR_SB_LOG_ZONES)
0885         return -EIO;
0886 
0887     return sb_log_location(bdev, zones, rw, bytenr_ret);
0888 }
0889 
0890 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
0891               u64 *bytenr_ret)
0892 {
0893     struct btrfs_zoned_device_info *zinfo = device->zone_info;
0894     u32 zone_num;
0895 
0896     /*
0897      * For a zoned filesystem on a non-zoned block device, use the same
0898      * super block locations as regular filesystem. Doing so, the super
0899      * block can always be retrieved and the zoned flag of the volume
0900      * detected from the super block information.
0901      */
0902     if (!bdev_is_zoned(device->bdev)) {
0903         *bytenr_ret = btrfs_sb_offset(mirror);
0904         return 0;
0905     }
0906 
0907     zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
0908     if (zone_num + 1 >= zinfo->nr_zones)
0909         return -ENOENT;
0910 
0911     return sb_log_location(device->bdev,
0912                    &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
0913                    rw, bytenr_ret);
0914 }
0915 
0916 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
0917                   int mirror)
0918 {
0919     u32 zone_num;
0920 
0921     if (!zinfo)
0922         return false;
0923 
0924     zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
0925     if (zone_num + 1 >= zinfo->nr_zones)
0926         return false;
0927 
0928     if (!test_bit(zone_num, zinfo->seq_zones))
0929         return false;
0930 
0931     return true;
0932 }
0933 
0934 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
0935 {
0936     struct btrfs_zoned_device_info *zinfo = device->zone_info;
0937     struct blk_zone *zone;
0938     int i;
0939 
0940     if (!is_sb_log_zone(zinfo, mirror))
0941         return 0;
0942 
0943     zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
0944     for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
0945         /* Advance the next zone */
0946         if (zone->cond == BLK_ZONE_COND_FULL) {
0947             zone++;
0948             continue;
0949         }
0950 
0951         if (zone->cond == BLK_ZONE_COND_EMPTY)
0952             zone->cond = BLK_ZONE_COND_IMP_OPEN;
0953 
0954         zone->wp += SUPER_INFO_SECTORS;
0955 
0956         if (sb_zone_is_full(zone)) {
0957             /*
0958              * No room left to write new superblock. Since
0959              * superblock is written with REQ_SYNC, it is safe to
0960              * finish the zone now.
0961              *
0962              * If the write pointer is exactly at the capacity,
0963              * explicit ZONE_FINISH is not necessary.
0964              */
0965             if (zone->wp != zone->start + zone->capacity) {
0966                 int ret;
0967 
0968                 ret = blkdev_zone_mgmt(device->bdev,
0969                         REQ_OP_ZONE_FINISH, zone->start,
0970                         zone->len, GFP_NOFS);
0971                 if (ret)
0972                     return ret;
0973             }
0974 
0975             zone->wp = zone->start + zone->len;
0976             zone->cond = BLK_ZONE_COND_FULL;
0977         }
0978         return 0;
0979     }
0980 
0981     /* All the zones are FULL. Should not reach here. */
0982     ASSERT(0);
0983     return -EIO;
0984 }
0985 
0986 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
0987 {
0988     sector_t zone_sectors;
0989     sector_t nr_sectors;
0990     u8 zone_sectors_shift;
0991     u32 sb_zone;
0992     u32 nr_zones;
0993 
0994     zone_sectors = bdev_zone_sectors(bdev);
0995     zone_sectors_shift = ilog2(zone_sectors);
0996     nr_sectors = bdev_nr_sectors(bdev);
0997     nr_zones = nr_sectors >> zone_sectors_shift;
0998 
0999     sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1000     if (sb_zone + 1 >= nr_zones)
1001         return -ENOENT;
1002 
1003     return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1004                 zone_start_sector(sb_zone, bdev),
1005                 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
1006 }
1007 
1008 /**
1009  * btrfs_find_allocatable_zones - find allocatable zones within a given region
1010  *
1011  * @device: the device to allocate a region on
1012  * @hole_start: the position of the hole to allocate the region
1013  * @num_bytes:  size of wanted region
1014  * @hole_end:   the end of the hole
1015  * @return: position of allocatable zones
1016  *
1017  * Allocatable region should not contain any superblock locations.
1018  */
1019 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1020                  u64 hole_end, u64 num_bytes)
1021 {
1022     struct btrfs_zoned_device_info *zinfo = device->zone_info;
1023     const u8 shift = zinfo->zone_size_shift;
1024     u64 nzones = num_bytes >> shift;
1025     u64 pos = hole_start;
1026     u64 begin, end;
1027     bool have_sb;
1028     int i;
1029 
1030     ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1031     ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1032 
1033     while (pos < hole_end) {
1034         begin = pos >> shift;
1035         end = begin + nzones;
1036 
1037         if (end > zinfo->nr_zones)
1038             return hole_end;
1039 
1040         /* Check if zones in the region are all empty */
1041         if (btrfs_dev_is_sequential(device, pos) &&
1042             find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1043             pos += zinfo->zone_size;
1044             continue;
1045         }
1046 
1047         have_sb = false;
1048         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1049             u32 sb_zone;
1050             u64 sb_pos;
1051 
1052             sb_zone = sb_zone_number(shift, i);
1053             if (!(end <= sb_zone ||
1054                   sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1055                 have_sb = true;
1056                 pos = zone_start_physical(
1057                     sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1058                 break;
1059             }
1060 
1061             /* We also need to exclude regular superblock positions */
1062             sb_pos = btrfs_sb_offset(i);
1063             if (!(pos + num_bytes <= sb_pos ||
1064                   sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1065                 have_sb = true;
1066                 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1067                         zinfo->zone_size);
1068                 break;
1069             }
1070         }
1071         if (!have_sb)
1072             break;
1073     }
1074 
1075     return pos;
1076 }
1077 
1078 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1079 {
1080     struct btrfs_zoned_device_info *zone_info = device->zone_info;
1081     unsigned int zno = (pos >> zone_info->zone_size_shift);
1082 
1083     /* We can use any number of zones */
1084     if (zone_info->max_active_zones == 0)
1085         return true;
1086 
1087     if (!test_bit(zno, zone_info->active_zones)) {
1088         /* Active zone left? */
1089         if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1090             return false;
1091         if (test_and_set_bit(zno, zone_info->active_zones)) {
1092             /* Someone already set the bit */
1093             atomic_inc(&zone_info->active_zones_left);
1094         }
1095     }
1096 
1097     return true;
1098 }
1099 
1100 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1101 {
1102     struct btrfs_zoned_device_info *zone_info = device->zone_info;
1103     unsigned int zno = (pos >> zone_info->zone_size_shift);
1104 
1105     /* We can use any number of zones */
1106     if (zone_info->max_active_zones == 0)
1107         return;
1108 
1109     if (test_and_clear_bit(zno, zone_info->active_zones))
1110         atomic_inc(&zone_info->active_zones_left);
1111 }
1112 
1113 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1114                 u64 length, u64 *bytes)
1115 {
1116     int ret;
1117 
1118     *bytes = 0;
1119     ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1120                    physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1121                    GFP_NOFS);
1122     if (ret)
1123         return ret;
1124 
1125     *bytes = length;
1126     while (length) {
1127         btrfs_dev_set_zone_empty(device, physical);
1128         btrfs_dev_clear_active_zone(device, physical);
1129         physical += device->zone_info->zone_size;
1130         length -= device->zone_info->zone_size;
1131     }
1132 
1133     return 0;
1134 }
1135 
1136 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1137 {
1138     struct btrfs_zoned_device_info *zinfo = device->zone_info;
1139     const u8 shift = zinfo->zone_size_shift;
1140     unsigned long begin = start >> shift;
1141     unsigned long end = (start + size) >> shift;
1142     u64 pos;
1143     int ret;
1144 
1145     ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1146     ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1147 
1148     if (end > zinfo->nr_zones)
1149         return -ERANGE;
1150 
1151     /* All the zones are conventional */
1152     if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1153         return 0;
1154 
1155     /* All the zones are sequential and empty */
1156     if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1157         find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1158         return 0;
1159 
1160     for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1161         u64 reset_bytes;
1162 
1163         if (!btrfs_dev_is_sequential(device, pos) ||
1164             btrfs_dev_is_empty_zone(device, pos))
1165             continue;
1166 
1167         /* Free regions should be empty */
1168         btrfs_warn_in_rcu(
1169             device->fs_info,
1170         "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1171             rcu_str_deref(device->name), device->devid, pos >> shift);
1172         WARN_ON_ONCE(1);
1173 
1174         ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1175                           &reset_bytes);
1176         if (ret)
1177             return ret;
1178     }
1179 
1180     return 0;
1181 }
1182 
1183 /*
1184  * Calculate an allocation pointer from the extent allocation information
1185  * for a block group consist of conventional zones. It is pointed to the
1186  * end of the highest addressed extent in the block group as an allocation
1187  * offset.
1188  */
1189 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1190                    u64 *offset_ret, bool new)
1191 {
1192     struct btrfs_fs_info *fs_info = cache->fs_info;
1193     struct btrfs_root *root;
1194     struct btrfs_path *path;
1195     struct btrfs_key key;
1196     struct btrfs_key found_key;
1197     int ret;
1198     u64 length;
1199 
1200     /*
1201      * Avoid  tree lookups for a new block group, there's no use for it.
1202      * It must always be 0.
1203      *
1204      * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1205      * For new a block group, this function is called from
1206      * btrfs_make_block_group() which is already taking the chunk mutex.
1207      * Thus, we cannot call calculate_alloc_pointer() which takes extent
1208      * buffer locks to avoid deadlock.
1209      */
1210     if (new) {
1211         *offset_ret = 0;
1212         return 0;
1213     }
1214 
1215     path = btrfs_alloc_path();
1216     if (!path)
1217         return -ENOMEM;
1218 
1219     key.objectid = cache->start + cache->length;
1220     key.type = 0;
1221     key.offset = 0;
1222 
1223     root = btrfs_extent_root(fs_info, key.objectid);
1224     ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1225     /* We should not find the exact match */
1226     if (!ret)
1227         ret = -EUCLEAN;
1228     if (ret < 0)
1229         goto out;
1230 
1231     ret = btrfs_previous_extent_item(root, path, cache->start);
1232     if (ret) {
1233         if (ret == 1) {
1234             ret = 0;
1235             *offset_ret = 0;
1236         }
1237         goto out;
1238     }
1239 
1240     btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1241 
1242     if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1243         length = found_key.offset;
1244     else
1245         length = fs_info->nodesize;
1246 
1247     if (!(found_key.objectid >= cache->start &&
1248            found_key.objectid + length <= cache->start + cache->length)) {
1249         ret = -EUCLEAN;
1250         goto out;
1251     }
1252     *offset_ret = found_key.objectid + length - cache->start;
1253     ret = 0;
1254 
1255 out:
1256     btrfs_free_path(path);
1257     return ret;
1258 }
1259 
1260 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1261 {
1262     struct btrfs_fs_info *fs_info = cache->fs_info;
1263     struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1264     struct extent_map *em;
1265     struct map_lookup *map;
1266     struct btrfs_device *device;
1267     u64 logical = cache->start;
1268     u64 length = cache->length;
1269     int ret;
1270     int i;
1271     unsigned int nofs_flag;
1272     u64 *alloc_offsets = NULL;
1273     u64 *caps = NULL;
1274     u64 *physical = NULL;
1275     unsigned long *active = NULL;
1276     u64 last_alloc = 0;
1277     u32 num_sequential = 0, num_conventional = 0;
1278 
1279     if (!btrfs_is_zoned(fs_info))
1280         return 0;
1281 
1282     /* Sanity check */
1283     if (!IS_ALIGNED(length, fs_info->zone_size)) {
1284         btrfs_err(fs_info,
1285         "zoned: block group %llu len %llu unaligned to zone size %llu",
1286               logical, length, fs_info->zone_size);
1287         return -EIO;
1288     }
1289 
1290     /* Get the chunk mapping */
1291     read_lock(&em_tree->lock);
1292     em = lookup_extent_mapping(em_tree, logical, length);
1293     read_unlock(&em_tree->lock);
1294 
1295     if (!em)
1296         return -EINVAL;
1297 
1298     map = em->map_lookup;
1299 
1300     cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1301     if (!cache->physical_map) {
1302         ret = -ENOMEM;
1303         goto out;
1304     }
1305 
1306     alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1307     if (!alloc_offsets) {
1308         ret = -ENOMEM;
1309         goto out;
1310     }
1311 
1312     caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1313     if (!caps) {
1314         ret = -ENOMEM;
1315         goto out;
1316     }
1317 
1318     physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1319     if (!physical) {
1320         ret = -ENOMEM;
1321         goto out;
1322     }
1323 
1324     active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1325     if (!active) {
1326         ret = -ENOMEM;
1327         goto out;
1328     }
1329 
1330     for (i = 0; i < map->num_stripes; i++) {
1331         bool is_sequential;
1332         struct blk_zone zone;
1333         struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1334         int dev_replace_is_ongoing = 0;
1335 
1336         device = map->stripes[i].dev;
1337         physical[i] = map->stripes[i].physical;
1338 
1339         if (device->bdev == NULL) {
1340             alloc_offsets[i] = WP_MISSING_DEV;
1341             continue;
1342         }
1343 
1344         is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1345         if (is_sequential)
1346             num_sequential++;
1347         else
1348             num_conventional++;
1349 
1350         /*
1351          * Consider a zone as active if we can allow any number of
1352          * active zones.
1353          */
1354         if (!device->zone_info->max_active_zones)
1355             __set_bit(i, active);
1356 
1357         if (!is_sequential) {
1358             alloc_offsets[i] = WP_CONVENTIONAL;
1359             continue;
1360         }
1361 
1362         /*
1363          * This zone will be used for allocation, so mark this zone
1364          * non-empty.
1365          */
1366         btrfs_dev_clear_zone_empty(device, physical[i]);
1367 
1368         down_read(&dev_replace->rwsem);
1369         dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1370         if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1371             btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1372         up_read(&dev_replace->rwsem);
1373 
1374         /*
1375          * The group is mapped to a sequential zone. Get the zone write
1376          * pointer to determine the allocation offset within the zone.
1377          */
1378         WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1379         nofs_flag = memalloc_nofs_save();
1380         ret = btrfs_get_dev_zone(device, physical[i], &zone);
1381         memalloc_nofs_restore(nofs_flag);
1382         if (ret == -EIO || ret == -EOPNOTSUPP) {
1383             ret = 0;
1384             alloc_offsets[i] = WP_MISSING_DEV;
1385             continue;
1386         } else if (ret) {
1387             goto out;
1388         }
1389 
1390         if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1391             btrfs_err_in_rcu(fs_info,
1392     "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1393                 zone.start << SECTOR_SHIFT,
1394                 rcu_str_deref(device->name), device->devid);
1395             ret = -EIO;
1396             goto out;
1397         }
1398 
1399         caps[i] = (zone.capacity << SECTOR_SHIFT);
1400 
1401         switch (zone.cond) {
1402         case BLK_ZONE_COND_OFFLINE:
1403         case BLK_ZONE_COND_READONLY:
1404             btrfs_err(fs_info,
1405         "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1406                   physical[i] >> device->zone_info->zone_size_shift,
1407                   rcu_str_deref(device->name), device->devid);
1408             alloc_offsets[i] = WP_MISSING_DEV;
1409             break;
1410         case BLK_ZONE_COND_EMPTY:
1411             alloc_offsets[i] = 0;
1412             break;
1413         case BLK_ZONE_COND_FULL:
1414             alloc_offsets[i] = caps[i];
1415             break;
1416         default:
1417             /* Partially used zone */
1418             alloc_offsets[i] =
1419                     ((zone.wp - zone.start) << SECTOR_SHIFT);
1420             __set_bit(i, active);
1421             break;
1422         }
1423     }
1424 
1425     if (num_sequential > 0)
1426         cache->seq_zone = true;
1427 
1428     if (num_conventional > 0) {
1429         /* Zone capacity is always zone size in emulation */
1430         cache->zone_capacity = cache->length;
1431         ret = calculate_alloc_pointer(cache, &last_alloc, new);
1432         if (ret) {
1433             btrfs_err(fs_info,
1434             "zoned: failed to determine allocation offset of bg %llu",
1435                   cache->start);
1436             goto out;
1437         } else if (map->num_stripes == num_conventional) {
1438             cache->alloc_offset = last_alloc;
1439             cache->zone_is_active = 1;
1440             goto out;
1441         }
1442     }
1443 
1444     switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1445     case 0: /* single */
1446         if (alloc_offsets[0] == WP_MISSING_DEV) {
1447             btrfs_err(fs_info,
1448             "zoned: cannot recover write pointer for zone %llu",
1449                 physical[0]);
1450             ret = -EIO;
1451             goto out;
1452         }
1453         cache->alloc_offset = alloc_offsets[0];
1454         cache->zone_capacity = caps[0];
1455         cache->zone_is_active = test_bit(0, active);
1456         break;
1457     case BTRFS_BLOCK_GROUP_DUP:
1458         if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1459             btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1460             ret = -EINVAL;
1461             goto out;
1462         }
1463         if (alloc_offsets[0] == WP_MISSING_DEV) {
1464             btrfs_err(fs_info,
1465             "zoned: cannot recover write pointer for zone %llu",
1466                 physical[0]);
1467             ret = -EIO;
1468             goto out;
1469         }
1470         if (alloc_offsets[1] == WP_MISSING_DEV) {
1471             btrfs_err(fs_info,
1472             "zoned: cannot recover write pointer for zone %llu",
1473                 physical[1]);
1474             ret = -EIO;
1475             goto out;
1476         }
1477         if (alloc_offsets[0] != alloc_offsets[1]) {
1478             btrfs_err(fs_info,
1479             "zoned: write pointer offset mismatch of zones in DUP profile");
1480             ret = -EIO;
1481             goto out;
1482         }
1483         if (test_bit(0, active) != test_bit(1, active)) {
1484             if (!btrfs_zone_activate(cache)) {
1485                 ret = -EIO;
1486                 goto out;
1487             }
1488         } else {
1489             cache->zone_is_active = test_bit(0, active);
1490         }
1491         cache->alloc_offset = alloc_offsets[0];
1492         cache->zone_capacity = min(caps[0], caps[1]);
1493         break;
1494     case BTRFS_BLOCK_GROUP_RAID1:
1495     case BTRFS_BLOCK_GROUP_RAID0:
1496     case BTRFS_BLOCK_GROUP_RAID10:
1497     case BTRFS_BLOCK_GROUP_RAID5:
1498     case BTRFS_BLOCK_GROUP_RAID6:
1499         /* non-single profiles are not supported yet */
1500     default:
1501         btrfs_err(fs_info, "zoned: profile %s not yet supported",
1502               btrfs_bg_type_to_raid_name(map->type));
1503         ret = -EINVAL;
1504         goto out;
1505     }
1506 
1507 out:
1508     if (cache->alloc_offset > fs_info->zone_size) {
1509         btrfs_err(fs_info,
1510             "zoned: invalid write pointer %llu in block group %llu",
1511             cache->alloc_offset, cache->start);
1512         ret = -EIO;
1513     }
1514 
1515     if (cache->alloc_offset > cache->zone_capacity) {
1516         btrfs_err(fs_info,
1517 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1518               cache->alloc_offset, cache->zone_capacity,
1519               cache->start);
1520         ret = -EIO;
1521     }
1522 
1523     /* An extent is allocated after the write pointer */
1524     if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1525         btrfs_err(fs_info,
1526               "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1527               logical, last_alloc, cache->alloc_offset);
1528         ret = -EIO;
1529     }
1530 
1531     if (!ret) {
1532         cache->meta_write_pointer = cache->alloc_offset + cache->start;
1533         if (cache->zone_is_active) {
1534             btrfs_get_block_group(cache);
1535             spin_lock(&fs_info->zone_active_bgs_lock);
1536             list_add_tail(&cache->active_bg_list,
1537                       &fs_info->zone_active_bgs);
1538             spin_unlock(&fs_info->zone_active_bgs_lock);
1539         }
1540     } else {
1541         kfree(cache->physical_map);
1542         cache->physical_map = NULL;
1543     }
1544     bitmap_free(active);
1545     kfree(physical);
1546     kfree(caps);
1547     kfree(alloc_offsets);
1548     free_extent_map(em);
1549 
1550     return ret;
1551 }
1552 
1553 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1554 {
1555     u64 unusable, free;
1556 
1557     if (!btrfs_is_zoned(cache->fs_info))
1558         return;
1559 
1560     WARN_ON(cache->bytes_super != 0);
1561     unusable = (cache->alloc_offset - cache->used) +
1562            (cache->length - cache->zone_capacity);
1563     free = cache->zone_capacity - cache->alloc_offset;
1564 
1565     /* We only need ->free_space in ALLOC_SEQ block groups */
1566     cache->last_byte_to_unpin = (u64)-1;
1567     cache->cached = BTRFS_CACHE_FINISHED;
1568     cache->free_space_ctl->free_space = free;
1569     cache->zone_unusable = unusable;
1570 }
1571 
1572 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1573                 struct extent_buffer *eb)
1574 {
1575     struct btrfs_fs_info *fs_info = eb->fs_info;
1576 
1577     if (!btrfs_is_zoned(fs_info) ||
1578         btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1579         !list_empty(&eb->release_list))
1580         return;
1581 
1582     set_extent_buffer_dirty(eb);
1583     set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1584                    eb->start + eb->len - 1, EXTENT_DIRTY);
1585     memzero_extent_buffer(eb, 0, eb->len);
1586     set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1587 
1588     spin_lock(&trans->releasing_ebs_lock);
1589     list_add_tail(&eb->release_list, &trans->releasing_ebs);
1590     spin_unlock(&trans->releasing_ebs_lock);
1591     atomic_inc(&eb->refs);
1592 }
1593 
1594 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1595 {
1596     spin_lock(&trans->releasing_ebs_lock);
1597     while (!list_empty(&trans->releasing_ebs)) {
1598         struct extent_buffer *eb;
1599 
1600         eb = list_first_entry(&trans->releasing_ebs,
1601                       struct extent_buffer, release_list);
1602         list_del_init(&eb->release_list);
1603         free_extent_buffer(eb);
1604     }
1605     spin_unlock(&trans->releasing_ebs_lock);
1606 }
1607 
1608 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1609 {
1610     struct btrfs_fs_info *fs_info = inode->root->fs_info;
1611     struct btrfs_block_group *cache;
1612     bool ret = false;
1613 
1614     if (!btrfs_is_zoned(fs_info))
1615         return false;
1616 
1617     if (!is_data_inode(&inode->vfs_inode))
1618         return false;
1619 
1620     /*
1621      * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1622      * extent layout the relocation code has.
1623      * Furthermore we have set aside own block-group from which only the
1624      * relocation "process" can allocate and make sure only one process at a
1625      * time can add pages to an extent that gets relocated, so it's safe to
1626      * use regular REQ_OP_WRITE for this special case.
1627      */
1628     if (btrfs_is_data_reloc_root(inode->root))
1629         return false;
1630 
1631     cache = btrfs_lookup_block_group(fs_info, start);
1632     ASSERT(cache);
1633     if (!cache)
1634         return false;
1635 
1636     ret = cache->seq_zone;
1637     btrfs_put_block_group(cache);
1638 
1639     return ret;
1640 }
1641 
1642 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1643                  struct bio *bio)
1644 {
1645     struct btrfs_ordered_extent *ordered;
1646     const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1647 
1648     if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1649         return;
1650 
1651     ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1652     if (WARN_ON(!ordered))
1653         return;
1654 
1655     ordered->physical = physical;
1656     ordered->bdev = bio->bi_bdev;
1657 
1658     btrfs_put_ordered_extent(ordered);
1659 }
1660 
1661 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1662 {
1663     struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1664     struct btrfs_fs_info *fs_info = inode->root->fs_info;
1665     struct extent_map_tree *em_tree;
1666     struct extent_map *em;
1667     struct btrfs_ordered_sum *sum;
1668     u64 orig_logical = ordered->disk_bytenr;
1669     u64 *logical = NULL;
1670     int nr, stripe_len;
1671 
1672     /* Zoned devices should not have partitions. So, we can assume it is 0 */
1673     ASSERT(!bdev_is_partition(ordered->bdev));
1674     if (WARN_ON(!ordered->bdev))
1675         return;
1676 
1677     if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1678                      ordered->physical, &logical, &nr,
1679                      &stripe_len)))
1680         goto out;
1681 
1682     WARN_ON(nr != 1);
1683 
1684     if (orig_logical == *logical)
1685         goto out;
1686 
1687     ordered->disk_bytenr = *logical;
1688 
1689     em_tree = &inode->extent_tree;
1690     write_lock(&em_tree->lock);
1691     em = search_extent_mapping(em_tree, ordered->file_offset,
1692                    ordered->num_bytes);
1693     em->block_start = *logical;
1694     free_extent_map(em);
1695     write_unlock(&em_tree->lock);
1696 
1697     list_for_each_entry(sum, &ordered->list, list) {
1698         if (*logical < orig_logical)
1699             sum->bytenr -= orig_logical - *logical;
1700         else
1701             sum->bytenr += *logical - orig_logical;
1702     }
1703 
1704 out:
1705     kfree(logical);
1706 }
1707 
1708 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1709                     struct extent_buffer *eb,
1710                     struct btrfs_block_group **cache_ret)
1711 {
1712     struct btrfs_block_group *cache;
1713     bool ret = true;
1714 
1715     if (!btrfs_is_zoned(fs_info))
1716         return true;
1717 
1718     cache = btrfs_lookup_block_group(fs_info, eb->start);
1719     if (!cache)
1720         return true;
1721 
1722     if (cache->meta_write_pointer != eb->start) {
1723         btrfs_put_block_group(cache);
1724         cache = NULL;
1725         ret = false;
1726     } else {
1727         cache->meta_write_pointer = eb->start + eb->len;
1728     }
1729 
1730     *cache_ret = cache;
1731 
1732     return ret;
1733 }
1734 
1735 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1736                      struct extent_buffer *eb)
1737 {
1738     if (!btrfs_is_zoned(eb->fs_info) || !cache)
1739         return;
1740 
1741     ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1742     cache->meta_write_pointer = eb->start;
1743 }
1744 
1745 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1746 {
1747     if (!btrfs_dev_is_sequential(device, physical))
1748         return -EOPNOTSUPP;
1749 
1750     return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1751                     length >> SECTOR_SHIFT, GFP_NOFS, 0);
1752 }
1753 
1754 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1755               struct blk_zone *zone)
1756 {
1757     struct btrfs_io_context *bioc = NULL;
1758     u64 mapped_length = PAGE_SIZE;
1759     unsigned int nofs_flag;
1760     int nmirrors;
1761     int i, ret;
1762 
1763     ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1764                    &mapped_length, &bioc);
1765     if (ret || !bioc || mapped_length < PAGE_SIZE) {
1766         ret = -EIO;
1767         goto out_put_bioc;
1768     }
1769 
1770     if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1771         ret = -EINVAL;
1772         goto out_put_bioc;
1773     }
1774 
1775     nofs_flag = memalloc_nofs_save();
1776     nmirrors = (int)bioc->num_stripes;
1777     for (i = 0; i < nmirrors; i++) {
1778         u64 physical = bioc->stripes[i].physical;
1779         struct btrfs_device *dev = bioc->stripes[i].dev;
1780 
1781         /* Missing device */
1782         if (!dev->bdev)
1783             continue;
1784 
1785         ret = btrfs_get_dev_zone(dev, physical, zone);
1786         /* Failing device */
1787         if (ret == -EIO || ret == -EOPNOTSUPP)
1788             continue;
1789         break;
1790     }
1791     memalloc_nofs_restore(nofs_flag);
1792 out_put_bioc:
1793     btrfs_put_bioc(bioc);
1794     return ret;
1795 }
1796 
1797 /*
1798  * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1799  * filling zeros between @physical_pos to a write pointer of dev-replace
1800  * source device.
1801  */
1802 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1803                     u64 physical_start, u64 physical_pos)
1804 {
1805     struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1806     struct blk_zone zone;
1807     u64 length;
1808     u64 wp;
1809     int ret;
1810 
1811     if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1812         return 0;
1813 
1814     ret = read_zone_info(fs_info, logical, &zone);
1815     if (ret)
1816         return ret;
1817 
1818     wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1819 
1820     if (physical_pos == wp)
1821         return 0;
1822 
1823     if (physical_pos > wp)
1824         return -EUCLEAN;
1825 
1826     length = wp - physical_pos;
1827     return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1828 }
1829 
1830 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1831                         u64 logical, u64 length)
1832 {
1833     struct btrfs_device *device;
1834     struct extent_map *em;
1835     struct map_lookup *map;
1836 
1837     em = btrfs_get_chunk_map(fs_info, logical, length);
1838     if (IS_ERR(em))
1839         return ERR_CAST(em);
1840 
1841     map = em->map_lookup;
1842     /* We only support single profile for now */
1843     device = map->stripes[0].dev;
1844 
1845     free_extent_map(em);
1846 
1847     return device;
1848 }
1849 
1850 /**
1851  * Activate block group and underlying device zones
1852  *
1853  * @block_group: the block group to activate
1854  *
1855  * Return: true on success, false otherwise
1856  */
1857 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1858 {
1859     struct btrfs_fs_info *fs_info = block_group->fs_info;
1860     struct btrfs_space_info *space_info = block_group->space_info;
1861     struct map_lookup *map;
1862     struct btrfs_device *device;
1863     u64 physical;
1864     bool ret;
1865     int i;
1866 
1867     if (!btrfs_is_zoned(block_group->fs_info))
1868         return true;
1869 
1870     map = block_group->physical_map;
1871 
1872     spin_lock(&space_info->lock);
1873     spin_lock(&block_group->lock);
1874     if (block_group->zone_is_active) {
1875         ret = true;
1876         goto out_unlock;
1877     }
1878 
1879     /* No space left */
1880     if (btrfs_zoned_bg_is_full(block_group)) {
1881         ret = false;
1882         goto out_unlock;
1883     }
1884 
1885     for (i = 0; i < map->num_stripes; i++) {
1886         device = map->stripes[i].dev;
1887         physical = map->stripes[i].physical;
1888 
1889         if (device->zone_info->max_active_zones == 0)
1890             continue;
1891 
1892         if (!btrfs_dev_set_active_zone(device, physical)) {
1893             /* Cannot activate the zone */
1894             ret = false;
1895             goto out_unlock;
1896         }
1897     }
1898 
1899     /* Successfully activated all the zones */
1900     block_group->zone_is_active = 1;
1901     space_info->active_total_bytes += block_group->length;
1902     spin_unlock(&block_group->lock);
1903     btrfs_try_granting_tickets(fs_info, space_info);
1904     spin_unlock(&space_info->lock);
1905 
1906     /* For the active block group list */
1907     btrfs_get_block_group(block_group);
1908 
1909     spin_lock(&fs_info->zone_active_bgs_lock);
1910     list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1911     spin_unlock(&fs_info->zone_active_bgs_lock);
1912 
1913     return true;
1914 
1915 out_unlock:
1916     spin_unlock(&block_group->lock);
1917     spin_unlock(&space_info->lock);
1918     return ret;
1919 }
1920 
1921 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
1922 {
1923     struct btrfs_fs_info *fs_info = block_group->fs_info;
1924     const u64 end = block_group->start + block_group->length;
1925     struct radix_tree_iter iter;
1926     struct extent_buffer *eb;
1927     void __rcu **slot;
1928 
1929     rcu_read_lock();
1930     radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
1931                  block_group->start >> fs_info->sectorsize_bits) {
1932         eb = radix_tree_deref_slot(slot);
1933         if (!eb)
1934             continue;
1935         if (radix_tree_deref_retry(eb)) {
1936             slot = radix_tree_iter_retry(&iter);
1937             continue;
1938         }
1939 
1940         if (eb->start < block_group->start)
1941             continue;
1942         if (eb->start >= end)
1943             break;
1944 
1945         slot = radix_tree_iter_resume(slot, &iter);
1946         rcu_read_unlock();
1947         wait_on_extent_buffer_writeback(eb);
1948         rcu_read_lock();
1949     }
1950     rcu_read_unlock();
1951 }
1952 
1953 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
1954 {
1955     struct btrfs_fs_info *fs_info = block_group->fs_info;
1956     struct map_lookup *map;
1957     const bool is_metadata = (block_group->flags &
1958             (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
1959     int ret = 0;
1960     int i;
1961 
1962     spin_lock(&block_group->lock);
1963     if (!block_group->zone_is_active) {
1964         spin_unlock(&block_group->lock);
1965         return 0;
1966     }
1967 
1968     /* Check if we have unwritten allocated space */
1969     if (is_metadata &&
1970         block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
1971         spin_unlock(&block_group->lock);
1972         return -EAGAIN;
1973     }
1974 
1975     /*
1976      * If we are sure that the block group is full (= no more room left for
1977      * new allocation) and the IO for the last usable block is completed, we
1978      * don't need to wait for the other IOs. This holds because we ensure
1979      * the sequential IO submissions using the ZONE_APPEND command for data
1980      * and block_group->meta_write_pointer for metadata.
1981      */
1982     if (!fully_written) {
1983         spin_unlock(&block_group->lock);
1984 
1985         ret = btrfs_inc_block_group_ro(block_group, false);
1986         if (ret)
1987             return ret;
1988 
1989         /* Ensure all writes in this block group finish */
1990         btrfs_wait_block_group_reservations(block_group);
1991         /* No need to wait for NOCOW writers. Zoned mode does not allow that */
1992         btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
1993                      block_group->length);
1994         /* Wait for extent buffers to be written. */
1995         if (is_metadata)
1996             wait_eb_writebacks(block_group);
1997 
1998         spin_lock(&block_group->lock);
1999 
2000         /*
2001          * Bail out if someone already deactivated the block group, or
2002          * allocated space is left in the block group.
2003          */
2004         if (!block_group->zone_is_active) {
2005             spin_unlock(&block_group->lock);
2006             btrfs_dec_block_group_ro(block_group);
2007             return 0;
2008         }
2009 
2010         if (block_group->reserved) {
2011             spin_unlock(&block_group->lock);
2012             btrfs_dec_block_group_ro(block_group);
2013             return -EAGAIN;
2014         }
2015     }
2016 
2017     block_group->zone_is_active = 0;
2018     block_group->alloc_offset = block_group->zone_capacity;
2019     block_group->free_space_ctl->free_space = 0;
2020     btrfs_clear_treelog_bg(block_group);
2021     btrfs_clear_data_reloc_bg(block_group);
2022     spin_unlock(&block_group->lock);
2023 
2024     map = block_group->physical_map;
2025     for (i = 0; i < map->num_stripes; i++) {
2026         struct btrfs_device *device = map->stripes[i].dev;
2027         const u64 physical = map->stripes[i].physical;
2028 
2029         if (device->zone_info->max_active_zones == 0)
2030             continue;
2031 
2032         ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2033                        physical >> SECTOR_SHIFT,
2034                        device->zone_info->zone_size >> SECTOR_SHIFT,
2035                        GFP_NOFS);
2036 
2037         if (ret)
2038             return ret;
2039 
2040         btrfs_dev_clear_active_zone(device, physical);
2041     }
2042 
2043     if (!fully_written)
2044         btrfs_dec_block_group_ro(block_group);
2045 
2046     spin_lock(&fs_info->zone_active_bgs_lock);
2047     ASSERT(!list_empty(&block_group->active_bg_list));
2048     list_del_init(&block_group->active_bg_list);
2049     spin_unlock(&fs_info->zone_active_bgs_lock);
2050 
2051     /* For active_bg_list */
2052     btrfs_put_block_group(block_group);
2053 
2054     clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2055 
2056     return 0;
2057 }
2058 
2059 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2060 {
2061     if (!btrfs_is_zoned(block_group->fs_info))
2062         return 0;
2063 
2064     return do_zone_finish(block_group, false);
2065 }
2066 
2067 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2068 {
2069     struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2070     struct btrfs_device *device;
2071     bool ret = false;
2072 
2073     if (!btrfs_is_zoned(fs_info))
2074         return true;
2075 
2076     /* Check if there is a device with active zones left */
2077     mutex_lock(&fs_info->chunk_mutex);
2078     list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2079         struct btrfs_zoned_device_info *zinfo = device->zone_info;
2080 
2081         if (!device->bdev)
2082             continue;
2083 
2084         if (!zinfo->max_active_zones ||
2085             atomic_read(&zinfo->active_zones_left)) {
2086             ret = true;
2087             break;
2088         }
2089     }
2090     mutex_unlock(&fs_info->chunk_mutex);
2091 
2092     if (!ret)
2093         set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2094 
2095     return ret;
2096 }
2097 
2098 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2099 {
2100     struct btrfs_block_group *block_group;
2101     u64 min_alloc_bytes;
2102 
2103     if (!btrfs_is_zoned(fs_info))
2104         return;
2105 
2106     block_group = btrfs_lookup_block_group(fs_info, logical);
2107     ASSERT(block_group);
2108 
2109     /* No MIXED_BG on zoned btrfs. */
2110     if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2111         min_alloc_bytes = fs_info->sectorsize;
2112     else
2113         min_alloc_bytes = fs_info->nodesize;
2114 
2115     /* Bail out if we can allocate more data from this block group. */
2116     if (logical + length + min_alloc_bytes <=
2117         block_group->start + block_group->zone_capacity)
2118         goto out;
2119 
2120     do_zone_finish(block_group, true);
2121 
2122 out:
2123     btrfs_put_block_group(block_group);
2124 }
2125 
2126 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2127 {
2128     struct btrfs_block_group *bg =
2129         container_of(work, struct btrfs_block_group, zone_finish_work);
2130 
2131     wait_on_extent_buffer_writeback(bg->last_eb);
2132     free_extent_buffer(bg->last_eb);
2133     btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2134     btrfs_put_block_group(bg);
2135 }
2136 
2137 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2138                    struct extent_buffer *eb)
2139 {
2140     if (!bg->seq_zone || eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2141         return;
2142 
2143     if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2144         btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2145               bg->start);
2146         return;
2147     }
2148 
2149     /* For the work */
2150     btrfs_get_block_group(bg);
2151     atomic_inc(&eb->refs);
2152     bg->last_eb = eb;
2153     INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2154     queue_work(system_unbound_wq, &bg->zone_finish_work);
2155 }
2156 
2157 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2158 {
2159     struct btrfs_fs_info *fs_info = bg->fs_info;
2160 
2161     spin_lock(&fs_info->relocation_bg_lock);
2162     if (fs_info->data_reloc_bg == bg->start)
2163         fs_info->data_reloc_bg = 0;
2164     spin_unlock(&fs_info->relocation_bg_lock);
2165 }
2166 
2167 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2168 {
2169     struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2170     struct btrfs_device *device;
2171 
2172     if (!btrfs_is_zoned(fs_info))
2173         return;
2174 
2175     mutex_lock(&fs_devices->device_list_mutex);
2176     list_for_each_entry(device, &fs_devices->devices, dev_list) {
2177         if (device->zone_info) {
2178             vfree(device->zone_info->zone_cache);
2179             device->zone_info->zone_cache = NULL;
2180         }
2181     }
2182     mutex_unlock(&fs_devices->device_list_mutex);
2183 }
2184 
2185 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2186 {
2187     struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2188     struct btrfs_device *device;
2189     u64 used = 0;
2190     u64 total = 0;
2191     u64 factor;
2192 
2193     ASSERT(btrfs_is_zoned(fs_info));
2194 
2195     if (fs_info->bg_reclaim_threshold == 0)
2196         return false;
2197 
2198     mutex_lock(&fs_devices->device_list_mutex);
2199     list_for_each_entry(device, &fs_devices->devices, dev_list) {
2200         if (!device->bdev)
2201             continue;
2202 
2203         total += device->disk_total_bytes;
2204         used += device->bytes_used;
2205     }
2206     mutex_unlock(&fs_devices->device_list_mutex);
2207 
2208     factor = div64_u64(used * 100, total);
2209     return factor >= fs_info->bg_reclaim_threshold;
2210 }
2211 
2212 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2213                        u64 length)
2214 {
2215     struct btrfs_block_group *block_group;
2216 
2217     if (!btrfs_is_zoned(fs_info))
2218         return;
2219 
2220     block_group = btrfs_lookup_block_group(fs_info, logical);
2221     /* It should be called on a previous data relocation block group. */
2222     ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2223 
2224     spin_lock(&block_group->lock);
2225     if (!block_group->zoned_data_reloc_ongoing)
2226         goto out;
2227 
2228     /* All relocation extents are written. */
2229     if (block_group->start + block_group->alloc_offset == logical + length) {
2230         /* Now, release this block group for further allocations. */
2231         block_group->zoned_data_reloc_ongoing = 0;
2232     }
2233 
2234 out:
2235     spin_unlock(&block_group->lock);
2236     btrfs_put_block_group(block_group);
2237 }
2238 
2239 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2240 {
2241     struct btrfs_block_group *block_group;
2242     struct btrfs_block_group *min_bg = NULL;
2243     u64 min_avail = U64_MAX;
2244     int ret;
2245 
2246     spin_lock(&fs_info->zone_active_bgs_lock);
2247     list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2248                 active_bg_list) {
2249         u64 avail;
2250 
2251         spin_lock(&block_group->lock);
2252         if (block_group->reserved ||
2253             (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)) {
2254             spin_unlock(&block_group->lock);
2255             continue;
2256         }
2257 
2258         avail = block_group->zone_capacity - block_group->alloc_offset;
2259         if (min_avail > avail) {
2260             if (min_bg)
2261                 btrfs_put_block_group(min_bg);
2262             min_bg = block_group;
2263             min_avail = avail;
2264             btrfs_get_block_group(min_bg);
2265         }
2266         spin_unlock(&block_group->lock);
2267     }
2268     spin_unlock(&fs_info->zone_active_bgs_lock);
2269 
2270     if (!min_bg)
2271         return 0;
2272 
2273     ret = btrfs_zone_finish(min_bg);
2274     btrfs_put_block_group(min_bg);
2275 
2276     return ret < 0 ? ret : 1;
2277 }
2278 
2279 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2280                 struct btrfs_space_info *space_info,
2281                 bool do_finish)
2282 {
2283     struct btrfs_block_group *bg;
2284     int index;
2285 
2286     if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2287         return 0;
2288 
2289     /* No more block groups to activate */
2290     if (space_info->active_total_bytes == space_info->total_bytes)
2291         return 0;
2292 
2293     for (;;) {
2294         int ret;
2295         bool need_finish = false;
2296 
2297         down_read(&space_info->groups_sem);
2298         for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2299             list_for_each_entry(bg, &space_info->block_groups[index],
2300                         list) {
2301                 if (!spin_trylock(&bg->lock))
2302                     continue;
2303                 if (btrfs_zoned_bg_is_full(bg) || bg->zone_is_active) {
2304                     spin_unlock(&bg->lock);
2305                     continue;
2306                 }
2307                 spin_unlock(&bg->lock);
2308 
2309                 if (btrfs_zone_activate(bg)) {
2310                     up_read(&space_info->groups_sem);
2311                     return 1;
2312                 }
2313 
2314                 need_finish = true;
2315             }
2316         }
2317         up_read(&space_info->groups_sem);
2318 
2319         if (!do_finish || !need_finish)
2320             break;
2321 
2322         ret = btrfs_zone_finish_one_bg(fs_info);
2323         if (ret == 0)
2324             break;
2325         if (ret < 0)
2326             return ret;
2327     }
2328 
2329     return 0;
2330 }