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