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