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