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