ALSA: hda - Add support for 92HD65 / 92HD66 family of codecs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / volumes.c
blob43baaf0c6749d9d03a61863f5f233c2b5a1b90cc
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
27 #include "compat.h"
28 #include "ctree.h"
29 #include "extent_map.h"
30 #include "disk-io.h"
31 #include "transaction.h"
32 #include "print-tree.h"
33 #include "volumes.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
41 static DEFINE_MUTEX(uuid_mutex);
42 static LIST_HEAD(fs_uuids);
44 static void lock_chunks(struct btrfs_root *root)
46 mutex_lock(&root->fs_info->chunk_mutex);
49 static void unlock_chunks(struct btrfs_root *root)
51 mutex_unlock(&root->fs_info->chunk_mutex);
54 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
62 kfree(device->name);
63 kfree(device);
65 kfree(fs_devices);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices *fs_devices;
72 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
76 free_fs_devices(fs_devices);
78 return 0;
81 static noinline struct btrfs_device *__find_device(struct list_head *head,
82 u64 devid, u8 *uuid)
84 struct btrfs_device *dev;
86 list_for_each_entry(dev, head, dev_list) {
87 if (dev->devid == devid &&
88 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
89 return dev;
92 return NULL;
95 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
97 struct btrfs_fs_devices *fs_devices;
99 list_for_each_entry(fs_devices, &fs_uuids, list) {
100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
101 return fs_devices;
103 return NULL;
106 static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
110 struct bio *old_head;
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
116 else
117 pending_bios->tail = tail;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline int run_scheduled_bios(struct btrfs_device *device)
133 struct bio *pending;
134 struct backing_dev_info *bdi;
135 struct btrfs_fs_info *fs_info;
136 struct btrfs_pending_bios *pending_bios;
137 struct bio *tail;
138 struct bio *cur;
139 int again = 0;
140 unsigned long num_run;
141 unsigned long batch_run = 0;
142 unsigned long limit;
143 unsigned long last_waited = 0;
144 int force_reg = 0;
145 struct blk_plug plug;
148 * this function runs all the bios we've collected for
149 * a particular device. We don't want to wander off to
150 * another device without first sending all of these down.
151 * So, setup a plug here and finish it off before we return
153 blk_start_plug(&plug);
155 bdi = blk_get_backing_dev_info(device->bdev);
156 fs_info = device->dev_root->fs_info;
157 limit = btrfs_async_submit_limit(fs_info);
158 limit = limit * 2 / 3;
160 loop:
161 spin_lock(&device->io_lock);
163 loop_lock:
164 num_run = 0;
166 /* take all the bios off the list at once and process them
167 * later on (without the lock held). But, remember the
168 * tail and other pointers so the bios can be properly reinserted
169 * into the list if we hit congestion
171 if (!force_reg && device->pending_sync_bios.head) {
172 pending_bios = &device->pending_sync_bios;
173 force_reg = 1;
174 } else {
175 pending_bios = &device->pending_bios;
176 force_reg = 0;
179 pending = pending_bios->head;
180 tail = pending_bios->tail;
181 WARN_ON(pending && !tail);
184 * if pending was null this time around, no bios need processing
185 * at all and we can stop. Otherwise it'll loop back up again
186 * and do an additional check so no bios are missed.
188 * device->running_pending is used to synchronize with the
189 * schedule_bio code.
191 if (device->pending_sync_bios.head == NULL &&
192 device->pending_bios.head == NULL) {
193 again = 0;
194 device->running_pending = 0;
195 } else {
196 again = 1;
197 device->running_pending = 1;
200 pending_bios->head = NULL;
201 pending_bios->tail = NULL;
203 spin_unlock(&device->io_lock);
205 while (pending) {
207 rmb();
208 /* we want to work on both lists, but do more bios on the
209 * sync list than the regular list
211 if ((num_run > 32 &&
212 pending_bios != &device->pending_sync_bios &&
213 device->pending_sync_bios.head) ||
214 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
215 device->pending_bios.head)) {
216 spin_lock(&device->io_lock);
217 requeue_list(pending_bios, pending, tail);
218 goto loop_lock;
221 cur = pending;
222 pending = pending->bi_next;
223 cur->bi_next = NULL;
224 atomic_dec(&fs_info->nr_async_bios);
226 if (atomic_read(&fs_info->nr_async_bios) < limit &&
227 waitqueue_active(&fs_info->async_submit_wait))
228 wake_up(&fs_info->async_submit_wait);
230 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
232 submit_bio(cur->bi_rw, cur);
233 num_run++;
234 batch_run++;
235 if (need_resched())
236 cond_resched();
239 * we made progress, there is more work to do and the bdi
240 * is now congested. Back off and let other work structs
241 * run instead
243 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
244 fs_info->fs_devices->open_devices > 1) {
245 struct io_context *ioc;
247 ioc = current->io_context;
250 * the main goal here is that we don't want to
251 * block if we're going to be able to submit
252 * more requests without blocking.
254 * This code does two great things, it pokes into
255 * the elevator code from a filesystem _and_
256 * it makes assumptions about how batching works.
258 if (ioc && ioc->nr_batch_requests > 0 &&
259 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
260 (last_waited == 0 ||
261 ioc->last_waited == last_waited)) {
263 * we want to go through our batch of
264 * requests and stop. So, we copy out
265 * the ioc->last_waited time and test
266 * against it before looping
268 last_waited = ioc->last_waited;
269 if (need_resched())
270 cond_resched();
271 continue;
273 spin_lock(&device->io_lock);
274 requeue_list(pending_bios, pending, tail);
275 device->running_pending = 1;
277 spin_unlock(&device->io_lock);
278 btrfs_requeue_work(&device->work);
279 goto done;
283 cond_resched();
284 if (again)
285 goto loop;
287 spin_lock(&device->io_lock);
288 if (device->pending_bios.head || device->pending_sync_bios.head)
289 goto loop_lock;
290 spin_unlock(&device->io_lock);
292 done:
293 blk_finish_plug(&plug);
294 return 0;
297 static void pending_bios_fn(struct btrfs_work *work)
299 struct btrfs_device *device;
301 device = container_of(work, struct btrfs_device, work);
302 run_scheduled_bios(device);
305 static noinline int device_list_add(const char *path,
306 struct btrfs_super_block *disk_super,
307 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
309 struct btrfs_device *device;
310 struct btrfs_fs_devices *fs_devices;
311 u64 found_transid = btrfs_super_generation(disk_super);
312 char *name;
314 fs_devices = find_fsid(disk_super->fsid);
315 if (!fs_devices) {
316 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
317 if (!fs_devices)
318 return -ENOMEM;
319 INIT_LIST_HEAD(&fs_devices->devices);
320 INIT_LIST_HEAD(&fs_devices->alloc_list);
321 list_add(&fs_devices->list, &fs_uuids);
322 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
323 fs_devices->latest_devid = devid;
324 fs_devices->latest_trans = found_transid;
325 mutex_init(&fs_devices->device_list_mutex);
326 device = NULL;
327 } else {
328 device = __find_device(&fs_devices->devices, devid,
329 disk_super->dev_item.uuid);
331 if (!device) {
332 if (fs_devices->opened)
333 return -EBUSY;
335 device = kzalloc(sizeof(*device), GFP_NOFS);
336 if (!device) {
337 /* we can safely leave the fs_devices entry around */
338 return -ENOMEM;
340 device->devid = devid;
341 device->work.func = pending_bios_fn;
342 memcpy(device->uuid, disk_super->dev_item.uuid,
343 BTRFS_UUID_SIZE);
344 spin_lock_init(&device->io_lock);
345 device->name = kstrdup(path, GFP_NOFS);
346 if (!device->name) {
347 kfree(device);
348 return -ENOMEM;
350 INIT_LIST_HEAD(&device->dev_alloc_list);
352 mutex_lock(&fs_devices->device_list_mutex);
353 list_add_rcu(&device->dev_list, &fs_devices->devices);
354 mutex_unlock(&fs_devices->device_list_mutex);
356 device->fs_devices = fs_devices;
357 fs_devices->num_devices++;
358 } else if (!device->name || strcmp(device->name, path)) {
359 name = kstrdup(path, GFP_NOFS);
360 if (!name)
361 return -ENOMEM;
362 kfree(device->name);
363 device->name = name;
364 if (device->missing) {
365 fs_devices->missing_devices--;
366 device->missing = 0;
370 if (found_transid > fs_devices->latest_trans) {
371 fs_devices->latest_devid = devid;
372 fs_devices->latest_trans = found_transid;
374 *fs_devices_ret = fs_devices;
375 return 0;
378 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
380 struct btrfs_fs_devices *fs_devices;
381 struct btrfs_device *device;
382 struct btrfs_device *orig_dev;
384 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
385 if (!fs_devices)
386 return ERR_PTR(-ENOMEM);
388 INIT_LIST_HEAD(&fs_devices->devices);
389 INIT_LIST_HEAD(&fs_devices->alloc_list);
390 INIT_LIST_HEAD(&fs_devices->list);
391 mutex_init(&fs_devices->device_list_mutex);
392 fs_devices->latest_devid = orig->latest_devid;
393 fs_devices->latest_trans = orig->latest_trans;
394 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
396 /* We have held the volume lock, it is safe to get the devices. */
397 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
398 device = kzalloc(sizeof(*device), GFP_NOFS);
399 if (!device)
400 goto error;
402 device->name = kstrdup(orig_dev->name, GFP_NOFS);
403 if (!device->name) {
404 kfree(device);
405 goto error;
408 device->devid = orig_dev->devid;
409 device->work.func = pending_bios_fn;
410 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
411 spin_lock_init(&device->io_lock);
412 INIT_LIST_HEAD(&device->dev_list);
413 INIT_LIST_HEAD(&device->dev_alloc_list);
415 list_add(&device->dev_list, &fs_devices->devices);
416 device->fs_devices = fs_devices;
417 fs_devices->num_devices++;
419 return fs_devices;
420 error:
421 free_fs_devices(fs_devices);
422 return ERR_PTR(-ENOMEM);
425 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
427 struct btrfs_device *device, *next;
429 mutex_lock(&uuid_mutex);
430 again:
431 /* This is the initialized path, it is safe to release the devices. */
432 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
433 if (device->in_fs_metadata)
434 continue;
436 if (device->bdev) {
437 blkdev_put(device->bdev, device->mode);
438 device->bdev = NULL;
439 fs_devices->open_devices--;
441 if (device->writeable) {
442 list_del_init(&device->dev_alloc_list);
443 device->writeable = 0;
444 fs_devices->rw_devices--;
446 list_del_init(&device->dev_list);
447 fs_devices->num_devices--;
448 kfree(device->name);
449 kfree(device);
452 if (fs_devices->seed) {
453 fs_devices = fs_devices->seed;
454 goto again;
457 mutex_unlock(&uuid_mutex);
458 return 0;
461 static void __free_device(struct work_struct *work)
463 struct btrfs_device *device;
465 device = container_of(work, struct btrfs_device, rcu_work);
467 if (device->bdev)
468 blkdev_put(device->bdev, device->mode);
470 kfree(device->name);
471 kfree(device);
474 static void free_device(struct rcu_head *head)
476 struct btrfs_device *device;
478 device = container_of(head, struct btrfs_device, rcu);
480 INIT_WORK(&device->rcu_work, __free_device);
481 schedule_work(&device->rcu_work);
484 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
486 struct btrfs_device *device;
488 if (--fs_devices->opened > 0)
489 return 0;
491 mutex_lock(&fs_devices->device_list_mutex);
492 list_for_each_entry(device, &fs_devices->devices, dev_list) {
493 struct btrfs_device *new_device;
495 if (device->bdev)
496 fs_devices->open_devices--;
498 if (device->writeable) {
499 list_del_init(&device->dev_alloc_list);
500 fs_devices->rw_devices--;
503 if (device->can_discard)
504 fs_devices->num_can_discard--;
506 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
507 BUG_ON(!new_device);
508 memcpy(new_device, device, sizeof(*new_device));
509 new_device->name = kstrdup(device->name, GFP_NOFS);
510 BUG_ON(device->name && !new_device->name);
511 new_device->bdev = NULL;
512 new_device->writeable = 0;
513 new_device->in_fs_metadata = 0;
514 new_device->can_discard = 0;
515 list_replace_rcu(&device->dev_list, &new_device->dev_list);
517 call_rcu(&device->rcu, free_device);
519 mutex_unlock(&fs_devices->device_list_mutex);
521 WARN_ON(fs_devices->open_devices);
522 WARN_ON(fs_devices->rw_devices);
523 fs_devices->opened = 0;
524 fs_devices->seeding = 0;
526 return 0;
529 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
531 struct btrfs_fs_devices *seed_devices = NULL;
532 int ret;
534 mutex_lock(&uuid_mutex);
535 ret = __btrfs_close_devices(fs_devices);
536 if (!fs_devices->opened) {
537 seed_devices = fs_devices->seed;
538 fs_devices->seed = NULL;
540 mutex_unlock(&uuid_mutex);
542 while (seed_devices) {
543 fs_devices = seed_devices;
544 seed_devices = fs_devices->seed;
545 __btrfs_close_devices(fs_devices);
546 free_fs_devices(fs_devices);
548 return ret;
551 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
552 fmode_t flags, void *holder)
554 struct request_queue *q;
555 struct block_device *bdev;
556 struct list_head *head = &fs_devices->devices;
557 struct btrfs_device *device;
558 struct block_device *latest_bdev = NULL;
559 struct buffer_head *bh;
560 struct btrfs_super_block *disk_super;
561 u64 latest_devid = 0;
562 u64 latest_transid = 0;
563 u64 devid;
564 int seeding = 1;
565 int ret = 0;
567 flags |= FMODE_EXCL;
569 list_for_each_entry(device, head, dev_list) {
570 if (device->bdev)
571 continue;
572 if (!device->name)
573 continue;
575 bdev = blkdev_get_by_path(device->name, flags, holder);
576 if (IS_ERR(bdev)) {
577 printk(KERN_INFO "open %s failed\n", device->name);
578 goto error;
580 set_blocksize(bdev, 4096);
582 bh = btrfs_read_dev_super(bdev);
583 if (!bh) {
584 ret = -EINVAL;
585 goto error_close;
588 disk_super = (struct btrfs_super_block *)bh->b_data;
589 devid = btrfs_stack_device_id(&disk_super->dev_item);
590 if (devid != device->devid)
591 goto error_brelse;
593 if (memcmp(device->uuid, disk_super->dev_item.uuid,
594 BTRFS_UUID_SIZE))
595 goto error_brelse;
597 device->generation = btrfs_super_generation(disk_super);
598 if (!latest_transid || device->generation > latest_transid) {
599 latest_devid = devid;
600 latest_transid = device->generation;
601 latest_bdev = bdev;
604 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
605 device->writeable = 0;
606 } else {
607 device->writeable = !bdev_read_only(bdev);
608 seeding = 0;
611 q = bdev_get_queue(bdev);
612 if (blk_queue_discard(q)) {
613 device->can_discard = 1;
614 fs_devices->num_can_discard++;
617 device->bdev = bdev;
618 device->in_fs_metadata = 0;
619 device->mode = flags;
621 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
622 fs_devices->rotating = 1;
624 fs_devices->open_devices++;
625 if (device->writeable) {
626 fs_devices->rw_devices++;
627 list_add(&device->dev_alloc_list,
628 &fs_devices->alloc_list);
630 brelse(bh);
631 continue;
633 error_brelse:
634 brelse(bh);
635 error_close:
636 blkdev_put(bdev, flags);
637 error:
638 continue;
640 if (fs_devices->open_devices == 0) {
641 ret = -EIO;
642 goto out;
644 fs_devices->seeding = seeding;
645 fs_devices->opened = 1;
646 fs_devices->latest_bdev = latest_bdev;
647 fs_devices->latest_devid = latest_devid;
648 fs_devices->latest_trans = latest_transid;
649 fs_devices->total_rw_bytes = 0;
650 out:
651 return ret;
654 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
655 fmode_t flags, void *holder)
657 int ret;
659 mutex_lock(&uuid_mutex);
660 if (fs_devices->opened) {
661 fs_devices->opened++;
662 ret = 0;
663 } else {
664 ret = __btrfs_open_devices(fs_devices, flags, holder);
666 mutex_unlock(&uuid_mutex);
667 return ret;
670 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
671 struct btrfs_fs_devices **fs_devices_ret)
673 struct btrfs_super_block *disk_super;
674 struct block_device *bdev;
675 struct buffer_head *bh;
676 int ret;
677 u64 devid;
678 u64 transid;
680 mutex_lock(&uuid_mutex);
682 flags |= FMODE_EXCL;
683 bdev = blkdev_get_by_path(path, flags, holder);
685 if (IS_ERR(bdev)) {
686 ret = PTR_ERR(bdev);
687 goto error;
690 ret = set_blocksize(bdev, 4096);
691 if (ret)
692 goto error_close;
693 bh = btrfs_read_dev_super(bdev);
694 if (!bh) {
695 ret = -EINVAL;
696 goto error_close;
698 disk_super = (struct btrfs_super_block *)bh->b_data;
699 devid = btrfs_stack_device_id(&disk_super->dev_item);
700 transid = btrfs_super_generation(disk_super);
701 if (disk_super->label[0])
702 printk(KERN_INFO "device label %s ", disk_super->label);
703 else
704 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
705 printk(KERN_CONT "devid %llu transid %llu %s\n",
706 (unsigned long long)devid, (unsigned long long)transid, path);
707 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
709 brelse(bh);
710 error_close:
711 blkdev_put(bdev, flags);
712 error:
713 mutex_unlock(&uuid_mutex);
714 return ret;
717 /* helper to account the used device space in the range */
718 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
719 u64 end, u64 *length)
721 struct btrfs_key key;
722 struct btrfs_root *root = device->dev_root;
723 struct btrfs_dev_extent *dev_extent;
724 struct btrfs_path *path;
725 u64 extent_end;
726 int ret;
727 int slot;
728 struct extent_buffer *l;
730 *length = 0;
732 if (start >= device->total_bytes)
733 return 0;
735 path = btrfs_alloc_path();
736 if (!path)
737 return -ENOMEM;
738 path->reada = 2;
740 key.objectid = device->devid;
741 key.offset = start;
742 key.type = BTRFS_DEV_EXTENT_KEY;
744 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
745 if (ret < 0)
746 goto out;
747 if (ret > 0) {
748 ret = btrfs_previous_item(root, path, key.objectid, key.type);
749 if (ret < 0)
750 goto out;
753 while (1) {
754 l = path->nodes[0];
755 slot = path->slots[0];
756 if (slot >= btrfs_header_nritems(l)) {
757 ret = btrfs_next_leaf(root, path);
758 if (ret == 0)
759 continue;
760 if (ret < 0)
761 goto out;
763 break;
765 btrfs_item_key_to_cpu(l, &key, slot);
767 if (key.objectid < device->devid)
768 goto next;
770 if (key.objectid > device->devid)
771 break;
773 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
774 goto next;
776 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
777 extent_end = key.offset + btrfs_dev_extent_length(l,
778 dev_extent);
779 if (key.offset <= start && extent_end > end) {
780 *length = end - start + 1;
781 break;
782 } else if (key.offset <= start && extent_end > start)
783 *length += extent_end - start;
784 else if (key.offset > start && extent_end <= end)
785 *length += extent_end - key.offset;
786 else if (key.offset > start && key.offset <= end) {
787 *length += end - key.offset + 1;
788 break;
789 } else if (key.offset > end)
790 break;
792 next:
793 path->slots[0]++;
795 ret = 0;
796 out:
797 btrfs_free_path(path);
798 return ret;
802 * find_free_dev_extent - find free space in the specified device
803 * @trans: transaction handler
804 * @device: the device which we search the free space in
805 * @num_bytes: the size of the free space that we need
806 * @start: store the start of the free space.
807 * @len: the size of the free space. that we find, or the size of the max
808 * free space if we don't find suitable free space
810 * this uses a pretty simple search, the expectation is that it is
811 * called very infrequently and that a given device has a small number
812 * of extents
814 * @start is used to store the start of the free space if we find. But if we
815 * don't find suitable free space, it will be used to store the start position
816 * of the max free space.
818 * @len is used to store the size of the free space that we find.
819 * But if we don't find suitable free space, it is used to store the size of
820 * the max free space.
822 int find_free_dev_extent(struct btrfs_trans_handle *trans,
823 struct btrfs_device *device, u64 num_bytes,
824 u64 *start, u64 *len)
826 struct btrfs_key key;
827 struct btrfs_root *root = device->dev_root;
828 struct btrfs_dev_extent *dev_extent;
829 struct btrfs_path *path;
830 u64 hole_size;
831 u64 max_hole_start;
832 u64 max_hole_size;
833 u64 extent_end;
834 u64 search_start;
835 u64 search_end = device->total_bytes;
836 int ret;
837 int slot;
838 struct extent_buffer *l;
840 /* FIXME use last free of some kind */
842 /* we don't want to overwrite the superblock on the drive,
843 * so we make sure to start at an offset of at least 1MB
845 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
847 max_hole_start = search_start;
848 max_hole_size = 0;
850 if (search_start >= search_end) {
851 ret = -ENOSPC;
852 goto error;
855 path = btrfs_alloc_path();
856 if (!path) {
857 ret = -ENOMEM;
858 goto error;
860 path->reada = 2;
862 key.objectid = device->devid;
863 key.offset = search_start;
864 key.type = BTRFS_DEV_EXTENT_KEY;
866 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
867 if (ret < 0)
868 goto out;
869 if (ret > 0) {
870 ret = btrfs_previous_item(root, path, key.objectid, key.type);
871 if (ret < 0)
872 goto out;
875 while (1) {
876 l = path->nodes[0];
877 slot = path->slots[0];
878 if (slot >= btrfs_header_nritems(l)) {
879 ret = btrfs_next_leaf(root, path);
880 if (ret == 0)
881 continue;
882 if (ret < 0)
883 goto out;
885 break;
887 btrfs_item_key_to_cpu(l, &key, slot);
889 if (key.objectid < device->devid)
890 goto next;
892 if (key.objectid > device->devid)
893 break;
895 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
896 goto next;
898 if (key.offset > search_start) {
899 hole_size = key.offset - search_start;
901 if (hole_size > max_hole_size) {
902 max_hole_start = search_start;
903 max_hole_size = hole_size;
907 * If this free space is greater than which we need,
908 * it must be the max free space that we have found
909 * until now, so max_hole_start must point to the start
910 * of this free space and the length of this free space
911 * is stored in max_hole_size. Thus, we return
912 * max_hole_start and max_hole_size and go back to the
913 * caller.
915 if (hole_size >= num_bytes) {
916 ret = 0;
917 goto out;
921 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
922 extent_end = key.offset + btrfs_dev_extent_length(l,
923 dev_extent);
924 if (extent_end > search_start)
925 search_start = extent_end;
926 next:
927 path->slots[0]++;
928 cond_resched();
931 hole_size = search_end- search_start;
932 if (hole_size > max_hole_size) {
933 max_hole_start = search_start;
934 max_hole_size = hole_size;
937 /* See above. */
938 if (hole_size < num_bytes)
939 ret = -ENOSPC;
940 else
941 ret = 0;
943 out:
944 btrfs_free_path(path);
945 error:
946 *start = max_hole_start;
947 if (len)
948 *len = max_hole_size;
949 return ret;
952 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
953 struct btrfs_device *device,
954 u64 start)
956 int ret;
957 struct btrfs_path *path;
958 struct btrfs_root *root = device->dev_root;
959 struct btrfs_key key;
960 struct btrfs_key found_key;
961 struct extent_buffer *leaf = NULL;
962 struct btrfs_dev_extent *extent = NULL;
964 path = btrfs_alloc_path();
965 if (!path)
966 return -ENOMEM;
968 key.objectid = device->devid;
969 key.offset = start;
970 key.type = BTRFS_DEV_EXTENT_KEY;
972 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
973 if (ret > 0) {
974 ret = btrfs_previous_item(root, path, key.objectid,
975 BTRFS_DEV_EXTENT_KEY);
976 if (ret)
977 goto out;
978 leaf = path->nodes[0];
979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
980 extent = btrfs_item_ptr(leaf, path->slots[0],
981 struct btrfs_dev_extent);
982 BUG_ON(found_key.offset > start || found_key.offset +
983 btrfs_dev_extent_length(leaf, extent) < start);
984 } else if (ret == 0) {
985 leaf = path->nodes[0];
986 extent = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_dev_extent);
989 BUG_ON(ret);
991 if (device->bytes_used > 0)
992 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
993 ret = btrfs_del_item(trans, root, path);
995 out:
996 btrfs_free_path(path);
997 return ret;
1000 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1001 struct btrfs_device *device,
1002 u64 chunk_tree, u64 chunk_objectid,
1003 u64 chunk_offset, u64 start, u64 num_bytes)
1005 int ret;
1006 struct btrfs_path *path;
1007 struct btrfs_root *root = device->dev_root;
1008 struct btrfs_dev_extent *extent;
1009 struct extent_buffer *leaf;
1010 struct btrfs_key key;
1012 WARN_ON(!device->in_fs_metadata);
1013 path = btrfs_alloc_path();
1014 if (!path)
1015 return -ENOMEM;
1017 key.objectid = device->devid;
1018 key.offset = start;
1019 key.type = BTRFS_DEV_EXTENT_KEY;
1020 ret = btrfs_insert_empty_item(trans, root, path, &key,
1021 sizeof(*extent));
1022 BUG_ON(ret);
1024 leaf = path->nodes[0];
1025 extent = btrfs_item_ptr(leaf, path->slots[0],
1026 struct btrfs_dev_extent);
1027 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1028 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1029 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1031 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1032 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1033 BTRFS_UUID_SIZE);
1035 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1036 btrfs_mark_buffer_dirty(leaf);
1037 btrfs_free_path(path);
1038 return ret;
1041 static noinline int find_next_chunk(struct btrfs_root *root,
1042 u64 objectid, u64 *offset)
1044 struct btrfs_path *path;
1045 int ret;
1046 struct btrfs_key key;
1047 struct btrfs_chunk *chunk;
1048 struct btrfs_key found_key;
1050 path = btrfs_alloc_path();
1051 BUG_ON(!path);
1053 key.objectid = objectid;
1054 key.offset = (u64)-1;
1055 key.type = BTRFS_CHUNK_ITEM_KEY;
1057 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1058 if (ret < 0)
1059 goto error;
1061 BUG_ON(ret == 0);
1063 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1064 if (ret) {
1065 *offset = 0;
1066 } else {
1067 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1068 path->slots[0]);
1069 if (found_key.objectid != objectid)
1070 *offset = 0;
1071 else {
1072 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1073 struct btrfs_chunk);
1074 *offset = found_key.offset +
1075 btrfs_chunk_length(path->nodes[0], chunk);
1078 ret = 0;
1079 error:
1080 btrfs_free_path(path);
1081 return ret;
1084 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1086 int ret;
1087 struct btrfs_key key;
1088 struct btrfs_key found_key;
1089 struct btrfs_path *path;
1091 root = root->fs_info->chunk_root;
1093 path = btrfs_alloc_path();
1094 if (!path)
1095 return -ENOMEM;
1097 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1098 key.type = BTRFS_DEV_ITEM_KEY;
1099 key.offset = (u64)-1;
1101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1102 if (ret < 0)
1103 goto error;
1105 BUG_ON(ret == 0);
1107 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1108 BTRFS_DEV_ITEM_KEY);
1109 if (ret) {
1110 *objectid = 1;
1111 } else {
1112 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1113 path->slots[0]);
1114 *objectid = found_key.offset + 1;
1116 ret = 0;
1117 error:
1118 btrfs_free_path(path);
1119 return ret;
1123 * the device information is stored in the chunk root
1124 * the btrfs_device struct should be fully filled in
1126 int btrfs_add_device(struct btrfs_trans_handle *trans,
1127 struct btrfs_root *root,
1128 struct btrfs_device *device)
1130 int ret;
1131 struct btrfs_path *path;
1132 struct btrfs_dev_item *dev_item;
1133 struct extent_buffer *leaf;
1134 struct btrfs_key key;
1135 unsigned long ptr;
1137 root = root->fs_info->chunk_root;
1139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
1143 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1144 key.type = BTRFS_DEV_ITEM_KEY;
1145 key.offset = device->devid;
1147 ret = btrfs_insert_empty_item(trans, root, path, &key,
1148 sizeof(*dev_item));
1149 if (ret)
1150 goto out;
1152 leaf = path->nodes[0];
1153 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1155 btrfs_set_device_id(leaf, dev_item, device->devid);
1156 btrfs_set_device_generation(leaf, dev_item, 0);
1157 btrfs_set_device_type(leaf, dev_item, device->type);
1158 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1159 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1160 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1161 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1162 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1163 btrfs_set_device_group(leaf, dev_item, 0);
1164 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1165 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1166 btrfs_set_device_start_offset(leaf, dev_item, 0);
1168 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1169 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1170 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1171 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1172 btrfs_mark_buffer_dirty(leaf);
1174 ret = 0;
1175 out:
1176 btrfs_free_path(path);
1177 return ret;
1180 static int btrfs_rm_dev_item(struct btrfs_root *root,
1181 struct btrfs_device *device)
1183 int ret;
1184 struct btrfs_path *path;
1185 struct btrfs_key key;
1186 struct btrfs_trans_handle *trans;
1188 root = root->fs_info->chunk_root;
1190 path = btrfs_alloc_path();
1191 if (!path)
1192 return -ENOMEM;
1194 trans = btrfs_start_transaction(root, 0);
1195 if (IS_ERR(trans)) {
1196 btrfs_free_path(path);
1197 return PTR_ERR(trans);
1199 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1200 key.type = BTRFS_DEV_ITEM_KEY;
1201 key.offset = device->devid;
1202 lock_chunks(root);
1204 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1205 if (ret < 0)
1206 goto out;
1208 if (ret > 0) {
1209 ret = -ENOENT;
1210 goto out;
1213 ret = btrfs_del_item(trans, root, path);
1214 if (ret)
1215 goto out;
1216 out:
1217 btrfs_free_path(path);
1218 unlock_chunks(root);
1219 btrfs_commit_transaction(trans, root);
1220 return ret;
1223 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1225 struct btrfs_device *device;
1226 struct btrfs_device *next_device;
1227 struct block_device *bdev;
1228 struct buffer_head *bh = NULL;
1229 struct btrfs_super_block *disk_super;
1230 struct btrfs_fs_devices *cur_devices;
1231 u64 all_avail;
1232 u64 devid;
1233 u64 num_devices;
1234 u8 *dev_uuid;
1235 int ret = 0;
1236 bool clear_super = false;
1238 mutex_lock(&uuid_mutex);
1239 mutex_lock(&root->fs_info->volume_mutex);
1241 all_avail = root->fs_info->avail_data_alloc_bits |
1242 root->fs_info->avail_system_alloc_bits |
1243 root->fs_info->avail_metadata_alloc_bits;
1245 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1246 root->fs_info->fs_devices->num_devices <= 4) {
1247 printk(KERN_ERR "btrfs: unable to go below four devices "
1248 "on raid10\n");
1249 ret = -EINVAL;
1250 goto out;
1253 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1254 root->fs_info->fs_devices->num_devices <= 2) {
1255 printk(KERN_ERR "btrfs: unable to go below two "
1256 "devices on raid1\n");
1257 ret = -EINVAL;
1258 goto out;
1261 if (strcmp(device_path, "missing") == 0) {
1262 struct list_head *devices;
1263 struct btrfs_device *tmp;
1265 device = NULL;
1266 devices = &root->fs_info->fs_devices->devices;
1268 * It is safe to read the devices since the volume_mutex
1269 * is held.
1271 list_for_each_entry(tmp, devices, dev_list) {
1272 if (tmp->in_fs_metadata && !tmp->bdev) {
1273 device = tmp;
1274 break;
1277 bdev = NULL;
1278 bh = NULL;
1279 disk_super = NULL;
1280 if (!device) {
1281 printk(KERN_ERR "btrfs: no missing devices found to "
1282 "remove\n");
1283 goto out;
1285 } else {
1286 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1287 root->fs_info->bdev_holder);
1288 if (IS_ERR(bdev)) {
1289 ret = PTR_ERR(bdev);
1290 goto out;
1293 set_blocksize(bdev, 4096);
1294 bh = btrfs_read_dev_super(bdev);
1295 if (!bh) {
1296 ret = -EINVAL;
1297 goto error_close;
1299 disk_super = (struct btrfs_super_block *)bh->b_data;
1300 devid = btrfs_stack_device_id(&disk_super->dev_item);
1301 dev_uuid = disk_super->dev_item.uuid;
1302 device = btrfs_find_device(root, devid, dev_uuid,
1303 disk_super->fsid);
1304 if (!device) {
1305 ret = -ENOENT;
1306 goto error_brelse;
1310 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1311 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1312 "device\n");
1313 ret = -EINVAL;
1314 goto error_brelse;
1317 if (device->writeable) {
1318 lock_chunks(root);
1319 list_del_init(&device->dev_alloc_list);
1320 unlock_chunks(root);
1321 root->fs_info->fs_devices->rw_devices--;
1322 clear_super = true;
1325 ret = btrfs_shrink_device(device, 0);
1326 if (ret)
1327 goto error_undo;
1329 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1330 if (ret)
1331 goto error_undo;
1333 device->in_fs_metadata = 0;
1334 btrfs_scrub_cancel_dev(root, device);
1337 * the device list mutex makes sure that we don't change
1338 * the device list while someone else is writing out all
1339 * the device supers.
1342 cur_devices = device->fs_devices;
1343 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1344 list_del_rcu(&device->dev_list);
1346 device->fs_devices->num_devices--;
1348 if (device->missing)
1349 root->fs_info->fs_devices->missing_devices--;
1351 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1352 struct btrfs_device, dev_list);
1353 if (device->bdev == root->fs_info->sb->s_bdev)
1354 root->fs_info->sb->s_bdev = next_device->bdev;
1355 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1356 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1358 if (device->bdev)
1359 device->fs_devices->open_devices--;
1361 call_rcu(&device->rcu, free_device);
1362 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1364 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1365 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1367 if (cur_devices->open_devices == 0) {
1368 struct btrfs_fs_devices *fs_devices;
1369 fs_devices = root->fs_info->fs_devices;
1370 while (fs_devices) {
1371 if (fs_devices->seed == cur_devices)
1372 break;
1373 fs_devices = fs_devices->seed;
1375 fs_devices->seed = cur_devices->seed;
1376 cur_devices->seed = NULL;
1377 lock_chunks(root);
1378 __btrfs_close_devices(cur_devices);
1379 unlock_chunks(root);
1380 free_fs_devices(cur_devices);
1384 * at this point, the device is zero sized. We want to
1385 * remove it from the devices list and zero out the old super
1387 if (clear_super) {
1388 /* make sure this device isn't detected as part of
1389 * the FS anymore
1391 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1392 set_buffer_dirty(bh);
1393 sync_dirty_buffer(bh);
1396 ret = 0;
1398 error_brelse:
1399 brelse(bh);
1400 error_close:
1401 if (bdev)
1402 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1403 out:
1404 mutex_unlock(&root->fs_info->volume_mutex);
1405 mutex_unlock(&uuid_mutex);
1406 return ret;
1407 error_undo:
1408 if (device->writeable) {
1409 lock_chunks(root);
1410 list_add(&device->dev_alloc_list,
1411 &root->fs_info->fs_devices->alloc_list);
1412 unlock_chunks(root);
1413 root->fs_info->fs_devices->rw_devices++;
1415 goto error_brelse;
1419 * does all the dirty work required for changing file system's UUID.
1421 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1422 struct btrfs_root *root)
1424 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1425 struct btrfs_fs_devices *old_devices;
1426 struct btrfs_fs_devices *seed_devices;
1427 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1428 struct btrfs_device *device;
1429 u64 super_flags;
1431 BUG_ON(!mutex_is_locked(&uuid_mutex));
1432 if (!fs_devices->seeding)
1433 return -EINVAL;
1435 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1436 if (!seed_devices)
1437 return -ENOMEM;
1439 old_devices = clone_fs_devices(fs_devices);
1440 if (IS_ERR(old_devices)) {
1441 kfree(seed_devices);
1442 return PTR_ERR(old_devices);
1445 list_add(&old_devices->list, &fs_uuids);
1447 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1448 seed_devices->opened = 1;
1449 INIT_LIST_HEAD(&seed_devices->devices);
1450 INIT_LIST_HEAD(&seed_devices->alloc_list);
1451 mutex_init(&seed_devices->device_list_mutex);
1453 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1454 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1455 synchronize_rcu);
1456 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1458 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1459 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1460 device->fs_devices = seed_devices;
1463 fs_devices->seeding = 0;
1464 fs_devices->num_devices = 0;
1465 fs_devices->open_devices = 0;
1466 fs_devices->seed = seed_devices;
1468 generate_random_uuid(fs_devices->fsid);
1469 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1470 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1471 super_flags = btrfs_super_flags(disk_super) &
1472 ~BTRFS_SUPER_FLAG_SEEDING;
1473 btrfs_set_super_flags(disk_super, super_flags);
1475 return 0;
1479 * strore the expected generation for seed devices in device items.
1481 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root)
1484 struct btrfs_path *path;
1485 struct extent_buffer *leaf;
1486 struct btrfs_dev_item *dev_item;
1487 struct btrfs_device *device;
1488 struct btrfs_key key;
1489 u8 fs_uuid[BTRFS_UUID_SIZE];
1490 u8 dev_uuid[BTRFS_UUID_SIZE];
1491 u64 devid;
1492 int ret;
1494 path = btrfs_alloc_path();
1495 if (!path)
1496 return -ENOMEM;
1498 root = root->fs_info->chunk_root;
1499 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1500 key.offset = 0;
1501 key.type = BTRFS_DEV_ITEM_KEY;
1503 while (1) {
1504 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1505 if (ret < 0)
1506 goto error;
1508 leaf = path->nodes[0];
1509 next_slot:
1510 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1511 ret = btrfs_next_leaf(root, path);
1512 if (ret > 0)
1513 break;
1514 if (ret < 0)
1515 goto error;
1516 leaf = path->nodes[0];
1517 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1518 btrfs_release_path(path);
1519 continue;
1522 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1523 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1524 key.type != BTRFS_DEV_ITEM_KEY)
1525 break;
1527 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1528 struct btrfs_dev_item);
1529 devid = btrfs_device_id(leaf, dev_item);
1530 read_extent_buffer(leaf, dev_uuid,
1531 (unsigned long)btrfs_device_uuid(dev_item),
1532 BTRFS_UUID_SIZE);
1533 read_extent_buffer(leaf, fs_uuid,
1534 (unsigned long)btrfs_device_fsid(dev_item),
1535 BTRFS_UUID_SIZE);
1536 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1537 BUG_ON(!device);
1539 if (device->fs_devices->seeding) {
1540 btrfs_set_device_generation(leaf, dev_item,
1541 device->generation);
1542 btrfs_mark_buffer_dirty(leaf);
1545 path->slots[0]++;
1546 goto next_slot;
1548 ret = 0;
1549 error:
1550 btrfs_free_path(path);
1551 return ret;
1554 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1556 struct request_queue *q;
1557 struct btrfs_trans_handle *trans;
1558 struct btrfs_device *device;
1559 struct block_device *bdev;
1560 struct list_head *devices;
1561 struct super_block *sb = root->fs_info->sb;
1562 u64 total_bytes;
1563 int seeding_dev = 0;
1564 int ret = 0;
1566 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1567 return -EINVAL;
1569 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1570 root->fs_info->bdev_holder);
1571 if (IS_ERR(bdev))
1572 return PTR_ERR(bdev);
1574 if (root->fs_info->fs_devices->seeding) {
1575 seeding_dev = 1;
1576 down_write(&sb->s_umount);
1577 mutex_lock(&uuid_mutex);
1580 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1581 mutex_lock(&root->fs_info->volume_mutex);
1583 devices = &root->fs_info->fs_devices->devices;
1585 * we have the volume lock, so we don't need the extra
1586 * device list mutex while reading the list here.
1588 list_for_each_entry(device, devices, dev_list) {
1589 if (device->bdev == bdev) {
1590 ret = -EEXIST;
1591 goto error;
1595 device = kzalloc(sizeof(*device), GFP_NOFS);
1596 if (!device) {
1597 /* we can safely leave the fs_devices entry around */
1598 ret = -ENOMEM;
1599 goto error;
1602 device->name = kstrdup(device_path, GFP_NOFS);
1603 if (!device->name) {
1604 kfree(device);
1605 ret = -ENOMEM;
1606 goto error;
1609 ret = find_next_devid(root, &device->devid);
1610 if (ret) {
1611 kfree(device->name);
1612 kfree(device);
1613 goto error;
1616 trans = btrfs_start_transaction(root, 0);
1617 if (IS_ERR(trans)) {
1618 kfree(device->name);
1619 kfree(device);
1620 ret = PTR_ERR(trans);
1621 goto error;
1624 lock_chunks(root);
1626 q = bdev_get_queue(bdev);
1627 if (blk_queue_discard(q))
1628 device->can_discard = 1;
1629 device->writeable = 1;
1630 device->work.func = pending_bios_fn;
1631 generate_random_uuid(device->uuid);
1632 spin_lock_init(&device->io_lock);
1633 device->generation = trans->transid;
1634 device->io_width = root->sectorsize;
1635 device->io_align = root->sectorsize;
1636 device->sector_size = root->sectorsize;
1637 device->total_bytes = i_size_read(bdev->bd_inode);
1638 device->disk_total_bytes = device->total_bytes;
1639 device->dev_root = root->fs_info->dev_root;
1640 device->bdev = bdev;
1641 device->in_fs_metadata = 1;
1642 device->mode = FMODE_EXCL;
1643 set_blocksize(device->bdev, 4096);
1645 if (seeding_dev) {
1646 sb->s_flags &= ~MS_RDONLY;
1647 ret = btrfs_prepare_sprout(trans, root);
1648 BUG_ON(ret);
1651 device->fs_devices = root->fs_info->fs_devices;
1654 * we don't want write_supers to jump in here with our device
1655 * half setup
1657 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1658 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1659 list_add(&device->dev_alloc_list,
1660 &root->fs_info->fs_devices->alloc_list);
1661 root->fs_info->fs_devices->num_devices++;
1662 root->fs_info->fs_devices->open_devices++;
1663 root->fs_info->fs_devices->rw_devices++;
1664 if (device->can_discard)
1665 root->fs_info->fs_devices->num_can_discard++;
1666 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1668 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1669 root->fs_info->fs_devices->rotating = 1;
1671 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1672 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1673 total_bytes + device->total_bytes);
1675 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1676 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1677 total_bytes + 1);
1678 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1680 if (seeding_dev) {
1681 ret = init_first_rw_device(trans, root, device);
1682 BUG_ON(ret);
1683 ret = btrfs_finish_sprout(trans, root);
1684 BUG_ON(ret);
1685 } else {
1686 ret = btrfs_add_device(trans, root, device);
1690 * we've got more storage, clear any full flags on the space
1691 * infos
1693 btrfs_clear_space_info_full(root->fs_info);
1695 unlock_chunks(root);
1696 btrfs_commit_transaction(trans, root);
1698 if (seeding_dev) {
1699 mutex_unlock(&uuid_mutex);
1700 up_write(&sb->s_umount);
1702 ret = btrfs_relocate_sys_chunks(root);
1703 BUG_ON(ret);
1705 out:
1706 mutex_unlock(&root->fs_info->volume_mutex);
1707 return ret;
1708 error:
1709 blkdev_put(bdev, FMODE_EXCL);
1710 if (seeding_dev) {
1711 mutex_unlock(&uuid_mutex);
1712 up_write(&sb->s_umount);
1714 goto out;
1717 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1718 struct btrfs_device *device)
1720 int ret;
1721 struct btrfs_path *path;
1722 struct btrfs_root *root;
1723 struct btrfs_dev_item *dev_item;
1724 struct extent_buffer *leaf;
1725 struct btrfs_key key;
1727 root = device->dev_root->fs_info->chunk_root;
1729 path = btrfs_alloc_path();
1730 if (!path)
1731 return -ENOMEM;
1733 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1734 key.type = BTRFS_DEV_ITEM_KEY;
1735 key.offset = device->devid;
1737 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1738 if (ret < 0)
1739 goto out;
1741 if (ret > 0) {
1742 ret = -ENOENT;
1743 goto out;
1746 leaf = path->nodes[0];
1747 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1749 btrfs_set_device_id(leaf, dev_item, device->devid);
1750 btrfs_set_device_type(leaf, dev_item, device->type);
1751 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1752 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1753 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1754 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1755 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1756 btrfs_mark_buffer_dirty(leaf);
1758 out:
1759 btrfs_free_path(path);
1760 return ret;
1763 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1764 struct btrfs_device *device, u64 new_size)
1766 struct btrfs_super_block *super_copy =
1767 &device->dev_root->fs_info->super_copy;
1768 u64 old_total = btrfs_super_total_bytes(super_copy);
1769 u64 diff = new_size - device->total_bytes;
1771 if (!device->writeable)
1772 return -EACCES;
1773 if (new_size <= device->total_bytes)
1774 return -EINVAL;
1776 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1777 device->fs_devices->total_rw_bytes += diff;
1779 device->total_bytes = new_size;
1780 device->disk_total_bytes = new_size;
1781 btrfs_clear_space_info_full(device->dev_root->fs_info);
1783 return btrfs_update_device(trans, device);
1786 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1787 struct btrfs_device *device, u64 new_size)
1789 int ret;
1790 lock_chunks(device->dev_root);
1791 ret = __btrfs_grow_device(trans, device, new_size);
1792 unlock_chunks(device->dev_root);
1793 return ret;
1796 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1797 struct btrfs_root *root,
1798 u64 chunk_tree, u64 chunk_objectid,
1799 u64 chunk_offset)
1801 int ret;
1802 struct btrfs_path *path;
1803 struct btrfs_key key;
1805 root = root->fs_info->chunk_root;
1806 path = btrfs_alloc_path();
1807 if (!path)
1808 return -ENOMEM;
1810 key.objectid = chunk_objectid;
1811 key.offset = chunk_offset;
1812 key.type = BTRFS_CHUNK_ITEM_KEY;
1814 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1815 BUG_ON(ret);
1817 ret = btrfs_del_item(trans, root, path);
1819 btrfs_free_path(path);
1820 return ret;
1823 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1824 chunk_offset)
1826 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1827 struct btrfs_disk_key *disk_key;
1828 struct btrfs_chunk *chunk;
1829 u8 *ptr;
1830 int ret = 0;
1831 u32 num_stripes;
1832 u32 array_size;
1833 u32 len = 0;
1834 u32 cur;
1835 struct btrfs_key key;
1837 array_size = btrfs_super_sys_array_size(super_copy);
1839 ptr = super_copy->sys_chunk_array;
1840 cur = 0;
1842 while (cur < array_size) {
1843 disk_key = (struct btrfs_disk_key *)ptr;
1844 btrfs_disk_key_to_cpu(&key, disk_key);
1846 len = sizeof(*disk_key);
1848 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1849 chunk = (struct btrfs_chunk *)(ptr + len);
1850 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1851 len += btrfs_chunk_item_size(num_stripes);
1852 } else {
1853 ret = -EIO;
1854 break;
1856 if (key.objectid == chunk_objectid &&
1857 key.offset == chunk_offset) {
1858 memmove(ptr, ptr + len, array_size - (cur + len));
1859 array_size -= len;
1860 btrfs_set_super_sys_array_size(super_copy, array_size);
1861 } else {
1862 ptr += len;
1863 cur += len;
1866 return ret;
1869 static int btrfs_relocate_chunk(struct btrfs_root *root,
1870 u64 chunk_tree, u64 chunk_objectid,
1871 u64 chunk_offset)
1873 struct extent_map_tree *em_tree;
1874 struct btrfs_root *extent_root;
1875 struct btrfs_trans_handle *trans;
1876 struct extent_map *em;
1877 struct map_lookup *map;
1878 int ret;
1879 int i;
1881 root = root->fs_info->chunk_root;
1882 extent_root = root->fs_info->extent_root;
1883 em_tree = &root->fs_info->mapping_tree.map_tree;
1885 ret = btrfs_can_relocate(extent_root, chunk_offset);
1886 if (ret)
1887 return -ENOSPC;
1889 /* step one, relocate all the extents inside this chunk */
1890 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1891 if (ret)
1892 return ret;
1894 trans = btrfs_start_transaction(root, 0);
1895 BUG_ON(IS_ERR(trans));
1897 lock_chunks(root);
1900 * step two, delete the device extents and the
1901 * chunk tree entries
1903 read_lock(&em_tree->lock);
1904 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1905 read_unlock(&em_tree->lock);
1907 BUG_ON(em->start > chunk_offset ||
1908 em->start + em->len < chunk_offset);
1909 map = (struct map_lookup *)em->bdev;
1911 for (i = 0; i < map->num_stripes; i++) {
1912 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1913 map->stripes[i].physical);
1914 BUG_ON(ret);
1916 if (map->stripes[i].dev) {
1917 ret = btrfs_update_device(trans, map->stripes[i].dev);
1918 BUG_ON(ret);
1921 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1922 chunk_offset);
1924 BUG_ON(ret);
1926 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1928 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1929 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1930 BUG_ON(ret);
1933 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1934 BUG_ON(ret);
1936 write_lock(&em_tree->lock);
1937 remove_extent_mapping(em_tree, em);
1938 write_unlock(&em_tree->lock);
1940 kfree(map);
1941 em->bdev = NULL;
1943 /* once for the tree */
1944 free_extent_map(em);
1945 /* once for us */
1946 free_extent_map(em);
1948 unlock_chunks(root);
1949 btrfs_end_transaction(trans, root);
1950 return 0;
1953 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1955 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1956 struct btrfs_path *path;
1957 struct extent_buffer *leaf;
1958 struct btrfs_chunk *chunk;
1959 struct btrfs_key key;
1960 struct btrfs_key found_key;
1961 u64 chunk_tree = chunk_root->root_key.objectid;
1962 u64 chunk_type;
1963 bool retried = false;
1964 int failed = 0;
1965 int ret;
1967 path = btrfs_alloc_path();
1968 if (!path)
1969 return -ENOMEM;
1971 again:
1972 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1973 key.offset = (u64)-1;
1974 key.type = BTRFS_CHUNK_ITEM_KEY;
1976 while (1) {
1977 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1978 if (ret < 0)
1979 goto error;
1980 BUG_ON(ret == 0);
1982 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1983 key.type);
1984 if (ret < 0)
1985 goto error;
1986 if (ret > 0)
1987 break;
1989 leaf = path->nodes[0];
1990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1992 chunk = btrfs_item_ptr(leaf, path->slots[0],
1993 struct btrfs_chunk);
1994 chunk_type = btrfs_chunk_type(leaf, chunk);
1995 btrfs_release_path(path);
1997 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1998 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1999 found_key.objectid,
2000 found_key.offset);
2001 if (ret == -ENOSPC)
2002 failed++;
2003 else if (ret)
2004 BUG();
2007 if (found_key.offset == 0)
2008 break;
2009 key.offset = found_key.offset - 1;
2011 ret = 0;
2012 if (failed && !retried) {
2013 failed = 0;
2014 retried = true;
2015 goto again;
2016 } else if (failed && retried) {
2017 WARN_ON(1);
2018 ret = -ENOSPC;
2020 error:
2021 btrfs_free_path(path);
2022 return ret;
2025 static u64 div_factor(u64 num, int factor)
2027 if (factor == 10)
2028 return num;
2029 num *= factor;
2030 do_div(num, 10);
2031 return num;
2034 int btrfs_balance(struct btrfs_root *dev_root)
2036 int ret;
2037 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2038 struct btrfs_device *device;
2039 u64 old_size;
2040 u64 size_to_free;
2041 struct btrfs_path *path;
2042 struct btrfs_key key;
2043 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2044 struct btrfs_trans_handle *trans;
2045 struct btrfs_key found_key;
2047 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2048 return -EROFS;
2050 if (!capable(CAP_SYS_ADMIN))
2051 return -EPERM;
2053 mutex_lock(&dev_root->fs_info->volume_mutex);
2054 dev_root = dev_root->fs_info->dev_root;
2056 /* step one make some room on all the devices */
2057 list_for_each_entry(device, devices, dev_list) {
2058 old_size = device->total_bytes;
2059 size_to_free = div_factor(old_size, 1);
2060 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2061 if (!device->writeable ||
2062 device->total_bytes - device->bytes_used > size_to_free)
2063 continue;
2065 ret = btrfs_shrink_device(device, old_size - size_to_free);
2066 if (ret == -ENOSPC)
2067 break;
2068 BUG_ON(ret);
2070 trans = btrfs_start_transaction(dev_root, 0);
2071 BUG_ON(IS_ERR(trans));
2073 ret = btrfs_grow_device(trans, device, old_size);
2074 BUG_ON(ret);
2076 btrfs_end_transaction(trans, dev_root);
2079 /* step two, relocate all the chunks */
2080 path = btrfs_alloc_path();
2081 BUG_ON(!path);
2083 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2084 key.offset = (u64)-1;
2085 key.type = BTRFS_CHUNK_ITEM_KEY;
2087 while (1) {
2088 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2089 if (ret < 0)
2090 goto error;
2093 * this shouldn't happen, it means the last relocate
2094 * failed
2096 if (ret == 0)
2097 break;
2099 ret = btrfs_previous_item(chunk_root, path, 0,
2100 BTRFS_CHUNK_ITEM_KEY);
2101 if (ret)
2102 break;
2104 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2105 path->slots[0]);
2106 if (found_key.objectid != key.objectid)
2107 break;
2109 /* chunk zero is special */
2110 if (found_key.offset == 0)
2111 break;
2113 btrfs_release_path(path);
2114 ret = btrfs_relocate_chunk(chunk_root,
2115 chunk_root->root_key.objectid,
2116 found_key.objectid,
2117 found_key.offset);
2118 if (ret && ret != -ENOSPC)
2119 goto error;
2120 key.offset = found_key.offset - 1;
2122 ret = 0;
2123 error:
2124 btrfs_free_path(path);
2125 mutex_unlock(&dev_root->fs_info->volume_mutex);
2126 return ret;
2130 * shrinking a device means finding all of the device extents past
2131 * the new size, and then following the back refs to the chunks.
2132 * The chunk relocation code actually frees the device extent
2134 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2136 struct btrfs_trans_handle *trans;
2137 struct btrfs_root *root = device->dev_root;
2138 struct btrfs_dev_extent *dev_extent = NULL;
2139 struct btrfs_path *path;
2140 u64 length;
2141 u64 chunk_tree;
2142 u64 chunk_objectid;
2143 u64 chunk_offset;
2144 int ret;
2145 int slot;
2146 int failed = 0;
2147 bool retried = false;
2148 struct extent_buffer *l;
2149 struct btrfs_key key;
2150 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2151 u64 old_total = btrfs_super_total_bytes(super_copy);
2152 u64 old_size = device->total_bytes;
2153 u64 diff = device->total_bytes - new_size;
2155 if (new_size >= device->total_bytes)
2156 return -EINVAL;
2158 path = btrfs_alloc_path();
2159 if (!path)
2160 return -ENOMEM;
2162 path->reada = 2;
2164 lock_chunks(root);
2166 device->total_bytes = new_size;
2167 if (device->writeable)
2168 device->fs_devices->total_rw_bytes -= diff;
2169 unlock_chunks(root);
2171 again:
2172 key.objectid = device->devid;
2173 key.offset = (u64)-1;
2174 key.type = BTRFS_DEV_EXTENT_KEY;
2176 while (1) {
2177 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2178 if (ret < 0)
2179 goto done;
2181 ret = btrfs_previous_item(root, path, 0, key.type);
2182 if (ret < 0)
2183 goto done;
2184 if (ret) {
2185 ret = 0;
2186 btrfs_release_path(path);
2187 break;
2190 l = path->nodes[0];
2191 slot = path->slots[0];
2192 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2194 if (key.objectid != device->devid) {
2195 btrfs_release_path(path);
2196 break;
2199 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2200 length = btrfs_dev_extent_length(l, dev_extent);
2202 if (key.offset + length <= new_size) {
2203 btrfs_release_path(path);
2204 break;
2207 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2208 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2209 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2210 btrfs_release_path(path);
2212 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2213 chunk_offset);
2214 if (ret && ret != -ENOSPC)
2215 goto done;
2216 if (ret == -ENOSPC)
2217 failed++;
2218 key.offset -= 1;
2221 if (failed && !retried) {
2222 failed = 0;
2223 retried = true;
2224 goto again;
2225 } else if (failed && retried) {
2226 ret = -ENOSPC;
2227 lock_chunks(root);
2229 device->total_bytes = old_size;
2230 if (device->writeable)
2231 device->fs_devices->total_rw_bytes += diff;
2232 unlock_chunks(root);
2233 goto done;
2236 /* Shrinking succeeded, else we would be at "done". */
2237 trans = btrfs_start_transaction(root, 0);
2238 if (IS_ERR(trans)) {
2239 ret = PTR_ERR(trans);
2240 goto done;
2243 lock_chunks(root);
2245 device->disk_total_bytes = new_size;
2246 /* Now btrfs_update_device() will change the on-disk size. */
2247 ret = btrfs_update_device(trans, device);
2248 if (ret) {
2249 unlock_chunks(root);
2250 btrfs_end_transaction(trans, root);
2251 goto done;
2253 WARN_ON(diff > old_total);
2254 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2255 unlock_chunks(root);
2256 btrfs_end_transaction(trans, root);
2257 done:
2258 btrfs_free_path(path);
2259 return ret;
2262 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2263 struct btrfs_root *root,
2264 struct btrfs_key *key,
2265 struct btrfs_chunk *chunk, int item_size)
2267 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2268 struct btrfs_disk_key disk_key;
2269 u32 array_size;
2270 u8 *ptr;
2272 array_size = btrfs_super_sys_array_size(super_copy);
2273 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2274 return -EFBIG;
2276 ptr = super_copy->sys_chunk_array + array_size;
2277 btrfs_cpu_key_to_disk(&disk_key, key);
2278 memcpy(ptr, &disk_key, sizeof(disk_key));
2279 ptr += sizeof(disk_key);
2280 memcpy(ptr, chunk, item_size);
2281 item_size += sizeof(disk_key);
2282 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2283 return 0;
2287 * sort the devices in descending order by max_avail, total_avail
2289 static int btrfs_cmp_device_info(const void *a, const void *b)
2291 const struct btrfs_device_info *di_a = a;
2292 const struct btrfs_device_info *di_b = b;
2294 if (di_a->max_avail > di_b->max_avail)
2295 return -1;
2296 if (di_a->max_avail < di_b->max_avail)
2297 return 1;
2298 if (di_a->total_avail > di_b->total_avail)
2299 return -1;
2300 if (di_a->total_avail < di_b->total_avail)
2301 return 1;
2302 return 0;
2305 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2306 struct btrfs_root *extent_root,
2307 struct map_lookup **map_ret,
2308 u64 *num_bytes_out, u64 *stripe_size_out,
2309 u64 start, u64 type)
2311 struct btrfs_fs_info *info = extent_root->fs_info;
2312 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2313 struct list_head *cur;
2314 struct map_lookup *map = NULL;
2315 struct extent_map_tree *em_tree;
2316 struct extent_map *em;
2317 struct btrfs_device_info *devices_info = NULL;
2318 u64 total_avail;
2319 int num_stripes; /* total number of stripes to allocate */
2320 int sub_stripes; /* sub_stripes info for map */
2321 int dev_stripes; /* stripes per dev */
2322 int devs_max; /* max devs to use */
2323 int devs_min; /* min devs needed */
2324 int devs_increment; /* ndevs has to be a multiple of this */
2325 int ncopies; /* how many copies to data has */
2326 int ret;
2327 u64 max_stripe_size;
2328 u64 max_chunk_size;
2329 u64 stripe_size;
2330 u64 num_bytes;
2331 int ndevs;
2332 int i;
2333 int j;
2335 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2336 (type & BTRFS_BLOCK_GROUP_DUP)) {
2337 WARN_ON(1);
2338 type &= ~BTRFS_BLOCK_GROUP_DUP;
2341 if (list_empty(&fs_devices->alloc_list))
2342 return -ENOSPC;
2344 sub_stripes = 1;
2345 dev_stripes = 1;
2346 devs_increment = 1;
2347 ncopies = 1;
2348 devs_max = 0; /* 0 == as many as possible */
2349 devs_min = 1;
2352 * define the properties of each RAID type.
2353 * FIXME: move this to a global table and use it in all RAID
2354 * calculation code
2356 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2357 dev_stripes = 2;
2358 ncopies = 2;
2359 devs_max = 1;
2360 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2361 devs_min = 2;
2362 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2363 devs_increment = 2;
2364 ncopies = 2;
2365 devs_max = 2;
2366 devs_min = 2;
2367 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2368 sub_stripes = 2;
2369 devs_increment = 2;
2370 ncopies = 2;
2371 devs_min = 4;
2372 } else {
2373 devs_max = 1;
2376 if (type & BTRFS_BLOCK_GROUP_DATA) {
2377 max_stripe_size = 1024 * 1024 * 1024;
2378 max_chunk_size = 10 * max_stripe_size;
2379 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2380 max_stripe_size = 256 * 1024 * 1024;
2381 max_chunk_size = max_stripe_size;
2382 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2383 max_stripe_size = 8 * 1024 * 1024;
2384 max_chunk_size = 2 * max_stripe_size;
2385 } else {
2386 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2387 type);
2388 BUG_ON(1);
2391 /* we don't want a chunk larger than 10% of writeable space */
2392 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2393 max_chunk_size);
2395 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2396 GFP_NOFS);
2397 if (!devices_info)
2398 return -ENOMEM;
2400 cur = fs_devices->alloc_list.next;
2403 * in the first pass through the devices list, we gather information
2404 * about the available holes on each device.
2406 ndevs = 0;
2407 while (cur != &fs_devices->alloc_list) {
2408 struct btrfs_device *device;
2409 u64 max_avail;
2410 u64 dev_offset;
2412 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2414 cur = cur->next;
2416 if (!device->writeable) {
2417 printk(KERN_ERR
2418 "btrfs: read-only device in alloc_list\n");
2419 WARN_ON(1);
2420 continue;
2423 if (!device->in_fs_metadata)
2424 continue;
2426 if (device->total_bytes > device->bytes_used)
2427 total_avail = device->total_bytes - device->bytes_used;
2428 else
2429 total_avail = 0;
2430 /* avail is off by max(alloc_start, 1MB), but that is the same
2431 * for all devices, so it doesn't hurt the sorting later on
2434 ret = find_free_dev_extent(trans, device,
2435 max_stripe_size * dev_stripes,
2436 &dev_offset, &max_avail);
2437 if (ret && ret != -ENOSPC)
2438 goto error;
2440 if (ret == 0)
2441 max_avail = max_stripe_size * dev_stripes;
2443 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2444 continue;
2446 devices_info[ndevs].dev_offset = dev_offset;
2447 devices_info[ndevs].max_avail = max_avail;
2448 devices_info[ndevs].total_avail = total_avail;
2449 devices_info[ndevs].dev = device;
2450 ++ndevs;
2454 * now sort the devices by hole size / available space
2456 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2457 btrfs_cmp_device_info, NULL);
2459 /* round down to number of usable stripes */
2460 ndevs -= ndevs % devs_increment;
2462 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2463 ret = -ENOSPC;
2464 goto error;
2467 if (devs_max && ndevs > devs_max)
2468 ndevs = devs_max;
2470 * the primary goal is to maximize the number of stripes, so use as many
2471 * devices as possible, even if the stripes are not maximum sized.
2473 stripe_size = devices_info[ndevs-1].max_avail;
2474 num_stripes = ndevs * dev_stripes;
2476 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2477 stripe_size = max_chunk_size * ncopies;
2478 do_div(stripe_size, num_stripes);
2481 do_div(stripe_size, dev_stripes);
2482 do_div(stripe_size, BTRFS_STRIPE_LEN);
2483 stripe_size *= BTRFS_STRIPE_LEN;
2485 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2486 if (!map) {
2487 ret = -ENOMEM;
2488 goto error;
2490 map->num_stripes = num_stripes;
2492 for (i = 0; i < ndevs; ++i) {
2493 for (j = 0; j < dev_stripes; ++j) {
2494 int s = i * dev_stripes + j;
2495 map->stripes[s].dev = devices_info[i].dev;
2496 map->stripes[s].physical = devices_info[i].dev_offset +
2497 j * stripe_size;
2500 map->sector_size = extent_root->sectorsize;
2501 map->stripe_len = BTRFS_STRIPE_LEN;
2502 map->io_align = BTRFS_STRIPE_LEN;
2503 map->io_width = BTRFS_STRIPE_LEN;
2504 map->type = type;
2505 map->sub_stripes = sub_stripes;
2507 *map_ret = map;
2508 num_bytes = stripe_size * (num_stripes / ncopies);
2510 *stripe_size_out = stripe_size;
2511 *num_bytes_out = num_bytes;
2513 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
2515 em = alloc_extent_map();
2516 if (!em) {
2517 ret = -ENOMEM;
2518 goto error;
2520 em->bdev = (struct block_device *)map;
2521 em->start = start;
2522 em->len = num_bytes;
2523 em->block_start = 0;
2524 em->block_len = em->len;
2526 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2527 write_lock(&em_tree->lock);
2528 ret = add_extent_mapping(em_tree, em);
2529 write_unlock(&em_tree->lock);
2530 BUG_ON(ret);
2531 free_extent_map(em);
2533 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2534 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2535 start, num_bytes);
2536 BUG_ON(ret);
2538 for (i = 0; i < map->num_stripes; ++i) {
2539 struct btrfs_device *device;
2540 u64 dev_offset;
2542 device = map->stripes[i].dev;
2543 dev_offset = map->stripes[i].physical;
2545 ret = btrfs_alloc_dev_extent(trans, device,
2546 info->chunk_root->root_key.objectid,
2547 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2548 start, dev_offset, stripe_size);
2549 BUG_ON(ret);
2552 kfree(devices_info);
2553 return 0;
2555 error:
2556 kfree(map);
2557 kfree(devices_info);
2558 return ret;
2561 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2562 struct btrfs_root *extent_root,
2563 struct map_lookup *map, u64 chunk_offset,
2564 u64 chunk_size, u64 stripe_size)
2566 u64 dev_offset;
2567 struct btrfs_key key;
2568 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2569 struct btrfs_device *device;
2570 struct btrfs_chunk *chunk;
2571 struct btrfs_stripe *stripe;
2572 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2573 int index = 0;
2574 int ret;
2576 chunk = kzalloc(item_size, GFP_NOFS);
2577 if (!chunk)
2578 return -ENOMEM;
2580 index = 0;
2581 while (index < map->num_stripes) {
2582 device = map->stripes[index].dev;
2583 device->bytes_used += stripe_size;
2584 ret = btrfs_update_device(trans, device);
2585 BUG_ON(ret);
2586 index++;
2589 index = 0;
2590 stripe = &chunk->stripe;
2591 while (index < map->num_stripes) {
2592 device = map->stripes[index].dev;
2593 dev_offset = map->stripes[index].physical;
2595 btrfs_set_stack_stripe_devid(stripe, device->devid);
2596 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2597 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2598 stripe++;
2599 index++;
2602 btrfs_set_stack_chunk_length(chunk, chunk_size);
2603 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2604 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2605 btrfs_set_stack_chunk_type(chunk, map->type);
2606 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2607 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2608 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2609 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2610 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2612 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2613 key.type = BTRFS_CHUNK_ITEM_KEY;
2614 key.offset = chunk_offset;
2616 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2617 BUG_ON(ret);
2619 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2620 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2621 item_size);
2622 BUG_ON(ret);
2625 kfree(chunk);
2626 return 0;
2630 * Chunk allocation falls into two parts. The first part does works
2631 * that make the new allocated chunk useable, but not do any operation
2632 * that modifies the chunk tree. The second part does the works that
2633 * require modifying the chunk tree. This division is important for the
2634 * bootstrap process of adding storage to a seed btrfs.
2636 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2637 struct btrfs_root *extent_root, u64 type)
2639 u64 chunk_offset;
2640 u64 chunk_size;
2641 u64 stripe_size;
2642 struct map_lookup *map;
2643 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2644 int ret;
2646 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2647 &chunk_offset);
2648 if (ret)
2649 return ret;
2651 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2652 &stripe_size, chunk_offset, type);
2653 if (ret)
2654 return ret;
2656 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2657 chunk_size, stripe_size);
2658 BUG_ON(ret);
2659 return 0;
2662 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2663 struct btrfs_root *root,
2664 struct btrfs_device *device)
2666 u64 chunk_offset;
2667 u64 sys_chunk_offset;
2668 u64 chunk_size;
2669 u64 sys_chunk_size;
2670 u64 stripe_size;
2671 u64 sys_stripe_size;
2672 u64 alloc_profile;
2673 struct map_lookup *map;
2674 struct map_lookup *sys_map;
2675 struct btrfs_fs_info *fs_info = root->fs_info;
2676 struct btrfs_root *extent_root = fs_info->extent_root;
2677 int ret;
2679 ret = find_next_chunk(fs_info->chunk_root,
2680 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2681 BUG_ON(ret);
2683 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2684 (fs_info->metadata_alloc_profile &
2685 fs_info->avail_metadata_alloc_bits);
2686 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2688 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2689 &stripe_size, chunk_offset, alloc_profile);
2690 BUG_ON(ret);
2692 sys_chunk_offset = chunk_offset + chunk_size;
2694 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2695 (fs_info->system_alloc_profile &
2696 fs_info->avail_system_alloc_bits);
2697 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2699 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2700 &sys_chunk_size, &sys_stripe_size,
2701 sys_chunk_offset, alloc_profile);
2702 BUG_ON(ret);
2704 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2705 BUG_ON(ret);
2708 * Modifying chunk tree needs allocating new blocks from both
2709 * system block group and metadata block group. So we only can
2710 * do operations require modifying the chunk tree after both
2711 * block groups were created.
2713 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2714 chunk_size, stripe_size);
2715 BUG_ON(ret);
2717 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2718 sys_chunk_offset, sys_chunk_size,
2719 sys_stripe_size);
2720 BUG_ON(ret);
2721 return 0;
2724 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2726 struct extent_map *em;
2727 struct map_lookup *map;
2728 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2729 int readonly = 0;
2730 int i;
2732 read_lock(&map_tree->map_tree.lock);
2733 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2734 read_unlock(&map_tree->map_tree.lock);
2735 if (!em)
2736 return 1;
2738 if (btrfs_test_opt(root, DEGRADED)) {
2739 free_extent_map(em);
2740 return 0;
2743 map = (struct map_lookup *)em->bdev;
2744 for (i = 0; i < map->num_stripes; i++) {
2745 if (!map->stripes[i].dev->writeable) {
2746 readonly = 1;
2747 break;
2750 free_extent_map(em);
2751 return readonly;
2754 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2756 extent_map_tree_init(&tree->map_tree);
2759 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2761 struct extent_map *em;
2763 while (1) {
2764 write_lock(&tree->map_tree.lock);
2765 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2766 if (em)
2767 remove_extent_mapping(&tree->map_tree, em);
2768 write_unlock(&tree->map_tree.lock);
2769 if (!em)
2770 break;
2771 kfree(em->bdev);
2772 /* once for us */
2773 free_extent_map(em);
2774 /* once for the tree */
2775 free_extent_map(em);
2779 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2781 struct extent_map *em;
2782 struct map_lookup *map;
2783 struct extent_map_tree *em_tree = &map_tree->map_tree;
2784 int ret;
2786 read_lock(&em_tree->lock);
2787 em = lookup_extent_mapping(em_tree, logical, len);
2788 read_unlock(&em_tree->lock);
2789 BUG_ON(!em);
2791 BUG_ON(em->start > logical || em->start + em->len < logical);
2792 map = (struct map_lookup *)em->bdev;
2793 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2794 ret = map->num_stripes;
2795 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2796 ret = map->sub_stripes;
2797 else
2798 ret = 1;
2799 free_extent_map(em);
2800 return ret;
2803 static int find_live_mirror(struct map_lookup *map, int first, int num,
2804 int optimal)
2806 int i;
2807 if (map->stripes[optimal].dev->bdev)
2808 return optimal;
2809 for (i = first; i < first + num; i++) {
2810 if (map->stripes[i].dev->bdev)
2811 return i;
2813 /* we couldn't find one that doesn't fail. Just return something
2814 * and the io error handling code will clean up eventually
2816 return optimal;
2819 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2820 u64 logical, u64 *length,
2821 struct btrfs_multi_bio **multi_ret,
2822 int mirror_num)
2824 struct extent_map *em;
2825 struct map_lookup *map;
2826 struct extent_map_tree *em_tree = &map_tree->map_tree;
2827 u64 offset;
2828 u64 stripe_offset;
2829 u64 stripe_end_offset;
2830 u64 stripe_nr;
2831 u64 stripe_nr_orig;
2832 u64 stripe_nr_end;
2833 int stripes_allocated = 8;
2834 int stripes_required = 1;
2835 int stripe_index;
2836 int i;
2837 int num_stripes;
2838 int max_errors = 0;
2839 struct btrfs_multi_bio *multi = NULL;
2841 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2842 stripes_allocated = 1;
2843 again:
2844 if (multi_ret) {
2845 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2846 GFP_NOFS);
2847 if (!multi)
2848 return -ENOMEM;
2850 atomic_set(&multi->error, 0);
2853 read_lock(&em_tree->lock);
2854 em = lookup_extent_mapping(em_tree, logical, *length);
2855 read_unlock(&em_tree->lock);
2857 if (!em) {
2858 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2859 (unsigned long long)logical,
2860 (unsigned long long)*length);
2861 BUG();
2864 BUG_ON(em->start > logical || em->start + em->len < logical);
2865 map = (struct map_lookup *)em->bdev;
2866 offset = logical - em->start;
2868 if (mirror_num > map->num_stripes)
2869 mirror_num = 0;
2871 /* if our multi bio struct is too small, back off and try again */
2872 if (rw & REQ_WRITE) {
2873 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2874 BTRFS_BLOCK_GROUP_DUP)) {
2875 stripes_required = map->num_stripes;
2876 max_errors = 1;
2877 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2878 stripes_required = map->sub_stripes;
2879 max_errors = 1;
2882 if (rw & REQ_DISCARD) {
2883 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2884 BTRFS_BLOCK_GROUP_RAID1 |
2885 BTRFS_BLOCK_GROUP_DUP |
2886 BTRFS_BLOCK_GROUP_RAID10)) {
2887 stripes_required = map->num_stripes;
2890 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2891 stripes_allocated < stripes_required) {
2892 stripes_allocated = map->num_stripes;
2893 free_extent_map(em);
2894 kfree(multi);
2895 goto again;
2897 stripe_nr = offset;
2899 * stripe_nr counts the total number of stripes we have to stride
2900 * to get to this block
2902 do_div(stripe_nr, map->stripe_len);
2904 stripe_offset = stripe_nr * map->stripe_len;
2905 BUG_ON(offset < stripe_offset);
2907 /* stripe_offset is the offset of this block in its stripe*/
2908 stripe_offset = offset - stripe_offset;
2910 if (rw & REQ_DISCARD)
2911 *length = min_t(u64, em->len - offset, *length);
2912 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2913 BTRFS_BLOCK_GROUP_RAID1 |
2914 BTRFS_BLOCK_GROUP_RAID10 |
2915 BTRFS_BLOCK_GROUP_DUP)) {
2916 /* we limit the length of each bio to what fits in a stripe */
2917 *length = min_t(u64, em->len - offset,
2918 map->stripe_len - stripe_offset);
2919 } else {
2920 *length = em->len - offset;
2923 if (!multi_ret)
2924 goto out;
2926 num_stripes = 1;
2927 stripe_index = 0;
2928 stripe_nr_orig = stripe_nr;
2929 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2930 (~(map->stripe_len - 1));
2931 do_div(stripe_nr_end, map->stripe_len);
2932 stripe_end_offset = stripe_nr_end * map->stripe_len -
2933 (offset + *length);
2934 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2935 if (rw & REQ_DISCARD)
2936 num_stripes = min_t(u64, map->num_stripes,
2937 stripe_nr_end - stripe_nr_orig);
2938 stripe_index = do_div(stripe_nr, map->num_stripes);
2939 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2940 if (rw & (REQ_WRITE | REQ_DISCARD))
2941 num_stripes = map->num_stripes;
2942 else if (mirror_num)
2943 stripe_index = mirror_num - 1;
2944 else {
2945 stripe_index = find_live_mirror(map, 0,
2946 map->num_stripes,
2947 current->pid % map->num_stripes);
2950 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2951 if (rw & (REQ_WRITE | REQ_DISCARD))
2952 num_stripes = map->num_stripes;
2953 else if (mirror_num)
2954 stripe_index = mirror_num - 1;
2956 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2957 int factor = map->num_stripes / map->sub_stripes;
2959 stripe_index = do_div(stripe_nr, factor);
2960 stripe_index *= map->sub_stripes;
2962 if (rw & REQ_WRITE)
2963 num_stripes = map->sub_stripes;
2964 else if (rw & REQ_DISCARD)
2965 num_stripes = min_t(u64, map->sub_stripes *
2966 (stripe_nr_end - stripe_nr_orig),
2967 map->num_stripes);
2968 else if (mirror_num)
2969 stripe_index += mirror_num - 1;
2970 else {
2971 stripe_index = find_live_mirror(map, stripe_index,
2972 map->sub_stripes, stripe_index +
2973 current->pid % map->sub_stripes);
2975 } else {
2977 * after this do_div call, stripe_nr is the number of stripes
2978 * on this device we have to walk to find the data, and
2979 * stripe_index is the number of our device in the stripe array
2981 stripe_index = do_div(stripe_nr, map->num_stripes);
2983 BUG_ON(stripe_index >= map->num_stripes);
2985 if (rw & REQ_DISCARD) {
2986 for (i = 0; i < num_stripes; i++) {
2987 multi->stripes[i].physical =
2988 map->stripes[stripe_index].physical +
2989 stripe_offset + stripe_nr * map->stripe_len;
2990 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2992 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2993 u64 stripes;
2994 u32 last_stripe = 0;
2995 int j;
2997 div_u64_rem(stripe_nr_end - 1,
2998 map->num_stripes,
2999 &last_stripe);
3001 for (j = 0; j < map->num_stripes; j++) {
3002 u32 test;
3004 div_u64_rem(stripe_nr_end - 1 - j,
3005 map->num_stripes, &test);
3006 if (test == stripe_index)
3007 break;
3009 stripes = stripe_nr_end - 1 - j;
3010 do_div(stripes, map->num_stripes);
3011 multi->stripes[i].length = map->stripe_len *
3012 (stripes - stripe_nr + 1);
3014 if (i == 0) {
3015 multi->stripes[i].length -=
3016 stripe_offset;
3017 stripe_offset = 0;
3019 if (stripe_index == last_stripe)
3020 multi->stripes[i].length -=
3021 stripe_end_offset;
3022 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3023 u64 stripes;
3024 int j;
3025 int factor = map->num_stripes /
3026 map->sub_stripes;
3027 u32 last_stripe = 0;
3029 div_u64_rem(stripe_nr_end - 1,
3030 factor, &last_stripe);
3031 last_stripe *= map->sub_stripes;
3033 for (j = 0; j < factor; j++) {
3034 u32 test;
3036 div_u64_rem(stripe_nr_end - 1 - j,
3037 factor, &test);
3039 if (test ==
3040 stripe_index / map->sub_stripes)
3041 break;
3043 stripes = stripe_nr_end - 1 - j;
3044 do_div(stripes, factor);
3045 multi->stripes[i].length = map->stripe_len *
3046 (stripes - stripe_nr + 1);
3048 if (i < map->sub_stripes) {
3049 multi->stripes[i].length -=
3050 stripe_offset;
3051 if (i == map->sub_stripes - 1)
3052 stripe_offset = 0;
3054 if (stripe_index >= last_stripe &&
3055 stripe_index <= (last_stripe +
3056 map->sub_stripes - 1)) {
3057 multi->stripes[i].length -=
3058 stripe_end_offset;
3060 } else
3061 multi->stripes[i].length = *length;
3063 stripe_index++;
3064 if (stripe_index == map->num_stripes) {
3065 /* This could only happen for RAID0/10 */
3066 stripe_index = 0;
3067 stripe_nr++;
3070 } else {
3071 for (i = 0; i < num_stripes; i++) {
3072 multi->stripes[i].physical =
3073 map->stripes[stripe_index].physical +
3074 stripe_offset +
3075 stripe_nr * map->stripe_len;
3076 multi->stripes[i].dev =
3077 map->stripes[stripe_index].dev;
3078 stripe_index++;
3081 if (multi_ret) {
3082 *multi_ret = multi;
3083 multi->num_stripes = num_stripes;
3084 multi->max_errors = max_errors;
3086 out:
3087 free_extent_map(em);
3088 return 0;
3091 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3092 u64 logical, u64 *length,
3093 struct btrfs_multi_bio **multi_ret, int mirror_num)
3095 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3096 mirror_num);
3099 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3100 u64 chunk_start, u64 physical, u64 devid,
3101 u64 **logical, int *naddrs, int *stripe_len)
3103 struct extent_map_tree *em_tree = &map_tree->map_tree;
3104 struct extent_map *em;
3105 struct map_lookup *map;
3106 u64 *buf;
3107 u64 bytenr;
3108 u64 length;
3109 u64 stripe_nr;
3110 int i, j, nr = 0;
3112 read_lock(&em_tree->lock);
3113 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3114 read_unlock(&em_tree->lock);
3116 BUG_ON(!em || em->start != chunk_start);
3117 map = (struct map_lookup *)em->bdev;
3119 length = em->len;
3120 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3121 do_div(length, map->num_stripes / map->sub_stripes);
3122 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3123 do_div(length, map->num_stripes);
3125 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3126 BUG_ON(!buf);
3128 for (i = 0; i < map->num_stripes; i++) {
3129 if (devid && map->stripes[i].dev->devid != devid)
3130 continue;
3131 if (map->stripes[i].physical > physical ||
3132 map->stripes[i].physical + length <= physical)
3133 continue;
3135 stripe_nr = physical - map->stripes[i].physical;
3136 do_div(stripe_nr, map->stripe_len);
3138 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3139 stripe_nr = stripe_nr * map->num_stripes + i;
3140 do_div(stripe_nr, map->sub_stripes);
3141 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3142 stripe_nr = stripe_nr * map->num_stripes + i;
3144 bytenr = chunk_start + stripe_nr * map->stripe_len;
3145 WARN_ON(nr >= map->num_stripes);
3146 for (j = 0; j < nr; j++) {
3147 if (buf[j] == bytenr)
3148 break;
3150 if (j == nr) {
3151 WARN_ON(nr >= map->num_stripes);
3152 buf[nr++] = bytenr;
3156 *logical = buf;
3157 *naddrs = nr;
3158 *stripe_len = map->stripe_len;
3160 free_extent_map(em);
3161 return 0;
3164 static void end_bio_multi_stripe(struct bio *bio, int err)
3166 struct btrfs_multi_bio *multi = bio->bi_private;
3167 int is_orig_bio = 0;
3169 if (err)
3170 atomic_inc(&multi->error);
3172 if (bio == multi->orig_bio)
3173 is_orig_bio = 1;
3175 if (atomic_dec_and_test(&multi->stripes_pending)) {
3176 if (!is_orig_bio) {
3177 bio_put(bio);
3178 bio = multi->orig_bio;
3180 bio->bi_private = multi->private;
3181 bio->bi_end_io = multi->end_io;
3182 /* only send an error to the higher layers if it is
3183 * beyond the tolerance of the multi-bio
3185 if (atomic_read(&multi->error) > multi->max_errors) {
3186 err = -EIO;
3187 } else if (err) {
3189 * this bio is actually up to date, we didn't
3190 * go over the max number of errors
3192 set_bit(BIO_UPTODATE, &bio->bi_flags);
3193 err = 0;
3195 kfree(multi);
3197 bio_endio(bio, err);
3198 } else if (!is_orig_bio) {
3199 bio_put(bio);
3203 struct async_sched {
3204 struct bio *bio;
3205 int rw;
3206 struct btrfs_fs_info *info;
3207 struct btrfs_work work;
3211 * see run_scheduled_bios for a description of why bios are collected for
3212 * async submit.
3214 * This will add one bio to the pending list for a device and make sure
3215 * the work struct is scheduled.
3217 static noinline int schedule_bio(struct btrfs_root *root,
3218 struct btrfs_device *device,
3219 int rw, struct bio *bio)
3221 int should_queue = 1;
3222 struct btrfs_pending_bios *pending_bios;
3224 /* don't bother with additional async steps for reads, right now */
3225 if (!(rw & REQ_WRITE)) {
3226 bio_get(bio);
3227 submit_bio(rw, bio);
3228 bio_put(bio);
3229 return 0;
3233 * nr_async_bios allows us to reliably return congestion to the
3234 * higher layers. Otherwise, the async bio makes it appear we have
3235 * made progress against dirty pages when we've really just put it
3236 * on a queue for later
3238 atomic_inc(&root->fs_info->nr_async_bios);
3239 WARN_ON(bio->bi_next);
3240 bio->bi_next = NULL;
3241 bio->bi_rw |= rw;
3243 spin_lock(&device->io_lock);
3244 if (bio->bi_rw & REQ_SYNC)
3245 pending_bios = &device->pending_sync_bios;
3246 else
3247 pending_bios = &device->pending_bios;
3249 if (pending_bios->tail)
3250 pending_bios->tail->bi_next = bio;
3252 pending_bios->tail = bio;
3253 if (!pending_bios->head)
3254 pending_bios->head = bio;
3255 if (device->running_pending)
3256 should_queue = 0;
3258 spin_unlock(&device->io_lock);
3260 if (should_queue)
3261 btrfs_queue_worker(&root->fs_info->submit_workers,
3262 &device->work);
3263 return 0;
3266 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3267 int mirror_num, int async_submit)
3269 struct btrfs_mapping_tree *map_tree;
3270 struct btrfs_device *dev;
3271 struct bio *first_bio = bio;
3272 u64 logical = (u64)bio->bi_sector << 9;
3273 u64 length = 0;
3274 u64 map_length;
3275 struct btrfs_multi_bio *multi = NULL;
3276 int ret;
3277 int dev_nr = 0;
3278 int total_devs = 1;
3280 length = bio->bi_size;
3281 map_tree = &root->fs_info->mapping_tree;
3282 map_length = length;
3284 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3285 mirror_num);
3286 BUG_ON(ret);
3288 total_devs = multi->num_stripes;
3289 if (map_length < length) {
3290 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3291 "len %llu\n", (unsigned long long)logical,
3292 (unsigned long long)length,
3293 (unsigned long long)map_length);
3294 BUG();
3296 multi->end_io = first_bio->bi_end_io;
3297 multi->private = first_bio->bi_private;
3298 multi->orig_bio = first_bio;
3299 atomic_set(&multi->stripes_pending, multi->num_stripes);
3301 while (dev_nr < total_devs) {
3302 if (total_devs > 1) {
3303 if (dev_nr < total_devs - 1) {
3304 bio = bio_clone(first_bio, GFP_NOFS);
3305 BUG_ON(!bio);
3306 } else {
3307 bio = first_bio;
3309 bio->bi_private = multi;
3310 bio->bi_end_io = end_bio_multi_stripe;
3312 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3313 dev = multi->stripes[dev_nr].dev;
3314 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3315 bio->bi_bdev = dev->bdev;
3316 if (async_submit)
3317 schedule_bio(root, dev, rw, bio);
3318 else
3319 submit_bio(rw, bio);
3320 } else {
3321 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3322 bio->bi_sector = logical >> 9;
3323 bio_endio(bio, -EIO);
3325 dev_nr++;
3327 if (total_devs == 1)
3328 kfree(multi);
3329 return 0;
3332 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3333 u8 *uuid, u8 *fsid)
3335 struct btrfs_device *device;
3336 struct btrfs_fs_devices *cur_devices;
3338 cur_devices = root->fs_info->fs_devices;
3339 while (cur_devices) {
3340 if (!fsid ||
3341 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3342 device = __find_device(&cur_devices->devices,
3343 devid, uuid);
3344 if (device)
3345 return device;
3347 cur_devices = cur_devices->seed;
3349 return NULL;
3352 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3353 u64 devid, u8 *dev_uuid)
3355 struct btrfs_device *device;
3356 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3358 device = kzalloc(sizeof(*device), GFP_NOFS);
3359 if (!device)
3360 return NULL;
3361 list_add(&device->dev_list,
3362 &fs_devices->devices);
3363 device->dev_root = root->fs_info->dev_root;
3364 device->devid = devid;
3365 device->work.func = pending_bios_fn;
3366 device->fs_devices = fs_devices;
3367 device->missing = 1;
3368 fs_devices->num_devices++;
3369 fs_devices->missing_devices++;
3370 spin_lock_init(&device->io_lock);
3371 INIT_LIST_HEAD(&device->dev_alloc_list);
3372 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3373 return device;
3376 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3377 struct extent_buffer *leaf,
3378 struct btrfs_chunk *chunk)
3380 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3381 struct map_lookup *map;
3382 struct extent_map *em;
3383 u64 logical;
3384 u64 length;
3385 u64 devid;
3386 u8 uuid[BTRFS_UUID_SIZE];
3387 int num_stripes;
3388 int ret;
3389 int i;
3391 logical = key->offset;
3392 length = btrfs_chunk_length(leaf, chunk);
3394 read_lock(&map_tree->map_tree.lock);
3395 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3396 read_unlock(&map_tree->map_tree.lock);
3398 /* already mapped? */
3399 if (em && em->start <= logical && em->start + em->len > logical) {
3400 free_extent_map(em);
3401 return 0;
3402 } else if (em) {
3403 free_extent_map(em);
3406 em = alloc_extent_map();
3407 if (!em)
3408 return -ENOMEM;
3409 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3410 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3411 if (!map) {
3412 free_extent_map(em);
3413 return -ENOMEM;
3416 em->bdev = (struct block_device *)map;
3417 em->start = logical;
3418 em->len = length;
3419 em->block_start = 0;
3420 em->block_len = em->len;
3422 map->num_stripes = num_stripes;
3423 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3424 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3425 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3426 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3427 map->type = btrfs_chunk_type(leaf, chunk);
3428 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3429 for (i = 0; i < num_stripes; i++) {
3430 map->stripes[i].physical =
3431 btrfs_stripe_offset_nr(leaf, chunk, i);
3432 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3433 read_extent_buffer(leaf, uuid, (unsigned long)
3434 btrfs_stripe_dev_uuid_nr(chunk, i),
3435 BTRFS_UUID_SIZE);
3436 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3437 NULL);
3438 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3439 kfree(map);
3440 free_extent_map(em);
3441 return -EIO;
3443 if (!map->stripes[i].dev) {
3444 map->stripes[i].dev =
3445 add_missing_dev(root, devid, uuid);
3446 if (!map->stripes[i].dev) {
3447 kfree(map);
3448 free_extent_map(em);
3449 return -EIO;
3452 map->stripes[i].dev->in_fs_metadata = 1;
3455 write_lock(&map_tree->map_tree.lock);
3456 ret = add_extent_mapping(&map_tree->map_tree, em);
3457 write_unlock(&map_tree->map_tree.lock);
3458 BUG_ON(ret);
3459 free_extent_map(em);
3461 return 0;
3464 static int fill_device_from_item(struct extent_buffer *leaf,
3465 struct btrfs_dev_item *dev_item,
3466 struct btrfs_device *device)
3468 unsigned long ptr;
3470 device->devid = btrfs_device_id(leaf, dev_item);
3471 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3472 device->total_bytes = device->disk_total_bytes;
3473 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3474 device->type = btrfs_device_type(leaf, dev_item);
3475 device->io_align = btrfs_device_io_align(leaf, dev_item);
3476 device->io_width = btrfs_device_io_width(leaf, dev_item);
3477 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3479 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3480 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3482 return 0;
3485 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3487 struct btrfs_fs_devices *fs_devices;
3488 int ret;
3490 mutex_lock(&uuid_mutex);
3492 fs_devices = root->fs_info->fs_devices->seed;
3493 while (fs_devices) {
3494 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3495 ret = 0;
3496 goto out;
3498 fs_devices = fs_devices->seed;
3501 fs_devices = find_fsid(fsid);
3502 if (!fs_devices) {
3503 ret = -ENOENT;
3504 goto out;
3507 fs_devices = clone_fs_devices(fs_devices);
3508 if (IS_ERR(fs_devices)) {
3509 ret = PTR_ERR(fs_devices);
3510 goto out;
3513 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3514 root->fs_info->bdev_holder);
3515 if (ret)
3516 goto out;
3518 if (!fs_devices->seeding) {
3519 __btrfs_close_devices(fs_devices);
3520 free_fs_devices(fs_devices);
3521 ret = -EINVAL;
3522 goto out;
3525 fs_devices->seed = root->fs_info->fs_devices->seed;
3526 root->fs_info->fs_devices->seed = fs_devices;
3527 out:
3528 mutex_unlock(&uuid_mutex);
3529 return ret;
3532 static int read_one_dev(struct btrfs_root *root,
3533 struct extent_buffer *leaf,
3534 struct btrfs_dev_item *dev_item)
3536 struct btrfs_device *device;
3537 u64 devid;
3538 int ret;
3539 u8 fs_uuid[BTRFS_UUID_SIZE];
3540 u8 dev_uuid[BTRFS_UUID_SIZE];
3542 devid = btrfs_device_id(leaf, dev_item);
3543 read_extent_buffer(leaf, dev_uuid,
3544 (unsigned long)btrfs_device_uuid(dev_item),
3545 BTRFS_UUID_SIZE);
3546 read_extent_buffer(leaf, fs_uuid,
3547 (unsigned long)btrfs_device_fsid(dev_item),
3548 BTRFS_UUID_SIZE);
3550 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3551 ret = open_seed_devices(root, fs_uuid);
3552 if (ret && !btrfs_test_opt(root, DEGRADED))
3553 return ret;
3556 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3557 if (!device || !device->bdev) {
3558 if (!btrfs_test_opt(root, DEGRADED))
3559 return -EIO;
3561 if (!device) {
3562 printk(KERN_WARNING "warning devid %llu missing\n",
3563 (unsigned long long)devid);
3564 device = add_missing_dev(root, devid, dev_uuid);
3565 if (!device)
3566 return -ENOMEM;
3567 } else if (!device->missing) {
3569 * this happens when a device that was properly setup
3570 * in the device info lists suddenly goes bad.
3571 * device->bdev is NULL, and so we have to set
3572 * device->missing to one here
3574 root->fs_info->fs_devices->missing_devices++;
3575 device->missing = 1;
3579 if (device->fs_devices != root->fs_info->fs_devices) {
3580 BUG_ON(device->writeable);
3581 if (device->generation !=
3582 btrfs_device_generation(leaf, dev_item))
3583 return -EINVAL;
3586 fill_device_from_item(leaf, dev_item, device);
3587 device->dev_root = root->fs_info->dev_root;
3588 device->in_fs_metadata = 1;
3589 if (device->writeable)
3590 device->fs_devices->total_rw_bytes += device->total_bytes;
3591 ret = 0;
3592 return ret;
3595 int btrfs_read_sys_array(struct btrfs_root *root)
3597 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3598 struct extent_buffer *sb;
3599 struct btrfs_disk_key *disk_key;
3600 struct btrfs_chunk *chunk;
3601 u8 *ptr;
3602 unsigned long sb_ptr;
3603 int ret = 0;
3604 u32 num_stripes;
3605 u32 array_size;
3606 u32 len = 0;
3607 u32 cur;
3608 struct btrfs_key key;
3610 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3611 BTRFS_SUPER_INFO_SIZE);
3612 if (!sb)
3613 return -ENOMEM;
3614 btrfs_set_buffer_uptodate(sb);
3615 btrfs_set_buffer_lockdep_class(sb, 0);
3617 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3618 array_size = btrfs_super_sys_array_size(super_copy);
3620 ptr = super_copy->sys_chunk_array;
3621 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3622 cur = 0;
3624 while (cur < array_size) {
3625 disk_key = (struct btrfs_disk_key *)ptr;
3626 btrfs_disk_key_to_cpu(&key, disk_key);
3628 len = sizeof(*disk_key); ptr += len;
3629 sb_ptr += len;
3630 cur += len;
3632 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3633 chunk = (struct btrfs_chunk *)sb_ptr;
3634 ret = read_one_chunk(root, &key, sb, chunk);
3635 if (ret)
3636 break;
3637 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3638 len = btrfs_chunk_item_size(num_stripes);
3639 } else {
3640 ret = -EIO;
3641 break;
3643 ptr += len;
3644 sb_ptr += len;
3645 cur += len;
3647 free_extent_buffer(sb);
3648 return ret;
3651 int btrfs_read_chunk_tree(struct btrfs_root *root)
3653 struct btrfs_path *path;
3654 struct extent_buffer *leaf;
3655 struct btrfs_key key;
3656 struct btrfs_key found_key;
3657 int ret;
3658 int slot;
3660 root = root->fs_info->chunk_root;
3662 path = btrfs_alloc_path();
3663 if (!path)
3664 return -ENOMEM;
3666 /* first we search for all of the device items, and then we
3667 * read in all of the chunk items. This way we can create chunk
3668 * mappings that reference all of the devices that are afound
3670 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3671 key.offset = 0;
3672 key.type = 0;
3673 again:
3674 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3675 if (ret < 0)
3676 goto error;
3677 while (1) {
3678 leaf = path->nodes[0];
3679 slot = path->slots[0];
3680 if (slot >= btrfs_header_nritems(leaf)) {
3681 ret = btrfs_next_leaf(root, path);
3682 if (ret == 0)
3683 continue;
3684 if (ret < 0)
3685 goto error;
3686 break;
3688 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3689 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3690 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3691 break;
3692 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3693 struct btrfs_dev_item *dev_item;
3694 dev_item = btrfs_item_ptr(leaf, slot,
3695 struct btrfs_dev_item);
3696 ret = read_one_dev(root, leaf, dev_item);
3697 if (ret)
3698 goto error;
3700 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3701 struct btrfs_chunk *chunk;
3702 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3703 ret = read_one_chunk(root, &found_key, leaf, chunk);
3704 if (ret)
3705 goto error;
3707 path->slots[0]++;
3709 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3710 key.objectid = 0;
3711 btrfs_release_path(path);
3712 goto again;
3714 ret = 0;
3715 error:
3716 btrfs_free_path(path);
3717 return ret;