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