xref: /linux/fs/btrfs/zoned.c (revision 9bdef63e2b5d4a88810329a1cab3642d6378a895)
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 btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
127 
128 		for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
129 			u64 zone_end = (zones[i].start + zones[i].capacity) << SECTOR_SHIFT;
130 			u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) -
131 						BTRFS_SUPER_INFO_SIZE;
132 			struct folio *folio;
133 
134 			filemap_invalidate_lock_shared(mapping);
135 			folio = mapping_read_folio_gfp(mapping, bytenr >> PAGE_SHIFT,
136 						       GFP_NOFS);
137 			filemap_invalidate_unlock_shared(mapping);
138 			if (IS_ERR(folio)) {
139 				if (i == 1)
140 					btrfs_release_disk_super(super[0]);
141 				return PTR_ERR(folio);
142 			}
143 			super[i] = folio_address(folio) + offset_in_folio(folio, bytenr);
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 			spin_lock(&fs_info->zone_active_bgs_lock);
2009 			if (list_empty(&cache->active_bg_list)) {
2010 				btrfs_get_block_group(cache);
2011 				list_add_tail(&cache->active_bg_list,
2012 					      &fs_info->zone_active_bgs);
2013 			}
2014 			spin_unlock(&fs_info->zone_active_bgs_lock);
2015 		}
2016 	} else {
2017 		btrfs_free_chunk_map(cache->physical_map);
2018 		cache->physical_map = NULL;
2019 	}
2020 	bitmap_free(active);
2021 
2022 	return ret;
2023 }
2024 
2025 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
2026 {
2027 	u64 unusable, free;
2028 
2029 	if (!btrfs_is_zoned(cache->fs_info))
2030 		return;
2031 
2032 	WARN_ON(cache->bytes_super != 0);
2033 	unusable = (cache->alloc_offset - cache->used) +
2034 		   (cache->length - cache->zone_capacity);
2035 	free = cache->zone_capacity - cache->alloc_offset;
2036 
2037 	/* We only need ->free_space in ALLOC_SEQ block groups */
2038 	cache->cached = BTRFS_CACHE_FINISHED;
2039 	cache->free_space_ctl->free_space = free;
2040 	cache->zone_unusable = unusable;
2041 }
2042 
2043 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
2044 {
2045 	u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
2046 	struct btrfs_inode *inode = bbio->inode;
2047 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
2048 	struct btrfs_block_group *cache;
2049 	bool ret = false;
2050 
2051 	if (!btrfs_is_zoned(fs_info))
2052 		return false;
2053 
2054 	if (!is_data_inode(inode))
2055 		return false;
2056 
2057 	if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE)
2058 		return false;
2059 
2060 	/*
2061 	 * Using REQ_OP_ZONE_APPEND for relocation can break assumptions on the
2062 	 * extent layout the relocation code has.
2063 	 * Furthermore we have set aside own block-group from which only the
2064 	 * relocation "process" can allocate and make sure only one process at a
2065 	 * time can add pages to an extent that gets relocated, so it's safe to
2066 	 * use regular REQ_OP_WRITE for this special case.
2067 	 */
2068 	if (btrfs_is_data_reloc_root(inode->root))
2069 		return false;
2070 
2071 	cache = btrfs_lookup_block_group(fs_info, start);
2072 	ASSERT(cache);
2073 	if (!cache)
2074 		return false;
2075 
2076 	ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
2077 	btrfs_put_block_group(cache);
2078 
2079 	return ret;
2080 }
2081 
2082 void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
2083 {
2084 	const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
2085 	struct btrfs_ordered_sum *sum = bbio->sums;
2086 
2087 	if (physical < bbio->orig_physical)
2088 		sum->logical -= bbio->orig_physical - physical;
2089 	else
2090 		sum->logical += physical - bbio->orig_physical;
2091 }
2092 
2093 static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered,
2094 					u64 logical)
2095 {
2096 	struct extent_map_tree *em_tree = &ordered->inode->extent_tree;
2097 	struct extent_map *em;
2098 
2099 	ordered->disk_bytenr = logical;
2100 
2101 	write_lock(&em_tree->lock);
2102 	em = btrfs_search_extent_mapping(em_tree, ordered->file_offset,
2103 					 ordered->num_bytes);
2104 	/* The em should be a new COW extent, thus it should not have an offset. */
2105 	ASSERT(em->offset == 0, "em->offset=%llu", em->offset);
2106 	em->disk_bytenr = logical;
2107 	btrfs_free_extent_map(em);
2108 	write_unlock(&em_tree->lock);
2109 }
2110 
2111 static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
2112 				      u64 logical, u64 len)
2113 {
2114 	struct btrfs_ordered_extent *new;
2115 
2116 	if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
2117 	    btrfs_split_extent_map(ordered->inode, ordered->file_offset,
2118 				   ordered->num_bytes, len, logical))
2119 		return false;
2120 
2121 	new = btrfs_split_ordered_extent(ordered, len);
2122 	if (IS_ERR(new))
2123 		return false;
2124 	new->disk_bytenr = logical;
2125 	btrfs_finish_one_ordered(new);
2126 	return true;
2127 }
2128 
2129 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
2130 {
2131 	struct btrfs_inode *inode = ordered->inode;
2132 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
2133 	struct btrfs_ordered_sum *sum;
2134 	u64 logical, len;
2135 
2136 	/*
2137 	 * Write to pre-allocated region is for the data relocation, and so
2138 	 * it should use WRITE operation. No split/rewrite are necessary.
2139 	 */
2140 	if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
2141 		return;
2142 
2143 	/*
2144 	 * A fully truncated ordered extent wrote no data and so has
2145 	 * no zone append result to record.
2146 	 */
2147 	if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags) &&
2148 	    ordered->truncated_len == 0) {
2149 		ASSERT(list_empty(&ordered->csum_list));
2150 		return;
2151 	}
2152 
2153 	ASSERT(!list_empty(&ordered->csum_list));
2154 	sum = list_first_entry(&ordered->csum_list, struct btrfs_ordered_sum, list);
2155 	logical = sum->logical;
2156 	len = sum->len;
2157 
2158 	while (len < ordered->disk_num_bytes) {
2159 		sum = list_next_entry(sum, list);
2160 		if (sum->logical == logical + len) {
2161 			len += sum->len;
2162 			continue;
2163 		}
2164 		if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
2165 			btrfs_mark_ordered_extent_error(ordered);
2166 			btrfs_err(fs_info, "failed to split ordered extent");
2167 			goto out;
2168 		}
2169 		logical = sum->logical;
2170 		len = sum->len;
2171 	}
2172 
2173 	if (ordered->disk_bytenr != logical)
2174 		btrfs_rewrite_logical_zoned(ordered, logical);
2175 
2176 out:
2177 	/*
2178 	 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
2179 	 * were allocated by btrfs_alloc_dummy_sum only to record the logical
2180 	 * addresses and don't contain actual checksums.  We thus must free them
2181 	 * here so that we don't attempt to log the csums later.
2182 	 */
2183 	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
2184 	    test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state)) {
2185 		while ((sum = list_first_entry_or_null(&ordered->csum_list,
2186 						       typeof(*sum), list))) {
2187 			list_del(&sum->list);
2188 			kfree(sum);
2189 		}
2190 	}
2191 }
2192 
2193 static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
2194 			       struct btrfs_block_group **active_bg)
2195 {
2196 	const struct writeback_control *wbc = ctx->wbc;
2197 	struct btrfs_block_group *block_group = ctx->zoned_bg;
2198 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2199 
2200 	if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
2201 		return true;
2202 
2203 	if (fs_info->treelog_bg == block_group->start) {
2204 		if (!btrfs_zone_activate(block_group)) {
2205 			int ret_fin;
2206 
2207 			btrfs_zoned_meta_io_unlock(fs_info);
2208 			ret_fin = btrfs_zone_finish_one_bg(fs_info);
2209 			btrfs_zoned_meta_io_lock(fs_info);
2210 
2211 			if (ret_fin != 1 || !btrfs_zone_activate(block_group))
2212 				return false;
2213 		}
2214 	} else if (*active_bg != block_group) {
2215 		struct btrfs_block_group *tgt = *active_bg;
2216 
2217 		/* zoned_meta_io_lock protects fs_info->active_{meta,system}_bg. */
2218 		lockdep_assert_held(&fs_info->zoned_meta_io_lock);
2219 
2220 		if (tgt) {
2221 			/*
2222 			 * If there is an unsent IO left in the allocated area,
2223 			 * we cannot wait for them as it may cause a deadlock.
2224 			 */
2225 			if (tgt->meta_write_pointer < tgt->start + tgt->alloc_offset) {
2226 				if (wbc->sync_mode == WB_SYNC_NONE ||
2227 				    (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync))
2228 					return false;
2229 			}
2230 
2231 			/* Pivot active metadata/system block group. */
2232 			btrfs_zoned_meta_io_unlock(fs_info);
2233 			wait_eb_writebacks(tgt);
2234 			do_zone_finish(tgt, true);
2235 			btrfs_zoned_meta_io_lock(fs_info);
2236 			if (*active_bg == tgt) {
2237 				btrfs_put_block_group(tgt);
2238 				*active_bg = NULL;
2239 			}
2240 		}
2241 		if (!btrfs_zone_activate(block_group))
2242 			return false;
2243 		if (*active_bg != block_group) {
2244 			ASSERT(*active_bg == NULL);
2245 			*active_bg = block_group;
2246 			btrfs_get_block_group(block_group);
2247 		}
2248 	}
2249 
2250 	return true;
2251 }
2252 
2253 /*
2254  * Check if @ctx->eb is aligned to the write pointer.
2255  *
2256  * Return:
2257  *   0:        @ctx->eb is at the write pointer. You can write it.
2258  *   -EAGAIN:  There is a hole. The caller should handle the case.
2259  *   -EBUSY:   There is a hole, but the caller can just bail out.
2260  */
2261 int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
2262 				   struct btrfs_eb_write_context *ctx)
2263 {
2264 	const struct writeback_control *wbc = ctx->wbc;
2265 	const struct extent_buffer *eb = ctx->eb;
2266 	struct btrfs_block_group *block_group = ctx->zoned_bg;
2267 
2268 	if (!btrfs_is_zoned(fs_info))
2269 		return 0;
2270 
2271 	if (block_group) {
2272 		if (block_group->start > eb->start ||
2273 		    btrfs_block_group_end(block_group) <= eb->start) {
2274 			btrfs_put_block_group(block_group);
2275 			block_group = NULL;
2276 			ctx->zoned_bg = NULL;
2277 		}
2278 	}
2279 
2280 	if (!block_group) {
2281 		block_group = btrfs_lookup_block_group(fs_info, eb->start);
2282 		if (!block_group)
2283 			return 0;
2284 		ctx->zoned_bg = block_group;
2285 	}
2286 
2287 	if (block_group->meta_write_pointer == eb->start) {
2288 		struct btrfs_block_group **tgt;
2289 
2290 		if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
2291 			return 0;
2292 
2293 		if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2294 			tgt = &fs_info->active_system_bg;
2295 		else
2296 			tgt = &fs_info->active_meta_bg;
2297 		if (check_bg_is_active(ctx, tgt))
2298 			return 0;
2299 	}
2300 
2301 	/*
2302 	 * Since we may release fs_info->zoned_meta_io_lock, someone can already
2303 	 * start writing this eb. In that case, we can just bail out.
2304 	 */
2305 	if (block_group->meta_write_pointer > eb->start)
2306 		return -EBUSY;
2307 
2308 	/* If for_sync, this hole will be filled with transaction commit. */
2309 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
2310 		return -EAGAIN;
2311 	return -EBUSY;
2312 }
2313 
2314 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
2315 {
2316 	if (!btrfs_dev_is_sequential(device, physical))
2317 		return -EOPNOTSUPP;
2318 
2319 	return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
2320 				    length >> SECTOR_SHIFT, GFP_NOFS, 0);
2321 }
2322 
2323 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
2324 			  struct blk_zone *zone)
2325 {
2326 	struct btrfs_io_context *bioc = NULL;
2327 	u64 mapped_length = PAGE_SIZE;
2328 	unsigned int nofs_flag;
2329 	int nmirrors;
2330 	int i, ret;
2331 
2332 	ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
2333 			      &mapped_length, &bioc, NULL, NULL);
2334 	if (unlikely(ret || !bioc || mapped_length < PAGE_SIZE)) {
2335 		ret = -EIO;
2336 		goto out_put_bioc;
2337 	}
2338 
2339 	if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
2340 		ret = -EINVAL;
2341 		goto out_put_bioc;
2342 	}
2343 
2344 	nofs_flag = memalloc_nofs_save();
2345 	nmirrors = (int)bioc->num_stripes;
2346 	for (i = 0; i < nmirrors; i++) {
2347 		u64 physical = bioc->stripes[i].physical;
2348 		struct btrfs_device *dev = bioc->stripes[i].dev;
2349 
2350 		/* Missing device */
2351 		if (!dev->bdev)
2352 			continue;
2353 
2354 		ret = btrfs_get_dev_zone(dev, physical, zone);
2355 		/* Failing device */
2356 		if (ret == -EIO || ret == -EOPNOTSUPP)
2357 			continue;
2358 		break;
2359 	}
2360 	memalloc_nofs_restore(nofs_flag);
2361 out_put_bioc:
2362 	btrfs_put_bioc(bioc);
2363 	return ret;
2364 }
2365 
2366 /*
2367  * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
2368  * filling zeros between @physical_pos to a write pointer of dev-replace
2369  * source device.
2370  */
2371 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
2372 				    u64 physical_start, u64 physical_pos)
2373 {
2374 	struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
2375 	struct blk_zone zone;
2376 	u64 length;
2377 	u64 wp;
2378 	int ret;
2379 
2380 	if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
2381 		return 0;
2382 
2383 	ret = read_zone_info(fs_info, logical, &zone);
2384 	if (ret)
2385 		return ret;
2386 
2387 	wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
2388 
2389 	if (physical_pos == wp)
2390 		return 0;
2391 
2392 	if (unlikely(physical_pos > wp))
2393 		return -EUCLEAN;
2394 
2395 	length = wp - physical_pos;
2396 	return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
2397 }
2398 
2399 /*
2400  * Activate block group and underlying device zones
2401  *
2402  * @block_group: the block group to activate
2403  *
2404  * Return: true on success, false otherwise
2405  */
2406 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
2407 {
2408 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2409 	struct btrfs_chunk_map *map;
2410 	struct btrfs_device *device;
2411 	u64 physical;
2412 	const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
2413 	bool ret;
2414 	int i;
2415 
2416 	if (!btrfs_is_zoned(block_group->fs_info))
2417 		return true;
2418 
2419 	if (unlikely(btrfs_is_testing(fs_info)))
2420 		return true;
2421 
2422 	map = block_group->physical_map;
2423 
2424 	spin_lock(&fs_info->zone_active_bgs_lock);
2425 	spin_lock(&block_group->lock);
2426 	if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2427 		ret = true;
2428 		goto out_unlock;
2429 	}
2430 
2431 	if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) {
2432 		/* The caller should check if the block group is full. */
2433 		if (WARN_ON_ONCE(btrfs_zoned_bg_is_full(block_group))) {
2434 			ret = false;
2435 			goto out_unlock;
2436 		}
2437 	} else {
2438 		/* Since it is already written, it should have been active. */
2439 		WARN_ON_ONCE(block_group->meta_write_pointer != block_group->start);
2440 	}
2441 
2442 	for (i = 0; i < map->num_stripes; i++) {
2443 		struct btrfs_zoned_device_info *zinfo;
2444 		int reserved = 0;
2445 
2446 		device = map->stripes[i].dev;
2447 		physical = map->stripes[i].physical;
2448 		zinfo = device->zone_info;
2449 
2450 		if (!device->bdev)
2451 			continue;
2452 
2453 		if (zinfo->max_active_zones == 0)
2454 			continue;
2455 
2456 		if (is_data)
2457 			reserved = zinfo->reserved_active_zones;
2458 		/*
2459 		 * For the data block group, leave active zones for one
2460 		 * metadata block group and one system block group.
2461 		 */
2462 		if (atomic_read(&zinfo->active_zones_left) <= reserved) {
2463 			ret = false;
2464 			goto out_unlock;
2465 		}
2466 
2467 		if (!btrfs_dev_set_active_zone(device, physical)) {
2468 			/* Cannot activate the zone */
2469 			ret = false;
2470 			goto out_unlock;
2471 		}
2472 		if (!is_data)
2473 			zinfo->reserved_active_zones--;
2474 	}
2475 
2476 	/* Successfully activated all the zones */
2477 	set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2478 	spin_unlock(&block_group->lock);
2479 
2480 	/* For the active block group list */
2481 	btrfs_get_block_group(block_group);
2482 	list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
2483 	spin_unlock(&fs_info->zone_active_bgs_lock);
2484 
2485 	return true;
2486 
2487 out_unlock:
2488 	spin_unlock(&block_group->lock);
2489 	spin_unlock(&fs_info->zone_active_bgs_lock);
2490 	return ret;
2491 }
2492 
2493 static void wait_eb_writebacks(struct btrfs_block_group *block_group)
2494 {
2495 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2496 	const u64 end = btrfs_block_group_end(block_group);
2497 	struct extent_buffer *eb;
2498 	unsigned long index, start = (block_group->start >> fs_info->nodesize_bits);
2499 
2500 	rcu_read_lock();
2501 	xa_for_each_start(&fs_info->buffer_tree, index, eb, start) {
2502 		if (eb->start < block_group->start)
2503 			continue;
2504 		if (eb->start >= end)
2505 			break;
2506 		rcu_read_unlock();
2507 		wait_on_extent_buffer_writeback(eb);
2508 		rcu_read_lock();
2509 	}
2510 	rcu_read_unlock();
2511 }
2512 
2513 static int call_zone_finish(struct btrfs_block_group *block_group,
2514 			    struct btrfs_io_stripe *stripe)
2515 {
2516 	struct btrfs_device *device = stripe->dev;
2517 	const u64 physical = stripe->physical;
2518 	struct btrfs_zoned_device_info *zinfo = device->zone_info;
2519 	int ret;
2520 
2521 	if (!device->bdev)
2522 		return 0;
2523 
2524 	if (zinfo->max_active_zones == 0)
2525 		return 0;
2526 
2527 	if (btrfs_dev_is_sequential(device, physical)) {
2528 		unsigned int nofs_flags;
2529 
2530 		nofs_flags = memalloc_nofs_save();
2531 		ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
2532 				       physical >> SECTOR_SHIFT,
2533 				       zinfo->zone_size >> SECTOR_SHIFT);
2534 		memalloc_nofs_restore(nofs_flags);
2535 
2536 		if (ret)
2537 			return ret;
2538 	}
2539 
2540 	if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA))
2541 		zinfo->reserved_active_zones++;
2542 	btrfs_dev_clear_active_zone(device, physical);
2543 
2544 	return 0;
2545 }
2546 
2547 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
2548 {
2549 	struct btrfs_fs_info *fs_info = block_group->fs_info;
2550 	struct btrfs_chunk_map *map;
2551 	const bool is_metadata = (block_group->flags &
2552 			(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
2553 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
2554 	int ret = 0;
2555 	int i;
2556 
2557 	spin_lock(&block_group->lock);
2558 	if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
2559 		spin_unlock(&block_group->lock);
2560 		return 0;
2561 	}
2562 
2563 	/* Check if we have unwritten allocated space */
2564 	if (is_metadata &&
2565 	    block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
2566 		spin_unlock(&block_group->lock);
2567 		return -EAGAIN;
2568 	}
2569 
2570 	/*
2571 	 * If we are sure that the block group is full (= no more room left for
2572 	 * new allocation) and the IO for the last usable block is completed, we
2573 	 * don't need to wait for the other IOs. This holds because we ensure
2574 	 * the sequential IO submissions using the ZONE_APPEND command for data
2575 	 * and block_group->meta_write_pointer for metadata.
2576 	 */
2577 	if (!fully_written) {
2578 		if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2579 			spin_unlock(&block_group->lock);
2580 			return -EAGAIN;
2581 		}
2582 		spin_unlock(&block_group->lock);
2583 
2584 		ret = btrfs_inc_block_group_ro(block_group, false);
2585 		if (ret)
2586 			return ret;
2587 
2588 		/* Ensure all writes in this block group finish */
2589 		btrfs_wait_block_group_reservations(block_group);
2590 		/* No need to wait for NOCOW writers. Zoned mode does not allow that */
2591 		btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group);
2592 		/* Wait for extent buffers to be written. */
2593 		if (is_metadata)
2594 			wait_eb_writebacks(block_group);
2595 
2596 		spin_lock(&block_group->lock);
2597 
2598 		/*
2599 		 * Bail out if someone already deactivated the block group, or
2600 		 * allocated space is left in the block group.
2601 		 */
2602 		if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2603 			      &block_group->runtime_flags)) {
2604 			spin_unlock(&block_group->lock);
2605 			btrfs_dec_block_group_ro(block_group);
2606 			return 0;
2607 		}
2608 
2609 		if (block_group->reserved ||
2610 		    test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2611 			     &block_group->runtime_flags)) {
2612 			spin_unlock(&block_group->lock);
2613 			btrfs_dec_block_group_ro(block_group);
2614 			return -EAGAIN;
2615 		}
2616 	}
2617 
2618 	clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
2619 	block_group->alloc_offset = block_group->zone_capacity;
2620 	if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
2621 		block_group->meta_write_pointer = block_group->start +
2622 						  block_group->zone_capacity;
2623 	block_group->free_space_ctl->free_space = 0;
2624 	btrfs_clear_treelog_bg(block_group);
2625 	btrfs_clear_data_reloc_bg(block_group);
2626 	spin_unlock(&block_group->lock);
2627 
2628 	down_read(&dev_replace->rwsem);
2629 	map = block_group->physical_map;
2630 	for (i = 0; i < map->num_stripes; i++) {
2631 		ret = call_zone_finish(block_group, &map->stripes[i]);
2632 		if (ret)
2633 			break;
2634 	}
2635 	up_read(&dev_replace->rwsem);
2636 
2637 	if (!ret && !fully_written)
2638 		btrfs_dec_block_group_ro(block_group);
2639 
2640 	spin_lock(&fs_info->zone_active_bgs_lock);
2641 	ASSERT(!list_empty(&block_group->active_bg_list));
2642 	list_del_init(&block_group->active_bg_list);
2643 	spin_unlock(&fs_info->zone_active_bgs_lock);
2644 
2645 	/* For active_bg_list */
2646 	btrfs_put_block_group(block_group);
2647 
2648 	clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2649 
2650 	return ret;
2651 }
2652 
2653 int btrfs_zone_finish(struct btrfs_block_group *block_group)
2654 {
2655 	if (!btrfs_is_zoned(block_group->fs_info))
2656 		return 0;
2657 
2658 	return do_zone_finish(block_group, false);
2659 }
2660 
2661 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
2662 {
2663 	struct btrfs_fs_info *fs_info = fs_devices->fs_info;
2664 	struct btrfs_device *device;
2665 	bool ret = false;
2666 
2667 	if (!btrfs_is_zoned(fs_info))
2668 		return true;
2669 
2670 	if (test_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags))
2671 		return false;
2672 
2673 	/* Check if there is a device with active zones left */
2674 	mutex_lock(&fs_info->chunk_mutex);
2675 	spin_lock(&fs_info->zone_active_bgs_lock);
2676 	list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
2677 		struct btrfs_zoned_device_info *zinfo = device->zone_info;
2678 		int reserved = 0;
2679 
2680 		if (!device->bdev)
2681 			continue;
2682 
2683 		if (!zinfo->max_active_zones) {
2684 			ret = true;
2685 			break;
2686 		}
2687 
2688 		if (flags & BTRFS_BLOCK_GROUP_DATA)
2689 			reserved = zinfo->reserved_active_zones;
2690 
2691 		switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
2692 		case 0: /* single */
2693 		case BTRFS_BLOCK_GROUP_RAID0:
2694 		case BTRFS_BLOCK_GROUP_RAID1:
2695 		case BTRFS_BLOCK_GROUP_RAID1C3:
2696 		case BTRFS_BLOCK_GROUP_RAID1C4:
2697 		case BTRFS_BLOCK_GROUP_RAID10:
2698 			ret = (atomic_read(&zinfo->active_zones_left) >= (1 + reserved));
2699 			break;
2700 		case BTRFS_BLOCK_GROUP_DUP:
2701 			ret = (atomic_read(&zinfo->active_zones_left) >= (2 + reserved));
2702 			break;
2703 		}
2704 		if (ret)
2705 			break;
2706 	}
2707 	spin_unlock(&fs_info->zone_active_bgs_lock);
2708 	mutex_unlock(&fs_info->chunk_mutex);
2709 
2710 	if (!ret)
2711 		set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
2712 
2713 	return ret;
2714 }
2715 
2716 int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2717 {
2718 	struct btrfs_block_group *block_group;
2719 	u64 min_alloc_bytes;
2720 	int ret = 0;
2721 
2722 	if (!btrfs_is_zoned(fs_info))
2723 		return 0;
2724 
2725 	block_group = btrfs_lookup_block_group(fs_info, logical);
2726 	if (WARN_ON_ONCE(!block_group))
2727 		return -ENOENT;
2728 
2729 	/* No MIXED_BG on zoned btrfs. */
2730 	if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
2731 		min_alloc_bytes = fs_info->sectorsize;
2732 	else
2733 		min_alloc_bytes = fs_info->nodesize;
2734 
2735 	/* Bail out if we can allocate more data from this block group. */
2736 	if (logical + length + min_alloc_bytes <=
2737 	    block_group->start + block_group->zone_capacity)
2738 		goto out;
2739 
2740 	ret = do_zone_finish(block_group, true);
2741 
2742 out:
2743 	btrfs_put_block_group(block_group);
2744 	return ret;
2745 }
2746 
2747 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
2748 {
2749 	int ret;
2750 	struct btrfs_block_group *bg =
2751 		container_of(work, struct btrfs_block_group, zone_finish_work);
2752 
2753 	wait_on_extent_buffer_writeback(bg->last_eb);
2754 	free_extent_buffer(bg->last_eb);
2755 	ret = do_zone_finish(bg, true);
2756 	if (ret)
2757 		btrfs_handle_fs_error(bg->fs_info, ret,
2758 				      "Failed to finish block-group's zone");
2759 	btrfs_put_block_group(bg);
2760 }
2761 
2762 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
2763 				   struct extent_buffer *eb)
2764 {
2765 	if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
2766 	    eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
2767 		return;
2768 
2769 	if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
2770 		btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
2771 			  bg->start);
2772 		return;
2773 	}
2774 
2775 	/* For the work */
2776 	btrfs_get_block_group(bg);
2777 	refcount_inc(&eb->refs);
2778 	bg->last_eb = eb;
2779 	INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
2780 	queue_work(system_dfl_wq, &bg->zone_finish_work);
2781 }
2782 
2783 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2784 {
2785 	struct btrfs_fs_info *fs_info = bg->fs_info;
2786 
2787 	spin_lock(&fs_info->relocation_bg_lock);
2788 	if (fs_info->data_reloc_bg == bg->start)
2789 		fs_info->data_reloc_bg = 0;
2790 	spin_unlock(&fs_info->relocation_bg_lock);
2791 }
2792 
2793 void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
2794 {
2795 	struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
2796 	struct btrfs_space_info *space_info = data_sinfo;
2797 	struct btrfs_trans_handle *trans;
2798 	struct btrfs_block_group *bg;
2799 	struct list_head *bg_list;
2800 	u64 alloc_flags;
2801 	bool did_chunk_alloc = false;
2802 	int index;
2803 	int ret;
2804 
2805 	if (!btrfs_is_zoned(fs_info))
2806 		return;
2807 
2808 	if (fs_info->data_reloc_bg)
2809 		return;
2810 
2811 	if (sb_rdonly(fs_info->sb))
2812 		return;
2813 
2814 	alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
2815 	index = btrfs_bg_flags_to_raid_index(alloc_flags);
2816 
2817 again:
2818 	bg_list = &space_info->block_groups[index];
2819 	list_for_each_entry(bg, bg_list, list) {
2820 
2821 		if (bg->alloc_offset != 0)
2822 			continue;
2823 
2824 		if (space_info == data_sinfo) {
2825 			/* Migrate the block group to the data relocation space_info. */
2826 			struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
2827 			int factor;
2828 
2829 			ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
2830 			       "reloc_sinfo->subgroup_id=%d", reloc_sinfo->subgroup_id);
2831 			factor = btrfs_bg_type_to_factor(bg->flags);
2832 
2833 			down_write(&space_info->groups_sem);
2834 			list_del_init(&bg->list);
2835 			up_write(&space_info->groups_sem);
2836 
2837 			spin_lock(&space_info->lock);
2838 			space_info->total_bytes -= bg->length;
2839 			space_info->disk_total -= bg->length * factor;
2840 			space_info->disk_total -= bg->zone_unusable;
2841 			/* There is no allocation ever happened. */
2842 			ASSERT(bg->used == 0, "bg->used=%llu", bg->used);
2843 			/* No super block in a block group on the zoned setup. */
2844 			ASSERT(bg->bytes_super == 0, "bg->bytes_super=%llu", bg->bytes_super);
2845 			spin_unlock(&space_info->lock);
2846 
2847 			bg->space_info = reloc_sinfo;
2848 			if (reloc_sinfo->block_group_kobjs[index] == NULL)
2849 				btrfs_sysfs_add_block_group_type(bg);
2850 
2851 			btrfs_add_bg_to_space_info(fs_info, bg);
2852 		}
2853 
2854 		fs_info->data_reloc_bg = bg->start;
2855 		set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
2856 		btrfs_zone_activate(bg);
2857 
2858 		return;
2859 	}
2860 
2861 	if (did_chunk_alloc)
2862 		return;
2863 
2864 	trans = btrfs_join_transaction(fs_info->tree_root);
2865 	if (IS_ERR(trans))
2866 		return;
2867 
2868 	/* Allocate new BG in the data relocation space_info. */
2869 	space_info = data_sinfo->sub_group[0];
2870 	ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
2871 	       "space_info->subgroup_id=%d", space_info->subgroup_id);
2872 	ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
2873 	btrfs_end_transaction(trans);
2874 	if (ret == 1) {
2875 		/*
2876 		 * We allocated a new block group in the data relocation space_info. We
2877 		 * can take that one.
2878 		 */
2879 		did_chunk_alloc = true;
2880 		goto again;
2881 	}
2882 }
2883 
2884 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2885 {
2886 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2887 	struct btrfs_device *device;
2888 
2889 	if (!btrfs_is_zoned(fs_info))
2890 		return;
2891 
2892 	mutex_lock(&fs_devices->device_list_mutex);
2893 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
2894 		if (device->zone_info) {
2895 			vfree(device->zone_info->zone_cache);
2896 			device->zone_info->zone_cache = NULL;
2897 		}
2898 	}
2899 	mutex_unlock(&fs_devices->device_list_mutex);
2900 }
2901 
2902 bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
2903 {
2904 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2905 	struct btrfs_device *device;
2906 	u64 total = btrfs_super_total_bytes(fs_info->super_copy);
2907 	u64 used = 0;
2908 	u64 factor;
2909 
2910 	ASSERT(btrfs_is_zoned(fs_info));
2911 
2912 	if (fs_info->bg_reclaim_threshold == 0)
2913 		return false;
2914 
2915 	mutex_lock(&fs_devices->device_list_mutex);
2916 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
2917 		if (!device->bdev)
2918 			continue;
2919 
2920 		used += device->bytes_used;
2921 	}
2922 	mutex_unlock(&fs_devices->device_list_mutex);
2923 
2924 	factor = div64_u64(used * 100, total);
2925 	return factor >= fs_info->bg_reclaim_threshold;
2926 }
2927 
2928 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
2929 				       u64 length)
2930 {
2931 	struct btrfs_block_group *block_group;
2932 
2933 	if (!btrfs_is_zoned(fs_info))
2934 		return;
2935 
2936 	block_group = btrfs_lookup_block_group(fs_info, logical);
2937 	/* It should be called on a previous data relocation block group. */
2938 	ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
2939 
2940 	spin_lock(&block_group->lock);
2941 	if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
2942 		goto out;
2943 
2944 	/* All relocation extents are written. */
2945 	if (block_group->start + block_group->alloc_offset == logical + length) {
2946 		/*
2947 		 * Now, release this block group for further allocations and
2948 		 * zone finish.
2949 		 */
2950 		clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
2951 			  &block_group->runtime_flags);
2952 	}
2953 
2954 out:
2955 	spin_unlock(&block_group->lock);
2956 	btrfs_put_block_group(block_group);
2957 }
2958 
2959 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
2960 {
2961 	struct btrfs_block_group *block_group;
2962 	struct btrfs_block_group *min_bg = NULL;
2963 	u64 min_avail = U64_MAX;
2964 	int ret;
2965 
2966 	spin_lock(&fs_info->zone_active_bgs_lock);
2967 	list_for_each_entry(block_group, &fs_info->zone_active_bgs,
2968 			    active_bg_list) {
2969 		u64 avail;
2970 
2971 		spin_lock(&block_group->lock);
2972 		if (block_group->reserved || block_group->alloc_offset == 0 ||
2973 		    !(block_group->flags & BTRFS_BLOCK_GROUP_DATA) ||
2974 		    test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
2975 			spin_unlock(&block_group->lock);
2976 			continue;
2977 		}
2978 
2979 		avail = block_group->zone_capacity - block_group->alloc_offset;
2980 		if (min_avail > avail) {
2981 			if (min_bg)
2982 				btrfs_put_block_group(min_bg);
2983 			min_bg = block_group;
2984 			min_avail = avail;
2985 			btrfs_get_block_group(min_bg);
2986 		}
2987 		spin_unlock(&block_group->lock);
2988 	}
2989 	spin_unlock(&fs_info->zone_active_bgs_lock);
2990 
2991 	if (!min_bg)
2992 		return 0;
2993 
2994 	ret = btrfs_zone_finish(min_bg);
2995 	btrfs_put_block_group(min_bg);
2996 
2997 	return ret < 0 ? ret : 1;
2998 }
2999 
3000 int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish)
3001 {
3002 	struct btrfs_fs_info *fs_info = space_info->fs_info;
3003 	struct btrfs_block_group *bg;
3004 	int index;
3005 
3006 	if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
3007 		return 0;
3008 
3009 	for (;;) {
3010 		int ret;
3011 		bool need_finish = false;
3012 
3013 		down_read(&space_info->groups_sem);
3014 		for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
3015 			list_for_each_entry(bg, &space_info->block_groups[index],
3016 					    list) {
3017 				if (!spin_trylock(&bg->lock))
3018 					continue;
3019 				if (btrfs_zoned_bg_is_full(bg) ||
3020 				    test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
3021 					     &bg->runtime_flags)) {
3022 					spin_unlock(&bg->lock);
3023 					continue;
3024 				}
3025 				spin_unlock(&bg->lock);
3026 
3027 				if (btrfs_zone_activate(bg)) {
3028 					up_read(&space_info->groups_sem);
3029 					return 1;
3030 				}
3031 
3032 				need_finish = true;
3033 			}
3034 		}
3035 		up_read(&space_info->groups_sem);
3036 
3037 		if (!do_finish || !need_finish)
3038 			break;
3039 
3040 		ret = btrfs_zone_finish_one_bg(fs_info);
3041 		if (ret == 0)
3042 			break;
3043 		if (ret < 0)
3044 			return ret;
3045 	}
3046 
3047 	return 0;
3048 }
3049 
3050 /*
3051  * Reserve zones for one metadata block group, one tree-log block group, and one
3052  * system block group.
3053  */
3054 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
3055 {
3056 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3057 	struct btrfs_block_group *block_group;
3058 	struct btrfs_device *device;
3059 	/* Reserve zones for normal SINGLE metadata and tree-log block group. */
3060 	unsigned int metadata_reserve = 2;
3061 	/* Reserve a zone for SINGLE system block group. */
3062 	unsigned int system_reserve = 1;
3063 
3064 	if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
3065 		return;
3066 
3067 	/*
3068 	 * This function is called from the mount context. So, there is no
3069 	 * parallel process touching the bits. No need for read_seqretry().
3070 	 */
3071 	if (fs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
3072 		metadata_reserve = 4;
3073 	if (fs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
3074 		system_reserve = 2;
3075 
3076 	/* Apply the reservation on all the devices. */
3077 	mutex_lock(&fs_devices->device_list_mutex);
3078 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
3079 		if (!device->bdev)
3080 			continue;
3081 
3082 		device->zone_info->reserved_active_zones =
3083 			metadata_reserve + system_reserve;
3084 	}
3085 	mutex_unlock(&fs_devices->device_list_mutex);
3086 
3087 	/* Release reservation for currently active block groups. */
3088 	spin_lock(&fs_info->zone_active_bgs_lock);
3089 	list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
3090 		struct btrfs_chunk_map *map = block_group->physical_map;
3091 
3092 		if (!(block_group->flags &
3093 		      (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
3094 			continue;
3095 
3096 		for (int i = 0; i < map->num_stripes; i++)
3097 			map->stripes[i].dev->zone_info->reserved_active_zones--;
3098 	}
3099 	spin_unlock(&fs_info->zone_active_bgs_lock);
3100 }
3101 
3102 /*
3103  * Reset the zones of unused block groups from @space_info->bytes_zone_unusable.
3104  *
3105  * @space_info:	the space to work on
3106  * @num_bytes:	targeting reclaim bytes
3107  *
3108  * This one resets the zones of a block group, so we can reuse the region
3109  * without removing the block group. On the other hand, btrfs_delete_unused_bgs()
3110  * just removes a block group and frees up the underlying zones. So, we still
3111  * need to allocate a new block group to reuse the zones.
3112  *
3113  * Resetting is faster than deleting/recreating a block group. It is similar
3114  * to freeing the logical space on the regular mode. However, we cannot change
3115  * the block group's profile with this operation.
3116  */
3117 int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes)
3118 {
3119 	struct btrfs_fs_info *fs_info = space_info->fs_info;
3120 	const sector_t zone_size_sectors = fs_info->zone_size >> SECTOR_SHIFT;
3121 
3122 	if (!btrfs_is_zoned(fs_info))
3123 		return 0;
3124 
3125 	while (num_bytes > 0) {
3126 		struct btrfs_chunk_map *map;
3127 		struct btrfs_block_group *bg = NULL;
3128 		bool found = false;
3129 		u64 reclaimed = 0;
3130 
3131 		/*
3132 		 * Here, we choose a fully zone_unusable block group. It's
3133 		 * technically possible to reset a partly zone_unusable block
3134 		 * group, which still has some free space left. However,
3135 		 * handling that needs to cope with the allocation side, which
3136 		 * makes the logic more complex. So, let's handle the easy case
3137 		 * for now.
3138 		 */
3139 		spin_lock(&fs_info->unused_bgs_lock);
3140 		list_for_each_entry(bg, &fs_info->unused_bgs, bg_list) {
3141 			if ((bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != space_info->flags)
3142 				continue;
3143 
3144 			/*
3145 			 * Use trylock to avoid locking order violation. In
3146 			 * btrfs_reclaim_bgs_work(), the lock order is
3147 			 * &bg->lock -> &fs_info->unused_bgs_lock. We skip a
3148 			 * block group if we cannot take its lock.
3149 			 */
3150 			if (!spin_trylock(&bg->lock))
3151 				continue;
3152 			if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length) {
3153 				spin_unlock(&bg->lock);
3154 				continue;
3155 			}
3156 			spin_unlock(&bg->lock);
3157 			found = true;
3158 			break;
3159 		}
3160 		if (!found) {
3161 			spin_unlock(&fs_info->unused_bgs_lock);
3162 			return 0;
3163 		}
3164 
3165 		list_del_init(&bg->bg_list);
3166 		btrfs_put_block_group(bg);
3167 		spin_unlock(&fs_info->unused_bgs_lock);
3168 
3169 		/*
3170 		 * Since the block group is fully zone_unusable and we cannot
3171 		 * allocate from this block group anymore, we don't need to set
3172 		 * this block group read-only.
3173 		 */
3174 
3175 		down_read(&fs_info->dev_replace.rwsem);
3176 		map = bg->physical_map;
3177 		for (int i = 0; i < map->num_stripes; i++) {
3178 			struct btrfs_io_stripe *stripe = &map->stripes[i];
3179 			unsigned int nofs_flags;
3180 			int ret;
3181 
3182 			nofs_flags = memalloc_nofs_save();
3183 			ret = blkdev_zone_mgmt(stripe->dev->bdev, REQ_OP_ZONE_RESET,
3184 					       stripe->physical >> SECTOR_SHIFT,
3185 					       zone_size_sectors);
3186 			memalloc_nofs_restore(nofs_flags);
3187 
3188 			if (ret) {
3189 				up_read(&fs_info->dev_replace.rwsem);
3190 				return ret;
3191 			}
3192 		}
3193 		up_read(&fs_info->dev_replace.rwsem);
3194 
3195 		spin_lock(&space_info->lock);
3196 		spin_lock(&bg->lock);
3197 		ASSERT(!btrfs_is_block_group_used(bg));
3198 		if (bg->ro) {
3199 			spin_unlock(&bg->lock);
3200 			spin_unlock(&space_info->lock);
3201 			continue;
3202 		}
3203 
3204 		reclaimed = bg->alloc_offset;
3205 		bg->zone_unusable = bg->length - bg->zone_capacity;
3206 		bg->alloc_offset = 0;
3207 		/*
3208 		 * The zone was just reset to empty, so alloc_offset went back to
3209 		 * the start of the zone. For metadata/system block groups the
3210 		 * write pointer must follow it back to the start of the zone;
3211 		 * otherwise it stays stale at the previous (finished) zone end,
3212 		 * and metadata written into the reused zone would sit behind the
3213 		 * write pointer, could never be written out in sequential order,
3214 		 * and would be stranded (pinning its folio) until unmount.
3215 		 */
3216 		if (bg->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
3217 			bg->meta_write_pointer = bg->start;
3218 		/*
3219 		 * This holds because we currently reset fully used then freed
3220 		 * block group.
3221 		 */
3222 		ASSERT(reclaimed == bg->zone_capacity,
3223 		       "reclaimed=%llu bg->zone_capacity=%llu", reclaimed, bg->zone_capacity);
3224 		bg->free_space_ctl->free_space += reclaimed;
3225 		space_info->bytes_zone_unusable -= reclaimed;
3226 		spin_unlock(&bg->lock);
3227 		btrfs_return_free_space(space_info, reclaimed);
3228 		spin_unlock(&space_info->lock);
3229 
3230 		if (num_bytes <= reclaimed)
3231 			break;
3232 		num_bytes -= reclaimed;
3233 	}
3234 
3235 	return 0;
3236 }
3237 
3238 void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq)
3239 {
3240 	struct btrfs_block_group *bg;
3241 	u64 data_reloc_bg;
3242 	u64 treelog_bg;
3243 
3244 	seq_puts(seq, "\n  zoned statistics:\n");
3245 
3246 	spin_lock(&fs_info->zone_active_bgs_lock);
3247 	seq_printf(seq, "\tactive block-groups: %zu\n",
3248 			     list_count_nodes(&fs_info->zone_active_bgs));
3249 	spin_unlock(&fs_info->zone_active_bgs_lock);
3250 
3251 	spin_lock(&fs_info->unused_bgs_lock);
3252 	seq_printf(seq, "\t  reclaimable: %zu\n",
3253 			     list_count_nodes(&fs_info->reclaim_bgs));
3254 	seq_printf(seq, "\t  unused: %zu\n", list_count_nodes(&fs_info->unused_bgs));
3255 	spin_unlock(&fs_info->unused_bgs_lock);
3256 
3257 	seq_printf(seq,"\t  need reclaim: %s\n",
3258 		   str_true_false(btrfs_zoned_should_reclaim(fs_info)));
3259 
3260 	data_reloc_bg = data_race(fs_info->data_reloc_bg);
3261 	if (data_reloc_bg)
3262 		seq_printf(seq, "\tdata relocation block-group: %llu\n",
3263 			   data_reloc_bg);
3264 	treelog_bg = data_race(fs_info->treelog_bg);
3265 	if (treelog_bg)
3266 		seq_printf(seq, "\ttree-log block-group: %llu\n", treelog_bg);
3267 
3268 	spin_lock(&fs_info->zone_active_bgs_lock);
3269 	seq_puts(seq, "\tactive zones:\n");
3270 	list_for_each_entry(bg, &fs_info->zone_active_bgs, active_bg_list) {
3271 		u64 start;
3272 		u64 alloc_offset;
3273 		u64 used;
3274 		u64 reserved;
3275 		u64 zone_unusable;
3276 		const char *typestr = btrfs_space_info_type_str(bg->space_info);
3277 
3278 		spin_lock(&bg->lock);
3279 		start = bg->start;
3280 		alloc_offset = bg->alloc_offset;
3281 		used = bg->used;
3282 		reserved = bg->reserved;
3283 		zone_unusable = bg->zone_unusable;
3284 		spin_unlock(&bg->lock);
3285 
3286 		seq_printf(seq,
3287 			   "\t  start: %llu, wp: %llu used: %llu, reserved: %llu, unusable: %llu (%s)\n",
3288 			   start, alloc_offset, used, reserved, zone_unusable, typestr);
3289 	}
3290 	spin_unlock(&fs_info->zone_active_bgs_lock);
3291 }
3292