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