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