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> |
5b316468 NA |
8 | #include "ctree.h" |
9 | #include "volumes.h" | |
10 | #include "zoned.h" | |
11 | #include "rcu-string.h" | |
1cd6121f | 12 | #include "disk-io.h" |
08e11a3d | 13 | #include "block-group.h" |
d3575156 | 14 | #include "transaction.h" |
6143c23c | 15 | #include "dev-replace.h" |
7db1c5d1 | 16 | #include "space-info.h" |
5b316468 NA |
17 | |
18 | /* Maximum number of zones to report per blkdev_report_zones() call */ | |
19 | #define BTRFS_REPORT_NR_ZONES 4096 | |
08e11a3d NA |
20 | /* Invalid allocation pointer value for missing devices */ |
21 | #define WP_MISSING_DEV ((u64)-1) | |
22 | /* Pseudo write pointer value for conventional zone */ | |
23 | #define WP_CONVENTIONAL ((u64)-2) | |
5b316468 | 24 | |
53b74fa9 NA |
25 | /* |
26 | * Location of the first zone of superblock logging zone pairs. | |
27 | * | |
28 | * - primary superblock: 0B (zone 0) | |
29 | * - first copy: 512G (zone starting at that offset) | |
30 | * - second copy: 4T (zone starting at that offset) | |
31 | */ | |
32 | #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL) | |
33 | #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G) | |
34 | #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G) | |
35 | ||
36 | #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET) | |
37 | #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET) | |
38 | ||
12659251 NA |
39 | /* Number of superblock log zones */ |
40 | #define BTRFS_NR_SB_LOG_ZONES 2 | |
41 | ||
ea6f8ddc NA |
42 | /* |
43 | * Minimum of active zones we need: | |
44 | * | |
45 | * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors | |
46 | * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group | |
47 | * - 1 zone for tree-log dedicated block group | |
48 | * - 1 zone for relocation | |
49 | */ | |
50 | #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5) | |
51 | ||
53b74fa9 NA |
52 | /* |
53 | * Maximum supported zone size. Currently, SMR disks have a zone size of | |
54 | * 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range. We do not | |
55 | * expect the zone size to become larger than 8GiB in the near future. | |
56 | */ | |
57 | #define BTRFS_MAX_ZONE_SIZE SZ_8G | |
58 | ||
5daaf552 NA |
59 | #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT) |
60 | ||
61 | static inline bool sb_zone_is_full(const struct blk_zone *zone) | |
62 | { | |
63 | return (zone->cond == BLK_ZONE_COND_FULL) || | |
64 | (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity); | |
65 | } | |
66 | ||
5b316468 NA |
67 | static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data) |
68 | { | |
69 | struct blk_zone *zones = data; | |
70 | ||
71 | memcpy(&zones[idx], zone, sizeof(*zone)); | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
12659251 NA |
76 | static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones, |
77 | u64 *wp_ret) | |
78 | { | |
79 | bool empty[BTRFS_NR_SB_LOG_ZONES]; | |
80 | bool full[BTRFS_NR_SB_LOG_ZONES]; | |
81 | sector_t sector; | |
5daaf552 | 82 | int i; |
12659251 | 83 | |
5daaf552 NA |
84 | for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) { |
85 | ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL); | |
86 | empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY); | |
87 | full[i] = sb_zone_is_full(&zones[i]); | |
88 | } | |
12659251 NA |
89 | |
90 | /* | |
91 | * Possible states of log buffer zones | |
92 | * | |
93 | * Empty[0] In use[0] Full[0] | |
94 | * Empty[1] * x 0 | |
95 | * In use[1] 0 x 0 | |
96 | * Full[1] 1 1 C | |
97 | * | |
98 | * Log position: | |
99 | * *: Special case, no superblock is written | |
100 | * 0: Use write pointer of zones[0] | |
101 | * 1: Use write pointer of zones[1] | |
1a9fd417 | 102 | * C: Compare super blocks from zones[0] and zones[1], use the latest |
12659251 NA |
103 | * one determined by generation |
104 | * x: Invalid state | |
105 | */ | |
106 | ||
107 | if (empty[0] && empty[1]) { | |
108 | /* Special case to distinguish no superblock to read */ | |
109 | *wp_ret = zones[0].start << SECTOR_SHIFT; | |
110 | return -ENOENT; | |
111 | } else if (full[0] && full[1]) { | |
112 | /* Compare two super blocks */ | |
113 | struct address_space *mapping = bdev->bd_inode->i_mapping; | |
114 | struct page *page[BTRFS_NR_SB_LOG_ZONES]; | |
115 | struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES]; | |
116 | int i; | |
117 | ||
118 | for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) { | |
119 | u64 bytenr; | |
120 | ||
121 | bytenr = ((zones[i].start + zones[i].len) | |
122 | << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE; | |
123 | ||
124 | page[i] = read_cache_page_gfp(mapping, | |
125 | bytenr >> PAGE_SHIFT, GFP_NOFS); | |
126 | if (IS_ERR(page[i])) { | |
127 | if (i == 1) | |
128 | btrfs_release_disk_super(super[0]); | |
129 | return PTR_ERR(page[i]); | |
130 | } | |
131 | super[i] = page_address(page[i]); | |
132 | } | |
133 | ||
134 | if (super[0]->generation > super[1]->generation) | |
135 | sector = zones[1].start; | |
136 | else | |
137 | sector = zones[0].start; | |
138 | ||
139 | for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) | |
140 | btrfs_release_disk_super(super[i]); | |
141 | } else if (!full[0] && (empty[1] || full[1])) { | |
142 | sector = zones[0].wp; | |
143 | } else if (full[0]) { | |
144 | sector = zones[1].wp; | |
145 | } else { | |
146 | return -EUCLEAN; | |
147 | } | |
148 | *wp_ret = sector << SECTOR_SHIFT; | |
149 | return 0; | |
150 | } | |
151 | ||
152 | /* | |
53b74fa9 | 153 | * Get the first zone number of the superblock mirror |
12659251 NA |
154 | */ |
155 | static inline u32 sb_zone_number(int shift, int mirror) | |
156 | { | |
53b74fa9 | 157 | u64 zone; |
12659251 | 158 | |
53b74fa9 | 159 | ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX); |
12659251 | 160 | switch (mirror) { |
53b74fa9 NA |
161 | case 0: zone = 0; break; |
162 | case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break; | |
163 | case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break; | |
12659251 NA |
164 | } |
165 | ||
53b74fa9 NA |
166 | ASSERT(zone <= U32_MAX); |
167 | ||
168 | return (u32)zone; | |
12659251 NA |
169 | } |
170 | ||
5b434df8 NA |
171 | static inline sector_t zone_start_sector(u32 zone_number, |
172 | struct block_device *bdev) | |
173 | { | |
174 | return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev)); | |
175 | } | |
176 | ||
177 | static inline u64 zone_start_physical(u32 zone_number, | |
178 | struct btrfs_zoned_device_info *zone_info) | |
179 | { | |
180 | return (u64)zone_number << zone_info->zone_size_shift; | |
181 | } | |
182 | ||
3c9daa09 JT |
183 | /* |
184 | * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block | |
185 | * device into static sized chunks and fake a conventional zone on each of | |
186 | * them. | |
187 | */ | |
188 | static int emulate_report_zones(struct btrfs_device *device, u64 pos, | |
189 | struct blk_zone *zones, unsigned int nr_zones) | |
190 | { | |
191 | const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT; | |
192 | sector_t bdev_size = bdev_nr_sectors(device->bdev); | |
193 | unsigned int i; | |
194 | ||
195 | pos >>= SECTOR_SHIFT; | |
196 | for (i = 0; i < nr_zones; i++) { | |
197 | zones[i].start = i * zone_sectors + pos; | |
198 | zones[i].len = zone_sectors; | |
199 | zones[i].capacity = zone_sectors; | |
200 | zones[i].wp = zones[i].start + zone_sectors; | |
201 | zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL; | |
202 | zones[i].cond = BLK_ZONE_COND_NOT_WP; | |
203 | ||
204 | if (zones[i].wp >= bdev_size) { | |
205 | i++; | |
206 | break; | |
207 | } | |
208 | } | |
209 | ||
210 | return i; | |
211 | } | |
212 | ||
5b316468 NA |
213 | static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos, |
214 | struct blk_zone *zones, unsigned int *nr_zones) | |
215 | { | |
216 | int ret; | |
217 | ||
218 | if (!*nr_zones) | |
219 | return 0; | |
220 | ||
3c9daa09 JT |
221 | if (!bdev_is_zoned(device->bdev)) { |
222 | ret = emulate_report_zones(device, pos, zones, *nr_zones); | |
223 | *nr_zones = ret; | |
224 | return 0; | |
225 | } | |
226 | ||
5b316468 NA |
227 | ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones, |
228 | copy_zone_info_cb, zones); | |
229 | if (ret < 0) { | |
230 | btrfs_err_in_rcu(device->fs_info, | |
231 | "zoned: failed to read zone %llu on %s (devid %llu)", | |
232 | pos, rcu_str_deref(device->name), | |
233 | device->devid); | |
234 | return ret; | |
235 | } | |
236 | *nr_zones = ret; | |
237 | if (!ret) | |
238 | return -EIO; | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
3c9daa09 JT |
243 | /* The emulated zone size is determined from the size of device extent */ |
244 | static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info) | |
245 | { | |
246 | struct btrfs_path *path; | |
247 | struct btrfs_root *root = fs_info->dev_root; | |
248 | struct btrfs_key key; | |
249 | struct extent_buffer *leaf; | |
250 | struct btrfs_dev_extent *dext; | |
251 | int ret = 0; | |
252 | ||
253 | key.objectid = 1; | |
254 | key.type = BTRFS_DEV_EXTENT_KEY; | |
255 | key.offset = 0; | |
256 | ||
257 | path = btrfs_alloc_path(); | |
258 | if (!path) | |
259 | return -ENOMEM; | |
260 | ||
261 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
262 | if (ret < 0) | |
263 | goto out; | |
264 | ||
265 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
ad9a9378 | 266 | ret = btrfs_next_leaf(root, path); |
3c9daa09 JT |
267 | if (ret < 0) |
268 | goto out; | |
269 | /* No dev extents at all? Not good */ | |
270 | if (ret > 0) { | |
271 | ret = -EUCLEAN; | |
272 | goto out; | |
273 | } | |
274 | } | |
275 | ||
276 | leaf = path->nodes[0]; | |
277 | dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); | |
278 | fs_info->zone_size = btrfs_dev_extent_length(leaf, dext); | |
279 | ret = 0; | |
280 | ||
281 | out: | |
282 | btrfs_free_path(path); | |
283 | ||
284 | return ret; | |
285 | } | |
286 | ||
73651042 NA |
287 | int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info) |
288 | { | |
289 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
290 | struct btrfs_device *device; | |
291 | int ret = 0; | |
292 | ||
293 | /* fs_info->zone_size might not set yet. Use the incomapt flag here. */ | |
294 | if (!btrfs_fs_incompat(fs_info, ZONED)) | |
295 | return 0; | |
296 | ||
297 | mutex_lock(&fs_devices->device_list_mutex); | |
298 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
299 | /* We can skip reading of zone info for missing devices */ | |
300 | if (!device->bdev) | |
301 | continue; | |
302 | ||
303 | ret = btrfs_get_dev_zone_info(device); | |
304 | if (ret) | |
305 | break; | |
306 | } | |
307 | mutex_unlock(&fs_devices->device_list_mutex); | |
308 | ||
309 | return ret; | |
310 | } | |
311 | ||
5b316468 NA |
312 | int btrfs_get_dev_zone_info(struct btrfs_device *device) |
313 | { | |
3c9daa09 | 314 | struct btrfs_fs_info *fs_info = device->fs_info; |
5b316468 NA |
315 | struct btrfs_zoned_device_info *zone_info = NULL; |
316 | struct block_device *bdev = device->bdev; | |
ea6f8ddc NA |
317 | struct request_queue *queue = bdev_get_queue(bdev); |
318 | unsigned int max_active_zones; | |
319 | unsigned int nactive; | |
5b316468 NA |
320 | sector_t nr_sectors; |
321 | sector_t sector = 0; | |
322 | struct blk_zone *zones = NULL; | |
323 | unsigned int i, nreported = 0, nr_zones; | |
d734492a | 324 | sector_t zone_sectors; |
3c9daa09 | 325 | char *model, *emulated; |
5b316468 NA |
326 | int ret; |
327 | ||
3c9daa09 JT |
328 | /* |
329 | * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not | |
330 | * yet be set. | |
331 | */ | |
332 | if (!btrfs_fs_incompat(fs_info, ZONED)) | |
5b316468 NA |
333 | return 0; |
334 | ||
335 | if (device->zone_info) | |
336 | return 0; | |
337 | ||
338 | zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL); | |
339 | if (!zone_info) | |
340 | return -ENOMEM; | |
341 | ||
3c9daa09 JT |
342 | if (!bdev_is_zoned(bdev)) { |
343 | if (!fs_info->zone_size) { | |
344 | ret = calculate_emulated_zone_size(fs_info); | |
345 | if (ret) | |
346 | goto out; | |
347 | } | |
348 | ||
349 | ASSERT(fs_info->zone_size); | |
350 | zone_sectors = fs_info->zone_size >> SECTOR_SHIFT; | |
351 | } else { | |
352 | zone_sectors = bdev_zone_sectors(bdev); | |
353 | } | |
354 | ||
5b316468 NA |
355 | /* Check if it's power of 2 (see is_power_of_2) */ |
356 | ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0); | |
357 | zone_info->zone_size = zone_sectors << SECTOR_SHIFT; | |
53b74fa9 NA |
358 | |
359 | /* We reject devices with a zone size larger than 8GB */ | |
360 | if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) { | |
361 | btrfs_err_in_rcu(fs_info, | |
362 | "zoned: %s: zone size %llu larger than supported maximum %llu", | |
363 | rcu_str_deref(device->name), | |
364 | zone_info->zone_size, BTRFS_MAX_ZONE_SIZE); | |
365 | ret = -EINVAL; | |
366 | goto out; | |
367 | } | |
368 | ||
369 | nr_sectors = bdev_nr_sectors(bdev); | |
5b316468 NA |
370 | zone_info->zone_size_shift = ilog2(zone_info->zone_size); |
371 | zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors); | |
372 | if (!IS_ALIGNED(nr_sectors, zone_sectors)) | |
373 | zone_info->nr_zones++; | |
374 | ||
ea6f8ddc NA |
375 | max_active_zones = queue_max_active_zones(queue); |
376 | if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) { | |
377 | btrfs_err_in_rcu(fs_info, | |
378 | "zoned: %s: max active zones %u is too small, need at least %u active zones", | |
379 | rcu_str_deref(device->name), max_active_zones, | |
380 | BTRFS_MIN_ACTIVE_ZONES); | |
381 | ret = -EINVAL; | |
382 | goto out; | |
383 | } | |
384 | zone_info->max_active_zones = max_active_zones; | |
385 | ||
5b316468 NA |
386 | zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); |
387 | if (!zone_info->seq_zones) { | |
388 | ret = -ENOMEM; | |
389 | goto out; | |
390 | } | |
391 | ||
392 | zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); | |
393 | if (!zone_info->empty_zones) { | |
394 | ret = -ENOMEM; | |
395 | goto out; | |
396 | } | |
397 | ||
ea6f8ddc NA |
398 | zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); |
399 | if (!zone_info->active_zones) { | |
400 | ret = -ENOMEM; | |
401 | goto out; | |
402 | } | |
403 | ||
5b316468 NA |
404 | zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL); |
405 | if (!zones) { | |
406 | ret = -ENOMEM; | |
407 | goto out; | |
408 | } | |
409 | ||
410 | /* Get zones type */ | |
ea6f8ddc | 411 | nactive = 0; |
5b316468 NA |
412 | while (sector < nr_sectors) { |
413 | nr_zones = BTRFS_REPORT_NR_ZONES; | |
414 | ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones, | |
415 | &nr_zones); | |
416 | if (ret) | |
417 | goto out; | |
418 | ||
419 | for (i = 0; i < nr_zones; i++) { | |
420 | if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ) | |
421 | __set_bit(nreported, zone_info->seq_zones); | |
ea6f8ddc NA |
422 | switch (zones[i].cond) { |
423 | case BLK_ZONE_COND_EMPTY: | |
5b316468 | 424 | __set_bit(nreported, zone_info->empty_zones); |
ea6f8ddc NA |
425 | break; |
426 | case BLK_ZONE_COND_IMP_OPEN: | |
427 | case BLK_ZONE_COND_EXP_OPEN: | |
428 | case BLK_ZONE_COND_CLOSED: | |
429 | __set_bit(nreported, zone_info->active_zones); | |
430 | nactive++; | |
431 | break; | |
432 | } | |
5b316468 NA |
433 | nreported++; |
434 | } | |
435 | sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len; | |
436 | } | |
437 | ||
438 | if (nreported != zone_info->nr_zones) { | |
439 | btrfs_err_in_rcu(device->fs_info, | |
440 | "inconsistent number of zones on %s (%u/%u)", | |
441 | rcu_str_deref(device->name), nreported, | |
442 | zone_info->nr_zones); | |
443 | ret = -EIO; | |
444 | goto out; | |
445 | } | |
446 | ||
ea6f8ddc NA |
447 | if (max_active_zones) { |
448 | if (nactive > max_active_zones) { | |
449 | btrfs_err_in_rcu(device->fs_info, | |
450 | "zoned: %u active zones on %s exceeds max_active_zones %u", | |
451 | nactive, rcu_str_deref(device->name), | |
452 | max_active_zones); | |
453 | ret = -EIO; | |
454 | goto out; | |
455 | } | |
456 | atomic_set(&zone_info->active_zones_left, | |
457 | max_active_zones - nactive); | |
458 | } | |
459 | ||
12659251 NA |
460 | /* Validate superblock log */ |
461 | nr_zones = BTRFS_NR_SB_LOG_ZONES; | |
462 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
463 | u32 sb_zone; | |
464 | u64 sb_wp; | |
465 | int sb_pos = BTRFS_NR_SB_LOG_ZONES * i; | |
466 | ||
467 | sb_zone = sb_zone_number(zone_info->zone_size_shift, i); | |
468 | if (sb_zone + 1 >= zone_info->nr_zones) | |
469 | continue; | |
470 | ||
5b434df8 NA |
471 | ret = btrfs_get_dev_zones(device, |
472 | zone_start_physical(sb_zone, zone_info), | |
12659251 NA |
473 | &zone_info->sb_zones[sb_pos], |
474 | &nr_zones); | |
475 | if (ret) | |
476 | goto out; | |
477 | ||
478 | if (nr_zones != BTRFS_NR_SB_LOG_ZONES) { | |
479 | btrfs_err_in_rcu(device->fs_info, | |
480 | "zoned: failed to read super block log zone info at devid %llu zone %u", | |
481 | device->devid, sb_zone); | |
482 | ret = -EUCLEAN; | |
483 | goto out; | |
484 | } | |
485 | ||
486 | /* | |
1a9fd417 | 487 | * If zones[0] is conventional, always use the beginning of the |
12659251 NA |
488 | * zone to record superblock. No need to validate in that case. |
489 | */ | |
490 | if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type == | |
491 | BLK_ZONE_TYPE_CONVENTIONAL) | |
492 | continue; | |
493 | ||
494 | ret = sb_write_pointer(device->bdev, | |
495 | &zone_info->sb_zones[sb_pos], &sb_wp); | |
496 | if (ret != -ENOENT && ret) { | |
497 | btrfs_err_in_rcu(device->fs_info, | |
498 | "zoned: super block log zone corrupted devid %llu zone %u", | |
499 | device->devid, sb_zone); | |
500 | ret = -EUCLEAN; | |
501 | goto out; | |
502 | } | |
503 | } | |
504 | ||
505 | ||
5b316468 NA |
506 | kfree(zones); |
507 | ||
508 | device->zone_info = zone_info; | |
509 | ||
3c9daa09 JT |
510 | switch (bdev_zoned_model(bdev)) { |
511 | case BLK_ZONED_HM: | |
512 | model = "host-managed zoned"; | |
513 | emulated = ""; | |
514 | break; | |
515 | case BLK_ZONED_HA: | |
516 | model = "host-aware zoned"; | |
517 | emulated = ""; | |
518 | break; | |
519 | case BLK_ZONED_NONE: | |
520 | model = "regular"; | |
521 | emulated = "emulated "; | |
522 | break; | |
523 | default: | |
524 | /* Just in case */ | |
525 | btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s", | |
526 | bdev_zoned_model(bdev), | |
527 | rcu_str_deref(device->name)); | |
528 | ret = -EOPNOTSUPP; | |
529 | goto out_free_zone_info; | |
530 | } | |
531 | ||
532 | btrfs_info_in_rcu(fs_info, | |
533 | "%s block device %s, %u %szones of %llu bytes", | |
534 | model, rcu_str_deref(device->name), zone_info->nr_zones, | |
535 | emulated, zone_info->zone_size); | |
5b316468 NA |
536 | |
537 | return 0; | |
538 | ||
539 | out: | |
540 | kfree(zones); | |
3c9daa09 | 541 | out_free_zone_info: |
ea6f8ddc | 542 | bitmap_free(zone_info->active_zones); |
5b316468 NA |
543 | bitmap_free(zone_info->empty_zones); |
544 | bitmap_free(zone_info->seq_zones); | |
545 | kfree(zone_info); | |
3c9daa09 | 546 | device->zone_info = NULL; |
5b316468 NA |
547 | |
548 | return ret; | |
549 | } | |
550 | ||
551 | void btrfs_destroy_dev_zone_info(struct btrfs_device *device) | |
552 | { | |
553 | struct btrfs_zoned_device_info *zone_info = device->zone_info; | |
554 | ||
555 | if (!zone_info) | |
556 | return; | |
557 | ||
ea6f8ddc | 558 | bitmap_free(zone_info->active_zones); |
5b316468 NA |
559 | bitmap_free(zone_info->seq_zones); |
560 | bitmap_free(zone_info->empty_zones); | |
561 | kfree(zone_info); | |
562 | device->zone_info = NULL; | |
563 | } | |
564 | ||
565 | int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, | |
566 | struct blk_zone *zone) | |
567 | { | |
568 | unsigned int nr_zones = 1; | |
569 | int ret; | |
570 | ||
571 | ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones); | |
572 | if (ret != 0 || !nr_zones) | |
573 | return ret ? ret : -EIO; | |
574 | ||
575 | return 0; | |
576 | } | |
b70f5097 NA |
577 | |
578 | int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info) | |
579 | { | |
580 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
581 | struct btrfs_device *device; | |
582 | u64 zoned_devices = 0; | |
583 | u64 nr_devices = 0; | |
584 | u64 zone_size = 0; | |
3c9daa09 | 585 | const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED); |
b70f5097 NA |
586 | int ret = 0; |
587 | ||
588 | /* Count zoned devices */ | |
589 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
590 | enum blk_zoned_model model; | |
591 | ||
592 | if (!device->bdev) | |
593 | continue; | |
594 | ||
595 | model = bdev_zoned_model(device->bdev); | |
3c9daa09 JT |
596 | /* |
597 | * A Host-Managed zoned device must be used as a zoned device. | |
598 | * A Host-Aware zoned device and a non-zoned devices can be | |
599 | * treated as a zoned device, if ZONED flag is enabled in the | |
600 | * superblock. | |
601 | */ | |
b70f5097 | 602 | if (model == BLK_ZONED_HM || |
3c9daa09 JT |
603 | (model == BLK_ZONED_HA && incompat_zoned) || |
604 | (model == BLK_ZONED_NONE && incompat_zoned)) { | |
605 | struct btrfs_zoned_device_info *zone_info = | |
606 | device->zone_info; | |
862931c7 NA |
607 | |
608 | zone_info = device->zone_info; | |
b70f5097 NA |
609 | zoned_devices++; |
610 | if (!zone_size) { | |
862931c7 NA |
611 | zone_size = zone_info->zone_size; |
612 | } else if (zone_info->zone_size != zone_size) { | |
b70f5097 NA |
613 | btrfs_err(fs_info, |
614 | "zoned: unequal block device zone sizes: have %llu found %llu", | |
615 | device->zone_info->zone_size, | |
616 | zone_size); | |
617 | ret = -EINVAL; | |
618 | goto out; | |
619 | } | |
620 | } | |
621 | nr_devices++; | |
622 | } | |
623 | ||
624 | if (!zoned_devices && !incompat_zoned) | |
625 | goto out; | |
626 | ||
627 | if (!zoned_devices && incompat_zoned) { | |
628 | /* No zoned block device found on ZONED filesystem */ | |
629 | btrfs_err(fs_info, | |
630 | "zoned: no zoned devices found on a zoned filesystem"); | |
631 | ret = -EINVAL; | |
632 | goto out; | |
633 | } | |
634 | ||
635 | if (zoned_devices && !incompat_zoned) { | |
636 | btrfs_err(fs_info, | |
637 | "zoned: mode not enabled but zoned device found"); | |
638 | ret = -EINVAL; | |
639 | goto out; | |
640 | } | |
641 | ||
642 | if (zoned_devices != nr_devices) { | |
643 | btrfs_err(fs_info, | |
644 | "zoned: cannot mix zoned and regular devices"); | |
645 | ret = -EINVAL; | |
646 | goto out; | |
647 | } | |
648 | ||
649 | /* | |
650 | * stripe_size is always aligned to BTRFS_STRIPE_LEN in | |
f6f39f7a | 651 | * btrfs_create_chunk(). Since we want stripe_len == zone_size, |
b70f5097 NA |
652 | * check the alignment here. |
653 | */ | |
654 | if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) { | |
655 | btrfs_err(fs_info, | |
656 | "zoned: zone size %llu not aligned to stripe %u", | |
657 | zone_size, BTRFS_STRIPE_LEN); | |
658 | ret = -EINVAL; | |
659 | goto out; | |
660 | } | |
661 | ||
a589dde0 NA |
662 | if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
663 | btrfs_err(fs_info, "zoned: mixed block groups not supported"); | |
664 | ret = -EINVAL; | |
665 | goto out; | |
666 | } | |
667 | ||
b70f5097 | 668 | fs_info->zone_size = zone_size; |
1cd6121f | 669 | fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED; |
b70f5097 | 670 | |
b53429ba JT |
671 | /* |
672 | * Check mount options here, because we might change fs_info->zoned | |
673 | * from fs_info->zone_size. | |
674 | */ | |
675 | ret = btrfs_check_mountopts_zoned(fs_info); | |
676 | if (ret) | |
677 | goto out; | |
678 | ||
b70f5097 NA |
679 | btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size); |
680 | out: | |
681 | return ret; | |
682 | } | |
5d1ab66c NA |
683 | |
684 | int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info) | |
685 | { | |
686 | if (!btrfs_is_zoned(info)) | |
687 | return 0; | |
688 | ||
689 | /* | |
690 | * Space cache writing is not COWed. Disable that to avoid write errors | |
691 | * in sequential zones. | |
692 | */ | |
693 | if (btrfs_test_opt(info, SPACE_CACHE)) { | |
694 | btrfs_err(info, "zoned: space cache v1 is not supported"); | |
695 | return -EINVAL; | |
696 | } | |
697 | ||
d206e9c9 NA |
698 | if (btrfs_test_opt(info, NODATACOW)) { |
699 | btrfs_err(info, "zoned: NODATACOW not supported"); | |
700 | return -EINVAL; | |
701 | } | |
702 | ||
5d1ab66c NA |
703 | return 0; |
704 | } | |
12659251 NA |
705 | |
706 | static int sb_log_location(struct block_device *bdev, struct blk_zone *zones, | |
707 | int rw, u64 *bytenr_ret) | |
708 | { | |
709 | u64 wp; | |
710 | int ret; | |
711 | ||
712 | if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) { | |
713 | *bytenr_ret = zones[0].start << SECTOR_SHIFT; | |
714 | return 0; | |
715 | } | |
716 | ||
717 | ret = sb_write_pointer(bdev, zones, &wp); | |
718 | if (ret != -ENOENT && ret < 0) | |
719 | return ret; | |
720 | ||
721 | if (rw == WRITE) { | |
722 | struct blk_zone *reset = NULL; | |
723 | ||
724 | if (wp == zones[0].start << SECTOR_SHIFT) | |
725 | reset = &zones[0]; | |
726 | else if (wp == zones[1].start << SECTOR_SHIFT) | |
727 | reset = &zones[1]; | |
728 | ||
729 | if (reset && reset->cond != BLK_ZONE_COND_EMPTY) { | |
5daaf552 | 730 | ASSERT(sb_zone_is_full(reset)); |
12659251 NA |
731 | |
732 | ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET, | |
733 | reset->start, reset->len, | |
734 | GFP_NOFS); | |
735 | if (ret) | |
736 | return ret; | |
737 | ||
738 | reset->cond = BLK_ZONE_COND_EMPTY; | |
739 | reset->wp = reset->start; | |
740 | } | |
741 | } else if (ret != -ENOENT) { | |
9658b72e NA |
742 | /* |
743 | * For READ, we want the previous one. Move write pointer to | |
744 | * the end of a zone, if it is at the head of a zone. | |
745 | */ | |
746 | u64 zone_end = 0; | |
747 | ||
12659251 | 748 | if (wp == zones[0].start << SECTOR_SHIFT) |
9658b72e NA |
749 | zone_end = zones[1].start + zones[1].capacity; |
750 | else if (wp == zones[1].start << SECTOR_SHIFT) | |
751 | zone_end = zones[0].start + zones[0].capacity; | |
752 | if (zone_end) | |
753 | wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT, | |
754 | BTRFS_SUPER_INFO_SIZE); | |
755 | ||
12659251 NA |
756 | wp -= BTRFS_SUPER_INFO_SIZE; |
757 | } | |
758 | ||
759 | *bytenr_ret = wp; | |
760 | return 0; | |
761 | ||
762 | } | |
763 | ||
764 | int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw, | |
765 | u64 *bytenr_ret) | |
766 | { | |
767 | struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES]; | |
d734492a | 768 | sector_t zone_sectors; |
12659251 NA |
769 | u32 sb_zone; |
770 | int ret; | |
12659251 NA |
771 | u8 zone_sectors_shift; |
772 | sector_t nr_sectors; | |
773 | u32 nr_zones; | |
774 | ||
775 | if (!bdev_is_zoned(bdev)) { | |
776 | *bytenr_ret = btrfs_sb_offset(mirror); | |
777 | return 0; | |
778 | } | |
779 | ||
780 | ASSERT(rw == READ || rw == WRITE); | |
781 | ||
782 | zone_sectors = bdev_zone_sectors(bdev); | |
783 | if (!is_power_of_2(zone_sectors)) | |
784 | return -EINVAL; | |
12659251 | 785 | zone_sectors_shift = ilog2(zone_sectors); |
ac7ac461 | 786 | nr_sectors = bdev_nr_sectors(bdev); |
12659251 NA |
787 | nr_zones = nr_sectors >> zone_sectors_shift; |
788 | ||
789 | sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror); | |
790 | if (sb_zone + 1 >= nr_zones) | |
791 | return -ENOENT; | |
792 | ||
5b434df8 | 793 | ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev), |
12659251 NA |
794 | BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb, |
795 | zones); | |
796 | if (ret < 0) | |
797 | return ret; | |
798 | if (ret != BTRFS_NR_SB_LOG_ZONES) | |
799 | return -EIO; | |
800 | ||
801 | return sb_log_location(bdev, zones, rw, bytenr_ret); | |
802 | } | |
803 | ||
804 | int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw, | |
805 | u64 *bytenr_ret) | |
806 | { | |
807 | struct btrfs_zoned_device_info *zinfo = device->zone_info; | |
808 | u32 zone_num; | |
809 | ||
d6639b35 NA |
810 | /* |
811 | * For a zoned filesystem on a non-zoned block device, use the same | |
812 | * super block locations as regular filesystem. Doing so, the super | |
813 | * block can always be retrieved and the zoned flag of the volume | |
814 | * detected from the super block information. | |
815 | */ | |
816 | if (!bdev_is_zoned(device->bdev)) { | |
12659251 NA |
817 | *bytenr_ret = btrfs_sb_offset(mirror); |
818 | return 0; | |
819 | } | |
820 | ||
821 | zone_num = sb_zone_number(zinfo->zone_size_shift, mirror); | |
822 | if (zone_num + 1 >= zinfo->nr_zones) | |
823 | return -ENOENT; | |
824 | ||
825 | return sb_log_location(device->bdev, | |
826 | &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror], | |
827 | rw, bytenr_ret); | |
828 | } | |
829 | ||
830 | static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo, | |
831 | int mirror) | |
832 | { | |
833 | u32 zone_num; | |
834 | ||
835 | if (!zinfo) | |
836 | return false; | |
837 | ||
838 | zone_num = sb_zone_number(zinfo->zone_size_shift, mirror); | |
839 | if (zone_num + 1 >= zinfo->nr_zones) | |
840 | return false; | |
841 | ||
842 | if (!test_bit(zone_num, zinfo->seq_zones)) | |
843 | return false; | |
844 | ||
845 | return true; | |
846 | } | |
847 | ||
8376d9e1 | 848 | int btrfs_advance_sb_log(struct btrfs_device *device, int mirror) |
12659251 NA |
849 | { |
850 | struct btrfs_zoned_device_info *zinfo = device->zone_info; | |
851 | struct blk_zone *zone; | |
8376d9e1 | 852 | int i; |
12659251 NA |
853 | |
854 | if (!is_sb_log_zone(zinfo, mirror)) | |
8376d9e1 | 855 | return 0; |
12659251 NA |
856 | |
857 | zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror]; | |
8376d9e1 NA |
858 | for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) { |
859 | /* Advance the next zone */ | |
860 | if (zone->cond == BLK_ZONE_COND_FULL) { | |
861 | zone++; | |
862 | continue; | |
863 | } | |
864 | ||
12659251 NA |
865 | if (zone->cond == BLK_ZONE_COND_EMPTY) |
866 | zone->cond = BLK_ZONE_COND_IMP_OPEN; | |
867 | ||
8376d9e1 NA |
868 | zone->wp += SUPER_INFO_SECTORS; |
869 | ||
870 | if (sb_zone_is_full(zone)) { | |
871 | /* | |
872 | * No room left to write new superblock. Since | |
873 | * superblock is written with REQ_SYNC, it is safe to | |
874 | * finish the zone now. | |
875 | * | |
876 | * If the write pointer is exactly at the capacity, | |
877 | * explicit ZONE_FINISH is not necessary. | |
878 | */ | |
879 | if (zone->wp != zone->start + zone->capacity) { | |
880 | int ret; | |
881 | ||
882 | ret = blkdev_zone_mgmt(device->bdev, | |
883 | REQ_OP_ZONE_FINISH, zone->start, | |
884 | zone->len, GFP_NOFS); | |
885 | if (ret) | |
886 | return ret; | |
887 | } | |
12659251 | 888 | |
8376d9e1 | 889 | zone->wp = zone->start + zone->len; |
12659251 | 890 | zone->cond = BLK_ZONE_COND_FULL; |
8376d9e1 NA |
891 | } |
892 | return 0; | |
12659251 NA |
893 | } |
894 | ||
8376d9e1 NA |
895 | /* All the zones are FULL. Should not reach here. */ |
896 | ASSERT(0); | |
897 | return -EIO; | |
12659251 NA |
898 | } |
899 | ||
900 | int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror) | |
901 | { | |
902 | sector_t zone_sectors; | |
903 | sector_t nr_sectors; | |
904 | u8 zone_sectors_shift; | |
905 | u32 sb_zone; | |
906 | u32 nr_zones; | |
907 | ||
908 | zone_sectors = bdev_zone_sectors(bdev); | |
909 | zone_sectors_shift = ilog2(zone_sectors); | |
ac7ac461 | 910 | nr_sectors = bdev_nr_sectors(bdev); |
12659251 NA |
911 | nr_zones = nr_sectors >> zone_sectors_shift; |
912 | ||
913 | sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror); | |
914 | if (sb_zone + 1 >= nr_zones) | |
915 | return -ENOENT; | |
916 | ||
917 | return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET, | |
5b434df8 | 918 | zone_start_sector(sb_zone, bdev), |
12659251 NA |
919 | zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS); |
920 | } | |
1cd6121f NA |
921 | |
922 | /** | |
923 | * btrfs_find_allocatable_zones - find allocatable zones within a given region | |
924 | * | |
925 | * @device: the device to allocate a region on | |
926 | * @hole_start: the position of the hole to allocate the region | |
927 | * @num_bytes: size of wanted region | |
928 | * @hole_end: the end of the hole | |
929 | * @return: position of allocatable zones | |
930 | * | |
931 | * Allocatable region should not contain any superblock locations. | |
932 | */ | |
933 | u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start, | |
934 | u64 hole_end, u64 num_bytes) | |
935 | { | |
936 | struct btrfs_zoned_device_info *zinfo = device->zone_info; | |
937 | const u8 shift = zinfo->zone_size_shift; | |
938 | u64 nzones = num_bytes >> shift; | |
939 | u64 pos = hole_start; | |
940 | u64 begin, end; | |
941 | bool have_sb; | |
942 | int i; | |
943 | ||
944 | ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size)); | |
945 | ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size)); | |
946 | ||
947 | while (pos < hole_end) { | |
948 | begin = pos >> shift; | |
949 | end = begin + nzones; | |
950 | ||
951 | if (end > zinfo->nr_zones) | |
952 | return hole_end; | |
953 | ||
954 | /* Check if zones in the region are all empty */ | |
955 | if (btrfs_dev_is_sequential(device, pos) && | |
956 | find_next_zero_bit(zinfo->empty_zones, end, begin) != end) { | |
957 | pos += zinfo->zone_size; | |
958 | continue; | |
959 | } | |
960 | ||
961 | have_sb = false; | |
962 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
963 | u32 sb_zone; | |
964 | u64 sb_pos; | |
965 | ||
966 | sb_zone = sb_zone_number(shift, i); | |
967 | if (!(end <= sb_zone || | |
968 | sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) { | |
969 | have_sb = true; | |
5b434df8 NA |
970 | pos = zone_start_physical( |
971 | sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo); | |
1cd6121f NA |
972 | break; |
973 | } | |
974 | ||
975 | /* We also need to exclude regular superblock positions */ | |
976 | sb_pos = btrfs_sb_offset(i); | |
977 | if (!(pos + num_bytes <= sb_pos || | |
978 | sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) { | |
979 | have_sb = true; | |
980 | pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE, | |
981 | zinfo->zone_size); | |
982 | break; | |
983 | } | |
984 | } | |
985 | if (!have_sb) | |
986 | break; | |
987 | } | |
988 | ||
989 | return pos; | |
990 | } | |
991 | ||
992 | int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical, | |
993 | u64 length, u64 *bytes) | |
994 | { | |
995 | int ret; | |
996 | ||
997 | *bytes = 0; | |
998 | ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET, | |
999 | physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT, | |
1000 | GFP_NOFS); | |
1001 | if (ret) | |
1002 | return ret; | |
1003 | ||
1004 | *bytes = length; | |
1005 | while (length) { | |
1006 | btrfs_dev_set_zone_empty(device, physical); | |
1007 | physical += device->zone_info->zone_size; | |
1008 | length -= device->zone_info->zone_size; | |
1009 | } | |
1010 | ||
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size) | |
1015 | { | |
1016 | struct btrfs_zoned_device_info *zinfo = device->zone_info; | |
1017 | const u8 shift = zinfo->zone_size_shift; | |
1018 | unsigned long begin = start >> shift; | |
1019 | unsigned long end = (start + size) >> shift; | |
1020 | u64 pos; | |
1021 | int ret; | |
1022 | ||
1023 | ASSERT(IS_ALIGNED(start, zinfo->zone_size)); | |
1024 | ASSERT(IS_ALIGNED(size, zinfo->zone_size)); | |
1025 | ||
1026 | if (end > zinfo->nr_zones) | |
1027 | return -ERANGE; | |
1028 | ||
1029 | /* All the zones are conventional */ | |
1030 | if (find_next_bit(zinfo->seq_zones, begin, end) == end) | |
1031 | return 0; | |
1032 | ||
1033 | /* All the zones are sequential and empty */ | |
1034 | if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end && | |
1035 | find_next_zero_bit(zinfo->empty_zones, begin, end) == end) | |
1036 | return 0; | |
1037 | ||
1038 | for (pos = start; pos < start + size; pos += zinfo->zone_size) { | |
1039 | u64 reset_bytes; | |
1040 | ||
1041 | if (!btrfs_dev_is_sequential(device, pos) || | |
1042 | btrfs_dev_is_empty_zone(device, pos)) | |
1043 | continue; | |
1044 | ||
1045 | /* Free regions should be empty */ | |
1046 | btrfs_warn_in_rcu( | |
1047 | device->fs_info, | |
1048 | "zoned: resetting device %s (devid %llu) zone %llu for allocation", | |
1049 | rcu_str_deref(device->name), device->devid, pos >> shift); | |
1050 | WARN_ON_ONCE(1); | |
1051 | ||
1052 | ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size, | |
1053 | &reset_bytes); | |
1054 | if (ret) | |
1055 | return ret; | |
1056 | } | |
1057 | ||
1058 | return 0; | |
1059 | } | |
08e11a3d | 1060 | |
a94794d5 NA |
1061 | /* |
1062 | * Calculate an allocation pointer from the extent allocation information | |
1063 | * for a block group consist of conventional zones. It is pointed to the | |
1064 | * end of the highest addressed extent in the block group as an allocation | |
1065 | * offset. | |
1066 | */ | |
1067 | static int calculate_alloc_pointer(struct btrfs_block_group *cache, | |
1068 | u64 *offset_ret) | |
1069 | { | |
1070 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
1071 | struct btrfs_root *root = fs_info->extent_root; | |
1072 | struct btrfs_path *path; | |
1073 | struct btrfs_key key; | |
1074 | struct btrfs_key found_key; | |
1075 | int ret; | |
1076 | u64 length; | |
1077 | ||
1078 | path = btrfs_alloc_path(); | |
1079 | if (!path) | |
1080 | return -ENOMEM; | |
1081 | ||
1082 | key.objectid = cache->start + cache->length; | |
1083 | key.type = 0; | |
1084 | key.offset = 0; | |
1085 | ||
1086 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1087 | /* We should not find the exact match */ | |
1088 | if (!ret) | |
1089 | ret = -EUCLEAN; | |
1090 | if (ret < 0) | |
1091 | goto out; | |
1092 | ||
1093 | ret = btrfs_previous_extent_item(root, path, cache->start); | |
1094 | if (ret) { | |
1095 | if (ret == 1) { | |
1096 | ret = 0; | |
1097 | *offset_ret = 0; | |
1098 | } | |
1099 | goto out; | |
1100 | } | |
1101 | ||
1102 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); | |
1103 | ||
1104 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY) | |
1105 | length = found_key.offset; | |
1106 | else | |
1107 | length = fs_info->nodesize; | |
1108 | ||
1109 | if (!(found_key.objectid >= cache->start && | |
1110 | found_key.objectid + length <= cache->start + cache->length)) { | |
1111 | ret = -EUCLEAN; | |
1112 | goto out; | |
1113 | } | |
1114 | *offset_ret = found_key.objectid + length - cache->start; | |
1115 | ret = 0; | |
1116 | ||
1117 | out: | |
1118 | btrfs_free_path(path); | |
1119 | return ret; | |
1120 | } | |
1121 | ||
1122 | int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) | |
08e11a3d NA |
1123 | { |
1124 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
1125 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; | |
1126 | struct extent_map *em; | |
1127 | struct map_lookup *map; | |
1128 | struct btrfs_device *device; | |
1129 | u64 logical = cache->start; | |
1130 | u64 length = cache->length; | |
1131 | u64 physical = 0; | |
1132 | int ret; | |
1133 | int i; | |
1134 | unsigned int nofs_flag; | |
1135 | u64 *alloc_offsets = NULL; | |
8eae532b | 1136 | u64 *caps = NULL; |
a94794d5 | 1137 | u64 last_alloc = 0; |
08e11a3d NA |
1138 | u32 num_sequential = 0, num_conventional = 0; |
1139 | ||
1140 | if (!btrfs_is_zoned(fs_info)) | |
1141 | return 0; | |
1142 | ||
1143 | /* Sanity check */ | |
1144 | if (!IS_ALIGNED(length, fs_info->zone_size)) { | |
1145 | btrfs_err(fs_info, | |
1146 | "zoned: block group %llu len %llu unaligned to zone size %llu", | |
1147 | logical, length, fs_info->zone_size); | |
1148 | return -EIO; | |
1149 | } | |
1150 | ||
1151 | /* Get the chunk mapping */ | |
1152 | read_lock(&em_tree->lock); | |
1153 | em = lookup_extent_mapping(em_tree, logical, length); | |
1154 | read_unlock(&em_tree->lock); | |
1155 | ||
1156 | if (!em) | |
1157 | return -EINVAL; | |
1158 | ||
1159 | map = em->map_lookup; | |
1160 | ||
1161 | alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS); | |
1162 | if (!alloc_offsets) { | |
1163 | free_extent_map(em); | |
1164 | return -ENOMEM; | |
1165 | } | |
1166 | ||
8eae532b NA |
1167 | caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS); |
1168 | if (!caps) { | |
1169 | ret = -ENOMEM; | |
1170 | goto out; | |
1171 | } | |
1172 | ||
08e11a3d NA |
1173 | for (i = 0; i < map->num_stripes; i++) { |
1174 | bool is_sequential; | |
1175 | struct blk_zone zone; | |
6143c23c NA |
1176 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
1177 | int dev_replace_is_ongoing = 0; | |
08e11a3d NA |
1178 | |
1179 | device = map->stripes[i].dev; | |
1180 | physical = map->stripes[i].physical; | |
1181 | ||
1182 | if (device->bdev == NULL) { | |
1183 | alloc_offsets[i] = WP_MISSING_DEV; | |
1184 | continue; | |
1185 | } | |
1186 | ||
1187 | is_sequential = btrfs_dev_is_sequential(device, physical); | |
1188 | if (is_sequential) | |
1189 | num_sequential++; | |
1190 | else | |
1191 | num_conventional++; | |
1192 | ||
1193 | if (!is_sequential) { | |
1194 | alloc_offsets[i] = WP_CONVENTIONAL; | |
1195 | continue; | |
1196 | } | |
1197 | ||
1198 | /* | |
1199 | * This zone will be used for allocation, so mark this zone | |
1200 | * non-empty. | |
1201 | */ | |
1202 | btrfs_dev_clear_zone_empty(device, physical); | |
1203 | ||
6143c23c NA |
1204 | down_read(&dev_replace->rwsem); |
1205 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); | |
1206 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) | |
1207 | btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical); | |
1208 | up_read(&dev_replace->rwsem); | |
1209 | ||
08e11a3d NA |
1210 | /* |
1211 | * The group is mapped to a sequential zone. Get the zone write | |
1212 | * pointer to determine the allocation offset within the zone. | |
1213 | */ | |
1214 | WARN_ON(!IS_ALIGNED(physical, fs_info->zone_size)); | |
1215 | nofs_flag = memalloc_nofs_save(); | |
1216 | ret = btrfs_get_dev_zone(device, physical, &zone); | |
1217 | memalloc_nofs_restore(nofs_flag); | |
1218 | if (ret == -EIO || ret == -EOPNOTSUPP) { | |
1219 | ret = 0; | |
1220 | alloc_offsets[i] = WP_MISSING_DEV; | |
1221 | continue; | |
1222 | } else if (ret) { | |
1223 | goto out; | |
1224 | } | |
1225 | ||
784daf2b | 1226 | if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) { |
47cdfb5e NA |
1227 | btrfs_err_in_rcu(fs_info, |
1228 | "zoned: unexpected conventional zone %llu on device %s (devid %llu)", | |
1229 | zone.start << SECTOR_SHIFT, | |
1230 | rcu_str_deref(device->name), device->devid); | |
784daf2b NA |
1231 | ret = -EIO; |
1232 | goto out; | |
1233 | } | |
1234 | ||
8eae532b NA |
1235 | caps[i] = (zone.capacity << SECTOR_SHIFT); |
1236 | ||
08e11a3d NA |
1237 | switch (zone.cond) { |
1238 | case BLK_ZONE_COND_OFFLINE: | |
1239 | case BLK_ZONE_COND_READONLY: | |
1240 | btrfs_err(fs_info, | |
1241 | "zoned: offline/readonly zone %llu on device %s (devid %llu)", | |
1242 | physical >> device->zone_info->zone_size_shift, | |
1243 | rcu_str_deref(device->name), device->devid); | |
1244 | alloc_offsets[i] = WP_MISSING_DEV; | |
1245 | break; | |
1246 | case BLK_ZONE_COND_EMPTY: | |
1247 | alloc_offsets[i] = 0; | |
1248 | break; | |
1249 | case BLK_ZONE_COND_FULL: | |
8eae532b | 1250 | alloc_offsets[i] = caps[i]; |
08e11a3d NA |
1251 | break; |
1252 | default: | |
1253 | /* Partially used zone */ | |
1254 | alloc_offsets[i] = | |
1255 | ((zone.wp - zone.start) << SECTOR_SHIFT); | |
1256 | break; | |
1257 | } | |
1258 | } | |
1259 | ||
08f45559 JT |
1260 | if (num_sequential > 0) |
1261 | cache->seq_zone = true; | |
1262 | ||
08e11a3d NA |
1263 | if (num_conventional > 0) { |
1264 | /* | |
a94794d5 NA |
1265 | * Avoid calling calculate_alloc_pointer() for new BG. It |
1266 | * is no use for new BG. It must be always 0. | |
1267 | * | |
1268 | * Also, we have a lock chain of extent buffer lock -> | |
1269 | * chunk mutex. For new BG, this function is called from | |
1270 | * btrfs_make_block_group() which is already taking the | |
1271 | * chunk mutex. Thus, we cannot call | |
1272 | * calculate_alloc_pointer() which takes extent buffer | |
1273 | * locks to avoid deadlock. | |
08e11a3d | 1274 | */ |
8eae532b NA |
1275 | |
1276 | /* Zone capacity is always zone size in emulation */ | |
1277 | cache->zone_capacity = cache->length; | |
a94794d5 NA |
1278 | if (new) { |
1279 | cache->alloc_offset = 0; | |
1280 | goto out; | |
1281 | } | |
1282 | ret = calculate_alloc_pointer(cache, &last_alloc); | |
1283 | if (ret || map->num_stripes == num_conventional) { | |
1284 | if (!ret) | |
1285 | cache->alloc_offset = last_alloc; | |
1286 | else | |
1287 | btrfs_err(fs_info, | |
1288 | "zoned: failed to determine allocation offset of bg %llu", | |
1289 | cache->start); | |
1290 | goto out; | |
1291 | } | |
08e11a3d NA |
1292 | } |
1293 | ||
1294 | switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { | |
1295 | case 0: /* single */ | |
06e1e7f4 JT |
1296 | if (alloc_offsets[0] == WP_MISSING_DEV) { |
1297 | btrfs_err(fs_info, | |
1298 | "zoned: cannot recover write pointer for zone %llu", | |
1299 | physical); | |
1300 | ret = -EIO; | |
1301 | goto out; | |
1302 | } | |
08e11a3d | 1303 | cache->alloc_offset = alloc_offsets[0]; |
8eae532b | 1304 | cache->zone_capacity = caps[0]; |
08e11a3d NA |
1305 | break; |
1306 | case BTRFS_BLOCK_GROUP_DUP: | |
1307 | case BTRFS_BLOCK_GROUP_RAID1: | |
1308 | case BTRFS_BLOCK_GROUP_RAID0: | |
1309 | case BTRFS_BLOCK_GROUP_RAID10: | |
1310 | case BTRFS_BLOCK_GROUP_RAID5: | |
1311 | case BTRFS_BLOCK_GROUP_RAID6: | |
1312 | /* non-single profiles are not supported yet */ | |
1313 | default: | |
1314 | btrfs_err(fs_info, "zoned: profile %s not yet supported", | |
1315 | btrfs_bg_type_to_raid_name(map->type)); | |
1316 | ret = -EINVAL; | |
1317 | goto out; | |
1318 | } | |
1319 | ||
1320 | out: | |
06e1e7f4 JT |
1321 | if (cache->alloc_offset > fs_info->zone_size) { |
1322 | btrfs_err(fs_info, | |
1323 | "zoned: invalid write pointer %llu in block group %llu", | |
1324 | cache->alloc_offset, cache->start); | |
1325 | ret = -EIO; | |
1326 | } | |
1327 | ||
8eae532b NA |
1328 | if (cache->alloc_offset > cache->zone_capacity) { |
1329 | btrfs_err(fs_info, | |
1330 | "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu", | |
1331 | cache->alloc_offset, cache->zone_capacity, | |
1332 | cache->start); | |
1333 | ret = -EIO; | |
1334 | } | |
1335 | ||
a94794d5 NA |
1336 | /* An extent is allocated after the write pointer */ |
1337 | if (!ret && num_conventional && last_alloc > cache->alloc_offset) { | |
1338 | btrfs_err(fs_info, | |
1339 | "zoned: got wrong write pointer in BG %llu: %llu > %llu", | |
1340 | logical, last_alloc, cache->alloc_offset); | |
1341 | ret = -EIO; | |
1342 | } | |
1343 | ||
0bc09ca1 NA |
1344 | if (!ret) |
1345 | cache->meta_write_pointer = cache->alloc_offset + cache->start; | |
1346 | ||
8eae532b | 1347 | kfree(caps); |
08e11a3d NA |
1348 | kfree(alloc_offsets); |
1349 | free_extent_map(em); | |
1350 | ||
1351 | return ret; | |
1352 | } | |
169e0da9 NA |
1353 | |
1354 | void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) | |
1355 | { | |
1356 | u64 unusable, free; | |
1357 | ||
1358 | if (!btrfs_is_zoned(cache->fs_info)) | |
1359 | return; | |
1360 | ||
1361 | WARN_ON(cache->bytes_super != 0); | |
98173255 NA |
1362 | unusable = (cache->alloc_offset - cache->used) + |
1363 | (cache->length - cache->zone_capacity); | |
1364 | free = cache->zone_capacity - cache->alloc_offset; | |
169e0da9 NA |
1365 | |
1366 | /* We only need ->free_space in ALLOC_SEQ block groups */ | |
1367 | cache->last_byte_to_unpin = (u64)-1; | |
1368 | cache->cached = BTRFS_CACHE_FINISHED; | |
1369 | cache->free_space_ctl->free_space = free; | |
1370 | cache->zone_unusable = unusable; | |
169e0da9 | 1371 | } |
d3575156 NA |
1372 | |
1373 | void btrfs_redirty_list_add(struct btrfs_transaction *trans, | |
1374 | struct extent_buffer *eb) | |
1375 | { | |
1376 | struct btrfs_fs_info *fs_info = eb->fs_info; | |
1377 | ||
1378 | if (!btrfs_is_zoned(fs_info) || | |
1379 | btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) || | |
1380 | !list_empty(&eb->release_list)) | |
1381 | return; | |
1382 | ||
1383 | set_extent_buffer_dirty(eb); | |
1384 | set_extent_bits_nowait(&trans->dirty_pages, eb->start, | |
1385 | eb->start + eb->len - 1, EXTENT_DIRTY); | |
1386 | memzero_extent_buffer(eb, 0, eb->len); | |
1387 | set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags); | |
1388 | ||
1389 | spin_lock(&trans->releasing_ebs_lock); | |
1390 | list_add_tail(&eb->release_list, &trans->releasing_ebs); | |
1391 | spin_unlock(&trans->releasing_ebs_lock); | |
1392 | atomic_inc(&eb->refs); | |
1393 | } | |
1394 | ||
1395 | void btrfs_free_redirty_list(struct btrfs_transaction *trans) | |
1396 | { | |
1397 | spin_lock(&trans->releasing_ebs_lock); | |
1398 | while (!list_empty(&trans->releasing_ebs)) { | |
1399 | struct extent_buffer *eb; | |
1400 | ||
1401 | eb = list_first_entry(&trans->releasing_ebs, | |
1402 | struct extent_buffer, release_list); | |
1403 | list_del_init(&eb->release_list); | |
1404 | free_extent_buffer(eb); | |
1405 | } | |
1406 | spin_unlock(&trans->releasing_ebs_lock); | |
1407 | } | |
08f45559 | 1408 | |
e380adfc | 1409 | bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start) |
08f45559 JT |
1410 | { |
1411 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
1412 | struct btrfs_block_group *cache; | |
1413 | bool ret = false; | |
1414 | ||
1415 | if (!btrfs_is_zoned(fs_info)) | |
1416 | return false; | |
1417 | ||
08f45559 JT |
1418 | if (!is_data_inode(&inode->vfs_inode)) |
1419 | return false; | |
1420 | ||
e380adfc | 1421 | cache = btrfs_lookup_block_group(fs_info, start); |
08f45559 JT |
1422 | ASSERT(cache); |
1423 | if (!cache) | |
1424 | return false; | |
1425 | ||
1426 | ret = cache->seq_zone; | |
1427 | btrfs_put_block_group(cache); | |
1428 | ||
1429 | return ret; | |
1430 | } | |
d8e3fb10 NA |
1431 | |
1432 | void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset, | |
1433 | struct bio *bio) | |
1434 | { | |
1435 | struct btrfs_ordered_extent *ordered; | |
1436 | const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT; | |
1437 | ||
1438 | if (bio_op(bio) != REQ_OP_ZONE_APPEND) | |
1439 | return; | |
1440 | ||
1441 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset); | |
1442 | if (WARN_ON(!ordered)) | |
1443 | return; | |
1444 | ||
1445 | ordered->physical = physical; | |
c7c3a6dc | 1446 | ordered->bdev = bio->bi_bdev; |
d8e3fb10 NA |
1447 | |
1448 | btrfs_put_ordered_extent(ordered); | |
1449 | } | |
1450 | ||
1451 | void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered) | |
1452 | { | |
1453 | struct btrfs_inode *inode = BTRFS_I(ordered->inode); | |
1454 | struct btrfs_fs_info *fs_info = inode->root->fs_info; | |
1455 | struct extent_map_tree *em_tree; | |
1456 | struct extent_map *em; | |
1457 | struct btrfs_ordered_sum *sum; | |
d8e3fb10 NA |
1458 | u64 orig_logical = ordered->disk_bytenr; |
1459 | u64 *logical = NULL; | |
1460 | int nr, stripe_len; | |
1461 | ||
1462 | /* Zoned devices should not have partitions. So, we can assume it is 0 */ | |
c7c3a6dc CH |
1463 | ASSERT(!bdev_is_partition(ordered->bdev)); |
1464 | if (WARN_ON(!ordered->bdev)) | |
d8e3fb10 NA |
1465 | return; |
1466 | ||
c7c3a6dc | 1467 | if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev, |
d8e3fb10 NA |
1468 | ordered->physical, &logical, &nr, |
1469 | &stripe_len))) | |
1470 | goto out; | |
1471 | ||
1472 | WARN_ON(nr != 1); | |
1473 | ||
1474 | if (orig_logical == *logical) | |
1475 | goto out; | |
1476 | ||
1477 | ordered->disk_bytenr = *logical; | |
1478 | ||
1479 | em_tree = &inode->extent_tree; | |
1480 | write_lock(&em_tree->lock); | |
1481 | em = search_extent_mapping(em_tree, ordered->file_offset, | |
1482 | ordered->num_bytes); | |
1483 | em->block_start = *logical; | |
1484 | free_extent_map(em); | |
1485 | write_unlock(&em_tree->lock); | |
1486 | ||
1487 | list_for_each_entry(sum, &ordered->list, list) { | |
1488 | if (*logical < orig_logical) | |
1489 | sum->bytenr -= orig_logical - *logical; | |
1490 | else | |
1491 | sum->bytenr += *logical - orig_logical; | |
1492 | } | |
1493 | ||
1494 | out: | |
1495 | kfree(logical); | |
d8e3fb10 | 1496 | } |
0bc09ca1 NA |
1497 | |
1498 | bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, | |
1499 | struct extent_buffer *eb, | |
1500 | struct btrfs_block_group **cache_ret) | |
1501 | { | |
1502 | struct btrfs_block_group *cache; | |
1503 | bool ret = true; | |
1504 | ||
1505 | if (!btrfs_is_zoned(fs_info)) | |
1506 | return true; | |
1507 | ||
1508 | cache = *cache_ret; | |
1509 | ||
1510 | if (cache && (eb->start < cache->start || | |
1511 | cache->start + cache->length <= eb->start)) { | |
1512 | btrfs_put_block_group(cache); | |
1513 | cache = NULL; | |
1514 | *cache_ret = NULL; | |
1515 | } | |
1516 | ||
1517 | if (!cache) | |
1518 | cache = btrfs_lookup_block_group(fs_info, eb->start); | |
1519 | ||
1520 | if (cache) { | |
1521 | if (cache->meta_write_pointer != eb->start) { | |
1522 | btrfs_put_block_group(cache); | |
1523 | cache = NULL; | |
1524 | ret = false; | |
1525 | } else { | |
1526 | cache->meta_write_pointer = eb->start + eb->len; | |
1527 | } | |
1528 | ||
1529 | *cache_ret = cache; | |
1530 | } | |
1531 | ||
1532 | return ret; | |
1533 | } | |
1534 | ||
1535 | void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache, | |
1536 | struct extent_buffer *eb) | |
1537 | { | |
1538 | if (!btrfs_is_zoned(eb->fs_info) || !cache) | |
1539 | return; | |
1540 | ||
1541 | ASSERT(cache->meta_write_pointer == eb->start + eb->len); | |
1542 | cache->meta_write_pointer = eb->start; | |
1543 | } | |
de17addc NA |
1544 | |
1545 | int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length) | |
1546 | { | |
1547 | if (!btrfs_dev_is_sequential(device, physical)) | |
1548 | return -EOPNOTSUPP; | |
1549 | ||
1550 | return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT, | |
1551 | length >> SECTOR_SHIFT, GFP_NOFS, 0); | |
1552 | } | |
7db1c5d1 NA |
1553 | |
1554 | static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical, | |
1555 | struct blk_zone *zone) | |
1556 | { | |
1557 | struct btrfs_bio *bbio = NULL; | |
1558 | u64 mapped_length = PAGE_SIZE; | |
1559 | unsigned int nofs_flag; | |
1560 | int nmirrors; | |
1561 | int i, ret; | |
1562 | ||
1563 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical, | |
1564 | &mapped_length, &bbio); | |
1565 | if (ret || !bbio || mapped_length < PAGE_SIZE) { | |
1566 | btrfs_put_bbio(bbio); | |
1567 | return -EIO; | |
1568 | } | |
1569 | ||
1570 | if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
1571 | return -EINVAL; | |
1572 | ||
1573 | nofs_flag = memalloc_nofs_save(); | |
1574 | nmirrors = (int)bbio->num_stripes; | |
1575 | for (i = 0; i < nmirrors; i++) { | |
1576 | u64 physical = bbio->stripes[i].physical; | |
1577 | struct btrfs_device *dev = bbio->stripes[i].dev; | |
1578 | ||
1579 | /* Missing device */ | |
1580 | if (!dev->bdev) | |
1581 | continue; | |
1582 | ||
1583 | ret = btrfs_get_dev_zone(dev, physical, zone); | |
1584 | /* Failing device */ | |
1585 | if (ret == -EIO || ret == -EOPNOTSUPP) | |
1586 | continue; | |
1587 | break; | |
1588 | } | |
1589 | memalloc_nofs_restore(nofs_flag); | |
1590 | ||
1591 | return ret; | |
1592 | } | |
1593 | ||
1594 | /* | |
1595 | * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by | |
1596 | * filling zeros between @physical_pos to a write pointer of dev-replace | |
1597 | * source device. | |
1598 | */ | |
1599 | int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical, | |
1600 | u64 physical_start, u64 physical_pos) | |
1601 | { | |
1602 | struct btrfs_fs_info *fs_info = tgt_dev->fs_info; | |
1603 | struct blk_zone zone; | |
1604 | u64 length; | |
1605 | u64 wp; | |
1606 | int ret; | |
1607 | ||
1608 | if (!btrfs_dev_is_sequential(tgt_dev, physical_pos)) | |
1609 | return 0; | |
1610 | ||
1611 | ret = read_zone_info(fs_info, logical, &zone); | |
1612 | if (ret) | |
1613 | return ret; | |
1614 | ||
1615 | wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT); | |
1616 | ||
1617 | if (physical_pos == wp) | |
1618 | return 0; | |
1619 | ||
1620 | if (physical_pos > wp) | |
1621 | return -EUCLEAN; | |
1622 | ||
1623 | length = wp - physical_pos; | |
1624 | return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length); | |
1625 | } | |
e7ff9e6b JT |
1626 | |
1627 | struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info, | |
1628 | u64 logical, u64 length) | |
1629 | { | |
1630 | struct btrfs_device *device; | |
1631 | struct extent_map *em; | |
1632 | struct map_lookup *map; | |
1633 | ||
1634 | em = btrfs_get_chunk_map(fs_info, logical, length); | |
1635 | if (IS_ERR(em)) | |
1636 | return ERR_CAST(em); | |
1637 | ||
1638 | map = em->map_lookup; | |
1639 | /* We only support single profile for now */ | |
1640 | ASSERT(map->num_stripes == 1); | |
1641 | device = map->stripes[0].dev; | |
1642 | ||
1643 | free_extent_map(em); | |
1644 | ||
1645 | return device; | |
1646 | } |