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