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