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> |
593060d7 | 26 | #include <asm/div64.h> |
4b4e25f2 | 27 | #include "compat.h" |
0b86a832 CM |
28 | #include "ctree.h" |
29 | #include "extent_map.h" | |
30 | #include "disk-io.h" | |
31 | #include "transaction.h" | |
32 | #include "print-tree.h" | |
33 | #include "volumes.h" | |
8b712842 | 34 | #include "async-thread.h" |
0b86a832 | 35 | |
2b82032c YZ |
36 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
37 | struct btrfs_root *root, | |
38 | struct btrfs_device *device); | |
39 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); | |
40 | ||
8a4b83cc CM |
41 | static DEFINE_MUTEX(uuid_mutex); |
42 | static LIST_HEAD(fs_uuids); | |
43 | ||
7d9eb12c CM |
44 | static void lock_chunks(struct btrfs_root *root) |
45 | { | |
7d9eb12c CM |
46 | mutex_lock(&root->fs_info->chunk_mutex); |
47 | } | |
48 | ||
49 | static void unlock_chunks(struct btrfs_root *root) | |
50 | { | |
7d9eb12c CM |
51 | mutex_unlock(&root->fs_info->chunk_mutex); |
52 | } | |
53 | ||
e4404d6e YZ |
54 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
55 | { | |
56 | struct btrfs_device *device; | |
57 | WARN_ON(fs_devices->opened); | |
58 | while (!list_empty(&fs_devices->devices)) { | |
59 | device = list_entry(fs_devices->devices.next, | |
60 | struct btrfs_device, dev_list); | |
61 | list_del(&device->dev_list); | |
62 | kfree(device->name); | |
63 | kfree(device); | |
64 | } | |
65 | kfree(fs_devices); | |
66 | } | |
67 | ||
8a4b83cc CM |
68 | int btrfs_cleanup_fs_uuids(void) |
69 | { | |
70 | struct btrfs_fs_devices *fs_devices; | |
8a4b83cc | 71 | |
2b82032c YZ |
72 | while (!list_empty(&fs_uuids)) { |
73 | fs_devices = list_entry(fs_uuids.next, | |
74 | struct btrfs_fs_devices, list); | |
75 | list_del(&fs_devices->list); | |
e4404d6e | 76 | free_fs_devices(fs_devices); |
8a4b83cc CM |
77 | } |
78 | return 0; | |
79 | } | |
80 | ||
a1b32a59 CM |
81 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
82 | u64 devid, u8 *uuid) | |
8a4b83cc CM |
83 | { |
84 | struct btrfs_device *dev; | |
8a4b83cc | 85 | |
c6e30871 | 86 | list_for_each_entry(dev, head, dev_list) { |
a443755f | 87 | if (dev->devid == devid && |
8f18cf13 | 88 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
8a4b83cc | 89 | return dev; |
a443755f | 90 | } |
8a4b83cc CM |
91 | } |
92 | return NULL; | |
93 | } | |
94 | ||
a1b32a59 | 95 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
8a4b83cc | 96 | { |
8a4b83cc CM |
97 | struct btrfs_fs_devices *fs_devices; |
98 | ||
c6e30871 | 99 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
8a4b83cc CM |
100 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
101 | return fs_devices; | |
102 | } | |
103 | return NULL; | |
104 | } | |
105 | ||
ffbd517d CM |
106 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
107 | struct bio *head, struct bio *tail) | |
108 | { | |
109 | ||
110 | struct bio *old_head; | |
111 | ||
112 | old_head = pending_bios->head; | |
113 | pending_bios->head = head; | |
114 | if (pending_bios->tail) | |
115 | tail->bi_next = old_head; | |
116 | else | |
117 | pending_bios->tail = tail; | |
118 | } | |
119 | ||
8b712842 CM |
120 | /* |
121 | * we try to collect pending bios for a device so we don't get a large | |
122 | * number of procs sending bios down to the same device. This greatly | |
123 | * improves the schedulers ability to collect and merge the bios. | |
124 | * | |
125 | * But, it also turns into a long list of bios to process and that is sure | |
126 | * to eventually make the worker thread block. The solution here is to | |
127 | * make some progress and then put this work struct back at the end of | |
128 | * the list if the block device is congested. This way, multiple devices | |
129 | * can make progress from a single worker thread. | |
130 | */ | |
d397712b | 131 | static noinline int run_scheduled_bios(struct btrfs_device *device) |
8b712842 CM |
132 | { |
133 | struct bio *pending; | |
134 | struct backing_dev_info *bdi; | |
b64a2851 | 135 | struct btrfs_fs_info *fs_info; |
ffbd517d | 136 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
137 | struct bio *tail; |
138 | struct bio *cur; | |
139 | int again = 0; | |
ffbd517d | 140 | unsigned long num_run; |
d644d8a1 | 141 | unsigned long batch_run = 0; |
b64a2851 | 142 | unsigned long limit; |
b765ead5 | 143 | unsigned long last_waited = 0; |
d84275c9 | 144 | int force_reg = 0; |
211588ad CM |
145 | struct blk_plug plug; |
146 | ||
147 | /* | |
148 | * this function runs all the bios we've collected for | |
149 | * a particular device. We don't want to wander off to | |
150 | * another device without first sending all of these down. | |
151 | * So, setup a plug here and finish it off before we return | |
152 | */ | |
153 | blk_start_plug(&plug); | |
8b712842 | 154 | |
bedf762b | 155 | bdi = blk_get_backing_dev_info(device->bdev); |
b64a2851 CM |
156 | fs_info = device->dev_root->fs_info; |
157 | limit = btrfs_async_submit_limit(fs_info); | |
158 | limit = limit * 2 / 3; | |
159 | ||
8b712842 CM |
160 | loop: |
161 | spin_lock(&device->io_lock); | |
162 | ||
a6837051 | 163 | loop_lock: |
d84275c9 | 164 | num_run = 0; |
ffbd517d | 165 | |
8b712842 CM |
166 | /* take all the bios off the list at once and process them |
167 | * later on (without the lock held). But, remember the | |
168 | * tail and other pointers so the bios can be properly reinserted | |
169 | * into the list if we hit congestion | |
170 | */ | |
d84275c9 | 171 | if (!force_reg && device->pending_sync_bios.head) { |
ffbd517d | 172 | pending_bios = &device->pending_sync_bios; |
d84275c9 CM |
173 | force_reg = 1; |
174 | } else { | |
ffbd517d | 175 | pending_bios = &device->pending_bios; |
d84275c9 CM |
176 | force_reg = 0; |
177 | } | |
ffbd517d CM |
178 | |
179 | pending = pending_bios->head; | |
180 | tail = pending_bios->tail; | |
8b712842 | 181 | WARN_ON(pending && !tail); |
8b712842 CM |
182 | |
183 | /* | |
184 | * if pending was null this time around, no bios need processing | |
185 | * at all and we can stop. Otherwise it'll loop back up again | |
186 | * and do an additional check so no bios are missed. | |
187 | * | |
188 | * device->running_pending is used to synchronize with the | |
189 | * schedule_bio code. | |
190 | */ | |
ffbd517d CM |
191 | if (device->pending_sync_bios.head == NULL && |
192 | device->pending_bios.head == NULL) { | |
8b712842 CM |
193 | again = 0; |
194 | device->running_pending = 0; | |
ffbd517d CM |
195 | } else { |
196 | again = 1; | |
197 | device->running_pending = 1; | |
8b712842 | 198 | } |
ffbd517d CM |
199 | |
200 | pending_bios->head = NULL; | |
201 | pending_bios->tail = NULL; | |
202 | ||
8b712842 CM |
203 | spin_unlock(&device->io_lock); |
204 | ||
d397712b | 205 | while (pending) { |
ffbd517d CM |
206 | |
207 | rmb(); | |
d84275c9 CM |
208 | /* we want to work on both lists, but do more bios on the |
209 | * sync list than the regular list | |
210 | */ | |
211 | if ((num_run > 32 && | |
212 | pending_bios != &device->pending_sync_bios && | |
213 | device->pending_sync_bios.head) || | |
214 | (num_run > 64 && pending_bios == &device->pending_sync_bios && | |
215 | device->pending_bios.head)) { | |
ffbd517d CM |
216 | spin_lock(&device->io_lock); |
217 | requeue_list(pending_bios, pending, tail); | |
218 | goto loop_lock; | |
219 | } | |
220 | ||
8b712842 CM |
221 | cur = pending; |
222 | pending = pending->bi_next; | |
223 | cur->bi_next = NULL; | |
b64a2851 CM |
224 | atomic_dec(&fs_info->nr_async_bios); |
225 | ||
226 | if (atomic_read(&fs_info->nr_async_bios) < limit && | |
227 | waitqueue_active(&fs_info->async_submit_wait)) | |
228 | wake_up(&fs_info->async_submit_wait); | |
492bb6de CM |
229 | |
230 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); | |
d644d8a1 | 231 | |
5ff7ba3a CM |
232 | submit_bio(cur->bi_rw, cur); |
233 | num_run++; | |
234 | batch_run++; | |
7eaceacc | 235 | if (need_resched()) |
ffbd517d | 236 | cond_resched(); |
8b712842 CM |
237 | |
238 | /* | |
239 | * we made progress, there is more work to do and the bdi | |
240 | * is now congested. Back off and let other work structs | |
241 | * run instead | |
242 | */ | |
57fd5a5f | 243 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
5f2cc086 | 244 | fs_info->fs_devices->open_devices > 1) { |
b765ead5 | 245 | struct io_context *ioc; |
8b712842 | 246 | |
b765ead5 CM |
247 | ioc = current->io_context; |
248 | ||
249 | /* | |
250 | * the main goal here is that we don't want to | |
251 | * block if we're going to be able to submit | |
252 | * more requests without blocking. | |
253 | * | |
254 | * This code does two great things, it pokes into | |
255 | * the elevator code from a filesystem _and_ | |
256 | * it makes assumptions about how batching works. | |
257 | */ | |
258 | if (ioc && ioc->nr_batch_requests > 0 && | |
259 | time_before(jiffies, ioc->last_waited + HZ/50UL) && | |
260 | (last_waited == 0 || | |
261 | ioc->last_waited == last_waited)) { | |
262 | /* | |
263 | * we want to go through our batch of | |
264 | * requests and stop. So, we copy out | |
265 | * the ioc->last_waited time and test | |
266 | * against it before looping | |
267 | */ | |
268 | last_waited = ioc->last_waited; | |
7eaceacc | 269 | if (need_resched()) |
ffbd517d | 270 | cond_resched(); |
b765ead5 CM |
271 | continue; |
272 | } | |
8b712842 | 273 | spin_lock(&device->io_lock); |
ffbd517d | 274 | requeue_list(pending_bios, pending, tail); |
a6837051 | 275 | device->running_pending = 1; |
8b712842 CM |
276 | |
277 | spin_unlock(&device->io_lock); | |
278 | btrfs_requeue_work(&device->work); | |
279 | goto done; | |
280 | } | |
281 | } | |
ffbd517d | 282 | |
51684082 CM |
283 | cond_resched(); |
284 | if (again) | |
285 | goto loop; | |
286 | ||
287 | spin_lock(&device->io_lock); | |
288 | if (device->pending_bios.head || device->pending_sync_bios.head) | |
289 | goto loop_lock; | |
290 | spin_unlock(&device->io_lock); | |
291 | ||
8b712842 | 292 | done: |
211588ad | 293 | blk_finish_plug(&plug); |
8b712842 CM |
294 | return 0; |
295 | } | |
296 | ||
b2950863 | 297 | static void pending_bios_fn(struct btrfs_work *work) |
8b712842 CM |
298 | { |
299 | struct btrfs_device *device; | |
300 | ||
301 | device = container_of(work, struct btrfs_device, work); | |
302 | run_scheduled_bios(device); | |
303 | } | |
304 | ||
a1b32a59 | 305 | static noinline int device_list_add(const char *path, |
8a4b83cc CM |
306 | struct btrfs_super_block *disk_super, |
307 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) | |
308 | { | |
309 | struct btrfs_device *device; | |
310 | struct btrfs_fs_devices *fs_devices; | |
311 | u64 found_transid = btrfs_super_generation(disk_super); | |
3a0524dc | 312 | char *name; |
8a4b83cc CM |
313 | |
314 | fs_devices = find_fsid(disk_super->fsid); | |
315 | if (!fs_devices) { | |
515dc322 | 316 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
8a4b83cc CM |
317 | if (!fs_devices) |
318 | return -ENOMEM; | |
319 | INIT_LIST_HEAD(&fs_devices->devices); | |
b3075717 | 320 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
8a4b83cc CM |
321 | list_add(&fs_devices->list, &fs_uuids); |
322 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); | |
323 | fs_devices->latest_devid = devid; | |
324 | fs_devices->latest_trans = found_transid; | |
e5e9a520 | 325 | mutex_init(&fs_devices->device_list_mutex); |
8a4b83cc CM |
326 | device = NULL; |
327 | } else { | |
a443755f CM |
328 | device = __find_device(&fs_devices->devices, devid, |
329 | disk_super->dev_item.uuid); | |
8a4b83cc CM |
330 | } |
331 | if (!device) { | |
2b82032c YZ |
332 | if (fs_devices->opened) |
333 | return -EBUSY; | |
334 | ||
8a4b83cc CM |
335 | device = kzalloc(sizeof(*device), GFP_NOFS); |
336 | if (!device) { | |
337 | /* we can safely leave the fs_devices entry around */ | |
338 | return -ENOMEM; | |
339 | } | |
340 | device->devid = devid; | |
8b712842 | 341 | device->work.func = pending_bios_fn; |
a443755f CM |
342 | memcpy(device->uuid, disk_super->dev_item.uuid, |
343 | BTRFS_UUID_SIZE); | |
b248a415 | 344 | spin_lock_init(&device->io_lock); |
8a4b83cc CM |
345 | device->name = kstrdup(path, GFP_NOFS); |
346 | if (!device->name) { | |
347 | kfree(device); | |
348 | return -ENOMEM; | |
349 | } | |
2b82032c | 350 | INIT_LIST_HEAD(&device->dev_alloc_list); |
e5e9a520 CM |
351 | |
352 | mutex_lock(&fs_devices->device_list_mutex); | |
1f78160c | 353 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
e5e9a520 CM |
354 | mutex_unlock(&fs_devices->device_list_mutex); |
355 | ||
2b82032c | 356 | device->fs_devices = fs_devices; |
8a4b83cc | 357 | fs_devices->num_devices++; |
cd02dca5 | 358 | } else if (!device->name || strcmp(device->name, path)) { |
3a0524dc TH |
359 | name = kstrdup(path, GFP_NOFS); |
360 | if (!name) | |
361 | return -ENOMEM; | |
362 | kfree(device->name); | |
363 | device->name = name; | |
cd02dca5 CM |
364 | if (device->missing) { |
365 | fs_devices->missing_devices--; | |
366 | device->missing = 0; | |
367 | } | |
8a4b83cc CM |
368 | } |
369 | ||
370 | if (found_transid > fs_devices->latest_trans) { | |
371 | fs_devices->latest_devid = devid; | |
372 | fs_devices->latest_trans = found_transid; | |
373 | } | |
8a4b83cc CM |
374 | *fs_devices_ret = fs_devices; |
375 | return 0; | |
376 | } | |
377 | ||
e4404d6e YZ |
378 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
379 | { | |
380 | struct btrfs_fs_devices *fs_devices; | |
381 | struct btrfs_device *device; | |
382 | struct btrfs_device *orig_dev; | |
383 | ||
384 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); | |
385 | if (!fs_devices) | |
386 | return ERR_PTR(-ENOMEM); | |
387 | ||
388 | INIT_LIST_HEAD(&fs_devices->devices); | |
389 | INIT_LIST_HEAD(&fs_devices->alloc_list); | |
390 | INIT_LIST_HEAD(&fs_devices->list); | |
e5e9a520 | 391 | mutex_init(&fs_devices->device_list_mutex); |
e4404d6e YZ |
392 | fs_devices->latest_devid = orig->latest_devid; |
393 | fs_devices->latest_trans = orig->latest_trans; | |
394 | memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); | |
395 | ||
46224705 | 396 | /* We have held the volume lock, it is safe to get the devices. */ |
e4404d6e YZ |
397 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
398 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
399 | if (!device) | |
400 | goto error; | |
401 | ||
402 | device->name = kstrdup(orig_dev->name, GFP_NOFS); | |
fd2696f3 JL |
403 | if (!device->name) { |
404 | kfree(device); | |
e4404d6e | 405 | goto error; |
fd2696f3 | 406 | } |
e4404d6e YZ |
407 | |
408 | device->devid = orig_dev->devid; | |
409 | device->work.func = pending_bios_fn; | |
410 | memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid)); | |
e4404d6e YZ |
411 | spin_lock_init(&device->io_lock); |
412 | INIT_LIST_HEAD(&device->dev_list); | |
413 | INIT_LIST_HEAD(&device->dev_alloc_list); | |
414 | ||
415 | list_add(&device->dev_list, &fs_devices->devices); | |
416 | device->fs_devices = fs_devices; | |
417 | fs_devices->num_devices++; | |
418 | } | |
419 | return fs_devices; | |
420 | error: | |
421 | free_fs_devices(fs_devices); | |
422 | return ERR_PTR(-ENOMEM); | |
423 | } | |
424 | ||
dfe25020 CM |
425 | int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) |
426 | { | |
c6e30871 | 427 | struct btrfs_device *device, *next; |
dfe25020 CM |
428 | |
429 | mutex_lock(&uuid_mutex); | |
430 | again: | |
46224705 | 431 | /* This is the initialized path, it is safe to release the devices. */ |
c6e30871 | 432 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
2b82032c YZ |
433 | if (device->in_fs_metadata) |
434 | continue; | |
435 | ||
436 | if (device->bdev) { | |
d4d77629 | 437 | blkdev_put(device->bdev, device->mode); |
2b82032c YZ |
438 | device->bdev = NULL; |
439 | fs_devices->open_devices--; | |
440 | } | |
441 | if (device->writeable) { | |
442 | list_del_init(&device->dev_alloc_list); | |
443 | device->writeable = 0; | |
444 | fs_devices->rw_devices--; | |
445 | } | |
e4404d6e YZ |
446 | list_del_init(&device->dev_list); |
447 | fs_devices->num_devices--; | |
448 | kfree(device->name); | |
449 | kfree(device); | |
dfe25020 | 450 | } |
2b82032c YZ |
451 | |
452 | if (fs_devices->seed) { | |
453 | fs_devices = fs_devices->seed; | |
2b82032c YZ |
454 | goto again; |
455 | } | |
456 | ||
dfe25020 CM |
457 | mutex_unlock(&uuid_mutex); |
458 | return 0; | |
459 | } | |
a0af469b | 460 | |
1f78160c XG |
461 | static void __free_device(struct work_struct *work) |
462 | { | |
463 | struct btrfs_device *device; | |
464 | ||
465 | device = container_of(work, struct btrfs_device, rcu_work); | |
466 | ||
467 | if (device->bdev) | |
468 | blkdev_put(device->bdev, device->mode); | |
469 | ||
470 | kfree(device->name); | |
471 | kfree(device); | |
472 | } | |
473 | ||
474 | static void free_device(struct rcu_head *head) | |
475 | { | |
476 | struct btrfs_device *device; | |
477 | ||
478 | device = container_of(head, struct btrfs_device, rcu); | |
479 | ||
480 | INIT_WORK(&device->rcu_work, __free_device); | |
481 | schedule_work(&device->rcu_work); | |
482 | } | |
483 | ||
2b82032c | 484 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
8a4b83cc | 485 | { |
8a4b83cc | 486 | struct btrfs_device *device; |
e4404d6e | 487 | |
2b82032c YZ |
488 | if (--fs_devices->opened > 0) |
489 | return 0; | |
8a4b83cc | 490 | |
c9513edb | 491 | mutex_lock(&fs_devices->device_list_mutex); |
c6e30871 | 492 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
1f78160c XG |
493 | struct btrfs_device *new_device; |
494 | ||
495 | if (device->bdev) | |
a0af469b | 496 | fs_devices->open_devices--; |
1f78160c | 497 | |
2b82032c YZ |
498 | if (device->writeable) { |
499 | list_del_init(&device->dev_alloc_list); | |
500 | fs_devices->rw_devices--; | |
501 | } | |
502 | ||
1f78160c XG |
503 | new_device = kmalloc(sizeof(*new_device), GFP_NOFS); |
504 | BUG_ON(!new_device); | |
505 | memcpy(new_device, device, sizeof(*new_device)); | |
506 | new_device->name = kstrdup(device->name, GFP_NOFS); | |
5f3f302a | 507 | BUG_ON(device->name && !new_device->name); |
1f78160c XG |
508 | new_device->bdev = NULL; |
509 | new_device->writeable = 0; | |
510 | new_device->in_fs_metadata = 0; | |
511 | list_replace_rcu(&device->dev_list, &new_device->dev_list); | |
512 | ||
513 | call_rcu(&device->rcu, free_device); | |
8a4b83cc | 514 | } |
c9513edb XG |
515 | mutex_unlock(&fs_devices->device_list_mutex); |
516 | ||
e4404d6e YZ |
517 | WARN_ON(fs_devices->open_devices); |
518 | WARN_ON(fs_devices->rw_devices); | |
2b82032c YZ |
519 | fs_devices->opened = 0; |
520 | fs_devices->seeding = 0; | |
2b82032c | 521 | |
8a4b83cc CM |
522 | return 0; |
523 | } | |
524 | ||
2b82032c YZ |
525 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
526 | { | |
e4404d6e | 527 | struct btrfs_fs_devices *seed_devices = NULL; |
2b82032c YZ |
528 | int ret; |
529 | ||
530 | mutex_lock(&uuid_mutex); | |
531 | ret = __btrfs_close_devices(fs_devices); | |
e4404d6e YZ |
532 | if (!fs_devices->opened) { |
533 | seed_devices = fs_devices->seed; | |
534 | fs_devices->seed = NULL; | |
535 | } | |
2b82032c | 536 | mutex_unlock(&uuid_mutex); |
e4404d6e YZ |
537 | |
538 | while (seed_devices) { | |
539 | fs_devices = seed_devices; | |
540 | seed_devices = fs_devices->seed; | |
541 | __btrfs_close_devices(fs_devices); | |
542 | free_fs_devices(fs_devices); | |
543 | } | |
2b82032c YZ |
544 | return ret; |
545 | } | |
546 | ||
e4404d6e YZ |
547 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
548 | fmode_t flags, void *holder) | |
8a4b83cc CM |
549 | { |
550 | struct block_device *bdev; | |
551 | struct list_head *head = &fs_devices->devices; | |
8a4b83cc | 552 | struct btrfs_device *device; |
a0af469b CM |
553 | struct block_device *latest_bdev = NULL; |
554 | struct buffer_head *bh; | |
555 | struct btrfs_super_block *disk_super; | |
556 | u64 latest_devid = 0; | |
557 | u64 latest_transid = 0; | |
a0af469b | 558 | u64 devid; |
2b82032c | 559 | int seeding = 1; |
a0af469b | 560 | int ret = 0; |
8a4b83cc | 561 | |
d4d77629 TH |
562 | flags |= FMODE_EXCL; |
563 | ||
c6e30871 | 564 | list_for_each_entry(device, head, dev_list) { |
c1c4d91c CM |
565 | if (device->bdev) |
566 | continue; | |
dfe25020 CM |
567 | if (!device->name) |
568 | continue; | |
569 | ||
d4d77629 | 570 | bdev = blkdev_get_by_path(device->name, flags, holder); |
8a4b83cc | 571 | if (IS_ERR(bdev)) { |
d397712b | 572 | printk(KERN_INFO "open %s failed\n", device->name); |
a0af469b | 573 | goto error; |
8a4b83cc | 574 | } |
a061fc8d | 575 | set_blocksize(bdev, 4096); |
a0af469b | 576 | |
a512bbf8 | 577 | bh = btrfs_read_dev_super(bdev); |
20b45077 DY |
578 | if (!bh) { |
579 | ret = -EINVAL; | |
a0af469b | 580 | goto error_close; |
20b45077 | 581 | } |
a0af469b CM |
582 | |
583 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 584 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
a0af469b CM |
585 | if (devid != device->devid) |
586 | goto error_brelse; | |
587 | ||
2b82032c YZ |
588 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
589 | BTRFS_UUID_SIZE)) | |
590 | goto error_brelse; | |
591 | ||
592 | device->generation = btrfs_super_generation(disk_super); | |
593 | if (!latest_transid || device->generation > latest_transid) { | |
a0af469b | 594 | latest_devid = devid; |
2b82032c | 595 | latest_transid = device->generation; |
a0af469b CM |
596 | latest_bdev = bdev; |
597 | } | |
598 | ||
2b82032c YZ |
599 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
600 | device->writeable = 0; | |
601 | } else { | |
602 | device->writeable = !bdev_read_only(bdev); | |
603 | seeding = 0; | |
604 | } | |
605 | ||
8a4b83cc | 606 | device->bdev = bdev; |
dfe25020 | 607 | device->in_fs_metadata = 0; |
15916de8 CM |
608 | device->mode = flags; |
609 | ||
c289811c CM |
610 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
611 | fs_devices->rotating = 1; | |
612 | ||
a0af469b | 613 | fs_devices->open_devices++; |
2b82032c YZ |
614 | if (device->writeable) { |
615 | fs_devices->rw_devices++; | |
616 | list_add(&device->dev_alloc_list, | |
617 | &fs_devices->alloc_list); | |
618 | } | |
4f6c9328 | 619 | brelse(bh); |
a0af469b | 620 | continue; |
a061fc8d | 621 | |
a0af469b CM |
622 | error_brelse: |
623 | brelse(bh); | |
624 | error_close: | |
d4d77629 | 625 | blkdev_put(bdev, flags); |
a0af469b CM |
626 | error: |
627 | continue; | |
8a4b83cc | 628 | } |
a0af469b CM |
629 | if (fs_devices->open_devices == 0) { |
630 | ret = -EIO; | |
631 | goto out; | |
632 | } | |
2b82032c YZ |
633 | fs_devices->seeding = seeding; |
634 | fs_devices->opened = 1; | |
a0af469b CM |
635 | fs_devices->latest_bdev = latest_bdev; |
636 | fs_devices->latest_devid = latest_devid; | |
637 | fs_devices->latest_trans = latest_transid; | |
2b82032c | 638 | fs_devices->total_rw_bytes = 0; |
a0af469b | 639 | out: |
2b82032c YZ |
640 | return ret; |
641 | } | |
642 | ||
643 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, | |
97288f2c | 644 | fmode_t flags, void *holder) |
2b82032c YZ |
645 | { |
646 | int ret; | |
647 | ||
648 | mutex_lock(&uuid_mutex); | |
649 | if (fs_devices->opened) { | |
e4404d6e YZ |
650 | fs_devices->opened++; |
651 | ret = 0; | |
2b82032c | 652 | } else { |
15916de8 | 653 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
2b82032c | 654 | } |
8a4b83cc | 655 | mutex_unlock(&uuid_mutex); |
8a4b83cc CM |
656 | return ret; |
657 | } | |
658 | ||
97288f2c | 659 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
8a4b83cc CM |
660 | struct btrfs_fs_devices **fs_devices_ret) |
661 | { | |
662 | struct btrfs_super_block *disk_super; | |
663 | struct block_device *bdev; | |
664 | struct buffer_head *bh; | |
665 | int ret; | |
666 | u64 devid; | |
f2984462 | 667 | u64 transid; |
8a4b83cc CM |
668 | |
669 | mutex_lock(&uuid_mutex); | |
670 | ||
d4d77629 TH |
671 | flags |= FMODE_EXCL; |
672 | bdev = blkdev_get_by_path(path, flags, holder); | |
8a4b83cc CM |
673 | |
674 | if (IS_ERR(bdev)) { | |
8a4b83cc CM |
675 | ret = PTR_ERR(bdev); |
676 | goto error; | |
677 | } | |
678 | ||
679 | ret = set_blocksize(bdev, 4096); | |
680 | if (ret) | |
681 | goto error_close; | |
a512bbf8 | 682 | bh = btrfs_read_dev_super(bdev); |
8a4b83cc | 683 | if (!bh) { |
20b45077 | 684 | ret = -EINVAL; |
8a4b83cc CM |
685 | goto error_close; |
686 | } | |
687 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 688 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
f2984462 | 689 | transid = btrfs_super_generation(disk_super); |
7ae9c09d | 690 | if (disk_super->label[0]) |
d397712b | 691 | printk(KERN_INFO "device label %s ", disk_super->label); |
22b63a29 ID |
692 | else |
693 | printk(KERN_INFO "device fsid %pU ", disk_super->fsid); | |
119e10cf | 694 | printk(KERN_CONT "devid %llu transid %llu %s\n", |
d397712b | 695 | (unsigned long long)devid, (unsigned long long)transid, path); |
8a4b83cc CM |
696 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
697 | ||
8a4b83cc CM |
698 | brelse(bh); |
699 | error_close: | |
d4d77629 | 700 | blkdev_put(bdev, flags); |
8a4b83cc CM |
701 | error: |
702 | mutex_unlock(&uuid_mutex); | |
703 | return ret; | |
704 | } | |
0b86a832 | 705 | |
6d07bcec MX |
706 | /* helper to account the used device space in the range */ |
707 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, | |
708 | u64 end, u64 *length) | |
709 | { | |
710 | struct btrfs_key key; | |
711 | struct btrfs_root *root = device->dev_root; | |
712 | struct btrfs_dev_extent *dev_extent; | |
713 | struct btrfs_path *path; | |
714 | u64 extent_end; | |
715 | int ret; | |
716 | int slot; | |
717 | struct extent_buffer *l; | |
718 | ||
719 | *length = 0; | |
720 | ||
721 | if (start >= device->total_bytes) | |
722 | return 0; | |
723 | ||
724 | path = btrfs_alloc_path(); | |
725 | if (!path) | |
726 | return -ENOMEM; | |
727 | path->reada = 2; | |
728 | ||
729 | key.objectid = device->devid; | |
730 | key.offset = start; | |
731 | key.type = BTRFS_DEV_EXTENT_KEY; | |
732 | ||
733 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
734 | if (ret < 0) | |
735 | goto out; | |
736 | if (ret > 0) { | |
737 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
738 | if (ret < 0) | |
739 | goto out; | |
740 | } | |
741 | ||
742 | while (1) { | |
743 | l = path->nodes[0]; | |
744 | slot = path->slots[0]; | |
745 | if (slot >= btrfs_header_nritems(l)) { | |
746 | ret = btrfs_next_leaf(root, path); | |
747 | if (ret == 0) | |
748 | continue; | |
749 | if (ret < 0) | |
750 | goto out; | |
751 | ||
752 | break; | |
753 | } | |
754 | btrfs_item_key_to_cpu(l, &key, slot); | |
755 | ||
756 | if (key.objectid < device->devid) | |
757 | goto next; | |
758 | ||
759 | if (key.objectid > device->devid) | |
760 | break; | |
761 | ||
762 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) | |
763 | goto next; | |
764 | ||
765 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
766 | extent_end = key.offset + btrfs_dev_extent_length(l, | |
767 | dev_extent); | |
768 | if (key.offset <= start && extent_end > end) { | |
769 | *length = end - start + 1; | |
770 | break; | |
771 | } else if (key.offset <= start && extent_end > start) | |
772 | *length += extent_end - start; | |
773 | else if (key.offset > start && extent_end <= end) | |
774 | *length += extent_end - key.offset; | |
775 | else if (key.offset > start && key.offset <= end) { | |
776 | *length += end - key.offset + 1; | |
777 | break; | |
778 | } else if (key.offset > end) | |
779 | break; | |
780 | ||
781 | next: | |
782 | path->slots[0]++; | |
783 | } | |
784 | ret = 0; | |
785 | out: | |
786 | btrfs_free_path(path); | |
787 | return ret; | |
788 | } | |
789 | ||
0b86a832 | 790 | /* |
7bfc837d MX |
791 | * find_free_dev_extent - find free space in the specified device |
792 | * @trans: transaction handler | |
793 | * @device: the device which we search the free space in | |
794 | * @num_bytes: the size of the free space that we need | |
795 | * @start: store the start of the free space. | |
796 | * @len: the size of the free space. that we find, or the size of the max | |
797 | * free space if we don't find suitable free space | |
798 | * | |
0b86a832 CM |
799 | * this uses a pretty simple search, the expectation is that it is |
800 | * called very infrequently and that a given device has a small number | |
801 | * of extents | |
7bfc837d MX |
802 | * |
803 | * @start is used to store the start of the free space if we find. But if we | |
804 | * don't find suitable free space, it will be used to store the start position | |
805 | * of the max free space. | |
806 | * | |
807 | * @len is used to store the size of the free space that we find. | |
808 | * But if we don't find suitable free space, it is used to store the size of | |
809 | * the max free space. | |
0b86a832 | 810 | */ |
ba1bf481 JB |
811 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
812 | struct btrfs_device *device, u64 num_bytes, | |
7bfc837d | 813 | u64 *start, u64 *len) |
0b86a832 CM |
814 | { |
815 | struct btrfs_key key; | |
816 | struct btrfs_root *root = device->dev_root; | |
7bfc837d | 817 | struct btrfs_dev_extent *dev_extent; |
2b82032c | 818 | struct btrfs_path *path; |
7bfc837d MX |
819 | u64 hole_size; |
820 | u64 max_hole_start; | |
821 | u64 max_hole_size; | |
822 | u64 extent_end; | |
823 | u64 search_start; | |
0b86a832 CM |
824 | u64 search_end = device->total_bytes; |
825 | int ret; | |
7bfc837d | 826 | int slot; |
0b86a832 CM |
827 | struct extent_buffer *l; |
828 | ||
0b86a832 CM |
829 | /* FIXME use last free of some kind */ |
830 | ||
8a4b83cc CM |
831 | /* we don't want to overwrite the superblock on the drive, |
832 | * so we make sure to start at an offset of at least 1MB | |
833 | */ | |
a9c9bf68 | 834 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
8f18cf13 | 835 | |
7bfc837d MX |
836 | max_hole_start = search_start; |
837 | max_hole_size = 0; | |
838 | ||
839 | if (search_start >= search_end) { | |
840 | ret = -ENOSPC; | |
841 | goto error; | |
842 | } | |
843 | ||
844 | path = btrfs_alloc_path(); | |
845 | if (!path) { | |
846 | ret = -ENOMEM; | |
847 | goto error; | |
848 | } | |
849 | path->reada = 2; | |
850 | ||
0b86a832 CM |
851 | key.objectid = device->devid; |
852 | key.offset = search_start; | |
853 | key.type = BTRFS_DEV_EXTENT_KEY; | |
7bfc837d | 854 | |
0b86a832 CM |
855 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
856 | if (ret < 0) | |
7bfc837d | 857 | goto out; |
1fcbac58 YZ |
858 | if (ret > 0) { |
859 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
860 | if (ret < 0) | |
7bfc837d | 861 | goto out; |
1fcbac58 | 862 | } |
7bfc837d | 863 | |
0b86a832 CM |
864 | while (1) { |
865 | l = path->nodes[0]; | |
866 | slot = path->slots[0]; | |
867 | if (slot >= btrfs_header_nritems(l)) { | |
868 | ret = btrfs_next_leaf(root, path); | |
869 | if (ret == 0) | |
870 | continue; | |
871 | if (ret < 0) | |
7bfc837d MX |
872 | goto out; |
873 | ||
874 | break; | |
0b86a832 CM |
875 | } |
876 | btrfs_item_key_to_cpu(l, &key, slot); | |
877 | ||
878 | if (key.objectid < device->devid) | |
879 | goto next; | |
880 | ||
881 | if (key.objectid > device->devid) | |
7bfc837d | 882 | break; |
0b86a832 | 883 | |
7bfc837d MX |
884 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
885 | goto next; | |
9779b72f | 886 | |
7bfc837d MX |
887 | if (key.offset > search_start) { |
888 | hole_size = key.offset - search_start; | |
9779b72f | 889 | |
7bfc837d MX |
890 | if (hole_size > max_hole_size) { |
891 | max_hole_start = search_start; | |
892 | max_hole_size = hole_size; | |
893 | } | |
9779b72f | 894 | |
7bfc837d MX |
895 | /* |
896 | * If this free space is greater than which we need, | |
897 | * it must be the max free space that we have found | |
898 | * until now, so max_hole_start must point to the start | |
899 | * of this free space and the length of this free space | |
900 | * is stored in max_hole_size. Thus, we return | |
901 | * max_hole_start and max_hole_size and go back to the | |
902 | * caller. | |
903 | */ | |
904 | if (hole_size >= num_bytes) { | |
905 | ret = 0; | |
906 | goto out; | |
0b86a832 CM |
907 | } |
908 | } | |
0b86a832 | 909 | |
0b86a832 | 910 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
7bfc837d MX |
911 | extent_end = key.offset + btrfs_dev_extent_length(l, |
912 | dev_extent); | |
913 | if (extent_end > search_start) | |
914 | search_start = extent_end; | |
0b86a832 CM |
915 | next: |
916 | path->slots[0]++; | |
917 | cond_resched(); | |
918 | } | |
0b86a832 | 919 | |
7bfc837d MX |
920 | hole_size = search_end- search_start; |
921 | if (hole_size > max_hole_size) { | |
922 | max_hole_start = search_start; | |
923 | max_hole_size = hole_size; | |
0b86a832 | 924 | } |
0b86a832 | 925 | |
7bfc837d MX |
926 | /* See above. */ |
927 | if (hole_size < num_bytes) | |
928 | ret = -ENOSPC; | |
929 | else | |
930 | ret = 0; | |
931 | ||
932 | out: | |
2b82032c | 933 | btrfs_free_path(path); |
7bfc837d MX |
934 | error: |
935 | *start = max_hole_start; | |
b2117a39 | 936 | if (len) |
7bfc837d | 937 | *len = max_hole_size; |
0b86a832 CM |
938 | return ret; |
939 | } | |
940 | ||
b2950863 | 941 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
942 | struct btrfs_device *device, |
943 | u64 start) | |
944 | { | |
945 | int ret; | |
946 | struct btrfs_path *path; | |
947 | struct btrfs_root *root = device->dev_root; | |
948 | struct btrfs_key key; | |
a061fc8d CM |
949 | struct btrfs_key found_key; |
950 | struct extent_buffer *leaf = NULL; | |
951 | struct btrfs_dev_extent *extent = NULL; | |
8f18cf13 CM |
952 | |
953 | path = btrfs_alloc_path(); | |
954 | if (!path) | |
955 | return -ENOMEM; | |
956 | ||
957 | key.objectid = device->devid; | |
958 | key.offset = start; | |
959 | key.type = BTRFS_DEV_EXTENT_KEY; | |
960 | ||
961 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
a061fc8d CM |
962 | if (ret > 0) { |
963 | ret = btrfs_previous_item(root, path, key.objectid, | |
964 | BTRFS_DEV_EXTENT_KEY); | |
b0b802d7 TI |
965 | if (ret) |
966 | goto out; | |
a061fc8d CM |
967 | leaf = path->nodes[0]; |
968 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
969 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
970 | struct btrfs_dev_extent); | |
971 | BUG_ON(found_key.offset > start || found_key.offset + | |
972 | btrfs_dev_extent_length(leaf, extent) < start); | |
a061fc8d CM |
973 | } else if (ret == 0) { |
974 | leaf = path->nodes[0]; | |
975 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
976 | struct btrfs_dev_extent); | |
977 | } | |
8f18cf13 CM |
978 | BUG_ON(ret); |
979 | ||
dfe25020 CM |
980 | if (device->bytes_used > 0) |
981 | device->bytes_used -= btrfs_dev_extent_length(leaf, extent); | |
8f18cf13 | 982 | ret = btrfs_del_item(trans, root, path); |
8f18cf13 | 983 | |
b0b802d7 | 984 | out: |
8f18cf13 CM |
985 | btrfs_free_path(path); |
986 | return ret; | |
987 | } | |
988 | ||
2b82032c | 989 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
0b86a832 | 990 | struct btrfs_device *device, |
e17cade2 | 991 | u64 chunk_tree, u64 chunk_objectid, |
2b82032c | 992 | u64 chunk_offset, u64 start, u64 num_bytes) |
0b86a832 CM |
993 | { |
994 | int ret; | |
995 | struct btrfs_path *path; | |
996 | struct btrfs_root *root = device->dev_root; | |
997 | struct btrfs_dev_extent *extent; | |
998 | struct extent_buffer *leaf; | |
999 | struct btrfs_key key; | |
1000 | ||
dfe25020 | 1001 | WARN_ON(!device->in_fs_metadata); |
0b86a832 CM |
1002 | path = btrfs_alloc_path(); |
1003 | if (!path) | |
1004 | return -ENOMEM; | |
1005 | ||
0b86a832 | 1006 | key.objectid = device->devid; |
2b82032c | 1007 | key.offset = start; |
0b86a832 CM |
1008 | key.type = BTRFS_DEV_EXTENT_KEY; |
1009 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1010 | sizeof(*extent)); | |
1011 | BUG_ON(ret); | |
1012 | ||
1013 | leaf = path->nodes[0]; | |
1014 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1015 | struct btrfs_dev_extent); | |
e17cade2 CM |
1016 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
1017 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); | |
1018 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); | |
1019 | ||
1020 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, | |
1021 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), | |
1022 | BTRFS_UUID_SIZE); | |
1023 | ||
0b86a832 CM |
1024 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
1025 | btrfs_mark_buffer_dirty(leaf); | |
0b86a832 CM |
1026 | btrfs_free_path(path); |
1027 | return ret; | |
1028 | } | |
1029 | ||
a1b32a59 CM |
1030 | static noinline int find_next_chunk(struct btrfs_root *root, |
1031 | u64 objectid, u64 *offset) | |
0b86a832 CM |
1032 | { |
1033 | struct btrfs_path *path; | |
1034 | int ret; | |
1035 | struct btrfs_key key; | |
e17cade2 | 1036 | struct btrfs_chunk *chunk; |
0b86a832 CM |
1037 | struct btrfs_key found_key; |
1038 | ||
1039 | path = btrfs_alloc_path(); | |
1040 | BUG_ON(!path); | |
1041 | ||
e17cade2 | 1042 | key.objectid = objectid; |
0b86a832 CM |
1043 | key.offset = (u64)-1; |
1044 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1045 | ||
1046 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1047 | if (ret < 0) | |
1048 | goto error; | |
1049 | ||
1050 | BUG_ON(ret == 0); | |
1051 | ||
1052 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); | |
1053 | if (ret) { | |
e17cade2 | 1054 | *offset = 0; |
0b86a832 CM |
1055 | } else { |
1056 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1057 | path->slots[0]); | |
e17cade2 CM |
1058 | if (found_key.objectid != objectid) |
1059 | *offset = 0; | |
1060 | else { | |
1061 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1062 | struct btrfs_chunk); | |
1063 | *offset = found_key.offset + | |
1064 | btrfs_chunk_length(path->nodes[0], chunk); | |
1065 | } | |
0b86a832 CM |
1066 | } |
1067 | ret = 0; | |
1068 | error: | |
1069 | btrfs_free_path(path); | |
1070 | return ret; | |
1071 | } | |
1072 | ||
2b82032c | 1073 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
0b86a832 CM |
1074 | { |
1075 | int ret; | |
1076 | struct btrfs_key key; | |
1077 | struct btrfs_key found_key; | |
2b82032c YZ |
1078 | struct btrfs_path *path; |
1079 | ||
1080 | root = root->fs_info->chunk_root; | |
1081 | ||
1082 | path = btrfs_alloc_path(); | |
1083 | if (!path) | |
1084 | return -ENOMEM; | |
0b86a832 CM |
1085 | |
1086 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1087 | key.type = BTRFS_DEV_ITEM_KEY; | |
1088 | key.offset = (u64)-1; | |
1089 | ||
1090 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1091 | if (ret < 0) | |
1092 | goto error; | |
1093 | ||
1094 | BUG_ON(ret == 0); | |
1095 | ||
1096 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, | |
1097 | BTRFS_DEV_ITEM_KEY); | |
1098 | if (ret) { | |
1099 | *objectid = 1; | |
1100 | } else { | |
1101 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1102 | path->slots[0]); | |
1103 | *objectid = found_key.offset + 1; | |
1104 | } | |
1105 | ret = 0; | |
1106 | error: | |
2b82032c | 1107 | btrfs_free_path(path); |
0b86a832 CM |
1108 | return ret; |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * the device information is stored in the chunk root | |
1113 | * the btrfs_device struct should be fully filled in | |
1114 | */ | |
1115 | int btrfs_add_device(struct btrfs_trans_handle *trans, | |
1116 | struct btrfs_root *root, | |
1117 | struct btrfs_device *device) | |
1118 | { | |
1119 | int ret; | |
1120 | struct btrfs_path *path; | |
1121 | struct btrfs_dev_item *dev_item; | |
1122 | struct extent_buffer *leaf; | |
1123 | struct btrfs_key key; | |
1124 | unsigned long ptr; | |
0b86a832 CM |
1125 | |
1126 | root = root->fs_info->chunk_root; | |
1127 | ||
1128 | path = btrfs_alloc_path(); | |
1129 | if (!path) | |
1130 | return -ENOMEM; | |
1131 | ||
0b86a832 CM |
1132 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1133 | key.type = BTRFS_DEV_ITEM_KEY; | |
2b82032c | 1134 | key.offset = device->devid; |
0b86a832 CM |
1135 | |
1136 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
0d81ba5d | 1137 | sizeof(*dev_item)); |
0b86a832 CM |
1138 | if (ret) |
1139 | goto out; | |
1140 | ||
1141 | leaf = path->nodes[0]; | |
1142 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1143 | ||
1144 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2b82032c | 1145 | btrfs_set_device_generation(leaf, dev_item, 0); |
0b86a832 CM |
1146 | btrfs_set_device_type(leaf, dev_item, device->type); |
1147 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1148 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1149 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
0b86a832 CM |
1150 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
1151 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | |
e17cade2 CM |
1152 | btrfs_set_device_group(leaf, dev_item, 0); |
1153 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
1154 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
c3027eb5 | 1155 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
0b86a832 | 1156 | |
0b86a832 | 1157 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
e17cade2 | 1158 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
2b82032c YZ |
1159 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
1160 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); | |
0b86a832 | 1161 | btrfs_mark_buffer_dirty(leaf); |
0b86a832 | 1162 | |
2b82032c | 1163 | ret = 0; |
0b86a832 CM |
1164 | out: |
1165 | btrfs_free_path(path); | |
1166 | return ret; | |
1167 | } | |
8f18cf13 | 1168 | |
a061fc8d CM |
1169 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
1170 | struct btrfs_device *device) | |
1171 | { | |
1172 | int ret; | |
1173 | struct btrfs_path *path; | |
a061fc8d | 1174 | struct btrfs_key key; |
a061fc8d CM |
1175 | struct btrfs_trans_handle *trans; |
1176 | ||
1177 | root = root->fs_info->chunk_root; | |
1178 | ||
1179 | path = btrfs_alloc_path(); | |
1180 | if (!path) | |
1181 | return -ENOMEM; | |
1182 | ||
a22285a6 | 1183 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1184 | if (IS_ERR(trans)) { |
1185 | btrfs_free_path(path); | |
1186 | return PTR_ERR(trans); | |
1187 | } | |
a061fc8d CM |
1188 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1189 | key.type = BTRFS_DEV_ITEM_KEY; | |
1190 | key.offset = device->devid; | |
7d9eb12c | 1191 | lock_chunks(root); |
a061fc8d CM |
1192 | |
1193 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1194 | if (ret < 0) | |
1195 | goto out; | |
1196 | ||
1197 | if (ret > 0) { | |
1198 | ret = -ENOENT; | |
1199 | goto out; | |
1200 | } | |
1201 | ||
1202 | ret = btrfs_del_item(trans, root, path); | |
1203 | if (ret) | |
1204 | goto out; | |
a061fc8d CM |
1205 | out: |
1206 | btrfs_free_path(path); | |
7d9eb12c | 1207 | unlock_chunks(root); |
a061fc8d CM |
1208 | btrfs_commit_transaction(trans, root); |
1209 | return ret; | |
1210 | } | |
1211 | ||
1212 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) | |
1213 | { | |
1214 | struct btrfs_device *device; | |
2b82032c | 1215 | struct btrfs_device *next_device; |
a061fc8d | 1216 | struct block_device *bdev; |
dfe25020 | 1217 | struct buffer_head *bh = NULL; |
a061fc8d | 1218 | struct btrfs_super_block *disk_super; |
1f78160c | 1219 | struct btrfs_fs_devices *cur_devices; |
a061fc8d CM |
1220 | u64 all_avail; |
1221 | u64 devid; | |
2b82032c YZ |
1222 | u64 num_devices; |
1223 | u8 *dev_uuid; | |
a061fc8d | 1224 | int ret = 0; |
1f78160c | 1225 | bool clear_super = false; |
a061fc8d | 1226 | |
a061fc8d | 1227 | mutex_lock(&uuid_mutex); |
7d9eb12c | 1228 | mutex_lock(&root->fs_info->volume_mutex); |
a061fc8d CM |
1229 | |
1230 | all_avail = root->fs_info->avail_data_alloc_bits | | |
1231 | root->fs_info->avail_system_alloc_bits | | |
1232 | root->fs_info->avail_metadata_alloc_bits; | |
1233 | ||
1234 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && | |
035fe03a | 1235 | root->fs_info->fs_devices->num_devices <= 4) { |
d397712b CM |
1236 | printk(KERN_ERR "btrfs: unable to go below four devices " |
1237 | "on raid10\n"); | |
a061fc8d CM |
1238 | ret = -EINVAL; |
1239 | goto out; | |
1240 | } | |
1241 | ||
1242 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && | |
035fe03a | 1243 | root->fs_info->fs_devices->num_devices <= 2) { |
d397712b CM |
1244 | printk(KERN_ERR "btrfs: unable to go below two " |
1245 | "devices on raid1\n"); | |
a061fc8d CM |
1246 | ret = -EINVAL; |
1247 | goto out; | |
1248 | } | |
1249 | ||
dfe25020 | 1250 | if (strcmp(device_path, "missing") == 0) { |
dfe25020 CM |
1251 | struct list_head *devices; |
1252 | struct btrfs_device *tmp; | |
a061fc8d | 1253 | |
dfe25020 CM |
1254 | device = NULL; |
1255 | devices = &root->fs_info->fs_devices->devices; | |
46224705 XG |
1256 | /* |
1257 | * It is safe to read the devices since the volume_mutex | |
1258 | * is held. | |
1259 | */ | |
c6e30871 | 1260 | list_for_each_entry(tmp, devices, dev_list) { |
dfe25020 CM |
1261 | if (tmp->in_fs_metadata && !tmp->bdev) { |
1262 | device = tmp; | |
1263 | break; | |
1264 | } | |
1265 | } | |
1266 | bdev = NULL; | |
1267 | bh = NULL; | |
1268 | disk_super = NULL; | |
1269 | if (!device) { | |
d397712b CM |
1270 | printk(KERN_ERR "btrfs: no missing devices found to " |
1271 | "remove\n"); | |
dfe25020 CM |
1272 | goto out; |
1273 | } | |
dfe25020 | 1274 | } else { |
d4d77629 TH |
1275 | bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL, |
1276 | root->fs_info->bdev_holder); | |
dfe25020 CM |
1277 | if (IS_ERR(bdev)) { |
1278 | ret = PTR_ERR(bdev); | |
1279 | goto out; | |
1280 | } | |
a061fc8d | 1281 | |
2b82032c | 1282 | set_blocksize(bdev, 4096); |
a512bbf8 | 1283 | bh = btrfs_read_dev_super(bdev); |
dfe25020 | 1284 | if (!bh) { |
20b45077 | 1285 | ret = -EINVAL; |
dfe25020 CM |
1286 | goto error_close; |
1287 | } | |
1288 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 1289 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
2b82032c YZ |
1290 | dev_uuid = disk_super->dev_item.uuid; |
1291 | device = btrfs_find_device(root, devid, dev_uuid, | |
1292 | disk_super->fsid); | |
dfe25020 CM |
1293 | if (!device) { |
1294 | ret = -ENOENT; | |
1295 | goto error_brelse; | |
1296 | } | |
2b82032c | 1297 | } |
dfe25020 | 1298 | |
2b82032c | 1299 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
d397712b CM |
1300 | printk(KERN_ERR "btrfs: unable to remove the only writeable " |
1301 | "device\n"); | |
2b82032c YZ |
1302 | ret = -EINVAL; |
1303 | goto error_brelse; | |
1304 | } | |
1305 | ||
1306 | if (device->writeable) { | |
0c1daee0 | 1307 | lock_chunks(root); |
2b82032c | 1308 | list_del_init(&device->dev_alloc_list); |
0c1daee0 | 1309 | unlock_chunks(root); |
2b82032c | 1310 | root->fs_info->fs_devices->rw_devices--; |
1f78160c | 1311 | clear_super = true; |
dfe25020 | 1312 | } |
a061fc8d CM |
1313 | |
1314 | ret = btrfs_shrink_device(device, 0); | |
1315 | if (ret) | |
9b3517e9 | 1316 | goto error_undo; |
a061fc8d | 1317 | |
a061fc8d CM |
1318 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
1319 | if (ret) | |
9b3517e9 | 1320 | goto error_undo; |
a061fc8d | 1321 | |
2b82032c | 1322 | device->in_fs_metadata = 0; |
a2de733c | 1323 | btrfs_scrub_cancel_dev(root, device); |
e5e9a520 CM |
1324 | |
1325 | /* | |
1326 | * the device list mutex makes sure that we don't change | |
1327 | * the device list while someone else is writing out all | |
1328 | * the device supers. | |
1329 | */ | |
1f78160c XG |
1330 | |
1331 | cur_devices = device->fs_devices; | |
e5e9a520 | 1332 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
1f78160c | 1333 | list_del_rcu(&device->dev_list); |
e5e9a520 | 1334 | |
e4404d6e | 1335 | device->fs_devices->num_devices--; |
2b82032c | 1336 | |
cd02dca5 CM |
1337 | if (device->missing) |
1338 | root->fs_info->fs_devices->missing_devices--; | |
1339 | ||
2b82032c YZ |
1340 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
1341 | struct btrfs_device, dev_list); | |
1342 | if (device->bdev == root->fs_info->sb->s_bdev) | |
1343 | root->fs_info->sb->s_bdev = next_device->bdev; | |
1344 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) | |
1345 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; | |
1346 | ||
1f78160c | 1347 | if (device->bdev) |
e4404d6e | 1348 | device->fs_devices->open_devices--; |
1f78160c XG |
1349 | |
1350 | call_rcu(&device->rcu, free_device); | |
1351 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); | |
e4404d6e | 1352 | |
2b82032c YZ |
1353 | num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; |
1354 | btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices); | |
1355 | ||
1f78160c | 1356 | if (cur_devices->open_devices == 0) { |
e4404d6e YZ |
1357 | struct btrfs_fs_devices *fs_devices; |
1358 | fs_devices = root->fs_info->fs_devices; | |
1359 | while (fs_devices) { | |
1f78160c | 1360 | if (fs_devices->seed == cur_devices) |
e4404d6e YZ |
1361 | break; |
1362 | fs_devices = fs_devices->seed; | |
2b82032c | 1363 | } |
1f78160c XG |
1364 | fs_devices->seed = cur_devices->seed; |
1365 | cur_devices->seed = NULL; | |
0c1daee0 | 1366 | lock_chunks(root); |
1f78160c | 1367 | __btrfs_close_devices(cur_devices); |
0c1daee0 | 1368 | unlock_chunks(root); |
1f78160c | 1369 | free_fs_devices(cur_devices); |
2b82032c YZ |
1370 | } |
1371 | ||
1372 | /* | |
1373 | * at this point, the device is zero sized. We want to | |
1374 | * remove it from the devices list and zero out the old super | |
1375 | */ | |
1f78160c | 1376 | if (clear_super) { |
dfe25020 CM |
1377 | /* make sure this device isn't detected as part of |
1378 | * the FS anymore | |
1379 | */ | |
1380 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); | |
1381 | set_buffer_dirty(bh); | |
1382 | sync_dirty_buffer(bh); | |
dfe25020 | 1383 | } |
a061fc8d | 1384 | |
a061fc8d | 1385 | ret = 0; |
a061fc8d CM |
1386 | |
1387 | error_brelse: | |
1388 | brelse(bh); | |
1389 | error_close: | |
dfe25020 | 1390 | if (bdev) |
e525fd89 | 1391 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
a061fc8d | 1392 | out: |
7d9eb12c | 1393 | mutex_unlock(&root->fs_info->volume_mutex); |
a061fc8d | 1394 | mutex_unlock(&uuid_mutex); |
a061fc8d | 1395 | return ret; |
9b3517e9 ID |
1396 | error_undo: |
1397 | if (device->writeable) { | |
0c1daee0 | 1398 | lock_chunks(root); |
9b3517e9 ID |
1399 | list_add(&device->dev_alloc_list, |
1400 | &root->fs_info->fs_devices->alloc_list); | |
0c1daee0 | 1401 | unlock_chunks(root); |
9b3517e9 ID |
1402 | root->fs_info->fs_devices->rw_devices++; |
1403 | } | |
1404 | goto error_brelse; | |
a061fc8d CM |
1405 | } |
1406 | ||
2b82032c YZ |
1407 | /* |
1408 | * does all the dirty work required for changing file system's UUID. | |
1409 | */ | |
1410 | static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans, | |
1411 | struct btrfs_root *root) | |
1412 | { | |
1413 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
1414 | struct btrfs_fs_devices *old_devices; | |
e4404d6e | 1415 | struct btrfs_fs_devices *seed_devices; |
2b82032c YZ |
1416 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
1417 | struct btrfs_device *device; | |
1418 | u64 super_flags; | |
1419 | ||
1420 | BUG_ON(!mutex_is_locked(&uuid_mutex)); | |
e4404d6e | 1421 | if (!fs_devices->seeding) |
2b82032c YZ |
1422 | return -EINVAL; |
1423 | ||
e4404d6e YZ |
1424 | seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
1425 | if (!seed_devices) | |
2b82032c YZ |
1426 | return -ENOMEM; |
1427 | ||
e4404d6e YZ |
1428 | old_devices = clone_fs_devices(fs_devices); |
1429 | if (IS_ERR(old_devices)) { | |
1430 | kfree(seed_devices); | |
1431 | return PTR_ERR(old_devices); | |
2b82032c | 1432 | } |
e4404d6e | 1433 | |
2b82032c YZ |
1434 | list_add(&old_devices->list, &fs_uuids); |
1435 | ||
e4404d6e YZ |
1436 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
1437 | seed_devices->opened = 1; | |
1438 | INIT_LIST_HEAD(&seed_devices->devices); | |
1439 | INIT_LIST_HEAD(&seed_devices->alloc_list); | |
e5e9a520 | 1440 | mutex_init(&seed_devices->device_list_mutex); |
c9513edb XG |
1441 | |
1442 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | |
1f78160c XG |
1443 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
1444 | synchronize_rcu); | |
c9513edb XG |
1445 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
1446 | ||
e4404d6e YZ |
1447 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
1448 | list_for_each_entry(device, &seed_devices->devices, dev_list) { | |
1449 | device->fs_devices = seed_devices; | |
1450 | } | |
1451 | ||
2b82032c YZ |
1452 | fs_devices->seeding = 0; |
1453 | fs_devices->num_devices = 0; | |
1454 | fs_devices->open_devices = 0; | |
e4404d6e | 1455 | fs_devices->seed = seed_devices; |
2b82032c YZ |
1456 | |
1457 | generate_random_uuid(fs_devices->fsid); | |
1458 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); | |
1459 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); | |
1460 | super_flags = btrfs_super_flags(disk_super) & | |
1461 | ~BTRFS_SUPER_FLAG_SEEDING; | |
1462 | btrfs_set_super_flags(disk_super, super_flags); | |
1463 | ||
1464 | return 0; | |
1465 | } | |
1466 | ||
1467 | /* | |
1468 | * strore the expected generation for seed devices in device items. | |
1469 | */ | |
1470 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, | |
1471 | struct btrfs_root *root) | |
1472 | { | |
1473 | struct btrfs_path *path; | |
1474 | struct extent_buffer *leaf; | |
1475 | struct btrfs_dev_item *dev_item; | |
1476 | struct btrfs_device *device; | |
1477 | struct btrfs_key key; | |
1478 | u8 fs_uuid[BTRFS_UUID_SIZE]; | |
1479 | u8 dev_uuid[BTRFS_UUID_SIZE]; | |
1480 | u64 devid; | |
1481 | int ret; | |
1482 | ||
1483 | path = btrfs_alloc_path(); | |
1484 | if (!path) | |
1485 | return -ENOMEM; | |
1486 | ||
1487 | root = root->fs_info->chunk_root; | |
1488 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1489 | key.offset = 0; | |
1490 | key.type = BTRFS_DEV_ITEM_KEY; | |
1491 | ||
1492 | while (1) { | |
1493 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1494 | if (ret < 0) | |
1495 | goto error; | |
1496 | ||
1497 | leaf = path->nodes[0]; | |
1498 | next_slot: | |
1499 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1500 | ret = btrfs_next_leaf(root, path); | |
1501 | if (ret > 0) | |
1502 | break; | |
1503 | if (ret < 0) | |
1504 | goto error; | |
1505 | leaf = path->nodes[0]; | |
1506 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 1507 | btrfs_release_path(path); |
2b82032c YZ |
1508 | continue; |
1509 | } | |
1510 | ||
1511 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1512 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || | |
1513 | key.type != BTRFS_DEV_ITEM_KEY) | |
1514 | break; | |
1515 | ||
1516 | dev_item = btrfs_item_ptr(leaf, path->slots[0], | |
1517 | struct btrfs_dev_item); | |
1518 | devid = btrfs_device_id(leaf, dev_item); | |
1519 | read_extent_buffer(leaf, dev_uuid, | |
1520 | (unsigned long)btrfs_device_uuid(dev_item), | |
1521 | BTRFS_UUID_SIZE); | |
1522 | read_extent_buffer(leaf, fs_uuid, | |
1523 | (unsigned long)btrfs_device_fsid(dev_item), | |
1524 | BTRFS_UUID_SIZE); | |
1525 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); | |
1526 | BUG_ON(!device); | |
1527 | ||
1528 | if (device->fs_devices->seeding) { | |
1529 | btrfs_set_device_generation(leaf, dev_item, | |
1530 | device->generation); | |
1531 | btrfs_mark_buffer_dirty(leaf); | |
1532 | } | |
1533 | ||
1534 | path->slots[0]++; | |
1535 | goto next_slot; | |
1536 | } | |
1537 | ret = 0; | |
1538 | error: | |
1539 | btrfs_free_path(path); | |
1540 | return ret; | |
1541 | } | |
1542 | ||
788f20eb CM |
1543 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
1544 | { | |
1545 | struct btrfs_trans_handle *trans; | |
1546 | struct btrfs_device *device; | |
1547 | struct block_device *bdev; | |
788f20eb | 1548 | struct list_head *devices; |
2b82032c | 1549 | struct super_block *sb = root->fs_info->sb; |
788f20eb | 1550 | u64 total_bytes; |
2b82032c | 1551 | int seeding_dev = 0; |
788f20eb CM |
1552 | int ret = 0; |
1553 | ||
2b82032c YZ |
1554 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
1555 | return -EINVAL; | |
788f20eb | 1556 | |
d4d77629 TH |
1557 | bdev = blkdev_get_by_path(device_path, FMODE_EXCL, |
1558 | root->fs_info->bdev_holder); | |
7f59203a JB |
1559 | if (IS_ERR(bdev)) |
1560 | return PTR_ERR(bdev); | |
a2135011 | 1561 | |
2b82032c YZ |
1562 | if (root->fs_info->fs_devices->seeding) { |
1563 | seeding_dev = 1; | |
1564 | down_write(&sb->s_umount); | |
1565 | mutex_lock(&uuid_mutex); | |
1566 | } | |
1567 | ||
8c8bee1d | 1568 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
7d9eb12c | 1569 | mutex_lock(&root->fs_info->volume_mutex); |
a2135011 | 1570 | |
788f20eb | 1571 | devices = &root->fs_info->fs_devices->devices; |
e5e9a520 CM |
1572 | /* |
1573 | * we have the volume lock, so we don't need the extra | |
1574 | * device list mutex while reading the list here. | |
1575 | */ | |
c6e30871 | 1576 | list_for_each_entry(device, devices, dev_list) { |
788f20eb CM |
1577 | if (device->bdev == bdev) { |
1578 | ret = -EEXIST; | |
2b82032c | 1579 | goto error; |
788f20eb CM |
1580 | } |
1581 | } | |
1582 | ||
1583 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
1584 | if (!device) { | |
1585 | /* we can safely leave the fs_devices entry around */ | |
1586 | ret = -ENOMEM; | |
2b82032c | 1587 | goto error; |
788f20eb CM |
1588 | } |
1589 | ||
788f20eb CM |
1590 | device->name = kstrdup(device_path, GFP_NOFS); |
1591 | if (!device->name) { | |
1592 | kfree(device); | |
2b82032c YZ |
1593 | ret = -ENOMEM; |
1594 | goto error; | |
788f20eb | 1595 | } |
2b82032c YZ |
1596 | |
1597 | ret = find_next_devid(root, &device->devid); | |
1598 | if (ret) { | |
67100f25 | 1599 | kfree(device->name); |
2b82032c YZ |
1600 | kfree(device); |
1601 | goto error; | |
1602 | } | |
1603 | ||
a22285a6 | 1604 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 1605 | if (IS_ERR(trans)) { |
67100f25 | 1606 | kfree(device->name); |
98d5dc13 TI |
1607 | kfree(device); |
1608 | ret = PTR_ERR(trans); | |
1609 | goto error; | |
1610 | } | |
1611 | ||
2b82032c YZ |
1612 | lock_chunks(root); |
1613 | ||
2b82032c YZ |
1614 | device->writeable = 1; |
1615 | device->work.func = pending_bios_fn; | |
1616 | generate_random_uuid(device->uuid); | |
1617 | spin_lock_init(&device->io_lock); | |
1618 | device->generation = trans->transid; | |
788f20eb CM |
1619 | device->io_width = root->sectorsize; |
1620 | device->io_align = root->sectorsize; | |
1621 | device->sector_size = root->sectorsize; | |
1622 | device->total_bytes = i_size_read(bdev->bd_inode); | |
2cc3c559 | 1623 | device->disk_total_bytes = device->total_bytes; |
788f20eb CM |
1624 | device->dev_root = root->fs_info->dev_root; |
1625 | device->bdev = bdev; | |
dfe25020 | 1626 | device->in_fs_metadata = 1; |
fb01aa85 | 1627 | device->mode = FMODE_EXCL; |
2b82032c | 1628 | set_blocksize(device->bdev, 4096); |
788f20eb | 1629 | |
2b82032c YZ |
1630 | if (seeding_dev) { |
1631 | sb->s_flags &= ~MS_RDONLY; | |
1632 | ret = btrfs_prepare_sprout(trans, root); | |
1633 | BUG_ON(ret); | |
1634 | } | |
788f20eb | 1635 | |
2b82032c | 1636 | device->fs_devices = root->fs_info->fs_devices; |
e5e9a520 CM |
1637 | |
1638 | /* | |
1639 | * we don't want write_supers to jump in here with our device | |
1640 | * half setup | |
1641 | */ | |
1642 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | |
1f78160c | 1643 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
2b82032c YZ |
1644 | list_add(&device->dev_alloc_list, |
1645 | &root->fs_info->fs_devices->alloc_list); | |
1646 | root->fs_info->fs_devices->num_devices++; | |
1647 | root->fs_info->fs_devices->open_devices++; | |
1648 | root->fs_info->fs_devices->rw_devices++; | |
1649 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; | |
325cd4ba | 1650 | |
c289811c CM |
1651 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
1652 | root->fs_info->fs_devices->rotating = 1; | |
1653 | ||
788f20eb CM |
1654 | total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
1655 | btrfs_set_super_total_bytes(&root->fs_info->super_copy, | |
1656 | total_bytes + device->total_bytes); | |
1657 | ||
1658 | total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); | |
1659 | btrfs_set_super_num_devices(&root->fs_info->super_copy, | |
1660 | total_bytes + 1); | |
e5e9a520 | 1661 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
788f20eb | 1662 | |
2b82032c YZ |
1663 | if (seeding_dev) { |
1664 | ret = init_first_rw_device(trans, root, device); | |
1665 | BUG_ON(ret); | |
1666 | ret = btrfs_finish_sprout(trans, root); | |
1667 | BUG_ON(ret); | |
1668 | } else { | |
1669 | ret = btrfs_add_device(trans, root, device); | |
1670 | } | |
1671 | ||
913d952e CM |
1672 | /* |
1673 | * we've got more storage, clear any full flags on the space | |
1674 | * infos | |
1675 | */ | |
1676 | btrfs_clear_space_info_full(root->fs_info); | |
1677 | ||
7d9eb12c | 1678 | unlock_chunks(root); |
2b82032c | 1679 | btrfs_commit_transaction(trans, root); |
a2135011 | 1680 | |
2b82032c YZ |
1681 | if (seeding_dev) { |
1682 | mutex_unlock(&uuid_mutex); | |
1683 | up_write(&sb->s_umount); | |
788f20eb | 1684 | |
2b82032c YZ |
1685 | ret = btrfs_relocate_sys_chunks(root); |
1686 | BUG_ON(ret); | |
1687 | } | |
1688 | out: | |
1689 | mutex_unlock(&root->fs_info->volume_mutex); | |
1690 | return ret; | |
1691 | error: | |
e525fd89 | 1692 | blkdev_put(bdev, FMODE_EXCL); |
2b82032c YZ |
1693 | if (seeding_dev) { |
1694 | mutex_unlock(&uuid_mutex); | |
1695 | up_write(&sb->s_umount); | |
1696 | } | |
788f20eb CM |
1697 | goto out; |
1698 | } | |
1699 | ||
d397712b CM |
1700 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
1701 | struct btrfs_device *device) | |
0b86a832 CM |
1702 | { |
1703 | int ret; | |
1704 | struct btrfs_path *path; | |
1705 | struct btrfs_root *root; | |
1706 | struct btrfs_dev_item *dev_item; | |
1707 | struct extent_buffer *leaf; | |
1708 | struct btrfs_key key; | |
1709 | ||
1710 | root = device->dev_root->fs_info->chunk_root; | |
1711 | ||
1712 | path = btrfs_alloc_path(); | |
1713 | if (!path) | |
1714 | return -ENOMEM; | |
1715 | ||
1716 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1717 | key.type = BTRFS_DEV_ITEM_KEY; | |
1718 | key.offset = device->devid; | |
1719 | ||
1720 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1721 | if (ret < 0) | |
1722 | goto out; | |
1723 | ||
1724 | if (ret > 0) { | |
1725 | ret = -ENOENT; | |
1726 | goto out; | |
1727 | } | |
1728 | ||
1729 | leaf = path->nodes[0]; | |
1730 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1731 | ||
1732 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
1733 | btrfs_set_device_type(leaf, dev_item, device->type); | |
1734 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1735 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1736 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
d6397bae | 1737 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
0b86a832 CM |
1738 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
1739 | btrfs_mark_buffer_dirty(leaf); | |
1740 | ||
1741 | out: | |
1742 | btrfs_free_path(path); | |
1743 | return ret; | |
1744 | } | |
1745 | ||
7d9eb12c | 1746 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
1747 | struct btrfs_device *device, u64 new_size) |
1748 | { | |
1749 | struct btrfs_super_block *super_copy = | |
1750 | &device->dev_root->fs_info->super_copy; | |
1751 | u64 old_total = btrfs_super_total_bytes(super_copy); | |
1752 | u64 diff = new_size - device->total_bytes; | |
1753 | ||
2b82032c YZ |
1754 | if (!device->writeable) |
1755 | return -EACCES; | |
1756 | if (new_size <= device->total_bytes) | |
1757 | return -EINVAL; | |
1758 | ||
8f18cf13 | 1759 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
2b82032c YZ |
1760 | device->fs_devices->total_rw_bytes += diff; |
1761 | ||
1762 | device->total_bytes = new_size; | |
9779b72f | 1763 | device->disk_total_bytes = new_size; |
4184ea7f CM |
1764 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
1765 | ||
8f18cf13 CM |
1766 | return btrfs_update_device(trans, device); |
1767 | } | |
1768 | ||
7d9eb12c CM |
1769 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
1770 | struct btrfs_device *device, u64 new_size) | |
1771 | { | |
1772 | int ret; | |
1773 | lock_chunks(device->dev_root); | |
1774 | ret = __btrfs_grow_device(trans, device, new_size); | |
1775 | unlock_chunks(device->dev_root); | |
1776 | return ret; | |
1777 | } | |
1778 | ||
8f18cf13 CM |
1779 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
1780 | struct btrfs_root *root, | |
1781 | u64 chunk_tree, u64 chunk_objectid, | |
1782 | u64 chunk_offset) | |
1783 | { | |
1784 | int ret; | |
1785 | struct btrfs_path *path; | |
1786 | struct btrfs_key key; | |
1787 | ||
1788 | root = root->fs_info->chunk_root; | |
1789 | path = btrfs_alloc_path(); | |
1790 | if (!path) | |
1791 | return -ENOMEM; | |
1792 | ||
1793 | key.objectid = chunk_objectid; | |
1794 | key.offset = chunk_offset; | |
1795 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1796 | ||
1797 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1798 | BUG_ON(ret); | |
1799 | ||
1800 | ret = btrfs_del_item(trans, root, path); | |
8f18cf13 CM |
1801 | |
1802 | btrfs_free_path(path); | |
65a246c5 | 1803 | return ret; |
8f18cf13 CM |
1804 | } |
1805 | ||
b2950863 | 1806 | static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 |
8f18cf13 CM |
1807 | chunk_offset) |
1808 | { | |
1809 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
1810 | struct btrfs_disk_key *disk_key; | |
1811 | struct btrfs_chunk *chunk; | |
1812 | u8 *ptr; | |
1813 | int ret = 0; | |
1814 | u32 num_stripes; | |
1815 | u32 array_size; | |
1816 | u32 len = 0; | |
1817 | u32 cur; | |
1818 | struct btrfs_key key; | |
1819 | ||
1820 | array_size = btrfs_super_sys_array_size(super_copy); | |
1821 | ||
1822 | ptr = super_copy->sys_chunk_array; | |
1823 | cur = 0; | |
1824 | ||
1825 | while (cur < array_size) { | |
1826 | disk_key = (struct btrfs_disk_key *)ptr; | |
1827 | btrfs_disk_key_to_cpu(&key, disk_key); | |
1828 | ||
1829 | len = sizeof(*disk_key); | |
1830 | ||
1831 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { | |
1832 | chunk = (struct btrfs_chunk *)(ptr + len); | |
1833 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); | |
1834 | len += btrfs_chunk_item_size(num_stripes); | |
1835 | } else { | |
1836 | ret = -EIO; | |
1837 | break; | |
1838 | } | |
1839 | if (key.objectid == chunk_objectid && | |
1840 | key.offset == chunk_offset) { | |
1841 | memmove(ptr, ptr + len, array_size - (cur + len)); | |
1842 | array_size -= len; | |
1843 | btrfs_set_super_sys_array_size(super_copy, array_size); | |
1844 | } else { | |
1845 | ptr += len; | |
1846 | cur += len; | |
1847 | } | |
1848 | } | |
1849 | return ret; | |
1850 | } | |
1851 | ||
b2950863 | 1852 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
8f18cf13 CM |
1853 | u64 chunk_tree, u64 chunk_objectid, |
1854 | u64 chunk_offset) | |
1855 | { | |
1856 | struct extent_map_tree *em_tree; | |
1857 | struct btrfs_root *extent_root; | |
1858 | struct btrfs_trans_handle *trans; | |
1859 | struct extent_map *em; | |
1860 | struct map_lookup *map; | |
1861 | int ret; | |
1862 | int i; | |
1863 | ||
1864 | root = root->fs_info->chunk_root; | |
1865 | extent_root = root->fs_info->extent_root; | |
1866 | em_tree = &root->fs_info->mapping_tree.map_tree; | |
1867 | ||
ba1bf481 JB |
1868 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
1869 | if (ret) | |
1870 | return -ENOSPC; | |
1871 | ||
8f18cf13 | 1872 | /* step one, relocate all the extents inside this chunk */ |
1a40e23b | 1873 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
a22285a6 YZ |
1874 | if (ret) |
1875 | return ret; | |
8f18cf13 | 1876 | |
a22285a6 | 1877 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 1878 | BUG_ON(IS_ERR(trans)); |
8f18cf13 | 1879 | |
7d9eb12c CM |
1880 | lock_chunks(root); |
1881 | ||
8f18cf13 CM |
1882 | /* |
1883 | * step two, delete the device extents and the | |
1884 | * chunk tree entries | |
1885 | */ | |
890871be | 1886 | read_lock(&em_tree->lock); |
8f18cf13 | 1887 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
890871be | 1888 | read_unlock(&em_tree->lock); |
8f18cf13 | 1889 | |
a061fc8d CM |
1890 | BUG_ON(em->start > chunk_offset || |
1891 | em->start + em->len < chunk_offset); | |
8f18cf13 CM |
1892 | map = (struct map_lookup *)em->bdev; |
1893 | ||
1894 | for (i = 0; i < map->num_stripes; i++) { | |
1895 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, | |
1896 | map->stripes[i].physical); | |
1897 | BUG_ON(ret); | |
a061fc8d | 1898 | |
dfe25020 CM |
1899 | if (map->stripes[i].dev) { |
1900 | ret = btrfs_update_device(trans, map->stripes[i].dev); | |
1901 | BUG_ON(ret); | |
1902 | } | |
8f18cf13 CM |
1903 | } |
1904 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, | |
1905 | chunk_offset); | |
1906 | ||
1907 | BUG_ON(ret); | |
1908 | ||
1abe9b8a | 1909 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
1910 | ||
8f18cf13 CM |
1911 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
1912 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); | |
1913 | BUG_ON(ret); | |
8f18cf13 CM |
1914 | } |
1915 | ||
2b82032c YZ |
1916 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
1917 | BUG_ON(ret); | |
1918 | ||
890871be | 1919 | write_lock(&em_tree->lock); |
2b82032c | 1920 | remove_extent_mapping(em_tree, em); |
890871be | 1921 | write_unlock(&em_tree->lock); |
2b82032c YZ |
1922 | |
1923 | kfree(map); | |
1924 | em->bdev = NULL; | |
1925 | ||
1926 | /* once for the tree */ | |
1927 | free_extent_map(em); | |
1928 | /* once for us */ | |
1929 | free_extent_map(em); | |
1930 | ||
1931 | unlock_chunks(root); | |
1932 | btrfs_end_transaction(trans, root); | |
1933 | return 0; | |
1934 | } | |
1935 | ||
1936 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) | |
1937 | { | |
1938 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; | |
1939 | struct btrfs_path *path; | |
1940 | struct extent_buffer *leaf; | |
1941 | struct btrfs_chunk *chunk; | |
1942 | struct btrfs_key key; | |
1943 | struct btrfs_key found_key; | |
1944 | u64 chunk_tree = chunk_root->root_key.objectid; | |
1945 | u64 chunk_type; | |
ba1bf481 JB |
1946 | bool retried = false; |
1947 | int failed = 0; | |
2b82032c YZ |
1948 | int ret; |
1949 | ||
1950 | path = btrfs_alloc_path(); | |
1951 | if (!path) | |
1952 | return -ENOMEM; | |
1953 | ||
ba1bf481 | 1954 | again: |
2b82032c YZ |
1955 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
1956 | key.offset = (u64)-1; | |
1957 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1958 | ||
1959 | while (1) { | |
1960 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); | |
1961 | if (ret < 0) | |
1962 | goto error; | |
1963 | BUG_ON(ret == 0); | |
1964 | ||
1965 | ret = btrfs_previous_item(chunk_root, path, key.objectid, | |
1966 | key.type); | |
1967 | if (ret < 0) | |
1968 | goto error; | |
1969 | if (ret > 0) | |
1970 | break; | |
1a40e23b | 1971 | |
2b82032c YZ |
1972 | leaf = path->nodes[0]; |
1973 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b | 1974 | |
2b82032c YZ |
1975 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
1976 | struct btrfs_chunk); | |
1977 | chunk_type = btrfs_chunk_type(leaf, chunk); | |
b3b4aa74 | 1978 | btrfs_release_path(path); |
8f18cf13 | 1979 | |
2b82032c YZ |
1980 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
1981 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, | |
1982 | found_key.objectid, | |
1983 | found_key.offset); | |
ba1bf481 JB |
1984 | if (ret == -ENOSPC) |
1985 | failed++; | |
1986 | else if (ret) | |
1987 | BUG(); | |
2b82032c | 1988 | } |
8f18cf13 | 1989 | |
2b82032c YZ |
1990 | if (found_key.offset == 0) |
1991 | break; | |
1992 | key.offset = found_key.offset - 1; | |
1993 | } | |
1994 | ret = 0; | |
ba1bf481 JB |
1995 | if (failed && !retried) { |
1996 | failed = 0; | |
1997 | retried = true; | |
1998 | goto again; | |
1999 | } else if (failed && retried) { | |
2000 | WARN_ON(1); | |
2001 | ret = -ENOSPC; | |
2002 | } | |
2b82032c YZ |
2003 | error: |
2004 | btrfs_free_path(path); | |
2005 | return ret; | |
8f18cf13 CM |
2006 | } |
2007 | ||
ec44a35c CM |
2008 | static u64 div_factor(u64 num, int factor) |
2009 | { | |
2010 | if (factor == 10) | |
2011 | return num; | |
2012 | num *= factor; | |
2013 | do_div(num, 10); | |
2014 | return num; | |
2015 | } | |
2016 | ||
ec44a35c CM |
2017 | int btrfs_balance(struct btrfs_root *dev_root) |
2018 | { | |
2019 | int ret; | |
ec44a35c CM |
2020 | struct list_head *devices = &dev_root->fs_info->fs_devices->devices; |
2021 | struct btrfs_device *device; | |
2022 | u64 old_size; | |
2023 | u64 size_to_free; | |
2024 | struct btrfs_path *path; | |
2025 | struct btrfs_key key; | |
ec44a35c CM |
2026 | struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root; |
2027 | struct btrfs_trans_handle *trans; | |
2028 | struct btrfs_key found_key; | |
2029 | ||
2b82032c YZ |
2030 | if (dev_root->fs_info->sb->s_flags & MS_RDONLY) |
2031 | return -EROFS; | |
ec44a35c | 2032 | |
6f88a440 BH |
2033 | if (!capable(CAP_SYS_ADMIN)) |
2034 | return -EPERM; | |
2035 | ||
7d9eb12c | 2036 | mutex_lock(&dev_root->fs_info->volume_mutex); |
ec44a35c CM |
2037 | dev_root = dev_root->fs_info->dev_root; |
2038 | ||
ec44a35c | 2039 | /* step one make some room on all the devices */ |
c6e30871 | 2040 | list_for_each_entry(device, devices, dev_list) { |
ec44a35c CM |
2041 | old_size = device->total_bytes; |
2042 | size_to_free = div_factor(old_size, 1); | |
2043 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); | |
2b82032c YZ |
2044 | if (!device->writeable || |
2045 | device->total_bytes - device->bytes_used > size_to_free) | |
ec44a35c CM |
2046 | continue; |
2047 | ||
2048 | ret = btrfs_shrink_device(device, old_size - size_to_free); | |
ba1bf481 JB |
2049 | if (ret == -ENOSPC) |
2050 | break; | |
ec44a35c CM |
2051 | BUG_ON(ret); |
2052 | ||
a22285a6 | 2053 | trans = btrfs_start_transaction(dev_root, 0); |
98d5dc13 | 2054 | BUG_ON(IS_ERR(trans)); |
ec44a35c CM |
2055 | |
2056 | ret = btrfs_grow_device(trans, device, old_size); | |
2057 | BUG_ON(ret); | |
2058 | ||
2059 | btrfs_end_transaction(trans, dev_root); | |
2060 | } | |
2061 | ||
2062 | /* step two, relocate all the chunks */ | |
2063 | path = btrfs_alloc_path(); | |
2064 | BUG_ON(!path); | |
2065 | ||
2066 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; | |
2067 | key.offset = (u64)-1; | |
2068 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2069 | ||
d397712b | 2070 | while (1) { |
ec44a35c CM |
2071 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
2072 | if (ret < 0) | |
2073 | goto error; | |
2074 | ||
2075 | /* | |
2076 | * this shouldn't happen, it means the last relocate | |
2077 | * failed | |
2078 | */ | |
2079 | if (ret == 0) | |
2080 | break; | |
2081 | ||
2082 | ret = btrfs_previous_item(chunk_root, path, 0, | |
2083 | BTRFS_CHUNK_ITEM_KEY); | |
7d9eb12c | 2084 | if (ret) |
ec44a35c | 2085 | break; |
7d9eb12c | 2086 | |
ec44a35c CM |
2087 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
2088 | path->slots[0]); | |
2089 | if (found_key.objectid != key.objectid) | |
2090 | break; | |
7d9eb12c | 2091 | |
ec44a35c | 2092 | /* chunk zero is special */ |
ba1bf481 | 2093 | if (found_key.offset == 0) |
ec44a35c CM |
2094 | break; |
2095 | ||
b3b4aa74 | 2096 | btrfs_release_path(path); |
ec44a35c CM |
2097 | ret = btrfs_relocate_chunk(chunk_root, |
2098 | chunk_root->root_key.objectid, | |
2099 | found_key.objectid, | |
2100 | found_key.offset); | |
ba1bf481 JB |
2101 | BUG_ON(ret && ret != -ENOSPC); |
2102 | key.offset = found_key.offset - 1; | |
ec44a35c CM |
2103 | } |
2104 | ret = 0; | |
2105 | error: | |
2106 | btrfs_free_path(path); | |
7d9eb12c | 2107 | mutex_unlock(&dev_root->fs_info->volume_mutex); |
ec44a35c CM |
2108 | return ret; |
2109 | } | |
2110 | ||
8f18cf13 CM |
2111 | /* |
2112 | * shrinking a device means finding all of the device extents past | |
2113 | * the new size, and then following the back refs to the chunks. | |
2114 | * The chunk relocation code actually frees the device extent | |
2115 | */ | |
2116 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) | |
2117 | { | |
2118 | struct btrfs_trans_handle *trans; | |
2119 | struct btrfs_root *root = device->dev_root; | |
2120 | struct btrfs_dev_extent *dev_extent = NULL; | |
2121 | struct btrfs_path *path; | |
2122 | u64 length; | |
2123 | u64 chunk_tree; | |
2124 | u64 chunk_objectid; | |
2125 | u64 chunk_offset; | |
2126 | int ret; | |
2127 | int slot; | |
ba1bf481 JB |
2128 | int failed = 0; |
2129 | bool retried = false; | |
8f18cf13 CM |
2130 | struct extent_buffer *l; |
2131 | struct btrfs_key key; | |
2132 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
2133 | u64 old_total = btrfs_super_total_bytes(super_copy); | |
ba1bf481 | 2134 | u64 old_size = device->total_bytes; |
8f18cf13 CM |
2135 | u64 diff = device->total_bytes - new_size; |
2136 | ||
2b82032c YZ |
2137 | if (new_size >= device->total_bytes) |
2138 | return -EINVAL; | |
8f18cf13 CM |
2139 | |
2140 | path = btrfs_alloc_path(); | |
2141 | if (!path) | |
2142 | return -ENOMEM; | |
2143 | ||
8f18cf13 CM |
2144 | path->reada = 2; |
2145 | ||
7d9eb12c CM |
2146 | lock_chunks(root); |
2147 | ||
8f18cf13 | 2148 | device->total_bytes = new_size; |
2b82032c YZ |
2149 | if (device->writeable) |
2150 | device->fs_devices->total_rw_bytes -= diff; | |
7d9eb12c | 2151 | unlock_chunks(root); |
8f18cf13 | 2152 | |
ba1bf481 | 2153 | again: |
8f18cf13 CM |
2154 | key.objectid = device->devid; |
2155 | key.offset = (u64)-1; | |
2156 | key.type = BTRFS_DEV_EXTENT_KEY; | |
2157 | ||
2158 | while (1) { | |
2159 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2160 | if (ret < 0) | |
2161 | goto done; | |
2162 | ||
2163 | ret = btrfs_previous_item(root, path, 0, key.type); | |
2164 | if (ret < 0) | |
2165 | goto done; | |
2166 | if (ret) { | |
2167 | ret = 0; | |
b3b4aa74 | 2168 | btrfs_release_path(path); |
bf1fb512 | 2169 | break; |
8f18cf13 CM |
2170 | } |
2171 | ||
2172 | l = path->nodes[0]; | |
2173 | slot = path->slots[0]; | |
2174 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
2175 | ||
ba1bf481 | 2176 | if (key.objectid != device->devid) { |
b3b4aa74 | 2177 | btrfs_release_path(path); |
bf1fb512 | 2178 | break; |
ba1bf481 | 2179 | } |
8f18cf13 CM |
2180 | |
2181 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
2182 | length = btrfs_dev_extent_length(l, dev_extent); | |
2183 | ||
ba1bf481 | 2184 | if (key.offset + length <= new_size) { |
b3b4aa74 | 2185 | btrfs_release_path(path); |
d6397bae | 2186 | break; |
ba1bf481 | 2187 | } |
8f18cf13 CM |
2188 | |
2189 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); | |
2190 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); | |
2191 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); | |
b3b4aa74 | 2192 | btrfs_release_path(path); |
8f18cf13 CM |
2193 | |
2194 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, | |
2195 | chunk_offset); | |
ba1bf481 | 2196 | if (ret && ret != -ENOSPC) |
8f18cf13 | 2197 | goto done; |
ba1bf481 JB |
2198 | if (ret == -ENOSPC) |
2199 | failed++; | |
2200 | key.offset -= 1; | |
2201 | } | |
2202 | ||
2203 | if (failed && !retried) { | |
2204 | failed = 0; | |
2205 | retried = true; | |
2206 | goto again; | |
2207 | } else if (failed && retried) { | |
2208 | ret = -ENOSPC; | |
2209 | lock_chunks(root); | |
2210 | ||
2211 | device->total_bytes = old_size; | |
2212 | if (device->writeable) | |
2213 | device->fs_devices->total_rw_bytes += diff; | |
2214 | unlock_chunks(root); | |
2215 | goto done; | |
8f18cf13 CM |
2216 | } |
2217 | ||
d6397bae | 2218 | /* Shrinking succeeded, else we would be at "done". */ |
a22285a6 | 2219 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
2220 | if (IS_ERR(trans)) { |
2221 | ret = PTR_ERR(trans); | |
2222 | goto done; | |
2223 | } | |
2224 | ||
d6397bae CB |
2225 | lock_chunks(root); |
2226 | ||
2227 | device->disk_total_bytes = new_size; | |
2228 | /* Now btrfs_update_device() will change the on-disk size. */ | |
2229 | ret = btrfs_update_device(trans, device); | |
2230 | if (ret) { | |
2231 | unlock_chunks(root); | |
2232 | btrfs_end_transaction(trans, root); | |
2233 | goto done; | |
2234 | } | |
2235 | WARN_ON(diff > old_total); | |
2236 | btrfs_set_super_total_bytes(super_copy, old_total - diff); | |
2237 | unlock_chunks(root); | |
2238 | btrfs_end_transaction(trans, root); | |
8f18cf13 CM |
2239 | done: |
2240 | btrfs_free_path(path); | |
2241 | return ret; | |
2242 | } | |
2243 | ||
b2950863 | 2244 | static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, |
0b86a832 CM |
2245 | struct btrfs_root *root, |
2246 | struct btrfs_key *key, | |
2247 | struct btrfs_chunk *chunk, int item_size) | |
2248 | { | |
2249 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
2250 | struct btrfs_disk_key disk_key; | |
2251 | u32 array_size; | |
2252 | u8 *ptr; | |
2253 | ||
2254 | array_size = btrfs_super_sys_array_size(super_copy); | |
2255 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) | |
2256 | return -EFBIG; | |
2257 | ||
2258 | ptr = super_copy->sys_chunk_array + array_size; | |
2259 | btrfs_cpu_key_to_disk(&disk_key, key); | |
2260 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
2261 | ptr += sizeof(disk_key); | |
2262 | memcpy(ptr, chunk, item_size); | |
2263 | item_size += sizeof(disk_key); | |
2264 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
2265 | return 0; | |
2266 | } | |
2267 | ||
73c5de00 AJ |
2268 | /* |
2269 | * sort the devices in descending order by max_avail, total_avail | |
2270 | */ | |
2271 | static int btrfs_cmp_device_info(const void *a, const void *b) | |
9b3f68b9 | 2272 | { |
73c5de00 AJ |
2273 | const struct btrfs_device_info *di_a = a; |
2274 | const struct btrfs_device_info *di_b = b; | |
9b3f68b9 | 2275 | |
73c5de00 | 2276 | if (di_a->max_avail > di_b->max_avail) |
b2117a39 | 2277 | return -1; |
73c5de00 | 2278 | if (di_a->max_avail < di_b->max_avail) |
b2117a39 | 2279 | return 1; |
73c5de00 AJ |
2280 | if (di_a->total_avail > di_b->total_avail) |
2281 | return -1; | |
2282 | if (di_a->total_avail < di_b->total_avail) | |
2283 | return 1; | |
2284 | return 0; | |
b2117a39 | 2285 | } |
0b86a832 | 2286 | |
73c5de00 AJ |
2287 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
2288 | struct btrfs_root *extent_root, | |
2289 | struct map_lookup **map_ret, | |
2290 | u64 *num_bytes_out, u64 *stripe_size_out, | |
2291 | u64 start, u64 type) | |
b2117a39 | 2292 | { |
73c5de00 AJ |
2293 | struct btrfs_fs_info *info = extent_root->fs_info; |
2294 | struct btrfs_fs_devices *fs_devices = info->fs_devices; | |
2295 | struct list_head *cur; | |
2296 | struct map_lookup *map = NULL; | |
2297 | struct extent_map_tree *em_tree; | |
2298 | struct extent_map *em; | |
2299 | struct btrfs_device_info *devices_info = NULL; | |
2300 | u64 total_avail; | |
2301 | int num_stripes; /* total number of stripes to allocate */ | |
2302 | int sub_stripes; /* sub_stripes info for map */ | |
2303 | int dev_stripes; /* stripes per dev */ | |
2304 | int devs_max; /* max devs to use */ | |
2305 | int devs_min; /* min devs needed */ | |
2306 | int devs_increment; /* ndevs has to be a multiple of this */ | |
2307 | int ncopies; /* how many copies to data has */ | |
2308 | int ret; | |
2309 | u64 max_stripe_size; | |
2310 | u64 max_chunk_size; | |
2311 | u64 stripe_size; | |
2312 | u64 num_bytes; | |
2313 | int ndevs; | |
2314 | int i; | |
2315 | int j; | |
593060d7 | 2316 | |
73c5de00 AJ |
2317 | if ((type & BTRFS_BLOCK_GROUP_RAID1) && |
2318 | (type & BTRFS_BLOCK_GROUP_DUP)) { | |
2319 | WARN_ON(1); | |
2320 | type &= ~BTRFS_BLOCK_GROUP_DUP; | |
321aecc6 | 2321 | } |
9b3f68b9 | 2322 | |
73c5de00 AJ |
2323 | if (list_empty(&fs_devices->alloc_list)) |
2324 | return -ENOSPC; | |
b2117a39 | 2325 | |
73c5de00 AJ |
2326 | sub_stripes = 1; |
2327 | dev_stripes = 1; | |
2328 | devs_increment = 1; | |
2329 | ncopies = 1; | |
2330 | devs_max = 0; /* 0 == as many as possible */ | |
2331 | devs_min = 1; | |
2332 | ||
2333 | /* | |
2334 | * define the properties of each RAID type. | |
2335 | * FIXME: move this to a global table and use it in all RAID | |
2336 | * calculation code | |
2337 | */ | |
2338 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { | |
2339 | dev_stripes = 2; | |
b2117a39 | 2340 | ncopies = 2; |
73c5de00 AJ |
2341 | devs_max = 1; |
2342 | } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) { | |
2343 | devs_min = 2; | |
2344 | } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) { | |
2345 | devs_increment = 2; | |
b2117a39 | 2346 | ncopies = 2; |
73c5de00 AJ |
2347 | devs_max = 2; |
2348 | devs_min = 2; | |
2349 | } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) { | |
2350 | sub_stripes = 2; | |
2351 | devs_increment = 2; | |
2352 | ncopies = 2; | |
2353 | devs_min = 4; | |
2354 | } else { | |
2355 | devs_max = 1; | |
2356 | } | |
b2117a39 | 2357 | |
9b3f68b9 | 2358 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
73c5de00 AJ |
2359 | max_stripe_size = 1024 * 1024 * 1024; |
2360 | max_chunk_size = 10 * max_stripe_size; | |
9b3f68b9 | 2361 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
73c5de00 AJ |
2362 | max_stripe_size = 256 * 1024 * 1024; |
2363 | max_chunk_size = max_stripe_size; | |
a40a90a0 | 2364 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
73c5de00 AJ |
2365 | max_stripe_size = 8 * 1024 * 1024; |
2366 | max_chunk_size = 2 * max_stripe_size; | |
2367 | } else { | |
2368 | printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n", | |
2369 | type); | |
2370 | BUG_ON(1); | |
9b3f68b9 CM |
2371 | } |
2372 | ||
2b82032c YZ |
2373 | /* we don't want a chunk larger than 10% of writeable space */ |
2374 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), | |
2375 | max_chunk_size); | |
9b3f68b9 | 2376 | |
73c5de00 AJ |
2377 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
2378 | GFP_NOFS); | |
2379 | if (!devices_info) | |
2380 | return -ENOMEM; | |
0cad8a11 | 2381 | |
73c5de00 | 2382 | cur = fs_devices->alloc_list.next; |
9b3f68b9 | 2383 | |
9f680ce0 | 2384 | /* |
73c5de00 AJ |
2385 | * in the first pass through the devices list, we gather information |
2386 | * about the available holes on each device. | |
9f680ce0 | 2387 | */ |
73c5de00 AJ |
2388 | ndevs = 0; |
2389 | while (cur != &fs_devices->alloc_list) { | |
2390 | struct btrfs_device *device; | |
2391 | u64 max_avail; | |
2392 | u64 dev_offset; | |
b2117a39 | 2393 | |
73c5de00 | 2394 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
9f680ce0 | 2395 | |
73c5de00 | 2396 | cur = cur->next; |
b2117a39 | 2397 | |
73c5de00 AJ |
2398 | if (!device->writeable) { |
2399 | printk(KERN_ERR | |
2400 | "btrfs: read-only device in alloc_list\n"); | |
2401 | WARN_ON(1); | |
2402 | continue; | |
2403 | } | |
b2117a39 | 2404 | |
73c5de00 AJ |
2405 | if (!device->in_fs_metadata) |
2406 | continue; | |
b2117a39 | 2407 | |
73c5de00 AJ |
2408 | if (device->total_bytes > device->bytes_used) |
2409 | total_avail = device->total_bytes - device->bytes_used; | |
2410 | else | |
2411 | total_avail = 0; | |
2412 | /* avail is off by max(alloc_start, 1MB), but that is the same | |
2413 | * for all devices, so it doesn't hurt the sorting later on | |
b2117a39 | 2414 | */ |
b2117a39 | 2415 | |
73c5de00 AJ |
2416 | ret = find_free_dev_extent(trans, device, |
2417 | max_stripe_size * dev_stripes, | |
2418 | &dev_offset, &max_avail); | |
2419 | if (ret && ret != -ENOSPC) | |
2420 | goto error; | |
b2117a39 | 2421 | |
73c5de00 AJ |
2422 | if (ret == 0) |
2423 | max_avail = max_stripe_size * dev_stripes; | |
b2117a39 | 2424 | |
73c5de00 AJ |
2425 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
2426 | continue; | |
b2117a39 | 2427 | |
73c5de00 AJ |
2428 | devices_info[ndevs].dev_offset = dev_offset; |
2429 | devices_info[ndevs].max_avail = max_avail; | |
2430 | devices_info[ndevs].total_avail = total_avail; | |
2431 | devices_info[ndevs].dev = device; | |
2432 | ++ndevs; | |
2433 | } | |
b2117a39 | 2434 | |
73c5de00 AJ |
2435 | /* |
2436 | * now sort the devices by hole size / available space | |
2437 | */ | |
2438 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), | |
2439 | btrfs_cmp_device_info, NULL); | |
b2117a39 | 2440 | |
73c5de00 AJ |
2441 | /* round down to number of usable stripes */ |
2442 | ndevs -= ndevs % devs_increment; | |
b2117a39 | 2443 | |
73c5de00 AJ |
2444 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
2445 | ret = -ENOSPC; | |
2446 | goto error; | |
b2117a39 | 2447 | } |
9f680ce0 | 2448 | |
73c5de00 AJ |
2449 | if (devs_max && ndevs > devs_max) |
2450 | ndevs = devs_max; | |
2451 | /* | |
2452 | * the primary goal is to maximize the number of stripes, so use as many | |
2453 | * devices as possible, even if the stripes are not maximum sized. | |
2454 | */ | |
2455 | stripe_size = devices_info[ndevs-1].max_avail; | |
2456 | num_stripes = ndevs * dev_stripes; | |
b2117a39 | 2457 | |
73c5de00 AJ |
2458 | if (stripe_size * num_stripes > max_chunk_size * ncopies) { |
2459 | stripe_size = max_chunk_size * ncopies; | |
2460 | do_div(stripe_size, num_stripes); | |
b2117a39 | 2461 | } |
b2117a39 | 2462 | |
73c5de00 AJ |
2463 | do_div(stripe_size, dev_stripes); |
2464 | do_div(stripe_size, BTRFS_STRIPE_LEN); | |
2465 | stripe_size *= BTRFS_STRIPE_LEN; | |
b2117a39 MX |
2466 | |
2467 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
2468 | if (!map) { | |
2469 | ret = -ENOMEM; | |
2470 | goto error; | |
2471 | } | |
2472 | map->num_stripes = num_stripes; | |
9b3f68b9 | 2473 | |
73c5de00 AJ |
2474 | for (i = 0; i < ndevs; ++i) { |
2475 | for (j = 0; j < dev_stripes; ++j) { | |
2476 | int s = i * dev_stripes + j; | |
2477 | map->stripes[s].dev = devices_info[i].dev; | |
2478 | map->stripes[s].physical = devices_info[i].dev_offset + | |
2479 | j * stripe_size; | |
6324fbf3 | 2480 | } |
6324fbf3 | 2481 | } |
2b82032c | 2482 | map->sector_size = extent_root->sectorsize; |
b2117a39 MX |
2483 | map->stripe_len = BTRFS_STRIPE_LEN; |
2484 | map->io_align = BTRFS_STRIPE_LEN; | |
2485 | map->io_width = BTRFS_STRIPE_LEN; | |
2b82032c | 2486 | map->type = type; |
2b82032c | 2487 | map->sub_stripes = sub_stripes; |
0b86a832 | 2488 | |
2b82032c | 2489 | *map_ret = map; |
73c5de00 | 2490 | num_bytes = stripe_size * (num_stripes / ncopies); |
0b86a832 | 2491 | |
73c5de00 AJ |
2492 | *stripe_size_out = stripe_size; |
2493 | *num_bytes_out = num_bytes; | |
0b86a832 | 2494 | |
73c5de00 | 2495 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
1abe9b8a | 2496 | |
172ddd60 | 2497 | em = alloc_extent_map(); |
2b82032c | 2498 | if (!em) { |
b2117a39 MX |
2499 | ret = -ENOMEM; |
2500 | goto error; | |
593060d7 | 2501 | } |
2b82032c YZ |
2502 | em->bdev = (struct block_device *)map; |
2503 | em->start = start; | |
73c5de00 | 2504 | em->len = num_bytes; |
2b82032c YZ |
2505 | em->block_start = 0; |
2506 | em->block_len = em->len; | |
593060d7 | 2507 | |
2b82032c | 2508 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
890871be | 2509 | write_lock(&em_tree->lock); |
2b82032c | 2510 | ret = add_extent_mapping(em_tree, em); |
890871be | 2511 | write_unlock(&em_tree->lock); |
2b82032c YZ |
2512 | BUG_ON(ret); |
2513 | free_extent_map(em); | |
0b86a832 | 2514 | |
2b82032c YZ |
2515 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
2516 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
73c5de00 | 2517 | start, num_bytes); |
2b82032c | 2518 | BUG_ON(ret); |
611f0e00 | 2519 | |
73c5de00 AJ |
2520 | for (i = 0; i < map->num_stripes; ++i) { |
2521 | struct btrfs_device *device; | |
2522 | u64 dev_offset; | |
2523 | ||
2524 | device = map->stripes[i].dev; | |
2525 | dev_offset = map->stripes[i].physical; | |
0b86a832 CM |
2526 | |
2527 | ret = btrfs_alloc_dev_extent(trans, device, | |
2b82032c YZ |
2528 | info->chunk_root->root_key.objectid, |
2529 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
73c5de00 | 2530 | start, dev_offset, stripe_size); |
0b86a832 | 2531 | BUG_ON(ret); |
2b82032c YZ |
2532 | } |
2533 | ||
b2117a39 | 2534 | kfree(devices_info); |
2b82032c | 2535 | return 0; |
b2117a39 MX |
2536 | |
2537 | error: | |
2538 | kfree(map); | |
2539 | kfree(devices_info); | |
2540 | return ret; | |
2b82032c YZ |
2541 | } |
2542 | ||
2543 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, | |
2544 | struct btrfs_root *extent_root, | |
2545 | struct map_lookup *map, u64 chunk_offset, | |
2546 | u64 chunk_size, u64 stripe_size) | |
2547 | { | |
2548 | u64 dev_offset; | |
2549 | struct btrfs_key key; | |
2550 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | |
2551 | struct btrfs_device *device; | |
2552 | struct btrfs_chunk *chunk; | |
2553 | struct btrfs_stripe *stripe; | |
2554 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); | |
2555 | int index = 0; | |
2556 | int ret; | |
2557 | ||
2558 | chunk = kzalloc(item_size, GFP_NOFS); | |
2559 | if (!chunk) | |
2560 | return -ENOMEM; | |
2561 | ||
2562 | index = 0; | |
2563 | while (index < map->num_stripes) { | |
2564 | device = map->stripes[index].dev; | |
2565 | device->bytes_used += stripe_size; | |
0b86a832 CM |
2566 | ret = btrfs_update_device(trans, device); |
2567 | BUG_ON(ret); | |
2b82032c YZ |
2568 | index++; |
2569 | } | |
2570 | ||
2571 | index = 0; | |
2572 | stripe = &chunk->stripe; | |
2573 | while (index < map->num_stripes) { | |
2574 | device = map->stripes[index].dev; | |
2575 | dev_offset = map->stripes[index].physical; | |
0b86a832 | 2576 | |
e17cade2 CM |
2577 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
2578 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
2579 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
2b82032c | 2580 | stripe++; |
0b86a832 CM |
2581 | index++; |
2582 | } | |
2583 | ||
2b82032c | 2584 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
0b86a832 | 2585 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
2b82032c YZ |
2586 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
2587 | btrfs_set_stack_chunk_type(chunk, map->type); | |
2588 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); | |
2589 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); | |
2590 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); | |
0b86a832 | 2591 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
2b82032c | 2592 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
0b86a832 | 2593 | |
2b82032c YZ |
2594 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
2595 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2596 | key.offset = chunk_offset; | |
0b86a832 | 2597 | |
2b82032c YZ |
2598 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
2599 | BUG_ON(ret); | |
0b86a832 | 2600 | |
2b82032c YZ |
2601 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
2602 | ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk, | |
2603 | item_size); | |
8f18cf13 CM |
2604 | BUG_ON(ret); |
2605 | } | |
1abe9b8a | 2606 | |
0b86a832 | 2607 | kfree(chunk); |
2b82032c YZ |
2608 | return 0; |
2609 | } | |
0b86a832 | 2610 | |
2b82032c YZ |
2611 | /* |
2612 | * Chunk allocation falls into two parts. The first part does works | |
2613 | * that make the new allocated chunk useable, but not do any operation | |
2614 | * that modifies the chunk tree. The second part does the works that | |
2615 | * require modifying the chunk tree. This division is important for the | |
2616 | * bootstrap process of adding storage to a seed btrfs. | |
2617 | */ | |
2618 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
2619 | struct btrfs_root *extent_root, u64 type) | |
2620 | { | |
2621 | u64 chunk_offset; | |
2622 | u64 chunk_size; | |
2623 | u64 stripe_size; | |
2624 | struct map_lookup *map; | |
2625 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | |
2626 | int ret; | |
2627 | ||
2628 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2629 | &chunk_offset); | |
2630 | if (ret) | |
2631 | return ret; | |
2632 | ||
2633 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, | |
2634 | &stripe_size, chunk_offset, type); | |
2635 | if (ret) | |
2636 | return ret; | |
2637 | ||
2638 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, | |
2639 | chunk_size, stripe_size); | |
2640 | BUG_ON(ret); | |
2641 | return 0; | |
2642 | } | |
2643 | ||
d397712b | 2644 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
2b82032c YZ |
2645 | struct btrfs_root *root, |
2646 | struct btrfs_device *device) | |
2647 | { | |
2648 | u64 chunk_offset; | |
2649 | u64 sys_chunk_offset; | |
2650 | u64 chunk_size; | |
2651 | u64 sys_chunk_size; | |
2652 | u64 stripe_size; | |
2653 | u64 sys_stripe_size; | |
2654 | u64 alloc_profile; | |
2655 | struct map_lookup *map; | |
2656 | struct map_lookup *sys_map; | |
2657 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2658 | struct btrfs_root *extent_root = fs_info->extent_root; | |
2659 | int ret; | |
2660 | ||
2661 | ret = find_next_chunk(fs_info->chunk_root, | |
2662 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); | |
2663 | BUG_ON(ret); | |
2664 | ||
2665 | alloc_profile = BTRFS_BLOCK_GROUP_METADATA | | |
2666 | (fs_info->metadata_alloc_profile & | |
2667 | fs_info->avail_metadata_alloc_bits); | |
2668 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); | |
2669 | ||
2670 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, | |
2671 | &stripe_size, chunk_offset, alloc_profile); | |
2672 | BUG_ON(ret); | |
2673 | ||
2674 | sys_chunk_offset = chunk_offset + chunk_size; | |
2675 | ||
2676 | alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM | | |
2677 | (fs_info->system_alloc_profile & | |
2678 | fs_info->avail_system_alloc_bits); | |
2679 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); | |
2680 | ||
2681 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, | |
2682 | &sys_chunk_size, &sys_stripe_size, | |
2683 | sys_chunk_offset, alloc_profile); | |
2684 | BUG_ON(ret); | |
2685 | ||
2686 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); | |
2687 | BUG_ON(ret); | |
2688 | ||
2689 | /* | |
2690 | * Modifying chunk tree needs allocating new blocks from both | |
2691 | * system block group and metadata block group. So we only can | |
2692 | * do operations require modifying the chunk tree after both | |
2693 | * block groups were created. | |
2694 | */ | |
2695 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, | |
2696 | chunk_size, stripe_size); | |
2697 | BUG_ON(ret); | |
2698 | ||
2699 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, | |
2700 | sys_chunk_offset, sys_chunk_size, | |
2701 | sys_stripe_size); | |
b248a415 | 2702 | BUG_ON(ret); |
2b82032c YZ |
2703 | return 0; |
2704 | } | |
2705 | ||
2706 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) | |
2707 | { | |
2708 | struct extent_map *em; | |
2709 | struct map_lookup *map; | |
2710 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
2711 | int readonly = 0; | |
2712 | int i; | |
2713 | ||
890871be | 2714 | read_lock(&map_tree->map_tree.lock); |
2b82032c | 2715 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
890871be | 2716 | read_unlock(&map_tree->map_tree.lock); |
2b82032c YZ |
2717 | if (!em) |
2718 | return 1; | |
2719 | ||
f48b9075 JB |
2720 | if (btrfs_test_opt(root, DEGRADED)) { |
2721 | free_extent_map(em); | |
2722 | return 0; | |
2723 | } | |
2724 | ||
2b82032c YZ |
2725 | map = (struct map_lookup *)em->bdev; |
2726 | for (i = 0; i < map->num_stripes; i++) { | |
2727 | if (!map->stripes[i].dev->writeable) { | |
2728 | readonly = 1; | |
2729 | break; | |
2730 | } | |
2731 | } | |
0b86a832 | 2732 | free_extent_map(em); |
2b82032c | 2733 | return readonly; |
0b86a832 CM |
2734 | } |
2735 | ||
2736 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | |
2737 | { | |
a8067e02 | 2738 | extent_map_tree_init(&tree->map_tree); |
0b86a832 CM |
2739 | } |
2740 | ||
2741 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | |
2742 | { | |
2743 | struct extent_map *em; | |
2744 | ||
d397712b | 2745 | while (1) { |
890871be | 2746 | write_lock(&tree->map_tree.lock); |
0b86a832 CM |
2747 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
2748 | if (em) | |
2749 | remove_extent_mapping(&tree->map_tree, em); | |
890871be | 2750 | write_unlock(&tree->map_tree.lock); |
0b86a832 CM |
2751 | if (!em) |
2752 | break; | |
2753 | kfree(em->bdev); | |
2754 | /* once for us */ | |
2755 | free_extent_map(em); | |
2756 | /* once for the tree */ | |
2757 | free_extent_map(em); | |
2758 | } | |
2759 | } | |
2760 | ||
f188591e CM |
2761 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
2762 | { | |
2763 | struct extent_map *em; | |
2764 | struct map_lookup *map; | |
2765 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2766 | int ret; | |
2767 | ||
890871be | 2768 | read_lock(&em_tree->lock); |
f188591e | 2769 | em = lookup_extent_mapping(em_tree, logical, len); |
890871be | 2770 | read_unlock(&em_tree->lock); |
f188591e CM |
2771 | BUG_ON(!em); |
2772 | ||
2773 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
2774 | map = (struct map_lookup *)em->bdev; | |
2775 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) | |
2776 | ret = map->num_stripes; | |
321aecc6 CM |
2777 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
2778 | ret = map->sub_stripes; | |
f188591e CM |
2779 | else |
2780 | ret = 1; | |
2781 | free_extent_map(em); | |
f188591e CM |
2782 | return ret; |
2783 | } | |
2784 | ||
dfe25020 CM |
2785 | static int find_live_mirror(struct map_lookup *map, int first, int num, |
2786 | int optimal) | |
2787 | { | |
2788 | int i; | |
2789 | if (map->stripes[optimal].dev->bdev) | |
2790 | return optimal; | |
2791 | for (i = first; i < first + num; i++) { | |
2792 | if (map->stripes[i].dev->bdev) | |
2793 | return i; | |
2794 | } | |
2795 | /* we couldn't find one that doesn't fail. Just return something | |
2796 | * and the io error handling code will clean up eventually | |
2797 | */ | |
2798 | return optimal; | |
2799 | } | |
2800 | ||
f2d8d74d CM |
2801 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
2802 | u64 logical, u64 *length, | |
2803 | struct btrfs_multi_bio **multi_ret, | |
7eaceacc | 2804 | int mirror_num) |
0b86a832 CM |
2805 | { |
2806 | struct extent_map *em; | |
2807 | struct map_lookup *map; | |
2808 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2809 | u64 offset; | |
593060d7 | 2810 | u64 stripe_offset; |
fce3bb9a | 2811 | u64 stripe_end_offset; |
593060d7 | 2812 | u64 stripe_nr; |
fce3bb9a LD |
2813 | u64 stripe_nr_orig; |
2814 | u64 stripe_nr_end; | |
cea9e445 | 2815 | int stripes_allocated = 8; |
321aecc6 | 2816 | int stripes_required = 1; |
593060d7 | 2817 | int stripe_index; |
cea9e445 | 2818 | int i; |
f2d8d74d | 2819 | int num_stripes; |
a236aed1 | 2820 | int max_errors = 0; |
cea9e445 | 2821 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 | 2822 | |
fce3bb9a | 2823 | if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD))) |
cea9e445 | 2824 | stripes_allocated = 1; |
cea9e445 CM |
2825 | again: |
2826 | if (multi_ret) { | |
2827 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), | |
2828 | GFP_NOFS); | |
2829 | if (!multi) | |
2830 | return -ENOMEM; | |
a236aed1 CM |
2831 | |
2832 | atomic_set(&multi->error, 0); | |
cea9e445 | 2833 | } |
0b86a832 | 2834 | |
890871be | 2835 | read_lock(&em_tree->lock); |
0b86a832 | 2836 | em = lookup_extent_mapping(em_tree, logical, *length); |
890871be | 2837 | read_unlock(&em_tree->lock); |
f2d8d74d | 2838 | |
3b951516 | 2839 | if (!em) { |
d397712b CM |
2840 | printk(KERN_CRIT "unable to find logical %llu len %llu\n", |
2841 | (unsigned long long)logical, | |
2842 | (unsigned long long)*length); | |
f2d8d74d | 2843 | BUG(); |
3b951516 | 2844 | } |
0b86a832 CM |
2845 | |
2846 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
2847 | map = (struct map_lookup *)em->bdev; | |
2848 | offset = logical - em->start; | |
593060d7 | 2849 | |
f188591e CM |
2850 | if (mirror_num > map->num_stripes) |
2851 | mirror_num = 0; | |
2852 | ||
cea9e445 | 2853 | /* if our multi bio struct is too small, back off and try again */ |
7b6d91da | 2854 | if (rw & REQ_WRITE) { |
321aecc6 CM |
2855 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
2856 | BTRFS_BLOCK_GROUP_DUP)) { | |
2857 | stripes_required = map->num_stripes; | |
a236aed1 | 2858 | max_errors = 1; |
321aecc6 CM |
2859 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
2860 | stripes_required = map->sub_stripes; | |
a236aed1 | 2861 | max_errors = 1; |
321aecc6 CM |
2862 | } |
2863 | } | |
fce3bb9a LD |
2864 | if (rw & REQ_DISCARD) { |
2865 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
2866 | BTRFS_BLOCK_GROUP_RAID1 | | |
2867 | BTRFS_BLOCK_GROUP_DUP | | |
2868 | BTRFS_BLOCK_GROUP_RAID10)) { | |
2869 | stripes_required = map->num_stripes; | |
2870 | } | |
2871 | } | |
2872 | if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) && | |
321aecc6 | 2873 | stripes_allocated < stripes_required) { |
cea9e445 | 2874 | stripes_allocated = map->num_stripes; |
cea9e445 CM |
2875 | free_extent_map(em); |
2876 | kfree(multi); | |
2877 | goto again; | |
2878 | } | |
593060d7 CM |
2879 | stripe_nr = offset; |
2880 | /* | |
2881 | * stripe_nr counts the total number of stripes we have to stride | |
2882 | * to get to this block | |
2883 | */ | |
2884 | do_div(stripe_nr, map->stripe_len); | |
2885 | ||
2886 | stripe_offset = stripe_nr * map->stripe_len; | |
2887 | BUG_ON(offset < stripe_offset); | |
2888 | ||
2889 | /* stripe_offset is the offset of this block in its stripe*/ | |
2890 | stripe_offset = offset - stripe_offset; | |
2891 | ||
fce3bb9a LD |
2892 | if (rw & REQ_DISCARD) |
2893 | *length = min_t(u64, em->len - offset, *length); | |
2894 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
2895 | BTRFS_BLOCK_GROUP_RAID1 | | |
2896 | BTRFS_BLOCK_GROUP_RAID10 | | |
2897 | BTRFS_BLOCK_GROUP_DUP)) { | |
cea9e445 CM |
2898 | /* we limit the length of each bio to what fits in a stripe */ |
2899 | *length = min_t(u64, em->len - offset, | |
fce3bb9a | 2900 | map->stripe_len - stripe_offset); |
cea9e445 CM |
2901 | } else { |
2902 | *length = em->len - offset; | |
2903 | } | |
f2d8d74d | 2904 | |
7eaceacc | 2905 | if (!multi_ret) |
cea9e445 CM |
2906 | goto out; |
2907 | ||
f2d8d74d | 2908 | num_stripes = 1; |
cea9e445 | 2909 | stripe_index = 0; |
fce3bb9a LD |
2910 | stripe_nr_orig = stripe_nr; |
2911 | stripe_nr_end = (offset + *length + map->stripe_len - 1) & | |
2912 | (~(map->stripe_len - 1)); | |
2913 | do_div(stripe_nr_end, map->stripe_len); | |
2914 | stripe_end_offset = stripe_nr_end * map->stripe_len - | |
2915 | (offset + *length); | |
2916 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { | |
2917 | if (rw & REQ_DISCARD) | |
2918 | num_stripes = min_t(u64, map->num_stripes, | |
2919 | stripe_nr_end - stripe_nr_orig); | |
2920 | stripe_index = do_div(stripe_nr, map->num_stripes); | |
2921 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { | |
212a17ab | 2922 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
f2d8d74d | 2923 | num_stripes = map->num_stripes; |
2fff734f | 2924 | else if (mirror_num) |
f188591e | 2925 | stripe_index = mirror_num - 1; |
dfe25020 CM |
2926 | else { |
2927 | stripe_index = find_live_mirror(map, 0, | |
2928 | map->num_stripes, | |
2929 | current->pid % map->num_stripes); | |
2930 | } | |
2fff734f | 2931 | |
611f0e00 | 2932 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
fce3bb9a | 2933 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
f2d8d74d | 2934 | num_stripes = map->num_stripes; |
f188591e CM |
2935 | else if (mirror_num) |
2936 | stripe_index = mirror_num - 1; | |
2fff734f | 2937 | |
321aecc6 CM |
2938 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
2939 | int factor = map->num_stripes / map->sub_stripes; | |
321aecc6 CM |
2940 | |
2941 | stripe_index = do_div(stripe_nr, factor); | |
2942 | stripe_index *= map->sub_stripes; | |
2943 | ||
7eaceacc | 2944 | if (rw & REQ_WRITE) |
f2d8d74d | 2945 | num_stripes = map->sub_stripes; |
fce3bb9a LD |
2946 | else if (rw & REQ_DISCARD) |
2947 | num_stripes = min_t(u64, map->sub_stripes * | |
2948 | (stripe_nr_end - stripe_nr_orig), | |
2949 | map->num_stripes); | |
321aecc6 CM |
2950 | else if (mirror_num) |
2951 | stripe_index += mirror_num - 1; | |
dfe25020 CM |
2952 | else { |
2953 | stripe_index = find_live_mirror(map, stripe_index, | |
2954 | map->sub_stripes, stripe_index + | |
2955 | current->pid % map->sub_stripes); | |
2956 | } | |
8790d502 CM |
2957 | } else { |
2958 | /* | |
2959 | * after this do_div call, stripe_nr is the number of stripes | |
2960 | * on this device we have to walk to find the data, and | |
2961 | * stripe_index is the number of our device in the stripe array | |
2962 | */ | |
2963 | stripe_index = do_div(stripe_nr, map->num_stripes); | |
2964 | } | |
593060d7 | 2965 | BUG_ON(stripe_index >= map->num_stripes); |
cea9e445 | 2966 | |
fce3bb9a LD |
2967 | if (rw & REQ_DISCARD) { |
2968 | for (i = 0; i < num_stripes; i++) { | |
f2d8d74d CM |
2969 | multi->stripes[i].physical = |
2970 | map->stripes[stripe_index].physical + | |
2971 | stripe_offset + stripe_nr * map->stripe_len; | |
2972 | multi->stripes[i].dev = map->stripes[stripe_index].dev; | |
fce3bb9a LD |
2973 | |
2974 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { | |
2975 | u64 stripes; | |
d9d04879 | 2976 | u32 last_stripe = 0; |
fce3bb9a LD |
2977 | int j; |
2978 | ||
d9d04879 CM |
2979 | div_u64_rem(stripe_nr_end - 1, |
2980 | map->num_stripes, | |
2981 | &last_stripe); | |
2982 | ||
fce3bb9a | 2983 | for (j = 0; j < map->num_stripes; j++) { |
d9d04879 CM |
2984 | u32 test; |
2985 | ||
2986 | div_u64_rem(stripe_nr_end - 1 - j, | |
2987 | map->num_stripes, &test); | |
2988 | if (test == stripe_index) | |
fce3bb9a LD |
2989 | break; |
2990 | } | |
2991 | stripes = stripe_nr_end - 1 - j; | |
2992 | do_div(stripes, map->num_stripes); | |
2993 | multi->stripes[i].length = map->stripe_len * | |
2994 | (stripes - stripe_nr + 1); | |
2995 | ||
2996 | if (i == 0) { | |
2997 | multi->stripes[i].length -= | |
2998 | stripe_offset; | |
2999 | stripe_offset = 0; | |
3000 | } | |
3001 | if (stripe_index == last_stripe) | |
3002 | multi->stripes[i].length -= | |
3003 | stripe_end_offset; | |
3004 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
3005 | u64 stripes; | |
3006 | int j; | |
3007 | int factor = map->num_stripes / | |
3008 | map->sub_stripes; | |
d9d04879 CM |
3009 | u32 last_stripe = 0; |
3010 | ||
3011 | div_u64_rem(stripe_nr_end - 1, | |
3012 | factor, &last_stripe); | |
fce3bb9a LD |
3013 | last_stripe *= map->sub_stripes; |
3014 | ||
3015 | for (j = 0; j < factor; j++) { | |
d9d04879 CM |
3016 | u32 test; |
3017 | ||
3018 | div_u64_rem(stripe_nr_end - 1 - j, | |
3019 | factor, &test); | |
3020 | ||
3021 | if (test == | |
fce3bb9a LD |
3022 | stripe_index / map->sub_stripes) |
3023 | break; | |
3024 | } | |
3025 | stripes = stripe_nr_end - 1 - j; | |
3026 | do_div(stripes, factor); | |
3027 | multi->stripes[i].length = map->stripe_len * | |
3028 | (stripes - stripe_nr + 1); | |
3029 | ||
3030 | if (i < map->sub_stripes) { | |
3031 | multi->stripes[i].length -= | |
3032 | stripe_offset; | |
3033 | if (i == map->sub_stripes - 1) | |
3034 | stripe_offset = 0; | |
3035 | } | |
3036 | if (stripe_index >= last_stripe && | |
3037 | stripe_index <= (last_stripe + | |
3038 | map->sub_stripes - 1)) { | |
3039 | multi->stripes[i].length -= | |
3040 | stripe_end_offset; | |
3041 | } | |
3042 | } else | |
3043 | multi->stripes[i].length = *length; | |
3044 | ||
3045 | stripe_index++; | |
3046 | if (stripe_index == map->num_stripes) { | |
3047 | /* This could only happen for RAID0/10 */ | |
3048 | stripe_index = 0; | |
3049 | stripe_nr++; | |
3050 | } | |
3051 | } | |
3052 | } else { | |
3053 | for (i = 0; i < num_stripes; i++) { | |
212a17ab LT |
3054 | multi->stripes[i].physical = |
3055 | map->stripes[stripe_index].physical + | |
3056 | stripe_offset + | |
3057 | stripe_nr * map->stripe_len; | |
3058 | multi->stripes[i].dev = | |
3059 | map->stripes[stripe_index].dev; | |
fce3bb9a | 3060 | stripe_index++; |
f2d8d74d | 3061 | } |
593060d7 | 3062 | } |
f2d8d74d CM |
3063 | if (multi_ret) { |
3064 | *multi_ret = multi; | |
3065 | multi->num_stripes = num_stripes; | |
a236aed1 | 3066 | multi->max_errors = max_errors; |
f2d8d74d | 3067 | } |
cea9e445 | 3068 | out: |
0b86a832 | 3069 | free_extent_map(em); |
0b86a832 CM |
3070 | return 0; |
3071 | } | |
3072 | ||
f2d8d74d CM |
3073 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
3074 | u64 logical, u64 *length, | |
3075 | struct btrfs_multi_bio **multi_ret, int mirror_num) | |
3076 | { | |
3077 | return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, | |
7eaceacc | 3078 | mirror_num); |
f2d8d74d CM |
3079 | } |
3080 | ||
a512bbf8 YZ |
3081 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
3082 | u64 chunk_start, u64 physical, u64 devid, | |
3083 | u64 **logical, int *naddrs, int *stripe_len) | |
3084 | { | |
3085 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
3086 | struct extent_map *em; | |
3087 | struct map_lookup *map; | |
3088 | u64 *buf; | |
3089 | u64 bytenr; | |
3090 | u64 length; | |
3091 | u64 stripe_nr; | |
3092 | int i, j, nr = 0; | |
3093 | ||
890871be | 3094 | read_lock(&em_tree->lock); |
a512bbf8 | 3095 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
890871be | 3096 | read_unlock(&em_tree->lock); |
a512bbf8 YZ |
3097 | |
3098 | BUG_ON(!em || em->start != chunk_start); | |
3099 | map = (struct map_lookup *)em->bdev; | |
3100 | ||
3101 | length = em->len; | |
3102 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) | |
3103 | do_div(length, map->num_stripes / map->sub_stripes); | |
3104 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) | |
3105 | do_div(length, map->num_stripes); | |
3106 | ||
3107 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); | |
3108 | BUG_ON(!buf); | |
3109 | ||
3110 | for (i = 0; i < map->num_stripes; i++) { | |
3111 | if (devid && map->stripes[i].dev->devid != devid) | |
3112 | continue; | |
3113 | if (map->stripes[i].physical > physical || | |
3114 | map->stripes[i].physical + length <= physical) | |
3115 | continue; | |
3116 | ||
3117 | stripe_nr = physical - map->stripes[i].physical; | |
3118 | do_div(stripe_nr, map->stripe_len); | |
3119 | ||
3120 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
3121 | stripe_nr = stripe_nr * map->num_stripes + i; | |
3122 | do_div(stripe_nr, map->sub_stripes); | |
3123 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { | |
3124 | stripe_nr = stripe_nr * map->num_stripes + i; | |
3125 | } | |
3126 | bytenr = chunk_start + stripe_nr * map->stripe_len; | |
934d375b | 3127 | WARN_ON(nr >= map->num_stripes); |
a512bbf8 YZ |
3128 | for (j = 0; j < nr; j++) { |
3129 | if (buf[j] == bytenr) | |
3130 | break; | |
3131 | } | |
934d375b CM |
3132 | if (j == nr) { |
3133 | WARN_ON(nr >= map->num_stripes); | |
a512bbf8 | 3134 | buf[nr++] = bytenr; |
934d375b | 3135 | } |
a512bbf8 YZ |
3136 | } |
3137 | ||
a512bbf8 YZ |
3138 | *logical = buf; |
3139 | *naddrs = nr; | |
3140 | *stripe_len = map->stripe_len; | |
3141 | ||
3142 | free_extent_map(em); | |
3143 | return 0; | |
f2d8d74d CM |
3144 | } |
3145 | ||
8790d502 | 3146 | static void end_bio_multi_stripe(struct bio *bio, int err) |
8790d502 | 3147 | { |
cea9e445 | 3148 | struct btrfs_multi_bio *multi = bio->bi_private; |
7d2b4daa | 3149 | int is_orig_bio = 0; |
8790d502 | 3150 | |
8790d502 | 3151 | if (err) |
a236aed1 | 3152 | atomic_inc(&multi->error); |
8790d502 | 3153 | |
7d2b4daa CM |
3154 | if (bio == multi->orig_bio) |
3155 | is_orig_bio = 1; | |
3156 | ||
cea9e445 | 3157 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
7d2b4daa CM |
3158 | if (!is_orig_bio) { |
3159 | bio_put(bio); | |
3160 | bio = multi->orig_bio; | |
3161 | } | |
8790d502 CM |
3162 | bio->bi_private = multi->private; |
3163 | bio->bi_end_io = multi->end_io; | |
a236aed1 CM |
3164 | /* only send an error to the higher layers if it is |
3165 | * beyond the tolerance of the multi-bio | |
3166 | */ | |
1259ab75 | 3167 | if (atomic_read(&multi->error) > multi->max_errors) { |
a236aed1 | 3168 | err = -EIO; |
1259ab75 CM |
3169 | } else if (err) { |
3170 | /* | |
3171 | * this bio is actually up to date, we didn't | |
3172 | * go over the max number of errors | |
3173 | */ | |
3174 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
a236aed1 | 3175 | err = 0; |
1259ab75 | 3176 | } |
8790d502 CM |
3177 | kfree(multi); |
3178 | ||
3179 | bio_endio(bio, err); | |
7d2b4daa | 3180 | } else if (!is_orig_bio) { |
8790d502 CM |
3181 | bio_put(bio); |
3182 | } | |
8790d502 CM |
3183 | } |
3184 | ||
8b712842 CM |
3185 | struct async_sched { |
3186 | struct bio *bio; | |
3187 | int rw; | |
3188 | struct btrfs_fs_info *info; | |
3189 | struct btrfs_work work; | |
3190 | }; | |
3191 | ||
3192 | /* | |
3193 | * see run_scheduled_bios for a description of why bios are collected for | |
3194 | * async submit. | |
3195 | * | |
3196 | * This will add one bio to the pending list for a device and make sure | |
3197 | * the work struct is scheduled. | |
3198 | */ | |
d397712b | 3199 | static noinline int schedule_bio(struct btrfs_root *root, |
a1b32a59 CM |
3200 | struct btrfs_device *device, |
3201 | int rw, struct bio *bio) | |
8b712842 CM |
3202 | { |
3203 | int should_queue = 1; | |
ffbd517d | 3204 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
3205 | |
3206 | /* don't bother with additional async steps for reads, right now */ | |
7b6d91da | 3207 | if (!(rw & REQ_WRITE)) { |
492bb6de | 3208 | bio_get(bio); |
8b712842 | 3209 | submit_bio(rw, bio); |
492bb6de | 3210 | bio_put(bio); |
8b712842 CM |
3211 | return 0; |
3212 | } | |
3213 | ||
3214 | /* | |
0986fe9e | 3215 | * nr_async_bios allows us to reliably return congestion to the |
8b712842 CM |
3216 | * higher layers. Otherwise, the async bio makes it appear we have |
3217 | * made progress against dirty pages when we've really just put it | |
3218 | * on a queue for later | |
3219 | */ | |
0986fe9e | 3220 | atomic_inc(&root->fs_info->nr_async_bios); |
492bb6de | 3221 | WARN_ON(bio->bi_next); |
8b712842 CM |
3222 | bio->bi_next = NULL; |
3223 | bio->bi_rw |= rw; | |
3224 | ||
3225 | spin_lock(&device->io_lock); | |
7b6d91da | 3226 | if (bio->bi_rw & REQ_SYNC) |
ffbd517d CM |
3227 | pending_bios = &device->pending_sync_bios; |
3228 | else | |
3229 | pending_bios = &device->pending_bios; | |
8b712842 | 3230 | |
ffbd517d CM |
3231 | if (pending_bios->tail) |
3232 | pending_bios->tail->bi_next = bio; | |
8b712842 | 3233 | |
ffbd517d CM |
3234 | pending_bios->tail = bio; |
3235 | if (!pending_bios->head) | |
3236 | pending_bios->head = bio; | |
8b712842 CM |
3237 | if (device->running_pending) |
3238 | should_queue = 0; | |
3239 | ||
3240 | spin_unlock(&device->io_lock); | |
3241 | ||
3242 | if (should_queue) | |
1cc127b5 CM |
3243 | btrfs_queue_worker(&root->fs_info->submit_workers, |
3244 | &device->work); | |
8b712842 CM |
3245 | return 0; |
3246 | } | |
3247 | ||
f188591e | 3248 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
8b712842 | 3249 | int mirror_num, int async_submit) |
0b86a832 CM |
3250 | { |
3251 | struct btrfs_mapping_tree *map_tree; | |
3252 | struct btrfs_device *dev; | |
8790d502 | 3253 | struct bio *first_bio = bio; |
a62b9401 | 3254 | u64 logical = (u64)bio->bi_sector << 9; |
0b86a832 CM |
3255 | u64 length = 0; |
3256 | u64 map_length; | |
cea9e445 | 3257 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 | 3258 | int ret; |
8790d502 CM |
3259 | int dev_nr = 0; |
3260 | int total_devs = 1; | |
0b86a832 | 3261 | |
f2d8d74d | 3262 | length = bio->bi_size; |
0b86a832 CM |
3263 | map_tree = &root->fs_info->mapping_tree; |
3264 | map_length = length; | |
cea9e445 | 3265 | |
f188591e CM |
3266 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
3267 | mirror_num); | |
cea9e445 CM |
3268 | BUG_ON(ret); |
3269 | ||
3270 | total_devs = multi->num_stripes; | |
3271 | if (map_length < length) { | |
d397712b CM |
3272 | printk(KERN_CRIT "mapping failed logical %llu bio len %llu " |
3273 | "len %llu\n", (unsigned long long)logical, | |
3274 | (unsigned long long)length, | |
3275 | (unsigned long long)map_length); | |
cea9e445 CM |
3276 | BUG(); |
3277 | } | |
3278 | multi->end_io = first_bio->bi_end_io; | |
3279 | multi->private = first_bio->bi_private; | |
7d2b4daa | 3280 | multi->orig_bio = first_bio; |
cea9e445 CM |
3281 | atomic_set(&multi->stripes_pending, multi->num_stripes); |
3282 | ||
d397712b | 3283 | while (dev_nr < total_devs) { |
8790d502 | 3284 | if (total_devs > 1) { |
8790d502 CM |
3285 | if (dev_nr < total_devs - 1) { |
3286 | bio = bio_clone(first_bio, GFP_NOFS); | |
3287 | BUG_ON(!bio); | |
3288 | } else { | |
3289 | bio = first_bio; | |
3290 | } | |
3291 | bio->bi_private = multi; | |
3292 | bio->bi_end_io = end_bio_multi_stripe; | |
3293 | } | |
cea9e445 CM |
3294 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
3295 | dev = multi->stripes[dev_nr].dev; | |
18e503d6 | 3296 | if (dev && dev->bdev && (rw != WRITE || dev->writeable)) { |
dfe25020 | 3297 | bio->bi_bdev = dev->bdev; |
8b712842 CM |
3298 | if (async_submit) |
3299 | schedule_bio(root, dev, rw, bio); | |
3300 | else | |
3301 | submit_bio(rw, bio); | |
dfe25020 CM |
3302 | } else { |
3303 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; | |
3304 | bio->bi_sector = logical >> 9; | |
dfe25020 | 3305 | bio_endio(bio, -EIO); |
dfe25020 | 3306 | } |
8790d502 CM |
3307 | dev_nr++; |
3308 | } | |
cea9e445 CM |
3309 | if (total_devs == 1) |
3310 | kfree(multi); | |
0b86a832 CM |
3311 | return 0; |
3312 | } | |
3313 | ||
a443755f | 3314 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
2b82032c | 3315 | u8 *uuid, u8 *fsid) |
0b86a832 | 3316 | { |
2b82032c YZ |
3317 | struct btrfs_device *device; |
3318 | struct btrfs_fs_devices *cur_devices; | |
3319 | ||
3320 | cur_devices = root->fs_info->fs_devices; | |
3321 | while (cur_devices) { | |
3322 | if (!fsid || | |
3323 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { | |
3324 | device = __find_device(&cur_devices->devices, | |
3325 | devid, uuid); | |
3326 | if (device) | |
3327 | return device; | |
3328 | } | |
3329 | cur_devices = cur_devices->seed; | |
3330 | } | |
3331 | return NULL; | |
0b86a832 CM |
3332 | } |
3333 | ||
dfe25020 CM |
3334 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
3335 | u64 devid, u8 *dev_uuid) | |
3336 | { | |
3337 | struct btrfs_device *device; | |
3338 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
3339 | ||
3340 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
7cbd8a83 | 3341 | if (!device) |
3342 | return NULL; | |
dfe25020 CM |
3343 | list_add(&device->dev_list, |
3344 | &fs_devices->devices); | |
dfe25020 CM |
3345 | device->dev_root = root->fs_info->dev_root; |
3346 | device->devid = devid; | |
8b712842 | 3347 | device->work.func = pending_bios_fn; |
e4404d6e | 3348 | device->fs_devices = fs_devices; |
cd02dca5 | 3349 | device->missing = 1; |
dfe25020 | 3350 | fs_devices->num_devices++; |
cd02dca5 | 3351 | fs_devices->missing_devices++; |
dfe25020 | 3352 | spin_lock_init(&device->io_lock); |
d20f7043 | 3353 | INIT_LIST_HEAD(&device->dev_alloc_list); |
dfe25020 CM |
3354 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
3355 | return device; | |
3356 | } | |
3357 | ||
0b86a832 CM |
3358 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
3359 | struct extent_buffer *leaf, | |
3360 | struct btrfs_chunk *chunk) | |
3361 | { | |
3362 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
3363 | struct map_lookup *map; | |
3364 | struct extent_map *em; | |
3365 | u64 logical; | |
3366 | u64 length; | |
3367 | u64 devid; | |
a443755f | 3368 | u8 uuid[BTRFS_UUID_SIZE]; |
593060d7 | 3369 | int num_stripes; |
0b86a832 | 3370 | int ret; |
593060d7 | 3371 | int i; |
0b86a832 | 3372 | |
e17cade2 CM |
3373 | logical = key->offset; |
3374 | length = btrfs_chunk_length(leaf, chunk); | |
a061fc8d | 3375 | |
890871be | 3376 | read_lock(&map_tree->map_tree.lock); |
0b86a832 | 3377 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
890871be | 3378 | read_unlock(&map_tree->map_tree.lock); |
0b86a832 CM |
3379 | |
3380 | /* already mapped? */ | |
3381 | if (em && em->start <= logical && em->start + em->len > logical) { | |
3382 | free_extent_map(em); | |
0b86a832 CM |
3383 | return 0; |
3384 | } else if (em) { | |
3385 | free_extent_map(em); | |
3386 | } | |
0b86a832 | 3387 | |
172ddd60 | 3388 | em = alloc_extent_map(); |
0b86a832 CM |
3389 | if (!em) |
3390 | return -ENOMEM; | |
593060d7 CM |
3391 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
3392 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
0b86a832 CM |
3393 | if (!map) { |
3394 | free_extent_map(em); | |
3395 | return -ENOMEM; | |
3396 | } | |
3397 | ||
3398 | em->bdev = (struct block_device *)map; | |
3399 | em->start = logical; | |
3400 | em->len = length; | |
3401 | em->block_start = 0; | |
c8b97818 | 3402 | em->block_len = em->len; |
0b86a832 | 3403 | |
593060d7 CM |
3404 | map->num_stripes = num_stripes; |
3405 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
3406 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
3407 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); | |
3408 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); | |
3409 | map->type = btrfs_chunk_type(leaf, chunk); | |
321aecc6 | 3410 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
593060d7 CM |
3411 | for (i = 0; i < num_stripes; i++) { |
3412 | map->stripes[i].physical = | |
3413 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
3414 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
a443755f CM |
3415 | read_extent_buffer(leaf, uuid, (unsigned long) |
3416 | btrfs_stripe_dev_uuid_nr(chunk, i), | |
3417 | BTRFS_UUID_SIZE); | |
2b82032c YZ |
3418 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid, |
3419 | NULL); | |
dfe25020 | 3420 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
593060d7 CM |
3421 | kfree(map); |
3422 | free_extent_map(em); | |
3423 | return -EIO; | |
3424 | } | |
dfe25020 CM |
3425 | if (!map->stripes[i].dev) { |
3426 | map->stripes[i].dev = | |
3427 | add_missing_dev(root, devid, uuid); | |
3428 | if (!map->stripes[i].dev) { | |
3429 | kfree(map); | |
3430 | free_extent_map(em); | |
3431 | return -EIO; | |
3432 | } | |
3433 | } | |
3434 | map->stripes[i].dev->in_fs_metadata = 1; | |
0b86a832 CM |
3435 | } |
3436 | ||
890871be | 3437 | write_lock(&map_tree->map_tree.lock); |
0b86a832 | 3438 | ret = add_extent_mapping(&map_tree->map_tree, em); |
890871be | 3439 | write_unlock(&map_tree->map_tree.lock); |
b248a415 | 3440 | BUG_ON(ret); |
0b86a832 CM |
3441 | free_extent_map(em); |
3442 | ||
3443 | return 0; | |
3444 | } | |
3445 | ||
3446 | static int fill_device_from_item(struct extent_buffer *leaf, | |
3447 | struct btrfs_dev_item *dev_item, | |
3448 | struct btrfs_device *device) | |
3449 | { | |
3450 | unsigned long ptr; | |
0b86a832 CM |
3451 | |
3452 | device->devid = btrfs_device_id(leaf, dev_item); | |
d6397bae CB |
3453 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
3454 | device->total_bytes = device->disk_total_bytes; | |
0b86a832 CM |
3455 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
3456 | device->type = btrfs_device_type(leaf, dev_item); | |
3457 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
3458 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
3459 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
0b86a832 CM |
3460 | |
3461 | ptr = (unsigned long)btrfs_device_uuid(dev_item); | |
e17cade2 | 3462 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 | 3463 | |
0b86a832 CM |
3464 | return 0; |
3465 | } | |
3466 | ||
2b82032c YZ |
3467 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
3468 | { | |
3469 | struct btrfs_fs_devices *fs_devices; | |
3470 | int ret; | |
3471 | ||
3472 | mutex_lock(&uuid_mutex); | |
3473 | ||
3474 | fs_devices = root->fs_info->fs_devices->seed; | |
3475 | while (fs_devices) { | |
3476 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { | |
3477 | ret = 0; | |
3478 | goto out; | |
3479 | } | |
3480 | fs_devices = fs_devices->seed; | |
3481 | } | |
3482 | ||
3483 | fs_devices = find_fsid(fsid); | |
3484 | if (!fs_devices) { | |
3485 | ret = -ENOENT; | |
3486 | goto out; | |
3487 | } | |
e4404d6e YZ |
3488 | |
3489 | fs_devices = clone_fs_devices(fs_devices); | |
3490 | if (IS_ERR(fs_devices)) { | |
3491 | ret = PTR_ERR(fs_devices); | |
2b82032c YZ |
3492 | goto out; |
3493 | } | |
3494 | ||
97288f2c | 3495 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
15916de8 | 3496 | root->fs_info->bdev_holder); |
2b82032c YZ |
3497 | if (ret) |
3498 | goto out; | |
3499 | ||
3500 | if (!fs_devices->seeding) { | |
3501 | __btrfs_close_devices(fs_devices); | |
e4404d6e | 3502 | free_fs_devices(fs_devices); |
2b82032c YZ |
3503 | ret = -EINVAL; |
3504 | goto out; | |
3505 | } | |
3506 | ||
3507 | fs_devices->seed = root->fs_info->fs_devices->seed; | |
3508 | root->fs_info->fs_devices->seed = fs_devices; | |
2b82032c YZ |
3509 | out: |
3510 | mutex_unlock(&uuid_mutex); | |
3511 | return ret; | |
3512 | } | |
3513 | ||
0d81ba5d | 3514 | static int read_one_dev(struct btrfs_root *root, |
0b86a832 CM |
3515 | struct extent_buffer *leaf, |
3516 | struct btrfs_dev_item *dev_item) | |
3517 | { | |
3518 | struct btrfs_device *device; | |
3519 | u64 devid; | |
3520 | int ret; | |
2b82032c | 3521 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
a443755f CM |
3522 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
3523 | ||
0b86a832 | 3524 | devid = btrfs_device_id(leaf, dev_item); |
a443755f CM |
3525 | read_extent_buffer(leaf, dev_uuid, |
3526 | (unsigned long)btrfs_device_uuid(dev_item), | |
3527 | BTRFS_UUID_SIZE); | |
2b82032c YZ |
3528 | read_extent_buffer(leaf, fs_uuid, |
3529 | (unsigned long)btrfs_device_fsid(dev_item), | |
3530 | BTRFS_UUID_SIZE); | |
3531 | ||
3532 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { | |
3533 | ret = open_seed_devices(root, fs_uuid); | |
e4404d6e | 3534 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
2b82032c | 3535 | return ret; |
2b82032c YZ |
3536 | } |
3537 | ||
3538 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); | |
3539 | if (!device || !device->bdev) { | |
e4404d6e | 3540 | if (!btrfs_test_opt(root, DEGRADED)) |
2b82032c YZ |
3541 | return -EIO; |
3542 | ||
3543 | if (!device) { | |
d397712b CM |
3544 | printk(KERN_WARNING "warning devid %llu missing\n", |
3545 | (unsigned long long)devid); | |
2b82032c YZ |
3546 | device = add_missing_dev(root, devid, dev_uuid); |
3547 | if (!device) | |
3548 | return -ENOMEM; | |
cd02dca5 CM |
3549 | } else if (!device->missing) { |
3550 | /* | |
3551 | * this happens when a device that was properly setup | |
3552 | * in the device info lists suddenly goes bad. | |
3553 | * device->bdev is NULL, and so we have to set | |
3554 | * device->missing to one here | |
3555 | */ | |
3556 | root->fs_info->fs_devices->missing_devices++; | |
3557 | device->missing = 1; | |
2b82032c YZ |
3558 | } |
3559 | } | |
3560 | ||
3561 | if (device->fs_devices != root->fs_info->fs_devices) { | |
3562 | BUG_ON(device->writeable); | |
3563 | if (device->generation != | |
3564 | btrfs_device_generation(leaf, dev_item)) | |
3565 | return -EINVAL; | |
6324fbf3 | 3566 | } |
0b86a832 CM |
3567 | |
3568 | fill_device_from_item(leaf, dev_item, device); | |
3569 | device->dev_root = root->fs_info->dev_root; | |
dfe25020 | 3570 | device->in_fs_metadata = 1; |
2b82032c YZ |
3571 | if (device->writeable) |
3572 | device->fs_devices->total_rw_bytes += device->total_bytes; | |
0b86a832 | 3573 | ret = 0; |
0b86a832 CM |
3574 | return ret; |
3575 | } | |
3576 | ||
e4404d6e | 3577 | int btrfs_read_sys_array(struct btrfs_root *root) |
0b86a832 CM |
3578 | { |
3579 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
a061fc8d | 3580 | struct extent_buffer *sb; |
0b86a832 | 3581 | struct btrfs_disk_key *disk_key; |
0b86a832 | 3582 | struct btrfs_chunk *chunk; |
84eed90f CM |
3583 | u8 *ptr; |
3584 | unsigned long sb_ptr; | |
3585 | int ret = 0; | |
0b86a832 CM |
3586 | u32 num_stripes; |
3587 | u32 array_size; | |
3588 | u32 len = 0; | |
0b86a832 | 3589 | u32 cur; |
84eed90f | 3590 | struct btrfs_key key; |
0b86a832 | 3591 | |
e4404d6e | 3592 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
a061fc8d CM |
3593 | BTRFS_SUPER_INFO_SIZE); |
3594 | if (!sb) | |
3595 | return -ENOMEM; | |
3596 | btrfs_set_buffer_uptodate(sb); | |
4008c04a CM |
3597 | btrfs_set_buffer_lockdep_class(sb, 0); |
3598 | ||
a061fc8d | 3599 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
0b86a832 CM |
3600 | array_size = btrfs_super_sys_array_size(super_copy); |
3601 | ||
0b86a832 CM |
3602 | ptr = super_copy->sys_chunk_array; |
3603 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); | |
3604 | cur = 0; | |
3605 | ||
3606 | while (cur < array_size) { | |
3607 | disk_key = (struct btrfs_disk_key *)ptr; | |
3608 | btrfs_disk_key_to_cpu(&key, disk_key); | |
3609 | ||
a061fc8d | 3610 | len = sizeof(*disk_key); ptr += len; |
0b86a832 CM |
3611 | sb_ptr += len; |
3612 | cur += len; | |
3613 | ||
0d81ba5d | 3614 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
0b86a832 | 3615 | chunk = (struct btrfs_chunk *)sb_ptr; |
0d81ba5d | 3616 | ret = read_one_chunk(root, &key, sb, chunk); |
84eed90f CM |
3617 | if (ret) |
3618 | break; | |
0b86a832 CM |
3619 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
3620 | len = btrfs_chunk_item_size(num_stripes); | |
3621 | } else { | |
84eed90f CM |
3622 | ret = -EIO; |
3623 | break; | |
0b86a832 CM |
3624 | } |
3625 | ptr += len; | |
3626 | sb_ptr += len; | |
3627 | cur += len; | |
3628 | } | |
a061fc8d | 3629 | free_extent_buffer(sb); |
84eed90f | 3630 | return ret; |
0b86a832 CM |
3631 | } |
3632 | ||
3633 | int btrfs_read_chunk_tree(struct btrfs_root *root) | |
3634 | { | |
3635 | struct btrfs_path *path; | |
3636 | struct extent_buffer *leaf; | |
3637 | struct btrfs_key key; | |
3638 | struct btrfs_key found_key; | |
3639 | int ret; | |
3640 | int slot; | |
3641 | ||
3642 | root = root->fs_info->chunk_root; | |
3643 | ||
3644 | path = btrfs_alloc_path(); | |
3645 | if (!path) | |
3646 | return -ENOMEM; | |
3647 | ||
3648 | /* first we search for all of the device items, and then we | |
3649 | * read in all of the chunk items. This way we can create chunk | |
3650 | * mappings that reference all of the devices that are afound | |
3651 | */ | |
3652 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
3653 | key.offset = 0; | |
3654 | key.type = 0; | |
3655 | again: | |
3656 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
ab59381e ZL |
3657 | if (ret < 0) |
3658 | goto error; | |
d397712b | 3659 | while (1) { |
0b86a832 CM |
3660 | leaf = path->nodes[0]; |
3661 | slot = path->slots[0]; | |
3662 | if (slot >= btrfs_header_nritems(leaf)) { | |
3663 | ret = btrfs_next_leaf(root, path); | |
3664 | if (ret == 0) | |
3665 | continue; | |
3666 | if (ret < 0) | |
3667 | goto error; | |
3668 | break; | |
3669 | } | |
3670 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3671 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
3672 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) | |
3673 | break; | |
3674 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { | |
3675 | struct btrfs_dev_item *dev_item; | |
3676 | dev_item = btrfs_item_ptr(leaf, slot, | |
3677 | struct btrfs_dev_item); | |
0d81ba5d | 3678 | ret = read_one_dev(root, leaf, dev_item); |
2b82032c YZ |
3679 | if (ret) |
3680 | goto error; | |
0b86a832 CM |
3681 | } |
3682 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { | |
3683 | struct btrfs_chunk *chunk; | |
3684 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | |
3685 | ret = read_one_chunk(root, &found_key, leaf, chunk); | |
2b82032c YZ |
3686 | if (ret) |
3687 | goto error; | |
0b86a832 CM |
3688 | } |
3689 | path->slots[0]++; | |
3690 | } | |
3691 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
3692 | key.objectid = 0; | |
b3b4aa74 | 3693 | btrfs_release_path(path); |
0b86a832 CM |
3694 | goto again; |
3695 | } | |
0b86a832 CM |
3696 | ret = 0; |
3697 | error: | |
2b82032c | 3698 | btrfs_free_path(path); |
0b86a832 CM |
3699 | return ret; |
3700 | } |