]>
Commit | Line | Data |
---|---|---|
0b86a832 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | #include <linux/sched.h> | |
19 | #include <linux/bio.h> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
8a4b83cc | 21 | #include <linux/buffer_head.h> |
f2d8d74d | 22 | #include <linux/blkdev.h> |
b765ead5 | 23 | #include <linux/iocontext.h> |
6f88a440 | 24 | #include <linux/capability.h> |
442a4f63 | 25 | #include <linux/ratelimit.h> |
59641015 | 26 | #include <linux/kthread.h> |
53b381b3 | 27 | #include <linux/raid/pq.h> |
803b2f54 | 28 | #include <linux/semaphore.h> |
8da4b8c4 | 29 | #include <linux/uuid.h> |
53b381b3 | 30 | #include <asm/div64.h> |
0b86a832 CM |
31 | #include "ctree.h" |
32 | #include "extent_map.h" | |
33 | #include "disk-io.h" | |
34 | #include "transaction.h" | |
35 | #include "print-tree.h" | |
36 | #include "volumes.h" | |
53b381b3 | 37 | #include "raid56.h" |
8b712842 | 38 | #include "async-thread.h" |
21adbd5c | 39 | #include "check-integrity.h" |
606686ee | 40 | #include "rcu-string.h" |
3fed40cc | 41 | #include "math.h" |
8dabb742 | 42 | #include "dev-replace.h" |
99994cde | 43 | #include "sysfs.h" |
0b86a832 | 44 | |
af902047 ZL |
45 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
46 | [BTRFS_RAID_RAID10] = { | |
47 | .sub_stripes = 2, | |
48 | .dev_stripes = 1, | |
49 | .devs_max = 0, /* 0 == as many as possible */ | |
50 | .devs_min = 4, | |
8789f4fe | 51 | .tolerated_failures = 1, |
af902047 ZL |
52 | .devs_increment = 2, |
53 | .ncopies = 2, | |
54 | }, | |
55 | [BTRFS_RAID_RAID1] = { | |
56 | .sub_stripes = 1, | |
57 | .dev_stripes = 1, | |
58 | .devs_max = 2, | |
59 | .devs_min = 2, | |
8789f4fe | 60 | .tolerated_failures = 1, |
af902047 ZL |
61 | .devs_increment = 2, |
62 | .ncopies = 2, | |
63 | }, | |
64 | [BTRFS_RAID_DUP] = { | |
65 | .sub_stripes = 1, | |
66 | .dev_stripes = 2, | |
67 | .devs_max = 1, | |
68 | .devs_min = 1, | |
8789f4fe | 69 | .tolerated_failures = 0, |
af902047 ZL |
70 | .devs_increment = 1, |
71 | .ncopies = 2, | |
72 | }, | |
73 | [BTRFS_RAID_RAID0] = { | |
74 | .sub_stripes = 1, | |
75 | .dev_stripes = 1, | |
76 | .devs_max = 0, | |
77 | .devs_min = 2, | |
8789f4fe | 78 | .tolerated_failures = 0, |
af902047 ZL |
79 | .devs_increment = 1, |
80 | .ncopies = 1, | |
81 | }, | |
82 | [BTRFS_RAID_SINGLE] = { | |
83 | .sub_stripes = 1, | |
84 | .dev_stripes = 1, | |
85 | .devs_max = 1, | |
86 | .devs_min = 1, | |
8789f4fe | 87 | .tolerated_failures = 0, |
af902047 ZL |
88 | .devs_increment = 1, |
89 | .ncopies = 1, | |
90 | }, | |
91 | [BTRFS_RAID_RAID5] = { | |
92 | .sub_stripes = 1, | |
93 | .dev_stripes = 1, | |
94 | .devs_max = 0, | |
95 | .devs_min = 2, | |
8789f4fe | 96 | .tolerated_failures = 1, |
af902047 ZL |
97 | .devs_increment = 1, |
98 | .ncopies = 2, | |
99 | }, | |
100 | [BTRFS_RAID_RAID6] = { | |
101 | .sub_stripes = 1, | |
102 | .dev_stripes = 1, | |
103 | .devs_max = 0, | |
104 | .devs_min = 3, | |
8789f4fe | 105 | .tolerated_failures = 2, |
af902047 ZL |
106 | .devs_increment = 1, |
107 | .ncopies = 3, | |
108 | }, | |
109 | }; | |
110 | ||
fb75d857 | 111 | const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = { |
af902047 ZL |
112 | [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10, |
113 | [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1, | |
114 | [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP, | |
115 | [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0, | |
116 | [BTRFS_RAID_SINGLE] = 0, | |
117 | [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5, | |
118 | [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6, | |
119 | }; | |
120 | ||
621292ba DS |
121 | /* |
122 | * Table to convert BTRFS_RAID_* to the error code if minimum number of devices | |
123 | * condition is not met. Zero means there's no corresponding | |
124 | * BTRFS_ERROR_DEV_*_NOT_MET value. | |
125 | */ | |
126 | const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = { | |
127 | [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, | |
128 | [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, | |
129 | [BTRFS_RAID_DUP] = 0, | |
130 | [BTRFS_RAID_RAID0] = 0, | |
131 | [BTRFS_RAID_SINGLE] = 0, | |
132 | [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, | |
133 | [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, | |
134 | }; | |
135 | ||
2b82032c | 136 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
e4a4dce7 | 137 | struct btrfs_fs_info *fs_info); |
2ff7e61e | 138 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
733f4fbb | 139 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
48a3b636 | 140 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
733f4fbb | 141 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
5ab56090 LB |
142 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
143 | enum btrfs_map_op op, | |
144 | u64 logical, u64 *length, | |
145 | struct btrfs_bio **bbio_ret, | |
146 | int mirror_num, int need_raid_map); | |
2b82032c | 147 | |
67a2c45e | 148 | DEFINE_MUTEX(uuid_mutex); |
8a4b83cc | 149 | static LIST_HEAD(fs_uuids); |
c73eccf7 AJ |
150 | struct list_head *btrfs_get_fs_uuids(void) |
151 | { | |
152 | return &fs_uuids; | |
153 | } | |
8a4b83cc | 154 | |
2dfeca9b DS |
155 | /* |
156 | * alloc_fs_devices - allocate struct btrfs_fs_devices | |
157 | * @fsid: if not NULL, copy the uuid to fs_devices::fsid | |
158 | * | |
159 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). | |
160 | * The returned struct is not linked onto any lists and can be destroyed with | |
161 | * kfree() right away. | |
162 | */ | |
163 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) | |
2208a378 ID |
164 | { |
165 | struct btrfs_fs_devices *fs_devs; | |
166 | ||
78f2c9e6 | 167 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
2208a378 ID |
168 | if (!fs_devs) |
169 | return ERR_PTR(-ENOMEM); | |
170 | ||
171 | mutex_init(&fs_devs->device_list_mutex); | |
172 | ||
173 | INIT_LIST_HEAD(&fs_devs->devices); | |
935e5cc9 | 174 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
2208a378 ID |
175 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
176 | INIT_LIST_HEAD(&fs_devs->list); | |
2208a378 ID |
177 | if (fsid) |
178 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); | |
2208a378 ID |
179 | |
180 | return fs_devs; | |
181 | } | |
182 | ||
e4404d6e YZ |
183 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
184 | { | |
185 | struct btrfs_device *device; | |
186 | WARN_ON(fs_devices->opened); | |
187 | while (!list_empty(&fs_devices->devices)) { | |
188 | device = list_entry(fs_devices->devices.next, | |
189 | struct btrfs_device, dev_list); | |
190 | list_del(&device->dev_list); | |
606686ee | 191 | rcu_string_free(device->name); |
3065ae5b | 192 | bio_put(device->flush_bio); |
e4404d6e YZ |
193 | kfree(device); |
194 | } | |
195 | kfree(fs_devices); | |
196 | } | |
197 | ||
b8b8ff59 LC |
198 | static void btrfs_kobject_uevent(struct block_device *bdev, |
199 | enum kobject_action action) | |
200 | { | |
201 | int ret; | |
202 | ||
203 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); | |
204 | if (ret) | |
efe120a0 | 205 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
b8b8ff59 LC |
206 | action, |
207 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), | |
208 | &disk_to_dev(bdev->bd_disk)->kobj); | |
209 | } | |
210 | ||
143bede5 | 211 | void btrfs_cleanup_fs_uuids(void) |
8a4b83cc CM |
212 | { |
213 | struct btrfs_fs_devices *fs_devices; | |
8a4b83cc | 214 | |
2b82032c YZ |
215 | while (!list_empty(&fs_uuids)) { |
216 | fs_devices = list_entry(fs_uuids.next, | |
217 | struct btrfs_fs_devices, list); | |
218 | list_del(&fs_devices->list); | |
e4404d6e | 219 | free_fs_devices(fs_devices); |
8a4b83cc | 220 | } |
8a4b83cc CM |
221 | } |
222 | ||
12bd2fc0 ID |
223 | static struct btrfs_device *__alloc_device(void) |
224 | { | |
225 | struct btrfs_device *dev; | |
226 | ||
78f2c9e6 | 227 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
12bd2fc0 ID |
228 | if (!dev) |
229 | return ERR_PTR(-ENOMEM); | |
230 | ||
e0ae9994 DS |
231 | /* |
232 | * Preallocate a bio that's always going to be used for flushing device | |
233 | * barriers and matches the device lifespan | |
234 | */ | |
235 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); | |
236 | if (!dev->flush_bio) { | |
237 | kfree(dev); | |
238 | return ERR_PTR(-ENOMEM); | |
239 | } | |
e0ae9994 | 240 | |
12bd2fc0 ID |
241 | INIT_LIST_HEAD(&dev->dev_list); |
242 | INIT_LIST_HEAD(&dev->dev_alloc_list); | |
935e5cc9 | 243 | INIT_LIST_HEAD(&dev->resized_list); |
12bd2fc0 ID |
244 | |
245 | spin_lock_init(&dev->io_lock); | |
246 | ||
247 | spin_lock_init(&dev->reada_lock); | |
248 | atomic_set(&dev->reada_in_flight, 0); | |
addc3fa7 | 249 | atomic_set(&dev->dev_stats_ccnt, 0); |
546bed63 | 250 | btrfs_device_data_ordered_init(dev); |
9bcaaea7 | 251 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
d0164adc | 252 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
12bd2fc0 ID |
253 | |
254 | return dev; | |
255 | } | |
256 | ||
35c70103 DS |
257 | /* |
258 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or | |
259 | * return NULL. | |
260 | * | |
261 | * If devid and uuid are both specified, the match must be exact, otherwise | |
262 | * only devid is used. | |
263 | */ | |
264 | static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices, | |
265 | u64 devid, const u8 *uuid) | |
8a4b83cc | 266 | { |
35c70103 | 267 | struct list_head *head = &fs_devices->devices; |
8a4b83cc | 268 | struct btrfs_device *dev; |
8a4b83cc | 269 | |
c6e30871 | 270 | list_for_each_entry(dev, head, dev_list) { |
a443755f | 271 | if (dev->devid == devid && |
8f18cf13 | 272 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
8a4b83cc | 273 | return dev; |
a443755f | 274 | } |
8a4b83cc CM |
275 | } |
276 | return NULL; | |
277 | } | |
278 | ||
a1b32a59 | 279 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
8a4b83cc | 280 | { |
8a4b83cc CM |
281 | struct btrfs_fs_devices *fs_devices; |
282 | ||
c6e30871 | 283 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
8a4b83cc CM |
284 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
285 | return fs_devices; | |
286 | } | |
287 | return NULL; | |
288 | } | |
289 | ||
beaf8ab3 SB |
290 | static int |
291 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, | |
292 | int flush, struct block_device **bdev, | |
293 | struct buffer_head **bh) | |
294 | { | |
295 | int ret; | |
296 | ||
297 | *bdev = blkdev_get_by_path(device_path, flags, holder); | |
298 | ||
299 | if (IS_ERR(*bdev)) { | |
300 | ret = PTR_ERR(*bdev); | |
beaf8ab3 SB |
301 | goto error; |
302 | } | |
303 | ||
304 | if (flush) | |
305 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); | |
9f6d2510 | 306 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
beaf8ab3 SB |
307 | if (ret) { |
308 | blkdev_put(*bdev, flags); | |
309 | goto error; | |
310 | } | |
311 | invalidate_bdev(*bdev); | |
312 | *bh = btrfs_read_dev_super(*bdev); | |
92fc03fb AJ |
313 | if (IS_ERR(*bh)) { |
314 | ret = PTR_ERR(*bh); | |
beaf8ab3 SB |
315 | blkdev_put(*bdev, flags); |
316 | goto error; | |
317 | } | |
318 | ||
319 | return 0; | |
320 | ||
321 | error: | |
322 | *bdev = NULL; | |
323 | *bh = NULL; | |
324 | return ret; | |
325 | } | |
326 | ||
ffbd517d CM |
327 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
328 | struct bio *head, struct bio *tail) | |
329 | { | |
330 | ||
331 | struct bio *old_head; | |
332 | ||
333 | old_head = pending_bios->head; | |
334 | pending_bios->head = head; | |
335 | if (pending_bios->tail) | |
336 | tail->bi_next = old_head; | |
337 | else | |
338 | pending_bios->tail = tail; | |
339 | } | |
340 | ||
8b712842 CM |
341 | /* |
342 | * we try to collect pending bios for a device so we don't get a large | |
343 | * number of procs sending bios down to the same device. This greatly | |
344 | * improves the schedulers ability to collect and merge the bios. | |
345 | * | |
346 | * But, it also turns into a long list of bios to process and that is sure | |
347 | * to eventually make the worker thread block. The solution here is to | |
348 | * make some progress and then put this work struct back at the end of | |
349 | * the list if the block device is congested. This way, multiple devices | |
350 | * can make progress from a single worker thread. | |
351 | */ | |
143bede5 | 352 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
8b712842 | 353 | { |
0b246afa | 354 | struct btrfs_fs_info *fs_info = device->fs_info; |
8b712842 CM |
355 | struct bio *pending; |
356 | struct backing_dev_info *bdi; | |
ffbd517d | 357 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
358 | struct bio *tail; |
359 | struct bio *cur; | |
360 | int again = 0; | |
ffbd517d | 361 | unsigned long num_run; |
d644d8a1 | 362 | unsigned long batch_run = 0; |
b765ead5 | 363 | unsigned long last_waited = 0; |
d84275c9 | 364 | int force_reg = 0; |
0e588859 | 365 | int sync_pending = 0; |
211588ad CM |
366 | struct blk_plug plug; |
367 | ||
368 | /* | |
369 | * this function runs all the bios we've collected for | |
370 | * a particular device. We don't want to wander off to | |
371 | * another device without first sending all of these down. | |
372 | * So, setup a plug here and finish it off before we return | |
373 | */ | |
374 | blk_start_plug(&plug); | |
8b712842 | 375 | |
efa7c9f9 | 376 | bdi = device->bdev->bd_bdi; |
b64a2851 | 377 | |
8b712842 CM |
378 | loop: |
379 | spin_lock(&device->io_lock); | |
380 | ||
a6837051 | 381 | loop_lock: |
d84275c9 | 382 | num_run = 0; |
ffbd517d | 383 | |
8b712842 CM |
384 | /* take all the bios off the list at once and process them |
385 | * later on (without the lock held). But, remember the | |
386 | * tail and other pointers so the bios can be properly reinserted | |
387 | * into the list if we hit congestion | |
388 | */ | |
d84275c9 | 389 | if (!force_reg && device->pending_sync_bios.head) { |
ffbd517d | 390 | pending_bios = &device->pending_sync_bios; |
d84275c9 CM |
391 | force_reg = 1; |
392 | } else { | |
ffbd517d | 393 | pending_bios = &device->pending_bios; |
d84275c9 CM |
394 | force_reg = 0; |
395 | } | |
ffbd517d CM |
396 | |
397 | pending = pending_bios->head; | |
398 | tail = pending_bios->tail; | |
8b712842 | 399 | WARN_ON(pending && !tail); |
8b712842 CM |
400 | |
401 | /* | |
402 | * if pending was null this time around, no bios need processing | |
403 | * at all and we can stop. Otherwise it'll loop back up again | |
404 | * and do an additional check so no bios are missed. | |
405 | * | |
406 | * device->running_pending is used to synchronize with the | |
407 | * schedule_bio code. | |
408 | */ | |
ffbd517d CM |
409 | if (device->pending_sync_bios.head == NULL && |
410 | device->pending_bios.head == NULL) { | |
8b712842 CM |
411 | again = 0; |
412 | device->running_pending = 0; | |
ffbd517d CM |
413 | } else { |
414 | again = 1; | |
415 | device->running_pending = 1; | |
8b712842 | 416 | } |
ffbd517d CM |
417 | |
418 | pending_bios->head = NULL; | |
419 | pending_bios->tail = NULL; | |
420 | ||
8b712842 CM |
421 | spin_unlock(&device->io_lock); |
422 | ||
d397712b | 423 | while (pending) { |
ffbd517d CM |
424 | |
425 | rmb(); | |
d84275c9 CM |
426 | /* we want to work on both lists, but do more bios on the |
427 | * sync list than the regular list | |
428 | */ | |
429 | if ((num_run > 32 && | |
430 | pending_bios != &device->pending_sync_bios && | |
431 | device->pending_sync_bios.head) || | |
432 | (num_run > 64 && pending_bios == &device->pending_sync_bios && | |
433 | device->pending_bios.head)) { | |
ffbd517d CM |
434 | spin_lock(&device->io_lock); |
435 | requeue_list(pending_bios, pending, tail); | |
436 | goto loop_lock; | |
437 | } | |
438 | ||
8b712842 CM |
439 | cur = pending; |
440 | pending = pending->bi_next; | |
441 | cur->bi_next = NULL; | |
b64a2851 | 442 | |
dac56212 | 443 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
d644d8a1 | 444 | |
2ab1ba68 CM |
445 | /* |
446 | * if we're doing the sync list, record that our | |
447 | * plug has some sync requests on it | |
448 | * | |
449 | * If we're doing the regular list and there are | |
450 | * sync requests sitting around, unplug before | |
451 | * we add more | |
452 | */ | |
453 | if (pending_bios == &device->pending_sync_bios) { | |
454 | sync_pending = 1; | |
455 | } else if (sync_pending) { | |
456 | blk_finish_plug(&plug); | |
457 | blk_start_plug(&plug); | |
458 | sync_pending = 0; | |
459 | } | |
460 | ||
4e49ea4a | 461 | btrfsic_submit_bio(cur); |
5ff7ba3a CM |
462 | num_run++; |
463 | batch_run++; | |
853d8ec4 DS |
464 | |
465 | cond_resched(); | |
8b712842 CM |
466 | |
467 | /* | |
468 | * we made progress, there is more work to do and the bdi | |
469 | * is now congested. Back off and let other work structs | |
470 | * run instead | |
471 | */ | |
57fd5a5f | 472 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
5f2cc086 | 473 | fs_info->fs_devices->open_devices > 1) { |
b765ead5 | 474 | struct io_context *ioc; |
8b712842 | 475 | |
b765ead5 CM |
476 | ioc = current->io_context; |
477 | ||
478 | /* | |
479 | * the main goal here is that we don't want to | |
480 | * block if we're going to be able to submit | |
481 | * more requests without blocking. | |
482 | * | |
483 | * This code does two great things, it pokes into | |
484 | * the elevator code from a filesystem _and_ | |
485 | * it makes assumptions about how batching works. | |
486 | */ | |
487 | if (ioc && ioc->nr_batch_requests > 0 && | |
488 | time_before(jiffies, ioc->last_waited + HZ/50UL) && | |
489 | (last_waited == 0 || | |
490 | ioc->last_waited == last_waited)) { | |
491 | /* | |
492 | * we want to go through our batch of | |
493 | * requests and stop. So, we copy out | |
494 | * the ioc->last_waited time and test | |
495 | * against it before looping | |
496 | */ | |
497 | last_waited = ioc->last_waited; | |
853d8ec4 | 498 | cond_resched(); |
b765ead5 CM |
499 | continue; |
500 | } | |
8b712842 | 501 | spin_lock(&device->io_lock); |
ffbd517d | 502 | requeue_list(pending_bios, pending, tail); |
a6837051 | 503 | device->running_pending = 1; |
8b712842 CM |
504 | |
505 | spin_unlock(&device->io_lock); | |
a8c93d4e QW |
506 | btrfs_queue_work(fs_info->submit_workers, |
507 | &device->work); | |
8b712842 CM |
508 | goto done; |
509 | } | |
510 | } | |
ffbd517d | 511 | |
51684082 CM |
512 | cond_resched(); |
513 | if (again) | |
514 | goto loop; | |
515 | ||
516 | spin_lock(&device->io_lock); | |
517 | if (device->pending_bios.head || device->pending_sync_bios.head) | |
518 | goto loop_lock; | |
519 | spin_unlock(&device->io_lock); | |
520 | ||
8b712842 | 521 | done: |
211588ad | 522 | blk_finish_plug(&plug); |
8b712842 CM |
523 | } |
524 | ||
b2950863 | 525 | static void pending_bios_fn(struct btrfs_work *work) |
8b712842 CM |
526 | { |
527 | struct btrfs_device *device; | |
528 | ||
529 | device = container_of(work, struct btrfs_device, work); | |
530 | run_scheduled_bios(device); | |
531 | } | |
532 | ||
4fde46f0 | 533 | |
c9162bdf | 534 | static void btrfs_free_stale_device(struct btrfs_device *cur_dev) |
4fde46f0 AJ |
535 | { |
536 | struct btrfs_fs_devices *fs_devs; | |
537 | struct btrfs_device *dev; | |
538 | ||
539 | if (!cur_dev->name) | |
540 | return; | |
541 | ||
542 | list_for_each_entry(fs_devs, &fs_uuids, list) { | |
543 | int del = 1; | |
544 | ||
545 | if (fs_devs->opened) | |
546 | continue; | |
547 | if (fs_devs->seeding) | |
548 | continue; | |
549 | ||
550 | list_for_each_entry(dev, &fs_devs->devices, dev_list) { | |
551 | ||
552 | if (dev == cur_dev) | |
553 | continue; | |
554 | if (!dev->name) | |
555 | continue; | |
556 | ||
557 | /* | |
558 | * Todo: This won't be enough. What if the same device | |
559 | * comes back (with new uuid and) with its mapper path? | |
560 | * But for now, this does help as mostly an admin will | |
561 | * either use mapper or non mapper path throughout. | |
562 | */ | |
563 | rcu_read_lock(); | |
564 | del = strcmp(rcu_str_deref(dev->name), | |
565 | rcu_str_deref(cur_dev->name)); | |
566 | rcu_read_unlock(); | |
567 | if (!del) | |
568 | break; | |
569 | } | |
570 | ||
571 | if (!del) { | |
572 | /* delete the stale device */ | |
573 | if (fs_devs->num_devices == 1) { | |
574 | btrfs_sysfs_remove_fsid(fs_devs); | |
575 | list_del(&fs_devs->list); | |
576 | free_fs_devices(fs_devs); | |
6b2f4cac | 577 | break; |
4fde46f0 AJ |
578 | } else { |
579 | fs_devs->num_devices--; | |
580 | list_del(&dev->dev_list); | |
581 | rcu_string_free(dev->name); | |
3065ae5b | 582 | bio_put(dev->flush_bio); |
4fde46f0 AJ |
583 | kfree(dev); |
584 | } | |
585 | break; | |
586 | } | |
587 | } | |
588 | } | |
589 | ||
60999ca4 DS |
590 | /* |
591 | * Add new device to list of registered devices | |
592 | * | |
593 | * Returns: | |
594 | * 1 - first time device is seen | |
595 | * 0 - device already known | |
596 | * < 0 - error | |
597 | */ | |
a1b32a59 | 598 | static noinline int device_list_add(const char *path, |
8a4b83cc CM |
599 | struct btrfs_super_block *disk_super, |
600 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) | |
601 | { | |
602 | struct btrfs_device *device; | |
603 | struct btrfs_fs_devices *fs_devices; | |
606686ee | 604 | struct rcu_string *name; |
60999ca4 | 605 | int ret = 0; |
8a4b83cc CM |
606 | u64 found_transid = btrfs_super_generation(disk_super); |
607 | ||
608 | fs_devices = find_fsid(disk_super->fsid); | |
609 | if (!fs_devices) { | |
2208a378 ID |
610 | fs_devices = alloc_fs_devices(disk_super->fsid); |
611 | if (IS_ERR(fs_devices)) | |
612 | return PTR_ERR(fs_devices); | |
613 | ||
8a4b83cc | 614 | list_add(&fs_devices->list, &fs_uuids); |
2208a378 | 615 | |
8a4b83cc CM |
616 | device = NULL; |
617 | } else { | |
35c70103 DS |
618 | device = find_device(fs_devices, devid, |
619 | disk_super->dev_item.uuid); | |
8a4b83cc | 620 | } |
443f24fe | 621 | |
8a4b83cc | 622 | if (!device) { |
2b82032c YZ |
623 | if (fs_devices->opened) |
624 | return -EBUSY; | |
625 | ||
12bd2fc0 ID |
626 | device = btrfs_alloc_device(NULL, &devid, |
627 | disk_super->dev_item.uuid); | |
628 | if (IS_ERR(device)) { | |
8a4b83cc | 629 | /* we can safely leave the fs_devices entry around */ |
12bd2fc0 | 630 | return PTR_ERR(device); |
8a4b83cc | 631 | } |
606686ee JB |
632 | |
633 | name = rcu_string_strdup(path, GFP_NOFS); | |
634 | if (!name) { | |
3065ae5b | 635 | bio_put(device->flush_bio); |
8a4b83cc CM |
636 | kfree(device); |
637 | return -ENOMEM; | |
638 | } | |
606686ee | 639 | rcu_assign_pointer(device->name, name); |
90519d66 | 640 | |
e5e9a520 | 641 | mutex_lock(&fs_devices->device_list_mutex); |
1f78160c | 642 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
f7171750 | 643 | fs_devices->num_devices++; |
e5e9a520 CM |
644 | mutex_unlock(&fs_devices->device_list_mutex); |
645 | ||
60999ca4 | 646 | ret = 1; |
2b82032c | 647 | device->fs_devices = fs_devices; |
606686ee | 648 | } else if (!device->name || strcmp(device->name->str, path)) { |
b96de000 AJ |
649 | /* |
650 | * When FS is already mounted. | |
651 | * 1. If you are here and if the device->name is NULL that | |
652 | * means this device was missing at time of FS mount. | |
653 | * 2. If you are here and if the device->name is different | |
654 | * from 'path' that means either | |
655 | * a. The same device disappeared and reappeared with | |
656 | * different name. or | |
657 | * b. The missing-disk-which-was-replaced, has | |
658 | * reappeared now. | |
659 | * | |
660 | * We must allow 1 and 2a above. But 2b would be a spurious | |
661 | * and unintentional. | |
662 | * | |
663 | * Further in case of 1 and 2a above, the disk at 'path' | |
664 | * would have missed some transaction when it was away and | |
665 | * in case of 2a the stale bdev has to be updated as well. | |
666 | * 2b must not be allowed at all time. | |
667 | */ | |
668 | ||
669 | /* | |
0f23ae74 CM |
670 | * For now, we do allow update to btrfs_fs_device through the |
671 | * btrfs dev scan cli after FS has been mounted. We're still | |
672 | * tracking a problem where systems fail mount by subvolume id | |
673 | * when we reject replacement on a mounted FS. | |
b96de000 | 674 | */ |
0f23ae74 | 675 | if (!fs_devices->opened && found_transid < device->generation) { |
77bdae4d AJ |
676 | /* |
677 | * That is if the FS is _not_ mounted and if you | |
678 | * are here, that means there is more than one | |
679 | * disk with same uuid and devid.We keep the one | |
680 | * with larger generation number or the last-in if | |
681 | * generation are equal. | |
682 | */ | |
0f23ae74 | 683 | return -EEXIST; |
77bdae4d | 684 | } |
b96de000 | 685 | |
606686ee | 686 | name = rcu_string_strdup(path, GFP_NOFS); |
3a0524dc TH |
687 | if (!name) |
688 | return -ENOMEM; | |
606686ee JB |
689 | rcu_string_free(device->name); |
690 | rcu_assign_pointer(device->name, name); | |
cd02dca5 CM |
691 | if (device->missing) { |
692 | fs_devices->missing_devices--; | |
693 | device->missing = 0; | |
694 | } | |
8a4b83cc CM |
695 | } |
696 | ||
77bdae4d AJ |
697 | /* |
698 | * Unmount does not free the btrfs_device struct but would zero | |
699 | * generation along with most of the other members. So just update | |
700 | * it back. We need it to pick the disk with largest generation | |
701 | * (as above). | |
702 | */ | |
703 | if (!fs_devices->opened) | |
704 | device->generation = found_transid; | |
705 | ||
4fde46f0 AJ |
706 | /* |
707 | * if there is new btrfs on an already registered device, | |
708 | * then remove the stale device entry. | |
709 | */ | |
02feae3c AJ |
710 | if (ret > 0) |
711 | btrfs_free_stale_device(device); | |
4fde46f0 | 712 | |
8a4b83cc | 713 | *fs_devices_ret = fs_devices; |
60999ca4 DS |
714 | |
715 | return ret; | |
8a4b83cc CM |
716 | } |
717 | ||
e4404d6e YZ |
718 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
719 | { | |
720 | struct btrfs_fs_devices *fs_devices; | |
721 | struct btrfs_device *device; | |
722 | struct btrfs_device *orig_dev; | |
723 | ||
2208a378 ID |
724 | fs_devices = alloc_fs_devices(orig->fsid); |
725 | if (IS_ERR(fs_devices)) | |
726 | return fs_devices; | |
e4404d6e | 727 | |
adbbb863 | 728 | mutex_lock(&orig->device_list_mutex); |
02db0844 | 729 | fs_devices->total_devices = orig->total_devices; |
e4404d6e | 730 | |
46224705 | 731 | /* We have held the volume lock, it is safe to get the devices. */ |
e4404d6e | 732 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
606686ee JB |
733 | struct rcu_string *name; |
734 | ||
12bd2fc0 ID |
735 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
736 | orig_dev->uuid); | |
737 | if (IS_ERR(device)) | |
e4404d6e YZ |
738 | goto error; |
739 | ||
606686ee JB |
740 | /* |
741 | * This is ok to do without rcu read locked because we hold the | |
742 | * uuid mutex so nothing we touch in here is going to disappear. | |
743 | */ | |
e755f780 | 744 | if (orig_dev->name) { |
78f2c9e6 DS |
745 | name = rcu_string_strdup(orig_dev->name->str, |
746 | GFP_KERNEL); | |
e755f780 | 747 | if (!name) { |
3065ae5b | 748 | bio_put(device->flush_bio); |
e755f780 AJ |
749 | kfree(device); |
750 | goto error; | |
751 | } | |
752 | rcu_assign_pointer(device->name, name); | |
fd2696f3 | 753 | } |
e4404d6e | 754 | |
e4404d6e YZ |
755 | list_add(&device->dev_list, &fs_devices->devices); |
756 | device->fs_devices = fs_devices; | |
757 | fs_devices->num_devices++; | |
758 | } | |
adbbb863 | 759 | mutex_unlock(&orig->device_list_mutex); |
e4404d6e YZ |
760 | return fs_devices; |
761 | error: | |
adbbb863 | 762 | mutex_unlock(&orig->device_list_mutex); |
e4404d6e YZ |
763 | free_fs_devices(fs_devices); |
764 | return ERR_PTR(-ENOMEM); | |
765 | } | |
766 | ||
9eaed21e | 767 | void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step) |
dfe25020 | 768 | { |
c6e30871 | 769 | struct btrfs_device *device, *next; |
443f24fe | 770 | struct btrfs_device *latest_dev = NULL; |
a6b0d5c8 | 771 | |
dfe25020 CM |
772 | mutex_lock(&uuid_mutex); |
773 | again: | |
46224705 | 774 | /* This is the initialized path, it is safe to release the devices. */ |
c6e30871 | 775 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
a6b0d5c8 | 776 | if (device->in_fs_metadata) { |
63a212ab | 777 | if (!device->is_tgtdev_for_dev_replace && |
443f24fe MX |
778 | (!latest_dev || |
779 | device->generation > latest_dev->generation)) { | |
780 | latest_dev = device; | |
a6b0d5c8 | 781 | } |
2b82032c | 782 | continue; |
a6b0d5c8 | 783 | } |
2b82032c | 784 | |
8dabb742 SB |
785 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
786 | /* | |
787 | * In the first step, keep the device which has | |
788 | * the correct fsid and the devid that is used | |
789 | * for the dev_replace procedure. | |
790 | * In the second step, the dev_replace state is | |
791 | * read from the device tree and it is known | |
792 | * whether the procedure is really active or | |
793 | * not, which means whether this device is | |
794 | * used or whether it should be removed. | |
795 | */ | |
796 | if (step == 0 || device->is_tgtdev_for_dev_replace) { | |
797 | continue; | |
798 | } | |
799 | } | |
2b82032c | 800 | if (device->bdev) { |
d4d77629 | 801 | blkdev_put(device->bdev, device->mode); |
2b82032c YZ |
802 | device->bdev = NULL; |
803 | fs_devices->open_devices--; | |
804 | } | |
805 | if (device->writeable) { | |
806 | list_del_init(&device->dev_alloc_list); | |
807 | device->writeable = 0; | |
8dabb742 SB |
808 | if (!device->is_tgtdev_for_dev_replace) |
809 | fs_devices->rw_devices--; | |
2b82032c | 810 | } |
e4404d6e YZ |
811 | list_del_init(&device->dev_list); |
812 | fs_devices->num_devices--; | |
606686ee | 813 | rcu_string_free(device->name); |
3065ae5b | 814 | bio_put(device->flush_bio); |
e4404d6e | 815 | kfree(device); |
dfe25020 | 816 | } |
2b82032c YZ |
817 | |
818 | if (fs_devices->seed) { | |
819 | fs_devices = fs_devices->seed; | |
2b82032c YZ |
820 | goto again; |
821 | } | |
822 | ||
443f24fe | 823 | fs_devices->latest_bdev = latest_dev->bdev; |
a6b0d5c8 | 824 | |
dfe25020 | 825 | mutex_unlock(&uuid_mutex); |
dfe25020 | 826 | } |
a0af469b | 827 | |
1f78160c XG |
828 | static void __free_device(struct work_struct *work) |
829 | { | |
830 | struct btrfs_device *device; | |
831 | ||
832 | device = container_of(work, struct btrfs_device, rcu_work); | |
606686ee | 833 | rcu_string_free(device->name); |
e0ae9994 | 834 | bio_put(device->flush_bio); |
1f78160c XG |
835 | kfree(device); |
836 | } | |
837 | ||
838 | static void free_device(struct rcu_head *head) | |
839 | { | |
840 | struct btrfs_device *device; | |
841 | ||
842 | device = container_of(head, struct btrfs_device, rcu); | |
843 | ||
844 | INIT_WORK(&device->rcu_work, __free_device); | |
845 | schedule_work(&device->rcu_work); | |
846 | } | |
847 | ||
14238819 AJ |
848 | static void btrfs_close_bdev(struct btrfs_device *device) |
849 | { | |
850 | if (device->bdev && device->writeable) { | |
851 | sync_blockdev(device->bdev); | |
852 | invalidate_bdev(device->bdev); | |
853 | } | |
854 | ||
855 | if (device->bdev) | |
856 | blkdev_put(device->bdev, device->mode); | |
857 | } | |
858 | ||
0ccd0528 | 859 | static void btrfs_prepare_close_one_device(struct btrfs_device *device) |
f448341a AJ |
860 | { |
861 | struct btrfs_fs_devices *fs_devices = device->fs_devices; | |
862 | struct btrfs_device *new_device; | |
863 | struct rcu_string *name; | |
864 | ||
865 | if (device->bdev) | |
866 | fs_devices->open_devices--; | |
867 | ||
868 | if (device->writeable && | |
869 | device->devid != BTRFS_DEV_REPLACE_DEVID) { | |
870 | list_del_init(&device->dev_alloc_list); | |
871 | fs_devices->rw_devices--; | |
872 | } | |
873 | ||
874 | if (device->missing) | |
875 | fs_devices->missing_devices--; | |
876 | ||
877 | new_device = btrfs_alloc_device(NULL, &device->devid, | |
878 | device->uuid); | |
879 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ | |
880 | ||
881 | /* Safe because we are under uuid_mutex */ | |
882 | if (device->name) { | |
883 | name = rcu_string_strdup(device->name->str, GFP_NOFS); | |
884 | BUG_ON(!name); /* -ENOMEM */ | |
885 | rcu_assign_pointer(new_device->name, name); | |
886 | } | |
887 | ||
888 | list_replace_rcu(&device->dev_list, &new_device->dev_list); | |
889 | new_device->fs_devices = device->fs_devices; | |
f448341a AJ |
890 | } |
891 | ||
2b82032c | 892 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
8a4b83cc | 893 | { |
2037a093 | 894 | struct btrfs_device *device, *tmp; |
0ccd0528 AJ |
895 | struct list_head pending_put; |
896 | ||
897 | INIT_LIST_HEAD(&pending_put); | |
e4404d6e | 898 | |
2b82032c YZ |
899 | if (--fs_devices->opened > 0) |
900 | return 0; | |
8a4b83cc | 901 | |
c9513edb | 902 | mutex_lock(&fs_devices->device_list_mutex); |
2037a093 | 903 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
0ccd0528 AJ |
904 | btrfs_prepare_close_one_device(device); |
905 | list_add(&device->dev_list, &pending_put); | |
8a4b83cc | 906 | } |
c9513edb XG |
907 | mutex_unlock(&fs_devices->device_list_mutex); |
908 | ||
0ccd0528 AJ |
909 | /* |
910 | * btrfs_show_devname() is using the device_list_mutex, | |
911 | * sometimes call to blkdev_put() leads vfs calling | |
912 | * into this func. So do put outside of device_list_mutex, | |
913 | * as of now. | |
914 | */ | |
915 | while (!list_empty(&pending_put)) { | |
916 | device = list_first_entry(&pending_put, | |
917 | struct btrfs_device, dev_list); | |
918 | list_del(&device->dev_list); | |
919 | btrfs_close_bdev(device); | |
920 | call_rcu(&device->rcu, free_device); | |
921 | } | |
922 | ||
e4404d6e YZ |
923 | WARN_ON(fs_devices->open_devices); |
924 | WARN_ON(fs_devices->rw_devices); | |
2b82032c YZ |
925 | fs_devices->opened = 0; |
926 | fs_devices->seeding = 0; | |
2b82032c | 927 | |
8a4b83cc CM |
928 | return 0; |
929 | } | |
930 | ||
2b82032c YZ |
931 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
932 | { | |
e4404d6e | 933 | struct btrfs_fs_devices *seed_devices = NULL; |
2b82032c YZ |
934 | int ret; |
935 | ||
936 | mutex_lock(&uuid_mutex); | |
937 | ret = __btrfs_close_devices(fs_devices); | |
e4404d6e YZ |
938 | if (!fs_devices->opened) { |
939 | seed_devices = fs_devices->seed; | |
940 | fs_devices->seed = NULL; | |
941 | } | |
2b82032c | 942 | mutex_unlock(&uuid_mutex); |
e4404d6e YZ |
943 | |
944 | while (seed_devices) { | |
945 | fs_devices = seed_devices; | |
946 | seed_devices = fs_devices->seed; | |
947 | __btrfs_close_devices(fs_devices); | |
948 | free_fs_devices(fs_devices); | |
949 | } | |
bc178622 ES |
950 | /* |
951 | * Wait for rcu kworkers under __btrfs_close_devices | |
952 | * to finish all blkdev_puts so device is really | |
953 | * free when umount is done. | |
954 | */ | |
955 | rcu_barrier(); | |
2b82032c YZ |
956 | return ret; |
957 | } | |
958 | ||
e4404d6e YZ |
959 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
960 | fmode_t flags, void *holder) | |
8a4b83cc | 961 | { |
d5e2003c | 962 | struct request_queue *q; |
8a4b83cc CM |
963 | struct block_device *bdev; |
964 | struct list_head *head = &fs_devices->devices; | |
8a4b83cc | 965 | struct btrfs_device *device; |
443f24fe | 966 | struct btrfs_device *latest_dev = NULL; |
a0af469b CM |
967 | struct buffer_head *bh; |
968 | struct btrfs_super_block *disk_super; | |
a0af469b | 969 | u64 devid; |
2b82032c | 970 | int seeding = 1; |
a0af469b | 971 | int ret = 0; |
8a4b83cc | 972 | |
d4d77629 TH |
973 | flags |= FMODE_EXCL; |
974 | ||
c6e30871 | 975 | list_for_each_entry(device, head, dev_list) { |
c1c4d91c CM |
976 | if (device->bdev) |
977 | continue; | |
dfe25020 CM |
978 | if (!device->name) |
979 | continue; | |
980 | ||
f63e0cca ES |
981 | /* Just open everything we can; ignore failures here */ |
982 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, | |
983 | &bdev, &bh)) | |
beaf8ab3 | 984 | continue; |
a0af469b CM |
985 | |
986 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 987 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
a0af469b CM |
988 | if (devid != device->devid) |
989 | goto error_brelse; | |
990 | ||
2b82032c YZ |
991 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
992 | BTRFS_UUID_SIZE)) | |
993 | goto error_brelse; | |
994 | ||
995 | device->generation = btrfs_super_generation(disk_super); | |
443f24fe MX |
996 | if (!latest_dev || |
997 | device->generation > latest_dev->generation) | |
998 | latest_dev = device; | |
a0af469b | 999 | |
2b82032c YZ |
1000 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
1001 | device->writeable = 0; | |
1002 | } else { | |
1003 | device->writeable = !bdev_read_only(bdev); | |
1004 | seeding = 0; | |
1005 | } | |
1006 | ||
d5e2003c | 1007 | q = bdev_get_queue(bdev); |
90180da4 | 1008 | if (blk_queue_discard(q)) |
d5e2003c | 1009 | device->can_discard = 1; |
e884f4f0 AJ |
1010 | if (!blk_queue_nonrot(q)) |
1011 | fs_devices->rotating = 1; | |
d5e2003c | 1012 | |
8a4b83cc | 1013 | device->bdev = bdev; |
dfe25020 | 1014 | device->in_fs_metadata = 0; |
15916de8 CM |
1015 | device->mode = flags; |
1016 | ||
a0af469b | 1017 | fs_devices->open_devices++; |
55e50e45 ID |
1018 | if (device->writeable && |
1019 | device->devid != BTRFS_DEV_REPLACE_DEVID) { | |
2b82032c YZ |
1020 | fs_devices->rw_devices++; |
1021 | list_add(&device->dev_alloc_list, | |
1022 | &fs_devices->alloc_list); | |
1023 | } | |
4f6c9328 | 1024 | brelse(bh); |
a0af469b | 1025 | continue; |
a061fc8d | 1026 | |
a0af469b CM |
1027 | error_brelse: |
1028 | brelse(bh); | |
d4d77629 | 1029 | blkdev_put(bdev, flags); |
a0af469b | 1030 | continue; |
8a4b83cc | 1031 | } |
a0af469b | 1032 | if (fs_devices->open_devices == 0) { |
20bcd649 | 1033 | ret = -EINVAL; |
a0af469b CM |
1034 | goto out; |
1035 | } | |
2b82032c YZ |
1036 | fs_devices->seeding = seeding; |
1037 | fs_devices->opened = 1; | |
443f24fe | 1038 | fs_devices->latest_bdev = latest_dev->bdev; |
2b82032c | 1039 | fs_devices->total_rw_bytes = 0; |
a0af469b | 1040 | out: |
2b82032c YZ |
1041 | return ret; |
1042 | } | |
1043 | ||
1044 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, | |
97288f2c | 1045 | fmode_t flags, void *holder) |
2b82032c YZ |
1046 | { |
1047 | int ret; | |
1048 | ||
1049 | mutex_lock(&uuid_mutex); | |
1050 | if (fs_devices->opened) { | |
e4404d6e YZ |
1051 | fs_devices->opened++; |
1052 | ret = 0; | |
2b82032c | 1053 | } else { |
15916de8 | 1054 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
2b82032c | 1055 | } |
8a4b83cc | 1056 | mutex_unlock(&uuid_mutex); |
8a4b83cc CM |
1057 | return ret; |
1058 | } | |
1059 | ||
c9162bdf | 1060 | static void btrfs_release_disk_super(struct page *page) |
6cf86a00 AJ |
1061 | { |
1062 | kunmap(page); | |
1063 | put_page(page); | |
1064 | } | |
1065 | ||
c9162bdf OS |
1066 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
1067 | struct page **page, | |
1068 | struct btrfs_super_block **disk_super) | |
6cf86a00 AJ |
1069 | { |
1070 | void *p; | |
1071 | pgoff_t index; | |
1072 | ||
1073 | /* make sure our super fits in the device */ | |
1074 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) | |
1075 | return 1; | |
1076 | ||
1077 | /* make sure our super fits in the page */ | |
1078 | if (sizeof(**disk_super) > PAGE_SIZE) | |
1079 | return 1; | |
1080 | ||
1081 | /* make sure our super doesn't straddle pages on disk */ | |
1082 | index = bytenr >> PAGE_SHIFT; | |
1083 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) | |
1084 | return 1; | |
1085 | ||
1086 | /* pull in the page with our super */ | |
1087 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, | |
1088 | index, GFP_KERNEL); | |
1089 | ||
1090 | if (IS_ERR_OR_NULL(*page)) | |
1091 | return 1; | |
1092 | ||
1093 | p = kmap(*page); | |
1094 | ||
1095 | /* align our pointer to the offset of the super block */ | |
1096 | *disk_super = p + (bytenr & ~PAGE_MASK); | |
1097 | ||
1098 | if (btrfs_super_bytenr(*disk_super) != bytenr || | |
1099 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { | |
1100 | btrfs_release_disk_super(*page); | |
1101 | return 1; | |
1102 | } | |
1103 | ||
1104 | if ((*disk_super)->label[0] && | |
1105 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) | |
1106 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
6f60cbd3 DS |
1111 | /* |
1112 | * Look for a btrfs signature on a device. This may be called out of the mount path | |
1113 | * and we are not allowed to call set_blocksize during the scan. The superblock | |
1114 | * is read via pagecache | |
1115 | */ | |
97288f2c | 1116 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
8a4b83cc CM |
1117 | struct btrfs_fs_devices **fs_devices_ret) |
1118 | { | |
1119 | struct btrfs_super_block *disk_super; | |
1120 | struct block_device *bdev; | |
6f60cbd3 | 1121 | struct page *page; |
6f60cbd3 | 1122 | int ret = -EINVAL; |
8a4b83cc | 1123 | u64 devid; |
f2984462 | 1124 | u64 transid; |
02db0844 | 1125 | u64 total_devices; |
6f60cbd3 | 1126 | u64 bytenr; |
8a4b83cc | 1127 | |
6f60cbd3 DS |
1128 | /* |
1129 | * we would like to check all the supers, but that would make | |
1130 | * a btrfs mount succeed after a mkfs from a different FS. | |
1131 | * So, we need to add a special mount option to scan for | |
1132 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead | |
1133 | */ | |
1134 | bytenr = btrfs_sb_offset(0); | |
d4d77629 | 1135 | flags |= FMODE_EXCL; |
10f6327b | 1136 | mutex_lock(&uuid_mutex); |
6f60cbd3 DS |
1137 | |
1138 | bdev = blkdev_get_by_path(path, flags, holder); | |
6f60cbd3 DS |
1139 | if (IS_ERR(bdev)) { |
1140 | ret = PTR_ERR(bdev); | |
beaf8ab3 | 1141 | goto error; |
6f60cbd3 DS |
1142 | } |
1143 | ||
6cf86a00 | 1144 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) |
6f60cbd3 DS |
1145 | goto error_bdev_put; |
1146 | ||
a343832f | 1147 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
f2984462 | 1148 | transid = btrfs_super_generation(disk_super); |
02db0844 | 1149 | total_devices = btrfs_super_num_devices(disk_super); |
6f60cbd3 | 1150 | |
8a4b83cc | 1151 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
60999ca4 DS |
1152 | if (ret > 0) { |
1153 | if (disk_super->label[0]) { | |
62e85577 | 1154 | pr_info("BTRFS: device label %s ", disk_super->label); |
60999ca4 | 1155 | } else { |
62e85577 | 1156 | pr_info("BTRFS: device fsid %pU ", disk_super->fsid); |
60999ca4 DS |
1157 | } |
1158 | ||
62e85577 | 1159 | pr_cont("devid %llu transid %llu %s\n", devid, transid, path); |
60999ca4 DS |
1160 | ret = 0; |
1161 | } | |
02db0844 JB |
1162 | if (!ret && fs_devices_ret) |
1163 | (*fs_devices_ret)->total_devices = total_devices; | |
6f60cbd3 | 1164 | |
6cf86a00 | 1165 | btrfs_release_disk_super(page); |
6f60cbd3 DS |
1166 | |
1167 | error_bdev_put: | |
d4d77629 | 1168 | blkdev_put(bdev, flags); |
8a4b83cc | 1169 | error: |
beaf8ab3 | 1170 | mutex_unlock(&uuid_mutex); |
8a4b83cc CM |
1171 | return ret; |
1172 | } | |
0b86a832 | 1173 | |
6d07bcec MX |
1174 | /* helper to account the used device space in the range */ |
1175 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, | |
1176 | u64 end, u64 *length) | |
1177 | { | |
1178 | struct btrfs_key key; | |
fb456252 | 1179 | struct btrfs_root *root = device->fs_info->dev_root; |
6d07bcec MX |
1180 | struct btrfs_dev_extent *dev_extent; |
1181 | struct btrfs_path *path; | |
1182 | u64 extent_end; | |
1183 | int ret; | |
1184 | int slot; | |
1185 | struct extent_buffer *l; | |
1186 | ||
1187 | *length = 0; | |
1188 | ||
63a212ab | 1189 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
6d07bcec MX |
1190 | return 0; |
1191 | ||
1192 | path = btrfs_alloc_path(); | |
1193 | if (!path) | |
1194 | return -ENOMEM; | |
e4058b54 | 1195 | path->reada = READA_FORWARD; |
6d07bcec MX |
1196 | |
1197 | key.objectid = device->devid; | |
1198 | key.offset = start; | |
1199 | key.type = BTRFS_DEV_EXTENT_KEY; | |
1200 | ||
1201 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1202 | if (ret < 0) | |
1203 | goto out; | |
1204 | if (ret > 0) { | |
1205 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
1206 | if (ret < 0) | |
1207 | goto out; | |
1208 | } | |
1209 | ||
1210 | while (1) { | |
1211 | l = path->nodes[0]; | |
1212 | slot = path->slots[0]; | |
1213 | if (slot >= btrfs_header_nritems(l)) { | |
1214 | ret = btrfs_next_leaf(root, path); | |
1215 | if (ret == 0) | |
1216 | continue; | |
1217 | if (ret < 0) | |
1218 | goto out; | |
1219 | ||
1220 | break; | |
1221 | } | |
1222 | btrfs_item_key_to_cpu(l, &key, slot); | |
1223 | ||
1224 | if (key.objectid < device->devid) | |
1225 | goto next; | |
1226 | ||
1227 | if (key.objectid > device->devid) | |
1228 | break; | |
1229 | ||
962a298f | 1230 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
6d07bcec MX |
1231 | goto next; |
1232 | ||
1233 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
1234 | extent_end = key.offset + btrfs_dev_extent_length(l, | |
1235 | dev_extent); | |
1236 | if (key.offset <= start && extent_end > end) { | |
1237 | *length = end - start + 1; | |
1238 | break; | |
1239 | } else if (key.offset <= start && extent_end > start) | |
1240 | *length += extent_end - start; | |
1241 | else if (key.offset > start && extent_end <= end) | |
1242 | *length += extent_end - key.offset; | |
1243 | else if (key.offset > start && key.offset <= end) { | |
1244 | *length += end - key.offset + 1; | |
1245 | break; | |
1246 | } else if (key.offset > end) | |
1247 | break; | |
1248 | ||
1249 | next: | |
1250 | path->slots[0]++; | |
1251 | } | |
1252 | ret = 0; | |
1253 | out: | |
1254 | btrfs_free_path(path); | |
1255 | return ret; | |
1256 | } | |
1257 | ||
499f377f | 1258 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
6df9a95e JB |
1259 | struct btrfs_device *device, |
1260 | u64 *start, u64 len) | |
1261 | { | |
fb456252 | 1262 | struct btrfs_fs_info *fs_info = device->fs_info; |
6df9a95e | 1263 | struct extent_map *em; |
499f377f | 1264 | struct list_head *search_list = &fs_info->pinned_chunks; |
6df9a95e | 1265 | int ret = 0; |
1b984508 | 1266 | u64 physical_start = *start; |
6df9a95e | 1267 | |
499f377f JM |
1268 | if (transaction) |
1269 | search_list = &transaction->pending_chunks; | |
04216820 FM |
1270 | again: |
1271 | list_for_each_entry(em, search_list, list) { | |
6df9a95e JB |
1272 | struct map_lookup *map; |
1273 | int i; | |
1274 | ||
95617d69 | 1275 | map = em->map_lookup; |
6df9a95e | 1276 | for (i = 0; i < map->num_stripes; i++) { |
c152b63e FM |
1277 | u64 end; |
1278 | ||
6df9a95e JB |
1279 | if (map->stripes[i].dev != device) |
1280 | continue; | |
1b984508 | 1281 | if (map->stripes[i].physical >= physical_start + len || |
6df9a95e | 1282 | map->stripes[i].physical + em->orig_block_len <= |
1b984508 | 1283 | physical_start) |
6df9a95e | 1284 | continue; |
c152b63e FM |
1285 | /* |
1286 | * Make sure that while processing the pinned list we do | |
1287 | * not override our *start with a lower value, because | |
1288 | * we can have pinned chunks that fall within this | |
1289 | * device hole and that have lower physical addresses | |
1290 | * than the pending chunks we processed before. If we | |
1291 | * do not take this special care we can end up getting | |
1292 | * 2 pending chunks that start at the same physical | |
1293 | * device offsets because the end offset of a pinned | |
1294 | * chunk can be equal to the start offset of some | |
1295 | * pending chunk. | |
1296 | */ | |
1297 | end = map->stripes[i].physical + em->orig_block_len; | |
1298 | if (end > *start) { | |
1299 | *start = end; | |
1300 | ret = 1; | |
1301 | } | |
6df9a95e JB |
1302 | } |
1303 | } | |
499f377f JM |
1304 | if (search_list != &fs_info->pinned_chunks) { |
1305 | search_list = &fs_info->pinned_chunks; | |
04216820 FM |
1306 | goto again; |
1307 | } | |
6df9a95e JB |
1308 | |
1309 | return ret; | |
1310 | } | |
1311 | ||
1312 | ||
0b86a832 | 1313 | /* |
499f377f JM |
1314 | * find_free_dev_extent_start - find free space in the specified device |
1315 | * @device: the device which we search the free space in | |
1316 | * @num_bytes: the size of the free space that we need | |
1317 | * @search_start: the position from which to begin the search | |
1318 | * @start: store the start of the free space. | |
1319 | * @len: the size of the free space. that we find, or the size | |
1320 | * of the max free space if we don't find suitable free space | |
7bfc837d | 1321 | * |
0b86a832 CM |
1322 | * this uses a pretty simple search, the expectation is that it is |
1323 | * called very infrequently and that a given device has a small number | |
1324 | * of extents | |
7bfc837d MX |
1325 | * |
1326 | * @start is used to store the start of the free space if we find. But if we | |
1327 | * don't find suitable free space, it will be used to store the start position | |
1328 | * of the max free space. | |
1329 | * | |
1330 | * @len is used to store the size of the free space that we find. | |
1331 | * But if we don't find suitable free space, it is used to store the size of | |
1332 | * the max free space. | |
0b86a832 | 1333 | */ |
499f377f JM |
1334 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
1335 | struct btrfs_device *device, u64 num_bytes, | |
1336 | u64 search_start, u64 *start, u64 *len) | |
0b86a832 | 1337 | { |
0b246afa JM |
1338 | struct btrfs_fs_info *fs_info = device->fs_info; |
1339 | struct btrfs_root *root = fs_info->dev_root; | |
0b86a832 | 1340 | struct btrfs_key key; |
7bfc837d | 1341 | struct btrfs_dev_extent *dev_extent; |
2b82032c | 1342 | struct btrfs_path *path; |
7bfc837d MX |
1343 | u64 hole_size; |
1344 | u64 max_hole_start; | |
1345 | u64 max_hole_size; | |
1346 | u64 extent_end; | |
0b86a832 CM |
1347 | u64 search_end = device->total_bytes; |
1348 | int ret; | |
7bfc837d | 1349 | int slot; |
0b86a832 | 1350 | struct extent_buffer *l; |
8cdc7c5b FM |
1351 | |
1352 | /* | |
1353 | * We don't want to overwrite the superblock on the drive nor any area | |
1354 | * used by the boot loader (grub for example), so we make sure to start | |
1355 | * at an offset of at least 1MB. | |
1356 | */ | |
0d0c71b3 | 1357 | search_start = max_t(u64, search_start, SZ_1M); |
0b86a832 | 1358 | |
6df9a95e JB |
1359 | path = btrfs_alloc_path(); |
1360 | if (!path) | |
1361 | return -ENOMEM; | |
f2ab7618 | 1362 | |
7bfc837d MX |
1363 | max_hole_start = search_start; |
1364 | max_hole_size = 0; | |
1365 | ||
f2ab7618 | 1366 | again: |
63a212ab | 1367 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
7bfc837d | 1368 | ret = -ENOSPC; |
6df9a95e | 1369 | goto out; |
7bfc837d MX |
1370 | } |
1371 | ||
e4058b54 | 1372 | path->reada = READA_FORWARD; |
6df9a95e JB |
1373 | path->search_commit_root = 1; |
1374 | path->skip_locking = 1; | |
7bfc837d | 1375 | |
0b86a832 CM |
1376 | key.objectid = device->devid; |
1377 | key.offset = search_start; | |
1378 | key.type = BTRFS_DEV_EXTENT_KEY; | |
7bfc837d | 1379 | |
125ccb0a | 1380 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
0b86a832 | 1381 | if (ret < 0) |
7bfc837d | 1382 | goto out; |
1fcbac58 YZ |
1383 | if (ret > 0) { |
1384 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
1385 | if (ret < 0) | |
7bfc837d | 1386 | goto out; |
1fcbac58 | 1387 | } |
7bfc837d | 1388 | |
0b86a832 CM |
1389 | while (1) { |
1390 | l = path->nodes[0]; | |
1391 | slot = path->slots[0]; | |
1392 | if (slot >= btrfs_header_nritems(l)) { | |
1393 | ret = btrfs_next_leaf(root, path); | |
1394 | if (ret == 0) | |
1395 | continue; | |
1396 | if (ret < 0) | |
7bfc837d MX |
1397 | goto out; |
1398 | ||
1399 | break; | |
0b86a832 CM |
1400 | } |
1401 | btrfs_item_key_to_cpu(l, &key, slot); | |
1402 | ||
1403 | if (key.objectid < device->devid) | |
1404 | goto next; | |
1405 | ||
1406 | if (key.objectid > device->devid) | |
7bfc837d | 1407 | break; |
0b86a832 | 1408 | |
962a298f | 1409 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
7bfc837d | 1410 | goto next; |
9779b72f | 1411 | |
7bfc837d MX |
1412 | if (key.offset > search_start) { |
1413 | hole_size = key.offset - search_start; | |
9779b72f | 1414 | |
6df9a95e JB |
1415 | /* |
1416 | * Have to check before we set max_hole_start, otherwise | |
1417 | * we could end up sending back this offset anyway. | |
1418 | */ | |
499f377f | 1419 | if (contains_pending_extent(transaction, device, |
6df9a95e | 1420 | &search_start, |
1b984508 FL |
1421 | hole_size)) { |
1422 | if (key.offset >= search_start) { | |
1423 | hole_size = key.offset - search_start; | |
1424 | } else { | |
1425 | WARN_ON_ONCE(1); | |
1426 | hole_size = 0; | |
1427 | } | |
1428 | } | |
6df9a95e | 1429 | |
7bfc837d MX |
1430 | if (hole_size > max_hole_size) { |
1431 | max_hole_start = search_start; | |
1432 | max_hole_size = hole_size; | |
1433 | } | |
9779b72f | 1434 | |
7bfc837d MX |
1435 | /* |
1436 | * If this free space is greater than which we need, | |
1437 | * it must be the max free space that we have found | |
1438 | * until now, so max_hole_start must point to the start | |
1439 | * of this free space and the length of this free space | |
1440 | * is stored in max_hole_size. Thus, we return | |
1441 | * max_hole_start and max_hole_size and go back to the | |
1442 | * caller. | |
1443 | */ | |
1444 | if (hole_size >= num_bytes) { | |
1445 | ret = 0; | |
1446 | goto out; | |
0b86a832 CM |
1447 | } |
1448 | } | |
0b86a832 | 1449 | |
0b86a832 | 1450 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
7bfc837d MX |
1451 | extent_end = key.offset + btrfs_dev_extent_length(l, |
1452 | dev_extent); | |
1453 | if (extent_end > search_start) | |
1454 | search_start = extent_end; | |
0b86a832 CM |
1455 | next: |
1456 | path->slots[0]++; | |
1457 | cond_resched(); | |
1458 | } | |
0b86a832 | 1459 | |
38c01b96 | 1460 | /* |
1461 | * At this point, search_start should be the end of | |
1462 | * allocated dev extents, and when shrinking the device, | |
1463 | * search_end may be smaller than search_start. | |
1464 | */ | |
f2ab7618 | 1465 | if (search_end > search_start) { |
38c01b96 | 1466 | hole_size = search_end - search_start; |
1467 | ||
499f377f | 1468 | if (contains_pending_extent(transaction, device, &search_start, |
f2ab7618 ZL |
1469 | hole_size)) { |
1470 | btrfs_release_path(path); | |
1471 | goto again; | |
1472 | } | |
0b86a832 | 1473 | |
f2ab7618 ZL |
1474 | if (hole_size > max_hole_size) { |
1475 | max_hole_start = search_start; | |
1476 | max_hole_size = hole_size; | |
1477 | } | |
6df9a95e JB |
1478 | } |
1479 | ||
7bfc837d | 1480 | /* See above. */ |
f2ab7618 | 1481 | if (max_hole_size < num_bytes) |
7bfc837d MX |
1482 | ret = -ENOSPC; |
1483 | else | |
1484 | ret = 0; | |
1485 | ||
1486 | out: | |
2b82032c | 1487 | btrfs_free_path(path); |
7bfc837d | 1488 | *start = max_hole_start; |
b2117a39 | 1489 | if (len) |
7bfc837d | 1490 | *len = max_hole_size; |
0b86a832 CM |
1491 | return ret; |
1492 | } | |
1493 | ||
499f377f JM |
1494 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
1495 | struct btrfs_device *device, u64 num_bytes, | |
1496 | u64 *start, u64 *len) | |
1497 | { | |
499f377f | 1498 | /* FIXME use last free of some kind */ |
499f377f | 1499 | return find_free_dev_extent_start(trans->transaction, device, |
8cdc7c5b | 1500 | num_bytes, 0, start, len); |
499f377f JM |
1501 | } |
1502 | ||
b2950863 | 1503 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
8f18cf13 | 1504 | struct btrfs_device *device, |
2196d6e8 | 1505 | u64 start, u64 *dev_extent_len) |
8f18cf13 | 1506 | { |
0b246afa JM |
1507 | struct btrfs_fs_info *fs_info = device->fs_info; |
1508 | struct btrfs_root *root = fs_info->dev_root; | |
8f18cf13 CM |
1509 | int ret; |
1510 | struct btrfs_path *path; | |
8f18cf13 | 1511 | struct btrfs_key key; |
a061fc8d CM |
1512 | struct btrfs_key found_key; |
1513 | struct extent_buffer *leaf = NULL; | |
1514 | struct btrfs_dev_extent *extent = NULL; | |
8f18cf13 CM |
1515 | |
1516 | path = btrfs_alloc_path(); | |
1517 | if (!path) | |
1518 | return -ENOMEM; | |
1519 | ||
1520 | key.objectid = device->devid; | |
1521 | key.offset = start; | |
1522 | key.type = BTRFS_DEV_EXTENT_KEY; | |
924cd8fb | 1523 | again: |
8f18cf13 | 1524 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
a061fc8d CM |
1525 | if (ret > 0) { |
1526 | ret = btrfs_previous_item(root, path, key.objectid, | |
1527 | BTRFS_DEV_EXTENT_KEY); | |
b0b802d7 TI |
1528 | if (ret) |
1529 | goto out; | |
a061fc8d CM |
1530 | leaf = path->nodes[0]; |
1531 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1532 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1533 | struct btrfs_dev_extent); | |
1534 | BUG_ON(found_key.offset > start || found_key.offset + | |
1535 | btrfs_dev_extent_length(leaf, extent) < start); | |
924cd8fb MX |
1536 | key = found_key; |
1537 | btrfs_release_path(path); | |
1538 | goto again; | |
a061fc8d CM |
1539 | } else if (ret == 0) { |
1540 | leaf = path->nodes[0]; | |
1541 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1542 | struct btrfs_dev_extent); | |
79787eaa | 1543 | } else { |
0b246afa | 1544 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
79787eaa | 1545 | goto out; |
a061fc8d | 1546 | } |
8f18cf13 | 1547 | |
2196d6e8 MX |
1548 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
1549 | ||
8f18cf13 | 1550 | ret = btrfs_del_item(trans, root, path); |
79787eaa | 1551 | if (ret) { |
0b246afa JM |
1552 | btrfs_handle_fs_error(fs_info, ret, |
1553 | "Failed to remove dev extent item"); | |
13212b54 | 1554 | } else { |
3204d33c | 1555 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
79787eaa | 1556 | } |
b0b802d7 | 1557 | out: |
8f18cf13 CM |
1558 | btrfs_free_path(path); |
1559 | return ret; | |
1560 | } | |
1561 | ||
48a3b636 ES |
1562 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
1563 | struct btrfs_device *device, | |
48a3b636 | 1564 | u64 chunk_offset, u64 start, u64 num_bytes) |
0b86a832 CM |
1565 | { |
1566 | int ret; | |
1567 | struct btrfs_path *path; | |
0b246afa JM |
1568 | struct btrfs_fs_info *fs_info = device->fs_info; |
1569 | struct btrfs_root *root = fs_info->dev_root; | |
0b86a832 CM |
1570 | struct btrfs_dev_extent *extent; |
1571 | struct extent_buffer *leaf; | |
1572 | struct btrfs_key key; | |
1573 | ||
dfe25020 | 1574 | WARN_ON(!device->in_fs_metadata); |
63a212ab | 1575 | WARN_ON(device->is_tgtdev_for_dev_replace); |
0b86a832 CM |
1576 | path = btrfs_alloc_path(); |
1577 | if (!path) | |
1578 | return -ENOMEM; | |
1579 | ||
0b86a832 | 1580 | key.objectid = device->devid; |
2b82032c | 1581 | key.offset = start; |
0b86a832 CM |
1582 | key.type = BTRFS_DEV_EXTENT_KEY; |
1583 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1584 | sizeof(*extent)); | |
2cdcecbc MF |
1585 | if (ret) |
1586 | goto out; | |
0b86a832 CM |
1587 | |
1588 | leaf = path->nodes[0]; | |
1589 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1590 | struct btrfs_dev_extent); | |
b5d9071c NB |
1591 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
1592 | BTRFS_CHUNK_TREE_OBJECTID); | |
0ca00afb NB |
1593 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
1594 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); | |
e17cade2 CM |
1595 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
1596 | ||
0b86a832 CM |
1597 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
1598 | btrfs_mark_buffer_dirty(leaf); | |
2cdcecbc | 1599 | out: |
0b86a832 CM |
1600 | btrfs_free_path(path); |
1601 | return ret; | |
1602 | } | |
1603 | ||
6df9a95e | 1604 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
0b86a832 | 1605 | { |
6df9a95e JB |
1606 | struct extent_map_tree *em_tree; |
1607 | struct extent_map *em; | |
1608 | struct rb_node *n; | |
1609 | u64 ret = 0; | |
0b86a832 | 1610 | |
6df9a95e JB |
1611 | em_tree = &fs_info->mapping_tree.map_tree; |
1612 | read_lock(&em_tree->lock); | |
1613 | n = rb_last(&em_tree->map); | |
1614 | if (n) { | |
1615 | em = rb_entry(n, struct extent_map, rb_node); | |
1616 | ret = em->start + em->len; | |
0b86a832 | 1617 | } |
6df9a95e JB |
1618 | read_unlock(&em_tree->lock); |
1619 | ||
0b86a832 CM |
1620 | return ret; |
1621 | } | |
1622 | ||
53f10659 ID |
1623 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
1624 | u64 *devid_ret) | |
0b86a832 CM |
1625 | { |
1626 | int ret; | |
1627 | struct btrfs_key key; | |
1628 | struct btrfs_key found_key; | |
2b82032c YZ |
1629 | struct btrfs_path *path; |
1630 | ||
2b82032c YZ |
1631 | path = btrfs_alloc_path(); |
1632 | if (!path) | |
1633 | return -ENOMEM; | |
0b86a832 CM |
1634 | |
1635 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1636 | key.type = BTRFS_DEV_ITEM_KEY; | |
1637 | key.offset = (u64)-1; | |
1638 | ||
53f10659 | 1639 | ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
0b86a832 CM |
1640 | if (ret < 0) |
1641 | goto error; | |
1642 | ||
79787eaa | 1643 | BUG_ON(ret == 0); /* Corruption */ |
0b86a832 | 1644 | |
53f10659 ID |
1645 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
1646 | BTRFS_DEV_ITEMS_OBJECTID, | |
0b86a832 CM |
1647 | BTRFS_DEV_ITEM_KEY); |
1648 | if (ret) { | |
53f10659 | 1649 | *devid_ret = 1; |
0b86a832 CM |
1650 | } else { |
1651 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1652 | path->slots[0]); | |
53f10659 | 1653 | *devid_ret = found_key.offset + 1; |
0b86a832 CM |
1654 | } |
1655 | ret = 0; | |
1656 | error: | |
2b82032c | 1657 | btrfs_free_path(path); |
0b86a832 CM |
1658 | return ret; |
1659 | } | |
1660 | ||
1661 | /* | |
1662 | * the device information is stored in the chunk root | |
1663 | * the btrfs_device struct should be fully filled in | |
1664 | */ | |
48a3b636 | 1665 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
5b4aacef | 1666 | struct btrfs_fs_info *fs_info, |
48a3b636 | 1667 | struct btrfs_device *device) |
0b86a832 | 1668 | { |
5b4aacef | 1669 | struct btrfs_root *root = fs_info->chunk_root; |
0b86a832 CM |
1670 | int ret; |
1671 | struct btrfs_path *path; | |
1672 | struct btrfs_dev_item *dev_item; | |
1673 | struct extent_buffer *leaf; | |
1674 | struct btrfs_key key; | |
1675 | unsigned long ptr; | |
0b86a832 | 1676 | |
0b86a832 CM |
1677 | path = btrfs_alloc_path(); |
1678 | if (!path) | |
1679 | return -ENOMEM; | |
1680 | ||
0b86a832 CM |
1681 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1682 | key.type = BTRFS_DEV_ITEM_KEY; | |
2b82032c | 1683 | key.offset = device->devid; |
0b86a832 CM |
1684 | |
1685 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
0d81ba5d | 1686 | sizeof(*dev_item)); |
0b86a832 CM |
1687 | if (ret) |
1688 | goto out; | |
1689 | ||
1690 | leaf = path->nodes[0]; | |
1691 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1692 | ||
1693 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2b82032c | 1694 | btrfs_set_device_generation(leaf, dev_item, 0); |
0b86a832 CM |
1695 | btrfs_set_device_type(leaf, dev_item, device->type); |
1696 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1697 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1698 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
7cc8e58d MX |
1699 | btrfs_set_device_total_bytes(leaf, dev_item, |
1700 | btrfs_device_get_disk_total_bytes(device)); | |
1701 | btrfs_set_device_bytes_used(leaf, dev_item, | |
1702 | btrfs_device_get_bytes_used(device)); | |
e17cade2 CM |
1703 | btrfs_set_device_group(leaf, dev_item, 0); |
1704 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
1705 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
c3027eb5 | 1706 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
0b86a832 | 1707 | |
410ba3a2 | 1708 | ptr = btrfs_device_uuid(dev_item); |
e17cade2 | 1709 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
1473b24e | 1710 | ptr = btrfs_device_fsid(dev_item); |
44880fdc | 1711 | write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE); |
0b86a832 | 1712 | btrfs_mark_buffer_dirty(leaf); |
0b86a832 | 1713 | |
2b82032c | 1714 | ret = 0; |
0b86a832 CM |
1715 | out: |
1716 | btrfs_free_path(path); | |
1717 | return ret; | |
1718 | } | |
8f18cf13 | 1719 | |
5a1972bd QW |
1720 | /* |
1721 | * Function to update ctime/mtime for a given device path. | |
1722 | * Mainly used for ctime/mtime based probe like libblkid. | |
1723 | */ | |
da353f6b | 1724 | static void update_dev_time(const char *path_name) |
5a1972bd QW |
1725 | { |
1726 | struct file *filp; | |
1727 | ||
1728 | filp = filp_open(path_name, O_RDWR, 0); | |
98af592f | 1729 | if (IS_ERR(filp)) |
5a1972bd QW |
1730 | return; |
1731 | file_update_time(filp); | |
1732 | filp_close(filp, NULL); | |
5a1972bd QW |
1733 | } |
1734 | ||
5b4aacef | 1735 | static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info, |
a061fc8d CM |
1736 | struct btrfs_device *device) |
1737 | { | |
5b4aacef | 1738 | struct btrfs_root *root = fs_info->chunk_root; |
a061fc8d CM |
1739 | int ret; |
1740 | struct btrfs_path *path; | |
a061fc8d | 1741 | struct btrfs_key key; |
a061fc8d CM |
1742 | struct btrfs_trans_handle *trans; |
1743 | ||
a061fc8d CM |
1744 | path = btrfs_alloc_path(); |
1745 | if (!path) | |
1746 | return -ENOMEM; | |
1747 | ||
a22285a6 | 1748 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1749 | if (IS_ERR(trans)) { |
1750 | btrfs_free_path(path); | |
1751 | return PTR_ERR(trans); | |
1752 | } | |
a061fc8d CM |
1753 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1754 | key.type = BTRFS_DEV_ITEM_KEY; | |
1755 | key.offset = device->devid; | |
1756 | ||
1757 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
5e9f2ad5 NB |
1758 | if (ret) { |
1759 | if (ret > 0) | |
1760 | ret = -ENOENT; | |
1761 | btrfs_abort_transaction(trans, ret); | |
1762 | btrfs_end_transaction(trans); | |
a061fc8d CM |
1763 | goto out; |
1764 | } | |
1765 | ||
1766 | ret = btrfs_del_item(trans, root, path); | |
5e9f2ad5 NB |
1767 | if (ret) { |
1768 | btrfs_abort_transaction(trans, ret); | |
1769 | btrfs_end_transaction(trans); | |
1770 | } | |
1771 | ||
a061fc8d CM |
1772 | out: |
1773 | btrfs_free_path(path); | |
5e9f2ad5 NB |
1774 | if (!ret) |
1775 | ret = btrfs_commit_transaction(trans); | |
a061fc8d CM |
1776 | return ret; |
1777 | } | |
1778 | ||
3cc31a0d DS |
1779 | /* |
1780 | * Verify that @num_devices satisfies the RAID profile constraints in the whole | |
1781 | * filesystem. It's up to the caller to adjust that number regarding eg. device | |
1782 | * replace. | |
1783 | */ | |
1784 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, | |
1785 | u64 num_devices) | |
a061fc8d | 1786 | { |
a061fc8d | 1787 | u64 all_avail; |
de98ced9 | 1788 | unsigned seq; |
418775a2 | 1789 | int i; |
a061fc8d | 1790 | |
de98ced9 | 1791 | do { |
bd45ffbc | 1792 | seq = read_seqbegin(&fs_info->profiles_lock); |
de98ced9 | 1793 | |
bd45ffbc AJ |
1794 | all_avail = fs_info->avail_data_alloc_bits | |
1795 | fs_info->avail_system_alloc_bits | | |
1796 | fs_info->avail_metadata_alloc_bits; | |
1797 | } while (read_seqretry(&fs_info->profiles_lock, seq)); | |
a061fc8d | 1798 | |
418775a2 DS |
1799 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
1800 | if (!(all_avail & btrfs_raid_group[i])) | |
1801 | continue; | |
a061fc8d | 1802 | |
418775a2 DS |
1803 | if (num_devices < btrfs_raid_array[i].devs_min) { |
1804 | int ret = btrfs_raid_mindev_error[i]; | |
bd45ffbc | 1805 | |
418775a2 DS |
1806 | if (ret) |
1807 | return ret; | |
1808 | } | |
53b381b3 DW |
1809 | } |
1810 | ||
bd45ffbc | 1811 | return 0; |
f1fa7f26 AJ |
1812 | } |
1813 | ||
c9162bdf OS |
1814 | static struct btrfs_device * btrfs_find_next_active_device( |
1815 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) | |
a061fc8d | 1816 | { |
2b82032c | 1817 | struct btrfs_device *next_device; |
88acff64 AJ |
1818 | |
1819 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { | |
1820 | if (next_device != device && | |
1821 | !next_device->missing && next_device->bdev) | |
1822 | return next_device; | |
1823 | } | |
1824 | ||
1825 | return NULL; | |
1826 | } | |
1827 | ||
1828 | /* | |
1829 | * Helper function to check if the given device is part of s_bdev / latest_bdev | |
1830 | * and replace it with the provided or the next active device, in the context | |
1831 | * where this function called, there should be always be another device (or | |
1832 | * this_dev) which is active. | |
1833 | */ | |
1834 | void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info, | |
1835 | struct btrfs_device *device, struct btrfs_device *this_dev) | |
1836 | { | |
1837 | struct btrfs_device *next_device; | |
1838 | ||
1839 | if (this_dev) | |
1840 | next_device = this_dev; | |
1841 | else | |
1842 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, | |
1843 | device); | |
1844 | ASSERT(next_device); | |
1845 | ||
1846 | if (fs_info->sb->s_bdev && | |
1847 | (fs_info->sb->s_bdev == device->bdev)) | |
1848 | fs_info->sb->s_bdev = next_device->bdev; | |
1849 | ||
1850 | if (fs_info->fs_devices->latest_bdev == device->bdev) | |
1851 | fs_info->fs_devices->latest_bdev = next_device->bdev; | |
1852 | } | |
1853 | ||
da353f6b DS |
1854 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
1855 | u64 devid) | |
f1fa7f26 AJ |
1856 | { |
1857 | struct btrfs_device *device; | |
1f78160c | 1858 | struct btrfs_fs_devices *cur_devices; |
2b82032c | 1859 | u64 num_devices; |
a061fc8d CM |
1860 | int ret = 0; |
1861 | ||
a061fc8d CM |
1862 | mutex_lock(&uuid_mutex); |
1863 | ||
0b246afa JM |
1864 | num_devices = fs_info->fs_devices->num_devices; |
1865 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); | |
1866 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { | |
8dabb742 SB |
1867 | WARN_ON(num_devices < 1); |
1868 | num_devices--; | |
1869 | } | |
0b246afa | 1870 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
8dabb742 | 1871 | |
0b246afa | 1872 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
f1fa7f26 | 1873 | if (ret) |
a061fc8d | 1874 | goto out; |
a061fc8d | 1875 | |
2ff7e61e JM |
1876 | ret = btrfs_find_device_by_devspec(fs_info, devid, device_path, |
1877 | &device); | |
24fc572f | 1878 | if (ret) |
53b381b3 | 1879 | goto out; |
dfe25020 | 1880 | |
63a212ab | 1881 | if (device->is_tgtdev_for_dev_replace) { |
183860f6 | 1882 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
24fc572f | 1883 | goto out; |
63a212ab SB |
1884 | } |
1885 | ||
0b246afa | 1886 | if (device->writeable && fs_info->fs_devices->rw_devices == 1) { |
183860f6 | 1887 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
24fc572f | 1888 | goto out; |
2b82032c YZ |
1889 | } |
1890 | ||
1891 | if (device->writeable) { | |
34441361 | 1892 | mutex_lock(&fs_info->chunk_mutex); |
2b82032c | 1893 | list_del_init(&device->dev_alloc_list); |
c3929c36 | 1894 | device->fs_devices->rw_devices--; |
34441361 | 1895 | mutex_unlock(&fs_info->chunk_mutex); |
dfe25020 | 1896 | } |
a061fc8d | 1897 | |
d7901554 | 1898 | mutex_unlock(&uuid_mutex); |
a061fc8d | 1899 | ret = btrfs_shrink_device(device, 0); |
d7901554 | 1900 | mutex_lock(&uuid_mutex); |
a061fc8d | 1901 | if (ret) |
9b3517e9 | 1902 | goto error_undo; |
a061fc8d | 1903 | |
63a212ab SB |
1904 | /* |
1905 | * TODO: the superblock still includes this device in its num_devices | |
1906 | * counter although write_all_supers() is not locked out. This | |
1907 | * could give a filesystem state which requires a degraded mount. | |
1908 | */ | |
0b246afa | 1909 | ret = btrfs_rm_dev_item(fs_info, device); |
a061fc8d | 1910 | if (ret) |
9b3517e9 | 1911 | goto error_undo; |
a061fc8d | 1912 | |
2b82032c | 1913 | device->in_fs_metadata = 0; |
0b246afa | 1914 | btrfs_scrub_cancel_dev(fs_info, device); |
e5e9a520 CM |
1915 | |
1916 | /* | |
1917 | * the device list mutex makes sure that we don't change | |
1918 | * the device list while someone else is writing out all | |
d7306801 FDBM |
1919 | * the device supers. Whoever is writing all supers, should |
1920 | * lock the device list mutex before getting the number of | |
1921 | * devices in the super block (super_copy). Conversely, | |
1922 | * whoever updates the number of devices in the super block | |
1923 | * (super_copy) should hold the device list mutex. | |
e5e9a520 | 1924 | */ |
1f78160c XG |
1925 | |
1926 | cur_devices = device->fs_devices; | |
0b246afa | 1927 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
1f78160c | 1928 | list_del_rcu(&device->dev_list); |
e5e9a520 | 1929 | |
e4404d6e | 1930 | device->fs_devices->num_devices--; |
02db0844 | 1931 | device->fs_devices->total_devices--; |
2b82032c | 1932 | |
cd02dca5 | 1933 | if (device->missing) |
3a7d55c8 | 1934 | device->fs_devices->missing_devices--; |
cd02dca5 | 1935 | |
0b246afa | 1936 | btrfs_assign_next_active_device(fs_info, device, NULL); |
2b82032c | 1937 | |
0bfaa9c5 | 1938 | if (device->bdev) { |
e4404d6e | 1939 | device->fs_devices->open_devices--; |
0bfaa9c5 | 1940 | /* remove sysfs entry */ |
0b246afa | 1941 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, device); |
0bfaa9c5 | 1942 | } |
99994cde | 1943 | |
0b246afa JM |
1944 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
1945 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); | |
1946 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
2b82032c | 1947 | |
cea67ab9 JM |
1948 | /* |
1949 | * at this point, the device is zero sized and detached from | |
1950 | * the devices list. All that's left is to zero out the old | |
1951 | * supers and free the device. | |
1952 | */ | |
1953 | if (device->writeable) | |
1954 | btrfs_scratch_superblocks(device->bdev, device->name->str); | |
1955 | ||
1956 | btrfs_close_bdev(device); | |
1957 | call_rcu(&device->rcu, free_device); | |
1958 | ||
1f78160c | 1959 | if (cur_devices->open_devices == 0) { |
e4404d6e | 1960 | struct btrfs_fs_devices *fs_devices; |
0b246afa | 1961 | fs_devices = fs_info->fs_devices; |
e4404d6e | 1962 | while (fs_devices) { |
8321cf25 RS |
1963 | if (fs_devices->seed == cur_devices) { |
1964 | fs_devices->seed = cur_devices->seed; | |
e4404d6e | 1965 | break; |
8321cf25 | 1966 | } |
e4404d6e | 1967 | fs_devices = fs_devices->seed; |
2b82032c | 1968 | } |
1f78160c | 1969 | cur_devices->seed = NULL; |
1f78160c | 1970 | __btrfs_close_devices(cur_devices); |
1f78160c | 1971 | free_fs_devices(cur_devices); |
2b82032c YZ |
1972 | } |
1973 | ||
a061fc8d CM |
1974 | out: |
1975 | mutex_unlock(&uuid_mutex); | |
a061fc8d | 1976 | return ret; |
24fc572f | 1977 | |
9b3517e9 ID |
1978 | error_undo: |
1979 | if (device->writeable) { | |
34441361 | 1980 | mutex_lock(&fs_info->chunk_mutex); |
9b3517e9 | 1981 | list_add(&device->dev_alloc_list, |
0b246afa | 1982 | &fs_info->fs_devices->alloc_list); |
c3929c36 | 1983 | device->fs_devices->rw_devices++; |
34441361 | 1984 | mutex_unlock(&fs_info->chunk_mutex); |
9b3517e9 | 1985 | } |
24fc572f | 1986 | goto out; |
a061fc8d CM |
1987 | } |
1988 | ||
084b6e7c QW |
1989 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
1990 | struct btrfs_device *srcdev) | |
e93c89c1 | 1991 | { |
d51908ce AJ |
1992 | struct btrfs_fs_devices *fs_devices; |
1993 | ||
e93c89c1 | 1994 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
1357272f | 1995 | |
25e8e911 AJ |
1996 | /* |
1997 | * in case of fs with no seed, srcdev->fs_devices will point | |
1998 | * to fs_devices of fs_info. However when the dev being replaced is | |
1999 | * a seed dev it will point to the seed's local fs_devices. In short | |
2000 | * srcdev will have its correct fs_devices in both the cases. | |
2001 | */ | |
2002 | fs_devices = srcdev->fs_devices; | |
d51908ce | 2003 | |
e93c89c1 | 2004 | list_del_rcu(&srcdev->dev_list); |
619c47f3 | 2005 | list_del(&srcdev->dev_alloc_list); |
d51908ce | 2006 | fs_devices->num_devices--; |
82372bc8 | 2007 | if (srcdev->missing) |
d51908ce | 2008 | fs_devices->missing_devices--; |
e93c89c1 | 2009 | |
48b3b9d4 | 2010 | if (srcdev->writeable) |
82372bc8 | 2011 | fs_devices->rw_devices--; |
1357272f | 2012 | |
82372bc8 | 2013 | if (srcdev->bdev) |
d51908ce | 2014 | fs_devices->open_devices--; |
084b6e7c QW |
2015 | } |
2016 | ||
2017 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, | |
2018 | struct btrfs_device *srcdev) | |
2019 | { | |
2020 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; | |
e93c89c1 | 2021 | |
48b3b9d4 AJ |
2022 | if (srcdev->writeable) { |
2023 | /* zero out the old super if it is writable */ | |
2024 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); | |
2025 | } | |
14238819 AJ |
2026 | |
2027 | btrfs_close_bdev(srcdev); | |
e93c89c1 | 2028 | call_rcu(&srcdev->rcu, free_device); |
94d5f0c2 | 2029 | |
94d5f0c2 AJ |
2030 | /* if this is no devs we rather delete the fs_devices */ |
2031 | if (!fs_devices->num_devices) { | |
2032 | struct btrfs_fs_devices *tmp_fs_devices; | |
2033 | ||
6dd38f81 AJ |
2034 | /* |
2035 | * On a mounted FS, num_devices can't be zero unless it's a | |
2036 | * seed. In case of a seed device being replaced, the replace | |
2037 | * target added to the sprout FS, so there will be no more | |
2038 | * device left under the seed FS. | |
2039 | */ | |
2040 | ASSERT(fs_devices->seeding); | |
2041 | ||
94d5f0c2 AJ |
2042 | tmp_fs_devices = fs_info->fs_devices; |
2043 | while (tmp_fs_devices) { | |
2044 | if (tmp_fs_devices->seed == fs_devices) { | |
2045 | tmp_fs_devices->seed = fs_devices->seed; | |
2046 | break; | |
2047 | } | |
2048 | tmp_fs_devices = tmp_fs_devices->seed; | |
2049 | } | |
2050 | fs_devices->seed = NULL; | |
8bef8401 AJ |
2051 | __btrfs_close_devices(fs_devices); |
2052 | free_fs_devices(fs_devices); | |
94d5f0c2 | 2053 | } |
e93c89c1 SB |
2054 | } |
2055 | ||
2056 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, | |
2057 | struct btrfs_device *tgtdev) | |
2058 | { | |
67a2c45e | 2059 | mutex_lock(&uuid_mutex); |
e93c89c1 SB |
2060 | WARN_ON(!tgtdev); |
2061 | mutex_lock(&fs_info->fs_devices->device_list_mutex); | |
d2ff1b20 | 2062 | |
32576040 | 2063 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev); |
d2ff1b20 | 2064 | |
779bf3fe | 2065 | if (tgtdev->bdev) |
e93c89c1 | 2066 | fs_info->fs_devices->open_devices--; |
779bf3fe | 2067 | |
e93c89c1 | 2068 | fs_info->fs_devices->num_devices--; |
e93c89c1 | 2069 | |
88acff64 | 2070 | btrfs_assign_next_active_device(fs_info, tgtdev, NULL); |
e93c89c1 | 2071 | |
e93c89c1 | 2072 | list_del_rcu(&tgtdev->dev_list); |
e93c89c1 SB |
2073 | |
2074 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
67a2c45e | 2075 | mutex_unlock(&uuid_mutex); |
779bf3fe AJ |
2076 | |
2077 | /* | |
2078 | * The update_dev_time() with in btrfs_scratch_superblocks() | |
2079 | * may lead to a call to btrfs_show_devname() which will try | |
2080 | * to hold device_list_mutex. And here this device | |
2081 | * is already out of device list, so we don't have to hold | |
2082 | * the device_list_mutex lock. | |
2083 | */ | |
2084 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); | |
14238819 AJ |
2085 | |
2086 | btrfs_close_bdev(tgtdev); | |
779bf3fe | 2087 | call_rcu(&tgtdev->rcu, free_device); |
e93c89c1 SB |
2088 | } |
2089 | ||
2ff7e61e | 2090 | static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info, |
da353f6b | 2091 | const char *device_path, |
48a3b636 | 2092 | struct btrfs_device **device) |
7ba15b7d SB |
2093 | { |
2094 | int ret = 0; | |
2095 | struct btrfs_super_block *disk_super; | |
2096 | u64 devid; | |
2097 | u8 *dev_uuid; | |
2098 | struct block_device *bdev; | |
2099 | struct buffer_head *bh; | |
2100 | ||
2101 | *device = NULL; | |
2102 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, | |
0b246afa | 2103 | fs_info->bdev_holder, 0, &bdev, &bh); |
7ba15b7d SB |
2104 | if (ret) |
2105 | return ret; | |
2106 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
2107 | devid = btrfs_stack_device_id(&disk_super->dev_item); | |
2108 | dev_uuid = disk_super->dev_item.uuid; | |
0b246afa | 2109 | *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid); |
7ba15b7d SB |
2110 | brelse(bh); |
2111 | if (!*device) | |
2112 | ret = -ENOENT; | |
2113 | blkdev_put(bdev, FMODE_READ); | |
2114 | return ret; | |
2115 | } | |
2116 | ||
2ff7e61e | 2117 | int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info, |
da353f6b | 2118 | const char *device_path, |
7ba15b7d SB |
2119 | struct btrfs_device **device) |
2120 | { | |
2121 | *device = NULL; | |
2122 | if (strcmp(device_path, "missing") == 0) { | |
2123 | struct list_head *devices; | |
2124 | struct btrfs_device *tmp; | |
2125 | ||
0b246afa | 2126 | devices = &fs_info->fs_devices->devices; |
7ba15b7d SB |
2127 | /* |
2128 | * It is safe to read the devices since the volume_mutex | |
2129 | * is held by the caller. | |
2130 | */ | |
2131 | list_for_each_entry(tmp, devices, dev_list) { | |
2132 | if (tmp->in_fs_metadata && !tmp->bdev) { | |
2133 | *device = tmp; | |
2134 | break; | |
2135 | } | |
2136 | } | |
2137 | ||
d74a6259 AJ |
2138 | if (!*device) |
2139 | return BTRFS_ERROR_DEV_MISSING_NOT_FOUND; | |
7ba15b7d SB |
2140 | |
2141 | return 0; | |
2142 | } else { | |
2ff7e61e | 2143 | return btrfs_find_device_by_path(fs_info, device_path, device); |
7ba15b7d SB |
2144 | } |
2145 | } | |
2146 | ||
5c5c0df0 DS |
2147 | /* |
2148 | * Lookup a device given by device id, or the path if the id is 0. | |
2149 | */ | |
2ff7e61e | 2150 | int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid, |
da353f6b DS |
2151 | const char *devpath, |
2152 | struct btrfs_device **device) | |
24e0474b AJ |
2153 | { |
2154 | int ret; | |
2155 | ||
5c5c0df0 | 2156 | if (devid) { |
24e0474b | 2157 | ret = 0; |
0b246afa | 2158 | *device = btrfs_find_device(fs_info, devid, NULL, NULL); |
24e0474b AJ |
2159 | if (!*device) |
2160 | ret = -ENOENT; | |
2161 | } else { | |
5c5c0df0 | 2162 | if (!devpath || !devpath[0]) |
b3d1b153 AJ |
2163 | return -EINVAL; |
2164 | ||
2ff7e61e | 2165 | ret = btrfs_find_device_missing_or_by_path(fs_info, devpath, |
24e0474b AJ |
2166 | device); |
2167 | } | |
2168 | return ret; | |
2169 | } | |
2170 | ||
2b82032c YZ |
2171 | /* |
2172 | * does all the dirty work required for changing file system's UUID. | |
2173 | */ | |
2ff7e61e | 2174 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
2b82032c | 2175 | { |
0b246afa | 2176 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
2b82032c | 2177 | struct btrfs_fs_devices *old_devices; |
e4404d6e | 2178 | struct btrfs_fs_devices *seed_devices; |
0b246afa | 2179 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
2b82032c YZ |
2180 | struct btrfs_device *device; |
2181 | u64 super_flags; | |
2182 | ||
2183 | BUG_ON(!mutex_is_locked(&uuid_mutex)); | |
e4404d6e | 2184 | if (!fs_devices->seeding) |
2b82032c YZ |
2185 | return -EINVAL; |
2186 | ||
2dfeca9b | 2187 | seed_devices = alloc_fs_devices(NULL); |
2208a378 ID |
2188 | if (IS_ERR(seed_devices)) |
2189 | return PTR_ERR(seed_devices); | |
2b82032c | 2190 | |
e4404d6e YZ |
2191 | old_devices = clone_fs_devices(fs_devices); |
2192 | if (IS_ERR(old_devices)) { | |
2193 | kfree(seed_devices); | |
2194 | return PTR_ERR(old_devices); | |
2b82032c | 2195 | } |
e4404d6e | 2196 | |
2b82032c YZ |
2197 | list_add(&old_devices->list, &fs_uuids); |
2198 | ||
e4404d6e YZ |
2199 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
2200 | seed_devices->opened = 1; | |
2201 | INIT_LIST_HEAD(&seed_devices->devices); | |
2202 | INIT_LIST_HEAD(&seed_devices->alloc_list); | |
e5e9a520 | 2203 | mutex_init(&seed_devices->device_list_mutex); |
c9513edb | 2204 | |
0b246afa | 2205 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
1f78160c XG |
2206 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
2207 | synchronize_rcu); | |
2196d6e8 MX |
2208 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
2209 | device->fs_devices = seed_devices; | |
c9513edb | 2210 | |
34441361 | 2211 | mutex_lock(&fs_info->chunk_mutex); |
e4404d6e | 2212 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
34441361 | 2213 | mutex_unlock(&fs_info->chunk_mutex); |
e4404d6e | 2214 | |
2b82032c YZ |
2215 | fs_devices->seeding = 0; |
2216 | fs_devices->num_devices = 0; | |
2217 | fs_devices->open_devices = 0; | |
69611ac8 | 2218 | fs_devices->missing_devices = 0; |
69611ac8 | 2219 | fs_devices->rotating = 0; |
e4404d6e | 2220 | fs_devices->seed = seed_devices; |
2b82032c YZ |
2221 | |
2222 | generate_random_uuid(fs_devices->fsid); | |
0b246afa | 2223 | memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
2b82032c | 2224 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
0b246afa | 2225 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
f7171750 | 2226 | |
2b82032c YZ |
2227 | super_flags = btrfs_super_flags(disk_super) & |
2228 | ~BTRFS_SUPER_FLAG_SEEDING; | |
2229 | btrfs_set_super_flags(disk_super, super_flags); | |
2230 | ||
2231 | return 0; | |
2232 | } | |
2233 | ||
2234 | /* | |
01327610 | 2235 | * Store the expected generation for seed devices in device items. |
2b82032c YZ |
2236 | */ |
2237 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, | |
5b4aacef | 2238 | struct btrfs_fs_info *fs_info) |
2b82032c | 2239 | { |
5b4aacef | 2240 | struct btrfs_root *root = fs_info->chunk_root; |
2b82032c YZ |
2241 | struct btrfs_path *path; |
2242 | struct extent_buffer *leaf; | |
2243 | struct btrfs_dev_item *dev_item; | |
2244 | struct btrfs_device *device; | |
2245 | struct btrfs_key key; | |
44880fdc | 2246 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
2b82032c YZ |
2247 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
2248 | u64 devid; | |
2249 | int ret; | |
2250 | ||
2251 | path = btrfs_alloc_path(); | |
2252 | if (!path) | |
2253 | return -ENOMEM; | |
2254 | ||
2b82032c YZ |
2255 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
2256 | key.offset = 0; | |
2257 | key.type = BTRFS_DEV_ITEM_KEY; | |
2258 | ||
2259 | while (1) { | |
2260 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
2261 | if (ret < 0) | |
2262 | goto error; | |
2263 | ||
2264 | leaf = path->nodes[0]; | |
2265 | next_slot: | |
2266 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
2267 | ret = btrfs_next_leaf(root, path); | |
2268 | if (ret > 0) | |
2269 | break; | |
2270 | if (ret < 0) | |
2271 | goto error; | |
2272 | leaf = path->nodes[0]; | |
2273 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 2274 | btrfs_release_path(path); |
2b82032c YZ |
2275 | continue; |
2276 | } | |
2277 | ||
2278 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2279 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || | |
2280 | key.type != BTRFS_DEV_ITEM_KEY) | |
2281 | break; | |
2282 | ||
2283 | dev_item = btrfs_item_ptr(leaf, path->slots[0], | |
2284 | struct btrfs_dev_item); | |
2285 | devid = btrfs_device_id(leaf, dev_item); | |
410ba3a2 | 2286 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
2b82032c | 2287 | BTRFS_UUID_SIZE); |
1473b24e | 2288 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
44880fdc | 2289 | BTRFS_FSID_SIZE); |
0b246afa | 2290 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
79787eaa | 2291 | BUG_ON(!device); /* Logic error */ |
2b82032c YZ |
2292 | |
2293 | if (device->fs_devices->seeding) { | |
2294 | btrfs_set_device_generation(leaf, dev_item, | |
2295 | device->generation); | |
2296 | btrfs_mark_buffer_dirty(leaf); | |
2297 | } | |
2298 | ||
2299 | path->slots[0]++; | |
2300 | goto next_slot; | |
2301 | } | |
2302 | ret = 0; | |
2303 | error: | |
2304 | btrfs_free_path(path); | |
2305 | return ret; | |
2306 | } | |
2307 | ||
da353f6b | 2308 | int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path) |
788f20eb | 2309 | { |
5112febb | 2310 | struct btrfs_root *root = fs_info->dev_root; |
d5e2003c | 2311 | struct request_queue *q; |
788f20eb CM |
2312 | struct btrfs_trans_handle *trans; |
2313 | struct btrfs_device *device; | |
2314 | struct block_device *bdev; | |
788f20eb | 2315 | struct list_head *devices; |
0b246afa | 2316 | struct super_block *sb = fs_info->sb; |
606686ee | 2317 | struct rcu_string *name; |
3c1dbdf5 | 2318 | u64 tmp; |
2b82032c | 2319 | int seeding_dev = 0; |
788f20eb | 2320 | int ret = 0; |
7132a262 | 2321 | bool unlocked = false; |
788f20eb | 2322 | |
bc98a42c | 2323 | if (sb_rdonly(sb) && !fs_info->fs_devices->seeding) |
f8c5d0b4 | 2324 | return -EROFS; |
788f20eb | 2325 | |
a5d16333 | 2326 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
0b246afa | 2327 | fs_info->bdev_holder); |
7f59203a JB |
2328 | if (IS_ERR(bdev)) |
2329 | return PTR_ERR(bdev); | |
a2135011 | 2330 | |
0b246afa | 2331 | if (fs_info->fs_devices->seeding) { |
2b82032c YZ |
2332 | seeding_dev = 1; |
2333 | down_write(&sb->s_umount); | |
2334 | mutex_lock(&uuid_mutex); | |
2335 | } | |
2336 | ||
8c8bee1d | 2337 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
a2135011 | 2338 | |
0b246afa | 2339 | devices = &fs_info->fs_devices->devices; |
d25628bd | 2340 | |
0b246afa | 2341 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
c6e30871 | 2342 | list_for_each_entry(device, devices, dev_list) { |
788f20eb CM |
2343 | if (device->bdev == bdev) { |
2344 | ret = -EEXIST; | |
d25628bd | 2345 | mutex_unlock( |
0b246afa | 2346 | &fs_info->fs_devices->device_list_mutex); |
2b82032c | 2347 | goto error; |
788f20eb CM |
2348 | } |
2349 | } | |
0b246afa | 2350 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
788f20eb | 2351 | |
0b246afa | 2352 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
12bd2fc0 | 2353 | if (IS_ERR(device)) { |
788f20eb | 2354 | /* we can safely leave the fs_devices entry around */ |
12bd2fc0 | 2355 | ret = PTR_ERR(device); |
2b82032c | 2356 | goto error; |
788f20eb CM |
2357 | } |
2358 | ||
78f2c9e6 | 2359 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
606686ee | 2360 | if (!name) { |
3065ae5b | 2361 | bio_put(device->flush_bio); |
788f20eb | 2362 | kfree(device); |
2b82032c YZ |
2363 | ret = -ENOMEM; |
2364 | goto error; | |
788f20eb | 2365 | } |
606686ee | 2366 | rcu_assign_pointer(device->name, name); |
2b82032c | 2367 | |
a22285a6 | 2368 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 2369 | if (IS_ERR(trans)) { |
606686ee | 2370 | rcu_string_free(device->name); |
3065ae5b | 2371 | bio_put(device->flush_bio); |
98d5dc13 TI |
2372 | kfree(device); |
2373 | ret = PTR_ERR(trans); | |
2374 | goto error; | |
2375 | } | |
2376 | ||
d5e2003c JB |
2377 | q = bdev_get_queue(bdev); |
2378 | if (blk_queue_discard(q)) | |
2379 | device->can_discard = 1; | |
2b82032c | 2380 | device->writeable = 1; |
2b82032c | 2381 | device->generation = trans->transid; |
0b246afa JM |
2382 | device->io_width = fs_info->sectorsize; |
2383 | device->io_align = fs_info->sectorsize; | |
2384 | device->sector_size = fs_info->sectorsize; | |
7dfb8be1 NB |
2385 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
2386 | fs_info->sectorsize); | |
2cc3c559 | 2387 | device->disk_total_bytes = device->total_bytes; |
935e5cc9 | 2388 | device->commit_total_bytes = device->total_bytes; |
fb456252 | 2389 | device->fs_info = fs_info; |
788f20eb | 2390 | device->bdev = bdev; |
dfe25020 | 2391 | device->in_fs_metadata = 1; |
63a212ab | 2392 | device->is_tgtdev_for_dev_replace = 0; |
fb01aa85 | 2393 | device->mode = FMODE_EXCL; |
27087f37 | 2394 | device->dev_stats_valid = 1; |
9f6d2510 | 2395 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
788f20eb | 2396 | |
2b82032c | 2397 | if (seeding_dev) { |
1751e8a6 | 2398 | sb->s_flags &= ~SB_RDONLY; |
2ff7e61e | 2399 | ret = btrfs_prepare_sprout(fs_info); |
d31c32f6 AJ |
2400 | if (ret) { |
2401 | btrfs_abort_transaction(trans, ret); | |
2402 | goto error_trans; | |
2403 | } | |
2b82032c | 2404 | } |
788f20eb | 2405 | |
0b246afa | 2406 | device->fs_devices = fs_info->fs_devices; |
e5e9a520 | 2407 | |
0b246afa | 2408 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
34441361 | 2409 | mutex_lock(&fs_info->chunk_mutex); |
0b246afa | 2410 | list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices); |
2b82032c | 2411 | list_add(&device->dev_alloc_list, |
0b246afa JM |
2412 | &fs_info->fs_devices->alloc_list); |
2413 | fs_info->fs_devices->num_devices++; | |
2414 | fs_info->fs_devices->open_devices++; | |
2415 | fs_info->fs_devices->rw_devices++; | |
2416 | fs_info->fs_devices->total_devices++; | |
2417 | fs_info->fs_devices->total_rw_bytes += device->total_bytes; | |
325cd4ba | 2418 | |
a5ed45f8 | 2419 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
2bf64758 | 2420 | |
e884f4f0 | 2421 | if (!blk_queue_nonrot(q)) |
0b246afa | 2422 | fs_info->fs_devices->rotating = 1; |
c289811c | 2423 | |
0b246afa JM |
2424 | tmp = btrfs_super_total_bytes(fs_info->super_copy); |
2425 | btrfs_set_super_total_bytes(fs_info->super_copy, | |
7dfb8be1 | 2426 | round_down(tmp + device->total_bytes, fs_info->sectorsize)); |
788f20eb | 2427 | |
0b246afa JM |
2428 | tmp = btrfs_super_num_devices(fs_info->super_copy); |
2429 | btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1); | |
0d39376a AJ |
2430 | |
2431 | /* add sysfs device entry */ | |
0b246afa | 2432 | btrfs_sysfs_add_device_link(fs_info->fs_devices, device); |
0d39376a | 2433 | |
2196d6e8 MX |
2434 | /* |
2435 | * we've got more storage, clear any full flags on the space | |
2436 | * infos | |
2437 | */ | |
0b246afa | 2438 | btrfs_clear_space_info_full(fs_info); |
2196d6e8 | 2439 | |
34441361 | 2440 | mutex_unlock(&fs_info->chunk_mutex); |
0b246afa | 2441 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
788f20eb | 2442 | |
2b82032c | 2443 | if (seeding_dev) { |
34441361 | 2444 | mutex_lock(&fs_info->chunk_mutex); |
e4a4dce7 | 2445 | ret = init_first_rw_device(trans, fs_info); |
34441361 | 2446 | mutex_unlock(&fs_info->chunk_mutex); |
005d6427 | 2447 | if (ret) { |
66642832 | 2448 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2449 | goto error_sysfs; |
005d6427 | 2450 | } |
2196d6e8 MX |
2451 | } |
2452 | ||
0b246afa | 2453 | ret = btrfs_add_device(trans, fs_info, device); |
2196d6e8 | 2454 | if (ret) { |
66642832 | 2455 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2456 | goto error_sysfs; |
2196d6e8 MX |
2457 | } |
2458 | ||
2459 | if (seeding_dev) { | |
2460 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; | |
2461 | ||
0b246afa | 2462 | ret = btrfs_finish_sprout(trans, fs_info); |
005d6427 | 2463 | if (ret) { |
66642832 | 2464 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2465 | goto error_sysfs; |
005d6427 | 2466 | } |
b2373f25 AJ |
2467 | |
2468 | /* Sprouting would change fsid of the mounted root, | |
2469 | * so rename the fsid on the sysfs | |
2470 | */ | |
2471 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", | |
0b246afa JM |
2472 | fs_info->fsid); |
2473 | if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf)) | |
2474 | btrfs_warn(fs_info, | |
2475 | "sysfs: failed to create fsid for sprout"); | |
2b82032c YZ |
2476 | } |
2477 | ||
3a45bb20 | 2478 | ret = btrfs_commit_transaction(trans); |
a2135011 | 2479 | |
2b82032c YZ |
2480 | if (seeding_dev) { |
2481 | mutex_unlock(&uuid_mutex); | |
2482 | up_write(&sb->s_umount); | |
7132a262 | 2483 | unlocked = true; |
788f20eb | 2484 | |
79787eaa JM |
2485 | if (ret) /* transaction commit */ |
2486 | return ret; | |
2487 | ||
2ff7e61e | 2488 | ret = btrfs_relocate_sys_chunks(fs_info); |
79787eaa | 2489 | if (ret < 0) |
0b246afa | 2490 | btrfs_handle_fs_error(fs_info, ret, |
5d163e0e | 2491 | "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); |
671415b7 MX |
2492 | trans = btrfs_attach_transaction(root); |
2493 | if (IS_ERR(trans)) { | |
2494 | if (PTR_ERR(trans) == -ENOENT) | |
2495 | return 0; | |
7132a262 AJ |
2496 | ret = PTR_ERR(trans); |
2497 | trans = NULL; | |
2498 | goto error_sysfs; | |
671415b7 | 2499 | } |
3a45bb20 | 2500 | ret = btrfs_commit_transaction(trans); |
2b82032c | 2501 | } |
c9e9f97b | 2502 | |
5a1972bd QW |
2503 | /* Update ctime/mtime for libblkid */ |
2504 | update_dev_time(device_path); | |
2b82032c | 2505 | return ret; |
79787eaa | 2506 | |
d31c32f6 AJ |
2507 | error_sysfs: |
2508 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, device); | |
79787eaa | 2509 | error_trans: |
0af2c4bf | 2510 | if (seeding_dev) |
1751e8a6 | 2511 | sb->s_flags |= SB_RDONLY; |
7132a262 AJ |
2512 | if (trans) |
2513 | btrfs_end_transaction(trans); | |
606686ee | 2514 | rcu_string_free(device->name); |
3065ae5b | 2515 | bio_put(device->flush_bio); |
79787eaa | 2516 | kfree(device); |
2b82032c | 2517 | error: |
e525fd89 | 2518 | blkdev_put(bdev, FMODE_EXCL); |
7132a262 | 2519 | if (seeding_dev && !unlocked) { |
2b82032c YZ |
2520 | mutex_unlock(&uuid_mutex); |
2521 | up_write(&sb->s_umount); | |
2522 | } | |
c9e9f97b | 2523 | return ret; |
788f20eb CM |
2524 | } |
2525 | ||
2ff7e61e | 2526 | int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
da353f6b | 2527 | const char *device_path, |
1c43366d | 2528 | struct btrfs_device *srcdev, |
e93c89c1 SB |
2529 | struct btrfs_device **device_out) |
2530 | { | |
2531 | struct request_queue *q; | |
2532 | struct btrfs_device *device; | |
2533 | struct block_device *bdev; | |
e93c89c1 SB |
2534 | struct list_head *devices; |
2535 | struct rcu_string *name; | |
12bd2fc0 | 2536 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
e93c89c1 SB |
2537 | int ret = 0; |
2538 | ||
2539 | *device_out = NULL; | |
1c43366d MX |
2540 | if (fs_info->fs_devices->seeding) { |
2541 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); | |
e93c89c1 | 2542 | return -EINVAL; |
1c43366d | 2543 | } |
e93c89c1 SB |
2544 | |
2545 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, | |
2546 | fs_info->bdev_holder); | |
1c43366d MX |
2547 | if (IS_ERR(bdev)) { |
2548 | btrfs_err(fs_info, "target device %s is invalid!", device_path); | |
e93c89c1 | 2549 | return PTR_ERR(bdev); |
1c43366d | 2550 | } |
e93c89c1 SB |
2551 | |
2552 | filemap_write_and_wait(bdev->bd_inode->i_mapping); | |
2553 | ||
2554 | devices = &fs_info->fs_devices->devices; | |
2555 | list_for_each_entry(device, devices, dev_list) { | |
2556 | if (device->bdev == bdev) { | |
5d163e0e JM |
2557 | btrfs_err(fs_info, |
2558 | "target device is in the filesystem!"); | |
e93c89c1 SB |
2559 | ret = -EEXIST; |
2560 | goto error; | |
2561 | } | |
2562 | } | |
2563 | ||
1c43366d | 2564 | |
7cc8e58d MX |
2565 | if (i_size_read(bdev->bd_inode) < |
2566 | btrfs_device_get_total_bytes(srcdev)) { | |
5d163e0e JM |
2567 | btrfs_err(fs_info, |
2568 | "target device is smaller than source device!"); | |
1c43366d MX |
2569 | ret = -EINVAL; |
2570 | goto error; | |
2571 | } | |
2572 | ||
2573 | ||
12bd2fc0 ID |
2574 | device = btrfs_alloc_device(NULL, &devid, NULL); |
2575 | if (IS_ERR(device)) { | |
2576 | ret = PTR_ERR(device); | |
e93c89c1 SB |
2577 | goto error; |
2578 | } | |
2579 | ||
6165572c | 2580 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
e93c89c1 | 2581 | if (!name) { |
3065ae5b | 2582 | bio_put(device->flush_bio); |
e93c89c1 SB |
2583 | kfree(device); |
2584 | ret = -ENOMEM; | |
2585 | goto error; | |
2586 | } | |
2587 | rcu_assign_pointer(device->name, name); | |
2588 | ||
2589 | q = bdev_get_queue(bdev); | |
2590 | if (blk_queue_discard(q)) | |
2591 | device->can_discard = 1; | |
0b246afa | 2592 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
e93c89c1 | 2593 | device->writeable = 1; |
e93c89c1 | 2594 | device->generation = 0; |
0b246afa JM |
2595 | device->io_width = fs_info->sectorsize; |
2596 | device->io_align = fs_info->sectorsize; | |
2597 | device->sector_size = fs_info->sectorsize; | |
7cc8e58d MX |
2598 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
2599 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); | |
2600 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); | |
935e5cc9 MX |
2601 | ASSERT(list_empty(&srcdev->resized_list)); |
2602 | device->commit_total_bytes = srcdev->commit_total_bytes; | |
ce7213c7 | 2603 | device->commit_bytes_used = device->bytes_used; |
fb456252 | 2604 | device->fs_info = fs_info; |
e93c89c1 SB |
2605 | device->bdev = bdev; |
2606 | device->in_fs_metadata = 1; | |
2607 | device->is_tgtdev_for_dev_replace = 1; | |
2608 | device->mode = FMODE_EXCL; | |
27087f37 | 2609 | device->dev_stats_valid = 1; |
9f6d2510 | 2610 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
e93c89c1 SB |
2611 | device->fs_devices = fs_info->fs_devices; |
2612 | list_add(&device->dev_list, &fs_info->fs_devices->devices); | |
2613 | fs_info->fs_devices->num_devices++; | |
2614 | fs_info->fs_devices->open_devices++; | |
0b246afa | 2615 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
e93c89c1 SB |
2616 | |
2617 | *device_out = device; | |
2618 | return ret; | |
2619 | ||
2620 | error: | |
2621 | blkdev_put(bdev, FMODE_EXCL); | |
2622 | return ret; | |
2623 | } | |
2624 | ||
2625 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, | |
2626 | struct btrfs_device *tgtdev) | |
2627 | { | |
da17066c JM |
2628 | u32 sectorsize = fs_info->sectorsize; |
2629 | ||
e93c89c1 | 2630 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
da17066c JM |
2631 | tgtdev->io_width = sectorsize; |
2632 | tgtdev->io_align = sectorsize; | |
2633 | tgtdev->sector_size = sectorsize; | |
fb456252 | 2634 | tgtdev->fs_info = fs_info; |
e93c89c1 SB |
2635 | tgtdev->in_fs_metadata = 1; |
2636 | } | |
2637 | ||
d397712b CM |
2638 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
2639 | struct btrfs_device *device) | |
0b86a832 CM |
2640 | { |
2641 | int ret; | |
2642 | struct btrfs_path *path; | |
0b246afa | 2643 | struct btrfs_root *root = device->fs_info->chunk_root; |
0b86a832 CM |
2644 | struct btrfs_dev_item *dev_item; |
2645 | struct extent_buffer *leaf; | |
2646 | struct btrfs_key key; | |
2647 | ||
0b86a832 CM |
2648 | path = btrfs_alloc_path(); |
2649 | if (!path) | |
2650 | return -ENOMEM; | |
2651 | ||
2652 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
2653 | key.type = BTRFS_DEV_ITEM_KEY; | |
2654 | key.offset = device->devid; | |
2655 | ||
2656 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
2657 | if (ret < 0) | |
2658 | goto out; | |
2659 | ||
2660 | if (ret > 0) { | |
2661 | ret = -ENOENT; | |
2662 | goto out; | |
2663 | } | |
2664 | ||
2665 | leaf = path->nodes[0]; | |
2666 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
2667 | ||
2668 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2669 | btrfs_set_device_type(leaf, dev_item, device->type); | |
2670 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
2671 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
2672 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
7cc8e58d MX |
2673 | btrfs_set_device_total_bytes(leaf, dev_item, |
2674 | btrfs_device_get_disk_total_bytes(device)); | |
2675 | btrfs_set_device_bytes_used(leaf, dev_item, | |
2676 | btrfs_device_get_bytes_used(device)); | |
0b86a832 CM |
2677 | btrfs_mark_buffer_dirty(leaf); |
2678 | ||
2679 | out: | |
2680 | btrfs_free_path(path); | |
2681 | return ret; | |
2682 | } | |
2683 | ||
2196d6e8 | 2684 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
2685 | struct btrfs_device *device, u64 new_size) |
2686 | { | |
0b246afa JM |
2687 | struct btrfs_fs_info *fs_info = device->fs_info; |
2688 | struct btrfs_super_block *super_copy = fs_info->super_copy; | |
935e5cc9 | 2689 | struct btrfs_fs_devices *fs_devices; |
2196d6e8 MX |
2690 | u64 old_total; |
2691 | u64 diff; | |
8f18cf13 | 2692 | |
2b82032c YZ |
2693 | if (!device->writeable) |
2694 | return -EACCES; | |
2196d6e8 | 2695 | |
7dfb8be1 NB |
2696 | new_size = round_down(new_size, fs_info->sectorsize); |
2697 | ||
34441361 | 2698 | mutex_lock(&fs_info->chunk_mutex); |
2196d6e8 | 2699 | old_total = btrfs_super_total_bytes(super_copy); |
0e4324a4 | 2700 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
2196d6e8 | 2701 | |
63a212ab | 2702 | if (new_size <= device->total_bytes || |
2196d6e8 | 2703 | device->is_tgtdev_for_dev_replace) { |
34441361 | 2704 | mutex_unlock(&fs_info->chunk_mutex); |
2b82032c | 2705 | return -EINVAL; |
2196d6e8 | 2706 | } |
2b82032c | 2707 | |
0b246afa | 2708 | fs_devices = fs_info->fs_devices; |
2b82032c | 2709 | |
7dfb8be1 NB |
2710 | btrfs_set_super_total_bytes(super_copy, |
2711 | round_down(old_total + diff, fs_info->sectorsize)); | |
2b82032c YZ |
2712 | device->fs_devices->total_rw_bytes += diff; |
2713 | ||
7cc8e58d MX |
2714 | btrfs_device_set_total_bytes(device, new_size); |
2715 | btrfs_device_set_disk_total_bytes(device, new_size); | |
fb456252 | 2716 | btrfs_clear_space_info_full(device->fs_info); |
935e5cc9 MX |
2717 | if (list_empty(&device->resized_list)) |
2718 | list_add_tail(&device->resized_list, | |
2719 | &fs_devices->resized_devices); | |
34441361 | 2720 | mutex_unlock(&fs_info->chunk_mutex); |
4184ea7f | 2721 | |
8f18cf13 CM |
2722 | return btrfs_update_device(trans, device); |
2723 | } | |
2724 | ||
2725 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, | |
408fbf19 | 2726 | struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 2727 | { |
5b4aacef | 2728 | struct btrfs_root *root = fs_info->chunk_root; |
8f18cf13 CM |
2729 | int ret; |
2730 | struct btrfs_path *path; | |
2731 | struct btrfs_key key; | |
2732 | ||
8f18cf13 CM |
2733 | path = btrfs_alloc_path(); |
2734 | if (!path) | |
2735 | return -ENOMEM; | |
2736 | ||
408fbf19 | 2737 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
8f18cf13 CM |
2738 | key.offset = chunk_offset; |
2739 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2740 | ||
2741 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
79787eaa JM |
2742 | if (ret < 0) |
2743 | goto out; | |
2744 | else if (ret > 0) { /* Logic error or corruption */ | |
0b246afa JM |
2745 | btrfs_handle_fs_error(fs_info, -ENOENT, |
2746 | "Failed lookup while freeing chunk."); | |
79787eaa JM |
2747 | ret = -ENOENT; |
2748 | goto out; | |
2749 | } | |
8f18cf13 CM |
2750 | |
2751 | ret = btrfs_del_item(trans, root, path); | |
79787eaa | 2752 | if (ret < 0) |
0b246afa JM |
2753 | btrfs_handle_fs_error(fs_info, ret, |
2754 | "Failed to delete chunk item."); | |
79787eaa | 2755 | out: |
8f18cf13 | 2756 | btrfs_free_path(path); |
65a246c5 | 2757 | return ret; |
8f18cf13 CM |
2758 | } |
2759 | ||
408fbf19 | 2760 | static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 2761 | { |
0b246afa | 2762 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
8f18cf13 CM |
2763 | struct btrfs_disk_key *disk_key; |
2764 | struct btrfs_chunk *chunk; | |
2765 | u8 *ptr; | |
2766 | int ret = 0; | |
2767 | u32 num_stripes; | |
2768 | u32 array_size; | |
2769 | u32 len = 0; | |
2770 | u32 cur; | |
2771 | struct btrfs_key key; | |
2772 | ||
34441361 | 2773 | mutex_lock(&fs_info->chunk_mutex); |
8f18cf13 CM |
2774 | array_size = btrfs_super_sys_array_size(super_copy); |
2775 | ||
2776 | ptr = super_copy->sys_chunk_array; | |
2777 | cur = 0; | |
2778 | ||
2779 | while (cur < array_size) { | |
2780 | disk_key = (struct btrfs_disk_key *)ptr; | |
2781 | btrfs_disk_key_to_cpu(&key, disk_key); | |
2782 | ||
2783 | len = sizeof(*disk_key); | |
2784 | ||
2785 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { | |
2786 | chunk = (struct btrfs_chunk *)(ptr + len); | |
2787 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); | |
2788 | len += btrfs_chunk_item_size(num_stripes); | |
2789 | } else { | |
2790 | ret = -EIO; | |
2791 | break; | |
2792 | } | |
408fbf19 | 2793 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
8f18cf13 CM |
2794 | key.offset == chunk_offset) { |
2795 | memmove(ptr, ptr + len, array_size - (cur + len)); | |
2796 | array_size -= len; | |
2797 | btrfs_set_super_sys_array_size(super_copy, array_size); | |
2798 | } else { | |
2799 | ptr += len; | |
2800 | cur += len; | |
2801 | } | |
2802 | } | |
34441361 | 2803 | mutex_unlock(&fs_info->chunk_mutex); |
8f18cf13 CM |
2804 | return ret; |
2805 | } | |
2806 | ||
592d92ee LB |
2807 | static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info, |
2808 | u64 logical, u64 length) | |
2809 | { | |
2810 | struct extent_map_tree *em_tree; | |
2811 | struct extent_map *em; | |
2812 | ||
2813 | em_tree = &fs_info->mapping_tree.map_tree; | |
2814 | read_lock(&em_tree->lock); | |
2815 | em = lookup_extent_mapping(em_tree, logical, length); | |
2816 | read_unlock(&em_tree->lock); | |
2817 | ||
2818 | if (!em) { | |
2819 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", | |
2820 | logical, length); | |
2821 | return ERR_PTR(-EINVAL); | |
2822 | } | |
2823 | ||
2824 | if (em->start > logical || em->start + em->len < logical) { | |
2825 | btrfs_crit(fs_info, | |
2826 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", | |
2827 | logical, length, em->start, em->start + em->len); | |
2828 | free_extent_map(em); | |
2829 | return ERR_PTR(-EINVAL); | |
2830 | } | |
2831 | ||
2832 | /* callers are responsible for dropping em's ref. */ | |
2833 | return em; | |
2834 | } | |
2835 | ||
47ab2a6c | 2836 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
5b4aacef | 2837 | struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 2838 | { |
8f18cf13 CM |
2839 | struct extent_map *em; |
2840 | struct map_lookup *map; | |
2196d6e8 | 2841 | u64 dev_extent_len = 0; |
47ab2a6c | 2842 | int i, ret = 0; |
0b246afa | 2843 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
8f18cf13 | 2844 | |
592d92ee LB |
2845 | em = get_chunk_map(fs_info, chunk_offset, 1); |
2846 | if (IS_ERR(em)) { | |
47ab2a6c JB |
2847 | /* |
2848 | * This is a logic error, but we don't want to just rely on the | |
bb7ab3b9 | 2849 | * user having built with ASSERT enabled, so if ASSERT doesn't |
47ab2a6c JB |
2850 | * do anything we still error out. |
2851 | */ | |
2852 | ASSERT(0); | |
592d92ee | 2853 | return PTR_ERR(em); |
47ab2a6c | 2854 | } |
95617d69 | 2855 | map = em->map_lookup; |
34441361 | 2856 | mutex_lock(&fs_info->chunk_mutex); |
2ff7e61e | 2857 | check_system_chunk(trans, fs_info, map->type); |
34441361 | 2858 | mutex_unlock(&fs_info->chunk_mutex); |
8f18cf13 | 2859 | |
57ba4cb8 FM |
2860 | /* |
2861 | * Take the device list mutex to prevent races with the final phase of | |
2862 | * a device replace operation that replaces the device object associated | |
2863 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). | |
2864 | */ | |
2865 | mutex_lock(&fs_devices->device_list_mutex); | |
8f18cf13 | 2866 | for (i = 0; i < map->num_stripes; i++) { |
47ab2a6c | 2867 | struct btrfs_device *device = map->stripes[i].dev; |
2196d6e8 MX |
2868 | ret = btrfs_free_dev_extent(trans, device, |
2869 | map->stripes[i].physical, | |
2870 | &dev_extent_len); | |
47ab2a6c | 2871 | if (ret) { |
57ba4cb8 | 2872 | mutex_unlock(&fs_devices->device_list_mutex); |
66642832 | 2873 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2874 | goto out; |
2875 | } | |
a061fc8d | 2876 | |
2196d6e8 | 2877 | if (device->bytes_used > 0) { |
34441361 | 2878 | mutex_lock(&fs_info->chunk_mutex); |
2196d6e8 MX |
2879 | btrfs_device_set_bytes_used(device, |
2880 | device->bytes_used - dev_extent_len); | |
a5ed45f8 | 2881 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
0b246afa | 2882 | btrfs_clear_space_info_full(fs_info); |
34441361 | 2883 | mutex_unlock(&fs_info->chunk_mutex); |
2196d6e8 | 2884 | } |
a061fc8d | 2885 | |
dfe25020 CM |
2886 | if (map->stripes[i].dev) { |
2887 | ret = btrfs_update_device(trans, map->stripes[i].dev); | |
47ab2a6c | 2888 | if (ret) { |
57ba4cb8 | 2889 | mutex_unlock(&fs_devices->device_list_mutex); |
66642832 | 2890 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2891 | goto out; |
2892 | } | |
dfe25020 | 2893 | } |
8f18cf13 | 2894 | } |
57ba4cb8 FM |
2895 | mutex_unlock(&fs_devices->device_list_mutex); |
2896 | ||
408fbf19 | 2897 | ret = btrfs_free_chunk(trans, fs_info, chunk_offset); |
47ab2a6c | 2898 | if (ret) { |
66642832 | 2899 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2900 | goto out; |
2901 | } | |
8f18cf13 | 2902 | |
6bccf3ab | 2903 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
1abe9b8a | 2904 | |
8f18cf13 | 2905 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
408fbf19 | 2906 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
47ab2a6c | 2907 | if (ret) { |
66642832 | 2908 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2909 | goto out; |
2910 | } | |
8f18cf13 CM |
2911 | } |
2912 | ||
6bccf3ab | 2913 | ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em); |
47ab2a6c | 2914 | if (ret) { |
66642832 | 2915 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2916 | goto out; |
2917 | } | |
2b82032c | 2918 | |
47ab2a6c | 2919 | out: |
2b82032c YZ |
2920 | /* once for us */ |
2921 | free_extent_map(em); | |
47ab2a6c JB |
2922 | return ret; |
2923 | } | |
2b82032c | 2924 | |
5b4aacef | 2925 | static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
47ab2a6c | 2926 | { |
5b4aacef | 2927 | struct btrfs_root *root = fs_info->chunk_root; |
19c4d2f9 | 2928 | struct btrfs_trans_handle *trans; |
47ab2a6c | 2929 | int ret; |
2b82032c | 2930 | |
67c5e7d4 FM |
2931 | /* |
2932 | * Prevent races with automatic removal of unused block groups. | |
2933 | * After we relocate and before we remove the chunk with offset | |
2934 | * chunk_offset, automatic removal of the block group can kick in, | |
2935 | * resulting in a failure when calling btrfs_remove_chunk() below. | |
2936 | * | |
2937 | * Make sure to acquire this mutex before doing a tree search (dev | |
2938 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might | |
2939 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after | |
2940 | * we release the path used to search the chunk/dev tree and before | |
2941 | * the current task acquires this mutex and calls us. | |
2942 | */ | |
0b246afa | 2943 | ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex)); |
67c5e7d4 | 2944 | |
0b246afa | 2945 | ret = btrfs_can_relocate(fs_info, chunk_offset); |
47ab2a6c JB |
2946 | if (ret) |
2947 | return -ENOSPC; | |
2948 | ||
2949 | /* step one, relocate all the extents inside this chunk */ | |
2ff7e61e | 2950 | btrfs_scrub_pause(fs_info); |
0b246afa | 2951 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
2ff7e61e | 2952 | btrfs_scrub_continue(fs_info); |
47ab2a6c JB |
2953 | if (ret) |
2954 | return ret; | |
2955 | ||
19c4d2f9 CM |
2956 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
2957 | chunk_offset); | |
2958 | if (IS_ERR(trans)) { | |
2959 | ret = PTR_ERR(trans); | |
2960 | btrfs_handle_fs_error(root->fs_info, ret, NULL); | |
2961 | return ret; | |
2962 | } | |
2963 | ||
47ab2a6c | 2964 | /* |
19c4d2f9 CM |
2965 | * step two, delete the device extents and the |
2966 | * chunk tree entries | |
47ab2a6c | 2967 | */ |
5b4aacef | 2968 | ret = btrfs_remove_chunk(trans, fs_info, chunk_offset); |
3a45bb20 | 2969 | btrfs_end_transaction(trans); |
19c4d2f9 | 2970 | return ret; |
2b82032c YZ |
2971 | } |
2972 | ||
2ff7e61e | 2973 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
2b82032c | 2974 | { |
0b246afa | 2975 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
2b82032c YZ |
2976 | struct btrfs_path *path; |
2977 | struct extent_buffer *leaf; | |
2978 | struct btrfs_chunk *chunk; | |
2979 | struct btrfs_key key; | |
2980 | struct btrfs_key found_key; | |
2b82032c | 2981 | u64 chunk_type; |
ba1bf481 JB |
2982 | bool retried = false; |
2983 | int failed = 0; | |
2b82032c YZ |
2984 | int ret; |
2985 | ||
2986 | path = btrfs_alloc_path(); | |
2987 | if (!path) | |
2988 | return -ENOMEM; | |
2989 | ||
ba1bf481 | 2990 | again: |
2b82032c YZ |
2991 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
2992 | key.offset = (u64)-1; | |
2993 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2994 | ||
2995 | while (1) { | |
0b246afa | 2996 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
2b82032c | 2997 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
67c5e7d4 | 2998 | if (ret < 0) { |
0b246afa | 2999 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
2b82032c | 3000 | goto error; |
67c5e7d4 | 3001 | } |
79787eaa | 3002 | BUG_ON(ret == 0); /* Corruption */ |
2b82032c YZ |
3003 | |
3004 | ret = btrfs_previous_item(chunk_root, path, key.objectid, | |
3005 | key.type); | |
67c5e7d4 | 3006 | if (ret) |
0b246afa | 3007 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
2b82032c YZ |
3008 | if (ret < 0) |
3009 | goto error; | |
3010 | if (ret > 0) | |
3011 | break; | |
1a40e23b | 3012 | |
2b82032c YZ |
3013 | leaf = path->nodes[0]; |
3014 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b | 3015 | |
2b82032c YZ |
3016 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
3017 | struct btrfs_chunk); | |
3018 | chunk_type = btrfs_chunk_type(leaf, chunk); | |
b3b4aa74 | 3019 | btrfs_release_path(path); |
8f18cf13 | 3020 | |
2b82032c | 3021 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
0b246afa | 3022 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
ba1bf481 JB |
3023 | if (ret == -ENOSPC) |
3024 | failed++; | |
14586651 HS |
3025 | else |
3026 | BUG_ON(ret); | |
2b82032c | 3027 | } |
0b246afa | 3028 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 | 3029 | |
2b82032c YZ |
3030 | if (found_key.offset == 0) |
3031 | break; | |
3032 | key.offset = found_key.offset - 1; | |
3033 | } | |
3034 | ret = 0; | |
ba1bf481 JB |
3035 | if (failed && !retried) { |
3036 | failed = 0; | |
3037 | retried = true; | |
3038 | goto again; | |
fae7f21c | 3039 | } else if (WARN_ON(failed && retried)) { |
ba1bf481 JB |
3040 | ret = -ENOSPC; |
3041 | } | |
2b82032c YZ |
3042 | error: |
3043 | btrfs_free_path(path); | |
3044 | return ret; | |
8f18cf13 CM |
3045 | } |
3046 | ||
6bccf3ab | 3047 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
0940ebf6 ID |
3048 | struct btrfs_balance_control *bctl) |
3049 | { | |
6bccf3ab | 3050 | struct btrfs_root *root = fs_info->tree_root; |
0940ebf6 ID |
3051 | struct btrfs_trans_handle *trans; |
3052 | struct btrfs_balance_item *item; | |
3053 | struct btrfs_disk_balance_args disk_bargs; | |
3054 | struct btrfs_path *path; | |
3055 | struct extent_buffer *leaf; | |
3056 | struct btrfs_key key; | |
3057 | int ret, err; | |
3058 | ||
3059 | path = btrfs_alloc_path(); | |
3060 | if (!path) | |
3061 | return -ENOMEM; | |
3062 | ||
3063 | trans = btrfs_start_transaction(root, 0); | |
3064 | if (IS_ERR(trans)) { | |
3065 | btrfs_free_path(path); | |
3066 | return PTR_ERR(trans); | |
3067 | } | |
3068 | ||
3069 | key.objectid = BTRFS_BALANCE_OBJECTID; | |
c479cb4f | 3070 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
0940ebf6 ID |
3071 | key.offset = 0; |
3072 | ||
3073 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
3074 | sizeof(*item)); | |
3075 | if (ret) | |
3076 | goto out; | |
3077 | ||
3078 | leaf = path->nodes[0]; | |
3079 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); | |
3080 | ||
b159fa28 | 3081 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
0940ebf6 ID |
3082 | |
3083 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); | |
3084 | btrfs_set_balance_data(leaf, item, &disk_bargs); | |
3085 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); | |
3086 | btrfs_set_balance_meta(leaf, item, &disk_bargs); | |
3087 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); | |
3088 | btrfs_set_balance_sys(leaf, item, &disk_bargs); | |
3089 | ||
3090 | btrfs_set_balance_flags(leaf, item, bctl->flags); | |
3091 | ||
3092 | btrfs_mark_buffer_dirty(leaf); | |
3093 | out: | |
3094 | btrfs_free_path(path); | |
3a45bb20 | 3095 | err = btrfs_commit_transaction(trans); |
0940ebf6 ID |
3096 | if (err && !ret) |
3097 | ret = err; | |
3098 | return ret; | |
3099 | } | |
3100 | ||
6bccf3ab | 3101 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
0940ebf6 | 3102 | { |
6bccf3ab | 3103 | struct btrfs_root *root = fs_info->tree_root; |
0940ebf6 ID |
3104 | struct btrfs_trans_handle *trans; |
3105 | struct btrfs_path *path; | |
3106 | struct btrfs_key key; | |
3107 | int ret, err; | |
3108 | ||
3109 | path = btrfs_alloc_path(); | |
3110 | if (!path) | |
3111 | return -ENOMEM; | |
3112 | ||
3113 | trans = btrfs_start_transaction(root, 0); | |
3114 | if (IS_ERR(trans)) { | |
3115 | btrfs_free_path(path); | |
3116 | return PTR_ERR(trans); | |
3117 | } | |
3118 | ||
3119 | key.objectid = BTRFS_BALANCE_OBJECTID; | |
c479cb4f | 3120 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
0940ebf6 ID |
3121 | key.offset = 0; |
3122 | ||
3123 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
3124 | if (ret < 0) | |
3125 | goto out; | |
3126 | if (ret > 0) { | |
3127 | ret = -ENOENT; | |
3128 | goto out; | |
3129 | } | |
3130 | ||
3131 | ret = btrfs_del_item(trans, root, path); | |
3132 | out: | |
3133 | btrfs_free_path(path); | |
3a45bb20 | 3134 | err = btrfs_commit_transaction(trans); |
0940ebf6 ID |
3135 | if (err && !ret) |
3136 | ret = err; | |
3137 | return ret; | |
3138 | } | |
3139 | ||
59641015 ID |
3140 | /* |
3141 | * This is a heuristic used to reduce the number of chunks balanced on | |
3142 | * resume after balance was interrupted. | |
3143 | */ | |
3144 | static void update_balance_args(struct btrfs_balance_control *bctl) | |
3145 | { | |
3146 | /* | |
3147 | * Turn on soft mode for chunk types that were being converted. | |
3148 | */ | |
3149 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3150 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3151 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3152 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3153 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3154 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3155 | ||
3156 | /* | |
3157 | * Turn on usage filter if is not already used. The idea is | |
3158 | * that chunks that we have already balanced should be | |
3159 | * reasonably full. Don't do it for chunks that are being | |
3160 | * converted - that will keep us from relocating unconverted | |
3161 | * (albeit full) chunks. | |
3162 | */ | |
3163 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3164 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3165 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3166 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3167 | bctl->data.usage = 90; | |
3168 | } | |
3169 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3170 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3171 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3172 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3173 | bctl->sys.usage = 90; | |
3174 | } | |
3175 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3176 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3177 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3178 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3179 | bctl->meta.usage = 90; | |
3180 | } | |
3181 | } | |
3182 | ||
c9e9f97b ID |
3183 | /* |
3184 | * Should be called with both balance and volume mutexes held to | |
3185 | * serialize other volume operations (add_dev/rm_dev/resize) with | |
3186 | * restriper. Same goes for unset_balance_control. | |
3187 | */ | |
3188 | static void set_balance_control(struct btrfs_balance_control *bctl) | |
3189 | { | |
3190 | struct btrfs_fs_info *fs_info = bctl->fs_info; | |
3191 | ||
3192 | BUG_ON(fs_info->balance_ctl); | |
3193 | ||
3194 | spin_lock(&fs_info->balance_lock); | |
3195 | fs_info->balance_ctl = bctl; | |
3196 | spin_unlock(&fs_info->balance_lock); | |
3197 | } | |
3198 | ||
3199 | static void unset_balance_control(struct btrfs_fs_info *fs_info) | |
3200 | { | |
3201 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
3202 | ||
3203 | BUG_ON(!fs_info->balance_ctl); | |
3204 | ||
3205 | spin_lock(&fs_info->balance_lock); | |
3206 | fs_info->balance_ctl = NULL; | |
3207 | spin_unlock(&fs_info->balance_lock); | |
3208 | ||
3209 | kfree(bctl); | |
3210 | } | |
3211 | ||
ed25e9b2 ID |
3212 | /* |
3213 | * Balance filters. Return 1 if chunk should be filtered out | |
3214 | * (should not be balanced). | |
3215 | */ | |
899c81ea | 3216 | static int chunk_profiles_filter(u64 chunk_type, |
ed25e9b2 ID |
3217 | struct btrfs_balance_args *bargs) |
3218 | { | |
899c81ea ID |
3219 | chunk_type = chunk_to_extended(chunk_type) & |
3220 | BTRFS_EXTENDED_PROFILE_MASK; | |
ed25e9b2 | 3221 | |
899c81ea | 3222 | if (bargs->profiles & chunk_type) |
ed25e9b2 ID |
3223 | return 0; |
3224 | ||
3225 | return 1; | |
3226 | } | |
3227 | ||
dba72cb3 | 3228 | static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
5ce5b3c0 | 3229 | struct btrfs_balance_args *bargs) |
bc309467 DS |
3230 | { |
3231 | struct btrfs_block_group_cache *cache; | |
3232 | u64 chunk_used; | |
3233 | u64 user_thresh_min; | |
3234 | u64 user_thresh_max; | |
3235 | int ret = 1; | |
3236 | ||
3237 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
3238 | chunk_used = btrfs_block_group_used(&cache->item); | |
3239 | ||
3240 | if (bargs->usage_min == 0) | |
3241 | user_thresh_min = 0; | |
3242 | else | |
3243 | user_thresh_min = div_factor_fine(cache->key.offset, | |
3244 | bargs->usage_min); | |
3245 | ||
3246 | if (bargs->usage_max == 0) | |
3247 | user_thresh_max = 1; | |
3248 | else if (bargs->usage_max > 100) | |
3249 | user_thresh_max = cache->key.offset; | |
3250 | else | |
3251 | user_thresh_max = div_factor_fine(cache->key.offset, | |
3252 | bargs->usage_max); | |
3253 | ||
3254 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) | |
3255 | ret = 0; | |
3256 | ||
3257 | btrfs_put_block_group(cache); | |
3258 | return ret; | |
3259 | } | |
3260 | ||
dba72cb3 | 3261 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
bc309467 | 3262 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
5ce5b3c0 ID |
3263 | { |
3264 | struct btrfs_block_group_cache *cache; | |
3265 | u64 chunk_used, user_thresh; | |
3266 | int ret = 1; | |
3267 | ||
3268 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
3269 | chunk_used = btrfs_block_group_used(&cache->item); | |
3270 | ||
bc309467 | 3271 | if (bargs->usage_min == 0) |
3e39cea6 | 3272 | user_thresh = 1; |
a105bb88 ID |
3273 | else if (bargs->usage > 100) |
3274 | user_thresh = cache->key.offset; | |
3275 | else | |
3276 | user_thresh = div_factor_fine(cache->key.offset, | |
3277 | bargs->usage); | |
3278 | ||
5ce5b3c0 ID |
3279 | if (chunk_used < user_thresh) |
3280 | ret = 0; | |
3281 | ||
3282 | btrfs_put_block_group(cache); | |
3283 | return ret; | |
3284 | } | |
3285 | ||
409d404b ID |
3286 | static int chunk_devid_filter(struct extent_buffer *leaf, |
3287 | struct btrfs_chunk *chunk, | |
3288 | struct btrfs_balance_args *bargs) | |
3289 | { | |
3290 | struct btrfs_stripe *stripe; | |
3291 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3292 | int i; | |
3293 | ||
3294 | for (i = 0; i < num_stripes; i++) { | |
3295 | stripe = btrfs_stripe_nr(chunk, i); | |
3296 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) | |
3297 | return 0; | |
3298 | } | |
3299 | ||
3300 | return 1; | |
3301 | } | |
3302 | ||
94e60d5a ID |
3303 | /* [pstart, pend) */ |
3304 | static int chunk_drange_filter(struct extent_buffer *leaf, | |
3305 | struct btrfs_chunk *chunk, | |
94e60d5a ID |
3306 | struct btrfs_balance_args *bargs) |
3307 | { | |
3308 | struct btrfs_stripe *stripe; | |
3309 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3310 | u64 stripe_offset; | |
3311 | u64 stripe_length; | |
3312 | int factor; | |
3313 | int i; | |
3314 | ||
3315 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) | |
3316 | return 0; | |
3317 | ||
3318 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | | |
53b381b3 DW |
3319 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
3320 | factor = num_stripes / 2; | |
3321 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { | |
3322 | factor = num_stripes - 1; | |
3323 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { | |
3324 | factor = num_stripes - 2; | |
3325 | } else { | |
3326 | factor = num_stripes; | |
3327 | } | |
94e60d5a ID |
3328 | |
3329 | for (i = 0; i < num_stripes; i++) { | |
3330 | stripe = btrfs_stripe_nr(chunk, i); | |
3331 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) | |
3332 | continue; | |
3333 | ||
3334 | stripe_offset = btrfs_stripe_offset(leaf, stripe); | |
3335 | stripe_length = btrfs_chunk_length(leaf, chunk); | |
b8b93add | 3336 | stripe_length = div_u64(stripe_length, factor); |
94e60d5a ID |
3337 | |
3338 | if (stripe_offset < bargs->pend && | |
3339 | stripe_offset + stripe_length > bargs->pstart) | |
3340 | return 0; | |
3341 | } | |
3342 | ||
3343 | return 1; | |
3344 | } | |
3345 | ||
ea67176a ID |
3346 | /* [vstart, vend) */ |
3347 | static int chunk_vrange_filter(struct extent_buffer *leaf, | |
3348 | struct btrfs_chunk *chunk, | |
3349 | u64 chunk_offset, | |
3350 | struct btrfs_balance_args *bargs) | |
3351 | { | |
3352 | if (chunk_offset < bargs->vend && | |
3353 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) | |
3354 | /* at least part of the chunk is inside this vrange */ | |
3355 | return 0; | |
3356 | ||
3357 | return 1; | |
3358 | } | |
3359 | ||
dee32d0a GAP |
3360 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
3361 | struct btrfs_chunk *chunk, | |
3362 | struct btrfs_balance_args *bargs) | |
3363 | { | |
3364 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3365 | ||
3366 | if (bargs->stripes_min <= num_stripes | |
3367 | && num_stripes <= bargs->stripes_max) | |
3368 | return 0; | |
3369 | ||
3370 | return 1; | |
3371 | } | |
3372 | ||
899c81ea | 3373 | static int chunk_soft_convert_filter(u64 chunk_type, |
cfa4c961 ID |
3374 | struct btrfs_balance_args *bargs) |
3375 | { | |
3376 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) | |
3377 | return 0; | |
3378 | ||
899c81ea ID |
3379 | chunk_type = chunk_to_extended(chunk_type) & |
3380 | BTRFS_EXTENDED_PROFILE_MASK; | |
cfa4c961 | 3381 | |
899c81ea | 3382 | if (bargs->target == chunk_type) |
cfa4c961 ID |
3383 | return 1; |
3384 | ||
3385 | return 0; | |
3386 | } | |
3387 | ||
2ff7e61e | 3388 | static int should_balance_chunk(struct btrfs_fs_info *fs_info, |
f43ffb60 ID |
3389 | struct extent_buffer *leaf, |
3390 | struct btrfs_chunk *chunk, u64 chunk_offset) | |
3391 | { | |
0b246afa | 3392 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
f43ffb60 ID |
3393 | struct btrfs_balance_args *bargs = NULL; |
3394 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); | |
3395 | ||
3396 | /* type filter */ | |
3397 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & | |
3398 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { | |
3399 | return 0; | |
3400 | } | |
3401 | ||
3402 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) | |
3403 | bargs = &bctl->data; | |
3404 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) | |
3405 | bargs = &bctl->sys; | |
3406 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) | |
3407 | bargs = &bctl->meta; | |
3408 | ||
ed25e9b2 ID |
3409 | /* profiles filter */ |
3410 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && | |
3411 | chunk_profiles_filter(chunk_type, bargs)) { | |
3412 | return 0; | |
5ce5b3c0 ID |
3413 | } |
3414 | ||
3415 | /* usage filter */ | |
3416 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && | |
0b246afa | 3417 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
5ce5b3c0 | 3418 | return 0; |
bc309467 | 3419 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
0b246afa | 3420 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
bc309467 | 3421 | return 0; |
409d404b ID |
3422 | } |
3423 | ||
3424 | /* devid filter */ | |
3425 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && | |
3426 | chunk_devid_filter(leaf, chunk, bargs)) { | |
3427 | return 0; | |
94e60d5a ID |
3428 | } |
3429 | ||
3430 | /* drange filter, makes sense only with devid filter */ | |
3431 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && | |
e4ff5fb5 | 3432 | chunk_drange_filter(leaf, chunk, bargs)) { |
94e60d5a | 3433 | return 0; |
ea67176a ID |
3434 | } |
3435 | ||
3436 | /* vrange filter */ | |
3437 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && | |
3438 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { | |
3439 | return 0; | |
ed25e9b2 ID |
3440 | } |
3441 | ||
dee32d0a GAP |
3442 | /* stripes filter */ |
3443 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && | |
3444 | chunk_stripes_range_filter(leaf, chunk, bargs)) { | |
3445 | return 0; | |
3446 | } | |
3447 | ||
cfa4c961 ID |
3448 | /* soft profile changing mode */ |
3449 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && | |
3450 | chunk_soft_convert_filter(chunk_type, bargs)) { | |
3451 | return 0; | |
3452 | } | |
3453 | ||
7d824b6f DS |
3454 | /* |
3455 | * limited by count, must be the last filter | |
3456 | */ | |
3457 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { | |
3458 | if (bargs->limit == 0) | |
3459 | return 0; | |
3460 | else | |
3461 | bargs->limit--; | |
12907fc7 DS |
3462 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
3463 | /* | |
3464 | * Same logic as the 'limit' filter; the minimum cannot be | |
01327610 | 3465 | * determined here because we do not have the global information |
12907fc7 DS |
3466 | * about the count of all chunks that satisfy the filters. |
3467 | */ | |
3468 | if (bargs->limit_max == 0) | |
3469 | return 0; | |
3470 | else | |
3471 | bargs->limit_max--; | |
7d824b6f DS |
3472 | } |
3473 | ||
f43ffb60 ID |
3474 | return 1; |
3475 | } | |
3476 | ||
c9e9f97b | 3477 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
ec44a35c | 3478 | { |
19a39dce | 3479 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
c9e9f97b ID |
3480 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
3481 | struct btrfs_root *dev_root = fs_info->dev_root; | |
3482 | struct list_head *devices; | |
ec44a35c CM |
3483 | struct btrfs_device *device; |
3484 | u64 old_size; | |
3485 | u64 size_to_free; | |
12907fc7 | 3486 | u64 chunk_type; |
f43ffb60 | 3487 | struct btrfs_chunk *chunk; |
5a488b9d | 3488 | struct btrfs_path *path = NULL; |
ec44a35c | 3489 | struct btrfs_key key; |
ec44a35c | 3490 | struct btrfs_key found_key; |
c9e9f97b | 3491 | struct btrfs_trans_handle *trans; |
f43ffb60 ID |
3492 | struct extent_buffer *leaf; |
3493 | int slot; | |
c9e9f97b ID |
3494 | int ret; |
3495 | int enospc_errors = 0; | |
19a39dce | 3496 | bool counting = true; |
12907fc7 | 3497 | /* The single value limit and min/max limits use the same bytes in the */ |
7d824b6f DS |
3498 | u64 limit_data = bctl->data.limit; |
3499 | u64 limit_meta = bctl->meta.limit; | |
3500 | u64 limit_sys = bctl->sys.limit; | |
12907fc7 DS |
3501 | u32 count_data = 0; |
3502 | u32 count_meta = 0; | |
3503 | u32 count_sys = 0; | |
2c9fe835 | 3504 | int chunk_reserved = 0; |
cf25ce51 | 3505 | u64 bytes_used = 0; |
ec44a35c | 3506 | |
ec44a35c | 3507 | /* step one make some room on all the devices */ |
c9e9f97b | 3508 | devices = &fs_info->fs_devices->devices; |
c6e30871 | 3509 | list_for_each_entry(device, devices, dev_list) { |
7cc8e58d | 3510 | old_size = btrfs_device_get_total_bytes(device); |
ec44a35c | 3511 | size_to_free = div_factor(old_size, 1); |
ee22184b | 3512 | size_to_free = min_t(u64, size_to_free, SZ_1M); |
2b82032c | 3513 | if (!device->writeable || |
7cc8e58d MX |
3514 | btrfs_device_get_total_bytes(device) - |
3515 | btrfs_device_get_bytes_used(device) > size_to_free || | |
63a212ab | 3516 | device->is_tgtdev_for_dev_replace) |
ec44a35c CM |
3517 | continue; |
3518 | ||
3519 | ret = btrfs_shrink_device(device, old_size - size_to_free); | |
ba1bf481 JB |
3520 | if (ret == -ENOSPC) |
3521 | break; | |
5a488b9d LB |
3522 | if (ret) { |
3523 | /* btrfs_shrink_device never returns ret > 0 */ | |
3524 | WARN_ON(ret > 0); | |
3525 | goto error; | |
3526 | } | |
ec44a35c | 3527 | |
a22285a6 | 3528 | trans = btrfs_start_transaction(dev_root, 0); |
5a488b9d LB |
3529 | if (IS_ERR(trans)) { |
3530 | ret = PTR_ERR(trans); | |
3531 | btrfs_info_in_rcu(fs_info, | |
3532 | "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu", | |
3533 | rcu_str_deref(device->name), ret, | |
3534 | old_size, old_size - size_to_free); | |
3535 | goto error; | |
3536 | } | |
ec44a35c CM |
3537 | |
3538 | ret = btrfs_grow_device(trans, device, old_size); | |
5a488b9d | 3539 | if (ret) { |
3a45bb20 | 3540 | btrfs_end_transaction(trans); |
5a488b9d LB |
3541 | /* btrfs_grow_device never returns ret > 0 */ |
3542 | WARN_ON(ret > 0); | |
3543 | btrfs_info_in_rcu(fs_info, | |
3544 | "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu", | |
3545 | rcu_str_deref(device->name), ret, | |
3546 | old_size, old_size - size_to_free); | |
3547 | goto error; | |
3548 | } | |
ec44a35c | 3549 | |
3a45bb20 | 3550 | btrfs_end_transaction(trans); |
ec44a35c CM |
3551 | } |
3552 | ||
3553 | /* step two, relocate all the chunks */ | |
3554 | path = btrfs_alloc_path(); | |
17e9f796 MF |
3555 | if (!path) { |
3556 | ret = -ENOMEM; | |
3557 | goto error; | |
3558 | } | |
19a39dce ID |
3559 | |
3560 | /* zero out stat counters */ | |
3561 | spin_lock(&fs_info->balance_lock); | |
3562 | memset(&bctl->stat, 0, sizeof(bctl->stat)); | |
3563 | spin_unlock(&fs_info->balance_lock); | |
3564 | again: | |
7d824b6f | 3565 | if (!counting) { |
12907fc7 DS |
3566 | /* |
3567 | * The single value limit and min/max limits use the same bytes | |
3568 | * in the | |
3569 | */ | |
7d824b6f DS |
3570 | bctl->data.limit = limit_data; |
3571 | bctl->meta.limit = limit_meta; | |
3572 | bctl->sys.limit = limit_sys; | |
3573 | } | |
ec44a35c CM |
3574 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
3575 | key.offset = (u64)-1; | |
3576 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
3577 | ||
d397712b | 3578 | while (1) { |
19a39dce | 3579 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
a7e99c69 | 3580 | atomic_read(&fs_info->balance_cancel_req)) { |
837d5b6e ID |
3581 | ret = -ECANCELED; |
3582 | goto error; | |
3583 | } | |
3584 | ||
67c5e7d4 | 3585 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
ec44a35c | 3586 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
67c5e7d4 FM |
3587 | if (ret < 0) { |
3588 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
ec44a35c | 3589 | goto error; |
67c5e7d4 | 3590 | } |
ec44a35c CM |
3591 | |
3592 | /* | |
3593 | * this shouldn't happen, it means the last relocate | |
3594 | * failed | |
3595 | */ | |
3596 | if (ret == 0) | |
c9e9f97b | 3597 | BUG(); /* FIXME break ? */ |
ec44a35c CM |
3598 | |
3599 | ret = btrfs_previous_item(chunk_root, path, 0, | |
3600 | BTRFS_CHUNK_ITEM_KEY); | |
c9e9f97b | 3601 | if (ret) { |
67c5e7d4 | 3602 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
c9e9f97b | 3603 | ret = 0; |
ec44a35c | 3604 | break; |
c9e9f97b | 3605 | } |
7d9eb12c | 3606 | |
f43ffb60 ID |
3607 | leaf = path->nodes[0]; |
3608 | slot = path->slots[0]; | |
3609 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
7d9eb12c | 3610 | |
67c5e7d4 FM |
3611 | if (found_key.objectid != key.objectid) { |
3612 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
ec44a35c | 3613 | break; |
67c5e7d4 | 3614 | } |
7d9eb12c | 3615 | |
f43ffb60 | 3616 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
12907fc7 | 3617 | chunk_type = btrfs_chunk_type(leaf, chunk); |
f43ffb60 | 3618 | |
19a39dce ID |
3619 | if (!counting) { |
3620 | spin_lock(&fs_info->balance_lock); | |
3621 | bctl->stat.considered++; | |
3622 | spin_unlock(&fs_info->balance_lock); | |
3623 | } | |
3624 | ||
2ff7e61e | 3625 | ret = should_balance_chunk(fs_info, leaf, chunk, |
f43ffb60 | 3626 | found_key.offset); |
2c9fe835 | 3627 | |
b3b4aa74 | 3628 | btrfs_release_path(path); |
67c5e7d4 FM |
3629 | if (!ret) { |
3630 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
f43ffb60 | 3631 | goto loop; |
67c5e7d4 | 3632 | } |
f43ffb60 | 3633 | |
19a39dce | 3634 | if (counting) { |
67c5e7d4 | 3635 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
19a39dce ID |
3636 | spin_lock(&fs_info->balance_lock); |
3637 | bctl->stat.expected++; | |
3638 | spin_unlock(&fs_info->balance_lock); | |
12907fc7 DS |
3639 | |
3640 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) | |
3641 | count_data++; | |
3642 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) | |
3643 | count_sys++; | |
3644 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) | |
3645 | count_meta++; | |
3646 | ||
3647 | goto loop; | |
3648 | } | |
3649 | ||
3650 | /* | |
3651 | * Apply limit_min filter, no need to check if the LIMITS | |
3652 | * filter is used, limit_min is 0 by default | |
3653 | */ | |
3654 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && | |
3655 | count_data < bctl->data.limit_min) | |
3656 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && | |
3657 | count_meta < bctl->meta.limit_min) | |
3658 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && | |
3659 | count_sys < bctl->sys.limit_min)) { | |
3660 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
19a39dce ID |
3661 | goto loop; |
3662 | } | |
3663 | ||
cf25ce51 LB |
3664 | ASSERT(fs_info->data_sinfo); |
3665 | spin_lock(&fs_info->data_sinfo->lock); | |
3666 | bytes_used = fs_info->data_sinfo->bytes_used; | |
3667 | spin_unlock(&fs_info->data_sinfo->lock); | |
3668 | ||
3669 | if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) && | |
3670 | !chunk_reserved && !bytes_used) { | |
2c9fe835 ZL |
3671 | trans = btrfs_start_transaction(chunk_root, 0); |
3672 | if (IS_ERR(trans)) { | |
3673 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
3674 | ret = PTR_ERR(trans); | |
3675 | goto error; | |
3676 | } | |
3677 | ||
2ff7e61e | 3678 | ret = btrfs_force_chunk_alloc(trans, fs_info, |
2c9fe835 | 3679 | BTRFS_BLOCK_GROUP_DATA); |
3a45bb20 | 3680 | btrfs_end_transaction(trans); |
2c9fe835 ZL |
3681 | if (ret < 0) { |
3682 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
3683 | goto error; | |
3684 | } | |
2c9fe835 ZL |
3685 | chunk_reserved = 1; |
3686 | } | |
3687 | ||
5b4aacef | 3688 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
67c5e7d4 | 3689 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
508794eb JB |
3690 | if (ret && ret != -ENOSPC) |
3691 | goto error; | |
19a39dce | 3692 | if (ret == -ENOSPC) { |
c9e9f97b | 3693 | enospc_errors++; |
19a39dce ID |
3694 | } else { |
3695 | spin_lock(&fs_info->balance_lock); | |
3696 | bctl->stat.completed++; | |
3697 | spin_unlock(&fs_info->balance_lock); | |
3698 | } | |
f43ffb60 | 3699 | loop: |
795a3321 ID |
3700 | if (found_key.offset == 0) |
3701 | break; | |
ba1bf481 | 3702 | key.offset = found_key.offset - 1; |
ec44a35c | 3703 | } |
c9e9f97b | 3704 | |
19a39dce ID |
3705 | if (counting) { |
3706 | btrfs_release_path(path); | |
3707 | counting = false; | |
3708 | goto again; | |
3709 | } | |
ec44a35c CM |
3710 | error: |
3711 | btrfs_free_path(path); | |
c9e9f97b | 3712 | if (enospc_errors) { |
efe120a0 | 3713 | btrfs_info(fs_info, "%d enospc errors during balance", |
5d163e0e | 3714 | enospc_errors); |
c9e9f97b ID |
3715 | if (!ret) |
3716 | ret = -ENOSPC; | |
3717 | } | |
3718 | ||
ec44a35c CM |
3719 | return ret; |
3720 | } | |
3721 | ||
0c460c0d ID |
3722 | /** |
3723 | * alloc_profile_is_valid - see if a given profile is valid and reduced | |
3724 | * @flags: profile to validate | |
3725 | * @extended: if true @flags is treated as an extended profile | |
3726 | */ | |
3727 | static int alloc_profile_is_valid(u64 flags, int extended) | |
3728 | { | |
3729 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : | |
3730 | BTRFS_BLOCK_GROUP_PROFILE_MASK); | |
3731 | ||
3732 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; | |
3733 | ||
3734 | /* 1) check that all other bits are zeroed */ | |
3735 | if (flags & ~mask) | |
3736 | return 0; | |
3737 | ||
3738 | /* 2) see if profile is reduced */ | |
3739 | if (flags == 0) | |
3740 | return !extended; /* "0" is valid for usual profiles */ | |
3741 | ||
3742 | /* true if exactly one bit set */ | |
3743 | return (flags & (flags - 1)) == 0; | |
3744 | } | |
3745 | ||
837d5b6e ID |
3746 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
3747 | { | |
a7e99c69 ID |
3748 | /* cancel requested || normal exit path */ |
3749 | return atomic_read(&fs_info->balance_cancel_req) || | |
3750 | (atomic_read(&fs_info->balance_pause_req) == 0 && | |
3751 | atomic_read(&fs_info->balance_cancel_req) == 0); | |
837d5b6e ID |
3752 | } |
3753 | ||
c9e9f97b ID |
3754 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
3755 | { | |
0940ebf6 ID |
3756 | int ret; |
3757 | ||
c9e9f97b | 3758 | unset_balance_control(fs_info); |
6bccf3ab | 3759 | ret = del_balance_item(fs_info); |
0f788c58 | 3760 | if (ret) |
34d97007 | 3761 | btrfs_handle_fs_error(fs_info, ret, NULL); |
ed0fb78f | 3762 | |
171938e5 | 3763 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
c9e9f97b ID |
3764 | } |
3765 | ||
bdcd3c97 AM |
3766 | /* Non-zero return value signifies invalidity */ |
3767 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, | |
3768 | u64 allowed) | |
3769 | { | |
3770 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
3771 | (!alloc_profile_is_valid(bctl_arg->target, 1) || | |
3772 | (bctl_arg->target & ~allowed))); | |
3773 | } | |
3774 | ||
c9e9f97b ID |
3775 | /* |
3776 | * Should be called with both balance and volume mutexes held | |
3777 | */ | |
3778 | int btrfs_balance(struct btrfs_balance_control *bctl, | |
3779 | struct btrfs_ioctl_balance_args *bargs) | |
3780 | { | |
3781 | struct btrfs_fs_info *fs_info = bctl->fs_info; | |
14506127 | 3782 | u64 meta_target, data_target; |
f43ffb60 | 3783 | u64 allowed; |
e4837f8f | 3784 | int mixed = 0; |
c9e9f97b | 3785 | int ret; |
8dabb742 | 3786 | u64 num_devices; |
de98ced9 | 3787 | unsigned seq; |
c9e9f97b | 3788 | |
837d5b6e | 3789 | if (btrfs_fs_closing(fs_info) || |
a7e99c69 ID |
3790 | atomic_read(&fs_info->balance_pause_req) || |
3791 | atomic_read(&fs_info->balance_cancel_req)) { | |
c9e9f97b ID |
3792 | ret = -EINVAL; |
3793 | goto out; | |
3794 | } | |
3795 | ||
e4837f8f ID |
3796 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
3797 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) | |
3798 | mixed = 1; | |
3799 | ||
f43ffb60 ID |
3800 | /* |
3801 | * In case of mixed groups both data and meta should be picked, | |
3802 | * and identical options should be given for both of them. | |
3803 | */ | |
e4837f8f ID |
3804 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
3805 | if (mixed && (bctl->flags & allowed)) { | |
f43ffb60 ID |
3806 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
3807 | !(bctl->flags & BTRFS_BALANCE_METADATA) || | |
3808 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { | |
5d163e0e JM |
3809 | btrfs_err(fs_info, |
3810 | "with mixed groups data and metadata balance options must be the same"); | |
f43ffb60 ID |
3811 | ret = -EINVAL; |
3812 | goto out; | |
3813 | } | |
3814 | } | |
3815 | ||
8dabb742 | 3816 | num_devices = fs_info->fs_devices->num_devices; |
73beece9 | 3817 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
8dabb742 SB |
3818 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
3819 | BUG_ON(num_devices < 1); | |
3820 | num_devices--; | |
3821 | } | |
73beece9 | 3822 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
88be159c AH |
3823 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP; |
3824 | if (num_devices > 1) | |
e4d8ec0f | 3825 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
8250dabe AP |
3826 | if (num_devices > 2) |
3827 | allowed |= BTRFS_BLOCK_GROUP_RAID5; | |
3828 | if (num_devices > 3) | |
3829 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | | |
3830 | BTRFS_BLOCK_GROUP_RAID6); | |
bdcd3c97 | 3831 | if (validate_convert_profile(&bctl->data, allowed)) { |
5d163e0e JM |
3832 | btrfs_err(fs_info, |
3833 | "unable to start balance with target data profile %llu", | |
3834 | bctl->data.target); | |
e4d8ec0f ID |
3835 | ret = -EINVAL; |
3836 | goto out; | |
3837 | } | |
bdcd3c97 | 3838 | if (validate_convert_profile(&bctl->meta, allowed)) { |
efe120a0 | 3839 | btrfs_err(fs_info, |
5d163e0e JM |
3840 | "unable to start balance with target metadata profile %llu", |
3841 | bctl->meta.target); | |
e4d8ec0f ID |
3842 | ret = -EINVAL; |
3843 | goto out; | |
3844 | } | |
bdcd3c97 | 3845 | if (validate_convert_profile(&bctl->sys, allowed)) { |
efe120a0 | 3846 | btrfs_err(fs_info, |
5d163e0e JM |
3847 | "unable to start balance with target system profile %llu", |
3848 | bctl->sys.target); | |
e4d8ec0f ID |
3849 | ret = -EINVAL; |
3850 | goto out; | |
3851 | } | |
3852 | ||
e4d8ec0f ID |
3853 | /* allow to reduce meta or sys integrity only if force set */ |
3854 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | | |
53b381b3 DW |
3855 | BTRFS_BLOCK_GROUP_RAID10 | |
3856 | BTRFS_BLOCK_GROUP_RAID5 | | |
3857 | BTRFS_BLOCK_GROUP_RAID6; | |
de98ced9 MX |
3858 | do { |
3859 | seq = read_seqbegin(&fs_info->profiles_lock); | |
3860 | ||
3861 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
3862 | (fs_info->avail_system_alloc_bits & allowed) && | |
3863 | !(bctl->sys.target & allowed)) || | |
3864 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
3865 | (fs_info->avail_metadata_alloc_bits & allowed) && | |
3866 | !(bctl->meta.target & allowed))) { | |
3867 | if (bctl->flags & BTRFS_BALANCE_FORCE) { | |
5d163e0e JM |
3868 | btrfs_info(fs_info, |
3869 | "force reducing metadata integrity"); | |
de98ced9 | 3870 | } else { |
5d163e0e JM |
3871 | btrfs_err(fs_info, |
3872 | "balance will reduce metadata integrity, use force if you want this"); | |
de98ced9 MX |
3873 | ret = -EINVAL; |
3874 | goto out; | |
3875 | } | |
e4d8ec0f | 3876 | } |
de98ced9 | 3877 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
e4d8ec0f | 3878 | |
14506127 AB |
3879 | /* if we're not converting, the target field is uninitialized */ |
3880 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? | |
3881 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; | |
3882 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? | |
3883 | bctl->data.target : fs_info->avail_data_alloc_bits; | |
3884 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < | |
3885 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { | |
ee592d07 | 3886 | btrfs_warn(fs_info, |
5d163e0e | 3887 | "metadata profile 0x%llx has lower redundancy than data profile 0x%llx", |
14506127 | 3888 | meta_target, data_target); |
ee592d07 ST |
3889 | } |
3890 | ||
6bccf3ab | 3891 | ret = insert_balance_item(fs_info, bctl); |
59641015 | 3892 | if (ret && ret != -EEXIST) |
0940ebf6 ID |
3893 | goto out; |
3894 | ||
59641015 ID |
3895 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
3896 | BUG_ON(ret == -EEXIST); | |
3897 | set_balance_control(bctl); | |
3898 | } else { | |
3899 | BUG_ON(ret != -EEXIST); | |
3900 | spin_lock(&fs_info->balance_lock); | |
3901 | update_balance_args(bctl); | |
3902 | spin_unlock(&fs_info->balance_lock); | |
3903 | } | |
c9e9f97b | 3904 | |
837d5b6e | 3905 | atomic_inc(&fs_info->balance_running); |
c9e9f97b ID |
3906 | mutex_unlock(&fs_info->balance_mutex); |
3907 | ||
3908 | ret = __btrfs_balance(fs_info); | |
3909 | ||
3910 | mutex_lock(&fs_info->balance_mutex); | |
837d5b6e | 3911 | atomic_dec(&fs_info->balance_running); |
c9e9f97b ID |
3912 | |
3913 | if (bargs) { | |
3914 | memset(bargs, 0, sizeof(*bargs)); | |
19a39dce | 3915 | update_ioctl_balance_args(fs_info, 0, bargs); |
c9e9f97b ID |
3916 | } |
3917 | ||
3a01aa7a ID |
3918 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
3919 | balance_need_close(fs_info)) { | |
3920 | __cancel_balance(fs_info); | |
3921 | } | |
3922 | ||
837d5b6e | 3923 | wake_up(&fs_info->balance_wait_q); |
c9e9f97b ID |
3924 | |
3925 | return ret; | |
3926 | out: | |
59641015 ID |
3927 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
3928 | __cancel_balance(fs_info); | |
ed0fb78f | 3929 | else { |
59641015 | 3930 | kfree(bctl); |
171938e5 | 3931 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
ed0fb78f | 3932 | } |
59641015 ID |
3933 | return ret; |
3934 | } | |
3935 | ||
3936 | static int balance_kthread(void *data) | |
3937 | { | |
2b6ba629 | 3938 | struct btrfs_fs_info *fs_info = data; |
9555c6c1 | 3939 | int ret = 0; |
59641015 ID |
3940 | |
3941 | mutex_lock(&fs_info->volume_mutex); | |
3942 | mutex_lock(&fs_info->balance_mutex); | |
3943 | ||
2b6ba629 | 3944 | if (fs_info->balance_ctl) { |
efe120a0 | 3945 | btrfs_info(fs_info, "continuing balance"); |
2b6ba629 | 3946 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
9555c6c1 | 3947 | } |
59641015 ID |
3948 | |
3949 | mutex_unlock(&fs_info->balance_mutex); | |
3950 | mutex_unlock(&fs_info->volume_mutex); | |
2b6ba629 | 3951 | |
59641015 ID |
3952 | return ret; |
3953 | } | |
3954 | ||
2b6ba629 ID |
3955 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
3956 | { | |
3957 | struct task_struct *tsk; | |
3958 | ||
3959 | spin_lock(&fs_info->balance_lock); | |
3960 | if (!fs_info->balance_ctl) { | |
3961 | spin_unlock(&fs_info->balance_lock); | |
3962 | return 0; | |
3963 | } | |
3964 | spin_unlock(&fs_info->balance_lock); | |
3965 | ||
3cdde224 | 3966 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
efe120a0 | 3967 | btrfs_info(fs_info, "force skipping balance"); |
2b6ba629 ID |
3968 | return 0; |
3969 | } | |
3970 | ||
1556b8d3 AJ |
3971 | /* |
3972 | * A ro->rw remount sequence should continue with the paused balance | |
3973 | * regardless of who pauses it, system or the user as of now, so set | |
3974 | * the resume flag. | |
3975 | */ | |
3976 | spin_lock(&fs_info->balance_lock); | |
3977 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; | |
3978 | spin_unlock(&fs_info->balance_lock); | |
3979 | ||
2b6ba629 | 3980 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
cd633972 | 3981 | return PTR_ERR_OR_ZERO(tsk); |
2b6ba629 ID |
3982 | } |
3983 | ||
68310a5e | 3984 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
59641015 | 3985 | { |
59641015 ID |
3986 | struct btrfs_balance_control *bctl; |
3987 | struct btrfs_balance_item *item; | |
3988 | struct btrfs_disk_balance_args disk_bargs; | |
3989 | struct btrfs_path *path; | |
3990 | struct extent_buffer *leaf; | |
3991 | struct btrfs_key key; | |
3992 | int ret; | |
3993 | ||
3994 | path = btrfs_alloc_path(); | |
3995 | if (!path) | |
3996 | return -ENOMEM; | |
3997 | ||
59641015 | 3998 | key.objectid = BTRFS_BALANCE_OBJECTID; |
c479cb4f | 3999 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
59641015 ID |
4000 | key.offset = 0; |
4001 | ||
68310a5e | 4002 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
59641015 | 4003 | if (ret < 0) |
68310a5e | 4004 | goto out; |
59641015 ID |
4005 | if (ret > 0) { /* ret = -ENOENT; */ |
4006 | ret = 0; | |
68310a5e ID |
4007 | goto out; |
4008 | } | |
4009 | ||
4010 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); | |
4011 | if (!bctl) { | |
4012 | ret = -ENOMEM; | |
4013 | goto out; | |
59641015 ID |
4014 | } |
4015 | ||
4016 | leaf = path->nodes[0]; | |
4017 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); | |
4018 | ||
68310a5e ID |
4019 | bctl->fs_info = fs_info; |
4020 | bctl->flags = btrfs_balance_flags(leaf, item); | |
4021 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
59641015 ID |
4022 | |
4023 | btrfs_balance_data(leaf, item, &disk_bargs); | |
4024 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); | |
4025 | btrfs_balance_meta(leaf, item, &disk_bargs); | |
4026 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); | |
4027 | btrfs_balance_sys(leaf, item, &disk_bargs); | |
4028 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); | |
4029 | ||
171938e5 | 4030 | WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
ed0fb78f | 4031 | |
68310a5e ID |
4032 | mutex_lock(&fs_info->volume_mutex); |
4033 | mutex_lock(&fs_info->balance_mutex); | |
59641015 | 4034 | |
68310a5e ID |
4035 | set_balance_control(bctl); |
4036 | ||
4037 | mutex_unlock(&fs_info->balance_mutex); | |
4038 | mutex_unlock(&fs_info->volume_mutex); | |
59641015 ID |
4039 | out: |
4040 | btrfs_free_path(path); | |
ec44a35c CM |
4041 | return ret; |
4042 | } | |
4043 | ||
837d5b6e ID |
4044 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
4045 | { | |
4046 | int ret = 0; | |
4047 | ||
4048 | mutex_lock(&fs_info->balance_mutex); | |
4049 | if (!fs_info->balance_ctl) { | |
4050 | mutex_unlock(&fs_info->balance_mutex); | |
4051 | return -ENOTCONN; | |
4052 | } | |
4053 | ||
4054 | if (atomic_read(&fs_info->balance_running)) { | |
4055 | atomic_inc(&fs_info->balance_pause_req); | |
4056 | mutex_unlock(&fs_info->balance_mutex); | |
4057 | ||
4058 | wait_event(fs_info->balance_wait_q, | |
4059 | atomic_read(&fs_info->balance_running) == 0); | |
4060 | ||
4061 | mutex_lock(&fs_info->balance_mutex); | |
4062 | /* we are good with balance_ctl ripped off from under us */ | |
4063 | BUG_ON(atomic_read(&fs_info->balance_running)); | |
4064 | atomic_dec(&fs_info->balance_pause_req); | |
4065 | } else { | |
4066 | ret = -ENOTCONN; | |
4067 | } | |
4068 | ||
4069 | mutex_unlock(&fs_info->balance_mutex); | |
4070 | return ret; | |
4071 | } | |
4072 | ||
a7e99c69 ID |
4073 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
4074 | { | |
bc98a42c | 4075 | if (sb_rdonly(fs_info->sb)) |
e649e587 ID |
4076 | return -EROFS; |
4077 | ||
a7e99c69 ID |
4078 | mutex_lock(&fs_info->balance_mutex); |
4079 | if (!fs_info->balance_ctl) { | |
4080 | mutex_unlock(&fs_info->balance_mutex); | |
4081 | return -ENOTCONN; | |
4082 | } | |
4083 | ||
4084 | atomic_inc(&fs_info->balance_cancel_req); | |
4085 | /* | |
4086 | * if we are running just wait and return, balance item is | |
4087 | * deleted in btrfs_balance in this case | |
4088 | */ | |
4089 | if (atomic_read(&fs_info->balance_running)) { | |
4090 | mutex_unlock(&fs_info->balance_mutex); | |
4091 | wait_event(fs_info->balance_wait_q, | |
4092 | atomic_read(&fs_info->balance_running) == 0); | |
4093 | mutex_lock(&fs_info->balance_mutex); | |
4094 | } else { | |
4095 | /* __cancel_balance needs volume_mutex */ | |
4096 | mutex_unlock(&fs_info->balance_mutex); | |
4097 | mutex_lock(&fs_info->volume_mutex); | |
4098 | mutex_lock(&fs_info->balance_mutex); | |
4099 | ||
4100 | if (fs_info->balance_ctl) | |
4101 | __cancel_balance(fs_info); | |
4102 | ||
4103 | mutex_unlock(&fs_info->volume_mutex); | |
4104 | } | |
4105 | ||
4106 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); | |
4107 | atomic_dec(&fs_info->balance_cancel_req); | |
4108 | mutex_unlock(&fs_info->balance_mutex); | |
4109 | return 0; | |
4110 | } | |
4111 | ||
803b2f54 SB |
4112 | static int btrfs_uuid_scan_kthread(void *data) |
4113 | { | |
4114 | struct btrfs_fs_info *fs_info = data; | |
4115 | struct btrfs_root *root = fs_info->tree_root; | |
4116 | struct btrfs_key key; | |
803b2f54 SB |
4117 | struct btrfs_path *path = NULL; |
4118 | int ret = 0; | |
4119 | struct extent_buffer *eb; | |
4120 | int slot; | |
4121 | struct btrfs_root_item root_item; | |
4122 | u32 item_size; | |
f45388f3 | 4123 | struct btrfs_trans_handle *trans = NULL; |
803b2f54 SB |
4124 | |
4125 | path = btrfs_alloc_path(); | |
4126 | if (!path) { | |
4127 | ret = -ENOMEM; | |
4128 | goto out; | |
4129 | } | |
4130 | ||
4131 | key.objectid = 0; | |
4132 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4133 | key.offset = 0; | |
4134 | ||
803b2f54 | 4135 | while (1) { |
6174d3cb | 4136 | ret = btrfs_search_forward(root, &key, path, 0); |
803b2f54 SB |
4137 | if (ret) { |
4138 | if (ret > 0) | |
4139 | ret = 0; | |
4140 | break; | |
4141 | } | |
4142 | ||
4143 | if (key.type != BTRFS_ROOT_ITEM_KEY || | |
4144 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && | |
4145 | key.objectid != BTRFS_FS_TREE_OBJECTID) || | |
4146 | key.objectid > BTRFS_LAST_FREE_OBJECTID) | |
4147 | goto skip; | |
4148 | ||
4149 | eb = path->nodes[0]; | |
4150 | slot = path->slots[0]; | |
4151 | item_size = btrfs_item_size_nr(eb, slot); | |
4152 | if (item_size < sizeof(root_item)) | |
4153 | goto skip; | |
4154 | ||
803b2f54 SB |
4155 | read_extent_buffer(eb, &root_item, |
4156 | btrfs_item_ptr_offset(eb, slot), | |
4157 | (int)sizeof(root_item)); | |
4158 | if (btrfs_root_refs(&root_item) == 0) | |
4159 | goto skip; | |
f45388f3 FDBM |
4160 | |
4161 | if (!btrfs_is_empty_uuid(root_item.uuid) || | |
4162 | !btrfs_is_empty_uuid(root_item.received_uuid)) { | |
4163 | if (trans) | |
4164 | goto update_tree; | |
4165 | ||
4166 | btrfs_release_path(path); | |
803b2f54 SB |
4167 | /* |
4168 | * 1 - subvol uuid item | |
4169 | * 1 - received_subvol uuid item | |
4170 | */ | |
4171 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); | |
4172 | if (IS_ERR(trans)) { | |
4173 | ret = PTR_ERR(trans); | |
4174 | break; | |
4175 | } | |
f45388f3 FDBM |
4176 | continue; |
4177 | } else { | |
4178 | goto skip; | |
4179 | } | |
4180 | update_tree: | |
4181 | if (!btrfs_is_empty_uuid(root_item.uuid)) { | |
6bccf3ab | 4182 | ret = btrfs_uuid_tree_add(trans, fs_info, |
803b2f54 SB |
4183 | root_item.uuid, |
4184 | BTRFS_UUID_KEY_SUBVOL, | |
4185 | key.objectid); | |
4186 | if (ret < 0) { | |
efe120a0 | 4187 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
803b2f54 | 4188 | ret); |
803b2f54 SB |
4189 | break; |
4190 | } | |
4191 | } | |
4192 | ||
4193 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { | |
6bccf3ab | 4194 | ret = btrfs_uuid_tree_add(trans, fs_info, |
803b2f54 SB |
4195 | root_item.received_uuid, |
4196 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4197 | key.objectid); | |
4198 | if (ret < 0) { | |
efe120a0 | 4199 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
803b2f54 | 4200 | ret); |
803b2f54 SB |
4201 | break; |
4202 | } | |
4203 | } | |
4204 | ||
f45388f3 | 4205 | skip: |
803b2f54 | 4206 | if (trans) { |
3a45bb20 | 4207 | ret = btrfs_end_transaction(trans); |
f45388f3 | 4208 | trans = NULL; |
803b2f54 SB |
4209 | if (ret) |
4210 | break; | |
4211 | } | |
4212 | ||
803b2f54 SB |
4213 | btrfs_release_path(path); |
4214 | if (key.offset < (u64)-1) { | |
4215 | key.offset++; | |
4216 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { | |
4217 | key.offset = 0; | |
4218 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4219 | } else if (key.objectid < (u64)-1) { | |
4220 | key.offset = 0; | |
4221 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4222 | key.objectid++; | |
4223 | } else { | |
4224 | break; | |
4225 | } | |
4226 | cond_resched(); | |
4227 | } | |
4228 | ||
4229 | out: | |
4230 | btrfs_free_path(path); | |
f45388f3 | 4231 | if (trans && !IS_ERR(trans)) |
3a45bb20 | 4232 | btrfs_end_transaction(trans); |
803b2f54 | 4233 | if (ret) |
efe120a0 | 4234 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
70f80175 | 4235 | else |
afcdd129 | 4236 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
803b2f54 SB |
4237 | up(&fs_info->uuid_tree_rescan_sem); |
4238 | return 0; | |
4239 | } | |
4240 | ||
70f80175 SB |
4241 | /* |
4242 | * Callback for btrfs_uuid_tree_iterate(). | |
4243 | * returns: | |
4244 | * 0 check succeeded, the entry is not outdated. | |
bb7ab3b9 | 4245 | * < 0 if an error occurred. |
70f80175 SB |
4246 | * > 0 if the check failed, which means the caller shall remove the entry. |
4247 | */ | |
4248 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, | |
4249 | u8 *uuid, u8 type, u64 subid) | |
4250 | { | |
4251 | struct btrfs_key key; | |
4252 | int ret = 0; | |
4253 | struct btrfs_root *subvol_root; | |
4254 | ||
4255 | if (type != BTRFS_UUID_KEY_SUBVOL && | |
4256 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) | |
4257 | goto out; | |
4258 | ||
4259 | key.objectid = subid; | |
4260 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4261 | key.offset = (u64)-1; | |
4262 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); | |
4263 | if (IS_ERR(subvol_root)) { | |
4264 | ret = PTR_ERR(subvol_root); | |
4265 | if (ret == -ENOENT) | |
4266 | ret = 1; | |
4267 | goto out; | |
4268 | } | |
4269 | ||
4270 | switch (type) { | |
4271 | case BTRFS_UUID_KEY_SUBVOL: | |
4272 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) | |
4273 | ret = 1; | |
4274 | break; | |
4275 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: | |
4276 | if (memcmp(uuid, subvol_root->root_item.received_uuid, | |
4277 | BTRFS_UUID_SIZE)) | |
4278 | ret = 1; | |
4279 | break; | |
4280 | } | |
4281 | ||
4282 | out: | |
4283 | return ret; | |
4284 | } | |
4285 | ||
4286 | static int btrfs_uuid_rescan_kthread(void *data) | |
4287 | { | |
4288 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; | |
4289 | int ret; | |
4290 | ||
4291 | /* | |
4292 | * 1st step is to iterate through the existing UUID tree and | |
4293 | * to delete all entries that contain outdated data. | |
4294 | * 2nd step is to add all missing entries to the UUID tree. | |
4295 | */ | |
4296 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); | |
4297 | if (ret < 0) { | |
efe120a0 | 4298 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
70f80175 SB |
4299 | up(&fs_info->uuid_tree_rescan_sem); |
4300 | return ret; | |
4301 | } | |
4302 | return btrfs_uuid_scan_kthread(data); | |
4303 | } | |
4304 | ||
f7a81ea4 SB |
4305 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
4306 | { | |
4307 | struct btrfs_trans_handle *trans; | |
4308 | struct btrfs_root *tree_root = fs_info->tree_root; | |
4309 | struct btrfs_root *uuid_root; | |
803b2f54 SB |
4310 | struct task_struct *task; |
4311 | int ret; | |
f7a81ea4 SB |
4312 | |
4313 | /* | |
4314 | * 1 - root node | |
4315 | * 1 - root item | |
4316 | */ | |
4317 | trans = btrfs_start_transaction(tree_root, 2); | |
4318 | if (IS_ERR(trans)) | |
4319 | return PTR_ERR(trans); | |
4320 | ||
4321 | uuid_root = btrfs_create_tree(trans, fs_info, | |
4322 | BTRFS_UUID_TREE_OBJECTID); | |
4323 | if (IS_ERR(uuid_root)) { | |
6d13f549 | 4324 | ret = PTR_ERR(uuid_root); |
66642832 | 4325 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4326 | btrfs_end_transaction(trans); |
6d13f549 | 4327 | return ret; |
f7a81ea4 SB |
4328 | } |
4329 | ||
4330 | fs_info->uuid_root = uuid_root; | |
4331 | ||
3a45bb20 | 4332 | ret = btrfs_commit_transaction(trans); |
803b2f54 SB |
4333 | if (ret) |
4334 | return ret; | |
4335 | ||
4336 | down(&fs_info->uuid_tree_rescan_sem); | |
4337 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); | |
4338 | if (IS_ERR(task)) { | |
70f80175 | 4339 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
efe120a0 | 4340 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
803b2f54 SB |
4341 | up(&fs_info->uuid_tree_rescan_sem); |
4342 | return PTR_ERR(task); | |
4343 | } | |
4344 | ||
4345 | return 0; | |
f7a81ea4 | 4346 | } |
803b2f54 | 4347 | |
70f80175 SB |
4348 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
4349 | { | |
4350 | struct task_struct *task; | |
4351 | ||
4352 | down(&fs_info->uuid_tree_rescan_sem); | |
4353 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); | |
4354 | if (IS_ERR(task)) { | |
4355 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ | |
efe120a0 | 4356 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
70f80175 SB |
4357 | up(&fs_info->uuid_tree_rescan_sem); |
4358 | return PTR_ERR(task); | |
4359 | } | |
4360 | ||
4361 | return 0; | |
4362 | } | |
4363 | ||
8f18cf13 CM |
4364 | /* |
4365 | * shrinking a device means finding all of the device extents past | |
4366 | * the new size, and then following the back refs to the chunks. | |
4367 | * The chunk relocation code actually frees the device extent | |
4368 | */ | |
4369 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) | |
4370 | { | |
0b246afa JM |
4371 | struct btrfs_fs_info *fs_info = device->fs_info; |
4372 | struct btrfs_root *root = fs_info->dev_root; | |
8f18cf13 | 4373 | struct btrfs_trans_handle *trans; |
8f18cf13 CM |
4374 | struct btrfs_dev_extent *dev_extent = NULL; |
4375 | struct btrfs_path *path; | |
4376 | u64 length; | |
8f18cf13 CM |
4377 | u64 chunk_offset; |
4378 | int ret; | |
4379 | int slot; | |
ba1bf481 JB |
4380 | int failed = 0; |
4381 | bool retried = false; | |
53e489bc | 4382 | bool checked_pending_chunks = false; |
8f18cf13 CM |
4383 | struct extent_buffer *l; |
4384 | struct btrfs_key key; | |
0b246afa | 4385 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
8f18cf13 | 4386 | u64 old_total = btrfs_super_total_bytes(super_copy); |
7cc8e58d | 4387 | u64 old_size = btrfs_device_get_total_bytes(device); |
7dfb8be1 NB |
4388 | u64 diff; |
4389 | ||
4390 | new_size = round_down(new_size, fs_info->sectorsize); | |
0e4324a4 | 4391 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
8f18cf13 | 4392 | |
63a212ab SB |
4393 | if (device->is_tgtdev_for_dev_replace) |
4394 | return -EINVAL; | |
4395 | ||
8f18cf13 CM |
4396 | path = btrfs_alloc_path(); |
4397 | if (!path) | |
4398 | return -ENOMEM; | |
4399 | ||
e4058b54 | 4400 | path->reada = READA_FORWARD; |
8f18cf13 | 4401 | |
34441361 | 4402 | mutex_lock(&fs_info->chunk_mutex); |
7d9eb12c | 4403 | |
7cc8e58d | 4404 | btrfs_device_set_total_bytes(device, new_size); |
2bf64758 | 4405 | if (device->writeable) { |
2b82032c | 4406 | device->fs_devices->total_rw_bytes -= diff; |
a5ed45f8 | 4407 | atomic64_sub(diff, &fs_info->free_chunk_space); |
2bf64758 | 4408 | } |
34441361 | 4409 | mutex_unlock(&fs_info->chunk_mutex); |
8f18cf13 | 4410 | |
ba1bf481 | 4411 | again: |
8f18cf13 CM |
4412 | key.objectid = device->devid; |
4413 | key.offset = (u64)-1; | |
4414 | key.type = BTRFS_DEV_EXTENT_KEY; | |
4415 | ||
213e64da | 4416 | do { |
0b246afa | 4417 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 | 4418 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
67c5e7d4 | 4419 | if (ret < 0) { |
0b246afa | 4420 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 | 4421 | goto done; |
67c5e7d4 | 4422 | } |
8f18cf13 CM |
4423 | |
4424 | ret = btrfs_previous_item(root, path, 0, key.type); | |
67c5e7d4 | 4425 | if (ret) |
0b246afa | 4426 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 CM |
4427 | if (ret < 0) |
4428 | goto done; | |
4429 | if (ret) { | |
4430 | ret = 0; | |
b3b4aa74 | 4431 | btrfs_release_path(path); |
bf1fb512 | 4432 | break; |
8f18cf13 CM |
4433 | } |
4434 | ||
4435 | l = path->nodes[0]; | |
4436 | slot = path->slots[0]; | |
4437 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
4438 | ||
ba1bf481 | 4439 | if (key.objectid != device->devid) { |
0b246afa | 4440 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
b3b4aa74 | 4441 | btrfs_release_path(path); |
bf1fb512 | 4442 | break; |
ba1bf481 | 4443 | } |
8f18cf13 CM |
4444 | |
4445 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
4446 | length = btrfs_dev_extent_length(l, dev_extent); | |
4447 | ||
ba1bf481 | 4448 | if (key.offset + length <= new_size) { |
0b246afa | 4449 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
b3b4aa74 | 4450 | btrfs_release_path(path); |
d6397bae | 4451 | break; |
ba1bf481 | 4452 | } |
8f18cf13 | 4453 | |
8f18cf13 | 4454 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
b3b4aa74 | 4455 | btrfs_release_path(path); |
8f18cf13 | 4456 | |
0b246afa JM |
4457 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
4458 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
ba1bf481 | 4459 | if (ret && ret != -ENOSPC) |
8f18cf13 | 4460 | goto done; |
ba1bf481 JB |
4461 | if (ret == -ENOSPC) |
4462 | failed++; | |
213e64da | 4463 | } while (key.offset-- > 0); |
ba1bf481 JB |
4464 | |
4465 | if (failed && !retried) { | |
4466 | failed = 0; | |
4467 | retried = true; | |
4468 | goto again; | |
4469 | } else if (failed && retried) { | |
4470 | ret = -ENOSPC; | |
ba1bf481 | 4471 | goto done; |
8f18cf13 CM |
4472 | } |
4473 | ||
d6397bae | 4474 | /* Shrinking succeeded, else we would be at "done". */ |
a22285a6 | 4475 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
4476 | if (IS_ERR(trans)) { |
4477 | ret = PTR_ERR(trans); | |
4478 | goto done; | |
4479 | } | |
4480 | ||
34441361 | 4481 | mutex_lock(&fs_info->chunk_mutex); |
53e489bc FM |
4482 | |
4483 | /* | |
4484 | * We checked in the above loop all device extents that were already in | |
4485 | * the device tree. However before we have updated the device's | |
4486 | * total_bytes to the new size, we might have had chunk allocations that | |
4487 | * have not complete yet (new block groups attached to transaction | |
4488 | * handles), and therefore their device extents were not yet in the | |
4489 | * device tree and we missed them in the loop above. So if we have any | |
4490 | * pending chunk using a device extent that overlaps the device range | |
4491 | * that we can not use anymore, commit the current transaction and | |
4492 | * repeat the search on the device tree - this way we guarantee we will | |
4493 | * not have chunks using device extents that end beyond 'new_size'. | |
4494 | */ | |
4495 | if (!checked_pending_chunks) { | |
4496 | u64 start = new_size; | |
4497 | u64 len = old_size - new_size; | |
4498 | ||
499f377f JM |
4499 | if (contains_pending_extent(trans->transaction, device, |
4500 | &start, len)) { | |
34441361 | 4501 | mutex_unlock(&fs_info->chunk_mutex); |
53e489bc FM |
4502 | checked_pending_chunks = true; |
4503 | failed = 0; | |
4504 | retried = false; | |
3a45bb20 | 4505 | ret = btrfs_commit_transaction(trans); |
53e489bc FM |
4506 | if (ret) |
4507 | goto done; | |
4508 | goto again; | |
4509 | } | |
4510 | } | |
4511 | ||
7cc8e58d | 4512 | btrfs_device_set_disk_total_bytes(device, new_size); |
935e5cc9 MX |
4513 | if (list_empty(&device->resized_list)) |
4514 | list_add_tail(&device->resized_list, | |
0b246afa | 4515 | &fs_info->fs_devices->resized_devices); |
d6397bae | 4516 | |
d6397bae | 4517 | WARN_ON(diff > old_total); |
7dfb8be1 NB |
4518 | btrfs_set_super_total_bytes(super_copy, |
4519 | round_down(old_total - diff, fs_info->sectorsize)); | |
34441361 | 4520 | mutex_unlock(&fs_info->chunk_mutex); |
2196d6e8 MX |
4521 | |
4522 | /* Now btrfs_update_device() will change the on-disk size. */ | |
4523 | ret = btrfs_update_device(trans, device); | |
3a45bb20 | 4524 | btrfs_end_transaction(trans); |
8f18cf13 CM |
4525 | done: |
4526 | btrfs_free_path(path); | |
53e489bc | 4527 | if (ret) { |
34441361 | 4528 | mutex_lock(&fs_info->chunk_mutex); |
53e489bc FM |
4529 | btrfs_device_set_total_bytes(device, old_size); |
4530 | if (device->writeable) | |
4531 | device->fs_devices->total_rw_bytes += diff; | |
a5ed45f8 | 4532 | atomic64_add(diff, &fs_info->free_chunk_space); |
34441361 | 4533 | mutex_unlock(&fs_info->chunk_mutex); |
53e489bc | 4534 | } |
8f18cf13 CM |
4535 | return ret; |
4536 | } | |
4537 | ||
2ff7e61e | 4538 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
0b86a832 CM |
4539 | struct btrfs_key *key, |
4540 | struct btrfs_chunk *chunk, int item_size) | |
4541 | { | |
0b246afa | 4542 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
0b86a832 CM |
4543 | struct btrfs_disk_key disk_key; |
4544 | u32 array_size; | |
4545 | u8 *ptr; | |
4546 | ||
34441361 | 4547 | mutex_lock(&fs_info->chunk_mutex); |
0b86a832 | 4548 | array_size = btrfs_super_sys_array_size(super_copy); |
5f43f86e | 4549 | if (array_size + item_size + sizeof(disk_key) |
fe48a5c0 | 4550 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
34441361 | 4551 | mutex_unlock(&fs_info->chunk_mutex); |
0b86a832 | 4552 | return -EFBIG; |
fe48a5c0 | 4553 | } |
0b86a832 CM |
4554 | |
4555 | ptr = super_copy->sys_chunk_array + array_size; | |
4556 | btrfs_cpu_key_to_disk(&disk_key, key); | |
4557 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
4558 | ptr += sizeof(disk_key); | |
4559 | memcpy(ptr, chunk, item_size); | |
4560 | item_size += sizeof(disk_key); | |
4561 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
34441361 | 4562 | mutex_unlock(&fs_info->chunk_mutex); |
fe48a5c0 | 4563 | |
0b86a832 CM |
4564 | return 0; |
4565 | } | |
4566 | ||
73c5de00 AJ |
4567 | /* |
4568 | * sort the devices in descending order by max_avail, total_avail | |
4569 | */ | |
4570 | static int btrfs_cmp_device_info(const void *a, const void *b) | |
9b3f68b9 | 4571 | { |
73c5de00 AJ |
4572 | const struct btrfs_device_info *di_a = a; |
4573 | const struct btrfs_device_info *di_b = b; | |
9b3f68b9 | 4574 | |
73c5de00 | 4575 | if (di_a->max_avail > di_b->max_avail) |
b2117a39 | 4576 | return -1; |
73c5de00 | 4577 | if (di_a->max_avail < di_b->max_avail) |
b2117a39 | 4578 | return 1; |
73c5de00 AJ |
4579 | if (di_a->total_avail > di_b->total_avail) |
4580 | return -1; | |
4581 | if (di_a->total_avail < di_b->total_avail) | |
4582 | return 1; | |
4583 | return 0; | |
b2117a39 | 4584 | } |
0b86a832 | 4585 | |
53b381b3 DW |
4586 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
4587 | { | |
ffe2d203 | 4588 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
53b381b3 DW |
4589 | return; |
4590 | ||
ceda0864 | 4591 | btrfs_set_fs_incompat(info, RAID56); |
53b381b3 DW |
4592 | } |
4593 | ||
da17066c | 4594 | #define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \ |
23f8f9b7 GH |
4595 | - sizeof(struct btrfs_chunk)) \ |
4596 | / sizeof(struct btrfs_stripe) + 1) | |
4597 | ||
4598 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ | |
4599 | - 2 * sizeof(struct btrfs_disk_key) \ | |
4600 | - 2 * sizeof(struct btrfs_chunk)) \ | |
4601 | / sizeof(struct btrfs_stripe) + 1) | |
4602 | ||
73c5de00 | 4603 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
72b468c8 | 4604 | u64 start, u64 type) |
b2117a39 | 4605 | { |
2ff7e61e | 4606 | struct btrfs_fs_info *info = trans->fs_info; |
73c5de00 | 4607 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
ebcc9301 | 4608 | struct btrfs_device *device; |
73c5de00 AJ |
4609 | struct map_lookup *map = NULL; |
4610 | struct extent_map_tree *em_tree; | |
4611 | struct extent_map *em; | |
4612 | struct btrfs_device_info *devices_info = NULL; | |
4613 | u64 total_avail; | |
4614 | int num_stripes; /* total number of stripes to allocate */ | |
53b381b3 DW |
4615 | int data_stripes; /* number of stripes that count for |
4616 | block group size */ | |
73c5de00 AJ |
4617 | int sub_stripes; /* sub_stripes info for map */ |
4618 | int dev_stripes; /* stripes per dev */ | |
4619 | int devs_max; /* max devs to use */ | |
4620 | int devs_min; /* min devs needed */ | |
4621 | int devs_increment; /* ndevs has to be a multiple of this */ | |
4622 | int ncopies; /* how many copies to data has */ | |
4623 | int ret; | |
4624 | u64 max_stripe_size; | |
4625 | u64 max_chunk_size; | |
4626 | u64 stripe_size; | |
4627 | u64 num_bytes; | |
4628 | int ndevs; | |
4629 | int i; | |
4630 | int j; | |
31e50229 | 4631 | int index; |
593060d7 | 4632 | |
0c460c0d | 4633 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
9b3f68b9 | 4634 | |
73c5de00 AJ |
4635 | if (list_empty(&fs_devices->alloc_list)) |
4636 | return -ENOSPC; | |
b2117a39 | 4637 | |
31e50229 | 4638 | index = __get_raid_index(type); |
73c5de00 | 4639 | |
31e50229 LB |
4640 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
4641 | dev_stripes = btrfs_raid_array[index].dev_stripes; | |
4642 | devs_max = btrfs_raid_array[index].devs_max; | |
4643 | devs_min = btrfs_raid_array[index].devs_min; | |
4644 | devs_increment = btrfs_raid_array[index].devs_increment; | |
4645 | ncopies = btrfs_raid_array[index].ncopies; | |
b2117a39 | 4646 | |
9b3f68b9 | 4647 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
ee22184b | 4648 | max_stripe_size = SZ_1G; |
73c5de00 | 4649 | max_chunk_size = 10 * max_stripe_size; |
23f8f9b7 GH |
4650 | if (!devs_max) |
4651 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); | |
9b3f68b9 | 4652 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
1100373f | 4653 | /* for larger filesystems, use larger metadata chunks */ |
ee22184b BL |
4654 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
4655 | max_stripe_size = SZ_1G; | |
1100373f | 4656 | else |
ee22184b | 4657 | max_stripe_size = SZ_256M; |
73c5de00 | 4658 | max_chunk_size = max_stripe_size; |
23f8f9b7 GH |
4659 | if (!devs_max) |
4660 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); | |
a40a90a0 | 4661 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
ee22184b | 4662 | max_stripe_size = SZ_32M; |
73c5de00 | 4663 | max_chunk_size = 2 * max_stripe_size; |
23f8f9b7 GH |
4664 | if (!devs_max) |
4665 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; | |
73c5de00 | 4666 | } else { |
351fd353 | 4667 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
73c5de00 AJ |
4668 | type); |
4669 | BUG_ON(1); | |
9b3f68b9 CM |
4670 | } |
4671 | ||
2b82032c YZ |
4672 | /* we don't want a chunk larger than 10% of writeable space */ |
4673 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), | |
4674 | max_chunk_size); | |
9b3f68b9 | 4675 | |
31e818fe | 4676 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
73c5de00 AJ |
4677 | GFP_NOFS); |
4678 | if (!devices_info) | |
4679 | return -ENOMEM; | |
0cad8a11 | 4680 | |
9f680ce0 | 4681 | /* |
73c5de00 AJ |
4682 | * in the first pass through the devices list, we gather information |
4683 | * about the available holes on each device. | |
9f680ce0 | 4684 | */ |
73c5de00 | 4685 | ndevs = 0; |
ebcc9301 | 4686 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
73c5de00 AJ |
4687 | u64 max_avail; |
4688 | u64 dev_offset; | |
b2117a39 | 4689 | |
73c5de00 | 4690 | if (!device->writeable) { |
31b1a2bd | 4691 | WARN(1, KERN_ERR |
efe120a0 | 4692 | "BTRFS: read-only device in alloc_list\n"); |
73c5de00 AJ |
4693 | continue; |
4694 | } | |
b2117a39 | 4695 | |
63a212ab SB |
4696 | if (!device->in_fs_metadata || |
4697 | device->is_tgtdev_for_dev_replace) | |
73c5de00 | 4698 | continue; |
b2117a39 | 4699 | |
73c5de00 AJ |
4700 | if (device->total_bytes > device->bytes_used) |
4701 | total_avail = device->total_bytes - device->bytes_used; | |
4702 | else | |
4703 | total_avail = 0; | |
38c01b96 | 4704 | |
4705 | /* If there is no space on this device, skip it. */ | |
4706 | if (total_avail == 0) | |
4707 | continue; | |
b2117a39 | 4708 | |
6df9a95e | 4709 | ret = find_free_dev_extent(trans, device, |
73c5de00 AJ |
4710 | max_stripe_size * dev_stripes, |
4711 | &dev_offset, &max_avail); | |
4712 | if (ret && ret != -ENOSPC) | |
4713 | goto error; | |
b2117a39 | 4714 | |
73c5de00 AJ |
4715 | if (ret == 0) |
4716 | max_avail = max_stripe_size * dev_stripes; | |
b2117a39 | 4717 | |
73c5de00 AJ |
4718 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
4719 | continue; | |
b2117a39 | 4720 | |
063d006f ES |
4721 | if (ndevs == fs_devices->rw_devices) { |
4722 | WARN(1, "%s: found more than %llu devices\n", | |
4723 | __func__, fs_devices->rw_devices); | |
4724 | break; | |
4725 | } | |
73c5de00 AJ |
4726 | devices_info[ndevs].dev_offset = dev_offset; |
4727 | devices_info[ndevs].max_avail = max_avail; | |
4728 | devices_info[ndevs].total_avail = total_avail; | |
4729 | devices_info[ndevs].dev = device; | |
4730 | ++ndevs; | |
4731 | } | |
b2117a39 | 4732 | |
73c5de00 AJ |
4733 | /* |
4734 | * now sort the devices by hole size / available space | |
4735 | */ | |
4736 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), | |
4737 | btrfs_cmp_device_info, NULL); | |
b2117a39 | 4738 | |
73c5de00 | 4739 | /* round down to number of usable stripes */ |
e5600fd6 | 4740 | ndevs = round_down(ndevs, devs_increment); |
b2117a39 | 4741 | |
73c5de00 AJ |
4742 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
4743 | ret = -ENOSPC; | |
4744 | goto error; | |
b2117a39 | 4745 | } |
9f680ce0 | 4746 | |
f148ef4d NB |
4747 | ndevs = min(ndevs, devs_max); |
4748 | ||
73c5de00 | 4749 | /* |
272b3be7 HK |
4750 | * The primary goal is to maximize the number of stripes, so use as |
4751 | * many devices as possible, even if the stripes are not maximum sized. | |
4752 | * | |
4753 | * The DUP profile stores more than one stripe per device, the | |
4754 | * max_avail is the total size so we have to adjust. | |
73c5de00 | 4755 | */ |
272b3be7 | 4756 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
73c5de00 | 4757 | num_stripes = ndevs * dev_stripes; |
b2117a39 | 4758 | |
53b381b3 DW |
4759 | /* |
4760 | * this will have to be fixed for RAID1 and RAID10 over | |
4761 | * more drives | |
4762 | */ | |
4763 | data_stripes = num_stripes / ncopies; | |
4764 | ||
500ceed8 | 4765 | if (type & BTRFS_BLOCK_GROUP_RAID5) |
53b381b3 | 4766 | data_stripes = num_stripes - 1; |
500ceed8 NB |
4767 | |
4768 | if (type & BTRFS_BLOCK_GROUP_RAID6) | |
53b381b3 | 4769 | data_stripes = num_stripes - 2; |
86db2578 CM |
4770 | |
4771 | /* | |
4772 | * Use the number of data stripes to figure out how big this chunk | |
4773 | * is really going to be in terms of logical address space, | |
4774 | * and compare that answer with the max chunk size | |
4775 | */ | |
4776 | if (stripe_size * data_stripes > max_chunk_size) { | |
4777 | u64 mask = (1ULL << 24) - 1; | |
b8b93add DS |
4778 | |
4779 | stripe_size = div_u64(max_chunk_size, data_stripes); | |
86db2578 CM |
4780 | |
4781 | /* bump the answer up to a 16MB boundary */ | |
4782 | stripe_size = (stripe_size + mask) & ~mask; | |
4783 | ||
4784 | /* but don't go higher than the limits we found | |
4785 | * while searching for free extents | |
4786 | */ | |
4787 | if (stripe_size > devices_info[ndevs-1].max_avail) | |
4788 | stripe_size = devices_info[ndevs-1].max_avail; | |
4789 | } | |
4790 | ||
37db63a4 | 4791 | /* align to BTRFS_STRIPE_LEN */ |
500ceed8 | 4792 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
b2117a39 MX |
4793 | |
4794 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
4795 | if (!map) { | |
4796 | ret = -ENOMEM; | |
4797 | goto error; | |
4798 | } | |
4799 | map->num_stripes = num_stripes; | |
9b3f68b9 | 4800 | |
73c5de00 AJ |
4801 | for (i = 0; i < ndevs; ++i) { |
4802 | for (j = 0; j < dev_stripes; ++j) { | |
4803 | int s = i * dev_stripes + j; | |
4804 | map->stripes[s].dev = devices_info[i].dev; | |
4805 | map->stripes[s].physical = devices_info[i].dev_offset + | |
4806 | j * stripe_size; | |
6324fbf3 | 4807 | } |
6324fbf3 | 4808 | } |
500ceed8 NB |
4809 | map->stripe_len = BTRFS_STRIPE_LEN; |
4810 | map->io_align = BTRFS_STRIPE_LEN; | |
4811 | map->io_width = BTRFS_STRIPE_LEN; | |
2b82032c | 4812 | map->type = type; |
2b82032c | 4813 | map->sub_stripes = sub_stripes; |
0b86a832 | 4814 | |
53b381b3 | 4815 | num_bytes = stripe_size * data_stripes; |
0b86a832 | 4816 | |
6bccf3ab | 4817 | trace_btrfs_chunk_alloc(info, map, start, num_bytes); |
1abe9b8a | 4818 | |
172ddd60 | 4819 | em = alloc_extent_map(); |
2b82032c | 4820 | if (!em) { |
298a8f9c | 4821 | kfree(map); |
b2117a39 MX |
4822 | ret = -ENOMEM; |
4823 | goto error; | |
593060d7 | 4824 | } |
298a8f9c | 4825 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
95617d69 | 4826 | em->map_lookup = map; |
2b82032c | 4827 | em->start = start; |
73c5de00 | 4828 | em->len = num_bytes; |
2b82032c YZ |
4829 | em->block_start = 0; |
4830 | em->block_len = em->len; | |
6df9a95e | 4831 | em->orig_block_len = stripe_size; |
593060d7 | 4832 | |
0b246afa | 4833 | em_tree = &info->mapping_tree.map_tree; |
890871be | 4834 | write_lock(&em_tree->lock); |
09a2a8f9 | 4835 | ret = add_extent_mapping(em_tree, em, 0); |
0f5d42b2 | 4836 | if (ret) { |
1efb72a3 | 4837 | write_unlock(&em_tree->lock); |
0f5d42b2 | 4838 | free_extent_map(em); |
1dd4602f | 4839 | goto error; |
0f5d42b2 | 4840 | } |
0b86a832 | 4841 | |
1efb72a3 NB |
4842 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
4843 | refcount_inc(&em->refs); | |
4844 | write_unlock(&em_tree->lock); | |
4845 | ||
0174484d | 4846 | ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes); |
6df9a95e JB |
4847 | if (ret) |
4848 | goto error_del_extent; | |
2b82032c | 4849 | |
7cc8e58d MX |
4850 | for (i = 0; i < map->num_stripes; i++) { |
4851 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; | |
4852 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); | |
4853 | } | |
43530c46 | 4854 | |
a5ed45f8 | 4855 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
1c116187 | 4856 | |
0f5d42b2 | 4857 | free_extent_map(em); |
0b246afa | 4858 | check_raid56_incompat_flag(info, type); |
53b381b3 | 4859 | |
b2117a39 | 4860 | kfree(devices_info); |
2b82032c | 4861 | return 0; |
b2117a39 | 4862 | |
6df9a95e | 4863 | error_del_extent: |
0f5d42b2 JB |
4864 | write_lock(&em_tree->lock); |
4865 | remove_extent_mapping(em_tree, em); | |
4866 | write_unlock(&em_tree->lock); | |
4867 | ||
4868 | /* One for our allocation */ | |
4869 | free_extent_map(em); | |
4870 | /* One for the tree reference */ | |
4871 | free_extent_map(em); | |
495e64f4 FM |
4872 | /* One for the pending_chunks list reference */ |
4873 | free_extent_map(em); | |
b2117a39 | 4874 | error: |
b2117a39 MX |
4875 | kfree(devices_info); |
4876 | return ret; | |
2b82032c YZ |
4877 | } |
4878 | ||
6df9a95e | 4879 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
6bccf3ab | 4880 | struct btrfs_fs_info *fs_info, |
6df9a95e | 4881 | u64 chunk_offset, u64 chunk_size) |
2b82032c | 4882 | { |
6bccf3ab JM |
4883 | struct btrfs_root *extent_root = fs_info->extent_root; |
4884 | struct btrfs_root *chunk_root = fs_info->chunk_root; | |
2b82032c | 4885 | struct btrfs_key key; |
2b82032c YZ |
4886 | struct btrfs_device *device; |
4887 | struct btrfs_chunk *chunk; | |
4888 | struct btrfs_stripe *stripe; | |
6df9a95e JB |
4889 | struct extent_map *em; |
4890 | struct map_lookup *map; | |
4891 | size_t item_size; | |
4892 | u64 dev_offset; | |
4893 | u64 stripe_size; | |
4894 | int i = 0; | |
140e639f | 4895 | int ret = 0; |
2b82032c | 4896 | |
592d92ee LB |
4897 | em = get_chunk_map(fs_info, chunk_offset, chunk_size); |
4898 | if (IS_ERR(em)) | |
4899 | return PTR_ERR(em); | |
6df9a95e | 4900 | |
95617d69 | 4901 | map = em->map_lookup; |
6df9a95e JB |
4902 | item_size = btrfs_chunk_item_size(map->num_stripes); |
4903 | stripe_size = em->orig_block_len; | |
4904 | ||
2b82032c | 4905 | chunk = kzalloc(item_size, GFP_NOFS); |
6df9a95e JB |
4906 | if (!chunk) { |
4907 | ret = -ENOMEM; | |
4908 | goto out; | |
4909 | } | |
4910 | ||
50460e37 FM |
4911 | /* |
4912 | * Take the device list mutex to prevent races with the final phase of | |
4913 | * a device replace operation that replaces the device object associated | |
4914 | * with the map's stripes, because the device object's id can change | |
4915 | * at any time during that final phase of the device replace operation | |
4916 | * (dev-replace.c:btrfs_dev_replace_finishing()). | |
4917 | */ | |
0b246afa | 4918 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
6df9a95e JB |
4919 | for (i = 0; i < map->num_stripes; i++) { |
4920 | device = map->stripes[i].dev; | |
4921 | dev_offset = map->stripes[i].physical; | |
2b82032c | 4922 | |
0b86a832 | 4923 | ret = btrfs_update_device(trans, device); |
3acd3953 | 4924 | if (ret) |
50460e37 | 4925 | break; |
b5d9071c NB |
4926 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
4927 | dev_offset, stripe_size); | |
6df9a95e | 4928 | if (ret) |
50460e37 FM |
4929 | break; |
4930 | } | |
4931 | if (ret) { | |
0b246afa | 4932 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
50460e37 | 4933 | goto out; |
2b82032c YZ |
4934 | } |
4935 | ||
2b82032c | 4936 | stripe = &chunk->stripe; |
6df9a95e JB |
4937 | for (i = 0; i < map->num_stripes; i++) { |
4938 | device = map->stripes[i].dev; | |
4939 | dev_offset = map->stripes[i].physical; | |
0b86a832 | 4940 | |
e17cade2 CM |
4941 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
4942 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
4943 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
2b82032c | 4944 | stripe++; |
0b86a832 | 4945 | } |
0b246afa | 4946 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0b86a832 | 4947 | |
2b82032c | 4948 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
0b86a832 | 4949 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
2b82032c YZ |
4950 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
4951 | btrfs_set_stack_chunk_type(chunk, map->type); | |
4952 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); | |
4953 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); | |
4954 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); | |
0b246afa | 4955 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
2b82032c | 4956 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
0b86a832 | 4957 | |
2b82032c YZ |
4958 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
4959 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
4960 | key.offset = chunk_offset; | |
0b86a832 | 4961 | |
2b82032c | 4962 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
4ed1d16e MF |
4963 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
4964 | /* | |
4965 | * TODO: Cleanup of inserted chunk root in case of | |
4966 | * failure. | |
4967 | */ | |
2ff7e61e | 4968 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
8f18cf13 | 4969 | } |
1abe9b8a | 4970 | |
6df9a95e | 4971 | out: |
0b86a832 | 4972 | kfree(chunk); |
6df9a95e | 4973 | free_extent_map(em); |
4ed1d16e | 4974 | return ret; |
2b82032c | 4975 | } |
0b86a832 | 4976 | |
2b82032c YZ |
4977 | /* |
4978 | * Chunk allocation falls into two parts. The first part does works | |
4979 | * that make the new allocated chunk useable, but not do any operation | |
4980 | * that modifies the chunk tree. The second part does the works that | |
4981 | * require modifying the chunk tree. This division is important for the | |
4982 | * bootstrap process of adding storage to a seed btrfs. | |
4983 | */ | |
4984 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
2ff7e61e | 4985 | struct btrfs_fs_info *fs_info, u64 type) |
2b82032c YZ |
4986 | { |
4987 | u64 chunk_offset; | |
2b82032c | 4988 | |
0b246afa JM |
4989 | ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); |
4990 | chunk_offset = find_next_chunk(fs_info); | |
72b468c8 | 4991 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
2b82032c YZ |
4992 | } |
4993 | ||
d397712b | 4994 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
e4a4dce7 | 4995 | struct btrfs_fs_info *fs_info) |
2b82032c YZ |
4996 | { |
4997 | u64 chunk_offset; | |
4998 | u64 sys_chunk_offset; | |
2b82032c | 4999 | u64 alloc_profile; |
2b82032c YZ |
5000 | int ret; |
5001 | ||
6df9a95e | 5002 | chunk_offset = find_next_chunk(fs_info); |
1b86826d | 5003 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
72b468c8 | 5004 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
79787eaa JM |
5005 | if (ret) |
5006 | return ret; | |
2b82032c | 5007 | |
0b246afa | 5008 | sys_chunk_offset = find_next_chunk(fs_info); |
1b86826d | 5009 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
72b468c8 | 5010 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
79787eaa | 5011 | return ret; |
2b82032c YZ |
5012 | } |
5013 | ||
d20983b4 MX |
5014 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
5015 | { | |
5016 | int max_errors; | |
5017 | ||
5018 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | | |
5019 | BTRFS_BLOCK_GROUP_RAID10 | | |
5020 | BTRFS_BLOCK_GROUP_RAID5 | | |
5021 | BTRFS_BLOCK_GROUP_DUP)) { | |
5022 | max_errors = 1; | |
5023 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { | |
5024 | max_errors = 2; | |
5025 | } else { | |
5026 | max_errors = 0; | |
005d6427 | 5027 | } |
2b82032c | 5028 | |
d20983b4 | 5029 | return max_errors; |
2b82032c YZ |
5030 | } |
5031 | ||
2ff7e61e | 5032 | int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
2b82032c YZ |
5033 | { |
5034 | struct extent_map *em; | |
5035 | struct map_lookup *map; | |
2b82032c | 5036 | int readonly = 0; |
d20983b4 | 5037 | int miss_ndevs = 0; |
2b82032c YZ |
5038 | int i; |
5039 | ||
592d92ee LB |
5040 | em = get_chunk_map(fs_info, chunk_offset, 1); |
5041 | if (IS_ERR(em)) | |
2b82032c YZ |
5042 | return 1; |
5043 | ||
95617d69 | 5044 | map = em->map_lookup; |
2b82032c | 5045 | for (i = 0; i < map->num_stripes; i++) { |
d20983b4 MX |
5046 | if (map->stripes[i].dev->missing) { |
5047 | miss_ndevs++; | |
5048 | continue; | |
5049 | } | |
5050 | ||
2b82032c YZ |
5051 | if (!map->stripes[i].dev->writeable) { |
5052 | readonly = 1; | |
d20983b4 | 5053 | goto end; |
2b82032c YZ |
5054 | } |
5055 | } | |
d20983b4 MX |
5056 | |
5057 | /* | |
5058 | * If the number of missing devices is larger than max errors, | |
5059 | * we can not write the data into that chunk successfully, so | |
5060 | * set it readonly. | |
5061 | */ | |
5062 | if (miss_ndevs > btrfs_chunk_max_errors(map)) | |
5063 | readonly = 1; | |
5064 | end: | |
0b86a832 | 5065 | free_extent_map(em); |
2b82032c | 5066 | return readonly; |
0b86a832 CM |
5067 | } |
5068 | ||
5069 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | |
5070 | { | |
a8067e02 | 5071 | extent_map_tree_init(&tree->map_tree); |
0b86a832 CM |
5072 | } |
5073 | ||
5074 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | |
5075 | { | |
5076 | struct extent_map *em; | |
5077 | ||
d397712b | 5078 | while (1) { |
890871be | 5079 | write_lock(&tree->map_tree.lock); |
0b86a832 CM |
5080 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
5081 | if (em) | |
5082 | remove_extent_mapping(&tree->map_tree, em); | |
890871be | 5083 | write_unlock(&tree->map_tree.lock); |
0b86a832 CM |
5084 | if (!em) |
5085 | break; | |
0b86a832 CM |
5086 | /* once for us */ |
5087 | free_extent_map(em); | |
5088 | /* once for the tree */ | |
5089 | free_extent_map(em); | |
5090 | } | |
5091 | } | |
5092 | ||
5d964051 | 5093 | int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
f188591e CM |
5094 | { |
5095 | struct extent_map *em; | |
5096 | struct map_lookup *map; | |
f188591e CM |
5097 | int ret; |
5098 | ||
592d92ee LB |
5099 | em = get_chunk_map(fs_info, logical, len); |
5100 | if (IS_ERR(em)) | |
5101 | /* | |
5102 | * We could return errors for these cases, but that could get | |
5103 | * ugly and we'd probably do the same thing which is just not do | |
5104 | * anything else and exit, so return 1 so the callers don't try | |
5105 | * to use other copies. | |
5106 | */ | |
fb7669b5 | 5107 | return 1; |
fb7669b5 | 5108 | |
95617d69 | 5109 | map = em->map_lookup; |
f188591e CM |
5110 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
5111 | ret = map->num_stripes; | |
321aecc6 CM |
5112 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
5113 | ret = map->sub_stripes; | |
53b381b3 DW |
5114 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
5115 | ret = 2; | |
5116 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) | |
cc41f4fe LB |
5117 | /* |
5118 | * There could be two corrupted data stripes, we need | |
5119 | * to loop retry in order to rebuild the correct data. | |
5120 | * | |
5121 | * Fail a stripe at a time on every retry except the | |
5122 | * stripe under reconstruction. | |
5123 | */ | |
5124 | ret = map->num_stripes; | |
f188591e CM |
5125 | else |
5126 | ret = 1; | |
5127 | free_extent_map(em); | |
ad6d620e | 5128 | |
73beece9 | 5129 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
6fad823f LB |
5130 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
5131 | fs_info->dev_replace.tgtdev) | |
ad6d620e | 5132 | ret++; |
73beece9 | 5133 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
ad6d620e | 5134 | |
f188591e CM |
5135 | return ret; |
5136 | } | |
5137 | ||
2ff7e61e | 5138 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
53b381b3 DW |
5139 | u64 logical) |
5140 | { | |
5141 | struct extent_map *em; | |
5142 | struct map_lookup *map; | |
0b246afa | 5143 | unsigned long len = fs_info->sectorsize; |
53b381b3 | 5144 | |
592d92ee | 5145 | em = get_chunk_map(fs_info, logical, len); |
53b381b3 | 5146 | |
69f03f13 NB |
5147 | if (!WARN_ON(IS_ERR(em))) { |
5148 | map = em->map_lookup; | |
5149 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
5150 | len = map->stripe_len * nr_data_stripes(map); | |
5151 | free_extent_map(em); | |
5152 | } | |
53b381b3 DW |
5153 | return len; |
5154 | } | |
5155 | ||
e4ff5fb5 | 5156 | int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
53b381b3 DW |
5157 | { |
5158 | struct extent_map *em; | |
5159 | struct map_lookup *map; | |
53b381b3 DW |
5160 | int ret = 0; |
5161 | ||
592d92ee | 5162 | em = get_chunk_map(fs_info, logical, len); |
53b381b3 | 5163 | |
69f03f13 NB |
5164 | if(!WARN_ON(IS_ERR(em))) { |
5165 | map = em->map_lookup; | |
5166 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
5167 | ret = 1; | |
5168 | free_extent_map(em); | |
5169 | } | |
53b381b3 DW |
5170 | return ret; |
5171 | } | |
5172 | ||
30d9861f SB |
5173 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
5174 | struct map_lookup *map, int first, int num, | |
5175 | int optimal, int dev_replace_is_ongoing) | |
dfe25020 CM |
5176 | { |
5177 | int i; | |
30d9861f SB |
5178 | int tolerance; |
5179 | struct btrfs_device *srcdev; | |
5180 | ||
5181 | if (dev_replace_is_ongoing && | |
5182 | fs_info->dev_replace.cont_reading_from_srcdev_mode == | |
5183 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) | |
5184 | srcdev = fs_info->dev_replace.srcdev; | |
5185 | else | |
5186 | srcdev = NULL; | |
5187 | ||
5188 | /* | |
5189 | * try to avoid the drive that is the source drive for a | |
5190 | * dev-replace procedure, only choose it if no other non-missing | |
5191 | * mirror is available | |
5192 | */ | |
5193 | for (tolerance = 0; tolerance < 2; tolerance++) { | |
5194 | if (map->stripes[optimal].dev->bdev && | |
5195 | (tolerance || map->stripes[optimal].dev != srcdev)) | |
5196 | return optimal; | |
5197 | for (i = first; i < first + num; i++) { | |
5198 | if (map->stripes[i].dev->bdev && | |
5199 | (tolerance || map->stripes[i].dev != srcdev)) | |
5200 | return i; | |
5201 | } | |
dfe25020 | 5202 | } |
30d9861f | 5203 | |
dfe25020 CM |
5204 | /* we couldn't find one that doesn't fail. Just return something |
5205 | * and the io error handling code will clean up eventually | |
5206 | */ | |
5207 | return optimal; | |
5208 | } | |
5209 | ||
53b381b3 DW |
5210 | static inline int parity_smaller(u64 a, u64 b) |
5211 | { | |
5212 | return a > b; | |
5213 | } | |
5214 | ||
5215 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ | |
8e5cfb55 | 5216 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
53b381b3 DW |
5217 | { |
5218 | struct btrfs_bio_stripe s; | |
5219 | int i; | |
5220 | u64 l; | |
5221 | int again = 1; | |
5222 | ||
5223 | while (again) { | |
5224 | again = 0; | |
cc7539ed | 5225 | for (i = 0; i < num_stripes - 1; i++) { |
8e5cfb55 ZL |
5226 | if (parity_smaller(bbio->raid_map[i], |
5227 | bbio->raid_map[i+1])) { | |
53b381b3 | 5228 | s = bbio->stripes[i]; |
8e5cfb55 | 5229 | l = bbio->raid_map[i]; |
53b381b3 | 5230 | bbio->stripes[i] = bbio->stripes[i+1]; |
8e5cfb55 | 5231 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
53b381b3 | 5232 | bbio->stripes[i+1] = s; |
8e5cfb55 | 5233 | bbio->raid_map[i+1] = l; |
2c8cdd6e | 5234 | |
53b381b3 DW |
5235 | again = 1; |
5236 | } | |
5237 | } | |
5238 | } | |
5239 | } | |
5240 | ||
6e9606d2 ZL |
5241 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
5242 | { | |
5243 | struct btrfs_bio *bbio = kzalloc( | |
e57cf21e | 5244 | /* the size of the btrfs_bio */ |
6e9606d2 | 5245 | sizeof(struct btrfs_bio) + |
e57cf21e | 5246 | /* plus the variable array for the stripes */ |
6e9606d2 | 5247 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
e57cf21e | 5248 | /* plus the variable array for the tgt dev */ |
6e9606d2 | 5249 | sizeof(int) * (real_stripes) + |
e57cf21e CM |
5250 | /* |
5251 | * plus the raid_map, which includes both the tgt dev | |
5252 | * and the stripes | |
5253 | */ | |
5254 | sizeof(u64) * (total_stripes), | |
277fb5fc | 5255 | GFP_NOFS|__GFP_NOFAIL); |
6e9606d2 ZL |
5256 | |
5257 | atomic_set(&bbio->error, 0); | |
140475ae | 5258 | refcount_set(&bbio->refs, 1); |
6e9606d2 ZL |
5259 | |
5260 | return bbio; | |
5261 | } | |
5262 | ||
5263 | void btrfs_get_bbio(struct btrfs_bio *bbio) | |
5264 | { | |
140475ae ER |
5265 | WARN_ON(!refcount_read(&bbio->refs)); |
5266 | refcount_inc(&bbio->refs); | |
6e9606d2 ZL |
5267 | } |
5268 | ||
5269 | void btrfs_put_bbio(struct btrfs_bio *bbio) | |
5270 | { | |
5271 | if (!bbio) | |
5272 | return; | |
140475ae | 5273 | if (refcount_dec_and_test(&bbio->refs)) |
6e9606d2 ZL |
5274 | kfree(bbio); |
5275 | } | |
5276 | ||
0b3d4cd3 LB |
5277 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
5278 | /* | |
5279 | * Please note that, discard won't be sent to target device of device | |
5280 | * replace. | |
5281 | */ | |
5282 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, | |
5283 | u64 logical, u64 length, | |
5284 | struct btrfs_bio **bbio_ret) | |
5285 | { | |
5286 | struct extent_map *em; | |
5287 | struct map_lookup *map; | |
5288 | struct btrfs_bio *bbio; | |
5289 | u64 offset; | |
5290 | u64 stripe_nr; | |
5291 | u64 stripe_nr_end; | |
5292 | u64 stripe_end_offset; | |
5293 | u64 stripe_cnt; | |
5294 | u64 stripe_len; | |
5295 | u64 stripe_offset; | |
5296 | u64 num_stripes; | |
5297 | u32 stripe_index; | |
5298 | u32 factor = 0; | |
5299 | u32 sub_stripes = 0; | |
5300 | u64 stripes_per_dev = 0; | |
5301 | u32 remaining_stripes = 0; | |
5302 | u32 last_stripe = 0; | |
5303 | int ret = 0; | |
5304 | int i; | |
5305 | ||
5306 | /* discard always return a bbio */ | |
5307 | ASSERT(bbio_ret); | |
5308 | ||
5309 | em = get_chunk_map(fs_info, logical, length); | |
5310 | if (IS_ERR(em)) | |
5311 | return PTR_ERR(em); | |
5312 | ||
5313 | map = em->map_lookup; | |
5314 | /* we don't discard raid56 yet */ | |
5315 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { | |
5316 | ret = -EOPNOTSUPP; | |
5317 | goto out; | |
5318 | } | |
5319 | ||
5320 | offset = logical - em->start; | |
5321 | length = min_t(u64, em->len - offset, length); | |
5322 | ||
5323 | stripe_len = map->stripe_len; | |
5324 | /* | |
5325 | * stripe_nr counts the total number of stripes we have to stride | |
5326 | * to get to this block | |
5327 | */ | |
5328 | stripe_nr = div64_u64(offset, stripe_len); | |
5329 | ||
5330 | /* stripe_offset is the offset of this block in its stripe */ | |
5331 | stripe_offset = offset - stripe_nr * stripe_len; | |
5332 | ||
5333 | stripe_nr_end = round_up(offset + length, map->stripe_len); | |
42c61ab6 | 5334 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
0b3d4cd3 LB |
5335 | stripe_cnt = stripe_nr_end - stripe_nr; |
5336 | stripe_end_offset = stripe_nr_end * map->stripe_len - | |
5337 | (offset + length); | |
5338 | /* | |
5339 | * after this, stripe_nr is the number of stripes on this | |
5340 | * device we have to walk to find the data, and stripe_index is | |
5341 | * the number of our device in the stripe array | |
5342 | */ | |
5343 | num_stripes = 1; | |
5344 | stripe_index = 0; | |
5345 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
5346 | BTRFS_BLOCK_GROUP_RAID10)) { | |
5347 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) | |
5348 | sub_stripes = 1; | |
5349 | else | |
5350 | sub_stripes = map->sub_stripes; | |
5351 | ||
5352 | factor = map->num_stripes / sub_stripes; | |
5353 | num_stripes = min_t(u64, map->num_stripes, | |
5354 | sub_stripes * stripe_cnt); | |
5355 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); | |
5356 | stripe_index *= sub_stripes; | |
5357 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, | |
5358 | &remaining_stripes); | |
5359 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); | |
5360 | last_stripe *= sub_stripes; | |
5361 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | | |
5362 | BTRFS_BLOCK_GROUP_DUP)) { | |
5363 | num_stripes = map->num_stripes; | |
5364 | } else { | |
5365 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, | |
5366 | &stripe_index); | |
5367 | } | |
5368 | ||
5369 | bbio = alloc_btrfs_bio(num_stripes, 0); | |
5370 | if (!bbio) { | |
5371 | ret = -ENOMEM; | |
5372 | goto out; | |
5373 | } | |
5374 | ||
5375 | for (i = 0; i < num_stripes; i++) { | |
5376 | bbio->stripes[i].physical = | |
5377 | map->stripes[stripe_index].physical + | |
5378 | stripe_offset + stripe_nr * map->stripe_len; | |
5379 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; | |
5380 | ||
5381 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
5382 | BTRFS_BLOCK_GROUP_RAID10)) { | |
5383 | bbio->stripes[i].length = stripes_per_dev * | |
5384 | map->stripe_len; | |
5385 | ||
5386 | if (i / sub_stripes < remaining_stripes) | |
5387 | bbio->stripes[i].length += | |
5388 | map->stripe_len; | |
5389 | ||
5390 | /* | |
5391 | * Special for the first stripe and | |
5392 | * the last stripe: | |
5393 | * | |
5394 | * |-------|...|-------| | |
5395 | * |----------| | |
5396 | * off end_off | |
5397 | */ | |
5398 | if (i < sub_stripes) | |
5399 | bbio->stripes[i].length -= | |
5400 | stripe_offset; | |
5401 | ||
5402 | if (stripe_index >= last_stripe && | |
5403 | stripe_index <= (last_stripe + | |
5404 | sub_stripes - 1)) | |
5405 | bbio->stripes[i].length -= | |
5406 | stripe_end_offset; | |
5407 | ||
5408 | if (i == sub_stripes - 1) | |
5409 | stripe_offset = 0; | |
5410 | } else { | |
5411 | bbio->stripes[i].length = length; | |
5412 | } | |
5413 | ||
5414 | stripe_index++; | |
5415 | if (stripe_index == map->num_stripes) { | |
5416 | stripe_index = 0; | |
5417 | stripe_nr++; | |
5418 | } | |
5419 | } | |
5420 | ||
5421 | *bbio_ret = bbio; | |
5422 | bbio->map_type = map->type; | |
5423 | bbio->num_stripes = num_stripes; | |
5424 | out: | |
5425 | free_extent_map(em); | |
5426 | return ret; | |
5427 | } | |
5428 | ||
5ab56090 LB |
5429 | /* |
5430 | * In dev-replace case, for repair case (that's the only case where the mirror | |
5431 | * is selected explicitly when calling btrfs_map_block), blocks left of the | |
5432 | * left cursor can also be read from the target drive. | |
5433 | * | |
5434 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the | |
5435 | * array of stripes. | |
5436 | * For READ, it also needs to be supported using the same mirror number. | |
5437 | * | |
5438 | * If the requested block is not left of the left cursor, EIO is returned. This | |
5439 | * can happen because btrfs_num_copies() returns one more in the dev-replace | |
5440 | * case. | |
5441 | */ | |
5442 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, | |
5443 | u64 logical, u64 length, | |
5444 | u64 srcdev_devid, int *mirror_num, | |
5445 | u64 *physical) | |
5446 | { | |
5447 | struct btrfs_bio *bbio = NULL; | |
5448 | int num_stripes; | |
5449 | int index_srcdev = 0; | |
5450 | int found = 0; | |
5451 | u64 physical_of_found = 0; | |
5452 | int i; | |
5453 | int ret = 0; | |
5454 | ||
5455 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, | |
5456 | logical, &length, &bbio, 0, 0); | |
5457 | if (ret) { | |
5458 | ASSERT(bbio == NULL); | |
5459 | return ret; | |
5460 | } | |
5461 | ||
5462 | num_stripes = bbio->num_stripes; | |
5463 | if (*mirror_num > num_stripes) { | |
5464 | /* | |
5465 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, | |
5466 | * that means that the requested area is not left of the left | |
5467 | * cursor | |
5468 | */ | |
5469 | btrfs_put_bbio(bbio); | |
5470 | return -EIO; | |
5471 | } | |
5472 | ||
5473 | /* | |
5474 | * process the rest of the function using the mirror_num of the source | |
5475 | * drive. Therefore look it up first. At the end, patch the device | |
5476 | * pointer to the one of the target drive. | |
5477 | */ | |
5478 | for (i = 0; i < num_stripes; i++) { | |
5479 | if (bbio->stripes[i].dev->devid != srcdev_devid) | |
5480 | continue; | |
5481 | ||
5482 | /* | |
5483 | * In case of DUP, in order to keep it simple, only add the | |
5484 | * mirror with the lowest physical address | |
5485 | */ | |
5486 | if (found && | |
5487 | physical_of_found <= bbio->stripes[i].physical) | |
5488 | continue; | |
5489 | ||
5490 | index_srcdev = i; | |
5491 | found = 1; | |
5492 | physical_of_found = bbio->stripes[i].physical; | |
5493 | } | |
5494 | ||
5495 | btrfs_put_bbio(bbio); | |
5496 | ||
5497 | ASSERT(found); | |
5498 | if (!found) | |
5499 | return -EIO; | |
5500 | ||
5501 | *mirror_num = index_srcdev + 1; | |
5502 | *physical = physical_of_found; | |
5503 | return ret; | |
5504 | } | |
5505 | ||
73c0f228 LB |
5506 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
5507 | struct btrfs_bio **bbio_ret, | |
5508 | struct btrfs_dev_replace *dev_replace, | |
5509 | int *num_stripes_ret, int *max_errors_ret) | |
5510 | { | |
5511 | struct btrfs_bio *bbio = *bbio_ret; | |
5512 | u64 srcdev_devid = dev_replace->srcdev->devid; | |
5513 | int tgtdev_indexes = 0; | |
5514 | int num_stripes = *num_stripes_ret; | |
5515 | int max_errors = *max_errors_ret; | |
5516 | int i; | |
5517 | ||
5518 | if (op == BTRFS_MAP_WRITE) { | |
5519 | int index_where_to_add; | |
5520 | ||
5521 | /* | |
5522 | * duplicate the write operations while the dev replace | |
5523 | * procedure is running. Since the copying of the old disk to | |
5524 | * the new disk takes place at run time while the filesystem is | |
5525 | * mounted writable, the regular write operations to the old | |
5526 | * disk have to be duplicated to go to the new disk as well. | |
5527 | * | |
5528 | * Note that device->missing is handled by the caller, and that | |
5529 | * the write to the old disk is already set up in the stripes | |
5530 | * array. | |
5531 | */ | |
5532 | index_where_to_add = num_stripes; | |
5533 | for (i = 0; i < num_stripes; i++) { | |
5534 | if (bbio->stripes[i].dev->devid == srcdev_devid) { | |
5535 | /* write to new disk, too */ | |
5536 | struct btrfs_bio_stripe *new = | |
5537 | bbio->stripes + index_where_to_add; | |
5538 | struct btrfs_bio_stripe *old = | |
5539 | bbio->stripes + i; | |
5540 | ||
5541 | new->physical = old->physical; | |
5542 | new->length = old->length; | |
5543 | new->dev = dev_replace->tgtdev; | |
5544 | bbio->tgtdev_map[i] = index_where_to_add; | |
5545 | index_where_to_add++; | |
5546 | max_errors++; | |
5547 | tgtdev_indexes++; | |
5548 | } | |
5549 | } | |
5550 | num_stripes = index_where_to_add; | |
5551 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { | |
5552 | int index_srcdev = 0; | |
5553 | int found = 0; | |
5554 | u64 physical_of_found = 0; | |
5555 | ||
5556 | /* | |
5557 | * During the dev-replace procedure, the target drive can also | |
5558 | * be used to read data in case it is needed to repair a corrupt | |
5559 | * block elsewhere. This is possible if the requested area is | |
5560 | * left of the left cursor. In this area, the target drive is a | |
5561 | * full copy of the source drive. | |
5562 | */ | |
5563 | for (i = 0; i < num_stripes; i++) { | |
5564 | if (bbio->stripes[i].dev->devid == srcdev_devid) { | |
5565 | /* | |
5566 | * In case of DUP, in order to keep it simple, | |
5567 | * only add the mirror with the lowest physical | |
5568 | * address | |
5569 | */ | |
5570 | if (found && | |
5571 | physical_of_found <= | |
5572 | bbio->stripes[i].physical) | |
5573 | continue; | |
5574 | index_srcdev = i; | |
5575 | found = 1; | |
5576 | physical_of_found = bbio->stripes[i].physical; | |
5577 | } | |
5578 | } | |
5579 | if (found) { | |
5580 | struct btrfs_bio_stripe *tgtdev_stripe = | |
5581 | bbio->stripes + num_stripes; | |
5582 | ||
5583 | tgtdev_stripe->physical = physical_of_found; | |
5584 | tgtdev_stripe->length = | |
5585 | bbio->stripes[index_srcdev].length; | |
5586 | tgtdev_stripe->dev = dev_replace->tgtdev; | |
5587 | bbio->tgtdev_map[index_srcdev] = num_stripes; | |
5588 | ||
5589 | tgtdev_indexes++; | |
5590 | num_stripes++; | |
5591 | } | |
5592 | } | |
5593 | ||
5594 | *num_stripes_ret = num_stripes; | |
5595 | *max_errors_ret = max_errors; | |
5596 | bbio->num_tgtdevs = tgtdev_indexes; | |
5597 | *bbio_ret = bbio; | |
5598 | } | |
5599 | ||
2b19a1fe LB |
5600 | static bool need_full_stripe(enum btrfs_map_op op) |
5601 | { | |
5602 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); | |
5603 | } | |
5604 | ||
cf8cddd3 CH |
5605 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
5606 | enum btrfs_map_op op, | |
f2d8d74d | 5607 | u64 logical, u64 *length, |
a1d3c478 | 5608 | struct btrfs_bio **bbio_ret, |
8e5cfb55 | 5609 | int mirror_num, int need_raid_map) |
0b86a832 CM |
5610 | { |
5611 | struct extent_map *em; | |
5612 | struct map_lookup *map; | |
0b86a832 | 5613 | u64 offset; |
593060d7 CM |
5614 | u64 stripe_offset; |
5615 | u64 stripe_nr; | |
53b381b3 | 5616 | u64 stripe_len; |
9d644a62 | 5617 | u32 stripe_index; |
cea9e445 | 5618 | int i; |
de11cc12 | 5619 | int ret = 0; |
f2d8d74d | 5620 | int num_stripes; |
a236aed1 | 5621 | int max_errors = 0; |
2c8cdd6e | 5622 | int tgtdev_indexes = 0; |
a1d3c478 | 5623 | struct btrfs_bio *bbio = NULL; |
472262f3 SB |
5624 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
5625 | int dev_replace_is_ongoing = 0; | |
5626 | int num_alloc_stripes; | |
ad6d620e SB |
5627 | int patch_the_first_stripe_for_dev_replace = 0; |
5628 | u64 physical_to_patch_in_first_stripe = 0; | |
53b381b3 | 5629 | u64 raid56_full_stripe_start = (u64)-1; |
0b86a832 | 5630 | |
0b3d4cd3 LB |
5631 | if (op == BTRFS_MAP_DISCARD) |
5632 | return __btrfs_map_block_for_discard(fs_info, logical, | |
5633 | *length, bbio_ret); | |
5634 | ||
592d92ee LB |
5635 | em = get_chunk_map(fs_info, logical, *length); |
5636 | if (IS_ERR(em)) | |
5637 | return PTR_ERR(em); | |
0b86a832 | 5638 | |
95617d69 | 5639 | map = em->map_lookup; |
0b86a832 | 5640 | offset = logical - em->start; |
593060d7 | 5641 | |
53b381b3 | 5642 | stripe_len = map->stripe_len; |
593060d7 CM |
5643 | stripe_nr = offset; |
5644 | /* | |
5645 | * stripe_nr counts the total number of stripes we have to stride | |
5646 | * to get to this block | |
5647 | */ | |
47c5713f | 5648 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
593060d7 | 5649 | |
53b381b3 | 5650 | stripe_offset = stripe_nr * stripe_len; |
e042d1ec | 5651 | if (offset < stripe_offset) { |
5d163e0e JM |
5652 | btrfs_crit(fs_info, |
5653 | "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu", | |
e042d1ec JB |
5654 | stripe_offset, offset, em->start, logical, |
5655 | stripe_len); | |
5656 | free_extent_map(em); | |
5657 | return -EINVAL; | |
5658 | } | |
593060d7 CM |
5659 | |
5660 | /* stripe_offset is the offset of this block in its stripe*/ | |
5661 | stripe_offset = offset - stripe_offset; | |
5662 | ||
53b381b3 | 5663 | /* if we're here for raid56, we need to know the stripe aligned start */ |
ffe2d203 | 5664 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
53b381b3 DW |
5665 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
5666 | raid56_full_stripe_start = offset; | |
5667 | ||
5668 | /* allow a write of a full stripe, but make sure we don't | |
5669 | * allow straddling of stripes | |
5670 | */ | |
47c5713f DS |
5671 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
5672 | full_stripe_len); | |
53b381b3 DW |
5673 | raid56_full_stripe_start *= full_stripe_len; |
5674 | } | |
5675 | ||
0b3d4cd3 | 5676 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
53b381b3 DW |
5677 | u64 max_len; |
5678 | /* For writes to RAID[56], allow a full stripeset across all disks. | |
5679 | For other RAID types and for RAID[56] reads, just allow a single | |
5680 | stripe (on a single disk). */ | |
ffe2d203 | 5681 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
cf8cddd3 | 5682 | (op == BTRFS_MAP_WRITE)) { |
53b381b3 DW |
5683 | max_len = stripe_len * nr_data_stripes(map) - |
5684 | (offset - raid56_full_stripe_start); | |
5685 | } else { | |
5686 | /* we limit the length of each bio to what fits in a stripe */ | |
5687 | max_len = stripe_len - stripe_offset; | |
5688 | } | |
5689 | *length = min_t(u64, em->len - offset, max_len); | |
cea9e445 CM |
5690 | } else { |
5691 | *length = em->len - offset; | |
5692 | } | |
f2d8d74d | 5693 | |
53b381b3 DW |
5694 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
5695 | it cares about is the length */ | |
a1d3c478 | 5696 | if (!bbio_ret) |
cea9e445 CM |
5697 | goto out; |
5698 | ||
73beece9 | 5699 | btrfs_dev_replace_lock(dev_replace, 0); |
472262f3 SB |
5700 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
5701 | if (!dev_replace_is_ongoing) | |
73beece9 LB |
5702 | btrfs_dev_replace_unlock(dev_replace, 0); |
5703 | else | |
5704 | btrfs_dev_replace_set_lock_blocking(dev_replace); | |
472262f3 | 5705 | |
ad6d620e | 5706 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
2b19a1fe | 5707 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
5ab56090 LB |
5708 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
5709 | dev_replace->srcdev->devid, | |
5710 | &mirror_num, | |
5711 | &physical_to_patch_in_first_stripe); | |
5712 | if (ret) | |
ad6d620e | 5713 | goto out; |
5ab56090 LB |
5714 | else |
5715 | patch_the_first_stripe_for_dev_replace = 1; | |
ad6d620e SB |
5716 | } else if (mirror_num > map->num_stripes) { |
5717 | mirror_num = 0; | |
5718 | } | |
5719 | ||
f2d8d74d | 5720 | num_stripes = 1; |
cea9e445 | 5721 | stripe_index = 0; |
fce3bb9a | 5722 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
47c5713f DS |
5723 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
5724 | &stripe_index); | |
de483734 | 5725 | if (!need_full_stripe(op)) |
28e1cc7d | 5726 | mirror_num = 1; |
fce3bb9a | 5727 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
de483734 | 5728 | if (need_full_stripe(op)) |
f2d8d74d | 5729 | num_stripes = map->num_stripes; |
2fff734f | 5730 | else if (mirror_num) |
f188591e | 5731 | stripe_index = mirror_num - 1; |
dfe25020 | 5732 | else { |
30d9861f | 5733 | stripe_index = find_live_mirror(fs_info, map, 0, |
dfe25020 | 5734 | map->num_stripes, |
30d9861f SB |
5735 | current->pid % map->num_stripes, |
5736 | dev_replace_is_ongoing); | |
a1d3c478 | 5737 | mirror_num = stripe_index + 1; |
dfe25020 | 5738 | } |
2fff734f | 5739 | |
611f0e00 | 5740 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
de483734 | 5741 | if (need_full_stripe(op)) { |
f2d8d74d | 5742 | num_stripes = map->num_stripes; |
a1d3c478 | 5743 | } else if (mirror_num) { |
f188591e | 5744 | stripe_index = mirror_num - 1; |
a1d3c478 JS |
5745 | } else { |
5746 | mirror_num = 1; | |
5747 | } | |
2fff734f | 5748 | |
321aecc6 | 5749 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
9d644a62 | 5750 | u32 factor = map->num_stripes / map->sub_stripes; |
321aecc6 | 5751 | |
47c5713f | 5752 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
321aecc6 CM |
5753 | stripe_index *= map->sub_stripes; |
5754 | ||
de483734 | 5755 | if (need_full_stripe(op)) |
f2d8d74d | 5756 | num_stripes = map->sub_stripes; |
321aecc6 CM |
5757 | else if (mirror_num) |
5758 | stripe_index += mirror_num - 1; | |
dfe25020 | 5759 | else { |
3e74317a | 5760 | int old_stripe_index = stripe_index; |
30d9861f SB |
5761 | stripe_index = find_live_mirror(fs_info, map, |
5762 | stripe_index, | |
dfe25020 | 5763 | map->sub_stripes, stripe_index + |
30d9861f SB |
5764 | current->pid % map->sub_stripes, |
5765 | dev_replace_is_ongoing); | |
3e74317a | 5766 | mirror_num = stripe_index - old_stripe_index + 1; |
dfe25020 | 5767 | } |
53b381b3 | 5768 | |
ffe2d203 | 5769 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
de483734 | 5770 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
53b381b3 | 5771 | /* push stripe_nr back to the start of the full stripe */ |
42c61ab6 | 5772 | stripe_nr = div64_u64(raid56_full_stripe_start, |
b8b93add | 5773 | stripe_len * nr_data_stripes(map)); |
53b381b3 DW |
5774 | |
5775 | /* RAID[56] write or recovery. Return all stripes */ | |
5776 | num_stripes = map->num_stripes; | |
5777 | max_errors = nr_parity_stripes(map); | |
5778 | ||
53b381b3 DW |
5779 | *length = map->stripe_len; |
5780 | stripe_index = 0; | |
5781 | stripe_offset = 0; | |
5782 | } else { | |
5783 | /* | |
5784 | * Mirror #0 or #1 means the original data block. | |
5785 | * Mirror #2 is RAID5 parity block. | |
5786 | * Mirror #3 is RAID6 Q block. | |
5787 | */ | |
47c5713f DS |
5788 | stripe_nr = div_u64_rem(stripe_nr, |
5789 | nr_data_stripes(map), &stripe_index); | |
53b381b3 DW |
5790 | if (mirror_num > 1) |
5791 | stripe_index = nr_data_stripes(map) + | |
5792 | mirror_num - 2; | |
5793 | ||
5794 | /* We distribute the parity blocks across stripes */ | |
47c5713f DS |
5795 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
5796 | &stripe_index); | |
de483734 | 5797 | if (!need_full_stripe(op) && mirror_num <= 1) |
28e1cc7d | 5798 | mirror_num = 1; |
53b381b3 | 5799 | } |
8790d502 CM |
5800 | } else { |
5801 | /* | |
47c5713f DS |
5802 | * after this, stripe_nr is the number of stripes on this |
5803 | * device we have to walk to find the data, and stripe_index is | |
5804 | * the number of our device in the stripe array | |
8790d502 | 5805 | */ |
47c5713f DS |
5806 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
5807 | &stripe_index); | |
a1d3c478 | 5808 | mirror_num = stripe_index + 1; |
8790d502 | 5809 | } |
e042d1ec | 5810 | if (stripe_index >= map->num_stripes) { |
5d163e0e JM |
5811 | btrfs_crit(fs_info, |
5812 | "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u", | |
e042d1ec JB |
5813 | stripe_index, map->num_stripes); |
5814 | ret = -EINVAL; | |
5815 | goto out; | |
5816 | } | |
cea9e445 | 5817 | |
472262f3 | 5818 | num_alloc_stripes = num_stripes; |
6fad823f | 5819 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
0b3d4cd3 | 5820 | if (op == BTRFS_MAP_WRITE) |
ad6d620e | 5821 | num_alloc_stripes <<= 1; |
cf8cddd3 | 5822 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
ad6d620e | 5823 | num_alloc_stripes++; |
2c8cdd6e | 5824 | tgtdev_indexes = num_stripes; |
ad6d620e | 5825 | } |
2c8cdd6e | 5826 | |
6e9606d2 | 5827 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
de11cc12 LZ |
5828 | if (!bbio) { |
5829 | ret = -ENOMEM; | |
5830 | goto out; | |
5831 | } | |
6fad823f | 5832 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
2c8cdd6e | 5833 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
de11cc12 | 5834 | |
8e5cfb55 | 5835 | /* build raid_map */ |
2b19a1fe LB |
5836 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
5837 | (need_full_stripe(op) || mirror_num > 1)) { | |
8e5cfb55 | 5838 | u64 tmp; |
9d644a62 | 5839 | unsigned rot; |
8e5cfb55 ZL |
5840 | |
5841 | bbio->raid_map = (u64 *)((void *)bbio->stripes + | |
5842 | sizeof(struct btrfs_bio_stripe) * | |
5843 | num_alloc_stripes + | |
5844 | sizeof(int) * tgtdev_indexes); | |
5845 | ||
5846 | /* Work out the disk rotation on this stripe-set */ | |
47c5713f | 5847 | div_u64_rem(stripe_nr, num_stripes, &rot); |
8e5cfb55 ZL |
5848 | |
5849 | /* Fill in the logical address of each stripe */ | |
5850 | tmp = stripe_nr * nr_data_stripes(map); | |
5851 | for (i = 0; i < nr_data_stripes(map); i++) | |
5852 | bbio->raid_map[(i+rot) % num_stripes] = | |
5853 | em->start + (tmp + i) * map->stripe_len; | |
5854 | ||
5855 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; | |
5856 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) | |
5857 | bbio->raid_map[(i+rot+1) % num_stripes] = | |
5858 | RAID6_Q_STRIPE; | |
5859 | } | |
5860 | ||
b89203f7 | 5861 | |
0b3d4cd3 LB |
5862 | for (i = 0; i < num_stripes; i++) { |
5863 | bbio->stripes[i].physical = | |
5864 | map->stripes[stripe_index].physical + | |
5865 | stripe_offset + | |
5866 | stripe_nr * map->stripe_len; | |
5867 | bbio->stripes[i].dev = | |
5868 | map->stripes[stripe_index].dev; | |
5869 | stripe_index++; | |
593060d7 | 5870 | } |
de11cc12 | 5871 | |
2b19a1fe | 5872 | if (need_full_stripe(op)) |
d20983b4 | 5873 | max_errors = btrfs_chunk_max_errors(map); |
de11cc12 | 5874 | |
8e5cfb55 ZL |
5875 | if (bbio->raid_map) |
5876 | sort_parity_stripes(bbio, num_stripes); | |
cc7539ed | 5877 | |
73c0f228 | 5878 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
2b19a1fe | 5879 | need_full_stripe(op)) { |
73c0f228 LB |
5880 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
5881 | &max_errors); | |
472262f3 SB |
5882 | } |
5883 | ||
de11cc12 | 5884 | *bbio_ret = bbio; |
10f11900 | 5885 | bbio->map_type = map->type; |
de11cc12 LZ |
5886 | bbio->num_stripes = num_stripes; |
5887 | bbio->max_errors = max_errors; | |
5888 | bbio->mirror_num = mirror_num; | |
ad6d620e SB |
5889 | |
5890 | /* | |
5891 | * this is the case that REQ_READ && dev_replace_is_ongoing && | |
5892 | * mirror_num == num_stripes + 1 && dev_replace target drive is | |
5893 | * available as a mirror | |
5894 | */ | |
5895 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { | |
5896 | WARN_ON(num_stripes > 1); | |
5897 | bbio->stripes[0].dev = dev_replace->tgtdev; | |
5898 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; | |
5899 | bbio->mirror_num = map->num_stripes + 1; | |
5900 | } | |
cea9e445 | 5901 | out: |
73beece9 LB |
5902 | if (dev_replace_is_ongoing) { |
5903 | btrfs_dev_replace_clear_lock_blocking(dev_replace); | |
5904 | btrfs_dev_replace_unlock(dev_replace, 0); | |
5905 | } | |
0b86a832 | 5906 | free_extent_map(em); |
de11cc12 | 5907 | return ret; |
0b86a832 CM |
5908 | } |
5909 | ||
cf8cddd3 | 5910 | int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
f2d8d74d | 5911 | u64 logical, u64 *length, |
a1d3c478 | 5912 | struct btrfs_bio **bbio_ret, int mirror_num) |
f2d8d74d | 5913 | { |
b3d3fa51 | 5914 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
8e5cfb55 | 5915 | mirror_num, 0); |
f2d8d74d CM |
5916 | } |
5917 | ||
af8e2d1d | 5918 | /* For Scrub/replace */ |
cf8cddd3 | 5919 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
af8e2d1d | 5920 | u64 logical, u64 *length, |
825ad4c9 | 5921 | struct btrfs_bio **bbio_ret) |
af8e2d1d | 5922 | { |
825ad4c9 | 5923 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1); |
af8e2d1d MX |
5924 | } |
5925 | ||
ab8d0fc4 | 5926 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, |
a512bbf8 YZ |
5927 | u64 chunk_start, u64 physical, u64 devid, |
5928 | u64 **logical, int *naddrs, int *stripe_len) | |
5929 | { | |
a512bbf8 YZ |
5930 | struct extent_map *em; |
5931 | struct map_lookup *map; | |
5932 | u64 *buf; | |
5933 | u64 bytenr; | |
5934 | u64 length; | |
5935 | u64 stripe_nr; | |
53b381b3 | 5936 | u64 rmap_len; |
a512bbf8 YZ |
5937 | int i, j, nr = 0; |
5938 | ||
592d92ee LB |
5939 | em = get_chunk_map(fs_info, chunk_start, 1); |
5940 | if (IS_ERR(em)) | |
835d974f | 5941 | return -EIO; |
835d974f | 5942 | |
95617d69 | 5943 | map = em->map_lookup; |
a512bbf8 | 5944 | length = em->len; |
53b381b3 DW |
5945 | rmap_len = map->stripe_len; |
5946 | ||
a512bbf8 | 5947 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
b8b93add | 5948 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
a512bbf8 | 5949 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
b8b93add | 5950 | length = div_u64(length, map->num_stripes); |
ffe2d203 | 5951 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
b8b93add | 5952 | length = div_u64(length, nr_data_stripes(map)); |
53b381b3 DW |
5953 | rmap_len = map->stripe_len * nr_data_stripes(map); |
5954 | } | |
a512bbf8 | 5955 | |
31e818fe | 5956 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
79787eaa | 5957 | BUG_ON(!buf); /* -ENOMEM */ |
a512bbf8 YZ |
5958 | |
5959 | for (i = 0; i < map->num_stripes; i++) { | |
5960 | if (devid && map->stripes[i].dev->devid != devid) | |
5961 | continue; | |
5962 | if (map->stripes[i].physical > physical || | |
5963 | map->stripes[i].physical + length <= physical) | |
5964 | continue; | |
5965 | ||
5966 | stripe_nr = physical - map->stripes[i].physical; | |
42c61ab6 | 5967 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
a512bbf8 YZ |
5968 | |
5969 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
5970 | stripe_nr = stripe_nr * map->num_stripes + i; | |
b8b93add | 5971 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
a512bbf8 YZ |
5972 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
5973 | stripe_nr = stripe_nr * map->num_stripes + i; | |
53b381b3 DW |
5974 | } /* else if RAID[56], multiply by nr_data_stripes(). |
5975 | * Alternatively, just use rmap_len below instead of | |
5976 | * map->stripe_len */ | |
5977 | ||
5978 | bytenr = chunk_start + stripe_nr * rmap_len; | |
934d375b | 5979 | WARN_ON(nr >= map->num_stripes); |
a512bbf8 YZ |
5980 | for (j = 0; j < nr; j++) { |
5981 | if (buf[j] == bytenr) | |
5982 | break; | |
5983 | } | |
934d375b CM |
5984 | if (j == nr) { |
5985 | WARN_ON(nr >= map->num_stripes); | |
a512bbf8 | 5986 | buf[nr++] = bytenr; |
934d375b | 5987 | } |
a512bbf8 YZ |
5988 | } |
5989 | ||
a512bbf8 YZ |
5990 | *logical = buf; |
5991 | *naddrs = nr; | |
53b381b3 | 5992 | *stripe_len = rmap_len; |
a512bbf8 YZ |
5993 | |
5994 | free_extent_map(em); | |
5995 | return 0; | |
f2d8d74d CM |
5996 | } |
5997 | ||
4246a0b6 | 5998 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio) |
8408c716 | 5999 | { |
326e1dbb MS |
6000 | bio->bi_private = bbio->private; |
6001 | bio->bi_end_io = bbio->end_io; | |
4246a0b6 | 6002 | bio_endio(bio); |
326e1dbb | 6003 | |
6e9606d2 | 6004 | btrfs_put_bbio(bbio); |
8408c716 MX |
6005 | } |
6006 | ||
4246a0b6 | 6007 | static void btrfs_end_bio(struct bio *bio) |
8790d502 | 6008 | { |
9be3395b | 6009 | struct btrfs_bio *bbio = bio->bi_private; |
7d2b4daa | 6010 | int is_orig_bio = 0; |
8790d502 | 6011 | |
4e4cbee9 | 6012 | if (bio->bi_status) { |
a1d3c478 | 6013 | atomic_inc(&bbio->error); |
4e4cbee9 CH |
6014 | if (bio->bi_status == BLK_STS_IOERR || |
6015 | bio->bi_status == BLK_STS_TARGET) { | |
442a4f63 | 6016 | unsigned int stripe_index = |
9be3395b | 6017 | btrfs_io_bio(bio)->stripe_index; |
65f53338 | 6018 | struct btrfs_device *dev; |
442a4f63 SB |
6019 | |
6020 | BUG_ON(stripe_index >= bbio->num_stripes); | |
6021 | dev = bbio->stripes[stripe_index].dev; | |
597a60fa | 6022 | if (dev->bdev) { |
37226b21 | 6023 | if (bio_op(bio) == REQ_OP_WRITE) |
597a60fa SB |
6024 | btrfs_dev_stat_inc(dev, |
6025 | BTRFS_DEV_STAT_WRITE_ERRS); | |
6026 | else | |
6027 | btrfs_dev_stat_inc(dev, | |
6028 | BTRFS_DEV_STAT_READ_ERRS); | |
70fd7614 | 6029 | if (bio->bi_opf & REQ_PREFLUSH) |
597a60fa SB |
6030 | btrfs_dev_stat_inc(dev, |
6031 | BTRFS_DEV_STAT_FLUSH_ERRS); | |
6032 | btrfs_dev_stat_print_on_error(dev); | |
6033 | } | |
442a4f63 SB |
6034 | } |
6035 | } | |
8790d502 | 6036 | |
a1d3c478 | 6037 | if (bio == bbio->orig_bio) |
7d2b4daa CM |
6038 | is_orig_bio = 1; |
6039 | ||
c404e0dc MX |
6040 | btrfs_bio_counter_dec(bbio->fs_info); |
6041 | ||
a1d3c478 | 6042 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
7d2b4daa CM |
6043 | if (!is_orig_bio) { |
6044 | bio_put(bio); | |
a1d3c478 | 6045 | bio = bbio->orig_bio; |
7d2b4daa | 6046 | } |
c7b22bb1 | 6047 | |
9be3395b | 6048 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
a236aed1 | 6049 | /* only send an error to the higher layers if it is |
53b381b3 | 6050 | * beyond the tolerance of the btrfs bio |
a236aed1 | 6051 | */ |
a1d3c478 | 6052 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
4e4cbee9 | 6053 | bio->bi_status = BLK_STS_IOERR; |
5dbc8fca | 6054 | } else { |
1259ab75 CM |
6055 | /* |
6056 | * this bio is actually up to date, we didn't | |
6057 | * go over the max number of errors | |
6058 | */ | |
2dbe0c77 | 6059 | bio->bi_status = BLK_STS_OK; |
1259ab75 | 6060 | } |
c55f1396 | 6061 | |
4246a0b6 | 6062 | btrfs_end_bbio(bbio, bio); |
7d2b4daa | 6063 | } else if (!is_orig_bio) { |
8790d502 CM |
6064 | bio_put(bio); |
6065 | } | |
8790d502 CM |
6066 | } |
6067 | ||
8b712842 CM |
6068 | /* |
6069 | * see run_scheduled_bios for a description of why bios are collected for | |
6070 | * async submit. | |
6071 | * | |
6072 | * This will add one bio to the pending list for a device and make sure | |
6073 | * the work struct is scheduled. | |
6074 | */ | |
2ff7e61e | 6075 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
4e49ea4a | 6076 | struct bio *bio) |
8b712842 | 6077 | { |
0b246afa | 6078 | struct btrfs_fs_info *fs_info = device->fs_info; |
8b712842 | 6079 | int should_queue = 1; |
ffbd517d | 6080 | struct btrfs_pending_bios *pending_bios; |
8b712842 | 6081 | |
53b381b3 | 6082 | if (device->missing || !device->bdev) { |
4246a0b6 | 6083 | bio_io_error(bio); |
53b381b3 DW |
6084 | return; |
6085 | } | |
6086 | ||
8b712842 | 6087 | /* don't bother with additional async steps for reads, right now */ |
37226b21 | 6088 | if (bio_op(bio) == REQ_OP_READ) { |
492bb6de | 6089 | bio_get(bio); |
4e49ea4a | 6090 | btrfsic_submit_bio(bio); |
492bb6de | 6091 | bio_put(bio); |
143bede5 | 6092 | return; |
8b712842 CM |
6093 | } |
6094 | ||
492bb6de | 6095 | WARN_ON(bio->bi_next); |
8b712842 | 6096 | bio->bi_next = NULL; |
8b712842 CM |
6097 | |
6098 | spin_lock(&device->io_lock); | |
67f055c7 | 6099 | if (op_is_sync(bio->bi_opf)) |
ffbd517d CM |
6100 | pending_bios = &device->pending_sync_bios; |
6101 | else | |
6102 | pending_bios = &device->pending_bios; | |
8b712842 | 6103 | |
ffbd517d CM |
6104 | if (pending_bios->tail) |
6105 | pending_bios->tail->bi_next = bio; | |
8b712842 | 6106 | |
ffbd517d CM |
6107 | pending_bios->tail = bio; |
6108 | if (!pending_bios->head) | |
6109 | pending_bios->head = bio; | |
8b712842 CM |
6110 | if (device->running_pending) |
6111 | should_queue = 0; | |
6112 | ||
6113 | spin_unlock(&device->io_lock); | |
6114 | ||
6115 | if (should_queue) | |
0b246afa | 6116 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
8b712842 CM |
6117 | } |
6118 | ||
2ff7e61e JM |
6119 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
6120 | u64 physical, int dev_nr, int async) | |
de1ee92a JB |
6121 | { |
6122 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; | |
2ff7e61e | 6123 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
de1ee92a JB |
6124 | |
6125 | bio->bi_private = bbio; | |
9be3395b | 6126 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
de1ee92a | 6127 | bio->bi_end_io = btrfs_end_bio; |
4f024f37 | 6128 | bio->bi_iter.bi_sector = physical >> 9; |
de1ee92a JB |
6129 | #ifdef DEBUG |
6130 | { | |
6131 | struct rcu_string *name; | |
6132 | ||
6133 | rcu_read_lock(); | |
6134 | name = rcu_dereference(dev->name); | |
ab8d0fc4 JM |
6135 | btrfs_debug(fs_info, |
6136 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", | |
6137 | bio_op(bio), bio->bi_opf, | |
6138 | (u64)bio->bi_iter.bi_sector, | |
6139 | (u_long)dev->bdev->bd_dev, name->str, dev->devid, | |
6140 | bio->bi_iter.bi_size); | |
de1ee92a JB |
6141 | rcu_read_unlock(); |
6142 | } | |
6143 | #endif | |
74d46992 | 6144 | bio_set_dev(bio, dev->bdev); |
c404e0dc | 6145 | |
2ff7e61e | 6146 | btrfs_bio_counter_inc_noblocked(fs_info); |
c404e0dc | 6147 | |
de1ee92a | 6148 | if (async) |
2ff7e61e | 6149 | btrfs_schedule_bio(dev, bio); |
de1ee92a | 6150 | else |
4e49ea4a | 6151 | btrfsic_submit_bio(bio); |
de1ee92a JB |
6152 | } |
6153 | ||
de1ee92a JB |
6154 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
6155 | { | |
6156 | atomic_inc(&bbio->error); | |
6157 | if (atomic_dec_and_test(&bbio->stripes_pending)) { | |
01327610 | 6158 | /* Should be the original bio. */ |
8408c716 MX |
6159 | WARN_ON(bio != bbio->orig_bio); |
6160 | ||
9be3395b | 6161 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
4f024f37 | 6162 | bio->bi_iter.bi_sector = logical >> 9; |
102ed2c5 AJ |
6163 | if (atomic_read(&bbio->error) > bbio->max_errors) |
6164 | bio->bi_status = BLK_STS_IOERR; | |
6165 | else | |
6166 | bio->bi_status = BLK_STS_OK; | |
4246a0b6 | 6167 | btrfs_end_bbio(bbio, bio); |
de1ee92a JB |
6168 | } |
6169 | } | |
6170 | ||
58efbc9f OS |
6171 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
6172 | int mirror_num, int async_submit) | |
0b86a832 | 6173 | { |
0b86a832 | 6174 | struct btrfs_device *dev; |
8790d502 | 6175 | struct bio *first_bio = bio; |
4f024f37 | 6176 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
0b86a832 CM |
6177 | u64 length = 0; |
6178 | u64 map_length; | |
0b86a832 | 6179 | int ret; |
08da757d ZL |
6180 | int dev_nr; |
6181 | int total_devs; | |
a1d3c478 | 6182 | struct btrfs_bio *bbio = NULL; |
0b86a832 | 6183 | |
4f024f37 | 6184 | length = bio->bi_iter.bi_size; |
0b86a832 | 6185 | map_length = length; |
cea9e445 | 6186 | |
0b246afa | 6187 | btrfs_bio_counter_inc_blocked(fs_info); |
bd7d63c2 | 6188 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
37226b21 | 6189 | &map_length, &bbio, mirror_num, 1); |
c404e0dc | 6190 | if (ret) { |
0b246afa | 6191 | btrfs_bio_counter_dec(fs_info); |
58efbc9f | 6192 | return errno_to_blk_status(ret); |
c404e0dc | 6193 | } |
cea9e445 | 6194 | |
a1d3c478 | 6195 | total_devs = bbio->num_stripes; |
53b381b3 DW |
6196 | bbio->orig_bio = first_bio; |
6197 | bbio->private = first_bio->bi_private; | |
6198 | bbio->end_io = first_bio->bi_end_io; | |
0b246afa | 6199 | bbio->fs_info = fs_info; |
53b381b3 DW |
6200 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
6201 | ||
ad1ba2a0 | 6202 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
37226b21 | 6203 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
53b381b3 DW |
6204 | /* In this case, map_length has been set to the length of |
6205 | a single stripe; not the whole write */ | |
37226b21 | 6206 | if (bio_op(bio) == REQ_OP_WRITE) { |
2ff7e61e JM |
6207 | ret = raid56_parity_write(fs_info, bio, bbio, |
6208 | map_length); | |
53b381b3 | 6209 | } else { |
2ff7e61e JM |
6210 | ret = raid56_parity_recover(fs_info, bio, bbio, |
6211 | map_length, mirror_num, 1); | |
53b381b3 | 6212 | } |
4245215d | 6213 | |
0b246afa | 6214 | btrfs_bio_counter_dec(fs_info); |
58efbc9f | 6215 | return errno_to_blk_status(ret); |
53b381b3 DW |
6216 | } |
6217 | ||
cea9e445 | 6218 | if (map_length < length) { |
0b246afa | 6219 | btrfs_crit(fs_info, |
5d163e0e JM |
6220 | "mapping failed logical %llu bio len %llu len %llu", |
6221 | logical, length, map_length); | |
cea9e445 CM |
6222 | BUG(); |
6223 | } | |
a1d3c478 | 6224 | |
08da757d | 6225 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
de1ee92a | 6226 | dev = bbio->stripes[dev_nr].dev; |
37226b21 | 6227 | if (!dev || !dev->bdev || |
a967efb3 | 6228 | (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) { |
de1ee92a | 6229 | bbio_error(bbio, first_bio, logical); |
de1ee92a JB |
6230 | continue; |
6231 | } | |
6232 | ||
3aa8e074 | 6233 | if (dev_nr < total_devs - 1) |
8b6c1d56 | 6234 | bio = btrfs_bio_clone(first_bio); |
3aa8e074 | 6235 | else |
a1d3c478 | 6236 | bio = first_bio; |
de1ee92a | 6237 | |
2ff7e61e JM |
6238 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
6239 | dev_nr, async_submit); | |
8790d502 | 6240 | } |
0b246afa | 6241 | btrfs_bio_counter_dec(fs_info); |
58efbc9f | 6242 | return BLK_STS_OK; |
0b86a832 CM |
6243 | } |
6244 | ||
aa1b8cd4 | 6245 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, |
2b82032c | 6246 | u8 *uuid, u8 *fsid) |
0b86a832 | 6247 | { |
2b82032c YZ |
6248 | struct btrfs_device *device; |
6249 | struct btrfs_fs_devices *cur_devices; | |
6250 | ||
aa1b8cd4 | 6251 | cur_devices = fs_info->fs_devices; |
2b82032c YZ |
6252 | while (cur_devices) { |
6253 | if (!fsid || | |
44880fdc | 6254 | !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) { |
35c70103 | 6255 | device = find_device(cur_devices, devid, uuid); |
2b82032c YZ |
6256 | if (device) |
6257 | return device; | |
6258 | } | |
6259 | cur_devices = cur_devices->seed; | |
6260 | } | |
6261 | return NULL; | |
0b86a832 CM |
6262 | } |
6263 | ||
2ff7e61e | 6264 | static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices, |
dfe25020 CM |
6265 | u64 devid, u8 *dev_uuid) |
6266 | { | |
6267 | struct btrfs_device *device; | |
dfe25020 | 6268 | |
12bd2fc0 ID |
6269 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
6270 | if (IS_ERR(device)) | |
adfb69af | 6271 | return device; |
12bd2fc0 ID |
6272 | |
6273 | list_add(&device->dev_list, &fs_devices->devices); | |
e4404d6e | 6274 | device->fs_devices = fs_devices; |
dfe25020 | 6275 | fs_devices->num_devices++; |
12bd2fc0 ID |
6276 | |
6277 | device->missing = 1; | |
cd02dca5 | 6278 | fs_devices->missing_devices++; |
12bd2fc0 | 6279 | |
dfe25020 CM |
6280 | return device; |
6281 | } | |
6282 | ||
12bd2fc0 ID |
6283 | /** |
6284 | * btrfs_alloc_device - allocate struct btrfs_device | |
6285 | * @fs_info: used only for generating a new devid, can be NULL if | |
6286 | * devid is provided (i.e. @devid != NULL). | |
6287 | * @devid: a pointer to devid for this device. If NULL a new devid | |
6288 | * is generated. | |
6289 | * @uuid: a pointer to UUID for this device. If NULL a new UUID | |
6290 | * is generated. | |
6291 | * | |
6292 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() | |
6293 | * on error. Returned struct is not linked onto any lists and can be | |
6294 | * destroyed with kfree() right away. | |
6295 | */ | |
6296 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, | |
6297 | const u64 *devid, | |
6298 | const u8 *uuid) | |
6299 | { | |
6300 | struct btrfs_device *dev; | |
6301 | u64 tmp; | |
6302 | ||
fae7f21c | 6303 | if (WARN_ON(!devid && !fs_info)) |
12bd2fc0 | 6304 | return ERR_PTR(-EINVAL); |
12bd2fc0 ID |
6305 | |
6306 | dev = __alloc_device(); | |
6307 | if (IS_ERR(dev)) | |
6308 | return dev; | |
6309 | ||
6310 | if (devid) | |
6311 | tmp = *devid; | |
6312 | else { | |
6313 | int ret; | |
6314 | ||
6315 | ret = find_next_devid(fs_info, &tmp); | |
6316 | if (ret) { | |
3065ae5b | 6317 | bio_put(dev->flush_bio); |
12bd2fc0 ID |
6318 | kfree(dev); |
6319 | return ERR_PTR(ret); | |
6320 | } | |
6321 | } | |
6322 | dev->devid = tmp; | |
6323 | ||
6324 | if (uuid) | |
6325 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); | |
6326 | else | |
6327 | generate_random_uuid(dev->uuid); | |
6328 | ||
9e0af237 LB |
6329 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
6330 | pending_bios_fn, NULL, NULL); | |
12bd2fc0 ID |
6331 | |
6332 | return dev; | |
6333 | } | |
6334 | ||
e06cd3dd | 6335 | /* Return -EIO if any error, otherwise return 0. */ |
2ff7e61e | 6336 | static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
e06cd3dd LB |
6337 | struct extent_buffer *leaf, |
6338 | struct btrfs_chunk *chunk, u64 logical) | |
0b86a832 | 6339 | { |
0b86a832 | 6340 | u64 length; |
f04b772b | 6341 | u64 stripe_len; |
e06cd3dd LB |
6342 | u16 num_stripes; |
6343 | u16 sub_stripes; | |
6344 | u64 type; | |
0b86a832 | 6345 | |
e17cade2 | 6346 | length = btrfs_chunk_length(leaf, chunk); |
f04b772b QW |
6347 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
6348 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
e06cd3dd LB |
6349 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
6350 | type = btrfs_chunk_type(leaf, chunk); | |
6351 | ||
f04b772b | 6352 | if (!num_stripes) { |
0b246afa | 6353 | btrfs_err(fs_info, "invalid chunk num_stripes: %u", |
f04b772b QW |
6354 | num_stripes); |
6355 | return -EIO; | |
6356 | } | |
0b246afa JM |
6357 | if (!IS_ALIGNED(logical, fs_info->sectorsize)) { |
6358 | btrfs_err(fs_info, "invalid chunk logical %llu", logical); | |
f04b772b QW |
6359 | return -EIO; |
6360 | } | |
0b246afa JM |
6361 | if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { |
6362 | btrfs_err(fs_info, "invalid chunk sectorsize %u", | |
e06cd3dd LB |
6363 | btrfs_chunk_sector_size(leaf, chunk)); |
6364 | return -EIO; | |
6365 | } | |
0b246afa JM |
6366 | if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { |
6367 | btrfs_err(fs_info, "invalid chunk length %llu", length); | |
f04b772b QW |
6368 | return -EIO; |
6369 | } | |
3d8da678 | 6370 | if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { |
0b246afa | 6371 | btrfs_err(fs_info, "invalid chunk stripe length: %llu", |
f04b772b QW |
6372 | stripe_len); |
6373 | return -EIO; | |
6374 | } | |
6375 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & | |
e06cd3dd | 6376 | type) { |
0b246afa | 6377 | btrfs_err(fs_info, "unrecognized chunk type: %llu", |
f04b772b QW |
6378 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
6379 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & | |
6380 | btrfs_chunk_type(leaf, chunk)); | |
6381 | return -EIO; | |
6382 | } | |
e06cd3dd LB |
6383 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || |
6384 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || | |
6385 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || | |
6386 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || | |
6387 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || | |
6388 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && | |
6389 | num_stripes != 1)) { | |
0b246afa | 6390 | btrfs_err(fs_info, |
e06cd3dd LB |
6391 | "invalid num_stripes:sub_stripes %u:%u for profile %llu", |
6392 | num_stripes, sub_stripes, | |
6393 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); | |
6394 | return -EIO; | |
6395 | } | |
6396 | ||
6397 | return 0; | |
6398 | } | |
6399 | ||
5a2b8e60 | 6400 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
2b902dfc | 6401 | u64 devid, u8 *uuid, bool error) |
5a2b8e60 | 6402 | { |
2b902dfc AJ |
6403 | if (error) |
6404 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", | |
6405 | devid, uuid); | |
6406 | else | |
6407 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", | |
6408 | devid, uuid); | |
5a2b8e60 AJ |
6409 | } |
6410 | ||
2ff7e61e | 6411 | static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key, |
e06cd3dd LB |
6412 | struct extent_buffer *leaf, |
6413 | struct btrfs_chunk *chunk) | |
6414 | { | |
0b246afa | 6415 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
e06cd3dd LB |
6416 | struct map_lookup *map; |
6417 | struct extent_map *em; | |
6418 | u64 logical; | |
6419 | u64 length; | |
e06cd3dd LB |
6420 | u64 devid; |
6421 | u8 uuid[BTRFS_UUID_SIZE]; | |
6422 | int num_stripes; | |
6423 | int ret; | |
6424 | int i; | |
6425 | ||
6426 | logical = key->offset; | |
6427 | length = btrfs_chunk_length(leaf, chunk); | |
e06cd3dd LB |
6428 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
6429 | ||
2ff7e61e | 6430 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical); |
e06cd3dd LB |
6431 | if (ret) |
6432 | return ret; | |
a061fc8d | 6433 | |
890871be | 6434 | read_lock(&map_tree->map_tree.lock); |
0b86a832 | 6435 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
890871be | 6436 | read_unlock(&map_tree->map_tree.lock); |
0b86a832 CM |
6437 | |
6438 | /* already mapped? */ | |
6439 | if (em && em->start <= logical && em->start + em->len > logical) { | |
6440 | free_extent_map(em); | |
0b86a832 CM |
6441 | return 0; |
6442 | } else if (em) { | |
6443 | free_extent_map(em); | |
6444 | } | |
0b86a832 | 6445 | |
172ddd60 | 6446 | em = alloc_extent_map(); |
0b86a832 CM |
6447 | if (!em) |
6448 | return -ENOMEM; | |
593060d7 | 6449 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
0b86a832 CM |
6450 | if (!map) { |
6451 | free_extent_map(em); | |
6452 | return -ENOMEM; | |
6453 | } | |
6454 | ||
298a8f9c | 6455 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
95617d69 | 6456 | em->map_lookup = map; |
0b86a832 CM |
6457 | em->start = logical; |
6458 | em->len = length; | |
70c8a91c | 6459 | em->orig_start = 0; |
0b86a832 | 6460 | em->block_start = 0; |
c8b97818 | 6461 | em->block_len = em->len; |
0b86a832 | 6462 | |
593060d7 CM |
6463 | map->num_stripes = num_stripes; |
6464 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
6465 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
593060d7 CM |
6466 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
6467 | map->type = btrfs_chunk_type(leaf, chunk); | |
321aecc6 | 6468 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
593060d7 CM |
6469 | for (i = 0; i < num_stripes; i++) { |
6470 | map->stripes[i].physical = | |
6471 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
6472 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
a443755f CM |
6473 | read_extent_buffer(leaf, uuid, (unsigned long) |
6474 | btrfs_stripe_dev_uuid_nr(chunk, i), | |
6475 | BTRFS_UUID_SIZE); | |
0b246afa | 6476 | map->stripes[i].dev = btrfs_find_device(fs_info, devid, |
aa1b8cd4 | 6477 | uuid, NULL); |
3cdde224 | 6478 | if (!map->stripes[i].dev && |
0b246afa | 6479 | !btrfs_test_opt(fs_info, DEGRADED)) { |
593060d7 | 6480 | free_extent_map(em); |
2b902dfc | 6481 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
45dbdbc9 | 6482 | return -ENOENT; |
593060d7 | 6483 | } |
dfe25020 CM |
6484 | if (!map->stripes[i].dev) { |
6485 | map->stripes[i].dev = | |
2ff7e61e JM |
6486 | add_missing_dev(fs_info->fs_devices, devid, |
6487 | uuid); | |
adfb69af | 6488 | if (IS_ERR(map->stripes[i].dev)) { |
dfe25020 | 6489 | free_extent_map(em); |
adfb69af AJ |
6490 | btrfs_err(fs_info, |
6491 | "failed to init missing dev %llu: %ld", | |
6492 | devid, PTR_ERR(map->stripes[i].dev)); | |
6493 | return PTR_ERR(map->stripes[i].dev); | |
dfe25020 | 6494 | } |
2b902dfc | 6495 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
dfe25020 CM |
6496 | } |
6497 | map->stripes[i].dev->in_fs_metadata = 1; | |
0b86a832 CM |
6498 | } |
6499 | ||
890871be | 6500 | write_lock(&map_tree->map_tree.lock); |
09a2a8f9 | 6501 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
890871be | 6502 | write_unlock(&map_tree->map_tree.lock); |
79787eaa | 6503 | BUG_ON(ret); /* Tree corruption */ |
0b86a832 CM |
6504 | free_extent_map(em); |
6505 | ||
6506 | return 0; | |
6507 | } | |
6508 | ||
143bede5 | 6509 | static void fill_device_from_item(struct extent_buffer *leaf, |
0b86a832 CM |
6510 | struct btrfs_dev_item *dev_item, |
6511 | struct btrfs_device *device) | |
6512 | { | |
6513 | unsigned long ptr; | |
0b86a832 CM |
6514 | |
6515 | device->devid = btrfs_device_id(leaf, dev_item); | |
d6397bae CB |
6516 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
6517 | device->total_bytes = device->disk_total_bytes; | |
935e5cc9 | 6518 | device->commit_total_bytes = device->disk_total_bytes; |
0b86a832 | 6519 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
ce7213c7 | 6520 | device->commit_bytes_used = device->bytes_used; |
0b86a832 CM |
6521 | device->type = btrfs_device_type(leaf, dev_item); |
6522 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
6523 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
6524 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
8dabb742 | 6525 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
63a212ab | 6526 | device->is_tgtdev_for_dev_replace = 0; |
0b86a832 | 6527 | |
410ba3a2 | 6528 | ptr = btrfs_device_uuid(dev_item); |
e17cade2 | 6529 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 CM |
6530 | } |
6531 | ||
2ff7e61e | 6532 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, |
5f375835 | 6533 | u8 *fsid) |
2b82032c YZ |
6534 | { |
6535 | struct btrfs_fs_devices *fs_devices; | |
6536 | int ret; | |
6537 | ||
b367e47f | 6538 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
2dfeca9b | 6539 | ASSERT(fsid); |
2b82032c | 6540 | |
0b246afa | 6541 | fs_devices = fs_info->fs_devices->seed; |
2b82032c | 6542 | while (fs_devices) { |
44880fdc | 6543 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
5f375835 MX |
6544 | return fs_devices; |
6545 | ||
2b82032c YZ |
6546 | fs_devices = fs_devices->seed; |
6547 | } | |
6548 | ||
6549 | fs_devices = find_fsid(fsid); | |
6550 | if (!fs_devices) { | |
0b246afa | 6551 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
5f375835 MX |
6552 | return ERR_PTR(-ENOENT); |
6553 | ||
6554 | fs_devices = alloc_fs_devices(fsid); | |
6555 | if (IS_ERR(fs_devices)) | |
6556 | return fs_devices; | |
6557 | ||
6558 | fs_devices->seeding = 1; | |
6559 | fs_devices->opened = 1; | |
6560 | return fs_devices; | |
2b82032c | 6561 | } |
e4404d6e YZ |
6562 | |
6563 | fs_devices = clone_fs_devices(fs_devices); | |
5f375835 MX |
6564 | if (IS_ERR(fs_devices)) |
6565 | return fs_devices; | |
2b82032c | 6566 | |
97288f2c | 6567 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
0b246afa | 6568 | fs_info->bdev_holder); |
48d28232 JL |
6569 | if (ret) { |
6570 | free_fs_devices(fs_devices); | |
5f375835 | 6571 | fs_devices = ERR_PTR(ret); |
2b82032c | 6572 | goto out; |
48d28232 | 6573 | } |
2b82032c YZ |
6574 | |
6575 | if (!fs_devices->seeding) { | |
6576 | __btrfs_close_devices(fs_devices); | |
e4404d6e | 6577 | free_fs_devices(fs_devices); |
5f375835 | 6578 | fs_devices = ERR_PTR(-EINVAL); |
2b82032c YZ |
6579 | goto out; |
6580 | } | |
6581 | ||
0b246afa JM |
6582 | fs_devices->seed = fs_info->fs_devices->seed; |
6583 | fs_info->fs_devices->seed = fs_devices; | |
2b82032c | 6584 | out: |
5f375835 | 6585 | return fs_devices; |
2b82032c YZ |
6586 | } |
6587 | ||
2ff7e61e | 6588 | static int read_one_dev(struct btrfs_fs_info *fs_info, |
0b86a832 CM |
6589 | struct extent_buffer *leaf, |
6590 | struct btrfs_dev_item *dev_item) | |
6591 | { | |
0b246afa | 6592 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
0b86a832 CM |
6593 | struct btrfs_device *device; |
6594 | u64 devid; | |
6595 | int ret; | |
44880fdc | 6596 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
a443755f CM |
6597 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
6598 | ||
0b86a832 | 6599 | devid = btrfs_device_id(leaf, dev_item); |
410ba3a2 | 6600 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
a443755f | 6601 | BTRFS_UUID_SIZE); |
1473b24e | 6602 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
44880fdc | 6603 | BTRFS_FSID_SIZE); |
2b82032c | 6604 | |
44880fdc | 6605 | if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) { |
2ff7e61e | 6606 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
5f375835 MX |
6607 | if (IS_ERR(fs_devices)) |
6608 | return PTR_ERR(fs_devices); | |
2b82032c YZ |
6609 | } |
6610 | ||
0b246afa | 6611 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
5f375835 | 6612 | if (!device) { |
c5502451 | 6613 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
2b902dfc AJ |
6614 | btrfs_report_missing_device(fs_info, devid, |
6615 | dev_uuid, true); | |
45dbdbc9 | 6616 | return -ENOENT; |
c5502451 | 6617 | } |
2b82032c | 6618 | |
2ff7e61e | 6619 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
adfb69af AJ |
6620 | if (IS_ERR(device)) { |
6621 | btrfs_err(fs_info, | |
6622 | "failed to add missing dev %llu: %ld", | |
6623 | devid, PTR_ERR(device)); | |
6624 | return PTR_ERR(device); | |
6625 | } | |
2b902dfc | 6626 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
5f375835 | 6627 | } else { |
c5502451 | 6628 | if (!device->bdev) { |
2b902dfc AJ |
6629 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
6630 | btrfs_report_missing_device(fs_info, | |
6631 | devid, dev_uuid, true); | |
45dbdbc9 | 6632 | return -ENOENT; |
2b902dfc AJ |
6633 | } |
6634 | btrfs_report_missing_device(fs_info, devid, | |
6635 | dev_uuid, false); | |
c5502451 | 6636 | } |
5f375835 MX |
6637 | |
6638 | if(!device->bdev && !device->missing) { | |
cd02dca5 CM |
6639 | /* |
6640 | * this happens when a device that was properly setup | |
6641 | * in the device info lists suddenly goes bad. | |
6642 | * device->bdev is NULL, and so we have to set | |
6643 | * device->missing to one here | |
6644 | */ | |
5f375835 | 6645 | device->fs_devices->missing_devices++; |
cd02dca5 | 6646 | device->missing = 1; |
2b82032c | 6647 | } |
5f375835 MX |
6648 | |
6649 | /* Move the device to its own fs_devices */ | |
6650 | if (device->fs_devices != fs_devices) { | |
6651 | ASSERT(device->missing); | |
6652 | ||
6653 | list_move(&device->dev_list, &fs_devices->devices); | |
6654 | device->fs_devices->num_devices--; | |
6655 | fs_devices->num_devices++; | |
6656 | ||
6657 | device->fs_devices->missing_devices--; | |
6658 | fs_devices->missing_devices++; | |
6659 | ||
6660 | device->fs_devices = fs_devices; | |
6661 | } | |
2b82032c YZ |
6662 | } |
6663 | ||
0b246afa | 6664 | if (device->fs_devices != fs_info->fs_devices) { |
2b82032c YZ |
6665 | BUG_ON(device->writeable); |
6666 | if (device->generation != | |
6667 | btrfs_device_generation(leaf, dev_item)) | |
6668 | return -EINVAL; | |
6324fbf3 | 6669 | } |
0b86a832 CM |
6670 | |
6671 | fill_device_from_item(leaf, dev_item, device); | |
dfe25020 | 6672 | device->in_fs_metadata = 1; |
63a212ab | 6673 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
2b82032c | 6674 | device->fs_devices->total_rw_bytes += device->total_bytes; |
a5ed45f8 NB |
6675 | atomic64_add(device->total_bytes - device->bytes_used, |
6676 | &fs_info->free_chunk_space); | |
2bf64758 | 6677 | } |
0b86a832 | 6678 | ret = 0; |
0b86a832 CM |
6679 | return ret; |
6680 | } | |
6681 | ||
6bccf3ab | 6682 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
0b86a832 | 6683 | { |
6bccf3ab | 6684 | struct btrfs_root *root = fs_info->tree_root; |
ab8d0fc4 | 6685 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
a061fc8d | 6686 | struct extent_buffer *sb; |
0b86a832 | 6687 | struct btrfs_disk_key *disk_key; |
0b86a832 | 6688 | struct btrfs_chunk *chunk; |
1ffb22cf DS |
6689 | u8 *array_ptr; |
6690 | unsigned long sb_array_offset; | |
84eed90f | 6691 | int ret = 0; |
0b86a832 CM |
6692 | u32 num_stripes; |
6693 | u32 array_size; | |
6694 | u32 len = 0; | |
1ffb22cf | 6695 | u32 cur_offset; |
e06cd3dd | 6696 | u64 type; |
84eed90f | 6697 | struct btrfs_key key; |
0b86a832 | 6698 | |
0b246afa | 6699 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
a83fffb7 DS |
6700 | /* |
6701 | * This will create extent buffer of nodesize, superblock size is | |
6702 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will | |
6703 | * overallocate but we can keep it as-is, only the first page is used. | |
6704 | */ | |
2ff7e61e | 6705 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
c871b0f2 LB |
6706 | if (IS_ERR(sb)) |
6707 | return PTR_ERR(sb); | |
4db8c528 | 6708 | set_extent_buffer_uptodate(sb); |
85d4e461 | 6709 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
8a334426 | 6710 | /* |
01327610 | 6711 | * The sb extent buffer is artificial and just used to read the system array. |
4db8c528 | 6712 | * set_extent_buffer_uptodate() call does not properly mark all it's |
8a334426 DS |
6713 | * pages up-to-date when the page is larger: extent does not cover the |
6714 | * whole page and consequently check_page_uptodate does not find all | |
6715 | * the page's extents up-to-date (the hole beyond sb), | |
6716 | * write_extent_buffer then triggers a WARN_ON. | |
6717 | * | |
6718 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, | |
6719 | * but sb spans only this function. Add an explicit SetPageUptodate call | |
6720 | * to silence the warning eg. on PowerPC 64. | |
6721 | */ | |
09cbfeaf | 6722 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
727011e0 | 6723 | SetPageUptodate(sb->pages[0]); |
4008c04a | 6724 | |
a061fc8d | 6725 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
0b86a832 CM |
6726 | array_size = btrfs_super_sys_array_size(super_copy); |
6727 | ||
1ffb22cf DS |
6728 | array_ptr = super_copy->sys_chunk_array; |
6729 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); | |
6730 | cur_offset = 0; | |
0b86a832 | 6731 | |
1ffb22cf DS |
6732 | while (cur_offset < array_size) { |
6733 | disk_key = (struct btrfs_disk_key *)array_ptr; | |
e3540eab DS |
6734 | len = sizeof(*disk_key); |
6735 | if (cur_offset + len > array_size) | |
6736 | goto out_short_read; | |
6737 | ||
0b86a832 CM |
6738 | btrfs_disk_key_to_cpu(&key, disk_key); |
6739 | ||
1ffb22cf DS |
6740 | array_ptr += len; |
6741 | sb_array_offset += len; | |
6742 | cur_offset += len; | |
0b86a832 | 6743 | |
0d81ba5d | 6744 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
1ffb22cf | 6745 | chunk = (struct btrfs_chunk *)sb_array_offset; |
e3540eab DS |
6746 | /* |
6747 | * At least one btrfs_chunk with one stripe must be | |
6748 | * present, exact stripe count check comes afterwards | |
6749 | */ | |
6750 | len = btrfs_chunk_item_size(1); | |
6751 | if (cur_offset + len > array_size) | |
6752 | goto out_short_read; | |
6753 | ||
6754 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); | |
f5cdedd7 | 6755 | if (!num_stripes) { |
ab8d0fc4 JM |
6756 | btrfs_err(fs_info, |
6757 | "invalid number of stripes %u in sys_array at offset %u", | |
f5cdedd7 DS |
6758 | num_stripes, cur_offset); |
6759 | ret = -EIO; | |
6760 | break; | |
6761 | } | |
6762 | ||
e06cd3dd LB |
6763 | type = btrfs_chunk_type(sb, chunk); |
6764 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { | |
ab8d0fc4 | 6765 | btrfs_err(fs_info, |
e06cd3dd LB |
6766 | "invalid chunk type %llu in sys_array at offset %u", |
6767 | type, cur_offset); | |
6768 | ret = -EIO; | |
6769 | break; | |
6770 | } | |
6771 | ||
e3540eab DS |
6772 | len = btrfs_chunk_item_size(num_stripes); |
6773 | if (cur_offset + len > array_size) | |
6774 | goto out_short_read; | |
6775 | ||
2ff7e61e | 6776 | ret = read_one_chunk(fs_info, &key, sb, chunk); |
84eed90f CM |
6777 | if (ret) |
6778 | break; | |
0b86a832 | 6779 | } else { |
ab8d0fc4 JM |
6780 | btrfs_err(fs_info, |
6781 | "unexpected item type %u in sys_array at offset %u", | |
6782 | (u32)key.type, cur_offset); | |
84eed90f CM |
6783 | ret = -EIO; |
6784 | break; | |
0b86a832 | 6785 | } |
1ffb22cf DS |
6786 | array_ptr += len; |
6787 | sb_array_offset += len; | |
6788 | cur_offset += len; | |
0b86a832 | 6789 | } |
d865177a | 6790 | clear_extent_buffer_uptodate(sb); |
1c8b5b6e | 6791 | free_extent_buffer_stale(sb); |
84eed90f | 6792 | return ret; |
e3540eab DS |
6793 | |
6794 | out_short_read: | |
ab8d0fc4 | 6795 | btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u", |
e3540eab | 6796 | len, cur_offset); |
d865177a | 6797 | clear_extent_buffer_uptodate(sb); |
1c8b5b6e | 6798 | free_extent_buffer_stale(sb); |
e3540eab | 6799 | return -EIO; |
0b86a832 CM |
6800 | } |
6801 | ||
21634a19 QW |
6802 | /* |
6803 | * Check if all chunks in the fs are OK for read-write degraded mount | |
6804 | * | |
6805 | * Return true if all chunks meet the minimal RW mount requirements. | |
6806 | * Return false if any chunk doesn't meet the minimal RW mount requirements. | |
6807 | */ | |
6808 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info) | |
6809 | { | |
6810 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; | |
6811 | struct extent_map *em; | |
6812 | u64 next_start = 0; | |
6813 | bool ret = true; | |
6814 | ||
6815 | read_lock(&map_tree->map_tree.lock); | |
6816 | em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1); | |
6817 | read_unlock(&map_tree->map_tree.lock); | |
6818 | /* No chunk at all? Return false anyway */ | |
6819 | if (!em) { | |
6820 | ret = false; | |
6821 | goto out; | |
6822 | } | |
6823 | while (em) { | |
6824 | struct map_lookup *map; | |
6825 | int missing = 0; | |
6826 | int max_tolerated; | |
6827 | int i; | |
6828 | ||
6829 | map = em->map_lookup; | |
6830 | max_tolerated = | |
6831 | btrfs_get_num_tolerated_disk_barrier_failures( | |
6832 | map->type); | |
6833 | for (i = 0; i < map->num_stripes; i++) { | |
6834 | struct btrfs_device *dev = map->stripes[i].dev; | |
6835 | ||
6836 | if (!dev || !dev->bdev || dev->missing || | |
6837 | dev->last_flush_error) | |
6838 | missing++; | |
6839 | } | |
6840 | if (missing > max_tolerated) { | |
6841 | btrfs_warn(fs_info, | |
6842 | "chunk %llu missing %d devices, max tolerance is %d for writeable mount", | |
6843 | em->start, missing, max_tolerated); | |
6844 | free_extent_map(em); | |
6845 | ret = false; | |
6846 | goto out; | |
6847 | } | |
6848 | next_start = extent_map_end(em); | |
6849 | free_extent_map(em); | |
6850 | ||
6851 | read_lock(&map_tree->map_tree.lock); | |
6852 | em = lookup_extent_mapping(&map_tree->map_tree, next_start, | |
6853 | (u64)(-1) - next_start); | |
6854 | read_unlock(&map_tree->map_tree.lock); | |
6855 | } | |
6856 | out: | |
6857 | return ret; | |
6858 | } | |
6859 | ||
5b4aacef | 6860 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
0b86a832 | 6861 | { |
5b4aacef | 6862 | struct btrfs_root *root = fs_info->chunk_root; |
0b86a832 CM |
6863 | struct btrfs_path *path; |
6864 | struct extent_buffer *leaf; | |
6865 | struct btrfs_key key; | |
6866 | struct btrfs_key found_key; | |
6867 | int ret; | |
6868 | int slot; | |
99e3ecfc | 6869 | u64 total_dev = 0; |
0b86a832 | 6870 | |
0b86a832 CM |
6871 | path = btrfs_alloc_path(); |
6872 | if (!path) | |
6873 | return -ENOMEM; | |
6874 | ||
b367e47f | 6875 | mutex_lock(&uuid_mutex); |
34441361 | 6876 | mutex_lock(&fs_info->chunk_mutex); |
b367e47f | 6877 | |
395927a9 FDBM |
6878 | /* |
6879 | * Read all device items, and then all the chunk items. All | |
6880 | * device items are found before any chunk item (their object id | |
6881 | * is smaller than the lowest possible object id for a chunk | |
6882 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). | |
0b86a832 CM |
6883 | */ |
6884 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
6885 | key.offset = 0; | |
6886 | key.type = 0; | |
0b86a832 | 6887 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
ab59381e ZL |
6888 | if (ret < 0) |
6889 | goto error; | |
d397712b | 6890 | while (1) { |
0b86a832 CM |
6891 | leaf = path->nodes[0]; |
6892 | slot = path->slots[0]; | |
6893 | if (slot >= btrfs_header_nritems(leaf)) { | |
6894 | ret = btrfs_next_leaf(root, path); | |
6895 | if (ret == 0) | |
6896 | continue; | |
6897 | if (ret < 0) | |
6898 | goto error; | |
6899 | break; | |
6900 | } | |
6901 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
395927a9 FDBM |
6902 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
6903 | struct btrfs_dev_item *dev_item; | |
6904 | dev_item = btrfs_item_ptr(leaf, slot, | |
0b86a832 | 6905 | struct btrfs_dev_item); |
2ff7e61e | 6906 | ret = read_one_dev(fs_info, leaf, dev_item); |
395927a9 FDBM |
6907 | if (ret) |
6908 | goto error; | |
99e3ecfc | 6909 | total_dev++; |
0b86a832 CM |
6910 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
6911 | struct btrfs_chunk *chunk; | |
6912 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | |
2ff7e61e | 6913 | ret = read_one_chunk(fs_info, &found_key, leaf, chunk); |
2b82032c YZ |
6914 | if (ret) |
6915 | goto error; | |
0b86a832 CM |
6916 | } |
6917 | path->slots[0]++; | |
6918 | } | |
99e3ecfc LB |
6919 | |
6920 | /* | |
6921 | * After loading chunk tree, we've got all device information, | |
6922 | * do another round of validation checks. | |
6923 | */ | |
0b246afa JM |
6924 | if (total_dev != fs_info->fs_devices->total_devices) { |
6925 | btrfs_err(fs_info, | |
99e3ecfc | 6926 | "super_num_devices %llu mismatch with num_devices %llu found here", |
0b246afa | 6927 | btrfs_super_num_devices(fs_info->super_copy), |
99e3ecfc LB |
6928 | total_dev); |
6929 | ret = -EINVAL; | |
6930 | goto error; | |
6931 | } | |
0b246afa JM |
6932 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
6933 | fs_info->fs_devices->total_rw_bytes) { | |
6934 | btrfs_err(fs_info, | |
99e3ecfc | 6935 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
0b246afa JM |
6936 | btrfs_super_total_bytes(fs_info->super_copy), |
6937 | fs_info->fs_devices->total_rw_bytes); | |
99e3ecfc LB |
6938 | ret = -EINVAL; |
6939 | goto error; | |
6940 | } | |
0b86a832 CM |
6941 | ret = 0; |
6942 | error: | |
34441361 | 6943 | mutex_unlock(&fs_info->chunk_mutex); |
b367e47f LZ |
6944 | mutex_unlock(&uuid_mutex); |
6945 | ||
2b82032c | 6946 | btrfs_free_path(path); |
0b86a832 CM |
6947 | return ret; |
6948 | } | |
442a4f63 | 6949 | |
cb517eab MX |
6950 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
6951 | { | |
6952 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
6953 | struct btrfs_device *device; | |
6954 | ||
29cc83f6 LB |
6955 | while (fs_devices) { |
6956 | mutex_lock(&fs_devices->device_list_mutex); | |
6957 | list_for_each_entry(device, &fs_devices->devices, dev_list) | |
fb456252 | 6958 | device->fs_info = fs_info; |
29cc83f6 LB |
6959 | mutex_unlock(&fs_devices->device_list_mutex); |
6960 | ||
6961 | fs_devices = fs_devices->seed; | |
6962 | } | |
cb517eab MX |
6963 | } |
6964 | ||
733f4fbb SB |
6965 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
6966 | { | |
6967 | int i; | |
6968 | ||
6969 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
6970 | btrfs_dev_stat_reset(dev, i); | |
6971 | } | |
6972 | ||
6973 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) | |
6974 | { | |
6975 | struct btrfs_key key; | |
6976 | struct btrfs_key found_key; | |
6977 | struct btrfs_root *dev_root = fs_info->dev_root; | |
6978 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
6979 | struct extent_buffer *eb; | |
6980 | int slot; | |
6981 | int ret = 0; | |
6982 | struct btrfs_device *device; | |
6983 | struct btrfs_path *path = NULL; | |
6984 | int i; | |
6985 | ||
6986 | path = btrfs_alloc_path(); | |
6987 | if (!path) { | |
6988 | ret = -ENOMEM; | |
6989 | goto out; | |
6990 | } | |
6991 | ||
6992 | mutex_lock(&fs_devices->device_list_mutex); | |
6993 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
6994 | int item_size; | |
6995 | struct btrfs_dev_stats_item *ptr; | |
6996 | ||
242e2956 DS |
6997 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
6998 | key.type = BTRFS_PERSISTENT_ITEM_KEY; | |
733f4fbb SB |
6999 | key.offset = device->devid; |
7000 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); | |
7001 | if (ret) { | |
733f4fbb SB |
7002 | __btrfs_reset_dev_stats(device); |
7003 | device->dev_stats_valid = 1; | |
7004 | btrfs_release_path(path); | |
7005 | continue; | |
7006 | } | |
7007 | slot = path->slots[0]; | |
7008 | eb = path->nodes[0]; | |
7009 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
7010 | item_size = btrfs_item_size_nr(eb, slot); | |
7011 | ||
7012 | ptr = btrfs_item_ptr(eb, slot, | |
7013 | struct btrfs_dev_stats_item); | |
7014 | ||
7015 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { | |
7016 | if (item_size >= (1 + i) * sizeof(__le64)) | |
7017 | btrfs_dev_stat_set(device, i, | |
7018 | btrfs_dev_stats_value(eb, ptr, i)); | |
7019 | else | |
7020 | btrfs_dev_stat_reset(device, i); | |
7021 | } | |
7022 | ||
7023 | device->dev_stats_valid = 1; | |
7024 | btrfs_dev_stat_print_on_load(device); | |
7025 | btrfs_release_path(path); | |
7026 | } | |
7027 | mutex_unlock(&fs_devices->device_list_mutex); | |
7028 | ||
7029 | out: | |
7030 | btrfs_free_path(path); | |
7031 | return ret < 0 ? ret : 0; | |
7032 | } | |
7033 | ||
7034 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, | |
6bccf3ab | 7035 | struct btrfs_fs_info *fs_info, |
733f4fbb SB |
7036 | struct btrfs_device *device) |
7037 | { | |
6bccf3ab | 7038 | struct btrfs_root *dev_root = fs_info->dev_root; |
733f4fbb SB |
7039 | struct btrfs_path *path; |
7040 | struct btrfs_key key; | |
7041 | struct extent_buffer *eb; | |
7042 | struct btrfs_dev_stats_item *ptr; | |
7043 | int ret; | |
7044 | int i; | |
7045 | ||
242e2956 DS |
7046 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
7047 | key.type = BTRFS_PERSISTENT_ITEM_KEY; | |
733f4fbb SB |
7048 | key.offset = device->devid; |
7049 | ||
7050 | path = btrfs_alloc_path(); | |
fa252992 DS |
7051 | if (!path) |
7052 | return -ENOMEM; | |
733f4fbb SB |
7053 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
7054 | if (ret < 0) { | |
0b246afa | 7055 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7056 | "error %d while searching for dev_stats item for device %s", |
606686ee | 7057 | ret, rcu_str_deref(device->name)); |
733f4fbb SB |
7058 | goto out; |
7059 | } | |
7060 | ||
7061 | if (ret == 0 && | |
7062 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { | |
7063 | /* need to delete old one and insert a new one */ | |
7064 | ret = btrfs_del_item(trans, dev_root, path); | |
7065 | if (ret != 0) { | |
0b246afa | 7066 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7067 | "delete too small dev_stats item for device %s failed %d", |
606686ee | 7068 | rcu_str_deref(device->name), ret); |
733f4fbb SB |
7069 | goto out; |
7070 | } | |
7071 | ret = 1; | |
7072 | } | |
7073 | ||
7074 | if (ret == 1) { | |
7075 | /* need to insert a new item */ | |
7076 | btrfs_release_path(path); | |
7077 | ret = btrfs_insert_empty_item(trans, dev_root, path, | |
7078 | &key, sizeof(*ptr)); | |
7079 | if (ret < 0) { | |
0b246afa | 7080 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b DS |
7081 | "insert dev_stats item for device %s failed %d", |
7082 | rcu_str_deref(device->name), ret); | |
733f4fbb SB |
7083 | goto out; |
7084 | } | |
7085 | } | |
7086 | ||
7087 | eb = path->nodes[0]; | |
7088 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); | |
7089 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7090 | btrfs_set_dev_stats_value(eb, ptr, i, | |
7091 | btrfs_dev_stat_read(device, i)); | |
7092 | btrfs_mark_buffer_dirty(eb); | |
7093 | ||
7094 | out: | |
7095 | btrfs_free_path(path); | |
7096 | return ret; | |
7097 | } | |
7098 | ||
7099 | /* | |
7100 | * called from commit_transaction. Writes all changed device stats to disk. | |
7101 | */ | |
7102 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, | |
7103 | struct btrfs_fs_info *fs_info) | |
7104 | { | |
733f4fbb SB |
7105 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
7106 | struct btrfs_device *device; | |
addc3fa7 | 7107 | int stats_cnt; |
733f4fbb SB |
7108 | int ret = 0; |
7109 | ||
7110 | mutex_lock(&fs_devices->device_list_mutex); | |
7111 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
9ee59c3a NB |
7112 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
7113 | if (!device->dev_stats_valid || stats_cnt == 0) | |
733f4fbb SB |
7114 | continue; |
7115 | ||
9ee59c3a NB |
7116 | |
7117 | /* | |
7118 | * There is a LOAD-LOAD control dependency between the value of | |
7119 | * dev_stats_ccnt and updating the on-disk values which requires | |
7120 | * reading the in-memory counters. Such control dependencies | |
7121 | * require explicit read memory barriers. | |
7122 | * | |
7123 | * This memory barriers pairs with smp_mb__before_atomic in | |
7124 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full | |
7125 | * barrier implied by atomic_xchg in | |
7126 | * btrfs_dev_stats_read_and_reset | |
7127 | */ | |
7128 | smp_rmb(); | |
7129 | ||
6bccf3ab | 7130 | ret = update_dev_stat_item(trans, fs_info, device); |
733f4fbb | 7131 | if (!ret) |
addc3fa7 | 7132 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
733f4fbb SB |
7133 | } |
7134 | mutex_unlock(&fs_devices->device_list_mutex); | |
7135 | ||
7136 | return ret; | |
7137 | } | |
7138 | ||
442a4f63 SB |
7139 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
7140 | { | |
7141 | btrfs_dev_stat_inc(dev, index); | |
7142 | btrfs_dev_stat_print_on_error(dev); | |
7143 | } | |
7144 | ||
48a3b636 | 7145 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
442a4f63 | 7146 | { |
733f4fbb SB |
7147 | if (!dev->dev_stats_valid) |
7148 | return; | |
fb456252 | 7149 | btrfs_err_rl_in_rcu(dev->fs_info, |
b14af3b4 | 7150 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
606686ee | 7151 | rcu_str_deref(dev->name), |
442a4f63 SB |
7152 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
7153 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), | |
7154 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), | |
efe120a0 FH |
7155 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
7156 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); | |
442a4f63 | 7157 | } |
c11d2c23 | 7158 | |
733f4fbb SB |
7159 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
7160 | { | |
a98cdb85 SB |
7161 | int i; |
7162 | ||
7163 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7164 | if (btrfs_dev_stat_read(dev, i) != 0) | |
7165 | break; | |
7166 | if (i == BTRFS_DEV_STAT_VALUES_MAX) | |
7167 | return; /* all values == 0, suppress message */ | |
7168 | ||
fb456252 | 7169 | btrfs_info_in_rcu(dev->fs_info, |
ecaeb14b | 7170 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
606686ee | 7171 | rcu_str_deref(dev->name), |
733f4fbb SB |
7172 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
7173 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), | |
7174 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), | |
7175 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), | |
7176 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); | |
7177 | } | |
7178 | ||
2ff7e61e | 7179 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 7180 | struct btrfs_ioctl_get_dev_stats *stats) |
c11d2c23 SB |
7181 | { |
7182 | struct btrfs_device *dev; | |
0b246afa | 7183 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
c11d2c23 SB |
7184 | int i; |
7185 | ||
7186 | mutex_lock(&fs_devices->device_list_mutex); | |
0b246afa | 7187 | dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL); |
c11d2c23 SB |
7188 | mutex_unlock(&fs_devices->device_list_mutex); |
7189 | ||
7190 | if (!dev) { | |
0b246afa | 7191 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
c11d2c23 | 7192 | return -ENODEV; |
733f4fbb | 7193 | } else if (!dev->dev_stats_valid) { |
0b246afa | 7194 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
733f4fbb | 7195 | return -ENODEV; |
b27f7c0c | 7196 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
c11d2c23 SB |
7197 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
7198 | if (stats->nr_items > i) | |
7199 | stats->values[i] = | |
7200 | btrfs_dev_stat_read_and_reset(dev, i); | |
7201 | else | |
7202 | btrfs_dev_stat_reset(dev, i); | |
7203 | } | |
7204 | } else { | |
7205 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7206 | if (stats->nr_items > i) | |
7207 | stats->values[i] = btrfs_dev_stat_read(dev, i); | |
7208 | } | |
7209 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) | |
7210 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; | |
7211 | return 0; | |
7212 | } | |
a8a6dab7 | 7213 | |
da353f6b | 7214 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
a8a6dab7 SB |
7215 | { |
7216 | struct buffer_head *bh; | |
7217 | struct btrfs_super_block *disk_super; | |
12b1c263 | 7218 | int copy_num; |
a8a6dab7 | 7219 | |
12b1c263 AJ |
7220 | if (!bdev) |
7221 | return; | |
a8a6dab7 | 7222 | |
12b1c263 AJ |
7223 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
7224 | copy_num++) { | |
a8a6dab7 | 7225 | |
12b1c263 AJ |
7226 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
7227 | continue; | |
7228 | ||
7229 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
7230 | ||
7231 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); | |
7232 | set_buffer_dirty(bh); | |
7233 | sync_dirty_buffer(bh); | |
7234 | brelse(bh); | |
7235 | } | |
7236 | ||
7237 | /* Notify udev that device has changed */ | |
7238 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); | |
7239 | ||
7240 | /* Update ctime/mtime for device path for libblkid */ | |
7241 | update_dev_time(device_path); | |
a8a6dab7 | 7242 | } |
935e5cc9 MX |
7243 | |
7244 | /* | |
7245 | * Update the size of all devices, which is used for writing out the | |
7246 | * super blocks. | |
7247 | */ | |
7248 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) | |
7249 | { | |
7250 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7251 | struct btrfs_device *curr, *next; | |
7252 | ||
7253 | if (list_empty(&fs_devices->resized_devices)) | |
7254 | return; | |
7255 | ||
7256 | mutex_lock(&fs_devices->device_list_mutex); | |
34441361 | 7257 | mutex_lock(&fs_info->chunk_mutex); |
935e5cc9 MX |
7258 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
7259 | resized_list) { | |
7260 | list_del_init(&curr->resized_list); | |
7261 | curr->commit_total_bytes = curr->disk_total_bytes; | |
7262 | } | |
34441361 | 7263 | mutex_unlock(&fs_info->chunk_mutex); |
935e5cc9 MX |
7264 | mutex_unlock(&fs_devices->device_list_mutex); |
7265 | } | |
ce7213c7 MX |
7266 | |
7267 | /* Must be invoked during the transaction commit */ | |
2ff7e61e | 7268 | void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info, |
ce7213c7 MX |
7269 | struct btrfs_transaction *transaction) |
7270 | { | |
7271 | struct extent_map *em; | |
7272 | struct map_lookup *map; | |
7273 | struct btrfs_device *dev; | |
7274 | int i; | |
7275 | ||
7276 | if (list_empty(&transaction->pending_chunks)) | |
7277 | return; | |
7278 | ||
7279 | /* In order to kick the device replace finish process */ | |
34441361 | 7280 | mutex_lock(&fs_info->chunk_mutex); |
ce7213c7 | 7281 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
95617d69 | 7282 | map = em->map_lookup; |
ce7213c7 MX |
7283 | |
7284 | for (i = 0; i < map->num_stripes; i++) { | |
7285 | dev = map->stripes[i].dev; | |
7286 | dev->commit_bytes_used = dev->bytes_used; | |
7287 | } | |
7288 | } | |
34441361 | 7289 | mutex_unlock(&fs_info->chunk_mutex); |
ce7213c7 | 7290 | } |
5a13f430 AJ |
7291 | |
7292 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) | |
7293 | { | |
7294 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7295 | while (fs_devices) { | |
7296 | fs_devices->fs_info = fs_info; | |
7297 | fs_devices = fs_devices->seed; | |
7298 | } | |
7299 | } | |
7300 | ||
7301 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) | |
7302 | { | |
7303 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7304 | while (fs_devices) { | |
7305 | fs_devices->fs_info = NULL; | |
7306 | fs_devices = fs_devices->seed; | |
7307 | } | |
7308 | } |