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