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