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>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.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
);
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
);
81 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
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
))) {
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)
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
;
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
)
134 struct backing_dev_info
*bdi
;
135 struct btrfs_fs_info
*fs_info
;
136 struct btrfs_pending_bios
*pending_bios
;
140 unsigned long num_run
;
141 unsigned long batch_run
= 0;
143 unsigned long last_waited
= 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;
161 spin_lock(&device
->io_lock
);
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
;
175 pending_bios
= &device
->pending_bios
;
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
191 if (device
->pending_sync_bios
.head
== NULL
&&
192 device
->pending_bios
.head
== NULL
) {
194 device
->running_pending
= 0;
197 device
->running_pending
= 1;
200 pending_bios
->head
= NULL
;
201 pending_bios
->tail
= NULL
;
203 spin_unlock(&device
->io_lock
);
208 /* we want to work on both lists, but do more bios on the
209 * sync list than the regular list
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
);
222 pending
= pending
->bi_next
;
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
);
239 * we made progress, there is more work to do and the bdi
240 * is now congested. Back off and let other work structs
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) &&
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
;
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
);
287 spin_lock(&device
->io_lock
);
288 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
290 spin_unlock(&device
->io_lock
);
293 blk_finish_plug(&plug
);
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
);
314 fs_devices
= find_fsid(disk_super
->fsid
);
316 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
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
);
328 device
= __find_device(&fs_devices
->devices
, devid
,
329 disk_super
->dev_item
.uuid
);
332 if (fs_devices
->opened
)
335 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
337 /* we can safely leave the fs_devices entry around */
340 device
->devid
= devid
;
341 device
->work
.func
= pending_bios_fn
;
342 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
344 spin_lock_init(&device
->io_lock
);
345 device
->name
= kstrdup(path
, GFP_NOFS
);
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
);
364 if (device
->missing
) {
365 fs_devices
->missing_devices
--;
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
;
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
);
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
);
402 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
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
++;
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
);
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
)
437 blkdev_put(device
->bdev
, device
->mode
);
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
--;
452 if (fs_devices
->seed
) {
453 fs_devices
= fs_devices
->seed
;
457 mutex_unlock(&uuid_mutex
);
461 static void __free_device(struct work_struct
*work
)
463 struct btrfs_device
*device
;
465 device
= container_of(work
, struct btrfs_device
, rcu_work
);
468 blkdev_put(device
->bdev
, device
->mode
);
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)
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
;
496 fs_devices
->open_devices
--;
498 if (device
->writeable
) {
499 list_del_init(&device
->dev_alloc_list
);
500 fs_devices
->rw_devices
--;
503 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
505 memcpy(new_device
, device
, sizeof(*new_device
));
506 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
507 BUG_ON(device
->name
&& !new_device
->name
);
508 new_device
->bdev
= NULL
;
509 new_device
->writeable
= 0;
510 new_device
->in_fs_metadata
= 0;
511 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
513 call_rcu(&device
->rcu
, free_device
);
515 mutex_unlock(&fs_devices
->device_list_mutex
);
517 WARN_ON(fs_devices
->open_devices
);
518 WARN_ON(fs_devices
->rw_devices
);
519 fs_devices
->opened
= 0;
520 fs_devices
->seeding
= 0;
525 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
527 struct btrfs_fs_devices
*seed_devices
= NULL
;
530 mutex_lock(&uuid_mutex
);
531 ret
= __btrfs_close_devices(fs_devices
);
532 if (!fs_devices
->opened
) {
533 seed_devices
= fs_devices
->seed
;
534 fs_devices
->seed
= NULL
;
536 mutex_unlock(&uuid_mutex
);
538 while (seed_devices
) {
539 fs_devices
= seed_devices
;
540 seed_devices
= fs_devices
->seed
;
541 __btrfs_close_devices(fs_devices
);
542 free_fs_devices(fs_devices
);
547 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
548 fmode_t flags
, void *holder
)
550 struct block_device
*bdev
;
551 struct list_head
*head
= &fs_devices
->devices
;
552 struct btrfs_device
*device
;
553 struct block_device
*latest_bdev
= NULL
;
554 struct buffer_head
*bh
;
555 struct btrfs_super_block
*disk_super
;
556 u64 latest_devid
= 0;
557 u64 latest_transid
= 0;
564 list_for_each_entry(device
, head
, dev_list
) {
570 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
572 printk(KERN_INFO
"open %s failed\n", device
->name
);
575 set_blocksize(bdev
, 4096);
577 bh
= btrfs_read_dev_super(bdev
);
583 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
584 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
585 if (devid
!= device
->devid
)
588 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
592 device
->generation
= btrfs_super_generation(disk_super
);
593 if (!latest_transid
|| device
->generation
> latest_transid
) {
594 latest_devid
= devid
;
595 latest_transid
= device
->generation
;
599 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
600 device
->writeable
= 0;
602 device
->writeable
= !bdev_read_only(bdev
);
607 device
->in_fs_metadata
= 0;
608 device
->mode
= flags
;
610 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
611 fs_devices
->rotating
= 1;
613 fs_devices
->open_devices
++;
614 if (device
->writeable
) {
615 fs_devices
->rw_devices
++;
616 list_add(&device
->dev_alloc_list
,
617 &fs_devices
->alloc_list
);
625 blkdev_put(bdev
, flags
);
629 if (fs_devices
->open_devices
== 0) {
633 fs_devices
->seeding
= seeding
;
634 fs_devices
->opened
= 1;
635 fs_devices
->latest_bdev
= latest_bdev
;
636 fs_devices
->latest_devid
= latest_devid
;
637 fs_devices
->latest_trans
= latest_transid
;
638 fs_devices
->total_rw_bytes
= 0;
643 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
644 fmode_t flags
, void *holder
)
648 mutex_lock(&uuid_mutex
);
649 if (fs_devices
->opened
) {
650 fs_devices
->opened
++;
653 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
655 mutex_unlock(&uuid_mutex
);
659 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
660 struct btrfs_fs_devices
**fs_devices_ret
)
662 struct btrfs_super_block
*disk_super
;
663 struct block_device
*bdev
;
664 struct buffer_head
*bh
;
669 mutex_lock(&uuid_mutex
);
672 bdev
= blkdev_get_by_path(path
, flags
, holder
);
679 ret
= set_blocksize(bdev
, 4096);
682 bh
= btrfs_read_dev_super(bdev
);
687 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
688 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
689 transid
= btrfs_super_generation(disk_super
);
690 if (disk_super
->label
[0])
691 printk(KERN_INFO
"device label %s ", disk_super
->label
);
693 /* FIXME, make a readl uuid parser */
694 printk(KERN_INFO
"device fsid %llx-%llx ",
695 *(unsigned long long *)disk_super
->fsid
,
696 *(unsigned long long *)(disk_super
->fsid
+ 8));
698 printk(KERN_CONT
"devid %llu transid %llu %s\n",
699 (unsigned long long)devid
, (unsigned long long)transid
, path
);
700 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
704 blkdev_put(bdev
, flags
);
706 mutex_unlock(&uuid_mutex
);
710 /* helper to account the used device space in the range */
711 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
712 u64 end
, u64
*length
)
714 struct btrfs_key key
;
715 struct btrfs_root
*root
= device
->dev_root
;
716 struct btrfs_dev_extent
*dev_extent
;
717 struct btrfs_path
*path
;
721 struct extent_buffer
*l
;
725 if (start
>= device
->total_bytes
)
728 path
= btrfs_alloc_path();
733 key
.objectid
= device
->devid
;
735 key
.type
= BTRFS_DEV_EXTENT_KEY
;
737 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
741 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
748 slot
= path
->slots
[0];
749 if (slot
>= btrfs_header_nritems(l
)) {
750 ret
= btrfs_next_leaf(root
, path
);
758 btrfs_item_key_to_cpu(l
, &key
, slot
);
760 if (key
.objectid
< device
->devid
)
763 if (key
.objectid
> device
->devid
)
766 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
769 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
770 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
772 if (key
.offset
<= start
&& extent_end
> end
) {
773 *length
= end
- start
+ 1;
775 } else if (key
.offset
<= start
&& extent_end
> start
)
776 *length
+= extent_end
- start
;
777 else if (key
.offset
> start
&& extent_end
<= end
)
778 *length
+= extent_end
- key
.offset
;
779 else if (key
.offset
> start
&& key
.offset
<= end
) {
780 *length
+= end
- key
.offset
+ 1;
782 } else if (key
.offset
> end
)
790 btrfs_free_path(path
);
795 * find_free_dev_extent - find free space in the specified device
796 * @trans: transaction handler
797 * @device: the device which we search the free space in
798 * @num_bytes: the size of the free space that we need
799 * @start: store the start of the free space.
800 * @len: the size of the free space. that we find, or the size of the max
801 * free space if we don't find suitable free space
803 * this uses a pretty simple search, the expectation is that it is
804 * called very infrequently and that a given device has a small number
807 * @start is used to store the start of the free space if we find. But if we
808 * don't find suitable free space, it will be used to store the start position
809 * of the max free space.
811 * @len is used to store the size of the free space that we find.
812 * But if we don't find suitable free space, it is used to store the size of
813 * the max free space.
815 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
816 struct btrfs_device
*device
, u64 num_bytes
,
817 u64
*start
, u64
*len
)
819 struct btrfs_key key
;
820 struct btrfs_root
*root
= device
->dev_root
;
821 struct btrfs_dev_extent
*dev_extent
;
822 struct btrfs_path
*path
;
828 u64 search_end
= device
->total_bytes
;
831 struct extent_buffer
*l
;
833 /* FIXME use last free of some kind */
835 /* we don't want to overwrite the superblock on the drive,
836 * so we make sure to start at an offset of at least 1MB
838 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
840 max_hole_start
= search_start
;
843 if (search_start
>= search_end
) {
848 path
= btrfs_alloc_path();
855 key
.objectid
= device
->devid
;
856 key
.offset
= search_start
;
857 key
.type
= BTRFS_DEV_EXTENT_KEY
;
859 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
863 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
870 slot
= path
->slots
[0];
871 if (slot
>= btrfs_header_nritems(l
)) {
872 ret
= btrfs_next_leaf(root
, path
);
880 btrfs_item_key_to_cpu(l
, &key
, slot
);
882 if (key
.objectid
< device
->devid
)
885 if (key
.objectid
> device
->devid
)
888 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
891 if (key
.offset
> search_start
) {
892 hole_size
= key
.offset
- search_start
;
894 if (hole_size
> max_hole_size
) {
895 max_hole_start
= search_start
;
896 max_hole_size
= hole_size
;
900 * If this free space is greater than which we need,
901 * it must be the max free space that we have found
902 * until now, so max_hole_start must point to the start
903 * of this free space and the length of this free space
904 * is stored in max_hole_size. Thus, we return
905 * max_hole_start and max_hole_size and go back to the
908 if (hole_size
>= num_bytes
) {
914 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
915 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
917 if (extent_end
> search_start
)
918 search_start
= extent_end
;
924 hole_size
= search_end
- search_start
;
925 if (hole_size
> max_hole_size
) {
926 max_hole_start
= search_start
;
927 max_hole_size
= hole_size
;
931 if (hole_size
< num_bytes
)
937 btrfs_free_path(path
);
939 *start
= max_hole_start
;
941 *len
= max_hole_size
;
945 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
946 struct btrfs_device
*device
,
950 struct btrfs_path
*path
;
951 struct btrfs_root
*root
= device
->dev_root
;
952 struct btrfs_key key
;
953 struct btrfs_key found_key
;
954 struct extent_buffer
*leaf
= NULL
;
955 struct btrfs_dev_extent
*extent
= NULL
;
957 path
= btrfs_alloc_path();
961 key
.objectid
= device
->devid
;
963 key
.type
= BTRFS_DEV_EXTENT_KEY
;
965 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
967 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
968 BTRFS_DEV_EXTENT_KEY
);
971 leaf
= path
->nodes
[0];
972 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
973 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
974 struct btrfs_dev_extent
);
975 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
976 btrfs_dev_extent_length(leaf
, extent
) < start
);
977 } else if (ret
== 0) {
978 leaf
= path
->nodes
[0];
979 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
980 struct btrfs_dev_extent
);
984 if (device
->bytes_used
> 0)
985 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
986 ret
= btrfs_del_item(trans
, root
, path
);
989 btrfs_free_path(path
);
993 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
994 struct btrfs_device
*device
,
995 u64 chunk_tree
, u64 chunk_objectid
,
996 u64 chunk_offset
, u64 start
, u64 num_bytes
)
999 struct btrfs_path
*path
;
1000 struct btrfs_root
*root
= device
->dev_root
;
1001 struct btrfs_dev_extent
*extent
;
1002 struct extent_buffer
*leaf
;
1003 struct btrfs_key key
;
1005 WARN_ON(!device
->in_fs_metadata
);
1006 path
= btrfs_alloc_path();
1010 key
.objectid
= device
->devid
;
1012 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1013 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1017 leaf
= path
->nodes
[0];
1018 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1019 struct btrfs_dev_extent
);
1020 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1021 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1022 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1024 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1025 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1028 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1029 btrfs_mark_buffer_dirty(leaf
);
1030 btrfs_free_path(path
);
1034 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1035 u64 objectid
, u64
*offset
)
1037 struct btrfs_path
*path
;
1039 struct btrfs_key key
;
1040 struct btrfs_chunk
*chunk
;
1041 struct btrfs_key found_key
;
1043 path
= btrfs_alloc_path();
1046 key
.objectid
= objectid
;
1047 key
.offset
= (u64
)-1;
1048 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1050 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1056 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1060 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1062 if (found_key
.objectid
!= objectid
)
1065 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1066 struct btrfs_chunk
);
1067 *offset
= found_key
.offset
+
1068 btrfs_chunk_length(path
->nodes
[0], chunk
);
1073 btrfs_free_path(path
);
1077 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1080 struct btrfs_key key
;
1081 struct btrfs_key found_key
;
1082 struct btrfs_path
*path
;
1084 root
= root
->fs_info
->chunk_root
;
1086 path
= btrfs_alloc_path();
1090 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1091 key
.type
= BTRFS_DEV_ITEM_KEY
;
1092 key
.offset
= (u64
)-1;
1094 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1100 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1101 BTRFS_DEV_ITEM_KEY
);
1105 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1107 *objectid
= found_key
.offset
+ 1;
1111 btrfs_free_path(path
);
1116 * the device information is stored in the chunk root
1117 * the btrfs_device struct should be fully filled in
1119 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1120 struct btrfs_root
*root
,
1121 struct btrfs_device
*device
)
1124 struct btrfs_path
*path
;
1125 struct btrfs_dev_item
*dev_item
;
1126 struct extent_buffer
*leaf
;
1127 struct btrfs_key key
;
1130 root
= root
->fs_info
->chunk_root
;
1132 path
= btrfs_alloc_path();
1136 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1137 key
.type
= BTRFS_DEV_ITEM_KEY
;
1138 key
.offset
= device
->devid
;
1140 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1145 leaf
= path
->nodes
[0];
1146 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1148 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1149 btrfs_set_device_generation(leaf
, dev_item
, 0);
1150 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1151 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1152 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1153 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1154 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1155 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1156 btrfs_set_device_group(leaf
, dev_item
, 0);
1157 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1158 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1159 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1161 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1162 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1163 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1164 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1165 btrfs_mark_buffer_dirty(leaf
);
1169 btrfs_free_path(path
);
1173 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1174 struct btrfs_device
*device
)
1177 struct btrfs_path
*path
;
1178 struct btrfs_key key
;
1179 struct btrfs_trans_handle
*trans
;
1181 root
= root
->fs_info
->chunk_root
;
1183 path
= btrfs_alloc_path();
1187 trans
= btrfs_start_transaction(root
, 0);
1188 if (IS_ERR(trans
)) {
1189 btrfs_free_path(path
);
1190 return PTR_ERR(trans
);
1192 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1193 key
.type
= BTRFS_DEV_ITEM_KEY
;
1194 key
.offset
= device
->devid
;
1197 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1206 ret
= btrfs_del_item(trans
, root
, path
);
1210 btrfs_free_path(path
);
1211 unlock_chunks(root
);
1212 btrfs_commit_transaction(trans
, root
);
1216 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1218 struct btrfs_device
*device
;
1219 struct btrfs_device
*next_device
;
1220 struct block_device
*bdev
;
1221 struct buffer_head
*bh
= NULL
;
1222 struct btrfs_super_block
*disk_super
;
1223 struct btrfs_fs_devices
*cur_devices
;
1229 bool clear_super
= false;
1231 mutex_lock(&uuid_mutex
);
1232 mutex_lock(&root
->fs_info
->volume_mutex
);
1234 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1235 root
->fs_info
->avail_system_alloc_bits
|
1236 root
->fs_info
->avail_metadata_alloc_bits
;
1238 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1239 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1240 printk(KERN_ERR
"btrfs: unable to go below four devices "
1246 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1247 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1248 printk(KERN_ERR
"btrfs: unable to go below two "
1249 "devices on raid1\n");
1254 if (strcmp(device_path
, "missing") == 0) {
1255 struct list_head
*devices
;
1256 struct btrfs_device
*tmp
;
1259 devices
= &root
->fs_info
->fs_devices
->devices
;
1261 * It is safe to read the devices since the volume_mutex
1264 list_for_each_entry(tmp
, devices
, dev_list
) {
1265 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1274 printk(KERN_ERR
"btrfs: no missing devices found to "
1279 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1280 root
->fs_info
->bdev_holder
);
1282 ret
= PTR_ERR(bdev
);
1286 set_blocksize(bdev
, 4096);
1287 bh
= btrfs_read_dev_super(bdev
);
1292 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1293 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1294 dev_uuid
= disk_super
->dev_item
.uuid
;
1295 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1303 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1304 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1310 if (device
->writeable
) {
1312 list_del_init(&device
->dev_alloc_list
);
1313 unlock_chunks(root
);
1314 root
->fs_info
->fs_devices
->rw_devices
--;
1318 ret
= btrfs_shrink_device(device
, 0);
1322 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1326 device
->in_fs_metadata
= 0;
1327 btrfs_scrub_cancel_dev(root
, device
);
1330 * the device list mutex makes sure that we don't change
1331 * the device list while someone else is writing out all
1332 * the device supers.
1335 cur_devices
= device
->fs_devices
;
1336 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1337 list_del_rcu(&device
->dev_list
);
1339 device
->fs_devices
->num_devices
--;
1341 if (device
->missing
)
1342 root
->fs_info
->fs_devices
->missing_devices
--;
1344 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1345 struct btrfs_device
, dev_list
);
1346 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1347 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1348 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1349 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1352 device
->fs_devices
->open_devices
--;
1354 call_rcu(&device
->rcu
, free_device
);
1355 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1357 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1358 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1360 if (cur_devices
->open_devices
== 0) {
1361 struct btrfs_fs_devices
*fs_devices
;
1362 fs_devices
= root
->fs_info
->fs_devices
;
1363 while (fs_devices
) {
1364 if (fs_devices
->seed
== cur_devices
)
1366 fs_devices
= fs_devices
->seed
;
1368 fs_devices
->seed
= cur_devices
->seed
;
1369 cur_devices
->seed
= NULL
;
1371 __btrfs_close_devices(cur_devices
);
1372 unlock_chunks(root
);
1373 free_fs_devices(cur_devices
);
1377 * at this point, the device is zero sized. We want to
1378 * remove it from the devices list and zero out the old super
1381 /* make sure this device isn't detected as part of
1384 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1385 set_buffer_dirty(bh
);
1386 sync_dirty_buffer(bh
);
1395 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1397 mutex_unlock(&root
->fs_info
->volume_mutex
);
1398 mutex_unlock(&uuid_mutex
);
1401 if (device
->writeable
) {
1403 list_add(&device
->dev_alloc_list
,
1404 &root
->fs_info
->fs_devices
->alloc_list
);
1405 unlock_chunks(root
);
1406 root
->fs_info
->fs_devices
->rw_devices
++;
1412 * does all the dirty work required for changing file system's UUID.
1414 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1415 struct btrfs_root
*root
)
1417 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1418 struct btrfs_fs_devices
*old_devices
;
1419 struct btrfs_fs_devices
*seed_devices
;
1420 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1421 struct btrfs_device
*device
;
1424 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1425 if (!fs_devices
->seeding
)
1428 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1432 old_devices
= clone_fs_devices(fs_devices
);
1433 if (IS_ERR(old_devices
)) {
1434 kfree(seed_devices
);
1435 return PTR_ERR(old_devices
);
1438 list_add(&old_devices
->list
, &fs_uuids
);
1440 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1441 seed_devices
->opened
= 1;
1442 INIT_LIST_HEAD(&seed_devices
->devices
);
1443 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1444 mutex_init(&seed_devices
->device_list_mutex
);
1446 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1447 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1449 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1451 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1452 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1453 device
->fs_devices
= seed_devices
;
1456 fs_devices
->seeding
= 0;
1457 fs_devices
->num_devices
= 0;
1458 fs_devices
->open_devices
= 0;
1459 fs_devices
->seed
= seed_devices
;
1461 generate_random_uuid(fs_devices
->fsid
);
1462 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1463 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1464 super_flags
= btrfs_super_flags(disk_super
) &
1465 ~BTRFS_SUPER_FLAG_SEEDING
;
1466 btrfs_set_super_flags(disk_super
, super_flags
);
1472 * strore the expected generation for seed devices in device items.
1474 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1475 struct btrfs_root
*root
)
1477 struct btrfs_path
*path
;
1478 struct extent_buffer
*leaf
;
1479 struct btrfs_dev_item
*dev_item
;
1480 struct btrfs_device
*device
;
1481 struct btrfs_key key
;
1482 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1483 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1487 path
= btrfs_alloc_path();
1491 root
= root
->fs_info
->chunk_root
;
1492 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1494 key
.type
= BTRFS_DEV_ITEM_KEY
;
1497 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1501 leaf
= path
->nodes
[0];
1503 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1504 ret
= btrfs_next_leaf(root
, path
);
1509 leaf
= path
->nodes
[0];
1510 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1511 btrfs_release_path(path
);
1515 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1516 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1517 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1520 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1521 struct btrfs_dev_item
);
1522 devid
= btrfs_device_id(leaf
, dev_item
);
1523 read_extent_buffer(leaf
, dev_uuid
,
1524 (unsigned long)btrfs_device_uuid(dev_item
),
1526 read_extent_buffer(leaf
, fs_uuid
,
1527 (unsigned long)btrfs_device_fsid(dev_item
),
1529 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1532 if (device
->fs_devices
->seeding
) {
1533 btrfs_set_device_generation(leaf
, dev_item
,
1534 device
->generation
);
1535 btrfs_mark_buffer_dirty(leaf
);
1543 btrfs_free_path(path
);
1547 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1549 struct btrfs_trans_handle
*trans
;
1550 struct btrfs_device
*device
;
1551 struct block_device
*bdev
;
1552 struct list_head
*devices
;
1553 struct super_block
*sb
= root
->fs_info
->sb
;
1555 int seeding_dev
= 0;
1558 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1561 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1562 root
->fs_info
->bdev_holder
);
1564 return PTR_ERR(bdev
);
1566 if (root
->fs_info
->fs_devices
->seeding
) {
1568 down_write(&sb
->s_umount
);
1569 mutex_lock(&uuid_mutex
);
1572 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1573 mutex_lock(&root
->fs_info
->volume_mutex
);
1575 devices
= &root
->fs_info
->fs_devices
->devices
;
1577 * we have the volume lock, so we don't need the extra
1578 * device list mutex while reading the list here.
1580 list_for_each_entry(device
, devices
, dev_list
) {
1581 if (device
->bdev
== bdev
) {
1587 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1589 /* we can safely leave the fs_devices entry around */
1594 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1595 if (!device
->name
) {
1601 ret
= find_next_devid(root
, &device
->devid
);
1603 kfree(device
->name
);
1608 trans
= btrfs_start_transaction(root
, 0);
1609 if (IS_ERR(trans
)) {
1610 kfree(device
->name
);
1612 ret
= PTR_ERR(trans
);
1618 device
->writeable
= 1;
1619 device
->work
.func
= pending_bios_fn
;
1620 generate_random_uuid(device
->uuid
);
1621 spin_lock_init(&device
->io_lock
);
1622 device
->generation
= trans
->transid
;
1623 device
->io_width
= root
->sectorsize
;
1624 device
->io_align
= root
->sectorsize
;
1625 device
->sector_size
= root
->sectorsize
;
1626 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1627 device
->disk_total_bytes
= device
->total_bytes
;
1628 device
->dev_root
= root
->fs_info
->dev_root
;
1629 device
->bdev
= bdev
;
1630 device
->in_fs_metadata
= 1;
1631 device
->mode
= FMODE_EXCL
;
1632 set_blocksize(device
->bdev
, 4096);
1635 sb
->s_flags
&= ~MS_RDONLY
;
1636 ret
= btrfs_prepare_sprout(trans
, root
);
1640 device
->fs_devices
= root
->fs_info
->fs_devices
;
1643 * we don't want write_supers to jump in here with our device
1646 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1647 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1648 list_add(&device
->dev_alloc_list
,
1649 &root
->fs_info
->fs_devices
->alloc_list
);
1650 root
->fs_info
->fs_devices
->num_devices
++;
1651 root
->fs_info
->fs_devices
->open_devices
++;
1652 root
->fs_info
->fs_devices
->rw_devices
++;
1653 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1655 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1656 root
->fs_info
->fs_devices
->rotating
= 1;
1658 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1659 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1660 total_bytes
+ device
->total_bytes
);
1662 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1663 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1665 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1668 ret
= init_first_rw_device(trans
, root
, device
);
1670 ret
= btrfs_finish_sprout(trans
, root
);
1673 ret
= btrfs_add_device(trans
, root
, device
);
1677 * we've got more storage, clear any full flags on the space
1680 btrfs_clear_space_info_full(root
->fs_info
);
1682 unlock_chunks(root
);
1683 btrfs_commit_transaction(trans
, root
);
1686 mutex_unlock(&uuid_mutex
);
1687 up_write(&sb
->s_umount
);
1689 ret
= btrfs_relocate_sys_chunks(root
);
1693 mutex_unlock(&root
->fs_info
->volume_mutex
);
1696 blkdev_put(bdev
, FMODE_EXCL
);
1698 mutex_unlock(&uuid_mutex
);
1699 up_write(&sb
->s_umount
);
1704 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1705 struct btrfs_device
*device
)
1708 struct btrfs_path
*path
;
1709 struct btrfs_root
*root
;
1710 struct btrfs_dev_item
*dev_item
;
1711 struct extent_buffer
*leaf
;
1712 struct btrfs_key key
;
1714 root
= device
->dev_root
->fs_info
->chunk_root
;
1716 path
= btrfs_alloc_path();
1720 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1721 key
.type
= BTRFS_DEV_ITEM_KEY
;
1722 key
.offset
= device
->devid
;
1724 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1733 leaf
= path
->nodes
[0];
1734 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1736 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1737 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1738 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1739 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1740 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1741 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1742 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1743 btrfs_mark_buffer_dirty(leaf
);
1746 btrfs_free_path(path
);
1750 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1751 struct btrfs_device
*device
, u64 new_size
)
1753 struct btrfs_super_block
*super_copy
=
1754 &device
->dev_root
->fs_info
->super_copy
;
1755 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1756 u64 diff
= new_size
- device
->total_bytes
;
1758 if (!device
->writeable
)
1760 if (new_size
<= device
->total_bytes
)
1763 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1764 device
->fs_devices
->total_rw_bytes
+= diff
;
1766 device
->total_bytes
= new_size
;
1767 device
->disk_total_bytes
= new_size
;
1768 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1770 return btrfs_update_device(trans
, device
);
1773 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1774 struct btrfs_device
*device
, u64 new_size
)
1777 lock_chunks(device
->dev_root
);
1778 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1779 unlock_chunks(device
->dev_root
);
1783 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1784 struct btrfs_root
*root
,
1785 u64 chunk_tree
, u64 chunk_objectid
,
1789 struct btrfs_path
*path
;
1790 struct btrfs_key key
;
1792 root
= root
->fs_info
->chunk_root
;
1793 path
= btrfs_alloc_path();
1797 key
.objectid
= chunk_objectid
;
1798 key
.offset
= chunk_offset
;
1799 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1801 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1804 ret
= btrfs_del_item(trans
, root
, path
);
1806 btrfs_free_path(path
);
1810 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1813 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1814 struct btrfs_disk_key
*disk_key
;
1815 struct btrfs_chunk
*chunk
;
1822 struct btrfs_key key
;
1824 array_size
= btrfs_super_sys_array_size(super_copy
);
1826 ptr
= super_copy
->sys_chunk_array
;
1829 while (cur
< array_size
) {
1830 disk_key
= (struct btrfs_disk_key
*)ptr
;
1831 btrfs_disk_key_to_cpu(&key
, disk_key
);
1833 len
= sizeof(*disk_key
);
1835 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1836 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1837 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1838 len
+= btrfs_chunk_item_size(num_stripes
);
1843 if (key
.objectid
== chunk_objectid
&&
1844 key
.offset
== chunk_offset
) {
1845 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1847 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1856 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1857 u64 chunk_tree
, u64 chunk_objectid
,
1860 struct extent_map_tree
*em_tree
;
1861 struct btrfs_root
*extent_root
;
1862 struct btrfs_trans_handle
*trans
;
1863 struct extent_map
*em
;
1864 struct map_lookup
*map
;
1868 root
= root
->fs_info
->chunk_root
;
1869 extent_root
= root
->fs_info
->extent_root
;
1870 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1872 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1876 /* step one, relocate all the extents inside this chunk */
1877 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1881 trans
= btrfs_start_transaction(root
, 0);
1882 BUG_ON(IS_ERR(trans
));
1887 * step two, delete the device extents and the
1888 * chunk tree entries
1890 read_lock(&em_tree
->lock
);
1891 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1892 read_unlock(&em_tree
->lock
);
1894 BUG_ON(em
->start
> chunk_offset
||
1895 em
->start
+ em
->len
< chunk_offset
);
1896 map
= (struct map_lookup
*)em
->bdev
;
1898 for (i
= 0; i
< map
->num_stripes
; i
++) {
1899 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1900 map
->stripes
[i
].physical
);
1903 if (map
->stripes
[i
].dev
) {
1904 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1908 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1913 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1915 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1916 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1920 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1923 write_lock(&em_tree
->lock
);
1924 remove_extent_mapping(em_tree
, em
);
1925 write_unlock(&em_tree
->lock
);
1930 /* once for the tree */
1931 free_extent_map(em
);
1933 free_extent_map(em
);
1935 unlock_chunks(root
);
1936 btrfs_end_transaction(trans
, root
);
1940 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1942 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1943 struct btrfs_path
*path
;
1944 struct extent_buffer
*leaf
;
1945 struct btrfs_chunk
*chunk
;
1946 struct btrfs_key key
;
1947 struct btrfs_key found_key
;
1948 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1950 bool retried
= false;
1954 path
= btrfs_alloc_path();
1959 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1960 key
.offset
= (u64
)-1;
1961 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1964 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1969 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1976 leaf
= path
->nodes
[0];
1977 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1979 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1980 struct btrfs_chunk
);
1981 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1982 btrfs_release_path(path
);
1984 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1985 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1994 if (found_key
.offset
== 0)
1996 key
.offset
= found_key
.offset
- 1;
1999 if (failed
&& !retried
) {
2003 } else if (failed
&& retried
) {
2008 btrfs_free_path(path
);
2012 static u64
div_factor(u64 num
, int factor
)
2021 int btrfs_balance(struct btrfs_root
*dev_root
)
2024 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2025 struct btrfs_device
*device
;
2028 struct btrfs_path
*path
;
2029 struct btrfs_key key
;
2030 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2031 struct btrfs_trans_handle
*trans
;
2032 struct btrfs_key found_key
;
2034 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2037 if (!capable(CAP_SYS_ADMIN
))
2040 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2041 dev_root
= dev_root
->fs_info
->dev_root
;
2043 /* step one make some room on all the devices */
2044 list_for_each_entry(device
, devices
, dev_list
) {
2045 old_size
= device
->total_bytes
;
2046 size_to_free
= div_factor(old_size
, 1);
2047 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2048 if (!device
->writeable
||
2049 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2052 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2057 trans
= btrfs_start_transaction(dev_root
, 0);
2058 BUG_ON(IS_ERR(trans
));
2060 ret
= btrfs_grow_device(trans
, device
, old_size
);
2063 btrfs_end_transaction(trans
, dev_root
);
2066 /* step two, relocate all the chunks */
2067 path
= btrfs_alloc_path();
2070 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2071 key
.offset
= (u64
)-1;
2072 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2075 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2080 * this shouldn't happen, it means the last relocate
2086 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2087 BTRFS_CHUNK_ITEM_KEY
);
2091 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2093 if (found_key
.objectid
!= key
.objectid
)
2096 /* chunk zero is special */
2097 if (found_key
.offset
== 0)
2100 btrfs_release_path(path
);
2101 ret
= btrfs_relocate_chunk(chunk_root
,
2102 chunk_root
->root_key
.objectid
,
2105 BUG_ON(ret
&& ret
!= -ENOSPC
);
2106 key
.offset
= found_key
.offset
- 1;
2110 btrfs_free_path(path
);
2111 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2116 * shrinking a device means finding all of the device extents past
2117 * the new size, and then following the back refs to the chunks.
2118 * The chunk relocation code actually frees the device extent
2120 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2122 struct btrfs_trans_handle
*trans
;
2123 struct btrfs_root
*root
= device
->dev_root
;
2124 struct btrfs_dev_extent
*dev_extent
= NULL
;
2125 struct btrfs_path
*path
;
2133 bool retried
= false;
2134 struct extent_buffer
*l
;
2135 struct btrfs_key key
;
2136 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2137 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2138 u64 old_size
= device
->total_bytes
;
2139 u64 diff
= device
->total_bytes
- new_size
;
2141 if (new_size
>= device
->total_bytes
)
2144 path
= btrfs_alloc_path();
2152 device
->total_bytes
= new_size
;
2153 if (device
->writeable
)
2154 device
->fs_devices
->total_rw_bytes
-= diff
;
2155 unlock_chunks(root
);
2158 key
.objectid
= device
->devid
;
2159 key
.offset
= (u64
)-1;
2160 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2163 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2167 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2172 btrfs_release_path(path
);
2177 slot
= path
->slots
[0];
2178 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2180 if (key
.objectid
!= device
->devid
) {
2181 btrfs_release_path(path
);
2185 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2186 length
= btrfs_dev_extent_length(l
, dev_extent
);
2188 if (key
.offset
+ length
<= new_size
) {
2189 btrfs_release_path(path
);
2193 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2194 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2195 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2196 btrfs_release_path(path
);
2198 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2200 if (ret
&& ret
!= -ENOSPC
)
2207 if (failed
&& !retried
) {
2211 } else if (failed
&& retried
) {
2215 device
->total_bytes
= old_size
;
2216 if (device
->writeable
)
2217 device
->fs_devices
->total_rw_bytes
+= diff
;
2218 unlock_chunks(root
);
2222 /* Shrinking succeeded, else we would be at "done". */
2223 trans
= btrfs_start_transaction(root
, 0);
2224 if (IS_ERR(trans
)) {
2225 ret
= PTR_ERR(trans
);
2231 device
->disk_total_bytes
= new_size
;
2232 /* Now btrfs_update_device() will change the on-disk size. */
2233 ret
= btrfs_update_device(trans
, device
);
2235 unlock_chunks(root
);
2236 btrfs_end_transaction(trans
, root
);
2239 WARN_ON(diff
> old_total
);
2240 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2241 unlock_chunks(root
);
2242 btrfs_end_transaction(trans
, root
);
2244 btrfs_free_path(path
);
2248 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2249 struct btrfs_root
*root
,
2250 struct btrfs_key
*key
,
2251 struct btrfs_chunk
*chunk
, int item_size
)
2253 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2254 struct btrfs_disk_key disk_key
;
2258 array_size
= btrfs_super_sys_array_size(super_copy
);
2259 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2262 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2263 btrfs_cpu_key_to_disk(&disk_key
, key
);
2264 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2265 ptr
+= sizeof(disk_key
);
2266 memcpy(ptr
, chunk
, item_size
);
2267 item_size
+= sizeof(disk_key
);
2268 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2273 * sort the devices in descending order by max_avail, total_avail
2275 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2277 const struct btrfs_device_info
*di_a
= a
;
2278 const struct btrfs_device_info
*di_b
= b
;
2280 if (di_a
->max_avail
> di_b
->max_avail
)
2282 if (di_a
->max_avail
< di_b
->max_avail
)
2284 if (di_a
->total_avail
> di_b
->total_avail
)
2286 if (di_a
->total_avail
< di_b
->total_avail
)
2291 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2292 struct btrfs_root
*extent_root
,
2293 struct map_lookup
**map_ret
,
2294 u64
*num_bytes_out
, u64
*stripe_size_out
,
2295 u64 start
, u64 type
)
2297 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2298 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2299 struct list_head
*cur
;
2300 struct map_lookup
*map
= NULL
;
2301 struct extent_map_tree
*em_tree
;
2302 struct extent_map
*em
;
2303 struct btrfs_device_info
*devices_info
= NULL
;
2305 int num_stripes
; /* total number of stripes to allocate */
2306 int sub_stripes
; /* sub_stripes info for map */
2307 int dev_stripes
; /* stripes per dev */
2308 int devs_max
; /* max devs to use */
2309 int devs_min
; /* min devs needed */
2310 int devs_increment
; /* ndevs has to be a multiple of this */
2311 int ncopies
; /* how many copies to data has */
2313 u64 max_stripe_size
;
2321 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2322 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2324 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2327 if (list_empty(&fs_devices
->alloc_list
))
2334 devs_max
= 0; /* 0 == as many as possible */
2338 * define the properties of each RAID type.
2339 * FIXME: move this to a global table and use it in all RAID
2342 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2346 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2348 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2353 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2362 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2363 max_stripe_size
= 1024 * 1024 * 1024;
2364 max_chunk_size
= 10 * max_stripe_size
;
2365 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2366 max_stripe_size
= 256 * 1024 * 1024;
2367 max_chunk_size
= max_stripe_size
;
2368 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2369 max_stripe_size
= 8 * 1024 * 1024;
2370 max_chunk_size
= 2 * max_stripe_size
;
2372 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
2377 /* we don't want a chunk larger than 10% of writeable space */
2378 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2381 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2386 cur
= fs_devices
->alloc_list
.next
;
2389 * in the first pass through the devices list, we gather information
2390 * about the available holes on each device.
2393 while (cur
!= &fs_devices
->alloc_list
) {
2394 struct btrfs_device
*device
;
2398 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2402 if (!device
->writeable
) {
2404 "btrfs: read-only device in alloc_list\n");
2409 if (!device
->in_fs_metadata
)
2412 if (device
->total_bytes
> device
->bytes_used
)
2413 total_avail
= device
->total_bytes
- device
->bytes_used
;
2416 /* avail is off by max(alloc_start, 1MB), but that is the same
2417 * for all devices, so it doesn't hurt the sorting later on
2420 ret
= find_free_dev_extent(trans
, device
,
2421 max_stripe_size
* dev_stripes
,
2422 &dev_offset
, &max_avail
);
2423 if (ret
&& ret
!= -ENOSPC
)
2427 max_avail
= max_stripe_size
* dev_stripes
;
2429 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
2432 devices_info
[ndevs
].dev_offset
= dev_offset
;
2433 devices_info
[ndevs
].max_avail
= max_avail
;
2434 devices_info
[ndevs
].total_avail
= total_avail
;
2435 devices_info
[ndevs
].dev
= device
;
2440 * now sort the devices by hole size / available space
2442 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
2443 btrfs_cmp_device_info
, NULL
);
2445 /* round down to number of usable stripes */
2446 ndevs
-= ndevs
% devs_increment
;
2448 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
2453 if (devs_max
&& ndevs
> devs_max
)
2456 * the primary goal is to maximize the number of stripes, so use as many
2457 * devices as possible, even if the stripes are not maximum sized.
2459 stripe_size
= devices_info
[ndevs
-1].max_avail
;
2460 num_stripes
= ndevs
* dev_stripes
;
2462 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
2463 stripe_size
= max_chunk_size
* ncopies
;
2464 do_div(stripe_size
, num_stripes
);
2467 do_div(stripe_size
, dev_stripes
);
2468 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
2469 stripe_size
*= BTRFS_STRIPE_LEN
;
2471 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2476 map
->num_stripes
= num_stripes
;
2478 for (i
= 0; i
< ndevs
; ++i
) {
2479 for (j
= 0; j
< dev_stripes
; ++j
) {
2480 int s
= i
* dev_stripes
+ j
;
2481 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
2482 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
2486 map
->sector_size
= extent_root
->sectorsize
;
2487 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2488 map
->io_align
= BTRFS_STRIPE_LEN
;
2489 map
->io_width
= BTRFS_STRIPE_LEN
;
2491 map
->sub_stripes
= sub_stripes
;
2494 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
2496 *stripe_size_out
= stripe_size
;
2497 *num_bytes_out
= num_bytes
;
2499 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
2501 em
= alloc_extent_map();
2506 em
->bdev
= (struct block_device
*)map
;
2508 em
->len
= num_bytes
;
2509 em
->block_start
= 0;
2510 em
->block_len
= em
->len
;
2512 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2513 write_lock(&em_tree
->lock
);
2514 ret
= add_extent_mapping(em_tree
, em
);
2515 write_unlock(&em_tree
->lock
);
2517 free_extent_map(em
);
2519 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2520 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2524 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2525 struct btrfs_device
*device
;
2528 device
= map
->stripes
[i
].dev
;
2529 dev_offset
= map
->stripes
[i
].physical
;
2531 ret
= btrfs_alloc_dev_extent(trans
, device
,
2532 info
->chunk_root
->root_key
.objectid
,
2533 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2534 start
, dev_offset
, stripe_size
);
2538 kfree(devices_info
);
2543 kfree(devices_info
);
2547 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2548 struct btrfs_root
*extent_root
,
2549 struct map_lookup
*map
, u64 chunk_offset
,
2550 u64 chunk_size
, u64 stripe_size
)
2553 struct btrfs_key key
;
2554 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2555 struct btrfs_device
*device
;
2556 struct btrfs_chunk
*chunk
;
2557 struct btrfs_stripe
*stripe
;
2558 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2562 chunk
= kzalloc(item_size
, GFP_NOFS
);
2567 while (index
< map
->num_stripes
) {
2568 device
= map
->stripes
[index
].dev
;
2569 device
->bytes_used
+= stripe_size
;
2570 ret
= btrfs_update_device(trans
, device
);
2576 stripe
= &chunk
->stripe
;
2577 while (index
< map
->num_stripes
) {
2578 device
= map
->stripes
[index
].dev
;
2579 dev_offset
= map
->stripes
[index
].physical
;
2581 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2582 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2583 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2588 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2589 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2590 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2591 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2592 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2593 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2594 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2595 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2596 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2598 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2599 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2600 key
.offset
= chunk_offset
;
2602 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2605 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2606 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2616 * Chunk allocation falls into two parts. The first part does works
2617 * that make the new allocated chunk useable, but not do any operation
2618 * that modifies the chunk tree. The second part does the works that
2619 * require modifying the chunk tree. This division is important for the
2620 * bootstrap process of adding storage to a seed btrfs.
2622 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2623 struct btrfs_root
*extent_root
, u64 type
)
2628 struct map_lookup
*map
;
2629 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2632 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2637 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2638 &stripe_size
, chunk_offset
, type
);
2642 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2643 chunk_size
, stripe_size
);
2648 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2649 struct btrfs_root
*root
,
2650 struct btrfs_device
*device
)
2653 u64 sys_chunk_offset
;
2657 u64 sys_stripe_size
;
2659 struct map_lookup
*map
;
2660 struct map_lookup
*sys_map
;
2661 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2662 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2665 ret
= find_next_chunk(fs_info
->chunk_root
,
2666 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2669 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2670 (fs_info
->metadata_alloc_profile
&
2671 fs_info
->avail_metadata_alloc_bits
);
2672 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2674 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2675 &stripe_size
, chunk_offset
, alloc_profile
);
2678 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2680 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2681 (fs_info
->system_alloc_profile
&
2682 fs_info
->avail_system_alloc_bits
);
2683 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2685 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2686 &sys_chunk_size
, &sys_stripe_size
,
2687 sys_chunk_offset
, alloc_profile
);
2690 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2694 * Modifying chunk tree needs allocating new blocks from both
2695 * system block group and metadata block group. So we only can
2696 * do operations require modifying the chunk tree after both
2697 * block groups were created.
2699 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2700 chunk_size
, stripe_size
);
2703 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2704 sys_chunk_offset
, sys_chunk_size
,
2710 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2712 struct extent_map
*em
;
2713 struct map_lookup
*map
;
2714 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2718 read_lock(&map_tree
->map_tree
.lock
);
2719 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2720 read_unlock(&map_tree
->map_tree
.lock
);
2724 if (btrfs_test_opt(root
, DEGRADED
)) {
2725 free_extent_map(em
);
2729 map
= (struct map_lookup
*)em
->bdev
;
2730 for (i
= 0; i
< map
->num_stripes
; i
++) {
2731 if (!map
->stripes
[i
].dev
->writeable
) {
2736 free_extent_map(em
);
2740 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2742 extent_map_tree_init(&tree
->map_tree
);
2745 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2747 struct extent_map
*em
;
2750 write_lock(&tree
->map_tree
.lock
);
2751 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2753 remove_extent_mapping(&tree
->map_tree
, em
);
2754 write_unlock(&tree
->map_tree
.lock
);
2759 free_extent_map(em
);
2760 /* once for the tree */
2761 free_extent_map(em
);
2765 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2767 struct extent_map
*em
;
2768 struct map_lookup
*map
;
2769 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2772 read_lock(&em_tree
->lock
);
2773 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2774 read_unlock(&em_tree
->lock
);
2777 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2778 map
= (struct map_lookup
*)em
->bdev
;
2779 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2780 ret
= map
->num_stripes
;
2781 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2782 ret
= map
->sub_stripes
;
2785 free_extent_map(em
);
2789 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2793 if (map
->stripes
[optimal
].dev
->bdev
)
2795 for (i
= first
; i
< first
+ num
; i
++) {
2796 if (map
->stripes
[i
].dev
->bdev
)
2799 /* we couldn't find one that doesn't fail. Just return something
2800 * and the io error handling code will clean up eventually
2805 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2806 u64 logical
, u64
*length
,
2807 struct btrfs_multi_bio
**multi_ret
,
2810 struct extent_map
*em
;
2811 struct map_lookup
*map
;
2812 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2815 u64 stripe_end_offset
;
2819 int stripes_allocated
= 8;
2820 int stripes_required
= 1;
2825 struct btrfs_multi_bio
*multi
= NULL
;
2827 if (multi_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2828 stripes_allocated
= 1;
2831 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2836 atomic_set(&multi
->error
, 0);
2839 read_lock(&em_tree
->lock
);
2840 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2841 read_unlock(&em_tree
->lock
);
2844 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2845 (unsigned long long)logical
,
2846 (unsigned long long)*length
);
2850 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2851 map
= (struct map_lookup
*)em
->bdev
;
2852 offset
= logical
- em
->start
;
2854 if (mirror_num
> map
->num_stripes
)
2857 /* if our multi bio struct is too small, back off and try again */
2858 if (rw
& REQ_WRITE
) {
2859 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2860 BTRFS_BLOCK_GROUP_DUP
)) {
2861 stripes_required
= map
->num_stripes
;
2863 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2864 stripes_required
= map
->sub_stripes
;
2868 if (rw
& REQ_DISCARD
) {
2869 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2870 BTRFS_BLOCK_GROUP_RAID1
|
2871 BTRFS_BLOCK_GROUP_DUP
|
2872 BTRFS_BLOCK_GROUP_RAID10
)) {
2873 stripes_required
= map
->num_stripes
;
2876 if (multi_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2877 stripes_allocated
< stripes_required
) {
2878 stripes_allocated
= map
->num_stripes
;
2879 free_extent_map(em
);
2885 * stripe_nr counts the total number of stripes we have to stride
2886 * to get to this block
2888 do_div(stripe_nr
, map
->stripe_len
);
2890 stripe_offset
= stripe_nr
* map
->stripe_len
;
2891 BUG_ON(offset
< stripe_offset
);
2893 /* stripe_offset is the offset of this block in its stripe*/
2894 stripe_offset
= offset
- stripe_offset
;
2896 if (rw
& REQ_DISCARD
)
2897 *length
= min_t(u64
, em
->len
- offset
, *length
);
2898 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2899 BTRFS_BLOCK_GROUP_RAID1
|
2900 BTRFS_BLOCK_GROUP_RAID10
|
2901 BTRFS_BLOCK_GROUP_DUP
)) {
2902 /* we limit the length of each bio to what fits in a stripe */
2903 *length
= min_t(u64
, em
->len
- offset
,
2904 map
->stripe_len
- stripe_offset
);
2906 *length
= em
->len
- offset
;
2914 stripe_nr_orig
= stripe_nr
;
2915 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
2916 (~(map
->stripe_len
- 1));
2917 do_div(stripe_nr_end
, map
->stripe_len
);
2918 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
2920 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2921 if (rw
& REQ_DISCARD
)
2922 num_stripes
= min_t(u64
, map
->num_stripes
,
2923 stripe_nr_end
- stripe_nr_orig
);
2924 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2925 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2926 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2927 num_stripes
= map
->num_stripes
;
2928 else if (mirror_num
)
2929 stripe_index
= mirror_num
- 1;
2931 stripe_index
= find_live_mirror(map
, 0,
2933 current
->pid
% map
->num_stripes
);
2936 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2937 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2938 num_stripes
= map
->num_stripes
;
2939 else if (mirror_num
)
2940 stripe_index
= mirror_num
- 1;
2942 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2943 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2945 stripe_index
= do_div(stripe_nr
, factor
);
2946 stripe_index
*= map
->sub_stripes
;
2949 num_stripes
= map
->sub_stripes
;
2950 else if (rw
& REQ_DISCARD
)
2951 num_stripes
= min_t(u64
, map
->sub_stripes
*
2952 (stripe_nr_end
- stripe_nr_orig
),
2954 else if (mirror_num
)
2955 stripe_index
+= mirror_num
- 1;
2957 stripe_index
= find_live_mirror(map
, stripe_index
,
2958 map
->sub_stripes
, stripe_index
+
2959 current
->pid
% map
->sub_stripes
);
2963 * after this do_div call, stripe_nr is the number of stripes
2964 * on this device we have to walk to find the data, and
2965 * stripe_index is the number of our device in the stripe array
2967 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2969 BUG_ON(stripe_index
>= map
->num_stripes
);
2971 if (rw
& REQ_DISCARD
) {
2972 for (i
= 0; i
< num_stripes
; i
++) {
2973 multi
->stripes
[i
].physical
=
2974 map
->stripes
[stripe_index
].physical
+
2975 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2976 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2978 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2980 u32 last_stripe
= 0;
2983 div_u64_rem(stripe_nr_end
- 1,
2987 for (j
= 0; j
< map
->num_stripes
; j
++) {
2990 div_u64_rem(stripe_nr_end
- 1 - j
,
2991 map
->num_stripes
, &test
);
2992 if (test
== stripe_index
)
2995 stripes
= stripe_nr_end
- 1 - j
;
2996 do_div(stripes
, map
->num_stripes
);
2997 multi
->stripes
[i
].length
= map
->stripe_len
*
2998 (stripes
- stripe_nr
+ 1);
3001 multi
->stripes
[i
].length
-=
3005 if (stripe_index
== last_stripe
)
3006 multi
->stripes
[i
].length
-=
3008 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3011 int factor
= map
->num_stripes
/
3013 u32 last_stripe
= 0;
3015 div_u64_rem(stripe_nr_end
- 1,
3016 factor
, &last_stripe
);
3017 last_stripe
*= map
->sub_stripes
;
3019 for (j
= 0; j
< factor
; j
++) {
3022 div_u64_rem(stripe_nr_end
- 1 - j
,
3026 stripe_index
/ map
->sub_stripes
)
3029 stripes
= stripe_nr_end
- 1 - j
;
3030 do_div(stripes
, factor
);
3031 multi
->stripes
[i
].length
= map
->stripe_len
*
3032 (stripes
- stripe_nr
+ 1);
3034 if (i
< map
->sub_stripes
) {
3035 multi
->stripes
[i
].length
-=
3037 if (i
== map
->sub_stripes
- 1)
3040 if (stripe_index
>= last_stripe
&&
3041 stripe_index
<= (last_stripe
+
3042 map
->sub_stripes
- 1)) {
3043 multi
->stripes
[i
].length
-=
3047 multi
->stripes
[i
].length
= *length
;
3050 if (stripe_index
== map
->num_stripes
) {
3051 /* This could only happen for RAID0/10 */
3057 for (i
= 0; i
< num_stripes
; i
++) {
3058 multi
->stripes
[i
].physical
=
3059 map
->stripes
[stripe_index
].physical
+
3061 stripe_nr
* map
->stripe_len
;
3062 multi
->stripes
[i
].dev
=
3063 map
->stripes
[stripe_index
].dev
;
3069 multi
->num_stripes
= num_stripes
;
3070 multi
->max_errors
= max_errors
;
3073 free_extent_map(em
);
3077 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3078 u64 logical
, u64
*length
,
3079 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3081 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3085 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3086 u64 chunk_start
, u64 physical
, u64 devid
,
3087 u64
**logical
, int *naddrs
, int *stripe_len
)
3089 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3090 struct extent_map
*em
;
3091 struct map_lookup
*map
;
3098 read_lock(&em_tree
->lock
);
3099 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3100 read_unlock(&em_tree
->lock
);
3102 BUG_ON(!em
|| em
->start
!= chunk_start
);
3103 map
= (struct map_lookup
*)em
->bdev
;
3106 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3107 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3108 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3109 do_div(length
, map
->num_stripes
);
3111 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3114 for (i
= 0; i
< map
->num_stripes
; i
++) {
3115 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3117 if (map
->stripes
[i
].physical
> physical
||
3118 map
->stripes
[i
].physical
+ length
<= physical
)
3121 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3122 do_div(stripe_nr
, map
->stripe_len
);
3124 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3125 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3126 do_div(stripe_nr
, map
->sub_stripes
);
3127 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3128 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3130 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3131 WARN_ON(nr
>= map
->num_stripes
);
3132 for (j
= 0; j
< nr
; j
++) {
3133 if (buf
[j
] == bytenr
)
3137 WARN_ON(nr
>= map
->num_stripes
);
3144 *stripe_len
= map
->stripe_len
;
3146 free_extent_map(em
);
3150 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3152 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3153 int is_orig_bio
= 0;
3156 atomic_inc(&multi
->error
);
3158 if (bio
== multi
->orig_bio
)
3161 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3164 bio
= multi
->orig_bio
;
3166 bio
->bi_private
= multi
->private;
3167 bio
->bi_end_io
= multi
->end_io
;
3168 /* only send an error to the higher layers if it is
3169 * beyond the tolerance of the multi-bio
3171 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3175 * this bio is actually up to date, we didn't
3176 * go over the max number of errors
3178 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3183 bio_endio(bio
, err
);
3184 } else if (!is_orig_bio
) {
3189 struct async_sched
{
3192 struct btrfs_fs_info
*info
;
3193 struct btrfs_work work
;
3197 * see run_scheduled_bios for a description of why bios are collected for
3200 * This will add one bio to the pending list for a device and make sure
3201 * the work struct is scheduled.
3203 static noinline
int schedule_bio(struct btrfs_root
*root
,
3204 struct btrfs_device
*device
,
3205 int rw
, struct bio
*bio
)
3207 int should_queue
= 1;
3208 struct btrfs_pending_bios
*pending_bios
;
3210 /* don't bother with additional async steps for reads, right now */
3211 if (!(rw
& REQ_WRITE
)) {
3213 submit_bio(rw
, bio
);
3219 * nr_async_bios allows us to reliably return congestion to the
3220 * higher layers. Otherwise, the async bio makes it appear we have
3221 * made progress against dirty pages when we've really just put it
3222 * on a queue for later
3224 atomic_inc(&root
->fs_info
->nr_async_bios
);
3225 WARN_ON(bio
->bi_next
);
3226 bio
->bi_next
= NULL
;
3229 spin_lock(&device
->io_lock
);
3230 if (bio
->bi_rw
& REQ_SYNC
)
3231 pending_bios
= &device
->pending_sync_bios
;
3233 pending_bios
= &device
->pending_bios
;
3235 if (pending_bios
->tail
)
3236 pending_bios
->tail
->bi_next
= bio
;
3238 pending_bios
->tail
= bio
;
3239 if (!pending_bios
->head
)
3240 pending_bios
->head
= bio
;
3241 if (device
->running_pending
)
3244 spin_unlock(&device
->io_lock
);
3247 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3252 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3253 int mirror_num
, int async_submit
)
3255 struct btrfs_mapping_tree
*map_tree
;
3256 struct btrfs_device
*dev
;
3257 struct bio
*first_bio
= bio
;
3258 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3261 struct btrfs_multi_bio
*multi
= NULL
;
3266 length
= bio
->bi_size
;
3267 map_tree
= &root
->fs_info
->mapping_tree
;
3268 map_length
= length
;
3270 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3274 total_devs
= multi
->num_stripes
;
3275 if (map_length
< length
) {
3276 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3277 "len %llu\n", (unsigned long long)logical
,
3278 (unsigned long long)length
,
3279 (unsigned long long)map_length
);
3282 multi
->end_io
= first_bio
->bi_end_io
;
3283 multi
->private = first_bio
->bi_private
;
3284 multi
->orig_bio
= first_bio
;
3285 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3287 while (dev_nr
< total_devs
) {
3288 if (total_devs
> 1) {
3289 if (dev_nr
< total_devs
- 1) {
3290 bio
= bio_clone(first_bio
, GFP_NOFS
);
3295 bio
->bi_private
= multi
;
3296 bio
->bi_end_io
= end_bio_multi_stripe
;
3298 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3299 dev
= multi
->stripes
[dev_nr
].dev
;
3300 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3301 bio
->bi_bdev
= dev
->bdev
;
3303 schedule_bio(root
, dev
, rw
, bio
);
3305 submit_bio(rw
, bio
);
3307 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3308 bio
->bi_sector
= logical
>> 9;
3309 bio_endio(bio
, -EIO
);
3313 if (total_devs
== 1)
3318 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3321 struct btrfs_device
*device
;
3322 struct btrfs_fs_devices
*cur_devices
;
3324 cur_devices
= root
->fs_info
->fs_devices
;
3325 while (cur_devices
) {
3327 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3328 device
= __find_device(&cur_devices
->devices
,
3333 cur_devices
= cur_devices
->seed
;
3338 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3339 u64 devid
, u8
*dev_uuid
)
3341 struct btrfs_device
*device
;
3342 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3344 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3347 list_add(&device
->dev_list
,
3348 &fs_devices
->devices
);
3349 device
->dev_root
= root
->fs_info
->dev_root
;
3350 device
->devid
= devid
;
3351 device
->work
.func
= pending_bios_fn
;
3352 device
->fs_devices
= fs_devices
;
3353 device
->missing
= 1;
3354 fs_devices
->num_devices
++;
3355 fs_devices
->missing_devices
++;
3356 spin_lock_init(&device
->io_lock
);
3357 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3358 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3362 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3363 struct extent_buffer
*leaf
,
3364 struct btrfs_chunk
*chunk
)
3366 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3367 struct map_lookup
*map
;
3368 struct extent_map
*em
;
3372 u8 uuid
[BTRFS_UUID_SIZE
];
3377 logical
= key
->offset
;
3378 length
= btrfs_chunk_length(leaf
, chunk
);
3380 read_lock(&map_tree
->map_tree
.lock
);
3381 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3382 read_unlock(&map_tree
->map_tree
.lock
);
3384 /* already mapped? */
3385 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3386 free_extent_map(em
);
3389 free_extent_map(em
);
3392 em
= alloc_extent_map();
3395 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3396 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3398 free_extent_map(em
);
3402 em
->bdev
= (struct block_device
*)map
;
3403 em
->start
= logical
;
3405 em
->block_start
= 0;
3406 em
->block_len
= em
->len
;
3408 map
->num_stripes
= num_stripes
;
3409 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3410 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3411 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3412 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3413 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3414 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3415 for (i
= 0; i
< num_stripes
; i
++) {
3416 map
->stripes
[i
].physical
=
3417 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3418 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3419 read_extent_buffer(leaf
, uuid
, (unsigned long)
3420 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3422 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3424 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3426 free_extent_map(em
);
3429 if (!map
->stripes
[i
].dev
) {
3430 map
->stripes
[i
].dev
=
3431 add_missing_dev(root
, devid
, uuid
);
3432 if (!map
->stripes
[i
].dev
) {
3434 free_extent_map(em
);
3438 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3441 write_lock(&map_tree
->map_tree
.lock
);
3442 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3443 write_unlock(&map_tree
->map_tree
.lock
);
3445 free_extent_map(em
);
3450 static int fill_device_from_item(struct extent_buffer
*leaf
,
3451 struct btrfs_dev_item
*dev_item
,
3452 struct btrfs_device
*device
)
3456 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3457 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3458 device
->total_bytes
= device
->disk_total_bytes
;
3459 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3460 device
->type
= btrfs_device_type(leaf
, dev_item
);
3461 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3462 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3463 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3465 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3466 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3471 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3473 struct btrfs_fs_devices
*fs_devices
;
3476 mutex_lock(&uuid_mutex
);
3478 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3479 while (fs_devices
) {
3480 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3484 fs_devices
= fs_devices
->seed
;
3487 fs_devices
= find_fsid(fsid
);
3493 fs_devices
= clone_fs_devices(fs_devices
);
3494 if (IS_ERR(fs_devices
)) {
3495 ret
= PTR_ERR(fs_devices
);
3499 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3500 root
->fs_info
->bdev_holder
);
3504 if (!fs_devices
->seeding
) {
3505 __btrfs_close_devices(fs_devices
);
3506 free_fs_devices(fs_devices
);
3511 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3512 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3514 mutex_unlock(&uuid_mutex
);
3518 static int read_one_dev(struct btrfs_root
*root
,
3519 struct extent_buffer
*leaf
,
3520 struct btrfs_dev_item
*dev_item
)
3522 struct btrfs_device
*device
;
3525 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3526 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3528 devid
= btrfs_device_id(leaf
, dev_item
);
3529 read_extent_buffer(leaf
, dev_uuid
,
3530 (unsigned long)btrfs_device_uuid(dev_item
),
3532 read_extent_buffer(leaf
, fs_uuid
,
3533 (unsigned long)btrfs_device_fsid(dev_item
),
3536 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3537 ret
= open_seed_devices(root
, fs_uuid
);
3538 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3542 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3543 if (!device
|| !device
->bdev
) {
3544 if (!btrfs_test_opt(root
, DEGRADED
))
3548 printk(KERN_WARNING
"warning devid %llu missing\n",
3549 (unsigned long long)devid
);
3550 device
= add_missing_dev(root
, devid
, dev_uuid
);
3553 } else if (!device
->missing
) {
3555 * this happens when a device that was properly setup
3556 * in the device info lists suddenly goes bad.
3557 * device->bdev is NULL, and so we have to set
3558 * device->missing to one here
3560 root
->fs_info
->fs_devices
->missing_devices
++;
3561 device
->missing
= 1;
3565 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3566 BUG_ON(device
->writeable
);
3567 if (device
->generation
!=
3568 btrfs_device_generation(leaf
, dev_item
))
3572 fill_device_from_item(leaf
, dev_item
, device
);
3573 device
->dev_root
= root
->fs_info
->dev_root
;
3574 device
->in_fs_metadata
= 1;
3575 if (device
->writeable
)
3576 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3581 int btrfs_read_sys_array(struct btrfs_root
*root
)
3583 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3584 struct extent_buffer
*sb
;
3585 struct btrfs_disk_key
*disk_key
;
3586 struct btrfs_chunk
*chunk
;
3588 unsigned long sb_ptr
;
3594 struct btrfs_key key
;
3596 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3597 BTRFS_SUPER_INFO_SIZE
);
3600 btrfs_set_buffer_uptodate(sb
);
3601 btrfs_set_buffer_lockdep_class(sb
, 0);
3603 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3604 array_size
= btrfs_super_sys_array_size(super_copy
);
3606 ptr
= super_copy
->sys_chunk_array
;
3607 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3610 while (cur
< array_size
) {
3611 disk_key
= (struct btrfs_disk_key
*)ptr
;
3612 btrfs_disk_key_to_cpu(&key
, disk_key
);
3614 len
= sizeof(*disk_key
); ptr
+= len
;
3618 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3619 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3620 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3623 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3624 len
= btrfs_chunk_item_size(num_stripes
);
3633 free_extent_buffer(sb
);
3637 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3639 struct btrfs_path
*path
;
3640 struct extent_buffer
*leaf
;
3641 struct btrfs_key key
;
3642 struct btrfs_key found_key
;
3646 root
= root
->fs_info
->chunk_root
;
3648 path
= btrfs_alloc_path();
3652 /* first we search for all of the device items, and then we
3653 * read in all of the chunk items. This way we can create chunk
3654 * mappings that reference all of the devices that are afound
3656 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3660 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3664 leaf
= path
->nodes
[0];
3665 slot
= path
->slots
[0];
3666 if (slot
>= btrfs_header_nritems(leaf
)) {
3667 ret
= btrfs_next_leaf(root
, path
);
3674 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3675 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3676 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3678 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3679 struct btrfs_dev_item
*dev_item
;
3680 dev_item
= btrfs_item_ptr(leaf
, slot
,
3681 struct btrfs_dev_item
);
3682 ret
= read_one_dev(root
, leaf
, dev_item
);
3686 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3687 struct btrfs_chunk
*chunk
;
3688 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3689 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3695 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3697 btrfs_release_path(path
);
3702 btrfs_free_path(path
);