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