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