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