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