xref: /linux/fs/btrfs/zoned.c (revision 3cd181cc46d36aa7bd4af85f14639d86a25beaec)
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 "disk-io.h"
13 #include "block-group.h"
14 #include "dev-replace.h"
15 #include "space-info.h"
16 #include "fs.h"
17 #include "accessors.h"
18 #include "bio.h"
19 #include "transaction.h"
20 #include "sysfs.h"
21 
22 /* Maximum number of zones to report per blkdev_report_zones() call */
23 #define BTRFS_REPORT_NR_ZONES   4096
24 /* Invalid allocation pointer value for missing devices */
25 #define WP_MISSING_DEV ((u64)-1)
26 /* Pseudo write pointer value for conventional zone */
27 #define WP_CONVENTIONAL ((u64)-2)
28 
29 /*
30  * Location of the first zone of superblock logging zone pairs.
31  *
32  * - primary superblock:    0B (zone 0)
33  * - first copy:          512G (zone starting at that offset)
34  * - second copy:           4T (zone starting at that offset)
35  */
36 #define BTRFS_SB_LOG_PRIMARY_OFFSET	(0ULL)
37 #define BTRFS_SB_LOG_FIRST_OFFSET	(512ULL * SZ_1G)
38 #define BTRFS_SB_LOG_SECOND_OFFSET	(4096ULL * SZ_1G)
39 
40 #define BTRFS_SB_LOG_FIRST_SHIFT	ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
41 #define BTRFS_SB_LOG_SECOND_SHIFT	ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
42 
43 /* Number of superblock log zones */
44 #define BTRFS_NR_SB_LOG_ZONES 2
45 
46 /* Default number of max active zones when the device has no limits. */
47 #define BTRFS_DEFAULT_MAX_ACTIVE_ZONES	128
48 
49 /*
50  * Minimum of active zones we need:
51  *
52  * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
53  * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
54  * - 1 zone for tree-log dedicated block group
55  * - 1 zone for relocation
56  */
57 #define BTRFS_MIN_ACTIVE_ZONES		(BTRFS_SUPER_MIRROR_MAX + 5)
58 
59 /*
60  * Minimum / maximum supported zone size. Currently, SMR disks have a zone
61  * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
62  * We do not expect the zone size to become larger than 8GiB or smaller than
63  * 4MiB in the near future.
64  */
65 #define BTRFS_MAX_ZONE_SIZE		SZ_8G
66 #define BTRFS_MIN_ZONE_SIZE		SZ_4M
67 
68 #define SUPER_INFO_SECTORS	((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
69 
70 static void wait_eb_writebacks(struct btrfs_block_group *block_group);
71 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written);
72 
73 static inline bool sb_zone_is_full(const struct blk_zone *zone)
74 {
75 	return (zone->cond == BLK_ZONE_COND_FULL) ||
76 		(zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
77 }
78 
79 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
80 {
81 	struct blk_zone *zones = data;
82 
83 	memcpy(&zones[idx], zone, sizeof(*zone));
84 
85 	return 0;
86 }
87 
88 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
89 			    u64 *wp_ret)
90 {
91 	bool empty[BTRFS_NR_SB_LOG_ZONES];
92 	bool full[BTRFS_NR_SB_LOG_ZONES];
93 	sector_t sector;
94 
95 	for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
96 		ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL,
97 		       "zones[%d].type=%d", i, zones[i].type);
98 		empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
99 		full[i] = sb_zone_is_full(&zones[i]);
100 	}
101 
102 	/*
103 	 * Possible states of log buffer zones
104 	 *
105 	 *           Empty[0]  In use[0]  Full[0]
106 	 * Empty[1]         *          0        1
107 	 * In use[1]        x          x        1
108 	 * Full[1]          0          0        C
109 	 *
110 	 * Log position:
111 	 *   *: Special case, no superblock is written
112 	 *   0: Use write pointer of zones[0]
113 	 *   1: Use write pointer of zones[1]
114 	 *   C: Compare super blocks from zones[0] and zones[1], use the latest
115 	 *      one determined by generation
116 	 *   x: Invalid state
117 	 */
118 
119 	if (empty[0] && empty[1]) {
120 		/* Special case to distinguish no superblock to read */
121 		*wp_ret = zones[0].start << SECTOR_SHIFT;
122 		return -ENOENT;
123 	} else if (full[0] && full[1]) {
124 		/* Compare two super blocks */
125 		struct address_space *mapping = bdev->bd_mapping;
126 		struct page *page[BTRFS_NR_SB_LOG_ZONES];
127 		struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
128 
129 		for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
130 			u64 zone_end = (zones[i].start + zones[i].capacity) << SECTOR_SHIFT;
131 			u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) -
132 						BTRFS_SUPER_INFO_SIZE;
133 
134 			page[i] = read_cache_page_gfp(mapping,
135 					bytenr >> PAGE_SHIFT, GFP_NOFS);
136 			if (IS_ERR(page[i])) {
137 				if (i == 1)
138 					btrfs_release_disk_super(super[0]);
139 				return PTR_ERR(page[i]);
140 			}
141 			super[i] = page_address(page[i]);
142 		}
143 
144 		if (btrfs_super_generation(super[0]) >
145 		    btrfs_super_generation(super[1]))
146 			sector = zones[1].start;
147 		else
148 			sector = zones[0].start;
149 
150 		for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
151 			btrfs_release_disk_super(super[i]);
152 	} else if (!full[0] && (empty[1] || full[1])) {
153 		sector = zones[0].wp;
154 	} else if (full[0]) {
155 		sector = zones[1].wp;
156 	} else {
157 		return -EUCLEAN;
158 	}
159 	*wp_ret = sector << SECTOR_SHIFT;
160 	return 0;
161 }
162 
163 /*
164  * Get the first zone number of the superblock mirror
165  */
166 static inline u32 sb_zone_number(int shift, int mirror)
167 {
168 	u64 zone = U64_MAX;
169 
170 	ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX, "mirror=%d", mirror);
171 	switch (mirror) {
172 	case 0: zone = 0; break;
173 	case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
174 	case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
175 	}
176 
177 	ASSERT(zone <= U32_MAX, "zone=%llu", zone);
178 
179 	return (u32)zone;
180 }
181 
182 static inline sector_t zone_start_sector(u32 zone_number,
183 					 struct block_device *bdev)
184 {
185 	return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
186 }
187 
188 static inline u64 zone_start_physical(u32 zone_number,
189 				      struct btrfs_zoned_device_info *zone_info)
190 {
191 	return (u64)zone_number << zone_info->zone_size_shift;
192 }
193 
194 /*
195  * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
196  * device into static sized chunks and fake a conventional zone on each of
197  * them.
198  */
199 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
200 				struct blk_zone *zones, unsigned int nr_zones)
201 {
202 	const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
203 	sector_t bdev_size = bdev_nr_sectors(device->bdev);
204 	unsigned int i;
205 
206 	pos >>= SECTOR_SHIFT;
207 	for (i = 0; i < nr_zones; i++) {
208 		zones[i].start = i * zone_sectors + pos;
209 		zones[i].len = zone_sectors;
210 		zones[i].capacity = zone_sectors;
211 		zones[i].wp = zones[i].start + zone_sectors;
212 		zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
213 		zones[i].cond = BLK_ZONE_COND_NOT_WP;
214 
215 		if (zones[i].wp >= bdev_size) {
216 			i++;
217 			break;
218 		}
219 	}
220 
221 	return i;
222 }
223 
224 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
225 			       struct blk_zone *zones, unsigned int *nr_zones)
226 {
227 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
228 	int ret;
229 
230 	if (!*nr_zones)
231 		return 0;
232 
233 	if (!bdev_is_zoned(device->bdev)) {
234 		ret = emulate_report_zones(device, pos, zones, *nr_zones);
235 		*nr_zones = ret;
236 		return 0;
237 	}
238 
239 	/* Check cache */
240 	if (zinfo->zone_cache) {
241 		unsigned int i;
242 		u32 zno;
243 
244 		ASSERT(IS_ALIGNED(pos, zinfo->zone_size),
245 		       "pos=%llu zinfo->zone_size=%llu", pos, zinfo->zone_size);
246 		zno = pos >> zinfo->zone_size_shift;
247 		/*
248 		 * We cannot report zones beyond the zone end. So, it is OK to
249 		 * cap *nr_zones to at the end.
250 		 */
251 		*nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
252 
253 		for (i = 0; i < *nr_zones; i++) {
254 			struct blk_zone *zone_info;
255 
256 			zone_info = &zinfo->zone_cache[zno + i];
257 			if (!zone_info->len)
258 				break;
259 		}
260 
261 		if (i == *nr_zones) {
262 			/* Cache hit on all the zones */
263 			memcpy(zones, zinfo->zone_cache + zno,
264 			       sizeof(*zinfo->zone_cache) * *nr_zones);
265 			return 0;
266 		}
267 	}
268 
269 	ret = blkdev_report_zones_cached(device->bdev, pos >> SECTOR_SHIFT,
270 					 *nr_zones, copy_zone_info_cb, zones);
271 	if (ret < 0) {
272 		btrfs_err(device->fs_info,
273 				 "zoned: failed to read zone %llu on %s (devid %llu)",
274 				 pos, rcu_dereference(device->name),
275 				 device->devid);
276 		return ret;
277 	}
278 	*nr_zones = ret;
279 	if (unlikely(!ret))
280 		return -EIO;
281 
282 	/* Populate cache */
283 	if (zinfo->zone_cache) {
284 		u32 zno = pos >> zinfo->zone_size_shift;
285 
286 		memcpy(zinfo->zone_cache + zno, zones,
287 		       sizeof(*zinfo->zone_cache) * *nr_zones);
288 	}
289 
290 	return 0;
291 }
292 
293 /* The emulated zone size is determined from the size of device extent */
294 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
295 {
296 	BTRFS_PATH_AUTO_FREE(path);
297 	struct btrfs_root *root = fs_info->dev_root;
298 	struct btrfs_key key;
299 	struct extent_buffer *leaf;
300 	struct btrfs_dev_extent *dext;
301 	int ret = 0;
302 
303 	key.objectid = 1;
304 	key.type = BTRFS_DEV_EXTENT_KEY;
305 	key.offset = 0;
306 
307 	path = btrfs_alloc_path();
308 	if (!path)
309 		return -ENOMEM;
310 
311 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
312 	if (ret < 0)
313 		return ret;
314 
315 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
316 		ret = btrfs_next_leaf(root, path);
317 		if (ret < 0)
318 			return ret;
319 		/* No dev extents at all? Not good */
320 		if (unlikely(ret > 0))
321 			return -EUCLEAN;
322 	}
323 
324 	leaf = path->nodes[0];
325 	dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
326 	fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
327 	return 0;
328 }
329 
330 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
331 {
332 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
333 	struct btrfs_device *device;
334 	int ret = 0;
335 
336 	/* fs_info->zone_size might not set yet. Use the incomapt flag here. */
337 	if (!btrfs_fs_incompat(fs_info, ZONED))
338 		return 0;
339 
340 	/*
341 	 * No need to take the device_list mutex here, we're still in the mount
342 	 * path and devices cannot be added to or removed from the list yet.
343 	 */
344 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
345 		/* We can skip reading of zone info for missing devices */
346 		if (!device->bdev)
347 			continue;
348 
349 		ret = btrfs_get_dev_zone_info(device, true);
350 		if (ret)
351 			break;
352 	}
353 
354 	return ret;
355 }
356 
357 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
358 {
359 	struct btrfs_fs_info *fs_info = device->fs_info;
360 	struct btrfs_zoned_device_info *zone_info = NULL;
361 	struct block_device *bdev = device->bdev;
362 	unsigned int max_active_zones;
363 	unsigned int nactive;
364 	sector_t nr_sectors;
365 	sector_t sector = 0;
366 	struct blk_zone *zones = NULL;
367 	unsigned int i, nreported = 0, nr_zones;
368 	sector_t zone_sectors;
369 	char *model, *emulated;
370 	int ret;
371 
372 	/*
373 	 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
374 	 * yet be set.
375 	 */
376 	if (!btrfs_fs_incompat(fs_info, ZONED))
377 		return 0;
378 
379 	if (device->zone_info)
380 		return 0;
381 
382 	zone_info = kzalloc_obj(*zone_info);
383 	if (!zone_info)
384 		return -ENOMEM;
385 
386 	device->zone_info = zone_info;
387 
388 	if (!bdev_is_zoned(bdev)) {
389 		if (!fs_info->zone_size) {
390 			ret = calculate_emulated_zone_size(fs_info);
391 			if (ret)
392 				goto out;
393 		}
394 
395 		ASSERT(fs_info->zone_size);
396 		zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
397 	} else {
398 		zone_sectors = bdev_zone_sectors(bdev);
399 	}
400 
401 	ASSERT(is_power_of_two_u64(zone_sectors));
402 	zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
403 
404 	/* We reject devices with a zone size larger than 8GB */
405 	if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
406 		btrfs_err(fs_info,
407 		"zoned: %s: zone size %llu larger than supported maximum %llu",
408 				 rcu_dereference(device->name),
409 				 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
410 		ret = -EINVAL;
411 		goto out;
412 	} else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
413 		btrfs_err(fs_info,
414 		"zoned: %s: zone size %llu smaller than supported minimum %u",
415 				 rcu_dereference(device->name),
416 				 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
417 		ret = -EINVAL;
418 		goto out;
419 	}
420 
421 	nr_sectors = bdev_nr_sectors(bdev);
422 	zone_info->zone_size_shift = ilog2(zone_info->zone_size);
423 	zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
424 	if (!IS_ALIGNED(nr_sectors, zone_sectors))
425 		zone_info->nr_zones++;
426 
427 	max_active_zones = min_not_zero(bdev_max_active_zones(bdev),
428 					bdev_max_open_zones(bdev));
429 	if (!max_active_zones && zone_info->nr_zones > BTRFS_DEFAULT_MAX_ACTIVE_ZONES)
430 		max_active_zones = BTRFS_DEFAULT_MAX_ACTIVE_ZONES;
431 	if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
432 		btrfs_err(fs_info,
433 "zoned: %s: max active zones %u is too small, need at least %u active zones",
434 				 rcu_dereference(device->name), max_active_zones,
435 				 BTRFS_MIN_ACTIVE_ZONES);
436 		ret = -EINVAL;
437 		goto out;
438 	}
439 	zone_info->max_active_zones = max_active_zones;
440 
441 	zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
442 	if (!zone_info->seq_zones) {
443 		ret = -ENOMEM;
444 		goto out;
445 	}
446 
447 	zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
448 	if (!zone_info->empty_zones) {
449 		ret = -ENOMEM;
450 		goto out;
451 	}
452 
453 	zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
454 	if (!zone_info->active_zones) {
455 		ret = -ENOMEM;
456 		goto out;
457 	}
458 
459 	zones = kvzalloc_objs(struct blk_zone, BTRFS_REPORT_NR_ZONES);
460 	if (!zones) {
461 		ret = -ENOMEM;
462 		goto out;
463 	}
464 
465 	/*
466 	 * Enable zone cache only for a zoned device. On a non-zoned device, we
467 	 * fill the zone info with emulated CONVENTIONAL zones, so no need to
468 	 * use the cache.
469 	 */
470 	if (populate_cache && bdev_is_zoned(device->bdev)) {
471 		zone_info->zone_cache = vcalloc(zone_info->nr_zones,
472 						sizeof(struct blk_zone));
473 		if (!zone_info->zone_cache) {
474 			btrfs_err(device->fs_info,
475 				"zoned: failed to allocate zone cache for %s",
476 				rcu_dereference(device->name));
477 			ret = -ENOMEM;
478 			goto out;
479 		}
480 	}
481 
482 	/* Get zones type */
483 	nactive = 0;
484 	while (sector < nr_sectors) {
485 		nr_zones = BTRFS_REPORT_NR_ZONES;
486 		ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
487 					  &nr_zones);
488 		if (ret)
489 			goto out;
490 
491 		for (i = 0; i < nr_zones; i++) {
492 			if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
493 				__set_bit(nreported, zone_info->seq_zones);
494 			switch (zones[i].cond) {
495 			case BLK_ZONE_COND_EMPTY:
496 				__set_bit(nreported, zone_info->empty_zones);
497 				break;
498 			case BLK_ZONE_COND_IMP_OPEN:
499 			case BLK_ZONE_COND_EXP_OPEN:
500 			case BLK_ZONE_COND_CLOSED:
501 			case BLK_ZONE_COND_ACTIVE:
502 				__set_bit(nreported, zone_info->active_zones);
503 				nactive++;
504 				break;
505 			}
506 			nreported++;
507 		}
508 		sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
509 	}
510 
511 	if (unlikely(nreported != zone_info->nr_zones)) {
512 		btrfs_err(device->fs_info,
513 				 "inconsistent number of zones on %s (%u/%u)",
514 				 rcu_dereference(device->name), nreported,
515 				 zone_info->nr_zones);
516 		ret = -EIO;
517 		goto out;
518 	}
519 
520 	if (max_active_zones) {
521 		if (unlikely(nactive > max_active_zones)) {
522 			if (bdev_max_active_zones(bdev) == 0) {
523 				max_active_zones = 0;
524 				zone_info->max_active_zones = 0;
525 				goto validate;
526 			}
527 			btrfs_err(device->fs_info,
528 			"zoned: %u active zones on %s exceeds max_active_zones %u",
529 					 nactive, rcu_dereference(device->name),
530 					 max_active_zones);
531 			ret = -EIO;
532 			goto out;
533 		}
534 		atomic_set(&zone_info->active_zones_left,
535 			   max_active_zones - nactive);
536 		set_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags);
537 	}
538 
539 validate:
540 	/* Validate superblock log */
541 	nr_zones = BTRFS_NR_SB_LOG_ZONES;
542 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
543 		u32 sb_zone;
544 		u64 sb_wp;
545 		int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
546 
547 		sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
548 		if (sb_zone + 1 >= zone_info->nr_zones)
549 			continue;
550 
551 		ret = btrfs_get_dev_zones(device,
552 					  zone_start_physical(sb_zone, zone_info),
553 					  &zone_info->sb_zones[sb_pos],
554 					  &nr_zones);
555 		if (ret)
556 			goto out;
557 
558 		if (unlikely(nr_zones != BTRFS_NR_SB_LOG_ZONES)) {
559 			btrfs_err(device->fs_info,
560 	"zoned: failed to read super block log zone info at devid %llu zone %u",
561 					 device->devid, sb_zone);
562 			ret = -EUCLEAN;
563 			goto out;
564 		}
565 
566 		/*
567 		 * If zones[0] is conventional, always use the beginning of the
568 		 * zone to record superblock. No need to validate in that case.
569 		 */
570 		if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
571 		    BLK_ZONE_TYPE_CONVENTIONAL)
572 			continue;
573 
574 		ret = sb_write_pointer(device->bdev,
575 				       &zone_info->sb_zones[sb_pos], &sb_wp);
576 		if (unlikely(ret != -ENOENT && ret)) {
577 			btrfs_err(device->fs_info,
578 			"zoned: super block log zone corrupted devid %llu zone %u",
579 					 device->devid, sb_zone);
580 			ret = -EUCLEAN;
581 			goto out;
582 		}
583 	}
584 
585 
586 	kvfree(zones);
587 
588 	if (bdev_is_zoned(bdev)) {
589 		model = "host-managed zoned";
590 		emulated = "";
591 	} else {
592 		model = "regular";
593 		emulated = "emulated ";
594 	}
595 
596 	btrfs_info(fs_info,
597 		"%s block device %s, %u %szones of %llu bytes",
598 		model, rcu_dereference(device->name), zone_info->nr_zones,
599 		emulated, zone_info->zone_size);
600 
601 	return 0;
602 
603 out:
604 	kvfree(zones);
605 	btrfs_destroy_dev_zone_info(device);
606 	return ret;
607 }
608 
609 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
610 {
611 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
612 
613 	if (!zone_info)
614 		return;
615 
616 	bitmap_free(zone_info->active_zones);
617 	bitmap_free(zone_info->seq_zones);
618 	bitmap_free(zone_info->empty_zones);
619 	vfree(zone_info->zone_cache);
620 	kfree(zone_info);
621 	device->zone_info = NULL;
622 }
623 
624 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
625 {
626 	struct btrfs_zoned_device_info *zone_info;
627 
628 	zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
629 	if (!zone_info)
630 		return NULL;
631 
632 	zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
633 	if (!zone_info->seq_zones)
634 		goto out;
635 
636 	bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
637 		    zone_info->nr_zones);
638 
639 	zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
640 	if (!zone_info->empty_zones)
641 		goto out;
642 
643 	bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
644 		    zone_info->nr_zones);
645 
646 	zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
647 	if (!zone_info->active_zones)
648 		goto out;
649 
650 	bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
651 		    zone_info->nr_zones);
652 	zone_info->zone_cache = NULL;
653 
654 	return zone_info;
655 
656 out:
657 	bitmap_free(zone_info->seq_zones);
658 	bitmap_free(zone_info->empty_zones);
659 	bitmap_free(zone_info->active_zones);
660 	kfree(zone_info);
661 	return NULL;
662 }
663 
664 static int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, struct blk_zone *zone)
665 {
666 	unsigned int nr_zones = 1;
667 	int ret;
668 
669 	ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
670 	if (ret != 0 || !nr_zones)
671 		return ret ? ret : -EIO;
672 
673 	return 0;
674 }
675 
676 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
677 {
678 	struct btrfs_device *device;
679 
680 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
681 		if (device->bdev && bdev_is_zoned(device->bdev)) {
682 			btrfs_err(fs_info,
683 				"zoned: mode not enabled but zoned device found: %pg",
684 				device->bdev);
685 			return -EINVAL;
686 		}
687 	}
688 
689 	return 0;
690 }
691 
692 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
693 {
694 	struct queue_limits *lim = &fs_info->limits;
695 	struct btrfs_device *device;
696 	u64 zone_size = 0;
697 	int ret;
698 
699 	/*
700 	 * Host-Managed devices can't be used without the ZONED flag.  With the
701 	 * ZONED all devices can be used, using zone emulation if required.
702 	 */
703 	if (!btrfs_fs_incompat(fs_info, ZONED))
704 		return btrfs_check_for_zoned_device(fs_info);
705 
706 	blk_set_stacking_limits(lim);
707 
708 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
709 		struct btrfs_zoned_device_info *zone_info = device->zone_info;
710 
711 		if (!device->bdev)
712 			continue;
713 
714 		if (!zone_size) {
715 			zone_size = zone_info->zone_size;
716 		} else if (zone_info->zone_size != zone_size) {
717 			btrfs_err(fs_info,
718 		"zoned: unequal block device zone sizes: have %llu found %llu",
719 				  zone_info->zone_size, zone_size);
720 			return -EINVAL;
721 		}
722 
723 		/*
724 		 * With the zoned emulation, we can have non-zoned device on the
725 		 * zoned mode. In this case, we don't have a valid max zone
726 		 * append size.
727 		 */
728 		if (bdev_is_zoned(device->bdev))
729 			blk_stack_limits(lim, bdev_limits(device->bdev), 0);
730 	}
731 
732 	ret = blk_validate_limits(lim);
733 	if (ret) {
734 		btrfs_err(fs_info, "zoned: failed to validate queue limits");
735 		return ret;
736 	}
737 
738 	/*
739 	 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
740 	 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
741 	 * check the alignment here.
742 	 */
743 	if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
744 		btrfs_err(fs_info,
745 			  "zoned: zone size %llu not aligned to stripe %u",
746 			  zone_size, BTRFS_STRIPE_LEN);
747 		return -EINVAL;
748 	}
749 
750 	if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
751 		btrfs_err(fs_info, "zoned: mixed block groups not supported");
752 		return -EINVAL;
753 	}
754 
755 	fs_info->zone_size = zone_size;
756 	/*
757 	 * Also limit max_zone_append_size by max_segments * PAGE_SIZE.
758 	 * Technically, we can have multiple pages per segment. But, since
759 	 * we add the pages one by one to a bio, and cannot increase the
760 	 * metadata reservation even if it increases the number of extents, it
761 	 * is safe to stick with the limit.
762 	 */
763 	fs_info->max_zone_append_size = ALIGN_DOWN(
764 		min3((u64)lim->max_zone_append_sectors << SECTOR_SHIFT,
765 		     (u64)lim->max_sectors << SECTOR_SHIFT,
766 		     (u64)lim->max_segments << PAGE_SHIFT),
767 		fs_info->sectorsize);
768 	fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
769 
770 	fs_info->max_extent_size = min_not_zero(fs_info->max_extent_size,
771 						fs_info->max_zone_append_size);
772 
773 	/*
774 	 * Check mount options here, because we might change fs_info->zoned
775 	 * from fs_info->zone_size.
776 	 */
777 	ret = btrfs_check_mountopts_zoned(fs_info, &fs_info->mount_opt);
778 	if (ret)
779 		return ret;
780 
781 	btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
782 	return 0;
783 }
784 
785 int btrfs_check_mountopts_zoned(const struct btrfs_fs_info *info,
786 				unsigned long long *mount_opt)
787 {
788 	if (!btrfs_is_zoned(info))
789 		return 0;
790 
791 	/*
792 	 * Space cache writing is not COWed. Disable that to avoid write errors
793 	 * in sequential zones.
794 	 */
795 	if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
796 		btrfs_err(info, "zoned: space cache v1 is not supported");
797 		return -EINVAL;
798 	}
799 
800 	if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) {
801 		btrfs_err(info, "zoned: NODATACOW not supported");
802 		return -EINVAL;
803 	}
804 
805 	if (btrfs_raw_test_opt(*mount_opt, DISCARD_ASYNC)) {
806 		btrfs_info(info,
807 			   "zoned: async discard ignored and disabled for zoned mode");
808 		btrfs_clear_opt(*mount_opt, DISCARD_ASYNC);
809 	}
810 
811 	return 0;
812 }
813 
814 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
815 			   int rw, u64 *bytenr_ret)
816 {
817 	u64 wp;
818 	int ret;
819 
820 	if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
821 		*bytenr_ret = zones[0].start << SECTOR_SHIFT;
822 		return 0;
823 	}
824 
825 	ret = sb_write_pointer(bdev, zones, &wp);
826 	if (ret != -ENOENT && ret < 0)
827 		return ret;
828 
829 	if (rw == WRITE) {
830 		struct blk_zone *reset = NULL;
831 
832 		if (wp == zones[0].start << SECTOR_SHIFT)
833 			reset = &zones[0];
834 		else if (wp == zones[1].start << SECTOR_SHIFT)
835 			reset = &zones[1];
836 
837 		if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
838 			unsigned int nofs_flags;
839 
840 			ASSERT(sb_zone_is_full(reset));
841 
842 			nofs_flags = memalloc_nofs_save();
843 			ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
844 					       reset->start, reset->len);
845 			memalloc_nofs_restore(nofs_flags);
846 			if (ret)
847 				return ret;
848 
849 			reset->cond = BLK_ZONE_COND_EMPTY;
850 			reset->wp = reset->start;
851 		}
852 	} else if (ret != -ENOENT) {
853 		/*
854 		 * For READ, we want the previous one. Move write pointer to
855 		 * the end of a zone, if it is at the head of a zone.
856 		 */
857 		u64 zone_end = 0;
858 
859 		if (wp == zones[0].start << SECTOR_SHIFT)
860 			zone_end = zones[1].start + zones[1].capacity;
861 		else if (wp == zones[1].start << SECTOR_SHIFT)
862 			zone_end = zones[0].start + zones[0].capacity;
863 		if (zone_end)
864 			wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
865 					BTRFS_SUPER_INFO_SIZE);
866 
867 		wp -= BTRFS_SUPER_INFO_SIZE;
868 	}
869 
870 	*bytenr_ret = wp;
871 	return 0;
872 
873 }
874 
875 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
876 			       u64 *bytenr_ret)
877 {
878 	struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
879 	sector_t zone_sectors;
880 	u32 sb_zone;
881 	int ret;
882 	u8 zone_sectors_shift;
883 	sector_t nr_sectors;
884 	u32 nr_zones;
885 
886 	if (!bdev_is_zoned(bdev)) {
887 		*bytenr_ret = btrfs_sb_offset(mirror);
888 		return 0;
889 	}
890 
891 	ASSERT(rw == READ || rw == WRITE);
892 
893 	zone_sectors = bdev_zone_sectors(bdev);
894 	if (!is_power_of_2(zone_sectors))
895 		return -EINVAL;
896 	zone_sectors_shift = ilog2(zone_sectors);
897 	nr_sectors = bdev_nr_sectors(bdev);
898 	nr_zones = nr_sectors >> zone_sectors_shift;
899 
900 	sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
901 	if (sb_zone + 1 >= nr_zones)
902 		return -ENOENT;
903 
904 	ret = blkdev_report_zones_cached(bdev, zone_start_sector(sb_zone, bdev),
905 					 BTRFS_NR_SB_LOG_ZONES,
906 					 copy_zone_info_cb, zones);
907 	if (ret < 0)
908 		return ret;
909 	if (unlikely(ret != BTRFS_NR_SB_LOG_ZONES))
910 		return -EIO;
911 
912 	return sb_log_location(bdev, zones, rw, bytenr_ret);
913 }
914 
915 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
916 			  u64 *bytenr_ret)
917 {
918 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
919 	u32 zone_num;
920 
921 	/*
922 	 * For a zoned filesystem on a non-zoned block device, use the same
923 	 * super block locations as regular filesystem. Doing so, the super
924 	 * block can always be retrieved and the zoned flag of the volume
925 	 * detected from the super block information.
926 	 */
927 	if (!bdev_is_zoned(device->bdev)) {
928 		*bytenr_ret = btrfs_sb_offset(mirror);
929 		return 0;
930 	}
931 
932 	zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
933 	if (zone_num + 1 >= zinfo->nr_zones)
934 		return -ENOENT;
935 
936 	return sb_log_location(device->bdev,
937 			       &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
938 			       rw, bytenr_ret);
939 }
940 
941 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
942 				  int mirror)
943 {
944 	u32 zone_num;
945 
946 	if (!zinfo)
947 		return false;
948 
949 	zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
950 	if (zone_num + 1 >= zinfo->nr_zones)
951 		return false;
952 
953 	if (!test_bit(zone_num, zinfo->seq_zones))
954 		return false;
955 
956 	return true;
957 }
958 
959 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
960 {
961 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
962 	struct blk_zone *zone;
963 	int i;
964 
965 	if (!is_sb_log_zone(zinfo, mirror))
966 		return 0;
967 
968 	zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
969 	for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
970 		/* Advance the next zone */
971 		if (zone->cond == BLK_ZONE_COND_FULL) {
972 			zone++;
973 			continue;
974 		}
975 
976 		if (zone->cond == BLK_ZONE_COND_EMPTY)
977 			zone->cond = BLK_ZONE_COND_IMP_OPEN;
978 
979 		zone->wp += SUPER_INFO_SECTORS;
980 
981 		if (sb_zone_is_full(zone)) {
982 			/*
983 			 * No room left to write new superblock. Since
984 			 * superblock is written with REQ_SYNC, it is safe to
985 			 * finish the zone now.
986 			 *
987 			 * If the write pointer is exactly at the capacity,
988 			 * explicit ZONE_FINISH is not necessary.
989 			 */
990 			if (zone->wp != zone->start + zone->capacity) {
991 				unsigned int nofs_flags;
992 				int ret;
993 
994 				nofs_flags = memalloc_nofs_save();
995 				ret = blkdev_zone_mgmt(device->bdev,
996 						REQ_OP_ZONE_FINISH, zone->start,
997 						zone->len);
998 				memalloc_nofs_restore(nofs_flags);
999 				if (ret)
1000 					return ret;
1001 			}
1002 
1003 			zone->wp = zone->start + zone->len;
1004 			zone->cond = BLK_ZONE_COND_FULL;
1005 		}
1006 		return 0;
1007 	}
1008 
1009 	/* All the zones are FULL. Should not reach here. */
1010 	DEBUG_WARN("unexpected state, all zones full");
1011 	return -EIO;
1012 }
1013 
1014 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
1015 {
1016 	unsigned int nofs_flags;
1017 	sector_t zone_sectors;
1018 	sector_t nr_sectors;
1019 	u8 zone_sectors_shift;
1020 	u32 sb_zone;
1021 	u32 nr_zones;
1022 	int ret;
1023 
1024 	zone_sectors = bdev_zone_sectors(bdev);
1025 	zone_sectors_shift = ilog2(zone_sectors);
1026 	nr_sectors = bdev_nr_sectors(bdev);
1027 	nr_zones = nr_sectors >> zone_sectors_shift;
1028 
1029 	sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
1030 	if (sb_zone + 1 >= nr_zones)
1031 		return -ENOENT;
1032 
1033 	nofs_flags = memalloc_nofs_save();
1034 	ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1035 			       zone_start_sector(sb_zone, bdev),
1036 			       zone_sectors * BTRFS_NR_SB_LOG_ZONES);
1037 	memalloc_nofs_restore(nofs_flags);
1038 	return ret;
1039 }
1040 
1041 /*
1042  * Find allocatable zones within a given region.
1043  *
1044  * @device:	the device to allocate a region on
1045  * @hole_start: the position of the hole to allocate the region
1046  * @num_bytes:	size of wanted region
1047  * @hole_end:	the end of the hole
1048  * @return:	position of allocatable zones
1049  *
1050  * Allocatable region should not contain any superblock locations.
1051  */
1052 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
1053 				 u64 hole_end, u64 num_bytes)
1054 {
1055 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
1056 	const u8 shift = zinfo->zone_size_shift;
1057 	u64 nzones = num_bytes >> shift;
1058 	u64 pos = hole_start;
1059 	u64 begin, end;
1060 	bool have_sb;
1061 	int i;
1062 
1063 	ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size),
1064 	       "hole_start=%llu zinfo->zone_size=%llu", hole_start, zinfo->zone_size);
1065 	ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size),
1066 	       "num_bytes=%llu zinfo->zone_size=%llu", num_bytes, zinfo->zone_size);
1067 
1068 	while (pos < hole_end) {
1069 		begin = pos >> shift;
1070 		end = begin + nzones;
1071 
1072 		if (end > zinfo->nr_zones)
1073 			return hole_end;
1074 
1075 		/* Check if zones in the region are all empty */
1076 		if (btrfs_dev_is_sequential(device, pos) &&
1077 		    !bitmap_test_range_all_set(zinfo->empty_zones, begin, nzones)) {
1078 			pos += zinfo->zone_size;
1079 			continue;
1080 		}
1081 
1082 		have_sb = false;
1083 		for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1084 			u32 sb_zone;
1085 			u64 sb_pos;
1086 
1087 			sb_zone = sb_zone_number(shift, i);
1088 			if (!(end <= sb_zone ||
1089 			      sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1090 				have_sb = true;
1091 				pos = zone_start_physical(
1092 					sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1093 				break;
1094 			}
1095 
1096 			/* We also need to exclude regular superblock positions */
1097 			sb_pos = btrfs_sb_offset(i);
1098 			if (!(pos + num_bytes <= sb_pos ||
1099 			      sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1100 				have_sb = true;
1101 				pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1102 					    zinfo->zone_size);
1103 				break;
1104 			}
1105 		}
1106 		if (!have_sb)
1107 			break;
1108 	}
1109 
1110 	return pos;
1111 }
1112 
1113 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1114 {
1115 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
1116 	unsigned int zno = (pos >> zone_info->zone_size_shift);
1117 
1118 	/* We can use any number of zones */
1119 	if (zone_info->max_active_zones == 0)
1120 		return true;
1121 
1122 	if (!test_bit(zno, zone_info->active_zones)) {
1123 		/* Active zone left? */
1124 		if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1125 			return false;
1126 		if (test_and_set_bit(zno, zone_info->active_zones)) {
1127 			/* Someone already set the bit */
1128 			atomic_inc(&zone_info->active_zones_left);
1129 		}
1130 	}
1131 
1132 	return true;
1133 }
1134 
1135 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1136 {
1137 	struct btrfs_zoned_device_info *zone_info = device->zone_info;
1138 	unsigned int zno = (pos >> zone_info->zone_size_shift);
1139 
1140 	/* We can use any number of zones */
1141 	if (zone_info->max_active_zones == 0)
1142 		return;
1143 
1144 	if (test_and_clear_bit(zno, zone_info->active_zones))
1145 		atomic_inc(&zone_info->active_zones_left);
1146 }
1147 
1148 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1149 			    u64 length, u64 *bytes)
1150 {
1151 	unsigned int nofs_flags;
1152 	int ret;
1153 
1154 	*bytes = 0;
1155 	nofs_flags = memalloc_nofs_save();
1156 	ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1157 			       physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT);
1158 	memalloc_nofs_restore(nofs_flags);
1159 	if (ret)
1160 		return ret;
1161 
1162 	*bytes = length;
1163 	while (length) {
1164 		btrfs_dev_set_zone_empty(device, physical);
1165 		btrfs_dev_clear_active_zone(device, physical);
1166 		physical += device->zone_info->zone_size;
1167 		length -= device->zone_info->zone_size;
1168 	}
1169 
1170 	return 0;
1171 }
1172 
1173 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1174 {
1175 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
1176 	const u8 shift = zinfo->zone_size_shift;
1177 	unsigned long begin = start >> shift;
1178 	unsigned long nbits = size >> shift;
1179 	u64 pos;
1180 	int ret;
1181 
1182 	ASSERT(IS_ALIGNED(start, zinfo->zone_size),
1183 	       "start=%llu, zinfo->zone_size=%llu", start, zinfo->zone_size);
1184 	ASSERT(IS_ALIGNED(size, zinfo->zone_size),
1185 	       "size=%llu, zinfo->zone_size=%llu", size, zinfo->zone_size);
1186 
1187 	if (begin + nbits > zinfo->nr_zones)
1188 		return -ERANGE;
1189 
1190 	/* All the zones are conventional */
1191 	if (bitmap_test_range_all_zero(zinfo->seq_zones, begin, nbits))
1192 		return 0;
1193 
1194 	/* All the zones are sequential and empty */
1195 	if (bitmap_test_range_all_set(zinfo->seq_zones, begin, nbits) &&
1196 	    bitmap_test_range_all_set(zinfo->empty_zones, begin, nbits))
1197 		return 0;
1198 
1199 	for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1200 		u64 reset_bytes;
1201 
1202 		if (!btrfs_dev_is_sequential(device, pos) ||
1203 		    btrfs_dev_is_empty_zone(device, pos))
1204 			continue;
1205 
1206 		/* Free regions should be empty */
1207 		btrfs_warn(
1208 			device->fs_info,
1209 		"zoned: resetting device %s (devid %llu) zone %llu for allocation",
1210 			rcu_dereference(device->name), device->devid, pos >> shift);
1211 		WARN_ON_ONCE(1);
1212 
1213 		ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1214 					      &reset_bytes);
1215 		if (ret)
1216 			return ret;
1217 	}
1218 
1219 	return 0;
1220 }
1221 
1222 /*
1223  * Calculate an allocation pointer from the extent allocation information
1224  * for a block group consist of conventional zones. It is pointed to the
1225  * end of the highest addressed extent in the block group as an allocation
1226  * offset.
1227  */
1228 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1229 				   u64 *offset_ret, bool new)
1230 {
1231 	struct btrfs_fs_info *fs_info = cache->fs_info;
1232 	struct btrfs_root *root;
1233 	BTRFS_PATH_AUTO_FREE(path);
1234 	struct btrfs_key key;
1235 	struct btrfs_key found_key;
1236 	const u64 bg_end = btrfs_block_group_end(cache);
1237 	int ret;
1238 	u64 length;
1239 
1240 	/*
1241 	 * Avoid  tree lookups for a new block group, there's no use for it.
1242 	 * It must always be 0.
1243 	 *
1244 	 * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1245 	 * For new a block group, this function is called from
1246 	 * btrfs_make_block_group() which is already taking the chunk mutex.
1247 	 * Thus, we cannot call calculate_alloc_pointer() which takes extent
1248 	 * buffer locks to avoid deadlock.
1249 	 */
1250 	if (new) {
1251 		*offset_ret = 0;
1252 		return 0;
1253 	}
1254 
1255 	path = btrfs_alloc_path();
1256 	if (!path)
1257 		return -ENOMEM;
1258 
1259 	key.objectid = bg_end;
1260 	key.type = 0;
1261 	key.offset = 0;
1262 
1263 	root = btrfs_extent_root(fs_info, key.objectid);
1264 	if (unlikely(!root)) {
1265 		btrfs_err(fs_info,
1266 			  "missing extent root for extent at bytenr %llu",
1267 			  key.objectid);
1268 		return -EUCLEAN;
1269 	}
1270 
1271 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1272 	/* We should not find the exact match */
1273 	if (unlikely(!ret))
1274 		ret = -EUCLEAN;
1275 	if (ret < 0)
1276 		return ret;
1277 
1278 	ret = btrfs_previous_extent_item(root, path, cache->start);
1279 	if (ret) {
1280 		if (ret == 1) {
1281 			ret = 0;
1282 			*offset_ret = 0;
1283 		}
1284 		return ret;
1285 	}
1286 
1287 	btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1288 
1289 	if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1290 		length = found_key.offset;
1291 	else
1292 		length = fs_info->nodesize;
1293 
1294 	if (unlikely(!(found_key.objectid >= cache->start &&
1295 		       found_key.objectid + length <= bg_end))) {
1296 		return -EUCLEAN;
1297 	}
1298 	*offset_ret = found_key.objectid + length - cache->start;
1299 	return 0;
1300 }
1301 
1302 struct zone_info {
1303 	u64 physical;
1304 	u64 capacity;
1305 	u64 alloc_offset;
1306 };
1307 
1308 static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
1309 				struct zone_info *info, unsigned long *active,
1310 				struct btrfs_chunk_map *map, bool new)
1311 {
1312 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1313 	struct btrfs_device *device;
1314 	int dev_replace_is_ongoing = 0;
1315 	unsigned int nofs_flag;
1316 	struct blk_zone zone;
1317 	int ret;
1318 
1319 	info->physical = map->stripes[zone_idx].physical;
1320 
1321 	down_read(&dev_replace->rwsem);
1322 	device = map->stripes[zone_idx].dev;
1323 
1324 	if (!device->bdev) {
1325 		up_read(&dev_replace->rwsem);
1326 		info->alloc_offset = WP_MISSING_DEV;
1327 		return 0;
1328 	}
1329 
1330 	/* Consider a zone as active if we can allow any number of active zones. */
1331 	if (!device->zone_info->max_active_zones)
1332 		__set_bit(zone_idx, active);
1333 
1334 	if (!btrfs_dev_is_sequential(device, info->physical)) {
1335 		up_read(&dev_replace->rwsem);
1336 		info->alloc_offset = WP_CONVENTIONAL;
1337 		info->capacity = device->zone_info->zone_size;
1338 		return 0;
1339 	}
1340 
1341 	ASSERT(!new || btrfs_dev_is_empty_zone(device, info->physical));
1342 
1343 	/* This zone will be used for allocation, so mark this zone non-empty. */
1344 	btrfs_dev_clear_zone_empty(device, info->physical);
1345 
1346 	dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1347 	if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1348 		btrfs_dev_clear_zone_empty(dev_replace->tgtdev, info->physical);
1349 
1350 	/*
1351 	 * The group is mapped to a sequential zone. Get the zone write pointer
1352 	 * to determine the allocation offset within the zone.
1353 	 */
1354 	WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size));
1355 
1356 	if (new) {
1357 		sector_t capacity;
1358 
1359 		capacity = bdev_zone_capacity(device->bdev, info->physical >> SECTOR_SHIFT);
1360 		up_read(&dev_replace->rwsem);
1361 		info->alloc_offset = 0;
1362 		info->capacity = capacity << SECTOR_SHIFT;
1363 
1364 		return 0;
1365 	}
1366 
1367 	nofs_flag = memalloc_nofs_save();
1368 	ret = btrfs_get_dev_zone(device, info->physical, &zone);
1369 	memalloc_nofs_restore(nofs_flag);
1370 	if (ret) {
1371 		up_read(&dev_replace->rwsem);
1372 		if (ret != -EIO && ret != -EOPNOTSUPP)
1373 			return ret;
1374 		info->alloc_offset = WP_MISSING_DEV;
1375 		return 0;
1376 	}
1377 
1378 	if (unlikely(zone.type == BLK_ZONE_TYPE_CONVENTIONAL)) {
1379 		btrfs_err(fs_info,
1380 		"zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1381 			zone.start << SECTOR_SHIFT, rcu_dereference(device->name),
1382 			device->devid);
1383 		up_read(&dev_replace->rwsem);
1384 		return -EIO;
1385 	}
1386 
1387 	info->capacity = (zone.capacity << SECTOR_SHIFT);
1388 
1389 	switch (zone.cond) {
1390 	case BLK_ZONE_COND_OFFLINE:
1391 	case BLK_ZONE_COND_READONLY:
1392 		btrfs_err(fs_info,
1393 		"zoned: offline/readonly zone %llu on device %s (devid %llu)",
1394 			  (info->physical >> device->zone_info->zone_size_shift),
1395 			  rcu_dereference(device->name), device->devid);
1396 		info->alloc_offset = WP_MISSING_DEV;
1397 		break;
1398 	case BLK_ZONE_COND_EMPTY:
1399 		info->alloc_offset = 0;
1400 		break;
1401 	case BLK_ZONE_COND_FULL:
1402 		info->alloc_offset = info->capacity;
1403 		break;
1404 	default:
1405 		/* Partially used zone. */
1406 		info->alloc_offset = ((zone.wp - zone.start) << SECTOR_SHIFT);
1407 		__set_bit(zone_idx, active);
1408 		break;
1409 	}
1410 
1411 	up_read(&dev_replace->rwsem);
1412 
1413 	return 0;
1414 }
1415 
1416 static int btrfs_load_block_group_single(struct btrfs_block_group *bg,
1417 					 struct zone_info *info,
1418 					 unsigned long *active)
1419 {
1420 	if (unlikely(info->alloc_offset == WP_MISSING_DEV)) {
1421 		btrfs_err(bg->fs_info,
1422 			"zoned: cannot recover write pointer for zone %llu",
1423 			info->physical);
1424 		return -EIO;
1425 	}
1426 
1427 	bg->alloc_offset = info->alloc_offset;
1428 	bg->zone_capacity = info->capacity;
1429 	if (test_bit(0, active))
1430 		set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1431 	return 0;
1432 }
1433 
1434 static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
1435 				      struct btrfs_chunk_map *map,
1436 				      struct zone_info *zone_info,
1437 				      unsigned long *active,
1438 				      u64 last_alloc)
1439 {
1440 	struct btrfs_fs_info *fs_info = bg->fs_info;
1441 
1442 	if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1443 		btrfs_err(fs_info, "zoned: data DUP profile needs raid-stripe-tree");
1444 		return -EINVAL;
1445 	}
1446 
1447 	bg->zone_capacity = min_not_zero(zone_info[0].capacity, zone_info[1].capacity);
1448 
1449 	if (unlikely(zone_info[0].alloc_offset == WP_MISSING_DEV)) {
1450 		btrfs_err(fs_info,
1451 			  "zoned: cannot recover write pointer for zone %llu",
1452 			  zone_info[0].physical);
1453 		return -EIO;
1454 	}
1455 	if (unlikely(zone_info[1].alloc_offset == WP_MISSING_DEV)) {
1456 		btrfs_err(fs_info,
1457 			  "zoned: cannot recover write pointer for zone %llu",
1458 			  zone_info[1].physical);
1459 		return -EIO;
1460 	}
1461 
1462 	/*
1463 	 * When the last extent is removed, last_alloc can be smaller than the other write
1464 	 * pointer. In that case, last_alloc should be moved to the corresponding write
1465 	 * pointer position.
1466 	 */
1467 	for (int i = 0; i < map->num_stripes; i++) {
1468 		if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1469 			continue;
1470 		if (last_alloc <= zone_info[i].alloc_offset) {
1471 			last_alloc = zone_info[i].alloc_offset;
1472 			break;
1473 		}
1474 	}
1475 
1476 	if (zone_info[0].alloc_offset == WP_CONVENTIONAL)
1477 		zone_info[0].alloc_offset = last_alloc;
1478 
1479 	if (zone_info[1].alloc_offset == WP_CONVENTIONAL)
1480 		zone_info[1].alloc_offset = last_alloc;
1481 
1482 	if (unlikely(zone_info[0].alloc_offset != zone_info[1].alloc_offset)) {
1483 		btrfs_err(fs_info,
1484 			  "zoned: write pointer offset mismatch of zones in DUP profile");
1485 		return -EIO;
1486 	}
1487 
1488 	if (test_bit(0, active) != test_bit(1, active)) {
1489 		if (unlikely(!btrfs_zone_activate(bg)))
1490 			return -EIO;
1491 	} else if (test_bit(0, active)) {
1492 		set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1493 	}
1494 
1495 	bg->alloc_offset = zone_info[0].alloc_offset;
1496 	return 0;
1497 }
1498 
1499 static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
1500 					struct btrfs_chunk_map *map,
1501 					struct zone_info *zone_info,
1502 					unsigned long *active,
1503 					u64 last_alloc)
1504 {
1505 	struct btrfs_fs_info *fs_info = bg->fs_info;
1506 	int i;
1507 
1508 	if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1509 		btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1510 			  btrfs_bg_type_to_raid_name(map->type));
1511 		return -EINVAL;
1512 	}
1513 
1514 	/* In case a device is missing we have a cap of 0, so don't use it. */
1515 	bg->zone_capacity = min_not_zero(zone_info[0].capacity, zone_info[1].capacity);
1516 
1517 	/*
1518 	 * When the last extent is removed, last_alloc can be smaller than the other write
1519 	 * pointer. In that case, last_alloc should be moved to the corresponding write
1520 	 * pointer position.
1521 	 */
1522 	for (i = 0; i < map->num_stripes; i++) {
1523 		if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1524 		    zone_info[i].alloc_offset == WP_CONVENTIONAL)
1525 			continue;
1526 		if (last_alloc <= zone_info[i].alloc_offset) {
1527 			last_alloc = zone_info[i].alloc_offset;
1528 			break;
1529 		}
1530 	}
1531 
1532 	for (i = 0; i < map->num_stripes; i++) {
1533 		if (zone_info[i].alloc_offset == WP_MISSING_DEV)
1534 			continue;
1535 
1536 		if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1537 			zone_info[i].alloc_offset = last_alloc;
1538 
1539 		if (unlikely((zone_info[0].alloc_offset != zone_info[i].alloc_offset) &&
1540 			     !btrfs_test_opt(fs_info, DEGRADED))) {
1541 			btrfs_err(fs_info,
1542 			"zoned: write pointer offset mismatch of zones in %s profile",
1543 				  btrfs_bg_type_to_raid_name(map->type));
1544 			return -EIO;
1545 		}
1546 		if (test_bit(0, active) != test_bit(i, active)) {
1547 			if (unlikely(!btrfs_test_opt(fs_info, DEGRADED) &&
1548 				     !btrfs_zone_activate(bg))) {
1549 				return -EIO;
1550 			}
1551 		} else {
1552 			if (test_bit(0, active))
1553 				set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1554 		}
1555 	}
1556 
1557 	if (zone_info[0].alloc_offset != WP_MISSING_DEV)
1558 		bg->alloc_offset = zone_info[0].alloc_offset;
1559 	else
1560 		bg->alloc_offset = zone_info[i - 1].alloc_offset;
1561 
1562 	return 0;
1563 }
1564 
1565 static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
1566 					struct btrfs_chunk_map *map,
1567 					struct zone_info *zone_info,
1568 					unsigned long *active,
1569 					u64 last_alloc)
1570 {
1571 	struct btrfs_fs_info *fs_info = bg->fs_info;
1572 	u64 stripe_nr = 0, stripe_offset = 0;
1573 	u64 prev_offset = 0;
1574 	u32 stripe_index = 0;
1575 	bool has_partial = false, has_conventional = false;
1576 
1577 	if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1578 		btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1579 			  btrfs_bg_type_to_raid_name(map->type));
1580 		return -EINVAL;
1581 	}
1582 
1583 	/*
1584 	 * When the last extent is removed, last_alloc can be smaller than the other write
1585 	 * pointer. In that case, last_alloc should be moved to the corresponding write
1586 	 * pointer position.
1587 	 */
1588 	for (int i = 0; i < map->num_stripes; i++) {
1589 		u64 alloc;
1590 
1591 		if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1592 		    zone_info[i].alloc_offset == WP_CONVENTIONAL)
1593 			continue;
1594 
1595 		stripe_nr = zone_info[i].alloc_offset >> BTRFS_STRIPE_LEN_SHIFT;
1596 		stripe_offset = zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK;
1597 		if (stripe_offset == 0 && stripe_nr > 0) {
1598 			stripe_nr--;
1599 			stripe_offset = BTRFS_STRIPE_LEN;
1600 		}
1601 		alloc = ((stripe_nr * map->num_stripes + i) << BTRFS_STRIPE_LEN_SHIFT) +
1602 			stripe_offset;
1603 		last_alloc = max(last_alloc, alloc);
1604 
1605 		/* Partially written stripe found. It should be last. */
1606 		if (zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK)
1607 			break;
1608 	}
1609 	stripe_nr = 0;
1610 	stripe_offset = 0;
1611 
1612 	if (last_alloc) {
1613 		u32 factor = map->num_stripes;
1614 
1615 		stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT;
1616 		stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK;
1617 		stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
1618 	}
1619 
1620 	for (int i = 0; i < map->num_stripes; i++) {
1621 		if (zone_info[i].alloc_offset == WP_MISSING_DEV)
1622 			continue;
1623 
1624 		if (zone_info[i].alloc_offset == WP_CONVENTIONAL) {
1625 			has_conventional = true;
1626 			zone_info[i].alloc_offset = btrfs_stripe_nr_to_offset(stripe_nr);
1627 
1628 			if (stripe_index > i)
1629 				zone_info[i].alloc_offset += BTRFS_STRIPE_LEN;
1630 			else if (stripe_index == i)
1631 				zone_info[i].alloc_offset += stripe_offset;
1632 		}
1633 
1634 		/* Verification */
1635 		if (i != 0) {
1636 			if (unlikely(prev_offset < zone_info[i].alloc_offset)) {
1637 				btrfs_err(fs_info,
1638 				"zoned: stripe position disorder found in block group %llu",
1639 					  bg->start);
1640 				return -EIO;
1641 			}
1642 
1643 			if (unlikely(has_partial &&
1644 				     (zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK))) {
1645 				btrfs_err(fs_info,
1646 				"zoned: multiple partial written stripe found in block group %llu",
1647 					  bg->start);
1648 				return -EIO;
1649 			}
1650 		}
1651 		prev_offset = zone_info[i].alloc_offset;
1652 
1653 		if ((zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK) != 0)
1654 			has_partial = true;
1655 
1656 		if (test_bit(0, active) != test_bit(i, active)) {
1657 			if (unlikely(!btrfs_zone_activate(bg)))
1658 				return -EIO;
1659 		} else {
1660 			if (test_bit(0, active))
1661 				set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1662 		}
1663 		bg->zone_capacity += zone_info[i].capacity;
1664 		bg->alloc_offset += zone_info[i].alloc_offset;
1665 	}
1666 
1667 	/* Check if all devices stay in the same stripe row. */
1668 	if (unlikely(zone_info[0].alloc_offset -
1669 		     zone_info[map->num_stripes - 1].alloc_offset > BTRFS_STRIPE_LEN)) {
1670 		btrfs_err(fs_info, "zoned: stripe gap too large in block group %llu", bg->start);
1671 		return -EIO;
1672 	}
1673 
1674 	if (unlikely(has_conventional && bg->alloc_offset < last_alloc)) {
1675 		btrfs_err(fs_info, "zoned: allocated extent stays beyond write pointers %llu %llu",
1676 			  bg->alloc_offset, last_alloc);
1677 		return -EIO;
1678 	}
1679 
1680 	return 0;
1681 }
1682 
1683 static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
1684 					 struct btrfs_chunk_map *map,
1685 					 struct zone_info *zone_info,
1686 					 unsigned long *active,
1687 					 u64 last_alloc)
1688 {
1689 	struct btrfs_fs_info *fs_info = bg->fs_info;
1690 	u64 AUTO_KFREE(raid0_allocs);
1691 	u64 stripe_nr = 0, stripe_offset = 0;
1692 	u32 stripe_index = 0;
1693 	bool has_partial = false, has_conventional = false;
1694 	u64 prev_offset = 0;
1695 
1696 	if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1697 		btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1698 			  btrfs_bg_type_to_raid_name(map->type));
1699 		return -EINVAL;
1700 	}
1701 
1702 	raid0_allocs = kzalloc_objs(*raid0_allocs, map->num_stripes / map->sub_stripes, GFP_NOFS);
1703 	if (!raid0_allocs)
1704 		return -ENOMEM;
1705 
1706 	/*
1707 	 * When the last extent is removed, last_alloc can be smaller than the other write
1708 	 * pointer. In that case, last_alloc should be moved to the corresponding write
1709 	 * pointer position.
1710 	 */
1711 	for (int i = 0; i < map->num_stripes; i += map->sub_stripes) {
1712 		u64 alloc = zone_info[i].alloc_offset;
1713 
1714 		for (int j = 1; j < map->sub_stripes; j++) {
1715 			int idx = i + j;
1716 
1717 			if (zone_info[idx].alloc_offset == WP_MISSING_DEV ||
1718 			    zone_info[idx].alloc_offset == WP_CONVENTIONAL)
1719 				continue;
1720 			if (alloc == WP_MISSING_DEV || alloc == WP_CONVENTIONAL) {
1721 				alloc = zone_info[idx].alloc_offset;
1722 			} else if (unlikely(zone_info[idx].alloc_offset != alloc)) {
1723 				btrfs_err(fs_info,
1724 				"zoned: write pointer mismatch found in block group %llu",
1725 					  bg->start);
1726 				return -EIO;
1727 			}
1728 		}
1729 
1730 		raid0_allocs[i / map->sub_stripes] = alloc;
1731 		if (alloc == WP_CONVENTIONAL)
1732 			continue;
1733 		if (unlikely(alloc == WP_MISSING_DEV)) {
1734 			btrfs_err(fs_info,
1735 			"zoned: cannot recover write pointer of block group %llu due to missing device",
1736 				  bg->start);
1737 			return -EIO;
1738 		}
1739 
1740 		stripe_nr = alloc >> BTRFS_STRIPE_LEN_SHIFT;
1741 		stripe_offset = alloc & BTRFS_STRIPE_LEN_MASK;
1742 		if (stripe_offset == 0 && stripe_nr > 0) {
1743 			stripe_nr--;
1744 			stripe_offset = BTRFS_STRIPE_LEN;
1745 		}
1746 
1747 		alloc = ((stripe_nr * (map->num_stripes / map->sub_stripes) +
1748 			  (i / map->sub_stripes)) <<
1749 			 BTRFS_STRIPE_LEN_SHIFT) + stripe_offset;
1750 		last_alloc = max(last_alloc, alloc);
1751 	}
1752 	stripe_nr = 0;
1753 	stripe_offset = 0;
1754 
1755 	if (last_alloc) {
1756 		u32 factor = map->num_stripes / map->sub_stripes;
1757 
1758 		stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT;
1759 		stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK;
1760 		stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
1761 	}
1762 
1763 	for (int i = 0; i < map->num_stripes; i++) {
1764 		int idx = i / map->sub_stripes;
1765 
1766 		if (raid0_allocs[idx] == WP_CONVENTIONAL) {
1767 			has_conventional = true;
1768 			raid0_allocs[idx] = btrfs_stripe_nr_to_offset(stripe_nr);
1769 
1770 			if (stripe_index > idx)
1771 				raid0_allocs[idx] += BTRFS_STRIPE_LEN;
1772 			else if (stripe_index == idx)
1773 				raid0_allocs[idx] += stripe_offset;
1774 		}
1775 
1776 		if ((i % map->sub_stripes) == 0) {
1777 			/* Verification */
1778 			if (i != 0) {
1779 				if (unlikely(prev_offset < raid0_allocs[idx])) {
1780 					btrfs_err(fs_info,
1781 					"zoned: stripe position disorder found in block group %llu",
1782 						  bg->start);
1783 					return -EIO;
1784 				}
1785 
1786 				if (unlikely(has_partial &&
1787 					     (raid0_allocs[idx] & BTRFS_STRIPE_LEN_MASK))) {
1788 					btrfs_err(fs_info,
1789 					"zoned: multiple partial written stripe found in block group %llu",
1790 						  bg->start);
1791 					return -EIO;
1792 				}
1793 			}
1794 			prev_offset = raid0_allocs[idx];
1795 
1796 			if ((raid0_allocs[idx] & BTRFS_STRIPE_LEN_MASK) != 0)
1797 				has_partial = true;
1798 		}
1799 
1800 		if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1801 		    zone_info[i].alloc_offset == WP_CONVENTIONAL)
1802 			zone_info[i].alloc_offset = raid0_allocs[idx];
1803 
1804 		if (test_bit(0, active) != test_bit(i, active)) {
1805 			if (unlikely(!btrfs_zone_activate(bg)))
1806 				return -EIO;
1807 		} else if (test_bit(0, active)) {
1808 			set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1809 		}
1810 
1811 		if ((i % map->sub_stripes) == 0) {
1812 			bg->zone_capacity += zone_info[i].capacity;
1813 			bg->alloc_offset += zone_info[i].alloc_offset;
1814 		}
1815 	}
1816 
1817 	/* Check if all devices stay in the same stripe row. */
1818 	if (unlikely(zone_info[0].alloc_offset -
1819 		     zone_info[map->num_stripes - 1].alloc_offset > BTRFS_STRIPE_LEN)) {
1820 		btrfs_err(fs_info, "zoned: stripe gap too large in block group %llu",
1821 			  bg->start);
1822 		return -EIO;
1823 	}
1824 
1825 	if (unlikely(has_conventional && bg->alloc_offset < last_alloc)) {
1826 		btrfs_err(fs_info, "zoned: allocated extent stays beyond write pointers %llu %llu",
1827 			  bg->alloc_offset, last_alloc);
1828 		return -EIO;
1829 	}
1830 
1831 	return 0;
1832 }
1833 
1834 EXPORT_FOR_TESTS
1835 int btrfs_load_block_group_by_raid_type(struct btrfs_block_group *bg,
1836 					struct btrfs_chunk_map *map,
1837 					struct zone_info *zone_info,
1838 					unsigned long *active, u64 last_alloc)
1839 {
1840 	struct btrfs_fs_info *fs_info = bg->fs_info;
1841 	u64 profile;
1842 	int ret;
1843 
1844 	profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1845 	switch (profile) {
1846 	case 0: /* single */
1847 		ret = btrfs_load_block_group_single(bg, &zone_info[0], active);
1848 		break;
1849 	case BTRFS_BLOCK_GROUP_DUP:
1850 		ret = btrfs_load_block_group_dup(bg, map, zone_info, active, last_alloc);
1851 		break;
1852 	case BTRFS_BLOCK_GROUP_RAID1:
1853 	case BTRFS_BLOCK_GROUP_RAID1C3:
1854 	case BTRFS_BLOCK_GROUP_RAID1C4:
1855 		ret = btrfs_load_block_group_raid1(bg, map, zone_info, active, last_alloc);
1856 		break;
1857 	case BTRFS_BLOCK_GROUP_RAID0:
1858 		ret = btrfs_load_block_group_raid0(bg, map, zone_info, active, last_alloc);
1859 		break;
1860 	case BTRFS_BLOCK_GROUP_RAID10:
1861 		ret = btrfs_load_block_group_raid10(bg, map, zone_info, active, last_alloc);
1862 		break;
1863 	case BTRFS_BLOCK_GROUP_RAID5:
1864 	case BTRFS_BLOCK_GROUP_RAID6:
1865 	default:
1866 		btrfs_err(fs_info, "zoned: profile %s not yet supported",
1867 			  btrfs_bg_type_to_raid_name(map->type));
1868 		return -EINVAL;
1869 	}
1870 
1871 	if (ret == -EIO && profile != 0 && profile != BTRFS_BLOCK_GROUP_RAID0 &&
1872 	    profile != BTRFS_BLOCK_GROUP_RAID10) {
1873 		/*
1874 		 * Detected broken write pointer.  Make this block group
1875 		 * unallocatable by setting the allocation pointer at the end of
1876 		 * allocatable region. Relocating this block group will fix the
1877 		 * mismatch.
1878 		 *
1879 		 * Currently, we cannot handle RAID0 or RAID10 case like this
1880 		 * because we don't have a proper zone_capacity value. But,
1881 		 * reading from this block group won't work anyway by a missing
1882 		 * stripe.
1883 		 */
1884 		bg->alloc_offset = bg->zone_capacity;
1885 	}
1886 
1887 	return ret;
1888 }
1889 
1890 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1891 {
1892 	struct btrfs_fs_info *fs_info = cache->fs_info;
1893 	struct btrfs_chunk_map *map;
1894 	u64 logical = cache->start;
1895 	u64 length = cache->length;
1896 	struct zone_info AUTO_KFREE(zone_info);
1897 	int ret;
1898 	int i;
1899 	unsigned long *active = NULL;
1900 	u64 last_alloc = 0;
1901 	u32 num_sequential = 0, num_conventional = 0;
1902 
1903 	if (!btrfs_is_zoned(fs_info))
1904 		return 0;
1905 
1906 	/* Sanity check */
1907 	if (unlikely(!IS_ALIGNED(length, fs_info->zone_size))) {
1908 		btrfs_err(fs_info,
1909 		"zoned: block group %llu len %llu unaligned to zone size %llu",
1910 			  logical, length, fs_info->zone_size);
1911 		return -EIO;
1912 	}
1913 
1914 	map = btrfs_find_chunk_map(fs_info, logical, length);
1915 	if (!map)
1916 		return -EINVAL;
1917 
1918 	cache->physical_map = map;
1919 
1920 	zone_info = kzalloc_objs(*zone_info, map->num_stripes, GFP_NOFS);
1921 	if (!zone_info) {
1922 		ret = -ENOMEM;
1923 		goto out;
1924 	}
1925 
1926 	active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1927 	if (!active) {
1928 		ret = -ENOMEM;
1929 		goto out;
1930 	}
1931 
1932 	for (i = 0; i < map->num_stripes; i++) {
1933 		ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map, new);
1934 		if (ret)
1935 			goto out;
1936 
1937 		if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1938 			num_conventional++;
1939 		else
1940 			num_sequential++;
1941 	}
1942 
1943 	if (num_sequential > 0)
1944 		set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
1945 
1946 	if (num_conventional > 0) {
1947 		ret = calculate_alloc_pointer(cache, &last_alloc, new);
1948 		if (ret) {
1949 			btrfs_err(fs_info,
1950 			"zoned: failed to determine allocation offset of bg %llu",
1951 				  cache->start);
1952 			goto out;
1953 		} else if (map->num_stripes == num_conventional) {
1954 			cache->alloc_offset = last_alloc;
1955 			cache->zone_capacity = cache->length;
1956 			set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1957 			goto out;
1958 		}
1959 	}
1960 
1961 	ret = btrfs_load_block_group_by_raid_type(cache, map, zone_info, active, last_alloc);
1962 
1963 out:
1964 	/* Reject non SINGLE data profiles without RST */
1965 	if ((map->type & BTRFS_BLOCK_GROUP_DATA) &&
1966 	    (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
1967 	    !fs_info->stripe_root) {
1968 		btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1969 			  btrfs_bg_type_to_raid_name(map->type));
1970 		ret = -EINVAL;
1971 	}
1972 
1973 	if (unlikely(cache->alloc_offset > cache->zone_capacity)) {
1974 		btrfs_err(fs_info,
1975 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1976 			  cache->alloc_offset, cache->zone_capacity,
1977 			  cache->start);
1978 		ret = -EIO;
1979 	}
1980 
1981 	/* An extent is allocated after the write pointer */
1982 	if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1983 		btrfs_err(fs_info,
1984 			  "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1985 			  logical, last_alloc, cache->alloc_offset);
1986 		ret = -EIO;
1987 	}
1988 
1989 	if (!ret) {
1990 		cache->meta_write_pointer = cache->alloc_offset + cache->start;
1991 		if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
1992 			btrfs_get_block_group(cache);
1993 			spin_lock(&fs_info->zone_active_bgs_lock);
1994 			list_add_tail(&cache->active_bg_list,
1995 				      &fs_info->zone_active_bgs);
1996 			spin_unlock(&fs_info->zone_active_bgs_lock);
1997 		}
1998 	} else {
1999 		btrfs_free_chunk_map(cache->physical_map);
2000 		cache->physical_map = NULL;
2001 	}
2002 	bitmap_free(active);
2003 
2004 	return ret;
2005 }
2006 
2007 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
2008 {
2009 	u64 unusable, free;
2010 
2011 	if (!btrfs_is_zoned(cache->fs_info))
2012 		return;
2013 
2014 	WARN_ON(cache->bytes_super != 0);
2015 	unusable = (cache->alloc_offset - cache->used) +
2016 		   (cache->length - cache->zone_capacity);
2017 	free = cache->zone_capacity - cache->alloc_offset;
2018 
2019 	/* We only need ->free_space in ALLOC_SEQ block groups */
2020 	cache->cached = BTRFS_CACHE_FINISHED;
2021 	cache->free_space_ctl->free_space = free;
2022 	cache->zone_unusable = unusable;
2023 }
2024 
2025 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
2026 {
2027 	u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
2028 	struct btrfs_inode *inode = bbio->inode;
2029 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
2030 	struct btrfs_block_group *cache;
2031 	bool ret = false;
2032 
2033 	if (!btrfs_is_zoned(fs_info))
2034 		return false;
2035 
2036 	if (!is_data_inode(inode))
2037 		return false;
2038 
2039 	if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE)
2040 		return false;
2041 
2042 	/*
2043 	 * Using REQ_OP_ZONE_APPEND for relocation can break assumptions on the
2044 	 * extent layout the relocation code has.
2045 	 * Furthermore we have set aside own block-group from which only the
2046 	 * relocation "process" can allocate and make sure only one process at a
2047 	 * time can add pages to an extent that gets relocated, so it's safe to
2048 	 * use regular REQ_OP_WRITE for this special case.
2049 	 */
2050 	if (btrfs_is_data_reloc_root(inode->root))
2051 		return false;
2052 
2053 	cache = btrfs_lookup_block_group(fs_info, start);
2054 	ASSERT(cache);
2055 	if (!cache)
2056 		return false;
2057 
2058 	ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
2059 	btrfs_put_block_group(cache);
2060 
2061 	return ret;
2062 }
2063 
2064 void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
2065 {
2066 	const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
2067 	struct btrfs_ordered_sum *sum = bbio->sums;
2068 
2069 	if (physical < bbio->orig_physical)
2070 		sum->logical -= bbio->orig_physical - physical;
2071 	else
2072 		sum->logical += physical - bbio->orig_physical;
2073 }
2074 
2075 static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered,
2076 					u64 logical)
2077 {
2078 	struct extent_map_tree *em_tree = &ordered->inode->extent_tree;
2079 	struct extent_map *em;
2080 
2081 	ordered->disk_bytenr = logical;
2082 
2083 	write_lock(&em_tree->lock);
2084 	em = btrfs_search_extent_mapping(em_tree, ordered->file_offset,
2085 					 ordered->num_bytes);
2086 	/* The em should be a new COW extent, thus it should not have an offset. */
2087 	ASSERT(em->offset == 0, "em->offset=%llu", em->offset);
2088 	em->disk_bytenr = logical;
2089 	btrfs_free_extent_map(em);
2090 	write_unlock(&em_tree->lock);
2091 }
2092 
2093 static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
2094 				      u64 logical, u64 len)
2095 {
2096 	struct btrfs_ordered_extent *new;
2097 
2098 	if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
2099 	    btrfs_split_extent_map(ordered->inode, ordered->file_offset,
2100 				   ordered->num_bytes, len, logical))
2101 		return false;
2102 
2103 	new = btrfs_split_ordered_extent(ordered, len);
2104 	if (IS_ERR(new))
2105 		return false;
2106 	new->disk_bytenr = logical;
2107 	btrfs_finish_one_ordered(new);
2108 	return true;
2109 }
2110 
2111 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
2112 {
2113 	struct btrfs_inode *inode = ordered->inode;
2114 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
2115 	struct btrfs_ordered_sum *sum;
2116 	u64 logical, len;
2117 
2118 	/*
2119 	 * Write to pre-allocated region is for the data relocation, and so
2120 	 * it should use WRITE operation. No split/rewrite are necessary.
2121 	 */
2122 	if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
2123 		return;
2124 
2125 	ASSERT(!list_empty(&ordered->csum_list));
2126 	sum = list_first_entry(&ordered->csum_list, struct btrfs_ordered_sum, list);
2127 	logical = sum->logical;
2128 	len = sum->len;
2129 
2130 	while (len < ordered->disk_num_bytes) {
2131 		sum = list_next_entry(sum, list);
2132 		if (sum->logical == logical + len) {
2133 			len += sum->len;
2134 			continue;
2135 		}
2136 		if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
2137 			btrfs_mark_ordered_extent_error(ordered);
2138 			btrfs_err(fs_info, "failed to split ordered extent");
2139 			goto out;
2140 		}
2141 		logical = sum->logical;
2142 		len = sum->len;
2143 	}
2144 
2145 	if (ordered->disk_bytenr != logical)
2146 		btrfs_rewrite_logical_zoned(ordered, logical);
2147 
2148 out:
2149 	/*
2150 	 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
2151 	 * were allocated by btrfs_alloc_dummy_sum only to record the logical
2152 	 * addresses and don't contain actual checksums.  We thus must free them
2153 	 * here so that we don't attempt to log the csums later.
2154 	 */
2155 	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
2156 	    test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state)) {
2157 		while ((sum = list_first_entry_or_null(&ordered->csum_list,
2158 						       typeof(*sum), list))) {
2159 			list_del(&sum->list);
2160 			kfree(sum);
2161 		}
2162 	}
2163 }
2164 
2165 static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
2166 			       struct btrfs_block_group **active_bg)
2167 {
2168 	const struct writeback_control *wbc = ctx->wbc;
2169 	struct btrfs_block_group *block_group = ctx->zoned_bg;
2170 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2171 
2172 	if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
2173 		return true;
2174 
2175 	if (fs_info->treelog_bg == block_group->start) {
2176 		if (!btrfs_zone_activate(block_group)) {
2177 			int ret_fin = btrfs_zone_finish_one_bg(fs_info);
2178 
2179 			if (ret_fin != 1 || !btrfs_zone_activate(block_group))
2180 				return false;
2181 		}
2182 	} else if (*active_bg != block_group) {
2183 		struct btrfs_block_group *tgt = *active_bg;
2184 
2185 		/* zoned_meta_io_lock protects fs_info->active_{meta,system}_bg. */
2186 		lockdep_assert_held(&fs_info->zoned_meta_io_lock);
2187 
2188 		if (tgt) {
2189 			/*
2190 			 * If there is an unsent IO left in the allocated area,
2191 			 * we cannot wait for them as it may cause a deadlock.
2192 			 */
2193 			if (tgt->meta_write_pointer < tgt->start + tgt->alloc_offset) {
2194 				if (wbc->sync_mode == WB_SYNC_NONE ||
2195 				    (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync))
2196 					return false;
2197 			}
2198 
2199 			/* Pivot active metadata/system block group. */
2200 			btrfs_zoned_meta_io_unlock(fs_info);
2201 			wait_eb_writebacks(tgt);
2202 			do_zone_finish(tgt, true);
2203 			btrfs_zoned_meta_io_lock(fs_info);
2204 			if (*active_bg == tgt) {
2205 				btrfs_put_block_group(tgt);
2206 				*active_bg = NULL;
2207 			}
2208 		}
2209 		if (!btrfs_zone_activate(block_group))
2210 			return false;
2211 		if (*active_bg != block_group) {
2212 			ASSERT(*active_bg == NULL);
2213 			*active_bg = block_group;
2214 			btrfs_get_block_group(block_group);
2215 		}
2216 	}
2217 
2218 	return true;
2219 }
2220 
2221 /*
2222  * Check if @ctx->eb is aligned to the write pointer.
2223  *
2224  * Return:
2225  *   0:        @ctx->eb is at the write pointer. You can write it.
2226  *   -EAGAIN:  There is a hole. The caller should handle the case.
2227  *   -EBUSY:   There is a hole, but the caller can just bail out.
2228  */
2229 int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
2230 				   struct btrfs_eb_write_context *ctx)
2231 {
2232 	const struct writeback_control *wbc = ctx->wbc;
2233 	const struct extent_buffer *eb = ctx->eb;
2234 	struct btrfs_block_group *block_group = ctx->zoned_bg;
2235 
2236 	if (!btrfs_is_zoned(fs_info))
2237 		return 0;
2238 
2239 	if (block_group) {
2240 		if (block_group->start > eb->start ||
2241 		    btrfs_block_group_end(block_group) <= eb->start) {
2242 			btrfs_put_block_group(block_group);
2243 			block_group = NULL;
2244 			ctx->zoned_bg = NULL;
2245 		}
2246 	}
2247 
2248 	if (!block_group) {
2249 		block_group = btrfs_lookup_block_group(fs_info, eb->start);
2250 		if (!block_group)
2251 			return 0;
2252 		ctx->zoned_bg = block_group;
2253 	}
2254 
2255 	if (block_group->meta_write_pointer == eb->start) {
2256 		struct btrfs_block_group **tgt;
2257 
2258 		if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
2259 			return 0;
2260 
2261 		if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2262 			tgt = &fs_info->active_system_bg;
2263 		else
2264 			tgt = &fs_info->active_meta_bg;
2265 		if (check_bg_is_active(ctx, tgt))
2266 			return 0;
2267 	}
2268 
2269 	/*
2270 	 * Since we may release fs_info->zoned_meta_io_lock, someone can already
2271 	 * start writing this eb. In that case, we can just bail out.
2272 	 */
2273 	if (block_group->meta_write_pointer > eb->start)
2274 		return -EBUSY;
2275 
2276 	/* If for_sync, this hole will be filled with transaction commit. */
2277 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
2278 		return -EAGAIN;
2279 	return -EBUSY;
2280 }
2281 
2282 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
2283 {
2284 	if (!btrfs_dev_is_sequential(device, physical))
2285 		return -EOPNOTSUPP;
2286 
2287 	return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
2288 				    length >> SECTOR_SHIFT, GFP_NOFS, 0);
2289 }
2290 
2291 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
2292 			  struct blk_zone *zone)
2293 {
2294 	struct btrfs_io_context *bioc = NULL;
2295 	u64 mapped_length = PAGE_SIZE;
2296 	unsigned int nofs_flag;
2297 	int nmirrors;
2298 	int i, ret;
2299 
2300 	ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
2301 			      &mapped_length, &bioc, NULL, NULL);
2302 	if (unlikely(ret || !bioc || mapped_length < PAGE_SIZE)) {
2303 		ret = -EIO;
2304 		goto out_put_bioc;
2305 	}
2306 
2307 	if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
2308 		ret = -EINVAL;
2309 		goto out_put_bioc;
2310 	}
2311 
2312 	nofs_flag = memalloc_nofs_save();
2313 	nmirrors = (int)bioc->num_stripes;
2314 	for (i = 0; i < nmirrors; i++) {
2315 		u64 physical = bioc->stripes[i].physical;
2316 		struct btrfs_device *dev = bioc->stripes[i].dev;
2317 
2318 		/* Missing device */
2319 		if (!dev->bdev)
2320 			continue;
2321 
2322 		ret = btrfs_get_dev_zone(dev, physical, zone);
2323 		/* Failing device */
2324 		if (ret == -EIO || ret == -EOPNOTSUPP)
2325 			continue;
2326 		break;
2327 	}
2328 	memalloc_nofs_restore(nofs_flag);
2329 out_put_bioc:
2330 	btrfs_put_bioc(bioc);
2331 	return ret;
2332 }
2333 
2334 /*
2335  * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
2336  * filling zeros between @physical_pos to a write pointer of dev-replace
2337  * source device.
2338  */
2339 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
2340 				    u64 physical_start, u64 physical_pos)
2341 {
2342 	struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
2343 	struct blk_zone zone;
2344 	u64 length;
2345 	u64 wp;
2346 	int ret;
2347 
2348 	if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
2349 		return 0;
2350 
2351 	ret = read_zone_info(fs_info, logical, &zone);
2352 	if (ret)
2353 		return ret;
2354 
2355 	wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
2356 
2357 	if (physical_pos == wp)
2358 		return 0;
2359 
2360 	if (unlikely(physical_pos > wp))
2361 		return -EUCLEAN;
2362 
2363 	length = wp - physical_pos;
2364 	return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
2365 }
2366 
2367 /*
2368  * Activate block group and underlying device zones
2369  *
2370  * @block_group: the block group to activate
2371  *
2372  * Return: true on success, false otherwise
2373  */
2374 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
2375 {
2376 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2377 	struct btrfs_chunk_map *map;
2378 	struct btrfs_device *device;
2379 	u64 physical;
2380 	const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
2381 	bool ret;
2382 	int i;
2383 
2384 	if (!btrfs_is_zoned(block_group->fs_info))
2385 		return true;
2386 
2387 	if (unlikely(btrfs_is_testing(fs_info)))
2388 		return true;
2389 
2390 	map = block_group->physical_map;
2391 
2392 	spin_lock(&fs_info->zone_active_bgs_lock);
2393 	spin_lock(&block_group->lock);
2394 	if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2395 		ret = true;
2396 		goto out_unlock;
2397 	}
2398 
2399 	if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) {
2400 		/* The caller should check if the block group is full. */
2401 		if (WARN_ON_ONCE(btrfs_zoned_bg_is_full(block_group))) {
2402 			ret = false;
2403 			goto out_unlock;
2404 		}
2405 	} else {
2406 		/* Since it is already written, it should have been active. */
2407 		WARN_ON_ONCE(block_group->meta_write_pointer != block_group->start);
2408 	}
2409 
2410 	for (i = 0; i < map->num_stripes; i++) {
2411 		struct btrfs_zoned_device_info *zinfo;
2412 		int reserved = 0;
2413 
2414 		device = map->stripes[i].dev;
2415 		physical = map->stripes[i].physical;
2416 		zinfo = device->zone_info;
2417 
2418 		if (!device->bdev)
2419 			continue;
2420 
2421 		if (zinfo->max_active_zones == 0)
2422 			continue;
2423 
2424 		if (is_data)
2425 			reserved = zinfo->reserved_active_zones;
2426 		/*
2427 		 * For the data block group, leave active zones for one
2428 		 * metadata block group and one system block group.
2429 		 */
2430 		if (atomic_read(&zinfo->active_zones_left) <= reserved) {
2431 			ret = false;
2432 			goto out_unlock;
2433 		}
2434 
2435 		if (!btrfs_dev_set_active_zone(device, physical)) {
2436 			/* Cannot activate the zone */
2437 			ret = false;
2438 			goto out_unlock;
2439 		}
2440 		if (!is_data)
2441 			zinfo->reserved_active_zones--;
2442 	}
2443 
2444 	/* Successfully activated all the zones */
2445 	set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2446 	spin_unlock(&block_group->lock);
2447 
2448 	/* For the active block group list */
2449 	btrfs_get_block_group(block_group);
2450 	list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
2451 	spin_unlock(&fs_info->zone_active_bgs_lock);
2452 
2453 	return true;
2454 
2455 out_unlock:
2456 	spin_unlock(&block_group->lock);
2457 	spin_unlock(&fs_info->zone_active_bgs_lock);
2458 	return ret;
2459 }
2460 
2461 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
2462 {
2463 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2464 	const u64 end = btrfs_block_group_end(block_group);
2465 	struct extent_buffer *eb;
2466 	unsigned long index, start = (block_group->start >> fs_info->nodesize_bits);
2467 
2468 	rcu_read_lock();
2469 	xa_for_each_start(&fs_info->buffer_tree, index, eb, start) {
2470 		if (eb->start < block_group->start)
2471 			continue;
2472 		if (eb->start >= end)
2473 			break;
2474 		rcu_read_unlock();
2475 		wait_on_extent_buffer_writeback(eb);
2476 		rcu_read_lock();
2477 	}
2478 	rcu_read_unlock();
2479 }
2480 
2481 static int call_zone_finish(struct btrfs_block_group *block_group,
2482 			    struct btrfs_io_stripe *stripe)
2483 {
2484 	struct btrfs_device *device = stripe->dev;
2485 	const u64 physical = stripe->physical;
2486 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
2487 	int ret;
2488 
2489 	if (!device->bdev)
2490 		return 0;
2491 
2492 	if (zinfo->max_active_zones == 0)
2493 		return 0;
2494 
2495 	if (btrfs_dev_is_sequential(device, physical)) {
2496 		unsigned int nofs_flags;
2497 
2498 		nofs_flags = memalloc_nofs_save();
2499 		ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2500 				       physical >> SECTOR_SHIFT,
2501 				       zinfo->zone_size >> SECTOR_SHIFT);
2502 		memalloc_nofs_restore(nofs_flags);
2503 
2504 		if (ret)
2505 			return ret;
2506 	}
2507 
2508 	if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA))
2509 		zinfo->reserved_active_zones++;
2510 	btrfs_dev_clear_active_zone(device, physical);
2511 
2512 	return 0;
2513 }
2514 
2515 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
2516 {
2517 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2518 	struct btrfs_chunk_map *map;
2519 	const bool is_metadata = (block_group->flags &
2520 			(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
2521 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
2522 	int ret = 0;
2523 	int i;
2524 
2525 	spin_lock(&block_group->lock);
2526 	if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2527 		spin_unlock(&block_group->lock);
2528 		return 0;
2529 	}
2530 
2531 	/* Check if we have unwritten allocated space */
2532 	if (is_metadata &&
2533 	    block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
2534 		spin_unlock(&block_group->lock);
2535 		return -EAGAIN;
2536 	}
2537 
2538 	/*
2539 	 * If we are sure that the block group is full (= no more room left for
2540 	 * new allocation) and the IO for the last usable block is completed, we
2541 	 * don't need to wait for the other IOs. This holds because we ensure
2542 	 * the sequential IO submissions using the ZONE_APPEND command for data
2543 	 * and block_group->meta_write_pointer for metadata.
2544 	 */
2545 	if (!fully_written) {
2546 		if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2547 			spin_unlock(&block_group->lock);
2548 			return -EAGAIN;
2549 		}
2550 		spin_unlock(&block_group->lock);
2551 
2552 		ret = btrfs_inc_block_group_ro(block_group, false);
2553 		if (ret)
2554 			return ret;
2555 
2556 		/* Ensure all writes in this block group finish */
2557 		btrfs_wait_block_group_reservations(block_group);
2558 		/* No need to wait for NOCOW writers. Zoned mode does not allow that */
2559 		btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group);
2560 		/* Wait for extent buffers to be written. */
2561 		if (is_metadata)
2562 			wait_eb_writebacks(block_group);
2563 
2564 		spin_lock(&block_group->lock);
2565 
2566 		/*
2567 		 * Bail out if someone already deactivated the block group, or
2568 		 * allocated space is left in the block group.
2569 		 */
2570 		if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2571 			      &block_group->runtime_flags)) {
2572 			spin_unlock(&block_group->lock);
2573 			btrfs_dec_block_group_ro(block_group);
2574 			return 0;
2575 		}
2576 
2577 		if (block_group->reserved ||
2578 		    test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2579 			     &block_group->runtime_flags)) {
2580 			spin_unlock(&block_group->lock);
2581 			btrfs_dec_block_group_ro(block_group);
2582 			return -EAGAIN;
2583 		}
2584 	}
2585 
2586 	clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2587 	block_group->alloc_offset = block_group->zone_capacity;
2588 	if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
2589 		block_group->meta_write_pointer = block_group->start +
2590 						  block_group->zone_capacity;
2591 	block_group->free_space_ctl->free_space = 0;
2592 	btrfs_clear_treelog_bg(block_group);
2593 	btrfs_clear_data_reloc_bg(block_group);
2594 	spin_unlock(&block_group->lock);
2595 
2596 	down_read(&dev_replace->rwsem);
2597 	map = block_group->physical_map;
2598 	for (i = 0; i < map->num_stripes; i++) {
2599 
2600 		ret = call_zone_finish(block_group, &map->stripes[i]);
2601 		if (ret) {
2602 			up_read(&dev_replace->rwsem);
2603 			return ret;
2604 		}
2605 	}
2606 	up_read(&dev_replace->rwsem);
2607 
2608 	if (!fully_written)
2609 		btrfs_dec_block_group_ro(block_group);
2610 
2611 	spin_lock(&fs_info->zone_active_bgs_lock);
2612 	ASSERT(!list_empty(&block_group->active_bg_list));
2613 	list_del_init(&block_group->active_bg_list);
2614 	spin_unlock(&fs_info->zone_active_bgs_lock);
2615 
2616 	/* For active_bg_list */
2617 	btrfs_put_block_group(block_group);
2618 
2619 	clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2620 
2621 	return 0;
2622 }
2623 
2624 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2625 {
2626 	if (!btrfs_is_zoned(block_group->fs_info))
2627 		return 0;
2628 
2629 	return do_zone_finish(block_group, false);
2630 }
2631 
2632 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2633 {
2634 	struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2635 	struct btrfs_device *device;
2636 	bool ret = false;
2637 
2638 	if (!btrfs_is_zoned(fs_info))
2639 		return true;
2640 
2641 	if (test_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags))
2642 		return false;
2643 
2644 	/* Check if there is a device with active zones left */
2645 	mutex_lock(&fs_info->chunk_mutex);
2646 	spin_lock(&fs_info->zone_active_bgs_lock);
2647 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2648 		struct btrfs_zoned_device_info *zinfo = device->zone_info;
2649 		int reserved = 0;
2650 
2651 		if (!device->bdev)
2652 			continue;
2653 
2654 		if (!zinfo->max_active_zones) {
2655 			ret = true;
2656 			break;
2657 		}
2658 
2659 		if (flags & BTRFS_BLOCK_GROUP_DATA)
2660 			reserved = zinfo->reserved_active_zones;
2661 
2662 		switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
2663 		case 0: /* single */
2664 			ret = (atomic_read(&zinfo->active_zones_left) >= (1 + reserved));
2665 			break;
2666 		case BTRFS_BLOCK_GROUP_DUP:
2667 			ret = (atomic_read(&zinfo->active_zones_left) >= (2 + reserved));
2668 			break;
2669 		}
2670 		if (ret)
2671 			break;
2672 	}
2673 	spin_unlock(&fs_info->zone_active_bgs_lock);
2674 	mutex_unlock(&fs_info->chunk_mutex);
2675 
2676 	if (!ret)
2677 		set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2678 
2679 	return ret;
2680 }
2681 
2682 int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2683 {
2684 	struct btrfs_block_group *block_group;
2685 	u64 min_alloc_bytes;
2686 
2687 	if (!btrfs_is_zoned(fs_info))
2688 		return 0;
2689 
2690 	block_group = btrfs_lookup_block_group(fs_info, logical);
2691 	if (WARN_ON_ONCE(!block_group))
2692 		return -ENOENT;
2693 
2694 	/* No MIXED_BG on zoned btrfs. */
2695 	if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2696 		min_alloc_bytes = fs_info->sectorsize;
2697 	else
2698 		min_alloc_bytes = fs_info->nodesize;
2699 
2700 	/* Bail out if we can allocate more data from this block group. */
2701 	if (logical + length + min_alloc_bytes <=
2702 	    block_group->start + block_group->zone_capacity)
2703 		goto out;
2704 
2705 	do_zone_finish(block_group, true);
2706 
2707 out:
2708 	btrfs_put_block_group(block_group);
2709 	return 0;
2710 }
2711 
2712 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2713 {
2714 	int ret;
2715 	struct btrfs_block_group *bg =
2716 		container_of(work, struct btrfs_block_group, zone_finish_work);
2717 
2718 	wait_on_extent_buffer_writeback(bg->last_eb);
2719 	free_extent_buffer(bg->last_eb);
2720 	ret = do_zone_finish(bg, true);
2721 	if (ret)
2722 		btrfs_handle_fs_error(bg->fs_info, ret,
2723 				      "Failed to finish block-group's zone");
2724 	btrfs_put_block_group(bg);
2725 }
2726 
2727 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2728 				   struct extent_buffer *eb)
2729 {
2730 	if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
2731 	    eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2732 		return;
2733 
2734 	if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2735 		btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2736 			  bg->start);
2737 		return;
2738 	}
2739 
2740 	/* For the work */
2741 	btrfs_get_block_group(bg);
2742 	refcount_inc(&eb->refs);
2743 	bg->last_eb = eb;
2744 	INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2745 	queue_work(system_dfl_wq, &bg->zone_finish_work);
2746 }
2747 
2748 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2749 {
2750 	struct btrfs_fs_info *fs_info = bg->fs_info;
2751 
2752 	spin_lock(&fs_info->relocation_bg_lock);
2753 	if (fs_info->data_reloc_bg == bg->start)
2754 		fs_info->data_reloc_bg = 0;
2755 	spin_unlock(&fs_info->relocation_bg_lock);
2756 }
2757 
2758 void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
2759 {
2760 	struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
2761 	struct btrfs_space_info *space_info = data_sinfo;
2762 	struct btrfs_trans_handle *trans;
2763 	struct btrfs_block_group *bg;
2764 	struct list_head *bg_list;
2765 	u64 alloc_flags;
2766 	bool first = true;
2767 	bool did_chunk_alloc = false;
2768 	int index;
2769 	int ret;
2770 
2771 	if (!btrfs_is_zoned(fs_info))
2772 		return;
2773 
2774 	if (fs_info->data_reloc_bg)
2775 		return;
2776 
2777 	if (sb_rdonly(fs_info->sb))
2778 		return;
2779 
2780 	alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
2781 	index = btrfs_bg_flags_to_raid_index(alloc_flags);
2782 
2783 	/* Scan the data space_info to find empty block groups. Take the second one. */
2784 again:
2785 	bg_list = &space_info->block_groups[index];
2786 	list_for_each_entry(bg, bg_list, list) {
2787 		if (bg->alloc_offset != 0)
2788 			continue;
2789 
2790 		if (first) {
2791 			first = false;
2792 			continue;
2793 		}
2794 
2795 		if (space_info == data_sinfo) {
2796 			/* Migrate the block group to the data relocation space_info. */
2797 			struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
2798 			int factor;
2799 
2800 			ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
2801 			       "reloc_sinfo->subgroup_id=%d", reloc_sinfo->subgroup_id);
2802 			factor = btrfs_bg_type_to_factor(bg->flags);
2803 
2804 			down_write(&space_info->groups_sem);
2805 			list_del_init(&bg->list);
2806 			/* We can assume this as we choose the second empty one. */
2807 			ASSERT(!list_empty(&space_info->block_groups[index]));
2808 			up_write(&space_info->groups_sem);
2809 
2810 			spin_lock(&space_info->lock);
2811 			space_info->total_bytes -= bg->length;
2812 			space_info->disk_total -= bg->length * factor;
2813 			space_info->disk_total -= bg->zone_unusable;
2814 			/* There is no allocation ever happened. */
2815 			ASSERT(bg->used == 0, "bg->used=%llu", bg->used);
2816 			/* No super block in a block group on the zoned setup. */
2817 			ASSERT(bg->bytes_super == 0, "bg->bytes_super=%llu", bg->bytes_super);
2818 			spin_unlock(&space_info->lock);
2819 
2820 			bg->space_info = reloc_sinfo;
2821 			if (reloc_sinfo->block_group_kobjs[index] == NULL)
2822 				btrfs_sysfs_add_block_group_type(bg);
2823 
2824 			btrfs_add_bg_to_space_info(fs_info, bg);
2825 		}
2826 
2827 		fs_info->data_reloc_bg = bg->start;
2828 		set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
2829 		btrfs_zone_activate(bg);
2830 
2831 		return;
2832 	}
2833 
2834 	if (did_chunk_alloc)
2835 		return;
2836 
2837 	trans = btrfs_join_transaction(fs_info->tree_root);
2838 	if (IS_ERR(trans))
2839 		return;
2840 
2841 	/* Allocate new BG in the data relocation space_info. */
2842 	space_info = data_sinfo->sub_group[0];
2843 	ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
2844 	       "space_info->subgroup_id=%d", space_info->subgroup_id);
2845 	ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
2846 	btrfs_end_transaction(trans);
2847 	if (ret == 1) {
2848 		/*
2849 		 * We allocated a new block group in the data relocation space_info. We
2850 		 * can take that one.
2851 		 */
2852 		first = false;
2853 		did_chunk_alloc = true;
2854 		goto again;
2855 	}
2856 }
2857 
2858 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2859 {
2860 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2861 	struct btrfs_device *device;
2862 
2863 	if (!btrfs_is_zoned(fs_info))
2864 		return;
2865 
2866 	mutex_lock(&fs_devices->device_list_mutex);
2867 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
2868 		if (device->zone_info) {
2869 			vfree(device->zone_info->zone_cache);
2870 			device->zone_info->zone_cache = NULL;
2871 		}
2872 	}
2873 	mutex_unlock(&fs_devices->device_list_mutex);
2874 }
2875 
2876 bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
2877 {
2878 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2879 	struct btrfs_device *device;
2880 	u64 total = btrfs_super_total_bytes(fs_info->super_copy);
2881 	u64 used = 0;
2882 	u64 factor;
2883 
2884 	ASSERT(btrfs_is_zoned(fs_info));
2885 
2886 	if (fs_info->bg_reclaim_threshold == 0)
2887 		return false;
2888 
2889 	mutex_lock(&fs_devices->device_list_mutex);
2890 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
2891 		if (!device->bdev)
2892 			continue;
2893 
2894 		used += device->bytes_used;
2895 	}
2896 	mutex_unlock(&fs_devices->device_list_mutex);
2897 
2898 	factor = div64_u64(used * 100, total);
2899 	return factor >= fs_info->bg_reclaim_threshold;
2900 }
2901 
2902 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2903 				       u64 length)
2904 {
2905 	struct btrfs_block_group *block_group;
2906 
2907 	if (!btrfs_is_zoned(fs_info))
2908 		return;
2909 
2910 	block_group = btrfs_lookup_block_group(fs_info, logical);
2911 	/* It should be called on a previous data relocation block group. */
2912 	ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2913 
2914 	spin_lock(&block_group->lock);
2915 	if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2916 		goto out;
2917 
2918 	/* All relocation extents are written. */
2919 	if (block_group->start + block_group->alloc_offset == logical + length) {
2920 		/*
2921 		 * Now, release this block group for further allocations and
2922 		 * zone finish.
2923 		 */
2924 		clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2925 			  &block_group->runtime_flags);
2926 	}
2927 
2928 out:
2929 	spin_unlock(&block_group->lock);
2930 	btrfs_put_block_group(block_group);
2931 }
2932 
2933 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2934 {
2935 	struct btrfs_block_group *block_group;
2936 	struct btrfs_block_group *min_bg = NULL;
2937 	u64 min_avail = U64_MAX;
2938 	int ret;
2939 
2940 	spin_lock(&fs_info->zone_active_bgs_lock);
2941 	list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2942 			    active_bg_list) {
2943 		u64 avail;
2944 
2945 		spin_lock(&block_group->lock);
2946 		if (block_group->reserved || block_group->alloc_offset == 0 ||
2947 		    !(block_group->flags & BTRFS_BLOCK_GROUP_DATA) ||
2948 		    test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2949 			spin_unlock(&block_group->lock);
2950 			continue;
2951 		}
2952 
2953 		avail = block_group->zone_capacity - block_group->alloc_offset;
2954 		if (min_avail > avail) {
2955 			if (min_bg)
2956 				btrfs_put_block_group(min_bg);
2957 			min_bg = block_group;
2958 			min_avail = avail;
2959 			btrfs_get_block_group(min_bg);
2960 		}
2961 		spin_unlock(&block_group->lock);
2962 	}
2963 	spin_unlock(&fs_info->zone_active_bgs_lock);
2964 
2965 	if (!min_bg)
2966 		return 0;
2967 
2968 	ret = btrfs_zone_finish(min_bg);
2969 	btrfs_put_block_group(min_bg);
2970 
2971 	return ret < 0 ? ret : 1;
2972 }
2973 
2974 int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish)
2975 {
2976 	struct btrfs_fs_info *fs_info = space_info->fs_info;
2977 	struct btrfs_block_group *bg;
2978 	int index;
2979 
2980 	if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
2981 		return 0;
2982 
2983 	for (;;) {
2984 		int ret;
2985 		bool need_finish = false;
2986 
2987 		down_read(&space_info->groups_sem);
2988 		for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
2989 			list_for_each_entry(bg, &space_info->block_groups[index],
2990 					    list) {
2991 				if (!spin_trylock(&bg->lock))
2992 					continue;
2993 				if (btrfs_zoned_bg_is_full(bg) ||
2994 				    test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2995 					     &bg->runtime_flags)) {
2996 					spin_unlock(&bg->lock);
2997 					continue;
2998 				}
2999 				spin_unlock(&bg->lock);
3000 
3001 				if (btrfs_zone_activate(bg)) {
3002 					up_read(&space_info->groups_sem);
3003 					return 1;
3004 				}
3005 
3006 				need_finish = true;
3007 			}
3008 		}
3009 		up_read(&space_info->groups_sem);
3010 
3011 		if (!do_finish || !need_finish)
3012 			break;
3013 
3014 		ret = btrfs_zone_finish_one_bg(fs_info);
3015 		if (ret == 0)
3016 			break;
3017 		if (ret < 0)
3018 			return ret;
3019 	}
3020 
3021 	return 0;
3022 }
3023 
3024 /*
3025  * Reserve zones for one metadata block group, one tree-log block group, and one
3026  * system block group.
3027  */
3028 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
3029 {
3030 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3031 	struct btrfs_block_group *block_group;
3032 	struct btrfs_device *device;
3033 	/* Reserve zones for normal SINGLE metadata and tree-log block group. */
3034 	unsigned int metadata_reserve = 2;
3035 	/* Reserve a zone for SINGLE system block group. */
3036 	unsigned int system_reserve = 1;
3037 
3038 	if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
3039 		return;
3040 
3041 	/*
3042 	 * This function is called from the mount context. So, there is no
3043 	 * parallel process touching the bits. No need for read_seqretry().
3044 	 */
3045 	if (fs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
3046 		metadata_reserve = 4;
3047 	if (fs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
3048 		system_reserve = 2;
3049 
3050 	/* Apply the reservation on all the devices. */
3051 	mutex_lock(&fs_devices->device_list_mutex);
3052 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
3053 		if (!device->bdev)
3054 			continue;
3055 
3056 		device->zone_info->reserved_active_zones =
3057 			metadata_reserve + system_reserve;
3058 	}
3059 	mutex_unlock(&fs_devices->device_list_mutex);
3060 
3061 	/* Release reservation for currently active block groups. */
3062 	spin_lock(&fs_info->zone_active_bgs_lock);
3063 	list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
3064 		struct btrfs_chunk_map *map = block_group->physical_map;
3065 
3066 		if (!(block_group->flags &
3067 		      (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
3068 			continue;
3069 
3070 		for (int i = 0; i < map->num_stripes; i++)
3071 			map->stripes[i].dev->zone_info->reserved_active_zones--;
3072 	}
3073 	spin_unlock(&fs_info->zone_active_bgs_lock);
3074 }
3075 
3076 /*
3077  * Reset the zones of unused block groups from @space_info->bytes_zone_unusable.
3078  *
3079  * @space_info:	the space to work on
3080  * @num_bytes:	targeting reclaim bytes
3081  *
3082  * This one resets the zones of a block group, so we can reuse the region
3083  * without removing the block group. On the other hand, btrfs_delete_unused_bgs()
3084  * just removes a block group and frees up the underlying zones. So, we still
3085  * need to allocate a new block group to reuse the zones.
3086  *
3087  * Resetting is faster than deleting/recreating a block group. It is similar
3088  * to freeing the logical space on the regular mode. However, we cannot change
3089  * the block group's profile with this operation.
3090  */
3091 int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes)
3092 {
3093 	struct btrfs_fs_info *fs_info = space_info->fs_info;
3094 	const sector_t zone_size_sectors = fs_info->zone_size >> SECTOR_SHIFT;
3095 
3096 	if (!btrfs_is_zoned(fs_info))
3097 		return 0;
3098 
3099 	while (num_bytes > 0) {
3100 		struct btrfs_chunk_map *map;
3101 		struct btrfs_block_group *bg = NULL;
3102 		bool found = false;
3103 		u64 reclaimed = 0;
3104 
3105 		/*
3106 		 * Here, we choose a fully zone_unusable block group. It's
3107 		 * technically possible to reset a partly zone_unusable block
3108 		 * group, which still has some free space left. However,
3109 		 * handling that needs to cope with the allocation side, which
3110 		 * makes the logic more complex. So, let's handle the easy case
3111 		 * for now.
3112 		 */
3113 		spin_lock(&fs_info->unused_bgs_lock);
3114 		list_for_each_entry(bg, &fs_info->unused_bgs, bg_list) {
3115 			if ((bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != space_info->flags)
3116 				continue;
3117 
3118 			/*
3119 			 * Use trylock to avoid locking order violation. In
3120 			 * btrfs_reclaim_bgs_work(), the lock order is
3121 			 * &bg->lock -> &fs_info->unused_bgs_lock. We skip a
3122 			 * block group if we cannot take its lock.
3123 			 */
3124 			if (!spin_trylock(&bg->lock))
3125 				continue;
3126 			if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length) {
3127 				spin_unlock(&bg->lock);
3128 				continue;
3129 			}
3130 			spin_unlock(&bg->lock);
3131 			found = true;
3132 			break;
3133 		}
3134 		if (!found) {
3135 			spin_unlock(&fs_info->unused_bgs_lock);
3136 			return 0;
3137 		}
3138 
3139 		list_del_init(&bg->bg_list);
3140 		btrfs_put_block_group(bg);
3141 		spin_unlock(&fs_info->unused_bgs_lock);
3142 
3143 		/*
3144 		 * Since the block group is fully zone_unusable and we cannot
3145 		 * allocate from this block group anymore, we don't need to set
3146 		 * this block group read-only.
3147 		 */
3148 
3149 		down_read(&fs_info->dev_replace.rwsem);
3150 		map = bg->physical_map;
3151 		for (int i = 0; i < map->num_stripes; i++) {
3152 			struct btrfs_io_stripe *stripe = &map->stripes[i];
3153 			unsigned int nofs_flags;
3154 			int ret;
3155 
3156 			nofs_flags = memalloc_nofs_save();
3157 			ret = blkdev_zone_mgmt(stripe->dev->bdev, REQ_OP_ZONE_RESET,
3158 					       stripe->physical >> SECTOR_SHIFT,
3159 					       zone_size_sectors);
3160 			memalloc_nofs_restore(nofs_flags);
3161 
3162 			if (ret) {
3163 				up_read(&fs_info->dev_replace.rwsem);
3164 				return ret;
3165 			}
3166 		}
3167 		up_read(&fs_info->dev_replace.rwsem);
3168 
3169 		spin_lock(&space_info->lock);
3170 		spin_lock(&bg->lock);
3171 		ASSERT(!btrfs_is_block_group_used(bg));
3172 		if (bg->ro) {
3173 			spin_unlock(&bg->lock);
3174 			spin_unlock(&space_info->lock);
3175 			continue;
3176 		}
3177 
3178 		reclaimed = bg->alloc_offset;
3179 		bg->zone_unusable = bg->length - bg->zone_capacity;
3180 		bg->alloc_offset = 0;
3181 		/*
3182 		 * This holds because we currently reset fully used then freed
3183 		 * block group.
3184 		 */
3185 		ASSERT(reclaimed == bg->zone_capacity,
3186 		       "reclaimed=%llu bg->zone_capacity=%llu", reclaimed, bg->zone_capacity);
3187 		bg->free_space_ctl->free_space += reclaimed;
3188 		space_info->bytes_zone_unusable -= reclaimed;
3189 		spin_unlock(&bg->lock);
3190 		btrfs_return_free_space(space_info, reclaimed);
3191 		spin_unlock(&space_info->lock);
3192 
3193 		if (num_bytes <= reclaimed)
3194 			break;
3195 		num_bytes -= reclaimed;
3196 	}
3197 
3198 	return 0;
3199 }
3200 
3201 void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq)
3202 {
3203 	struct btrfs_block_group *bg;
3204 	u64 data_reloc_bg;
3205 	u64 treelog_bg;
3206 
3207 	seq_puts(seq, "\n  zoned statistics:\n");
3208 
3209 	spin_lock(&fs_info->zone_active_bgs_lock);
3210 	seq_printf(seq, "\tactive block-groups: %zu\n",
3211 			     list_count_nodes(&fs_info->zone_active_bgs));
3212 	spin_unlock(&fs_info->zone_active_bgs_lock);
3213 
3214 	spin_lock(&fs_info->unused_bgs_lock);
3215 	seq_printf(seq, "\t  reclaimable: %zu\n",
3216 			     list_count_nodes(&fs_info->reclaim_bgs));
3217 	seq_printf(seq, "\t  unused: %zu\n", list_count_nodes(&fs_info->unused_bgs));
3218 	spin_unlock(&fs_info->unused_bgs_lock);
3219 
3220 	seq_printf(seq,"\t  need reclaim: %s\n",
3221 		   str_true_false(btrfs_zoned_should_reclaim(fs_info)));
3222 
3223 	data_reloc_bg = data_race(fs_info->data_reloc_bg);
3224 	if (data_reloc_bg)
3225 		seq_printf(seq, "\tdata relocation block-group: %llu\n",
3226 			   data_reloc_bg);
3227 	treelog_bg = data_race(fs_info->treelog_bg);
3228 	if (treelog_bg)
3229 		seq_printf(seq, "\ttree-log block-group: %llu\n", treelog_bg);
3230 
3231 	spin_lock(&fs_info->zone_active_bgs_lock);
3232 	seq_puts(seq, "\tactive zones:\n");
3233 	list_for_each_entry(bg, &fs_info->zone_active_bgs, active_bg_list) {
3234 		u64 start;
3235 		u64 alloc_offset;
3236 		u64 used;
3237 		u64 reserved;
3238 		u64 zone_unusable;
3239 		const char *typestr = btrfs_space_info_type_str(bg->space_info);
3240 
3241 		spin_lock(&bg->lock);
3242 		start = bg->start;
3243 		alloc_offset = bg->alloc_offset;
3244 		used = bg->used;
3245 		reserved = bg->reserved;
3246 		zone_unusable = bg->zone_unusable;
3247 		spin_unlock(&bg->lock);
3248 
3249 		seq_printf(seq,
3250 			   "\t  start: %llu, wp: %llu used: %llu, reserved: %llu, unusable: %llu (%s)\n",
3251 			   start, alloc_offset, used, reserved, zone_unusable, typestr);
3252 	}
3253 	spin_unlock(&fs_info->zone_active_bgs_lock);
3254 }
3255