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 <asm/div64.h>
28 #include "extent_map.h"
30 #include "transaction.h"
31 #include "print-tree.h"
33 #include "async-thread.h"
43 struct btrfs_bio_stripe stripes
[];
46 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
47 struct btrfs_root
*root
,
48 struct btrfs_device
*device
);
49 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
51 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
52 (sizeof(struct btrfs_bio_stripe) * (n)))
54 static DEFINE_MUTEX(uuid_mutex
);
55 static LIST_HEAD(fs_uuids
);
57 void btrfs_lock_volumes(void)
59 mutex_lock(&uuid_mutex
);
62 void btrfs_unlock_volumes(void)
64 mutex_unlock(&uuid_mutex
);
67 static void lock_chunks(struct btrfs_root
*root
)
69 mutex_lock(&root
->fs_info
->chunk_mutex
);
72 static void unlock_chunks(struct btrfs_root
*root
)
74 mutex_unlock(&root
->fs_info
->chunk_mutex
);
77 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
79 struct btrfs_device
*device
;
80 WARN_ON(fs_devices
->opened
);
81 while (!list_empty(&fs_devices
->devices
)) {
82 device
= list_entry(fs_devices
->devices
.next
,
83 struct btrfs_device
, dev_list
);
84 list_del(&device
->dev_list
);
91 int btrfs_cleanup_fs_uuids(void)
93 struct btrfs_fs_devices
*fs_devices
;
95 while (!list_empty(&fs_uuids
)) {
96 fs_devices
= list_entry(fs_uuids
.next
,
97 struct btrfs_fs_devices
, list
);
98 list_del(&fs_devices
->list
);
99 free_fs_devices(fs_devices
);
104 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
107 struct btrfs_device
*dev
;
109 list_for_each_entry(dev
, head
, dev_list
) {
110 if (dev
->devid
== devid
&&
111 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
118 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
120 struct btrfs_fs_devices
*fs_devices
;
122 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
123 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
129 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
130 struct bio
*head
, struct bio
*tail
)
133 struct bio
*old_head
;
135 old_head
= pending_bios
->head
;
136 pending_bios
->head
= head
;
137 if (pending_bios
->tail
)
138 tail
->bi_next
= old_head
;
140 pending_bios
->tail
= tail
;
144 * we try to collect pending bios for a device so we don't get a large
145 * number of procs sending bios down to the same device. This greatly
146 * improves the schedulers ability to collect and merge the bios.
148 * But, it also turns into a long list of bios to process and that is sure
149 * to eventually make the worker thread block. The solution here is to
150 * make some progress and then put this work struct back at the end of
151 * the list if the block device is congested. This way, multiple devices
152 * can make progress from a single worker thread.
154 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
157 struct backing_dev_info
*bdi
;
158 struct btrfs_fs_info
*fs_info
;
159 struct btrfs_pending_bios
*pending_bios
;
163 unsigned long num_run
;
164 unsigned long num_sync_run
;
165 unsigned long batch_run
= 0;
167 unsigned long last_waited
= 0;
170 bdi
= blk_get_backing_dev_info(device
->bdev
);
171 fs_info
= device
->dev_root
->fs_info
;
172 limit
= btrfs_async_submit_limit(fs_info
);
173 limit
= limit
* 2 / 3;
175 /* we want to make sure that every time we switch from the sync
176 * list to the normal list, we unplug
181 spin_lock(&device
->io_lock
);
186 /* take all the bios off the list at once and process them
187 * later on (without the lock held). But, remember the
188 * tail and other pointers so the bios can be properly reinserted
189 * into the list if we hit congestion
191 if (!force_reg
&& device
->pending_sync_bios
.head
) {
192 pending_bios
= &device
->pending_sync_bios
;
195 pending_bios
= &device
->pending_bios
;
199 pending
= pending_bios
->head
;
200 tail
= pending_bios
->tail
;
201 WARN_ON(pending
&& !tail
);
204 * if pending was null this time around, no bios need processing
205 * at all and we can stop. Otherwise it'll loop back up again
206 * and do an additional check so no bios are missed.
208 * device->running_pending is used to synchronize with the
211 if (device
->pending_sync_bios
.head
== NULL
&&
212 device
->pending_bios
.head
== NULL
) {
214 device
->running_pending
= 0;
217 device
->running_pending
= 1;
220 pending_bios
->head
= NULL
;
221 pending_bios
->tail
= NULL
;
223 spin_unlock(&device
->io_lock
);
226 * if we're doing the regular priority list, make sure we unplug
227 * for any high prio bios we've sent down
229 if (pending_bios
== &device
->pending_bios
&& num_sync_run
> 0) {
231 blk_run_backing_dev(bdi
, NULL
);
237 /* we want to work on both lists, but do more bios on the
238 * sync list than the regular list
241 pending_bios
!= &device
->pending_sync_bios
&&
242 device
->pending_sync_bios
.head
) ||
243 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
244 device
->pending_bios
.head
)) {
245 spin_lock(&device
->io_lock
);
246 requeue_list(pending_bios
, pending
, tail
);
251 pending
= pending
->bi_next
;
253 atomic_dec(&fs_info
->nr_async_bios
);
255 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
256 waitqueue_active(&fs_info
->async_submit_wait
))
257 wake_up(&fs_info
->async_submit_wait
);
259 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
261 if (cur
->bi_rw
& REQ_SYNC
)
264 submit_bio(cur
->bi_rw
, cur
);
267 if (need_resched()) {
269 blk_run_backing_dev(bdi
, NULL
);
276 * we made progress, there is more work to do and the bdi
277 * is now congested. Back off and let other work structs
280 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
281 fs_info
->fs_devices
->open_devices
> 1) {
282 struct io_context
*ioc
;
284 ioc
= current
->io_context
;
287 * the main goal here is that we don't want to
288 * block if we're going to be able to submit
289 * more requests without blocking.
291 * This code does two great things, it pokes into
292 * the elevator code from a filesystem _and_
293 * it makes assumptions about how batching works.
295 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
296 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
298 ioc
->last_waited
== last_waited
)) {
300 * we want to go through our batch of
301 * requests and stop. So, we copy out
302 * the ioc->last_waited time and test
303 * against it before looping
305 last_waited
= ioc
->last_waited
;
306 if (need_resched()) {
308 blk_run_backing_dev(bdi
, NULL
);
315 spin_lock(&device
->io_lock
);
316 requeue_list(pending_bios
, pending
, tail
);
317 device
->running_pending
= 1;
319 spin_unlock(&device
->io_lock
);
320 btrfs_requeue_work(&device
->work
);
327 blk_run_backing_dev(bdi
, NULL
);
330 * IO has already been through a long path to get here. Checksumming,
331 * async helper threads, perhaps compression. We've done a pretty
332 * good job of collecting a batch of IO and should just unplug
333 * the device right away.
335 * This will help anyone who is waiting on the IO, they might have
336 * already unplugged, but managed to do so before the bio they
337 * cared about found its way down here.
339 blk_run_backing_dev(bdi
, NULL
);
345 spin_lock(&device
->io_lock
);
346 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
348 spin_unlock(&device
->io_lock
);
354 static void pending_bios_fn(struct btrfs_work
*work
)
356 struct btrfs_device
*device
;
358 device
= container_of(work
, struct btrfs_device
, work
);
359 run_scheduled_bios(device
);
362 static noinline
int device_list_add(const char *path
,
363 struct btrfs_super_block
*disk_super
,
364 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
366 struct btrfs_device
*device
;
367 struct btrfs_fs_devices
*fs_devices
;
368 u64 found_transid
= btrfs_super_generation(disk_super
);
371 fs_devices
= find_fsid(disk_super
->fsid
);
373 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
376 INIT_LIST_HEAD(&fs_devices
->devices
);
377 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
378 list_add(&fs_devices
->list
, &fs_uuids
);
379 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
380 fs_devices
->latest_devid
= devid
;
381 fs_devices
->latest_trans
= found_transid
;
382 mutex_init(&fs_devices
->device_list_mutex
);
385 device
= __find_device(&fs_devices
->devices
, devid
,
386 disk_super
->dev_item
.uuid
);
389 if (fs_devices
->opened
)
392 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
394 /* we can safely leave the fs_devices entry around */
397 device
->devid
= devid
;
398 device
->work
.func
= pending_bios_fn
;
399 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
401 device
->barriers
= 1;
402 spin_lock_init(&device
->io_lock
);
403 device
->name
= kstrdup(path
, GFP_NOFS
);
408 INIT_LIST_HEAD(&device
->dev_alloc_list
);
410 mutex_lock(&fs_devices
->device_list_mutex
);
411 list_add(&device
->dev_list
, &fs_devices
->devices
);
412 mutex_unlock(&fs_devices
->device_list_mutex
);
414 device
->fs_devices
= fs_devices
;
415 fs_devices
->num_devices
++;
416 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
417 name
= kstrdup(path
, GFP_NOFS
);
422 if (device
->missing
) {
423 fs_devices
->missing_devices
--;
428 if (found_transid
> fs_devices
->latest_trans
) {
429 fs_devices
->latest_devid
= devid
;
430 fs_devices
->latest_trans
= found_transid
;
432 *fs_devices_ret
= fs_devices
;
436 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
438 struct btrfs_fs_devices
*fs_devices
;
439 struct btrfs_device
*device
;
440 struct btrfs_device
*orig_dev
;
442 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
444 return ERR_PTR(-ENOMEM
);
446 INIT_LIST_HEAD(&fs_devices
->devices
);
447 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
448 INIT_LIST_HEAD(&fs_devices
->list
);
449 mutex_init(&fs_devices
->device_list_mutex
);
450 fs_devices
->latest_devid
= orig
->latest_devid
;
451 fs_devices
->latest_trans
= orig
->latest_trans
;
452 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
454 mutex_lock(&orig
->device_list_mutex
);
455 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
456 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
460 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
466 device
->devid
= orig_dev
->devid
;
467 device
->work
.func
= pending_bios_fn
;
468 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
469 device
->barriers
= 1;
470 spin_lock_init(&device
->io_lock
);
471 INIT_LIST_HEAD(&device
->dev_list
);
472 INIT_LIST_HEAD(&device
->dev_alloc_list
);
474 list_add(&device
->dev_list
, &fs_devices
->devices
);
475 device
->fs_devices
= fs_devices
;
476 fs_devices
->num_devices
++;
478 mutex_unlock(&orig
->device_list_mutex
);
481 mutex_unlock(&orig
->device_list_mutex
);
482 free_fs_devices(fs_devices
);
483 return ERR_PTR(-ENOMEM
);
486 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
488 struct btrfs_device
*device
, *next
;
490 mutex_lock(&uuid_mutex
);
492 mutex_lock(&fs_devices
->device_list_mutex
);
493 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
494 if (device
->in_fs_metadata
)
498 close_bdev_exclusive(device
->bdev
, device
->mode
);
500 fs_devices
->open_devices
--;
502 if (device
->writeable
) {
503 list_del_init(&device
->dev_alloc_list
);
504 device
->writeable
= 0;
505 fs_devices
->rw_devices
--;
507 list_del_init(&device
->dev_list
);
508 fs_devices
->num_devices
--;
512 mutex_unlock(&fs_devices
->device_list_mutex
);
514 if (fs_devices
->seed
) {
515 fs_devices
= fs_devices
->seed
;
519 mutex_unlock(&uuid_mutex
);
523 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
525 struct btrfs_device
*device
;
527 if (--fs_devices
->opened
> 0)
530 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
532 close_bdev_exclusive(device
->bdev
, device
->mode
);
533 fs_devices
->open_devices
--;
535 if (device
->writeable
) {
536 list_del_init(&device
->dev_alloc_list
);
537 fs_devices
->rw_devices
--;
541 device
->writeable
= 0;
542 device
->in_fs_metadata
= 0;
544 WARN_ON(fs_devices
->open_devices
);
545 WARN_ON(fs_devices
->rw_devices
);
546 fs_devices
->opened
= 0;
547 fs_devices
->seeding
= 0;
552 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
554 struct btrfs_fs_devices
*seed_devices
= NULL
;
557 mutex_lock(&uuid_mutex
);
558 ret
= __btrfs_close_devices(fs_devices
);
559 if (!fs_devices
->opened
) {
560 seed_devices
= fs_devices
->seed
;
561 fs_devices
->seed
= NULL
;
563 mutex_unlock(&uuid_mutex
);
565 while (seed_devices
) {
566 fs_devices
= seed_devices
;
567 seed_devices
= fs_devices
->seed
;
568 __btrfs_close_devices(fs_devices
);
569 free_fs_devices(fs_devices
);
574 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
575 fmode_t flags
, void *holder
)
577 struct block_device
*bdev
;
578 struct list_head
*head
= &fs_devices
->devices
;
579 struct btrfs_device
*device
;
580 struct block_device
*latest_bdev
= NULL
;
581 struct buffer_head
*bh
;
582 struct btrfs_super_block
*disk_super
;
583 u64 latest_devid
= 0;
584 u64 latest_transid
= 0;
589 list_for_each_entry(device
, head
, dev_list
) {
595 bdev
= open_bdev_exclusive(device
->name
, flags
, holder
);
597 printk(KERN_INFO
"open %s failed\n", device
->name
);
600 set_blocksize(bdev
, 4096);
602 bh
= btrfs_read_dev_super(bdev
);
606 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
607 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
608 if (devid
!= device
->devid
)
611 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
615 device
->generation
= btrfs_super_generation(disk_super
);
616 if (!latest_transid
|| device
->generation
> latest_transid
) {
617 latest_devid
= devid
;
618 latest_transid
= device
->generation
;
622 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
623 device
->writeable
= 0;
625 device
->writeable
= !bdev_read_only(bdev
);
630 device
->in_fs_metadata
= 0;
631 device
->mode
= flags
;
633 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
634 fs_devices
->rotating
= 1;
636 fs_devices
->open_devices
++;
637 if (device
->writeable
) {
638 fs_devices
->rw_devices
++;
639 list_add(&device
->dev_alloc_list
,
640 &fs_devices
->alloc_list
);
647 close_bdev_exclusive(bdev
, FMODE_READ
);
651 if (fs_devices
->open_devices
== 0) {
655 fs_devices
->seeding
= seeding
;
656 fs_devices
->opened
= 1;
657 fs_devices
->latest_bdev
= latest_bdev
;
658 fs_devices
->latest_devid
= latest_devid
;
659 fs_devices
->latest_trans
= latest_transid
;
660 fs_devices
->total_rw_bytes
= 0;
665 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
666 fmode_t flags
, void *holder
)
670 mutex_lock(&uuid_mutex
);
671 if (fs_devices
->opened
) {
672 fs_devices
->opened
++;
675 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
677 mutex_unlock(&uuid_mutex
);
681 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
682 struct btrfs_fs_devices
**fs_devices_ret
)
684 struct btrfs_super_block
*disk_super
;
685 struct block_device
*bdev
;
686 struct buffer_head
*bh
;
691 mutex_lock(&uuid_mutex
);
693 bdev
= open_bdev_exclusive(path
, flags
, holder
);
700 ret
= set_blocksize(bdev
, 4096);
703 bh
= btrfs_read_dev_super(bdev
);
708 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
709 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
710 transid
= btrfs_super_generation(disk_super
);
711 if (disk_super
->label
[0])
712 printk(KERN_INFO
"device label %s ", disk_super
->label
);
714 /* FIXME, make a readl uuid parser */
715 printk(KERN_INFO
"device fsid %llx-%llx ",
716 *(unsigned long long *)disk_super
->fsid
,
717 *(unsigned long long *)(disk_super
->fsid
+ 8));
719 printk(KERN_CONT
"devid %llu transid %llu %s\n",
720 (unsigned long long)devid
, (unsigned long long)transid
, path
);
721 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
725 close_bdev_exclusive(bdev
, flags
);
727 mutex_unlock(&uuid_mutex
);
732 * this uses a pretty simple search, the expectation is that it is
733 * called very infrequently and that a given device has a small number
736 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
737 struct btrfs_device
*device
, u64 num_bytes
,
738 u64
*start
, u64
*max_avail
)
740 struct btrfs_key key
;
741 struct btrfs_root
*root
= device
->dev_root
;
742 struct btrfs_dev_extent
*dev_extent
= NULL
;
743 struct btrfs_path
*path
;
746 u64 search_start
= 0;
747 u64 search_end
= device
->total_bytes
;
751 struct extent_buffer
*l
;
753 path
= btrfs_alloc_path();
759 /* FIXME use last free of some kind */
761 /* we don't want to overwrite the superblock on the drive,
762 * so we make sure to start at an offset of at least 1MB
764 search_start
= max((u64
)1024 * 1024, search_start
);
766 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
767 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
769 key
.objectid
= device
->devid
;
770 key
.offset
= search_start
;
771 key
.type
= BTRFS_DEV_EXTENT_KEY
;
772 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
776 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
783 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
786 slot
= path
->slots
[0];
787 if (slot
>= btrfs_header_nritems(l
)) {
788 ret
= btrfs_next_leaf(root
, path
);
795 if (search_start
>= search_end
) {
799 *start
= search_start
;
803 *start
= last_byte
> search_start
?
804 last_byte
: search_start
;
805 if (search_end
<= *start
) {
811 btrfs_item_key_to_cpu(l
, &key
, slot
);
813 if (key
.objectid
< device
->devid
)
816 if (key
.objectid
> device
->devid
)
819 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
821 if (last_byte
< search_start
)
822 last_byte
= search_start
;
823 hole_size
= key
.offset
- last_byte
;
825 if (hole_size
> *max_avail
)
826 *max_avail
= hole_size
;
828 if (key
.offset
> last_byte
&&
829 hole_size
>= num_bytes
) {
834 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
838 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
839 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
845 /* we have to make sure we didn't find an extent that has already
846 * been allocated by the map tree or the original allocation
848 BUG_ON(*start
< search_start
);
850 if (*start
+ num_bytes
> search_end
) {
854 /* check for pending inserts here */
858 btrfs_free_path(path
);
862 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
863 struct btrfs_device
*device
,
867 struct btrfs_path
*path
;
868 struct btrfs_root
*root
= device
->dev_root
;
869 struct btrfs_key key
;
870 struct btrfs_key found_key
;
871 struct extent_buffer
*leaf
= NULL
;
872 struct btrfs_dev_extent
*extent
= NULL
;
874 path
= btrfs_alloc_path();
878 key
.objectid
= device
->devid
;
880 key
.type
= BTRFS_DEV_EXTENT_KEY
;
882 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
884 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
885 BTRFS_DEV_EXTENT_KEY
);
887 leaf
= path
->nodes
[0];
888 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
889 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
890 struct btrfs_dev_extent
);
891 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
892 btrfs_dev_extent_length(leaf
, extent
) < start
);
894 } else if (ret
== 0) {
895 leaf
= path
->nodes
[0];
896 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
897 struct btrfs_dev_extent
);
901 if (device
->bytes_used
> 0)
902 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
903 ret
= btrfs_del_item(trans
, root
, path
);
906 btrfs_free_path(path
);
910 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
911 struct btrfs_device
*device
,
912 u64 chunk_tree
, u64 chunk_objectid
,
913 u64 chunk_offset
, u64 start
, u64 num_bytes
)
916 struct btrfs_path
*path
;
917 struct btrfs_root
*root
= device
->dev_root
;
918 struct btrfs_dev_extent
*extent
;
919 struct extent_buffer
*leaf
;
920 struct btrfs_key key
;
922 WARN_ON(!device
->in_fs_metadata
);
923 path
= btrfs_alloc_path();
927 key
.objectid
= device
->devid
;
929 key
.type
= BTRFS_DEV_EXTENT_KEY
;
930 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
934 leaf
= path
->nodes
[0];
935 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
936 struct btrfs_dev_extent
);
937 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
938 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
939 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
941 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
942 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
945 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
946 btrfs_mark_buffer_dirty(leaf
);
947 btrfs_free_path(path
);
951 static noinline
int find_next_chunk(struct btrfs_root
*root
,
952 u64 objectid
, u64
*offset
)
954 struct btrfs_path
*path
;
956 struct btrfs_key key
;
957 struct btrfs_chunk
*chunk
;
958 struct btrfs_key found_key
;
960 path
= btrfs_alloc_path();
963 key
.objectid
= objectid
;
964 key
.offset
= (u64
)-1;
965 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
967 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
973 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
977 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
979 if (found_key
.objectid
!= objectid
)
982 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
984 *offset
= found_key
.offset
+
985 btrfs_chunk_length(path
->nodes
[0], chunk
);
990 btrfs_free_path(path
);
994 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
997 struct btrfs_key key
;
998 struct btrfs_key found_key
;
999 struct btrfs_path
*path
;
1001 root
= root
->fs_info
->chunk_root
;
1003 path
= btrfs_alloc_path();
1007 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1008 key
.type
= BTRFS_DEV_ITEM_KEY
;
1009 key
.offset
= (u64
)-1;
1011 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1017 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1018 BTRFS_DEV_ITEM_KEY
);
1022 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1024 *objectid
= found_key
.offset
+ 1;
1028 btrfs_free_path(path
);
1033 * the device information is stored in the chunk root
1034 * the btrfs_device struct should be fully filled in
1036 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1037 struct btrfs_root
*root
,
1038 struct btrfs_device
*device
)
1041 struct btrfs_path
*path
;
1042 struct btrfs_dev_item
*dev_item
;
1043 struct extent_buffer
*leaf
;
1044 struct btrfs_key key
;
1047 root
= root
->fs_info
->chunk_root
;
1049 path
= btrfs_alloc_path();
1053 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1054 key
.type
= BTRFS_DEV_ITEM_KEY
;
1055 key
.offset
= device
->devid
;
1057 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1062 leaf
= path
->nodes
[0];
1063 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1065 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1066 btrfs_set_device_generation(leaf
, dev_item
, 0);
1067 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1068 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1069 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1070 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1071 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1072 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1073 btrfs_set_device_group(leaf
, dev_item
, 0);
1074 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1075 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1076 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1078 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1079 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1080 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1081 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1082 btrfs_mark_buffer_dirty(leaf
);
1086 btrfs_free_path(path
);
1090 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1091 struct btrfs_device
*device
)
1094 struct btrfs_path
*path
;
1095 struct btrfs_key key
;
1096 struct btrfs_trans_handle
*trans
;
1098 root
= root
->fs_info
->chunk_root
;
1100 path
= btrfs_alloc_path();
1104 trans
= btrfs_start_transaction(root
, 0);
1105 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1106 key
.type
= BTRFS_DEV_ITEM_KEY
;
1107 key
.offset
= device
->devid
;
1110 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1119 ret
= btrfs_del_item(trans
, root
, path
);
1123 btrfs_free_path(path
);
1124 unlock_chunks(root
);
1125 btrfs_commit_transaction(trans
, root
);
1129 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1131 struct btrfs_device
*device
;
1132 struct btrfs_device
*next_device
;
1133 struct block_device
*bdev
;
1134 struct buffer_head
*bh
= NULL
;
1135 struct btrfs_super_block
*disk_super
;
1142 mutex_lock(&uuid_mutex
);
1143 mutex_lock(&root
->fs_info
->volume_mutex
);
1145 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1146 root
->fs_info
->avail_system_alloc_bits
|
1147 root
->fs_info
->avail_metadata_alloc_bits
;
1149 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1150 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1151 printk(KERN_ERR
"btrfs: unable to go below four devices "
1157 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1158 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1159 printk(KERN_ERR
"btrfs: unable to go below two "
1160 "devices on raid1\n");
1165 if (strcmp(device_path
, "missing") == 0) {
1166 struct list_head
*devices
;
1167 struct btrfs_device
*tmp
;
1170 devices
= &root
->fs_info
->fs_devices
->devices
;
1171 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1172 list_for_each_entry(tmp
, devices
, dev_list
) {
1173 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1178 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1183 printk(KERN_ERR
"btrfs: no missing devices found to "
1188 bdev
= open_bdev_exclusive(device_path
, FMODE_READ
,
1189 root
->fs_info
->bdev_holder
);
1191 ret
= PTR_ERR(bdev
);
1195 set_blocksize(bdev
, 4096);
1196 bh
= btrfs_read_dev_super(bdev
);
1201 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1202 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1203 dev_uuid
= disk_super
->dev_item
.uuid
;
1204 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1212 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1213 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1219 if (device
->writeable
) {
1220 list_del_init(&device
->dev_alloc_list
);
1221 root
->fs_info
->fs_devices
->rw_devices
--;
1224 ret
= btrfs_shrink_device(device
, 0);
1228 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1232 device
->in_fs_metadata
= 0;
1235 * the device list mutex makes sure that we don't change
1236 * the device list while someone else is writing out all
1237 * the device supers.
1239 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1240 list_del_init(&device
->dev_list
);
1241 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1243 device
->fs_devices
->num_devices
--;
1245 if (device
->missing
)
1246 root
->fs_info
->fs_devices
->missing_devices
--;
1248 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1249 struct btrfs_device
, dev_list
);
1250 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1251 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1252 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1253 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1256 close_bdev_exclusive(device
->bdev
, device
->mode
);
1257 device
->bdev
= NULL
;
1258 device
->fs_devices
->open_devices
--;
1261 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1262 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1264 if (device
->fs_devices
->open_devices
== 0) {
1265 struct btrfs_fs_devices
*fs_devices
;
1266 fs_devices
= root
->fs_info
->fs_devices
;
1267 while (fs_devices
) {
1268 if (fs_devices
->seed
== device
->fs_devices
)
1270 fs_devices
= fs_devices
->seed
;
1272 fs_devices
->seed
= device
->fs_devices
->seed
;
1273 device
->fs_devices
->seed
= NULL
;
1274 __btrfs_close_devices(device
->fs_devices
);
1275 free_fs_devices(device
->fs_devices
);
1279 * at this point, the device is zero sized. We want to
1280 * remove it from the devices list and zero out the old super
1282 if (device
->writeable
) {
1283 /* make sure this device isn't detected as part of
1286 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1287 set_buffer_dirty(bh
);
1288 sync_dirty_buffer(bh
);
1291 kfree(device
->name
);
1299 close_bdev_exclusive(bdev
, FMODE_READ
);
1301 mutex_unlock(&root
->fs_info
->volume_mutex
);
1302 mutex_unlock(&uuid_mutex
);
1307 * does all the dirty work required for changing file system's UUID.
1309 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1310 struct btrfs_root
*root
)
1312 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1313 struct btrfs_fs_devices
*old_devices
;
1314 struct btrfs_fs_devices
*seed_devices
;
1315 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1316 struct btrfs_device
*device
;
1319 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1320 if (!fs_devices
->seeding
)
1323 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1327 old_devices
= clone_fs_devices(fs_devices
);
1328 if (IS_ERR(old_devices
)) {
1329 kfree(seed_devices
);
1330 return PTR_ERR(old_devices
);
1333 list_add(&old_devices
->list
, &fs_uuids
);
1335 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1336 seed_devices
->opened
= 1;
1337 INIT_LIST_HEAD(&seed_devices
->devices
);
1338 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1339 mutex_init(&seed_devices
->device_list_mutex
);
1340 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1341 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1342 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1343 device
->fs_devices
= seed_devices
;
1346 fs_devices
->seeding
= 0;
1347 fs_devices
->num_devices
= 0;
1348 fs_devices
->open_devices
= 0;
1349 fs_devices
->seed
= seed_devices
;
1351 generate_random_uuid(fs_devices
->fsid
);
1352 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1353 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1354 super_flags
= btrfs_super_flags(disk_super
) &
1355 ~BTRFS_SUPER_FLAG_SEEDING
;
1356 btrfs_set_super_flags(disk_super
, super_flags
);
1362 * strore the expected generation for seed devices in device items.
1364 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1365 struct btrfs_root
*root
)
1367 struct btrfs_path
*path
;
1368 struct extent_buffer
*leaf
;
1369 struct btrfs_dev_item
*dev_item
;
1370 struct btrfs_device
*device
;
1371 struct btrfs_key key
;
1372 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1373 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1377 path
= btrfs_alloc_path();
1381 root
= root
->fs_info
->chunk_root
;
1382 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1384 key
.type
= BTRFS_DEV_ITEM_KEY
;
1387 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1391 leaf
= path
->nodes
[0];
1393 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1394 ret
= btrfs_next_leaf(root
, path
);
1399 leaf
= path
->nodes
[0];
1400 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1401 btrfs_release_path(root
, path
);
1405 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1406 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1407 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1410 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1411 struct btrfs_dev_item
);
1412 devid
= btrfs_device_id(leaf
, dev_item
);
1413 read_extent_buffer(leaf
, dev_uuid
,
1414 (unsigned long)btrfs_device_uuid(dev_item
),
1416 read_extent_buffer(leaf
, fs_uuid
,
1417 (unsigned long)btrfs_device_fsid(dev_item
),
1419 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1422 if (device
->fs_devices
->seeding
) {
1423 btrfs_set_device_generation(leaf
, dev_item
,
1424 device
->generation
);
1425 btrfs_mark_buffer_dirty(leaf
);
1433 btrfs_free_path(path
);
1437 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1439 struct btrfs_trans_handle
*trans
;
1440 struct btrfs_device
*device
;
1441 struct block_device
*bdev
;
1442 struct list_head
*devices
;
1443 struct super_block
*sb
= root
->fs_info
->sb
;
1445 int seeding_dev
= 0;
1448 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1451 bdev
= open_bdev_exclusive(device_path
, 0, root
->fs_info
->bdev_holder
);
1453 return PTR_ERR(bdev
);
1455 if (root
->fs_info
->fs_devices
->seeding
) {
1457 down_write(&sb
->s_umount
);
1458 mutex_lock(&uuid_mutex
);
1461 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1462 mutex_lock(&root
->fs_info
->volume_mutex
);
1464 devices
= &root
->fs_info
->fs_devices
->devices
;
1466 * we have the volume lock, so we don't need the extra
1467 * device list mutex while reading the list here.
1469 list_for_each_entry(device
, devices
, dev_list
) {
1470 if (device
->bdev
== bdev
) {
1476 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1478 /* we can safely leave the fs_devices entry around */
1483 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1484 if (!device
->name
) {
1490 ret
= find_next_devid(root
, &device
->devid
);
1496 trans
= btrfs_start_transaction(root
, 0);
1499 device
->barriers
= 1;
1500 device
->writeable
= 1;
1501 device
->work
.func
= pending_bios_fn
;
1502 generate_random_uuid(device
->uuid
);
1503 spin_lock_init(&device
->io_lock
);
1504 device
->generation
= trans
->transid
;
1505 device
->io_width
= root
->sectorsize
;
1506 device
->io_align
= root
->sectorsize
;
1507 device
->sector_size
= root
->sectorsize
;
1508 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1509 device
->disk_total_bytes
= device
->total_bytes
;
1510 device
->dev_root
= root
->fs_info
->dev_root
;
1511 device
->bdev
= bdev
;
1512 device
->in_fs_metadata
= 1;
1514 set_blocksize(device
->bdev
, 4096);
1517 sb
->s_flags
&= ~MS_RDONLY
;
1518 ret
= btrfs_prepare_sprout(trans
, root
);
1522 device
->fs_devices
= root
->fs_info
->fs_devices
;
1525 * we don't want write_supers to jump in here with our device
1528 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1529 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1530 list_add(&device
->dev_alloc_list
,
1531 &root
->fs_info
->fs_devices
->alloc_list
);
1532 root
->fs_info
->fs_devices
->num_devices
++;
1533 root
->fs_info
->fs_devices
->open_devices
++;
1534 root
->fs_info
->fs_devices
->rw_devices
++;
1535 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1537 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1538 root
->fs_info
->fs_devices
->rotating
= 1;
1540 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1541 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1542 total_bytes
+ device
->total_bytes
);
1544 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1545 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1547 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1550 ret
= init_first_rw_device(trans
, root
, device
);
1552 ret
= btrfs_finish_sprout(trans
, root
);
1555 ret
= btrfs_add_device(trans
, root
, device
);
1559 * we've got more storage, clear any full flags on the space
1562 btrfs_clear_space_info_full(root
->fs_info
);
1564 unlock_chunks(root
);
1565 btrfs_commit_transaction(trans
, root
);
1568 mutex_unlock(&uuid_mutex
);
1569 up_write(&sb
->s_umount
);
1571 ret
= btrfs_relocate_sys_chunks(root
);
1575 mutex_unlock(&root
->fs_info
->volume_mutex
);
1578 close_bdev_exclusive(bdev
, 0);
1580 mutex_unlock(&uuid_mutex
);
1581 up_write(&sb
->s_umount
);
1586 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1587 struct btrfs_device
*device
)
1590 struct btrfs_path
*path
;
1591 struct btrfs_root
*root
;
1592 struct btrfs_dev_item
*dev_item
;
1593 struct extent_buffer
*leaf
;
1594 struct btrfs_key key
;
1596 root
= device
->dev_root
->fs_info
->chunk_root
;
1598 path
= btrfs_alloc_path();
1602 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1603 key
.type
= BTRFS_DEV_ITEM_KEY
;
1604 key
.offset
= device
->devid
;
1606 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1615 leaf
= path
->nodes
[0];
1616 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1618 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1619 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1620 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1621 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1622 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1623 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1624 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1625 btrfs_mark_buffer_dirty(leaf
);
1628 btrfs_free_path(path
);
1632 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1633 struct btrfs_device
*device
, u64 new_size
)
1635 struct btrfs_super_block
*super_copy
=
1636 &device
->dev_root
->fs_info
->super_copy
;
1637 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1638 u64 diff
= new_size
- device
->total_bytes
;
1640 if (!device
->writeable
)
1642 if (new_size
<= device
->total_bytes
)
1645 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1646 device
->fs_devices
->total_rw_bytes
+= diff
;
1648 device
->total_bytes
= new_size
;
1649 device
->disk_total_bytes
= new_size
;
1650 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1652 return btrfs_update_device(trans
, device
);
1655 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1656 struct btrfs_device
*device
, u64 new_size
)
1659 lock_chunks(device
->dev_root
);
1660 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1661 unlock_chunks(device
->dev_root
);
1665 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1666 struct btrfs_root
*root
,
1667 u64 chunk_tree
, u64 chunk_objectid
,
1671 struct btrfs_path
*path
;
1672 struct btrfs_key key
;
1674 root
= root
->fs_info
->chunk_root
;
1675 path
= btrfs_alloc_path();
1679 key
.objectid
= chunk_objectid
;
1680 key
.offset
= chunk_offset
;
1681 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1683 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1686 ret
= btrfs_del_item(trans
, root
, path
);
1689 btrfs_free_path(path
);
1693 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1696 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1697 struct btrfs_disk_key
*disk_key
;
1698 struct btrfs_chunk
*chunk
;
1705 struct btrfs_key key
;
1707 array_size
= btrfs_super_sys_array_size(super_copy
);
1709 ptr
= super_copy
->sys_chunk_array
;
1712 while (cur
< array_size
) {
1713 disk_key
= (struct btrfs_disk_key
*)ptr
;
1714 btrfs_disk_key_to_cpu(&key
, disk_key
);
1716 len
= sizeof(*disk_key
);
1718 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1719 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1720 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1721 len
+= btrfs_chunk_item_size(num_stripes
);
1726 if (key
.objectid
== chunk_objectid
&&
1727 key
.offset
== chunk_offset
) {
1728 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1730 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1739 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1740 u64 chunk_tree
, u64 chunk_objectid
,
1743 struct extent_map_tree
*em_tree
;
1744 struct btrfs_root
*extent_root
;
1745 struct btrfs_trans_handle
*trans
;
1746 struct extent_map
*em
;
1747 struct map_lookup
*map
;
1751 root
= root
->fs_info
->chunk_root
;
1752 extent_root
= root
->fs_info
->extent_root
;
1753 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1755 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1759 /* step one, relocate all the extents inside this chunk */
1760 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1764 trans
= btrfs_start_transaction(root
, 0);
1770 * step two, delete the device extents and the
1771 * chunk tree entries
1773 read_lock(&em_tree
->lock
);
1774 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1775 read_unlock(&em_tree
->lock
);
1777 BUG_ON(em
->start
> chunk_offset
||
1778 em
->start
+ em
->len
< chunk_offset
);
1779 map
= (struct map_lookup
*)em
->bdev
;
1781 for (i
= 0; i
< map
->num_stripes
; i
++) {
1782 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1783 map
->stripes
[i
].physical
);
1786 if (map
->stripes
[i
].dev
) {
1787 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1791 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1796 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1797 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1801 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1804 write_lock(&em_tree
->lock
);
1805 remove_extent_mapping(em_tree
, em
);
1806 write_unlock(&em_tree
->lock
);
1811 /* once for the tree */
1812 free_extent_map(em
);
1814 free_extent_map(em
);
1816 unlock_chunks(root
);
1817 btrfs_end_transaction(trans
, root
);
1821 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1823 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1824 struct btrfs_path
*path
;
1825 struct extent_buffer
*leaf
;
1826 struct btrfs_chunk
*chunk
;
1827 struct btrfs_key key
;
1828 struct btrfs_key found_key
;
1829 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1831 bool retried
= false;
1835 path
= btrfs_alloc_path();
1840 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1841 key
.offset
= (u64
)-1;
1842 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1845 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1850 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1857 leaf
= path
->nodes
[0];
1858 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1860 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1861 struct btrfs_chunk
);
1862 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1863 btrfs_release_path(chunk_root
, path
);
1865 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1866 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1875 if (found_key
.offset
== 0)
1877 key
.offset
= found_key
.offset
- 1;
1880 if (failed
&& !retried
) {
1884 } else if (failed
&& retried
) {
1889 btrfs_free_path(path
);
1893 static u64
div_factor(u64 num
, int factor
)
1902 int btrfs_balance(struct btrfs_root
*dev_root
)
1905 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1906 struct btrfs_device
*device
;
1909 struct btrfs_path
*path
;
1910 struct btrfs_key key
;
1911 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1912 struct btrfs_trans_handle
*trans
;
1913 struct btrfs_key found_key
;
1915 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1918 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1919 dev_root
= dev_root
->fs_info
->dev_root
;
1921 /* step one make some room on all the devices */
1922 list_for_each_entry(device
, devices
, dev_list
) {
1923 old_size
= device
->total_bytes
;
1924 size_to_free
= div_factor(old_size
, 1);
1925 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1926 if (!device
->writeable
||
1927 device
->total_bytes
- device
->bytes_used
> size_to_free
)
1930 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1935 trans
= btrfs_start_transaction(dev_root
, 0);
1938 ret
= btrfs_grow_device(trans
, device
, old_size
);
1941 btrfs_end_transaction(trans
, dev_root
);
1944 /* step two, relocate all the chunks */
1945 path
= btrfs_alloc_path();
1948 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1949 key
.offset
= (u64
)-1;
1950 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1953 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1958 * this shouldn't happen, it means the last relocate
1964 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1965 BTRFS_CHUNK_ITEM_KEY
);
1969 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1971 if (found_key
.objectid
!= key
.objectid
)
1974 /* chunk zero is special */
1975 if (found_key
.offset
== 0)
1978 btrfs_release_path(chunk_root
, path
);
1979 ret
= btrfs_relocate_chunk(chunk_root
,
1980 chunk_root
->root_key
.objectid
,
1983 BUG_ON(ret
&& ret
!= -ENOSPC
);
1984 key
.offset
= found_key
.offset
- 1;
1988 btrfs_free_path(path
);
1989 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
1994 * shrinking a device means finding all of the device extents past
1995 * the new size, and then following the back refs to the chunks.
1996 * The chunk relocation code actually frees the device extent
1998 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2000 struct btrfs_trans_handle
*trans
;
2001 struct btrfs_root
*root
= device
->dev_root
;
2002 struct btrfs_dev_extent
*dev_extent
= NULL
;
2003 struct btrfs_path
*path
;
2011 bool retried
= false;
2012 struct extent_buffer
*l
;
2013 struct btrfs_key key
;
2014 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2015 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2016 u64 old_size
= device
->total_bytes
;
2017 u64 diff
= device
->total_bytes
- new_size
;
2019 if (new_size
>= device
->total_bytes
)
2022 path
= btrfs_alloc_path();
2030 device
->total_bytes
= new_size
;
2031 if (device
->writeable
)
2032 device
->fs_devices
->total_rw_bytes
-= diff
;
2033 unlock_chunks(root
);
2036 key
.objectid
= device
->devid
;
2037 key
.offset
= (u64
)-1;
2038 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2041 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2045 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2050 btrfs_release_path(root
, path
);
2055 slot
= path
->slots
[0];
2056 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2058 if (key
.objectid
!= device
->devid
) {
2059 btrfs_release_path(root
, path
);
2063 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2064 length
= btrfs_dev_extent_length(l
, dev_extent
);
2066 if (key
.offset
+ length
<= new_size
) {
2067 btrfs_release_path(root
, path
);
2071 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2072 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2073 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2074 btrfs_release_path(root
, path
);
2076 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2078 if (ret
&& ret
!= -ENOSPC
)
2085 if (failed
&& !retried
) {
2089 } else if (failed
&& retried
) {
2093 device
->total_bytes
= old_size
;
2094 if (device
->writeable
)
2095 device
->fs_devices
->total_rw_bytes
+= diff
;
2096 unlock_chunks(root
);
2100 /* Shrinking succeeded, else we would be at "done". */
2101 trans
= btrfs_start_transaction(root
, 0);
2104 device
->disk_total_bytes
= new_size
;
2105 /* Now btrfs_update_device() will change the on-disk size. */
2106 ret
= btrfs_update_device(trans
, device
);
2108 unlock_chunks(root
);
2109 btrfs_end_transaction(trans
, root
);
2112 WARN_ON(diff
> old_total
);
2113 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2114 unlock_chunks(root
);
2115 btrfs_end_transaction(trans
, root
);
2117 btrfs_free_path(path
);
2121 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2122 struct btrfs_root
*root
,
2123 struct btrfs_key
*key
,
2124 struct btrfs_chunk
*chunk
, int item_size
)
2126 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2127 struct btrfs_disk_key disk_key
;
2131 array_size
= btrfs_super_sys_array_size(super_copy
);
2132 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2135 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2136 btrfs_cpu_key_to_disk(&disk_key
, key
);
2137 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2138 ptr
+= sizeof(disk_key
);
2139 memcpy(ptr
, chunk
, item_size
);
2140 item_size
+= sizeof(disk_key
);
2141 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2145 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2146 int num_stripes
, int sub_stripes
)
2148 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2150 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2151 return calc_size
* (num_stripes
/ sub_stripes
);
2153 return calc_size
* num_stripes
;
2156 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2157 struct btrfs_root
*extent_root
,
2158 struct map_lookup
**map_ret
,
2159 u64
*num_bytes
, u64
*stripe_size
,
2160 u64 start
, u64 type
)
2162 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2163 struct btrfs_device
*device
= NULL
;
2164 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2165 struct list_head
*cur
;
2166 struct map_lookup
*map
= NULL
;
2167 struct extent_map_tree
*em_tree
;
2168 struct extent_map
*em
;
2169 struct list_head private_devs
;
2170 int min_stripe_size
= 1 * 1024 * 1024;
2171 u64 calc_size
= 1024 * 1024 * 1024;
2172 u64 max_chunk_size
= calc_size
;
2177 int num_stripes
= 1;
2178 int min_stripes
= 1;
2179 int sub_stripes
= 0;
2184 int stripe_len
= 64 * 1024;
2186 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2187 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2189 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2191 if (list_empty(&fs_devices
->alloc_list
))
2194 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2195 num_stripes
= fs_devices
->rw_devices
;
2198 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2203 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2204 if (fs_devices
->rw_devices
< 2)
2210 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2211 num_stripes
= fs_devices
->rw_devices
;
2212 if (num_stripes
< 4)
2214 num_stripes
&= ~(u32
)1;
2220 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2221 max_chunk_size
= 10 * calc_size
;
2222 min_stripe_size
= 64 * 1024 * 1024;
2223 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2224 max_chunk_size
= 256 * 1024 * 1024;
2225 min_stripe_size
= 32 * 1024 * 1024;
2226 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2227 calc_size
= 8 * 1024 * 1024;
2228 max_chunk_size
= calc_size
* 2;
2229 min_stripe_size
= 1 * 1024 * 1024;
2232 /* we don't want a chunk larger than 10% of writeable space */
2233 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2238 if (!map
|| map
->num_stripes
!= num_stripes
) {
2240 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2243 map
->num_stripes
= num_stripes
;
2246 if (calc_size
* num_stripes
> max_chunk_size
* ncopies
) {
2247 calc_size
= max_chunk_size
* ncopies
;
2248 do_div(calc_size
, num_stripes
);
2249 do_div(calc_size
, stripe_len
);
2250 calc_size
*= stripe_len
;
2253 /* we don't want tiny stripes */
2255 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2258 * we're about to do_div by the stripe_len so lets make sure
2259 * we end up with something bigger than a stripe
2261 calc_size
= max_t(u64
, calc_size
, stripe_len
* 4);
2263 do_div(calc_size
, stripe_len
);
2264 calc_size
*= stripe_len
;
2266 cur
= fs_devices
->alloc_list
.next
;
2269 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2270 min_free
= calc_size
* 2;
2272 min_free
= calc_size
;
2275 * we add 1MB because we never use the first 1MB of the device, unless
2276 * we've looped, then we are likely allocating the maximum amount of
2277 * space left already
2280 min_free
+= 1024 * 1024;
2282 INIT_LIST_HEAD(&private_devs
);
2283 while (index
< num_stripes
) {
2284 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2285 BUG_ON(!device
->writeable
);
2286 if (device
->total_bytes
> device
->bytes_used
)
2287 avail
= device
->total_bytes
- device
->bytes_used
;
2292 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2293 ret
= find_free_dev_extent(trans
, device
,
2294 min_free
, &dev_offset
,
2297 list_move_tail(&device
->dev_alloc_list
,
2299 map
->stripes
[index
].dev
= device
;
2300 map
->stripes
[index
].physical
= dev_offset
;
2302 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2303 map
->stripes
[index
].dev
= device
;
2304 map
->stripes
[index
].physical
=
2305 dev_offset
+ calc_size
;
2309 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
2311 if (cur
== &fs_devices
->alloc_list
)
2314 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2315 if (index
< num_stripes
) {
2316 if (index
>= min_stripes
) {
2317 num_stripes
= index
;
2318 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2319 num_stripes
/= sub_stripes
;
2320 num_stripes
*= sub_stripes
;
2325 if (!looped
&& max_avail
> 0) {
2327 calc_size
= max_avail
;
2328 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2329 do_div(calc_size
, 2);
2335 map
->sector_size
= extent_root
->sectorsize
;
2336 map
->stripe_len
= stripe_len
;
2337 map
->io_align
= stripe_len
;
2338 map
->io_width
= stripe_len
;
2340 map
->num_stripes
= num_stripes
;
2341 map
->sub_stripes
= sub_stripes
;
2344 *stripe_size
= calc_size
;
2345 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2346 num_stripes
, sub_stripes
);
2348 em
= alloc_extent_map(GFP_NOFS
);
2353 em
->bdev
= (struct block_device
*)map
;
2355 em
->len
= *num_bytes
;
2356 em
->block_start
= 0;
2357 em
->block_len
= em
->len
;
2359 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2360 write_lock(&em_tree
->lock
);
2361 ret
= add_extent_mapping(em_tree
, em
);
2362 write_unlock(&em_tree
->lock
);
2364 free_extent_map(em
);
2366 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2367 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2372 while (index
< map
->num_stripes
) {
2373 device
= map
->stripes
[index
].dev
;
2374 dev_offset
= map
->stripes
[index
].physical
;
2376 ret
= btrfs_alloc_dev_extent(trans
, device
,
2377 info
->chunk_root
->root_key
.objectid
,
2378 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2379 start
, dev_offset
, calc_size
);
2387 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2388 struct btrfs_root
*extent_root
,
2389 struct map_lookup
*map
, u64 chunk_offset
,
2390 u64 chunk_size
, u64 stripe_size
)
2393 struct btrfs_key key
;
2394 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2395 struct btrfs_device
*device
;
2396 struct btrfs_chunk
*chunk
;
2397 struct btrfs_stripe
*stripe
;
2398 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2402 chunk
= kzalloc(item_size
, GFP_NOFS
);
2407 while (index
< map
->num_stripes
) {
2408 device
= map
->stripes
[index
].dev
;
2409 device
->bytes_used
+= stripe_size
;
2410 ret
= btrfs_update_device(trans
, device
);
2416 stripe
= &chunk
->stripe
;
2417 while (index
< map
->num_stripes
) {
2418 device
= map
->stripes
[index
].dev
;
2419 dev_offset
= map
->stripes
[index
].physical
;
2421 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2422 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2423 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2428 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2429 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2430 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2431 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2432 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2433 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2434 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2435 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2436 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2438 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2439 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2440 key
.offset
= chunk_offset
;
2442 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2445 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2446 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2455 * Chunk allocation falls into two parts. The first part does works
2456 * that make the new allocated chunk useable, but not do any operation
2457 * that modifies the chunk tree. The second part does the works that
2458 * require modifying the chunk tree. This division is important for the
2459 * bootstrap process of adding storage to a seed btrfs.
2461 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2462 struct btrfs_root
*extent_root
, u64 type
)
2467 struct map_lookup
*map
;
2468 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2471 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2476 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2477 &stripe_size
, chunk_offset
, type
);
2481 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2482 chunk_size
, stripe_size
);
2487 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2488 struct btrfs_root
*root
,
2489 struct btrfs_device
*device
)
2492 u64 sys_chunk_offset
;
2496 u64 sys_stripe_size
;
2498 struct map_lookup
*map
;
2499 struct map_lookup
*sys_map
;
2500 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2501 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2504 ret
= find_next_chunk(fs_info
->chunk_root
,
2505 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2508 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2509 (fs_info
->metadata_alloc_profile
&
2510 fs_info
->avail_metadata_alloc_bits
);
2511 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2513 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2514 &stripe_size
, chunk_offset
, alloc_profile
);
2517 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2519 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2520 (fs_info
->system_alloc_profile
&
2521 fs_info
->avail_system_alloc_bits
);
2522 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2524 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2525 &sys_chunk_size
, &sys_stripe_size
,
2526 sys_chunk_offset
, alloc_profile
);
2529 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2533 * Modifying chunk tree needs allocating new blocks from both
2534 * system block group and metadata block group. So we only can
2535 * do operations require modifying the chunk tree after both
2536 * block groups were created.
2538 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2539 chunk_size
, stripe_size
);
2542 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2543 sys_chunk_offset
, sys_chunk_size
,
2549 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2551 struct extent_map
*em
;
2552 struct map_lookup
*map
;
2553 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2557 read_lock(&map_tree
->map_tree
.lock
);
2558 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2559 read_unlock(&map_tree
->map_tree
.lock
);
2563 if (btrfs_test_opt(root
, DEGRADED
)) {
2564 free_extent_map(em
);
2568 map
= (struct map_lookup
*)em
->bdev
;
2569 for (i
= 0; i
< map
->num_stripes
; i
++) {
2570 if (!map
->stripes
[i
].dev
->writeable
) {
2575 free_extent_map(em
);
2579 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2581 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2584 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2586 struct extent_map
*em
;
2589 write_lock(&tree
->map_tree
.lock
);
2590 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2592 remove_extent_mapping(&tree
->map_tree
, em
);
2593 write_unlock(&tree
->map_tree
.lock
);
2598 free_extent_map(em
);
2599 /* once for the tree */
2600 free_extent_map(em
);
2604 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2606 struct extent_map
*em
;
2607 struct map_lookup
*map
;
2608 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2611 read_lock(&em_tree
->lock
);
2612 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2613 read_unlock(&em_tree
->lock
);
2616 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2617 map
= (struct map_lookup
*)em
->bdev
;
2618 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2619 ret
= map
->num_stripes
;
2620 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2621 ret
= map
->sub_stripes
;
2624 free_extent_map(em
);
2628 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2632 if (map
->stripes
[optimal
].dev
->bdev
)
2634 for (i
= first
; i
< first
+ num
; i
++) {
2635 if (map
->stripes
[i
].dev
->bdev
)
2638 /* we couldn't find one that doesn't fail. Just return something
2639 * and the io error handling code will clean up eventually
2644 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2645 u64 logical
, u64
*length
,
2646 struct btrfs_multi_bio
**multi_ret
,
2647 int mirror_num
, struct page
*unplug_page
)
2649 struct extent_map
*em
;
2650 struct map_lookup
*map
;
2651 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2655 int stripes_allocated
= 8;
2656 int stripes_required
= 1;
2661 struct btrfs_multi_bio
*multi
= NULL
;
2663 if (multi_ret
&& !(rw
& REQ_WRITE
))
2664 stripes_allocated
= 1;
2667 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2672 atomic_set(&multi
->error
, 0);
2675 read_lock(&em_tree
->lock
);
2676 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2677 read_unlock(&em_tree
->lock
);
2679 if (!em
&& unplug_page
) {
2685 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2686 (unsigned long long)logical
,
2687 (unsigned long long)*length
);
2691 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2692 map
= (struct map_lookup
*)em
->bdev
;
2693 offset
= logical
- em
->start
;
2695 if (mirror_num
> map
->num_stripes
)
2698 /* if our multi bio struct is too small, back off and try again */
2699 if (rw
& REQ_WRITE
) {
2700 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2701 BTRFS_BLOCK_GROUP_DUP
)) {
2702 stripes_required
= map
->num_stripes
;
2704 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2705 stripes_required
= map
->sub_stripes
;
2709 if (multi_ret
&& (rw
& REQ_WRITE
) &&
2710 stripes_allocated
< stripes_required
) {
2711 stripes_allocated
= map
->num_stripes
;
2712 free_extent_map(em
);
2718 * stripe_nr counts the total number of stripes we have to stride
2719 * to get to this block
2721 do_div(stripe_nr
, map
->stripe_len
);
2723 stripe_offset
= stripe_nr
* map
->stripe_len
;
2724 BUG_ON(offset
< stripe_offset
);
2726 /* stripe_offset is the offset of this block in its stripe*/
2727 stripe_offset
= offset
- stripe_offset
;
2729 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2730 BTRFS_BLOCK_GROUP_RAID10
|
2731 BTRFS_BLOCK_GROUP_DUP
)) {
2732 /* we limit the length of each bio to what fits in a stripe */
2733 *length
= min_t(u64
, em
->len
- offset
,
2734 map
->stripe_len
- stripe_offset
);
2736 *length
= em
->len
- offset
;
2739 if (!multi_ret
&& !unplug_page
)
2744 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2745 if (unplug_page
|| (rw
& REQ_WRITE
))
2746 num_stripes
= map
->num_stripes
;
2747 else if (mirror_num
)
2748 stripe_index
= mirror_num
- 1;
2750 stripe_index
= find_live_mirror(map
, 0,
2752 current
->pid
% map
->num_stripes
);
2755 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2757 num_stripes
= map
->num_stripes
;
2758 else if (mirror_num
)
2759 stripe_index
= mirror_num
- 1;
2761 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2762 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2764 stripe_index
= do_div(stripe_nr
, factor
);
2765 stripe_index
*= map
->sub_stripes
;
2767 if (unplug_page
|| (rw
& REQ_WRITE
))
2768 num_stripes
= map
->sub_stripes
;
2769 else if (mirror_num
)
2770 stripe_index
+= mirror_num
- 1;
2772 stripe_index
= find_live_mirror(map
, stripe_index
,
2773 map
->sub_stripes
, stripe_index
+
2774 current
->pid
% map
->sub_stripes
);
2778 * after this do_div call, stripe_nr is the number of stripes
2779 * on this device we have to walk to find the data, and
2780 * stripe_index is the number of our device in the stripe array
2782 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2784 BUG_ON(stripe_index
>= map
->num_stripes
);
2786 for (i
= 0; i
< num_stripes
; i
++) {
2788 struct btrfs_device
*device
;
2789 struct backing_dev_info
*bdi
;
2791 device
= map
->stripes
[stripe_index
].dev
;
2793 bdi
= blk_get_backing_dev_info(device
->bdev
);
2794 if (bdi
->unplug_io_fn
)
2795 bdi
->unplug_io_fn(bdi
, unplug_page
);
2798 multi
->stripes
[i
].physical
=
2799 map
->stripes
[stripe_index
].physical
+
2800 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2801 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2807 multi
->num_stripes
= num_stripes
;
2808 multi
->max_errors
= max_errors
;
2811 free_extent_map(em
);
2815 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2816 u64 logical
, u64
*length
,
2817 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2819 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2823 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
2824 u64 chunk_start
, u64 physical
, u64 devid
,
2825 u64
**logical
, int *naddrs
, int *stripe_len
)
2827 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2828 struct extent_map
*em
;
2829 struct map_lookup
*map
;
2836 read_lock(&em_tree
->lock
);
2837 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
2838 read_unlock(&em_tree
->lock
);
2840 BUG_ON(!em
|| em
->start
!= chunk_start
);
2841 map
= (struct map_lookup
*)em
->bdev
;
2844 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2845 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
2846 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
2847 do_div(length
, map
->num_stripes
);
2849 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
2852 for (i
= 0; i
< map
->num_stripes
; i
++) {
2853 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
2855 if (map
->stripes
[i
].physical
> physical
||
2856 map
->stripes
[i
].physical
+ length
<= physical
)
2859 stripe_nr
= physical
- map
->stripes
[i
].physical
;
2860 do_div(stripe_nr
, map
->stripe_len
);
2862 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2863 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2864 do_div(stripe_nr
, map
->sub_stripes
);
2865 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2866 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2868 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
2869 WARN_ON(nr
>= map
->num_stripes
);
2870 for (j
= 0; j
< nr
; j
++) {
2871 if (buf
[j
] == bytenr
)
2875 WARN_ON(nr
>= map
->num_stripes
);
2882 *stripe_len
= map
->stripe_len
;
2884 free_extent_map(em
);
2888 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2889 u64 logical
, struct page
*page
)
2891 u64 length
= PAGE_CACHE_SIZE
;
2892 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2896 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2898 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2899 int is_orig_bio
= 0;
2902 atomic_inc(&multi
->error
);
2904 if (bio
== multi
->orig_bio
)
2907 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2910 bio
= multi
->orig_bio
;
2912 bio
->bi_private
= multi
->private;
2913 bio
->bi_end_io
= multi
->end_io
;
2914 /* only send an error to the higher layers if it is
2915 * beyond the tolerance of the multi-bio
2917 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2921 * this bio is actually up to date, we didn't
2922 * go over the max number of errors
2924 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2929 bio_endio(bio
, err
);
2930 } else if (!is_orig_bio
) {
2935 struct async_sched
{
2938 struct btrfs_fs_info
*info
;
2939 struct btrfs_work work
;
2943 * see run_scheduled_bios for a description of why bios are collected for
2946 * This will add one bio to the pending list for a device and make sure
2947 * the work struct is scheduled.
2949 static noinline
int schedule_bio(struct btrfs_root
*root
,
2950 struct btrfs_device
*device
,
2951 int rw
, struct bio
*bio
)
2953 int should_queue
= 1;
2954 struct btrfs_pending_bios
*pending_bios
;
2956 /* don't bother with additional async steps for reads, right now */
2957 if (!(rw
& REQ_WRITE
)) {
2959 submit_bio(rw
, bio
);
2965 * nr_async_bios allows us to reliably return congestion to the
2966 * higher layers. Otherwise, the async bio makes it appear we have
2967 * made progress against dirty pages when we've really just put it
2968 * on a queue for later
2970 atomic_inc(&root
->fs_info
->nr_async_bios
);
2971 WARN_ON(bio
->bi_next
);
2972 bio
->bi_next
= NULL
;
2975 spin_lock(&device
->io_lock
);
2976 if (bio
->bi_rw
& REQ_SYNC
)
2977 pending_bios
= &device
->pending_sync_bios
;
2979 pending_bios
= &device
->pending_bios
;
2981 if (pending_bios
->tail
)
2982 pending_bios
->tail
->bi_next
= bio
;
2984 pending_bios
->tail
= bio
;
2985 if (!pending_bios
->head
)
2986 pending_bios
->head
= bio
;
2987 if (device
->running_pending
)
2990 spin_unlock(&device
->io_lock
);
2993 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2998 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2999 int mirror_num
, int async_submit
)
3001 struct btrfs_mapping_tree
*map_tree
;
3002 struct btrfs_device
*dev
;
3003 struct bio
*first_bio
= bio
;
3004 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3007 struct btrfs_multi_bio
*multi
= NULL
;
3012 length
= bio
->bi_size
;
3013 map_tree
= &root
->fs_info
->mapping_tree
;
3014 map_length
= length
;
3016 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3020 total_devs
= multi
->num_stripes
;
3021 if (map_length
< length
) {
3022 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3023 "len %llu\n", (unsigned long long)logical
,
3024 (unsigned long long)length
,
3025 (unsigned long long)map_length
);
3028 multi
->end_io
= first_bio
->bi_end_io
;
3029 multi
->private = first_bio
->bi_private
;
3030 multi
->orig_bio
= first_bio
;
3031 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3033 while (dev_nr
< total_devs
) {
3034 if (total_devs
> 1) {
3035 if (dev_nr
< total_devs
- 1) {
3036 bio
= bio_clone(first_bio
, GFP_NOFS
);
3041 bio
->bi_private
= multi
;
3042 bio
->bi_end_io
= end_bio_multi_stripe
;
3044 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3045 dev
= multi
->stripes
[dev_nr
].dev
;
3046 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3047 bio
->bi_bdev
= dev
->bdev
;
3049 schedule_bio(root
, dev
, rw
, bio
);
3051 submit_bio(rw
, bio
);
3053 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3054 bio
->bi_sector
= logical
>> 9;
3055 bio_endio(bio
, -EIO
);
3059 if (total_devs
== 1)
3064 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3067 struct btrfs_device
*device
;
3068 struct btrfs_fs_devices
*cur_devices
;
3070 cur_devices
= root
->fs_info
->fs_devices
;
3071 while (cur_devices
) {
3073 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3074 device
= __find_device(&cur_devices
->devices
,
3079 cur_devices
= cur_devices
->seed
;
3084 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3085 u64 devid
, u8
*dev_uuid
)
3087 struct btrfs_device
*device
;
3088 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3090 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3093 list_add(&device
->dev_list
,
3094 &fs_devices
->devices
);
3095 device
->barriers
= 1;
3096 device
->dev_root
= root
->fs_info
->dev_root
;
3097 device
->devid
= devid
;
3098 device
->work
.func
= pending_bios_fn
;
3099 device
->fs_devices
= fs_devices
;
3100 device
->missing
= 1;
3101 fs_devices
->num_devices
++;
3102 fs_devices
->missing_devices
++;
3103 spin_lock_init(&device
->io_lock
);
3104 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3105 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3109 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3110 struct extent_buffer
*leaf
,
3111 struct btrfs_chunk
*chunk
)
3113 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3114 struct map_lookup
*map
;
3115 struct extent_map
*em
;
3119 u8 uuid
[BTRFS_UUID_SIZE
];
3124 logical
= key
->offset
;
3125 length
= btrfs_chunk_length(leaf
, chunk
);
3127 read_lock(&map_tree
->map_tree
.lock
);
3128 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3129 read_unlock(&map_tree
->map_tree
.lock
);
3131 /* already mapped? */
3132 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3133 free_extent_map(em
);
3136 free_extent_map(em
);
3139 em
= alloc_extent_map(GFP_NOFS
);
3142 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3143 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3145 free_extent_map(em
);
3149 em
->bdev
= (struct block_device
*)map
;
3150 em
->start
= logical
;
3152 em
->block_start
= 0;
3153 em
->block_len
= em
->len
;
3155 map
->num_stripes
= num_stripes
;
3156 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3157 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3158 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3159 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3160 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3161 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3162 for (i
= 0; i
< num_stripes
; i
++) {
3163 map
->stripes
[i
].physical
=
3164 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3165 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3166 read_extent_buffer(leaf
, uuid
, (unsigned long)
3167 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3169 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3171 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3173 free_extent_map(em
);
3176 if (!map
->stripes
[i
].dev
) {
3177 map
->stripes
[i
].dev
=
3178 add_missing_dev(root
, devid
, uuid
);
3179 if (!map
->stripes
[i
].dev
) {
3181 free_extent_map(em
);
3185 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3188 write_lock(&map_tree
->map_tree
.lock
);
3189 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3190 write_unlock(&map_tree
->map_tree
.lock
);
3192 free_extent_map(em
);
3197 static int fill_device_from_item(struct extent_buffer
*leaf
,
3198 struct btrfs_dev_item
*dev_item
,
3199 struct btrfs_device
*device
)
3203 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3204 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3205 device
->total_bytes
= device
->disk_total_bytes
;
3206 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3207 device
->type
= btrfs_device_type(leaf
, dev_item
);
3208 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3209 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3210 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3212 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3213 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3218 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3220 struct btrfs_fs_devices
*fs_devices
;
3223 mutex_lock(&uuid_mutex
);
3225 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3226 while (fs_devices
) {
3227 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3231 fs_devices
= fs_devices
->seed
;
3234 fs_devices
= find_fsid(fsid
);
3240 fs_devices
= clone_fs_devices(fs_devices
);
3241 if (IS_ERR(fs_devices
)) {
3242 ret
= PTR_ERR(fs_devices
);
3246 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3247 root
->fs_info
->bdev_holder
);
3251 if (!fs_devices
->seeding
) {
3252 __btrfs_close_devices(fs_devices
);
3253 free_fs_devices(fs_devices
);
3258 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3259 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3261 mutex_unlock(&uuid_mutex
);
3265 static int read_one_dev(struct btrfs_root
*root
,
3266 struct extent_buffer
*leaf
,
3267 struct btrfs_dev_item
*dev_item
)
3269 struct btrfs_device
*device
;
3272 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3273 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3275 devid
= btrfs_device_id(leaf
, dev_item
);
3276 read_extent_buffer(leaf
, dev_uuid
,
3277 (unsigned long)btrfs_device_uuid(dev_item
),
3279 read_extent_buffer(leaf
, fs_uuid
,
3280 (unsigned long)btrfs_device_fsid(dev_item
),
3283 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3284 ret
= open_seed_devices(root
, fs_uuid
);
3285 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3289 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3290 if (!device
|| !device
->bdev
) {
3291 if (!btrfs_test_opt(root
, DEGRADED
))
3295 printk(KERN_WARNING
"warning devid %llu missing\n",
3296 (unsigned long long)devid
);
3297 device
= add_missing_dev(root
, devid
, dev_uuid
);
3300 } else if (!device
->missing
) {
3302 * this happens when a device that was properly setup
3303 * in the device info lists suddenly goes bad.
3304 * device->bdev is NULL, and so we have to set
3305 * device->missing to one here
3307 root
->fs_info
->fs_devices
->missing_devices
++;
3308 device
->missing
= 1;
3312 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3313 BUG_ON(device
->writeable
);
3314 if (device
->generation
!=
3315 btrfs_device_generation(leaf
, dev_item
))
3319 fill_device_from_item(leaf
, dev_item
, device
);
3320 device
->dev_root
= root
->fs_info
->dev_root
;
3321 device
->in_fs_metadata
= 1;
3322 if (device
->writeable
)
3323 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3328 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3330 struct btrfs_dev_item
*dev_item
;
3332 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3334 return read_one_dev(root
, buf
, dev_item
);
3337 int btrfs_read_sys_array(struct btrfs_root
*root
)
3339 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3340 struct extent_buffer
*sb
;
3341 struct btrfs_disk_key
*disk_key
;
3342 struct btrfs_chunk
*chunk
;
3344 unsigned long sb_ptr
;
3350 struct btrfs_key key
;
3352 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3353 BTRFS_SUPER_INFO_SIZE
);
3356 btrfs_set_buffer_uptodate(sb
);
3357 btrfs_set_buffer_lockdep_class(sb
, 0);
3359 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3360 array_size
= btrfs_super_sys_array_size(super_copy
);
3362 ptr
= super_copy
->sys_chunk_array
;
3363 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3366 while (cur
< array_size
) {
3367 disk_key
= (struct btrfs_disk_key
*)ptr
;
3368 btrfs_disk_key_to_cpu(&key
, disk_key
);
3370 len
= sizeof(*disk_key
); ptr
+= len
;
3374 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3375 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3376 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3379 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3380 len
= btrfs_chunk_item_size(num_stripes
);
3389 free_extent_buffer(sb
);
3393 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3395 struct btrfs_path
*path
;
3396 struct extent_buffer
*leaf
;
3397 struct btrfs_key key
;
3398 struct btrfs_key found_key
;
3402 root
= root
->fs_info
->chunk_root
;
3404 path
= btrfs_alloc_path();
3408 /* first we search for all of the device items, and then we
3409 * read in all of the chunk items. This way we can create chunk
3410 * mappings that reference all of the devices that are afound
3412 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3416 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3420 leaf
= path
->nodes
[0];
3421 slot
= path
->slots
[0];
3422 if (slot
>= btrfs_header_nritems(leaf
)) {
3423 ret
= btrfs_next_leaf(root
, path
);
3430 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3431 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3432 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3434 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3435 struct btrfs_dev_item
*dev_item
;
3436 dev_item
= btrfs_item_ptr(leaf
, slot
,
3437 struct btrfs_dev_item
);
3438 ret
= read_one_dev(root
, leaf
, dev_item
);
3442 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3443 struct btrfs_chunk
*chunk
;
3444 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3445 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3451 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3453 btrfs_release_path(root
, path
);
3458 btrfs_free_path(path
);