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