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