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 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
42 (sizeof(struct btrfs_bio_stripe) * (n)))
44 static DEFINE_MUTEX(uuid_mutex
);
45 static LIST_HEAD(fs_uuids
);
47 void btrfs_lock_volumes(void)
49 mutex_lock(&uuid_mutex
);
52 void btrfs_unlock_volumes(void)
54 mutex_unlock(&uuid_mutex
);
57 static void lock_chunks(struct btrfs_root
*root
)
59 mutex_lock(&root
->fs_info
->chunk_mutex
);
62 static void unlock_chunks(struct btrfs_root
*root
)
64 mutex_unlock(&root
->fs_info
->chunk_mutex
);
67 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
69 struct btrfs_device
*device
;
70 WARN_ON(fs_devices
->opened
);
71 while (!list_empty(&fs_devices
->devices
)) {
72 device
= list_entry(fs_devices
->devices
.next
,
73 struct btrfs_device
, dev_list
);
74 list_del(&device
->dev_list
);
81 int btrfs_cleanup_fs_uuids(void)
83 struct btrfs_fs_devices
*fs_devices
;
85 while (!list_empty(&fs_uuids
)) {
86 fs_devices
= list_entry(fs_uuids
.next
,
87 struct btrfs_fs_devices
, list
);
88 list_del(&fs_devices
->list
);
89 free_fs_devices(fs_devices
);
94 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
97 struct btrfs_device
*dev
;
99 list_for_each_entry(dev
, head
, dev_list
) {
100 if (dev
->devid
== devid
&&
101 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
108 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
110 struct btrfs_fs_devices
*fs_devices
;
112 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
113 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
119 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
120 struct bio
*head
, struct bio
*tail
)
123 struct bio
*old_head
;
125 old_head
= pending_bios
->head
;
126 pending_bios
->head
= head
;
127 if (pending_bios
->tail
)
128 tail
->bi_next
= old_head
;
130 pending_bios
->tail
= tail
;
134 * we try to collect pending bios for a device so we don't get a large
135 * number of procs sending bios down to the same device. This greatly
136 * improves the schedulers ability to collect and merge the bios.
138 * But, it also turns into a long list of bios to process and that is sure
139 * to eventually make the worker thread block. The solution here is to
140 * make some progress and then put this work struct back at the end of
141 * the list if the block device is congested. This way, multiple devices
142 * can make progress from a single worker thread.
144 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
147 struct backing_dev_info
*bdi
;
148 struct btrfs_fs_info
*fs_info
;
149 struct btrfs_pending_bios
*pending_bios
;
153 unsigned long num_run
;
154 unsigned long batch_run
= 0;
156 unsigned long last_waited
= 0;
158 struct blk_plug plug
;
161 * this function runs all the bios we've collected for
162 * a particular device. We don't want to wander off to
163 * another device without first sending all of these down.
164 * So, setup a plug here and finish it off before we return
166 blk_start_plug(&plug
);
168 bdi
= blk_get_backing_dev_info(device
->bdev
);
169 fs_info
= device
->dev_root
->fs_info
;
170 limit
= btrfs_async_submit_limit(fs_info
);
171 limit
= limit
* 2 / 3;
174 spin_lock(&device
->io_lock
);
179 /* take all the bios off the list at once and process them
180 * later on (without the lock held). But, remember the
181 * tail and other pointers so the bios can be properly reinserted
182 * into the list if we hit congestion
184 if (!force_reg
&& device
->pending_sync_bios
.head
) {
185 pending_bios
= &device
->pending_sync_bios
;
188 pending_bios
= &device
->pending_bios
;
192 pending
= pending_bios
->head
;
193 tail
= pending_bios
->tail
;
194 WARN_ON(pending
&& !tail
);
197 * if pending was null this time around, no bios need processing
198 * at all and we can stop. Otherwise it'll loop back up again
199 * and do an additional check so no bios are missed.
201 * device->running_pending is used to synchronize with the
204 if (device
->pending_sync_bios
.head
== NULL
&&
205 device
->pending_bios
.head
== NULL
) {
207 device
->running_pending
= 0;
210 device
->running_pending
= 1;
213 pending_bios
->head
= NULL
;
214 pending_bios
->tail
= NULL
;
216 spin_unlock(&device
->io_lock
);
221 /* we want to work on both lists, but do more bios on the
222 * sync list than the regular list
225 pending_bios
!= &device
->pending_sync_bios
&&
226 device
->pending_sync_bios
.head
) ||
227 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
228 device
->pending_bios
.head
)) {
229 spin_lock(&device
->io_lock
);
230 requeue_list(pending_bios
, pending
, tail
);
235 pending
= pending
->bi_next
;
237 atomic_dec(&fs_info
->nr_async_bios
);
239 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
240 waitqueue_active(&fs_info
->async_submit_wait
))
241 wake_up(&fs_info
->async_submit_wait
);
243 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
245 submit_bio(cur
->bi_rw
, cur
);
252 * we made progress, there is more work to do and the bdi
253 * is now congested. Back off and let other work structs
256 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
257 fs_info
->fs_devices
->open_devices
> 1) {
258 struct io_context
*ioc
;
260 ioc
= current
->io_context
;
263 * the main goal here is that we don't want to
264 * block if we're going to be able to submit
265 * more requests without blocking.
267 * This code does two great things, it pokes into
268 * the elevator code from a filesystem _and_
269 * it makes assumptions about how batching works.
271 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
272 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
274 ioc
->last_waited
== last_waited
)) {
276 * we want to go through our batch of
277 * requests and stop. So, we copy out
278 * the ioc->last_waited time and test
279 * against it before looping
281 last_waited
= ioc
->last_waited
;
286 spin_lock(&device
->io_lock
);
287 requeue_list(pending_bios
, pending
, tail
);
288 device
->running_pending
= 1;
290 spin_unlock(&device
->io_lock
);
291 btrfs_requeue_work(&device
->work
);
300 spin_lock(&device
->io_lock
);
301 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
303 spin_unlock(&device
->io_lock
);
306 blk_finish_plug(&plug
);
310 static void pending_bios_fn(struct btrfs_work
*work
)
312 struct btrfs_device
*device
;
314 device
= container_of(work
, struct btrfs_device
, work
);
315 run_scheduled_bios(device
);
318 static noinline
int device_list_add(const char *path
,
319 struct btrfs_super_block
*disk_super
,
320 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
322 struct btrfs_device
*device
;
323 struct btrfs_fs_devices
*fs_devices
;
324 u64 found_transid
= btrfs_super_generation(disk_super
);
327 fs_devices
= find_fsid(disk_super
->fsid
);
329 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
332 INIT_LIST_HEAD(&fs_devices
->devices
);
333 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
334 list_add(&fs_devices
->list
, &fs_uuids
);
335 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
336 fs_devices
->latest_devid
= devid
;
337 fs_devices
->latest_trans
= found_transid
;
338 mutex_init(&fs_devices
->device_list_mutex
);
341 device
= __find_device(&fs_devices
->devices
, devid
,
342 disk_super
->dev_item
.uuid
);
345 if (fs_devices
->opened
)
348 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
350 /* we can safely leave the fs_devices entry around */
353 device
->devid
= devid
;
354 device
->work
.func
= pending_bios_fn
;
355 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
357 spin_lock_init(&device
->io_lock
);
358 device
->name
= kstrdup(path
, GFP_NOFS
);
363 INIT_LIST_HEAD(&device
->dev_alloc_list
);
365 mutex_lock(&fs_devices
->device_list_mutex
);
366 list_add(&device
->dev_list
, &fs_devices
->devices
);
367 mutex_unlock(&fs_devices
->device_list_mutex
);
369 device
->fs_devices
= fs_devices
;
370 fs_devices
->num_devices
++;
371 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
372 name
= kstrdup(path
, GFP_NOFS
);
377 if (device
->missing
) {
378 fs_devices
->missing_devices
--;
383 if (found_transid
> fs_devices
->latest_trans
) {
384 fs_devices
->latest_devid
= devid
;
385 fs_devices
->latest_trans
= found_transid
;
387 *fs_devices_ret
= fs_devices
;
391 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
393 struct btrfs_fs_devices
*fs_devices
;
394 struct btrfs_device
*device
;
395 struct btrfs_device
*orig_dev
;
397 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
399 return ERR_PTR(-ENOMEM
);
401 INIT_LIST_HEAD(&fs_devices
->devices
);
402 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
403 INIT_LIST_HEAD(&fs_devices
->list
);
404 mutex_init(&fs_devices
->device_list_mutex
);
405 fs_devices
->latest_devid
= orig
->latest_devid
;
406 fs_devices
->latest_trans
= orig
->latest_trans
;
407 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
409 mutex_lock(&orig
->device_list_mutex
);
410 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
411 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
415 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
421 device
->devid
= orig_dev
->devid
;
422 device
->work
.func
= pending_bios_fn
;
423 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
424 spin_lock_init(&device
->io_lock
);
425 INIT_LIST_HEAD(&device
->dev_list
);
426 INIT_LIST_HEAD(&device
->dev_alloc_list
);
428 list_add(&device
->dev_list
, &fs_devices
->devices
);
429 device
->fs_devices
= fs_devices
;
430 fs_devices
->num_devices
++;
432 mutex_unlock(&orig
->device_list_mutex
);
435 mutex_unlock(&orig
->device_list_mutex
);
436 free_fs_devices(fs_devices
);
437 return ERR_PTR(-ENOMEM
);
440 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
442 struct btrfs_device
*device
, *next
;
444 mutex_lock(&uuid_mutex
);
446 mutex_lock(&fs_devices
->device_list_mutex
);
447 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
448 if (device
->in_fs_metadata
)
452 blkdev_put(device
->bdev
, device
->mode
);
454 fs_devices
->open_devices
--;
456 if (device
->writeable
) {
457 list_del_init(&device
->dev_alloc_list
);
458 device
->writeable
= 0;
459 fs_devices
->rw_devices
--;
461 list_del_init(&device
->dev_list
);
462 fs_devices
->num_devices
--;
466 mutex_unlock(&fs_devices
->device_list_mutex
);
468 if (fs_devices
->seed
) {
469 fs_devices
= fs_devices
->seed
;
473 mutex_unlock(&uuid_mutex
);
477 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
479 struct btrfs_device
*device
;
481 if (--fs_devices
->opened
> 0)
484 mutex_lock(&fs_devices
->device_list_mutex
);
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 mutex_unlock(&fs_devices
->device_list_mutex
);
501 WARN_ON(fs_devices
->open_devices
);
502 WARN_ON(fs_devices
->rw_devices
);
503 fs_devices
->opened
= 0;
504 fs_devices
->seeding
= 0;
509 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
511 struct btrfs_fs_devices
*seed_devices
= NULL
;
514 mutex_lock(&uuid_mutex
);
515 ret
= __btrfs_close_devices(fs_devices
);
516 if (!fs_devices
->opened
) {
517 seed_devices
= fs_devices
->seed
;
518 fs_devices
->seed
= NULL
;
520 mutex_unlock(&uuid_mutex
);
522 while (seed_devices
) {
523 fs_devices
= seed_devices
;
524 seed_devices
= fs_devices
->seed
;
525 __btrfs_close_devices(fs_devices
);
526 free_fs_devices(fs_devices
);
531 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
532 fmode_t flags
, void *holder
)
534 struct block_device
*bdev
;
535 struct list_head
*head
= &fs_devices
->devices
;
536 struct btrfs_device
*device
;
537 struct block_device
*latest_bdev
= NULL
;
538 struct buffer_head
*bh
;
539 struct btrfs_super_block
*disk_super
;
540 u64 latest_devid
= 0;
541 u64 latest_transid
= 0;
548 list_for_each_entry(device
, head
, dev_list
) {
554 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
556 printk(KERN_INFO
"open %s failed\n", device
->name
);
559 set_blocksize(bdev
, 4096);
561 bh
= btrfs_read_dev_super(bdev
);
567 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
568 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
569 if (devid
!= device
->devid
)
572 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
576 device
->generation
= btrfs_super_generation(disk_super
);
577 if (!latest_transid
|| device
->generation
> latest_transid
) {
578 latest_devid
= devid
;
579 latest_transid
= device
->generation
;
583 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
584 device
->writeable
= 0;
586 device
->writeable
= !bdev_read_only(bdev
);
591 device
->in_fs_metadata
= 0;
592 device
->mode
= flags
;
594 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
595 fs_devices
->rotating
= 1;
597 fs_devices
->open_devices
++;
598 if (device
->writeable
) {
599 fs_devices
->rw_devices
++;
600 list_add(&device
->dev_alloc_list
,
601 &fs_devices
->alloc_list
);
609 blkdev_put(bdev
, flags
);
613 if (fs_devices
->open_devices
== 0) {
617 fs_devices
->seeding
= seeding
;
618 fs_devices
->opened
= 1;
619 fs_devices
->latest_bdev
= latest_bdev
;
620 fs_devices
->latest_devid
= latest_devid
;
621 fs_devices
->latest_trans
= latest_transid
;
622 fs_devices
->total_rw_bytes
= 0;
627 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
628 fmode_t flags
, void *holder
)
632 mutex_lock(&uuid_mutex
);
633 if (fs_devices
->opened
) {
634 fs_devices
->opened
++;
637 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
639 mutex_unlock(&uuid_mutex
);
643 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
644 struct btrfs_fs_devices
**fs_devices_ret
)
646 struct btrfs_super_block
*disk_super
;
647 struct block_device
*bdev
;
648 struct buffer_head
*bh
;
653 mutex_lock(&uuid_mutex
);
656 bdev
= blkdev_get_by_path(path
, flags
, holder
);
663 ret
= set_blocksize(bdev
, 4096);
666 bh
= btrfs_read_dev_super(bdev
);
671 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
672 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
673 transid
= btrfs_super_generation(disk_super
);
674 if (disk_super
->label
[0])
675 printk(KERN_INFO
"device label %s ", disk_super
->label
);
677 /* FIXME, make a readl uuid parser */
678 printk(KERN_INFO
"device fsid %llx-%llx ",
679 *(unsigned long long *)disk_super
->fsid
,
680 *(unsigned long long *)(disk_super
->fsid
+ 8));
682 printk(KERN_CONT
"devid %llu transid %llu %s\n",
683 (unsigned long long)devid
, (unsigned long long)transid
, path
);
684 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
688 blkdev_put(bdev
, flags
);
690 mutex_unlock(&uuid_mutex
);
694 /* helper to account the used device space in the range */
695 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
696 u64 end
, u64
*length
)
698 struct btrfs_key key
;
699 struct btrfs_root
*root
= device
->dev_root
;
700 struct btrfs_dev_extent
*dev_extent
;
701 struct btrfs_path
*path
;
705 struct extent_buffer
*l
;
709 if (start
>= device
->total_bytes
)
712 path
= btrfs_alloc_path();
717 key
.objectid
= device
->devid
;
719 key
.type
= BTRFS_DEV_EXTENT_KEY
;
721 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
725 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
732 slot
= path
->slots
[0];
733 if (slot
>= btrfs_header_nritems(l
)) {
734 ret
= btrfs_next_leaf(root
, path
);
742 btrfs_item_key_to_cpu(l
, &key
, slot
);
744 if (key
.objectid
< device
->devid
)
747 if (key
.objectid
> device
->devid
)
750 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
753 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
754 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
756 if (key
.offset
<= start
&& extent_end
> end
) {
757 *length
= end
- start
+ 1;
759 } else if (key
.offset
<= start
&& extent_end
> start
)
760 *length
+= extent_end
- start
;
761 else if (key
.offset
> start
&& extent_end
<= end
)
762 *length
+= extent_end
- key
.offset
;
763 else if (key
.offset
> start
&& key
.offset
<= end
) {
764 *length
+= end
- key
.offset
+ 1;
766 } else if (key
.offset
> end
)
774 btrfs_free_path(path
);
779 * find_free_dev_extent - find free space in the specified device
780 * @trans: transaction handler
781 * @device: the device which we search the free space in
782 * @num_bytes: the size of the free space that we need
783 * @start: store the start of the free space.
784 * @len: the size of the free space. that we find, or the size of the max
785 * free space if we don't find suitable free space
787 * this uses a pretty simple search, the expectation is that it is
788 * called very infrequently and that a given device has a small number
791 * @start is used to store the start of the free space if we find. But if we
792 * don't find suitable free space, it will be used to store the start position
793 * of the max free space.
795 * @len is used to store the size of the free space that we find.
796 * But if we don't find suitable free space, it is used to store the size of
797 * the max free space.
799 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
800 struct btrfs_device
*device
, u64 num_bytes
,
801 u64
*start
, u64
*len
)
803 struct btrfs_key key
;
804 struct btrfs_root
*root
= device
->dev_root
;
805 struct btrfs_dev_extent
*dev_extent
;
806 struct btrfs_path
*path
;
812 u64 search_end
= device
->total_bytes
;
815 struct extent_buffer
*l
;
817 /* FIXME use last free of some kind */
819 /* we don't want to overwrite the superblock on the drive,
820 * so we make sure to start at an offset of at least 1MB
822 search_start
= 1024 * 1024;
824 if (root
->fs_info
->alloc_start
+ num_bytes
<= search_end
)
825 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
827 max_hole_start
= search_start
;
830 if (search_start
>= search_end
) {
835 path
= btrfs_alloc_path();
842 key
.objectid
= device
->devid
;
843 key
.offset
= search_start
;
844 key
.type
= BTRFS_DEV_EXTENT_KEY
;
846 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
850 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
857 slot
= path
->slots
[0];
858 if (slot
>= btrfs_header_nritems(l
)) {
859 ret
= btrfs_next_leaf(root
, path
);
867 btrfs_item_key_to_cpu(l
, &key
, slot
);
869 if (key
.objectid
< device
->devid
)
872 if (key
.objectid
> device
->devid
)
875 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
878 if (key
.offset
> search_start
) {
879 hole_size
= key
.offset
- search_start
;
881 if (hole_size
> max_hole_size
) {
882 max_hole_start
= search_start
;
883 max_hole_size
= hole_size
;
887 * If this free space is greater than which we need,
888 * it must be the max free space that we have found
889 * until now, so max_hole_start must point to the start
890 * of this free space and the length of this free space
891 * is stored in max_hole_size. Thus, we return
892 * max_hole_start and max_hole_size and go back to the
895 if (hole_size
>= num_bytes
) {
901 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
902 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
904 if (extent_end
> search_start
)
905 search_start
= extent_end
;
911 hole_size
= search_end
- search_start
;
912 if (hole_size
> max_hole_size
) {
913 max_hole_start
= search_start
;
914 max_hole_size
= hole_size
;
918 if (hole_size
< num_bytes
)
924 btrfs_free_path(path
);
926 *start
= max_hole_start
;
928 *len
= max_hole_size
;
932 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
933 struct btrfs_device
*device
,
937 struct btrfs_path
*path
;
938 struct btrfs_root
*root
= device
->dev_root
;
939 struct btrfs_key key
;
940 struct btrfs_key found_key
;
941 struct extent_buffer
*leaf
= NULL
;
942 struct btrfs_dev_extent
*extent
= NULL
;
944 path
= btrfs_alloc_path();
948 key
.objectid
= device
->devid
;
950 key
.type
= BTRFS_DEV_EXTENT_KEY
;
952 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
954 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
955 BTRFS_DEV_EXTENT_KEY
);
958 leaf
= path
->nodes
[0];
959 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
960 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
961 struct btrfs_dev_extent
);
962 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
963 btrfs_dev_extent_length(leaf
, extent
) < start
);
964 } else if (ret
== 0) {
965 leaf
= path
->nodes
[0];
966 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
967 struct btrfs_dev_extent
);
971 if (device
->bytes_used
> 0)
972 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
973 ret
= btrfs_del_item(trans
, root
, path
);
976 btrfs_free_path(path
);
980 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
981 struct btrfs_device
*device
,
982 u64 chunk_tree
, u64 chunk_objectid
,
983 u64 chunk_offset
, u64 start
, u64 num_bytes
)
986 struct btrfs_path
*path
;
987 struct btrfs_root
*root
= device
->dev_root
;
988 struct btrfs_dev_extent
*extent
;
989 struct extent_buffer
*leaf
;
990 struct btrfs_key key
;
992 WARN_ON(!device
->in_fs_metadata
);
993 path
= btrfs_alloc_path();
997 key
.objectid
= device
->devid
;
999 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1000 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1004 leaf
= path
->nodes
[0];
1005 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1006 struct btrfs_dev_extent
);
1007 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1008 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1009 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1011 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1012 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1015 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1016 btrfs_mark_buffer_dirty(leaf
);
1017 btrfs_free_path(path
);
1021 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1022 u64 objectid
, u64
*offset
)
1024 struct btrfs_path
*path
;
1026 struct btrfs_key key
;
1027 struct btrfs_chunk
*chunk
;
1028 struct btrfs_key found_key
;
1030 path
= btrfs_alloc_path();
1033 key
.objectid
= objectid
;
1034 key
.offset
= (u64
)-1;
1035 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1037 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1043 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1047 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1049 if (found_key
.objectid
!= objectid
)
1052 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1053 struct btrfs_chunk
);
1054 *offset
= found_key
.offset
+
1055 btrfs_chunk_length(path
->nodes
[0], chunk
);
1060 btrfs_free_path(path
);
1064 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1067 struct btrfs_key key
;
1068 struct btrfs_key found_key
;
1069 struct btrfs_path
*path
;
1071 root
= root
->fs_info
->chunk_root
;
1073 path
= btrfs_alloc_path();
1077 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1078 key
.type
= BTRFS_DEV_ITEM_KEY
;
1079 key
.offset
= (u64
)-1;
1081 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1087 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1088 BTRFS_DEV_ITEM_KEY
);
1092 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1094 *objectid
= found_key
.offset
+ 1;
1098 btrfs_free_path(path
);
1103 * the device information is stored in the chunk root
1104 * the btrfs_device struct should be fully filled in
1106 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1107 struct btrfs_root
*root
,
1108 struct btrfs_device
*device
)
1111 struct btrfs_path
*path
;
1112 struct btrfs_dev_item
*dev_item
;
1113 struct extent_buffer
*leaf
;
1114 struct btrfs_key key
;
1117 root
= root
->fs_info
->chunk_root
;
1119 path
= btrfs_alloc_path();
1123 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1124 key
.type
= BTRFS_DEV_ITEM_KEY
;
1125 key
.offset
= device
->devid
;
1127 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1132 leaf
= path
->nodes
[0];
1133 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1135 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1136 btrfs_set_device_generation(leaf
, dev_item
, 0);
1137 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1138 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1139 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1140 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1141 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1142 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1143 btrfs_set_device_group(leaf
, dev_item
, 0);
1144 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1145 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1146 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1148 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1149 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1150 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1151 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1152 btrfs_mark_buffer_dirty(leaf
);
1156 btrfs_free_path(path
);
1160 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1161 struct btrfs_device
*device
)
1164 struct btrfs_path
*path
;
1165 struct btrfs_key key
;
1166 struct btrfs_trans_handle
*trans
;
1168 root
= root
->fs_info
->chunk_root
;
1170 path
= btrfs_alloc_path();
1174 trans
= btrfs_start_transaction(root
, 0);
1175 if (IS_ERR(trans
)) {
1176 btrfs_free_path(path
);
1177 return PTR_ERR(trans
);
1179 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1180 key
.type
= BTRFS_DEV_ITEM_KEY
;
1181 key
.offset
= device
->devid
;
1184 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1193 ret
= btrfs_del_item(trans
, root
, path
);
1197 btrfs_free_path(path
);
1198 unlock_chunks(root
);
1199 btrfs_commit_transaction(trans
, root
);
1203 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1205 struct btrfs_device
*device
;
1206 struct btrfs_device
*next_device
;
1207 struct block_device
*bdev
;
1208 struct buffer_head
*bh
= NULL
;
1209 struct btrfs_super_block
*disk_super
;
1216 mutex_lock(&uuid_mutex
);
1217 mutex_lock(&root
->fs_info
->volume_mutex
);
1219 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1220 root
->fs_info
->avail_system_alloc_bits
|
1221 root
->fs_info
->avail_metadata_alloc_bits
;
1223 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1224 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1225 printk(KERN_ERR
"btrfs: unable to go below four devices "
1231 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1232 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1233 printk(KERN_ERR
"btrfs: unable to go below two "
1234 "devices on raid1\n");
1239 if (strcmp(device_path
, "missing") == 0) {
1240 struct list_head
*devices
;
1241 struct btrfs_device
*tmp
;
1244 devices
= &root
->fs_info
->fs_devices
->devices
;
1245 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1246 list_for_each_entry(tmp
, devices
, dev_list
) {
1247 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1252 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1257 printk(KERN_ERR
"btrfs: no missing devices found to "
1262 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1263 root
->fs_info
->bdev_holder
);
1265 ret
= PTR_ERR(bdev
);
1269 set_blocksize(bdev
, 4096);
1270 bh
= btrfs_read_dev_super(bdev
);
1275 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1276 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1277 dev_uuid
= disk_super
->dev_item
.uuid
;
1278 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1286 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1287 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1293 if (device
->writeable
) {
1294 list_del_init(&device
->dev_alloc_list
);
1295 root
->fs_info
->fs_devices
->rw_devices
--;
1298 ret
= btrfs_shrink_device(device
, 0);
1302 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1306 device
->in_fs_metadata
= 0;
1309 * the device list mutex makes sure that we don't change
1310 * the device list while someone else is writing out all
1311 * the device supers.
1313 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1314 list_del_init(&device
->dev_list
);
1315 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1317 device
->fs_devices
->num_devices
--;
1319 if (device
->missing
)
1320 root
->fs_info
->fs_devices
->missing_devices
--;
1322 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1323 struct btrfs_device
, dev_list
);
1324 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1325 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1326 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1327 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1330 blkdev_put(device
->bdev
, device
->mode
);
1331 device
->bdev
= NULL
;
1332 device
->fs_devices
->open_devices
--;
1335 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1336 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1338 if (device
->fs_devices
->open_devices
== 0) {
1339 struct btrfs_fs_devices
*fs_devices
;
1340 fs_devices
= root
->fs_info
->fs_devices
;
1341 while (fs_devices
) {
1342 if (fs_devices
->seed
== device
->fs_devices
)
1344 fs_devices
= fs_devices
->seed
;
1346 fs_devices
->seed
= device
->fs_devices
->seed
;
1347 device
->fs_devices
->seed
= NULL
;
1348 __btrfs_close_devices(device
->fs_devices
);
1349 free_fs_devices(device
->fs_devices
);
1353 * at this point, the device is zero sized. We want to
1354 * remove it from the devices list and zero out the old super
1356 if (device
->writeable
) {
1357 /* make sure this device isn't detected as part of
1360 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1361 set_buffer_dirty(bh
);
1362 sync_dirty_buffer(bh
);
1365 kfree(device
->name
);
1373 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1375 mutex_unlock(&root
->fs_info
->volume_mutex
);
1376 mutex_unlock(&uuid_mutex
);
1379 if (device
->writeable
) {
1380 list_add(&device
->dev_alloc_list
,
1381 &root
->fs_info
->fs_devices
->alloc_list
);
1382 root
->fs_info
->fs_devices
->rw_devices
++;
1388 * does all the dirty work required for changing file system's UUID.
1390 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1391 struct btrfs_root
*root
)
1393 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1394 struct btrfs_fs_devices
*old_devices
;
1395 struct btrfs_fs_devices
*seed_devices
;
1396 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1397 struct btrfs_device
*device
;
1400 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1401 if (!fs_devices
->seeding
)
1404 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1408 old_devices
= clone_fs_devices(fs_devices
);
1409 if (IS_ERR(old_devices
)) {
1410 kfree(seed_devices
);
1411 return PTR_ERR(old_devices
);
1414 list_add(&old_devices
->list
, &fs_uuids
);
1416 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1417 seed_devices
->opened
= 1;
1418 INIT_LIST_HEAD(&seed_devices
->devices
);
1419 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1420 mutex_init(&seed_devices
->device_list_mutex
);
1422 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1423 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1424 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1426 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1427 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1428 device
->fs_devices
= seed_devices
;
1431 fs_devices
->seeding
= 0;
1432 fs_devices
->num_devices
= 0;
1433 fs_devices
->open_devices
= 0;
1434 fs_devices
->seed
= seed_devices
;
1436 generate_random_uuid(fs_devices
->fsid
);
1437 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1438 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1439 super_flags
= btrfs_super_flags(disk_super
) &
1440 ~BTRFS_SUPER_FLAG_SEEDING
;
1441 btrfs_set_super_flags(disk_super
, super_flags
);
1447 * strore the expected generation for seed devices in device items.
1449 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1450 struct btrfs_root
*root
)
1452 struct btrfs_path
*path
;
1453 struct extent_buffer
*leaf
;
1454 struct btrfs_dev_item
*dev_item
;
1455 struct btrfs_device
*device
;
1456 struct btrfs_key key
;
1457 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1458 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1462 path
= btrfs_alloc_path();
1466 root
= root
->fs_info
->chunk_root
;
1467 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1469 key
.type
= BTRFS_DEV_ITEM_KEY
;
1472 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1476 leaf
= path
->nodes
[0];
1478 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1479 ret
= btrfs_next_leaf(root
, path
);
1484 leaf
= path
->nodes
[0];
1485 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1486 btrfs_release_path(root
, path
);
1490 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1491 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1492 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1495 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1496 struct btrfs_dev_item
);
1497 devid
= btrfs_device_id(leaf
, dev_item
);
1498 read_extent_buffer(leaf
, dev_uuid
,
1499 (unsigned long)btrfs_device_uuid(dev_item
),
1501 read_extent_buffer(leaf
, fs_uuid
,
1502 (unsigned long)btrfs_device_fsid(dev_item
),
1504 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1507 if (device
->fs_devices
->seeding
) {
1508 btrfs_set_device_generation(leaf
, dev_item
,
1509 device
->generation
);
1510 btrfs_mark_buffer_dirty(leaf
);
1518 btrfs_free_path(path
);
1522 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1524 struct btrfs_trans_handle
*trans
;
1525 struct btrfs_device
*device
;
1526 struct block_device
*bdev
;
1527 struct list_head
*devices
;
1528 struct super_block
*sb
= root
->fs_info
->sb
;
1530 int seeding_dev
= 0;
1533 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1536 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1537 root
->fs_info
->bdev_holder
);
1539 return PTR_ERR(bdev
);
1541 if (root
->fs_info
->fs_devices
->seeding
) {
1543 down_write(&sb
->s_umount
);
1544 mutex_lock(&uuid_mutex
);
1547 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1548 mutex_lock(&root
->fs_info
->volume_mutex
);
1550 devices
= &root
->fs_info
->fs_devices
->devices
;
1552 * we have the volume lock, so we don't need the extra
1553 * device list mutex while reading the list here.
1555 list_for_each_entry(device
, devices
, dev_list
) {
1556 if (device
->bdev
== bdev
) {
1562 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1564 /* we can safely leave the fs_devices entry around */
1569 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1570 if (!device
->name
) {
1576 ret
= find_next_devid(root
, &device
->devid
);
1578 kfree(device
->name
);
1583 trans
= btrfs_start_transaction(root
, 0);
1584 if (IS_ERR(trans
)) {
1585 kfree(device
->name
);
1587 ret
= PTR_ERR(trans
);
1593 device
->writeable
= 1;
1594 device
->work
.func
= pending_bios_fn
;
1595 generate_random_uuid(device
->uuid
);
1596 spin_lock_init(&device
->io_lock
);
1597 device
->generation
= trans
->transid
;
1598 device
->io_width
= root
->sectorsize
;
1599 device
->io_align
= root
->sectorsize
;
1600 device
->sector_size
= root
->sectorsize
;
1601 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1602 device
->disk_total_bytes
= device
->total_bytes
;
1603 device
->dev_root
= root
->fs_info
->dev_root
;
1604 device
->bdev
= bdev
;
1605 device
->in_fs_metadata
= 1;
1606 device
->mode
= FMODE_EXCL
;
1607 set_blocksize(device
->bdev
, 4096);
1610 sb
->s_flags
&= ~MS_RDONLY
;
1611 ret
= btrfs_prepare_sprout(trans
, root
);
1615 device
->fs_devices
= root
->fs_info
->fs_devices
;
1618 * we don't want write_supers to jump in here with our device
1621 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1622 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1623 list_add(&device
->dev_alloc_list
,
1624 &root
->fs_info
->fs_devices
->alloc_list
);
1625 root
->fs_info
->fs_devices
->num_devices
++;
1626 root
->fs_info
->fs_devices
->open_devices
++;
1627 root
->fs_info
->fs_devices
->rw_devices
++;
1628 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1630 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1631 root
->fs_info
->fs_devices
->rotating
= 1;
1633 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1634 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1635 total_bytes
+ device
->total_bytes
);
1637 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1638 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1640 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1643 ret
= init_first_rw_device(trans
, root
, device
);
1645 ret
= btrfs_finish_sprout(trans
, root
);
1648 ret
= btrfs_add_device(trans
, root
, device
);
1652 * we've got more storage, clear any full flags on the space
1655 btrfs_clear_space_info_full(root
->fs_info
);
1657 unlock_chunks(root
);
1658 btrfs_commit_transaction(trans
, root
);
1661 mutex_unlock(&uuid_mutex
);
1662 up_write(&sb
->s_umount
);
1664 ret
= btrfs_relocate_sys_chunks(root
);
1668 mutex_unlock(&root
->fs_info
->volume_mutex
);
1671 blkdev_put(bdev
, FMODE_EXCL
);
1673 mutex_unlock(&uuid_mutex
);
1674 up_write(&sb
->s_umount
);
1679 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1680 struct btrfs_device
*device
)
1683 struct btrfs_path
*path
;
1684 struct btrfs_root
*root
;
1685 struct btrfs_dev_item
*dev_item
;
1686 struct extent_buffer
*leaf
;
1687 struct btrfs_key key
;
1689 root
= device
->dev_root
->fs_info
->chunk_root
;
1691 path
= btrfs_alloc_path();
1695 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1696 key
.type
= BTRFS_DEV_ITEM_KEY
;
1697 key
.offset
= device
->devid
;
1699 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1708 leaf
= path
->nodes
[0];
1709 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1711 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1712 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1713 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1714 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1715 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1716 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1717 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1718 btrfs_mark_buffer_dirty(leaf
);
1721 btrfs_free_path(path
);
1725 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1726 struct btrfs_device
*device
, u64 new_size
)
1728 struct btrfs_super_block
*super_copy
=
1729 &device
->dev_root
->fs_info
->super_copy
;
1730 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1731 u64 diff
= new_size
- device
->total_bytes
;
1733 if (!device
->writeable
)
1735 if (new_size
<= device
->total_bytes
)
1738 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1739 device
->fs_devices
->total_rw_bytes
+= diff
;
1741 device
->total_bytes
= new_size
;
1742 device
->disk_total_bytes
= new_size
;
1743 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1745 return btrfs_update_device(trans
, device
);
1748 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1749 struct btrfs_device
*device
, u64 new_size
)
1752 lock_chunks(device
->dev_root
);
1753 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1754 unlock_chunks(device
->dev_root
);
1758 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1759 struct btrfs_root
*root
,
1760 u64 chunk_tree
, u64 chunk_objectid
,
1764 struct btrfs_path
*path
;
1765 struct btrfs_key key
;
1767 root
= root
->fs_info
->chunk_root
;
1768 path
= btrfs_alloc_path();
1772 key
.objectid
= chunk_objectid
;
1773 key
.offset
= chunk_offset
;
1774 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1776 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1779 ret
= btrfs_del_item(trans
, root
, path
);
1781 btrfs_free_path(path
);
1785 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1788 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1789 struct btrfs_disk_key
*disk_key
;
1790 struct btrfs_chunk
*chunk
;
1797 struct btrfs_key key
;
1799 array_size
= btrfs_super_sys_array_size(super_copy
);
1801 ptr
= super_copy
->sys_chunk_array
;
1804 while (cur
< array_size
) {
1805 disk_key
= (struct btrfs_disk_key
*)ptr
;
1806 btrfs_disk_key_to_cpu(&key
, disk_key
);
1808 len
= sizeof(*disk_key
);
1810 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1811 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1812 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1813 len
+= btrfs_chunk_item_size(num_stripes
);
1818 if (key
.objectid
== chunk_objectid
&&
1819 key
.offset
== chunk_offset
) {
1820 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1822 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1831 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1832 u64 chunk_tree
, u64 chunk_objectid
,
1835 struct extent_map_tree
*em_tree
;
1836 struct btrfs_root
*extent_root
;
1837 struct btrfs_trans_handle
*trans
;
1838 struct extent_map
*em
;
1839 struct map_lookup
*map
;
1843 root
= root
->fs_info
->chunk_root
;
1844 extent_root
= root
->fs_info
->extent_root
;
1845 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1847 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1851 /* step one, relocate all the extents inside this chunk */
1852 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1856 trans
= btrfs_start_transaction(root
, 0);
1857 BUG_ON(IS_ERR(trans
));
1862 * step two, delete the device extents and the
1863 * chunk tree entries
1865 read_lock(&em_tree
->lock
);
1866 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1867 read_unlock(&em_tree
->lock
);
1869 BUG_ON(em
->start
> chunk_offset
||
1870 em
->start
+ em
->len
< chunk_offset
);
1871 map
= (struct map_lookup
*)em
->bdev
;
1873 for (i
= 0; i
< map
->num_stripes
; i
++) {
1874 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1875 map
->stripes
[i
].physical
);
1878 if (map
->stripes
[i
].dev
) {
1879 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1883 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1888 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1890 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1891 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1895 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1898 write_lock(&em_tree
->lock
);
1899 remove_extent_mapping(em_tree
, em
);
1900 write_unlock(&em_tree
->lock
);
1905 /* once for the tree */
1906 free_extent_map(em
);
1908 free_extent_map(em
);
1910 unlock_chunks(root
);
1911 btrfs_end_transaction(trans
, root
);
1915 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1917 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1918 struct btrfs_path
*path
;
1919 struct extent_buffer
*leaf
;
1920 struct btrfs_chunk
*chunk
;
1921 struct btrfs_key key
;
1922 struct btrfs_key found_key
;
1923 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1925 bool retried
= false;
1929 path
= btrfs_alloc_path();
1934 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1935 key
.offset
= (u64
)-1;
1936 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1939 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1944 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1951 leaf
= path
->nodes
[0];
1952 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1954 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1955 struct btrfs_chunk
);
1956 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1957 btrfs_release_path(chunk_root
, path
);
1959 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1960 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1969 if (found_key
.offset
== 0)
1971 key
.offset
= found_key
.offset
- 1;
1974 if (failed
&& !retried
) {
1978 } else if (failed
&& retried
) {
1983 btrfs_free_path(path
);
1987 static u64
div_factor(u64 num
, int factor
)
1996 int btrfs_balance(struct btrfs_root
*dev_root
)
1999 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2000 struct btrfs_device
*device
;
2003 struct btrfs_path
*path
;
2004 struct btrfs_key key
;
2005 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2006 struct btrfs_trans_handle
*trans
;
2007 struct btrfs_key found_key
;
2009 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2012 if (!capable(CAP_SYS_ADMIN
))
2015 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2016 dev_root
= dev_root
->fs_info
->dev_root
;
2018 /* step one make some room on all the devices */
2019 list_for_each_entry(device
, devices
, dev_list
) {
2020 old_size
= device
->total_bytes
;
2021 size_to_free
= div_factor(old_size
, 1);
2022 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2023 if (!device
->writeable
||
2024 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2027 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2032 trans
= btrfs_start_transaction(dev_root
, 0);
2033 BUG_ON(IS_ERR(trans
));
2035 ret
= btrfs_grow_device(trans
, device
, old_size
);
2038 btrfs_end_transaction(trans
, dev_root
);
2041 /* step two, relocate all the chunks */
2042 path
= btrfs_alloc_path();
2045 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2046 key
.offset
= (u64
)-1;
2047 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2050 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2055 * this shouldn't happen, it means the last relocate
2061 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2062 BTRFS_CHUNK_ITEM_KEY
);
2066 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2068 if (found_key
.objectid
!= key
.objectid
)
2071 /* chunk zero is special */
2072 if (found_key
.offset
== 0)
2075 btrfs_release_path(chunk_root
, path
);
2076 ret
= btrfs_relocate_chunk(chunk_root
,
2077 chunk_root
->root_key
.objectid
,
2080 BUG_ON(ret
&& ret
!= -ENOSPC
);
2081 key
.offset
= found_key
.offset
- 1;
2085 btrfs_free_path(path
);
2086 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2091 * shrinking a device means finding all of the device extents past
2092 * the new size, and then following the back refs to the chunks.
2093 * The chunk relocation code actually frees the device extent
2095 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2097 struct btrfs_trans_handle
*trans
;
2098 struct btrfs_root
*root
= device
->dev_root
;
2099 struct btrfs_dev_extent
*dev_extent
= NULL
;
2100 struct btrfs_path
*path
;
2108 bool retried
= false;
2109 struct extent_buffer
*l
;
2110 struct btrfs_key key
;
2111 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2112 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2113 u64 old_size
= device
->total_bytes
;
2114 u64 diff
= device
->total_bytes
- new_size
;
2116 if (new_size
>= device
->total_bytes
)
2119 path
= btrfs_alloc_path();
2127 device
->total_bytes
= new_size
;
2128 if (device
->writeable
)
2129 device
->fs_devices
->total_rw_bytes
-= diff
;
2130 unlock_chunks(root
);
2133 key
.objectid
= device
->devid
;
2134 key
.offset
= (u64
)-1;
2135 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2138 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2142 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2147 btrfs_release_path(root
, path
);
2152 slot
= path
->slots
[0];
2153 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2155 if (key
.objectid
!= device
->devid
) {
2156 btrfs_release_path(root
, path
);
2160 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2161 length
= btrfs_dev_extent_length(l
, dev_extent
);
2163 if (key
.offset
+ length
<= new_size
) {
2164 btrfs_release_path(root
, path
);
2168 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2169 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2170 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2171 btrfs_release_path(root
, path
);
2173 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2175 if (ret
&& ret
!= -ENOSPC
)
2182 if (failed
&& !retried
) {
2186 } else if (failed
&& retried
) {
2190 device
->total_bytes
= old_size
;
2191 if (device
->writeable
)
2192 device
->fs_devices
->total_rw_bytes
+= diff
;
2193 unlock_chunks(root
);
2197 /* Shrinking succeeded, else we would be at "done". */
2198 trans
= btrfs_start_transaction(root
, 0);
2199 if (IS_ERR(trans
)) {
2200 ret
= PTR_ERR(trans
);
2206 device
->disk_total_bytes
= new_size
;
2207 /* Now btrfs_update_device() will change the on-disk size. */
2208 ret
= btrfs_update_device(trans
, device
);
2210 unlock_chunks(root
);
2211 btrfs_end_transaction(trans
, root
);
2214 WARN_ON(diff
> old_total
);
2215 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2216 unlock_chunks(root
);
2217 btrfs_end_transaction(trans
, root
);
2219 btrfs_free_path(path
);
2223 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2224 struct btrfs_root
*root
,
2225 struct btrfs_key
*key
,
2226 struct btrfs_chunk
*chunk
, int item_size
)
2228 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2229 struct btrfs_disk_key disk_key
;
2233 array_size
= btrfs_super_sys_array_size(super_copy
);
2234 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2237 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2238 btrfs_cpu_key_to_disk(&disk_key
, key
);
2239 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2240 ptr
+= sizeof(disk_key
);
2241 memcpy(ptr
, chunk
, item_size
);
2242 item_size
+= sizeof(disk_key
);
2243 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2247 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2248 int num_stripes
, int sub_stripes
)
2250 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2252 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2253 return calc_size
* (num_stripes
/ sub_stripes
);
2255 return calc_size
* num_stripes
;
2258 /* Used to sort the devices by max_avail(descending sort) */
2259 int btrfs_cmp_device_free_bytes(const void *dev_info1
, const void *dev_info2
)
2261 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
2262 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2264 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
2265 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2271 static int __btrfs_calc_nstripes(struct btrfs_fs_devices
*fs_devices
, u64 type
,
2272 int *num_stripes
, int *min_stripes
,
2279 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2280 *num_stripes
= fs_devices
->rw_devices
;
2283 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2287 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2288 if (fs_devices
->rw_devices
< 2)
2293 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2294 *num_stripes
= fs_devices
->rw_devices
;
2295 if (*num_stripes
< 4)
2297 *num_stripes
&= ~(u32
)1;
2305 static u64
__btrfs_calc_stripe_size(struct btrfs_fs_devices
*fs_devices
,
2306 u64 proposed_size
, u64 type
,
2307 int num_stripes
, int small_stripe
)
2309 int min_stripe_size
= 1 * 1024 * 1024;
2310 u64 calc_size
= proposed_size
;
2311 u64 max_chunk_size
= calc_size
;
2314 if (type
& (BTRFS_BLOCK_GROUP_RAID1
|
2315 BTRFS_BLOCK_GROUP_DUP
|
2316 BTRFS_BLOCK_GROUP_RAID10
))
2319 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2320 max_chunk_size
= 10 * calc_size
;
2321 min_stripe_size
= 64 * 1024 * 1024;
2322 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2323 max_chunk_size
= 256 * 1024 * 1024;
2324 min_stripe_size
= 32 * 1024 * 1024;
2325 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2326 calc_size
= 8 * 1024 * 1024;
2327 max_chunk_size
= calc_size
* 2;
2328 min_stripe_size
= 1 * 1024 * 1024;
2331 /* we don't want a chunk larger than 10% of writeable space */
2332 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2335 if (calc_size
* num_stripes
> max_chunk_size
* ncopies
) {
2336 calc_size
= max_chunk_size
* ncopies
;
2337 do_div(calc_size
, num_stripes
);
2338 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2339 calc_size
*= BTRFS_STRIPE_LEN
;
2342 /* we don't want tiny stripes */
2344 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2347 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2348 * we end up with something bigger than a stripe
2350 calc_size
= max_t(u64
, calc_size
, BTRFS_STRIPE_LEN
);
2352 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2353 calc_size
*= BTRFS_STRIPE_LEN
;
2358 static struct map_lookup
*__shrink_map_lookup_stripes(struct map_lookup
*map
,
2361 struct map_lookup
*new;
2362 size_t len
= map_lookup_size(num_stripes
);
2364 BUG_ON(map
->num_stripes
< num_stripes
);
2366 if (map
->num_stripes
== num_stripes
)
2369 new = kmalloc(len
, GFP_NOFS
);
2371 /* just change map->num_stripes */
2372 map
->num_stripes
= num_stripes
;
2376 memcpy(new, map
, len
);
2377 new->num_stripes
= num_stripes
;
2383 * helper to allocate device space from btrfs_device_info, in which we stored
2384 * max free space information of every device. It is used when we can not
2385 * allocate chunks by default size.
2387 * By this helper, we can allocate a new chunk as larger as possible.
2389 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle
*trans
,
2390 struct btrfs_fs_devices
*fs_devices
,
2391 struct btrfs_device_info
*devices
,
2392 int nr_device
, u64 type
,
2393 struct map_lookup
**map_lookup
,
2394 int min_stripes
, u64
*stripe_size
)
2396 int i
, index
, sort_again
= 0;
2397 int min_devices
= min_stripes
;
2398 u64 max_avail
, min_free
;
2399 struct map_lookup
*map
= *map_lookup
;
2402 if (nr_device
< min_stripes
)
2405 btrfs_descending_sort_devices(devices
, nr_device
);
2407 max_avail
= devices
[0].max_avail
;
2411 for (i
= 0; i
< nr_device
; i
++) {
2413 * if dev_offset = 0, it means the free space of this device
2414 * is less than what we need, and we didn't search max avail
2415 * extent on this device, so do it now.
2417 if (!devices
[i
].dev_offset
) {
2418 ret
= find_free_dev_extent(trans
, devices
[i
].dev
,
2420 &devices
[i
].dev_offset
,
2421 &devices
[i
].max_avail
);
2422 if (ret
!= 0 && ret
!= -ENOSPC
)
2428 /* we update the max avail free extent of each devices, sort again */
2430 btrfs_descending_sort_devices(devices
, nr_device
);
2432 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2435 if (!devices
[min_devices
- 1].max_avail
)
2438 max_avail
= devices
[min_devices
- 1].max_avail
;
2439 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2440 do_div(max_avail
, 2);
2442 max_avail
= __btrfs_calc_stripe_size(fs_devices
, max_avail
, type
,
2444 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2445 min_free
= max_avail
* 2;
2447 min_free
= max_avail
;
2449 if (min_free
> devices
[min_devices
- 1].max_avail
)
2452 map
= __shrink_map_lookup_stripes(map
, min_stripes
);
2453 *stripe_size
= max_avail
;
2456 for (i
= 0; i
< min_stripes
; i
++) {
2457 map
->stripes
[i
].dev
= devices
[index
].dev
;
2458 map
->stripes
[i
].physical
= devices
[index
].dev_offset
;
2459 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2461 map
->stripes
[i
].dev
= devices
[index
].dev
;
2462 map
->stripes
[i
].physical
= devices
[index
].dev_offset
+
2472 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2473 struct btrfs_root
*extent_root
,
2474 struct map_lookup
**map_ret
,
2475 u64
*num_bytes
, u64
*stripe_size
,
2476 u64 start
, u64 type
)
2478 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2479 struct btrfs_device
*device
= NULL
;
2480 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2481 struct list_head
*cur
;
2482 struct map_lookup
*map
;
2483 struct extent_map_tree
*em_tree
;
2484 struct extent_map
*em
;
2485 struct btrfs_device_info
*devices_info
;
2486 struct list_head private_devs
;
2487 u64 calc_size
= 1024 * 1024 * 1024;
2494 int min_devices
; /* the min number of devices we need */
2499 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2500 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2502 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2504 if (list_empty(&fs_devices
->alloc_list
))
2507 ret
= __btrfs_calc_nstripes(fs_devices
, type
, &num_stripes
,
2508 &min_stripes
, &sub_stripes
);
2512 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2517 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2522 map
->num_stripes
= num_stripes
;
2524 cur
= fs_devices
->alloc_list
.next
;
2528 calc_size
= __btrfs_calc_stripe_size(fs_devices
, calc_size
, type
,
2531 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2532 min_free
= calc_size
* 2;
2535 min_free
= calc_size
;
2536 min_devices
= min_stripes
;
2539 INIT_LIST_HEAD(&private_devs
);
2540 while (index
< num_stripes
) {
2541 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2542 BUG_ON(!device
->writeable
);
2543 if (device
->total_bytes
> device
->bytes_used
)
2544 avail
= device
->total_bytes
- device
->bytes_used
;
2549 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2550 ret
= find_free_dev_extent(trans
, device
, min_free
,
2551 &devices_info
[i
].dev_offset
,
2552 &devices_info
[i
].max_avail
);
2554 list_move_tail(&device
->dev_alloc_list
,
2556 map
->stripes
[index
].dev
= device
;
2557 map
->stripes
[index
].physical
=
2558 devices_info
[i
].dev_offset
;
2560 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2561 map
->stripes
[index
].dev
= device
;
2562 map
->stripes
[index
].physical
=
2563 devices_info
[i
].dev_offset
+
2567 } else if (ret
!= -ENOSPC
)
2570 devices_info
[i
].dev
= device
;
2572 } else if (device
->in_fs_metadata
&&
2573 avail
>= BTRFS_STRIPE_LEN
) {
2574 devices_info
[i
].dev
= device
;
2575 devices_info
[i
].max_avail
= avail
;
2579 if (cur
== &fs_devices
->alloc_list
)
2583 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2584 if (index
< num_stripes
) {
2585 if (index
>= min_stripes
) {
2586 num_stripes
= index
;
2587 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2588 num_stripes
/= sub_stripes
;
2589 num_stripes
*= sub_stripes
;
2592 map
= __shrink_map_lookup_stripes(map
, num_stripes
);
2593 } else if (i
>= min_devices
) {
2594 ret
= __btrfs_alloc_tiny_space(trans
, fs_devices
,
2595 devices_info
, i
, type
,
2605 map
->sector_size
= extent_root
->sectorsize
;
2606 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2607 map
->io_align
= BTRFS_STRIPE_LEN
;
2608 map
->io_width
= BTRFS_STRIPE_LEN
;
2610 map
->sub_stripes
= sub_stripes
;
2613 *stripe_size
= calc_size
;
2614 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2615 map
->num_stripes
, sub_stripes
);
2617 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, *num_bytes
);
2619 em
= alloc_extent_map(GFP_NOFS
);
2624 em
->bdev
= (struct block_device
*)map
;
2626 em
->len
= *num_bytes
;
2627 em
->block_start
= 0;
2628 em
->block_len
= em
->len
;
2630 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2631 write_lock(&em_tree
->lock
);
2632 ret
= add_extent_mapping(em_tree
, em
);
2633 write_unlock(&em_tree
->lock
);
2635 free_extent_map(em
);
2637 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2638 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2643 while (index
< map
->num_stripes
) {
2644 device
= map
->stripes
[index
].dev
;
2645 dev_offset
= map
->stripes
[index
].physical
;
2647 ret
= btrfs_alloc_dev_extent(trans
, device
,
2648 info
->chunk_root
->root_key
.objectid
,
2649 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2650 start
, dev_offset
, calc_size
);
2655 kfree(devices_info
);
2660 kfree(devices_info
);
2664 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2665 struct btrfs_root
*extent_root
,
2666 struct map_lookup
*map
, u64 chunk_offset
,
2667 u64 chunk_size
, u64 stripe_size
)
2670 struct btrfs_key key
;
2671 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2672 struct btrfs_device
*device
;
2673 struct btrfs_chunk
*chunk
;
2674 struct btrfs_stripe
*stripe
;
2675 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2679 chunk
= kzalloc(item_size
, GFP_NOFS
);
2684 while (index
< map
->num_stripes
) {
2685 device
= map
->stripes
[index
].dev
;
2686 device
->bytes_used
+= stripe_size
;
2687 ret
= btrfs_update_device(trans
, device
);
2693 stripe
= &chunk
->stripe
;
2694 while (index
< map
->num_stripes
) {
2695 device
= map
->stripes
[index
].dev
;
2696 dev_offset
= map
->stripes
[index
].physical
;
2698 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2699 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2700 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2705 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2706 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2707 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2708 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2709 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2710 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2711 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2712 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2713 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2715 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2716 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2717 key
.offset
= chunk_offset
;
2719 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2722 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2723 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2733 * Chunk allocation falls into two parts. The first part does works
2734 * that make the new allocated chunk useable, but not do any operation
2735 * that modifies the chunk tree. The second part does the works that
2736 * require modifying the chunk tree. This division is important for the
2737 * bootstrap process of adding storage to a seed btrfs.
2739 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2740 struct btrfs_root
*extent_root
, u64 type
)
2745 struct map_lookup
*map
;
2746 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2749 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2754 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2755 &stripe_size
, chunk_offset
, type
);
2759 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2760 chunk_size
, stripe_size
);
2765 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2766 struct btrfs_root
*root
,
2767 struct btrfs_device
*device
)
2770 u64 sys_chunk_offset
;
2774 u64 sys_stripe_size
;
2776 struct map_lookup
*map
;
2777 struct map_lookup
*sys_map
;
2778 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2779 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2782 ret
= find_next_chunk(fs_info
->chunk_root
,
2783 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2786 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2787 (fs_info
->metadata_alloc_profile
&
2788 fs_info
->avail_metadata_alloc_bits
);
2789 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2791 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2792 &stripe_size
, chunk_offset
, alloc_profile
);
2795 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2797 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2798 (fs_info
->system_alloc_profile
&
2799 fs_info
->avail_system_alloc_bits
);
2800 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2802 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2803 &sys_chunk_size
, &sys_stripe_size
,
2804 sys_chunk_offset
, alloc_profile
);
2807 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2811 * Modifying chunk tree needs allocating new blocks from both
2812 * system block group and metadata block group. So we only can
2813 * do operations require modifying the chunk tree after both
2814 * block groups were created.
2816 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2817 chunk_size
, stripe_size
);
2820 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2821 sys_chunk_offset
, sys_chunk_size
,
2827 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2829 struct extent_map
*em
;
2830 struct map_lookup
*map
;
2831 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2835 read_lock(&map_tree
->map_tree
.lock
);
2836 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2837 read_unlock(&map_tree
->map_tree
.lock
);
2841 if (btrfs_test_opt(root
, DEGRADED
)) {
2842 free_extent_map(em
);
2846 map
= (struct map_lookup
*)em
->bdev
;
2847 for (i
= 0; i
< map
->num_stripes
; i
++) {
2848 if (!map
->stripes
[i
].dev
->writeable
) {
2853 free_extent_map(em
);
2857 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2859 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2862 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2864 struct extent_map
*em
;
2867 write_lock(&tree
->map_tree
.lock
);
2868 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2870 remove_extent_mapping(&tree
->map_tree
, em
);
2871 write_unlock(&tree
->map_tree
.lock
);
2876 free_extent_map(em
);
2877 /* once for the tree */
2878 free_extent_map(em
);
2882 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2884 struct extent_map
*em
;
2885 struct map_lookup
*map
;
2886 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2889 read_lock(&em_tree
->lock
);
2890 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2891 read_unlock(&em_tree
->lock
);
2894 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2895 map
= (struct map_lookup
*)em
->bdev
;
2896 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2897 ret
= map
->num_stripes
;
2898 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2899 ret
= map
->sub_stripes
;
2902 free_extent_map(em
);
2906 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2910 if (map
->stripes
[optimal
].dev
->bdev
)
2912 for (i
= first
; i
< first
+ num
; i
++) {
2913 if (map
->stripes
[i
].dev
->bdev
)
2916 /* we couldn't find one that doesn't fail. Just return something
2917 * and the io error handling code will clean up eventually
2922 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2923 u64 logical
, u64
*length
,
2924 struct btrfs_multi_bio
**multi_ret
,
2927 struct extent_map
*em
;
2928 struct map_lookup
*map
;
2929 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2932 u64 stripe_end_offset
;
2936 int stripes_allocated
= 8;
2937 int stripes_required
= 1;
2942 struct btrfs_multi_bio
*multi
= NULL
;
2944 if (multi_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2945 stripes_allocated
= 1;
2948 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2953 atomic_set(&multi
->error
, 0);
2956 read_lock(&em_tree
->lock
);
2957 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2958 read_unlock(&em_tree
->lock
);
2961 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2962 (unsigned long long)logical
,
2963 (unsigned long long)*length
);
2967 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2968 map
= (struct map_lookup
*)em
->bdev
;
2969 offset
= logical
- em
->start
;
2971 if (mirror_num
> map
->num_stripes
)
2974 /* if our multi bio struct is too small, back off and try again */
2975 if (rw
& REQ_WRITE
) {
2976 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2977 BTRFS_BLOCK_GROUP_DUP
)) {
2978 stripes_required
= map
->num_stripes
;
2980 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2981 stripes_required
= map
->sub_stripes
;
2985 if (rw
& REQ_DISCARD
) {
2986 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2987 BTRFS_BLOCK_GROUP_RAID1
|
2988 BTRFS_BLOCK_GROUP_DUP
|
2989 BTRFS_BLOCK_GROUP_RAID10
)) {
2990 stripes_required
= map
->num_stripes
;
2993 if (multi_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2994 stripes_allocated
< stripes_required
) {
2995 stripes_allocated
= map
->num_stripes
;
2996 free_extent_map(em
);
3002 * stripe_nr counts the total number of stripes we have to stride
3003 * to get to this block
3005 do_div(stripe_nr
, map
->stripe_len
);
3007 stripe_offset
= stripe_nr
* map
->stripe_len
;
3008 BUG_ON(offset
< stripe_offset
);
3010 /* stripe_offset is the offset of this block in its stripe*/
3011 stripe_offset
= offset
- stripe_offset
;
3013 if (rw
& REQ_DISCARD
)
3014 *length
= min_t(u64
, em
->len
- offset
, *length
);
3015 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
3016 BTRFS_BLOCK_GROUP_RAID1
|
3017 BTRFS_BLOCK_GROUP_RAID10
|
3018 BTRFS_BLOCK_GROUP_DUP
)) {
3019 /* we limit the length of each bio to what fits in a stripe */
3020 *length
= min_t(u64
, em
->len
- offset
,
3021 map
->stripe_len
- stripe_offset
);
3023 *length
= em
->len
- offset
;
3031 stripe_nr_orig
= stripe_nr
;
3032 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3033 (~(map
->stripe_len
- 1));
3034 do_div(stripe_nr_end
, map
->stripe_len
);
3035 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3037 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3038 if (rw
& REQ_DISCARD
)
3039 num_stripes
= min_t(u64
, map
->num_stripes
,
3040 stripe_nr_end
- stripe_nr_orig
);
3041 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3042 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3043 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3044 num_stripes
= map
->num_stripes
;
3045 else if (mirror_num
)
3046 stripe_index
= mirror_num
- 1;
3048 stripe_index
= find_live_mirror(map
, 0,
3050 current
->pid
% map
->num_stripes
);
3053 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3054 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3055 num_stripes
= map
->num_stripes
;
3056 else if (mirror_num
)
3057 stripe_index
= mirror_num
- 1;
3059 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3060 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3062 stripe_index
= do_div(stripe_nr
, factor
);
3063 stripe_index
*= map
->sub_stripes
;
3066 num_stripes
= map
->sub_stripes
;
3067 else if (rw
& REQ_DISCARD
)
3068 num_stripes
= min_t(u64
, map
->sub_stripes
*
3069 (stripe_nr_end
- stripe_nr_orig
),
3071 else if (mirror_num
)
3072 stripe_index
+= mirror_num
- 1;
3074 stripe_index
= find_live_mirror(map
, stripe_index
,
3075 map
->sub_stripes
, stripe_index
+
3076 current
->pid
% map
->sub_stripes
);
3080 * after this do_div call, stripe_nr is the number of stripes
3081 * on this device we have to walk to find the data, and
3082 * stripe_index is the number of our device in the stripe array
3084 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3086 BUG_ON(stripe_index
>= map
->num_stripes
);
3088 if (rw
& REQ_DISCARD
) {
3089 for (i
= 0; i
< num_stripes
; i
++) {
3090 multi
->stripes
[i
].physical
=
3091 map
->stripes
[stripe_index
].physical
+
3092 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3093 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3095 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3097 u32 last_stripe
= 0;
3100 div_u64_rem(stripe_nr_end
- 1,
3104 for (j
= 0; j
< map
->num_stripes
; j
++) {
3107 div_u64_rem(stripe_nr_end
- 1 - j
,
3108 map
->num_stripes
, &test
);
3109 if (test
== stripe_index
)
3112 stripes
= stripe_nr_end
- 1 - j
;
3113 do_div(stripes
, map
->num_stripes
);
3114 multi
->stripes
[i
].length
= map
->stripe_len
*
3115 (stripes
- stripe_nr
+ 1);
3118 multi
->stripes
[i
].length
-=
3122 if (stripe_index
== last_stripe
)
3123 multi
->stripes
[i
].length
-=
3125 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3128 int factor
= map
->num_stripes
/
3130 u32 last_stripe
= 0;
3132 div_u64_rem(stripe_nr_end
- 1,
3133 factor
, &last_stripe
);
3134 last_stripe
*= map
->sub_stripes
;
3136 for (j
= 0; j
< factor
; j
++) {
3139 div_u64_rem(stripe_nr_end
- 1 - j
,
3143 stripe_index
/ map
->sub_stripes
)
3146 stripes
= stripe_nr_end
- 1 - j
;
3147 do_div(stripes
, factor
);
3148 multi
->stripes
[i
].length
= map
->stripe_len
*
3149 (stripes
- stripe_nr
+ 1);
3151 if (i
< map
->sub_stripes
) {
3152 multi
->stripes
[i
].length
-=
3154 if (i
== map
->sub_stripes
- 1)
3157 if (stripe_index
>= last_stripe
&&
3158 stripe_index
<= (last_stripe
+
3159 map
->sub_stripes
- 1)) {
3160 multi
->stripes
[i
].length
-=
3164 multi
->stripes
[i
].length
= *length
;
3167 if (stripe_index
== map
->num_stripes
) {
3168 /* This could only happen for RAID0/10 */
3174 for (i
= 0; i
< num_stripes
; i
++) {
3175 multi
->stripes
[i
].physical
=
3176 map
->stripes
[stripe_index
].physical
+
3178 stripe_nr
* map
->stripe_len
;
3179 multi
->stripes
[i
].dev
=
3180 map
->stripes
[stripe_index
].dev
;
3186 multi
->num_stripes
= num_stripes
;
3187 multi
->max_errors
= max_errors
;
3190 free_extent_map(em
);
3194 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3195 u64 logical
, u64
*length
,
3196 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3198 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3202 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3203 u64 chunk_start
, u64 physical
, u64 devid
,
3204 u64
**logical
, int *naddrs
, int *stripe_len
)
3206 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3207 struct extent_map
*em
;
3208 struct map_lookup
*map
;
3215 read_lock(&em_tree
->lock
);
3216 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3217 read_unlock(&em_tree
->lock
);
3219 BUG_ON(!em
|| em
->start
!= chunk_start
);
3220 map
= (struct map_lookup
*)em
->bdev
;
3223 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3224 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3225 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3226 do_div(length
, map
->num_stripes
);
3228 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3231 for (i
= 0; i
< map
->num_stripes
; i
++) {
3232 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3234 if (map
->stripes
[i
].physical
> physical
||
3235 map
->stripes
[i
].physical
+ length
<= physical
)
3238 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3239 do_div(stripe_nr
, map
->stripe_len
);
3241 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3242 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3243 do_div(stripe_nr
, map
->sub_stripes
);
3244 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3245 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3247 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3248 WARN_ON(nr
>= map
->num_stripes
);
3249 for (j
= 0; j
< nr
; j
++) {
3250 if (buf
[j
] == bytenr
)
3254 WARN_ON(nr
>= map
->num_stripes
);
3261 *stripe_len
= map
->stripe_len
;
3263 free_extent_map(em
);
3267 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3269 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3270 int is_orig_bio
= 0;
3273 atomic_inc(&multi
->error
);
3275 if (bio
== multi
->orig_bio
)
3278 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3281 bio
= multi
->orig_bio
;
3283 bio
->bi_private
= multi
->private;
3284 bio
->bi_end_io
= multi
->end_io
;
3285 /* only send an error to the higher layers if it is
3286 * beyond the tolerance of the multi-bio
3288 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3292 * this bio is actually up to date, we didn't
3293 * go over the max number of errors
3295 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3300 bio_endio(bio
, err
);
3301 } else if (!is_orig_bio
) {
3306 struct async_sched
{
3309 struct btrfs_fs_info
*info
;
3310 struct btrfs_work work
;
3314 * see run_scheduled_bios for a description of why bios are collected for
3317 * This will add one bio to the pending list for a device and make sure
3318 * the work struct is scheduled.
3320 static noinline
int schedule_bio(struct btrfs_root
*root
,
3321 struct btrfs_device
*device
,
3322 int rw
, struct bio
*bio
)
3324 int should_queue
= 1;
3325 struct btrfs_pending_bios
*pending_bios
;
3327 /* don't bother with additional async steps for reads, right now */
3328 if (!(rw
& REQ_WRITE
)) {
3330 submit_bio(rw
, bio
);
3336 * nr_async_bios allows us to reliably return congestion to the
3337 * higher layers. Otherwise, the async bio makes it appear we have
3338 * made progress against dirty pages when we've really just put it
3339 * on a queue for later
3341 atomic_inc(&root
->fs_info
->nr_async_bios
);
3342 WARN_ON(bio
->bi_next
);
3343 bio
->bi_next
= NULL
;
3346 spin_lock(&device
->io_lock
);
3347 if (bio
->bi_rw
& REQ_SYNC
)
3348 pending_bios
= &device
->pending_sync_bios
;
3350 pending_bios
= &device
->pending_bios
;
3352 if (pending_bios
->tail
)
3353 pending_bios
->tail
->bi_next
= bio
;
3355 pending_bios
->tail
= bio
;
3356 if (!pending_bios
->head
)
3357 pending_bios
->head
= bio
;
3358 if (device
->running_pending
)
3361 spin_unlock(&device
->io_lock
);
3364 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3369 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3370 int mirror_num
, int async_submit
)
3372 struct btrfs_mapping_tree
*map_tree
;
3373 struct btrfs_device
*dev
;
3374 struct bio
*first_bio
= bio
;
3375 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3378 struct btrfs_multi_bio
*multi
= NULL
;
3383 length
= bio
->bi_size
;
3384 map_tree
= &root
->fs_info
->mapping_tree
;
3385 map_length
= length
;
3387 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3391 total_devs
= multi
->num_stripes
;
3392 if (map_length
< length
) {
3393 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3394 "len %llu\n", (unsigned long long)logical
,
3395 (unsigned long long)length
,
3396 (unsigned long long)map_length
);
3399 multi
->end_io
= first_bio
->bi_end_io
;
3400 multi
->private = first_bio
->bi_private
;
3401 multi
->orig_bio
= first_bio
;
3402 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3404 while (dev_nr
< total_devs
) {
3405 if (total_devs
> 1) {
3406 if (dev_nr
< total_devs
- 1) {
3407 bio
= bio_clone(first_bio
, GFP_NOFS
);
3412 bio
->bi_private
= multi
;
3413 bio
->bi_end_io
= end_bio_multi_stripe
;
3415 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3416 dev
= multi
->stripes
[dev_nr
].dev
;
3417 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3418 bio
->bi_bdev
= dev
->bdev
;
3420 schedule_bio(root
, dev
, rw
, bio
);
3422 submit_bio(rw
, bio
);
3424 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3425 bio
->bi_sector
= logical
>> 9;
3426 bio_endio(bio
, -EIO
);
3430 if (total_devs
== 1)
3435 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3438 struct btrfs_device
*device
;
3439 struct btrfs_fs_devices
*cur_devices
;
3441 cur_devices
= root
->fs_info
->fs_devices
;
3442 while (cur_devices
) {
3444 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3445 device
= __find_device(&cur_devices
->devices
,
3450 cur_devices
= cur_devices
->seed
;
3455 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3456 u64 devid
, u8
*dev_uuid
)
3458 struct btrfs_device
*device
;
3459 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3461 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3464 list_add(&device
->dev_list
,
3465 &fs_devices
->devices
);
3466 device
->dev_root
= root
->fs_info
->dev_root
;
3467 device
->devid
= devid
;
3468 device
->work
.func
= pending_bios_fn
;
3469 device
->fs_devices
= fs_devices
;
3470 device
->missing
= 1;
3471 fs_devices
->num_devices
++;
3472 fs_devices
->missing_devices
++;
3473 spin_lock_init(&device
->io_lock
);
3474 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3475 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3479 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3480 struct extent_buffer
*leaf
,
3481 struct btrfs_chunk
*chunk
)
3483 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3484 struct map_lookup
*map
;
3485 struct extent_map
*em
;
3489 u8 uuid
[BTRFS_UUID_SIZE
];
3494 logical
= key
->offset
;
3495 length
= btrfs_chunk_length(leaf
, chunk
);
3497 read_lock(&map_tree
->map_tree
.lock
);
3498 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3499 read_unlock(&map_tree
->map_tree
.lock
);
3501 /* already mapped? */
3502 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3503 free_extent_map(em
);
3506 free_extent_map(em
);
3509 em
= alloc_extent_map(GFP_NOFS
);
3512 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3513 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3515 free_extent_map(em
);
3519 em
->bdev
= (struct block_device
*)map
;
3520 em
->start
= logical
;
3522 em
->block_start
= 0;
3523 em
->block_len
= em
->len
;
3525 map
->num_stripes
= num_stripes
;
3526 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3527 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3528 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3529 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3530 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3531 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3532 for (i
= 0; i
< num_stripes
; i
++) {
3533 map
->stripes
[i
].physical
=
3534 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3535 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3536 read_extent_buffer(leaf
, uuid
, (unsigned long)
3537 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3539 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3541 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3543 free_extent_map(em
);
3546 if (!map
->stripes
[i
].dev
) {
3547 map
->stripes
[i
].dev
=
3548 add_missing_dev(root
, devid
, uuid
);
3549 if (!map
->stripes
[i
].dev
) {
3551 free_extent_map(em
);
3555 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3558 write_lock(&map_tree
->map_tree
.lock
);
3559 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3560 write_unlock(&map_tree
->map_tree
.lock
);
3562 free_extent_map(em
);
3567 static int fill_device_from_item(struct extent_buffer
*leaf
,
3568 struct btrfs_dev_item
*dev_item
,
3569 struct btrfs_device
*device
)
3573 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3574 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3575 device
->total_bytes
= device
->disk_total_bytes
;
3576 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3577 device
->type
= btrfs_device_type(leaf
, dev_item
);
3578 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3579 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3580 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3582 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3583 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3588 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3590 struct btrfs_fs_devices
*fs_devices
;
3593 mutex_lock(&uuid_mutex
);
3595 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3596 while (fs_devices
) {
3597 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3601 fs_devices
= fs_devices
->seed
;
3604 fs_devices
= find_fsid(fsid
);
3610 fs_devices
= clone_fs_devices(fs_devices
);
3611 if (IS_ERR(fs_devices
)) {
3612 ret
= PTR_ERR(fs_devices
);
3616 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3617 root
->fs_info
->bdev_holder
);
3621 if (!fs_devices
->seeding
) {
3622 __btrfs_close_devices(fs_devices
);
3623 free_fs_devices(fs_devices
);
3628 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3629 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3631 mutex_unlock(&uuid_mutex
);
3635 static int read_one_dev(struct btrfs_root
*root
,
3636 struct extent_buffer
*leaf
,
3637 struct btrfs_dev_item
*dev_item
)
3639 struct btrfs_device
*device
;
3642 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3643 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3645 devid
= btrfs_device_id(leaf
, dev_item
);
3646 read_extent_buffer(leaf
, dev_uuid
,
3647 (unsigned long)btrfs_device_uuid(dev_item
),
3649 read_extent_buffer(leaf
, fs_uuid
,
3650 (unsigned long)btrfs_device_fsid(dev_item
),
3653 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3654 ret
= open_seed_devices(root
, fs_uuid
);
3655 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3659 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3660 if (!device
|| !device
->bdev
) {
3661 if (!btrfs_test_opt(root
, DEGRADED
))
3665 printk(KERN_WARNING
"warning devid %llu missing\n",
3666 (unsigned long long)devid
);
3667 device
= add_missing_dev(root
, devid
, dev_uuid
);
3670 } else if (!device
->missing
) {
3672 * this happens when a device that was properly setup
3673 * in the device info lists suddenly goes bad.
3674 * device->bdev is NULL, and so we have to set
3675 * device->missing to one here
3677 root
->fs_info
->fs_devices
->missing_devices
++;
3678 device
->missing
= 1;
3682 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3683 BUG_ON(device
->writeable
);
3684 if (device
->generation
!=
3685 btrfs_device_generation(leaf
, dev_item
))
3689 fill_device_from_item(leaf
, dev_item
, device
);
3690 device
->dev_root
= root
->fs_info
->dev_root
;
3691 device
->in_fs_metadata
= 1;
3692 if (device
->writeable
)
3693 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3698 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3700 struct btrfs_dev_item
*dev_item
;
3702 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3704 return read_one_dev(root
, buf
, dev_item
);
3707 int btrfs_read_sys_array(struct btrfs_root
*root
)
3709 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3710 struct extent_buffer
*sb
;
3711 struct btrfs_disk_key
*disk_key
;
3712 struct btrfs_chunk
*chunk
;
3714 unsigned long sb_ptr
;
3720 struct btrfs_key key
;
3722 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3723 BTRFS_SUPER_INFO_SIZE
);
3726 btrfs_set_buffer_uptodate(sb
);
3727 btrfs_set_buffer_lockdep_class(sb
, 0);
3729 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3730 array_size
= btrfs_super_sys_array_size(super_copy
);
3732 ptr
= super_copy
->sys_chunk_array
;
3733 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3736 while (cur
< array_size
) {
3737 disk_key
= (struct btrfs_disk_key
*)ptr
;
3738 btrfs_disk_key_to_cpu(&key
, disk_key
);
3740 len
= sizeof(*disk_key
); ptr
+= len
;
3744 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3745 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3746 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3749 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3750 len
= btrfs_chunk_item_size(num_stripes
);
3759 free_extent_buffer(sb
);
3763 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3765 struct btrfs_path
*path
;
3766 struct extent_buffer
*leaf
;
3767 struct btrfs_key key
;
3768 struct btrfs_key found_key
;
3772 root
= root
->fs_info
->chunk_root
;
3774 path
= btrfs_alloc_path();
3778 /* first we search for all of the device items, and then we
3779 * read in all of the chunk items. This way we can create chunk
3780 * mappings that reference all of the devices that are afound
3782 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3786 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3790 leaf
= path
->nodes
[0];
3791 slot
= path
->slots
[0];
3792 if (slot
>= btrfs_header_nritems(leaf
)) {
3793 ret
= btrfs_next_leaf(root
, path
);
3800 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3801 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3802 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3804 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3805 struct btrfs_dev_item
*dev_item
;
3806 dev_item
= btrfs_item_ptr(leaf
, slot
,
3807 struct btrfs_dev_item
);
3808 ret
= read_one_dev(root
, leaf
, dev_item
);
3812 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3813 struct btrfs_chunk
*chunk
;
3814 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3815 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3821 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3823 btrfs_release_path(root
, path
);
3828 btrfs_free_path(path
);