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