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