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