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