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 num_sync_run
;
166 unsigned long batch_run
= 0;
168 unsigned long last_waited
= 0;
171 bdi
= blk_get_backing_dev_info(device
->bdev
);
172 fs_info
= device
->dev_root
->fs_info
;
173 limit
= btrfs_async_submit_limit(fs_info
);
174 limit
= limit
* 2 / 3;
176 /* we want to make sure that every time we switch from the sync
177 * list to the normal list, we unplug
182 spin_lock(&device
->io_lock
);
187 /* take all the bios off the list at once and process them
188 * later on (without the lock held). But, remember the
189 * tail and other pointers so the bios can be properly reinserted
190 * into the list if we hit congestion
192 if (!force_reg
&& device
->pending_sync_bios
.head
) {
193 pending_bios
= &device
->pending_sync_bios
;
196 pending_bios
= &device
->pending_bios
;
200 pending
= pending_bios
->head
;
201 tail
= pending_bios
->tail
;
202 WARN_ON(pending
&& !tail
);
205 * if pending was null this time around, no bios need processing
206 * at all and we can stop. Otherwise it'll loop back up again
207 * and do an additional check so no bios are missed.
209 * device->running_pending is used to synchronize with the
212 if (device
->pending_sync_bios
.head
== NULL
&&
213 device
->pending_bios
.head
== NULL
) {
215 device
->running_pending
= 0;
218 device
->running_pending
= 1;
221 pending_bios
->head
= NULL
;
222 pending_bios
->tail
= NULL
;
224 spin_unlock(&device
->io_lock
);
227 * if we're doing the regular priority list, make sure we unplug
228 * for any high prio bios we've sent down
230 if (pending_bios
== &device
->pending_bios
&& num_sync_run
> 0) {
232 blk_run_backing_dev(bdi
, NULL
);
238 /* we want to work on both lists, but do more bios on the
239 * sync list than the regular list
242 pending_bios
!= &device
->pending_sync_bios
&&
243 device
->pending_sync_bios
.head
) ||
244 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
245 device
->pending_bios
.head
)) {
246 spin_lock(&device
->io_lock
);
247 requeue_list(pending_bios
, pending
, tail
);
252 pending
= pending
->bi_next
;
254 atomic_dec(&fs_info
->nr_async_bios
);
256 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
257 waitqueue_active(&fs_info
->async_submit_wait
))
258 wake_up(&fs_info
->async_submit_wait
);
260 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
262 if (cur
->bi_rw
& REQ_SYNC
)
265 submit_bio(cur
->bi_rw
, cur
);
268 if (need_resched()) {
270 blk_run_backing_dev(bdi
, NULL
);
277 * we made progress, there is more work to do and the bdi
278 * is now congested. Back off and let other work structs
281 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
282 fs_info
->fs_devices
->open_devices
> 1) {
283 struct io_context
*ioc
;
285 ioc
= current
->io_context
;
288 * the main goal here is that we don't want to
289 * block if we're going to be able to submit
290 * more requests without blocking.
292 * This code does two great things, it pokes into
293 * the elevator code from a filesystem _and_
294 * it makes assumptions about how batching works.
296 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
297 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
299 ioc
->last_waited
== last_waited
)) {
301 * we want to go through our batch of
302 * requests and stop. So, we copy out
303 * the ioc->last_waited time and test
304 * against it before looping
306 last_waited
= ioc
->last_waited
;
307 if (need_resched()) {
309 blk_run_backing_dev(bdi
, NULL
);
316 spin_lock(&device
->io_lock
);
317 requeue_list(pending_bios
, pending
, tail
);
318 device
->running_pending
= 1;
320 spin_unlock(&device
->io_lock
);
321 btrfs_requeue_work(&device
->work
);
328 blk_run_backing_dev(bdi
, NULL
);
331 * IO has already been through a long path to get here. Checksumming,
332 * async helper threads, perhaps compression. We've done a pretty
333 * good job of collecting a batch of IO and should just unplug
334 * the device right away.
336 * This will help anyone who is waiting on the IO, they might have
337 * already unplugged, but managed to do so before the bio they
338 * cared about found its way down here.
340 blk_run_backing_dev(bdi
, NULL
);
346 spin_lock(&device
->io_lock
);
347 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
349 spin_unlock(&device
->io_lock
);
355 static void pending_bios_fn(struct btrfs_work
*work
)
357 struct btrfs_device
*device
;
359 device
= container_of(work
, struct btrfs_device
, work
);
360 run_scheduled_bios(device
);
363 static noinline
int device_list_add(const char *path
,
364 struct btrfs_super_block
*disk_super
,
365 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
367 struct btrfs_device
*device
;
368 struct btrfs_fs_devices
*fs_devices
;
369 u64 found_transid
= btrfs_super_generation(disk_super
);
372 fs_devices
= find_fsid(disk_super
->fsid
);
374 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
377 INIT_LIST_HEAD(&fs_devices
->devices
);
378 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
379 list_add(&fs_devices
->list
, &fs_uuids
);
380 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
381 fs_devices
->latest_devid
= devid
;
382 fs_devices
->latest_trans
= found_transid
;
383 mutex_init(&fs_devices
->device_list_mutex
);
386 device
= __find_device(&fs_devices
->devices
, devid
,
387 disk_super
->dev_item
.uuid
);
390 if (fs_devices
->opened
)
393 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
395 /* we can safely leave the fs_devices entry around */
398 device
->devid
= devid
;
399 device
->work
.func
= pending_bios_fn
;
400 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
402 spin_lock_init(&device
->io_lock
);
403 device
->name
= kstrdup(path
, GFP_NOFS
);
408 INIT_LIST_HEAD(&device
->dev_alloc_list
);
410 mutex_lock(&fs_devices
->device_list_mutex
);
411 list_add(&device
->dev_list
, &fs_devices
->devices
);
412 mutex_unlock(&fs_devices
->device_list_mutex
);
414 device
->fs_devices
= fs_devices
;
415 fs_devices
->num_devices
++;
416 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
417 name
= kstrdup(path
, GFP_NOFS
);
422 if (device
->missing
) {
423 fs_devices
->missing_devices
--;
428 if (found_transid
> fs_devices
->latest_trans
) {
429 fs_devices
->latest_devid
= devid
;
430 fs_devices
->latest_trans
= found_transid
;
432 *fs_devices_ret
= fs_devices
;
436 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
438 struct btrfs_fs_devices
*fs_devices
;
439 struct btrfs_device
*device
;
440 struct btrfs_device
*orig_dev
;
442 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
444 return ERR_PTR(-ENOMEM
);
446 INIT_LIST_HEAD(&fs_devices
->devices
);
447 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
448 INIT_LIST_HEAD(&fs_devices
->list
);
449 mutex_init(&fs_devices
->device_list_mutex
);
450 fs_devices
->latest_devid
= orig
->latest_devid
;
451 fs_devices
->latest_trans
= orig
->latest_trans
;
452 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
454 mutex_lock(&orig
->device_list_mutex
);
455 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
456 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
460 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
466 device
->devid
= orig_dev
->devid
;
467 device
->work
.func
= pending_bios_fn
;
468 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
469 spin_lock_init(&device
->io_lock
);
470 INIT_LIST_HEAD(&device
->dev_list
);
471 INIT_LIST_HEAD(&device
->dev_alloc_list
);
473 list_add(&device
->dev_list
, &fs_devices
->devices
);
474 device
->fs_devices
= fs_devices
;
475 fs_devices
->num_devices
++;
477 mutex_unlock(&orig
->device_list_mutex
);
480 mutex_unlock(&orig
->device_list_mutex
);
481 free_fs_devices(fs_devices
);
482 return ERR_PTR(-ENOMEM
);
485 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
487 struct btrfs_device
*device
, *next
;
489 mutex_lock(&uuid_mutex
);
491 mutex_lock(&fs_devices
->device_list_mutex
);
492 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
493 if (device
->in_fs_metadata
)
497 blkdev_put(device
->bdev
, device
->mode
);
499 fs_devices
->open_devices
--;
501 if (device
->writeable
) {
502 list_del_init(&device
->dev_alloc_list
);
503 device
->writeable
= 0;
504 fs_devices
->rw_devices
--;
506 list_del_init(&device
->dev_list
);
507 fs_devices
->num_devices
--;
511 mutex_unlock(&fs_devices
->device_list_mutex
);
513 if (fs_devices
->seed
) {
514 fs_devices
= fs_devices
->seed
;
518 mutex_unlock(&uuid_mutex
);
522 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
524 struct btrfs_device
*device
;
526 if (--fs_devices
->opened
> 0)
529 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
531 blkdev_put(device
->bdev
, device
->mode
);
532 fs_devices
->open_devices
--;
534 if (device
->writeable
) {
535 list_del_init(&device
->dev_alloc_list
);
536 fs_devices
->rw_devices
--;
540 device
->writeable
= 0;
541 device
->in_fs_metadata
= 0;
543 WARN_ON(fs_devices
->open_devices
);
544 WARN_ON(fs_devices
->rw_devices
);
545 fs_devices
->opened
= 0;
546 fs_devices
->seeding
= 0;
551 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
553 struct btrfs_fs_devices
*seed_devices
= NULL
;
556 mutex_lock(&uuid_mutex
);
557 ret
= __btrfs_close_devices(fs_devices
);
558 if (!fs_devices
->opened
) {
559 seed_devices
= fs_devices
->seed
;
560 fs_devices
->seed
= NULL
;
562 mutex_unlock(&uuid_mutex
);
564 while (seed_devices
) {
565 fs_devices
= seed_devices
;
566 seed_devices
= fs_devices
->seed
;
567 __btrfs_close_devices(fs_devices
);
568 free_fs_devices(fs_devices
);
573 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
574 fmode_t flags
, void *holder
)
576 struct block_device
*bdev
;
577 struct list_head
*head
= &fs_devices
->devices
;
578 struct btrfs_device
*device
;
579 struct block_device
*latest_bdev
= NULL
;
580 struct buffer_head
*bh
;
581 struct btrfs_super_block
*disk_super
;
582 u64 latest_devid
= 0;
583 u64 latest_transid
= 0;
590 list_for_each_entry(device
, head
, dev_list
) {
596 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
598 printk(KERN_INFO
"open %s failed\n", device
->name
);
601 set_blocksize(bdev
, 4096);
603 bh
= btrfs_read_dev_super(bdev
);
609 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
610 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
611 if (devid
!= device
->devid
)
614 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
618 device
->generation
= btrfs_super_generation(disk_super
);
619 if (!latest_transid
|| device
->generation
> latest_transid
) {
620 latest_devid
= devid
;
621 latest_transid
= device
->generation
;
625 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
626 device
->writeable
= 0;
628 device
->writeable
= !bdev_read_only(bdev
);
633 device
->in_fs_metadata
= 0;
634 device
->mode
= flags
;
636 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
637 fs_devices
->rotating
= 1;
639 fs_devices
->open_devices
++;
640 if (device
->writeable
) {
641 fs_devices
->rw_devices
++;
642 list_add(&device
->dev_alloc_list
,
643 &fs_devices
->alloc_list
);
650 blkdev_put(bdev
, flags
);
654 if (fs_devices
->open_devices
== 0) {
658 fs_devices
->seeding
= seeding
;
659 fs_devices
->opened
= 1;
660 fs_devices
->latest_bdev
= latest_bdev
;
661 fs_devices
->latest_devid
= latest_devid
;
662 fs_devices
->latest_trans
= latest_transid
;
663 fs_devices
->total_rw_bytes
= 0;
668 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
669 fmode_t flags
, void *holder
)
673 mutex_lock(&uuid_mutex
);
674 if (fs_devices
->opened
) {
675 fs_devices
->opened
++;
678 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
680 mutex_unlock(&uuid_mutex
);
684 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
685 struct btrfs_fs_devices
**fs_devices_ret
)
687 struct btrfs_super_block
*disk_super
;
688 struct block_device
*bdev
;
689 struct buffer_head
*bh
;
694 mutex_lock(&uuid_mutex
);
697 bdev
= blkdev_get_by_path(path
, flags
, holder
);
704 ret
= set_blocksize(bdev
, 4096);
707 bh
= btrfs_read_dev_super(bdev
);
712 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
713 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
714 transid
= btrfs_super_generation(disk_super
);
715 if (disk_super
->label
[0])
716 printk(KERN_INFO
"device label %s ", disk_super
->label
);
718 /* FIXME, make a readl uuid parser */
719 printk(KERN_INFO
"device fsid %llx-%llx ",
720 *(unsigned long long *)disk_super
->fsid
,
721 *(unsigned long long *)(disk_super
->fsid
+ 8));
723 printk(KERN_CONT
"devid %llu transid %llu %s\n",
724 (unsigned long long)devid
, (unsigned long long)transid
, path
);
725 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
729 blkdev_put(bdev
, flags
);
731 mutex_unlock(&uuid_mutex
);
735 /* helper to account the used device space in the range */
736 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
737 u64 end
, u64
*length
)
739 struct btrfs_key key
;
740 struct btrfs_root
*root
= device
->dev_root
;
741 struct btrfs_dev_extent
*dev_extent
;
742 struct btrfs_path
*path
;
746 struct extent_buffer
*l
;
750 if (start
>= device
->total_bytes
)
753 path
= btrfs_alloc_path();
758 key
.objectid
= device
->devid
;
760 key
.type
= BTRFS_DEV_EXTENT_KEY
;
762 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
766 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
773 slot
= path
->slots
[0];
774 if (slot
>= btrfs_header_nritems(l
)) {
775 ret
= btrfs_next_leaf(root
, path
);
783 btrfs_item_key_to_cpu(l
, &key
, slot
);
785 if (key
.objectid
< device
->devid
)
788 if (key
.objectid
> device
->devid
)
791 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
794 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
795 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
797 if (key
.offset
<= start
&& extent_end
> end
) {
798 *length
= end
- start
+ 1;
800 } else if (key
.offset
<= start
&& extent_end
> start
)
801 *length
+= extent_end
- start
;
802 else if (key
.offset
> start
&& extent_end
<= end
)
803 *length
+= extent_end
- key
.offset
;
804 else if (key
.offset
> start
&& key
.offset
<= end
) {
805 *length
+= end
- key
.offset
+ 1;
807 } else if (key
.offset
> end
)
815 btrfs_free_path(path
);
820 * find_free_dev_extent - find free space in the specified device
821 * @trans: transaction handler
822 * @device: the device which we search the free space in
823 * @num_bytes: the size of the free space that we need
824 * @start: store the start of the free space.
825 * @len: the size of the free space. that we find, or the size of the max
826 * free space if we don't find suitable free space
828 * this uses a pretty simple search, the expectation is that it is
829 * called very infrequently and that a given device has a small number
832 * @start is used to store the start of the free space if we find. But if we
833 * don't find suitable free space, it will be used to store the start position
834 * of the max free space.
836 * @len is used to store the size of the free space that we find.
837 * But if we don't find suitable free space, it is used to store the size of
838 * the max free space.
840 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
841 struct btrfs_device
*device
, u64 num_bytes
,
842 u64
*start
, u64
*len
)
844 struct btrfs_key key
;
845 struct btrfs_root
*root
= device
->dev_root
;
846 struct btrfs_dev_extent
*dev_extent
;
847 struct btrfs_path
*path
;
853 u64 search_end
= device
->total_bytes
;
856 struct extent_buffer
*l
;
858 /* FIXME use last free of some kind */
860 /* we don't want to overwrite the superblock on the drive,
861 * so we make sure to start at an offset of at least 1MB
863 search_start
= 1024 * 1024;
865 if (root
->fs_info
->alloc_start
+ num_bytes
<= search_end
)
866 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
868 max_hole_start
= search_start
;
871 if (search_start
>= search_end
) {
876 path
= btrfs_alloc_path();
883 key
.objectid
= device
->devid
;
884 key
.offset
= search_start
;
885 key
.type
= BTRFS_DEV_EXTENT_KEY
;
887 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
891 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
898 slot
= path
->slots
[0];
899 if (slot
>= btrfs_header_nritems(l
)) {
900 ret
= btrfs_next_leaf(root
, path
);
908 btrfs_item_key_to_cpu(l
, &key
, slot
);
910 if (key
.objectid
< device
->devid
)
913 if (key
.objectid
> device
->devid
)
916 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
919 if (key
.offset
> search_start
) {
920 hole_size
= key
.offset
- search_start
;
922 if (hole_size
> max_hole_size
) {
923 max_hole_start
= search_start
;
924 max_hole_size
= hole_size
;
928 * If this free space is greater than which we need,
929 * it must be the max free space that we have found
930 * until now, so max_hole_start must point to the start
931 * of this free space and the length of this free space
932 * is stored in max_hole_size. Thus, we return
933 * max_hole_start and max_hole_size and go back to the
936 if (hole_size
>= num_bytes
) {
942 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
943 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
945 if (extent_end
> search_start
)
946 search_start
= extent_end
;
952 hole_size
= search_end
- search_start
;
953 if (hole_size
> max_hole_size
) {
954 max_hole_start
= search_start
;
955 max_hole_size
= hole_size
;
959 if (hole_size
< num_bytes
)
965 btrfs_free_path(path
);
967 *start
= max_hole_start
;
969 *len
= max_hole_size
;
973 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
974 struct btrfs_device
*device
,
978 struct btrfs_path
*path
;
979 struct btrfs_root
*root
= device
->dev_root
;
980 struct btrfs_key key
;
981 struct btrfs_key found_key
;
982 struct extent_buffer
*leaf
= NULL
;
983 struct btrfs_dev_extent
*extent
= NULL
;
985 path
= btrfs_alloc_path();
989 key
.objectid
= device
->devid
;
991 key
.type
= BTRFS_DEV_EXTENT_KEY
;
993 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
995 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
996 BTRFS_DEV_EXTENT_KEY
);
998 leaf
= path
->nodes
[0];
999 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1000 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1001 struct btrfs_dev_extent
);
1002 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1003 btrfs_dev_extent_length(leaf
, extent
) < start
);
1005 } else if (ret
== 0) {
1006 leaf
= path
->nodes
[0];
1007 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1008 struct btrfs_dev_extent
);
1012 if (device
->bytes_used
> 0)
1013 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
1014 ret
= btrfs_del_item(trans
, root
, path
);
1017 btrfs_free_path(path
);
1021 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1022 struct btrfs_device
*device
,
1023 u64 chunk_tree
, u64 chunk_objectid
,
1024 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1027 struct btrfs_path
*path
;
1028 struct btrfs_root
*root
= device
->dev_root
;
1029 struct btrfs_dev_extent
*extent
;
1030 struct extent_buffer
*leaf
;
1031 struct btrfs_key key
;
1033 WARN_ON(!device
->in_fs_metadata
);
1034 path
= btrfs_alloc_path();
1038 key
.objectid
= device
->devid
;
1040 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1041 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1045 leaf
= path
->nodes
[0];
1046 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1047 struct btrfs_dev_extent
);
1048 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1049 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1050 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1052 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1053 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1056 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1057 btrfs_mark_buffer_dirty(leaf
);
1058 btrfs_free_path(path
);
1062 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1063 u64 objectid
, u64
*offset
)
1065 struct btrfs_path
*path
;
1067 struct btrfs_key key
;
1068 struct btrfs_chunk
*chunk
;
1069 struct btrfs_key found_key
;
1071 path
= btrfs_alloc_path();
1074 key
.objectid
= objectid
;
1075 key
.offset
= (u64
)-1;
1076 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1078 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1084 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1088 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1090 if (found_key
.objectid
!= objectid
)
1093 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1094 struct btrfs_chunk
);
1095 *offset
= found_key
.offset
+
1096 btrfs_chunk_length(path
->nodes
[0], chunk
);
1101 btrfs_free_path(path
);
1105 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1108 struct btrfs_key key
;
1109 struct btrfs_key found_key
;
1110 struct btrfs_path
*path
;
1112 root
= root
->fs_info
->chunk_root
;
1114 path
= btrfs_alloc_path();
1118 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1119 key
.type
= BTRFS_DEV_ITEM_KEY
;
1120 key
.offset
= (u64
)-1;
1122 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1128 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1129 BTRFS_DEV_ITEM_KEY
);
1133 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1135 *objectid
= found_key
.offset
+ 1;
1139 btrfs_free_path(path
);
1144 * the device information is stored in the chunk root
1145 * the btrfs_device struct should be fully filled in
1147 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1148 struct btrfs_root
*root
,
1149 struct btrfs_device
*device
)
1152 struct btrfs_path
*path
;
1153 struct btrfs_dev_item
*dev_item
;
1154 struct extent_buffer
*leaf
;
1155 struct btrfs_key key
;
1158 root
= root
->fs_info
->chunk_root
;
1160 path
= btrfs_alloc_path();
1164 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1165 key
.type
= BTRFS_DEV_ITEM_KEY
;
1166 key
.offset
= device
->devid
;
1168 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1173 leaf
= path
->nodes
[0];
1174 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1176 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1177 btrfs_set_device_generation(leaf
, dev_item
, 0);
1178 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1179 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1180 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1181 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1182 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1183 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1184 btrfs_set_device_group(leaf
, dev_item
, 0);
1185 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1186 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1187 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1189 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1190 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1191 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1192 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1193 btrfs_mark_buffer_dirty(leaf
);
1197 btrfs_free_path(path
);
1201 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1202 struct btrfs_device
*device
)
1205 struct btrfs_path
*path
;
1206 struct btrfs_key key
;
1207 struct btrfs_trans_handle
*trans
;
1209 root
= root
->fs_info
->chunk_root
;
1211 path
= btrfs_alloc_path();
1215 trans
= btrfs_start_transaction(root
, 0);
1216 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1217 key
.type
= BTRFS_DEV_ITEM_KEY
;
1218 key
.offset
= device
->devid
;
1221 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1230 ret
= btrfs_del_item(trans
, root
, path
);
1234 btrfs_free_path(path
);
1235 unlock_chunks(root
);
1236 btrfs_commit_transaction(trans
, root
);
1240 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1242 struct btrfs_device
*device
;
1243 struct btrfs_device
*next_device
;
1244 struct block_device
*bdev
;
1245 struct buffer_head
*bh
= NULL
;
1246 struct btrfs_super_block
*disk_super
;
1253 mutex_lock(&uuid_mutex
);
1254 mutex_lock(&root
->fs_info
->volume_mutex
);
1256 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1257 root
->fs_info
->avail_system_alloc_bits
|
1258 root
->fs_info
->avail_metadata_alloc_bits
;
1260 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1261 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1262 printk(KERN_ERR
"btrfs: unable to go below four devices "
1268 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1269 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1270 printk(KERN_ERR
"btrfs: unable to go below two "
1271 "devices on raid1\n");
1276 if (strcmp(device_path
, "missing") == 0) {
1277 struct list_head
*devices
;
1278 struct btrfs_device
*tmp
;
1281 devices
= &root
->fs_info
->fs_devices
->devices
;
1282 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1283 list_for_each_entry(tmp
, devices
, dev_list
) {
1284 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1289 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1294 printk(KERN_ERR
"btrfs: no missing devices found to "
1299 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1300 root
->fs_info
->bdev_holder
);
1302 ret
= PTR_ERR(bdev
);
1306 set_blocksize(bdev
, 4096);
1307 bh
= btrfs_read_dev_super(bdev
);
1312 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1313 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1314 dev_uuid
= disk_super
->dev_item
.uuid
;
1315 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1323 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1324 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1330 if (device
->writeable
) {
1331 list_del_init(&device
->dev_alloc_list
);
1332 root
->fs_info
->fs_devices
->rw_devices
--;
1335 ret
= btrfs_shrink_device(device
, 0);
1339 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1343 device
->in_fs_metadata
= 0;
1346 * the device list mutex makes sure that we don't change
1347 * the device list while someone else is writing out all
1348 * the device supers.
1350 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1351 list_del_init(&device
->dev_list
);
1352 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1354 device
->fs_devices
->num_devices
--;
1356 if (device
->missing
)
1357 root
->fs_info
->fs_devices
->missing_devices
--;
1359 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1360 struct btrfs_device
, dev_list
);
1361 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1362 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1363 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1364 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1367 blkdev_put(device
->bdev
, device
->mode
);
1368 device
->bdev
= NULL
;
1369 device
->fs_devices
->open_devices
--;
1372 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1373 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1375 if (device
->fs_devices
->open_devices
== 0) {
1376 struct btrfs_fs_devices
*fs_devices
;
1377 fs_devices
= root
->fs_info
->fs_devices
;
1378 while (fs_devices
) {
1379 if (fs_devices
->seed
== device
->fs_devices
)
1381 fs_devices
= fs_devices
->seed
;
1383 fs_devices
->seed
= device
->fs_devices
->seed
;
1384 device
->fs_devices
->seed
= NULL
;
1385 __btrfs_close_devices(device
->fs_devices
);
1386 free_fs_devices(device
->fs_devices
);
1390 * at this point, the device is zero sized. We want to
1391 * remove it from the devices list and zero out the old super
1393 if (device
->writeable
) {
1394 /* make sure this device isn't detected as part of
1397 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1398 set_buffer_dirty(bh
);
1399 sync_dirty_buffer(bh
);
1402 kfree(device
->name
);
1410 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1412 mutex_unlock(&root
->fs_info
->volume_mutex
);
1413 mutex_unlock(&uuid_mutex
);
1418 * does all the dirty work required for changing file system's UUID.
1420 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1421 struct btrfs_root
*root
)
1423 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1424 struct btrfs_fs_devices
*old_devices
;
1425 struct btrfs_fs_devices
*seed_devices
;
1426 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1427 struct btrfs_device
*device
;
1430 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1431 if (!fs_devices
->seeding
)
1434 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1438 old_devices
= clone_fs_devices(fs_devices
);
1439 if (IS_ERR(old_devices
)) {
1440 kfree(seed_devices
);
1441 return PTR_ERR(old_devices
);
1444 list_add(&old_devices
->list
, &fs_uuids
);
1446 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1447 seed_devices
->opened
= 1;
1448 INIT_LIST_HEAD(&seed_devices
->devices
);
1449 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1450 mutex_init(&seed_devices
->device_list_mutex
);
1451 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1452 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1453 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1454 device
->fs_devices
= seed_devices
;
1457 fs_devices
->seeding
= 0;
1458 fs_devices
->num_devices
= 0;
1459 fs_devices
->open_devices
= 0;
1460 fs_devices
->seed
= seed_devices
;
1462 generate_random_uuid(fs_devices
->fsid
);
1463 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1464 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1465 super_flags
= btrfs_super_flags(disk_super
) &
1466 ~BTRFS_SUPER_FLAG_SEEDING
;
1467 btrfs_set_super_flags(disk_super
, super_flags
);
1473 * strore the expected generation for seed devices in device items.
1475 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1476 struct btrfs_root
*root
)
1478 struct btrfs_path
*path
;
1479 struct extent_buffer
*leaf
;
1480 struct btrfs_dev_item
*dev_item
;
1481 struct btrfs_device
*device
;
1482 struct btrfs_key key
;
1483 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1484 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1488 path
= btrfs_alloc_path();
1492 root
= root
->fs_info
->chunk_root
;
1493 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1495 key
.type
= BTRFS_DEV_ITEM_KEY
;
1498 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1502 leaf
= path
->nodes
[0];
1504 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1505 ret
= btrfs_next_leaf(root
, path
);
1510 leaf
= path
->nodes
[0];
1511 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1512 btrfs_release_path(root
, path
);
1516 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1517 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1518 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1521 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1522 struct btrfs_dev_item
);
1523 devid
= btrfs_device_id(leaf
, dev_item
);
1524 read_extent_buffer(leaf
, dev_uuid
,
1525 (unsigned long)btrfs_device_uuid(dev_item
),
1527 read_extent_buffer(leaf
, fs_uuid
,
1528 (unsigned long)btrfs_device_fsid(dev_item
),
1530 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1533 if (device
->fs_devices
->seeding
) {
1534 btrfs_set_device_generation(leaf
, dev_item
,
1535 device
->generation
);
1536 btrfs_mark_buffer_dirty(leaf
);
1544 btrfs_free_path(path
);
1548 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1550 struct btrfs_trans_handle
*trans
;
1551 struct btrfs_device
*device
;
1552 struct block_device
*bdev
;
1553 struct list_head
*devices
;
1554 struct super_block
*sb
= root
->fs_info
->sb
;
1556 int seeding_dev
= 0;
1559 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1562 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1563 root
->fs_info
->bdev_holder
);
1565 return PTR_ERR(bdev
);
1567 if (root
->fs_info
->fs_devices
->seeding
) {
1569 down_write(&sb
->s_umount
);
1570 mutex_lock(&uuid_mutex
);
1573 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1574 mutex_lock(&root
->fs_info
->volume_mutex
);
1576 devices
= &root
->fs_info
->fs_devices
->devices
;
1578 * we have the volume lock, so we don't need the extra
1579 * device list mutex while reading the list here.
1581 list_for_each_entry(device
, devices
, dev_list
) {
1582 if (device
->bdev
== bdev
) {
1588 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1590 /* we can safely leave the fs_devices entry around */
1595 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1596 if (!device
->name
) {
1602 ret
= find_next_devid(root
, &device
->devid
);
1608 trans
= btrfs_start_transaction(root
, 0);
1611 device
->writeable
= 1;
1612 device
->work
.func
= pending_bios_fn
;
1613 generate_random_uuid(device
->uuid
);
1614 spin_lock_init(&device
->io_lock
);
1615 device
->generation
= trans
->transid
;
1616 device
->io_width
= root
->sectorsize
;
1617 device
->io_align
= root
->sectorsize
;
1618 device
->sector_size
= root
->sectorsize
;
1619 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1620 device
->disk_total_bytes
= device
->total_bytes
;
1621 device
->dev_root
= root
->fs_info
->dev_root
;
1622 device
->bdev
= bdev
;
1623 device
->in_fs_metadata
= 1;
1625 set_blocksize(device
->bdev
, 4096);
1628 sb
->s_flags
&= ~MS_RDONLY
;
1629 ret
= btrfs_prepare_sprout(trans
, root
);
1633 device
->fs_devices
= root
->fs_info
->fs_devices
;
1636 * we don't want write_supers to jump in here with our device
1639 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1640 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1641 list_add(&device
->dev_alloc_list
,
1642 &root
->fs_info
->fs_devices
->alloc_list
);
1643 root
->fs_info
->fs_devices
->num_devices
++;
1644 root
->fs_info
->fs_devices
->open_devices
++;
1645 root
->fs_info
->fs_devices
->rw_devices
++;
1646 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1648 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1649 root
->fs_info
->fs_devices
->rotating
= 1;
1651 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1652 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1653 total_bytes
+ device
->total_bytes
);
1655 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1656 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1658 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1661 ret
= init_first_rw_device(trans
, root
, device
);
1663 ret
= btrfs_finish_sprout(trans
, root
);
1666 ret
= btrfs_add_device(trans
, root
, device
);
1670 * we've got more storage, clear any full flags on the space
1673 btrfs_clear_space_info_full(root
->fs_info
);
1675 unlock_chunks(root
);
1676 btrfs_commit_transaction(trans
, root
);
1679 mutex_unlock(&uuid_mutex
);
1680 up_write(&sb
->s_umount
);
1682 ret
= btrfs_relocate_sys_chunks(root
);
1686 mutex_unlock(&root
->fs_info
->volume_mutex
);
1689 blkdev_put(bdev
, FMODE_EXCL
);
1691 mutex_unlock(&uuid_mutex
);
1692 up_write(&sb
->s_umount
);
1697 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1698 struct btrfs_device
*device
)
1701 struct btrfs_path
*path
;
1702 struct btrfs_root
*root
;
1703 struct btrfs_dev_item
*dev_item
;
1704 struct extent_buffer
*leaf
;
1705 struct btrfs_key key
;
1707 root
= device
->dev_root
->fs_info
->chunk_root
;
1709 path
= btrfs_alloc_path();
1713 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1714 key
.type
= BTRFS_DEV_ITEM_KEY
;
1715 key
.offset
= device
->devid
;
1717 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1726 leaf
= path
->nodes
[0];
1727 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1729 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1730 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1731 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1732 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1733 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1734 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1735 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1736 btrfs_mark_buffer_dirty(leaf
);
1739 btrfs_free_path(path
);
1743 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1744 struct btrfs_device
*device
, u64 new_size
)
1746 struct btrfs_super_block
*super_copy
=
1747 &device
->dev_root
->fs_info
->super_copy
;
1748 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1749 u64 diff
= new_size
- device
->total_bytes
;
1751 if (!device
->writeable
)
1753 if (new_size
<= device
->total_bytes
)
1756 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1757 device
->fs_devices
->total_rw_bytes
+= diff
;
1759 device
->total_bytes
= new_size
;
1760 device
->disk_total_bytes
= new_size
;
1761 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1763 return btrfs_update_device(trans
, device
);
1766 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1767 struct btrfs_device
*device
, u64 new_size
)
1770 lock_chunks(device
->dev_root
);
1771 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1772 unlock_chunks(device
->dev_root
);
1776 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1777 struct btrfs_root
*root
,
1778 u64 chunk_tree
, u64 chunk_objectid
,
1782 struct btrfs_path
*path
;
1783 struct btrfs_key key
;
1785 root
= root
->fs_info
->chunk_root
;
1786 path
= btrfs_alloc_path();
1790 key
.objectid
= chunk_objectid
;
1791 key
.offset
= chunk_offset
;
1792 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1794 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1797 ret
= btrfs_del_item(trans
, root
, path
);
1800 btrfs_free_path(path
);
1804 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1807 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1808 struct btrfs_disk_key
*disk_key
;
1809 struct btrfs_chunk
*chunk
;
1816 struct btrfs_key key
;
1818 array_size
= btrfs_super_sys_array_size(super_copy
);
1820 ptr
= super_copy
->sys_chunk_array
;
1823 while (cur
< array_size
) {
1824 disk_key
= (struct btrfs_disk_key
*)ptr
;
1825 btrfs_disk_key_to_cpu(&key
, disk_key
);
1827 len
= sizeof(*disk_key
);
1829 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1830 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1831 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1832 len
+= btrfs_chunk_item_size(num_stripes
);
1837 if (key
.objectid
== chunk_objectid
&&
1838 key
.offset
== chunk_offset
) {
1839 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1841 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1850 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1851 u64 chunk_tree
, u64 chunk_objectid
,
1854 struct extent_map_tree
*em_tree
;
1855 struct btrfs_root
*extent_root
;
1856 struct btrfs_trans_handle
*trans
;
1857 struct extent_map
*em
;
1858 struct map_lookup
*map
;
1862 root
= root
->fs_info
->chunk_root
;
1863 extent_root
= root
->fs_info
->extent_root
;
1864 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1866 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1870 /* step one, relocate all the extents inside this chunk */
1871 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1875 trans
= btrfs_start_transaction(root
, 0);
1881 * step two, delete the device extents and the
1882 * chunk tree entries
1884 read_lock(&em_tree
->lock
);
1885 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1886 read_unlock(&em_tree
->lock
);
1888 BUG_ON(em
->start
> chunk_offset
||
1889 em
->start
+ em
->len
< chunk_offset
);
1890 map
= (struct map_lookup
*)em
->bdev
;
1892 for (i
= 0; i
< map
->num_stripes
; i
++) {
1893 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1894 map
->stripes
[i
].physical
);
1897 if (map
->stripes
[i
].dev
) {
1898 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1902 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1907 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1908 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1912 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1915 write_lock(&em_tree
->lock
);
1916 remove_extent_mapping(em_tree
, em
);
1917 write_unlock(&em_tree
->lock
);
1922 /* once for the tree */
1923 free_extent_map(em
);
1925 free_extent_map(em
);
1927 unlock_chunks(root
);
1928 btrfs_end_transaction(trans
, root
);
1932 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1934 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1935 struct btrfs_path
*path
;
1936 struct extent_buffer
*leaf
;
1937 struct btrfs_chunk
*chunk
;
1938 struct btrfs_key key
;
1939 struct btrfs_key found_key
;
1940 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1942 bool retried
= false;
1946 path
= btrfs_alloc_path();
1951 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1952 key
.offset
= (u64
)-1;
1953 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1956 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1961 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1968 leaf
= path
->nodes
[0];
1969 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1971 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1972 struct btrfs_chunk
);
1973 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1974 btrfs_release_path(chunk_root
, path
);
1976 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1977 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1986 if (found_key
.offset
== 0)
1988 key
.offset
= found_key
.offset
- 1;
1991 if (failed
&& !retried
) {
1995 } else if (failed
&& retried
) {
2000 btrfs_free_path(path
);
2004 static u64
div_factor(u64 num
, int factor
)
2013 int btrfs_balance(struct btrfs_root
*dev_root
)
2016 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2017 struct btrfs_device
*device
;
2020 struct btrfs_path
*path
;
2021 struct btrfs_key key
;
2022 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2023 struct btrfs_trans_handle
*trans
;
2024 struct btrfs_key found_key
;
2026 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2029 if (!capable(CAP_SYS_ADMIN
))
2032 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2033 dev_root
= dev_root
->fs_info
->dev_root
;
2035 /* step one make some room on all the devices */
2036 list_for_each_entry(device
, devices
, dev_list
) {
2037 old_size
= device
->total_bytes
;
2038 size_to_free
= div_factor(old_size
, 1);
2039 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2040 if (!device
->writeable
||
2041 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2044 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2049 trans
= btrfs_start_transaction(dev_root
, 0);
2052 ret
= btrfs_grow_device(trans
, device
, old_size
);
2055 btrfs_end_transaction(trans
, dev_root
);
2058 /* step two, relocate all the chunks */
2059 path
= btrfs_alloc_path();
2062 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2063 key
.offset
= (u64
)-1;
2064 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2067 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2072 * this shouldn't happen, it means the last relocate
2078 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2079 BTRFS_CHUNK_ITEM_KEY
);
2083 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2085 if (found_key
.objectid
!= key
.objectid
)
2088 /* chunk zero is special */
2089 if (found_key
.offset
== 0)
2092 btrfs_release_path(chunk_root
, path
);
2093 ret
= btrfs_relocate_chunk(chunk_root
,
2094 chunk_root
->root_key
.objectid
,
2097 BUG_ON(ret
&& ret
!= -ENOSPC
);
2098 key
.offset
= found_key
.offset
- 1;
2102 btrfs_free_path(path
);
2103 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2108 * shrinking a device means finding all of the device extents past
2109 * the new size, and then following the back refs to the chunks.
2110 * The chunk relocation code actually frees the device extent
2112 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2114 struct btrfs_trans_handle
*trans
;
2115 struct btrfs_root
*root
= device
->dev_root
;
2116 struct btrfs_dev_extent
*dev_extent
= NULL
;
2117 struct btrfs_path
*path
;
2125 bool retried
= false;
2126 struct extent_buffer
*l
;
2127 struct btrfs_key key
;
2128 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2129 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2130 u64 old_size
= device
->total_bytes
;
2131 u64 diff
= device
->total_bytes
- new_size
;
2133 if (new_size
>= device
->total_bytes
)
2136 path
= btrfs_alloc_path();
2144 device
->total_bytes
= new_size
;
2145 if (device
->writeable
)
2146 device
->fs_devices
->total_rw_bytes
-= diff
;
2147 unlock_chunks(root
);
2150 key
.objectid
= device
->devid
;
2151 key
.offset
= (u64
)-1;
2152 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2155 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2159 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2164 btrfs_release_path(root
, path
);
2169 slot
= path
->slots
[0];
2170 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2172 if (key
.objectid
!= device
->devid
) {
2173 btrfs_release_path(root
, path
);
2177 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2178 length
= btrfs_dev_extent_length(l
, dev_extent
);
2180 if (key
.offset
+ length
<= new_size
) {
2181 btrfs_release_path(root
, path
);
2185 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2186 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2187 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2188 btrfs_release_path(root
, path
);
2190 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2192 if (ret
&& ret
!= -ENOSPC
)
2199 if (failed
&& !retried
) {
2203 } else if (failed
&& retried
) {
2207 device
->total_bytes
= old_size
;
2208 if (device
->writeable
)
2209 device
->fs_devices
->total_rw_bytes
+= diff
;
2210 unlock_chunks(root
);
2214 /* Shrinking succeeded, else we would be at "done". */
2215 trans
= btrfs_start_transaction(root
, 0);
2218 device
->disk_total_bytes
= new_size
;
2219 /* Now btrfs_update_device() will change the on-disk size. */
2220 ret
= btrfs_update_device(trans
, device
);
2222 unlock_chunks(root
);
2223 btrfs_end_transaction(trans
, root
);
2226 WARN_ON(diff
> old_total
);
2227 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2228 unlock_chunks(root
);
2229 btrfs_end_transaction(trans
, root
);
2231 btrfs_free_path(path
);
2235 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2236 struct btrfs_root
*root
,
2237 struct btrfs_key
*key
,
2238 struct btrfs_chunk
*chunk
, int item_size
)
2240 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2241 struct btrfs_disk_key disk_key
;
2245 array_size
= btrfs_super_sys_array_size(super_copy
);
2246 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2249 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2250 btrfs_cpu_key_to_disk(&disk_key
, key
);
2251 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2252 ptr
+= sizeof(disk_key
);
2253 memcpy(ptr
, chunk
, item_size
);
2254 item_size
+= sizeof(disk_key
);
2255 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2259 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2260 int num_stripes
, int sub_stripes
)
2262 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2264 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2265 return calc_size
* (num_stripes
/ sub_stripes
);
2267 return calc_size
* num_stripes
;
2270 /* Used to sort the devices by max_avail(descending sort) */
2271 int btrfs_cmp_device_free_bytes(const void *dev_info1
, const void *dev_info2
)
2273 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
2274 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2276 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
2277 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2283 static int __btrfs_calc_nstripes(struct btrfs_fs_devices
*fs_devices
, u64 type
,
2284 int *num_stripes
, int *min_stripes
,
2291 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2292 *num_stripes
= fs_devices
->rw_devices
;
2295 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2299 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2300 if (fs_devices
->rw_devices
< 2)
2305 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2306 *num_stripes
= fs_devices
->rw_devices
;
2307 if (*num_stripes
< 4)
2309 *num_stripes
&= ~(u32
)1;
2317 static u64
__btrfs_calc_stripe_size(struct btrfs_fs_devices
*fs_devices
,
2318 u64 proposed_size
, u64 type
,
2319 int num_stripes
, int small_stripe
)
2321 int min_stripe_size
= 1 * 1024 * 1024;
2322 u64 calc_size
= proposed_size
;
2323 u64 max_chunk_size
= calc_size
;
2326 if (type
& (BTRFS_BLOCK_GROUP_RAID1
|
2327 BTRFS_BLOCK_GROUP_DUP
|
2328 BTRFS_BLOCK_GROUP_RAID10
))
2331 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2332 max_chunk_size
= 10 * calc_size
;
2333 min_stripe_size
= 64 * 1024 * 1024;
2334 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2335 max_chunk_size
= 256 * 1024 * 1024;
2336 min_stripe_size
= 32 * 1024 * 1024;
2337 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2338 calc_size
= 8 * 1024 * 1024;
2339 max_chunk_size
= calc_size
* 2;
2340 min_stripe_size
= 1 * 1024 * 1024;
2343 /* we don't want a chunk larger than 10% of writeable space */
2344 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2347 if (calc_size
* num_stripes
> max_chunk_size
* ncopies
) {
2348 calc_size
= max_chunk_size
* ncopies
;
2349 do_div(calc_size
, num_stripes
);
2350 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2351 calc_size
*= BTRFS_STRIPE_LEN
;
2354 /* we don't want tiny stripes */
2356 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2359 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2360 * we end up with something bigger than a stripe
2362 calc_size
= max_t(u64
, calc_size
, BTRFS_STRIPE_LEN
);
2364 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2365 calc_size
*= BTRFS_STRIPE_LEN
;
2370 static struct map_lookup
*__shrink_map_lookup_stripes(struct map_lookup
*map
,
2373 struct map_lookup
*new;
2374 size_t len
= map_lookup_size(num_stripes
);
2376 BUG_ON(map
->num_stripes
< num_stripes
);
2378 if (map
->num_stripes
== num_stripes
)
2381 new = kmalloc(len
, GFP_NOFS
);
2383 /* just change map->num_stripes */
2384 map
->num_stripes
= num_stripes
;
2388 memcpy(new, map
, len
);
2389 new->num_stripes
= num_stripes
;
2395 * helper to allocate device space from btrfs_device_info, in which we stored
2396 * max free space information of every device. It is used when we can not
2397 * allocate chunks by default size.
2399 * By this helper, we can allocate a new chunk as larger as possible.
2401 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle
*trans
,
2402 struct btrfs_fs_devices
*fs_devices
,
2403 struct btrfs_device_info
*devices
,
2404 int nr_device
, u64 type
,
2405 struct map_lookup
**map_lookup
,
2406 int min_stripes
, u64
*stripe_size
)
2408 int i
, index
, sort_again
= 0;
2409 int min_devices
= min_stripes
;
2410 u64 max_avail
, min_free
;
2411 struct map_lookup
*map
= *map_lookup
;
2414 if (nr_device
< min_stripes
)
2417 btrfs_descending_sort_devices(devices
, nr_device
);
2419 max_avail
= devices
[0].max_avail
;
2423 for (i
= 0; i
< nr_device
; i
++) {
2425 * if dev_offset = 0, it means the free space of this device
2426 * is less than what we need, and we didn't search max avail
2427 * extent on this device, so do it now.
2429 if (!devices
[i
].dev_offset
) {
2430 ret
= find_free_dev_extent(trans
, devices
[i
].dev
,
2432 &devices
[i
].dev_offset
,
2433 &devices
[i
].max_avail
);
2434 if (ret
!= 0 && ret
!= -ENOSPC
)
2440 /* we update the max avail free extent of each devices, sort again */
2442 btrfs_descending_sort_devices(devices
, nr_device
);
2444 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2447 if (!devices
[min_devices
- 1].max_avail
)
2450 max_avail
= devices
[min_devices
- 1].max_avail
;
2451 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2452 do_div(max_avail
, 2);
2454 max_avail
= __btrfs_calc_stripe_size(fs_devices
, max_avail
, type
,
2456 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2457 min_free
= max_avail
* 2;
2459 min_free
= max_avail
;
2461 if (min_free
> devices
[min_devices
- 1].max_avail
)
2464 map
= __shrink_map_lookup_stripes(map
, min_stripes
);
2465 *stripe_size
= max_avail
;
2468 for (i
= 0; i
< min_stripes
; i
++) {
2469 map
->stripes
[i
].dev
= devices
[index
].dev
;
2470 map
->stripes
[i
].physical
= devices
[index
].dev_offset
;
2471 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2473 map
->stripes
[i
].dev
= devices
[index
].dev
;
2474 map
->stripes
[i
].physical
= devices
[index
].dev_offset
+
2484 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2485 struct btrfs_root
*extent_root
,
2486 struct map_lookup
**map_ret
,
2487 u64
*num_bytes
, u64
*stripe_size
,
2488 u64 start
, u64 type
)
2490 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2491 struct btrfs_device
*device
= NULL
;
2492 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2493 struct list_head
*cur
;
2494 struct map_lookup
*map
;
2495 struct extent_map_tree
*em_tree
;
2496 struct extent_map
*em
;
2497 struct btrfs_device_info
*devices_info
;
2498 struct list_head private_devs
;
2499 u64 calc_size
= 1024 * 1024 * 1024;
2506 int min_devices
; /* the min number of devices we need */
2511 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2512 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2514 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2516 if (list_empty(&fs_devices
->alloc_list
))
2519 ret
= __btrfs_calc_nstripes(fs_devices
, type
, &num_stripes
,
2520 &min_stripes
, &sub_stripes
);
2524 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2529 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2534 map
->num_stripes
= num_stripes
;
2536 cur
= fs_devices
->alloc_list
.next
;
2540 calc_size
= __btrfs_calc_stripe_size(fs_devices
, calc_size
, type
,
2543 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2544 min_free
= calc_size
* 2;
2547 min_free
= calc_size
;
2548 min_devices
= min_stripes
;
2551 INIT_LIST_HEAD(&private_devs
);
2552 while (index
< num_stripes
) {
2553 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2554 BUG_ON(!device
->writeable
);
2555 if (device
->total_bytes
> device
->bytes_used
)
2556 avail
= device
->total_bytes
- device
->bytes_used
;
2561 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2562 ret
= find_free_dev_extent(trans
, device
, min_free
,
2563 &devices_info
[i
].dev_offset
,
2564 &devices_info
[i
].max_avail
);
2566 list_move_tail(&device
->dev_alloc_list
,
2568 map
->stripes
[index
].dev
= device
;
2569 map
->stripes
[index
].physical
=
2570 devices_info
[i
].dev_offset
;
2572 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2573 map
->stripes
[index
].dev
= device
;
2574 map
->stripes
[index
].physical
=
2575 devices_info
[i
].dev_offset
+
2579 } else if (ret
!= -ENOSPC
)
2582 devices_info
[i
].dev
= device
;
2584 } else if (device
->in_fs_metadata
&&
2585 avail
>= BTRFS_STRIPE_LEN
) {
2586 devices_info
[i
].dev
= device
;
2587 devices_info
[i
].max_avail
= avail
;
2591 if (cur
== &fs_devices
->alloc_list
)
2595 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2596 if (index
< num_stripes
) {
2597 if (index
>= min_stripes
) {
2598 num_stripes
= index
;
2599 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2600 num_stripes
/= sub_stripes
;
2601 num_stripes
*= sub_stripes
;
2604 map
= __shrink_map_lookup_stripes(map
, num_stripes
);
2605 } else if (i
>= min_devices
) {
2606 ret
= __btrfs_alloc_tiny_space(trans
, fs_devices
,
2607 devices_info
, i
, type
,
2617 map
->sector_size
= extent_root
->sectorsize
;
2618 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2619 map
->io_align
= BTRFS_STRIPE_LEN
;
2620 map
->io_width
= BTRFS_STRIPE_LEN
;
2622 map
->sub_stripes
= sub_stripes
;
2625 *stripe_size
= calc_size
;
2626 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2627 map
->num_stripes
, sub_stripes
);
2629 em
= alloc_extent_map(GFP_NOFS
);
2634 em
->bdev
= (struct block_device
*)map
;
2636 em
->len
= *num_bytes
;
2637 em
->block_start
= 0;
2638 em
->block_len
= em
->len
;
2640 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2641 write_lock(&em_tree
->lock
);
2642 ret
= add_extent_mapping(em_tree
, em
);
2643 write_unlock(&em_tree
->lock
);
2645 free_extent_map(em
);
2647 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2648 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2653 while (index
< map
->num_stripes
) {
2654 device
= map
->stripes
[index
].dev
;
2655 dev_offset
= map
->stripes
[index
].physical
;
2657 ret
= btrfs_alloc_dev_extent(trans
, device
,
2658 info
->chunk_root
->root_key
.objectid
,
2659 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2660 start
, dev_offset
, calc_size
);
2665 kfree(devices_info
);
2670 kfree(devices_info
);
2674 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2675 struct btrfs_root
*extent_root
,
2676 struct map_lookup
*map
, u64 chunk_offset
,
2677 u64 chunk_size
, u64 stripe_size
)
2680 struct btrfs_key key
;
2681 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2682 struct btrfs_device
*device
;
2683 struct btrfs_chunk
*chunk
;
2684 struct btrfs_stripe
*stripe
;
2685 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2689 chunk
= kzalloc(item_size
, GFP_NOFS
);
2694 while (index
< map
->num_stripes
) {
2695 device
= map
->stripes
[index
].dev
;
2696 device
->bytes_used
+= stripe_size
;
2697 ret
= btrfs_update_device(trans
, device
);
2703 stripe
= &chunk
->stripe
;
2704 while (index
< map
->num_stripes
) {
2705 device
= map
->stripes
[index
].dev
;
2706 dev_offset
= map
->stripes
[index
].physical
;
2708 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2709 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2710 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2715 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2716 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2717 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2718 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2719 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2720 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2721 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2722 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2723 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2725 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2726 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2727 key
.offset
= chunk_offset
;
2729 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2732 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2733 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2742 * Chunk allocation falls into two parts. The first part does works
2743 * that make the new allocated chunk useable, but not do any operation
2744 * that modifies the chunk tree. The second part does the works that
2745 * require modifying the chunk tree. This division is important for the
2746 * bootstrap process of adding storage to a seed btrfs.
2748 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2749 struct btrfs_root
*extent_root
, u64 type
)
2754 struct map_lookup
*map
;
2755 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2758 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2763 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2764 &stripe_size
, chunk_offset
, type
);
2768 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2769 chunk_size
, stripe_size
);
2774 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2775 struct btrfs_root
*root
,
2776 struct btrfs_device
*device
)
2779 u64 sys_chunk_offset
;
2783 u64 sys_stripe_size
;
2785 struct map_lookup
*map
;
2786 struct map_lookup
*sys_map
;
2787 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2788 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2791 ret
= find_next_chunk(fs_info
->chunk_root
,
2792 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2795 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2796 (fs_info
->metadata_alloc_profile
&
2797 fs_info
->avail_metadata_alloc_bits
);
2798 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2800 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2801 &stripe_size
, chunk_offset
, alloc_profile
);
2804 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2806 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2807 (fs_info
->system_alloc_profile
&
2808 fs_info
->avail_system_alloc_bits
);
2809 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2811 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2812 &sys_chunk_size
, &sys_stripe_size
,
2813 sys_chunk_offset
, alloc_profile
);
2816 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2820 * Modifying chunk tree needs allocating new blocks from both
2821 * system block group and metadata block group. So we only can
2822 * do operations require modifying the chunk tree after both
2823 * block groups were created.
2825 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2826 chunk_size
, stripe_size
);
2829 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2830 sys_chunk_offset
, sys_chunk_size
,
2836 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2838 struct extent_map
*em
;
2839 struct map_lookup
*map
;
2840 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2844 read_lock(&map_tree
->map_tree
.lock
);
2845 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2846 read_unlock(&map_tree
->map_tree
.lock
);
2850 if (btrfs_test_opt(root
, DEGRADED
)) {
2851 free_extent_map(em
);
2855 map
= (struct map_lookup
*)em
->bdev
;
2856 for (i
= 0; i
< map
->num_stripes
; i
++) {
2857 if (!map
->stripes
[i
].dev
->writeable
) {
2862 free_extent_map(em
);
2866 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2868 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2871 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2873 struct extent_map
*em
;
2876 write_lock(&tree
->map_tree
.lock
);
2877 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2879 remove_extent_mapping(&tree
->map_tree
, em
);
2880 write_unlock(&tree
->map_tree
.lock
);
2885 free_extent_map(em
);
2886 /* once for the tree */
2887 free_extent_map(em
);
2891 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2893 struct extent_map
*em
;
2894 struct map_lookup
*map
;
2895 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2898 read_lock(&em_tree
->lock
);
2899 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2900 read_unlock(&em_tree
->lock
);
2903 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2904 map
= (struct map_lookup
*)em
->bdev
;
2905 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2906 ret
= map
->num_stripes
;
2907 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2908 ret
= map
->sub_stripes
;
2911 free_extent_map(em
);
2915 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2919 if (map
->stripes
[optimal
].dev
->bdev
)
2921 for (i
= first
; i
< first
+ num
; i
++) {
2922 if (map
->stripes
[i
].dev
->bdev
)
2925 /* we couldn't find one that doesn't fail. Just return something
2926 * and the io error handling code will clean up eventually
2931 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2932 u64 logical
, u64
*length
,
2933 struct btrfs_multi_bio
**multi_ret
,
2934 int mirror_num
, struct page
*unplug_page
)
2936 struct extent_map
*em
;
2937 struct map_lookup
*map
;
2938 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2942 int stripes_allocated
= 8;
2943 int stripes_required
= 1;
2948 struct btrfs_multi_bio
*multi
= NULL
;
2950 if (multi_ret
&& !(rw
& REQ_WRITE
))
2951 stripes_allocated
= 1;
2954 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2959 atomic_set(&multi
->error
, 0);
2962 read_lock(&em_tree
->lock
);
2963 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2964 read_unlock(&em_tree
->lock
);
2966 if (!em
&& unplug_page
) {
2972 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2973 (unsigned long long)logical
,
2974 (unsigned long long)*length
);
2978 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2979 map
= (struct map_lookup
*)em
->bdev
;
2980 offset
= logical
- em
->start
;
2982 if (mirror_num
> map
->num_stripes
)
2985 /* if our multi bio struct is too small, back off and try again */
2986 if (rw
& REQ_WRITE
) {
2987 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2988 BTRFS_BLOCK_GROUP_DUP
)) {
2989 stripes_required
= map
->num_stripes
;
2991 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2992 stripes_required
= map
->sub_stripes
;
2996 if (multi_ret
&& (rw
& REQ_WRITE
) &&
2997 stripes_allocated
< stripes_required
) {
2998 stripes_allocated
= map
->num_stripes
;
2999 free_extent_map(em
);
3005 * stripe_nr counts the total number of stripes we have to stride
3006 * to get to this block
3008 do_div(stripe_nr
, map
->stripe_len
);
3010 stripe_offset
= stripe_nr
* map
->stripe_len
;
3011 BUG_ON(offset
< stripe_offset
);
3013 /* stripe_offset is the offset of this block in its stripe*/
3014 stripe_offset
= offset
- stripe_offset
;
3016 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| 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
;
3026 if (!multi_ret
&& !unplug_page
)
3031 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3032 if (unplug_page
|| (rw
& REQ_WRITE
))
3033 num_stripes
= map
->num_stripes
;
3034 else if (mirror_num
)
3035 stripe_index
= mirror_num
- 1;
3037 stripe_index
= find_live_mirror(map
, 0,
3039 current
->pid
% map
->num_stripes
);
3042 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3044 num_stripes
= map
->num_stripes
;
3045 else if (mirror_num
)
3046 stripe_index
= mirror_num
- 1;
3048 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3049 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3051 stripe_index
= do_div(stripe_nr
, factor
);
3052 stripe_index
*= map
->sub_stripes
;
3054 if (unplug_page
|| (rw
& REQ_WRITE
))
3055 num_stripes
= map
->sub_stripes
;
3056 else if (mirror_num
)
3057 stripe_index
+= mirror_num
- 1;
3059 stripe_index
= find_live_mirror(map
, stripe_index
,
3060 map
->sub_stripes
, stripe_index
+
3061 current
->pid
% map
->sub_stripes
);
3065 * after this do_div call, stripe_nr is the number of stripes
3066 * on this device we have to walk to find the data, and
3067 * stripe_index is the number of our device in the stripe array
3069 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3071 BUG_ON(stripe_index
>= map
->num_stripes
);
3073 for (i
= 0; i
< num_stripes
; i
++) {
3075 struct btrfs_device
*device
;
3076 struct backing_dev_info
*bdi
;
3078 device
= map
->stripes
[stripe_index
].dev
;
3080 bdi
= blk_get_backing_dev_info(device
->bdev
);
3081 if (bdi
->unplug_io_fn
)
3082 bdi
->unplug_io_fn(bdi
, unplug_page
);
3085 multi
->stripes
[i
].physical
=
3086 map
->stripes
[stripe_index
].physical
+
3087 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3088 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3094 multi
->num_stripes
= num_stripes
;
3095 multi
->max_errors
= max_errors
;
3098 free_extent_map(em
);
3102 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3103 u64 logical
, u64
*length
,
3104 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3106 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3110 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3111 u64 chunk_start
, u64 physical
, u64 devid
,
3112 u64
**logical
, int *naddrs
, int *stripe_len
)
3114 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3115 struct extent_map
*em
;
3116 struct map_lookup
*map
;
3123 read_lock(&em_tree
->lock
);
3124 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3125 read_unlock(&em_tree
->lock
);
3127 BUG_ON(!em
|| em
->start
!= chunk_start
);
3128 map
= (struct map_lookup
*)em
->bdev
;
3131 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3132 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3133 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3134 do_div(length
, map
->num_stripes
);
3136 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3139 for (i
= 0; i
< map
->num_stripes
; i
++) {
3140 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3142 if (map
->stripes
[i
].physical
> physical
||
3143 map
->stripes
[i
].physical
+ length
<= physical
)
3146 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3147 do_div(stripe_nr
, map
->stripe_len
);
3149 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3150 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3151 do_div(stripe_nr
, map
->sub_stripes
);
3152 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3153 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3155 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3156 WARN_ON(nr
>= map
->num_stripes
);
3157 for (j
= 0; j
< nr
; j
++) {
3158 if (buf
[j
] == bytenr
)
3162 WARN_ON(nr
>= map
->num_stripes
);
3169 *stripe_len
= map
->stripe_len
;
3171 free_extent_map(em
);
3175 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
3176 u64 logical
, struct page
*page
)
3178 u64 length
= PAGE_CACHE_SIZE
;
3179 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
3183 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3185 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3186 int is_orig_bio
= 0;
3189 atomic_inc(&multi
->error
);
3191 if (bio
== multi
->orig_bio
)
3194 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3197 bio
= multi
->orig_bio
;
3199 bio
->bi_private
= multi
->private;
3200 bio
->bi_end_io
= multi
->end_io
;
3201 /* only send an error to the higher layers if it is
3202 * beyond the tolerance of the multi-bio
3204 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3208 * this bio is actually up to date, we didn't
3209 * go over the max number of errors
3211 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3216 bio_endio(bio
, err
);
3217 } else if (!is_orig_bio
) {
3222 struct async_sched
{
3225 struct btrfs_fs_info
*info
;
3226 struct btrfs_work work
;
3230 * see run_scheduled_bios for a description of why bios are collected for
3233 * This will add one bio to the pending list for a device and make sure
3234 * the work struct is scheduled.
3236 static noinline
int schedule_bio(struct btrfs_root
*root
,
3237 struct btrfs_device
*device
,
3238 int rw
, struct bio
*bio
)
3240 int should_queue
= 1;
3241 struct btrfs_pending_bios
*pending_bios
;
3243 /* don't bother with additional async steps for reads, right now */
3244 if (!(rw
& REQ_WRITE
)) {
3246 submit_bio(rw
, bio
);
3252 * nr_async_bios allows us to reliably return congestion to the
3253 * higher layers. Otherwise, the async bio makes it appear we have
3254 * made progress against dirty pages when we've really just put it
3255 * on a queue for later
3257 atomic_inc(&root
->fs_info
->nr_async_bios
);
3258 WARN_ON(bio
->bi_next
);
3259 bio
->bi_next
= NULL
;
3262 spin_lock(&device
->io_lock
);
3263 if (bio
->bi_rw
& REQ_SYNC
)
3264 pending_bios
= &device
->pending_sync_bios
;
3266 pending_bios
= &device
->pending_bios
;
3268 if (pending_bios
->tail
)
3269 pending_bios
->tail
->bi_next
= bio
;
3271 pending_bios
->tail
= bio
;
3272 if (!pending_bios
->head
)
3273 pending_bios
->head
= bio
;
3274 if (device
->running_pending
)
3277 spin_unlock(&device
->io_lock
);
3280 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3285 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3286 int mirror_num
, int async_submit
)
3288 struct btrfs_mapping_tree
*map_tree
;
3289 struct btrfs_device
*dev
;
3290 struct bio
*first_bio
= bio
;
3291 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3294 struct btrfs_multi_bio
*multi
= NULL
;
3299 length
= bio
->bi_size
;
3300 map_tree
= &root
->fs_info
->mapping_tree
;
3301 map_length
= length
;
3303 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3307 total_devs
= multi
->num_stripes
;
3308 if (map_length
< length
) {
3309 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3310 "len %llu\n", (unsigned long long)logical
,
3311 (unsigned long long)length
,
3312 (unsigned long long)map_length
);
3315 multi
->end_io
= first_bio
->bi_end_io
;
3316 multi
->private = first_bio
->bi_private
;
3317 multi
->orig_bio
= first_bio
;
3318 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3320 while (dev_nr
< total_devs
) {
3321 if (total_devs
> 1) {
3322 if (dev_nr
< total_devs
- 1) {
3323 bio
= bio_clone(first_bio
, GFP_NOFS
);
3328 bio
->bi_private
= multi
;
3329 bio
->bi_end_io
= end_bio_multi_stripe
;
3331 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3332 dev
= multi
->stripes
[dev_nr
].dev
;
3333 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3334 bio
->bi_bdev
= dev
->bdev
;
3336 schedule_bio(root
, dev
, rw
, bio
);
3338 submit_bio(rw
, bio
);
3340 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3341 bio
->bi_sector
= logical
>> 9;
3342 bio_endio(bio
, -EIO
);
3346 if (total_devs
== 1)
3351 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3354 struct btrfs_device
*device
;
3355 struct btrfs_fs_devices
*cur_devices
;
3357 cur_devices
= root
->fs_info
->fs_devices
;
3358 while (cur_devices
) {
3360 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3361 device
= __find_device(&cur_devices
->devices
,
3366 cur_devices
= cur_devices
->seed
;
3371 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3372 u64 devid
, u8
*dev_uuid
)
3374 struct btrfs_device
*device
;
3375 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3377 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3380 list_add(&device
->dev_list
,
3381 &fs_devices
->devices
);
3382 device
->dev_root
= root
->fs_info
->dev_root
;
3383 device
->devid
= devid
;
3384 device
->work
.func
= pending_bios_fn
;
3385 device
->fs_devices
= fs_devices
;
3386 device
->missing
= 1;
3387 fs_devices
->num_devices
++;
3388 fs_devices
->missing_devices
++;
3389 spin_lock_init(&device
->io_lock
);
3390 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3391 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3395 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3396 struct extent_buffer
*leaf
,
3397 struct btrfs_chunk
*chunk
)
3399 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3400 struct map_lookup
*map
;
3401 struct extent_map
*em
;
3405 u8 uuid
[BTRFS_UUID_SIZE
];
3410 logical
= key
->offset
;
3411 length
= btrfs_chunk_length(leaf
, chunk
);
3413 read_lock(&map_tree
->map_tree
.lock
);
3414 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3415 read_unlock(&map_tree
->map_tree
.lock
);
3417 /* already mapped? */
3418 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3419 free_extent_map(em
);
3422 free_extent_map(em
);
3425 em
= alloc_extent_map(GFP_NOFS
);
3428 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3429 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3431 free_extent_map(em
);
3435 em
->bdev
= (struct block_device
*)map
;
3436 em
->start
= logical
;
3438 em
->block_start
= 0;
3439 em
->block_len
= em
->len
;
3441 map
->num_stripes
= num_stripes
;
3442 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3443 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3444 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3445 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3446 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3447 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3448 for (i
= 0; i
< num_stripes
; i
++) {
3449 map
->stripes
[i
].physical
=
3450 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3451 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3452 read_extent_buffer(leaf
, uuid
, (unsigned long)
3453 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3455 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3457 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3459 free_extent_map(em
);
3462 if (!map
->stripes
[i
].dev
) {
3463 map
->stripes
[i
].dev
=
3464 add_missing_dev(root
, devid
, uuid
);
3465 if (!map
->stripes
[i
].dev
) {
3467 free_extent_map(em
);
3471 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3474 write_lock(&map_tree
->map_tree
.lock
);
3475 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3476 write_unlock(&map_tree
->map_tree
.lock
);
3478 free_extent_map(em
);
3483 static int fill_device_from_item(struct extent_buffer
*leaf
,
3484 struct btrfs_dev_item
*dev_item
,
3485 struct btrfs_device
*device
)
3489 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3490 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3491 device
->total_bytes
= device
->disk_total_bytes
;
3492 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3493 device
->type
= btrfs_device_type(leaf
, dev_item
);
3494 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3495 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3496 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3498 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3499 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3504 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3506 struct btrfs_fs_devices
*fs_devices
;
3509 mutex_lock(&uuid_mutex
);
3511 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3512 while (fs_devices
) {
3513 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3517 fs_devices
= fs_devices
->seed
;
3520 fs_devices
= find_fsid(fsid
);
3526 fs_devices
= clone_fs_devices(fs_devices
);
3527 if (IS_ERR(fs_devices
)) {
3528 ret
= PTR_ERR(fs_devices
);
3532 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3533 root
->fs_info
->bdev_holder
);
3537 if (!fs_devices
->seeding
) {
3538 __btrfs_close_devices(fs_devices
);
3539 free_fs_devices(fs_devices
);
3544 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3545 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3547 mutex_unlock(&uuid_mutex
);
3551 static int read_one_dev(struct btrfs_root
*root
,
3552 struct extent_buffer
*leaf
,
3553 struct btrfs_dev_item
*dev_item
)
3555 struct btrfs_device
*device
;
3558 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3559 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3561 devid
= btrfs_device_id(leaf
, dev_item
);
3562 read_extent_buffer(leaf
, dev_uuid
,
3563 (unsigned long)btrfs_device_uuid(dev_item
),
3565 read_extent_buffer(leaf
, fs_uuid
,
3566 (unsigned long)btrfs_device_fsid(dev_item
),
3569 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3570 ret
= open_seed_devices(root
, fs_uuid
);
3571 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3575 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3576 if (!device
|| !device
->bdev
) {
3577 if (!btrfs_test_opt(root
, DEGRADED
))
3581 printk(KERN_WARNING
"warning devid %llu missing\n",
3582 (unsigned long long)devid
);
3583 device
= add_missing_dev(root
, devid
, dev_uuid
);
3586 } else if (!device
->missing
) {
3588 * this happens when a device that was properly setup
3589 * in the device info lists suddenly goes bad.
3590 * device->bdev is NULL, and so we have to set
3591 * device->missing to one here
3593 root
->fs_info
->fs_devices
->missing_devices
++;
3594 device
->missing
= 1;
3598 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3599 BUG_ON(device
->writeable
);
3600 if (device
->generation
!=
3601 btrfs_device_generation(leaf
, dev_item
))
3605 fill_device_from_item(leaf
, dev_item
, device
);
3606 device
->dev_root
= root
->fs_info
->dev_root
;
3607 device
->in_fs_metadata
= 1;
3608 if (device
->writeable
)
3609 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3614 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3616 struct btrfs_dev_item
*dev_item
;
3618 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3620 return read_one_dev(root
, buf
, dev_item
);
3623 int btrfs_read_sys_array(struct btrfs_root
*root
)
3625 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3626 struct extent_buffer
*sb
;
3627 struct btrfs_disk_key
*disk_key
;
3628 struct btrfs_chunk
*chunk
;
3630 unsigned long sb_ptr
;
3636 struct btrfs_key key
;
3638 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3639 BTRFS_SUPER_INFO_SIZE
);
3642 btrfs_set_buffer_uptodate(sb
);
3643 btrfs_set_buffer_lockdep_class(sb
, 0);
3645 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3646 array_size
= btrfs_super_sys_array_size(super_copy
);
3648 ptr
= super_copy
->sys_chunk_array
;
3649 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3652 while (cur
< array_size
) {
3653 disk_key
= (struct btrfs_disk_key
*)ptr
;
3654 btrfs_disk_key_to_cpu(&key
, disk_key
);
3656 len
= sizeof(*disk_key
); ptr
+= len
;
3660 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3661 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3662 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3665 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3666 len
= btrfs_chunk_item_size(num_stripes
);
3675 free_extent_buffer(sb
);
3679 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3681 struct btrfs_path
*path
;
3682 struct extent_buffer
*leaf
;
3683 struct btrfs_key key
;
3684 struct btrfs_key found_key
;
3688 root
= root
->fs_info
->chunk_root
;
3690 path
= btrfs_alloc_path();
3694 /* first we search for all of the device items, and then we
3695 * read in all of the chunk items. This way we can create chunk
3696 * mappings that reference all of the devices that are afound
3698 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3702 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3706 leaf
= path
->nodes
[0];
3707 slot
= path
->slots
[0];
3708 if (slot
>= btrfs_header_nritems(leaf
)) {
3709 ret
= btrfs_next_leaf(root
, path
);
3716 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3717 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3718 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3720 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3721 struct btrfs_dev_item
*dev_item
;
3722 dev_item
= btrfs_item_ptr(leaf
, slot
,
3723 struct btrfs_dev_item
);
3724 ret
= read_one_dev(root
, leaf
, dev_item
);
3728 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3729 struct btrfs_chunk
*chunk
;
3730 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3731 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3737 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3739 btrfs_release_path(root
, path
);
3744 btrfs_free_path(path
);