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