]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
0b86a832 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
0b86a832 | 4 | */ |
c1d7c514 | 5 | |
0b86a832 | 6 | #include <linux/sched.h> |
fccc0007 | 7 | #include <linux/sched/mm.h> |
5a0e3ad6 | 8 | #include <linux/slab.h> |
442a4f63 | 9 | #include <linux/ratelimit.h> |
59641015 | 10 | #include <linux/kthread.h> |
803b2f54 | 11 | #include <linux/semaphore.h> |
8da4b8c4 | 12 | #include <linux/uuid.h> |
f8e10cd3 | 13 | #include <linux/list_sort.h> |
54fde91f | 14 | #include <linux/namei.h> |
784352fe | 15 | #include "misc.h" |
0b86a832 CM |
16 | #include "ctree.h" |
17 | #include "extent_map.h" | |
18 | #include "disk-io.h" | |
19 | #include "transaction.h" | |
20 | #include "print-tree.h" | |
21 | #include "volumes.h" | |
53b381b3 | 22 | #include "raid56.h" |
606686ee | 23 | #include "rcu-string.h" |
8dabb742 | 24 | #include "dev-replace.h" |
99994cde | 25 | #include "sysfs.h" |
82fc28fb | 26 | #include "tree-checker.h" |
8719aaae | 27 | #include "space-info.h" |
aac0023c | 28 | #include "block-group.h" |
b0643e59 | 29 | #include "discard.h" |
5b316468 | 30 | #include "zoned.h" |
c7f13d42 | 31 | #include "fs.h" |
07e81dc9 | 32 | #include "accessors.h" |
c7a03b52 | 33 | #include "uuid-tree.h" |
7572dec8 | 34 | #include "ioctl.h" |
67707479 | 35 | #include "relocation.h" |
2fc6822c | 36 | #include "scrub.h" |
7f0add25 | 37 | #include "super.h" |
0b86a832 | 38 | |
bf08387f QW |
39 | #define BTRFS_BLOCK_GROUP_STRIPE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \ |
40 | BTRFS_BLOCK_GROUP_RAID10 | \ | |
41 | BTRFS_BLOCK_GROUP_RAID56_MASK) | |
42 | ||
af902047 ZL |
43 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
44 | [BTRFS_RAID_RAID10] = { | |
45 | .sub_stripes = 2, | |
46 | .dev_stripes = 1, | |
47 | .devs_max = 0, /* 0 == as many as possible */ | |
b2f78e88 | 48 | .devs_min = 2, |
8789f4fe | 49 | .tolerated_failures = 1, |
af902047 ZL |
50 | .devs_increment = 2, |
51 | .ncopies = 2, | |
b50836ed | 52 | .nparity = 0, |
ed23467b | 53 | .raid_name = "raid10", |
41a6e891 | 54 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
f9fbcaa2 | 55 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
af902047 ZL |
56 | }, |
57 | [BTRFS_RAID_RAID1] = { | |
58 | .sub_stripes = 1, | |
59 | .dev_stripes = 1, | |
60 | .devs_max = 2, | |
61 | .devs_min = 2, | |
8789f4fe | 62 | .tolerated_failures = 1, |
af902047 ZL |
63 | .devs_increment = 2, |
64 | .ncopies = 2, | |
b50836ed | 65 | .nparity = 0, |
ed23467b | 66 | .raid_name = "raid1", |
41a6e891 | 67 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
f9fbcaa2 | 68 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
af902047 | 69 | }, |
47e6f742 DS |
70 | [BTRFS_RAID_RAID1C3] = { |
71 | .sub_stripes = 1, | |
72 | .dev_stripes = 1, | |
cf93e15e | 73 | .devs_max = 3, |
47e6f742 DS |
74 | .devs_min = 3, |
75 | .tolerated_failures = 2, | |
76 | .devs_increment = 3, | |
77 | .ncopies = 3, | |
db26a024 | 78 | .nparity = 0, |
47e6f742 DS |
79 | .raid_name = "raid1c3", |
80 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3, | |
81 | .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET, | |
82 | }, | |
8d6fac00 DS |
83 | [BTRFS_RAID_RAID1C4] = { |
84 | .sub_stripes = 1, | |
85 | .dev_stripes = 1, | |
cf93e15e | 86 | .devs_max = 4, |
8d6fac00 DS |
87 | .devs_min = 4, |
88 | .tolerated_failures = 3, | |
89 | .devs_increment = 4, | |
90 | .ncopies = 4, | |
db26a024 | 91 | .nparity = 0, |
8d6fac00 DS |
92 | .raid_name = "raid1c4", |
93 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4, | |
94 | .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET, | |
95 | }, | |
af902047 ZL |
96 | [BTRFS_RAID_DUP] = { |
97 | .sub_stripes = 1, | |
98 | .dev_stripes = 2, | |
99 | .devs_max = 1, | |
100 | .devs_min = 1, | |
8789f4fe | 101 | .tolerated_failures = 0, |
af902047 ZL |
102 | .devs_increment = 1, |
103 | .ncopies = 2, | |
b50836ed | 104 | .nparity = 0, |
ed23467b | 105 | .raid_name = "dup", |
41a6e891 | 106 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
f9fbcaa2 | 107 | .mindev_error = 0, |
af902047 ZL |
108 | }, |
109 | [BTRFS_RAID_RAID0] = { | |
110 | .sub_stripes = 1, | |
111 | .dev_stripes = 1, | |
112 | .devs_max = 0, | |
b2f78e88 | 113 | .devs_min = 1, |
8789f4fe | 114 | .tolerated_failures = 0, |
af902047 ZL |
115 | .devs_increment = 1, |
116 | .ncopies = 1, | |
b50836ed | 117 | .nparity = 0, |
ed23467b | 118 | .raid_name = "raid0", |
41a6e891 | 119 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
f9fbcaa2 | 120 | .mindev_error = 0, |
af902047 ZL |
121 | }, |
122 | [BTRFS_RAID_SINGLE] = { | |
123 | .sub_stripes = 1, | |
124 | .dev_stripes = 1, | |
125 | .devs_max = 1, | |
126 | .devs_min = 1, | |
8789f4fe | 127 | .tolerated_failures = 0, |
af902047 ZL |
128 | .devs_increment = 1, |
129 | .ncopies = 1, | |
b50836ed | 130 | .nparity = 0, |
ed23467b | 131 | .raid_name = "single", |
41a6e891 | 132 | .bg_flag = 0, |
f9fbcaa2 | 133 | .mindev_error = 0, |
af902047 ZL |
134 | }, |
135 | [BTRFS_RAID_RAID5] = { | |
136 | .sub_stripes = 1, | |
137 | .dev_stripes = 1, | |
138 | .devs_max = 0, | |
139 | .devs_min = 2, | |
8789f4fe | 140 | .tolerated_failures = 1, |
af902047 | 141 | .devs_increment = 1, |
da612e31 | 142 | .ncopies = 1, |
b50836ed | 143 | .nparity = 1, |
ed23467b | 144 | .raid_name = "raid5", |
41a6e891 | 145 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
f9fbcaa2 | 146 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
af902047 ZL |
147 | }, |
148 | [BTRFS_RAID_RAID6] = { | |
149 | .sub_stripes = 1, | |
150 | .dev_stripes = 1, | |
151 | .devs_max = 0, | |
152 | .devs_min = 3, | |
8789f4fe | 153 | .tolerated_failures = 2, |
af902047 | 154 | .devs_increment = 1, |
da612e31 | 155 | .ncopies = 1, |
b50836ed | 156 | .nparity = 2, |
ed23467b | 157 | .raid_name = "raid6", |
41a6e891 | 158 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
f9fbcaa2 | 159 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
af902047 ZL |
160 | }, |
161 | }; | |
162 | ||
500a44c9 DS |
163 | /* |
164 | * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which | |
165 | * can be used as index to access btrfs_raid_array[]. | |
166 | */ | |
167 | enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags) | |
168 | { | |
719fae89 | 169 | const u64 profile = (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
500a44c9 | 170 | |
719fae89 QW |
171 | if (!profile) |
172 | return BTRFS_RAID_SINGLE; | |
173 | ||
174 | return BTRFS_BG_FLAG_TO_INDEX(profile); | |
500a44c9 DS |
175 | } |
176 | ||
158da513 | 177 | const char *btrfs_bg_type_to_raid_name(u64 flags) |
ed23467b | 178 | { |
158da513 DS |
179 | const int index = btrfs_bg_flags_to_raid_index(flags); |
180 | ||
181 | if (index >= BTRFS_NR_RAID_TYPES) | |
ed23467b AJ |
182 | return NULL; |
183 | ||
158da513 | 184 | return btrfs_raid_array[index].raid_name; |
ed23467b AJ |
185 | } |
186 | ||
0b30f719 QW |
187 | int btrfs_nr_parity_stripes(u64 type) |
188 | { | |
189 | enum btrfs_raid_types index = btrfs_bg_flags_to_raid_index(type); | |
190 | ||
191 | return btrfs_raid_array[index].nparity; | |
192 | } | |
193 | ||
f89e09cf AJ |
194 | /* |
195 | * Fill @buf with textual description of @bg_flags, no more than @size_buf | |
196 | * bytes including terminating null byte. | |
197 | */ | |
198 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) | |
199 | { | |
200 | int i; | |
201 | int ret; | |
202 | char *bp = buf; | |
203 | u64 flags = bg_flags; | |
204 | u32 size_bp = size_buf; | |
205 | ||
206 | if (!flags) { | |
207 | strcpy(bp, "NONE"); | |
208 | return; | |
209 | } | |
210 | ||
211 | #define DESCRIBE_FLAG(flag, desc) \ | |
212 | do { \ | |
213 | if (flags & (flag)) { \ | |
214 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ | |
215 | if (ret < 0 || ret >= size_bp) \ | |
216 | goto out_overflow; \ | |
217 | size_bp -= ret; \ | |
218 | bp += ret; \ | |
219 | flags &= ~(flag); \ | |
220 | } \ | |
221 | } while (0) | |
222 | ||
223 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); | |
224 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); | |
225 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); | |
226 | ||
227 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); | |
228 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) | |
229 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, | |
230 | btrfs_raid_array[i].raid_name); | |
231 | #undef DESCRIBE_FLAG | |
232 | ||
233 | if (flags) { | |
234 | ret = snprintf(bp, size_bp, "0x%llx|", flags); | |
235 | size_bp -= ret; | |
236 | } | |
237 | ||
238 | if (size_bp < size_buf) | |
239 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ | |
240 | ||
241 | /* | |
242 | * The text is trimmed, it's up to the caller to provide sufficiently | |
243 | * large buffer | |
244 | */ | |
245 | out_overflow:; | |
246 | } | |
247 | ||
6f8e0fc7 | 248 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
2ff7e61e | 249 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
733f4fbb | 250 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
2b82032c | 251 | |
9c6b1c4d DS |
252 | /* |
253 | * Device locking | |
254 | * ============== | |
255 | * | |
256 | * There are several mutexes that protect manipulation of devices and low-level | |
257 | * structures like chunks but not block groups, extents or files | |
258 | * | |
259 | * uuid_mutex (global lock) | |
260 | * ------------------------ | |
261 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from | |
262 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first | |
263 | * device) or requested by the device= mount option | |
264 | * | |
265 | * the mutex can be very coarse and can cover long-running operations | |
266 | * | |
267 | * protects: updates to fs_devices counters like missing devices, rw devices, | |
52042d8e | 268 | * seeding, structure cloning, opening/closing devices at mount/umount time |
9c6b1c4d DS |
269 | * |
270 | * global::fs_devs - add, remove, updates to the global list | |
271 | * | |
18c850fd JB |
272 | * does not protect: manipulation of the fs_devices::devices list in general |
273 | * but in mount context it could be used to exclude list modifications by eg. | |
274 | * scan ioctl | |
9c6b1c4d DS |
275 | * |
276 | * btrfs_device::name - renames (write side), read is RCU | |
277 | * | |
278 | * fs_devices::device_list_mutex (per-fs, with RCU) | |
279 | * ------------------------------------------------ | |
280 | * protects updates to fs_devices::devices, ie. adding and deleting | |
281 | * | |
282 | * simple list traversal with read-only actions can be done with RCU protection | |
283 | * | |
284 | * may be used to exclude some operations from running concurrently without any | |
285 | * modifications to the list (see write_all_supers) | |
286 | * | |
18c850fd JB |
287 | * Is not required at mount and close times, because our device list is |
288 | * protected by the uuid_mutex at that point. | |
289 | * | |
9c6b1c4d DS |
290 | * balance_mutex |
291 | * ------------- | |
292 | * protects balance structures (status, state) and context accessed from | |
293 | * several places (internally, ioctl) | |
294 | * | |
295 | * chunk_mutex | |
296 | * ----------- | |
297 | * protects chunks, adding or removing during allocation, trim or when a new | |
0b6f5d40 NB |
298 | * device is added/removed. Additionally it also protects post_commit_list of |
299 | * individual devices, since they can be added to the transaction's | |
300 | * post_commit_list only with chunk_mutex held. | |
9c6b1c4d DS |
301 | * |
302 | * cleaner_mutex | |
303 | * ------------- | |
304 | * a big lock that is held by the cleaner thread and prevents running subvolume | |
305 | * cleaning together with relocation or delayed iputs | |
306 | * | |
307 | * | |
308 | * Lock nesting | |
309 | * ============ | |
310 | * | |
311 | * uuid_mutex | |
ae3e715f AJ |
312 | * device_list_mutex |
313 | * chunk_mutex | |
314 | * balance_mutex | |
89595e80 AJ |
315 | * |
316 | * | |
c3e1f96c GR |
317 | * Exclusive operations |
318 | * ==================== | |
89595e80 AJ |
319 | * |
320 | * Maintains the exclusivity of the following operations that apply to the | |
321 | * whole filesystem and cannot run in parallel. | |
322 | * | |
323 | * - Balance (*) | |
324 | * - Device add | |
325 | * - Device remove | |
326 | * - Device replace (*) | |
327 | * - Resize | |
328 | * | |
329 | * The device operations (as above) can be in one of the following states: | |
330 | * | |
331 | * - Running state | |
332 | * - Paused state | |
333 | * - Completed state | |
334 | * | |
335 | * Only device operations marked with (*) can go into the Paused state for the | |
336 | * following reasons: | |
337 | * | |
338 | * - ioctl (only Balance can be Paused through ioctl) | |
339 | * - filesystem remounted as read-only | |
340 | * - filesystem unmounted and mounted as read-only | |
341 | * - system power-cycle and filesystem mounted as read-only | |
342 | * - filesystem or device errors leading to forced read-only | |
343 | * | |
c3e1f96c GR |
344 | * The status of exclusive operation is set and cleared atomically. |
345 | * During the course of Paused state, fs_info::exclusive_operation remains set. | |
89595e80 AJ |
346 | * A device operation in Paused or Running state can be canceled or resumed |
347 | * either by ioctl (Balance only) or when remounted as read-write. | |
c3e1f96c | 348 | * The exclusive status is cleared when the device operation is canceled or |
89595e80 | 349 | * completed. |
9c6b1c4d DS |
350 | */ |
351 | ||
67a2c45e | 352 | DEFINE_MUTEX(uuid_mutex); |
8a4b83cc | 353 | static LIST_HEAD(fs_uuids); |
4143cb8b | 354 | struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
c73eccf7 AJ |
355 | { |
356 | return &fs_uuids; | |
357 | } | |
8a4b83cc | 358 | |
2dfeca9b | 359 | /* |
f7361d8c AJ |
360 | * Allocate new btrfs_fs_devices structure identified by a fsid. |
361 | * | |
362 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid and to | |
363 | * fs_devices::metadata_fsid | |
2dfeca9b DS |
364 | * |
365 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). | |
366 | * The returned struct is not linked onto any lists and can be destroyed with | |
367 | * kfree() right away. | |
368 | */ | |
f7361d8c | 369 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
2208a378 ID |
370 | { |
371 | struct btrfs_fs_devices *fs_devs; | |
372 | ||
78f2c9e6 | 373 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
2208a378 ID |
374 | if (!fs_devs) |
375 | return ERR_PTR(-ENOMEM); | |
376 | ||
377 | mutex_init(&fs_devs->device_list_mutex); | |
378 | ||
379 | INIT_LIST_HEAD(&fs_devs->devices); | |
380 | INIT_LIST_HEAD(&fs_devs->alloc_list); | |
c4babc5e | 381 | INIT_LIST_HEAD(&fs_devs->fs_list); |
944d3f9f | 382 | INIT_LIST_HEAD(&fs_devs->seed_list); |
2208a378 | 383 | |
19c4c49c AJ |
384 | if (fsid) { |
385 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); | |
f7361d8c | 386 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
19c4c49c | 387 | } |
7239ff4b | 388 | |
2208a378 ID |
389 | return fs_devs; |
390 | } | |
391 | ||
f2db4d5c | 392 | static void btrfs_free_device(struct btrfs_device *device) |
48dae9cf | 393 | { |
bbbf7243 | 394 | WARN_ON(!list_empty(&device->post_commit_list)); |
48dae9cf | 395 | rcu_string_free(device->name); |
611ccc58 | 396 | extent_io_tree_release(&device->alloc_state); |
5b316468 | 397 | btrfs_destroy_dev_zone_info(device); |
48dae9cf DS |
398 | kfree(device); |
399 | } | |
400 | ||
e4404d6e YZ |
401 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
402 | { | |
403 | struct btrfs_device *device; | |
5f58d783 | 404 | |
e4404d6e YZ |
405 | WARN_ON(fs_devices->opened); |
406 | while (!list_empty(&fs_devices->devices)) { | |
407 | device = list_entry(fs_devices->devices.next, | |
408 | struct btrfs_device, dev_list); | |
409 | list_del(&device->dev_list); | |
a425f9d4 | 410 | btrfs_free_device(device); |
e4404d6e YZ |
411 | } |
412 | kfree(fs_devices); | |
413 | } | |
414 | ||
ffc5a379 | 415 | void __exit btrfs_cleanup_fs_uuids(void) |
8a4b83cc CM |
416 | { |
417 | struct btrfs_fs_devices *fs_devices; | |
8a4b83cc | 418 | |
2b82032c YZ |
419 | while (!list_empty(&fs_uuids)) { |
420 | fs_devices = list_entry(fs_uuids.next, | |
c4babc5e AJ |
421 | struct btrfs_fs_devices, fs_list); |
422 | list_del(&fs_devices->fs_list); | |
e4404d6e | 423 | free_fs_devices(fs_devices); |
8a4b83cc | 424 | } |
8a4b83cc CM |
425 | } |
426 | ||
1a898345 AJ |
427 | static bool match_fsid_fs_devices(const struct btrfs_fs_devices *fs_devices, |
428 | const u8 *fsid, const u8 *metadata_fsid) | |
429 | { | |
430 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) != 0) | |
431 | return false; | |
432 | ||
433 | if (!metadata_fsid) | |
434 | return true; | |
435 | ||
436 | if (memcmp(metadata_fsid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE) != 0) | |
437 | return false; | |
438 | ||
439 | return true; | |
440 | } | |
441 | ||
7239ff4b NB |
442 | static noinline struct btrfs_fs_devices *find_fsid( |
443 | const u8 *fsid, const u8 *metadata_fsid) | |
8a4b83cc | 444 | { |
8a4b83cc CM |
445 | struct btrfs_fs_devices *fs_devices; |
446 | ||
7239ff4b NB |
447 | ASSERT(fsid); |
448 | ||
7a62d0f0 | 449 | /* Handle non-split brain cases */ |
c4babc5e | 450 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
1a898345 AJ |
451 | if (match_fsid_fs_devices(fs_devices, fsid, metadata_fsid)) |
452 | return fs_devices; | |
8a4b83cc CM |
453 | } |
454 | return NULL; | |
455 | } | |
456 | ||
a3c54b0b AJ |
457 | /* |
458 | * First check if the metadata_uuid is different from the fsid in the given | |
459 | * fs_devices. Then check if the given fsid is the same as the metadata_uuid | |
460 | * in the fs_devices. If it is, return true; otherwise, return false. | |
461 | */ | |
462 | static inline bool check_fsid_changed(const struct btrfs_fs_devices *fs_devices, | |
463 | const u8 *fsid) | |
464 | { | |
465 | return memcmp(fs_devices->fsid, fs_devices->metadata_uuid, | |
466 | BTRFS_FSID_SIZE) != 0 && | |
467 | memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE) == 0; | |
468 | } | |
469 | ||
c6730a0e SY |
470 | static struct btrfs_fs_devices *find_fsid_with_metadata_uuid( |
471 | struct btrfs_super_block *disk_super) | |
472 | { | |
473 | ||
474 | struct btrfs_fs_devices *fs_devices; | |
475 | ||
476 | /* | |
477 | * Handle scanned device having completed its fsid change but | |
478 | * belonging to a fs_devices that was created by first scanning | |
479 | * a device which didn't have its fsid/metadata_uuid changed | |
480 | * at all and the CHANGING_FSID_V2 flag set. | |
481 | */ | |
482 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { | |
1a898345 AJ |
483 | if (!fs_devices->fsid_change) |
484 | continue; | |
485 | ||
486 | if (match_fsid_fs_devices(fs_devices, disk_super->metadata_uuid, | |
487 | fs_devices->fsid)) | |
c6730a0e | 488 | return fs_devices; |
c6730a0e | 489 | } |
1a898345 | 490 | |
c6730a0e SY |
491 | /* |
492 | * Handle scanned device having completed its fsid change but | |
493 | * belonging to a fs_devices that was created by a device that | |
494 | * has an outdated pair of fsid/metadata_uuid and | |
495 | * CHANGING_FSID_V2 flag set. | |
496 | */ | |
497 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { | |
a3c54b0b AJ |
498 | if (!fs_devices->fsid_change) |
499 | continue; | |
500 | ||
501 | if (check_fsid_changed(fs_devices, disk_super->metadata_uuid)) | |
c6730a0e | 502 | return fs_devices; |
c6730a0e SY |
503 | } |
504 | ||
505 | return find_fsid(disk_super->fsid, disk_super->metadata_uuid); | |
506 | } | |
507 | ||
508 | ||
beaf8ab3 | 509 | static int |
05bdb996 | 510 | btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder, |
beaf8ab3 | 511 | int flush, struct block_device **bdev, |
8f32380d | 512 | struct btrfs_super_block **disk_super) |
beaf8ab3 SB |
513 | { |
514 | int ret; | |
515 | ||
0718afd4 | 516 | *bdev = blkdev_get_by_path(device_path, flags, holder, NULL); |
beaf8ab3 SB |
517 | |
518 | if (IS_ERR(*bdev)) { | |
519 | ret = PTR_ERR(*bdev); | |
beaf8ab3 SB |
520 | goto error; |
521 | } | |
522 | ||
523 | if (flush) | |
1226dfff | 524 | sync_blockdev(*bdev); |
9f6d2510 | 525 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
beaf8ab3 | 526 | if (ret) { |
2736e8ee | 527 | blkdev_put(*bdev, holder); |
beaf8ab3 SB |
528 | goto error; |
529 | } | |
530 | invalidate_bdev(*bdev); | |
8f32380d JT |
531 | *disk_super = btrfs_read_dev_super(*bdev); |
532 | if (IS_ERR(*disk_super)) { | |
533 | ret = PTR_ERR(*disk_super); | |
2736e8ee | 534 | blkdev_put(*bdev, holder); |
beaf8ab3 SB |
535 | goto error; |
536 | } | |
537 | ||
538 | return 0; | |
539 | ||
540 | error: | |
541 | *bdev = NULL; | |
beaf8ab3 SB |
542 | return ret; |
543 | } | |
544 | ||
43dd529a DS |
545 | /* |
546 | * Search and remove all stale devices (which are not mounted). When both | |
547 | * inputs are NULL, it will search and release all stale devices. | |
16cab91a | 548 | * |
43dd529a DS |
549 | * @devt: Optional. When provided will it release all unmounted devices |
550 | * matching this devt only. | |
16cab91a | 551 | * @skip_device: Optional. Will skip this device when searching for the stale |
43dd529a | 552 | * devices. |
16cab91a AJ |
553 | * |
554 | * Return: 0 for success or if @devt is 0. | |
555 | * -EBUSY if @devt is a mounted device. | |
556 | * -ENOENT if @devt does not match any device in the list. | |
d8367db3 | 557 | */ |
16cab91a | 558 | static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device) |
4fde46f0 | 559 | { |
fa6d2ae5 AJ |
560 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
561 | struct btrfs_device *device, *tmp_device; | |
bc27d6f0 AJ |
562 | int ret; |
563 | bool freed = false; | |
70bc7088 | 564 | |
c1247069 AJ |
565 | lockdep_assert_held(&uuid_mutex); |
566 | ||
bc27d6f0 AJ |
567 | /* Return good status if there is no instance of devt. */ |
568 | ret = 0; | |
fa6d2ae5 | 569 | list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { |
4fde46f0 | 570 | |
70bc7088 | 571 | mutex_lock(&fs_devices->device_list_mutex); |
fa6d2ae5 AJ |
572 | list_for_each_entry_safe(device, tmp_device, |
573 | &fs_devices->devices, dev_list) { | |
fa6d2ae5 | 574 | if (skip_device && skip_device == device) |
d8367db3 | 575 | continue; |
330a5bf4 | 576 | if (devt && devt != device->devt) |
38cf665d | 577 | continue; |
70bc7088 | 578 | if (fs_devices->opened) { |
bc27d6f0 | 579 | if (devt) |
70bc7088 AJ |
580 | ret = -EBUSY; |
581 | break; | |
582 | } | |
4fde46f0 | 583 | |
4fde46f0 | 584 | /* delete the stale device */ |
7bcb8164 AJ |
585 | fs_devices->num_devices--; |
586 | list_del(&device->dev_list); | |
587 | btrfs_free_device(device); | |
588 | ||
bc27d6f0 | 589 | freed = true; |
7bcb8164 AJ |
590 | } |
591 | mutex_unlock(&fs_devices->device_list_mutex); | |
70bc7088 | 592 | |
7bcb8164 AJ |
593 | if (fs_devices->num_devices == 0) { |
594 | btrfs_sysfs_remove_fsid(fs_devices); | |
595 | list_del(&fs_devices->fs_list); | |
596 | free_fs_devices(fs_devices); | |
4fde46f0 AJ |
597 | } |
598 | } | |
70bc7088 | 599 | |
bc27d6f0 AJ |
600 | /* If there is at least one freed device return 0. */ |
601 | if (freed) | |
602 | return 0; | |
603 | ||
70bc7088 | 604 | return ret; |
4fde46f0 AJ |
605 | } |
606 | ||
18c850fd JB |
607 | /* |
608 | * This is only used on mount, and we are protected from competing things | |
609 | * messing with our fs_devices by the uuid_mutex, thus we do not need the | |
610 | * fs_devices->device_list_mutex here. | |
611 | */ | |
0fb08bcc | 612 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
05bdb996 | 613 | struct btrfs_device *device, blk_mode_t flags, |
0fb08bcc AJ |
614 | void *holder) |
615 | { | |
0fb08bcc | 616 | struct block_device *bdev; |
0fb08bcc AJ |
617 | struct btrfs_super_block *disk_super; |
618 | u64 devid; | |
619 | int ret; | |
620 | ||
621 | if (device->bdev) | |
622 | return -EINVAL; | |
623 | if (!device->name) | |
624 | return -EINVAL; | |
625 | ||
626 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, | |
8f32380d | 627 | &bdev, &disk_super); |
0fb08bcc AJ |
628 | if (ret) |
629 | return ret; | |
630 | ||
0fb08bcc AJ |
631 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
632 | if (devid != device->devid) | |
8f32380d | 633 | goto error_free_page; |
0fb08bcc AJ |
634 | |
635 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) | |
8f32380d | 636 | goto error_free_page; |
0fb08bcc AJ |
637 | |
638 | device->generation = btrfs_super_generation(disk_super); | |
639 | ||
640 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { | |
7239ff4b NB |
641 | if (btrfs_super_incompat_flags(disk_super) & |
642 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { | |
643 | pr_err( | |
644 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); | |
8f32380d | 645 | goto error_free_page; |
7239ff4b NB |
646 | } |
647 | ||
ebbede42 | 648 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
0395d84f | 649 | fs_devices->seeding = true; |
0fb08bcc | 650 | } else { |
ebbede42 AJ |
651 | if (bdev_read_only(bdev)) |
652 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); | |
653 | else | |
654 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); | |
0fb08bcc AJ |
655 | } |
656 | ||
10f0d2a5 | 657 | if (!bdev_nonrot(bdev)) |
7f0432d0 | 658 | fs_devices->rotating = true; |
0fb08bcc | 659 | |
63a7cb13 DS |
660 | if (bdev_max_discard_sectors(bdev)) |
661 | fs_devices->discardable = true; | |
662 | ||
0fb08bcc | 663 | device->bdev = bdev; |
e12c9621 | 664 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
2736e8ee | 665 | device->holder = holder; |
0fb08bcc AJ |
666 | |
667 | fs_devices->open_devices++; | |
ebbede42 AJ |
668 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
669 | device->devid != BTRFS_DEV_REPLACE_DEVID) { | |
0fb08bcc | 670 | fs_devices->rw_devices++; |
b1b8e386 | 671 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
0fb08bcc | 672 | } |
8f32380d | 673 | btrfs_release_disk_super(disk_super); |
0fb08bcc AJ |
674 | |
675 | return 0; | |
676 | ||
8f32380d JT |
677 | error_free_page: |
678 | btrfs_release_disk_super(disk_super); | |
2736e8ee | 679 | blkdev_put(bdev, holder); |
0fb08bcc AJ |
680 | |
681 | return -EINVAL; | |
682 | } | |
683 | ||
4844c366 AJ |
684 | u8 *btrfs_sb_fsid_ptr(struct btrfs_super_block *sb) |
685 | { | |
686 | bool has_metadata_uuid = (btrfs_super_incompat_flags(sb) & | |
687 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); | |
688 | ||
689 | return has_metadata_uuid ? sb->metadata_uuid : sb->fsid; | |
690 | } | |
691 | ||
7a62d0f0 NB |
692 | /* |
693 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices | |
c0d81c7c SY |
694 | * being created with a disk that has already completed its fsid change. Such |
695 | * disk can belong to an fs which has its FSID changed or to one which doesn't. | |
696 | * Handle both cases here. | |
7a62d0f0 NB |
697 | */ |
698 | static struct btrfs_fs_devices *find_fsid_inprogress( | |
699 | struct btrfs_super_block *disk_super) | |
700 | { | |
701 | struct btrfs_fs_devices *fs_devices; | |
702 | ||
703 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { | |
a3c54b0b AJ |
704 | if (fs_devices->fsid_change) |
705 | continue; | |
706 | ||
707 | if (check_fsid_changed(fs_devices, disk_super->fsid)) | |
7a62d0f0 | 708 | return fs_devices; |
7a62d0f0 NB |
709 | } |
710 | ||
c0d81c7c | 711 | return find_fsid(disk_super->fsid, NULL); |
7a62d0f0 NB |
712 | } |
713 | ||
cc5de4e7 NB |
714 | static struct btrfs_fs_devices *find_fsid_changed( |
715 | struct btrfs_super_block *disk_super) | |
716 | { | |
717 | struct btrfs_fs_devices *fs_devices; | |
718 | ||
719 | /* | |
720 | * Handles the case where scanned device is part of an fs that had | |
1a9fd417 | 721 | * multiple successful changes of FSID but currently device didn't |
05840710 NB |
722 | * observe it. Meaning our fsid will be different than theirs. We need |
723 | * to handle two subcases : | |
724 | * 1 - The fs still continues to have different METADATA/FSID uuids. | |
725 | * 2 - The fs is switched back to its original FSID (METADATA/FSID | |
726 | * are equal). | |
cc5de4e7 NB |
727 | */ |
728 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { | |
05840710 | 729 | /* Changed UUIDs */ |
a3c54b0b | 730 | if (check_fsid_changed(fs_devices, disk_super->metadata_uuid) && |
cc5de4e7 | 731 | memcmp(fs_devices->fsid, disk_super->fsid, |
05840710 NB |
732 | BTRFS_FSID_SIZE) != 0) |
733 | return fs_devices; | |
734 | ||
735 | /* Unchanged UUIDs */ | |
736 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, | |
737 | BTRFS_FSID_SIZE) == 0 && | |
738 | memcmp(fs_devices->fsid, disk_super->metadata_uuid, | |
739 | BTRFS_FSID_SIZE) == 0) | |
cc5de4e7 | 740 | return fs_devices; |
cc5de4e7 NB |
741 | } |
742 | ||
743 | return NULL; | |
744 | } | |
1362089d NB |
745 | |
746 | static struct btrfs_fs_devices *find_fsid_reverted_metadata( | |
747 | struct btrfs_super_block *disk_super) | |
748 | { | |
749 | struct btrfs_fs_devices *fs_devices; | |
750 | ||
751 | /* | |
752 | * Handle the case where the scanned device is part of an fs whose last | |
753 | * metadata UUID change reverted it to the original FSID. At the same | |
67da05b3 | 754 | * time fs_devices was first created by another constituent device |
1362089d NB |
755 | * which didn't fully observe the operation. This results in an |
756 | * btrfs_fs_devices created with metadata/fsid different AND | |
757 | * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the | |
758 | * fs_devices equal to the FSID of the disk. | |
759 | */ | |
760 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { | |
a3c54b0b AJ |
761 | if (!fs_devices->fsid_change) |
762 | continue; | |
763 | ||
764 | if (check_fsid_changed(fs_devices, disk_super->fsid)) | |
1362089d NB |
765 | return fs_devices; |
766 | } | |
767 | ||
768 | return NULL; | |
769 | } | |
60999ca4 DS |
770 | /* |
771 | * Add new device to list of registered devices | |
772 | * | |
773 | * Returns: | |
e124ece5 AJ |
774 | * device pointer which was just added or updated when successful |
775 | * error pointer when failed | |
60999ca4 | 776 | */ |
e124ece5 | 777 | static noinline struct btrfs_device *device_list_add(const char *path, |
4306a974 AJ |
778 | struct btrfs_super_block *disk_super, |
779 | bool *new_device_added) | |
8a4b83cc CM |
780 | { |
781 | struct btrfs_device *device; | |
7a62d0f0 | 782 | struct btrfs_fs_devices *fs_devices = NULL; |
606686ee | 783 | struct rcu_string *name; |
8a4b83cc | 784 | u64 found_transid = btrfs_super_generation(disk_super); |
3acbcbfc | 785 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
4889bc05 AJ |
786 | dev_t path_devt; |
787 | int error; | |
7239ff4b NB |
788 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
789 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); | |
d1a63002 NB |
790 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
791 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); | |
7239ff4b | 792 | |
4889bc05 | 793 | error = lookup_bdev(path, &path_devt); |
ed02363f QW |
794 | if (error) { |
795 | btrfs_err(NULL, "failed to lookup block device for path %s: %d", | |
796 | path, error); | |
4889bc05 | 797 | return ERR_PTR(error); |
ed02363f | 798 | } |
4889bc05 | 799 | |
cc5de4e7 | 800 | if (fsid_change_in_progress) { |
c0d81c7c | 801 | if (!has_metadata_uuid) |
cc5de4e7 | 802 | fs_devices = find_fsid_inprogress(disk_super); |
c0d81c7c | 803 | else |
cc5de4e7 | 804 | fs_devices = find_fsid_changed(disk_super); |
7a62d0f0 | 805 | } else if (has_metadata_uuid) { |
c6730a0e | 806 | fs_devices = find_fsid_with_metadata_uuid(disk_super); |
7a62d0f0 | 807 | } else { |
1362089d NB |
808 | fs_devices = find_fsid_reverted_metadata(disk_super); |
809 | if (!fs_devices) | |
810 | fs_devices = find_fsid(disk_super->fsid, NULL); | |
7a62d0f0 NB |
811 | } |
812 | ||
8a4b83cc | 813 | |
8a4b83cc | 814 | if (!fs_devices) { |
f7361d8c AJ |
815 | fs_devices = alloc_fs_devices(disk_super->fsid); |
816 | if (has_metadata_uuid) | |
817 | memcpy(fs_devices->metadata_uuid, | |
818 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); | |
819 | ||
2208a378 | 820 | if (IS_ERR(fs_devices)) |
e124ece5 | 821 | return ERR_CAST(fs_devices); |
2208a378 | 822 | |
92900e51 AV |
823 | fs_devices->fsid_change = fsid_change_in_progress; |
824 | ||
9c6d173e | 825 | mutex_lock(&fs_devices->device_list_mutex); |
c4babc5e | 826 | list_add(&fs_devices->fs_list, &fs_uuids); |
2208a378 | 827 | |
8a4b83cc CM |
828 | device = NULL; |
829 | } else { | |
562d7b15 JB |
830 | struct btrfs_dev_lookup_args args = { |
831 | .devid = devid, | |
832 | .uuid = disk_super->dev_item.uuid, | |
833 | }; | |
834 | ||
9c6d173e | 835 | mutex_lock(&fs_devices->device_list_mutex); |
562d7b15 | 836 | device = btrfs_find_device(fs_devices, &args); |
7a62d0f0 NB |
837 | |
838 | /* | |
839 | * If this disk has been pulled into an fs devices created by | |
840 | * a device which had the CHANGING_FSID_V2 flag then replace the | |
841 | * metadata_uuid/fsid values of the fs_devices. | |
842 | */ | |
1362089d | 843 | if (fs_devices->fsid_change && |
7a62d0f0 NB |
844 | found_transid > fs_devices->latest_generation) { |
845 | memcpy(fs_devices->fsid, disk_super->fsid, | |
846 | BTRFS_FSID_SIZE); | |
319baafc AJ |
847 | memcpy(fs_devices->metadata_uuid, |
848 | btrfs_sb_fsid_ptr(disk_super), BTRFS_FSID_SIZE); | |
7a62d0f0 NB |
849 | fs_devices->fsid_change = false; |
850 | } | |
8a4b83cc | 851 | } |
443f24fe | 852 | |
8a4b83cc | 853 | if (!device) { |
bb21e302 AJ |
854 | unsigned int nofs_flag; |
855 | ||
9c6d173e | 856 | if (fs_devices->opened) { |
ed02363f | 857 | btrfs_err(NULL, |
7f9879eb AJ |
858 | "device %s belongs to fsid %pU, and the fs is already mounted, scanned by %s (%d)", |
859 | path, fs_devices->fsid, current->comm, | |
860 | task_pid_nr(current)); | |
9c6d173e | 861 | mutex_unlock(&fs_devices->device_list_mutex); |
e124ece5 | 862 | return ERR_PTR(-EBUSY); |
9c6d173e | 863 | } |
2b82032c | 864 | |
bb21e302 | 865 | nofs_flag = memalloc_nofs_save(); |
12bd2fc0 | 866 | device = btrfs_alloc_device(NULL, &devid, |
bb21e302 AJ |
867 | disk_super->dev_item.uuid, path); |
868 | memalloc_nofs_restore(nofs_flag); | |
12bd2fc0 | 869 | if (IS_ERR(device)) { |
9c6d173e | 870 | mutex_unlock(&fs_devices->device_list_mutex); |
8a4b83cc | 871 | /* we can safely leave the fs_devices entry around */ |
e124ece5 | 872 | return device; |
8a4b83cc | 873 | } |
606686ee | 874 | |
4889bc05 | 875 | device->devt = path_devt; |
90519d66 | 876 | |
1f78160c | 877 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
f7171750 | 878 | fs_devices->num_devices++; |
e5e9a520 | 879 | |
2b82032c | 880 | device->fs_devices = fs_devices; |
4306a974 | 881 | *new_device_added = true; |
327f18cc AJ |
882 | |
883 | if (disk_super->label[0]) | |
aa6c0df7 AJ |
884 | pr_info( |
885 | "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", | |
886 | disk_super->label, devid, found_transid, path, | |
887 | current->comm, task_pid_nr(current)); | |
327f18cc | 888 | else |
aa6c0df7 AJ |
889 | pr_info( |
890 | "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", | |
891 | disk_super->fsid, devid, found_transid, path, | |
892 | current->comm, task_pid_nr(current)); | |
327f18cc | 893 | |
606686ee | 894 | } else if (!device->name || strcmp(device->name->str, path)) { |
b96de000 AJ |
895 | /* |
896 | * When FS is already mounted. | |
897 | * 1. If you are here and if the device->name is NULL that | |
898 | * means this device was missing at time of FS mount. | |
899 | * 2. If you are here and if the device->name is different | |
900 | * from 'path' that means either | |
901 | * a. The same device disappeared and reappeared with | |
902 | * different name. or | |
903 | * b. The missing-disk-which-was-replaced, has | |
904 | * reappeared now. | |
905 | * | |
906 | * We must allow 1 and 2a above. But 2b would be a spurious | |
907 | * and unintentional. | |
908 | * | |
909 | * Further in case of 1 and 2a above, the disk at 'path' | |
910 | * would have missed some transaction when it was away and | |
911 | * in case of 2a the stale bdev has to be updated as well. | |
912 | * 2b must not be allowed at all time. | |
913 | */ | |
914 | ||
915 | /* | |
0f23ae74 CM |
916 | * For now, we do allow update to btrfs_fs_device through the |
917 | * btrfs dev scan cli after FS has been mounted. We're still | |
918 | * tracking a problem where systems fail mount by subvolume id | |
919 | * when we reject replacement on a mounted FS. | |
b96de000 | 920 | */ |
0f23ae74 | 921 | if (!fs_devices->opened && found_transid < device->generation) { |
77bdae4d AJ |
922 | /* |
923 | * That is if the FS is _not_ mounted and if you | |
924 | * are here, that means there is more than one | |
925 | * disk with same uuid and devid.We keep the one | |
926 | * with larger generation number or the last-in if | |
927 | * generation are equal. | |
928 | */ | |
9c6d173e | 929 | mutex_unlock(&fs_devices->device_list_mutex); |
ed02363f QW |
930 | btrfs_err(NULL, |
931 | "device %s already registered with a higher generation, found %llu expect %llu", | |
932 | path, found_transid, device->generation); | |
e124ece5 | 933 | return ERR_PTR(-EEXIST); |
77bdae4d | 934 | } |
b96de000 | 935 | |
a9261d41 AJ |
936 | /* |
937 | * We are going to replace the device path for a given devid, | |
938 | * make sure it's the same device if the device is mounted | |
79c9234b DM |
939 | * |
940 | * NOTE: the device->fs_info may not be reliable here so pass | |
941 | * in a NULL to message helpers instead. This avoids a possible | |
942 | * use-after-free when the fs_info and fs_info->sb are already | |
943 | * torn down. | |
a9261d41 AJ |
944 | */ |
945 | if (device->bdev) { | |
4889bc05 | 946 | if (device->devt != path_devt) { |
a9261d41 | 947 | mutex_unlock(&fs_devices->device_list_mutex); |
0697d9a6 | 948 | btrfs_warn_in_rcu(NULL, |
79dae17d AJ |
949 | "duplicate device %s devid %llu generation %llu scanned by %s (%d)", |
950 | path, devid, found_transid, | |
951 | current->comm, | |
952 | task_pid_nr(current)); | |
a9261d41 AJ |
953 | return ERR_PTR(-EEXIST); |
954 | } | |
79c9234b | 955 | btrfs_info_in_rcu(NULL, |
79dae17d | 956 | "devid %llu device path %s changed to %s scanned by %s (%d)", |
cb3e217b | 957 | devid, btrfs_dev_name(device), |
79dae17d AJ |
958 | path, current->comm, |
959 | task_pid_nr(current)); | |
a9261d41 AJ |
960 | } |
961 | ||
606686ee | 962 | name = rcu_string_strdup(path, GFP_NOFS); |
9c6d173e AJ |
963 | if (!name) { |
964 | mutex_unlock(&fs_devices->device_list_mutex); | |
e124ece5 | 965 | return ERR_PTR(-ENOMEM); |
9c6d173e | 966 | } |
606686ee JB |
967 | rcu_string_free(device->name); |
968 | rcu_assign_pointer(device->name, name); | |
e6e674bd | 969 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
cd02dca5 | 970 | fs_devices->missing_devices--; |
e6e674bd | 971 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
cd02dca5 | 972 | } |
4889bc05 | 973 | device->devt = path_devt; |
8a4b83cc CM |
974 | } |
975 | ||
77bdae4d AJ |
976 | /* |
977 | * Unmount does not free the btrfs_device struct but would zero | |
978 | * generation along with most of the other members. So just update | |
979 | * it back. We need it to pick the disk with largest generation | |
980 | * (as above). | |
981 | */ | |
d1a63002 | 982 | if (!fs_devices->opened) { |
77bdae4d | 983 | device->generation = found_transid; |
d1a63002 NB |
984 | fs_devices->latest_generation = max_t(u64, found_transid, |
985 | fs_devices->latest_generation); | |
986 | } | |
77bdae4d | 987 | |
f2788d2f AJ |
988 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
989 | ||
9c6d173e | 990 | mutex_unlock(&fs_devices->device_list_mutex); |
e124ece5 | 991 | return device; |
8a4b83cc CM |
992 | } |
993 | ||
e4404d6e YZ |
994 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
995 | { | |
996 | struct btrfs_fs_devices *fs_devices; | |
997 | struct btrfs_device *device; | |
998 | struct btrfs_device *orig_dev; | |
d2979aa2 | 999 | int ret = 0; |
e4404d6e | 1000 | |
c1247069 AJ |
1001 | lockdep_assert_held(&uuid_mutex); |
1002 | ||
f7361d8c | 1003 | fs_devices = alloc_fs_devices(orig->fsid); |
2208a378 ID |
1004 | if (IS_ERR(fs_devices)) |
1005 | return fs_devices; | |
e4404d6e | 1006 | |
02db0844 | 1007 | fs_devices->total_devices = orig->total_devices; |
e4404d6e YZ |
1008 | |
1009 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { | |
bb21e302 AJ |
1010 | const char *dev_path = NULL; |
1011 | ||
1012 | /* | |
1013 | * This is ok to do without RCU read locked because we hold the | |
1014 | * uuid mutex so nothing we touch in here is going to disappear. | |
1015 | */ | |
1016 | if (orig_dev->name) | |
1017 | dev_path = orig_dev->name->str; | |
606686ee | 1018 | |
12bd2fc0 | 1019 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
bb21e302 | 1020 | orig_dev->uuid, dev_path); |
d2979aa2 AJ |
1021 | if (IS_ERR(device)) { |
1022 | ret = PTR_ERR(device); | |
e4404d6e | 1023 | goto error; |
d2979aa2 | 1024 | } |
e4404d6e | 1025 | |
21e61ec6 JT |
1026 | if (orig_dev->zone_info) { |
1027 | struct btrfs_zoned_device_info *zone_info; | |
1028 | ||
1029 | zone_info = btrfs_clone_dev_zone_info(orig_dev); | |
1030 | if (!zone_info) { | |
1031 | btrfs_free_device(device); | |
1032 | ret = -ENOMEM; | |
1033 | goto error; | |
1034 | } | |
1035 | device->zone_info = zone_info; | |
1036 | } | |
1037 | ||
e4404d6e YZ |
1038 | list_add(&device->dev_list, &fs_devices->devices); |
1039 | device->fs_devices = fs_devices; | |
1040 | fs_devices->num_devices++; | |
1041 | } | |
1042 | return fs_devices; | |
1043 | error: | |
1044 | free_fs_devices(fs_devices); | |
d2979aa2 | 1045 | return ERR_PTR(ret); |
e4404d6e YZ |
1046 | } |
1047 | ||
3712ccb7 | 1048 | static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, |
bacce86a | 1049 | struct btrfs_device **latest_dev) |
dfe25020 | 1050 | { |
c6e30871 | 1051 | struct btrfs_device *device, *next; |
a6b0d5c8 | 1052 | |
46224705 | 1053 | /* This is the initialized path, it is safe to release the devices. */ |
c6e30871 | 1054 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
3712ccb7 | 1055 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) { |
401e29c1 | 1056 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
3712ccb7 | 1057 | &device->dev_state) && |
998a0671 AJ |
1058 | !test_bit(BTRFS_DEV_STATE_MISSING, |
1059 | &device->dev_state) && | |
3712ccb7 NB |
1060 | (!*latest_dev || |
1061 | device->generation > (*latest_dev)->generation)) { | |
1062 | *latest_dev = device; | |
a6b0d5c8 | 1063 | } |
2b82032c | 1064 | continue; |
a6b0d5c8 | 1065 | } |
2b82032c | 1066 | |
cf89af14 AJ |
1067 | /* |
1068 | * We have already validated the presence of BTRFS_DEV_REPLACE_DEVID, | |
1069 | * in btrfs_init_dev_replace() so just continue. | |
1070 | */ | |
1071 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) | |
1072 | continue; | |
1073 | ||
2b82032c | 1074 | if (device->bdev) { |
2736e8ee | 1075 | blkdev_put(device->bdev, device->holder); |
2b82032c YZ |
1076 | device->bdev = NULL; |
1077 | fs_devices->open_devices--; | |
1078 | } | |
ebbede42 | 1079 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
2b82032c | 1080 | list_del_init(&device->dev_alloc_list); |
ebbede42 | 1081 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
b2a61667 | 1082 | fs_devices->rw_devices--; |
2b82032c | 1083 | } |
e4404d6e YZ |
1084 | list_del_init(&device->dev_list); |
1085 | fs_devices->num_devices--; | |
a425f9d4 | 1086 | btrfs_free_device(device); |
dfe25020 | 1087 | } |
2b82032c | 1088 | |
3712ccb7 NB |
1089 | } |
1090 | ||
1091 | /* | |
1092 | * After we have read the system tree and know devids belonging to this | |
1093 | * filesystem, remove the device which does not belong there. | |
1094 | */ | |
bacce86a | 1095 | void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices) |
3712ccb7 NB |
1096 | { |
1097 | struct btrfs_device *latest_dev = NULL; | |
944d3f9f | 1098 | struct btrfs_fs_devices *seed_dev; |
3712ccb7 NB |
1099 | |
1100 | mutex_lock(&uuid_mutex); | |
bacce86a | 1101 | __btrfs_free_extra_devids(fs_devices, &latest_dev); |
944d3f9f NB |
1102 | |
1103 | list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list) | |
bacce86a | 1104 | __btrfs_free_extra_devids(seed_dev, &latest_dev); |
2b82032c | 1105 | |
d24fa5c1 | 1106 | fs_devices->latest_dev = latest_dev; |
a6b0d5c8 | 1107 | |
dfe25020 | 1108 | mutex_unlock(&uuid_mutex); |
dfe25020 | 1109 | } |
a0af469b | 1110 | |
14238819 AJ |
1111 | static void btrfs_close_bdev(struct btrfs_device *device) |
1112 | { | |
08ffcae8 DS |
1113 | if (!device->bdev) |
1114 | return; | |
1115 | ||
ebbede42 | 1116 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
14238819 AJ |
1117 | sync_blockdev(device->bdev); |
1118 | invalidate_bdev(device->bdev); | |
1119 | } | |
1120 | ||
2736e8ee | 1121 | blkdev_put(device->bdev, device->holder); |
14238819 AJ |
1122 | } |
1123 | ||
959b1c04 | 1124 | static void btrfs_close_one_device(struct btrfs_device *device) |
f448341a AJ |
1125 | { |
1126 | struct btrfs_fs_devices *fs_devices = device->fs_devices; | |
f448341a | 1127 | |
ebbede42 | 1128 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
f448341a AJ |
1129 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
1130 | list_del_init(&device->dev_alloc_list); | |
1131 | fs_devices->rw_devices--; | |
1132 | } | |
1133 | ||
0d977e0e DCZX |
1134 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) |
1135 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); | |
1136 | ||
5d03dbeb LZ |
1137 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
1138 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); | |
f448341a | 1139 | fs_devices->missing_devices--; |
5d03dbeb | 1140 | } |
f448341a | 1141 | |
959b1c04 | 1142 | btrfs_close_bdev(device); |
321f69f8 | 1143 | if (device->bdev) { |
3fff3975 | 1144 | fs_devices->open_devices--; |
321f69f8 | 1145 | device->bdev = NULL; |
f448341a | 1146 | } |
321f69f8 | 1147 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
5b316468 | 1148 | btrfs_destroy_dev_zone_info(device); |
f448341a | 1149 | |
321f69f8 JT |
1150 | device->fs_info = NULL; |
1151 | atomic_set(&device->dev_stats_ccnt, 0); | |
1152 | extent_io_tree_release(&device->alloc_state); | |
959b1c04 | 1153 | |
6b225baa FM |
1154 | /* |
1155 | * Reset the flush error record. We might have a transient flush error | |
1156 | * in this mount, and if so we aborted the current transaction and set | |
1157 | * the fs to an error state, guaranteeing no super blocks can be further | |
1158 | * committed. However that error might be transient and if we unmount the | |
1159 | * filesystem and mount it again, we should allow the mount to succeed | |
1160 | * (btrfs_check_rw_degradable() should not fail) - if after mounting the | |
1161 | * filesystem again we still get flush errors, then we will again abort | |
1162 | * any transaction and set the error state, guaranteeing no commits of | |
1163 | * unsafe super blocks. | |
1164 | */ | |
1165 | device->last_flush_error = 0; | |
1166 | ||
321f69f8 | 1167 | /* Verify the device is back in a pristine state */ |
1f16033c AJ |
1168 | WARN_ON(test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); |
1169 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); | |
1170 | WARN_ON(!list_empty(&device->dev_alloc_list)); | |
1171 | WARN_ON(!list_empty(&device->post_commit_list)); | |
f448341a AJ |
1172 | } |
1173 | ||
54eed6ae | 1174 | static void close_fs_devices(struct btrfs_fs_devices *fs_devices) |
8a4b83cc | 1175 | { |
2037a093 | 1176 | struct btrfs_device *device, *tmp; |
e4404d6e | 1177 | |
425c6ed6 JB |
1178 | lockdep_assert_held(&uuid_mutex); |
1179 | ||
2b82032c | 1180 | if (--fs_devices->opened > 0) |
54eed6ae | 1181 | return; |
8a4b83cc | 1182 | |
425c6ed6 | 1183 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) |
959b1c04 | 1184 | btrfs_close_one_device(device); |
c9513edb | 1185 | |
e4404d6e YZ |
1186 | WARN_ON(fs_devices->open_devices); |
1187 | WARN_ON(fs_devices->rw_devices); | |
2b82032c | 1188 | fs_devices->opened = 0; |
0395d84f | 1189 | fs_devices->seeding = false; |
c4989c2f | 1190 | fs_devices->fs_info = NULL; |
8a4b83cc CM |
1191 | } |
1192 | ||
54eed6ae | 1193 | void btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
2b82032c | 1194 | { |
944d3f9f NB |
1195 | LIST_HEAD(list); |
1196 | struct btrfs_fs_devices *tmp; | |
2b82032c YZ |
1197 | |
1198 | mutex_lock(&uuid_mutex); | |
54eed6ae | 1199 | close_fs_devices(fs_devices); |
5f58d783 | 1200 | if (!fs_devices->opened) { |
944d3f9f | 1201 | list_splice_init(&fs_devices->seed_list, &list); |
e4404d6e | 1202 | |
5f58d783 AJ |
1203 | /* |
1204 | * If the struct btrfs_fs_devices is not assembled with any | |
1205 | * other device, it can be re-initialized during the next mount | |
1206 | * without the needing device-scan step. Therefore, it can be | |
1207 | * fully freed. | |
1208 | */ | |
1209 | if (fs_devices->num_devices == 1) { | |
1210 | list_del(&fs_devices->fs_list); | |
1211 | free_fs_devices(fs_devices); | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | ||
944d3f9f | 1216 | list_for_each_entry_safe(fs_devices, tmp, &list, seed_list) { |
0226e0eb | 1217 | close_fs_devices(fs_devices); |
944d3f9f | 1218 | list_del(&fs_devices->seed_list); |
e4404d6e YZ |
1219 | free_fs_devices(fs_devices); |
1220 | } | |
425c6ed6 | 1221 | mutex_unlock(&uuid_mutex); |
2b82032c YZ |
1222 | } |
1223 | ||
897fb573 | 1224 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
05bdb996 | 1225 | blk_mode_t flags, void *holder) |
8a4b83cc | 1226 | { |
8a4b83cc | 1227 | struct btrfs_device *device; |
443f24fe | 1228 | struct btrfs_device *latest_dev = NULL; |
96c2e067 | 1229 | struct btrfs_device *tmp_device; |
8a4b83cc | 1230 | |
96c2e067 AJ |
1231 | list_for_each_entry_safe(device, tmp_device, &fs_devices->devices, |
1232 | dev_list) { | |
1233 | int ret; | |
a0af469b | 1234 | |
96c2e067 AJ |
1235 | ret = btrfs_open_one_device(fs_devices, device, flags, holder); |
1236 | if (ret == 0 && | |
1237 | (!latest_dev || device->generation > latest_dev->generation)) { | |
9f050db4 | 1238 | latest_dev = device; |
96c2e067 AJ |
1239 | } else if (ret == -ENODATA) { |
1240 | fs_devices->num_devices--; | |
1241 | list_del(&device->dev_list); | |
1242 | btrfs_free_device(device); | |
1243 | } | |
8a4b83cc | 1244 | } |
1ed802c9 AJ |
1245 | if (fs_devices->open_devices == 0) |
1246 | return -EINVAL; | |
1247 | ||
2b82032c | 1248 | fs_devices->opened = 1; |
d24fa5c1 | 1249 | fs_devices->latest_dev = latest_dev; |
2b82032c | 1250 | fs_devices->total_rw_bytes = 0; |
c4a816c6 | 1251 | fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; |
33fd2f71 | 1252 | fs_devices->read_policy = BTRFS_READ_POLICY_PID; |
1ed802c9 AJ |
1253 | |
1254 | return 0; | |
2b82032c YZ |
1255 | } |
1256 | ||
4f0f586b ST |
1257 | static int devid_cmp(void *priv, const struct list_head *a, |
1258 | const struct list_head *b) | |
f8e10cd3 | 1259 | { |
214cc184 | 1260 | const struct btrfs_device *dev1, *dev2; |
f8e10cd3 AJ |
1261 | |
1262 | dev1 = list_entry(a, struct btrfs_device, dev_list); | |
1263 | dev2 = list_entry(b, struct btrfs_device, dev_list); | |
1264 | ||
1265 | if (dev1->devid < dev2->devid) | |
1266 | return -1; | |
1267 | else if (dev1->devid > dev2->devid) | |
1268 | return 1; | |
1269 | return 0; | |
1270 | } | |
1271 | ||
2b82032c | 1272 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
05bdb996 | 1273 | blk_mode_t flags, void *holder) |
2b82032c YZ |
1274 | { |
1275 | int ret; | |
1276 | ||
f5194e34 | 1277 | lockdep_assert_held(&uuid_mutex); |
18c850fd JB |
1278 | /* |
1279 | * The device_list_mutex cannot be taken here in case opening the | |
a8698707 | 1280 | * underlying device takes further locks like open_mutex. |
18c850fd JB |
1281 | * |
1282 | * We also don't need the lock here as this is called during mount and | |
1283 | * exclusion is provided by uuid_mutex | |
1284 | */ | |
f5194e34 | 1285 | |
2b82032c | 1286 | if (fs_devices->opened) { |
e4404d6e YZ |
1287 | fs_devices->opened++; |
1288 | ret = 0; | |
2b82032c | 1289 | } else { |
f8e10cd3 | 1290 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
897fb573 | 1291 | ret = open_fs_devices(fs_devices, flags, holder); |
2b82032c | 1292 | } |
542c5908 | 1293 | |
8a4b83cc CM |
1294 | return ret; |
1295 | } | |
1296 | ||
8f32380d | 1297 | void btrfs_release_disk_super(struct btrfs_super_block *super) |
6cf86a00 | 1298 | { |
8f32380d JT |
1299 | struct page *page = virt_to_page(super); |
1300 | ||
6cf86a00 AJ |
1301 | put_page(page); |
1302 | } | |
1303 | ||
b335eab8 | 1304 | static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev, |
12659251 | 1305 | u64 bytenr, u64 bytenr_orig) |
6cf86a00 | 1306 | { |
b335eab8 NB |
1307 | struct btrfs_super_block *disk_super; |
1308 | struct page *page; | |
6cf86a00 AJ |
1309 | void *p; |
1310 | pgoff_t index; | |
1311 | ||
1312 | /* make sure our super fits in the device */ | |
cda00eba | 1313 | if (bytenr + PAGE_SIZE >= bdev_nr_bytes(bdev)) |
b335eab8 | 1314 | return ERR_PTR(-EINVAL); |
6cf86a00 AJ |
1315 | |
1316 | /* make sure our super fits in the page */ | |
b335eab8 NB |
1317 | if (sizeof(*disk_super) > PAGE_SIZE) |
1318 | return ERR_PTR(-EINVAL); | |
6cf86a00 AJ |
1319 | |
1320 | /* make sure our super doesn't straddle pages on disk */ | |
1321 | index = bytenr >> PAGE_SHIFT; | |
b335eab8 NB |
1322 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index) |
1323 | return ERR_PTR(-EINVAL); | |
6cf86a00 AJ |
1324 | |
1325 | /* pull in the page with our super */ | |
b335eab8 | 1326 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL); |
6cf86a00 | 1327 | |
b335eab8 NB |
1328 | if (IS_ERR(page)) |
1329 | return ERR_CAST(page); | |
6cf86a00 | 1330 | |
b335eab8 | 1331 | p = page_address(page); |
6cf86a00 AJ |
1332 | |
1333 | /* align our pointer to the offset of the super block */ | |
b335eab8 | 1334 | disk_super = p + offset_in_page(bytenr); |
6cf86a00 | 1335 | |
12659251 | 1336 | if (btrfs_super_bytenr(disk_super) != bytenr_orig || |
b335eab8 | 1337 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
8f32380d | 1338 | btrfs_release_disk_super(p); |
b335eab8 | 1339 | return ERR_PTR(-EINVAL); |
6cf86a00 AJ |
1340 | } |
1341 | ||
b335eab8 NB |
1342 | if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1]) |
1343 | disk_super->label[BTRFS_LABEL_SIZE - 1] = 0; | |
6cf86a00 | 1344 | |
b335eab8 | 1345 | return disk_super; |
6cf86a00 AJ |
1346 | } |
1347 | ||
16cab91a | 1348 | int btrfs_forget_devices(dev_t devt) |
228a73ab AJ |
1349 | { |
1350 | int ret; | |
1351 | ||
1352 | mutex_lock(&uuid_mutex); | |
16cab91a | 1353 | ret = btrfs_free_stale_devices(devt, NULL); |
228a73ab AJ |
1354 | mutex_unlock(&uuid_mutex); |
1355 | ||
1356 | return ret; | |
1357 | } | |
1358 | ||
6f60cbd3 DS |
1359 | /* |
1360 | * Look for a btrfs signature on a device. This may be called out of the mount path | |
1361 | * and we are not allowed to call set_blocksize during the scan. The superblock | |
bc27d6f0 AJ |
1362 | * is read via pagecache. |
1363 | * | |
1364 | * With @mount_arg_dev it's a scan during mount time that will always register | |
1365 | * the device or return an error. Multi-device and seeding devices are registered | |
1366 | * in both cases. | |
6f60cbd3 | 1367 | */ |
bc27d6f0 AJ |
1368 | struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags, |
1369 | bool mount_arg_dev) | |
8a4b83cc CM |
1370 | { |
1371 | struct btrfs_super_block *disk_super; | |
4306a974 | 1372 | bool new_device_added = false; |
36350e95 | 1373 | struct btrfs_device *device = NULL; |
8a4b83cc | 1374 | struct block_device *bdev; |
12659251 NA |
1375 | u64 bytenr, bytenr_orig; |
1376 | int ret; | |
8a4b83cc | 1377 | |
899f9307 DS |
1378 | lockdep_assert_held(&uuid_mutex); |
1379 | ||
6f60cbd3 DS |
1380 | /* |
1381 | * we would like to check all the supers, but that would make | |
1382 | * a btrfs mount succeed after a mkfs from a different FS. | |
1383 | * So, we need to add a special mount option to scan for | |
1384 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead | |
1385 | */ | |
6f60cbd3 | 1386 | |
50d281fc | 1387 | /* |
2ef78928 CH |
1388 | * Avoid an exclusive open here, as the systemd-udev may initiate the |
1389 | * device scan which may race with the user's mount or mkfs command, | |
1390 | * resulting in failure. | |
1391 | * Since the device scan is solely for reading purposes, there is no | |
1392 | * need for an exclusive open. Additionally, the devices are read again | |
50d281fc AJ |
1393 | * during the mount process. It is ok to get some inconsistent |
1394 | * values temporarily, as the device paths of the fsid are the only | |
1395 | * required information for assembling the volume. | |
1396 | */ | |
2ef78928 | 1397 | bdev = blkdev_get_by_path(path, flags, NULL, NULL); |
b6ed73bc | 1398 | if (IS_ERR(bdev)) |
36350e95 | 1399 | return ERR_CAST(bdev); |
6f60cbd3 | 1400 | |
12659251 NA |
1401 | bytenr_orig = btrfs_sb_offset(0); |
1402 | ret = btrfs_sb_log_location_bdev(bdev, 0, READ, &bytenr); | |
4989d4a0 SK |
1403 | if (ret) { |
1404 | device = ERR_PTR(ret); | |
1405 | goto error_bdev_put; | |
1406 | } | |
12659251 NA |
1407 | |
1408 | disk_super = btrfs_read_disk_super(bdev, bytenr, bytenr_orig); | |
b335eab8 NB |
1409 | if (IS_ERR(disk_super)) { |
1410 | device = ERR_CAST(disk_super); | |
6f60cbd3 | 1411 | goto error_bdev_put; |
05a5c55d | 1412 | } |
6f60cbd3 | 1413 | |
bc27d6f0 AJ |
1414 | if (!mount_arg_dev && btrfs_super_num_devices(disk_super) == 1 && |
1415 | !(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING)) { | |
1416 | dev_t devt; | |
1417 | ||
1418 | ret = lookup_bdev(path, &devt); | |
1419 | if (ret) | |
1420 | btrfs_warn(NULL, "lookup bdev failed for path %s: %d", | |
1421 | path, ret); | |
1422 | else | |
1423 | btrfs_free_stale_devices(devt, NULL); | |
1424 | ||
1425 | pr_debug("BTRFS: skip registering single non-seed device %s\n", path); | |
1426 | device = NULL; | |
1427 | goto free_disk_super; | |
1428 | } | |
1429 | ||
4306a974 | 1430 | device = device_list_add(path, disk_super, &new_device_added); |
4889bc05 AJ |
1431 | if (!IS_ERR(device) && new_device_added) |
1432 | btrfs_free_stale_devices(device->devt, device); | |
6f60cbd3 | 1433 | |
bc27d6f0 | 1434 | free_disk_super: |
8f32380d | 1435 | btrfs_release_disk_super(disk_super); |
6f60cbd3 DS |
1436 | |
1437 | error_bdev_put: | |
2736e8ee | 1438 | blkdev_put(bdev, NULL); |
b6ed73bc | 1439 | |
36350e95 | 1440 | return device; |
8a4b83cc | 1441 | } |
0b86a832 | 1442 | |
1c11b63e JM |
1443 | /* |
1444 | * Try to find a chunk that intersects [start, start + len] range and when one | |
1445 | * such is found, record the end of it in *start | |
1446 | */ | |
1c11b63e JM |
1447 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
1448 | u64 len) | |
6df9a95e | 1449 | { |
1c11b63e | 1450 | u64 physical_start, physical_end; |
6df9a95e | 1451 | |
1c11b63e | 1452 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
6df9a95e | 1453 | |
e5860f82 FM |
1454 | if (find_first_extent_bit(&device->alloc_state, *start, |
1455 | &physical_start, &physical_end, | |
1456 | CHUNK_ALLOCATED, NULL)) { | |
c152b63e | 1457 | |
1c11b63e JM |
1458 | if (in_range(physical_start, *start, len) || |
1459 | in_range(*start, physical_start, | |
1460 | physical_end - physical_start)) { | |
1461 | *start = physical_end + 1; | |
1462 | return true; | |
6df9a95e JB |
1463 | } |
1464 | } | |
1c11b63e | 1465 | return false; |
6df9a95e JB |
1466 | } |
1467 | ||
ed8947bc | 1468 | static u64 dev_extent_search_start(struct btrfs_device *device) |
3b4ffa40 NA |
1469 | { |
1470 | switch (device->fs_devices->chunk_alloc_policy) { | |
1471 | case BTRFS_CHUNK_ALLOC_REGULAR: | |
ed8947bc | 1472 | return BTRFS_DEVICE_RANGE_RESERVED; |
1cd6121f NA |
1473 | case BTRFS_CHUNK_ALLOC_ZONED: |
1474 | /* | |
1475 | * We don't care about the starting region like regular | |
1476 | * allocator, because we anyway use/reserve the first two zones | |
1477 | * for superblock logging. | |
1478 | */ | |
ed8947bc | 1479 | return 0; |
3b4ffa40 NA |
1480 | default: |
1481 | BUG(); | |
1482 | } | |
1483 | } | |
1484 | ||
1cd6121f NA |
1485 | static bool dev_extent_hole_check_zoned(struct btrfs_device *device, |
1486 | u64 *hole_start, u64 *hole_size, | |
1487 | u64 num_bytes) | |
1488 | { | |
1489 | u64 zone_size = device->zone_info->zone_size; | |
1490 | u64 pos; | |
1491 | int ret; | |
1492 | bool changed = false; | |
1493 | ||
1494 | ASSERT(IS_ALIGNED(*hole_start, zone_size)); | |
1495 | ||
1496 | while (*hole_size > 0) { | |
1497 | pos = btrfs_find_allocatable_zones(device, *hole_start, | |
1498 | *hole_start + *hole_size, | |
1499 | num_bytes); | |
1500 | if (pos != *hole_start) { | |
1501 | *hole_size = *hole_start + *hole_size - pos; | |
1502 | *hole_start = pos; | |
1503 | changed = true; | |
1504 | if (*hole_size < num_bytes) | |
1505 | break; | |
1506 | } | |
1507 | ||
1508 | ret = btrfs_ensure_empty_zones(device, pos, num_bytes); | |
1509 | ||
1510 | /* Range is ensured to be empty */ | |
1511 | if (!ret) | |
1512 | return changed; | |
1513 | ||
1514 | /* Given hole range was invalid (outside of device) */ | |
1515 | if (ret == -ERANGE) { | |
1516 | *hole_start += *hole_size; | |
d6f67afb | 1517 | *hole_size = 0; |
7000babd | 1518 | return true; |
1cd6121f NA |
1519 | } |
1520 | ||
1521 | *hole_start += zone_size; | |
1522 | *hole_size -= zone_size; | |
1523 | changed = true; | |
1524 | } | |
1525 | ||
1526 | return changed; | |
1527 | } | |
1528 | ||
43dd529a DS |
1529 | /* |
1530 | * Check if specified hole is suitable for allocation. | |
1531 | * | |
3b4ffa40 NA |
1532 | * @device: the device which we have the hole |
1533 | * @hole_start: starting position of the hole | |
1534 | * @hole_size: the size of the hole | |
1535 | * @num_bytes: the size of the free space that we need | |
1536 | * | |
1cd6121f | 1537 | * This function may modify @hole_start and @hole_size to reflect the suitable |
3b4ffa40 NA |
1538 | * position for allocation. Returns 1 if hole position is updated, 0 otherwise. |
1539 | */ | |
1540 | static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start, | |
1541 | u64 *hole_size, u64 num_bytes) | |
1542 | { | |
1543 | bool changed = false; | |
1544 | u64 hole_end = *hole_start + *hole_size; | |
1545 | ||
1cd6121f NA |
1546 | for (;;) { |
1547 | /* | |
1548 | * Check before we set max_hole_start, otherwise we could end up | |
1549 | * sending back this offset anyway. | |
1550 | */ | |
1551 | if (contains_pending_extent(device, hole_start, *hole_size)) { | |
1552 | if (hole_end >= *hole_start) | |
1553 | *hole_size = hole_end - *hole_start; | |
1554 | else | |
1555 | *hole_size = 0; | |
1556 | changed = true; | |
1557 | } | |
1558 | ||
1559 | switch (device->fs_devices->chunk_alloc_policy) { | |
1560 | case BTRFS_CHUNK_ALLOC_REGULAR: | |
1561 | /* No extra check */ | |
1562 | break; | |
1563 | case BTRFS_CHUNK_ALLOC_ZONED: | |
1564 | if (dev_extent_hole_check_zoned(device, hole_start, | |
1565 | hole_size, num_bytes)) { | |
1566 | changed = true; | |
1567 | /* | |
1568 | * The changed hole can contain pending extent. | |
1569 | * Loop again to check that. | |
1570 | */ | |
1571 | continue; | |
1572 | } | |
1573 | break; | |
1574 | default: | |
1575 | BUG(); | |
1576 | } | |
3b4ffa40 | 1577 | |
3b4ffa40 | 1578 | break; |
3b4ffa40 NA |
1579 | } |
1580 | ||
1581 | return changed; | |
1582 | } | |
6df9a95e | 1583 | |
0b86a832 | 1584 | /* |
43dd529a DS |
1585 | * Find free space in the specified device. |
1586 | * | |
499f377f JM |
1587 | * @device: the device which we search the free space in |
1588 | * @num_bytes: the size of the free space that we need | |
1589 | * @search_start: the position from which to begin the search | |
1590 | * @start: store the start of the free space. | |
1591 | * @len: the size of the free space. that we find, or the size | |
1592 | * of the max free space if we don't find suitable free space | |
7bfc837d | 1593 | * |
43dd529a DS |
1594 | * This does a pretty simple search, the expectation is that it is called very |
1595 | * infrequently and that a given device has a small number of extents. | |
7bfc837d MX |
1596 | * |
1597 | * @start is used to store the start of the free space if we find. But if we | |
1598 | * don't find suitable free space, it will be used to store the start position | |
1599 | * of the max free space. | |
1600 | * | |
1601 | * @len is used to store the size of the free space that we find. | |
1602 | * But if we don't find suitable free space, it is used to store the size of | |
1603 | * the max free space. | |
135da976 QW |
1604 | * |
1605 | * NOTE: This function will search *commit* root of device tree, and does extra | |
1606 | * check to ensure dev extents are not double allocated. | |
1607 | * This makes the function safe to allocate dev extents but may not report | |
1608 | * correct usable device space, as device extent freed in current transaction | |
1a9fd417 | 1609 | * is not reported as available. |
0b86a832 | 1610 | */ |
ed8947bc FM |
1611 | static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
1612 | u64 *start, u64 *len) | |
0b86a832 | 1613 | { |
0b246afa JM |
1614 | struct btrfs_fs_info *fs_info = device->fs_info; |
1615 | struct btrfs_root *root = fs_info->dev_root; | |
0b86a832 | 1616 | struct btrfs_key key; |
7bfc837d | 1617 | struct btrfs_dev_extent *dev_extent; |
2b82032c | 1618 | struct btrfs_path *path; |
ed8947bc | 1619 | u64 search_start; |
7bfc837d MX |
1620 | u64 hole_size; |
1621 | u64 max_hole_start; | |
20218dfb | 1622 | u64 max_hole_size = 0; |
7bfc837d | 1623 | u64 extent_end; |
0b86a832 CM |
1624 | u64 search_end = device->total_bytes; |
1625 | int ret; | |
7bfc837d | 1626 | int slot; |
0b86a832 | 1627 | struct extent_buffer *l; |
8cdc7c5b | 1628 | |
ed8947bc | 1629 | search_start = dev_extent_search_start(device); |
20218dfb | 1630 | max_hole_start = search_start; |
0b86a832 | 1631 | |
1cd6121f NA |
1632 | WARN_ON(device->zone_info && |
1633 | !IS_ALIGNED(num_bytes, device->zone_info->zone_size)); | |
1634 | ||
6df9a95e | 1635 | path = btrfs_alloc_path(); |
20218dfb JB |
1636 | if (!path) { |
1637 | ret = -ENOMEM; | |
1638 | goto out; | |
1639 | } | |
f2ab7618 | 1640 | again: |
401e29c1 AJ |
1641 | if (search_start >= search_end || |
1642 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { | |
7bfc837d | 1643 | ret = -ENOSPC; |
6df9a95e | 1644 | goto out; |
7bfc837d MX |
1645 | } |
1646 | ||
e4058b54 | 1647 | path->reada = READA_FORWARD; |
6df9a95e JB |
1648 | path->search_commit_root = 1; |
1649 | path->skip_locking = 1; | |
7bfc837d | 1650 | |
0b86a832 CM |
1651 | key.objectid = device->devid; |
1652 | key.offset = search_start; | |
1653 | key.type = BTRFS_DEV_EXTENT_KEY; | |
7bfc837d | 1654 | |
0ff40a91 | 1655 | ret = btrfs_search_backwards(root, &key, path); |
0b86a832 | 1656 | if (ret < 0) |
7bfc837d | 1657 | goto out; |
7bfc837d | 1658 | |
3c538de0 | 1659 | while (search_start < search_end) { |
0b86a832 CM |
1660 | l = path->nodes[0]; |
1661 | slot = path->slots[0]; | |
1662 | if (slot >= btrfs_header_nritems(l)) { | |
1663 | ret = btrfs_next_leaf(root, path); | |
1664 | if (ret == 0) | |
1665 | continue; | |
1666 | if (ret < 0) | |
7bfc837d MX |
1667 | goto out; |
1668 | ||
1669 | break; | |
0b86a832 CM |
1670 | } |
1671 | btrfs_item_key_to_cpu(l, &key, slot); | |
1672 | ||
1673 | if (key.objectid < device->devid) | |
1674 | goto next; | |
1675 | ||
1676 | if (key.objectid > device->devid) | |
7bfc837d | 1677 | break; |
0b86a832 | 1678 | |
962a298f | 1679 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
7bfc837d | 1680 | goto next; |
9779b72f | 1681 | |
3c538de0 JB |
1682 | if (key.offset > search_end) |
1683 | break; | |
1684 | ||
7bfc837d MX |
1685 | if (key.offset > search_start) { |
1686 | hole_size = key.offset - search_start; | |
3b4ffa40 NA |
1687 | dev_extent_hole_check(device, &search_start, &hole_size, |
1688 | num_bytes); | |
6df9a95e | 1689 | |
7bfc837d MX |
1690 | if (hole_size > max_hole_size) { |
1691 | max_hole_start = search_start; | |
1692 | max_hole_size = hole_size; | |
1693 | } | |
9779b72f | 1694 | |
7bfc837d MX |
1695 | /* |
1696 | * If this free space is greater than which we need, | |
1697 | * it must be the max free space that we have found | |
1698 | * until now, so max_hole_start must point to the start | |
1699 | * of this free space and the length of this free space | |
1700 | * is stored in max_hole_size. Thus, we return | |
1701 | * max_hole_start and max_hole_size and go back to the | |
1702 | * caller. | |
1703 | */ | |
1704 | if (hole_size >= num_bytes) { | |
1705 | ret = 0; | |
1706 | goto out; | |
0b86a832 CM |
1707 | } |
1708 | } | |
0b86a832 | 1709 | |
0b86a832 | 1710 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
7bfc837d MX |
1711 | extent_end = key.offset + btrfs_dev_extent_length(l, |
1712 | dev_extent); | |
1713 | if (extent_end > search_start) | |
1714 | search_start = extent_end; | |
0b86a832 CM |
1715 | next: |
1716 | path->slots[0]++; | |
1717 | cond_resched(); | |
1718 | } | |
0b86a832 | 1719 | |
38c01b96 | 1720 | /* |
1721 | * At this point, search_start should be the end of | |
1722 | * allocated dev extents, and when shrinking the device, | |
1723 | * search_end may be smaller than search_start. | |
1724 | */ | |
f2ab7618 | 1725 | if (search_end > search_start) { |
38c01b96 | 1726 | hole_size = search_end - search_start; |
3b4ffa40 NA |
1727 | if (dev_extent_hole_check(device, &search_start, &hole_size, |
1728 | num_bytes)) { | |
f2ab7618 ZL |
1729 | btrfs_release_path(path); |
1730 | goto again; | |
1731 | } | |
0b86a832 | 1732 | |
f2ab7618 ZL |
1733 | if (hole_size > max_hole_size) { |
1734 | max_hole_start = search_start; | |
1735 | max_hole_size = hole_size; | |
1736 | } | |
6df9a95e JB |
1737 | } |
1738 | ||
7bfc837d | 1739 | /* See above. */ |
f2ab7618 | 1740 | if (max_hole_size < num_bytes) |
7bfc837d MX |
1741 | ret = -ENOSPC; |
1742 | else | |
1743 | ret = 0; | |
1744 | ||
3c538de0 | 1745 | ASSERT(max_hole_start + max_hole_size <= search_end); |
7bfc837d | 1746 | out: |
2b82032c | 1747 | btrfs_free_path(path); |
7bfc837d | 1748 | *start = max_hole_start; |
b2117a39 | 1749 | if (len) |
7bfc837d | 1750 | *len = max_hole_size; |
0b86a832 CM |
1751 | return ret; |
1752 | } | |
1753 | ||
b2950863 | 1754 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
8f18cf13 | 1755 | struct btrfs_device *device, |
2196d6e8 | 1756 | u64 start, u64 *dev_extent_len) |
8f18cf13 | 1757 | { |
0b246afa JM |
1758 | struct btrfs_fs_info *fs_info = device->fs_info; |
1759 | struct btrfs_root *root = fs_info->dev_root; | |
8f18cf13 CM |
1760 | int ret; |
1761 | struct btrfs_path *path; | |
8f18cf13 | 1762 | struct btrfs_key key; |
a061fc8d CM |
1763 | struct btrfs_key found_key; |
1764 | struct extent_buffer *leaf = NULL; | |
1765 | struct btrfs_dev_extent *extent = NULL; | |
8f18cf13 CM |
1766 | |
1767 | path = btrfs_alloc_path(); | |
1768 | if (!path) | |
1769 | return -ENOMEM; | |
1770 | ||
1771 | key.objectid = device->devid; | |
1772 | key.offset = start; | |
1773 | key.type = BTRFS_DEV_EXTENT_KEY; | |
924cd8fb | 1774 | again: |
8f18cf13 | 1775 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
a061fc8d CM |
1776 | if (ret > 0) { |
1777 | ret = btrfs_previous_item(root, path, key.objectid, | |
1778 | BTRFS_DEV_EXTENT_KEY); | |
b0b802d7 TI |
1779 | if (ret) |
1780 | goto out; | |
a061fc8d CM |
1781 | leaf = path->nodes[0]; |
1782 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1783 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1784 | struct btrfs_dev_extent); | |
1785 | BUG_ON(found_key.offset > start || found_key.offset + | |
1786 | btrfs_dev_extent_length(leaf, extent) < start); | |
924cd8fb MX |
1787 | key = found_key; |
1788 | btrfs_release_path(path); | |
1789 | goto again; | |
a061fc8d CM |
1790 | } else if (ret == 0) { |
1791 | leaf = path->nodes[0]; | |
1792 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1793 | struct btrfs_dev_extent); | |
79787eaa | 1794 | } else { |
79787eaa | 1795 | goto out; |
a061fc8d | 1796 | } |
8f18cf13 | 1797 | |
2196d6e8 MX |
1798 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
1799 | ||
8f18cf13 | 1800 | ret = btrfs_del_item(trans, root, path); |
79bd3712 | 1801 | if (ret == 0) |
3204d33c | 1802 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
b0b802d7 | 1803 | out: |
8f18cf13 CM |
1804 | btrfs_free_path(path); |
1805 | return ret; | |
1806 | } | |
1807 | ||
6df9a95e | 1808 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
0b86a832 | 1809 | { |
6df9a95e JB |
1810 | struct extent_map_tree *em_tree; |
1811 | struct extent_map *em; | |
1812 | struct rb_node *n; | |
1813 | u64 ret = 0; | |
0b86a832 | 1814 | |
c8bf1b67 | 1815 | em_tree = &fs_info->mapping_tree; |
6df9a95e | 1816 | read_lock(&em_tree->lock); |
07e1ce09 | 1817 | n = rb_last(&em_tree->map.rb_root); |
6df9a95e JB |
1818 | if (n) { |
1819 | em = rb_entry(n, struct extent_map, rb_node); | |
1820 | ret = em->start + em->len; | |
0b86a832 | 1821 | } |
6df9a95e JB |
1822 | read_unlock(&em_tree->lock); |
1823 | ||
0b86a832 CM |
1824 | return ret; |
1825 | } | |
1826 | ||
53f10659 ID |
1827 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
1828 | u64 *devid_ret) | |
0b86a832 CM |
1829 | { |
1830 | int ret; | |
1831 | struct btrfs_key key; | |
1832 | struct btrfs_key found_key; | |
2b82032c YZ |
1833 | struct btrfs_path *path; |
1834 | ||
2b82032c YZ |
1835 | path = btrfs_alloc_path(); |
1836 | if (!path) | |
1837 | return -ENOMEM; | |
0b86a832 CM |
1838 | |
1839 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1840 | key.type = BTRFS_DEV_ITEM_KEY; | |
1841 | key.offset = (u64)-1; | |
1842 | ||
53f10659 | 1843 | ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
0b86a832 CM |
1844 | if (ret < 0) |
1845 | goto error; | |
1846 | ||
a06dee4d AJ |
1847 | if (ret == 0) { |
1848 | /* Corruption */ | |
1849 | btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); | |
1850 | ret = -EUCLEAN; | |
1851 | goto error; | |
1852 | } | |
0b86a832 | 1853 | |
53f10659 ID |
1854 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
1855 | BTRFS_DEV_ITEMS_OBJECTID, | |
0b86a832 CM |
1856 | BTRFS_DEV_ITEM_KEY); |
1857 | if (ret) { | |
53f10659 | 1858 | *devid_ret = 1; |
0b86a832 CM |
1859 | } else { |
1860 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1861 | path->slots[0]); | |
53f10659 | 1862 | *devid_ret = found_key.offset + 1; |
0b86a832 CM |
1863 | } |
1864 | ret = 0; | |
1865 | error: | |
2b82032c | 1866 | btrfs_free_path(path); |
0b86a832 CM |
1867 | return ret; |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * the device information is stored in the chunk root | |
1872 | * the btrfs_device struct should be fully filled in | |
1873 | */ | |
c74a0b02 | 1874 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
48a3b636 | 1875 | struct btrfs_device *device) |
0b86a832 CM |
1876 | { |
1877 | int ret; | |
1878 | struct btrfs_path *path; | |
1879 | struct btrfs_dev_item *dev_item; | |
1880 | struct extent_buffer *leaf; | |
1881 | struct btrfs_key key; | |
1882 | unsigned long ptr; | |
0b86a832 | 1883 | |
0b86a832 CM |
1884 | path = btrfs_alloc_path(); |
1885 | if (!path) | |
1886 | return -ENOMEM; | |
1887 | ||
0b86a832 CM |
1888 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1889 | key.type = BTRFS_DEV_ITEM_KEY; | |
2b82032c | 1890 | key.offset = device->devid; |
0b86a832 | 1891 | |
2bb2e00e | 1892 | btrfs_reserve_chunk_metadata(trans, true); |
8e87e856 NB |
1893 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
1894 | &key, sizeof(*dev_item)); | |
2bb2e00e | 1895 | btrfs_trans_release_chunk_metadata(trans); |
0b86a832 CM |
1896 | if (ret) |
1897 | goto out; | |
1898 | ||
1899 | leaf = path->nodes[0]; | |
1900 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1901 | ||
1902 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2b82032c | 1903 | btrfs_set_device_generation(leaf, dev_item, 0); |
0b86a832 CM |
1904 | btrfs_set_device_type(leaf, dev_item, device->type); |
1905 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1906 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1907 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
7cc8e58d MX |
1908 | btrfs_set_device_total_bytes(leaf, dev_item, |
1909 | btrfs_device_get_disk_total_bytes(device)); | |
1910 | btrfs_set_device_bytes_used(leaf, dev_item, | |
1911 | btrfs_device_get_bytes_used(device)); | |
e17cade2 CM |
1912 | btrfs_set_device_group(leaf, dev_item, 0); |
1913 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
1914 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
c3027eb5 | 1915 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
0b86a832 | 1916 | |
410ba3a2 | 1917 | ptr = btrfs_device_uuid(dev_item); |
e17cade2 | 1918 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
1473b24e | 1919 | ptr = btrfs_device_fsid(dev_item); |
de37aa51 NB |
1920 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
1921 | ptr, BTRFS_FSID_SIZE); | |
0b86a832 | 1922 | btrfs_mark_buffer_dirty(leaf); |
0b86a832 | 1923 | |
2b82032c | 1924 | ret = 0; |
0b86a832 CM |
1925 | out: |
1926 | btrfs_free_path(path); | |
1927 | return ret; | |
1928 | } | |
8f18cf13 | 1929 | |
5a1972bd QW |
1930 | /* |
1931 | * Function to update ctime/mtime for a given device path. | |
1932 | * Mainly used for ctime/mtime based probe like libblkid. | |
54fde91f JB |
1933 | * |
1934 | * We don't care about errors here, this is just to be kind to userspace. | |
5a1972bd | 1935 | */ |
54fde91f | 1936 | static void update_dev_time(const char *device_path) |
5a1972bd | 1937 | { |
54fde91f | 1938 | struct path path; |
54fde91f | 1939 | int ret; |
5a1972bd | 1940 | |
54fde91f JB |
1941 | ret = kern_path(device_path, LOOKUP_FOLLOW, &path); |
1942 | if (ret) | |
5a1972bd | 1943 | return; |
8f96a5bf | 1944 | |
913e9928 | 1945 | inode_update_time(d_inode(path.dentry), S_MTIME | S_CTIME | S_VERSION); |
54fde91f | 1946 | path_put(&path); |
5a1972bd QW |
1947 | } |
1948 | ||
bbac5869 QW |
1949 | static int btrfs_rm_dev_item(struct btrfs_trans_handle *trans, |
1950 | struct btrfs_device *device) | |
a061fc8d | 1951 | { |
f331a952 | 1952 | struct btrfs_root *root = device->fs_info->chunk_root; |
a061fc8d CM |
1953 | int ret; |
1954 | struct btrfs_path *path; | |
a061fc8d | 1955 | struct btrfs_key key; |
a061fc8d | 1956 | |
a061fc8d CM |
1957 | path = btrfs_alloc_path(); |
1958 | if (!path) | |
1959 | return -ENOMEM; | |
1960 | ||
a061fc8d CM |
1961 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1962 | key.type = BTRFS_DEV_ITEM_KEY; | |
1963 | key.offset = device->devid; | |
1964 | ||
2bb2e00e | 1965 | btrfs_reserve_chunk_metadata(trans, false); |
a061fc8d | 1966 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
2bb2e00e | 1967 | btrfs_trans_release_chunk_metadata(trans); |
5e9f2ad5 NB |
1968 | if (ret) { |
1969 | if (ret > 0) | |
1970 | ret = -ENOENT; | |
a061fc8d CM |
1971 | goto out; |
1972 | } | |
1973 | ||
1974 | ret = btrfs_del_item(trans, root, path); | |
a061fc8d CM |
1975 | out: |
1976 | btrfs_free_path(path); | |
a061fc8d CM |
1977 | return ret; |
1978 | } | |
1979 | ||
3cc31a0d DS |
1980 | /* |
1981 | * Verify that @num_devices satisfies the RAID profile constraints in the whole | |
1982 | * filesystem. It's up to the caller to adjust that number regarding eg. device | |
1983 | * replace. | |
1984 | */ | |
1985 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, | |
1986 | u64 num_devices) | |
a061fc8d | 1987 | { |
a061fc8d | 1988 | u64 all_avail; |
de98ced9 | 1989 | unsigned seq; |
418775a2 | 1990 | int i; |
a061fc8d | 1991 | |
de98ced9 | 1992 | do { |
bd45ffbc | 1993 | seq = read_seqbegin(&fs_info->profiles_lock); |
de98ced9 | 1994 | |
bd45ffbc AJ |
1995 | all_avail = fs_info->avail_data_alloc_bits | |
1996 | fs_info->avail_system_alloc_bits | | |
1997 | fs_info->avail_metadata_alloc_bits; | |
1998 | } while (read_seqretry(&fs_info->profiles_lock, seq)); | |
a061fc8d | 1999 | |
418775a2 | 2000 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
41a6e891 | 2001 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
418775a2 | 2002 | continue; |
a061fc8d | 2003 | |
efc222f8 AJ |
2004 | if (num_devices < btrfs_raid_array[i].devs_min) |
2005 | return btrfs_raid_array[i].mindev_error; | |
53b381b3 DW |
2006 | } |
2007 | ||
bd45ffbc | 2008 | return 0; |
f1fa7f26 AJ |
2009 | } |
2010 | ||
c9162bdf OS |
2011 | static struct btrfs_device * btrfs_find_next_active_device( |
2012 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) | |
a061fc8d | 2013 | { |
2b82032c | 2014 | struct btrfs_device *next_device; |
88acff64 AJ |
2015 | |
2016 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { | |
2017 | if (next_device != device && | |
e6e674bd AJ |
2018 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
2019 | && next_device->bdev) | |
88acff64 AJ |
2020 | return next_device; |
2021 | } | |
2022 | ||
2023 | return NULL; | |
2024 | } | |
2025 | ||
2026 | /* | |
d24fa5c1 | 2027 | * Helper function to check if the given device is part of s_bdev / latest_dev |
88acff64 AJ |
2028 | * and replace it with the provided or the next active device, in the context |
2029 | * where this function called, there should be always be another device (or | |
2030 | * this_dev) which is active. | |
2031 | */ | |
b105e927 | 2032 | void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
e493e8f9 | 2033 | struct btrfs_device *next_device) |
88acff64 | 2034 | { |
d6507cf1 | 2035 | struct btrfs_fs_info *fs_info = device->fs_info; |
88acff64 | 2036 | |
e493e8f9 | 2037 | if (!next_device) |
88acff64 | 2038 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
e493e8f9 | 2039 | device); |
88acff64 AJ |
2040 | ASSERT(next_device); |
2041 | ||
2042 | if (fs_info->sb->s_bdev && | |
2043 | (fs_info->sb->s_bdev == device->bdev)) | |
2044 | fs_info->sb->s_bdev = next_device->bdev; | |
2045 | ||
d24fa5c1 AJ |
2046 | if (fs_info->fs_devices->latest_dev->bdev == device->bdev) |
2047 | fs_info->fs_devices->latest_dev = next_device; | |
88acff64 AJ |
2048 | } |
2049 | ||
1da73967 AJ |
2050 | /* |
2051 | * Return btrfs_fs_devices::num_devices excluding the device that's being | |
2052 | * currently replaced. | |
2053 | */ | |
2054 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) | |
2055 | { | |
2056 | u64 num_devices = fs_info->fs_devices->num_devices; | |
2057 | ||
cb5583dd | 2058 | down_read(&fs_info->dev_replace.rwsem); |
1da73967 AJ |
2059 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
2060 | ASSERT(num_devices > 1); | |
2061 | num_devices--; | |
2062 | } | |
cb5583dd | 2063 | up_read(&fs_info->dev_replace.rwsem); |
1da73967 AJ |
2064 | |
2065 | return num_devices; | |
2066 | } | |
2067 | ||
0e0078f7 CH |
2068 | static void btrfs_scratch_superblock(struct btrfs_fs_info *fs_info, |
2069 | struct block_device *bdev, int copy_num) | |
2070 | { | |
2071 | struct btrfs_super_block *disk_super; | |
26ecf243 CH |
2072 | const size_t len = sizeof(disk_super->magic); |
2073 | const u64 bytenr = btrfs_sb_offset(copy_num); | |
0e0078f7 CH |
2074 | int ret; |
2075 | ||
26ecf243 | 2076 | disk_super = btrfs_read_disk_super(bdev, bytenr, bytenr); |
0e0078f7 CH |
2077 | if (IS_ERR(disk_super)) |
2078 | return; | |
2079 | ||
26ecf243 CH |
2080 | memset(&disk_super->magic, 0, len); |
2081 | folio_mark_dirty(virt_to_folio(disk_super)); | |
2082 | btrfs_release_disk_super(disk_super); | |
2083 | ||
2084 | ret = sync_blockdev_range(bdev, bytenr, bytenr + len - 1); | |
0e0078f7 CH |
2085 | if (ret) |
2086 | btrfs_warn(fs_info, "error clearing superblock number %d (%d)", | |
2087 | copy_num, ret); | |
0e0078f7 CH |
2088 | } |
2089 | ||
313b0858 JB |
2090 | void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, |
2091 | struct block_device *bdev, | |
2092 | const char *device_path) | |
6fbceb9f | 2093 | { |
6fbceb9f JT |
2094 | int copy_num; |
2095 | ||
2096 | if (!bdev) | |
2097 | return; | |
2098 | ||
2099 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) { | |
0e0078f7 | 2100 | if (bdev_is_zoned(bdev)) |
12659251 | 2101 | btrfs_reset_sb_log_zones(bdev, copy_num); |
0e0078f7 CH |
2102 | else |
2103 | btrfs_scratch_superblock(fs_info, bdev, copy_num); | |
6fbceb9f JT |
2104 | } |
2105 | ||
2106 | /* Notify udev that device has changed */ | |
2107 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); | |
2108 | ||
2109 | /* Update ctime/mtime for device path for libblkid */ | |
54fde91f | 2110 | update_dev_time(device_path); |
6fbceb9f JT |
2111 | } |
2112 | ||
1a15eb72 JB |
2113 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, |
2114 | struct btrfs_dev_lookup_args *args, | |
2736e8ee | 2115 | struct block_device **bdev, void **holder) |
f1fa7f26 | 2116 | { |
bbac5869 | 2117 | struct btrfs_trans_handle *trans; |
f1fa7f26 | 2118 | struct btrfs_device *device; |
1f78160c | 2119 | struct btrfs_fs_devices *cur_devices; |
b5185197 | 2120 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
2b82032c | 2121 | u64 num_devices; |
a061fc8d CM |
2122 | int ret = 0; |
2123 | ||
914a519b JB |
2124 | if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { |
2125 | btrfs_err(fs_info, "device remove not supported on extent tree v2 yet"); | |
2126 | return -EINVAL; | |
2127 | } | |
2128 | ||
8ef9dc0f JB |
2129 | /* |
2130 | * The device list in fs_devices is accessed without locks (neither | |
2131 | * uuid_mutex nor device_list_mutex) as it won't change on a mounted | |
2132 | * filesystem and another device rm cannot run. | |
2133 | */ | |
1da73967 | 2134 | num_devices = btrfs_num_devices(fs_info); |
8dabb742 | 2135 | |
0b246afa | 2136 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
f1fa7f26 | 2137 | if (ret) |
bbac5869 | 2138 | return ret; |
a061fc8d | 2139 | |
1a15eb72 JB |
2140 | device = btrfs_find_device(fs_info->fs_devices, args); |
2141 | if (!device) { | |
2142 | if (args->missing) | |
a27a94c2 NB |
2143 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
2144 | else | |
1a15eb72 | 2145 | ret = -ENOENT; |
bbac5869 | 2146 | return ret; |
a27a94c2 | 2147 | } |
dfe25020 | 2148 | |
eede2bf3 OS |
2149 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
2150 | btrfs_warn_in_rcu(fs_info, | |
2151 | "cannot remove device %s (devid %llu) due to active swapfile", | |
cb3e217b | 2152 | btrfs_dev_name(device), device->devid); |
bbac5869 | 2153 | return -ETXTBSY; |
eede2bf3 OS |
2154 | } |
2155 | ||
bbac5869 QW |
2156 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
2157 | return BTRFS_ERROR_DEV_TGT_REPLACE; | |
63a212ab | 2158 | |
ebbede42 | 2159 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
bbac5869 QW |
2160 | fs_info->fs_devices->rw_devices == 1) |
2161 | return BTRFS_ERROR_DEV_ONLY_WRITABLE; | |
2b82032c | 2162 | |
ebbede42 | 2163 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
34441361 | 2164 | mutex_lock(&fs_info->chunk_mutex); |
2b82032c | 2165 | list_del_init(&device->dev_alloc_list); |
c3929c36 | 2166 | device->fs_devices->rw_devices--; |
34441361 | 2167 | mutex_unlock(&fs_info->chunk_mutex); |
dfe25020 | 2168 | } |
a061fc8d CM |
2169 | |
2170 | ret = btrfs_shrink_device(device, 0); | |
2171 | if (ret) | |
9b3517e9 | 2172 | goto error_undo; |
a061fc8d | 2173 | |
bbac5869 QW |
2174 | trans = btrfs_start_transaction(fs_info->chunk_root, 0); |
2175 | if (IS_ERR(trans)) { | |
2176 | ret = PTR_ERR(trans); | |
9b3517e9 | 2177 | goto error_undo; |
bbac5869 QW |
2178 | } |
2179 | ||
2180 | ret = btrfs_rm_dev_item(trans, device); | |
2181 | if (ret) { | |
2182 | /* Any error in dev item removal is critical */ | |
2183 | btrfs_crit(fs_info, | |
2184 | "failed to remove device item for devid %llu: %d", | |
2185 | device->devid, ret); | |
2186 | btrfs_abort_transaction(trans, ret); | |
2187 | btrfs_end_transaction(trans); | |
2188 | return ret; | |
2189 | } | |
a061fc8d | 2190 | |
e12c9621 | 2191 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
163e97ee | 2192 | btrfs_scrub_cancel_dev(device); |
e5e9a520 CM |
2193 | |
2194 | /* | |
2195 | * the device list mutex makes sure that we don't change | |
2196 | * the device list while someone else is writing out all | |
d7306801 FDBM |
2197 | * the device supers. Whoever is writing all supers, should |
2198 | * lock the device list mutex before getting the number of | |
2199 | * devices in the super block (super_copy). Conversely, | |
2200 | * whoever updates the number of devices in the super block | |
2201 | * (super_copy) should hold the device list mutex. | |
e5e9a520 | 2202 | */ |
1f78160c | 2203 | |
41a52a0f AJ |
2204 | /* |
2205 | * In normal cases the cur_devices == fs_devices. But in case | |
2206 | * of deleting a seed device, the cur_devices should point to | |
9675ea8c | 2207 | * its own fs_devices listed under the fs_devices->seed_list. |
41a52a0f | 2208 | */ |
1f78160c | 2209 | cur_devices = device->fs_devices; |
b5185197 | 2210 | mutex_lock(&fs_devices->device_list_mutex); |
1f78160c | 2211 | list_del_rcu(&device->dev_list); |
e5e9a520 | 2212 | |
41a52a0f AJ |
2213 | cur_devices->num_devices--; |
2214 | cur_devices->total_devices--; | |
b4993e64 AJ |
2215 | /* Update total_devices of the parent fs_devices if it's seed */ |
2216 | if (cur_devices != fs_devices) | |
2217 | fs_devices->total_devices--; | |
2b82032c | 2218 | |
e6e674bd | 2219 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
41a52a0f | 2220 | cur_devices->missing_devices--; |
cd02dca5 | 2221 | |
d6507cf1 | 2222 | btrfs_assign_next_active_device(device, NULL); |
2b82032c | 2223 | |
0bfaa9c5 | 2224 | if (device->bdev) { |
41a52a0f | 2225 | cur_devices->open_devices--; |
0bfaa9c5 | 2226 | /* remove sysfs entry */ |
53f8a74c | 2227 | btrfs_sysfs_remove_device(device); |
0bfaa9c5 | 2228 | } |
99994cde | 2229 | |
0b246afa JM |
2230 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
2231 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); | |
b5185197 | 2232 | mutex_unlock(&fs_devices->device_list_mutex); |
2b82032c | 2233 | |
cea67ab9 | 2234 | /* |
3fa421de JB |
2235 | * At this point, the device is zero sized and detached from the |
2236 | * devices list. All that's left is to zero out the old supers and | |
2237 | * free the device. | |
2238 | * | |
2239 | * We cannot call btrfs_close_bdev() here because we're holding the sb | |
2240 | * write lock, and blkdev_put() will pull in the ->open_mutex on the | |
2241 | * block device and it's dependencies. Instead just flush the device | |
2242 | * and let the caller do the final blkdev_put. | |
cea67ab9 | 2243 | */ |
3fa421de | 2244 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
8f32380d JT |
2245 | btrfs_scratch_superblocks(fs_info, device->bdev, |
2246 | device->name->str); | |
3fa421de JB |
2247 | if (device->bdev) { |
2248 | sync_blockdev(device->bdev); | |
2249 | invalidate_bdev(device->bdev); | |
2250 | } | |
2251 | } | |
cea67ab9 | 2252 | |
3fa421de | 2253 | *bdev = device->bdev; |
2736e8ee | 2254 | *holder = device->holder; |
8e75fd89 NB |
2255 | synchronize_rcu(); |
2256 | btrfs_free_device(device); | |
cea67ab9 | 2257 | |
8b41393f JB |
2258 | /* |
2259 | * This can happen if cur_devices is the private seed devices list. We | |
2260 | * cannot call close_fs_devices() here because it expects the uuid_mutex | |
2261 | * to be held, but in fact we don't need that for the private | |
2262 | * seed_devices, we can simply decrement cur_devices->opened and then | |
2263 | * remove it from our list and free the fs_devices. | |
2264 | */ | |
8e906945 | 2265 | if (cur_devices->num_devices == 0) { |
944d3f9f | 2266 | list_del_init(&cur_devices->seed_list); |
8b41393f JB |
2267 | ASSERT(cur_devices->opened == 1); |
2268 | cur_devices->opened--; | |
1f78160c | 2269 | free_fs_devices(cur_devices); |
2b82032c YZ |
2270 | } |
2271 | ||
bbac5869 QW |
2272 | ret = btrfs_commit_transaction(trans); |
2273 | ||
a061fc8d | 2274 | return ret; |
24fc572f | 2275 | |
9b3517e9 | 2276 | error_undo: |
ebbede42 | 2277 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
34441361 | 2278 | mutex_lock(&fs_info->chunk_mutex); |
9b3517e9 | 2279 | list_add(&device->dev_alloc_list, |
b5185197 | 2280 | &fs_devices->alloc_list); |
c3929c36 | 2281 | device->fs_devices->rw_devices++; |
34441361 | 2282 | mutex_unlock(&fs_info->chunk_mutex); |
9b3517e9 | 2283 | } |
bbac5869 | 2284 | return ret; |
a061fc8d CM |
2285 | } |
2286 | ||
68a9db5f | 2287 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
e93c89c1 | 2288 | { |
d51908ce AJ |
2289 | struct btrfs_fs_devices *fs_devices; |
2290 | ||
68a9db5f | 2291 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
1357272f | 2292 | |
25e8e911 AJ |
2293 | /* |
2294 | * in case of fs with no seed, srcdev->fs_devices will point | |
2295 | * to fs_devices of fs_info. However when the dev being replaced is | |
2296 | * a seed dev it will point to the seed's local fs_devices. In short | |
2297 | * srcdev will have its correct fs_devices in both the cases. | |
2298 | */ | |
2299 | fs_devices = srcdev->fs_devices; | |
d51908ce | 2300 | |
e93c89c1 | 2301 | list_del_rcu(&srcdev->dev_list); |
619c47f3 | 2302 | list_del(&srcdev->dev_alloc_list); |
d51908ce | 2303 | fs_devices->num_devices--; |
e6e674bd | 2304 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
d51908ce | 2305 | fs_devices->missing_devices--; |
e93c89c1 | 2306 | |
ebbede42 | 2307 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
82372bc8 | 2308 | fs_devices->rw_devices--; |
1357272f | 2309 | |
82372bc8 | 2310 | if (srcdev->bdev) |
d51908ce | 2311 | fs_devices->open_devices--; |
084b6e7c QW |
2312 | } |
2313 | ||
65237ee3 | 2314 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
084b6e7c QW |
2315 | { |
2316 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; | |
e93c89c1 | 2317 | |
a466c85e JB |
2318 | mutex_lock(&uuid_mutex); |
2319 | ||
14238819 | 2320 | btrfs_close_bdev(srcdev); |
8e75fd89 NB |
2321 | synchronize_rcu(); |
2322 | btrfs_free_device(srcdev); | |
94d5f0c2 | 2323 | |
94d5f0c2 AJ |
2324 | /* if this is no devs we rather delete the fs_devices */ |
2325 | if (!fs_devices->num_devices) { | |
6dd38f81 AJ |
2326 | /* |
2327 | * On a mounted FS, num_devices can't be zero unless it's a | |
2328 | * seed. In case of a seed device being replaced, the replace | |
2329 | * target added to the sprout FS, so there will be no more | |
2330 | * device left under the seed FS. | |
2331 | */ | |
2332 | ASSERT(fs_devices->seeding); | |
2333 | ||
944d3f9f | 2334 | list_del_init(&fs_devices->seed_list); |
0226e0eb | 2335 | close_fs_devices(fs_devices); |
8bef8401 | 2336 | free_fs_devices(fs_devices); |
94d5f0c2 | 2337 | } |
a466c85e | 2338 | mutex_unlock(&uuid_mutex); |
e93c89c1 SB |
2339 | } |
2340 | ||
4f5ad7bd | 2341 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
e93c89c1 | 2342 | { |
4f5ad7bd | 2343 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
d9a071f0 | 2344 | |
d9a071f0 | 2345 | mutex_lock(&fs_devices->device_list_mutex); |
d2ff1b20 | 2346 | |
53f8a74c | 2347 | btrfs_sysfs_remove_device(tgtdev); |
d2ff1b20 | 2348 | |
779bf3fe | 2349 | if (tgtdev->bdev) |
d9a071f0 | 2350 | fs_devices->open_devices--; |
779bf3fe | 2351 | |
d9a071f0 | 2352 | fs_devices->num_devices--; |
e93c89c1 | 2353 | |
d6507cf1 | 2354 | btrfs_assign_next_active_device(tgtdev, NULL); |
e93c89c1 | 2355 | |
e93c89c1 | 2356 | list_del_rcu(&tgtdev->dev_list); |
e93c89c1 | 2357 | |
d9a071f0 | 2358 | mutex_unlock(&fs_devices->device_list_mutex); |
779bf3fe | 2359 | |
8f32380d JT |
2360 | btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev, |
2361 | tgtdev->name->str); | |
14238819 AJ |
2362 | |
2363 | btrfs_close_bdev(tgtdev); | |
8e75fd89 NB |
2364 | synchronize_rcu(); |
2365 | btrfs_free_device(tgtdev); | |
e93c89c1 SB |
2366 | } |
2367 | ||
43dd529a DS |
2368 | /* |
2369 | * Populate args from device at path. | |
faa775c4 JB |
2370 | * |
2371 | * @fs_info: the filesystem | |
2372 | * @args: the args to populate | |
2373 | * @path: the path to the device | |
2374 | * | |
2375 | * This will read the super block of the device at @path and populate @args with | |
2376 | * the devid, fsid, and uuid. This is meant to be used for ioctls that need to | |
2377 | * lookup a device to operate on, but need to do it before we take any locks. | |
2378 | * This properly handles the special case of "missing" that a user may pass in, | |
2379 | * and does some basic sanity checks. The caller must make sure that @path is | |
2380 | * properly NUL terminated before calling in, and must call | |
2381 | * btrfs_put_dev_args_from_path() in order to free up the temporary fsid and | |
2382 | * uuid buffers. | |
2383 | * | |
2384 | * Return: 0 for success, -errno for failure | |
2385 | */ | |
2386 | int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info, | |
2387 | struct btrfs_dev_lookup_args *args, | |
2388 | const char *path) | |
7ba15b7d | 2389 | { |
7ba15b7d | 2390 | struct btrfs_super_block *disk_super; |
7ba15b7d | 2391 | struct block_device *bdev; |
faa775c4 | 2392 | int ret; |
7ba15b7d | 2393 | |
faa775c4 JB |
2394 | if (!path || !path[0]) |
2395 | return -EINVAL; | |
2396 | if (!strcmp(path, "missing")) { | |
2397 | args->missing = true; | |
2398 | return 0; | |
2399 | } | |
8f32380d | 2400 | |
faa775c4 JB |
2401 | args->uuid = kzalloc(BTRFS_UUID_SIZE, GFP_KERNEL); |
2402 | args->fsid = kzalloc(BTRFS_FSID_SIZE, GFP_KERNEL); | |
2403 | if (!args->uuid || !args->fsid) { | |
2404 | btrfs_put_dev_args_from_path(args); | |
2405 | return -ENOMEM; | |
2406 | } | |
8f32380d | 2407 | |
05bdb996 | 2408 | ret = btrfs_get_bdev_and_sb(path, BLK_OPEN_READ, NULL, 0, |
faa775c4 | 2409 | &bdev, &disk_super); |
9ea0106a ZF |
2410 | if (ret) { |
2411 | btrfs_put_dev_args_from_path(args); | |
faa775c4 | 2412 | return ret; |
9ea0106a ZF |
2413 | } |
2414 | ||
faa775c4 JB |
2415 | args->devid = btrfs_stack_device_id(&disk_super->dev_item); |
2416 | memcpy(args->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE); | |
7239ff4b | 2417 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
faa775c4 | 2418 | memcpy(args->fsid, disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
7239ff4b | 2419 | else |
faa775c4 | 2420 | memcpy(args->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
8f32380d | 2421 | btrfs_release_disk_super(disk_super); |
2736e8ee | 2422 | blkdev_put(bdev, NULL); |
faa775c4 | 2423 | return 0; |
7ba15b7d SB |
2424 | } |
2425 | ||
5c5c0df0 | 2426 | /* |
faa775c4 JB |
2427 | * Only use this jointly with btrfs_get_dev_args_from_path() because we will |
2428 | * allocate our ->uuid and ->fsid pointers, everybody else uses local variables | |
2429 | * that don't need to be freed. | |
5c5c0df0 | 2430 | */ |
faa775c4 JB |
2431 | void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args) |
2432 | { | |
2433 | kfree(args->uuid); | |
2434 | kfree(args->fsid); | |
2435 | args->uuid = NULL; | |
2436 | args->fsid = NULL; | |
2437 | } | |
2438 | ||
a27a94c2 | 2439 | struct btrfs_device *btrfs_find_device_by_devspec( |
6e927ceb AJ |
2440 | struct btrfs_fs_info *fs_info, u64 devid, |
2441 | const char *device_path) | |
24e0474b | 2442 | { |
562d7b15 | 2443 | BTRFS_DEV_LOOKUP_ARGS(args); |
a27a94c2 | 2444 | struct btrfs_device *device; |
faa775c4 | 2445 | int ret; |
24e0474b | 2446 | |
5c5c0df0 | 2447 | if (devid) { |
562d7b15 JB |
2448 | args.devid = devid; |
2449 | device = btrfs_find_device(fs_info->fs_devices, &args); | |
a27a94c2 NB |
2450 | if (!device) |
2451 | return ERR_PTR(-ENOENT); | |
6e927ceb AJ |
2452 | return device; |
2453 | } | |
2454 | ||
faa775c4 JB |
2455 | ret = btrfs_get_dev_args_from_path(fs_info, &args, device_path); |
2456 | if (ret) | |
2457 | return ERR_PTR(ret); | |
2458 | device = btrfs_find_device(fs_info->fs_devices, &args); | |
2459 | btrfs_put_dev_args_from_path(&args); | |
2460 | if (!device) | |
6e927ceb | 2461 | return ERR_PTR(-ENOENT); |
faa775c4 | 2462 | return device; |
24e0474b AJ |
2463 | } |
2464 | ||
849eae5e | 2465 | static struct btrfs_fs_devices *btrfs_init_sprout(struct btrfs_fs_info *fs_info) |
2b82032c | 2466 | { |
0b246afa | 2467 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
2b82032c | 2468 | struct btrfs_fs_devices *old_devices; |
e4404d6e | 2469 | struct btrfs_fs_devices *seed_devices; |
2b82032c | 2470 | |
a32bf9a3 | 2471 | lockdep_assert_held(&uuid_mutex); |
e4404d6e | 2472 | if (!fs_devices->seeding) |
849eae5e | 2473 | return ERR_PTR(-EINVAL); |
2b82032c | 2474 | |
427c8fdd NB |
2475 | /* |
2476 | * Private copy of the seed devices, anchored at | |
2477 | * fs_info->fs_devices->seed_list | |
2478 | */ | |
f7361d8c | 2479 | seed_devices = alloc_fs_devices(NULL); |
2208a378 | 2480 | if (IS_ERR(seed_devices)) |
849eae5e | 2481 | return seed_devices; |
2b82032c | 2482 | |
427c8fdd NB |
2483 | /* |
2484 | * It's necessary to retain a copy of the original seed fs_devices in | |
2485 | * fs_uuids so that filesystems which have been seeded can successfully | |
2486 | * reference the seed device from open_seed_devices. This also supports | |
2487 | * multiple fs seed. | |
2488 | */ | |
e4404d6e YZ |
2489 | old_devices = clone_fs_devices(fs_devices); |
2490 | if (IS_ERR(old_devices)) { | |
2491 | kfree(seed_devices); | |
849eae5e | 2492 | return old_devices; |
2b82032c | 2493 | } |
e4404d6e | 2494 | |
c4babc5e | 2495 | list_add(&old_devices->fs_list, &fs_uuids); |
2b82032c | 2496 | |
e4404d6e YZ |
2497 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
2498 | seed_devices->opened = 1; | |
2499 | INIT_LIST_HEAD(&seed_devices->devices); | |
2500 | INIT_LIST_HEAD(&seed_devices->alloc_list); | |
e5e9a520 | 2501 | mutex_init(&seed_devices->device_list_mutex); |
c9513edb | 2502 | |
849eae5e AJ |
2503 | return seed_devices; |
2504 | } | |
2505 | ||
2506 | /* | |
2507 | * Splice seed devices into the sprout fs_devices. | |
2508 | * Generate a new fsid for the sprouted read-write filesystem. | |
2509 | */ | |
2510 | static void btrfs_setup_sprout(struct btrfs_fs_info *fs_info, | |
2511 | struct btrfs_fs_devices *seed_devices) | |
2512 | { | |
2513 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
2514 | struct btrfs_super_block *disk_super = fs_info->super_copy; | |
2515 | struct btrfs_device *device; | |
2516 | u64 super_flags; | |
2517 | ||
2518 | /* | |
2519 | * We are updating the fsid, the thread leading to device_list_add() | |
2520 | * could race, so uuid_mutex is needed. | |
2521 | */ | |
2522 | lockdep_assert_held(&uuid_mutex); | |
2523 | ||
2524 | /* | |
2525 | * The threads listed below may traverse dev_list but can do that without | |
2526 | * device_list_mutex: | |
2527 | * - All device ops and balance - as we are in btrfs_exclop_start. | |
2528 | * - Various dev_list readers - are using RCU. | |
2529 | * - btrfs_ioctl_fitrim() - is using RCU. | |
2530 | * | |
2531 | * For-read threads as below are using device_list_mutex: | |
2532 | * - Readonly scrub btrfs_scrub_dev() | |
2533 | * - Readonly scrub btrfs_scrub_progress() | |
2534 | * - btrfs_get_dev_stats() | |
2535 | */ | |
2536 | lockdep_assert_held(&fs_devices->device_list_mutex); | |
2537 | ||
1f78160c XG |
2538 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
2539 | synchronize_rcu); | |
2196d6e8 MX |
2540 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
2541 | device->fs_devices = seed_devices; | |
c9513edb | 2542 | |
0395d84f | 2543 | fs_devices->seeding = false; |
2b82032c YZ |
2544 | fs_devices->num_devices = 0; |
2545 | fs_devices->open_devices = 0; | |
69611ac8 | 2546 | fs_devices->missing_devices = 0; |
7f0432d0 | 2547 | fs_devices->rotating = false; |
944d3f9f | 2548 | list_add(&seed_devices->seed_list, &fs_devices->seed_list); |
2b82032c YZ |
2549 | |
2550 | generate_random_uuid(fs_devices->fsid); | |
7239ff4b | 2551 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
2b82032c | 2552 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
f7171750 | 2553 | |
2b82032c YZ |
2554 | super_flags = btrfs_super_flags(disk_super) & |
2555 | ~BTRFS_SUPER_FLAG_SEEDING; | |
2556 | btrfs_set_super_flags(disk_super, super_flags); | |
2b82032c YZ |
2557 | } |
2558 | ||
2559 | /* | |
01327610 | 2560 | * Store the expected generation for seed devices in device items. |
2b82032c | 2561 | */ |
5c466629 | 2562 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
2b82032c | 2563 | { |
562d7b15 | 2564 | BTRFS_DEV_LOOKUP_ARGS(args); |
5c466629 | 2565 | struct btrfs_fs_info *fs_info = trans->fs_info; |
5b4aacef | 2566 | struct btrfs_root *root = fs_info->chunk_root; |
2b82032c YZ |
2567 | struct btrfs_path *path; |
2568 | struct extent_buffer *leaf; | |
2569 | struct btrfs_dev_item *dev_item; | |
2570 | struct btrfs_device *device; | |
2571 | struct btrfs_key key; | |
44880fdc | 2572 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
2b82032c | 2573 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
2b82032c YZ |
2574 | int ret; |
2575 | ||
2576 | path = btrfs_alloc_path(); | |
2577 | if (!path) | |
2578 | return -ENOMEM; | |
2579 | ||
2b82032c YZ |
2580 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
2581 | key.offset = 0; | |
2582 | key.type = BTRFS_DEV_ITEM_KEY; | |
2583 | ||
2584 | while (1) { | |
2bb2e00e | 2585 | btrfs_reserve_chunk_metadata(trans, false); |
2b82032c | 2586 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
2bb2e00e | 2587 | btrfs_trans_release_chunk_metadata(trans); |
2b82032c YZ |
2588 | if (ret < 0) |
2589 | goto error; | |
2590 | ||
2591 | leaf = path->nodes[0]; | |
2592 | next_slot: | |
2593 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
2594 | ret = btrfs_next_leaf(root, path); | |
2595 | if (ret > 0) | |
2596 | break; | |
2597 | if (ret < 0) | |
2598 | goto error; | |
2599 | leaf = path->nodes[0]; | |
2600 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 2601 | btrfs_release_path(path); |
2b82032c YZ |
2602 | continue; |
2603 | } | |
2604 | ||
2605 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2606 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || | |
2607 | key.type != BTRFS_DEV_ITEM_KEY) | |
2608 | break; | |
2609 | ||
2610 | dev_item = btrfs_item_ptr(leaf, path->slots[0], | |
2611 | struct btrfs_dev_item); | |
562d7b15 | 2612 | args.devid = btrfs_device_id(leaf, dev_item); |
410ba3a2 | 2613 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
2b82032c | 2614 | BTRFS_UUID_SIZE); |
1473b24e | 2615 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
44880fdc | 2616 | BTRFS_FSID_SIZE); |
562d7b15 JB |
2617 | args.uuid = dev_uuid; |
2618 | args.fsid = fs_uuid; | |
2619 | device = btrfs_find_device(fs_info->fs_devices, &args); | |
79787eaa | 2620 | BUG_ON(!device); /* Logic error */ |
2b82032c YZ |
2621 | |
2622 | if (device->fs_devices->seeding) { | |
2623 | btrfs_set_device_generation(leaf, dev_item, | |
2624 | device->generation); | |
2625 | btrfs_mark_buffer_dirty(leaf); | |
2626 | } | |
2627 | ||
2628 | path->slots[0]++; | |
2629 | goto next_slot; | |
2630 | } | |
2631 | ret = 0; | |
2632 | error: | |
2633 | btrfs_free_path(path); | |
2634 | return ret; | |
2635 | } | |
2636 | ||
da353f6b | 2637 | int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path) |
788f20eb | 2638 | { |
5112febb | 2639 | struct btrfs_root *root = fs_info->dev_root; |
788f20eb CM |
2640 | struct btrfs_trans_handle *trans; |
2641 | struct btrfs_device *device; | |
2642 | struct block_device *bdev; | |
0b246afa | 2643 | struct super_block *sb = fs_info->sb; |
5da54bc1 | 2644 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
8ba7d5f5 | 2645 | struct btrfs_fs_devices *seed_devices = NULL; |
39379faa NA |
2646 | u64 orig_super_total_bytes; |
2647 | u64 orig_super_num_devices; | |
788f20eb | 2648 | int ret = 0; |
fd880809 | 2649 | bool seeding_dev = false; |
44cab9ba | 2650 | bool locked = false; |
788f20eb | 2651 | |
5da54bc1 | 2652 | if (sb_rdonly(sb) && !fs_devices->seeding) |
f8c5d0b4 | 2653 | return -EROFS; |
788f20eb | 2654 | |
05bdb996 | 2655 | bdev = blkdev_get_by_path(device_path, BLK_OPEN_WRITE, |
0718afd4 | 2656 | fs_info->bdev_holder, NULL); |
7f59203a JB |
2657 | if (IS_ERR(bdev)) |
2658 | return PTR_ERR(bdev); | |
a2135011 | 2659 | |
b70f5097 NA |
2660 | if (!btrfs_check_device_zone_type(fs_info, bdev)) { |
2661 | ret = -EINVAL; | |
2662 | goto error; | |
2663 | } | |
2664 | ||
5da54bc1 | 2665 | if (fs_devices->seeding) { |
fd880809 | 2666 | seeding_dev = true; |
2b82032c YZ |
2667 | down_write(&sb->s_umount); |
2668 | mutex_lock(&uuid_mutex); | |
44cab9ba | 2669 | locked = true; |
2b82032c YZ |
2670 | } |
2671 | ||
b9ba017f | 2672 | sync_blockdev(bdev); |
a2135011 | 2673 | |
f4cfa9bd NB |
2674 | rcu_read_lock(); |
2675 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { | |
788f20eb CM |
2676 | if (device->bdev == bdev) { |
2677 | ret = -EEXIST; | |
f4cfa9bd | 2678 | rcu_read_unlock(); |
2b82032c | 2679 | goto error; |
788f20eb CM |
2680 | } |
2681 | } | |
f4cfa9bd | 2682 | rcu_read_unlock(); |
788f20eb | 2683 | |
bb21e302 | 2684 | device = btrfs_alloc_device(fs_info, NULL, NULL, device_path); |
12bd2fc0 | 2685 | if (IS_ERR(device)) { |
788f20eb | 2686 | /* we can safely leave the fs_devices entry around */ |
12bd2fc0 | 2687 | ret = PTR_ERR(device); |
2b82032c | 2688 | goto error; |
788f20eb CM |
2689 | } |
2690 | ||
5b316468 NA |
2691 | device->fs_info = fs_info; |
2692 | device->bdev = bdev; | |
4889bc05 AJ |
2693 | ret = lookup_bdev(device_path, &device->devt); |
2694 | if (ret) | |
2695 | goto error_free_device; | |
5b316468 | 2696 | |
16beac87 | 2697 | ret = btrfs_get_dev_zone_info(device, false); |
5b316468 NA |
2698 | if (ret) |
2699 | goto error_free_device; | |
2700 | ||
a22285a6 | 2701 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 2702 | if (IS_ERR(trans)) { |
98d5dc13 | 2703 | ret = PTR_ERR(trans); |
5b316468 | 2704 | goto error_free_zone; |
98d5dc13 TI |
2705 | } |
2706 | ||
ebbede42 | 2707 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
2b82032c | 2708 | device->generation = trans->transid; |
0b246afa JM |
2709 | device->io_width = fs_info->sectorsize; |
2710 | device->io_align = fs_info->sectorsize; | |
2711 | device->sector_size = fs_info->sectorsize; | |
cda00eba CH |
2712 | device->total_bytes = |
2713 | round_down(bdev_nr_bytes(bdev), fs_info->sectorsize); | |
2cc3c559 | 2714 | device->disk_total_bytes = device->total_bytes; |
935e5cc9 | 2715 | device->commit_total_bytes = device->total_bytes; |
e12c9621 | 2716 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
401e29c1 | 2717 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
2736e8ee | 2718 | device->holder = fs_info->bdev_holder; |
27087f37 | 2719 | device->dev_stats_valid = 1; |
9f6d2510 | 2720 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
788f20eb | 2721 | |
2b82032c | 2722 | if (seeding_dev) { |
a0a1db70 | 2723 | btrfs_clear_sb_rdonly(sb); |
849eae5e AJ |
2724 | |
2725 | /* GFP_KERNEL allocation must not be under device_list_mutex */ | |
2726 | seed_devices = btrfs_init_sprout(fs_info); | |
2727 | if (IS_ERR(seed_devices)) { | |
2728 | ret = PTR_ERR(seed_devices); | |
d31c32f6 AJ |
2729 | btrfs_abort_transaction(trans, ret); |
2730 | goto error_trans; | |
2731 | } | |
849eae5e AJ |
2732 | } |
2733 | ||
2734 | mutex_lock(&fs_devices->device_list_mutex); | |
2735 | if (seeding_dev) { | |
2736 | btrfs_setup_sprout(fs_info, seed_devices); | |
b7cb29e6 AJ |
2737 | btrfs_assign_next_active_device(fs_info->fs_devices->latest_dev, |
2738 | device); | |
2b82032c | 2739 | } |
788f20eb | 2740 | |
5da54bc1 | 2741 | device->fs_devices = fs_devices; |
e5e9a520 | 2742 | |
34441361 | 2743 | mutex_lock(&fs_info->chunk_mutex); |
5da54bc1 AJ |
2744 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
2745 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); | |
2746 | fs_devices->num_devices++; | |
2747 | fs_devices->open_devices++; | |
2748 | fs_devices->rw_devices++; | |
2749 | fs_devices->total_devices++; | |
2750 | fs_devices->total_rw_bytes += device->total_bytes; | |
325cd4ba | 2751 | |
a5ed45f8 | 2752 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
2bf64758 | 2753 | |
10f0d2a5 | 2754 | if (!bdev_nonrot(bdev)) |
7f0432d0 | 2755 | fs_devices->rotating = true; |
c289811c | 2756 | |
39379faa | 2757 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
0b246afa | 2758 | btrfs_set_super_total_bytes(fs_info->super_copy, |
39379faa NA |
2759 | round_down(orig_super_total_bytes + device->total_bytes, |
2760 | fs_info->sectorsize)); | |
788f20eb | 2761 | |
39379faa NA |
2762 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
2763 | btrfs_set_super_num_devices(fs_info->super_copy, | |
2764 | orig_super_num_devices + 1); | |
0d39376a | 2765 | |
2196d6e8 MX |
2766 | /* |
2767 | * we've got more storage, clear any full flags on the space | |
2768 | * infos | |
2769 | */ | |
0b246afa | 2770 | btrfs_clear_space_info_full(fs_info); |
2196d6e8 | 2771 | |
34441361 | 2772 | mutex_unlock(&fs_info->chunk_mutex); |
ca10845a JB |
2773 | |
2774 | /* Add sysfs device entry */ | |
cd36da2e | 2775 | btrfs_sysfs_add_device(device); |
ca10845a | 2776 | |
5da54bc1 | 2777 | mutex_unlock(&fs_devices->device_list_mutex); |
788f20eb | 2778 | |
2b82032c | 2779 | if (seeding_dev) { |
34441361 | 2780 | mutex_lock(&fs_info->chunk_mutex); |
6f8e0fc7 | 2781 | ret = init_first_rw_device(trans); |
34441361 | 2782 | mutex_unlock(&fs_info->chunk_mutex); |
005d6427 | 2783 | if (ret) { |
66642832 | 2784 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2785 | goto error_sysfs; |
005d6427 | 2786 | } |
2196d6e8 MX |
2787 | } |
2788 | ||
8e87e856 | 2789 | ret = btrfs_add_dev_item(trans, device); |
2196d6e8 | 2790 | if (ret) { |
66642832 | 2791 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2792 | goto error_sysfs; |
2196d6e8 MX |
2793 | } |
2794 | ||
2795 | if (seeding_dev) { | |
5c466629 | 2796 | ret = btrfs_finish_sprout(trans); |
005d6427 | 2797 | if (ret) { |
66642832 | 2798 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2799 | goto error_sysfs; |
005d6427 | 2800 | } |
b2373f25 | 2801 | |
8e560081 NB |
2802 | /* |
2803 | * fs_devices now represents the newly sprouted filesystem and | |
849eae5e | 2804 | * its fsid has been changed by btrfs_sprout_splice(). |
8e560081 NB |
2805 | */ |
2806 | btrfs_sysfs_update_sprout_fsid(fs_devices); | |
2b82032c YZ |
2807 | } |
2808 | ||
3a45bb20 | 2809 | ret = btrfs_commit_transaction(trans); |
a2135011 | 2810 | |
2b82032c YZ |
2811 | if (seeding_dev) { |
2812 | mutex_unlock(&uuid_mutex); | |
2813 | up_write(&sb->s_umount); | |
44cab9ba | 2814 | locked = false; |
788f20eb | 2815 | |
79787eaa JM |
2816 | if (ret) /* transaction commit */ |
2817 | return ret; | |
2818 | ||
2ff7e61e | 2819 | ret = btrfs_relocate_sys_chunks(fs_info); |
79787eaa | 2820 | if (ret < 0) |
0b246afa | 2821 | btrfs_handle_fs_error(fs_info, ret, |
5d163e0e | 2822 | "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); |
671415b7 MX |
2823 | trans = btrfs_attach_transaction(root); |
2824 | if (IS_ERR(trans)) { | |
2825 | if (PTR_ERR(trans) == -ENOENT) | |
2826 | return 0; | |
7132a262 AJ |
2827 | ret = PTR_ERR(trans); |
2828 | trans = NULL; | |
2829 | goto error_sysfs; | |
671415b7 | 2830 | } |
3a45bb20 | 2831 | ret = btrfs_commit_transaction(trans); |
2b82032c | 2832 | } |
c9e9f97b | 2833 | |
7f551d96 AJ |
2834 | /* |
2835 | * Now that we have written a new super block to this device, check all | |
2836 | * other fs_devices list if device_path alienates any other scanned | |
2837 | * device. | |
2838 | * We can ignore the return value as it typically returns -EINVAL and | |
2839 | * only succeeds if the device was an alien. | |
2840 | */ | |
4889bc05 | 2841 | btrfs_forget_devices(device->devt); |
7f551d96 AJ |
2842 | |
2843 | /* Update ctime/mtime for blkid or udev */ | |
54fde91f | 2844 | update_dev_time(device_path); |
7f551d96 | 2845 | |
2b82032c | 2846 | return ret; |
79787eaa | 2847 | |
d31c32f6 | 2848 | error_sysfs: |
53f8a74c | 2849 | btrfs_sysfs_remove_device(device); |
39379faa NA |
2850 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
2851 | mutex_lock(&fs_info->chunk_mutex); | |
2852 | list_del_rcu(&device->dev_list); | |
2853 | list_del(&device->dev_alloc_list); | |
2854 | fs_info->fs_devices->num_devices--; | |
2855 | fs_info->fs_devices->open_devices--; | |
2856 | fs_info->fs_devices->rw_devices--; | |
2857 | fs_info->fs_devices->total_devices--; | |
2858 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; | |
2859 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); | |
2860 | btrfs_set_super_total_bytes(fs_info->super_copy, | |
2861 | orig_super_total_bytes); | |
2862 | btrfs_set_super_num_devices(fs_info->super_copy, | |
2863 | orig_super_num_devices); | |
2864 | mutex_unlock(&fs_info->chunk_mutex); | |
2865 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
79787eaa | 2866 | error_trans: |
0af2c4bf | 2867 | if (seeding_dev) |
a0a1db70 | 2868 | btrfs_set_sb_rdonly(sb); |
7132a262 AJ |
2869 | if (trans) |
2870 | btrfs_end_transaction(trans); | |
5b316468 NA |
2871 | error_free_zone: |
2872 | btrfs_destroy_dev_zone_info(device); | |
5c4cf6c9 | 2873 | error_free_device: |
a425f9d4 | 2874 | btrfs_free_device(device); |
2b82032c | 2875 | error: |
2736e8ee | 2876 | blkdev_put(bdev, fs_info->bdev_holder); |
44cab9ba | 2877 | if (locked) { |
2b82032c YZ |
2878 | mutex_unlock(&uuid_mutex); |
2879 | up_write(&sb->s_umount); | |
2880 | } | |
c9e9f97b | 2881 | return ret; |
788f20eb CM |
2882 | } |
2883 | ||
d397712b CM |
2884 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
2885 | struct btrfs_device *device) | |
0b86a832 CM |
2886 | { |
2887 | int ret; | |
2888 | struct btrfs_path *path; | |
0b246afa | 2889 | struct btrfs_root *root = device->fs_info->chunk_root; |
0b86a832 CM |
2890 | struct btrfs_dev_item *dev_item; |
2891 | struct extent_buffer *leaf; | |
2892 | struct btrfs_key key; | |
2893 | ||
0b86a832 CM |
2894 | path = btrfs_alloc_path(); |
2895 | if (!path) | |
2896 | return -ENOMEM; | |
2897 | ||
2898 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
2899 | key.type = BTRFS_DEV_ITEM_KEY; | |
2900 | key.offset = device->devid; | |
2901 | ||
2902 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
2903 | if (ret < 0) | |
2904 | goto out; | |
2905 | ||
2906 | if (ret > 0) { | |
2907 | ret = -ENOENT; | |
2908 | goto out; | |
2909 | } | |
2910 | ||
2911 | leaf = path->nodes[0]; | |
2912 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
2913 | ||
2914 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2915 | btrfs_set_device_type(leaf, dev_item, device->type); | |
2916 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
2917 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
2918 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
7cc8e58d MX |
2919 | btrfs_set_device_total_bytes(leaf, dev_item, |
2920 | btrfs_device_get_disk_total_bytes(device)); | |
2921 | btrfs_set_device_bytes_used(leaf, dev_item, | |
2922 | btrfs_device_get_bytes_used(device)); | |
0b86a832 CM |
2923 | btrfs_mark_buffer_dirty(leaf); |
2924 | ||
2925 | out: | |
2926 | btrfs_free_path(path); | |
2927 | return ret; | |
2928 | } | |
2929 | ||
2196d6e8 | 2930 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
2931 | struct btrfs_device *device, u64 new_size) |
2932 | { | |
0b246afa JM |
2933 | struct btrfs_fs_info *fs_info = device->fs_info; |
2934 | struct btrfs_super_block *super_copy = fs_info->super_copy; | |
2196d6e8 MX |
2935 | u64 old_total; |
2936 | u64 diff; | |
2bb2e00e | 2937 | int ret; |
8f18cf13 | 2938 | |
ebbede42 | 2939 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
2b82032c | 2940 | return -EACCES; |
2196d6e8 | 2941 | |
7dfb8be1 NB |
2942 | new_size = round_down(new_size, fs_info->sectorsize); |
2943 | ||
34441361 | 2944 | mutex_lock(&fs_info->chunk_mutex); |
2196d6e8 | 2945 | old_total = btrfs_super_total_bytes(super_copy); |
0e4324a4 | 2946 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
2196d6e8 | 2947 | |
63a212ab | 2948 | if (new_size <= device->total_bytes || |
401e29c1 | 2949 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
34441361 | 2950 | mutex_unlock(&fs_info->chunk_mutex); |
2b82032c | 2951 | return -EINVAL; |
2196d6e8 | 2952 | } |
2b82032c | 2953 | |
7dfb8be1 NB |
2954 | btrfs_set_super_total_bytes(super_copy, |
2955 | round_down(old_total + diff, fs_info->sectorsize)); | |
2b82032c YZ |
2956 | device->fs_devices->total_rw_bytes += diff; |
2957 | ||
7cc8e58d MX |
2958 | btrfs_device_set_total_bytes(device, new_size); |
2959 | btrfs_device_set_disk_total_bytes(device, new_size); | |
fb456252 | 2960 | btrfs_clear_space_info_full(device->fs_info); |
bbbf7243 NB |
2961 | if (list_empty(&device->post_commit_list)) |
2962 | list_add_tail(&device->post_commit_list, | |
2963 | &trans->transaction->dev_update_list); | |
34441361 | 2964 | mutex_unlock(&fs_info->chunk_mutex); |
4184ea7f | 2965 | |
2bb2e00e FM |
2966 | btrfs_reserve_chunk_metadata(trans, false); |
2967 | ret = btrfs_update_device(trans, device); | |
2968 | btrfs_trans_release_chunk_metadata(trans); | |
2969 | ||
2970 | return ret; | |
8f18cf13 CM |
2971 | } |
2972 | ||
f4208794 | 2973 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
8f18cf13 | 2974 | { |
f4208794 | 2975 | struct btrfs_fs_info *fs_info = trans->fs_info; |
5b4aacef | 2976 | struct btrfs_root *root = fs_info->chunk_root; |
8f18cf13 CM |
2977 | int ret; |
2978 | struct btrfs_path *path; | |
2979 | struct btrfs_key key; | |
2980 | ||
8f18cf13 CM |
2981 | path = btrfs_alloc_path(); |
2982 | if (!path) | |
2983 | return -ENOMEM; | |
2984 | ||
408fbf19 | 2985 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
8f18cf13 CM |
2986 | key.offset = chunk_offset; |
2987 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2988 | ||
2989 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
79787eaa JM |
2990 | if (ret < 0) |
2991 | goto out; | |
2992 | else if (ret > 0) { /* Logic error or corruption */ | |
0b246afa JM |
2993 | btrfs_handle_fs_error(fs_info, -ENOENT, |
2994 | "Failed lookup while freeing chunk."); | |
79787eaa JM |
2995 | ret = -ENOENT; |
2996 | goto out; | |
2997 | } | |
8f18cf13 CM |
2998 | |
2999 | ret = btrfs_del_item(trans, root, path); | |
79787eaa | 3000 | if (ret < 0) |
0b246afa JM |
3001 | btrfs_handle_fs_error(fs_info, ret, |
3002 | "Failed to delete chunk item."); | |
79787eaa | 3003 | out: |
8f18cf13 | 3004 | btrfs_free_path(path); |
65a246c5 | 3005 | return ret; |
8f18cf13 CM |
3006 | } |
3007 | ||
408fbf19 | 3008 | static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 3009 | { |
0b246afa | 3010 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
8f18cf13 CM |
3011 | struct btrfs_disk_key *disk_key; |
3012 | struct btrfs_chunk *chunk; | |
3013 | u8 *ptr; | |
3014 | int ret = 0; | |
3015 | u32 num_stripes; | |
3016 | u32 array_size; | |
3017 | u32 len = 0; | |
3018 | u32 cur; | |
3019 | struct btrfs_key key; | |
3020 | ||
79bd3712 | 3021 | lockdep_assert_held(&fs_info->chunk_mutex); |
8f18cf13 CM |
3022 | array_size = btrfs_super_sys_array_size(super_copy); |
3023 | ||
3024 | ptr = super_copy->sys_chunk_array; | |
3025 | cur = 0; | |
3026 | ||
3027 | while (cur < array_size) { | |
3028 | disk_key = (struct btrfs_disk_key *)ptr; | |
3029 | btrfs_disk_key_to_cpu(&key, disk_key); | |
3030 | ||
3031 | len = sizeof(*disk_key); | |
3032 | ||
3033 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { | |
3034 | chunk = (struct btrfs_chunk *)(ptr + len); | |
3035 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); | |
3036 | len += btrfs_chunk_item_size(num_stripes); | |
3037 | } else { | |
3038 | ret = -EIO; | |
3039 | break; | |
3040 | } | |
408fbf19 | 3041 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
8f18cf13 CM |
3042 | key.offset == chunk_offset) { |
3043 | memmove(ptr, ptr + len, array_size - (cur + len)); | |
3044 | array_size -= len; | |
3045 | btrfs_set_super_sys_array_size(super_copy, array_size); | |
3046 | } else { | |
3047 | ptr += len; | |
3048 | cur += len; | |
3049 | } | |
3050 | } | |
3051 | return ret; | |
3052 | } | |
3053 | ||
60ca842e | 3054 | /* |
9580503b DS |
3055 | * Find the mapping containing the given logical extent. |
3056 | * | |
60ca842e OS |
3057 | * @logical: Logical block offset in bytes. |
3058 | * @length: Length of extent in bytes. | |
3059 | * | |
3060 | * Return: Chunk mapping or ERR_PTR. | |
3061 | */ | |
3062 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, | |
3063 | u64 logical, u64 length) | |
592d92ee LB |
3064 | { |
3065 | struct extent_map_tree *em_tree; | |
3066 | struct extent_map *em; | |
3067 | ||
c8bf1b67 | 3068 | em_tree = &fs_info->mapping_tree; |
592d92ee LB |
3069 | read_lock(&em_tree->lock); |
3070 | em = lookup_extent_mapping(em_tree, logical, length); | |
3071 | read_unlock(&em_tree->lock); | |
3072 | ||
3073 | if (!em) { | |
3074 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", | |
3075 | logical, length); | |
3076 | return ERR_PTR(-EINVAL); | |
3077 | } | |
3078 | ||
3079 | if (em->start > logical || em->start + em->len < logical) { | |
3080 | btrfs_crit(fs_info, | |
3081 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", | |
3082 | logical, length, em->start, em->start + em->len); | |
3083 | free_extent_map(em); | |
3084 | return ERR_PTR(-EINVAL); | |
3085 | } | |
3086 | ||
3087 | /* callers are responsible for dropping em's ref. */ | |
3088 | return em; | |
3089 | } | |
3090 | ||
79bd3712 FM |
3091 | static int remove_chunk_item(struct btrfs_trans_handle *trans, |
3092 | struct map_lookup *map, u64 chunk_offset) | |
3093 | { | |
3094 | int i; | |
3095 | ||
3096 | /* | |
3097 | * Removing chunk items and updating the device items in the chunks btree | |
3098 | * requires holding the chunk_mutex. | |
3099 | * See the comment at btrfs_chunk_alloc() for the details. | |
3100 | */ | |
3101 | lockdep_assert_held(&trans->fs_info->chunk_mutex); | |
3102 | ||
3103 | for (i = 0; i < map->num_stripes; i++) { | |
3104 | int ret; | |
3105 | ||
3106 | ret = btrfs_update_device(trans, map->stripes[i].dev); | |
3107 | if (ret) | |
3108 | return ret; | |
3109 | } | |
3110 | ||
3111 | return btrfs_free_chunk(trans, chunk_offset); | |
3112 | } | |
3113 | ||
97aff912 | 3114 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
8f18cf13 | 3115 | { |
97aff912 | 3116 | struct btrfs_fs_info *fs_info = trans->fs_info; |
8f18cf13 CM |
3117 | struct extent_map *em; |
3118 | struct map_lookup *map; | |
2196d6e8 | 3119 | u64 dev_extent_len = 0; |
47ab2a6c | 3120 | int i, ret = 0; |
0b246afa | 3121 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
8f18cf13 | 3122 | |
60ca842e | 3123 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
592d92ee | 3124 | if (IS_ERR(em)) { |
47ab2a6c JB |
3125 | /* |
3126 | * This is a logic error, but we don't want to just rely on the | |
bb7ab3b9 | 3127 | * user having built with ASSERT enabled, so if ASSERT doesn't |
47ab2a6c JB |
3128 | * do anything we still error out. |
3129 | */ | |
3130 | ASSERT(0); | |
592d92ee | 3131 | return PTR_ERR(em); |
47ab2a6c | 3132 | } |
95617d69 | 3133 | map = em->map_lookup; |
8f18cf13 | 3134 | |
57ba4cb8 | 3135 | /* |
79bd3712 FM |
3136 | * First delete the device extent items from the devices btree. |
3137 | * We take the device_list_mutex to avoid racing with the finishing phase | |
3138 | * of a device replace operation. See the comment below before acquiring | |
3139 | * fs_info->chunk_mutex. Note that here we do not acquire the chunk_mutex | |
3140 | * because that can result in a deadlock when deleting the device extent | |
3141 | * items from the devices btree - COWing an extent buffer from the btree | |
3142 | * may result in allocating a new metadata chunk, which would attempt to | |
3143 | * lock again fs_info->chunk_mutex. | |
57ba4cb8 FM |
3144 | */ |
3145 | mutex_lock(&fs_devices->device_list_mutex); | |
8f18cf13 | 3146 | for (i = 0; i < map->num_stripes; i++) { |
47ab2a6c | 3147 | struct btrfs_device *device = map->stripes[i].dev; |
2196d6e8 MX |
3148 | ret = btrfs_free_dev_extent(trans, device, |
3149 | map->stripes[i].physical, | |
3150 | &dev_extent_len); | |
47ab2a6c | 3151 | if (ret) { |
57ba4cb8 | 3152 | mutex_unlock(&fs_devices->device_list_mutex); |
66642832 | 3153 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
3154 | goto out; |
3155 | } | |
a061fc8d | 3156 | |
2196d6e8 | 3157 | if (device->bytes_used > 0) { |
34441361 | 3158 | mutex_lock(&fs_info->chunk_mutex); |
2196d6e8 MX |
3159 | btrfs_device_set_bytes_used(device, |
3160 | device->bytes_used - dev_extent_len); | |
a5ed45f8 | 3161 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
0b246afa | 3162 | btrfs_clear_space_info_full(fs_info); |
34441361 | 3163 | mutex_unlock(&fs_info->chunk_mutex); |
2196d6e8 | 3164 | } |
79bd3712 FM |
3165 | } |
3166 | mutex_unlock(&fs_devices->device_list_mutex); | |
a061fc8d | 3167 | |
79bd3712 FM |
3168 | /* |
3169 | * We acquire fs_info->chunk_mutex for 2 reasons: | |
3170 | * | |
3171 | * 1) Just like with the first phase of the chunk allocation, we must | |
3172 | * reserve system space, do all chunk btree updates and deletions, and | |
3173 | * update the system chunk array in the superblock while holding this | |
3174 | * mutex. This is for similar reasons as explained on the comment at | |
3175 | * the top of btrfs_chunk_alloc(); | |
3176 | * | |
3177 | * 2) Prevent races with the final phase of a device replace operation | |
3178 | * that replaces the device object associated with the map's stripes, | |
3179 | * because the device object's id can change at any time during that | |
3180 | * final phase of the device replace operation | |
3181 | * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the | |
3182 | * replaced device and then see it with an ID of | |
3183 | * BTRFS_DEV_REPLACE_DEVID, which would cause a failure when updating | |
3184 | * the device item, which does not exists on the chunk btree. | |
3185 | * The finishing phase of device replace acquires both the | |
3186 | * device_list_mutex and the chunk_mutex, in that order, so we are | |
3187 | * safe by just acquiring the chunk_mutex. | |
3188 | */ | |
3189 | trans->removing_chunk = true; | |
3190 | mutex_lock(&fs_info->chunk_mutex); | |
3191 | ||
3192 | check_system_chunk(trans, map->type); | |
3193 | ||
3194 | ret = remove_chunk_item(trans, map, chunk_offset); | |
3195 | /* | |
3196 | * Normally we should not get -ENOSPC since we reserved space before | |
3197 | * through the call to check_system_chunk(). | |
3198 | * | |
3199 | * Despite our system space_info having enough free space, we may not | |
3200 | * be able to allocate extents from its block groups, because all have | |
3201 | * an incompatible profile, which will force us to allocate a new system | |
3202 | * block group with the right profile, or right after we called | |
3203 | * check_system_space() above, a scrub turned the only system block group | |
3204 | * with enough free space into RO mode. | |
3205 | * This is explained with more detail at do_chunk_alloc(). | |
3206 | * | |
3207 | * So if we get -ENOSPC, allocate a new system chunk and retry once. | |
3208 | */ | |
3209 | if (ret == -ENOSPC) { | |
3210 | const u64 sys_flags = btrfs_system_alloc_profile(fs_info); | |
3211 | struct btrfs_block_group *sys_bg; | |
3212 | ||
f6f39f7a | 3213 | sys_bg = btrfs_create_chunk(trans, sys_flags); |
79bd3712 FM |
3214 | if (IS_ERR(sys_bg)) { |
3215 | ret = PTR_ERR(sys_bg); | |
3216 | btrfs_abort_transaction(trans, ret); | |
3217 | goto out; | |
3218 | } | |
3219 | ||
3220 | ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg); | |
64bc6c2a | 3221 | if (ret) { |
64bc6c2a NB |
3222 | btrfs_abort_transaction(trans, ret); |
3223 | goto out; | |
dfe25020 | 3224 | } |
57ba4cb8 | 3225 | |
79bd3712 FM |
3226 | ret = remove_chunk_item(trans, map, chunk_offset); |
3227 | if (ret) { | |
3228 | btrfs_abort_transaction(trans, ret); | |
3229 | goto out; | |
3230 | } | |
3231 | } else if (ret) { | |
66642832 | 3232 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
3233 | goto out; |
3234 | } | |
8f18cf13 | 3235 | |
6bccf3ab | 3236 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
1abe9b8a | 3237 | |
8f18cf13 | 3238 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
408fbf19 | 3239 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
47ab2a6c | 3240 | if (ret) { |
66642832 | 3241 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
3242 | goto out; |
3243 | } | |
8f18cf13 CM |
3244 | } |
3245 | ||
79bd3712 FM |
3246 | mutex_unlock(&fs_info->chunk_mutex); |
3247 | trans->removing_chunk = false; | |
3248 | ||
3249 | /* | |
3250 | * We are done with chunk btree updates and deletions, so release the | |
3251 | * system space we previously reserved (with check_system_chunk()). | |
3252 | */ | |
3253 | btrfs_trans_release_chunk_metadata(trans); | |
3254 | ||
5a98ec01 | 3255 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
47ab2a6c | 3256 | if (ret) { |
66642832 | 3257 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
3258 | goto out; |
3259 | } | |
2b82032c | 3260 | |
47ab2a6c | 3261 | out: |
79bd3712 FM |
3262 | if (trans->removing_chunk) { |
3263 | mutex_unlock(&fs_info->chunk_mutex); | |
3264 | trans->removing_chunk = false; | |
3265 | } | |
2b82032c YZ |
3266 | /* once for us */ |
3267 | free_extent_map(em); | |
47ab2a6c JB |
3268 | return ret; |
3269 | } | |
2b82032c | 3270 | |
18bb8bbf | 3271 | int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
47ab2a6c | 3272 | { |
5b4aacef | 3273 | struct btrfs_root *root = fs_info->chunk_root; |
19c4d2f9 | 3274 | struct btrfs_trans_handle *trans; |
b0643e59 | 3275 | struct btrfs_block_group *block_group; |
01e86008 | 3276 | u64 length; |
47ab2a6c | 3277 | int ret; |
2b82032c | 3278 | |
4b349253 JB |
3279 | if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { |
3280 | btrfs_err(fs_info, | |
3281 | "relocate: not supported on extent tree v2 yet"); | |
3282 | return -EINVAL; | |
3283 | } | |
3284 | ||
67c5e7d4 FM |
3285 | /* |
3286 | * Prevent races with automatic removal of unused block groups. | |
3287 | * After we relocate and before we remove the chunk with offset | |
3288 | * chunk_offset, automatic removal of the block group can kick in, | |
3289 | * resulting in a failure when calling btrfs_remove_chunk() below. | |
3290 | * | |
3291 | * Make sure to acquire this mutex before doing a tree search (dev | |
3292 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might | |
3293 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after | |
3294 | * we release the path used to search the chunk/dev tree and before | |
3295 | * the current task acquires this mutex and calls us. | |
3296 | */ | |
f3372065 | 3297 | lockdep_assert_held(&fs_info->reclaim_bgs_lock); |
67c5e7d4 | 3298 | |
47ab2a6c | 3299 | /* step one, relocate all the extents inside this chunk */ |
2ff7e61e | 3300 | btrfs_scrub_pause(fs_info); |
0b246afa | 3301 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
2ff7e61e | 3302 | btrfs_scrub_continue(fs_info); |
2d82a40a FM |
3303 | if (ret) { |
3304 | /* | |
3305 | * If we had a transaction abort, stop all running scrubs. | |
3306 | * See transaction.c:cleanup_transaction() why we do it here. | |
3307 | */ | |
3308 | if (BTRFS_FS_ERROR(fs_info)) | |
3309 | btrfs_scrub_cancel(fs_info); | |
47ab2a6c | 3310 | return ret; |
2d82a40a | 3311 | } |
47ab2a6c | 3312 | |
b0643e59 DZ |
3313 | block_group = btrfs_lookup_block_group(fs_info, chunk_offset); |
3314 | if (!block_group) | |
3315 | return -ENOENT; | |
3316 | btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); | |
01e86008 | 3317 | length = block_group->length; |
b0643e59 DZ |
3318 | btrfs_put_block_group(block_group); |
3319 | ||
01e86008 JT |
3320 | /* |
3321 | * On a zoned file system, discard the whole block group, this will | |
3322 | * trigger a REQ_OP_ZONE_RESET operation on the device zone. If | |
3323 | * resetting the zone fails, don't treat it as a fatal problem from the | |
3324 | * filesystem's point of view. | |
3325 | */ | |
3326 | if (btrfs_is_zoned(fs_info)) { | |
3327 | ret = btrfs_discard_extent(fs_info, chunk_offset, length, NULL); | |
3328 | if (ret) | |
3329 | btrfs_info(fs_info, | |
3330 | "failed to reset zone %llu after relocation", | |
3331 | chunk_offset); | |
3332 | } | |
3333 | ||
19c4d2f9 CM |
3334 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
3335 | chunk_offset); | |
3336 | if (IS_ERR(trans)) { | |
3337 | ret = PTR_ERR(trans); | |
3338 | btrfs_handle_fs_error(root->fs_info, ret, NULL); | |
3339 | return ret; | |
3340 | } | |
3341 | ||
47ab2a6c | 3342 | /* |
19c4d2f9 CM |
3343 | * step two, delete the device extents and the |
3344 | * chunk tree entries | |
47ab2a6c | 3345 | */ |
97aff912 | 3346 | ret = btrfs_remove_chunk(trans, chunk_offset); |
3a45bb20 | 3347 | btrfs_end_transaction(trans); |
19c4d2f9 | 3348 | return ret; |
2b82032c YZ |
3349 | } |
3350 | ||
2ff7e61e | 3351 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
2b82032c | 3352 | { |
0b246afa | 3353 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
2b82032c YZ |
3354 | struct btrfs_path *path; |
3355 | struct extent_buffer *leaf; | |
3356 | struct btrfs_chunk *chunk; | |
3357 | struct btrfs_key key; | |
3358 | struct btrfs_key found_key; | |
2b82032c | 3359 | u64 chunk_type; |
ba1bf481 JB |
3360 | bool retried = false; |
3361 | int failed = 0; | |
2b82032c YZ |
3362 | int ret; |
3363 | ||
3364 | path = btrfs_alloc_path(); | |
3365 | if (!path) | |
3366 | return -ENOMEM; | |
3367 | ||
ba1bf481 | 3368 | again: |
2b82032c YZ |
3369 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
3370 | key.offset = (u64)-1; | |
3371 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
3372 | ||
3373 | while (1) { | |
f3372065 | 3374 | mutex_lock(&fs_info->reclaim_bgs_lock); |
2b82032c | 3375 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
67c5e7d4 | 3376 | if (ret < 0) { |
f3372065 | 3377 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
2b82032c | 3378 | goto error; |
67c5e7d4 | 3379 | } |
79787eaa | 3380 | BUG_ON(ret == 0); /* Corruption */ |
2b82032c YZ |
3381 | |
3382 | ret = btrfs_previous_item(chunk_root, path, key.objectid, | |
3383 | key.type); | |
67c5e7d4 | 3384 | if (ret) |
f3372065 | 3385 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
2b82032c YZ |
3386 | if (ret < 0) |
3387 | goto error; | |
3388 | if (ret > 0) | |
3389 | break; | |
1a40e23b | 3390 | |
2b82032c YZ |
3391 | leaf = path->nodes[0]; |
3392 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b | 3393 | |
2b82032c YZ |
3394 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
3395 | struct btrfs_chunk); | |
3396 | chunk_type = btrfs_chunk_type(leaf, chunk); | |
b3b4aa74 | 3397 | btrfs_release_path(path); |
8f18cf13 | 3398 | |
2b82032c | 3399 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
0b246afa | 3400 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
ba1bf481 JB |
3401 | if (ret == -ENOSPC) |
3402 | failed++; | |
14586651 HS |
3403 | else |
3404 | BUG_ON(ret); | |
2b82032c | 3405 | } |
f3372065 | 3406 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
8f18cf13 | 3407 | |
2b82032c YZ |
3408 | if (found_key.offset == 0) |
3409 | break; | |
3410 | key.offset = found_key.offset - 1; | |
3411 | } | |
3412 | ret = 0; | |
ba1bf481 JB |
3413 | if (failed && !retried) { |
3414 | failed = 0; | |
3415 | retried = true; | |
3416 | goto again; | |
fae7f21c | 3417 | } else if (WARN_ON(failed && retried)) { |
ba1bf481 JB |
3418 | ret = -ENOSPC; |
3419 | } | |
2b82032c YZ |
3420 | error: |
3421 | btrfs_free_path(path); | |
3422 | return ret; | |
8f18cf13 CM |
3423 | } |
3424 | ||
a6f93c71 LB |
3425 | /* |
3426 | * return 1 : allocate a data chunk successfully, | |
3427 | * return <0: errors during allocating a data chunk, | |
3428 | * return 0 : no need to allocate a data chunk. | |
3429 | */ | |
3430 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, | |
3431 | u64 chunk_offset) | |
3432 | { | |
32da5386 | 3433 | struct btrfs_block_group *cache; |
a6f93c71 LB |
3434 | u64 bytes_used; |
3435 | u64 chunk_type; | |
3436 | ||
3437 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
3438 | ASSERT(cache); | |
3439 | chunk_type = cache->flags; | |
3440 | btrfs_put_block_group(cache); | |
3441 | ||
5ae21692 JT |
3442 | if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA)) |
3443 | return 0; | |
3444 | ||
3445 | spin_lock(&fs_info->data_sinfo->lock); | |
3446 | bytes_used = fs_info->data_sinfo->bytes_used; | |
3447 | spin_unlock(&fs_info->data_sinfo->lock); | |
3448 | ||
3449 | if (!bytes_used) { | |
3450 | struct btrfs_trans_handle *trans; | |
3451 | int ret; | |
3452 | ||
3453 | trans = btrfs_join_transaction(fs_info->tree_root); | |
3454 | if (IS_ERR(trans)) | |
3455 | return PTR_ERR(trans); | |
3456 | ||
3457 | ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA); | |
3458 | btrfs_end_transaction(trans); | |
3459 | if (ret < 0) | |
3460 | return ret; | |
3461 | return 1; | |
a6f93c71 | 3462 | } |
5ae21692 | 3463 | |
a6f93c71 LB |
3464 | return 0; |
3465 | } | |
3466 | ||
6bccf3ab | 3467 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
0940ebf6 ID |
3468 | struct btrfs_balance_control *bctl) |
3469 | { | |
6bccf3ab | 3470 | struct btrfs_root *root = fs_info->tree_root; |
0940ebf6 ID |
3471 | struct btrfs_trans_handle *trans; |
3472 | struct btrfs_balance_item *item; | |
3473 | struct btrfs_disk_balance_args disk_bargs; | |
3474 | struct btrfs_path *path; | |
3475 | struct extent_buffer *leaf; | |
3476 | struct btrfs_key key; | |
3477 | int ret, err; | |
3478 | ||
3479 | path = btrfs_alloc_path(); | |
3480 | if (!path) | |
3481 | return -ENOMEM; | |
3482 | ||
3483 | trans = btrfs_start_transaction(root, 0); | |
3484 | if (IS_ERR(trans)) { | |
3485 | btrfs_free_path(path); | |
3486 | return PTR_ERR(trans); | |
3487 | } | |
3488 | ||
3489 | key.objectid = BTRFS_BALANCE_OBJECTID; | |
c479cb4f | 3490 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
0940ebf6 ID |
3491 | key.offset = 0; |
3492 | ||
3493 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
3494 | sizeof(*item)); | |
3495 | if (ret) | |
3496 | goto out; | |
3497 | ||
3498 | leaf = path->nodes[0]; | |
3499 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); | |
3500 | ||
b159fa28 | 3501 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
0940ebf6 ID |
3502 | |
3503 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); | |
3504 | btrfs_set_balance_data(leaf, item, &disk_bargs); | |
3505 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); | |
3506 | btrfs_set_balance_meta(leaf, item, &disk_bargs); | |
3507 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); | |
3508 | btrfs_set_balance_sys(leaf, item, &disk_bargs); | |
3509 | ||
3510 | btrfs_set_balance_flags(leaf, item, bctl->flags); | |
3511 | ||
3512 | btrfs_mark_buffer_dirty(leaf); | |
3513 | out: | |
3514 | btrfs_free_path(path); | |
3a45bb20 | 3515 | err = btrfs_commit_transaction(trans); |
0940ebf6 ID |
3516 | if (err && !ret) |
3517 | ret = err; | |
3518 | return ret; | |
3519 | } | |
3520 | ||
6bccf3ab | 3521 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
0940ebf6 | 3522 | { |
6bccf3ab | 3523 | struct btrfs_root *root = fs_info->tree_root; |
0940ebf6 ID |
3524 | struct btrfs_trans_handle *trans; |
3525 | struct btrfs_path *path; | |
3526 | struct btrfs_key key; | |
3527 | int ret, err; | |
3528 | ||
3529 | path = btrfs_alloc_path(); | |
3530 | if (!path) | |
3531 | return -ENOMEM; | |
3532 | ||
3502a8c0 | 3533 | trans = btrfs_start_transaction_fallback_global_rsv(root, 0); |
0940ebf6 ID |
3534 | if (IS_ERR(trans)) { |
3535 | btrfs_free_path(path); | |
3536 | return PTR_ERR(trans); | |
3537 | } | |
3538 | ||
3539 | key.objectid = BTRFS_BALANCE_OBJECTID; | |
c479cb4f | 3540 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
0940ebf6 ID |
3541 | key.offset = 0; |
3542 | ||
3543 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
3544 | if (ret < 0) | |
3545 | goto out; | |
3546 | if (ret > 0) { | |
3547 | ret = -ENOENT; | |
3548 | goto out; | |
3549 | } | |
3550 | ||
3551 | ret = btrfs_del_item(trans, root, path); | |
3552 | out: | |
3553 | btrfs_free_path(path); | |
3a45bb20 | 3554 | err = btrfs_commit_transaction(trans); |
0940ebf6 ID |
3555 | if (err && !ret) |
3556 | ret = err; | |
3557 | return ret; | |
3558 | } | |
3559 | ||
59641015 ID |
3560 | /* |
3561 | * This is a heuristic used to reduce the number of chunks balanced on | |
3562 | * resume after balance was interrupted. | |
3563 | */ | |
3564 | static void update_balance_args(struct btrfs_balance_control *bctl) | |
3565 | { | |
3566 | /* | |
3567 | * Turn on soft mode for chunk types that were being converted. | |
3568 | */ | |
3569 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3570 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3571 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3572 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3573 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3574 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3575 | ||
3576 | /* | |
3577 | * Turn on usage filter if is not already used. The idea is | |
3578 | * that chunks that we have already balanced should be | |
3579 | * reasonably full. Don't do it for chunks that are being | |
3580 | * converted - that will keep us from relocating unconverted | |
3581 | * (albeit full) chunks. | |
3582 | */ | |
3583 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3584 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3585 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3586 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3587 | bctl->data.usage = 90; | |
3588 | } | |
3589 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3590 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3591 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3592 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3593 | bctl->sys.usage = 90; | |
3594 | } | |
3595 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3596 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3597 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3598 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3599 | bctl->meta.usage = 90; | |
3600 | } | |
3601 | } | |
3602 | ||
149196a2 DS |
3603 | /* |
3604 | * Clear the balance status in fs_info and delete the balance item from disk. | |
3605 | */ | |
3606 | static void reset_balance_state(struct btrfs_fs_info *fs_info) | |
c9e9f97b ID |
3607 | { |
3608 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
149196a2 | 3609 | int ret; |
c9e9f97b ID |
3610 | |
3611 | BUG_ON(!fs_info->balance_ctl); | |
3612 | ||
3613 | spin_lock(&fs_info->balance_lock); | |
3614 | fs_info->balance_ctl = NULL; | |
3615 | spin_unlock(&fs_info->balance_lock); | |
3616 | ||
3617 | kfree(bctl); | |
149196a2 DS |
3618 | ret = del_balance_item(fs_info); |
3619 | if (ret) | |
3620 | btrfs_handle_fs_error(fs_info, ret, NULL); | |
c9e9f97b ID |
3621 | } |
3622 | ||
ed25e9b2 ID |
3623 | /* |
3624 | * Balance filters. Return 1 if chunk should be filtered out | |
3625 | * (should not be balanced). | |
3626 | */ | |
899c81ea | 3627 | static int chunk_profiles_filter(u64 chunk_type, |
ed25e9b2 ID |
3628 | struct btrfs_balance_args *bargs) |
3629 | { | |
899c81ea ID |
3630 | chunk_type = chunk_to_extended(chunk_type) & |
3631 | BTRFS_EXTENDED_PROFILE_MASK; | |
ed25e9b2 | 3632 | |
899c81ea | 3633 | if (bargs->profiles & chunk_type) |
ed25e9b2 ID |
3634 | return 0; |
3635 | ||
3636 | return 1; | |
3637 | } | |
3638 | ||
dba72cb3 | 3639 | static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
5ce5b3c0 | 3640 | struct btrfs_balance_args *bargs) |
bc309467 | 3641 | { |
32da5386 | 3642 | struct btrfs_block_group *cache; |
bc309467 DS |
3643 | u64 chunk_used; |
3644 | u64 user_thresh_min; | |
3645 | u64 user_thresh_max; | |
3646 | int ret = 1; | |
3647 | ||
3648 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
bf38be65 | 3649 | chunk_used = cache->used; |
bc309467 DS |
3650 | |
3651 | if (bargs->usage_min == 0) | |
3652 | user_thresh_min = 0; | |
3653 | else | |
428c8e03 | 3654 | user_thresh_min = mult_perc(cache->length, bargs->usage_min); |
bc309467 DS |
3655 | |
3656 | if (bargs->usage_max == 0) | |
3657 | user_thresh_max = 1; | |
3658 | else if (bargs->usage_max > 100) | |
b3470b5d | 3659 | user_thresh_max = cache->length; |
bc309467 | 3660 | else |
428c8e03 | 3661 | user_thresh_max = mult_perc(cache->length, bargs->usage_max); |
bc309467 DS |
3662 | |
3663 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) | |
3664 | ret = 0; | |
3665 | ||
3666 | btrfs_put_block_group(cache); | |
3667 | return ret; | |
3668 | } | |
3669 | ||
dba72cb3 | 3670 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
bc309467 | 3671 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
5ce5b3c0 | 3672 | { |
32da5386 | 3673 | struct btrfs_block_group *cache; |
5ce5b3c0 ID |
3674 | u64 chunk_used, user_thresh; |
3675 | int ret = 1; | |
3676 | ||
3677 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
bf38be65 | 3678 | chunk_used = cache->used; |
5ce5b3c0 | 3679 | |
bc309467 | 3680 | if (bargs->usage_min == 0) |
3e39cea6 | 3681 | user_thresh = 1; |
a105bb88 | 3682 | else if (bargs->usage > 100) |
b3470b5d | 3683 | user_thresh = cache->length; |
a105bb88 | 3684 | else |
428c8e03 | 3685 | user_thresh = mult_perc(cache->length, bargs->usage); |
a105bb88 | 3686 | |
5ce5b3c0 ID |
3687 | if (chunk_used < user_thresh) |
3688 | ret = 0; | |
3689 | ||
3690 | btrfs_put_block_group(cache); | |
3691 | return ret; | |
3692 | } | |
3693 | ||
409d404b ID |
3694 | static int chunk_devid_filter(struct extent_buffer *leaf, |
3695 | struct btrfs_chunk *chunk, | |
3696 | struct btrfs_balance_args *bargs) | |
3697 | { | |
3698 | struct btrfs_stripe *stripe; | |
3699 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3700 | int i; | |
3701 | ||
3702 | for (i = 0; i < num_stripes; i++) { | |
3703 | stripe = btrfs_stripe_nr(chunk, i); | |
3704 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) | |
3705 | return 0; | |
3706 | } | |
3707 | ||
3708 | return 1; | |
3709 | } | |
3710 | ||
946c9256 DS |
3711 | static u64 calc_data_stripes(u64 type, int num_stripes) |
3712 | { | |
3713 | const int index = btrfs_bg_flags_to_raid_index(type); | |
3714 | const int ncopies = btrfs_raid_array[index].ncopies; | |
3715 | const int nparity = btrfs_raid_array[index].nparity; | |
3716 | ||
d58ede8d | 3717 | return (num_stripes - nparity) / ncopies; |
946c9256 DS |
3718 | } |
3719 | ||
94e60d5a ID |
3720 | /* [pstart, pend) */ |
3721 | static int chunk_drange_filter(struct extent_buffer *leaf, | |
3722 | struct btrfs_chunk *chunk, | |
94e60d5a ID |
3723 | struct btrfs_balance_args *bargs) |
3724 | { | |
3725 | struct btrfs_stripe *stripe; | |
3726 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3727 | u64 stripe_offset; | |
3728 | u64 stripe_length; | |
946c9256 | 3729 | u64 type; |
94e60d5a ID |
3730 | int factor; |
3731 | int i; | |
3732 | ||
3733 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) | |
3734 | return 0; | |
3735 | ||
946c9256 DS |
3736 | type = btrfs_chunk_type(leaf, chunk); |
3737 | factor = calc_data_stripes(type, num_stripes); | |
94e60d5a ID |
3738 | |
3739 | for (i = 0; i < num_stripes; i++) { | |
3740 | stripe = btrfs_stripe_nr(chunk, i); | |
3741 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) | |
3742 | continue; | |
3743 | ||
3744 | stripe_offset = btrfs_stripe_offset(leaf, stripe); | |
3745 | stripe_length = btrfs_chunk_length(leaf, chunk); | |
b8b93add | 3746 | stripe_length = div_u64(stripe_length, factor); |
94e60d5a ID |
3747 | |
3748 | if (stripe_offset < bargs->pend && | |
3749 | stripe_offset + stripe_length > bargs->pstart) | |
3750 | return 0; | |
3751 | } | |
3752 | ||
3753 | return 1; | |
3754 | } | |
3755 | ||
ea67176a ID |
3756 | /* [vstart, vend) */ |
3757 | static int chunk_vrange_filter(struct extent_buffer *leaf, | |
3758 | struct btrfs_chunk *chunk, | |
3759 | u64 chunk_offset, | |
3760 | struct btrfs_balance_args *bargs) | |
3761 | { | |
3762 | if (chunk_offset < bargs->vend && | |
3763 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) | |
3764 | /* at least part of the chunk is inside this vrange */ | |
3765 | return 0; | |
3766 | ||
3767 | return 1; | |
3768 | } | |
3769 | ||
dee32d0a GAP |
3770 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
3771 | struct btrfs_chunk *chunk, | |
3772 | struct btrfs_balance_args *bargs) | |
3773 | { | |
3774 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3775 | ||
3776 | if (bargs->stripes_min <= num_stripes | |
3777 | && num_stripes <= bargs->stripes_max) | |
3778 | return 0; | |
3779 | ||
3780 | return 1; | |
3781 | } | |
3782 | ||
899c81ea | 3783 | static int chunk_soft_convert_filter(u64 chunk_type, |
cfa4c961 ID |
3784 | struct btrfs_balance_args *bargs) |
3785 | { | |
3786 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) | |
3787 | return 0; | |
3788 | ||
899c81ea ID |
3789 | chunk_type = chunk_to_extended(chunk_type) & |
3790 | BTRFS_EXTENDED_PROFILE_MASK; | |
cfa4c961 | 3791 | |
899c81ea | 3792 | if (bargs->target == chunk_type) |
cfa4c961 ID |
3793 | return 1; |
3794 | ||
3795 | return 0; | |
3796 | } | |
3797 | ||
6ec0896c | 3798 | static int should_balance_chunk(struct extent_buffer *leaf, |
f43ffb60 ID |
3799 | struct btrfs_chunk *chunk, u64 chunk_offset) |
3800 | { | |
6ec0896c | 3801 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
0b246afa | 3802 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
f43ffb60 ID |
3803 | struct btrfs_balance_args *bargs = NULL; |
3804 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); | |
3805 | ||
3806 | /* type filter */ | |
3807 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & | |
3808 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { | |
3809 | return 0; | |
3810 | } | |
3811 | ||
3812 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) | |
3813 | bargs = &bctl->data; | |
3814 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) | |
3815 | bargs = &bctl->sys; | |
3816 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) | |
3817 | bargs = &bctl->meta; | |
3818 | ||
ed25e9b2 ID |
3819 | /* profiles filter */ |
3820 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && | |
3821 | chunk_profiles_filter(chunk_type, bargs)) { | |
3822 | return 0; | |
5ce5b3c0 ID |
3823 | } |
3824 | ||
3825 | /* usage filter */ | |
3826 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && | |
0b246afa | 3827 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
5ce5b3c0 | 3828 | return 0; |
bc309467 | 3829 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
0b246afa | 3830 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
bc309467 | 3831 | return 0; |
409d404b ID |
3832 | } |
3833 | ||
3834 | /* devid filter */ | |
3835 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && | |
3836 | chunk_devid_filter(leaf, chunk, bargs)) { | |
3837 | return 0; | |
94e60d5a ID |
3838 | } |
3839 | ||
3840 | /* drange filter, makes sense only with devid filter */ | |
3841 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && | |
e4ff5fb5 | 3842 | chunk_drange_filter(leaf, chunk, bargs)) { |
94e60d5a | 3843 | return 0; |
ea67176a ID |
3844 | } |
3845 | ||
3846 | /* vrange filter */ | |
3847 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && | |
3848 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { | |
3849 | return 0; | |
ed25e9b2 ID |
3850 | } |
3851 | ||
dee32d0a GAP |
3852 | /* stripes filter */ |
3853 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && | |
3854 | chunk_stripes_range_filter(leaf, chunk, bargs)) { | |
3855 | return 0; | |
3856 | } | |
3857 | ||
cfa4c961 ID |
3858 | /* soft profile changing mode */ |
3859 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && | |
3860 | chunk_soft_convert_filter(chunk_type, bargs)) { | |
3861 | return 0; | |
3862 | } | |
3863 | ||
7d824b6f DS |
3864 | /* |
3865 | * limited by count, must be the last filter | |
3866 | */ | |
3867 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { | |
3868 | if (bargs->limit == 0) | |
3869 | return 0; | |
3870 | else | |
3871 | bargs->limit--; | |
12907fc7 DS |
3872 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
3873 | /* | |
3874 | * Same logic as the 'limit' filter; the minimum cannot be | |
01327610 | 3875 | * determined here because we do not have the global information |
12907fc7 DS |
3876 | * about the count of all chunks that satisfy the filters. |
3877 | */ | |
3878 | if (bargs->limit_max == 0) | |
3879 | return 0; | |
3880 | else | |
3881 | bargs->limit_max--; | |
7d824b6f DS |
3882 | } |
3883 | ||
f43ffb60 ID |
3884 | return 1; |
3885 | } | |
3886 | ||
c9e9f97b | 3887 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
ec44a35c | 3888 | { |
19a39dce | 3889 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
c9e9f97b | 3890 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
12907fc7 | 3891 | u64 chunk_type; |
f43ffb60 | 3892 | struct btrfs_chunk *chunk; |
5a488b9d | 3893 | struct btrfs_path *path = NULL; |
ec44a35c | 3894 | struct btrfs_key key; |
ec44a35c | 3895 | struct btrfs_key found_key; |
f43ffb60 ID |
3896 | struct extent_buffer *leaf; |
3897 | int slot; | |
c9e9f97b ID |
3898 | int ret; |
3899 | int enospc_errors = 0; | |
19a39dce | 3900 | bool counting = true; |
12907fc7 | 3901 | /* The single value limit and min/max limits use the same bytes in the */ |
7d824b6f DS |
3902 | u64 limit_data = bctl->data.limit; |
3903 | u64 limit_meta = bctl->meta.limit; | |
3904 | u64 limit_sys = bctl->sys.limit; | |
12907fc7 DS |
3905 | u32 count_data = 0; |
3906 | u32 count_meta = 0; | |
3907 | u32 count_sys = 0; | |
2c9fe835 | 3908 | int chunk_reserved = 0; |
ec44a35c | 3909 | |
ec44a35c | 3910 | path = btrfs_alloc_path(); |
17e9f796 MF |
3911 | if (!path) { |
3912 | ret = -ENOMEM; | |
3913 | goto error; | |
3914 | } | |
19a39dce ID |
3915 | |
3916 | /* zero out stat counters */ | |
3917 | spin_lock(&fs_info->balance_lock); | |
3918 | memset(&bctl->stat, 0, sizeof(bctl->stat)); | |
3919 | spin_unlock(&fs_info->balance_lock); | |
3920 | again: | |
7d824b6f | 3921 | if (!counting) { |
12907fc7 DS |
3922 | /* |
3923 | * The single value limit and min/max limits use the same bytes | |
3924 | * in the | |
3925 | */ | |
7d824b6f DS |
3926 | bctl->data.limit = limit_data; |
3927 | bctl->meta.limit = limit_meta; | |
3928 | bctl->sys.limit = limit_sys; | |
3929 | } | |
ec44a35c CM |
3930 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
3931 | key.offset = (u64)-1; | |
3932 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
3933 | ||
d397712b | 3934 | while (1) { |
19a39dce | 3935 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
a7e99c69 | 3936 | atomic_read(&fs_info->balance_cancel_req)) { |
837d5b6e ID |
3937 | ret = -ECANCELED; |
3938 | goto error; | |
3939 | } | |
3940 | ||
f3372065 | 3941 | mutex_lock(&fs_info->reclaim_bgs_lock); |
ec44a35c | 3942 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
67c5e7d4 | 3943 | if (ret < 0) { |
f3372065 | 3944 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
ec44a35c | 3945 | goto error; |
67c5e7d4 | 3946 | } |
ec44a35c CM |
3947 | |
3948 | /* | |
3949 | * this shouldn't happen, it means the last relocate | |
3950 | * failed | |
3951 | */ | |
3952 | if (ret == 0) | |
c9e9f97b | 3953 | BUG(); /* FIXME break ? */ |
ec44a35c CM |
3954 | |
3955 | ret = btrfs_previous_item(chunk_root, path, 0, | |
3956 | BTRFS_CHUNK_ITEM_KEY); | |
c9e9f97b | 3957 | if (ret) { |
f3372065 | 3958 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
c9e9f97b | 3959 | ret = 0; |
ec44a35c | 3960 | break; |
c9e9f97b | 3961 | } |
7d9eb12c | 3962 | |
f43ffb60 ID |
3963 | leaf = path->nodes[0]; |
3964 | slot = path->slots[0]; | |
3965 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
7d9eb12c | 3966 | |
67c5e7d4 | 3967 | if (found_key.objectid != key.objectid) { |
f3372065 | 3968 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
ec44a35c | 3969 | break; |
67c5e7d4 | 3970 | } |
7d9eb12c | 3971 | |
f43ffb60 | 3972 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
12907fc7 | 3973 | chunk_type = btrfs_chunk_type(leaf, chunk); |
f43ffb60 | 3974 | |
19a39dce ID |
3975 | if (!counting) { |
3976 | spin_lock(&fs_info->balance_lock); | |
3977 | bctl->stat.considered++; | |
3978 | spin_unlock(&fs_info->balance_lock); | |
3979 | } | |
3980 | ||
6ec0896c | 3981 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
2c9fe835 | 3982 | |
b3b4aa74 | 3983 | btrfs_release_path(path); |
67c5e7d4 | 3984 | if (!ret) { |
f3372065 | 3985 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
f43ffb60 | 3986 | goto loop; |
67c5e7d4 | 3987 | } |
f43ffb60 | 3988 | |
19a39dce | 3989 | if (counting) { |
f3372065 | 3990 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
19a39dce ID |
3991 | spin_lock(&fs_info->balance_lock); |
3992 | bctl->stat.expected++; | |
3993 | spin_unlock(&fs_info->balance_lock); | |
12907fc7 DS |
3994 | |
3995 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) | |
3996 | count_data++; | |
3997 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) | |
3998 | count_sys++; | |
3999 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) | |
4000 | count_meta++; | |
4001 | ||
4002 | goto loop; | |
4003 | } | |
4004 | ||
4005 | /* | |
4006 | * Apply limit_min filter, no need to check if the LIMITS | |
4007 | * filter is used, limit_min is 0 by default | |
4008 | */ | |
4009 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && | |
4010 | count_data < bctl->data.limit_min) | |
4011 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && | |
4012 | count_meta < bctl->meta.limit_min) | |
4013 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && | |
4014 | count_sys < bctl->sys.limit_min)) { | |
f3372065 | 4015 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
19a39dce ID |
4016 | goto loop; |
4017 | } | |
4018 | ||
a6f93c71 LB |
4019 | if (!chunk_reserved) { |
4020 | /* | |
4021 | * We may be relocating the only data chunk we have, | |
4022 | * which could potentially end up with losing data's | |
4023 | * raid profile, so lets allocate an empty one in | |
4024 | * advance. | |
4025 | */ | |
4026 | ret = btrfs_may_alloc_data_chunk(fs_info, | |
4027 | found_key.offset); | |
2c9fe835 | 4028 | if (ret < 0) { |
f3372065 | 4029 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
2c9fe835 | 4030 | goto error; |
a6f93c71 LB |
4031 | } else if (ret == 1) { |
4032 | chunk_reserved = 1; | |
2c9fe835 | 4033 | } |
2c9fe835 ZL |
4034 | } |
4035 | ||
5b4aacef | 4036 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
f3372065 | 4037 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
19a39dce | 4038 | if (ret == -ENOSPC) { |
c9e9f97b | 4039 | enospc_errors++; |
eede2bf3 OS |
4040 | } else if (ret == -ETXTBSY) { |
4041 | btrfs_info(fs_info, | |
4042 | "skipping relocation of block group %llu due to active swapfile", | |
4043 | found_key.offset); | |
4044 | ret = 0; | |
4045 | } else if (ret) { | |
4046 | goto error; | |
19a39dce ID |
4047 | } else { |
4048 | spin_lock(&fs_info->balance_lock); | |
4049 | bctl->stat.completed++; | |
4050 | spin_unlock(&fs_info->balance_lock); | |
4051 | } | |
f43ffb60 | 4052 | loop: |
795a3321 ID |
4053 | if (found_key.offset == 0) |
4054 | break; | |
ba1bf481 | 4055 | key.offset = found_key.offset - 1; |
ec44a35c | 4056 | } |
c9e9f97b | 4057 | |
19a39dce ID |
4058 | if (counting) { |
4059 | btrfs_release_path(path); | |
4060 | counting = false; | |
4061 | goto again; | |
4062 | } | |
ec44a35c CM |
4063 | error: |
4064 | btrfs_free_path(path); | |
c9e9f97b | 4065 | if (enospc_errors) { |
efe120a0 | 4066 | btrfs_info(fs_info, "%d enospc errors during balance", |
5d163e0e | 4067 | enospc_errors); |
c9e9f97b ID |
4068 | if (!ret) |
4069 | ret = -ENOSPC; | |
4070 | } | |
4071 | ||
ec44a35c CM |
4072 | return ret; |
4073 | } | |
4074 | ||
43dd529a DS |
4075 | /* |
4076 | * See if a given profile is valid and reduced. | |
4077 | * | |
4078 | * @flags: profile to validate | |
4079 | * @extended: if true @flags is treated as an extended profile | |
0c460c0d ID |
4080 | */ |
4081 | static int alloc_profile_is_valid(u64 flags, int extended) | |
4082 | { | |
4083 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : | |
4084 | BTRFS_BLOCK_GROUP_PROFILE_MASK); | |
4085 | ||
4086 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; | |
4087 | ||
4088 | /* 1) check that all other bits are zeroed */ | |
4089 | if (flags & ~mask) | |
4090 | return 0; | |
4091 | ||
4092 | /* 2) see if profile is reduced */ | |
4093 | if (flags == 0) | |
4094 | return !extended; /* "0" is valid for usual profiles */ | |
4095 | ||
c1499166 | 4096 | return has_single_bit_set(flags); |
0c460c0d ID |
4097 | } |
4098 | ||
5ba366c3 DS |
4099 | /* |
4100 | * Validate target profile against allowed profiles and return true if it's OK. | |
4101 | * Otherwise print the error message and return false. | |
4102 | */ | |
4103 | static inline int validate_convert_profile(struct btrfs_fs_info *fs_info, | |
4104 | const struct btrfs_balance_args *bargs, | |
4105 | u64 allowed, const char *type) | |
bdcd3c97 | 4106 | { |
5ba366c3 DS |
4107 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
4108 | return true; | |
4109 | ||
4110 | /* Profile is valid and does not have bits outside of the allowed set */ | |
4111 | if (alloc_profile_is_valid(bargs->target, 1) && | |
4112 | (bargs->target & ~allowed) == 0) | |
4113 | return true; | |
4114 | ||
4115 | btrfs_err(fs_info, "balance: invalid convert %s profile %s", | |
4116 | type, btrfs_bg_type_to_raid_name(bargs->target)); | |
4117 | return false; | |
bdcd3c97 AM |
4118 | } |
4119 | ||
56fc37d9 AJ |
4120 | /* |
4121 | * Fill @buf with textual description of balance filter flags @bargs, up to | |
4122 | * @size_buf including the terminating null. The output may be trimmed if it | |
4123 | * does not fit into the provided buffer. | |
4124 | */ | |
4125 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, | |
4126 | u32 size_buf) | |
4127 | { | |
4128 | int ret; | |
4129 | u32 size_bp = size_buf; | |
4130 | char *bp = buf; | |
4131 | u64 flags = bargs->flags; | |
4132 | char tmp_buf[128] = {'\0'}; | |
4133 | ||
4134 | if (!flags) | |
4135 | return; | |
4136 | ||
4137 | #define CHECK_APPEND_NOARG(a) \ | |
4138 | do { \ | |
4139 | ret = snprintf(bp, size_bp, (a)); \ | |
4140 | if (ret < 0 || ret >= size_bp) \ | |
4141 | goto out_overflow; \ | |
4142 | size_bp -= ret; \ | |
4143 | bp += ret; \ | |
4144 | } while (0) | |
4145 | ||
4146 | #define CHECK_APPEND_1ARG(a, v1) \ | |
4147 | do { \ | |
4148 | ret = snprintf(bp, size_bp, (a), (v1)); \ | |
4149 | if (ret < 0 || ret >= size_bp) \ | |
4150 | goto out_overflow; \ | |
4151 | size_bp -= ret; \ | |
4152 | bp += ret; \ | |
4153 | } while (0) | |
4154 | ||
4155 | #define CHECK_APPEND_2ARG(a, v1, v2) \ | |
4156 | do { \ | |
4157 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ | |
4158 | if (ret < 0 || ret >= size_bp) \ | |
4159 | goto out_overflow; \ | |
4160 | size_bp -= ret; \ | |
4161 | bp += ret; \ | |
4162 | } while (0) | |
4163 | ||
158da513 DS |
4164 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
4165 | CHECK_APPEND_1ARG("convert=%s,", | |
4166 | btrfs_bg_type_to_raid_name(bargs->target)); | |
56fc37d9 AJ |
4167 | |
4168 | if (flags & BTRFS_BALANCE_ARGS_SOFT) | |
4169 | CHECK_APPEND_NOARG("soft,"); | |
4170 | ||
4171 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { | |
4172 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, | |
4173 | sizeof(tmp_buf)); | |
4174 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); | |
4175 | } | |
4176 | ||
4177 | if (flags & BTRFS_BALANCE_ARGS_USAGE) | |
4178 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); | |
4179 | ||
4180 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) | |
4181 | CHECK_APPEND_2ARG("usage=%u..%u,", | |
4182 | bargs->usage_min, bargs->usage_max); | |
4183 | ||
4184 | if (flags & BTRFS_BALANCE_ARGS_DEVID) | |
4185 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); | |
4186 | ||
4187 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) | |
4188 | CHECK_APPEND_2ARG("drange=%llu..%llu,", | |
4189 | bargs->pstart, bargs->pend); | |
4190 | ||
4191 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) | |
4192 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", | |
4193 | bargs->vstart, bargs->vend); | |
4194 | ||
4195 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) | |
4196 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); | |
4197 | ||
4198 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) | |
4199 | CHECK_APPEND_2ARG("limit=%u..%u,", | |
4200 | bargs->limit_min, bargs->limit_max); | |
4201 | ||
4202 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) | |
4203 | CHECK_APPEND_2ARG("stripes=%u..%u,", | |
4204 | bargs->stripes_min, bargs->stripes_max); | |
4205 | ||
4206 | #undef CHECK_APPEND_2ARG | |
4207 | #undef CHECK_APPEND_1ARG | |
4208 | #undef CHECK_APPEND_NOARG | |
4209 | ||
4210 | out_overflow: | |
4211 | ||
4212 | if (size_bp < size_buf) | |
4213 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ | |
4214 | else | |
4215 | buf[0] = '\0'; | |
4216 | } | |
4217 | ||
4218 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) | |
4219 | { | |
4220 | u32 size_buf = 1024; | |
4221 | char tmp_buf[192] = {'\0'}; | |
4222 | char *buf; | |
4223 | char *bp; | |
4224 | u32 size_bp = size_buf; | |
4225 | int ret; | |
4226 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
4227 | ||
4228 | buf = kzalloc(size_buf, GFP_KERNEL); | |
4229 | if (!buf) | |
4230 | return; | |
4231 | ||
4232 | bp = buf; | |
4233 | ||
4234 | #define CHECK_APPEND_1ARG(a, v1) \ | |
4235 | do { \ | |
4236 | ret = snprintf(bp, size_bp, (a), (v1)); \ | |
4237 | if (ret < 0 || ret >= size_bp) \ | |
4238 | goto out_overflow; \ | |
4239 | size_bp -= ret; \ | |
4240 | bp += ret; \ | |
4241 | } while (0) | |
4242 | ||
4243 | if (bctl->flags & BTRFS_BALANCE_FORCE) | |
4244 | CHECK_APPEND_1ARG("%s", "-f "); | |
4245 | ||
4246 | if (bctl->flags & BTRFS_BALANCE_DATA) { | |
4247 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); | |
4248 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); | |
4249 | } | |
4250 | ||
4251 | if (bctl->flags & BTRFS_BALANCE_METADATA) { | |
4252 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); | |
4253 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); | |
4254 | } | |
4255 | ||
4256 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { | |
4257 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); | |
4258 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); | |
4259 | } | |
4260 | ||
4261 | #undef CHECK_APPEND_1ARG | |
4262 | ||
4263 | out_overflow: | |
4264 | ||
4265 | if (size_bp < size_buf) | |
4266 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ | |
4267 | btrfs_info(fs_info, "balance: %s %s", | |
4268 | (bctl->flags & BTRFS_BALANCE_RESUME) ? | |
4269 | "resume" : "start", buf); | |
4270 | ||
4271 | kfree(buf); | |
4272 | } | |
4273 | ||
c9e9f97b | 4274 | /* |
dccdb07b | 4275 | * Should be called with balance mutexe held |
c9e9f97b | 4276 | */ |
6fcf6e2b DS |
4277 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
4278 | struct btrfs_balance_control *bctl, | |
c9e9f97b ID |
4279 | struct btrfs_ioctl_balance_args *bargs) |
4280 | { | |
14506127 | 4281 | u64 meta_target, data_target; |
f43ffb60 | 4282 | u64 allowed; |
e4837f8f | 4283 | int mixed = 0; |
c9e9f97b | 4284 | int ret; |
8dabb742 | 4285 | u64 num_devices; |
de98ced9 | 4286 | unsigned seq; |
e62869be | 4287 | bool reducing_redundancy; |
b19c98f2 | 4288 | bool paused = false; |
081db89b | 4289 | int i; |
c9e9f97b | 4290 | |
837d5b6e | 4291 | if (btrfs_fs_closing(fs_info) || |
a7e99c69 | 4292 | atomic_read(&fs_info->balance_pause_req) || |
726a3421 | 4293 | btrfs_should_cancel_balance(fs_info)) { |
c9e9f97b ID |
4294 | ret = -EINVAL; |
4295 | goto out; | |
4296 | } | |
4297 | ||
e4837f8f ID |
4298 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
4299 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) | |
4300 | mixed = 1; | |
4301 | ||
f43ffb60 ID |
4302 | /* |
4303 | * In case of mixed groups both data and meta should be picked, | |
4304 | * and identical options should be given for both of them. | |
4305 | */ | |
e4837f8f ID |
4306 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
4307 | if (mixed && (bctl->flags & allowed)) { | |
f43ffb60 ID |
4308 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
4309 | !(bctl->flags & BTRFS_BALANCE_METADATA) || | |
4310 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { | |
5d163e0e | 4311 | btrfs_err(fs_info, |
6dac13f8 | 4312 | "balance: mixed groups data and metadata options must be the same"); |
f43ffb60 ID |
4313 | ret = -EINVAL; |
4314 | goto out; | |
4315 | } | |
4316 | } | |
4317 | ||
b35cf1f0 JB |
4318 | /* |
4319 | * rw_devices will not change at the moment, device add/delete/replace | |
c3e1f96c | 4320 | * are exclusive |
b35cf1f0 JB |
4321 | */ |
4322 | num_devices = fs_info->fs_devices->rw_devices; | |
fab27359 QW |
4323 | |
4324 | /* | |
4325 | * SINGLE profile on-disk has no profile bit, but in-memory we have a | |
4326 | * special bit for it, to make it easier to distinguish. Thus we need | |
4327 | * to set it manually, or balance would refuse the profile. | |
4328 | */ | |
4329 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; | |
081db89b DS |
4330 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
4331 | if (num_devices >= btrfs_raid_array[i].devs_min) | |
4332 | allowed |= btrfs_raid_array[i].bg_flag; | |
1da73967 | 4333 | |
5ba366c3 DS |
4334 | if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") || |
4335 | !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") || | |
4336 | !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) { | |
e4d8ec0f ID |
4337 | ret = -EINVAL; |
4338 | goto out; | |
4339 | } | |
4340 | ||
6079e12c DS |
4341 | /* |
4342 | * Allow to reduce metadata or system integrity only if force set for | |
4343 | * profiles with redundancy (copies, parity) | |
4344 | */ | |
4345 | allowed = 0; | |
4346 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { | |
4347 | if (btrfs_raid_array[i].ncopies >= 2 || | |
4348 | btrfs_raid_array[i].tolerated_failures >= 1) | |
4349 | allowed |= btrfs_raid_array[i].bg_flag; | |
4350 | } | |
de98ced9 MX |
4351 | do { |
4352 | seq = read_seqbegin(&fs_info->profiles_lock); | |
4353 | ||
4354 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
4355 | (fs_info->avail_system_alloc_bits & allowed) && | |
4356 | !(bctl->sys.target & allowed)) || | |
4357 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
4358 | (fs_info->avail_metadata_alloc_bits & allowed) && | |
5a8067c0 | 4359 | !(bctl->meta.target & allowed))) |
e62869be | 4360 | reducing_redundancy = true; |
5a8067c0 | 4361 | else |
e62869be | 4362 | reducing_redundancy = false; |
5a8067c0 FM |
4363 | |
4364 | /* if we're not converting, the target field is uninitialized */ | |
4365 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? | |
4366 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; | |
4367 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? | |
4368 | bctl->data.target : fs_info->avail_data_alloc_bits; | |
de98ced9 | 4369 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
e4d8ec0f | 4370 | |
e62869be | 4371 | if (reducing_redundancy) { |
5a8067c0 FM |
4372 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
4373 | btrfs_info(fs_info, | |
e62869be | 4374 | "balance: force reducing metadata redundancy"); |
5a8067c0 FM |
4375 | } else { |
4376 | btrfs_err(fs_info, | |
e62869be | 4377 | "balance: reduces metadata redundancy, use --force if you want this"); |
5a8067c0 FM |
4378 | ret = -EINVAL; |
4379 | goto out; | |
4380 | } | |
4381 | } | |
4382 | ||
14506127 AB |
4383 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
4384 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { | |
ee592d07 | 4385 | btrfs_warn(fs_info, |
6dac13f8 | 4386 | "balance: metadata profile %s has lower redundancy than data profile %s", |
158da513 DS |
4387 | btrfs_bg_type_to_raid_name(meta_target), |
4388 | btrfs_bg_type_to_raid_name(data_target)); | |
ee592d07 ST |
4389 | } |
4390 | ||
6bccf3ab | 4391 | ret = insert_balance_item(fs_info, bctl); |
59641015 | 4392 | if (ret && ret != -EEXIST) |
0940ebf6 ID |
4393 | goto out; |
4394 | ||
59641015 ID |
4395 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
4396 | BUG_ON(ret == -EEXIST); | |
833aae18 DS |
4397 | BUG_ON(fs_info->balance_ctl); |
4398 | spin_lock(&fs_info->balance_lock); | |
4399 | fs_info->balance_ctl = bctl; | |
4400 | spin_unlock(&fs_info->balance_lock); | |
59641015 ID |
4401 | } else { |
4402 | BUG_ON(ret != -EEXIST); | |
4403 | spin_lock(&fs_info->balance_lock); | |
4404 | update_balance_args(bctl); | |
4405 | spin_unlock(&fs_info->balance_lock); | |
4406 | } | |
c9e9f97b | 4407 | |
3009a62f DS |
4408 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
4409 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); | |
56fc37d9 | 4410 | describe_balance_start_or_resume(fs_info); |
c9e9f97b ID |
4411 | mutex_unlock(&fs_info->balance_mutex); |
4412 | ||
4413 | ret = __btrfs_balance(fs_info); | |
4414 | ||
4415 | mutex_lock(&fs_info->balance_mutex); | |
efc0e69c | 4416 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) { |
7333bd02 | 4417 | btrfs_info(fs_info, "balance: paused"); |
efc0e69c | 4418 | btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED); |
b19c98f2 | 4419 | paused = true; |
efc0e69c | 4420 | } |
44d354ab QW |
4421 | /* |
4422 | * Balance can be canceled by: | |
4423 | * | |
4424 | * - Regular cancel request | |
4425 | * Then ret == -ECANCELED and balance_cancel_req > 0 | |
4426 | * | |
4427 | * - Fatal signal to "btrfs" process | |
4428 | * Either the signal caught by wait_reserve_ticket() and callers | |
4429 | * got -EINTR, or caught by btrfs_should_cancel_balance() and | |
4430 | * got -ECANCELED. | |
4431 | * Either way, in this case balance_cancel_req = 0, and | |
4432 | * ret == -EINTR or ret == -ECANCELED. | |
4433 | * | |
4434 | * So here we only check the return value to catch canceled balance. | |
4435 | */ | |
4436 | else if (ret == -ECANCELED || ret == -EINTR) | |
7333bd02 AJ |
4437 | btrfs_info(fs_info, "balance: canceled"); |
4438 | else | |
4439 | btrfs_info(fs_info, "balance: ended with status: %d", ret); | |
4440 | ||
3009a62f | 4441 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
c9e9f97b ID |
4442 | |
4443 | if (bargs) { | |
4444 | memset(bargs, 0, sizeof(*bargs)); | |
008ef096 | 4445 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
c9e9f97b ID |
4446 | } |
4447 | ||
b19c98f2 JB |
4448 | /* We didn't pause, we can clean everything up. */ |
4449 | if (!paused) { | |
149196a2 | 4450 | reset_balance_state(fs_info); |
c3e1f96c | 4451 | btrfs_exclop_finish(fs_info); |
3a01aa7a ID |
4452 | } |
4453 | ||
837d5b6e | 4454 | wake_up(&fs_info->balance_wait_q); |
c9e9f97b ID |
4455 | |
4456 | return ret; | |
4457 | out: | |
59641015 | 4458 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
149196a2 | 4459 | reset_balance_state(fs_info); |
a17c95df | 4460 | else |
59641015 | 4461 | kfree(bctl); |
c3e1f96c | 4462 | btrfs_exclop_finish(fs_info); |
a17c95df | 4463 | |
59641015 ID |
4464 | return ret; |
4465 | } | |
4466 | ||
4467 | static int balance_kthread(void *data) | |
4468 | { | |
2b6ba629 | 4469 | struct btrfs_fs_info *fs_info = data; |
9555c6c1 | 4470 | int ret = 0; |
59641015 | 4471 | |
a690e5f2 | 4472 | sb_start_write(fs_info->sb); |
59641015 | 4473 | mutex_lock(&fs_info->balance_mutex); |
56fc37d9 | 4474 | if (fs_info->balance_ctl) |
6fcf6e2b | 4475 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
59641015 | 4476 | mutex_unlock(&fs_info->balance_mutex); |
a690e5f2 | 4477 | sb_end_write(fs_info->sb); |
2b6ba629 | 4478 | |
59641015 ID |
4479 | return ret; |
4480 | } | |
4481 | ||
2b6ba629 ID |
4482 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
4483 | { | |
4484 | struct task_struct *tsk; | |
4485 | ||
1354e1a1 | 4486 | mutex_lock(&fs_info->balance_mutex); |
2b6ba629 | 4487 | if (!fs_info->balance_ctl) { |
1354e1a1 | 4488 | mutex_unlock(&fs_info->balance_mutex); |
2b6ba629 ID |
4489 | return 0; |
4490 | } | |
1354e1a1 | 4491 | mutex_unlock(&fs_info->balance_mutex); |
2b6ba629 | 4492 | |
3cdde224 | 4493 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
6dac13f8 | 4494 | btrfs_info(fs_info, "balance: resume skipped"); |
2b6ba629 ID |
4495 | return 0; |
4496 | } | |
4497 | ||
efc0e69c NB |
4498 | spin_lock(&fs_info->super_lock); |
4499 | ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); | |
4500 | fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; | |
4501 | spin_unlock(&fs_info->super_lock); | |
02ee654d AJ |
4502 | /* |
4503 | * A ro->rw remount sequence should continue with the paused balance | |
4504 | * regardless of who pauses it, system or the user as of now, so set | |
4505 | * the resume flag. | |
4506 | */ | |
4507 | spin_lock(&fs_info->balance_lock); | |
4508 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; | |
4509 | spin_unlock(&fs_info->balance_lock); | |
4510 | ||
2b6ba629 | 4511 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
cd633972 | 4512 | return PTR_ERR_OR_ZERO(tsk); |
2b6ba629 ID |
4513 | } |
4514 | ||
68310a5e | 4515 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
59641015 | 4516 | { |
59641015 ID |
4517 | struct btrfs_balance_control *bctl; |
4518 | struct btrfs_balance_item *item; | |
4519 | struct btrfs_disk_balance_args disk_bargs; | |
4520 | struct btrfs_path *path; | |
4521 | struct extent_buffer *leaf; | |
4522 | struct btrfs_key key; | |
4523 | int ret; | |
4524 | ||
4525 | path = btrfs_alloc_path(); | |
4526 | if (!path) | |
4527 | return -ENOMEM; | |
4528 | ||
59641015 | 4529 | key.objectid = BTRFS_BALANCE_OBJECTID; |
c479cb4f | 4530 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
59641015 ID |
4531 | key.offset = 0; |
4532 | ||
68310a5e | 4533 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
59641015 | 4534 | if (ret < 0) |
68310a5e | 4535 | goto out; |
59641015 ID |
4536 | if (ret > 0) { /* ret = -ENOENT; */ |
4537 | ret = 0; | |
68310a5e ID |
4538 | goto out; |
4539 | } | |
4540 | ||
4541 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); | |
4542 | if (!bctl) { | |
4543 | ret = -ENOMEM; | |
4544 | goto out; | |
59641015 ID |
4545 | } |
4546 | ||
4547 | leaf = path->nodes[0]; | |
4548 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); | |
4549 | ||
68310a5e ID |
4550 | bctl->flags = btrfs_balance_flags(leaf, item); |
4551 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
59641015 ID |
4552 | |
4553 | btrfs_balance_data(leaf, item, &disk_bargs); | |
4554 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); | |
4555 | btrfs_balance_meta(leaf, item, &disk_bargs); | |
4556 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); | |
4557 | btrfs_balance_sys(leaf, item, &disk_bargs); | |
4558 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); | |
4559 | ||
eee95e3f DS |
4560 | /* |
4561 | * This should never happen, as the paused balance state is recovered | |
4562 | * during mount without any chance of other exclusive ops to collide. | |
4563 | * | |
4564 | * This gives the exclusive op status to balance and keeps in paused | |
4565 | * state until user intervention (cancel or umount). If the ownership | |
4566 | * cannot be assigned, show a message but do not fail. The balance | |
4567 | * is in a paused state and must have fs_info::balance_ctl properly | |
4568 | * set up. | |
4569 | */ | |
efc0e69c | 4570 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED)) |
eee95e3f | 4571 | btrfs_warn(fs_info, |
6dac13f8 | 4572 | "balance: cannot set exclusive op status, resume manually"); |
ed0fb78f | 4573 | |
fb286100 JB |
4574 | btrfs_release_path(path); |
4575 | ||
68310a5e | 4576 | mutex_lock(&fs_info->balance_mutex); |
833aae18 DS |
4577 | BUG_ON(fs_info->balance_ctl); |
4578 | spin_lock(&fs_info->balance_lock); | |
4579 | fs_info->balance_ctl = bctl; | |
4580 | spin_unlock(&fs_info->balance_lock); | |
68310a5e | 4581 | mutex_unlock(&fs_info->balance_mutex); |
59641015 ID |
4582 | out: |
4583 | btrfs_free_path(path); | |
ec44a35c CM |
4584 | return ret; |
4585 | } | |
4586 | ||
837d5b6e ID |
4587 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
4588 | { | |
4589 | int ret = 0; | |
4590 | ||
4591 | mutex_lock(&fs_info->balance_mutex); | |
4592 | if (!fs_info->balance_ctl) { | |
4593 | mutex_unlock(&fs_info->balance_mutex); | |
4594 | return -ENOTCONN; | |
4595 | } | |
4596 | ||
3009a62f | 4597 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
837d5b6e ID |
4598 | atomic_inc(&fs_info->balance_pause_req); |
4599 | mutex_unlock(&fs_info->balance_mutex); | |
4600 | ||
4601 | wait_event(fs_info->balance_wait_q, | |
3009a62f | 4602 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
837d5b6e ID |
4603 | |
4604 | mutex_lock(&fs_info->balance_mutex); | |
4605 | /* we are good with balance_ctl ripped off from under us */ | |
3009a62f | 4606 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
837d5b6e ID |
4607 | atomic_dec(&fs_info->balance_pause_req); |
4608 | } else { | |
4609 | ret = -ENOTCONN; | |
4610 | } | |
4611 | ||
4612 | mutex_unlock(&fs_info->balance_mutex); | |
4613 | return ret; | |
4614 | } | |
4615 | ||
a7e99c69 ID |
4616 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
4617 | { | |
4618 | mutex_lock(&fs_info->balance_mutex); | |
4619 | if (!fs_info->balance_ctl) { | |
4620 | mutex_unlock(&fs_info->balance_mutex); | |
4621 | return -ENOTCONN; | |
4622 | } | |
4623 | ||
cf7d20f4 DS |
4624 | /* |
4625 | * A paused balance with the item stored on disk can be resumed at | |
4626 | * mount time if the mount is read-write. Otherwise it's still paused | |
4627 | * and we must not allow cancelling as it deletes the item. | |
4628 | */ | |
4629 | if (sb_rdonly(fs_info->sb)) { | |
4630 | mutex_unlock(&fs_info->balance_mutex); | |
4631 | return -EROFS; | |
4632 | } | |
4633 | ||
a7e99c69 ID |
4634 | atomic_inc(&fs_info->balance_cancel_req); |
4635 | /* | |
4636 | * if we are running just wait and return, balance item is | |
4637 | * deleted in btrfs_balance in this case | |
4638 | */ | |
3009a62f | 4639 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
a7e99c69 ID |
4640 | mutex_unlock(&fs_info->balance_mutex); |
4641 | wait_event(fs_info->balance_wait_q, | |
3009a62f | 4642 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
a7e99c69 ID |
4643 | mutex_lock(&fs_info->balance_mutex); |
4644 | } else { | |
a7e99c69 | 4645 | mutex_unlock(&fs_info->balance_mutex); |
dccdb07b DS |
4646 | /* |
4647 | * Lock released to allow other waiters to continue, we'll | |
4648 | * reexamine the status again. | |
4649 | */ | |
a7e99c69 ID |
4650 | mutex_lock(&fs_info->balance_mutex); |
4651 | ||
a17c95df | 4652 | if (fs_info->balance_ctl) { |
149196a2 | 4653 | reset_balance_state(fs_info); |
c3e1f96c | 4654 | btrfs_exclop_finish(fs_info); |
6dac13f8 | 4655 | btrfs_info(fs_info, "balance: canceled"); |
a17c95df | 4656 | } |
a7e99c69 ID |
4657 | } |
4658 | ||
29eefa6d | 4659 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
a7e99c69 ID |
4660 | atomic_dec(&fs_info->balance_cancel_req); |
4661 | mutex_unlock(&fs_info->balance_mutex); | |
4662 | return 0; | |
4663 | } | |
4664 | ||
97f4dd09 | 4665 | int btrfs_uuid_scan_kthread(void *data) |
803b2f54 SB |
4666 | { |
4667 | struct btrfs_fs_info *fs_info = data; | |
4668 | struct btrfs_root *root = fs_info->tree_root; | |
4669 | struct btrfs_key key; | |
803b2f54 SB |
4670 | struct btrfs_path *path = NULL; |
4671 | int ret = 0; | |
4672 | struct extent_buffer *eb; | |
4673 | int slot; | |
4674 | struct btrfs_root_item root_item; | |
4675 | u32 item_size; | |
f45388f3 | 4676 | struct btrfs_trans_handle *trans = NULL; |
c94bec2c | 4677 | bool closing = false; |
803b2f54 SB |
4678 | |
4679 | path = btrfs_alloc_path(); | |
4680 | if (!path) { | |
4681 | ret = -ENOMEM; | |
4682 | goto out; | |
4683 | } | |
4684 | ||
4685 | key.objectid = 0; | |
4686 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4687 | key.offset = 0; | |
4688 | ||
803b2f54 | 4689 | while (1) { |
c94bec2c JB |
4690 | if (btrfs_fs_closing(fs_info)) { |
4691 | closing = true; | |
4692 | break; | |
4693 | } | |
7c829b72 AJ |
4694 | ret = btrfs_search_forward(root, &key, path, |
4695 | BTRFS_OLDEST_GENERATION); | |
803b2f54 SB |
4696 | if (ret) { |
4697 | if (ret > 0) | |
4698 | ret = 0; | |
4699 | break; | |
4700 | } | |
4701 | ||
4702 | if (key.type != BTRFS_ROOT_ITEM_KEY || | |
4703 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && | |
4704 | key.objectid != BTRFS_FS_TREE_OBJECTID) || | |
4705 | key.objectid > BTRFS_LAST_FREE_OBJECTID) | |
4706 | goto skip; | |
4707 | ||
4708 | eb = path->nodes[0]; | |
4709 | slot = path->slots[0]; | |
3212fa14 | 4710 | item_size = btrfs_item_size(eb, slot); |
803b2f54 SB |
4711 | if (item_size < sizeof(root_item)) |
4712 | goto skip; | |
4713 | ||
803b2f54 SB |
4714 | read_extent_buffer(eb, &root_item, |
4715 | btrfs_item_ptr_offset(eb, slot), | |
4716 | (int)sizeof(root_item)); | |
4717 | if (btrfs_root_refs(&root_item) == 0) | |
4718 | goto skip; | |
f45388f3 FDBM |
4719 | |
4720 | if (!btrfs_is_empty_uuid(root_item.uuid) || | |
4721 | !btrfs_is_empty_uuid(root_item.received_uuid)) { | |
4722 | if (trans) | |
4723 | goto update_tree; | |
4724 | ||
4725 | btrfs_release_path(path); | |
803b2f54 SB |
4726 | /* |
4727 | * 1 - subvol uuid item | |
4728 | * 1 - received_subvol uuid item | |
4729 | */ | |
4730 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); | |
4731 | if (IS_ERR(trans)) { | |
4732 | ret = PTR_ERR(trans); | |
4733 | break; | |
4734 | } | |
f45388f3 FDBM |
4735 | continue; |
4736 | } else { | |
4737 | goto skip; | |
4738 | } | |
4739 | update_tree: | |
9771a5cf | 4740 | btrfs_release_path(path); |
f45388f3 | 4741 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
cdb345a8 | 4742 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
803b2f54 SB |
4743 | BTRFS_UUID_KEY_SUBVOL, |
4744 | key.objectid); | |
4745 | if (ret < 0) { | |
efe120a0 | 4746 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
803b2f54 | 4747 | ret); |
803b2f54 SB |
4748 | break; |
4749 | } | |
4750 | } | |
4751 | ||
4752 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { | |
cdb345a8 | 4753 | ret = btrfs_uuid_tree_add(trans, |
803b2f54 SB |
4754 | root_item.received_uuid, |
4755 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4756 | key.objectid); | |
4757 | if (ret < 0) { | |
efe120a0 | 4758 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
803b2f54 | 4759 | ret); |
803b2f54 SB |
4760 | break; |
4761 | } | |
4762 | } | |
4763 | ||
f45388f3 | 4764 | skip: |
9771a5cf | 4765 | btrfs_release_path(path); |
803b2f54 | 4766 | if (trans) { |
3a45bb20 | 4767 | ret = btrfs_end_transaction(trans); |
f45388f3 | 4768 | trans = NULL; |
803b2f54 SB |
4769 | if (ret) |
4770 | break; | |
4771 | } | |
4772 | ||
803b2f54 SB |
4773 | if (key.offset < (u64)-1) { |
4774 | key.offset++; | |
4775 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { | |
4776 | key.offset = 0; | |
4777 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4778 | } else if (key.objectid < (u64)-1) { | |
4779 | key.offset = 0; | |
4780 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4781 | key.objectid++; | |
4782 | } else { | |
4783 | break; | |
4784 | } | |
4785 | cond_resched(); | |
4786 | } | |
4787 | ||
4788 | out: | |
4789 | btrfs_free_path(path); | |
f45388f3 | 4790 | if (trans && !IS_ERR(trans)) |
3a45bb20 | 4791 | btrfs_end_transaction(trans); |
803b2f54 | 4792 | if (ret) |
efe120a0 | 4793 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
c94bec2c | 4794 | else if (!closing) |
afcdd129 | 4795 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
803b2f54 SB |
4796 | up(&fs_info->uuid_tree_rescan_sem); |
4797 | return 0; | |
4798 | } | |
4799 | ||
f7a81ea4 SB |
4800 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
4801 | { | |
4802 | struct btrfs_trans_handle *trans; | |
4803 | struct btrfs_root *tree_root = fs_info->tree_root; | |
4804 | struct btrfs_root *uuid_root; | |
803b2f54 SB |
4805 | struct task_struct *task; |
4806 | int ret; | |
f7a81ea4 SB |
4807 | |
4808 | /* | |
4809 | * 1 - root node | |
4810 | * 1 - root item | |
4811 | */ | |
4812 | trans = btrfs_start_transaction(tree_root, 2); | |
4813 | if (IS_ERR(trans)) | |
4814 | return PTR_ERR(trans); | |
4815 | ||
9b7a2440 | 4816 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
f7a81ea4 | 4817 | if (IS_ERR(uuid_root)) { |
6d13f549 | 4818 | ret = PTR_ERR(uuid_root); |
66642832 | 4819 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4820 | btrfs_end_transaction(trans); |
6d13f549 | 4821 | return ret; |
f7a81ea4 SB |
4822 | } |
4823 | ||
4824 | fs_info->uuid_root = uuid_root; | |
4825 | ||
3a45bb20 | 4826 | ret = btrfs_commit_transaction(trans); |
803b2f54 SB |
4827 | if (ret) |
4828 | return ret; | |
4829 | ||
4830 | down(&fs_info->uuid_tree_rescan_sem); | |
4831 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); | |
4832 | if (IS_ERR(task)) { | |
70f80175 | 4833 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
efe120a0 | 4834 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
803b2f54 SB |
4835 | up(&fs_info->uuid_tree_rescan_sem); |
4836 | return PTR_ERR(task); | |
4837 | } | |
4838 | ||
4839 | return 0; | |
f7a81ea4 | 4840 | } |
803b2f54 | 4841 | |
8f18cf13 CM |
4842 | /* |
4843 | * shrinking a device means finding all of the device extents past | |
4844 | * the new size, and then following the back refs to the chunks. | |
4845 | * The chunk relocation code actually frees the device extent | |
4846 | */ | |
4847 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) | |
4848 | { | |
0b246afa JM |
4849 | struct btrfs_fs_info *fs_info = device->fs_info; |
4850 | struct btrfs_root *root = fs_info->dev_root; | |
8f18cf13 | 4851 | struct btrfs_trans_handle *trans; |
8f18cf13 CM |
4852 | struct btrfs_dev_extent *dev_extent = NULL; |
4853 | struct btrfs_path *path; | |
4854 | u64 length; | |
8f18cf13 CM |
4855 | u64 chunk_offset; |
4856 | int ret; | |
4857 | int slot; | |
ba1bf481 JB |
4858 | int failed = 0; |
4859 | bool retried = false; | |
8f18cf13 CM |
4860 | struct extent_buffer *l; |
4861 | struct btrfs_key key; | |
0b246afa | 4862 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
8f18cf13 | 4863 | u64 old_total = btrfs_super_total_bytes(super_copy); |
7cc8e58d | 4864 | u64 old_size = btrfs_device_get_total_bytes(device); |
7dfb8be1 | 4865 | u64 diff; |
61d0d0d2 | 4866 | u64 start; |
7dfb8be1 NB |
4867 | |
4868 | new_size = round_down(new_size, fs_info->sectorsize); | |
61d0d0d2 | 4869 | start = new_size; |
0e4324a4 | 4870 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
8f18cf13 | 4871 | |
401e29c1 | 4872 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
63a212ab SB |
4873 | return -EINVAL; |
4874 | ||
8f18cf13 CM |
4875 | path = btrfs_alloc_path(); |
4876 | if (!path) | |
4877 | return -ENOMEM; | |
4878 | ||
0338dff6 | 4879 | path->reada = READA_BACK; |
8f18cf13 | 4880 | |
61d0d0d2 NB |
4881 | trans = btrfs_start_transaction(root, 0); |
4882 | if (IS_ERR(trans)) { | |
4883 | btrfs_free_path(path); | |
4884 | return PTR_ERR(trans); | |
4885 | } | |
4886 | ||
34441361 | 4887 | mutex_lock(&fs_info->chunk_mutex); |
7d9eb12c | 4888 | |
7cc8e58d | 4889 | btrfs_device_set_total_bytes(device, new_size); |
ebbede42 | 4890 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
2b82032c | 4891 | device->fs_devices->total_rw_bytes -= diff; |
a5ed45f8 | 4892 | atomic64_sub(diff, &fs_info->free_chunk_space); |
2bf64758 | 4893 | } |
61d0d0d2 NB |
4894 | |
4895 | /* | |
4896 | * Once the device's size has been set to the new size, ensure all | |
4897 | * in-memory chunks are synced to disk so that the loop below sees them | |
4898 | * and relocates them accordingly. | |
4899 | */ | |
1c11b63e | 4900 | if (contains_pending_extent(device, &start, diff)) { |
61d0d0d2 NB |
4901 | mutex_unlock(&fs_info->chunk_mutex); |
4902 | ret = btrfs_commit_transaction(trans); | |
4903 | if (ret) | |
4904 | goto done; | |
4905 | } else { | |
4906 | mutex_unlock(&fs_info->chunk_mutex); | |
4907 | btrfs_end_transaction(trans); | |
4908 | } | |
8f18cf13 | 4909 | |
ba1bf481 | 4910 | again: |
8f18cf13 CM |
4911 | key.objectid = device->devid; |
4912 | key.offset = (u64)-1; | |
4913 | key.type = BTRFS_DEV_EXTENT_KEY; | |
4914 | ||
213e64da | 4915 | do { |
f3372065 | 4916 | mutex_lock(&fs_info->reclaim_bgs_lock); |
8f18cf13 | 4917 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
67c5e7d4 | 4918 | if (ret < 0) { |
f3372065 | 4919 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
8f18cf13 | 4920 | goto done; |
67c5e7d4 | 4921 | } |
8f18cf13 CM |
4922 | |
4923 | ret = btrfs_previous_item(root, path, 0, key.type); | |
8f18cf13 | 4924 | if (ret) { |
f3372065 | 4925 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
7056bf69 NB |
4926 | if (ret < 0) |
4927 | goto done; | |
8f18cf13 | 4928 | ret = 0; |
b3b4aa74 | 4929 | btrfs_release_path(path); |
bf1fb512 | 4930 | break; |
8f18cf13 CM |
4931 | } |
4932 | ||
4933 | l = path->nodes[0]; | |
4934 | slot = path->slots[0]; | |
4935 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
4936 | ||
ba1bf481 | 4937 | if (key.objectid != device->devid) { |
f3372065 | 4938 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
b3b4aa74 | 4939 | btrfs_release_path(path); |
bf1fb512 | 4940 | break; |
ba1bf481 | 4941 | } |
8f18cf13 CM |
4942 | |
4943 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
4944 | length = btrfs_dev_extent_length(l, dev_extent); | |
4945 | ||
ba1bf481 | 4946 | if (key.offset + length <= new_size) { |
f3372065 | 4947 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
b3b4aa74 | 4948 | btrfs_release_path(path); |
d6397bae | 4949 | break; |
ba1bf481 | 4950 | } |
8f18cf13 | 4951 | |
8f18cf13 | 4952 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
b3b4aa74 | 4953 | btrfs_release_path(path); |
8f18cf13 | 4954 | |
a6f93c71 LB |
4955 | /* |
4956 | * We may be relocating the only data chunk we have, | |
4957 | * which could potentially end up with losing data's | |
4958 | * raid profile, so lets allocate an empty one in | |
4959 | * advance. | |
4960 | */ | |
4961 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); | |
4962 | if (ret < 0) { | |
f3372065 | 4963 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
a6f93c71 LB |
4964 | goto done; |
4965 | } | |
4966 | ||
0b246afa | 4967 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
f3372065 | 4968 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
eede2bf3 | 4969 | if (ret == -ENOSPC) { |
ba1bf481 | 4970 | failed++; |
eede2bf3 OS |
4971 | } else if (ret) { |
4972 | if (ret == -ETXTBSY) { | |
4973 | btrfs_warn(fs_info, | |
4974 | "could not shrink block group %llu due to active swapfile", | |
4975 | chunk_offset); | |
4976 | } | |
4977 | goto done; | |
4978 | } | |
213e64da | 4979 | } while (key.offset-- > 0); |
ba1bf481 JB |
4980 | |
4981 | if (failed && !retried) { | |
4982 | failed = 0; | |
4983 | retried = true; | |
4984 | goto again; | |
4985 | } else if (failed && retried) { | |
4986 | ret = -ENOSPC; | |
ba1bf481 | 4987 | goto done; |
8f18cf13 CM |
4988 | } |
4989 | ||
d6397bae | 4990 | /* Shrinking succeeded, else we would be at "done". */ |
a22285a6 | 4991 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
4992 | if (IS_ERR(trans)) { |
4993 | ret = PTR_ERR(trans); | |
4994 | goto done; | |
4995 | } | |
4996 | ||
34441361 | 4997 | mutex_lock(&fs_info->chunk_mutex); |
c57dd1f2 QW |
4998 | /* Clear all state bits beyond the shrunk device size */ |
4999 | clear_extent_bits(&device->alloc_state, new_size, (u64)-1, | |
5000 | CHUNK_STATE_MASK); | |
5001 | ||
7cc8e58d | 5002 | btrfs_device_set_disk_total_bytes(device, new_size); |
bbbf7243 NB |
5003 | if (list_empty(&device->post_commit_list)) |
5004 | list_add_tail(&device->post_commit_list, | |
5005 | &trans->transaction->dev_update_list); | |
d6397bae | 5006 | |
d6397bae | 5007 | WARN_ON(diff > old_total); |
7dfb8be1 NB |
5008 | btrfs_set_super_total_bytes(super_copy, |
5009 | round_down(old_total - diff, fs_info->sectorsize)); | |
34441361 | 5010 | mutex_unlock(&fs_info->chunk_mutex); |
2196d6e8 | 5011 | |
2bb2e00e | 5012 | btrfs_reserve_chunk_metadata(trans, false); |
2196d6e8 MX |
5013 | /* Now btrfs_update_device() will change the on-disk size. */ |
5014 | ret = btrfs_update_device(trans, device); | |
2bb2e00e | 5015 | btrfs_trans_release_chunk_metadata(trans); |
801660b0 AJ |
5016 | if (ret < 0) { |
5017 | btrfs_abort_transaction(trans, ret); | |
5018 | btrfs_end_transaction(trans); | |
5019 | } else { | |
5020 | ret = btrfs_commit_transaction(trans); | |
5021 | } | |
8f18cf13 CM |
5022 | done: |
5023 | btrfs_free_path(path); | |
53e489bc | 5024 | if (ret) { |
34441361 | 5025 | mutex_lock(&fs_info->chunk_mutex); |
53e489bc | 5026 | btrfs_device_set_total_bytes(device, old_size); |
ebbede42 | 5027 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
53e489bc | 5028 | device->fs_devices->total_rw_bytes += diff; |
a5ed45f8 | 5029 | atomic64_add(diff, &fs_info->free_chunk_space); |
34441361 | 5030 | mutex_unlock(&fs_info->chunk_mutex); |
53e489bc | 5031 | } |
8f18cf13 CM |
5032 | return ret; |
5033 | } | |
5034 | ||
2ff7e61e | 5035 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
0b86a832 CM |
5036 | struct btrfs_key *key, |
5037 | struct btrfs_chunk *chunk, int item_size) | |
5038 | { | |
0b246afa | 5039 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
0b86a832 CM |
5040 | struct btrfs_disk_key disk_key; |
5041 | u32 array_size; | |
5042 | u8 *ptr; | |
5043 | ||
79bd3712 FM |
5044 | lockdep_assert_held(&fs_info->chunk_mutex); |
5045 | ||
0b86a832 | 5046 | array_size = btrfs_super_sys_array_size(super_copy); |
5f43f86e | 5047 | if (array_size + item_size + sizeof(disk_key) |
79bd3712 | 5048 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
0b86a832 CM |
5049 | return -EFBIG; |
5050 | ||
5051 | ptr = super_copy->sys_chunk_array + array_size; | |
5052 | btrfs_cpu_key_to_disk(&disk_key, key); | |
5053 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
5054 | ptr += sizeof(disk_key); | |
5055 | memcpy(ptr, chunk, item_size); | |
5056 | item_size += sizeof(disk_key); | |
5057 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
fe48a5c0 | 5058 | |
0b86a832 CM |
5059 | return 0; |
5060 | } | |
5061 | ||
73c5de00 AJ |
5062 | /* |
5063 | * sort the devices in descending order by max_avail, total_avail | |
5064 | */ | |
5065 | static int btrfs_cmp_device_info(const void *a, const void *b) | |
9b3f68b9 | 5066 | { |
73c5de00 AJ |
5067 | const struct btrfs_device_info *di_a = a; |
5068 | const struct btrfs_device_info *di_b = b; | |
9b3f68b9 | 5069 | |
73c5de00 | 5070 | if (di_a->max_avail > di_b->max_avail) |
b2117a39 | 5071 | return -1; |
73c5de00 | 5072 | if (di_a->max_avail < di_b->max_avail) |
b2117a39 | 5073 | return 1; |
73c5de00 AJ |
5074 | if (di_a->total_avail > di_b->total_avail) |
5075 | return -1; | |
5076 | if (di_a->total_avail < di_b->total_avail) | |
5077 | return 1; | |
5078 | return 0; | |
b2117a39 | 5079 | } |
0b86a832 | 5080 | |
53b381b3 DW |
5081 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
5082 | { | |
ffe2d203 | 5083 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
53b381b3 DW |
5084 | return; |
5085 | ||
ceda0864 | 5086 | btrfs_set_fs_incompat(info, RAID56); |
53b381b3 DW |
5087 | } |
5088 | ||
cfbb825c DS |
5089 | static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type) |
5090 | { | |
5091 | if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))) | |
5092 | return; | |
5093 | ||
5094 | btrfs_set_fs_incompat(info, RAID1C34); | |
5095 | } | |
5096 | ||
4f2bafe8 | 5097 | /* |
f6f39f7a | 5098 | * Structure used internally for btrfs_create_chunk() function. |
4f2bafe8 NA |
5099 | * Wraps needed parameters. |
5100 | */ | |
5101 | struct alloc_chunk_ctl { | |
5102 | u64 start; | |
5103 | u64 type; | |
5104 | /* Total number of stripes to allocate */ | |
5105 | int num_stripes; | |
5106 | /* sub_stripes info for map */ | |
5107 | int sub_stripes; | |
5108 | /* Stripes per device */ | |
5109 | int dev_stripes; | |
5110 | /* Maximum number of devices to use */ | |
5111 | int devs_max; | |
5112 | /* Minimum number of devices to use */ | |
5113 | int devs_min; | |
5114 | /* ndevs has to be a multiple of this */ | |
5115 | int devs_increment; | |
5116 | /* Number of copies */ | |
5117 | int ncopies; | |
5118 | /* Number of stripes worth of bytes to store parity information */ | |
5119 | int nparity; | |
5120 | u64 max_stripe_size; | |
5121 | u64 max_chunk_size; | |
6aafb303 | 5122 | u64 dev_extent_min; |
4f2bafe8 NA |
5123 | u64 stripe_size; |
5124 | u64 chunk_size; | |
5125 | int ndevs; | |
5126 | }; | |
5127 | ||
27c314d5 NA |
5128 | static void init_alloc_chunk_ctl_policy_regular( |
5129 | struct btrfs_fs_devices *fs_devices, | |
5130 | struct alloc_chunk_ctl *ctl) | |
5131 | { | |
f6fca391 | 5132 | struct btrfs_space_info *space_info; |
27c314d5 | 5133 | |
f6fca391 SR |
5134 | space_info = btrfs_find_space_info(fs_devices->fs_info, ctl->type); |
5135 | ASSERT(space_info); | |
5136 | ||
5137 | ctl->max_chunk_size = READ_ONCE(space_info->chunk_size); | |
5138 | ctl->max_stripe_size = ctl->max_chunk_size; | |
5139 | ||
5140 | if (ctl->type & BTRFS_BLOCK_GROUP_SYSTEM) | |
5141 | ctl->devs_max = min_t(int, ctl->devs_max, BTRFS_MAX_DEVS_SYS_CHUNK); | |
27c314d5 NA |
5142 | |
5143 | /* We don't want a chunk larger than 10% of writable space */ | |
428c8e03 | 5144 | ctl->max_chunk_size = min(mult_perc(fs_devices->total_rw_bytes, 10), |
27c314d5 | 5145 | ctl->max_chunk_size); |
cb091225 | 5146 | ctl->dev_extent_min = btrfs_stripe_nr_to_offset(ctl->dev_stripes); |
27c314d5 NA |
5147 | } |
5148 | ||
1cd6121f NA |
5149 | static void init_alloc_chunk_ctl_policy_zoned( |
5150 | struct btrfs_fs_devices *fs_devices, | |
5151 | struct alloc_chunk_ctl *ctl) | |
5152 | { | |
5153 | u64 zone_size = fs_devices->fs_info->zone_size; | |
5154 | u64 limit; | |
5155 | int min_num_stripes = ctl->devs_min * ctl->dev_stripes; | |
5156 | int min_data_stripes = (min_num_stripes - ctl->nparity) / ctl->ncopies; | |
5157 | u64 min_chunk_size = min_data_stripes * zone_size; | |
5158 | u64 type = ctl->type; | |
5159 | ||
5160 | ctl->max_stripe_size = zone_size; | |
5161 | if (type & BTRFS_BLOCK_GROUP_DATA) { | |
5162 | ctl->max_chunk_size = round_down(BTRFS_MAX_DATA_CHUNK_SIZE, | |
5163 | zone_size); | |
5164 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { | |
5165 | ctl->max_chunk_size = ctl->max_stripe_size; | |
5166 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { | |
5167 | ctl->max_chunk_size = 2 * ctl->max_stripe_size; | |
5168 | ctl->devs_max = min_t(int, ctl->devs_max, | |
5169 | BTRFS_MAX_DEVS_SYS_CHUNK); | |
bb05b298 AB |
5170 | } else { |
5171 | BUG(); | |
1cd6121f NA |
5172 | } |
5173 | ||
5174 | /* We don't want a chunk larger than 10% of writable space */ | |
428c8e03 | 5175 | limit = max(round_down(mult_perc(fs_devices->total_rw_bytes, 10), |
1cd6121f NA |
5176 | zone_size), |
5177 | min_chunk_size); | |
5178 | ctl->max_chunk_size = min(limit, ctl->max_chunk_size); | |
5179 | ctl->dev_extent_min = zone_size * ctl->dev_stripes; | |
5180 | } | |
5181 | ||
27c314d5 NA |
5182 | static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices, |
5183 | struct alloc_chunk_ctl *ctl) | |
5184 | { | |
5185 | int index = btrfs_bg_flags_to_raid_index(ctl->type); | |
5186 | ||
5187 | ctl->sub_stripes = btrfs_raid_array[index].sub_stripes; | |
5188 | ctl->dev_stripes = btrfs_raid_array[index].dev_stripes; | |
5189 | ctl->devs_max = btrfs_raid_array[index].devs_max; | |
5190 | if (!ctl->devs_max) | |
5191 | ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info); | |
5192 | ctl->devs_min = btrfs_raid_array[index].devs_min; | |
5193 | ctl->devs_increment = btrfs_raid_array[index].devs_increment; | |
5194 | ctl->ncopies = btrfs_raid_array[index].ncopies; | |
5195 | ctl->nparity = btrfs_raid_array[index].nparity; | |
5196 | ctl->ndevs = 0; | |
5197 | ||
5198 | switch (fs_devices->chunk_alloc_policy) { | |
5199 | case BTRFS_CHUNK_ALLOC_REGULAR: | |
5200 | init_alloc_chunk_ctl_policy_regular(fs_devices, ctl); | |
5201 | break; | |
1cd6121f NA |
5202 | case BTRFS_CHUNK_ALLOC_ZONED: |
5203 | init_alloc_chunk_ctl_policy_zoned(fs_devices, ctl); | |
5204 | break; | |
27c314d5 NA |
5205 | default: |
5206 | BUG(); | |
5207 | } | |
5208 | } | |
5209 | ||
560156cb NA |
5210 | static int gather_device_info(struct btrfs_fs_devices *fs_devices, |
5211 | struct alloc_chunk_ctl *ctl, | |
5212 | struct btrfs_device_info *devices_info) | |
b2117a39 | 5213 | { |
560156cb | 5214 | struct btrfs_fs_info *info = fs_devices->fs_info; |
ebcc9301 | 5215 | struct btrfs_device *device; |
73c5de00 | 5216 | u64 total_avail; |
560156cb | 5217 | u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes; |
73c5de00 | 5218 | int ret; |
560156cb NA |
5219 | int ndevs = 0; |
5220 | u64 max_avail; | |
5221 | u64 dev_offset; | |
0cad8a11 | 5222 | |
9f680ce0 | 5223 | /* |
73c5de00 AJ |
5224 | * in the first pass through the devices list, we gather information |
5225 | * about the available holes on each device. | |
9f680ce0 | 5226 | */ |
ebcc9301 | 5227 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
ebbede42 | 5228 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
31b1a2bd | 5229 | WARN(1, KERN_ERR |
efe120a0 | 5230 | "BTRFS: read-only device in alloc_list\n"); |
73c5de00 AJ |
5231 | continue; |
5232 | } | |
b2117a39 | 5233 | |
e12c9621 AJ |
5234 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
5235 | &device->dev_state) || | |
401e29c1 | 5236 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
73c5de00 | 5237 | continue; |
b2117a39 | 5238 | |
73c5de00 AJ |
5239 | if (device->total_bytes > device->bytes_used) |
5240 | total_avail = device->total_bytes - device->bytes_used; | |
5241 | else | |
5242 | total_avail = 0; | |
38c01b96 | 5243 | |
5244 | /* If there is no space on this device, skip it. */ | |
6aafb303 | 5245 | if (total_avail < ctl->dev_extent_min) |
38c01b96 | 5246 | continue; |
b2117a39 | 5247 | |
560156cb NA |
5248 | ret = find_free_dev_extent(device, dev_extent_want, &dev_offset, |
5249 | &max_avail); | |
73c5de00 | 5250 | if (ret && ret != -ENOSPC) |
560156cb | 5251 | return ret; |
b2117a39 | 5252 | |
73c5de00 | 5253 | if (ret == 0) |
560156cb | 5254 | max_avail = dev_extent_want; |
b2117a39 | 5255 | |
6aafb303 | 5256 | if (max_avail < ctl->dev_extent_min) { |
4117f207 QW |
5257 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
5258 | btrfs_debug(info, | |
560156cb | 5259 | "%s: devid %llu has no free space, have=%llu want=%llu", |
4117f207 | 5260 | __func__, device->devid, max_avail, |
6aafb303 | 5261 | ctl->dev_extent_min); |
73c5de00 | 5262 | continue; |
4117f207 | 5263 | } |
b2117a39 | 5264 | |
063d006f ES |
5265 | if (ndevs == fs_devices->rw_devices) { |
5266 | WARN(1, "%s: found more than %llu devices\n", | |
5267 | __func__, fs_devices->rw_devices); | |
5268 | break; | |
5269 | } | |
73c5de00 AJ |
5270 | devices_info[ndevs].dev_offset = dev_offset; |
5271 | devices_info[ndevs].max_avail = max_avail; | |
5272 | devices_info[ndevs].total_avail = total_avail; | |
5273 | devices_info[ndevs].dev = device; | |
5274 | ++ndevs; | |
5275 | } | |
560156cb | 5276 | ctl->ndevs = ndevs; |
b2117a39 | 5277 | |
73c5de00 AJ |
5278 | /* |
5279 | * now sort the devices by hole size / available space | |
5280 | */ | |
560156cb | 5281 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
73c5de00 | 5282 | btrfs_cmp_device_info, NULL); |
b2117a39 | 5283 | |
560156cb NA |
5284 | return 0; |
5285 | } | |
5286 | ||
5badf512 NA |
5287 | static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl, |
5288 | struct btrfs_device_info *devices_info) | |
5289 | { | |
5290 | /* Number of stripes that count for block group size */ | |
5291 | int data_stripes; | |
5292 | ||
5293 | /* | |
5294 | * The primary goal is to maximize the number of stripes, so use as | |
5295 | * many devices as possible, even if the stripes are not maximum sized. | |
5296 | * | |
5297 | * The DUP profile stores more than one stripe per device, the | |
5298 | * max_avail is the total size so we have to adjust. | |
5299 | */ | |
5300 | ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail, | |
5301 | ctl->dev_stripes); | |
5302 | ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; | |
5303 | ||
5304 | /* This will have to be fixed for RAID1 and RAID10 over more drives */ | |
5305 | data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; | |
5306 | ||
5307 | /* | |
5308 | * Use the number of data stripes to figure out how big this chunk is | |
5309 | * really going to be in terms of logical address space, and compare | |
5310 | * that answer with the max chunk size. If it's higher, we try to | |
5311 | * reduce stripe_size. | |
5312 | */ | |
5313 | if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) { | |
5314 | /* | |
5315 | * Reduce stripe_size, round it up to a 16MB boundary again and | |
5316 | * then use it, unless it ends up being even bigger than the | |
5317 | * previous value we had already. | |
5318 | */ | |
5319 | ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size, | |
5320 | data_stripes), SZ_16M), | |
5321 | ctl->stripe_size); | |
5322 | } | |
5323 | ||
5da431b7 QW |
5324 | /* Stripe size should not go beyond 1G. */ |
5325 | ctl->stripe_size = min_t(u64, ctl->stripe_size, SZ_1G); | |
5326 | ||
5badf512 NA |
5327 | /* Align to BTRFS_STRIPE_LEN */ |
5328 | ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN); | |
5329 | ctl->chunk_size = ctl->stripe_size * data_stripes; | |
5330 | ||
5331 | return 0; | |
5332 | } | |
5333 | ||
1cd6121f NA |
5334 | static int decide_stripe_size_zoned(struct alloc_chunk_ctl *ctl, |
5335 | struct btrfs_device_info *devices_info) | |
5336 | { | |
5337 | u64 zone_size = devices_info[0].dev->zone_info->zone_size; | |
5338 | /* Number of stripes that count for block group size */ | |
5339 | int data_stripes; | |
5340 | ||
5341 | /* | |
5342 | * It should hold because: | |
5343 | * dev_extent_min == dev_extent_want == zone_size * dev_stripes | |
5344 | */ | |
5345 | ASSERT(devices_info[ctl->ndevs - 1].max_avail == ctl->dev_extent_min); | |
5346 | ||
5347 | ctl->stripe_size = zone_size; | |
5348 | ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; | |
5349 | data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; | |
5350 | ||
5351 | /* stripe_size is fixed in zoned filesysmte. Reduce ndevs instead. */ | |
5352 | if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) { | |
5353 | ctl->ndevs = div_u64(div_u64(ctl->max_chunk_size * ctl->ncopies, | |
5354 | ctl->stripe_size) + ctl->nparity, | |
5355 | ctl->dev_stripes); | |
5356 | ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; | |
5357 | data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; | |
5358 | ASSERT(ctl->stripe_size * data_stripes <= ctl->max_chunk_size); | |
5359 | } | |
5360 | ||
5361 | ctl->chunk_size = ctl->stripe_size * data_stripes; | |
5362 | ||
5363 | return 0; | |
5364 | } | |
5365 | ||
5badf512 NA |
5366 | static int decide_stripe_size(struct btrfs_fs_devices *fs_devices, |
5367 | struct alloc_chunk_ctl *ctl, | |
5368 | struct btrfs_device_info *devices_info) | |
5369 | { | |
5370 | struct btrfs_fs_info *info = fs_devices->fs_info; | |
5371 | ||
5372 | /* | |
5373 | * Round down to number of usable stripes, devs_increment can be any | |
5374 | * number so we can't use round_down() that requires power of 2, while | |
5375 | * rounddown is safe. | |
5376 | */ | |
5377 | ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment); | |
5378 | ||
5379 | if (ctl->ndevs < ctl->devs_min) { | |
5380 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { | |
5381 | btrfs_debug(info, | |
5382 | "%s: not enough devices with free space: have=%d minimum required=%d", | |
5383 | __func__, ctl->ndevs, ctl->devs_min); | |
5384 | } | |
5385 | return -ENOSPC; | |
5386 | } | |
5387 | ||
5388 | ctl->ndevs = min(ctl->ndevs, ctl->devs_max); | |
5389 | ||
5390 | switch (fs_devices->chunk_alloc_policy) { | |
5391 | case BTRFS_CHUNK_ALLOC_REGULAR: | |
5392 | return decide_stripe_size_regular(ctl, devices_info); | |
1cd6121f NA |
5393 | case BTRFS_CHUNK_ALLOC_ZONED: |
5394 | return decide_stripe_size_zoned(ctl, devices_info); | |
5badf512 NA |
5395 | default: |
5396 | BUG(); | |
5397 | } | |
5398 | } | |
5399 | ||
79bd3712 | 5400 | static struct btrfs_block_group *create_chunk(struct btrfs_trans_handle *trans, |
dce580ca NA |
5401 | struct alloc_chunk_ctl *ctl, |
5402 | struct btrfs_device_info *devices_info) | |
560156cb NA |
5403 | { |
5404 | struct btrfs_fs_info *info = trans->fs_info; | |
560156cb NA |
5405 | struct map_lookup *map = NULL; |
5406 | struct extent_map_tree *em_tree; | |
79bd3712 | 5407 | struct btrfs_block_group *block_group; |
560156cb | 5408 | struct extent_map *em; |
dce580ca NA |
5409 | u64 start = ctl->start; |
5410 | u64 type = ctl->type; | |
560156cb NA |
5411 | int ret; |
5412 | int i; | |
5413 | int j; | |
5414 | ||
dce580ca NA |
5415 | map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS); |
5416 | if (!map) | |
79bd3712 | 5417 | return ERR_PTR(-ENOMEM); |
dce580ca | 5418 | map->num_stripes = ctl->num_stripes; |
560156cb | 5419 | |
dce580ca NA |
5420 | for (i = 0; i < ctl->ndevs; ++i) { |
5421 | for (j = 0; j < ctl->dev_stripes; ++j) { | |
5422 | int s = i * ctl->dev_stripes + j; | |
73c5de00 AJ |
5423 | map->stripes[s].dev = devices_info[i].dev; |
5424 | map->stripes[s].physical = devices_info[i].dev_offset + | |
dce580ca | 5425 | j * ctl->stripe_size; |
6324fbf3 | 5426 | } |
6324fbf3 | 5427 | } |
500ceed8 NB |
5428 | map->io_align = BTRFS_STRIPE_LEN; |
5429 | map->io_width = BTRFS_STRIPE_LEN; | |
2b82032c | 5430 | map->type = type; |
dce580ca | 5431 | map->sub_stripes = ctl->sub_stripes; |
0b86a832 | 5432 | |
dce580ca | 5433 | trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size); |
1abe9b8a | 5434 | |
172ddd60 | 5435 | em = alloc_extent_map(); |
2b82032c | 5436 | if (!em) { |
298a8f9c | 5437 | kfree(map); |
79bd3712 | 5438 | return ERR_PTR(-ENOMEM); |
593060d7 | 5439 | } |
298a8f9c | 5440 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
95617d69 | 5441 | em->map_lookup = map; |
2b82032c | 5442 | em->start = start; |
dce580ca | 5443 | em->len = ctl->chunk_size; |
2b82032c YZ |
5444 | em->block_start = 0; |
5445 | em->block_len = em->len; | |
dce580ca | 5446 | em->orig_block_len = ctl->stripe_size; |
593060d7 | 5447 | |
c8bf1b67 | 5448 | em_tree = &info->mapping_tree; |
890871be | 5449 | write_lock(&em_tree->lock); |
09a2a8f9 | 5450 | ret = add_extent_mapping(em_tree, em, 0); |
0f5d42b2 | 5451 | if (ret) { |
1efb72a3 | 5452 | write_unlock(&em_tree->lock); |
0f5d42b2 | 5453 | free_extent_map(em); |
79bd3712 | 5454 | return ERR_PTR(ret); |
0f5d42b2 | 5455 | } |
1efb72a3 NB |
5456 | write_unlock(&em_tree->lock); |
5457 | ||
5758d1bd | 5458 | block_group = btrfs_make_block_group(trans, type, start, ctl->chunk_size); |
79bd3712 | 5459 | if (IS_ERR(block_group)) |
6df9a95e | 5460 | goto error_del_extent; |
2b82032c | 5461 | |
bbbf7243 NB |
5462 | for (i = 0; i < map->num_stripes; i++) { |
5463 | struct btrfs_device *dev = map->stripes[i].dev; | |
5464 | ||
4f2bafe8 | 5465 | btrfs_device_set_bytes_used(dev, |
dce580ca | 5466 | dev->bytes_used + ctl->stripe_size); |
bbbf7243 NB |
5467 | if (list_empty(&dev->post_commit_list)) |
5468 | list_add_tail(&dev->post_commit_list, | |
5469 | &trans->transaction->dev_update_list); | |
5470 | } | |
43530c46 | 5471 | |
dce580ca | 5472 | atomic64_sub(ctl->stripe_size * map->num_stripes, |
4f2bafe8 | 5473 | &info->free_chunk_space); |
1c116187 | 5474 | |
0f5d42b2 | 5475 | free_extent_map(em); |
0b246afa | 5476 | check_raid56_incompat_flag(info, type); |
cfbb825c | 5477 | check_raid1c34_incompat_flag(info, type); |
53b381b3 | 5478 | |
79bd3712 | 5479 | return block_group; |
b2117a39 | 5480 | |
6df9a95e | 5481 | error_del_extent: |
0f5d42b2 JB |
5482 | write_lock(&em_tree->lock); |
5483 | remove_extent_mapping(em_tree, em); | |
5484 | write_unlock(&em_tree->lock); | |
5485 | ||
5486 | /* One for our allocation */ | |
5487 | free_extent_map(em); | |
5488 | /* One for the tree reference */ | |
5489 | free_extent_map(em); | |
dce580ca | 5490 | |
79bd3712 | 5491 | return block_group; |
dce580ca NA |
5492 | } |
5493 | ||
f6f39f7a | 5494 | struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans, |
79bd3712 | 5495 | u64 type) |
dce580ca NA |
5496 | { |
5497 | struct btrfs_fs_info *info = trans->fs_info; | |
5498 | struct btrfs_fs_devices *fs_devices = info->fs_devices; | |
5499 | struct btrfs_device_info *devices_info = NULL; | |
5500 | struct alloc_chunk_ctl ctl; | |
79bd3712 | 5501 | struct btrfs_block_group *block_group; |
dce580ca NA |
5502 | int ret; |
5503 | ||
11c67b1a NB |
5504 | lockdep_assert_held(&info->chunk_mutex); |
5505 | ||
dce580ca NA |
5506 | if (!alloc_profile_is_valid(type, 0)) { |
5507 | ASSERT(0); | |
79bd3712 | 5508 | return ERR_PTR(-EINVAL); |
dce580ca NA |
5509 | } |
5510 | ||
5511 | if (list_empty(&fs_devices->alloc_list)) { | |
5512 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) | |
5513 | btrfs_debug(info, "%s: no writable device", __func__); | |
79bd3712 | 5514 | return ERR_PTR(-ENOSPC); |
dce580ca NA |
5515 | } |
5516 | ||
5517 | if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { | |
5518 | btrfs_err(info, "invalid chunk type 0x%llx requested", type); | |
5519 | ASSERT(0); | |
79bd3712 | 5520 | return ERR_PTR(-EINVAL); |
dce580ca NA |
5521 | } |
5522 | ||
11c67b1a | 5523 | ctl.start = find_next_chunk(info); |
dce580ca NA |
5524 | ctl.type = type; |
5525 | init_alloc_chunk_ctl(fs_devices, &ctl); | |
5526 | ||
5527 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), | |
5528 | GFP_NOFS); | |
5529 | if (!devices_info) | |
79bd3712 | 5530 | return ERR_PTR(-ENOMEM); |
dce580ca NA |
5531 | |
5532 | ret = gather_device_info(fs_devices, &ctl, devices_info); | |
79bd3712 FM |
5533 | if (ret < 0) { |
5534 | block_group = ERR_PTR(ret); | |
dce580ca | 5535 | goto out; |
79bd3712 | 5536 | } |
dce580ca NA |
5537 | |
5538 | ret = decide_stripe_size(fs_devices, &ctl, devices_info); | |
79bd3712 FM |
5539 | if (ret < 0) { |
5540 | block_group = ERR_PTR(ret); | |
dce580ca | 5541 | goto out; |
79bd3712 | 5542 | } |
dce580ca | 5543 | |
79bd3712 | 5544 | block_group = create_chunk(trans, &ctl, devices_info); |
dce580ca NA |
5545 | |
5546 | out: | |
b2117a39 | 5547 | kfree(devices_info); |
79bd3712 | 5548 | return block_group; |
2b82032c YZ |
5549 | } |
5550 | ||
79bd3712 FM |
5551 | /* |
5552 | * This function, btrfs_chunk_alloc_add_chunk_item(), typically belongs to the | |
5553 | * phase 1 of chunk allocation. It belongs to phase 2 only when allocating system | |
5554 | * chunks. | |
5555 | * | |
5556 | * See the comment at btrfs_chunk_alloc() for details about the chunk allocation | |
5557 | * phases. | |
5558 | */ | |
5559 | int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, | |
5560 | struct btrfs_block_group *bg) | |
5561 | { | |
5562 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
79bd3712 FM |
5563 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
5564 | struct btrfs_key key; | |
5565 | struct btrfs_chunk *chunk; | |
5566 | struct btrfs_stripe *stripe; | |
5567 | struct extent_map *em; | |
5568 | struct map_lookup *map; | |
5569 | size_t item_size; | |
5570 | int i; | |
5571 | int ret; | |
5572 | ||
5573 | /* | |
5574 | * We take the chunk_mutex for 2 reasons: | |
5575 | * | |
5576 | * 1) Updates and insertions in the chunk btree must be done while holding | |
5577 | * the chunk_mutex, as well as updating the system chunk array in the | |
5578 | * superblock. See the comment on top of btrfs_chunk_alloc() for the | |
5579 | * details; | |
5580 | * | |
5581 | * 2) To prevent races with the final phase of a device replace operation | |
5582 | * that replaces the device object associated with the map's stripes, | |
5583 | * because the device object's id can change at any time during that | |
5584 | * final phase of the device replace operation | |
5585 | * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the | |
5586 | * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID, | |
5587 | * which would cause a failure when updating the device item, which does | |
5588 | * not exists, or persisting a stripe of the chunk item with such ID. | |
5589 | * Here we can't use the device_list_mutex because our caller already | |
5590 | * has locked the chunk_mutex, and the final phase of device replace | |
5591 | * acquires both mutexes - first the device_list_mutex and then the | |
5592 | * chunk_mutex. Using any of those two mutexes protects us from a | |
5593 | * concurrent device replace. | |
5594 | */ | |
5595 | lockdep_assert_held(&fs_info->chunk_mutex); | |
5596 | ||
5597 | em = btrfs_get_chunk_map(fs_info, bg->start, bg->length); | |
5598 | if (IS_ERR(em)) { | |
5599 | ret = PTR_ERR(em); | |
5600 | btrfs_abort_transaction(trans, ret); | |
5601 | return ret; | |
5602 | } | |
5603 | ||
5604 | map = em->map_lookup; | |
5605 | item_size = btrfs_chunk_item_size(map->num_stripes); | |
5606 | ||
5607 | chunk = kzalloc(item_size, GFP_NOFS); | |
5608 | if (!chunk) { | |
5609 | ret = -ENOMEM; | |
5610 | btrfs_abort_transaction(trans, ret); | |
50460e37 | 5611 | goto out; |
2b82032c YZ |
5612 | } |
5613 | ||
79bd3712 FM |
5614 | for (i = 0; i < map->num_stripes; i++) { |
5615 | struct btrfs_device *device = map->stripes[i].dev; | |
5616 | ||
5617 | ret = btrfs_update_device(trans, device); | |
5618 | if (ret) | |
5619 | goto out; | |
5620 | } | |
5621 | ||
2b82032c | 5622 | stripe = &chunk->stripe; |
6df9a95e | 5623 | for (i = 0; i < map->num_stripes; i++) { |
79bd3712 FM |
5624 | struct btrfs_device *device = map->stripes[i].dev; |
5625 | const u64 dev_offset = map->stripes[i].physical; | |
0b86a832 | 5626 | |
e17cade2 CM |
5627 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
5628 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
5629 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
2b82032c | 5630 | stripe++; |
0b86a832 CM |
5631 | } |
5632 | ||
79bd3712 | 5633 | btrfs_set_stack_chunk_length(chunk, bg->length); |
fd51eb2f | 5634 | btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); |
a97699d1 | 5635 | btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); |
2b82032c YZ |
5636 | btrfs_set_stack_chunk_type(chunk, map->type); |
5637 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); | |
a97699d1 QW |
5638 | btrfs_set_stack_chunk_io_align(chunk, BTRFS_STRIPE_LEN); |
5639 | btrfs_set_stack_chunk_io_width(chunk, BTRFS_STRIPE_LEN); | |
0b246afa | 5640 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
2b82032c | 5641 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
0b86a832 | 5642 | |
2b82032c YZ |
5643 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
5644 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
79bd3712 | 5645 | key.offset = bg->start; |
0b86a832 | 5646 | |
2b82032c | 5647 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
79bd3712 FM |
5648 | if (ret) |
5649 | goto out; | |
5650 | ||
3349b57f | 5651 | set_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED, &bg->runtime_flags); |
79bd3712 FM |
5652 | |
5653 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { | |
2ff7e61e | 5654 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
79bd3712 FM |
5655 | if (ret) |
5656 | goto out; | |
8f18cf13 | 5657 | } |
1abe9b8a | 5658 | |
6df9a95e | 5659 | out: |
0b86a832 | 5660 | kfree(chunk); |
6df9a95e | 5661 | free_extent_map(em); |
4ed1d16e | 5662 | return ret; |
2b82032c | 5663 | } |
0b86a832 | 5664 | |
6f8e0fc7 | 5665 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
2b82032c | 5666 | { |
6f8e0fc7 | 5667 | struct btrfs_fs_info *fs_info = trans->fs_info; |
2b82032c | 5668 | u64 alloc_profile; |
79bd3712 FM |
5669 | struct btrfs_block_group *meta_bg; |
5670 | struct btrfs_block_group *sys_bg; | |
5671 | ||
5672 | /* | |
5673 | * When adding a new device for sprouting, the seed device is read-only | |
5674 | * so we must first allocate a metadata and a system chunk. But before | |
5675 | * adding the block group items to the extent, device and chunk btrees, | |
5676 | * we must first: | |
5677 | * | |
5678 | * 1) Create both chunks without doing any changes to the btrees, as | |
5679 | * otherwise we would get -ENOSPC since the block groups from the | |
5680 | * seed device are read-only; | |
5681 | * | |
5682 | * 2) Add the device item for the new sprout device - finishing the setup | |
5683 | * of a new block group requires updating the device item in the chunk | |
5684 | * btree, so it must exist when we attempt to do it. The previous step | |
5685 | * ensures this does not fail with -ENOSPC. | |
5686 | * | |
5687 | * After that we can add the block group items to their btrees: | |
5688 | * update existing device item in the chunk btree, add a new block group | |
5689 | * item to the extent btree, add a new chunk item to the chunk btree and | |
5690 | * finally add the new device extent items to the devices btree. | |
5691 | */ | |
2b82032c | 5692 | |
1b86826d | 5693 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
f6f39f7a | 5694 | meta_bg = btrfs_create_chunk(trans, alloc_profile); |
79bd3712 FM |
5695 | if (IS_ERR(meta_bg)) |
5696 | return PTR_ERR(meta_bg); | |
2b82032c | 5697 | |
1b86826d | 5698 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
f6f39f7a | 5699 | sys_bg = btrfs_create_chunk(trans, alloc_profile); |
79bd3712 FM |
5700 | if (IS_ERR(sys_bg)) |
5701 | return PTR_ERR(sys_bg); | |
5702 | ||
5703 | return 0; | |
2b82032c YZ |
5704 | } |
5705 | ||
d20983b4 MX |
5706 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
5707 | { | |
fc9a2ac7 | 5708 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
2b82032c | 5709 | |
fc9a2ac7 | 5710 | return btrfs_raid_array[index].tolerated_failures; |
2b82032c YZ |
5711 | } |
5712 | ||
a09f23c3 | 5713 | bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
2b82032c YZ |
5714 | { |
5715 | struct extent_map *em; | |
5716 | struct map_lookup *map; | |
d20983b4 | 5717 | int miss_ndevs = 0; |
2b82032c | 5718 | int i; |
a09f23c3 | 5719 | bool ret = true; |
2b82032c | 5720 | |
60ca842e | 5721 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
592d92ee | 5722 | if (IS_ERR(em)) |
a09f23c3 | 5723 | return false; |
2b82032c | 5724 | |
95617d69 | 5725 | map = em->map_lookup; |
2b82032c | 5726 | for (i = 0; i < map->num_stripes; i++) { |
e6e674bd AJ |
5727 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
5728 | &map->stripes[i].dev->dev_state)) { | |
d20983b4 MX |
5729 | miss_ndevs++; |
5730 | continue; | |
5731 | } | |
ebbede42 AJ |
5732 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
5733 | &map->stripes[i].dev->dev_state)) { | |
a09f23c3 | 5734 | ret = false; |
d20983b4 | 5735 | goto end; |
2b82032c YZ |
5736 | } |
5737 | } | |
d20983b4 MX |
5738 | |
5739 | /* | |
a09f23c3 AJ |
5740 | * If the number of missing devices is larger than max errors, we can |
5741 | * not write the data into that chunk successfully. | |
d20983b4 MX |
5742 | */ |
5743 | if (miss_ndevs > btrfs_chunk_max_errors(map)) | |
a09f23c3 | 5744 | ret = false; |
d20983b4 | 5745 | end: |
0b86a832 | 5746 | free_extent_map(em); |
a09f23c3 | 5747 | return ret; |
0b86a832 CM |
5748 | } |
5749 | ||
c8bf1b67 | 5750 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
0b86a832 CM |
5751 | { |
5752 | struct extent_map *em; | |
5753 | ||
d397712b | 5754 | while (1) { |
c8bf1b67 DS |
5755 | write_lock(&tree->lock); |
5756 | em = lookup_extent_mapping(tree, 0, (u64)-1); | |
0b86a832 | 5757 | if (em) |
c8bf1b67 DS |
5758 | remove_extent_mapping(tree, em); |
5759 | write_unlock(&tree->lock); | |
0b86a832 CM |
5760 | if (!em) |
5761 | break; | |
0b86a832 CM |
5762 | /* once for us */ |
5763 | free_extent_map(em); | |
5764 | /* once for the tree */ | |
5765 | free_extent_map(em); | |
5766 | } | |
5767 | } | |
5768 | ||
5d964051 | 5769 | int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
f188591e CM |
5770 | { |
5771 | struct extent_map *em; | |
5772 | struct map_lookup *map; | |
6d322b48 QW |
5773 | enum btrfs_raid_types index; |
5774 | int ret = 1; | |
f188591e | 5775 | |
60ca842e | 5776 | em = btrfs_get_chunk_map(fs_info, logical, len); |
592d92ee LB |
5777 | if (IS_ERR(em)) |
5778 | /* | |
5779 | * We could return errors for these cases, but that could get | |
5780 | * ugly and we'd probably do the same thing which is just not do | |
5781 | * anything else and exit, so return 1 so the callers don't try | |
5782 | * to use other copies. | |
5783 | */ | |
fb7669b5 | 5784 | return 1; |
fb7669b5 | 5785 | |
95617d69 | 5786 | map = em->map_lookup; |
6d322b48 QW |
5787 | index = btrfs_bg_flags_to_raid_index(map->type); |
5788 | ||
5789 | /* Non-RAID56, use their ncopies from btrfs_raid_array. */ | |
5790 | if (!(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)) | |
5791 | ret = btrfs_raid_array[index].ncopies; | |
53b381b3 DW |
5792 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
5793 | ret = 2; | |
5794 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) | |
8810f751 LB |
5795 | /* |
5796 | * There could be two corrupted data stripes, we need | |
5797 | * to loop retry in order to rebuild the correct data. | |
e7e02096 | 5798 | * |
8810f751 LB |
5799 | * Fail a stripe at a time on every retry except the |
5800 | * stripe under reconstruction. | |
5801 | */ | |
5802 | ret = map->num_stripes; | |
f188591e | 5803 | free_extent_map(em); |
f188591e CM |
5804 | return ret; |
5805 | } | |
5806 | ||
2ff7e61e | 5807 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
53b381b3 DW |
5808 | u64 logical) |
5809 | { | |
5810 | struct extent_map *em; | |
5811 | struct map_lookup *map; | |
0b246afa | 5812 | unsigned long len = fs_info->sectorsize; |
53b381b3 | 5813 | |
b036f479 QW |
5814 | if (!btrfs_fs_incompat(fs_info, RAID56)) |
5815 | return len; | |
5816 | ||
60ca842e | 5817 | em = btrfs_get_chunk_map(fs_info, logical, len); |
53b381b3 | 5818 | |
69f03f13 NB |
5819 | if (!WARN_ON(IS_ERR(em))) { |
5820 | map = em->map_lookup; | |
5821 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
cb091225 | 5822 | len = btrfs_stripe_nr_to_offset(nr_data_stripes(map)); |
69f03f13 NB |
5823 | free_extent_map(em); |
5824 | } | |
53b381b3 DW |
5825 | return len; |
5826 | } | |
5827 | ||
e4ff5fb5 | 5828 | int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
53b381b3 DW |
5829 | { |
5830 | struct extent_map *em; | |
5831 | struct map_lookup *map; | |
53b381b3 DW |
5832 | int ret = 0; |
5833 | ||
b036f479 QW |
5834 | if (!btrfs_fs_incompat(fs_info, RAID56)) |
5835 | return 0; | |
5836 | ||
60ca842e | 5837 | em = btrfs_get_chunk_map(fs_info, logical, len); |
53b381b3 | 5838 | |
69f03f13 NB |
5839 | if(!WARN_ON(IS_ERR(em))) { |
5840 | map = em->map_lookup; | |
5841 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
5842 | ret = 1; | |
5843 | free_extent_map(em); | |
5844 | } | |
53b381b3 DW |
5845 | return ret; |
5846 | } | |
5847 | ||
30d9861f | 5848 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
99f92a7c | 5849 | struct map_lookup *map, int first, |
8ba0ae78 | 5850 | int dev_replace_is_ongoing) |
dfe25020 CM |
5851 | { |
5852 | int i; | |
99f92a7c | 5853 | int num_stripes; |
8ba0ae78 | 5854 | int preferred_mirror; |
30d9861f SB |
5855 | int tolerance; |
5856 | struct btrfs_device *srcdev; | |
5857 | ||
99f92a7c | 5858 | ASSERT((map->type & |
c7369b3f | 5859 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
99f92a7c AJ |
5860 | |
5861 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) | |
5862 | num_stripes = map->sub_stripes; | |
5863 | else | |
5864 | num_stripes = map->num_stripes; | |
5865 | ||
33fd2f71 AJ |
5866 | switch (fs_info->fs_devices->read_policy) { |
5867 | default: | |
5868 | /* Shouldn't happen, just warn and use pid instead of failing */ | |
5869 | btrfs_warn_rl(fs_info, | |
5870 | "unknown read_policy type %u, reset to pid", | |
5871 | fs_info->fs_devices->read_policy); | |
5872 | fs_info->fs_devices->read_policy = BTRFS_READ_POLICY_PID; | |
5873 | fallthrough; | |
5874 | case BTRFS_READ_POLICY_PID: | |
5875 | preferred_mirror = first + (current->pid % num_stripes); | |
5876 | break; | |
5877 | } | |
8ba0ae78 | 5878 | |
30d9861f SB |
5879 | if (dev_replace_is_ongoing && |
5880 | fs_info->dev_replace.cont_reading_from_srcdev_mode == | |
5881 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) | |
5882 | srcdev = fs_info->dev_replace.srcdev; | |
5883 | else | |
5884 | srcdev = NULL; | |
5885 | ||
5886 | /* | |
5887 | * try to avoid the drive that is the source drive for a | |
5888 | * dev-replace procedure, only choose it if no other non-missing | |
5889 | * mirror is available | |
5890 | */ | |
5891 | for (tolerance = 0; tolerance < 2; tolerance++) { | |
8ba0ae78 AJ |
5892 | if (map->stripes[preferred_mirror].dev->bdev && |
5893 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) | |
5894 | return preferred_mirror; | |
99f92a7c | 5895 | for (i = first; i < first + num_stripes; i++) { |
30d9861f SB |
5896 | if (map->stripes[i].dev->bdev && |
5897 | (tolerance || map->stripes[i].dev != srcdev)) | |
5898 | return i; | |
5899 | } | |
dfe25020 | 5900 | } |
30d9861f | 5901 | |
dfe25020 CM |
5902 | /* we couldn't find one that doesn't fail. Just return something |
5903 | * and the io error handling code will clean up eventually | |
5904 | */ | |
8ba0ae78 | 5905 | return preferred_mirror; |
dfe25020 CM |
5906 | } |
5907 | ||
731ccf15 | 5908 | static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info, |
1faf3885 | 5909 | u16 total_stripes) |
6e9606d2 | 5910 | { |
4ced85f8 QW |
5911 | struct btrfs_io_context *bioc; |
5912 | ||
5913 | bioc = kzalloc( | |
4c664611 QW |
5914 | /* The size of btrfs_io_context */ |
5915 | sizeof(struct btrfs_io_context) + | |
5916 | /* Plus the variable array for the stripes */ | |
18d758a2 | 5917 | sizeof(struct btrfs_io_stripe) * (total_stripes), |
9f0eac07 L |
5918 | GFP_NOFS); |
5919 | ||
5920 | if (!bioc) | |
5921 | return NULL; | |
6e9606d2 | 5922 | |
4c664611 | 5923 | refcount_set(&bioc->refs, 1); |
6e9606d2 | 5924 | |
731ccf15 | 5925 | bioc->fs_info = fs_info; |
1faf3885 | 5926 | bioc->replace_stripe_src = -1; |
18d758a2 | 5927 | bioc->full_stripe_logical = (u64)-1; |
608769a4 | 5928 | |
4c664611 | 5929 | return bioc; |
6e9606d2 ZL |
5930 | } |
5931 | ||
4c664611 | 5932 | void btrfs_get_bioc(struct btrfs_io_context *bioc) |
6e9606d2 | 5933 | { |
4c664611 QW |
5934 | WARN_ON(!refcount_read(&bioc->refs)); |
5935 | refcount_inc(&bioc->refs); | |
6e9606d2 ZL |
5936 | } |
5937 | ||
4c664611 | 5938 | void btrfs_put_bioc(struct btrfs_io_context *bioc) |
6e9606d2 | 5939 | { |
4c664611 | 5940 | if (!bioc) |
6e9606d2 | 5941 | return; |
4c664611 QW |
5942 | if (refcount_dec_and_test(&bioc->refs)) |
5943 | kfree(bioc); | |
6e9606d2 ZL |
5944 | } |
5945 | ||
0b3d4cd3 LB |
5946 | /* |
5947 | * Please note that, discard won't be sent to target device of device | |
5948 | * replace. | |
5949 | */ | |
a4012f06 CH |
5950 | struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, |
5951 | u64 logical, u64 *length_ret, | |
5952 | u32 *num_stripes) | |
0b3d4cd3 LB |
5953 | { |
5954 | struct extent_map *em; | |
5955 | struct map_lookup *map; | |
a4012f06 | 5956 | struct btrfs_discard_stripe *stripes; |
6b7faadd | 5957 | u64 length = *length_ret; |
0b3d4cd3 | 5958 | u64 offset; |
6ded22c1 QW |
5959 | u32 stripe_nr; |
5960 | u32 stripe_nr_end; | |
5961 | u32 stripe_cnt; | |
0b3d4cd3 | 5962 | u64 stripe_end_offset; |
0b3d4cd3 | 5963 | u64 stripe_offset; |
0b3d4cd3 LB |
5964 | u32 stripe_index; |
5965 | u32 factor = 0; | |
5966 | u32 sub_stripes = 0; | |
6ded22c1 | 5967 | u32 stripes_per_dev = 0; |
0b3d4cd3 LB |
5968 | u32 remaining_stripes = 0; |
5969 | u32 last_stripe = 0; | |
a4012f06 | 5970 | int ret; |
0b3d4cd3 LB |
5971 | int i; |
5972 | ||
60ca842e | 5973 | em = btrfs_get_chunk_map(fs_info, logical, length); |
0b3d4cd3 | 5974 | if (IS_ERR(em)) |
a4012f06 | 5975 | return ERR_CAST(em); |
0b3d4cd3 LB |
5976 | |
5977 | map = em->map_lookup; | |
a4012f06 | 5978 | |
0b3d4cd3 LB |
5979 | /* we don't discard raid56 yet */ |
5980 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { | |
5981 | ret = -EOPNOTSUPP; | |
a4012f06 | 5982 | goto out_free_map; |
a97699d1 | 5983 | } |
0b3d4cd3 LB |
5984 | |
5985 | offset = logical - em->start; | |
2d974619 | 5986 | length = min_t(u64, em->start + em->len - logical, length); |
6b7faadd | 5987 | *length_ret = length; |
0b3d4cd3 | 5988 | |
0b3d4cd3 LB |
5989 | /* |
5990 | * stripe_nr counts the total number of stripes we have to stride | |
5991 | * to get to this block | |
5992 | */ | |
a97699d1 | 5993 | stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT; |
0b3d4cd3 LB |
5994 | |
5995 | /* stripe_offset is the offset of this block in its stripe */ | |
cb091225 | 5996 | stripe_offset = offset - btrfs_stripe_nr_to_offset(stripe_nr); |
0b3d4cd3 | 5997 | |
a97699d1 QW |
5998 | stripe_nr_end = round_up(offset + length, BTRFS_STRIPE_LEN) >> |
5999 | BTRFS_STRIPE_LEN_SHIFT; | |
0b3d4cd3 | 6000 | stripe_cnt = stripe_nr_end - stripe_nr; |
cb091225 | 6001 | stripe_end_offset = btrfs_stripe_nr_to_offset(stripe_nr_end) - |
0b3d4cd3 LB |
6002 | (offset + length); |
6003 | /* | |
6004 | * after this, stripe_nr is the number of stripes on this | |
6005 | * device we have to walk to find the data, and stripe_index is | |
6006 | * the number of our device in the stripe array | |
6007 | */ | |
a4012f06 | 6008 | *num_stripes = 1; |
0b3d4cd3 LB |
6009 | stripe_index = 0; |
6010 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
6011 | BTRFS_BLOCK_GROUP_RAID10)) { | |
6012 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) | |
6013 | sub_stripes = 1; | |
6014 | else | |
6015 | sub_stripes = map->sub_stripes; | |
6016 | ||
6017 | factor = map->num_stripes / sub_stripes; | |
a4012f06 | 6018 | *num_stripes = min_t(u64, map->num_stripes, |
0b3d4cd3 | 6019 | sub_stripes * stripe_cnt); |
6ded22c1 QW |
6020 | stripe_index = stripe_nr % factor; |
6021 | stripe_nr /= factor; | |
0b3d4cd3 | 6022 | stripe_index *= sub_stripes; |
6ded22c1 QW |
6023 | |
6024 | remaining_stripes = stripe_cnt % factor; | |
6025 | stripes_per_dev = stripe_cnt / factor; | |
6026 | last_stripe = ((stripe_nr_end - 1) % factor) * sub_stripes; | |
c7369b3f | 6027 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
0b3d4cd3 | 6028 | BTRFS_BLOCK_GROUP_DUP)) { |
a4012f06 | 6029 | *num_stripes = map->num_stripes; |
0b3d4cd3 | 6030 | } else { |
6ded22c1 QW |
6031 | stripe_index = stripe_nr % map->num_stripes; |
6032 | stripe_nr /= map->num_stripes; | |
0b3d4cd3 LB |
6033 | } |
6034 | ||
a4012f06 CH |
6035 | stripes = kcalloc(*num_stripes, sizeof(*stripes), GFP_NOFS); |
6036 | if (!stripes) { | |
0b3d4cd3 | 6037 | ret = -ENOMEM; |
a4012f06 | 6038 | goto out_free_map; |
0b3d4cd3 LB |
6039 | } |
6040 | ||
a4012f06 CH |
6041 | for (i = 0; i < *num_stripes; i++) { |
6042 | stripes[i].physical = | |
0b3d4cd3 | 6043 | map->stripes[stripe_index].physical + |
cb091225 | 6044 | stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr); |
a4012f06 | 6045 | stripes[i].dev = map->stripes[stripe_index].dev; |
0b3d4cd3 LB |
6046 | |
6047 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
6048 | BTRFS_BLOCK_GROUP_RAID10)) { | |
cb091225 | 6049 | stripes[i].length = btrfs_stripe_nr_to_offset(stripes_per_dev); |
0b3d4cd3 LB |
6050 | |
6051 | if (i / sub_stripes < remaining_stripes) | |
a97699d1 | 6052 | stripes[i].length += BTRFS_STRIPE_LEN; |
0b3d4cd3 LB |
6053 | |
6054 | /* | |
6055 | * Special for the first stripe and | |
6056 | * the last stripe: | |
6057 | * | |
6058 | * |-------|...|-------| | |
6059 | * |----------| | |
6060 | * off end_off | |
6061 | */ | |
6062 | if (i < sub_stripes) | |
a4012f06 | 6063 | stripes[i].length -= stripe_offset; |
0b3d4cd3 LB |
6064 | |
6065 | if (stripe_index >= last_stripe && | |
6066 | stripe_index <= (last_stripe + | |
6067 | sub_stripes - 1)) | |
a4012f06 | 6068 | stripes[i].length -= stripe_end_offset; |
0b3d4cd3 LB |
6069 | |
6070 | if (i == sub_stripes - 1) | |
6071 | stripe_offset = 0; | |
6072 | } else { | |
a4012f06 | 6073 | stripes[i].length = length; |
0b3d4cd3 LB |
6074 | } |
6075 | ||
6076 | stripe_index++; | |
6077 | if (stripe_index == map->num_stripes) { | |
6078 | stripe_index = 0; | |
6079 | stripe_nr++; | |
6080 | } | |
6081 | } | |
6082 | ||
0b3d4cd3 | 6083 | free_extent_map(em); |
a4012f06 CH |
6084 | return stripes; |
6085 | out_free_map: | |
6086 | free_extent_map(em); | |
6087 | return ERR_PTR(ret); | |
0b3d4cd3 LB |
6088 | } |
6089 | ||
6143c23c NA |
6090 | static bool is_block_group_to_copy(struct btrfs_fs_info *fs_info, u64 logical) |
6091 | { | |
6092 | struct btrfs_block_group *cache; | |
6093 | bool ret; | |
6094 | ||
de17addc | 6095 | /* Non zoned filesystem does not use "to_copy" flag */ |
6143c23c NA |
6096 | if (!btrfs_is_zoned(fs_info)) |
6097 | return false; | |
6098 | ||
6099 | cache = btrfs_lookup_block_group(fs_info, logical); | |
6100 | ||
3349b57f | 6101 | ret = test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags); |
6143c23c NA |
6102 | |
6103 | btrfs_put_block_group(cache); | |
6104 | return ret; | |
6105 | } | |
6106 | ||
73c0f228 | 6107 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
be5c7edb | 6108 | struct btrfs_io_context *bioc, |
73c0f228 | 6109 | struct btrfs_dev_replace *dev_replace, |
6143c23c | 6110 | u64 logical, |
73c0f228 LB |
6111 | int *num_stripes_ret, int *max_errors_ret) |
6112 | { | |
73c0f228 | 6113 | u64 srcdev_devid = dev_replace->srcdev->devid; |
1faf3885 QW |
6114 | /* |
6115 | * At this stage, num_stripes is still the real number of stripes, | |
6116 | * excluding the duplicated stripes. | |
6117 | */ | |
73c0f228 | 6118 | int num_stripes = *num_stripes_ret; |
1faf3885 | 6119 | int nr_extra_stripes = 0; |
73c0f228 LB |
6120 | int max_errors = *max_errors_ret; |
6121 | int i; | |
6122 | ||
1faf3885 QW |
6123 | /* |
6124 | * A block group which has "to_copy" set will eventually be copied by | |
6125 | * the dev-replace process. We can avoid cloning IO here. | |
6126 | */ | |
6127 | if (is_block_group_to_copy(dev_replace->srcdev->fs_info, logical)) | |
6128 | return; | |
73c0f228 | 6129 | |
1faf3885 QW |
6130 | /* |
6131 | * Duplicate the write operations while the dev-replace procedure is | |
6132 | * running. Since the copying of the old disk to the new disk takes | |
6133 | * place at run time while the filesystem is mounted writable, the | |
6134 | * regular write operations to the old disk have to be duplicated to go | |
6135 | * to the new disk as well. | |
6136 | * | |
6137 | * Note that device->missing is handled by the caller, and that the | |
6138 | * write to the old disk is already set up in the stripes array. | |
6139 | */ | |
6140 | for (i = 0; i < num_stripes; i++) { | |
6141 | struct btrfs_io_stripe *old = &bioc->stripes[i]; | |
6142 | struct btrfs_io_stripe *new = &bioc->stripes[num_stripes + nr_extra_stripes]; | |
6143c23c | 6143 | |
1faf3885 QW |
6144 | if (old->dev->devid != srcdev_devid) |
6145 | continue; | |
73c0f228 | 6146 | |
1faf3885 QW |
6147 | new->physical = old->physical; |
6148 | new->dev = dev_replace->tgtdev; | |
6149 | if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
6150 | bioc->replace_stripe_src = i; | |
6151 | nr_extra_stripes++; | |
6152 | } | |
73c0f228 | 6153 | |
1faf3885 QW |
6154 | /* We can only have at most 2 extra nr_stripes (for DUP). */ |
6155 | ASSERT(nr_extra_stripes <= 2); | |
6156 | /* | |
6157 | * For GET_READ_MIRRORS, we can only return at most 1 extra stripe for | |
6158 | * replace. | |
6159 | * If we have 2 extra stripes, only choose the one with smaller physical. | |
6160 | */ | |
6161 | if (op == BTRFS_MAP_GET_READ_MIRRORS && nr_extra_stripes == 2) { | |
6162 | struct btrfs_io_stripe *first = &bioc->stripes[num_stripes]; | |
6163 | struct btrfs_io_stripe *second = &bioc->stripes[num_stripes + 1]; | |
73c0f228 | 6164 | |
1faf3885 QW |
6165 | /* Only DUP can have two extra stripes. */ |
6166 | ASSERT(bioc->map_type & BTRFS_BLOCK_GROUP_DUP); | |
6167 | ||
6168 | /* | |
6169 | * Swap the last stripe stripes and reduce @nr_extra_stripes. | |
6170 | * The extra stripe would still be there, but won't be accessed. | |
6171 | */ | |
6172 | if (first->physical > second->physical) { | |
6173 | swap(second->physical, first->physical); | |
6174 | swap(second->dev, first->dev); | |
6175 | nr_extra_stripes--; | |
73c0f228 LB |
6176 | } |
6177 | } | |
6178 | ||
1faf3885 QW |
6179 | *num_stripes_ret = num_stripes + nr_extra_stripes; |
6180 | *max_errors_ret = max_errors + nr_extra_stripes; | |
6181 | bioc->replace_nr_stripes = nr_extra_stripes; | |
73c0f228 LB |
6182 | } |
6183 | ||
f8a02dc6 | 6184 | static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, |
6ded22c1 | 6185 | u64 offset, u32 *stripe_nr, u64 *stripe_offset, |
f8a02dc6 | 6186 | u64 *full_stripe_start) |
5f141126 | 6187 | { |
cc353a8b | 6188 | /* |
f8a02dc6 CH |
6189 | * Stripe_nr is the stripe where this block falls. stripe_offset is |
6190 | * the offset of this block in its stripe. | |
cc353a8b | 6191 | */ |
a97699d1 QW |
6192 | *stripe_offset = offset & BTRFS_STRIPE_LEN_MASK; |
6193 | *stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT; | |
f8a02dc6 | 6194 | ASSERT(*stripe_offset < U32_MAX); |
5f141126 | 6195 | |
f8a02dc6 | 6196 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
cb091225 QW |
6197 | unsigned long full_stripe_len = |
6198 | btrfs_stripe_nr_to_offset(nr_data_stripes(map)); | |
5f141126 | 6199 | |
a97699d1 QW |
6200 | /* |
6201 | * For full stripe start, we use previously calculated | |
6202 | * @stripe_nr. Align it to nr_data_stripes, then multiply with | |
6203 | * STRIPE_LEN. | |
6204 | * | |
6205 | * By this we can avoid u64 division completely. And we have | |
6206 | * to go rounddown(), not round_down(), as nr_data_stripes is | |
6207 | * not ensured to be power of 2. | |
6208 | */ | |
f8a02dc6 | 6209 | *full_stripe_start = |
cb091225 QW |
6210 | btrfs_stripe_nr_to_offset( |
6211 | rounddown(*stripe_nr, nr_data_stripes(map))); | |
5f141126 | 6212 | |
a7299a18 QW |
6213 | ASSERT(*full_stripe_start + full_stripe_len > offset); |
6214 | ASSERT(*full_stripe_start <= offset); | |
5f141126 | 6215 | /* |
f8a02dc6 CH |
6216 | * For writes to RAID56, allow to write a full stripe set, but |
6217 | * no straddling of stripe sets. | |
5f141126 | 6218 | */ |
f8a02dc6 CH |
6219 | if (op == BTRFS_MAP_WRITE) |
6220 | return full_stripe_len - (offset - *full_stripe_start); | |
5f141126 NB |
6221 | } |
6222 | ||
f8a02dc6 CH |
6223 | /* |
6224 | * For other RAID types and for RAID56 reads, allow a single stripe (on | |
6225 | * a single disk). | |
6226 | */ | |
6227 | if (map->type & BTRFS_BLOCK_GROUP_STRIPE_MASK) | |
a97699d1 | 6228 | return BTRFS_STRIPE_LEN - *stripe_offset; |
f8a02dc6 | 6229 | return U64_MAX; |
5f141126 NB |
6230 | } |
6231 | ||
03793cbb | 6232 | static void set_io_stripe(struct btrfs_io_stripe *dst, const struct map_lookup *map, |
6ded22c1 | 6233 | u32 stripe_index, u64 stripe_offset, u32 stripe_nr) |
03793cbb CH |
6234 | { |
6235 | dst->dev = map->stripes[stripe_index].dev; | |
6236 | dst->physical = map->stripes[stripe_index].physical + | |
cb091225 | 6237 | stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr); |
03793cbb CH |
6238 | } |
6239 | ||
ed3764f7 QW |
6240 | /* |
6241 | * Map one logical range to one or more physical ranges. | |
6242 | * | |
6243 | * @length: (Mandatory) mapped length of this run. | |
6244 | * One logical range can be split into different segments | |
6245 | * due to factors like zones and RAID0/5/6/10 stripe | |
6246 | * boundaries. | |
6247 | * | |
6248 | * @bioc_ret: (Mandatory) returned btrfs_io_context structure. | |
6249 | * which has one or more physical ranges (btrfs_io_stripe) | |
6250 | * recorded inside. | |
6251 | * Caller should call btrfs_put_bioc() to free it after use. | |
6252 | * | |
6253 | * @smap: (Optional) single physical range optimization. | |
6254 | * If the map request can be fulfilled by one single | |
6255 | * physical range, and this is parameter is not NULL, | |
6256 | * then @bioc_ret would be NULL, and @smap would be | |
6257 | * updated. | |
6258 | * | |
6259 | * @mirror_num_ret: (Mandatory) returned mirror number if the original | |
6260 | * value is 0. | |
6261 | * | |
6262 | * Mirror number 0 means to choose any live mirrors. | |
6263 | * | |
6264 | * For non-RAID56 profiles, non-zero mirror_num means | |
6265 | * the Nth mirror. (e.g. mirror_num 1 means the first | |
6266 | * copy). | |
6267 | * | |
6268 | * For RAID56 profile, mirror 1 means rebuild from P and | |
6269 | * the remaining data stripes. | |
6270 | * | |
6271 | * For RAID6 profile, mirror > 2 means mark another | |
6272 | * data/P stripe error and rebuild from the remaining | |
6273 | * stripes.. | |
ed3764f7 | 6274 | */ |
cd4efd21 CH |
6275 | int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
6276 | u64 logical, u64 *length, | |
6277 | struct btrfs_io_context **bioc_ret, | |
9fb2acc2 | 6278 | struct btrfs_io_stripe *smap, int *mirror_num_ret) |
0b86a832 CM |
6279 | { |
6280 | struct extent_map *em; | |
6281 | struct map_lookup *map; | |
f8a02dc6 | 6282 | u64 map_offset; |
593060d7 | 6283 | u64 stripe_offset; |
6ded22c1 | 6284 | u32 stripe_nr; |
9d644a62 | 6285 | u32 stripe_index; |
cff82672 | 6286 | int data_stripes; |
cea9e445 | 6287 | int i; |
de11cc12 | 6288 | int ret = 0; |
03793cbb | 6289 | int mirror_num = (mirror_num_ret ? *mirror_num_ret : 0); |
f2d8d74d | 6290 | int num_stripes; |
5f50fa91 | 6291 | int num_copies; |
a236aed1 | 6292 | int max_errors = 0; |
4c664611 | 6293 | struct btrfs_io_context *bioc = NULL; |
472262f3 SB |
6294 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
6295 | int dev_replace_is_ongoing = 0; | |
4ced85f8 | 6296 | u16 num_alloc_stripes; |
53b381b3 | 6297 | u64 raid56_full_stripe_start = (u64)-1; |
f8a02dc6 | 6298 | u64 max_len; |
89b798ad | 6299 | |
4c664611 | 6300 | ASSERT(bioc_ret); |
0b3d4cd3 | 6301 | |
5f50fa91 QW |
6302 | num_copies = btrfs_num_copies(fs_info, logical, fs_info->sectorsize); |
6303 | if (mirror_num > num_copies) | |
6304 | return -EINVAL; | |
6305 | ||
42034313 | 6306 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
1c3ab6df QW |
6307 | if (IS_ERR(em)) |
6308 | return PTR_ERR(em); | |
42034313 | 6309 | |
95617d69 | 6310 | map = em->map_lookup; |
cff82672 | 6311 | data_stripes = nr_data_stripes(map); |
f8a02dc6 CH |
6312 | |
6313 | map_offset = logical - em->start; | |
6314 | max_len = btrfs_max_io_len(map, op, map_offset, &stripe_nr, | |
6315 | &stripe_offset, &raid56_full_stripe_start); | |
6316 | *length = min_t(u64, em->len - map_offset, max_len); | |
593060d7 | 6317 | |
cb5583dd | 6318 | down_read(&dev_replace->rwsem); |
472262f3 | 6319 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
53176dde DS |
6320 | /* |
6321 | * Hold the semaphore for read during the whole operation, write is | |
6322 | * requested at commit time but must wait. | |
6323 | */ | |
472262f3 | 6324 | if (!dev_replace_is_ongoing) |
cb5583dd | 6325 | up_read(&dev_replace->rwsem); |
472262f3 | 6326 | |
f2d8d74d | 6327 | num_stripes = 1; |
cea9e445 | 6328 | stripe_index = 0; |
fce3bb9a | 6329 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
6ded22c1 QW |
6330 | stripe_index = stripe_nr % map->num_stripes; |
6331 | stripe_nr /= map->num_stripes; | |
8680e587 | 6332 | if (op == BTRFS_MAP_READ) |
28e1cc7d | 6333 | mirror_num = 1; |
c7369b3f | 6334 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
8680e587 | 6335 | if (op != BTRFS_MAP_READ) { |
f2d8d74d | 6336 | num_stripes = map->num_stripes; |
8680e587 | 6337 | } else if (mirror_num) { |
f188591e | 6338 | stripe_index = mirror_num - 1; |
8680e587 | 6339 | } else { |
30d9861f | 6340 | stripe_index = find_live_mirror(fs_info, map, 0, |
30d9861f | 6341 | dev_replace_is_ongoing); |
a1d3c478 | 6342 | mirror_num = stripe_index + 1; |
dfe25020 | 6343 | } |
2fff734f | 6344 | |
611f0e00 | 6345 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
8680e587 | 6346 | if (op != BTRFS_MAP_READ) { |
f2d8d74d | 6347 | num_stripes = map->num_stripes; |
a1d3c478 | 6348 | } else if (mirror_num) { |
f188591e | 6349 | stripe_index = mirror_num - 1; |
a1d3c478 JS |
6350 | } else { |
6351 | mirror_num = 1; | |
6352 | } | |
2fff734f | 6353 | |
321aecc6 | 6354 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
9d644a62 | 6355 | u32 factor = map->num_stripes / map->sub_stripes; |
321aecc6 | 6356 | |
6ded22c1 QW |
6357 | stripe_index = (stripe_nr % factor) * map->sub_stripes; |
6358 | stripe_nr /= factor; | |
321aecc6 | 6359 | |
8680e587 | 6360 | if (op != BTRFS_MAP_READ) |
f2d8d74d | 6361 | num_stripes = map->sub_stripes; |
321aecc6 CM |
6362 | else if (mirror_num) |
6363 | stripe_index += mirror_num - 1; | |
dfe25020 | 6364 | else { |
3e74317a | 6365 | int old_stripe_index = stripe_index; |
30d9861f SB |
6366 | stripe_index = find_live_mirror(fs_info, map, |
6367 | stripe_index, | |
30d9861f | 6368 | dev_replace_is_ongoing); |
3e74317a | 6369 | mirror_num = stripe_index - old_stripe_index + 1; |
dfe25020 | 6370 | } |
53b381b3 | 6371 | |
ffe2d203 | 6372 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
9fb2acc2 | 6373 | if (op != BTRFS_MAP_READ || mirror_num > 1) { |
6ded22c1 | 6374 | /* |
9fb2acc2 QW |
6375 | * Needs full stripe mapping. |
6376 | * | |
6ded22c1 QW |
6377 | * Push stripe_nr back to the start of the full stripe |
6378 | * For those cases needing a full stripe, @stripe_nr | |
6379 | * is the full stripe number. | |
6380 | * | |
6381 | * Originally we go raid56_full_stripe_start / full_stripe_len, | |
6382 | * but that can be expensive. Here we just divide | |
6383 | * @stripe_nr with @data_stripes. | |
6384 | */ | |
6385 | stripe_nr /= data_stripes; | |
53b381b3 DW |
6386 | |
6387 | /* RAID[56] write or recovery. Return all stripes */ | |
6388 | num_stripes = map->num_stripes; | |
6dead96c | 6389 | max_errors = btrfs_chunk_max_errors(map); |
53b381b3 | 6390 | |
462b0b2a QW |
6391 | /* Return the length to the full stripe end */ |
6392 | *length = min(logical + *length, | |
6393 | raid56_full_stripe_start + em->start + | |
cb091225 QW |
6394 | btrfs_stripe_nr_to_offset(data_stripes)) - |
6395 | logical; | |
53b381b3 DW |
6396 | stripe_index = 0; |
6397 | stripe_offset = 0; | |
6398 | } else { | |
9fb2acc2 QW |
6399 | ASSERT(mirror_num <= 1); |
6400 | /* Just grab the data stripe directly. */ | |
6ded22c1 QW |
6401 | stripe_index = stripe_nr % data_stripes; |
6402 | stripe_nr /= data_stripes; | |
53b381b3 DW |
6403 | |
6404 | /* We distribute the parity blocks across stripes */ | |
6ded22c1 | 6405 | stripe_index = (stripe_nr + stripe_index) % map->num_stripes; |
9fb2acc2 | 6406 | if (op == BTRFS_MAP_READ && mirror_num < 1) |
28e1cc7d | 6407 | mirror_num = 1; |
53b381b3 | 6408 | } |
8790d502 CM |
6409 | } else { |
6410 | /* | |
6ded22c1 | 6411 | * After this, stripe_nr is the number of stripes on this |
47c5713f DS |
6412 | * device we have to walk to find the data, and stripe_index is |
6413 | * the number of our device in the stripe array | |
8790d502 | 6414 | */ |
6ded22c1 QW |
6415 | stripe_index = stripe_nr % map->num_stripes; |
6416 | stripe_nr /= map->num_stripes; | |
a1d3c478 | 6417 | mirror_num = stripe_index + 1; |
8790d502 | 6418 | } |
e042d1ec | 6419 | if (stripe_index >= map->num_stripes) { |
5d163e0e JM |
6420 | btrfs_crit(fs_info, |
6421 | "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u", | |
e042d1ec JB |
6422 | stripe_index, map->num_stripes); |
6423 | ret = -EINVAL; | |
6424 | goto out; | |
6425 | } | |
cea9e445 | 6426 | |
472262f3 | 6427 | num_alloc_stripes = num_stripes; |
1faf3885 QW |
6428 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
6429 | op != BTRFS_MAP_READ) | |
6430 | /* | |
6431 | * For replace case, we need to add extra stripes for extra | |
6432 | * duplicated stripes. | |
6433 | * | |
6434 | * For both WRITE and GET_READ_MIRRORS, we may have at most | |
6435 | * 2 more stripes (DUP types, otherwise 1). | |
6436 | */ | |
6437 | num_alloc_stripes += 2; | |
2c8cdd6e | 6438 | |
03793cbb CH |
6439 | /* |
6440 | * If this I/O maps to a single device, try to return the device and | |
6441 | * physical block information on the stack instead of allocating an | |
6442 | * I/O context structure. | |
6443 | */ | |
6444 | if (smap && num_alloc_stripes == 1 && | |
b2cc4400 | 6445 | !((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1)) { |
5f50fa91 | 6446 | set_io_stripe(smap, map, stripe_index, stripe_offset, stripe_nr); |
4e7de35e CH |
6447 | if (mirror_num_ret) |
6448 | *mirror_num_ret = mirror_num; | |
03793cbb CH |
6449 | *bioc_ret = NULL; |
6450 | ret = 0; | |
6451 | goto out; | |
6452 | } | |
6453 | ||
1faf3885 | 6454 | bioc = alloc_btrfs_io_context(fs_info, num_alloc_stripes); |
4c664611 | 6455 | if (!bioc) { |
de11cc12 LZ |
6456 | ret = -ENOMEM; |
6457 | goto out; | |
6458 | } | |
1faf3885 | 6459 | bioc->map_type = map->type; |
608769a4 | 6460 | |
18d758a2 QW |
6461 | /* |
6462 | * For RAID56 full map, we need to make sure the stripes[] follows the | |
6463 | * rule that data stripes are all ordered, then followed with P and Q | |
6464 | * (if we have). | |
6465 | * | |
6466 | * It's still mostly the same as other profiles, just with extra rotation. | |
6467 | */ | |
9fb2acc2 | 6468 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
8680e587 | 6469 | (op != BTRFS_MAP_READ || mirror_num > 1)) { |
18d758a2 QW |
6470 | /* |
6471 | * For RAID56 @stripe_nr is already the number of full stripes | |
6472 | * before us, which is also the rotation value (needs to modulo | |
6473 | * with num_stripes). | |
6474 | * | |
6475 | * In this case, we just add @stripe_nr with @i, then do the | |
6476 | * modulo, to reduce one modulo call. | |
6477 | */ | |
6478 | bioc->full_stripe_logical = em->start + | |
cb091225 | 6479 | btrfs_stripe_nr_to_offset(stripe_nr * data_stripes); |
18d758a2 QW |
6480 | for (i = 0; i < num_stripes; i++) |
6481 | set_io_stripe(&bioc->stripes[i], map, | |
6482 | (i + stripe_nr) % num_stripes, | |
6483 | stripe_offset, stripe_nr); | |
6484 | } else { | |
6485 | /* | |
6486 | * For all other non-RAID56 profiles, just copy the target | |
6487 | * stripe into the bioc. | |
6488 | */ | |
6489 | for (i = 0; i < num_stripes; i++) { | |
6490 | set_io_stripe(&bioc->stripes[i], map, stripe_index, | |
6491 | stripe_offset, stripe_nr); | |
6492 | stripe_index++; | |
6493 | } | |
593060d7 | 6494 | } |
de11cc12 | 6495 | |
8680e587 | 6496 | if (op != BTRFS_MAP_READ) |
d20983b4 | 6497 | max_errors = btrfs_chunk_max_errors(map); |
de11cc12 | 6498 | |
73c0f228 | 6499 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
8680e587 | 6500 | op != BTRFS_MAP_READ) { |
be5c7edb | 6501 | handle_ops_on_dev_replace(op, bioc, dev_replace, logical, |
6143c23c | 6502 | &num_stripes, &max_errors); |
472262f3 SB |
6503 | } |
6504 | ||
4c664611 | 6505 | *bioc_ret = bioc; |
4c664611 QW |
6506 | bioc->num_stripes = num_stripes; |
6507 | bioc->max_errors = max_errors; | |
6508 | bioc->mirror_num = mirror_num; | |
ad6d620e | 6509 | |
cea9e445 | 6510 | out: |
73beece9 | 6511 | if (dev_replace_is_ongoing) { |
53176dde DS |
6512 | lockdep_assert_held(&dev_replace->rwsem); |
6513 | /* Unlock and let waiting writers proceed */ | |
cb5583dd | 6514 | up_read(&dev_replace->rwsem); |
73beece9 | 6515 | } |
0b86a832 | 6516 | free_extent_map(em); |
de11cc12 | 6517 | return ret; |
0b86a832 CM |
6518 | } |
6519 | ||
562d7b15 JB |
6520 | static bool dev_args_match_fs_devices(const struct btrfs_dev_lookup_args *args, |
6521 | const struct btrfs_fs_devices *fs_devices) | |
6522 | { | |
6523 | if (args->fsid == NULL) | |
6524 | return true; | |
6525 | if (memcmp(fs_devices->metadata_uuid, args->fsid, BTRFS_FSID_SIZE) == 0) | |
6526 | return true; | |
6527 | return false; | |
6528 | } | |
6529 | ||
6530 | static bool dev_args_match_device(const struct btrfs_dev_lookup_args *args, | |
6531 | const struct btrfs_device *device) | |
6532 | { | |
0fca385d LS |
6533 | if (args->missing) { |
6534 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) && | |
6535 | !device->bdev) | |
6536 | return true; | |
6537 | return false; | |
6538 | } | |
562d7b15 | 6539 | |
0fca385d | 6540 | if (device->devid != args->devid) |
562d7b15 JB |
6541 | return false; |
6542 | if (args->uuid && memcmp(device->uuid, args->uuid, BTRFS_UUID_SIZE) != 0) | |
6543 | return false; | |
0fca385d | 6544 | return true; |
562d7b15 JB |
6545 | } |
6546 | ||
09ba3bc9 AJ |
6547 | /* |
6548 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or | |
6549 | * return NULL. | |
6550 | * | |
6551 | * If devid and uuid are both specified, the match must be exact, otherwise | |
6552 | * only devid is used. | |
09ba3bc9 | 6553 | */ |
562d7b15 JB |
6554 | struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices, |
6555 | const struct btrfs_dev_lookup_args *args) | |
0b86a832 | 6556 | { |
2b82032c | 6557 | struct btrfs_device *device; |
944d3f9f NB |
6558 | struct btrfs_fs_devices *seed_devs; |
6559 | ||
562d7b15 | 6560 | if (dev_args_match_fs_devices(args, fs_devices)) { |
944d3f9f | 6561 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
562d7b15 | 6562 | if (dev_args_match_device(args, device)) |
944d3f9f NB |
6563 | return device; |
6564 | } | |
6565 | } | |
2b82032c | 6566 | |
944d3f9f | 6567 | list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) { |
562d7b15 JB |
6568 | if (!dev_args_match_fs_devices(args, seed_devs)) |
6569 | continue; | |
6570 | list_for_each_entry(device, &seed_devs->devices, dev_list) { | |
6571 | if (dev_args_match_device(args, device)) | |
6572 | return device; | |
2b82032c | 6573 | } |
2b82032c | 6574 | } |
944d3f9f | 6575 | |
2b82032c | 6576 | return NULL; |
0b86a832 CM |
6577 | } |
6578 | ||
2ff7e61e | 6579 | static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices, |
dfe25020 CM |
6580 | u64 devid, u8 *dev_uuid) |
6581 | { | |
6582 | struct btrfs_device *device; | |
fccc0007 | 6583 | unsigned int nofs_flag; |
dfe25020 | 6584 | |
fccc0007 JB |
6585 | /* |
6586 | * We call this under the chunk_mutex, so we want to use NOFS for this | |
6587 | * allocation, however we don't want to change btrfs_alloc_device() to | |
6588 | * always do NOFS because we use it in a lot of other GFP_KERNEL safe | |
6589 | * places. | |
6590 | */ | |
bb21e302 | 6591 | |
fccc0007 | 6592 | nofs_flag = memalloc_nofs_save(); |
bb21e302 | 6593 | device = btrfs_alloc_device(NULL, &devid, dev_uuid, NULL); |
fccc0007 | 6594 | memalloc_nofs_restore(nofs_flag); |
12bd2fc0 | 6595 | if (IS_ERR(device)) |
adfb69af | 6596 | return device; |
12bd2fc0 ID |
6597 | |
6598 | list_add(&device->dev_list, &fs_devices->devices); | |
e4404d6e | 6599 | device->fs_devices = fs_devices; |
dfe25020 | 6600 | fs_devices->num_devices++; |
12bd2fc0 | 6601 | |
e6e674bd | 6602 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
cd02dca5 | 6603 | fs_devices->missing_devices++; |
12bd2fc0 | 6604 | |
dfe25020 CM |
6605 | return device; |
6606 | } | |
6607 | ||
43dd529a DS |
6608 | /* |
6609 | * Allocate new device struct, set up devid and UUID. | |
6610 | * | |
12bd2fc0 ID |
6611 | * @fs_info: used only for generating a new devid, can be NULL if |
6612 | * devid is provided (i.e. @devid != NULL). | |
6613 | * @devid: a pointer to devid for this device. If NULL a new devid | |
6614 | * is generated. | |
6615 | * @uuid: a pointer to UUID for this device. If NULL a new UUID | |
6616 | * is generated. | |
bb21e302 | 6617 | * @path: a pointer to device path if available, NULL otherwise. |
12bd2fc0 ID |
6618 | * |
6619 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() | |
48dae9cf | 6620 | * on error. Returned struct is not linked onto any lists and must be |
a425f9d4 | 6621 | * destroyed with btrfs_free_device. |
12bd2fc0 ID |
6622 | */ |
6623 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, | |
bb21e302 AJ |
6624 | const u64 *devid, const u8 *uuid, |
6625 | const char *path) | |
12bd2fc0 ID |
6626 | { |
6627 | struct btrfs_device *dev; | |
6628 | u64 tmp; | |
6629 | ||
fae7f21c | 6630 | if (WARN_ON(!devid && !fs_info)) |
12bd2fc0 | 6631 | return ERR_PTR(-EINVAL); |
12bd2fc0 | 6632 | |
fe4f46d4 DS |
6633 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
6634 | if (!dev) | |
6635 | return ERR_PTR(-ENOMEM); | |
6636 | ||
fe4f46d4 DS |
6637 | INIT_LIST_HEAD(&dev->dev_list); |
6638 | INIT_LIST_HEAD(&dev->dev_alloc_list); | |
6639 | INIT_LIST_HEAD(&dev->post_commit_list); | |
6640 | ||
fe4f46d4 DS |
6641 | atomic_set(&dev->dev_stats_ccnt, 0); |
6642 | btrfs_device_data_ordered_init(dev); | |
35da5a7e | 6643 | extent_io_tree_init(fs_info, &dev->alloc_state, IO_TREE_DEVICE_ALLOC_STATE); |
12bd2fc0 ID |
6644 | |
6645 | if (devid) | |
6646 | tmp = *devid; | |
6647 | else { | |
6648 | int ret; | |
6649 | ||
6650 | ret = find_next_devid(fs_info, &tmp); | |
6651 | if (ret) { | |
a425f9d4 | 6652 | btrfs_free_device(dev); |
12bd2fc0 ID |
6653 | return ERR_PTR(ret); |
6654 | } | |
6655 | } | |
6656 | dev->devid = tmp; | |
6657 | ||
6658 | if (uuid) | |
6659 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); | |
6660 | else | |
6661 | generate_random_uuid(dev->uuid); | |
6662 | ||
bb21e302 AJ |
6663 | if (path) { |
6664 | struct rcu_string *name; | |
6665 | ||
6666 | name = rcu_string_strdup(path, GFP_KERNEL); | |
6667 | if (!name) { | |
6668 | btrfs_free_device(dev); | |
6669 | return ERR_PTR(-ENOMEM); | |
6670 | } | |
6671 | rcu_assign_pointer(dev->name, name); | |
6672 | } | |
6673 | ||
12bd2fc0 ID |
6674 | return dev; |
6675 | } | |
6676 | ||
5a2b8e60 | 6677 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
2b902dfc | 6678 | u64 devid, u8 *uuid, bool error) |
5a2b8e60 | 6679 | { |
2b902dfc AJ |
6680 | if (error) |
6681 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", | |
6682 | devid, uuid); | |
6683 | else | |
6684 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", | |
6685 | devid, uuid); | |
5a2b8e60 AJ |
6686 | } |
6687 | ||
bc88b486 | 6688 | u64 btrfs_calc_stripe_length(const struct extent_map *em) |
39e264a4 | 6689 | { |
bc88b486 QW |
6690 | const struct map_lookup *map = em->map_lookup; |
6691 | const int data_stripes = calc_data_stripes(map->type, map->num_stripes); | |
e4f6c6be | 6692 | |
bc88b486 | 6693 | return div_u64(em->len, data_stripes); |
39e264a4 NB |
6694 | } |
6695 | ||
e9306ad4 QW |
6696 | #if BITS_PER_LONG == 32 |
6697 | /* | |
6698 | * Due to page cache limit, metadata beyond BTRFS_32BIT_MAX_FILE_SIZE | |
6699 | * can't be accessed on 32bit systems. | |
6700 | * | |
6701 | * This function do mount time check to reject the fs if it already has | |
6702 | * metadata chunk beyond that limit. | |
6703 | */ | |
6704 | static int check_32bit_meta_chunk(struct btrfs_fs_info *fs_info, | |
6705 | u64 logical, u64 length, u64 type) | |
6706 | { | |
6707 | if (!(type & BTRFS_BLOCK_GROUP_METADATA)) | |
6708 | return 0; | |
6709 | ||
6710 | if (logical + length < MAX_LFS_FILESIZE) | |
6711 | return 0; | |
6712 | ||
6713 | btrfs_err_32bit_limit(fs_info); | |
6714 | return -EOVERFLOW; | |
6715 | } | |
6716 | ||
6717 | /* | |
6718 | * This is to give early warning for any metadata chunk reaching | |
6719 | * BTRFS_32BIT_EARLY_WARN_THRESHOLD. | |
6720 | * Although we can still access the metadata, it's not going to be possible | |
6721 | * once the limit is reached. | |
6722 | */ | |
6723 | static void warn_32bit_meta_chunk(struct btrfs_fs_info *fs_info, | |
6724 | u64 logical, u64 length, u64 type) | |
6725 | { | |
6726 | if (!(type & BTRFS_BLOCK_GROUP_METADATA)) | |
6727 | return; | |
6728 | ||
6729 | if (logical + length < BTRFS_32BIT_EARLY_WARN_THRESHOLD) | |
6730 | return; | |
6731 | ||
6732 | btrfs_warn_32bit_limit(fs_info); | |
6733 | } | |
6734 | #endif | |
6735 | ||
ff37c89f NB |
6736 | static struct btrfs_device *handle_missing_device(struct btrfs_fs_info *fs_info, |
6737 | u64 devid, u8 *uuid) | |
6738 | { | |
6739 | struct btrfs_device *dev; | |
6740 | ||
6741 | if (!btrfs_test_opt(fs_info, DEGRADED)) { | |
6742 | btrfs_report_missing_device(fs_info, devid, uuid, true); | |
6743 | return ERR_PTR(-ENOENT); | |
6744 | } | |
6745 | ||
6746 | dev = add_missing_dev(fs_info->fs_devices, devid, uuid); | |
6747 | if (IS_ERR(dev)) { | |
6748 | btrfs_err(fs_info, "failed to init missing device %llu: %ld", | |
6749 | devid, PTR_ERR(dev)); | |
6750 | return dev; | |
6751 | } | |
6752 | btrfs_report_missing_device(fs_info, devid, uuid, false); | |
6753 | ||
6754 | return dev; | |
6755 | } | |
6756 | ||
9690ac09 | 6757 | static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, |
e06cd3dd LB |
6758 | struct btrfs_chunk *chunk) |
6759 | { | |
562d7b15 | 6760 | BTRFS_DEV_LOOKUP_ARGS(args); |
9690ac09 | 6761 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
c8bf1b67 | 6762 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
e06cd3dd LB |
6763 | struct map_lookup *map; |
6764 | struct extent_map *em; | |
6765 | u64 logical; | |
6766 | u64 length; | |
e06cd3dd | 6767 | u64 devid; |
e9306ad4 | 6768 | u64 type; |
e06cd3dd | 6769 | u8 uuid[BTRFS_UUID_SIZE]; |
76a66ba1 | 6770 | int index; |
e06cd3dd LB |
6771 | int num_stripes; |
6772 | int ret; | |
6773 | int i; | |
6774 | ||
6775 | logical = key->offset; | |
6776 | length = btrfs_chunk_length(leaf, chunk); | |
e9306ad4 | 6777 | type = btrfs_chunk_type(leaf, chunk); |
76a66ba1 | 6778 | index = btrfs_bg_flags_to_raid_index(type); |
e06cd3dd LB |
6779 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
6780 | ||
e9306ad4 QW |
6781 | #if BITS_PER_LONG == 32 |
6782 | ret = check_32bit_meta_chunk(fs_info, logical, length, type); | |
6783 | if (ret < 0) | |
6784 | return ret; | |
6785 | warn_32bit_meta_chunk(fs_info, logical, length, type); | |
6786 | #endif | |
6787 | ||
075cb3c7 QW |
6788 | /* |
6789 | * Only need to verify chunk item if we're reading from sys chunk array, | |
6790 | * as chunk item in tree block is already verified by tree-checker. | |
6791 | */ | |
6792 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { | |
ddaf1d5a | 6793 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
075cb3c7 QW |
6794 | if (ret) |
6795 | return ret; | |
6796 | } | |
a061fc8d | 6797 | |
c8bf1b67 DS |
6798 | read_lock(&map_tree->lock); |
6799 | em = lookup_extent_mapping(map_tree, logical, 1); | |
6800 | read_unlock(&map_tree->lock); | |
0b86a832 CM |
6801 | |
6802 | /* already mapped? */ | |
6803 | if (em && em->start <= logical && em->start + em->len > logical) { | |
6804 | free_extent_map(em); | |
0b86a832 CM |
6805 | return 0; |
6806 | } else if (em) { | |
6807 | free_extent_map(em); | |
6808 | } | |
0b86a832 | 6809 | |
172ddd60 | 6810 | em = alloc_extent_map(); |
0b86a832 CM |
6811 | if (!em) |
6812 | return -ENOMEM; | |
593060d7 | 6813 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
0b86a832 CM |
6814 | if (!map) { |
6815 | free_extent_map(em); | |
6816 | return -ENOMEM; | |
6817 | } | |
6818 | ||
298a8f9c | 6819 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
95617d69 | 6820 | em->map_lookup = map; |
0b86a832 CM |
6821 | em->start = logical; |
6822 | em->len = length; | |
70c8a91c | 6823 | em->orig_start = 0; |
0b86a832 | 6824 | em->block_start = 0; |
c8b97818 | 6825 | em->block_len = em->len; |
0b86a832 | 6826 | |
593060d7 CM |
6827 | map->num_stripes = num_stripes; |
6828 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
6829 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
e9306ad4 | 6830 | map->type = type; |
76a66ba1 QW |
6831 | /* |
6832 | * We can't use the sub_stripes value, as for profiles other than | |
6833 | * RAID10, they may have 0 as sub_stripes for filesystems created by | |
6834 | * older mkfs (<v5.4). | |
6835 | * In that case, it can cause divide-by-zero errors later. | |
6836 | * Since currently sub_stripes is fixed for each profile, let's | |
6837 | * use the trusted value instead. | |
6838 | */ | |
6839 | map->sub_stripes = btrfs_raid_array[index].sub_stripes; | |
cf90d884 | 6840 | map->verified_stripes = 0; |
bc88b486 | 6841 | em->orig_block_len = btrfs_calc_stripe_length(em); |
593060d7 CM |
6842 | for (i = 0; i < num_stripes; i++) { |
6843 | map->stripes[i].physical = | |
6844 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
6845 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
562d7b15 | 6846 | args.devid = devid; |
a443755f CM |
6847 | read_extent_buffer(leaf, uuid, (unsigned long) |
6848 | btrfs_stripe_dev_uuid_nr(chunk, i), | |
6849 | BTRFS_UUID_SIZE); | |
562d7b15 JB |
6850 | args.uuid = uuid; |
6851 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, &args); | |
dfe25020 | 6852 | if (!map->stripes[i].dev) { |
ff37c89f NB |
6853 | map->stripes[i].dev = handle_missing_device(fs_info, |
6854 | devid, uuid); | |
adfb69af | 6855 | if (IS_ERR(map->stripes[i].dev)) { |
1742e1c9 | 6856 | ret = PTR_ERR(map->stripes[i].dev); |
dfe25020 | 6857 | free_extent_map(em); |
1742e1c9 | 6858 | return ret; |
dfe25020 CM |
6859 | } |
6860 | } | |
ff37c89f | 6861 | |
e12c9621 AJ |
6862 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
6863 | &(map->stripes[i].dev->dev_state)); | |
0b86a832 CM |
6864 | } |
6865 | ||
c8bf1b67 DS |
6866 | write_lock(&map_tree->lock); |
6867 | ret = add_extent_mapping(map_tree, em, 0); | |
6868 | write_unlock(&map_tree->lock); | |
64f64f43 QW |
6869 | if (ret < 0) { |
6870 | btrfs_err(fs_info, | |
6871 | "failed to add chunk map, start=%llu len=%llu: %d", | |
6872 | em->start, em->len, ret); | |
6873 | } | |
0b86a832 CM |
6874 | free_extent_map(em); |
6875 | ||
64f64f43 | 6876 | return ret; |
0b86a832 CM |
6877 | } |
6878 | ||
143bede5 | 6879 | static void fill_device_from_item(struct extent_buffer *leaf, |
0b86a832 CM |
6880 | struct btrfs_dev_item *dev_item, |
6881 | struct btrfs_device *device) | |
6882 | { | |
6883 | unsigned long ptr; | |
0b86a832 CM |
6884 | |
6885 | device->devid = btrfs_device_id(leaf, dev_item); | |
d6397bae CB |
6886 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
6887 | device->total_bytes = device->disk_total_bytes; | |
935e5cc9 | 6888 | device->commit_total_bytes = device->disk_total_bytes; |
0b86a832 | 6889 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
ce7213c7 | 6890 | device->commit_bytes_used = device->bytes_used; |
0b86a832 CM |
6891 | device->type = btrfs_device_type(leaf, dev_item); |
6892 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
6893 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
6894 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
8dabb742 | 6895 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
401e29c1 | 6896 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
0b86a832 | 6897 | |
410ba3a2 | 6898 | ptr = btrfs_device_uuid(dev_item); |
e17cade2 | 6899 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 CM |
6900 | } |
6901 | ||
2ff7e61e | 6902 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, |
5f375835 | 6903 | u8 *fsid) |
2b82032c YZ |
6904 | { |
6905 | struct btrfs_fs_devices *fs_devices; | |
6906 | int ret; | |
6907 | ||
a32bf9a3 | 6908 | lockdep_assert_held(&uuid_mutex); |
2dfeca9b | 6909 | ASSERT(fsid); |
2b82032c | 6910 | |
427c8fdd | 6911 | /* This will match only for multi-device seed fs */ |
944d3f9f | 6912 | list_for_each_entry(fs_devices, &fs_info->fs_devices->seed_list, seed_list) |
44880fdc | 6913 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
5f375835 MX |
6914 | return fs_devices; |
6915 | ||
2b82032c | 6916 | |
7239ff4b | 6917 | fs_devices = find_fsid(fsid, NULL); |
2b82032c | 6918 | if (!fs_devices) { |
0b246afa | 6919 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
5f375835 MX |
6920 | return ERR_PTR(-ENOENT); |
6921 | ||
f7361d8c | 6922 | fs_devices = alloc_fs_devices(fsid); |
5f375835 MX |
6923 | if (IS_ERR(fs_devices)) |
6924 | return fs_devices; | |
6925 | ||
0395d84f | 6926 | fs_devices->seeding = true; |
5f375835 MX |
6927 | fs_devices->opened = 1; |
6928 | return fs_devices; | |
2b82032c | 6929 | } |
e4404d6e | 6930 | |
427c8fdd NB |
6931 | /* |
6932 | * Upon first call for a seed fs fsid, just create a private copy of the | |
6933 | * respective fs_devices and anchor it at fs_info->fs_devices->seed_list | |
6934 | */ | |
e4404d6e | 6935 | fs_devices = clone_fs_devices(fs_devices); |
5f375835 MX |
6936 | if (IS_ERR(fs_devices)) |
6937 | return fs_devices; | |
2b82032c | 6938 | |
05bdb996 | 6939 | ret = open_fs_devices(fs_devices, BLK_OPEN_READ, fs_info->bdev_holder); |
48d28232 JL |
6940 | if (ret) { |
6941 | free_fs_devices(fs_devices); | |
c83b60c0 | 6942 | return ERR_PTR(ret); |
48d28232 | 6943 | } |
2b82032c YZ |
6944 | |
6945 | if (!fs_devices->seeding) { | |
0226e0eb | 6946 | close_fs_devices(fs_devices); |
e4404d6e | 6947 | free_fs_devices(fs_devices); |
c83b60c0 | 6948 | return ERR_PTR(-EINVAL); |
2b82032c YZ |
6949 | } |
6950 | ||
944d3f9f | 6951 | list_add(&fs_devices->seed_list, &fs_info->fs_devices->seed_list); |
c83b60c0 | 6952 | |
5f375835 | 6953 | return fs_devices; |
2b82032c YZ |
6954 | } |
6955 | ||
17850759 | 6956 | static int read_one_dev(struct extent_buffer *leaf, |
0b86a832 CM |
6957 | struct btrfs_dev_item *dev_item) |
6958 | { | |
562d7b15 | 6959 | BTRFS_DEV_LOOKUP_ARGS(args); |
17850759 | 6960 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
0b246afa | 6961 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
0b86a832 CM |
6962 | struct btrfs_device *device; |
6963 | u64 devid; | |
6964 | int ret; | |
44880fdc | 6965 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
a443755f CM |
6966 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
6967 | ||
c1867eb3 DS |
6968 | devid = btrfs_device_id(leaf, dev_item); |
6969 | args.devid = devid; | |
410ba3a2 | 6970 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
a443755f | 6971 | BTRFS_UUID_SIZE); |
1473b24e | 6972 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
44880fdc | 6973 | BTRFS_FSID_SIZE); |
562d7b15 JB |
6974 | args.uuid = dev_uuid; |
6975 | args.fsid = fs_uuid; | |
2b82032c | 6976 | |
de37aa51 | 6977 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
2ff7e61e | 6978 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
5f375835 MX |
6979 | if (IS_ERR(fs_devices)) |
6980 | return PTR_ERR(fs_devices); | |
2b82032c YZ |
6981 | } |
6982 | ||
562d7b15 | 6983 | device = btrfs_find_device(fs_info->fs_devices, &args); |
5f375835 | 6984 | if (!device) { |
c5502451 | 6985 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
2b902dfc AJ |
6986 | btrfs_report_missing_device(fs_info, devid, |
6987 | dev_uuid, true); | |
45dbdbc9 | 6988 | return -ENOENT; |
c5502451 | 6989 | } |
2b82032c | 6990 | |
2ff7e61e | 6991 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
adfb69af AJ |
6992 | if (IS_ERR(device)) { |
6993 | btrfs_err(fs_info, | |
6994 | "failed to add missing dev %llu: %ld", | |
6995 | devid, PTR_ERR(device)); | |
6996 | return PTR_ERR(device); | |
6997 | } | |
2b902dfc | 6998 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
5f375835 | 6999 | } else { |
c5502451 | 7000 | if (!device->bdev) { |
2b902dfc AJ |
7001 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
7002 | btrfs_report_missing_device(fs_info, | |
7003 | devid, dev_uuid, true); | |
45dbdbc9 | 7004 | return -ENOENT; |
2b902dfc AJ |
7005 | } |
7006 | btrfs_report_missing_device(fs_info, devid, | |
7007 | dev_uuid, false); | |
c5502451 | 7008 | } |
5f375835 | 7009 | |
e6e674bd AJ |
7010 | if (!device->bdev && |
7011 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { | |
cd02dca5 CM |
7012 | /* |
7013 | * this happens when a device that was properly setup | |
7014 | * in the device info lists suddenly goes bad. | |
7015 | * device->bdev is NULL, and so we have to set | |
7016 | * device->missing to one here | |
7017 | */ | |
5f375835 | 7018 | device->fs_devices->missing_devices++; |
e6e674bd | 7019 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
2b82032c | 7020 | } |
5f375835 MX |
7021 | |
7022 | /* Move the device to its own fs_devices */ | |
7023 | if (device->fs_devices != fs_devices) { | |
e6e674bd AJ |
7024 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
7025 | &device->dev_state)); | |
5f375835 MX |
7026 | |
7027 | list_move(&device->dev_list, &fs_devices->devices); | |
7028 | device->fs_devices->num_devices--; | |
7029 | fs_devices->num_devices++; | |
7030 | ||
7031 | device->fs_devices->missing_devices--; | |
7032 | fs_devices->missing_devices++; | |
7033 | ||
7034 | device->fs_devices = fs_devices; | |
7035 | } | |
2b82032c YZ |
7036 | } |
7037 | ||
0b246afa | 7038 | if (device->fs_devices != fs_info->fs_devices) { |
ebbede42 | 7039 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
2b82032c YZ |
7040 | if (device->generation != |
7041 | btrfs_device_generation(leaf, dev_item)) | |
7042 | return -EINVAL; | |
6324fbf3 | 7043 | } |
0b86a832 CM |
7044 | |
7045 | fill_device_from_item(leaf, dev_item, device); | |
3a160a93 | 7046 | if (device->bdev) { |
cda00eba | 7047 | u64 max_total_bytes = bdev_nr_bytes(device->bdev); |
3a160a93 AJ |
7048 | |
7049 | if (device->total_bytes > max_total_bytes) { | |
7050 | btrfs_err(fs_info, | |
7051 | "device total_bytes should be at most %llu but found %llu", | |
7052 | max_total_bytes, device->total_bytes); | |
7053 | return -EINVAL; | |
7054 | } | |
7055 | } | |
e12c9621 | 7056 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
ebbede42 | 7057 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
401e29c1 | 7058 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
2b82032c | 7059 | device->fs_devices->total_rw_bytes += device->total_bytes; |
a5ed45f8 NB |
7060 | atomic64_add(device->total_bytes - device->bytes_used, |
7061 | &fs_info->free_chunk_space); | |
2bf64758 | 7062 | } |
0b86a832 | 7063 | ret = 0; |
0b86a832 CM |
7064 | return ret; |
7065 | } | |
7066 | ||
6bccf3ab | 7067 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
0b86a832 | 7068 | { |
ab8d0fc4 | 7069 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
a061fc8d | 7070 | struct extent_buffer *sb; |
0b86a832 | 7071 | struct btrfs_disk_key *disk_key; |
0b86a832 | 7072 | struct btrfs_chunk *chunk; |
1ffb22cf DS |
7073 | u8 *array_ptr; |
7074 | unsigned long sb_array_offset; | |
84eed90f | 7075 | int ret = 0; |
0b86a832 CM |
7076 | u32 num_stripes; |
7077 | u32 array_size; | |
7078 | u32 len = 0; | |
1ffb22cf | 7079 | u32 cur_offset; |
e06cd3dd | 7080 | u64 type; |
84eed90f | 7081 | struct btrfs_key key; |
0b86a832 | 7082 | |
0b246afa | 7083 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
e959d3c1 | 7084 | |
a83fffb7 | 7085 | /* |
e959d3c1 QW |
7086 | * We allocated a dummy extent, just to use extent buffer accessors. |
7087 | * There will be unused space after BTRFS_SUPER_INFO_SIZE, but | |
7088 | * that's fine, we will not go beyond system chunk array anyway. | |
a83fffb7 | 7089 | */ |
e959d3c1 QW |
7090 | sb = alloc_dummy_extent_buffer(fs_info, BTRFS_SUPER_INFO_OFFSET); |
7091 | if (!sb) | |
7092 | return -ENOMEM; | |
4db8c528 | 7093 | set_extent_buffer_uptodate(sb); |
4008c04a | 7094 | |
a061fc8d | 7095 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
0b86a832 CM |
7096 | array_size = btrfs_super_sys_array_size(super_copy); |
7097 | ||
1ffb22cf DS |
7098 | array_ptr = super_copy->sys_chunk_array; |
7099 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); | |
7100 | cur_offset = 0; | |
0b86a832 | 7101 | |
1ffb22cf DS |
7102 | while (cur_offset < array_size) { |
7103 | disk_key = (struct btrfs_disk_key *)array_ptr; | |
e3540eab DS |
7104 | len = sizeof(*disk_key); |
7105 | if (cur_offset + len > array_size) | |
7106 | goto out_short_read; | |
7107 | ||
0b86a832 CM |
7108 | btrfs_disk_key_to_cpu(&key, disk_key); |
7109 | ||
1ffb22cf DS |
7110 | array_ptr += len; |
7111 | sb_array_offset += len; | |
7112 | cur_offset += len; | |
0b86a832 | 7113 | |
32ab3d1b JT |
7114 | if (key.type != BTRFS_CHUNK_ITEM_KEY) { |
7115 | btrfs_err(fs_info, | |
7116 | "unexpected item type %u in sys_array at offset %u", | |
7117 | (u32)key.type, cur_offset); | |
7118 | ret = -EIO; | |
7119 | break; | |
7120 | } | |
f5cdedd7 | 7121 | |
32ab3d1b JT |
7122 | chunk = (struct btrfs_chunk *)sb_array_offset; |
7123 | /* | |
7124 | * At least one btrfs_chunk with one stripe must be present, | |
7125 | * exact stripe count check comes afterwards | |
7126 | */ | |
7127 | len = btrfs_chunk_item_size(1); | |
7128 | if (cur_offset + len > array_size) | |
7129 | goto out_short_read; | |
e06cd3dd | 7130 | |
32ab3d1b JT |
7131 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
7132 | if (!num_stripes) { | |
7133 | btrfs_err(fs_info, | |
7134 | "invalid number of stripes %u in sys_array at offset %u", | |
7135 | num_stripes, cur_offset); | |
7136 | ret = -EIO; | |
7137 | break; | |
7138 | } | |
e3540eab | 7139 | |
32ab3d1b JT |
7140 | type = btrfs_chunk_type(sb, chunk); |
7141 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { | |
ab8d0fc4 | 7142 | btrfs_err(fs_info, |
32ab3d1b JT |
7143 | "invalid chunk type %llu in sys_array at offset %u", |
7144 | type, cur_offset); | |
84eed90f CM |
7145 | ret = -EIO; |
7146 | break; | |
0b86a832 | 7147 | } |
32ab3d1b JT |
7148 | |
7149 | len = btrfs_chunk_item_size(num_stripes); | |
7150 | if (cur_offset + len > array_size) | |
7151 | goto out_short_read; | |
7152 | ||
7153 | ret = read_one_chunk(&key, sb, chunk); | |
7154 | if (ret) | |
7155 | break; | |
7156 | ||
1ffb22cf DS |
7157 | array_ptr += len; |
7158 | sb_array_offset += len; | |
7159 | cur_offset += len; | |
0b86a832 | 7160 | } |
d865177a | 7161 | clear_extent_buffer_uptodate(sb); |
1c8b5b6e | 7162 | free_extent_buffer_stale(sb); |
84eed90f | 7163 | return ret; |
e3540eab DS |
7164 | |
7165 | out_short_read: | |
ab8d0fc4 | 7166 | btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u", |
e3540eab | 7167 | len, cur_offset); |
d865177a | 7168 | clear_extent_buffer_uptodate(sb); |
1c8b5b6e | 7169 | free_extent_buffer_stale(sb); |
e3540eab | 7170 | return -EIO; |
0b86a832 CM |
7171 | } |
7172 | ||
21634a19 QW |
7173 | /* |
7174 | * Check if all chunks in the fs are OK for read-write degraded mount | |
7175 | * | |
6528b99d AJ |
7176 | * If the @failing_dev is specified, it's accounted as missing. |
7177 | * | |
21634a19 QW |
7178 | * Return true if all chunks meet the minimal RW mount requirements. |
7179 | * Return false if any chunk doesn't meet the minimal RW mount requirements. | |
7180 | */ | |
6528b99d AJ |
7181 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
7182 | struct btrfs_device *failing_dev) | |
21634a19 | 7183 | { |
c8bf1b67 | 7184 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
21634a19 QW |
7185 | struct extent_map *em; |
7186 | u64 next_start = 0; | |
7187 | bool ret = true; | |
7188 | ||
c8bf1b67 DS |
7189 | read_lock(&map_tree->lock); |
7190 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); | |
7191 | read_unlock(&map_tree->lock); | |
21634a19 QW |
7192 | /* No chunk at all? Return false anyway */ |
7193 | if (!em) { | |
7194 | ret = false; | |
7195 | goto out; | |
7196 | } | |
7197 | while (em) { | |
7198 | struct map_lookup *map; | |
7199 | int missing = 0; | |
7200 | int max_tolerated; | |
7201 | int i; | |
7202 | ||
7203 | map = em->map_lookup; | |
7204 | max_tolerated = | |
7205 | btrfs_get_num_tolerated_disk_barrier_failures( | |
7206 | map->type); | |
7207 | for (i = 0; i < map->num_stripes; i++) { | |
7208 | struct btrfs_device *dev = map->stripes[i].dev; | |
7209 | ||
e6e674bd AJ |
7210 | if (!dev || !dev->bdev || |
7211 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || | |
21634a19 QW |
7212 | dev->last_flush_error) |
7213 | missing++; | |
6528b99d AJ |
7214 | else if (failing_dev && failing_dev == dev) |
7215 | missing++; | |
21634a19 QW |
7216 | } |
7217 | if (missing > max_tolerated) { | |
6528b99d AJ |
7218 | if (!failing_dev) |
7219 | btrfs_warn(fs_info, | |
52042d8e | 7220 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
21634a19 QW |
7221 | em->start, missing, max_tolerated); |
7222 | free_extent_map(em); | |
7223 | ret = false; | |
7224 | goto out; | |
7225 | } | |
7226 | next_start = extent_map_end(em); | |
7227 | free_extent_map(em); | |
7228 | ||
c8bf1b67 DS |
7229 | read_lock(&map_tree->lock); |
7230 | em = lookup_extent_mapping(map_tree, next_start, | |
21634a19 | 7231 | (u64)(-1) - next_start); |
c8bf1b67 | 7232 | read_unlock(&map_tree->lock); |
21634a19 QW |
7233 | } |
7234 | out: | |
7235 | return ret; | |
7236 | } | |
7237 | ||
d85327b1 DS |
7238 | static void readahead_tree_node_children(struct extent_buffer *node) |
7239 | { | |
7240 | int i; | |
7241 | const int nr_items = btrfs_header_nritems(node); | |
7242 | ||
bfb484d9 JB |
7243 | for (i = 0; i < nr_items; i++) |
7244 | btrfs_readahead_node_child(node, i); | |
d85327b1 DS |
7245 | } |
7246 | ||
5b4aacef | 7247 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
0b86a832 | 7248 | { |
5b4aacef | 7249 | struct btrfs_root *root = fs_info->chunk_root; |
0b86a832 CM |
7250 | struct btrfs_path *path; |
7251 | struct extent_buffer *leaf; | |
7252 | struct btrfs_key key; | |
7253 | struct btrfs_key found_key; | |
7254 | int ret; | |
7255 | int slot; | |
43cb1478 | 7256 | int iter_ret = 0; |
99e3ecfc | 7257 | u64 total_dev = 0; |
d85327b1 | 7258 | u64 last_ra_node = 0; |
0b86a832 | 7259 | |
0b86a832 CM |
7260 | path = btrfs_alloc_path(); |
7261 | if (!path) | |
7262 | return -ENOMEM; | |
7263 | ||
3dd0f7a3 AJ |
7264 | /* |
7265 | * uuid_mutex is needed only if we are mounting a sprout FS | |
7266 | * otherwise we don't need it. | |
7267 | */ | |
b367e47f | 7268 | mutex_lock(&uuid_mutex); |
b367e47f | 7269 | |
48cfa61b BB |
7270 | /* |
7271 | * It is possible for mount and umount to race in such a way that | |
7272 | * we execute this code path, but open_fs_devices failed to clear | |
7273 | * total_rw_bytes. We certainly want it cleared before reading the | |
7274 | * device items, so clear it here. | |
7275 | */ | |
7276 | fs_info->fs_devices->total_rw_bytes = 0; | |
7277 | ||
4d9380e0 FM |
7278 | /* |
7279 | * Lockdep complains about possible circular locking dependency between | |
7280 | * a disk's open_mutex (struct gendisk.open_mutex), the rw semaphores | |
7281 | * used for freeze procection of a fs (struct super_block.s_writers), | |
7282 | * which we take when starting a transaction, and extent buffers of the | |
7283 | * chunk tree if we call read_one_dev() while holding a lock on an | |
7284 | * extent buffer of the chunk tree. Since we are mounting the filesystem | |
7285 | * and at this point there can't be any concurrent task modifying the | |
7286 | * chunk tree, to keep it simple, just skip locking on the chunk tree. | |
7287 | */ | |
7288 | ASSERT(!test_bit(BTRFS_FS_OPEN, &fs_info->flags)); | |
7289 | path->skip_locking = 1; | |
7290 | ||
395927a9 FDBM |
7291 | /* |
7292 | * Read all device items, and then all the chunk items. All | |
7293 | * device items are found before any chunk item (their object id | |
7294 | * is smaller than the lowest possible object id for a chunk | |
7295 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). | |
0b86a832 CM |
7296 | */ |
7297 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
7298 | key.offset = 0; | |
7299 | key.type = 0; | |
43cb1478 GN |
7300 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { |
7301 | struct extent_buffer *node = path->nodes[1]; | |
d85327b1 | 7302 | |
0b86a832 CM |
7303 | leaf = path->nodes[0]; |
7304 | slot = path->slots[0]; | |
43cb1478 | 7305 | |
d85327b1 DS |
7306 | if (node) { |
7307 | if (last_ra_node != node->start) { | |
7308 | readahead_tree_node_children(node); | |
7309 | last_ra_node = node->start; | |
7310 | } | |
7311 | } | |
395927a9 FDBM |
7312 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
7313 | struct btrfs_dev_item *dev_item; | |
7314 | dev_item = btrfs_item_ptr(leaf, slot, | |
0b86a832 | 7315 | struct btrfs_dev_item); |
17850759 | 7316 | ret = read_one_dev(leaf, dev_item); |
395927a9 FDBM |
7317 | if (ret) |
7318 | goto error; | |
99e3ecfc | 7319 | total_dev++; |
0b86a832 CM |
7320 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
7321 | struct btrfs_chunk *chunk; | |
79bd3712 FM |
7322 | |
7323 | /* | |
7324 | * We are only called at mount time, so no need to take | |
7325 | * fs_info->chunk_mutex. Plus, to avoid lockdep warnings, | |
7326 | * we always lock first fs_info->chunk_mutex before | |
7327 | * acquiring any locks on the chunk tree. This is a | |
7328 | * requirement for chunk allocation, see the comment on | |
7329 | * top of btrfs_chunk_alloc() for details. | |
7330 | */ | |
0b86a832 | 7331 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
9690ac09 | 7332 | ret = read_one_chunk(&found_key, leaf, chunk); |
2b82032c YZ |
7333 | if (ret) |
7334 | goto error; | |
0b86a832 | 7335 | } |
43cb1478 GN |
7336 | } |
7337 | /* Catch error found during iteration */ | |
7338 | if (iter_ret < 0) { | |
7339 | ret = iter_ret; | |
7340 | goto error; | |
0b86a832 | 7341 | } |
99e3ecfc LB |
7342 | |
7343 | /* | |
7344 | * After loading chunk tree, we've got all device information, | |
7345 | * do another round of validation checks. | |
7346 | */ | |
0b246afa | 7347 | if (total_dev != fs_info->fs_devices->total_devices) { |
d201238c QW |
7348 | btrfs_warn(fs_info, |
7349 | "super block num_devices %llu mismatch with DEV_ITEM count %llu, will be repaired on next transaction commit", | |
0b246afa | 7350 | btrfs_super_num_devices(fs_info->super_copy), |
99e3ecfc | 7351 | total_dev); |
d201238c QW |
7352 | fs_info->fs_devices->total_devices = total_dev; |
7353 | btrfs_set_super_num_devices(fs_info->super_copy, total_dev); | |
99e3ecfc | 7354 | } |
0b246afa JM |
7355 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
7356 | fs_info->fs_devices->total_rw_bytes) { | |
7357 | btrfs_err(fs_info, | |
99e3ecfc | 7358 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
0b246afa JM |
7359 | btrfs_super_total_bytes(fs_info->super_copy), |
7360 | fs_info->fs_devices->total_rw_bytes); | |
99e3ecfc LB |
7361 | ret = -EINVAL; |
7362 | goto error; | |
7363 | } | |
0b86a832 CM |
7364 | ret = 0; |
7365 | error: | |
b367e47f LZ |
7366 | mutex_unlock(&uuid_mutex); |
7367 | ||
2b82032c | 7368 | btrfs_free_path(path); |
0b86a832 CM |
7369 | return ret; |
7370 | } | |
442a4f63 | 7371 | |
a8d1b164 | 7372 | int btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
cb517eab | 7373 | { |
944d3f9f | 7374 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; |
cb517eab | 7375 | struct btrfs_device *device; |
a8d1b164 | 7376 | int ret = 0; |
cb517eab | 7377 | |
944d3f9f NB |
7378 | fs_devices->fs_info = fs_info; |
7379 | ||
7380 | mutex_lock(&fs_devices->device_list_mutex); | |
7381 | list_for_each_entry(device, &fs_devices->devices, dev_list) | |
7382 | device->fs_info = fs_info; | |
944d3f9f NB |
7383 | |
7384 | list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) { | |
a8d1b164 | 7385 | list_for_each_entry(device, &seed_devs->devices, dev_list) { |
fb456252 | 7386 | device->fs_info = fs_info; |
a8d1b164 JT |
7387 | ret = btrfs_get_dev_zone_info(device, false); |
7388 | if (ret) | |
7389 | break; | |
7390 | } | |
29cc83f6 | 7391 | |
944d3f9f | 7392 | seed_devs->fs_info = fs_info; |
29cc83f6 | 7393 | } |
e17125b5 | 7394 | mutex_unlock(&fs_devices->device_list_mutex); |
a8d1b164 JT |
7395 | |
7396 | return ret; | |
cb517eab MX |
7397 | } |
7398 | ||
1dc990df DS |
7399 | static u64 btrfs_dev_stats_value(const struct extent_buffer *eb, |
7400 | const struct btrfs_dev_stats_item *ptr, | |
7401 | int index) | |
7402 | { | |
7403 | u64 val; | |
7404 | ||
7405 | read_extent_buffer(eb, &val, | |
7406 | offsetof(struct btrfs_dev_stats_item, values) + | |
7407 | ((unsigned long)ptr) + (index * sizeof(u64)), | |
7408 | sizeof(val)); | |
7409 | return val; | |
7410 | } | |
7411 | ||
7412 | static void btrfs_set_dev_stats_value(struct extent_buffer *eb, | |
7413 | struct btrfs_dev_stats_item *ptr, | |
7414 | int index, u64 val) | |
7415 | { | |
7416 | write_extent_buffer(eb, &val, | |
7417 | offsetof(struct btrfs_dev_stats_item, values) + | |
7418 | ((unsigned long)ptr) + (index * sizeof(u64)), | |
7419 | sizeof(val)); | |
7420 | } | |
7421 | ||
92e26df4 JB |
7422 | static int btrfs_device_init_dev_stats(struct btrfs_device *device, |
7423 | struct btrfs_path *path) | |
733f4fbb | 7424 | { |
124604eb | 7425 | struct btrfs_dev_stats_item *ptr; |
733f4fbb | 7426 | struct extent_buffer *eb; |
124604eb JB |
7427 | struct btrfs_key key; |
7428 | int item_size; | |
7429 | int i, ret, slot; | |
7430 | ||
82d62d06 JB |
7431 | if (!device->fs_info->dev_root) |
7432 | return 0; | |
7433 | ||
124604eb JB |
7434 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
7435 | key.type = BTRFS_PERSISTENT_ITEM_KEY; | |
7436 | key.offset = device->devid; | |
7437 | ret = btrfs_search_slot(NULL, device->fs_info->dev_root, &key, path, 0, 0); | |
7438 | if (ret) { | |
7439 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7440 | btrfs_dev_stat_set(device, i, 0); | |
7441 | device->dev_stats_valid = 1; | |
7442 | btrfs_release_path(path); | |
92e26df4 | 7443 | return ret < 0 ? ret : 0; |
124604eb JB |
7444 | } |
7445 | slot = path->slots[0]; | |
7446 | eb = path->nodes[0]; | |
3212fa14 | 7447 | item_size = btrfs_item_size(eb, slot); |
124604eb JB |
7448 | |
7449 | ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_stats_item); | |
7450 | ||
7451 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { | |
7452 | if (item_size >= (1 + i) * sizeof(__le64)) | |
7453 | btrfs_dev_stat_set(device, i, | |
7454 | btrfs_dev_stats_value(eb, ptr, i)); | |
7455 | else | |
7456 | btrfs_dev_stat_set(device, i, 0); | |
7457 | } | |
7458 | ||
7459 | device->dev_stats_valid = 1; | |
7460 | btrfs_dev_stat_print_on_load(device); | |
7461 | btrfs_release_path(path); | |
92e26df4 JB |
7462 | |
7463 | return 0; | |
124604eb JB |
7464 | } |
7465 | ||
7466 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) | |
7467 | { | |
7468 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; | |
733f4fbb SB |
7469 | struct btrfs_device *device; |
7470 | struct btrfs_path *path = NULL; | |
92e26df4 | 7471 | int ret = 0; |
733f4fbb SB |
7472 | |
7473 | path = btrfs_alloc_path(); | |
3b80a984 AJ |
7474 | if (!path) |
7475 | return -ENOMEM; | |
733f4fbb SB |
7476 | |
7477 | mutex_lock(&fs_devices->device_list_mutex); | |
92e26df4 JB |
7478 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
7479 | ret = btrfs_device_init_dev_stats(device, path); | |
7480 | if (ret) | |
7481 | goto out; | |
7482 | } | |
124604eb | 7483 | list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) { |
92e26df4 JB |
7484 | list_for_each_entry(device, &seed_devs->devices, dev_list) { |
7485 | ret = btrfs_device_init_dev_stats(device, path); | |
7486 | if (ret) | |
7487 | goto out; | |
7488 | } | |
733f4fbb | 7489 | } |
92e26df4 | 7490 | out: |
733f4fbb SB |
7491 | mutex_unlock(&fs_devices->device_list_mutex); |
7492 | ||
733f4fbb | 7493 | btrfs_free_path(path); |
92e26df4 | 7494 | return ret; |
733f4fbb SB |
7495 | } |
7496 | ||
7497 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, | |
733f4fbb SB |
7498 | struct btrfs_device *device) |
7499 | { | |
5495f195 | 7500 | struct btrfs_fs_info *fs_info = trans->fs_info; |
6bccf3ab | 7501 | struct btrfs_root *dev_root = fs_info->dev_root; |
733f4fbb SB |
7502 | struct btrfs_path *path; |
7503 | struct btrfs_key key; | |
7504 | struct extent_buffer *eb; | |
7505 | struct btrfs_dev_stats_item *ptr; | |
7506 | int ret; | |
7507 | int i; | |
7508 | ||
242e2956 DS |
7509 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
7510 | key.type = BTRFS_PERSISTENT_ITEM_KEY; | |
733f4fbb SB |
7511 | key.offset = device->devid; |
7512 | ||
7513 | path = btrfs_alloc_path(); | |
fa252992 DS |
7514 | if (!path) |
7515 | return -ENOMEM; | |
733f4fbb SB |
7516 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
7517 | if (ret < 0) { | |
0b246afa | 7518 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7519 | "error %d while searching for dev_stats item for device %s", |
cb3e217b | 7520 | ret, btrfs_dev_name(device)); |
733f4fbb SB |
7521 | goto out; |
7522 | } | |
7523 | ||
7524 | if (ret == 0 && | |
3212fa14 | 7525 | btrfs_item_size(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
733f4fbb SB |
7526 | /* need to delete old one and insert a new one */ |
7527 | ret = btrfs_del_item(trans, dev_root, path); | |
7528 | if (ret != 0) { | |
0b246afa | 7529 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7530 | "delete too small dev_stats item for device %s failed %d", |
cb3e217b | 7531 | btrfs_dev_name(device), ret); |
733f4fbb SB |
7532 | goto out; |
7533 | } | |
7534 | ret = 1; | |
7535 | } | |
7536 | ||
7537 | if (ret == 1) { | |
7538 | /* need to insert a new item */ | |
7539 | btrfs_release_path(path); | |
7540 | ret = btrfs_insert_empty_item(trans, dev_root, path, | |
7541 | &key, sizeof(*ptr)); | |
7542 | if (ret < 0) { | |
0b246afa | 7543 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7544 | "insert dev_stats item for device %s failed %d", |
cb3e217b | 7545 | btrfs_dev_name(device), ret); |
733f4fbb SB |
7546 | goto out; |
7547 | } | |
7548 | } | |
7549 | ||
7550 | eb = path->nodes[0]; | |
7551 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); | |
7552 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7553 | btrfs_set_dev_stats_value(eb, ptr, i, | |
7554 | btrfs_dev_stat_read(device, i)); | |
7555 | btrfs_mark_buffer_dirty(eb); | |
7556 | ||
7557 | out: | |
7558 | btrfs_free_path(path); | |
7559 | return ret; | |
7560 | } | |
7561 | ||
7562 | /* | |
7563 | * called from commit_transaction. Writes all changed device stats to disk. | |
7564 | */ | |
196c9d8d | 7565 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
733f4fbb | 7566 | { |
196c9d8d | 7567 | struct btrfs_fs_info *fs_info = trans->fs_info; |
733f4fbb SB |
7568 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
7569 | struct btrfs_device *device; | |
addc3fa7 | 7570 | int stats_cnt; |
733f4fbb SB |
7571 | int ret = 0; |
7572 | ||
7573 | mutex_lock(&fs_devices->device_list_mutex); | |
7574 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
9deae968 NB |
7575 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
7576 | if (!device->dev_stats_valid || stats_cnt == 0) | |
733f4fbb SB |
7577 | continue; |
7578 | ||
9deae968 NB |
7579 | |
7580 | /* | |
7581 | * There is a LOAD-LOAD control dependency between the value of | |
7582 | * dev_stats_ccnt and updating the on-disk values which requires | |
7583 | * reading the in-memory counters. Such control dependencies | |
7584 | * require explicit read memory barriers. | |
7585 | * | |
7586 | * This memory barriers pairs with smp_mb__before_atomic in | |
7587 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full | |
7588 | * barrier implied by atomic_xchg in | |
7589 | * btrfs_dev_stats_read_and_reset | |
7590 | */ | |
7591 | smp_rmb(); | |
7592 | ||
5495f195 | 7593 | ret = update_dev_stat_item(trans, device); |
733f4fbb | 7594 | if (!ret) |
addc3fa7 | 7595 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
733f4fbb SB |
7596 | } |
7597 | mutex_unlock(&fs_devices->device_list_mutex); | |
7598 | ||
7599 | return ret; | |
7600 | } | |
7601 | ||
442a4f63 SB |
7602 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
7603 | { | |
7604 | btrfs_dev_stat_inc(dev, index); | |
442a4f63 | 7605 | |
733f4fbb SB |
7606 | if (!dev->dev_stats_valid) |
7607 | return; | |
fb456252 | 7608 | btrfs_err_rl_in_rcu(dev->fs_info, |
b14af3b4 | 7609 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
cb3e217b | 7610 | btrfs_dev_name(dev), |
442a4f63 SB |
7611 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
7612 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), | |
7613 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), | |
efe120a0 FH |
7614 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
7615 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); | |
442a4f63 | 7616 | } |
c11d2c23 | 7617 | |
733f4fbb SB |
7618 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
7619 | { | |
a98cdb85 SB |
7620 | int i; |
7621 | ||
7622 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7623 | if (btrfs_dev_stat_read(dev, i) != 0) | |
7624 | break; | |
7625 | if (i == BTRFS_DEV_STAT_VALUES_MAX) | |
7626 | return; /* all values == 0, suppress message */ | |
7627 | ||
fb456252 | 7628 | btrfs_info_in_rcu(dev->fs_info, |
ecaeb14b | 7629 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
cb3e217b | 7630 | btrfs_dev_name(dev), |
733f4fbb SB |
7631 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
7632 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), | |
7633 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), | |
7634 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), | |
7635 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); | |
7636 | } | |
7637 | ||
2ff7e61e | 7638 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 7639 | struct btrfs_ioctl_get_dev_stats *stats) |
c11d2c23 | 7640 | { |
562d7b15 | 7641 | BTRFS_DEV_LOOKUP_ARGS(args); |
c11d2c23 | 7642 | struct btrfs_device *dev; |
0b246afa | 7643 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
c11d2c23 SB |
7644 | int i; |
7645 | ||
7646 | mutex_lock(&fs_devices->device_list_mutex); | |
562d7b15 JB |
7647 | args.devid = stats->devid; |
7648 | dev = btrfs_find_device(fs_info->fs_devices, &args); | |
c11d2c23 SB |
7649 | mutex_unlock(&fs_devices->device_list_mutex); |
7650 | ||
7651 | if (!dev) { | |
0b246afa | 7652 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
c11d2c23 | 7653 | return -ENODEV; |
733f4fbb | 7654 | } else if (!dev->dev_stats_valid) { |
0b246afa | 7655 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
733f4fbb | 7656 | return -ENODEV; |
b27f7c0c | 7657 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
c11d2c23 SB |
7658 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
7659 | if (stats->nr_items > i) | |
7660 | stats->values[i] = | |
7661 | btrfs_dev_stat_read_and_reset(dev, i); | |
7662 | else | |
4e411a7d | 7663 | btrfs_dev_stat_set(dev, i, 0); |
c11d2c23 | 7664 | } |
a69976bc AJ |
7665 | btrfs_info(fs_info, "device stats zeroed by %s (%d)", |
7666 | current->comm, task_pid_nr(current)); | |
c11d2c23 SB |
7667 | } else { |
7668 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7669 | if (stats->nr_items > i) | |
7670 | stats->values[i] = btrfs_dev_stat_read(dev, i); | |
7671 | } | |
7672 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) | |
7673 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; | |
7674 | return 0; | |
7675 | } | |
a8a6dab7 | 7676 | |
935e5cc9 | 7677 | /* |
bbbf7243 NB |
7678 | * Update the size and bytes used for each device where it changed. This is |
7679 | * delayed since we would otherwise get errors while writing out the | |
7680 | * superblocks. | |
7681 | * | |
7682 | * Must be invoked during transaction commit. | |
935e5cc9 | 7683 | */ |
bbbf7243 | 7684 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
935e5cc9 | 7685 | { |
935e5cc9 MX |
7686 | struct btrfs_device *curr, *next; |
7687 | ||
bbbf7243 | 7688 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
ce7213c7 | 7689 | |
bbbf7243 | 7690 | if (list_empty(&trans->dev_update_list)) |
ce7213c7 MX |
7691 | return; |
7692 | ||
bbbf7243 NB |
7693 | /* |
7694 | * We don't need the device_list_mutex here. This list is owned by the | |
7695 | * transaction and the transaction must complete before the device is | |
7696 | * released. | |
7697 | */ | |
7698 | mutex_lock(&trans->fs_info->chunk_mutex); | |
7699 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, | |
7700 | post_commit_list) { | |
7701 | list_del_init(&curr->post_commit_list); | |
7702 | curr->commit_total_bytes = curr->disk_total_bytes; | |
7703 | curr->commit_bytes_used = curr->bytes_used; | |
ce7213c7 | 7704 | } |
bbbf7243 | 7705 | mutex_unlock(&trans->fs_info->chunk_mutex); |
ce7213c7 | 7706 | } |
5a13f430 | 7707 | |
46df06b8 DS |
7708 | /* |
7709 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. | |
7710 | */ | |
7711 | int btrfs_bg_type_to_factor(u64 flags) | |
7712 | { | |
44b28ada DS |
7713 | const int index = btrfs_bg_flags_to_raid_index(flags); |
7714 | ||
7715 | return btrfs_raid_array[index].ncopies; | |
46df06b8 | 7716 | } |
cf90d884 QW |
7717 | |
7718 | ||
cf90d884 QW |
7719 | |
7720 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, | |
7721 | u64 chunk_offset, u64 devid, | |
7722 | u64 physical_offset, u64 physical_len) | |
7723 | { | |
562d7b15 | 7724 | struct btrfs_dev_lookup_args args = { .devid = devid }; |
c8bf1b67 | 7725 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
cf90d884 QW |
7726 | struct extent_map *em; |
7727 | struct map_lookup *map; | |
05a37c48 | 7728 | struct btrfs_device *dev; |
cf90d884 QW |
7729 | u64 stripe_len; |
7730 | bool found = false; | |
7731 | int ret = 0; | |
7732 | int i; | |
7733 | ||
7734 | read_lock(&em_tree->lock); | |
7735 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); | |
7736 | read_unlock(&em_tree->lock); | |
7737 | ||
7738 | if (!em) { | |
7739 | btrfs_err(fs_info, | |
7740 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", | |
7741 | physical_offset, devid); | |
7742 | ret = -EUCLEAN; | |
7743 | goto out; | |
7744 | } | |
7745 | ||
7746 | map = em->map_lookup; | |
bc88b486 | 7747 | stripe_len = btrfs_calc_stripe_length(em); |
cf90d884 QW |
7748 | if (physical_len != stripe_len) { |
7749 | btrfs_err(fs_info, | |
7750 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", | |
7751 | physical_offset, devid, em->start, physical_len, | |
7752 | stripe_len); | |
7753 | ret = -EUCLEAN; | |
7754 | goto out; | |
7755 | } | |
7756 | ||
3613249a QW |
7757 | /* |
7758 | * Very old mkfs.btrfs (before v4.1) will not respect the reserved | |
7759 | * space. Although kernel can handle it without problem, better to warn | |
7760 | * the users. | |
7761 | */ | |
7762 | if (physical_offset < BTRFS_DEVICE_RANGE_RESERVED) | |
7763 | btrfs_warn(fs_info, | |
7764 | "devid %llu physical %llu len %llu inside the reserved space", | |
7765 | devid, physical_offset, physical_len); | |
7766 | ||
cf90d884 QW |
7767 | for (i = 0; i < map->num_stripes; i++) { |
7768 | if (map->stripes[i].dev->devid == devid && | |
7769 | map->stripes[i].physical == physical_offset) { | |
7770 | found = true; | |
7771 | if (map->verified_stripes >= map->num_stripes) { | |
7772 | btrfs_err(fs_info, | |
7773 | "too many dev extents for chunk %llu found", | |
7774 | em->start); | |
7775 | ret = -EUCLEAN; | |
7776 | goto out; | |
7777 | } | |
7778 | map->verified_stripes++; | |
7779 | break; | |
7780 | } | |
7781 | } | |
7782 | if (!found) { | |
7783 | btrfs_err(fs_info, | |
7784 | "dev extent physical offset %llu devid %llu has no corresponding chunk", | |
7785 | physical_offset, devid); | |
7786 | ret = -EUCLEAN; | |
7787 | } | |
05a37c48 | 7788 | |
1a9fd417 | 7789 | /* Make sure no dev extent is beyond device boundary */ |
562d7b15 | 7790 | dev = btrfs_find_device(fs_info->fs_devices, &args); |
05a37c48 QW |
7791 | if (!dev) { |
7792 | btrfs_err(fs_info, "failed to find devid %llu", devid); | |
7793 | ret = -EUCLEAN; | |
7794 | goto out; | |
7795 | } | |
1b3922a8 | 7796 | |
05a37c48 QW |
7797 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
7798 | btrfs_err(fs_info, | |
7799 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", | |
7800 | devid, physical_offset, physical_len, | |
7801 | dev->disk_total_bytes); | |
7802 | ret = -EUCLEAN; | |
7803 | goto out; | |
7804 | } | |
381a696e NA |
7805 | |
7806 | if (dev->zone_info) { | |
7807 | u64 zone_size = dev->zone_info->zone_size; | |
7808 | ||
7809 | if (!IS_ALIGNED(physical_offset, zone_size) || | |
7810 | !IS_ALIGNED(physical_len, zone_size)) { | |
7811 | btrfs_err(fs_info, | |
7812 | "zoned: dev extent devid %llu physical offset %llu len %llu is not aligned to device zone", | |
7813 | devid, physical_offset, physical_len); | |
7814 | ret = -EUCLEAN; | |
7815 | goto out; | |
7816 | } | |
7817 | } | |
7818 | ||
cf90d884 QW |
7819 | out: |
7820 | free_extent_map(em); | |
7821 | return ret; | |
7822 | } | |
7823 | ||
7824 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) | |
7825 | { | |
c8bf1b67 | 7826 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
cf90d884 QW |
7827 | struct extent_map *em; |
7828 | struct rb_node *node; | |
7829 | int ret = 0; | |
7830 | ||
7831 | read_lock(&em_tree->lock); | |
07e1ce09 | 7832 | for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) { |
cf90d884 QW |
7833 | em = rb_entry(node, struct extent_map, rb_node); |
7834 | if (em->map_lookup->num_stripes != | |
7835 | em->map_lookup->verified_stripes) { | |
7836 | btrfs_err(fs_info, | |
7837 | "chunk %llu has missing dev extent, have %d expect %d", | |
7838 | em->start, em->map_lookup->verified_stripes, | |
7839 | em->map_lookup->num_stripes); | |
7840 | ret = -EUCLEAN; | |
7841 | goto out; | |
7842 | } | |
7843 | } | |
7844 | out: | |
7845 | read_unlock(&em_tree->lock); | |
7846 | return ret; | |
7847 | } | |
7848 | ||
7849 | /* | |
7850 | * Ensure that all dev extents are mapped to correct chunk, otherwise | |
7851 | * later chunk allocation/free would cause unexpected behavior. | |
7852 | * | |
7853 | * NOTE: This will iterate through the whole device tree, which should be of | |
7854 | * the same size level as the chunk tree. This slightly increases mount time. | |
7855 | */ | |
7856 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) | |
7857 | { | |
7858 | struct btrfs_path *path; | |
7859 | struct btrfs_root *root = fs_info->dev_root; | |
7860 | struct btrfs_key key; | |
5eb19381 QW |
7861 | u64 prev_devid = 0; |
7862 | u64 prev_dev_ext_end = 0; | |
cf90d884 QW |
7863 | int ret = 0; |
7864 | ||
42437a63 JB |
7865 | /* |
7866 | * We don't have a dev_root because we mounted with ignorebadroots and | |
7867 | * failed to load the root, so we want to skip the verification in this | |
7868 | * case for sure. | |
7869 | * | |
7870 | * However if the dev root is fine, but the tree itself is corrupted | |
7871 | * we'd still fail to mount. This verification is only to make sure | |
7872 | * writes can happen safely, so instead just bypass this check | |
7873 | * completely in the case of IGNOREBADROOTS. | |
7874 | */ | |
7875 | if (btrfs_test_opt(fs_info, IGNOREBADROOTS)) | |
7876 | return 0; | |
7877 | ||
cf90d884 QW |
7878 | key.objectid = 1; |
7879 | key.type = BTRFS_DEV_EXTENT_KEY; | |
7880 | key.offset = 0; | |
7881 | ||
7882 | path = btrfs_alloc_path(); | |
7883 | if (!path) | |
7884 | return -ENOMEM; | |
7885 | ||
7886 | path->reada = READA_FORWARD; | |
7887 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
7888 | if (ret < 0) | |
7889 | goto out; | |
7890 | ||
7891 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
ad9a9378 | 7892 | ret = btrfs_next_leaf(root, path); |
cf90d884 QW |
7893 | if (ret < 0) |
7894 | goto out; | |
7895 | /* No dev extents at all? Not good */ | |
7896 | if (ret > 0) { | |
7897 | ret = -EUCLEAN; | |
7898 | goto out; | |
7899 | } | |
7900 | } | |
7901 | while (1) { | |
7902 | struct extent_buffer *leaf = path->nodes[0]; | |
7903 | struct btrfs_dev_extent *dext; | |
7904 | int slot = path->slots[0]; | |
7905 | u64 chunk_offset; | |
7906 | u64 physical_offset; | |
7907 | u64 physical_len; | |
7908 | u64 devid; | |
7909 | ||
7910 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
7911 | if (key.type != BTRFS_DEV_EXTENT_KEY) | |
7912 | break; | |
7913 | devid = key.objectid; | |
7914 | physical_offset = key.offset; | |
7915 | ||
7916 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); | |
7917 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); | |
7918 | physical_len = btrfs_dev_extent_length(leaf, dext); | |
7919 | ||
5eb19381 QW |
7920 | /* Check if this dev extent overlaps with the previous one */ |
7921 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { | |
7922 | btrfs_err(fs_info, | |
7923 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", | |
7924 | devid, physical_offset, prev_dev_ext_end); | |
7925 | ret = -EUCLEAN; | |
7926 | goto out; | |
7927 | } | |
7928 | ||
cf90d884 QW |
7929 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
7930 | physical_offset, physical_len); | |
7931 | if (ret < 0) | |
7932 | goto out; | |
5eb19381 QW |
7933 | prev_devid = devid; |
7934 | prev_dev_ext_end = physical_offset + physical_len; | |
7935 | ||
cf90d884 QW |
7936 | ret = btrfs_next_item(root, path); |
7937 | if (ret < 0) | |
7938 | goto out; | |
7939 | if (ret > 0) { | |
7940 | ret = 0; | |
7941 | break; | |
7942 | } | |
7943 | } | |
7944 | ||
7945 | /* Ensure all chunks have corresponding dev extents */ | |
7946 | ret = verify_chunk_dev_extent_mapping(fs_info); | |
7947 | out: | |
7948 | btrfs_free_path(path); | |
7949 | return ret; | |
7950 | } | |
eede2bf3 OS |
7951 | |
7952 | /* | |
7953 | * Check whether the given block group or device is pinned by any inode being | |
7954 | * used as a swapfile. | |
7955 | */ | |
7956 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) | |
7957 | { | |
7958 | struct btrfs_swapfile_pin *sp; | |
7959 | struct rb_node *node; | |
7960 | ||
7961 | spin_lock(&fs_info->swapfile_pins_lock); | |
7962 | node = fs_info->swapfile_pins.rb_node; | |
7963 | while (node) { | |
7964 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); | |
7965 | if (ptr < sp->ptr) | |
7966 | node = node->rb_left; | |
7967 | else if (ptr > sp->ptr) | |
7968 | node = node->rb_right; | |
7969 | else | |
7970 | break; | |
7971 | } | |
7972 | spin_unlock(&fs_info->swapfile_pins_lock); | |
7973 | return node != NULL; | |
7974 | } | |
f7ef5287 NA |
7975 | |
7976 | static int relocating_repair_kthread(void *data) | |
7977 | { | |
0d031dc4 | 7978 | struct btrfs_block_group *cache = data; |
f7ef5287 NA |
7979 | struct btrfs_fs_info *fs_info = cache->fs_info; |
7980 | u64 target; | |
7981 | int ret = 0; | |
7982 | ||
7983 | target = cache->start; | |
7984 | btrfs_put_block_group(cache); | |
7985 | ||
ca5e4ea0 | 7986 | sb_start_write(fs_info->sb); |
f7ef5287 NA |
7987 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) { |
7988 | btrfs_info(fs_info, | |
7989 | "zoned: skip relocating block group %llu to repair: EBUSY", | |
7990 | target); | |
ca5e4ea0 | 7991 | sb_end_write(fs_info->sb); |
f7ef5287 NA |
7992 | return -EBUSY; |
7993 | } | |
7994 | ||
f3372065 | 7995 | mutex_lock(&fs_info->reclaim_bgs_lock); |
f7ef5287 NA |
7996 | |
7997 | /* Ensure block group still exists */ | |
7998 | cache = btrfs_lookup_block_group(fs_info, target); | |
7999 | if (!cache) | |
8000 | goto out; | |
8001 | ||
3349b57f | 8002 | if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags)) |
f7ef5287 NA |
8003 | goto out; |
8004 | ||
8005 | ret = btrfs_may_alloc_data_chunk(fs_info, target); | |
8006 | if (ret < 0) | |
8007 | goto out; | |
8008 | ||
8009 | btrfs_info(fs_info, | |
8010 | "zoned: relocating block group %llu to repair IO failure", | |
8011 | target); | |
8012 | ret = btrfs_relocate_chunk(fs_info, target); | |
8013 | ||
8014 | out: | |
8015 | if (cache) | |
8016 | btrfs_put_block_group(cache); | |
f3372065 | 8017 | mutex_unlock(&fs_info->reclaim_bgs_lock); |
f7ef5287 | 8018 | btrfs_exclop_finish(fs_info); |
ca5e4ea0 | 8019 | sb_end_write(fs_info->sb); |
f7ef5287 NA |
8020 | |
8021 | return ret; | |
8022 | } | |
8023 | ||
554aed7d | 8024 | bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical) |
f7ef5287 NA |
8025 | { |
8026 | struct btrfs_block_group *cache; | |
8027 | ||
554aed7d JT |
8028 | if (!btrfs_is_zoned(fs_info)) |
8029 | return false; | |
8030 | ||
f7ef5287 NA |
8031 | /* Do not attempt to repair in degraded state */ |
8032 | if (btrfs_test_opt(fs_info, DEGRADED)) | |
554aed7d | 8033 | return true; |
f7ef5287 NA |
8034 | |
8035 | cache = btrfs_lookup_block_group(fs_info, logical); | |
8036 | if (!cache) | |
554aed7d | 8037 | return true; |
f7ef5287 | 8038 | |
3349b57f | 8039 | if (test_and_set_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags)) { |
f7ef5287 | 8040 | btrfs_put_block_group(cache); |
554aed7d | 8041 | return true; |
f7ef5287 | 8042 | } |
f7ef5287 NA |
8043 | |
8044 | kthread_run(relocating_repair_kthread, cache, | |
8045 | "btrfs-relocating-repair"); | |
8046 | ||
554aed7d | 8047 | return true; |
f7ef5287 | 8048 | } |
4886ff7b QW |
8049 | |
8050 | static void map_raid56_repair_block(struct btrfs_io_context *bioc, | |
8051 | struct btrfs_io_stripe *smap, | |
8052 | u64 logical) | |
8053 | { | |
8054 | int data_stripes = nr_bioc_data_stripes(bioc); | |
8055 | int i; | |
8056 | ||
8057 | for (i = 0; i < data_stripes; i++) { | |
8058 | u64 stripe_start = bioc->full_stripe_logical + | |
cb091225 | 8059 | btrfs_stripe_nr_to_offset(i); |
4886ff7b QW |
8060 | |
8061 | if (logical >= stripe_start && | |
8062 | logical < stripe_start + BTRFS_STRIPE_LEN) | |
8063 | break; | |
8064 | } | |
8065 | ASSERT(i < data_stripes); | |
8066 | smap->dev = bioc->stripes[i].dev; | |
8067 | smap->physical = bioc->stripes[i].physical + | |
8068 | ((logical - bioc->full_stripe_logical) & | |
8069 | BTRFS_STRIPE_LEN_MASK); | |
8070 | } | |
8071 | ||
8072 | /* | |
8073 | * Map a repair write into a single device. | |
8074 | * | |
8075 | * A repair write is triggered by read time repair or scrub, which would only | |
8076 | * update the contents of a single device. | |
8077 | * Not update any other mirrors nor go through RMW path. | |
8078 | * | |
8079 | * Callers should ensure: | |
8080 | * | |
8081 | * - Call btrfs_bio_counter_inc_blocked() first | |
8082 | * - The range does not cross stripe boundary | |
8083 | * - Has a valid @mirror_num passed in. | |
8084 | */ | |
8085 | int btrfs_map_repair_block(struct btrfs_fs_info *fs_info, | |
8086 | struct btrfs_io_stripe *smap, u64 logical, | |
8087 | u32 length, int mirror_num) | |
8088 | { | |
8089 | struct btrfs_io_context *bioc = NULL; | |
8090 | u64 map_length = length; | |
8091 | int mirror_ret = mirror_num; | |
8092 | int ret; | |
8093 | ||
8094 | ASSERT(mirror_num > 0); | |
8095 | ||
cd4efd21 | 8096 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, &map_length, |
9fb2acc2 | 8097 | &bioc, smap, &mirror_ret); |
4886ff7b QW |
8098 | if (ret < 0) |
8099 | return ret; | |
8100 | ||
8101 | /* The map range should not cross stripe boundary. */ | |
8102 | ASSERT(map_length >= length); | |
8103 | ||
8104 | /* Already mapped to single stripe. */ | |
8105 | if (!bioc) | |
8106 | goto out; | |
8107 | ||
8108 | /* Map the RAID56 multi-stripe writes to a single one. */ | |
8109 | if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { | |
8110 | map_raid56_repair_block(bioc, smap, logical); | |
8111 | goto out; | |
8112 | } | |
8113 | ||
8114 | ASSERT(mirror_num <= bioc->num_stripes); | |
8115 | smap->dev = bioc->stripes[mirror_num - 1].dev; | |
8116 | smap->physical = bioc->stripes[mirror_num - 1].physical; | |
8117 | out: | |
8118 | btrfs_put_bioc(bioc); | |
8119 | ASSERT(smap->dev); | |
8120 | return 0; | |
8121 | } |