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