Merge tag 'mm-stable-2022-10-08' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-starfive.git] / fs / btrfs / zoned.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
5 #include <linux/blkdev.h>
6 #include <linux/sched/mm.h>
7 #include <linux/atomic.h>
8 #include <linux/vmalloc.h>
9 #include "ctree.h"
10 #include "volumes.h"
11 #include "zoned.h"
12 #include "rcu-string.h"
13 #include "disk-io.h"
14 #include "block-group.h"
15 #include "transaction.h"
16 #include "dev-replace.h"
17 #include "space-info.h"
18
19 /* Maximum number of zones to report per blkdev_report_zones() call */
20 #define BTRFS_REPORT_NR_ZONES   4096
21 /* Invalid allocation pointer value for missing devices */
22 #define WP_MISSING_DEV ((u64)-1)
23 /* Pseudo write pointer value for conventional zone */
24 #define WP_CONVENTIONAL ((u64)-2)
25
26 /*
27  * Location of the first zone of superblock logging zone pairs.
28  *
29  * - primary superblock:    0B (zone 0)
30  * - first copy:          512G (zone starting at that offset)
31  * - second copy:           4T (zone starting at that offset)
32  */
33 #define BTRFS_SB_LOG_PRIMARY_OFFSET     (0ULL)
34 #define BTRFS_SB_LOG_FIRST_OFFSET       (512ULL * SZ_1G)
35 #define BTRFS_SB_LOG_SECOND_OFFSET      (4096ULL * SZ_1G)
36
37 #define BTRFS_SB_LOG_FIRST_SHIFT        const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
38 #define BTRFS_SB_LOG_SECOND_SHIFT       const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
39
40 /* Number of superblock log zones */
41 #define BTRFS_NR_SB_LOG_ZONES 2
42
43 /*
44  * Minimum of active zones we need:
45  *
46  * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
47  * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
48  * - 1 zone for tree-log dedicated block group
49  * - 1 zone for relocation
50  */
51 #define BTRFS_MIN_ACTIVE_ZONES          (BTRFS_SUPER_MIRROR_MAX + 5)
52
53 /*
54  * Minimum / maximum supported zone size. Currently, SMR disks have a zone
55  * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
56  * We do not expect the zone size to become larger than 8GiB or smaller than
57  * 4MiB in the near future.
58  */
59 #define BTRFS_MAX_ZONE_SIZE             SZ_8G
60 #define BTRFS_MIN_ZONE_SIZE             SZ_4M
61
62 #define SUPER_INFO_SECTORS      ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
63
64 static inline bool sb_zone_is_full(const struct blk_zone *zone)
65 {
66         return (zone->cond == BLK_ZONE_COND_FULL) ||
67                 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
68 }
69
70 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
71 {
72         struct blk_zone *zones = data;
73
74         memcpy(&zones[idx], zone, sizeof(*zone));
75
76         return 0;
77 }
78
79 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
80                             u64 *wp_ret)
81 {
82         bool empty[BTRFS_NR_SB_LOG_ZONES];
83         bool full[BTRFS_NR_SB_LOG_ZONES];
84         sector_t sector;
85         int i;
86
87         for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
88                 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
89                 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
90                 full[i] = sb_zone_is_full(&zones[i]);
91         }
92
93         /*
94          * Possible states of log buffer zones
95          *
96          *           Empty[0]  In use[0]  Full[0]
97          * Empty[1]         *          0        1
98          * In use[1]        x          x        1
99          * Full[1]          0          0        C
100          *
101          * Log position:
102          *   *: Special case, no superblock is written
103          *   0: Use write pointer of zones[0]
104          *   1: Use write pointer of zones[1]
105          *   C: Compare super blocks from zones[0] and zones[1], use the latest
106          *      one determined by generation
107          *   x: Invalid state
108          */
109
110         if (empty[0] && empty[1]) {
111                 /* Special case to distinguish no superblock to read */
112                 *wp_ret = zones[0].start << SECTOR_SHIFT;
113                 return -ENOENT;
114         } else if (full[0] && full[1]) {
115                 /* Compare two super blocks */
116                 struct address_space *mapping = bdev->bd_inode->i_mapping;
117                 struct page *page[BTRFS_NR_SB_LOG_ZONES];
118                 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
119                 int i;
120
121                 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
122                         u64 bytenr;
123
124                         bytenr = ((zones[i].start + zones[i].len)
125                                    << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
126
127                         page[i] = read_cache_page_gfp(mapping,
128                                         bytenr >> PAGE_SHIFT, GFP_NOFS);
129                         if (IS_ERR(page[i])) {
130                                 if (i == 1)
131                                         btrfs_release_disk_super(super[0]);
132                                 return PTR_ERR(page[i]);
133                         }
134                         super[i] = page_address(page[i]);
135                 }
136
137                 if (super[0]->generation > super[1]->generation)
138                         sector = zones[1].start;
139                 else
140                         sector = zones[0].start;
141
142                 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
143                         btrfs_release_disk_super(super[i]);
144         } else if (!full[0] && (empty[1] || full[1])) {
145                 sector = zones[0].wp;
146         } else if (full[0]) {
147                 sector = zones[1].wp;
148         } else {
149                 return -EUCLEAN;
150         }
151         *wp_ret = sector << SECTOR_SHIFT;
152         return 0;
153 }
154
155 /*
156  * Get the first zone number of the superblock mirror
157  */
158 static inline u32 sb_zone_number(int shift, int mirror)
159 {
160         u64 zone;
161
162         ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
163         switch (mirror) {
164         case 0: zone = 0; break;
165         case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
166         case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
167         }
168
169         ASSERT(zone <= U32_MAX);
170
171         return (u32)zone;
172 }
173
174 static inline sector_t zone_start_sector(u32 zone_number,
175                                          struct block_device *bdev)
176 {
177         return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
178 }
179
180 static inline u64 zone_start_physical(u32 zone_number,
181                                       struct btrfs_zoned_device_info *zone_info)
182 {
183         return (u64)zone_number << zone_info->zone_size_shift;
184 }
185
186 /*
187  * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
188  * device into static sized chunks and fake a conventional zone on each of
189  * them.
190  */
191 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
192                                 struct blk_zone *zones, unsigned int nr_zones)
193 {
194         const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
195         sector_t bdev_size = bdev_nr_sectors(device->bdev);
196         unsigned int i;
197
198         pos >>= SECTOR_SHIFT;
199         for (i = 0; i < nr_zones; i++) {
200                 zones[i].start = i * zone_sectors + pos;
201                 zones[i].len = zone_sectors;
202                 zones[i].capacity = zone_sectors;
203                 zones[i].wp = zones[i].start + zone_sectors;
204                 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
205                 zones[i].cond = BLK_ZONE_COND_NOT_WP;
206
207                 if (zones[i].wp >= bdev_size) {
208                         i++;
209                         break;
210                 }
211         }
212
213         return i;
214 }
215
216 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
217                                struct blk_zone *zones, unsigned int *nr_zones)
218 {
219         struct btrfs_zoned_device_info *zinfo = device->zone_info;
220         u32 zno;
221         int ret;
222
223         if (!*nr_zones)
224                 return 0;
225
226         if (!bdev_is_zoned(device->bdev)) {
227                 ret = emulate_report_zones(device, pos, zones, *nr_zones);
228                 *nr_zones = ret;
229                 return 0;
230         }
231
232         /* Check cache */
233         if (zinfo->zone_cache) {
234                 unsigned int i;
235
236                 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
237                 zno = pos >> zinfo->zone_size_shift;
238                 /*
239                  * We cannot report zones beyond the zone end. So, it is OK to
240                  * cap *nr_zones to at the end.
241                  */
242                 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
243
244                 for (i = 0; i < *nr_zones; i++) {
245                         struct blk_zone *zone_info;
246
247                         zone_info = &zinfo->zone_cache[zno + i];
248                         if (!zone_info->len)
249                                 break;
250                 }
251
252                 if (i == *nr_zones) {
253                         /* Cache hit on all the zones */
254                         memcpy(zones, zinfo->zone_cache + zno,
255                                sizeof(*zinfo->zone_cache) * *nr_zones);
256                         return 0;
257                 }
258         }
259
260         ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
261                                   copy_zone_info_cb, zones);
262         if (ret < 0) {
263                 btrfs_err_in_rcu(device->fs_info,
264                                  "zoned: failed to read zone %llu on %s (devid %llu)",
265                                  pos, rcu_str_deref(device->name),
266                                  device->devid);
267                 return ret;
268         }
269         *nr_zones = ret;
270         if (!ret)
271                 return -EIO;
272
273         /* Populate cache */
274         if (zinfo->zone_cache)
275                 memcpy(zinfo->zone_cache + zno, zones,
276                        sizeof(*zinfo->zone_cache) * *nr_zones);
277
278         return 0;
279 }
280
281 /* The emulated zone size is determined from the size of device extent */
282 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
283 {
284         struct btrfs_path *path;
285         struct btrfs_root *root = fs_info->dev_root;
286         struct btrfs_key key;
287         struct extent_buffer *leaf;
288         struct btrfs_dev_extent *dext;
289         int ret = 0;
290
291         key.objectid = 1;
292         key.type = BTRFS_DEV_EXTENT_KEY;
293         key.offset = 0;
294
295         path = btrfs_alloc_path();
296         if (!path)
297                 return -ENOMEM;
298
299         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
300         if (ret < 0)
301                 goto out;
302
303         if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
304                 ret = btrfs_next_leaf(root, path);
305                 if (ret < 0)
306                         goto out;
307                 /* No dev extents at all? Not good */
308                 if (ret > 0) {
309                         ret = -EUCLEAN;
310                         goto out;
311                 }
312         }
313
314         leaf = path->nodes[0];
315         dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
316         fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
317         ret = 0;
318
319 out:
320         btrfs_free_path(path);
321
322         return ret;
323 }
324
325 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
326 {
327         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
328         struct btrfs_device *device;
329         int ret = 0;
330
331         /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
332         if (!btrfs_fs_incompat(fs_info, ZONED))
333                 return 0;
334
335         mutex_lock(&fs_devices->device_list_mutex);
336         list_for_each_entry(device, &fs_devices->devices, dev_list) {
337                 /* We can skip reading of zone info for missing devices */
338                 if (!device->bdev)
339                         continue;
340
341                 ret = btrfs_get_dev_zone_info(device, true);
342                 if (ret)
343                         break;
344         }
345         mutex_unlock(&fs_devices->device_list_mutex);
346
347         return ret;
348 }
349
350 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
351 {
352         struct btrfs_fs_info *fs_info = device->fs_info;
353         struct btrfs_zoned_device_info *zone_info = NULL;
354         struct block_device *bdev = device->bdev;
355         unsigned int max_active_zones;
356         unsigned int nactive;
357         sector_t nr_sectors;
358         sector_t sector = 0;
359         struct blk_zone *zones = NULL;
360         unsigned int i, nreported = 0, nr_zones;
361         sector_t zone_sectors;
362         char *model, *emulated;
363         int ret;
364
365         /*
366          * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
367          * yet be set.
368          */
369         if (!btrfs_fs_incompat(fs_info, ZONED))
370                 return 0;
371
372         if (device->zone_info)
373                 return 0;
374
375         zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
376         if (!zone_info)
377                 return -ENOMEM;
378
379         device->zone_info = zone_info;
380
381         if (!bdev_is_zoned(bdev)) {
382                 if (!fs_info->zone_size) {
383                         ret = calculate_emulated_zone_size(fs_info);
384                         if (ret)
385                                 goto out;
386                 }
387
388                 ASSERT(fs_info->zone_size);
389                 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
390         } else {
391                 zone_sectors = bdev_zone_sectors(bdev);
392         }
393
394         /* Check if it's power of 2 (see is_power_of_2) */
395         ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
396         zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
397
398         /* We reject devices with a zone size larger than 8GB */
399         if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
400                 btrfs_err_in_rcu(fs_info,
401                 "zoned: %s: zone size %llu larger than supported maximum %llu",
402                                  rcu_str_deref(device->name),
403                                  zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
404                 ret = -EINVAL;
405                 goto out;
406         } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
407                 btrfs_err_in_rcu(fs_info,
408                 "zoned: %s: zone size %llu smaller than supported minimum %u",
409                                  rcu_str_deref(device->name),
410                                  zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
411                 ret = -EINVAL;
412                 goto out;
413         }
414
415         nr_sectors = bdev_nr_sectors(bdev);
416         zone_info->zone_size_shift = ilog2(zone_info->zone_size);
417         zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
418         /*
419          * We limit max_zone_append_size also by max_segments *
420          * PAGE_SIZE. Technically, we can have multiple pages per segment. But,
421          * since btrfs adds the pages one by one to a bio, and btrfs cannot
422          * increase the metadata reservation even if it increases the number of
423          * extents, it is safe to stick with the limit.
424          *
425          * With the zoned emulation, we can have non-zoned device on the zoned
426          * mode. In this case, we don't have a valid max zone append size. So,
427          * use max_segments * PAGE_SIZE as the pseudo max_zone_append_size.
428          */
429         if (bdev_is_zoned(bdev)) {
430                 zone_info->max_zone_append_size = min_t(u64,
431                         (u64)bdev_max_zone_append_sectors(bdev) << SECTOR_SHIFT,
432                         (u64)bdev_max_segments(bdev) << PAGE_SHIFT);
433         } else {
434                 zone_info->max_zone_append_size =
435                         (u64)bdev_max_segments(bdev) << PAGE_SHIFT;
436         }
437         if (!IS_ALIGNED(nr_sectors, zone_sectors))
438                 zone_info->nr_zones++;
439
440         max_active_zones = bdev_max_active_zones(bdev);
441         if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
442                 btrfs_err_in_rcu(fs_info,
443 "zoned: %s: max active zones %u is too small, need at least %u active zones",
444                                  rcu_str_deref(device->name), max_active_zones,
445                                  BTRFS_MIN_ACTIVE_ZONES);
446                 ret = -EINVAL;
447                 goto out;
448         }
449         zone_info->max_active_zones = max_active_zones;
450
451         zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
452         if (!zone_info->seq_zones) {
453                 ret = -ENOMEM;
454                 goto out;
455         }
456
457         zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
458         if (!zone_info->empty_zones) {
459                 ret = -ENOMEM;
460                 goto out;
461         }
462
463         zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
464         if (!zone_info->active_zones) {
465                 ret = -ENOMEM;
466                 goto out;
467         }
468
469         zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
470         if (!zones) {
471                 ret = -ENOMEM;
472                 goto out;
473         }
474
475         /*
476          * Enable zone cache only for a zoned device. On a non-zoned device, we
477          * fill the zone info with emulated CONVENTIONAL zones, so no need to
478          * use the cache.
479          */
480         if (populate_cache && bdev_is_zoned(device->bdev)) {
481                 zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
482                                                 zone_info->nr_zones);
483                 if (!zone_info->zone_cache) {
484                         btrfs_err_in_rcu(device->fs_info,
485                                 "zoned: failed to allocate zone cache for %s",
486                                 rcu_str_deref(device->name));
487                         ret = -ENOMEM;
488                         goto out;
489                 }
490         }
491
492         /* Get zones type */
493         nactive = 0;
494         while (sector < nr_sectors) {
495                 nr_zones = BTRFS_REPORT_NR_ZONES;
496                 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
497                                           &nr_zones);
498                 if (ret)
499                         goto out;
500
501                 for (i = 0; i < nr_zones; i++) {
502                         if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
503                                 __set_bit(nreported, zone_info->seq_zones);
504                         switch (zones[i].cond) {
505                         case BLK_ZONE_COND_EMPTY:
506                                 __set_bit(nreported, zone_info->empty_zones);
507                                 break;
508                         case BLK_ZONE_COND_IMP_OPEN:
509                         case BLK_ZONE_COND_EXP_OPEN:
510                         case BLK_ZONE_COND_CLOSED:
511                                 __set_bit(nreported, zone_info->active_zones);
512                                 nactive++;
513                                 break;
514                         }
515                         nreported++;
516                 }
517                 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
518         }
519
520         if (nreported != zone_info->nr_zones) {
521                 btrfs_err_in_rcu(device->fs_info,
522                                  "inconsistent number of zones on %s (%u/%u)",
523                                  rcu_str_deref(device->name), nreported,
524                                  zone_info->nr_zones);
525                 ret = -EIO;
526                 goto out;
527         }
528
529         if (max_active_zones) {
530                 if (nactive > max_active_zones) {
531                         btrfs_err_in_rcu(device->fs_info,
532                         "zoned: %u active zones on %s exceeds max_active_zones %u",
533                                          nactive, rcu_str_deref(device->name),
534                                          max_active_zones);
535                         ret = -EIO;
536                         goto out;
537                 }
538                 atomic_set(&zone_info->active_zones_left,
539                            max_active_zones - nactive);
540         }
541
542         /* Validate superblock log */
543         nr_zones = BTRFS_NR_SB_LOG_ZONES;
544         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
545                 u32 sb_zone;
546                 u64 sb_wp;
547                 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
548
549                 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
550                 if (sb_zone + 1 >= zone_info->nr_zones)
551                         continue;
552
553                 ret = btrfs_get_dev_zones(device,
554                                           zone_start_physical(sb_zone, zone_info),
555                                           &zone_info->sb_zones[sb_pos],
556                                           &nr_zones);
557                 if (ret)
558                         goto out;
559
560                 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
561                         btrfs_err_in_rcu(device->fs_info,
562         "zoned: failed to read super block log zone info at devid %llu zone %u",
563                                          device->devid, sb_zone);
564                         ret = -EUCLEAN;
565                         goto out;
566                 }
567
568                 /*
569                  * If zones[0] is conventional, always use the beginning of the
570                  * zone to record superblock. No need to validate in that case.
571                  */
572                 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
573                     BLK_ZONE_TYPE_CONVENTIONAL)
574                         continue;
575
576                 ret = sb_write_pointer(device->bdev,
577                                        &zone_info->sb_zones[sb_pos], &sb_wp);
578                 if (ret != -ENOENT && ret) {
579                         btrfs_err_in_rcu(device->fs_info,
580                         "zoned: super block log zone corrupted devid %llu zone %u",
581                                          device->devid, sb_zone);
582                         ret = -EUCLEAN;
583                         goto out;
584                 }
585         }
586
587
588         kfree(zones);
589
590         switch (bdev_zoned_model(bdev)) {
591         case BLK_ZONED_HM:
592                 model = "host-managed zoned";
593                 emulated = "";
594                 break;
595         case BLK_ZONED_HA:
596                 model = "host-aware zoned";
597                 emulated = "";
598                 break;
599         case BLK_ZONED_NONE:
600                 model = "regular";
601                 emulated = "emulated ";
602                 break;
603         default:
604                 /* Just in case */
605                 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
606                                  bdev_zoned_model(bdev),
607                                  rcu_str_deref(device->name));
608                 ret = -EOPNOTSUPP;
609                 goto out_free_zone_info;
610         }
611
612         btrfs_info_in_rcu(fs_info,
613                 "%s block device %s, %u %szones of %llu bytes",
614                 model, rcu_str_deref(device->name), zone_info->nr_zones,
615                 emulated, zone_info->zone_size);
616
617         return 0;
618
619 out:
620         kfree(zones);
621 out_free_zone_info:
622         btrfs_destroy_dev_zone_info(device);
623
624         return ret;
625 }
626
627 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
628 {
629         struct btrfs_zoned_device_info *zone_info = device->zone_info;
630
631         if (!zone_info)
632                 return;
633
634         bitmap_free(zone_info->active_zones);
635         bitmap_free(zone_info->seq_zones);
636         bitmap_free(zone_info->empty_zones);
637         vfree(zone_info->zone_cache);
638         kfree(zone_info);
639         device->zone_info = NULL;
640 }
641
642 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
643                        struct blk_zone *zone)
644 {
645         unsigned int nr_zones = 1;
646         int ret;
647
648         ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
649         if (ret != 0 || !nr_zones)
650                 return ret ? ret : -EIO;
651
652         return 0;
653 }
654
655 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
656 {
657         struct btrfs_device *device;
658
659         list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
660                 if (device->bdev &&
661                     bdev_zoned_model(device->bdev) == BLK_ZONED_HM) {
662                         btrfs_err(fs_info,
663                                 "zoned: mode not enabled but zoned device found: %pg",
664                                 device->bdev);
665                         return -EINVAL;
666                 }
667         }
668
669         return 0;
670 }
671
672 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
673 {
674         struct btrfs_device *device;
675         u64 zone_size = 0;
676         u64 max_zone_append_size = 0;
677         int ret;
678
679         /*
680          * Host-Managed devices can't be used without the ZONED flag.  With the
681          * ZONED all devices can be used, using zone emulation if required.
682          */
683         if (!btrfs_fs_incompat(fs_info, ZONED))
684                 return btrfs_check_for_zoned_device(fs_info);
685
686         list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
687                 struct btrfs_zoned_device_info *zone_info = device->zone_info;
688
689                 if (!device->bdev)
690                         continue;
691
692                 if (!zone_size) {
693                         zone_size = zone_info->zone_size;
694                 } else if (zone_info->zone_size != zone_size) {
695                         btrfs_err(fs_info,
696                 "zoned: unequal block device zone sizes: have %llu found %llu",
697                                   zone_info->zone_size, zone_size);
698                         return -EINVAL;
699                 }
700                 if (!max_zone_append_size ||
701                     (zone_info->max_zone_append_size &&
702                      zone_info->max_zone_append_size < max_zone_append_size))
703                         max_zone_append_size = zone_info->max_zone_append_size;
704         }
705
706         /*
707          * stripe_size is always aligned to BTRFS_STRIPE_LEN in
708          * btrfs_create_chunk(). Since we want stripe_len == zone_size,
709          * check the alignment here.
710          */
711         if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
712                 btrfs_err(fs_info,
713                           "zoned: zone size %llu not aligned to stripe %u",
714                           zone_size, BTRFS_STRIPE_LEN);
715                 return -EINVAL;
716         }
717
718         if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
719                 btrfs_err(fs_info, "zoned: mixed block groups not supported");
720                 return -EINVAL;
721         }
722
723         fs_info->zone_size = zone_size;
724         fs_info->max_zone_append_size = ALIGN_DOWN(max_zone_append_size,
725                                                    fs_info->sectorsize);
726         fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
727         if (fs_info->max_zone_append_size < fs_info->max_extent_size)
728                 fs_info->max_extent_size = fs_info->max_zone_append_size;
729
730         /*
731          * Check mount options here, because we might change fs_info->zoned
732          * from fs_info->zone_size.
733          */
734         ret = btrfs_check_mountopts_zoned(fs_info);
735         if (ret)
736                 return ret;
737
738         btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
739         return 0;
740 }
741
742 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
743 {
744         if (!btrfs_is_zoned(info))
745                 return 0;
746
747         /*
748          * Space cache writing is not COWed. Disable that to avoid write errors
749          * in sequential zones.
750          */
751         if (btrfs_test_opt(info, SPACE_CACHE)) {
752                 btrfs_err(info, "zoned: space cache v1 is not supported");
753                 return -EINVAL;
754         }
755
756         if (btrfs_test_opt(info, NODATACOW)) {
757                 btrfs_err(info, "zoned: NODATACOW not supported");
758                 return -EINVAL;
759         }
760
761         return 0;
762 }
763
764 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
765                            int rw, u64 *bytenr_ret)
766 {
767         u64 wp;
768         int ret;
769
770         if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
771                 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
772                 return 0;
773         }
774
775         ret = sb_write_pointer(bdev, zones, &wp);
776         if (ret != -ENOENT && ret < 0)
777                 return ret;
778
779         if (rw == WRITE) {
780                 struct blk_zone *reset = NULL;
781
782                 if (wp == zones[0].start << SECTOR_SHIFT)
783                         reset = &zones[0];
784                 else if (wp == zones[1].start << SECTOR_SHIFT)
785                         reset = &zones[1];
786
787                 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
788                         ASSERT(sb_zone_is_full(reset));
789
790                         ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
791                                                reset->start, reset->len,
792                                                GFP_NOFS);
793                         if (ret)
794                                 return ret;
795
796                         reset->cond = BLK_ZONE_COND_EMPTY;
797                         reset->wp = reset->start;
798                 }
799         } else if (ret != -ENOENT) {
800                 /*
801                  * For READ, we want the previous one. Move write pointer to
802                  * the end of a zone, if it is at the head of a zone.
803                  */
804                 u64 zone_end = 0;
805
806                 if (wp == zones[0].start << SECTOR_SHIFT)
807                         zone_end = zones[1].start + zones[1].capacity;
808                 else if (wp == zones[1].start << SECTOR_SHIFT)
809                         zone_end = zones[0].start + zones[0].capacity;
810                 if (zone_end)
811                         wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
812                                         BTRFS_SUPER_INFO_SIZE);
813
814                 wp -= BTRFS_SUPER_INFO_SIZE;
815         }
816
817         *bytenr_ret = wp;
818         return 0;
819
820 }
821
822 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
823                                u64 *bytenr_ret)
824 {
825         struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
826         sector_t zone_sectors;
827         u32 sb_zone;
828         int ret;
829         u8 zone_sectors_shift;
830         sector_t nr_sectors;
831         u32 nr_zones;
832
833         if (!bdev_is_zoned(bdev)) {
834                 *bytenr_ret = btrfs_sb_offset(mirror);
835                 return 0;
836         }
837
838         ASSERT(rw == READ || rw == WRITE);
839
840         zone_sectors = bdev_zone_sectors(bdev);
841         if (!is_power_of_2(zone_sectors))
842                 return -EINVAL;
843         zone_sectors_shift = ilog2(zone_sectors);
844         nr_sectors = bdev_nr_sectors(bdev);
845         nr_zones = nr_sectors >> zone_sectors_shift;
846
847         sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
848         if (sb_zone + 1 >= nr_zones)
849                 return -ENOENT;
850
851         ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
852                                   BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
853                                   zones);
854         if (ret < 0)
855                 return ret;
856         if (ret != BTRFS_NR_SB_LOG_ZONES)
857                 return -EIO;
858
859         return sb_log_location(bdev, zones, rw, bytenr_ret);
860 }
861
862 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
863                           u64 *bytenr_ret)
864 {
865         struct btrfs_zoned_device_info *zinfo = device->zone_info;
866         u32 zone_num;
867
868         /*
869          * For a zoned filesystem on a non-zoned block device, use the same
870          * super block locations as regular filesystem. Doing so, the super
871          * block can always be retrieved and the zoned flag of the volume
872          * detected from the super block information.
873          */
874         if (!bdev_is_zoned(device->bdev)) {
875                 *bytenr_ret = btrfs_sb_offset(mirror);
876                 return 0;
877         }
878
879         zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
880         if (zone_num + 1 >= zinfo->nr_zones)
881                 return -ENOENT;
882
883         return sb_log_location(device->bdev,
884                                &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
885                                rw, bytenr_ret);
886 }
887
888 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
889                                   int mirror)
890 {
891         u32 zone_num;
892
893         if (!zinfo)
894                 return false;
895
896         zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
897         if (zone_num + 1 >= zinfo->nr_zones)
898                 return false;
899
900         if (!test_bit(zone_num, zinfo->seq_zones))
901                 return false;
902
903         return true;
904 }
905
906 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
907 {
908         struct btrfs_zoned_device_info *zinfo = device->zone_info;
909         struct blk_zone *zone;
910         int i;
911
912         if (!is_sb_log_zone(zinfo, mirror))
913                 return 0;
914
915         zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
916         for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
917                 /* Advance the next zone */
918                 if (zone->cond == BLK_ZONE_COND_FULL) {
919                         zone++;
920                         continue;
921                 }
922
923                 if (zone->cond == BLK_ZONE_COND_EMPTY)
924                         zone->cond = BLK_ZONE_COND_IMP_OPEN;
925
926                 zone->wp += SUPER_INFO_SECTORS;
927
928                 if (sb_zone_is_full(zone)) {
929                         /*
930                          * No room left to write new superblock. Since
931                          * superblock is written with REQ_SYNC, it is safe to
932                          * finish the zone now.
933                          *
934                          * If the write pointer is exactly at the capacity,
935                          * explicit ZONE_FINISH is not necessary.
936                          */
937                         if (zone->wp != zone->start + zone->capacity) {
938                                 int ret;
939
940                                 ret = blkdev_zone_mgmt(device->bdev,
941                                                 REQ_OP_ZONE_FINISH, zone->start,
942                                                 zone->len, GFP_NOFS);
943                                 if (ret)
944                                         return ret;
945                         }
946
947                         zone->wp = zone->start + zone->len;
948                         zone->cond = BLK_ZONE_COND_FULL;
949                 }
950                 return 0;
951         }
952
953         /* All the zones are FULL. Should not reach here. */
954         ASSERT(0);
955         return -EIO;
956 }
957
958 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
959 {
960         sector_t zone_sectors;
961         sector_t nr_sectors;
962         u8 zone_sectors_shift;
963         u32 sb_zone;
964         u32 nr_zones;
965
966         zone_sectors = bdev_zone_sectors(bdev);
967         zone_sectors_shift = ilog2(zone_sectors);
968         nr_sectors = bdev_nr_sectors(bdev);
969         nr_zones = nr_sectors >> zone_sectors_shift;
970
971         sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
972         if (sb_zone + 1 >= nr_zones)
973                 return -ENOENT;
974
975         return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
976                                 zone_start_sector(sb_zone, bdev),
977                                 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
978 }
979
980 /**
981  * btrfs_find_allocatable_zones - find allocatable zones within a given region
982  *
983  * @device:     the device to allocate a region on
984  * @hole_start: the position of the hole to allocate the region
985  * @num_bytes:  size of wanted region
986  * @hole_end:   the end of the hole
987  * @return:     position of allocatable zones
988  *
989  * Allocatable region should not contain any superblock locations.
990  */
991 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
992                                  u64 hole_end, u64 num_bytes)
993 {
994         struct btrfs_zoned_device_info *zinfo = device->zone_info;
995         const u8 shift = zinfo->zone_size_shift;
996         u64 nzones = num_bytes >> shift;
997         u64 pos = hole_start;
998         u64 begin, end;
999         bool have_sb;
1000         int i;
1001
1002         ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
1003         ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
1004
1005         while (pos < hole_end) {
1006                 begin = pos >> shift;
1007                 end = begin + nzones;
1008
1009                 if (end > zinfo->nr_zones)
1010                         return hole_end;
1011
1012                 /* Check if zones in the region are all empty */
1013                 if (btrfs_dev_is_sequential(device, pos) &&
1014                     find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1015                         pos += zinfo->zone_size;
1016                         continue;
1017                 }
1018
1019                 have_sb = false;
1020                 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1021                         u32 sb_zone;
1022                         u64 sb_pos;
1023
1024                         sb_zone = sb_zone_number(shift, i);
1025                         if (!(end <= sb_zone ||
1026                               sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1027                                 have_sb = true;
1028                                 pos = zone_start_physical(
1029                                         sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1030                                 break;
1031                         }
1032
1033                         /* We also need to exclude regular superblock positions */
1034                         sb_pos = btrfs_sb_offset(i);
1035                         if (!(pos + num_bytes <= sb_pos ||
1036                               sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1037                                 have_sb = true;
1038                                 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1039                                             zinfo->zone_size);
1040                                 break;
1041                         }
1042                 }
1043                 if (!have_sb)
1044                         break;
1045         }
1046
1047         return pos;
1048 }
1049
1050 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1051 {
1052         struct btrfs_zoned_device_info *zone_info = device->zone_info;
1053         unsigned int zno = (pos >> zone_info->zone_size_shift);
1054
1055         /* We can use any number of zones */
1056         if (zone_info->max_active_zones == 0)
1057                 return true;
1058
1059         if (!test_bit(zno, zone_info->active_zones)) {
1060                 /* Active zone left? */
1061                 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1062                         return false;
1063                 if (test_and_set_bit(zno, zone_info->active_zones)) {
1064                         /* Someone already set the bit */
1065                         atomic_inc(&zone_info->active_zones_left);
1066                 }
1067         }
1068
1069         return true;
1070 }
1071
1072 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1073 {
1074         struct btrfs_zoned_device_info *zone_info = device->zone_info;
1075         unsigned int zno = (pos >> zone_info->zone_size_shift);
1076
1077         /* We can use any number of zones */
1078         if (zone_info->max_active_zones == 0)
1079                 return;
1080
1081         if (test_and_clear_bit(zno, zone_info->active_zones))
1082                 atomic_inc(&zone_info->active_zones_left);
1083 }
1084
1085 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1086                             u64 length, u64 *bytes)
1087 {
1088         int ret;
1089
1090         *bytes = 0;
1091         ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1092                                physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1093                                GFP_NOFS);
1094         if (ret)
1095                 return ret;
1096
1097         *bytes = length;
1098         while (length) {
1099                 btrfs_dev_set_zone_empty(device, physical);
1100                 btrfs_dev_clear_active_zone(device, physical);
1101                 physical += device->zone_info->zone_size;
1102                 length -= device->zone_info->zone_size;
1103         }
1104
1105         return 0;
1106 }
1107
1108 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1109 {
1110         struct btrfs_zoned_device_info *zinfo = device->zone_info;
1111         const u8 shift = zinfo->zone_size_shift;
1112         unsigned long begin = start >> shift;
1113         unsigned long end = (start + size) >> shift;
1114         u64 pos;
1115         int ret;
1116
1117         ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1118         ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1119
1120         if (end > zinfo->nr_zones)
1121                 return -ERANGE;
1122
1123         /* All the zones are conventional */
1124         if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1125                 return 0;
1126
1127         /* All the zones are sequential and empty */
1128         if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1129             find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1130                 return 0;
1131
1132         for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1133                 u64 reset_bytes;
1134
1135                 if (!btrfs_dev_is_sequential(device, pos) ||
1136                     btrfs_dev_is_empty_zone(device, pos))
1137                         continue;
1138
1139                 /* Free regions should be empty */
1140                 btrfs_warn_in_rcu(
1141                         device->fs_info,
1142                 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1143                         rcu_str_deref(device->name), device->devid, pos >> shift);
1144                 WARN_ON_ONCE(1);
1145
1146                 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1147                                               &reset_bytes);
1148                 if (ret)
1149                         return ret;
1150         }
1151
1152         return 0;
1153 }
1154
1155 /*
1156  * Calculate an allocation pointer from the extent allocation information
1157  * for a block group consist of conventional zones. It is pointed to the
1158  * end of the highest addressed extent in the block group as an allocation
1159  * offset.
1160  */
1161 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1162                                    u64 *offset_ret, bool new)
1163 {
1164         struct btrfs_fs_info *fs_info = cache->fs_info;
1165         struct btrfs_root *root;
1166         struct btrfs_path *path;
1167         struct btrfs_key key;
1168         struct btrfs_key found_key;
1169         int ret;
1170         u64 length;
1171
1172         /*
1173          * Avoid  tree lookups for a new block group, there's no use for it.
1174          * It must always be 0.
1175          *
1176          * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1177          * For new a block group, this function is called from
1178          * btrfs_make_block_group() which is already taking the chunk mutex.
1179          * Thus, we cannot call calculate_alloc_pointer() which takes extent
1180          * buffer locks to avoid deadlock.
1181          */
1182         if (new) {
1183                 *offset_ret = 0;
1184                 return 0;
1185         }
1186
1187         path = btrfs_alloc_path();
1188         if (!path)
1189                 return -ENOMEM;
1190
1191         key.objectid = cache->start + cache->length;
1192         key.type = 0;
1193         key.offset = 0;
1194
1195         root = btrfs_extent_root(fs_info, key.objectid);
1196         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1197         /* We should not find the exact match */
1198         if (!ret)
1199                 ret = -EUCLEAN;
1200         if (ret < 0)
1201                 goto out;
1202
1203         ret = btrfs_previous_extent_item(root, path, cache->start);
1204         if (ret) {
1205                 if (ret == 1) {
1206                         ret = 0;
1207                         *offset_ret = 0;
1208                 }
1209                 goto out;
1210         }
1211
1212         btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1213
1214         if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1215                 length = found_key.offset;
1216         else
1217                 length = fs_info->nodesize;
1218
1219         if (!(found_key.objectid >= cache->start &&
1220                found_key.objectid + length <= cache->start + cache->length)) {
1221                 ret = -EUCLEAN;
1222                 goto out;
1223         }
1224         *offset_ret = found_key.objectid + length - cache->start;
1225         ret = 0;
1226
1227 out:
1228         btrfs_free_path(path);
1229         return ret;
1230 }
1231
1232 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1233 {
1234         struct btrfs_fs_info *fs_info = cache->fs_info;
1235         struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1236         struct extent_map *em;
1237         struct map_lookup *map;
1238         struct btrfs_device *device;
1239         u64 logical = cache->start;
1240         u64 length = cache->length;
1241         int ret;
1242         int i;
1243         unsigned int nofs_flag;
1244         u64 *alloc_offsets = NULL;
1245         u64 *caps = NULL;
1246         u64 *physical = NULL;
1247         unsigned long *active = NULL;
1248         u64 last_alloc = 0;
1249         u32 num_sequential = 0, num_conventional = 0;
1250
1251         if (!btrfs_is_zoned(fs_info))
1252                 return 0;
1253
1254         /* Sanity check */
1255         if (!IS_ALIGNED(length, fs_info->zone_size)) {
1256                 btrfs_err(fs_info,
1257                 "zoned: block group %llu len %llu unaligned to zone size %llu",
1258                           logical, length, fs_info->zone_size);
1259                 return -EIO;
1260         }
1261
1262         /* Get the chunk mapping */
1263         read_lock(&em_tree->lock);
1264         em = lookup_extent_mapping(em_tree, logical, length);
1265         read_unlock(&em_tree->lock);
1266
1267         if (!em)
1268                 return -EINVAL;
1269
1270         map = em->map_lookup;
1271
1272         cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1273         if (!cache->physical_map) {
1274                 ret = -ENOMEM;
1275                 goto out;
1276         }
1277
1278         alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1279         if (!alloc_offsets) {
1280                 ret = -ENOMEM;
1281                 goto out;
1282         }
1283
1284         caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1285         if (!caps) {
1286                 ret = -ENOMEM;
1287                 goto out;
1288         }
1289
1290         physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1291         if (!physical) {
1292                 ret = -ENOMEM;
1293                 goto out;
1294         }
1295
1296         active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1297         if (!active) {
1298                 ret = -ENOMEM;
1299                 goto out;
1300         }
1301
1302         for (i = 0; i < map->num_stripes; i++) {
1303                 bool is_sequential;
1304                 struct blk_zone zone;
1305                 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1306                 int dev_replace_is_ongoing = 0;
1307
1308                 device = map->stripes[i].dev;
1309                 physical[i] = map->stripes[i].physical;
1310
1311                 if (device->bdev == NULL) {
1312                         alloc_offsets[i] = WP_MISSING_DEV;
1313                         continue;
1314                 }
1315
1316                 is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1317                 if (is_sequential)
1318                         num_sequential++;
1319                 else
1320                         num_conventional++;
1321
1322                 /*
1323                  * Consider a zone as active if we can allow any number of
1324                  * active zones.
1325                  */
1326                 if (!device->zone_info->max_active_zones)
1327                         __set_bit(i, active);
1328
1329                 if (!is_sequential) {
1330                         alloc_offsets[i] = WP_CONVENTIONAL;
1331                         continue;
1332                 }
1333
1334                 /*
1335                  * This zone will be used for allocation, so mark this zone
1336                  * non-empty.
1337                  */
1338                 btrfs_dev_clear_zone_empty(device, physical[i]);
1339
1340                 down_read(&dev_replace->rwsem);
1341                 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1342                 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1343                         btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1344                 up_read(&dev_replace->rwsem);
1345
1346                 /*
1347                  * The group is mapped to a sequential zone. Get the zone write
1348                  * pointer to determine the allocation offset within the zone.
1349                  */
1350                 WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1351                 nofs_flag = memalloc_nofs_save();
1352                 ret = btrfs_get_dev_zone(device, physical[i], &zone);
1353                 memalloc_nofs_restore(nofs_flag);
1354                 if (ret == -EIO || ret == -EOPNOTSUPP) {
1355                         ret = 0;
1356                         alloc_offsets[i] = WP_MISSING_DEV;
1357                         continue;
1358                 } else if (ret) {
1359                         goto out;
1360                 }
1361
1362                 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1363                         btrfs_err_in_rcu(fs_info,
1364         "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1365                                 zone.start << SECTOR_SHIFT,
1366                                 rcu_str_deref(device->name), device->devid);
1367                         ret = -EIO;
1368                         goto out;
1369                 }
1370
1371                 caps[i] = (zone.capacity << SECTOR_SHIFT);
1372
1373                 switch (zone.cond) {
1374                 case BLK_ZONE_COND_OFFLINE:
1375                 case BLK_ZONE_COND_READONLY:
1376                         btrfs_err(fs_info,
1377                 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1378                                   physical[i] >> device->zone_info->zone_size_shift,
1379                                   rcu_str_deref(device->name), device->devid);
1380                         alloc_offsets[i] = WP_MISSING_DEV;
1381                         break;
1382                 case BLK_ZONE_COND_EMPTY:
1383                         alloc_offsets[i] = 0;
1384                         break;
1385                 case BLK_ZONE_COND_FULL:
1386                         alloc_offsets[i] = caps[i];
1387                         break;
1388                 default:
1389                         /* Partially used zone */
1390                         alloc_offsets[i] =
1391                                         ((zone.wp - zone.start) << SECTOR_SHIFT);
1392                         __set_bit(i, active);
1393                         break;
1394                 }
1395         }
1396
1397         if (num_sequential > 0)
1398                 cache->seq_zone = true;
1399
1400         if (num_conventional > 0) {
1401                 /* Zone capacity is always zone size in emulation */
1402                 cache->zone_capacity = cache->length;
1403                 ret = calculate_alloc_pointer(cache, &last_alloc, new);
1404                 if (ret) {
1405                         btrfs_err(fs_info,
1406                         "zoned: failed to determine allocation offset of bg %llu",
1407                                   cache->start);
1408                         goto out;
1409                 } else if (map->num_stripes == num_conventional) {
1410                         cache->alloc_offset = last_alloc;
1411                         set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1412                         goto out;
1413                 }
1414         }
1415
1416         switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1417         case 0: /* single */
1418                 if (alloc_offsets[0] == WP_MISSING_DEV) {
1419                         btrfs_err(fs_info,
1420                         "zoned: cannot recover write pointer for zone %llu",
1421                                 physical[0]);
1422                         ret = -EIO;
1423                         goto out;
1424                 }
1425                 cache->alloc_offset = alloc_offsets[0];
1426                 cache->zone_capacity = caps[0];
1427                 if (test_bit(0, active))
1428                         set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1429                 break;
1430         case BTRFS_BLOCK_GROUP_DUP:
1431                 if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1432                         btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1433                         ret = -EINVAL;
1434                         goto out;
1435                 }
1436                 if (alloc_offsets[0] == WP_MISSING_DEV) {
1437                         btrfs_err(fs_info,
1438                         "zoned: cannot recover write pointer for zone %llu",
1439                                 physical[0]);
1440                         ret = -EIO;
1441                         goto out;
1442                 }
1443                 if (alloc_offsets[1] == WP_MISSING_DEV) {
1444                         btrfs_err(fs_info,
1445                         "zoned: cannot recover write pointer for zone %llu",
1446                                 physical[1]);
1447                         ret = -EIO;
1448                         goto out;
1449                 }
1450                 if (alloc_offsets[0] != alloc_offsets[1]) {
1451                         btrfs_err(fs_info,
1452                         "zoned: write pointer offset mismatch of zones in DUP profile");
1453                         ret = -EIO;
1454                         goto out;
1455                 }
1456                 if (test_bit(0, active) != test_bit(1, active)) {
1457                         if (!btrfs_zone_activate(cache)) {
1458                                 ret = -EIO;
1459                                 goto out;
1460                         }
1461                 } else {
1462                         if (test_bit(0, active))
1463                                 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1464                                         &cache->runtime_flags);
1465                 }
1466                 cache->alloc_offset = alloc_offsets[0];
1467                 cache->zone_capacity = min(caps[0], caps[1]);
1468                 break;
1469         case BTRFS_BLOCK_GROUP_RAID1:
1470         case BTRFS_BLOCK_GROUP_RAID0:
1471         case BTRFS_BLOCK_GROUP_RAID10:
1472         case BTRFS_BLOCK_GROUP_RAID5:
1473         case BTRFS_BLOCK_GROUP_RAID6:
1474                 /* non-single profiles are not supported yet */
1475         default:
1476                 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1477                           btrfs_bg_type_to_raid_name(map->type));
1478                 ret = -EINVAL;
1479                 goto out;
1480         }
1481
1482 out:
1483         if (cache->alloc_offset > fs_info->zone_size) {
1484                 btrfs_err(fs_info,
1485                         "zoned: invalid write pointer %llu in block group %llu",
1486                         cache->alloc_offset, cache->start);
1487                 ret = -EIO;
1488         }
1489
1490         if (cache->alloc_offset > cache->zone_capacity) {
1491                 btrfs_err(fs_info,
1492 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1493                           cache->alloc_offset, cache->zone_capacity,
1494                           cache->start);
1495                 ret = -EIO;
1496         }
1497
1498         /* An extent is allocated after the write pointer */
1499         if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1500                 btrfs_err(fs_info,
1501                           "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1502                           logical, last_alloc, cache->alloc_offset);
1503                 ret = -EIO;
1504         }
1505
1506         if (!ret) {
1507                 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1508                 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1509                         btrfs_get_block_group(cache);
1510                         spin_lock(&fs_info->zone_active_bgs_lock);
1511                         list_add_tail(&cache->active_bg_list,
1512                                       &fs_info->zone_active_bgs);
1513                         spin_unlock(&fs_info->zone_active_bgs_lock);
1514                 }
1515         } else {
1516                 kfree(cache->physical_map);
1517                 cache->physical_map = NULL;
1518         }
1519         bitmap_free(active);
1520         kfree(physical);
1521         kfree(caps);
1522         kfree(alloc_offsets);
1523         free_extent_map(em);
1524
1525         return ret;
1526 }
1527
1528 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1529 {
1530         u64 unusable, free;
1531
1532         if (!btrfs_is_zoned(cache->fs_info))
1533                 return;
1534
1535         WARN_ON(cache->bytes_super != 0);
1536         unusable = (cache->alloc_offset - cache->used) +
1537                    (cache->length - cache->zone_capacity);
1538         free = cache->zone_capacity - cache->alloc_offset;
1539
1540         /* We only need ->free_space in ALLOC_SEQ block groups */
1541         cache->cached = BTRFS_CACHE_FINISHED;
1542         cache->free_space_ctl->free_space = free;
1543         cache->zone_unusable = unusable;
1544 }
1545
1546 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1547                             struct extent_buffer *eb)
1548 {
1549         struct btrfs_fs_info *fs_info = eb->fs_info;
1550
1551         if (!btrfs_is_zoned(fs_info) ||
1552             btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1553             !list_empty(&eb->release_list))
1554                 return;
1555
1556         set_extent_buffer_dirty(eb);
1557         set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1558                                eb->start + eb->len - 1, EXTENT_DIRTY);
1559         memzero_extent_buffer(eb, 0, eb->len);
1560         set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1561
1562         spin_lock(&trans->releasing_ebs_lock);
1563         list_add_tail(&eb->release_list, &trans->releasing_ebs);
1564         spin_unlock(&trans->releasing_ebs_lock);
1565         atomic_inc(&eb->refs);
1566 }
1567
1568 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1569 {
1570         spin_lock(&trans->releasing_ebs_lock);
1571         while (!list_empty(&trans->releasing_ebs)) {
1572                 struct extent_buffer *eb;
1573
1574                 eb = list_first_entry(&trans->releasing_ebs,
1575                                       struct extent_buffer, release_list);
1576                 list_del_init(&eb->release_list);
1577                 free_extent_buffer(eb);
1578         }
1579         spin_unlock(&trans->releasing_ebs_lock);
1580 }
1581
1582 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1583 {
1584         struct btrfs_fs_info *fs_info = inode->root->fs_info;
1585         struct btrfs_block_group *cache;
1586         bool ret = false;
1587
1588         if (!btrfs_is_zoned(fs_info))
1589                 return false;
1590
1591         if (!is_data_inode(&inode->vfs_inode))
1592                 return false;
1593
1594         /*
1595          * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1596          * extent layout the relocation code has.
1597          * Furthermore we have set aside own block-group from which only the
1598          * relocation "process" can allocate and make sure only one process at a
1599          * time can add pages to an extent that gets relocated, so it's safe to
1600          * use regular REQ_OP_WRITE for this special case.
1601          */
1602         if (btrfs_is_data_reloc_root(inode->root))
1603                 return false;
1604
1605         cache = btrfs_lookup_block_group(fs_info, start);
1606         ASSERT(cache);
1607         if (!cache)
1608                 return false;
1609
1610         ret = cache->seq_zone;
1611         btrfs_put_block_group(cache);
1612
1613         return ret;
1614 }
1615
1616 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1617                                  struct bio *bio)
1618 {
1619         struct btrfs_ordered_extent *ordered;
1620         const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1621
1622         if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1623                 return;
1624
1625         ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1626         if (WARN_ON(!ordered))
1627                 return;
1628
1629         ordered->physical = physical;
1630         ordered->bdev = bio->bi_bdev;
1631
1632         btrfs_put_ordered_extent(ordered);
1633 }
1634
1635 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1636 {
1637         struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1638         struct btrfs_fs_info *fs_info = inode->root->fs_info;
1639         struct extent_map_tree *em_tree;
1640         struct extent_map *em;
1641         struct btrfs_ordered_sum *sum;
1642         u64 orig_logical = ordered->disk_bytenr;
1643         u64 *logical = NULL;
1644         int nr, stripe_len;
1645
1646         /* Zoned devices should not have partitions. So, we can assume it is 0 */
1647         ASSERT(!bdev_is_partition(ordered->bdev));
1648         if (WARN_ON(!ordered->bdev))
1649                 return;
1650
1651         if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1652                                      ordered->physical, &logical, &nr,
1653                                      &stripe_len)))
1654                 goto out;
1655
1656         WARN_ON(nr != 1);
1657
1658         if (orig_logical == *logical)
1659                 goto out;
1660
1661         ordered->disk_bytenr = *logical;
1662
1663         em_tree = &inode->extent_tree;
1664         write_lock(&em_tree->lock);
1665         em = search_extent_mapping(em_tree, ordered->file_offset,
1666                                    ordered->num_bytes);
1667         em->block_start = *logical;
1668         free_extent_map(em);
1669         write_unlock(&em_tree->lock);
1670
1671         list_for_each_entry(sum, &ordered->list, list) {
1672                 if (*logical < orig_logical)
1673                         sum->bytenr -= orig_logical - *logical;
1674                 else
1675                         sum->bytenr += *logical - orig_logical;
1676         }
1677
1678 out:
1679         kfree(logical);
1680 }
1681
1682 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1683                                     struct extent_buffer *eb,
1684                                     struct btrfs_block_group **cache_ret)
1685 {
1686         struct btrfs_block_group *cache;
1687         bool ret = true;
1688
1689         if (!btrfs_is_zoned(fs_info))
1690                 return true;
1691
1692         cache = btrfs_lookup_block_group(fs_info, eb->start);
1693         if (!cache)
1694                 return true;
1695
1696         if (cache->meta_write_pointer != eb->start) {
1697                 btrfs_put_block_group(cache);
1698                 cache = NULL;
1699                 ret = false;
1700         } else {
1701                 cache->meta_write_pointer = eb->start + eb->len;
1702         }
1703
1704         *cache_ret = cache;
1705
1706         return ret;
1707 }
1708
1709 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1710                                      struct extent_buffer *eb)
1711 {
1712         if (!btrfs_is_zoned(eb->fs_info) || !cache)
1713                 return;
1714
1715         ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1716         cache->meta_write_pointer = eb->start;
1717 }
1718
1719 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1720 {
1721         if (!btrfs_dev_is_sequential(device, physical))
1722                 return -EOPNOTSUPP;
1723
1724         return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1725                                     length >> SECTOR_SHIFT, GFP_NOFS, 0);
1726 }
1727
1728 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1729                           struct blk_zone *zone)
1730 {
1731         struct btrfs_io_context *bioc = NULL;
1732         u64 mapped_length = PAGE_SIZE;
1733         unsigned int nofs_flag;
1734         int nmirrors;
1735         int i, ret;
1736
1737         ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1738                                &mapped_length, &bioc);
1739         if (ret || !bioc || mapped_length < PAGE_SIZE) {
1740                 ret = -EIO;
1741                 goto out_put_bioc;
1742         }
1743
1744         if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1745                 ret = -EINVAL;
1746                 goto out_put_bioc;
1747         }
1748
1749         nofs_flag = memalloc_nofs_save();
1750         nmirrors = (int)bioc->num_stripes;
1751         for (i = 0; i < nmirrors; i++) {
1752                 u64 physical = bioc->stripes[i].physical;
1753                 struct btrfs_device *dev = bioc->stripes[i].dev;
1754
1755                 /* Missing device */
1756                 if (!dev->bdev)
1757                         continue;
1758
1759                 ret = btrfs_get_dev_zone(dev, physical, zone);
1760                 /* Failing device */
1761                 if (ret == -EIO || ret == -EOPNOTSUPP)
1762                         continue;
1763                 break;
1764         }
1765         memalloc_nofs_restore(nofs_flag);
1766 out_put_bioc:
1767         btrfs_put_bioc(bioc);
1768         return ret;
1769 }
1770
1771 /*
1772  * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1773  * filling zeros between @physical_pos to a write pointer of dev-replace
1774  * source device.
1775  */
1776 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1777                                     u64 physical_start, u64 physical_pos)
1778 {
1779         struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1780         struct blk_zone zone;
1781         u64 length;
1782         u64 wp;
1783         int ret;
1784
1785         if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1786                 return 0;
1787
1788         ret = read_zone_info(fs_info, logical, &zone);
1789         if (ret)
1790                 return ret;
1791
1792         wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1793
1794         if (physical_pos == wp)
1795                 return 0;
1796
1797         if (physical_pos > wp)
1798                 return -EUCLEAN;
1799
1800         length = wp - physical_pos;
1801         return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1802 }
1803
1804 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1805                                             u64 logical, u64 length)
1806 {
1807         struct btrfs_device *device;
1808         struct extent_map *em;
1809         struct map_lookup *map;
1810
1811         em = btrfs_get_chunk_map(fs_info, logical, length);
1812         if (IS_ERR(em))
1813                 return ERR_CAST(em);
1814
1815         map = em->map_lookup;
1816         /* We only support single profile for now */
1817         device = map->stripes[0].dev;
1818
1819         free_extent_map(em);
1820
1821         return device;
1822 }
1823
1824 /**
1825  * Activate block group and underlying device zones
1826  *
1827  * @block_group: the block group to activate
1828  *
1829  * Return: true on success, false otherwise
1830  */
1831 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1832 {
1833         struct btrfs_fs_info *fs_info = block_group->fs_info;
1834         struct btrfs_space_info *space_info = block_group->space_info;
1835         struct map_lookup *map;
1836         struct btrfs_device *device;
1837         u64 physical;
1838         bool ret;
1839         int i;
1840
1841         if (!btrfs_is_zoned(block_group->fs_info))
1842                 return true;
1843
1844         map = block_group->physical_map;
1845
1846         spin_lock(&space_info->lock);
1847         spin_lock(&block_group->lock);
1848         if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1849                 ret = true;
1850                 goto out_unlock;
1851         }
1852
1853         /* No space left */
1854         if (btrfs_zoned_bg_is_full(block_group)) {
1855                 ret = false;
1856                 goto out_unlock;
1857         }
1858
1859         for (i = 0; i < map->num_stripes; i++) {
1860                 device = map->stripes[i].dev;
1861                 physical = map->stripes[i].physical;
1862
1863                 if (device->zone_info->max_active_zones == 0)
1864                         continue;
1865
1866                 if (!btrfs_dev_set_active_zone(device, physical)) {
1867                         /* Cannot activate the zone */
1868                         ret = false;
1869                         goto out_unlock;
1870                 }
1871         }
1872
1873         /* Successfully activated all the zones */
1874         set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
1875         space_info->active_total_bytes += block_group->length;
1876         spin_unlock(&block_group->lock);
1877         btrfs_try_granting_tickets(fs_info, space_info);
1878         spin_unlock(&space_info->lock);
1879
1880         /* For the active block group list */
1881         btrfs_get_block_group(block_group);
1882
1883         spin_lock(&fs_info->zone_active_bgs_lock);
1884         list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1885         spin_unlock(&fs_info->zone_active_bgs_lock);
1886
1887         return true;
1888
1889 out_unlock:
1890         spin_unlock(&block_group->lock);
1891         spin_unlock(&space_info->lock);
1892         return ret;
1893 }
1894
1895 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
1896 {
1897         struct btrfs_fs_info *fs_info = block_group->fs_info;
1898         const u64 end = block_group->start + block_group->length;
1899         struct radix_tree_iter iter;
1900         struct extent_buffer *eb;
1901         void __rcu **slot;
1902
1903         rcu_read_lock();
1904         radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter,
1905                                  block_group->start >> fs_info->sectorsize_bits) {
1906                 eb = radix_tree_deref_slot(slot);
1907                 if (!eb)
1908                         continue;
1909                 if (radix_tree_deref_retry(eb)) {
1910                         slot = radix_tree_iter_retry(&iter);
1911                         continue;
1912                 }
1913
1914                 if (eb->start < block_group->start)
1915                         continue;
1916                 if (eb->start >= end)
1917                         break;
1918
1919                 slot = radix_tree_iter_resume(slot, &iter);
1920                 rcu_read_unlock();
1921                 wait_on_extent_buffer_writeback(eb);
1922                 rcu_read_lock();
1923         }
1924         rcu_read_unlock();
1925 }
1926
1927 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
1928 {
1929         struct btrfs_fs_info *fs_info = block_group->fs_info;
1930         struct map_lookup *map;
1931         const bool is_metadata = (block_group->flags &
1932                         (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
1933         int ret = 0;
1934         int i;
1935
1936         spin_lock(&block_group->lock);
1937         if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
1938                 spin_unlock(&block_group->lock);
1939                 return 0;
1940         }
1941
1942         /* Check if we have unwritten allocated space */
1943         if (is_metadata &&
1944             block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
1945                 spin_unlock(&block_group->lock);
1946                 return -EAGAIN;
1947         }
1948
1949         /*
1950          * If we are sure that the block group is full (= no more room left for
1951          * new allocation) and the IO for the last usable block is completed, we
1952          * don't need to wait for the other IOs. This holds because we ensure
1953          * the sequential IO submissions using the ZONE_APPEND command for data
1954          * and block_group->meta_write_pointer for metadata.
1955          */
1956         if (!fully_written) {
1957                 spin_unlock(&block_group->lock);
1958
1959                 ret = btrfs_inc_block_group_ro(block_group, false);
1960                 if (ret)
1961                         return ret;
1962
1963                 /* Ensure all writes in this block group finish */
1964                 btrfs_wait_block_group_reservations(block_group);
1965                 /* No need to wait for NOCOW writers. Zoned mode does not allow that */
1966                 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
1967                                          block_group->length);
1968                 /* Wait for extent buffers to be written. */
1969                 if (is_metadata)
1970                         wait_eb_writebacks(block_group);
1971
1972                 spin_lock(&block_group->lock);
1973
1974                 /*
1975                  * Bail out if someone already deactivated the block group, or
1976                  * allocated space is left in the block group.
1977                  */
1978                 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1979                               &block_group->runtime_flags)) {
1980                         spin_unlock(&block_group->lock);
1981                         btrfs_dec_block_group_ro(block_group);
1982                         return 0;
1983                 }
1984
1985                 if (block_group->reserved) {
1986                         spin_unlock(&block_group->lock);
1987                         btrfs_dec_block_group_ro(block_group);
1988                         return -EAGAIN;
1989                 }
1990         }
1991
1992         clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
1993         block_group->alloc_offset = block_group->zone_capacity;
1994         block_group->free_space_ctl->free_space = 0;
1995         btrfs_clear_treelog_bg(block_group);
1996         btrfs_clear_data_reloc_bg(block_group);
1997         spin_unlock(&block_group->lock);
1998
1999         map = block_group->physical_map;
2000         for (i = 0; i < map->num_stripes; i++) {
2001                 struct btrfs_device *device = map->stripes[i].dev;
2002                 const u64 physical = map->stripes[i].physical;
2003
2004                 if (device->zone_info->max_active_zones == 0)
2005                         continue;
2006
2007                 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2008                                        physical >> SECTOR_SHIFT,
2009                                        device->zone_info->zone_size >> SECTOR_SHIFT,
2010                                        GFP_NOFS);
2011
2012                 if (ret)
2013                         return ret;
2014
2015                 btrfs_dev_clear_active_zone(device, physical);
2016         }
2017
2018         if (!fully_written)
2019                 btrfs_dec_block_group_ro(block_group);
2020
2021         spin_lock(&fs_info->zone_active_bgs_lock);
2022         ASSERT(!list_empty(&block_group->active_bg_list));
2023         list_del_init(&block_group->active_bg_list);
2024         spin_unlock(&fs_info->zone_active_bgs_lock);
2025
2026         /* For active_bg_list */
2027         btrfs_put_block_group(block_group);
2028
2029         clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2030
2031         return 0;
2032 }
2033
2034 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2035 {
2036         if (!btrfs_is_zoned(block_group->fs_info))
2037                 return 0;
2038
2039         return do_zone_finish(block_group, false);
2040 }
2041
2042 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2043 {
2044         struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2045         struct btrfs_device *device;
2046         bool ret = false;
2047
2048         if (!btrfs_is_zoned(fs_info))
2049                 return true;
2050
2051         /* Check if there is a device with active zones left */
2052         mutex_lock(&fs_info->chunk_mutex);
2053         list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2054                 struct btrfs_zoned_device_info *zinfo = device->zone_info;
2055
2056                 if (!device->bdev)
2057                         continue;
2058
2059                 if (!zinfo->max_active_zones ||
2060                     atomic_read(&zinfo->active_zones_left)) {
2061                         ret = true;
2062                         break;
2063                 }
2064         }
2065         mutex_unlock(&fs_info->chunk_mutex);
2066
2067         if (!ret)
2068                 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2069
2070         return ret;
2071 }
2072
2073 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2074 {
2075         struct btrfs_block_group *block_group;
2076         u64 min_alloc_bytes;
2077
2078         if (!btrfs_is_zoned(fs_info))
2079                 return;
2080
2081         block_group = btrfs_lookup_block_group(fs_info, logical);
2082         ASSERT(block_group);
2083
2084         /* No MIXED_BG on zoned btrfs. */
2085         if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2086                 min_alloc_bytes = fs_info->sectorsize;
2087         else
2088                 min_alloc_bytes = fs_info->nodesize;
2089
2090         /* Bail out if we can allocate more data from this block group. */
2091         if (logical + length + min_alloc_bytes <=
2092             block_group->start + block_group->zone_capacity)
2093                 goto out;
2094
2095         do_zone_finish(block_group, true);
2096
2097 out:
2098         btrfs_put_block_group(block_group);
2099 }
2100
2101 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2102 {
2103         struct btrfs_block_group *bg =
2104                 container_of(work, struct btrfs_block_group, zone_finish_work);
2105
2106         wait_on_extent_buffer_writeback(bg->last_eb);
2107         free_extent_buffer(bg->last_eb);
2108         btrfs_zone_finish_endio(bg->fs_info, bg->start, bg->length);
2109         btrfs_put_block_group(bg);
2110 }
2111
2112 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2113                                    struct extent_buffer *eb)
2114 {
2115         if (!bg->seq_zone || eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2116                 return;
2117
2118         if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2119                 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2120                           bg->start);
2121                 return;
2122         }
2123
2124         /* For the work */
2125         btrfs_get_block_group(bg);
2126         atomic_inc(&eb->refs);
2127         bg->last_eb = eb;
2128         INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2129         queue_work(system_unbound_wq, &bg->zone_finish_work);
2130 }
2131
2132 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2133 {
2134         struct btrfs_fs_info *fs_info = bg->fs_info;
2135
2136         spin_lock(&fs_info->relocation_bg_lock);
2137         if (fs_info->data_reloc_bg == bg->start)
2138                 fs_info->data_reloc_bg = 0;
2139         spin_unlock(&fs_info->relocation_bg_lock);
2140 }
2141
2142 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2143 {
2144         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2145         struct btrfs_device *device;
2146
2147         if (!btrfs_is_zoned(fs_info))
2148                 return;
2149
2150         mutex_lock(&fs_devices->device_list_mutex);
2151         list_for_each_entry(device, &fs_devices->devices, dev_list) {
2152                 if (device->zone_info) {
2153                         vfree(device->zone_info->zone_cache);
2154                         device->zone_info->zone_cache = NULL;
2155                 }
2156         }
2157         mutex_unlock(&fs_devices->device_list_mutex);
2158 }
2159
2160 bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info)
2161 {
2162         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2163         struct btrfs_device *device;
2164         u64 used = 0;
2165         u64 total = 0;
2166         u64 factor;
2167
2168         ASSERT(btrfs_is_zoned(fs_info));
2169
2170         if (fs_info->bg_reclaim_threshold == 0)
2171                 return false;
2172
2173         mutex_lock(&fs_devices->device_list_mutex);
2174         list_for_each_entry(device, &fs_devices->devices, dev_list) {
2175                 if (!device->bdev)
2176                         continue;
2177
2178                 total += device->disk_total_bytes;
2179                 used += device->bytes_used;
2180         }
2181         mutex_unlock(&fs_devices->device_list_mutex);
2182
2183         factor = div64_u64(used * 100, total);
2184         return factor >= fs_info->bg_reclaim_threshold;
2185 }
2186
2187 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2188                                        u64 length)
2189 {
2190         struct btrfs_block_group *block_group;
2191
2192         if (!btrfs_is_zoned(fs_info))
2193                 return;
2194
2195         block_group = btrfs_lookup_block_group(fs_info, logical);
2196         /* It should be called on a previous data relocation block group. */
2197         ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2198
2199         spin_lock(&block_group->lock);
2200         if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2201                 goto out;
2202
2203         /* All relocation extents are written. */
2204         if (block_group->start + block_group->alloc_offset == logical + length) {
2205                 /* Now, release this block group for further allocations. */
2206                 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2207                           &block_group->runtime_flags);
2208         }
2209
2210 out:
2211         spin_unlock(&block_group->lock);
2212         btrfs_put_block_group(block_group);
2213 }
2214
2215 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2216 {
2217         struct btrfs_block_group *block_group;
2218         struct btrfs_block_group *min_bg = NULL;
2219         u64 min_avail = U64_MAX;
2220         int ret;
2221
2222         spin_lock(&fs_info->zone_active_bgs_lock);
2223         list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2224                             active_bg_list) {
2225                 u64 avail;
2226
2227                 spin_lock(&block_group->lock);
2228                 if (block_group->reserved ||
2229                     (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)) {
2230                         spin_unlock(&block_group->lock);
2231                         continue;
2232                 }
2233
2234                 avail = block_group->zone_capacity - block_group->alloc_offset;
2235                 if (min_avail > avail) {
2236                         if (min_bg)
2237                                 btrfs_put_block_group(min_bg);
2238                         min_bg = block_group;
2239                         min_avail = avail;
2240                         btrfs_get_block_group(min_bg);
2241                 }
2242                 spin_unlock(&block_group->lock);
2243         }
2244         spin_unlock(&fs_info->zone_active_bgs_lock);
2245
2246         if (!min_bg)
2247                 return 0;
2248
2249         ret = btrfs_zone_finish(min_bg);
2250         btrfs_put_block_group(min_bg);
2251
2252         return ret < 0 ? ret : 1;
2253 }
2254
2255 int btrfs_zoned_activate_one_bg(struct btrfs_fs_info *fs_info,
2256                                 struct btrfs_space_info *space_info,
2257                                 bool do_finish)
2258 {
2259         struct btrfs_block_group *bg;
2260         int index;
2261
2262         if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2263                 return 0;
2264
2265         /* No more block groups to activate */
2266         if (space_info->active_total_bytes == space_info->total_bytes)
2267                 return 0;
2268
2269         for (;;) {
2270                 int ret;
2271                 bool need_finish = false;
2272
2273                 down_read(&space_info->groups_sem);
2274                 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2275                         list_for_each_entry(bg, &space_info->block_groups[index],
2276                                             list) {
2277                                 if (!spin_trylock(&bg->lock))
2278                                         continue;
2279                                 if (btrfs_zoned_bg_is_full(bg) ||
2280                                     test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2281                                              &bg->runtime_flags)) {
2282                                         spin_unlock(&bg->lock);
2283                                         continue;
2284                                 }
2285                                 spin_unlock(&bg->lock);
2286
2287                                 if (btrfs_zone_activate(bg)) {
2288                                         up_read(&space_info->groups_sem);
2289                                         return 1;
2290                                 }
2291
2292                                 need_finish = true;
2293                         }
2294                 }
2295                 up_read(&space_info->groups_sem);
2296
2297                 if (!do_finish || !need_finish)
2298                         break;
2299
2300                 ret = btrfs_zone_finish_one_bg(fs_info);
2301                 if (ret == 0)
2302                         break;
2303                 if (ret < 0)
2304                         return ret;
2305         }
2306
2307         return 0;
2308 }