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