btrfs: zoned: implement cloning for zoned device-replace
[linux-2.6-block.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 "ctree.h"
8 #include "volumes.h"
9 #include "zoned.h"
10 #include "rcu-string.h"
11 #include "disk-io.h"
12 #include "block-group.h"
13 #include "transaction.h"
14 #include "dev-replace.h"
15
16 /* Maximum number of zones to report per blkdev_report_zones() call */
17 #define BTRFS_REPORT_NR_ZONES   4096
18 /* Invalid allocation pointer value for missing devices */
19 #define WP_MISSING_DEV ((u64)-1)
20 /* Pseudo write pointer value for conventional zone */
21 #define WP_CONVENTIONAL ((u64)-2)
22
23 /* Number of superblock log zones */
24 #define BTRFS_NR_SB_LOG_ZONES 2
25
26 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
27 {
28         struct blk_zone *zones = data;
29
30         memcpy(&zones[idx], zone, sizeof(*zone));
31
32         return 0;
33 }
34
35 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
36                             u64 *wp_ret)
37 {
38         bool empty[BTRFS_NR_SB_LOG_ZONES];
39         bool full[BTRFS_NR_SB_LOG_ZONES];
40         sector_t sector;
41
42         ASSERT(zones[0].type != BLK_ZONE_TYPE_CONVENTIONAL &&
43                zones[1].type != BLK_ZONE_TYPE_CONVENTIONAL);
44
45         empty[0] = (zones[0].cond == BLK_ZONE_COND_EMPTY);
46         empty[1] = (zones[1].cond == BLK_ZONE_COND_EMPTY);
47         full[0] = (zones[0].cond == BLK_ZONE_COND_FULL);
48         full[1] = (zones[1].cond == BLK_ZONE_COND_FULL);
49
50         /*
51          * Possible states of log buffer zones
52          *
53          *           Empty[0]  In use[0]  Full[0]
54          * Empty[1]         *          x        0
55          * In use[1]        0          x        0
56          * Full[1]          1          1        C
57          *
58          * Log position:
59          *   *: Special case, no superblock is written
60          *   0: Use write pointer of zones[0]
61          *   1: Use write pointer of zones[1]
62          *   C: Compare super blcoks from zones[0] and zones[1], use the latest
63          *      one determined by generation
64          *   x: Invalid state
65          */
66
67         if (empty[0] && empty[1]) {
68                 /* Special case to distinguish no superblock to read */
69                 *wp_ret = zones[0].start << SECTOR_SHIFT;
70                 return -ENOENT;
71         } else if (full[0] && full[1]) {
72                 /* Compare two super blocks */
73                 struct address_space *mapping = bdev->bd_inode->i_mapping;
74                 struct page *page[BTRFS_NR_SB_LOG_ZONES];
75                 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
76                 int i;
77
78                 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
79                         u64 bytenr;
80
81                         bytenr = ((zones[i].start + zones[i].len)
82                                    << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
83
84                         page[i] = read_cache_page_gfp(mapping,
85                                         bytenr >> PAGE_SHIFT, GFP_NOFS);
86                         if (IS_ERR(page[i])) {
87                                 if (i == 1)
88                                         btrfs_release_disk_super(super[0]);
89                                 return PTR_ERR(page[i]);
90                         }
91                         super[i] = page_address(page[i]);
92                 }
93
94                 if (super[0]->generation > super[1]->generation)
95                         sector = zones[1].start;
96                 else
97                         sector = zones[0].start;
98
99                 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
100                         btrfs_release_disk_super(super[i]);
101         } else if (!full[0] && (empty[1] || full[1])) {
102                 sector = zones[0].wp;
103         } else if (full[0]) {
104                 sector = zones[1].wp;
105         } else {
106                 return -EUCLEAN;
107         }
108         *wp_ret = sector << SECTOR_SHIFT;
109         return 0;
110 }
111
112 /*
113  * The following zones are reserved as the circular buffer on ZONED btrfs.
114  *  - The primary superblock: zones 0 and 1
115  *  - The first copy: zones 16 and 17
116  *  - The second copy: zones 1024 or zone at 256GB which is minimum, and
117  *                     the following one
118  */
119 static inline u32 sb_zone_number(int shift, int mirror)
120 {
121         ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
122
123         switch (mirror) {
124         case 0: return 0;
125         case 1: return 16;
126         case 2: return min_t(u64, btrfs_sb_offset(mirror) >> shift, 1024);
127         }
128
129         return 0;
130 }
131
132 /*
133  * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
134  * device into static sized chunks and fake a conventional zone on each of
135  * them.
136  */
137 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
138                                 struct blk_zone *zones, unsigned int nr_zones)
139 {
140         const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
141         sector_t bdev_size = bdev_nr_sectors(device->bdev);
142         unsigned int i;
143
144         pos >>= SECTOR_SHIFT;
145         for (i = 0; i < nr_zones; i++) {
146                 zones[i].start = i * zone_sectors + pos;
147                 zones[i].len = zone_sectors;
148                 zones[i].capacity = zone_sectors;
149                 zones[i].wp = zones[i].start + zone_sectors;
150                 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
151                 zones[i].cond = BLK_ZONE_COND_NOT_WP;
152
153                 if (zones[i].wp >= bdev_size) {
154                         i++;
155                         break;
156                 }
157         }
158
159         return i;
160 }
161
162 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
163                                struct blk_zone *zones, unsigned int *nr_zones)
164 {
165         int ret;
166
167         if (!*nr_zones)
168                 return 0;
169
170         if (!bdev_is_zoned(device->bdev)) {
171                 ret = emulate_report_zones(device, pos, zones, *nr_zones);
172                 *nr_zones = ret;
173                 return 0;
174         }
175
176         ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
177                                   copy_zone_info_cb, zones);
178         if (ret < 0) {
179                 btrfs_err_in_rcu(device->fs_info,
180                                  "zoned: failed to read zone %llu on %s (devid %llu)",
181                                  pos, rcu_str_deref(device->name),
182                                  device->devid);
183                 return ret;
184         }
185         *nr_zones = ret;
186         if (!ret)
187                 return -EIO;
188
189         return 0;
190 }
191
192 /* The emulated zone size is determined from the size of device extent */
193 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
194 {
195         struct btrfs_path *path;
196         struct btrfs_root *root = fs_info->dev_root;
197         struct btrfs_key key;
198         struct extent_buffer *leaf;
199         struct btrfs_dev_extent *dext;
200         int ret = 0;
201
202         key.objectid = 1;
203         key.type = BTRFS_DEV_EXTENT_KEY;
204         key.offset = 0;
205
206         path = btrfs_alloc_path();
207         if (!path)
208                 return -ENOMEM;
209
210         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
211         if (ret < 0)
212                 goto out;
213
214         if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
215                 ret = btrfs_next_item(root, path);
216                 if (ret < 0)
217                         goto out;
218                 /* No dev extents at all? Not good */
219                 if (ret > 0) {
220                         ret = -EUCLEAN;
221                         goto out;
222                 }
223         }
224
225         leaf = path->nodes[0];
226         dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
227         fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
228         ret = 0;
229
230 out:
231         btrfs_free_path(path);
232
233         return ret;
234 }
235
236 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
237 {
238         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
239         struct btrfs_device *device;
240         int ret = 0;
241
242         /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
243         if (!btrfs_fs_incompat(fs_info, ZONED))
244                 return 0;
245
246         mutex_lock(&fs_devices->device_list_mutex);
247         list_for_each_entry(device, &fs_devices->devices, dev_list) {
248                 /* We can skip reading of zone info for missing devices */
249                 if (!device->bdev)
250                         continue;
251
252                 ret = btrfs_get_dev_zone_info(device);
253                 if (ret)
254                         break;
255         }
256         mutex_unlock(&fs_devices->device_list_mutex);
257
258         return ret;
259 }
260
261 int btrfs_get_dev_zone_info(struct btrfs_device *device)
262 {
263         struct btrfs_fs_info *fs_info = device->fs_info;
264         struct btrfs_zoned_device_info *zone_info = NULL;
265         struct block_device *bdev = device->bdev;
266         struct request_queue *queue = bdev_get_queue(bdev);
267         sector_t nr_sectors;
268         sector_t sector = 0;
269         struct blk_zone *zones = NULL;
270         unsigned int i, nreported = 0, nr_zones;
271         unsigned int zone_sectors;
272         char *model, *emulated;
273         int ret;
274
275         /*
276          * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
277          * yet be set.
278          */
279         if (!btrfs_fs_incompat(fs_info, ZONED))
280                 return 0;
281
282         if (device->zone_info)
283                 return 0;
284
285         zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
286         if (!zone_info)
287                 return -ENOMEM;
288
289         if (!bdev_is_zoned(bdev)) {
290                 if (!fs_info->zone_size) {
291                         ret = calculate_emulated_zone_size(fs_info);
292                         if (ret)
293                                 goto out;
294                 }
295
296                 ASSERT(fs_info->zone_size);
297                 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
298         } else {
299                 zone_sectors = bdev_zone_sectors(bdev);
300         }
301
302         nr_sectors = bdev_nr_sectors(bdev);
303         /* Check if it's power of 2 (see is_power_of_2) */
304         ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
305         zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
306         zone_info->zone_size_shift = ilog2(zone_info->zone_size);
307         zone_info->max_zone_append_size =
308                 (u64)queue_max_zone_append_sectors(queue) << SECTOR_SHIFT;
309         zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
310         if (!IS_ALIGNED(nr_sectors, zone_sectors))
311                 zone_info->nr_zones++;
312
313         zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
314         if (!zone_info->seq_zones) {
315                 ret = -ENOMEM;
316                 goto out;
317         }
318
319         zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
320         if (!zone_info->empty_zones) {
321                 ret = -ENOMEM;
322                 goto out;
323         }
324
325         zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
326         if (!zones) {
327                 ret = -ENOMEM;
328                 goto out;
329         }
330
331         /* Get zones type */
332         while (sector < nr_sectors) {
333                 nr_zones = BTRFS_REPORT_NR_ZONES;
334                 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
335                                           &nr_zones);
336                 if (ret)
337                         goto out;
338
339                 for (i = 0; i < nr_zones; i++) {
340                         if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
341                                 __set_bit(nreported, zone_info->seq_zones);
342                         if (zones[i].cond == BLK_ZONE_COND_EMPTY)
343                                 __set_bit(nreported, zone_info->empty_zones);
344                         nreported++;
345                 }
346                 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
347         }
348
349         if (nreported != zone_info->nr_zones) {
350                 btrfs_err_in_rcu(device->fs_info,
351                                  "inconsistent number of zones on %s (%u/%u)",
352                                  rcu_str_deref(device->name), nreported,
353                                  zone_info->nr_zones);
354                 ret = -EIO;
355                 goto out;
356         }
357
358         /* Validate superblock log */
359         nr_zones = BTRFS_NR_SB_LOG_ZONES;
360         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
361                 u32 sb_zone;
362                 u64 sb_wp;
363                 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
364
365                 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
366                 if (sb_zone + 1 >= zone_info->nr_zones)
367                         continue;
368
369                 sector = sb_zone << (zone_info->zone_size_shift - SECTOR_SHIFT);
370                 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT,
371                                           &zone_info->sb_zones[sb_pos],
372                                           &nr_zones);
373                 if (ret)
374                         goto out;
375
376                 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
377                         btrfs_err_in_rcu(device->fs_info,
378         "zoned: failed to read super block log zone info at devid %llu zone %u",
379                                          device->devid, sb_zone);
380                         ret = -EUCLEAN;
381                         goto out;
382                 }
383
384                 /*
385                  * If zones[0] is conventional, always use the beggining of the
386                  * zone to record superblock. No need to validate in that case.
387                  */
388                 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
389                     BLK_ZONE_TYPE_CONVENTIONAL)
390                         continue;
391
392                 ret = sb_write_pointer(device->bdev,
393                                        &zone_info->sb_zones[sb_pos], &sb_wp);
394                 if (ret != -ENOENT && ret) {
395                         btrfs_err_in_rcu(device->fs_info,
396                         "zoned: super block log zone corrupted devid %llu zone %u",
397                                          device->devid, sb_zone);
398                         ret = -EUCLEAN;
399                         goto out;
400                 }
401         }
402
403
404         kfree(zones);
405
406         device->zone_info = zone_info;
407
408         switch (bdev_zoned_model(bdev)) {
409         case BLK_ZONED_HM:
410                 model = "host-managed zoned";
411                 emulated = "";
412                 break;
413         case BLK_ZONED_HA:
414                 model = "host-aware zoned";
415                 emulated = "";
416                 break;
417         case BLK_ZONED_NONE:
418                 model = "regular";
419                 emulated = "emulated ";
420                 break;
421         default:
422                 /* Just in case */
423                 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
424                                  bdev_zoned_model(bdev),
425                                  rcu_str_deref(device->name));
426                 ret = -EOPNOTSUPP;
427                 goto out_free_zone_info;
428         }
429
430         btrfs_info_in_rcu(fs_info,
431                 "%s block device %s, %u %szones of %llu bytes",
432                 model, rcu_str_deref(device->name), zone_info->nr_zones,
433                 emulated, zone_info->zone_size);
434
435         return 0;
436
437 out:
438         kfree(zones);
439 out_free_zone_info:
440         bitmap_free(zone_info->empty_zones);
441         bitmap_free(zone_info->seq_zones);
442         kfree(zone_info);
443         device->zone_info = NULL;
444
445         return ret;
446 }
447
448 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
449 {
450         struct btrfs_zoned_device_info *zone_info = device->zone_info;
451
452         if (!zone_info)
453                 return;
454
455         bitmap_free(zone_info->seq_zones);
456         bitmap_free(zone_info->empty_zones);
457         kfree(zone_info);
458         device->zone_info = NULL;
459 }
460
461 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
462                        struct blk_zone *zone)
463 {
464         unsigned int nr_zones = 1;
465         int ret;
466
467         ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
468         if (ret != 0 || !nr_zones)
469                 return ret ? ret : -EIO;
470
471         return 0;
472 }
473
474 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
475 {
476         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
477         struct btrfs_device *device;
478         u64 zoned_devices = 0;
479         u64 nr_devices = 0;
480         u64 zone_size = 0;
481         u64 max_zone_append_size = 0;
482         const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
483         int ret = 0;
484
485         /* Count zoned devices */
486         list_for_each_entry(device, &fs_devices->devices, dev_list) {
487                 enum blk_zoned_model model;
488
489                 if (!device->bdev)
490                         continue;
491
492                 model = bdev_zoned_model(device->bdev);
493                 /*
494                  * A Host-Managed zoned device must be used as a zoned device.
495                  * A Host-Aware zoned device and a non-zoned devices can be
496                  * treated as a zoned device, if ZONED flag is enabled in the
497                  * superblock.
498                  */
499                 if (model == BLK_ZONED_HM ||
500                     (model == BLK_ZONED_HA && incompat_zoned) ||
501                     (model == BLK_ZONED_NONE && incompat_zoned)) {
502                         struct btrfs_zoned_device_info *zone_info =
503                                 device->zone_info;
504
505                         zone_info = device->zone_info;
506                         zoned_devices++;
507                         if (!zone_size) {
508                                 zone_size = zone_info->zone_size;
509                         } else if (zone_info->zone_size != zone_size) {
510                                 btrfs_err(fs_info,
511                 "zoned: unequal block device zone sizes: have %llu found %llu",
512                                           device->zone_info->zone_size,
513                                           zone_size);
514                                 ret = -EINVAL;
515                                 goto out;
516                         }
517                         if (!max_zone_append_size ||
518                             (zone_info->max_zone_append_size &&
519                              zone_info->max_zone_append_size < max_zone_append_size))
520                                 max_zone_append_size =
521                                         zone_info->max_zone_append_size;
522                 }
523                 nr_devices++;
524         }
525
526         if (!zoned_devices && !incompat_zoned)
527                 goto out;
528
529         if (!zoned_devices && incompat_zoned) {
530                 /* No zoned block device found on ZONED filesystem */
531                 btrfs_err(fs_info,
532                           "zoned: no zoned devices found on a zoned filesystem");
533                 ret = -EINVAL;
534                 goto out;
535         }
536
537         if (zoned_devices && !incompat_zoned) {
538                 btrfs_err(fs_info,
539                           "zoned: mode not enabled but zoned device found");
540                 ret = -EINVAL;
541                 goto out;
542         }
543
544         if (zoned_devices != nr_devices) {
545                 btrfs_err(fs_info,
546                           "zoned: cannot mix zoned and regular devices");
547                 ret = -EINVAL;
548                 goto out;
549         }
550
551         /*
552          * stripe_size is always aligned to BTRFS_STRIPE_LEN in
553          * __btrfs_alloc_chunk(). Since we want stripe_len == zone_size,
554          * check the alignment here.
555          */
556         if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
557                 btrfs_err(fs_info,
558                           "zoned: zone size %llu not aligned to stripe %u",
559                           zone_size, BTRFS_STRIPE_LEN);
560                 ret = -EINVAL;
561                 goto out;
562         }
563
564         if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
565                 btrfs_err(fs_info, "zoned: mixed block groups not supported");
566                 ret = -EINVAL;
567                 goto out;
568         }
569
570         fs_info->zone_size = zone_size;
571         fs_info->max_zone_append_size = max_zone_append_size;
572         fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
573
574         /*
575          * Check mount options here, because we might change fs_info->zoned
576          * from fs_info->zone_size.
577          */
578         ret = btrfs_check_mountopts_zoned(fs_info);
579         if (ret)
580                 goto out;
581
582         btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
583 out:
584         return ret;
585 }
586
587 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
588 {
589         if (!btrfs_is_zoned(info))
590                 return 0;
591
592         /*
593          * Space cache writing is not COWed. Disable that to avoid write errors
594          * in sequential zones.
595          */
596         if (btrfs_test_opt(info, SPACE_CACHE)) {
597                 btrfs_err(info, "zoned: space cache v1 is not supported");
598                 return -EINVAL;
599         }
600
601         if (btrfs_test_opt(info, NODATACOW)) {
602                 btrfs_err(info, "zoned: NODATACOW not supported");
603                 return -EINVAL;
604         }
605
606         return 0;
607 }
608
609 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
610                            int rw, u64 *bytenr_ret)
611 {
612         u64 wp;
613         int ret;
614
615         if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
616                 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
617                 return 0;
618         }
619
620         ret = sb_write_pointer(bdev, zones, &wp);
621         if (ret != -ENOENT && ret < 0)
622                 return ret;
623
624         if (rw == WRITE) {
625                 struct blk_zone *reset = NULL;
626
627                 if (wp == zones[0].start << SECTOR_SHIFT)
628                         reset = &zones[0];
629                 else if (wp == zones[1].start << SECTOR_SHIFT)
630                         reset = &zones[1];
631
632                 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
633                         ASSERT(reset->cond == BLK_ZONE_COND_FULL);
634
635                         ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
636                                                reset->start, reset->len,
637                                                GFP_NOFS);
638                         if (ret)
639                                 return ret;
640
641                         reset->cond = BLK_ZONE_COND_EMPTY;
642                         reset->wp = reset->start;
643                 }
644         } else if (ret != -ENOENT) {
645                 /* For READ, we want the precious one */
646                 if (wp == zones[0].start << SECTOR_SHIFT)
647                         wp = (zones[1].start + zones[1].len) << SECTOR_SHIFT;
648                 wp -= BTRFS_SUPER_INFO_SIZE;
649         }
650
651         *bytenr_ret = wp;
652         return 0;
653
654 }
655
656 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
657                                u64 *bytenr_ret)
658 {
659         struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
660         unsigned int zone_sectors;
661         u32 sb_zone;
662         int ret;
663         u8 zone_sectors_shift;
664         sector_t nr_sectors;
665         u32 nr_zones;
666
667         if (!bdev_is_zoned(bdev)) {
668                 *bytenr_ret = btrfs_sb_offset(mirror);
669                 return 0;
670         }
671
672         ASSERT(rw == READ || rw == WRITE);
673
674         zone_sectors = bdev_zone_sectors(bdev);
675         if (!is_power_of_2(zone_sectors))
676                 return -EINVAL;
677         zone_sectors_shift = ilog2(zone_sectors);
678         nr_sectors = bdev_nr_sectors(bdev);
679         nr_zones = nr_sectors >> zone_sectors_shift;
680
681         sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
682         if (sb_zone + 1 >= nr_zones)
683                 return -ENOENT;
684
685         ret = blkdev_report_zones(bdev, sb_zone << zone_sectors_shift,
686                                   BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
687                                   zones);
688         if (ret < 0)
689                 return ret;
690         if (ret != BTRFS_NR_SB_LOG_ZONES)
691                 return -EIO;
692
693         return sb_log_location(bdev, zones, rw, bytenr_ret);
694 }
695
696 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
697                           u64 *bytenr_ret)
698 {
699         struct btrfs_zoned_device_info *zinfo = device->zone_info;
700         u32 zone_num;
701
702         /*
703          * For a zoned filesystem on a non-zoned block device, use the same
704          * super block locations as regular filesystem. Doing so, the super
705          * block can always be retrieved and the zoned flag of the volume
706          * detected from the super block information.
707          */
708         if (!bdev_is_zoned(device->bdev)) {
709                 *bytenr_ret = btrfs_sb_offset(mirror);
710                 return 0;
711         }
712
713         zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
714         if (zone_num + 1 >= zinfo->nr_zones)
715                 return -ENOENT;
716
717         return sb_log_location(device->bdev,
718                                &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
719                                rw, bytenr_ret);
720 }
721
722 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
723                                   int mirror)
724 {
725         u32 zone_num;
726
727         if (!zinfo)
728                 return false;
729
730         zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
731         if (zone_num + 1 >= zinfo->nr_zones)
732                 return false;
733
734         if (!test_bit(zone_num, zinfo->seq_zones))
735                 return false;
736
737         return true;
738 }
739
740 void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
741 {
742         struct btrfs_zoned_device_info *zinfo = device->zone_info;
743         struct blk_zone *zone;
744
745         if (!is_sb_log_zone(zinfo, mirror))
746                 return;
747
748         zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
749         if (zone->cond != BLK_ZONE_COND_FULL) {
750                 if (zone->cond == BLK_ZONE_COND_EMPTY)
751                         zone->cond = BLK_ZONE_COND_IMP_OPEN;
752
753                 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
754
755                 if (zone->wp == zone->start + zone->len)
756                         zone->cond = BLK_ZONE_COND_FULL;
757
758                 return;
759         }
760
761         zone++;
762         ASSERT(zone->cond != BLK_ZONE_COND_FULL);
763         if (zone->cond == BLK_ZONE_COND_EMPTY)
764                 zone->cond = BLK_ZONE_COND_IMP_OPEN;
765
766         zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
767
768         if (zone->wp == zone->start + zone->len)
769                 zone->cond = BLK_ZONE_COND_FULL;
770 }
771
772 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
773 {
774         sector_t zone_sectors;
775         sector_t nr_sectors;
776         u8 zone_sectors_shift;
777         u32 sb_zone;
778         u32 nr_zones;
779
780         zone_sectors = bdev_zone_sectors(bdev);
781         zone_sectors_shift = ilog2(zone_sectors);
782         nr_sectors = bdev_nr_sectors(bdev);
783         nr_zones = nr_sectors >> zone_sectors_shift;
784
785         sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
786         if (sb_zone + 1 >= nr_zones)
787                 return -ENOENT;
788
789         return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
790                                 sb_zone << zone_sectors_shift,
791                                 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
792 }
793
794 /**
795  * btrfs_find_allocatable_zones - find allocatable zones within a given region
796  *
797  * @device:     the device to allocate a region on
798  * @hole_start: the position of the hole to allocate the region
799  * @num_bytes:  size of wanted region
800  * @hole_end:   the end of the hole
801  * @return:     position of allocatable zones
802  *
803  * Allocatable region should not contain any superblock locations.
804  */
805 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
806                                  u64 hole_end, u64 num_bytes)
807 {
808         struct btrfs_zoned_device_info *zinfo = device->zone_info;
809         const u8 shift = zinfo->zone_size_shift;
810         u64 nzones = num_bytes >> shift;
811         u64 pos = hole_start;
812         u64 begin, end;
813         bool have_sb;
814         int i;
815
816         ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
817         ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
818
819         while (pos < hole_end) {
820                 begin = pos >> shift;
821                 end = begin + nzones;
822
823                 if (end > zinfo->nr_zones)
824                         return hole_end;
825
826                 /* Check if zones in the region are all empty */
827                 if (btrfs_dev_is_sequential(device, pos) &&
828                     find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
829                         pos += zinfo->zone_size;
830                         continue;
831                 }
832
833                 have_sb = false;
834                 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
835                         u32 sb_zone;
836                         u64 sb_pos;
837
838                         sb_zone = sb_zone_number(shift, i);
839                         if (!(end <= sb_zone ||
840                               sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
841                                 have_sb = true;
842                                 pos = ((u64)sb_zone + BTRFS_NR_SB_LOG_ZONES) << shift;
843                                 break;
844                         }
845
846                         /* We also need to exclude regular superblock positions */
847                         sb_pos = btrfs_sb_offset(i);
848                         if (!(pos + num_bytes <= sb_pos ||
849                               sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
850                                 have_sb = true;
851                                 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
852                                             zinfo->zone_size);
853                                 break;
854                         }
855                 }
856                 if (!have_sb)
857                         break;
858         }
859
860         return pos;
861 }
862
863 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
864                             u64 length, u64 *bytes)
865 {
866         int ret;
867
868         *bytes = 0;
869         ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
870                                physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
871                                GFP_NOFS);
872         if (ret)
873                 return ret;
874
875         *bytes = length;
876         while (length) {
877                 btrfs_dev_set_zone_empty(device, physical);
878                 physical += device->zone_info->zone_size;
879                 length -= device->zone_info->zone_size;
880         }
881
882         return 0;
883 }
884
885 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
886 {
887         struct btrfs_zoned_device_info *zinfo = device->zone_info;
888         const u8 shift = zinfo->zone_size_shift;
889         unsigned long begin = start >> shift;
890         unsigned long end = (start + size) >> shift;
891         u64 pos;
892         int ret;
893
894         ASSERT(IS_ALIGNED(start, zinfo->zone_size));
895         ASSERT(IS_ALIGNED(size, zinfo->zone_size));
896
897         if (end > zinfo->nr_zones)
898                 return -ERANGE;
899
900         /* All the zones are conventional */
901         if (find_next_bit(zinfo->seq_zones, begin, end) == end)
902                 return 0;
903
904         /* All the zones are sequential and empty */
905         if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
906             find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
907                 return 0;
908
909         for (pos = start; pos < start + size; pos += zinfo->zone_size) {
910                 u64 reset_bytes;
911
912                 if (!btrfs_dev_is_sequential(device, pos) ||
913                     btrfs_dev_is_empty_zone(device, pos))
914                         continue;
915
916                 /* Free regions should be empty */
917                 btrfs_warn_in_rcu(
918                         device->fs_info,
919                 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
920                         rcu_str_deref(device->name), device->devid, pos >> shift);
921                 WARN_ON_ONCE(1);
922
923                 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
924                                               &reset_bytes);
925                 if (ret)
926                         return ret;
927         }
928
929         return 0;
930 }
931
932 /*
933  * Calculate an allocation pointer from the extent allocation information
934  * for a block group consist of conventional zones. It is pointed to the
935  * end of the highest addressed extent in the block group as an allocation
936  * offset.
937  */
938 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
939                                    u64 *offset_ret)
940 {
941         struct btrfs_fs_info *fs_info = cache->fs_info;
942         struct btrfs_root *root = fs_info->extent_root;
943         struct btrfs_path *path;
944         struct btrfs_key key;
945         struct btrfs_key found_key;
946         int ret;
947         u64 length;
948
949         path = btrfs_alloc_path();
950         if (!path)
951                 return -ENOMEM;
952
953         key.objectid = cache->start + cache->length;
954         key.type = 0;
955         key.offset = 0;
956
957         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
958         /* We should not find the exact match */
959         if (!ret)
960                 ret = -EUCLEAN;
961         if (ret < 0)
962                 goto out;
963
964         ret = btrfs_previous_extent_item(root, path, cache->start);
965         if (ret) {
966                 if (ret == 1) {
967                         ret = 0;
968                         *offset_ret = 0;
969                 }
970                 goto out;
971         }
972
973         btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
974
975         if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
976                 length = found_key.offset;
977         else
978                 length = fs_info->nodesize;
979
980         if (!(found_key.objectid >= cache->start &&
981                found_key.objectid + length <= cache->start + cache->length)) {
982                 ret = -EUCLEAN;
983                 goto out;
984         }
985         *offset_ret = found_key.objectid + length - cache->start;
986         ret = 0;
987
988 out:
989         btrfs_free_path(path);
990         return ret;
991 }
992
993 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
994 {
995         struct btrfs_fs_info *fs_info = cache->fs_info;
996         struct extent_map_tree *em_tree = &fs_info->mapping_tree;
997         struct extent_map *em;
998         struct map_lookup *map;
999         struct btrfs_device *device;
1000         u64 logical = cache->start;
1001         u64 length = cache->length;
1002         u64 physical = 0;
1003         int ret;
1004         int i;
1005         unsigned int nofs_flag;
1006         u64 *alloc_offsets = NULL;
1007         u64 last_alloc = 0;
1008         u32 num_sequential = 0, num_conventional = 0;
1009
1010         if (!btrfs_is_zoned(fs_info))
1011                 return 0;
1012
1013         /* Sanity check */
1014         if (!IS_ALIGNED(length, fs_info->zone_size)) {
1015                 btrfs_err(fs_info,
1016                 "zoned: block group %llu len %llu unaligned to zone size %llu",
1017                           logical, length, fs_info->zone_size);
1018                 return -EIO;
1019         }
1020
1021         /* Get the chunk mapping */
1022         read_lock(&em_tree->lock);
1023         em = lookup_extent_mapping(em_tree, logical, length);
1024         read_unlock(&em_tree->lock);
1025
1026         if (!em)
1027                 return -EINVAL;
1028
1029         map = em->map_lookup;
1030
1031         alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1032         if (!alloc_offsets) {
1033                 free_extent_map(em);
1034                 return -ENOMEM;
1035         }
1036
1037         for (i = 0; i < map->num_stripes; i++) {
1038                 bool is_sequential;
1039                 struct blk_zone zone;
1040                 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1041                 int dev_replace_is_ongoing = 0;
1042
1043                 device = map->stripes[i].dev;
1044                 physical = map->stripes[i].physical;
1045
1046                 if (device->bdev == NULL) {
1047                         alloc_offsets[i] = WP_MISSING_DEV;
1048                         continue;
1049                 }
1050
1051                 is_sequential = btrfs_dev_is_sequential(device, physical);
1052                 if (is_sequential)
1053                         num_sequential++;
1054                 else
1055                         num_conventional++;
1056
1057                 if (!is_sequential) {
1058                         alloc_offsets[i] = WP_CONVENTIONAL;
1059                         continue;
1060                 }
1061
1062                 /*
1063                  * This zone will be used for allocation, so mark this zone
1064                  * non-empty.
1065                  */
1066                 btrfs_dev_clear_zone_empty(device, physical);
1067
1068                 down_read(&dev_replace->rwsem);
1069                 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1070                 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1071                         btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical);
1072                 up_read(&dev_replace->rwsem);
1073
1074                 /*
1075                  * The group is mapped to a sequential zone. Get the zone write
1076                  * pointer to determine the allocation offset within the zone.
1077                  */
1078                 WARN_ON(!IS_ALIGNED(physical, fs_info->zone_size));
1079                 nofs_flag = memalloc_nofs_save();
1080                 ret = btrfs_get_dev_zone(device, physical, &zone);
1081                 memalloc_nofs_restore(nofs_flag);
1082                 if (ret == -EIO || ret == -EOPNOTSUPP) {
1083                         ret = 0;
1084                         alloc_offsets[i] = WP_MISSING_DEV;
1085                         continue;
1086                 } else if (ret) {
1087                         goto out;
1088                 }
1089
1090                 switch (zone.cond) {
1091                 case BLK_ZONE_COND_OFFLINE:
1092                 case BLK_ZONE_COND_READONLY:
1093                         btrfs_err(fs_info,
1094                 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1095                                   physical >> device->zone_info->zone_size_shift,
1096                                   rcu_str_deref(device->name), device->devid);
1097                         alloc_offsets[i] = WP_MISSING_DEV;
1098                         break;
1099                 case BLK_ZONE_COND_EMPTY:
1100                         alloc_offsets[i] = 0;
1101                         break;
1102                 case BLK_ZONE_COND_FULL:
1103                         alloc_offsets[i] = fs_info->zone_size;
1104                         break;
1105                 default:
1106                         /* Partially used zone */
1107                         alloc_offsets[i] =
1108                                         ((zone.wp - zone.start) << SECTOR_SHIFT);
1109                         break;
1110                 }
1111         }
1112
1113         if (num_sequential > 0)
1114                 cache->seq_zone = true;
1115
1116         if (num_conventional > 0) {
1117                 /*
1118                  * Avoid calling calculate_alloc_pointer() for new BG. It
1119                  * is no use for new BG. It must be always 0.
1120                  *
1121                  * Also, we have a lock chain of extent buffer lock ->
1122                  * chunk mutex.  For new BG, this function is called from
1123                  * btrfs_make_block_group() which is already taking the
1124                  * chunk mutex. Thus, we cannot call
1125                  * calculate_alloc_pointer() which takes extent buffer
1126                  * locks to avoid deadlock.
1127                  */
1128                 if (new) {
1129                         cache->alloc_offset = 0;
1130                         goto out;
1131                 }
1132                 ret = calculate_alloc_pointer(cache, &last_alloc);
1133                 if (ret || map->num_stripes == num_conventional) {
1134                         if (!ret)
1135                                 cache->alloc_offset = last_alloc;
1136                         else
1137                                 btrfs_err(fs_info,
1138                         "zoned: failed to determine allocation offset of bg %llu",
1139                                           cache->start);
1140                         goto out;
1141                 }
1142         }
1143
1144         switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1145         case 0: /* single */
1146                 cache->alloc_offset = alloc_offsets[0];
1147                 break;
1148         case BTRFS_BLOCK_GROUP_DUP:
1149         case BTRFS_BLOCK_GROUP_RAID1:
1150         case BTRFS_BLOCK_GROUP_RAID0:
1151         case BTRFS_BLOCK_GROUP_RAID10:
1152         case BTRFS_BLOCK_GROUP_RAID5:
1153         case BTRFS_BLOCK_GROUP_RAID6:
1154                 /* non-single profiles are not supported yet */
1155         default:
1156                 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1157                           btrfs_bg_type_to_raid_name(map->type));
1158                 ret = -EINVAL;
1159                 goto out;
1160         }
1161
1162 out:
1163         /* An extent is allocated after the write pointer */
1164         if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1165                 btrfs_err(fs_info,
1166                           "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1167                           logical, last_alloc, cache->alloc_offset);
1168                 ret = -EIO;
1169         }
1170
1171         if (!ret)
1172                 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1173
1174         kfree(alloc_offsets);
1175         free_extent_map(em);
1176
1177         return ret;
1178 }
1179
1180 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1181 {
1182         u64 unusable, free;
1183
1184         if (!btrfs_is_zoned(cache->fs_info))
1185                 return;
1186
1187         WARN_ON(cache->bytes_super != 0);
1188         unusable = cache->alloc_offset - cache->used;
1189         free = cache->length - cache->alloc_offset;
1190
1191         /* We only need ->free_space in ALLOC_SEQ block groups */
1192         cache->last_byte_to_unpin = (u64)-1;
1193         cache->cached = BTRFS_CACHE_FINISHED;
1194         cache->free_space_ctl->free_space = free;
1195         cache->zone_unusable = unusable;
1196
1197         /* Should not have any excluded extents. Just in case, though */
1198         btrfs_free_excluded_extents(cache);
1199 }
1200
1201 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1202                             struct extent_buffer *eb)
1203 {
1204         struct btrfs_fs_info *fs_info = eb->fs_info;
1205
1206         if (!btrfs_is_zoned(fs_info) ||
1207             btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1208             !list_empty(&eb->release_list))
1209                 return;
1210
1211         set_extent_buffer_dirty(eb);
1212         set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1213                                eb->start + eb->len - 1, EXTENT_DIRTY);
1214         memzero_extent_buffer(eb, 0, eb->len);
1215         set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1216
1217         spin_lock(&trans->releasing_ebs_lock);
1218         list_add_tail(&eb->release_list, &trans->releasing_ebs);
1219         spin_unlock(&trans->releasing_ebs_lock);
1220         atomic_inc(&eb->refs);
1221 }
1222
1223 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1224 {
1225         spin_lock(&trans->releasing_ebs_lock);
1226         while (!list_empty(&trans->releasing_ebs)) {
1227                 struct extent_buffer *eb;
1228
1229                 eb = list_first_entry(&trans->releasing_ebs,
1230                                       struct extent_buffer, release_list);
1231                 list_del_init(&eb->release_list);
1232                 free_extent_buffer(eb);
1233         }
1234         spin_unlock(&trans->releasing_ebs_lock);
1235 }
1236
1237 bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em)
1238 {
1239         struct btrfs_fs_info *fs_info = inode->root->fs_info;
1240         struct btrfs_block_group *cache;
1241         bool ret = false;
1242
1243         if (!btrfs_is_zoned(fs_info))
1244                 return false;
1245
1246         if (!fs_info->max_zone_append_size)
1247                 return false;
1248
1249         if (!is_data_inode(&inode->vfs_inode))
1250                 return false;
1251
1252         cache = btrfs_lookup_block_group(fs_info, em->block_start);
1253         ASSERT(cache);
1254         if (!cache)
1255                 return false;
1256
1257         ret = cache->seq_zone;
1258         btrfs_put_block_group(cache);
1259
1260         return ret;
1261 }
1262
1263 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1264                                  struct bio *bio)
1265 {
1266         struct btrfs_ordered_extent *ordered;
1267         const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1268
1269         if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1270                 return;
1271
1272         ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1273         if (WARN_ON(!ordered))
1274                 return;
1275
1276         ordered->physical = physical;
1277         ordered->disk = bio->bi_disk;
1278         ordered->partno = bio->bi_partno;
1279
1280         btrfs_put_ordered_extent(ordered);
1281 }
1282
1283 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1284 {
1285         struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1286         struct btrfs_fs_info *fs_info = inode->root->fs_info;
1287         struct extent_map_tree *em_tree;
1288         struct extent_map *em;
1289         struct btrfs_ordered_sum *sum;
1290         struct block_device *bdev;
1291         u64 orig_logical = ordered->disk_bytenr;
1292         u64 *logical = NULL;
1293         int nr, stripe_len;
1294
1295         /* Zoned devices should not have partitions. So, we can assume it is 0 */
1296         ASSERT(ordered->partno == 0);
1297         bdev = bdgrab(ordered->disk->part0);
1298         if (WARN_ON(!bdev))
1299                 return;
1300
1301         if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, bdev,
1302                                      ordered->physical, &logical, &nr,
1303                                      &stripe_len)))
1304                 goto out;
1305
1306         WARN_ON(nr != 1);
1307
1308         if (orig_logical == *logical)
1309                 goto out;
1310
1311         ordered->disk_bytenr = *logical;
1312
1313         em_tree = &inode->extent_tree;
1314         write_lock(&em_tree->lock);
1315         em = search_extent_mapping(em_tree, ordered->file_offset,
1316                                    ordered->num_bytes);
1317         em->block_start = *logical;
1318         free_extent_map(em);
1319         write_unlock(&em_tree->lock);
1320
1321         list_for_each_entry(sum, &ordered->list, list) {
1322                 if (*logical < orig_logical)
1323                         sum->bytenr -= orig_logical - *logical;
1324                 else
1325                         sum->bytenr += *logical - orig_logical;
1326         }
1327
1328 out:
1329         kfree(logical);
1330         bdput(bdev);
1331 }
1332
1333 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1334                                     struct extent_buffer *eb,
1335                                     struct btrfs_block_group **cache_ret)
1336 {
1337         struct btrfs_block_group *cache;
1338         bool ret = true;
1339
1340         if (!btrfs_is_zoned(fs_info))
1341                 return true;
1342
1343         cache = *cache_ret;
1344
1345         if (cache && (eb->start < cache->start ||
1346                       cache->start + cache->length <= eb->start)) {
1347                 btrfs_put_block_group(cache);
1348                 cache = NULL;
1349                 *cache_ret = NULL;
1350         }
1351
1352         if (!cache)
1353                 cache = btrfs_lookup_block_group(fs_info, eb->start);
1354
1355         if (cache) {
1356                 if (cache->meta_write_pointer != eb->start) {
1357                         btrfs_put_block_group(cache);
1358                         cache = NULL;
1359                         ret = false;
1360                 } else {
1361                         cache->meta_write_pointer = eb->start + eb->len;
1362                 }
1363
1364                 *cache_ret = cache;
1365         }
1366
1367         return ret;
1368 }
1369
1370 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1371                                      struct extent_buffer *eb)
1372 {
1373         if (!btrfs_is_zoned(eb->fs_info) || !cache)
1374                 return;
1375
1376         ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1377         cache->meta_write_pointer = eb->start;
1378 }