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"
44 struct btrfs_bio_stripe stripes
[];
47 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
48 struct btrfs_root
*root
,
49 struct btrfs_device
*device
);
50 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
52 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
53 (sizeof(struct btrfs_bio_stripe) * (n)))
55 static DEFINE_MUTEX(uuid_mutex
);
56 static LIST_HEAD(fs_uuids
);
58 void btrfs_lock_volumes(void)
60 mutex_lock(&uuid_mutex
);
63 void btrfs_unlock_volumes(void)
65 mutex_unlock(&uuid_mutex
);
68 static void lock_chunks(struct btrfs_root
*root
)
70 mutex_lock(&root
->fs_info
->chunk_mutex
);
73 static void unlock_chunks(struct btrfs_root
*root
)
75 mutex_unlock(&root
->fs_info
->chunk_mutex
);
78 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
80 struct btrfs_device
*device
;
81 WARN_ON(fs_devices
->opened
);
82 while (!list_empty(&fs_devices
->devices
)) {
83 device
= list_entry(fs_devices
->devices
.next
,
84 struct btrfs_device
, dev_list
);
85 list_del(&device
->dev_list
);
92 int btrfs_cleanup_fs_uuids(void)
94 struct btrfs_fs_devices
*fs_devices
;
96 while (!list_empty(&fs_uuids
)) {
97 fs_devices
= list_entry(fs_uuids
.next
,
98 struct btrfs_fs_devices
, list
);
99 list_del(&fs_devices
->list
);
100 free_fs_devices(fs_devices
);
105 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
108 struct btrfs_device
*dev
;
110 list_for_each_entry(dev
, head
, dev_list
) {
111 if (dev
->devid
== devid
&&
112 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
119 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
121 struct btrfs_fs_devices
*fs_devices
;
123 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
124 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
130 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
131 struct bio
*head
, struct bio
*tail
)
134 struct bio
*old_head
;
136 old_head
= pending_bios
->head
;
137 pending_bios
->head
= head
;
138 if (pending_bios
->tail
)
139 tail
->bi_next
= old_head
;
141 pending_bios
->tail
= tail
;
145 * we try to collect pending bios for a device so we don't get a large
146 * number of procs sending bios down to the same device. This greatly
147 * improves the schedulers ability to collect and merge the bios.
149 * But, it also turns into a long list of bios to process and that is sure
150 * to eventually make the worker thread block. The solution here is to
151 * make some progress and then put this work struct back at the end of
152 * the list if the block device is congested. This way, multiple devices
153 * can make progress from a single worker thread.
155 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
158 struct backing_dev_info
*bdi
;
159 struct btrfs_fs_info
*fs_info
;
160 struct btrfs_pending_bios
*pending_bios
;
164 unsigned long num_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;
176 spin_lock(&device
->io_lock
);
181 /* take all the bios off the list at once and process them
182 * later on (without the lock held). But, remember the
183 * tail and other pointers so the bios can be properly reinserted
184 * into the list if we hit congestion
186 if (!force_reg
&& device
->pending_sync_bios
.head
) {
187 pending_bios
= &device
->pending_sync_bios
;
190 pending_bios
= &device
->pending_bios
;
194 pending
= pending_bios
->head
;
195 tail
= pending_bios
->tail
;
196 WARN_ON(pending
&& !tail
);
199 * if pending was null this time around, no bios need processing
200 * at all and we can stop. Otherwise it'll loop back up again
201 * and do an additional check so no bios are missed.
203 * device->running_pending is used to synchronize with the
206 if (device
->pending_sync_bios
.head
== NULL
&&
207 device
->pending_bios
.head
== NULL
) {
209 device
->running_pending
= 0;
212 device
->running_pending
= 1;
215 pending_bios
->head
= NULL
;
216 pending_bios
->tail
= NULL
;
218 spin_unlock(&device
->io_lock
);
223 /* we want to work on both lists, but do more bios on the
224 * sync list than the regular list
227 pending_bios
!= &device
->pending_sync_bios
&&
228 device
->pending_sync_bios
.head
) ||
229 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
230 device
->pending_bios
.head
)) {
231 spin_lock(&device
->io_lock
);
232 requeue_list(pending_bios
, pending
, tail
);
237 pending
= pending
->bi_next
;
239 atomic_dec(&fs_info
->nr_async_bios
);
241 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
242 waitqueue_active(&fs_info
->async_submit_wait
))
243 wake_up(&fs_info
->async_submit_wait
);
245 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
247 submit_bio(cur
->bi_rw
, cur
);
254 * we made progress, there is more work to do and the bdi
255 * is now congested. Back off and let other work structs
258 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
259 fs_info
->fs_devices
->open_devices
> 1) {
260 struct io_context
*ioc
;
262 ioc
= current
->io_context
;
265 * the main goal here is that we don't want to
266 * block if we're going to be able to submit
267 * more requests without blocking.
269 * This code does two great things, it pokes into
270 * the elevator code from a filesystem _and_
271 * it makes assumptions about how batching works.
273 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
274 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
276 ioc
->last_waited
== last_waited
)) {
278 * we want to go through our batch of
279 * requests and stop. So, we copy out
280 * the ioc->last_waited time and test
281 * against it before looping
283 last_waited
= ioc
->last_waited
;
288 spin_lock(&device
->io_lock
);
289 requeue_list(pending_bios
, pending
, tail
);
290 device
->running_pending
= 1;
292 spin_unlock(&device
->io_lock
);
293 btrfs_requeue_work(&device
->work
);
302 spin_lock(&device
->io_lock
);
303 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
305 spin_unlock(&device
->io_lock
);
311 static void pending_bios_fn(struct btrfs_work
*work
)
313 struct btrfs_device
*device
;
315 device
= container_of(work
, struct btrfs_device
, work
);
316 run_scheduled_bios(device
);
319 static noinline
int device_list_add(const char *path
,
320 struct btrfs_super_block
*disk_super
,
321 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
323 struct btrfs_device
*device
;
324 struct btrfs_fs_devices
*fs_devices
;
325 u64 found_transid
= btrfs_super_generation(disk_super
);
328 fs_devices
= find_fsid(disk_super
->fsid
);
330 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
333 INIT_LIST_HEAD(&fs_devices
->devices
);
334 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
335 list_add(&fs_devices
->list
, &fs_uuids
);
336 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
337 fs_devices
->latest_devid
= devid
;
338 fs_devices
->latest_trans
= found_transid
;
339 mutex_init(&fs_devices
->device_list_mutex
);
342 device
= __find_device(&fs_devices
->devices
, devid
,
343 disk_super
->dev_item
.uuid
);
346 if (fs_devices
->opened
)
349 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
351 /* we can safely leave the fs_devices entry around */
354 device
->devid
= devid
;
355 device
->work
.func
= pending_bios_fn
;
356 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
358 spin_lock_init(&device
->io_lock
);
359 device
->name
= kstrdup(path
, GFP_NOFS
);
364 INIT_LIST_HEAD(&device
->dev_alloc_list
);
366 mutex_lock(&fs_devices
->device_list_mutex
);
367 list_add(&device
->dev_list
, &fs_devices
->devices
);
368 mutex_unlock(&fs_devices
->device_list_mutex
);
370 device
->fs_devices
= fs_devices
;
371 fs_devices
->num_devices
++;
372 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
373 name
= kstrdup(path
, GFP_NOFS
);
378 if (device
->missing
) {
379 fs_devices
->missing_devices
--;
384 if (found_transid
> fs_devices
->latest_trans
) {
385 fs_devices
->latest_devid
= devid
;
386 fs_devices
->latest_trans
= found_transid
;
388 *fs_devices_ret
= fs_devices
;
392 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
394 struct btrfs_fs_devices
*fs_devices
;
395 struct btrfs_device
*device
;
396 struct btrfs_device
*orig_dev
;
398 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
400 return ERR_PTR(-ENOMEM
);
402 INIT_LIST_HEAD(&fs_devices
->devices
);
403 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
404 INIT_LIST_HEAD(&fs_devices
->list
);
405 mutex_init(&fs_devices
->device_list_mutex
);
406 fs_devices
->latest_devid
= orig
->latest_devid
;
407 fs_devices
->latest_trans
= orig
->latest_trans
;
408 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
410 mutex_lock(&orig
->device_list_mutex
);
411 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
412 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
416 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
422 device
->devid
= orig_dev
->devid
;
423 device
->work
.func
= pending_bios_fn
;
424 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
425 spin_lock_init(&device
->io_lock
);
426 INIT_LIST_HEAD(&device
->dev_list
);
427 INIT_LIST_HEAD(&device
->dev_alloc_list
);
429 list_add(&device
->dev_list
, &fs_devices
->devices
);
430 device
->fs_devices
= fs_devices
;
431 fs_devices
->num_devices
++;
433 mutex_unlock(&orig
->device_list_mutex
);
436 mutex_unlock(&orig
->device_list_mutex
);
437 free_fs_devices(fs_devices
);
438 return ERR_PTR(-ENOMEM
);
441 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
443 struct btrfs_device
*device
, *next
;
445 mutex_lock(&uuid_mutex
);
447 mutex_lock(&fs_devices
->device_list_mutex
);
448 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
449 if (device
->in_fs_metadata
)
453 blkdev_put(device
->bdev
, device
->mode
);
455 fs_devices
->open_devices
--;
457 if (device
->writeable
) {
458 list_del_init(&device
->dev_alloc_list
);
459 device
->writeable
= 0;
460 fs_devices
->rw_devices
--;
462 list_del_init(&device
->dev_list
);
463 fs_devices
->num_devices
--;
467 mutex_unlock(&fs_devices
->device_list_mutex
);
469 if (fs_devices
->seed
) {
470 fs_devices
= fs_devices
->seed
;
474 mutex_unlock(&uuid_mutex
);
478 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
480 struct btrfs_device
*device
;
482 if (--fs_devices
->opened
> 0)
485 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
487 blkdev_put(device
->bdev
, device
->mode
);
488 fs_devices
->open_devices
--;
490 if (device
->writeable
) {
491 list_del_init(&device
->dev_alloc_list
);
492 fs_devices
->rw_devices
--;
496 device
->writeable
= 0;
497 device
->in_fs_metadata
= 0;
499 WARN_ON(fs_devices
->open_devices
);
500 WARN_ON(fs_devices
->rw_devices
);
501 fs_devices
->opened
= 0;
502 fs_devices
->seeding
= 0;
507 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
509 struct btrfs_fs_devices
*seed_devices
= NULL
;
512 mutex_lock(&uuid_mutex
);
513 ret
= __btrfs_close_devices(fs_devices
);
514 if (!fs_devices
->opened
) {
515 seed_devices
= fs_devices
->seed
;
516 fs_devices
->seed
= NULL
;
518 mutex_unlock(&uuid_mutex
);
520 while (seed_devices
) {
521 fs_devices
= seed_devices
;
522 seed_devices
= fs_devices
->seed
;
523 __btrfs_close_devices(fs_devices
);
524 free_fs_devices(fs_devices
);
529 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
530 fmode_t flags
, void *holder
)
532 struct block_device
*bdev
;
533 struct list_head
*head
= &fs_devices
->devices
;
534 struct btrfs_device
*device
;
535 struct block_device
*latest_bdev
= NULL
;
536 struct buffer_head
*bh
;
537 struct btrfs_super_block
*disk_super
;
538 u64 latest_devid
= 0;
539 u64 latest_transid
= 0;
546 list_for_each_entry(device
, head
, dev_list
) {
552 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
554 printk(KERN_INFO
"open %s failed\n", device
->name
);
557 set_blocksize(bdev
, 4096);
559 bh
= btrfs_read_dev_super(bdev
);
565 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
566 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
567 if (devid
!= device
->devid
)
570 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
574 device
->generation
= btrfs_super_generation(disk_super
);
575 if (!latest_transid
|| device
->generation
> latest_transid
) {
576 latest_devid
= devid
;
577 latest_transid
= device
->generation
;
581 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
582 device
->writeable
= 0;
584 device
->writeable
= !bdev_read_only(bdev
);
589 device
->in_fs_metadata
= 0;
590 device
->mode
= flags
;
592 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
593 fs_devices
->rotating
= 1;
595 fs_devices
->open_devices
++;
596 if (device
->writeable
) {
597 fs_devices
->rw_devices
++;
598 list_add(&device
->dev_alloc_list
,
599 &fs_devices
->alloc_list
);
606 blkdev_put(bdev
, flags
);
610 if (fs_devices
->open_devices
== 0) {
614 fs_devices
->seeding
= seeding
;
615 fs_devices
->opened
= 1;
616 fs_devices
->latest_bdev
= latest_bdev
;
617 fs_devices
->latest_devid
= latest_devid
;
618 fs_devices
->latest_trans
= latest_transid
;
619 fs_devices
->total_rw_bytes
= 0;
624 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
625 fmode_t flags
, void *holder
)
629 mutex_lock(&uuid_mutex
);
630 if (fs_devices
->opened
) {
631 fs_devices
->opened
++;
634 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
636 mutex_unlock(&uuid_mutex
);
640 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
641 struct btrfs_fs_devices
**fs_devices_ret
)
643 struct btrfs_super_block
*disk_super
;
644 struct block_device
*bdev
;
645 struct buffer_head
*bh
;
650 mutex_lock(&uuid_mutex
);
653 bdev
= blkdev_get_by_path(path
, flags
, holder
);
660 ret
= set_blocksize(bdev
, 4096);
663 bh
= btrfs_read_dev_super(bdev
);
668 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
669 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
670 transid
= btrfs_super_generation(disk_super
);
671 if (disk_super
->label
[0])
672 printk(KERN_INFO
"device label %s ", disk_super
->label
);
674 /* FIXME, make a readl uuid parser */
675 printk(KERN_INFO
"device fsid %llx-%llx ",
676 *(unsigned long long *)disk_super
->fsid
,
677 *(unsigned long long *)(disk_super
->fsid
+ 8));
679 printk(KERN_CONT
"devid %llu transid %llu %s\n",
680 (unsigned long long)devid
, (unsigned long long)transid
, path
);
681 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
685 blkdev_put(bdev
, flags
);
687 mutex_unlock(&uuid_mutex
);
691 /* helper to account the used device space in the range */
692 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
693 u64 end
, u64
*length
)
695 struct btrfs_key key
;
696 struct btrfs_root
*root
= device
->dev_root
;
697 struct btrfs_dev_extent
*dev_extent
;
698 struct btrfs_path
*path
;
702 struct extent_buffer
*l
;
706 if (start
>= device
->total_bytes
)
709 path
= btrfs_alloc_path();
714 key
.objectid
= device
->devid
;
716 key
.type
= BTRFS_DEV_EXTENT_KEY
;
718 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
722 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
729 slot
= path
->slots
[0];
730 if (slot
>= btrfs_header_nritems(l
)) {
731 ret
= btrfs_next_leaf(root
, path
);
739 btrfs_item_key_to_cpu(l
, &key
, slot
);
741 if (key
.objectid
< device
->devid
)
744 if (key
.objectid
> device
->devid
)
747 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
750 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
751 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
753 if (key
.offset
<= start
&& extent_end
> end
) {
754 *length
= end
- start
+ 1;
756 } else if (key
.offset
<= start
&& extent_end
> start
)
757 *length
+= extent_end
- start
;
758 else if (key
.offset
> start
&& extent_end
<= end
)
759 *length
+= extent_end
- key
.offset
;
760 else if (key
.offset
> start
&& key
.offset
<= end
) {
761 *length
+= end
- key
.offset
+ 1;
763 } else if (key
.offset
> end
)
771 btrfs_free_path(path
);
776 * find_free_dev_extent - find free space in the specified device
777 * @trans: transaction handler
778 * @device: the device which we search the free space in
779 * @num_bytes: the size of the free space that we need
780 * @start: store the start of the free space.
781 * @len: the size of the free space. that we find, or the size of the max
782 * free space if we don't find suitable free space
784 * this uses a pretty simple search, the expectation is that it is
785 * called very infrequently and that a given device has a small number
788 * @start is used to store the start of the free space if we find. But if we
789 * don't find suitable free space, it will be used to store the start position
790 * of the max free space.
792 * @len is used to store the size of the free space that we find.
793 * But if we don't find suitable free space, it is used to store the size of
794 * the max free space.
796 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
797 struct btrfs_device
*device
, u64 num_bytes
,
798 u64
*start
, u64
*len
)
800 struct btrfs_key key
;
801 struct btrfs_root
*root
= device
->dev_root
;
802 struct btrfs_dev_extent
*dev_extent
;
803 struct btrfs_path
*path
;
809 u64 search_end
= device
->total_bytes
;
812 struct extent_buffer
*l
;
814 /* FIXME use last free of some kind */
816 /* we don't want to overwrite the superblock on the drive,
817 * so we make sure to start at an offset of at least 1MB
819 search_start
= 1024 * 1024;
821 if (root
->fs_info
->alloc_start
+ num_bytes
<= search_end
)
822 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
824 max_hole_start
= search_start
;
827 if (search_start
>= search_end
) {
832 path
= btrfs_alloc_path();
839 key
.objectid
= device
->devid
;
840 key
.offset
= search_start
;
841 key
.type
= BTRFS_DEV_EXTENT_KEY
;
843 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
847 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
854 slot
= path
->slots
[0];
855 if (slot
>= btrfs_header_nritems(l
)) {
856 ret
= btrfs_next_leaf(root
, path
);
864 btrfs_item_key_to_cpu(l
, &key
, slot
);
866 if (key
.objectid
< device
->devid
)
869 if (key
.objectid
> device
->devid
)
872 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
875 if (key
.offset
> search_start
) {
876 hole_size
= key
.offset
- search_start
;
878 if (hole_size
> max_hole_size
) {
879 max_hole_start
= search_start
;
880 max_hole_size
= hole_size
;
884 * If this free space is greater than which we need,
885 * it must be the max free space that we have found
886 * until now, so max_hole_start must point to the start
887 * of this free space and the length of this free space
888 * is stored in max_hole_size. Thus, we return
889 * max_hole_start and max_hole_size and go back to the
892 if (hole_size
>= num_bytes
) {
898 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
899 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
901 if (extent_end
> search_start
)
902 search_start
= extent_end
;
908 hole_size
= search_end
- search_start
;
909 if (hole_size
> max_hole_size
) {
910 max_hole_start
= search_start
;
911 max_hole_size
= hole_size
;
915 if (hole_size
< num_bytes
)
921 btrfs_free_path(path
);
923 *start
= max_hole_start
;
925 *len
= max_hole_size
;
929 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
930 struct btrfs_device
*device
,
934 struct btrfs_path
*path
;
935 struct btrfs_root
*root
= device
->dev_root
;
936 struct btrfs_key key
;
937 struct btrfs_key found_key
;
938 struct extent_buffer
*leaf
= NULL
;
939 struct btrfs_dev_extent
*extent
= NULL
;
941 path
= btrfs_alloc_path();
945 key
.objectid
= device
->devid
;
947 key
.type
= BTRFS_DEV_EXTENT_KEY
;
949 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
951 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
952 BTRFS_DEV_EXTENT_KEY
);
954 leaf
= path
->nodes
[0];
955 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
956 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
957 struct btrfs_dev_extent
);
958 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
959 btrfs_dev_extent_length(leaf
, extent
) < start
);
961 } else if (ret
== 0) {
962 leaf
= path
->nodes
[0];
963 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
964 struct btrfs_dev_extent
);
968 if (device
->bytes_used
> 0)
969 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
970 ret
= btrfs_del_item(trans
, root
, path
);
973 btrfs_free_path(path
);
977 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
978 struct btrfs_device
*device
,
979 u64 chunk_tree
, u64 chunk_objectid
,
980 u64 chunk_offset
, u64 start
, u64 num_bytes
)
983 struct btrfs_path
*path
;
984 struct btrfs_root
*root
= device
->dev_root
;
985 struct btrfs_dev_extent
*extent
;
986 struct extent_buffer
*leaf
;
987 struct btrfs_key key
;
989 WARN_ON(!device
->in_fs_metadata
);
990 path
= btrfs_alloc_path();
994 key
.objectid
= device
->devid
;
996 key
.type
= BTRFS_DEV_EXTENT_KEY
;
997 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1001 leaf
= path
->nodes
[0];
1002 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1003 struct btrfs_dev_extent
);
1004 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1005 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1006 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1008 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1009 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1012 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1013 btrfs_mark_buffer_dirty(leaf
);
1014 btrfs_free_path(path
);
1018 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1019 u64 objectid
, u64
*offset
)
1021 struct btrfs_path
*path
;
1023 struct btrfs_key key
;
1024 struct btrfs_chunk
*chunk
;
1025 struct btrfs_key found_key
;
1027 path
= btrfs_alloc_path();
1030 key
.objectid
= objectid
;
1031 key
.offset
= (u64
)-1;
1032 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1034 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1040 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1044 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1046 if (found_key
.objectid
!= objectid
)
1049 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1050 struct btrfs_chunk
);
1051 *offset
= found_key
.offset
+
1052 btrfs_chunk_length(path
->nodes
[0], chunk
);
1057 btrfs_free_path(path
);
1061 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1064 struct btrfs_key key
;
1065 struct btrfs_key found_key
;
1066 struct btrfs_path
*path
;
1068 root
= root
->fs_info
->chunk_root
;
1070 path
= btrfs_alloc_path();
1074 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1075 key
.type
= BTRFS_DEV_ITEM_KEY
;
1076 key
.offset
= (u64
)-1;
1078 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1084 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1085 BTRFS_DEV_ITEM_KEY
);
1089 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1091 *objectid
= found_key
.offset
+ 1;
1095 btrfs_free_path(path
);
1100 * the device information is stored in the chunk root
1101 * the btrfs_device struct should be fully filled in
1103 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1104 struct btrfs_root
*root
,
1105 struct btrfs_device
*device
)
1108 struct btrfs_path
*path
;
1109 struct btrfs_dev_item
*dev_item
;
1110 struct extent_buffer
*leaf
;
1111 struct btrfs_key key
;
1114 root
= root
->fs_info
->chunk_root
;
1116 path
= btrfs_alloc_path();
1120 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1121 key
.type
= BTRFS_DEV_ITEM_KEY
;
1122 key
.offset
= device
->devid
;
1124 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1129 leaf
= path
->nodes
[0];
1130 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1132 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1133 btrfs_set_device_generation(leaf
, dev_item
, 0);
1134 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1135 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1136 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1137 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1138 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1139 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1140 btrfs_set_device_group(leaf
, dev_item
, 0);
1141 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1142 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1143 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1145 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1146 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1147 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1148 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1149 btrfs_mark_buffer_dirty(leaf
);
1153 btrfs_free_path(path
);
1157 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1158 struct btrfs_device
*device
)
1161 struct btrfs_path
*path
;
1162 struct btrfs_key key
;
1163 struct btrfs_trans_handle
*trans
;
1165 root
= root
->fs_info
->chunk_root
;
1167 path
= btrfs_alloc_path();
1171 trans
= btrfs_start_transaction(root
, 0);
1172 if (IS_ERR(trans
)) {
1173 btrfs_free_path(path
);
1174 return PTR_ERR(trans
);
1176 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1177 key
.type
= BTRFS_DEV_ITEM_KEY
;
1178 key
.offset
= device
->devid
;
1181 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1190 ret
= btrfs_del_item(trans
, root
, path
);
1194 btrfs_free_path(path
);
1195 unlock_chunks(root
);
1196 btrfs_commit_transaction(trans
, root
);
1200 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1202 struct btrfs_device
*device
;
1203 struct btrfs_device
*next_device
;
1204 struct block_device
*bdev
;
1205 struct buffer_head
*bh
= NULL
;
1206 struct btrfs_super_block
*disk_super
;
1213 mutex_lock(&uuid_mutex
);
1214 mutex_lock(&root
->fs_info
->volume_mutex
);
1216 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1217 root
->fs_info
->avail_system_alloc_bits
|
1218 root
->fs_info
->avail_metadata_alloc_bits
;
1220 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1221 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1222 printk(KERN_ERR
"btrfs: unable to go below four devices "
1228 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1229 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1230 printk(KERN_ERR
"btrfs: unable to go below two "
1231 "devices on raid1\n");
1236 if (strcmp(device_path
, "missing") == 0) {
1237 struct list_head
*devices
;
1238 struct btrfs_device
*tmp
;
1241 devices
= &root
->fs_info
->fs_devices
->devices
;
1242 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1243 list_for_each_entry(tmp
, devices
, dev_list
) {
1244 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1249 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1254 printk(KERN_ERR
"btrfs: no missing devices found to "
1259 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1260 root
->fs_info
->bdev_holder
);
1262 ret
= PTR_ERR(bdev
);
1266 set_blocksize(bdev
, 4096);
1267 bh
= btrfs_read_dev_super(bdev
);
1272 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1273 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1274 dev_uuid
= disk_super
->dev_item
.uuid
;
1275 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1283 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1284 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1290 if (device
->writeable
) {
1291 list_del_init(&device
->dev_alloc_list
);
1292 root
->fs_info
->fs_devices
->rw_devices
--;
1295 ret
= btrfs_shrink_device(device
, 0);
1299 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1303 device
->in_fs_metadata
= 0;
1306 * the device list mutex makes sure that we don't change
1307 * the device list while someone else is writing out all
1308 * the device supers.
1310 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1311 list_del_init(&device
->dev_list
);
1312 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1314 device
->fs_devices
->num_devices
--;
1316 if (device
->missing
)
1317 root
->fs_info
->fs_devices
->missing_devices
--;
1319 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1320 struct btrfs_device
, dev_list
);
1321 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1322 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1323 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1324 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1327 blkdev_put(device
->bdev
, device
->mode
);
1328 device
->bdev
= NULL
;
1329 device
->fs_devices
->open_devices
--;
1332 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1333 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1335 if (device
->fs_devices
->open_devices
== 0) {
1336 struct btrfs_fs_devices
*fs_devices
;
1337 fs_devices
= root
->fs_info
->fs_devices
;
1338 while (fs_devices
) {
1339 if (fs_devices
->seed
== device
->fs_devices
)
1341 fs_devices
= fs_devices
->seed
;
1343 fs_devices
->seed
= device
->fs_devices
->seed
;
1344 device
->fs_devices
->seed
= NULL
;
1345 __btrfs_close_devices(device
->fs_devices
);
1346 free_fs_devices(device
->fs_devices
);
1350 * at this point, the device is zero sized. We want to
1351 * remove it from the devices list and zero out the old super
1353 if (device
->writeable
) {
1354 /* make sure this device isn't detected as part of
1357 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1358 set_buffer_dirty(bh
);
1359 sync_dirty_buffer(bh
);
1362 kfree(device
->name
);
1370 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1372 mutex_unlock(&root
->fs_info
->volume_mutex
);
1373 mutex_unlock(&uuid_mutex
);
1378 * does all the dirty work required for changing file system's UUID.
1380 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1381 struct btrfs_root
*root
)
1383 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1384 struct btrfs_fs_devices
*old_devices
;
1385 struct btrfs_fs_devices
*seed_devices
;
1386 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1387 struct btrfs_device
*device
;
1390 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1391 if (!fs_devices
->seeding
)
1394 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1398 old_devices
= clone_fs_devices(fs_devices
);
1399 if (IS_ERR(old_devices
)) {
1400 kfree(seed_devices
);
1401 return PTR_ERR(old_devices
);
1404 list_add(&old_devices
->list
, &fs_uuids
);
1406 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1407 seed_devices
->opened
= 1;
1408 INIT_LIST_HEAD(&seed_devices
->devices
);
1409 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1410 mutex_init(&seed_devices
->device_list_mutex
);
1411 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1412 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1413 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1414 device
->fs_devices
= seed_devices
;
1417 fs_devices
->seeding
= 0;
1418 fs_devices
->num_devices
= 0;
1419 fs_devices
->open_devices
= 0;
1420 fs_devices
->seed
= seed_devices
;
1422 generate_random_uuid(fs_devices
->fsid
);
1423 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1424 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1425 super_flags
= btrfs_super_flags(disk_super
) &
1426 ~BTRFS_SUPER_FLAG_SEEDING
;
1427 btrfs_set_super_flags(disk_super
, super_flags
);
1433 * strore the expected generation for seed devices in device items.
1435 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1436 struct btrfs_root
*root
)
1438 struct btrfs_path
*path
;
1439 struct extent_buffer
*leaf
;
1440 struct btrfs_dev_item
*dev_item
;
1441 struct btrfs_device
*device
;
1442 struct btrfs_key key
;
1443 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1444 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1448 path
= btrfs_alloc_path();
1452 root
= root
->fs_info
->chunk_root
;
1453 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1455 key
.type
= BTRFS_DEV_ITEM_KEY
;
1458 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1462 leaf
= path
->nodes
[0];
1464 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1465 ret
= btrfs_next_leaf(root
, path
);
1470 leaf
= path
->nodes
[0];
1471 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1472 btrfs_release_path(root
, path
);
1476 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1477 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1478 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1481 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1482 struct btrfs_dev_item
);
1483 devid
= btrfs_device_id(leaf
, dev_item
);
1484 read_extent_buffer(leaf
, dev_uuid
,
1485 (unsigned long)btrfs_device_uuid(dev_item
),
1487 read_extent_buffer(leaf
, fs_uuid
,
1488 (unsigned long)btrfs_device_fsid(dev_item
),
1490 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1493 if (device
->fs_devices
->seeding
) {
1494 btrfs_set_device_generation(leaf
, dev_item
,
1495 device
->generation
);
1496 btrfs_mark_buffer_dirty(leaf
);
1504 btrfs_free_path(path
);
1508 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1510 struct btrfs_trans_handle
*trans
;
1511 struct btrfs_device
*device
;
1512 struct block_device
*bdev
;
1513 struct list_head
*devices
;
1514 struct super_block
*sb
= root
->fs_info
->sb
;
1516 int seeding_dev
= 0;
1519 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1522 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1523 root
->fs_info
->bdev_holder
);
1525 return PTR_ERR(bdev
);
1527 if (root
->fs_info
->fs_devices
->seeding
) {
1529 down_write(&sb
->s_umount
);
1530 mutex_lock(&uuid_mutex
);
1533 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1534 mutex_lock(&root
->fs_info
->volume_mutex
);
1536 devices
= &root
->fs_info
->fs_devices
->devices
;
1538 * we have the volume lock, so we don't need the extra
1539 * device list mutex while reading the list here.
1541 list_for_each_entry(device
, devices
, dev_list
) {
1542 if (device
->bdev
== bdev
) {
1548 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1550 /* we can safely leave the fs_devices entry around */
1555 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1556 if (!device
->name
) {
1562 ret
= find_next_devid(root
, &device
->devid
);
1564 kfree(device
->name
);
1569 trans
= btrfs_start_transaction(root
, 0);
1570 if (IS_ERR(trans
)) {
1571 kfree(device
->name
);
1573 ret
= PTR_ERR(trans
);
1579 device
->writeable
= 1;
1580 device
->work
.func
= pending_bios_fn
;
1581 generate_random_uuid(device
->uuid
);
1582 spin_lock_init(&device
->io_lock
);
1583 device
->generation
= trans
->transid
;
1584 device
->io_width
= root
->sectorsize
;
1585 device
->io_align
= root
->sectorsize
;
1586 device
->sector_size
= root
->sectorsize
;
1587 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1588 device
->disk_total_bytes
= device
->total_bytes
;
1589 device
->dev_root
= root
->fs_info
->dev_root
;
1590 device
->bdev
= bdev
;
1591 device
->in_fs_metadata
= 1;
1593 set_blocksize(device
->bdev
, 4096);
1596 sb
->s_flags
&= ~MS_RDONLY
;
1597 ret
= btrfs_prepare_sprout(trans
, root
);
1601 device
->fs_devices
= root
->fs_info
->fs_devices
;
1604 * we don't want write_supers to jump in here with our device
1607 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1608 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1609 list_add(&device
->dev_alloc_list
,
1610 &root
->fs_info
->fs_devices
->alloc_list
);
1611 root
->fs_info
->fs_devices
->num_devices
++;
1612 root
->fs_info
->fs_devices
->open_devices
++;
1613 root
->fs_info
->fs_devices
->rw_devices
++;
1614 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1616 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1617 root
->fs_info
->fs_devices
->rotating
= 1;
1619 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1620 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1621 total_bytes
+ device
->total_bytes
);
1623 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1624 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1626 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1629 ret
= init_first_rw_device(trans
, root
, device
);
1631 ret
= btrfs_finish_sprout(trans
, root
);
1634 ret
= btrfs_add_device(trans
, root
, device
);
1638 * we've got more storage, clear any full flags on the space
1641 btrfs_clear_space_info_full(root
->fs_info
);
1643 unlock_chunks(root
);
1644 btrfs_commit_transaction(trans
, root
);
1647 mutex_unlock(&uuid_mutex
);
1648 up_write(&sb
->s_umount
);
1650 ret
= btrfs_relocate_sys_chunks(root
);
1654 mutex_unlock(&root
->fs_info
->volume_mutex
);
1657 blkdev_put(bdev
, FMODE_EXCL
);
1659 mutex_unlock(&uuid_mutex
);
1660 up_write(&sb
->s_umount
);
1665 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1666 struct btrfs_device
*device
)
1669 struct btrfs_path
*path
;
1670 struct btrfs_root
*root
;
1671 struct btrfs_dev_item
*dev_item
;
1672 struct extent_buffer
*leaf
;
1673 struct btrfs_key key
;
1675 root
= device
->dev_root
->fs_info
->chunk_root
;
1677 path
= btrfs_alloc_path();
1681 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1682 key
.type
= BTRFS_DEV_ITEM_KEY
;
1683 key
.offset
= device
->devid
;
1685 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1694 leaf
= path
->nodes
[0];
1695 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1697 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1698 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1699 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1700 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1701 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1702 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1703 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1704 btrfs_mark_buffer_dirty(leaf
);
1707 btrfs_free_path(path
);
1711 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1712 struct btrfs_device
*device
, u64 new_size
)
1714 struct btrfs_super_block
*super_copy
=
1715 &device
->dev_root
->fs_info
->super_copy
;
1716 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1717 u64 diff
= new_size
- device
->total_bytes
;
1719 if (!device
->writeable
)
1721 if (new_size
<= device
->total_bytes
)
1724 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1725 device
->fs_devices
->total_rw_bytes
+= diff
;
1727 device
->total_bytes
= new_size
;
1728 device
->disk_total_bytes
= new_size
;
1729 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1731 return btrfs_update_device(trans
, device
);
1734 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1735 struct btrfs_device
*device
, u64 new_size
)
1738 lock_chunks(device
->dev_root
);
1739 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1740 unlock_chunks(device
->dev_root
);
1744 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1745 struct btrfs_root
*root
,
1746 u64 chunk_tree
, u64 chunk_objectid
,
1750 struct btrfs_path
*path
;
1751 struct btrfs_key key
;
1753 root
= root
->fs_info
->chunk_root
;
1754 path
= btrfs_alloc_path();
1758 key
.objectid
= chunk_objectid
;
1759 key
.offset
= chunk_offset
;
1760 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1762 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1765 ret
= btrfs_del_item(trans
, root
, path
);
1768 btrfs_free_path(path
);
1772 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1775 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1776 struct btrfs_disk_key
*disk_key
;
1777 struct btrfs_chunk
*chunk
;
1784 struct btrfs_key key
;
1786 array_size
= btrfs_super_sys_array_size(super_copy
);
1788 ptr
= super_copy
->sys_chunk_array
;
1791 while (cur
< array_size
) {
1792 disk_key
= (struct btrfs_disk_key
*)ptr
;
1793 btrfs_disk_key_to_cpu(&key
, disk_key
);
1795 len
= sizeof(*disk_key
);
1797 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1798 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1799 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1800 len
+= btrfs_chunk_item_size(num_stripes
);
1805 if (key
.objectid
== chunk_objectid
&&
1806 key
.offset
== chunk_offset
) {
1807 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1809 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1818 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1819 u64 chunk_tree
, u64 chunk_objectid
,
1822 struct extent_map_tree
*em_tree
;
1823 struct btrfs_root
*extent_root
;
1824 struct btrfs_trans_handle
*trans
;
1825 struct extent_map
*em
;
1826 struct map_lookup
*map
;
1830 root
= root
->fs_info
->chunk_root
;
1831 extent_root
= root
->fs_info
->extent_root
;
1832 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1834 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1838 /* step one, relocate all the extents inside this chunk */
1839 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1843 trans
= btrfs_start_transaction(root
, 0);
1844 BUG_ON(IS_ERR(trans
));
1849 * step two, delete the device extents and the
1850 * chunk tree entries
1852 read_lock(&em_tree
->lock
);
1853 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1854 read_unlock(&em_tree
->lock
);
1856 BUG_ON(em
->start
> chunk_offset
||
1857 em
->start
+ em
->len
< chunk_offset
);
1858 map
= (struct map_lookup
*)em
->bdev
;
1860 for (i
= 0; i
< map
->num_stripes
; i
++) {
1861 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1862 map
->stripes
[i
].physical
);
1865 if (map
->stripes
[i
].dev
) {
1866 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1870 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1875 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1876 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1880 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1883 write_lock(&em_tree
->lock
);
1884 remove_extent_mapping(em_tree
, em
);
1885 write_unlock(&em_tree
->lock
);
1890 /* once for the tree */
1891 free_extent_map(em
);
1893 free_extent_map(em
);
1895 unlock_chunks(root
);
1896 btrfs_end_transaction(trans
, root
);
1900 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1902 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1903 struct btrfs_path
*path
;
1904 struct extent_buffer
*leaf
;
1905 struct btrfs_chunk
*chunk
;
1906 struct btrfs_key key
;
1907 struct btrfs_key found_key
;
1908 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1910 bool retried
= false;
1914 path
= btrfs_alloc_path();
1919 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1920 key
.offset
= (u64
)-1;
1921 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1924 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1929 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1936 leaf
= path
->nodes
[0];
1937 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1939 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1940 struct btrfs_chunk
);
1941 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1942 btrfs_release_path(chunk_root
, path
);
1944 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1945 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1954 if (found_key
.offset
== 0)
1956 key
.offset
= found_key
.offset
- 1;
1959 if (failed
&& !retried
) {
1963 } else if (failed
&& retried
) {
1968 btrfs_free_path(path
);
1972 static u64
div_factor(u64 num
, int factor
)
1981 int btrfs_balance(struct btrfs_root
*dev_root
)
1984 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1985 struct btrfs_device
*device
;
1988 struct btrfs_path
*path
;
1989 struct btrfs_key key
;
1990 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1991 struct btrfs_trans_handle
*trans
;
1992 struct btrfs_key found_key
;
1994 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1997 if (!capable(CAP_SYS_ADMIN
))
2000 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2001 dev_root
= dev_root
->fs_info
->dev_root
;
2003 /* step one make some room on all the devices */
2004 list_for_each_entry(device
, devices
, dev_list
) {
2005 old_size
= device
->total_bytes
;
2006 size_to_free
= div_factor(old_size
, 1);
2007 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2008 if (!device
->writeable
||
2009 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2012 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2017 trans
= btrfs_start_transaction(dev_root
, 0);
2018 BUG_ON(IS_ERR(trans
));
2020 ret
= btrfs_grow_device(trans
, device
, old_size
);
2023 btrfs_end_transaction(trans
, dev_root
);
2026 /* step two, relocate all the chunks */
2027 path
= btrfs_alloc_path();
2030 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2031 key
.offset
= (u64
)-1;
2032 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2035 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2040 * this shouldn't happen, it means the last relocate
2046 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2047 BTRFS_CHUNK_ITEM_KEY
);
2051 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2053 if (found_key
.objectid
!= key
.objectid
)
2056 /* chunk zero is special */
2057 if (found_key
.offset
== 0)
2060 btrfs_release_path(chunk_root
, path
);
2061 ret
= btrfs_relocate_chunk(chunk_root
,
2062 chunk_root
->root_key
.objectid
,
2065 BUG_ON(ret
&& ret
!= -ENOSPC
);
2066 key
.offset
= found_key
.offset
- 1;
2070 btrfs_free_path(path
);
2071 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2076 * shrinking a device means finding all of the device extents past
2077 * the new size, and then following the back refs to the chunks.
2078 * The chunk relocation code actually frees the device extent
2080 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2082 struct btrfs_trans_handle
*trans
;
2083 struct btrfs_root
*root
= device
->dev_root
;
2084 struct btrfs_dev_extent
*dev_extent
= NULL
;
2085 struct btrfs_path
*path
;
2093 bool retried
= false;
2094 struct extent_buffer
*l
;
2095 struct btrfs_key key
;
2096 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2097 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2098 u64 old_size
= device
->total_bytes
;
2099 u64 diff
= device
->total_bytes
- new_size
;
2101 if (new_size
>= device
->total_bytes
)
2104 path
= btrfs_alloc_path();
2112 device
->total_bytes
= new_size
;
2113 if (device
->writeable
)
2114 device
->fs_devices
->total_rw_bytes
-= diff
;
2115 unlock_chunks(root
);
2118 key
.objectid
= device
->devid
;
2119 key
.offset
= (u64
)-1;
2120 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2123 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2127 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2132 btrfs_release_path(root
, path
);
2137 slot
= path
->slots
[0];
2138 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2140 if (key
.objectid
!= device
->devid
) {
2141 btrfs_release_path(root
, path
);
2145 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2146 length
= btrfs_dev_extent_length(l
, dev_extent
);
2148 if (key
.offset
+ length
<= new_size
) {
2149 btrfs_release_path(root
, path
);
2153 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2154 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2155 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2156 btrfs_release_path(root
, path
);
2158 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2160 if (ret
&& ret
!= -ENOSPC
)
2167 if (failed
&& !retried
) {
2171 } else if (failed
&& retried
) {
2175 device
->total_bytes
= old_size
;
2176 if (device
->writeable
)
2177 device
->fs_devices
->total_rw_bytes
+= diff
;
2178 unlock_chunks(root
);
2182 /* Shrinking succeeded, else we would be at "done". */
2183 trans
= btrfs_start_transaction(root
, 0);
2184 if (IS_ERR(trans
)) {
2185 ret
= PTR_ERR(trans
);
2191 device
->disk_total_bytes
= new_size
;
2192 /* Now btrfs_update_device() will change the on-disk size. */
2193 ret
= btrfs_update_device(trans
, device
);
2195 unlock_chunks(root
);
2196 btrfs_end_transaction(trans
, root
);
2199 WARN_ON(diff
> old_total
);
2200 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2201 unlock_chunks(root
);
2202 btrfs_end_transaction(trans
, root
);
2204 btrfs_free_path(path
);
2208 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2209 struct btrfs_root
*root
,
2210 struct btrfs_key
*key
,
2211 struct btrfs_chunk
*chunk
, int item_size
)
2213 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2214 struct btrfs_disk_key disk_key
;
2218 array_size
= btrfs_super_sys_array_size(super_copy
);
2219 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2222 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2223 btrfs_cpu_key_to_disk(&disk_key
, key
);
2224 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2225 ptr
+= sizeof(disk_key
);
2226 memcpy(ptr
, chunk
, item_size
);
2227 item_size
+= sizeof(disk_key
);
2228 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2232 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2233 int num_stripes
, int sub_stripes
)
2235 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2237 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2238 return calc_size
* (num_stripes
/ sub_stripes
);
2240 return calc_size
* num_stripes
;
2243 /* Used to sort the devices by max_avail(descending sort) */
2244 int btrfs_cmp_device_free_bytes(const void *dev_info1
, const void *dev_info2
)
2246 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
2247 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2249 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
2250 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2256 static int __btrfs_calc_nstripes(struct btrfs_fs_devices
*fs_devices
, u64 type
,
2257 int *num_stripes
, int *min_stripes
,
2264 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2265 *num_stripes
= fs_devices
->rw_devices
;
2268 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2272 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2273 if (fs_devices
->rw_devices
< 2)
2278 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2279 *num_stripes
= fs_devices
->rw_devices
;
2280 if (*num_stripes
< 4)
2282 *num_stripes
&= ~(u32
)1;
2290 static u64
__btrfs_calc_stripe_size(struct btrfs_fs_devices
*fs_devices
,
2291 u64 proposed_size
, u64 type
,
2292 int num_stripes
, int small_stripe
)
2294 int min_stripe_size
= 1 * 1024 * 1024;
2295 u64 calc_size
= proposed_size
;
2296 u64 max_chunk_size
= calc_size
;
2299 if (type
& (BTRFS_BLOCK_GROUP_RAID1
|
2300 BTRFS_BLOCK_GROUP_DUP
|
2301 BTRFS_BLOCK_GROUP_RAID10
))
2304 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2305 max_chunk_size
= 10 * calc_size
;
2306 min_stripe_size
= 64 * 1024 * 1024;
2307 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2308 max_chunk_size
= 256 * 1024 * 1024;
2309 min_stripe_size
= 32 * 1024 * 1024;
2310 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2311 calc_size
= 8 * 1024 * 1024;
2312 max_chunk_size
= calc_size
* 2;
2313 min_stripe_size
= 1 * 1024 * 1024;
2316 /* we don't want a chunk larger than 10% of writeable space */
2317 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2320 if (calc_size
* num_stripes
> max_chunk_size
* ncopies
) {
2321 calc_size
= max_chunk_size
* ncopies
;
2322 do_div(calc_size
, num_stripes
);
2323 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2324 calc_size
*= BTRFS_STRIPE_LEN
;
2327 /* we don't want tiny stripes */
2329 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2332 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2333 * we end up with something bigger than a stripe
2335 calc_size
= max_t(u64
, calc_size
, BTRFS_STRIPE_LEN
);
2337 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2338 calc_size
*= BTRFS_STRIPE_LEN
;
2343 static struct map_lookup
*__shrink_map_lookup_stripes(struct map_lookup
*map
,
2346 struct map_lookup
*new;
2347 size_t len
= map_lookup_size(num_stripes
);
2349 BUG_ON(map
->num_stripes
< num_stripes
);
2351 if (map
->num_stripes
== num_stripes
)
2354 new = kmalloc(len
, GFP_NOFS
);
2356 /* just change map->num_stripes */
2357 map
->num_stripes
= num_stripes
;
2361 memcpy(new, map
, len
);
2362 new->num_stripes
= num_stripes
;
2368 * helper to allocate device space from btrfs_device_info, in which we stored
2369 * max free space information of every device. It is used when we can not
2370 * allocate chunks by default size.
2372 * By this helper, we can allocate a new chunk as larger as possible.
2374 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle
*trans
,
2375 struct btrfs_fs_devices
*fs_devices
,
2376 struct btrfs_device_info
*devices
,
2377 int nr_device
, u64 type
,
2378 struct map_lookup
**map_lookup
,
2379 int min_stripes
, u64
*stripe_size
)
2381 int i
, index
, sort_again
= 0;
2382 int min_devices
= min_stripes
;
2383 u64 max_avail
, min_free
;
2384 struct map_lookup
*map
= *map_lookup
;
2387 if (nr_device
< min_stripes
)
2390 btrfs_descending_sort_devices(devices
, nr_device
);
2392 max_avail
= devices
[0].max_avail
;
2396 for (i
= 0; i
< nr_device
; i
++) {
2398 * if dev_offset = 0, it means the free space of this device
2399 * is less than what we need, and we didn't search max avail
2400 * extent on this device, so do it now.
2402 if (!devices
[i
].dev_offset
) {
2403 ret
= find_free_dev_extent(trans
, devices
[i
].dev
,
2405 &devices
[i
].dev_offset
,
2406 &devices
[i
].max_avail
);
2407 if (ret
!= 0 && ret
!= -ENOSPC
)
2413 /* we update the max avail free extent of each devices, sort again */
2415 btrfs_descending_sort_devices(devices
, nr_device
);
2417 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2420 if (!devices
[min_devices
- 1].max_avail
)
2423 max_avail
= devices
[min_devices
- 1].max_avail
;
2424 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2425 do_div(max_avail
, 2);
2427 max_avail
= __btrfs_calc_stripe_size(fs_devices
, max_avail
, type
,
2429 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2430 min_free
= max_avail
* 2;
2432 min_free
= max_avail
;
2434 if (min_free
> devices
[min_devices
- 1].max_avail
)
2437 map
= __shrink_map_lookup_stripes(map
, min_stripes
);
2438 *stripe_size
= max_avail
;
2441 for (i
= 0; i
< min_stripes
; i
++) {
2442 map
->stripes
[i
].dev
= devices
[index
].dev
;
2443 map
->stripes
[i
].physical
= devices
[index
].dev_offset
;
2444 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2446 map
->stripes
[i
].dev
= devices
[index
].dev
;
2447 map
->stripes
[i
].physical
= devices
[index
].dev_offset
+
2457 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2458 struct btrfs_root
*extent_root
,
2459 struct map_lookup
**map_ret
,
2460 u64
*num_bytes
, u64
*stripe_size
,
2461 u64 start
, u64 type
)
2463 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2464 struct btrfs_device
*device
= NULL
;
2465 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2466 struct list_head
*cur
;
2467 struct map_lookup
*map
;
2468 struct extent_map_tree
*em_tree
;
2469 struct extent_map
*em
;
2470 struct btrfs_device_info
*devices_info
;
2471 struct list_head private_devs
;
2472 u64 calc_size
= 1024 * 1024 * 1024;
2479 int min_devices
; /* the min number of devices we need */
2484 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2485 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2487 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2489 if (list_empty(&fs_devices
->alloc_list
))
2492 ret
= __btrfs_calc_nstripes(fs_devices
, type
, &num_stripes
,
2493 &min_stripes
, &sub_stripes
);
2497 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2502 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2507 map
->num_stripes
= num_stripes
;
2509 cur
= fs_devices
->alloc_list
.next
;
2513 calc_size
= __btrfs_calc_stripe_size(fs_devices
, calc_size
, type
,
2516 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2517 min_free
= calc_size
* 2;
2520 min_free
= calc_size
;
2521 min_devices
= min_stripes
;
2524 INIT_LIST_HEAD(&private_devs
);
2525 while (index
< num_stripes
) {
2526 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2527 BUG_ON(!device
->writeable
);
2528 if (device
->total_bytes
> device
->bytes_used
)
2529 avail
= device
->total_bytes
- device
->bytes_used
;
2534 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2535 ret
= find_free_dev_extent(trans
, device
, min_free
,
2536 &devices_info
[i
].dev_offset
,
2537 &devices_info
[i
].max_avail
);
2539 list_move_tail(&device
->dev_alloc_list
,
2541 map
->stripes
[index
].dev
= device
;
2542 map
->stripes
[index
].physical
=
2543 devices_info
[i
].dev_offset
;
2545 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2546 map
->stripes
[index
].dev
= device
;
2547 map
->stripes
[index
].physical
=
2548 devices_info
[i
].dev_offset
+
2552 } else if (ret
!= -ENOSPC
)
2555 devices_info
[i
].dev
= device
;
2557 } else if (device
->in_fs_metadata
&&
2558 avail
>= BTRFS_STRIPE_LEN
) {
2559 devices_info
[i
].dev
= device
;
2560 devices_info
[i
].max_avail
= avail
;
2564 if (cur
== &fs_devices
->alloc_list
)
2568 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2569 if (index
< num_stripes
) {
2570 if (index
>= min_stripes
) {
2571 num_stripes
= index
;
2572 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2573 num_stripes
/= sub_stripes
;
2574 num_stripes
*= sub_stripes
;
2577 map
= __shrink_map_lookup_stripes(map
, num_stripes
);
2578 } else if (i
>= min_devices
) {
2579 ret
= __btrfs_alloc_tiny_space(trans
, fs_devices
,
2580 devices_info
, i
, type
,
2590 map
->sector_size
= extent_root
->sectorsize
;
2591 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2592 map
->io_align
= BTRFS_STRIPE_LEN
;
2593 map
->io_width
= BTRFS_STRIPE_LEN
;
2595 map
->sub_stripes
= sub_stripes
;
2598 *stripe_size
= calc_size
;
2599 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2600 map
->num_stripes
, sub_stripes
);
2602 em
= alloc_extent_map(GFP_NOFS
);
2607 em
->bdev
= (struct block_device
*)map
;
2609 em
->len
= *num_bytes
;
2610 em
->block_start
= 0;
2611 em
->block_len
= em
->len
;
2613 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2614 write_lock(&em_tree
->lock
);
2615 ret
= add_extent_mapping(em_tree
, em
);
2616 write_unlock(&em_tree
->lock
);
2618 free_extent_map(em
);
2620 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2621 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2626 while (index
< map
->num_stripes
) {
2627 device
= map
->stripes
[index
].dev
;
2628 dev_offset
= map
->stripes
[index
].physical
;
2630 ret
= btrfs_alloc_dev_extent(trans
, device
,
2631 info
->chunk_root
->root_key
.objectid
,
2632 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2633 start
, dev_offset
, calc_size
);
2638 kfree(devices_info
);
2643 kfree(devices_info
);
2647 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2648 struct btrfs_root
*extent_root
,
2649 struct map_lookup
*map
, u64 chunk_offset
,
2650 u64 chunk_size
, u64 stripe_size
)
2653 struct btrfs_key key
;
2654 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2655 struct btrfs_device
*device
;
2656 struct btrfs_chunk
*chunk
;
2657 struct btrfs_stripe
*stripe
;
2658 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2662 chunk
= kzalloc(item_size
, GFP_NOFS
);
2667 while (index
< map
->num_stripes
) {
2668 device
= map
->stripes
[index
].dev
;
2669 device
->bytes_used
+= stripe_size
;
2670 ret
= btrfs_update_device(trans
, device
);
2676 stripe
= &chunk
->stripe
;
2677 while (index
< map
->num_stripes
) {
2678 device
= map
->stripes
[index
].dev
;
2679 dev_offset
= map
->stripes
[index
].physical
;
2681 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2682 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2683 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2688 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2689 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2690 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2691 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2692 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2693 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2694 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2695 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2696 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2698 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2699 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2700 key
.offset
= chunk_offset
;
2702 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2705 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2706 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2715 * Chunk allocation falls into two parts. The first part does works
2716 * that make the new allocated chunk useable, but not do any operation
2717 * that modifies the chunk tree. The second part does the works that
2718 * require modifying the chunk tree. This division is important for the
2719 * bootstrap process of adding storage to a seed btrfs.
2721 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2722 struct btrfs_root
*extent_root
, u64 type
)
2727 struct map_lookup
*map
;
2728 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2731 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2736 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2737 &stripe_size
, chunk_offset
, type
);
2741 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2742 chunk_size
, stripe_size
);
2747 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2748 struct btrfs_root
*root
,
2749 struct btrfs_device
*device
)
2752 u64 sys_chunk_offset
;
2756 u64 sys_stripe_size
;
2758 struct map_lookup
*map
;
2759 struct map_lookup
*sys_map
;
2760 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2761 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2764 ret
= find_next_chunk(fs_info
->chunk_root
,
2765 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2768 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2769 (fs_info
->metadata_alloc_profile
&
2770 fs_info
->avail_metadata_alloc_bits
);
2771 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2773 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2774 &stripe_size
, chunk_offset
, alloc_profile
);
2777 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2779 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2780 (fs_info
->system_alloc_profile
&
2781 fs_info
->avail_system_alloc_bits
);
2782 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2784 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2785 &sys_chunk_size
, &sys_stripe_size
,
2786 sys_chunk_offset
, alloc_profile
);
2789 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2793 * Modifying chunk tree needs allocating new blocks from both
2794 * system block group and metadata block group. So we only can
2795 * do operations require modifying the chunk tree after both
2796 * block groups were created.
2798 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2799 chunk_size
, stripe_size
);
2802 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2803 sys_chunk_offset
, sys_chunk_size
,
2809 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2811 struct extent_map
*em
;
2812 struct map_lookup
*map
;
2813 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2817 read_lock(&map_tree
->map_tree
.lock
);
2818 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2819 read_unlock(&map_tree
->map_tree
.lock
);
2823 if (btrfs_test_opt(root
, DEGRADED
)) {
2824 free_extent_map(em
);
2828 map
= (struct map_lookup
*)em
->bdev
;
2829 for (i
= 0; i
< map
->num_stripes
; i
++) {
2830 if (!map
->stripes
[i
].dev
->writeable
) {
2835 free_extent_map(em
);
2839 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2841 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2844 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2846 struct extent_map
*em
;
2849 write_lock(&tree
->map_tree
.lock
);
2850 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2852 remove_extent_mapping(&tree
->map_tree
, em
);
2853 write_unlock(&tree
->map_tree
.lock
);
2858 free_extent_map(em
);
2859 /* once for the tree */
2860 free_extent_map(em
);
2864 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2866 struct extent_map
*em
;
2867 struct map_lookup
*map
;
2868 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2871 read_lock(&em_tree
->lock
);
2872 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2873 read_unlock(&em_tree
->lock
);
2876 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2877 map
= (struct map_lookup
*)em
->bdev
;
2878 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2879 ret
= map
->num_stripes
;
2880 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2881 ret
= map
->sub_stripes
;
2884 free_extent_map(em
);
2888 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2892 if (map
->stripes
[optimal
].dev
->bdev
)
2894 for (i
= first
; i
< first
+ num
; i
++) {
2895 if (map
->stripes
[i
].dev
->bdev
)
2898 /* we couldn't find one that doesn't fail. Just return something
2899 * and the io error handling code will clean up eventually
2904 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2905 u64 logical
, u64
*length
,
2906 struct btrfs_multi_bio
**multi_ret
,
2909 struct extent_map
*em
;
2910 struct map_lookup
*map
;
2911 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2915 int stripes_allocated
= 8;
2916 int stripes_required
= 1;
2921 struct btrfs_multi_bio
*multi
= NULL
;
2923 if (multi_ret
&& !(rw
& REQ_WRITE
))
2924 stripes_allocated
= 1;
2927 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2932 atomic_set(&multi
->error
, 0);
2935 read_lock(&em_tree
->lock
);
2936 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2937 read_unlock(&em_tree
->lock
);
2940 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2941 (unsigned long long)logical
,
2942 (unsigned long long)*length
);
2946 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2947 map
= (struct map_lookup
*)em
->bdev
;
2948 offset
= logical
- em
->start
;
2950 if (mirror_num
> map
->num_stripes
)
2953 /* if our multi bio struct is too small, back off and try again */
2954 if (rw
& REQ_WRITE
) {
2955 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2956 BTRFS_BLOCK_GROUP_DUP
)) {
2957 stripes_required
= map
->num_stripes
;
2959 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2960 stripes_required
= map
->sub_stripes
;
2964 if (multi_ret
&& (rw
& REQ_WRITE
) &&
2965 stripes_allocated
< stripes_required
) {
2966 stripes_allocated
= map
->num_stripes
;
2967 free_extent_map(em
);
2973 * stripe_nr counts the total number of stripes we have to stride
2974 * to get to this block
2976 do_div(stripe_nr
, map
->stripe_len
);
2978 stripe_offset
= stripe_nr
* map
->stripe_len
;
2979 BUG_ON(offset
< stripe_offset
);
2981 /* stripe_offset is the offset of this block in its stripe*/
2982 stripe_offset
= offset
- stripe_offset
;
2984 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2985 BTRFS_BLOCK_GROUP_RAID10
|
2986 BTRFS_BLOCK_GROUP_DUP
)) {
2987 /* we limit the length of each bio to what fits in a stripe */
2988 *length
= min_t(u64
, em
->len
- offset
,
2989 map
->stripe_len
- stripe_offset
);
2991 *length
= em
->len
- offset
;
2999 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3001 num_stripes
= map
->num_stripes
;
3002 else if (mirror_num
)
3003 stripe_index
= mirror_num
- 1;
3005 stripe_index
= find_live_mirror(map
, 0,
3007 current
->pid
% map
->num_stripes
);
3010 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3012 num_stripes
= map
->num_stripes
;
3013 else if (mirror_num
)
3014 stripe_index
= mirror_num
- 1;
3016 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3017 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3019 stripe_index
= do_div(stripe_nr
, factor
);
3020 stripe_index
*= map
->sub_stripes
;
3023 num_stripes
= map
->sub_stripes
;
3024 else if (mirror_num
)
3025 stripe_index
+= mirror_num
- 1;
3027 stripe_index
= find_live_mirror(map
, stripe_index
,
3028 map
->sub_stripes
, stripe_index
+
3029 current
->pid
% map
->sub_stripes
);
3033 * after this do_div call, stripe_nr is the number of stripes
3034 * on this device we have to walk to find the data, and
3035 * stripe_index is the number of our device in the stripe array
3037 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3039 BUG_ON(stripe_index
>= map
->num_stripes
);
3041 for (i
= 0; i
< num_stripes
; i
++) {
3042 multi
->stripes
[i
].physical
=
3043 map
->stripes
[stripe_index
].physical
+
3044 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3045 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3050 multi
->num_stripes
= num_stripes
;
3051 multi
->max_errors
= max_errors
;
3054 free_extent_map(em
);
3058 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3059 u64 logical
, u64
*length
,
3060 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3062 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3066 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3067 u64 chunk_start
, u64 physical
, u64 devid
,
3068 u64
**logical
, int *naddrs
, int *stripe_len
)
3070 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3071 struct extent_map
*em
;
3072 struct map_lookup
*map
;
3079 read_lock(&em_tree
->lock
);
3080 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3081 read_unlock(&em_tree
->lock
);
3083 BUG_ON(!em
|| em
->start
!= chunk_start
);
3084 map
= (struct map_lookup
*)em
->bdev
;
3087 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3088 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3089 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3090 do_div(length
, map
->num_stripes
);
3092 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3095 for (i
= 0; i
< map
->num_stripes
; i
++) {
3096 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3098 if (map
->stripes
[i
].physical
> physical
||
3099 map
->stripes
[i
].physical
+ length
<= physical
)
3102 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3103 do_div(stripe_nr
, map
->stripe_len
);
3105 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3106 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3107 do_div(stripe_nr
, map
->sub_stripes
);
3108 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3109 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3111 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3112 WARN_ON(nr
>= map
->num_stripes
);
3113 for (j
= 0; j
< nr
; j
++) {
3114 if (buf
[j
] == bytenr
)
3118 WARN_ON(nr
>= map
->num_stripes
);
3125 *stripe_len
= map
->stripe_len
;
3127 free_extent_map(em
);
3131 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3133 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3134 int is_orig_bio
= 0;
3137 atomic_inc(&multi
->error
);
3139 if (bio
== multi
->orig_bio
)
3142 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3145 bio
= multi
->orig_bio
;
3147 bio
->bi_private
= multi
->private;
3148 bio
->bi_end_io
= multi
->end_io
;
3149 /* only send an error to the higher layers if it is
3150 * beyond the tolerance of the multi-bio
3152 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3156 * this bio is actually up to date, we didn't
3157 * go over the max number of errors
3159 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3164 bio_endio(bio
, err
);
3165 } else if (!is_orig_bio
) {
3170 struct async_sched
{
3173 struct btrfs_fs_info
*info
;
3174 struct btrfs_work work
;
3178 * see run_scheduled_bios for a description of why bios are collected for
3181 * This will add one bio to the pending list for a device and make sure
3182 * the work struct is scheduled.
3184 static noinline
int schedule_bio(struct btrfs_root
*root
,
3185 struct btrfs_device
*device
,
3186 int rw
, struct bio
*bio
)
3188 int should_queue
= 1;
3189 struct btrfs_pending_bios
*pending_bios
;
3191 /* don't bother with additional async steps for reads, right now */
3192 if (!(rw
& REQ_WRITE
)) {
3194 submit_bio(rw
, bio
);
3200 * nr_async_bios allows us to reliably return congestion to the
3201 * higher layers. Otherwise, the async bio makes it appear we have
3202 * made progress against dirty pages when we've really just put it
3203 * on a queue for later
3205 atomic_inc(&root
->fs_info
->nr_async_bios
);
3206 WARN_ON(bio
->bi_next
);
3207 bio
->bi_next
= NULL
;
3210 spin_lock(&device
->io_lock
);
3211 if (bio
->bi_rw
& REQ_SYNC
)
3212 pending_bios
= &device
->pending_sync_bios
;
3214 pending_bios
= &device
->pending_bios
;
3216 if (pending_bios
->tail
)
3217 pending_bios
->tail
->bi_next
= bio
;
3219 pending_bios
->tail
= bio
;
3220 if (!pending_bios
->head
)
3221 pending_bios
->head
= bio
;
3222 if (device
->running_pending
)
3225 spin_unlock(&device
->io_lock
);
3228 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3233 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3234 int mirror_num
, int async_submit
)
3236 struct btrfs_mapping_tree
*map_tree
;
3237 struct btrfs_device
*dev
;
3238 struct bio
*first_bio
= bio
;
3239 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3242 struct btrfs_multi_bio
*multi
= NULL
;
3247 length
= bio
->bi_size
;
3248 map_tree
= &root
->fs_info
->mapping_tree
;
3249 map_length
= length
;
3251 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3255 total_devs
= multi
->num_stripes
;
3256 if (map_length
< length
) {
3257 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3258 "len %llu\n", (unsigned long long)logical
,
3259 (unsigned long long)length
,
3260 (unsigned long long)map_length
);
3263 multi
->end_io
= first_bio
->bi_end_io
;
3264 multi
->private = first_bio
->bi_private
;
3265 multi
->orig_bio
= first_bio
;
3266 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3268 while (dev_nr
< total_devs
) {
3269 if (total_devs
> 1) {
3270 if (dev_nr
< total_devs
- 1) {
3271 bio
= bio_clone(first_bio
, GFP_NOFS
);
3276 bio
->bi_private
= multi
;
3277 bio
->bi_end_io
= end_bio_multi_stripe
;
3279 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3280 dev
= multi
->stripes
[dev_nr
].dev
;
3281 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3282 bio
->bi_bdev
= dev
->bdev
;
3284 schedule_bio(root
, dev
, rw
, bio
);
3286 submit_bio(rw
, bio
);
3288 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3289 bio
->bi_sector
= logical
>> 9;
3290 bio_endio(bio
, -EIO
);
3294 if (total_devs
== 1)
3299 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3302 struct btrfs_device
*device
;
3303 struct btrfs_fs_devices
*cur_devices
;
3305 cur_devices
= root
->fs_info
->fs_devices
;
3306 while (cur_devices
) {
3308 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3309 device
= __find_device(&cur_devices
->devices
,
3314 cur_devices
= cur_devices
->seed
;
3319 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3320 u64 devid
, u8
*dev_uuid
)
3322 struct btrfs_device
*device
;
3323 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3325 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3328 list_add(&device
->dev_list
,
3329 &fs_devices
->devices
);
3330 device
->dev_root
= root
->fs_info
->dev_root
;
3331 device
->devid
= devid
;
3332 device
->work
.func
= pending_bios_fn
;
3333 device
->fs_devices
= fs_devices
;
3334 device
->missing
= 1;
3335 fs_devices
->num_devices
++;
3336 fs_devices
->missing_devices
++;
3337 spin_lock_init(&device
->io_lock
);
3338 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3339 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3343 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3344 struct extent_buffer
*leaf
,
3345 struct btrfs_chunk
*chunk
)
3347 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3348 struct map_lookup
*map
;
3349 struct extent_map
*em
;
3353 u8 uuid
[BTRFS_UUID_SIZE
];
3358 logical
= key
->offset
;
3359 length
= btrfs_chunk_length(leaf
, chunk
);
3361 read_lock(&map_tree
->map_tree
.lock
);
3362 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3363 read_unlock(&map_tree
->map_tree
.lock
);
3365 /* already mapped? */
3366 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3367 free_extent_map(em
);
3370 free_extent_map(em
);
3373 em
= alloc_extent_map(GFP_NOFS
);
3376 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3377 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3379 free_extent_map(em
);
3383 em
->bdev
= (struct block_device
*)map
;
3384 em
->start
= logical
;
3386 em
->block_start
= 0;
3387 em
->block_len
= em
->len
;
3389 map
->num_stripes
= num_stripes
;
3390 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3391 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3392 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3393 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3394 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3395 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3396 for (i
= 0; i
< num_stripes
; i
++) {
3397 map
->stripes
[i
].physical
=
3398 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3399 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3400 read_extent_buffer(leaf
, uuid
, (unsigned long)
3401 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3403 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3405 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3407 free_extent_map(em
);
3410 if (!map
->stripes
[i
].dev
) {
3411 map
->stripes
[i
].dev
=
3412 add_missing_dev(root
, devid
, uuid
);
3413 if (!map
->stripes
[i
].dev
) {
3415 free_extent_map(em
);
3419 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3422 write_lock(&map_tree
->map_tree
.lock
);
3423 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3424 write_unlock(&map_tree
->map_tree
.lock
);
3426 free_extent_map(em
);
3431 static int fill_device_from_item(struct extent_buffer
*leaf
,
3432 struct btrfs_dev_item
*dev_item
,
3433 struct btrfs_device
*device
)
3437 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3438 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3439 device
->total_bytes
= device
->disk_total_bytes
;
3440 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3441 device
->type
= btrfs_device_type(leaf
, dev_item
);
3442 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3443 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3444 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3446 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3447 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3452 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3454 struct btrfs_fs_devices
*fs_devices
;
3457 mutex_lock(&uuid_mutex
);
3459 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3460 while (fs_devices
) {
3461 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3465 fs_devices
= fs_devices
->seed
;
3468 fs_devices
= find_fsid(fsid
);
3474 fs_devices
= clone_fs_devices(fs_devices
);
3475 if (IS_ERR(fs_devices
)) {
3476 ret
= PTR_ERR(fs_devices
);
3480 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3481 root
->fs_info
->bdev_holder
);
3485 if (!fs_devices
->seeding
) {
3486 __btrfs_close_devices(fs_devices
);
3487 free_fs_devices(fs_devices
);
3492 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3493 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3495 mutex_unlock(&uuid_mutex
);
3499 static int read_one_dev(struct btrfs_root
*root
,
3500 struct extent_buffer
*leaf
,
3501 struct btrfs_dev_item
*dev_item
)
3503 struct btrfs_device
*device
;
3506 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3507 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3509 devid
= btrfs_device_id(leaf
, dev_item
);
3510 read_extent_buffer(leaf
, dev_uuid
,
3511 (unsigned long)btrfs_device_uuid(dev_item
),
3513 read_extent_buffer(leaf
, fs_uuid
,
3514 (unsigned long)btrfs_device_fsid(dev_item
),
3517 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3518 ret
= open_seed_devices(root
, fs_uuid
);
3519 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3523 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3524 if (!device
|| !device
->bdev
) {
3525 if (!btrfs_test_opt(root
, DEGRADED
))
3529 printk(KERN_WARNING
"warning devid %llu missing\n",
3530 (unsigned long long)devid
);
3531 device
= add_missing_dev(root
, devid
, dev_uuid
);
3534 } else if (!device
->missing
) {
3536 * this happens when a device that was properly setup
3537 * in the device info lists suddenly goes bad.
3538 * device->bdev is NULL, and so we have to set
3539 * device->missing to one here
3541 root
->fs_info
->fs_devices
->missing_devices
++;
3542 device
->missing
= 1;
3546 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3547 BUG_ON(device
->writeable
);
3548 if (device
->generation
!=
3549 btrfs_device_generation(leaf
, dev_item
))
3553 fill_device_from_item(leaf
, dev_item
, device
);
3554 device
->dev_root
= root
->fs_info
->dev_root
;
3555 device
->in_fs_metadata
= 1;
3556 if (device
->writeable
)
3557 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3562 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3564 struct btrfs_dev_item
*dev_item
;
3566 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3568 return read_one_dev(root
, buf
, dev_item
);
3571 int btrfs_read_sys_array(struct btrfs_root
*root
)
3573 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3574 struct extent_buffer
*sb
;
3575 struct btrfs_disk_key
*disk_key
;
3576 struct btrfs_chunk
*chunk
;
3578 unsigned long sb_ptr
;
3584 struct btrfs_key key
;
3586 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3587 BTRFS_SUPER_INFO_SIZE
);
3590 btrfs_set_buffer_uptodate(sb
);
3591 btrfs_set_buffer_lockdep_class(sb
, 0);
3593 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3594 array_size
= btrfs_super_sys_array_size(super_copy
);
3596 ptr
= super_copy
->sys_chunk_array
;
3597 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3600 while (cur
< array_size
) {
3601 disk_key
= (struct btrfs_disk_key
*)ptr
;
3602 btrfs_disk_key_to_cpu(&key
, disk_key
);
3604 len
= sizeof(*disk_key
); ptr
+= len
;
3608 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3609 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3610 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3613 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3614 len
= btrfs_chunk_item_size(num_stripes
);
3623 free_extent_buffer(sb
);
3627 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3629 struct btrfs_path
*path
;
3630 struct extent_buffer
*leaf
;
3631 struct btrfs_key key
;
3632 struct btrfs_key found_key
;
3636 root
= root
->fs_info
->chunk_root
;
3638 path
= btrfs_alloc_path();
3642 /* first we search for all of the device items, and then we
3643 * read in all of the chunk items. This way we can create chunk
3644 * mappings that reference all of the devices that are afound
3646 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3650 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3654 leaf
= path
->nodes
[0];
3655 slot
= path
->slots
[0];
3656 if (slot
>= btrfs_header_nritems(leaf
)) {
3657 ret
= btrfs_next_leaf(root
, path
);
3664 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3665 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3666 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3668 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3669 struct btrfs_dev_item
*dev_item
;
3670 dev_item
= btrfs_item_ptr(leaf
, slot
,
3671 struct btrfs_dev_item
);
3672 ret
= read_one_dev(root
, leaf
, dev_item
);
3676 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3677 struct btrfs_chunk
*chunk
;
3678 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3679 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3685 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3687 btrfs_release_path(root
, path
);
3692 btrfs_free_path(path
);