2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 struct btrfs_device
*device
);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
41 static DEFINE_MUTEX(uuid_mutex
);
42 static LIST_HEAD(fs_uuids
);
44 static void lock_chunks(struct btrfs_root
*root
)
46 mutex_lock(&root
->fs_info
->chunk_mutex
);
49 static void unlock_chunks(struct btrfs_root
*root
)
51 mutex_unlock(&root
->fs_info
->chunk_mutex
);
54 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
56 struct btrfs_device
*device
;
57 WARN_ON(fs_devices
->opened
);
58 while (!list_empty(&fs_devices
->devices
)) {
59 device
= list_entry(fs_devices
->devices
.next
,
60 struct btrfs_device
, dev_list
);
61 list_del(&device
->dev_list
);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices
*fs_devices
;
72 while (!list_empty(&fs_uuids
)) {
73 fs_devices
= list_entry(fs_uuids
.next
,
74 struct btrfs_fs_devices
, list
);
75 list_del(&fs_devices
->list
);
76 free_fs_devices(fs_devices
);
81 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
84 struct btrfs_device
*dev
;
86 list_for_each_entry(dev
, head
, dev_list
) {
87 if (dev
->devid
== devid
&&
88 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
95 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
97 struct btrfs_fs_devices
*fs_devices
;
99 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
100 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
106 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
107 struct bio
*head
, struct bio
*tail
)
110 struct bio
*old_head
;
112 old_head
= pending_bios
->head
;
113 pending_bios
->head
= head
;
114 if (pending_bios
->tail
)
115 tail
->bi_next
= old_head
;
117 pending_bios
->tail
= tail
;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
134 struct backing_dev_info
*bdi
;
135 struct btrfs_fs_info
*fs_info
;
136 struct btrfs_pending_bios
*pending_bios
;
140 unsigned long num_run
;
141 unsigned long batch_run
= 0;
143 unsigned long last_waited
= 0;
145 int sync_pending
= 0;
146 struct blk_plug plug
;
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
154 blk_start_plug(&plug
);
156 bdi
= blk_get_backing_dev_info(device
->bdev
);
157 fs_info
= device
->dev_root
->fs_info
;
158 limit
= btrfs_async_submit_limit(fs_info
);
159 limit
= limit
* 2 / 3;
162 spin_lock(&device
->io_lock
);
167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
172 if (!force_reg
&& device
->pending_sync_bios
.head
) {
173 pending_bios
= &device
->pending_sync_bios
;
176 pending_bios
= &device
->pending_bios
;
180 pending
= pending_bios
->head
;
181 tail
= pending_bios
->tail
;
182 WARN_ON(pending
&& !tail
);
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
189 * device->running_pending is used to synchronize with the
192 if (device
->pending_sync_bios
.head
== NULL
&&
193 device
->pending_bios
.head
== NULL
) {
195 device
->running_pending
= 0;
198 device
->running_pending
= 1;
201 pending_bios
->head
= NULL
;
202 pending_bios
->tail
= NULL
;
204 spin_unlock(&device
->io_lock
);
209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
213 pending_bios
!= &device
->pending_sync_bios
&&
214 device
->pending_sync_bios
.head
) ||
215 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
216 device
->pending_bios
.head
)) {
217 spin_lock(&device
->io_lock
);
218 requeue_list(pending_bios
, pending
, tail
);
223 pending
= pending
->bi_next
;
225 atomic_dec(&fs_info
->nr_async_bios
);
227 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
228 waitqueue_active(&fs_info
->async_submit_wait
))
229 wake_up(&fs_info
->async_submit_wait
);
231 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
241 if (pending_bios
== &device
->pending_sync_bios
) {
243 } else if (sync_pending
) {
244 blk_finish_plug(&plug
);
245 blk_start_plug(&plug
);
249 submit_bio(cur
->bi_rw
, cur
);
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
260 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
261 fs_info
->fs_devices
->open_devices
> 1) {
262 struct io_context
*ioc
;
264 ioc
= current
->io_context
;
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
275 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
276 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
278 ioc
->last_waited
== last_waited
)) {
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
285 last_waited
= ioc
->last_waited
;
290 spin_lock(&device
->io_lock
);
291 requeue_list(pending_bios
, pending
, tail
);
292 device
->running_pending
= 1;
294 spin_unlock(&device
->io_lock
);
295 btrfs_requeue_work(&device
->work
);
304 spin_lock(&device
->io_lock
);
305 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
307 spin_unlock(&device
->io_lock
);
310 blk_finish_plug(&plug
);
314 static void pending_bios_fn(struct btrfs_work
*work
)
316 struct btrfs_device
*device
;
318 device
= container_of(work
, struct btrfs_device
, work
);
319 run_scheduled_bios(device
);
322 static noinline
int device_list_add(const char *path
,
323 struct btrfs_super_block
*disk_super
,
324 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
326 struct btrfs_device
*device
;
327 struct btrfs_fs_devices
*fs_devices
;
328 u64 found_transid
= btrfs_super_generation(disk_super
);
331 fs_devices
= find_fsid(disk_super
->fsid
);
333 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
336 INIT_LIST_HEAD(&fs_devices
->devices
);
337 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
338 list_add(&fs_devices
->list
, &fs_uuids
);
339 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
340 fs_devices
->latest_devid
= devid
;
341 fs_devices
->latest_trans
= found_transid
;
342 mutex_init(&fs_devices
->device_list_mutex
);
345 device
= __find_device(&fs_devices
->devices
, devid
,
346 disk_super
->dev_item
.uuid
);
349 if (fs_devices
->opened
)
352 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
354 /* we can safely leave the fs_devices entry around */
357 device
->devid
= devid
;
358 device
->work
.func
= pending_bios_fn
;
359 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
361 spin_lock_init(&device
->io_lock
);
362 device
->name
= kstrdup(path
, GFP_NOFS
);
367 INIT_LIST_HEAD(&device
->dev_alloc_list
);
369 mutex_lock(&fs_devices
->device_list_mutex
);
370 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
371 mutex_unlock(&fs_devices
->device_list_mutex
);
373 device
->fs_devices
= fs_devices
;
374 fs_devices
->num_devices
++;
375 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
376 name
= kstrdup(path
, GFP_NOFS
);
381 if (device
->missing
) {
382 fs_devices
->missing_devices
--;
387 if (found_transid
> fs_devices
->latest_trans
) {
388 fs_devices
->latest_devid
= devid
;
389 fs_devices
->latest_trans
= found_transid
;
391 *fs_devices_ret
= fs_devices
;
395 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
397 struct btrfs_fs_devices
*fs_devices
;
398 struct btrfs_device
*device
;
399 struct btrfs_device
*orig_dev
;
401 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
403 return ERR_PTR(-ENOMEM
);
405 INIT_LIST_HEAD(&fs_devices
->devices
);
406 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
407 INIT_LIST_HEAD(&fs_devices
->list
);
408 mutex_init(&fs_devices
->device_list_mutex
);
409 fs_devices
->latest_devid
= orig
->latest_devid
;
410 fs_devices
->latest_trans
= orig
->latest_trans
;
411 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
413 /* We have held the volume lock, it is safe to get the devices. */
414 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
415 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
419 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
425 device
->devid
= orig_dev
->devid
;
426 device
->work
.func
= pending_bios_fn
;
427 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
428 spin_lock_init(&device
->io_lock
);
429 INIT_LIST_HEAD(&device
->dev_list
);
430 INIT_LIST_HEAD(&device
->dev_alloc_list
);
432 list_add(&device
->dev_list
, &fs_devices
->devices
);
433 device
->fs_devices
= fs_devices
;
434 fs_devices
->num_devices
++;
438 free_fs_devices(fs_devices
);
439 return ERR_PTR(-ENOMEM
);
442 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
444 struct btrfs_device
*device
, *next
;
446 mutex_lock(&uuid_mutex
);
448 /* This is the initialized path, it is safe to release the devices. */
449 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
450 if (device
->in_fs_metadata
)
454 blkdev_put(device
->bdev
, device
->mode
);
456 fs_devices
->open_devices
--;
458 if (device
->writeable
) {
459 list_del_init(&device
->dev_alloc_list
);
460 device
->writeable
= 0;
461 fs_devices
->rw_devices
--;
463 list_del_init(&device
->dev_list
);
464 fs_devices
->num_devices
--;
469 if (fs_devices
->seed
) {
470 fs_devices
= fs_devices
->seed
;
474 mutex_unlock(&uuid_mutex
);
478 static void __free_device(struct work_struct
*work
)
480 struct btrfs_device
*device
;
482 device
= container_of(work
, struct btrfs_device
, rcu_work
);
485 blkdev_put(device
->bdev
, device
->mode
);
491 static void free_device(struct rcu_head
*head
)
493 struct btrfs_device
*device
;
495 device
= container_of(head
, struct btrfs_device
, rcu
);
497 INIT_WORK(&device
->rcu_work
, __free_device
);
498 schedule_work(&device
->rcu_work
);
501 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
503 struct btrfs_device
*device
;
505 if (--fs_devices
->opened
> 0)
508 mutex_lock(&fs_devices
->device_list_mutex
);
509 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
510 struct btrfs_device
*new_device
;
513 fs_devices
->open_devices
--;
515 if (device
->writeable
) {
516 list_del_init(&device
->dev_alloc_list
);
517 fs_devices
->rw_devices
--;
520 if (device
->can_discard
)
521 fs_devices
->num_can_discard
--;
523 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
525 memcpy(new_device
, device
, sizeof(*new_device
));
526 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
527 BUG_ON(device
->name
&& !new_device
->name
);
528 new_device
->bdev
= NULL
;
529 new_device
->writeable
= 0;
530 new_device
->in_fs_metadata
= 0;
531 new_device
->can_discard
= 0;
532 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
534 call_rcu(&device
->rcu
, free_device
);
536 mutex_unlock(&fs_devices
->device_list_mutex
);
538 WARN_ON(fs_devices
->open_devices
);
539 WARN_ON(fs_devices
->rw_devices
);
540 fs_devices
->opened
= 0;
541 fs_devices
->seeding
= 0;
546 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
548 struct btrfs_fs_devices
*seed_devices
= NULL
;
551 mutex_lock(&uuid_mutex
);
552 ret
= __btrfs_close_devices(fs_devices
);
553 if (!fs_devices
->opened
) {
554 seed_devices
= fs_devices
->seed
;
555 fs_devices
->seed
= NULL
;
557 mutex_unlock(&uuid_mutex
);
559 while (seed_devices
) {
560 fs_devices
= seed_devices
;
561 seed_devices
= fs_devices
->seed
;
562 __btrfs_close_devices(fs_devices
);
563 free_fs_devices(fs_devices
);
568 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
569 fmode_t flags
, void *holder
)
571 struct request_queue
*q
;
572 struct block_device
*bdev
;
573 struct list_head
*head
= &fs_devices
->devices
;
574 struct btrfs_device
*device
;
575 struct block_device
*latest_bdev
= NULL
;
576 struct buffer_head
*bh
;
577 struct btrfs_super_block
*disk_super
;
578 u64 latest_devid
= 0;
579 u64 latest_transid
= 0;
586 list_for_each_entry(device
, head
, dev_list
) {
592 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
594 printk(KERN_INFO
"open %s failed\n", device
->name
);
597 set_blocksize(bdev
, 4096);
599 bh
= btrfs_read_dev_super(bdev
);
605 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
606 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
607 if (devid
!= device
->devid
)
610 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
614 device
->generation
= btrfs_super_generation(disk_super
);
615 if (!latest_transid
|| device
->generation
> latest_transid
) {
616 latest_devid
= devid
;
617 latest_transid
= device
->generation
;
621 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
622 device
->writeable
= 0;
624 device
->writeable
= !bdev_read_only(bdev
);
628 q
= bdev_get_queue(bdev
);
629 if (blk_queue_discard(q
)) {
630 device
->can_discard
= 1;
631 fs_devices
->num_can_discard
++;
635 device
->in_fs_metadata
= 0;
636 device
->mode
= flags
;
638 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
639 fs_devices
->rotating
= 1;
641 fs_devices
->open_devices
++;
642 if (device
->writeable
) {
643 fs_devices
->rw_devices
++;
644 list_add(&device
->dev_alloc_list
,
645 &fs_devices
->alloc_list
);
653 blkdev_put(bdev
, flags
);
657 if (fs_devices
->open_devices
== 0) {
661 fs_devices
->seeding
= seeding
;
662 fs_devices
->opened
= 1;
663 fs_devices
->latest_bdev
= latest_bdev
;
664 fs_devices
->latest_devid
= latest_devid
;
665 fs_devices
->latest_trans
= latest_transid
;
666 fs_devices
->total_rw_bytes
= 0;
671 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
672 fmode_t flags
, void *holder
)
676 mutex_lock(&uuid_mutex
);
677 if (fs_devices
->opened
) {
678 fs_devices
->opened
++;
681 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
683 mutex_unlock(&uuid_mutex
);
687 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
688 struct btrfs_fs_devices
**fs_devices_ret
)
690 struct btrfs_super_block
*disk_super
;
691 struct block_device
*bdev
;
692 struct buffer_head
*bh
;
697 mutex_lock(&uuid_mutex
);
700 bdev
= blkdev_get_by_path(path
, flags
, holder
);
707 ret
= set_blocksize(bdev
, 4096);
710 bh
= btrfs_read_dev_super(bdev
);
715 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
716 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
717 transid
= btrfs_super_generation(disk_super
);
718 if (disk_super
->label
[0])
719 printk(KERN_INFO
"device label %s ", disk_super
->label
);
721 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
722 printk(KERN_CONT
"devid %llu transid %llu %s\n",
723 (unsigned long long)devid
, (unsigned long long)transid
, path
);
724 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
728 blkdev_put(bdev
, flags
);
730 mutex_unlock(&uuid_mutex
);
734 /* helper to account the used device space in the range */
735 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
736 u64 end
, u64
*length
)
738 struct btrfs_key key
;
739 struct btrfs_root
*root
= device
->dev_root
;
740 struct btrfs_dev_extent
*dev_extent
;
741 struct btrfs_path
*path
;
745 struct extent_buffer
*l
;
749 if (start
>= device
->total_bytes
)
752 path
= btrfs_alloc_path();
757 key
.objectid
= device
->devid
;
759 key
.type
= BTRFS_DEV_EXTENT_KEY
;
761 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
765 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
772 slot
= path
->slots
[0];
773 if (slot
>= btrfs_header_nritems(l
)) {
774 ret
= btrfs_next_leaf(root
, path
);
782 btrfs_item_key_to_cpu(l
, &key
, slot
);
784 if (key
.objectid
< device
->devid
)
787 if (key
.objectid
> device
->devid
)
790 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
793 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
794 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
796 if (key
.offset
<= start
&& extent_end
> end
) {
797 *length
= end
- start
+ 1;
799 } else if (key
.offset
<= start
&& extent_end
> start
)
800 *length
+= extent_end
- start
;
801 else if (key
.offset
> start
&& extent_end
<= end
)
802 *length
+= extent_end
- key
.offset
;
803 else if (key
.offset
> start
&& key
.offset
<= end
) {
804 *length
+= end
- key
.offset
+ 1;
806 } else if (key
.offset
> end
)
814 btrfs_free_path(path
);
819 * find_free_dev_extent - find free space in the specified device
820 * @trans: transaction handler
821 * @device: the device which we search the free space in
822 * @num_bytes: the size of the free space that we need
823 * @start: store the start of the free space.
824 * @len: the size of the free space. that we find, or the size of the max
825 * free space if we don't find suitable free space
827 * this uses a pretty simple search, the expectation is that it is
828 * called very infrequently and that a given device has a small number
831 * @start is used to store the start of the free space if we find. But if we
832 * don't find suitable free space, it will be used to store the start position
833 * of the max free space.
835 * @len is used to store the size of the free space that we find.
836 * But if we don't find suitable free space, it is used to store the size of
837 * the max free space.
839 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
840 struct btrfs_device
*device
, u64 num_bytes
,
841 u64
*start
, u64
*len
)
843 struct btrfs_key key
;
844 struct btrfs_root
*root
= device
->dev_root
;
845 struct btrfs_dev_extent
*dev_extent
;
846 struct btrfs_path
*path
;
852 u64 search_end
= device
->total_bytes
;
855 struct extent_buffer
*l
;
857 /* FIXME use last free of some kind */
859 /* we don't want to overwrite the superblock on the drive,
860 * so we make sure to start at an offset of at least 1MB
862 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
864 max_hole_start
= search_start
;
868 if (search_start
>= search_end
) {
873 path
= btrfs_alloc_path();
880 key
.objectid
= device
->devid
;
881 key
.offset
= search_start
;
882 key
.type
= BTRFS_DEV_EXTENT_KEY
;
884 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
888 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
895 slot
= path
->slots
[0];
896 if (slot
>= btrfs_header_nritems(l
)) {
897 ret
= btrfs_next_leaf(root
, path
);
905 btrfs_item_key_to_cpu(l
, &key
, slot
);
907 if (key
.objectid
< device
->devid
)
910 if (key
.objectid
> device
->devid
)
913 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
916 if (key
.offset
> search_start
) {
917 hole_size
= key
.offset
- search_start
;
919 if (hole_size
> max_hole_size
) {
920 max_hole_start
= search_start
;
921 max_hole_size
= hole_size
;
925 * If this free space is greater than which we need,
926 * it must be the max free space that we have found
927 * until now, so max_hole_start must point to the start
928 * of this free space and the length of this free space
929 * is stored in max_hole_size. Thus, we return
930 * max_hole_start and max_hole_size and go back to the
933 if (hole_size
>= num_bytes
) {
939 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
940 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
942 if (extent_end
> search_start
)
943 search_start
= extent_end
;
950 * At this point, search_start should be the end of
951 * allocated dev extents, and when shrinking the device,
952 * search_end may be smaller than search_start.
954 if (search_end
> search_start
)
955 hole_size
= search_end
- search_start
;
957 if (hole_size
> max_hole_size
) {
958 max_hole_start
= search_start
;
959 max_hole_size
= hole_size
;
963 if (hole_size
< num_bytes
)
969 btrfs_free_path(path
);
971 *start
= max_hole_start
;
973 *len
= max_hole_size
;
977 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
978 struct btrfs_device
*device
,
982 struct btrfs_path
*path
;
983 struct btrfs_root
*root
= device
->dev_root
;
984 struct btrfs_key key
;
985 struct btrfs_key found_key
;
986 struct extent_buffer
*leaf
= NULL
;
987 struct btrfs_dev_extent
*extent
= NULL
;
989 path
= btrfs_alloc_path();
993 key
.objectid
= device
->devid
;
995 key
.type
= BTRFS_DEV_EXTENT_KEY
;
997 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
999 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1000 BTRFS_DEV_EXTENT_KEY
);
1003 leaf
= path
->nodes
[0];
1004 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1005 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1006 struct btrfs_dev_extent
);
1007 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1008 btrfs_dev_extent_length(leaf
, extent
) < start
);
1009 } else if (ret
== 0) {
1010 leaf
= path
->nodes
[0];
1011 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1012 struct btrfs_dev_extent
);
1016 if (device
->bytes_used
> 0)
1017 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
1018 ret
= btrfs_del_item(trans
, root
, path
);
1021 btrfs_free_path(path
);
1025 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1026 struct btrfs_device
*device
,
1027 u64 chunk_tree
, u64 chunk_objectid
,
1028 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1031 struct btrfs_path
*path
;
1032 struct btrfs_root
*root
= device
->dev_root
;
1033 struct btrfs_dev_extent
*extent
;
1034 struct extent_buffer
*leaf
;
1035 struct btrfs_key key
;
1037 WARN_ON(!device
->in_fs_metadata
);
1038 path
= btrfs_alloc_path();
1042 key
.objectid
= device
->devid
;
1044 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1045 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1049 leaf
= path
->nodes
[0];
1050 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1051 struct btrfs_dev_extent
);
1052 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1053 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1054 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1056 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1057 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1060 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1061 btrfs_mark_buffer_dirty(leaf
);
1062 btrfs_free_path(path
);
1066 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1067 u64 objectid
, u64
*offset
)
1069 struct btrfs_path
*path
;
1071 struct btrfs_key key
;
1072 struct btrfs_chunk
*chunk
;
1073 struct btrfs_key found_key
;
1075 path
= btrfs_alloc_path();
1079 key
.objectid
= objectid
;
1080 key
.offset
= (u64
)-1;
1081 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1083 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1089 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1093 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1095 if (found_key
.objectid
!= objectid
)
1098 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1099 struct btrfs_chunk
);
1100 *offset
= found_key
.offset
+
1101 btrfs_chunk_length(path
->nodes
[0], chunk
);
1106 btrfs_free_path(path
);
1110 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1113 struct btrfs_key key
;
1114 struct btrfs_key found_key
;
1115 struct btrfs_path
*path
;
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
= (u64
)-1;
1127 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1133 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1134 BTRFS_DEV_ITEM_KEY
);
1138 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1140 *objectid
= found_key
.offset
+ 1;
1144 btrfs_free_path(path
);
1149 * the device information is stored in the chunk root
1150 * the btrfs_device struct should be fully filled in
1152 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1153 struct btrfs_root
*root
,
1154 struct btrfs_device
*device
)
1157 struct btrfs_path
*path
;
1158 struct btrfs_dev_item
*dev_item
;
1159 struct extent_buffer
*leaf
;
1160 struct btrfs_key key
;
1163 root
= root
->fs_info
->chunk_root
;
1165 path
= btrfs_alloc_path();
1169 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1170 key
.type
= BTRFS_DEV_ITEM_KEY
;
1171 key
.offset
= device
->devid
;
1173 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1178 leaf
= path
->nodes
[0];
1179 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1181 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1182 btrfs_set_device_generation(leaf
, dev_item
, 0);
1183 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1184 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1185 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1186 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1187 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1188 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1189 btrfs_set_device_group(leaf
, dev_item
, 0);
1190 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1191 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1192 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1194 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1195 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1196 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1197 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1198 btrfs_mark_buffer_dirty(leaf
);
1202 btrfs_free_path(path
);
1206 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1207 struct btrfs_device
*device
)
1210 struct btrfs_path
*path
;
1211 struct btrfs_key key
;
1212 struct btrfs_trans_handle
*trans
;
1214 root
= root
->fs_info
->chunk_root
;
1216 path
= btrfs_alloc_path();
1220 trans
= btrfs_start_transaction(root
, 0);
1221 if (IS_ERR(trans
)) {
1222 btrfs_free_path(path
);
1223 return PTR_ERR(trans
);
1225 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1226 key
.type
= BTRFS_DEV_ITEM_KEY
;
1227 key
.offset
= device
->devid
;
1230 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1239 ret
= btrfs_del_item(trans
, root
, path
);
1243 btrfs_free_path(path
);
1244 unlock_chunks(root
);
1245 btrfs_commit_transaction(trans
, root
);
1249 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1251 struct btrfs_device
*device
;
1252 struct btrfs_device
*next_device
;
1253 struct block_device
*bdev
;
1254 struct buffer_head
*bh
= NULL
;
1255 struct btrfs_super_block
*disk_super
;
1256 struct btrfs_fs_devices
*cur_devices
;
1262 bool clear_super
= false;
1264 mutex_lock(&uuid_mutex
);
1265 mutex_lock(&root
->fs_info
->volume_mutex
);
1267 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1268 root
->fs_info
->avail_system_alloc_bits
|
1269 root
->fs_info
->avail_metadata_alloc_bits
;
1271 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1272 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1273 printk(KERN_ERR
"btrfs: unable to go below four devices "
1279 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1280 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1281 printk(KERN_ERR
"btrfs: unable to go below two "
1282 "devices on raid1\n");
1287 if (strcmp(device_path
, "missing") == 0) {
1288 struct list_head
*devices
;
1289 struct btrfs_device
*tmp
;
1292 devices
= &root
->fs_info
->fs_devices
->devices
;
1294 * It is safe to read the devices since the volume_mutex
1297 list_for_each_entry(tmp
, devices
, dev_list
) {
1298 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1307 printk(KERN_ERR
"btrfs: no missing devices found to "
1312 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1313 root
->fs_info
->bdev_holder
);
1315 ret
= PTR_ERR(bdev
);
1319 set_blocksize(bdev
, 4096);
1320 bh
= btrfs_read_dev_super(bdev
);
1325 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1326 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1327 dev_uuid
= disk_super
->dev_item
.uuid
;
1328 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1336 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1337 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1343 if (device
->writeable
) {
1345 list_del_init(&device
->dev_alloc_list
);
1346 unlock_chunks(root
);
1347 root
->fs_info
->fs_devices
->rw_devices
--;
1351 ret
= btrfs_shrink_device(device
, 0);
1355 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1359 device
->in_fs_metadata
= 0;
1360 btrfs_scrub_cancel_dev(root
, device
);
1363 * the device list mutex makes sure that we don't change
1364 * the device list while someone else is writing out all
1365 * the device supers.
1368 cur_devices
= device
->fs_devices
;
1369 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1370 list_del_rcu(&device
->dev_list
);
1372 device
->fs_devices
->num_devices
--;
1374 if (device
->missing
)
1375 root
->fs_info
->fs_devices
->missing_devices
--;
1377 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1378 struct btrfs_device
, dev_list
);
1379 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1380 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1381 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1382 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1385 device
->fs_devices
->open_devices
--;
1387 call_rcu(&device
->rcu
, free_device
);
1388 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1390 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1391 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1393 if (cur_devices
->open_devices
== 0) {
1394 struct btrfs_fs_devices
*fs_devices
;
1395 fs_devices
= root
->fs_info
->fs_devices
;
1396 while (fs_devices
) {
1397 if (fs_devices
->seed
== cur_devices
)
1399 fs_devices
= fs_devices
->seed
;
1401 fs_devices
->seed
= cur_devices
->seed
;
1402 cur_devices
->seed
= NULL
;
1404 __btrfs_close_devices(cur_devices
);
1405 unlock_chunks(root
);
1406 free_fs_devices(cur_devices
);
1410 * at this point, the device is zero sized. We want to
1411 * remove it from the devices list and zero out the old super
1414 /* make sure this device isn't detected as part of
1417 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1418 set_buffer_dirty(bh
);
1419 sync_dirty_buffer(bh
);
1428 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1430 mutex_unlock(&root
->fs_info
->volume_mutex
);
1431 mutex_unlock(&uuid_mutex
);
1434 if (device
->writeable
) {
1436 list_add(&device
->dev_alloc_list
,
1437 &root
->fs_info
->fs_devices
->alloc_list
);
1438 unlock_chunks(root
);
1439 root
->fs_info
->fs_devices
->rw_devices
++;
1445 * does all the dirty work required for changing file system's UUID.
1447 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1448 struct btrfs_root
*root
)
1450 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1451 struct btrfs_fs_devices
*old_devices
;
1452 struct btrfs_fs_devices
*seed_devices
;
1453 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1454 struct btrfs_device
*device
;
1457 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1458 if (!fs_devices
->seeding
)
1461 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1465 old_devices
= clone_fs_devices(fs_devices
);
1466 if (IS_ERR(old_devices
)) {
1467 kfree(seed_devices
);
1468 return PTR_ERR(old_devices
);
1471 list_add(&old_devices
->list
, &fs_uuids
);
1473 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1474 seed_devices
->opened
= 1;
1475 INIT_LIST_HEAD(&seed_devices
->devices
);
1476 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1477 mutex_init(&seed_devices
->device_list_mutex
);
1479 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1480 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1482 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1484 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1485 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1486 device
->fs_devices
= seed_devices
;
1489 fs_devices
->seeding
= 0;
1490 fs_devices
->num_devices
= 0;
1491 fs_devices
->open_devices
= 0;
1492 fs_devices
->seed
= seed_devices
;
1494 generate_random_uuid(fs_devices
->fsid
);
1495 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1496 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1497 super_flags
= btrfs_super_flags(disk_super
) &
1498 ~BTRFS_SUPER_FLAG_SEEDING
;
1499 btrfs_set_super_flags(disk_super
, super_flags
);
1505 * strore the expected generation for seed devices in device items.
1507 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1508 struct btrfs_root
*root
)
1510 struct btrfs_path
*path
;
1511 struct extent_buffer
*leaf
;
1512 struct btrfs_dev_item
*dev_item
;
1513 struct btrfs_device
*device
;
1514 struct btrfs_key key
;
1515 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1516 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1520 path
= btrfs_alloc_path();
1524 root
= root
->fs_info
->chunk_root
;
1525 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1527 key
.type
= BTRFS_DEV_ITEM_KEY
;
1530 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1534 leaf
= path
->nodes
[0];
1536 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1537 ret
= btrfs_next_leaf(root
, path
);
1542 leaf
= path
->nodes
[0];
1543 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1544 btrfs_release_path(path
);
1548 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1549 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1550 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1553 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1554 struct btrfs_dev_item
);
1555 devid
= btrfs_device_id(leaf
, dev_item
);
1556 read_extent_buffer(leaf
, dev_uuid
,
1557 (unsigned long)btrfs_device_uuid(dev_item
),
1559 read_extent_buffer(leaf
, fs_uuid
,
1560 (unsigned long)btrfs_device_fsid(dev_item
),
1562 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1565 if (device
->fs_devices
->seeding
) {
1566 btrfs_set_device_generation(leaf
, dev_item
,
1567 device
->generation
);
1568 btrfs_mark_buffer_dirty(leaf
);
1576 btrfs_free_path(path
);
1580 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1582 struct request_queue
*q
;
1583 struct btrfs_trans_handle
*trans
;
1584 struct btrfs_device
*device
;
1585 struct block_device
*bdev
;
1586 struct list_head
*devices
;
1587 struct super_block
*sb
= root
->fs_info
->sb
;
1589 int seeding_dev
= 0;
1592 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1595 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1596 root
->fs_info
->bdev_holder
);
1598 return PTR_ERR(bdev
);
1600 if (root
->fs_info
->fs_devices
->seeding
) {
1602 down_write(&sb
->s_umount
);
1603 mutex_lock(&uuid_mutex
);
1606 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1607 mutex_lock(&root
->fs_info
->volume_mutex
);
1609 devices
= &root
->fs_info
->fs_devices
->devices
;
1611 * we have the volume lock, so we don't need the extra
1612 * device list mutex while reading the list here.
1614 list_for_each_entry(device
, devices
, dev_list
) {
1615 if (device
->bdev
== bdev
) {
1621 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1623 /* we can safely leave the fs_devices entry around */
1628 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1629 if (!device
->name
) {
1635 ret
= find_next_devid(root
, &device
->devid
);
1637 kfree(device
->name
);
1642 trans
= btrfs_start_transaction(root
, 0);
1643 if (IS_ERR(trans
)) {
1644 kfree(device
->name
);
1646 ret
= PTR_ERR(trans
);
1652 q
= bdev_get_queue(bdev
);
1653 if (blk_queue_discard(q
))
1654 device
->can_discard
= 1;
1655 device
->writeable
= 1;
1656 device
->work
.func
= pending_bios_fn
;
1657 generate_random_uuid(device
->uuid
);
1658 spin_lock_init(&device
->io_lock
);
1659 device
->generation
= trans
->transid
;
1660 device
->io_width
= root
->sectorsize
;
1661 device
->io_align
= root
->sectorsize
;
1662 device
->sector_size
= root
->sectorsize
;
1663 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1664 device
->disk_total_bytes
= device
->total_bytes
;
1665 device
->dev_root
= root
->fs_info
->dev_root
;
1666 device
->bdev
= bdev
;
1667 device
->in_fs_metadata
= 1;
1668 device
->mode
= FMODE_EXCL
;
1669 set_blocksize(device
->bdev
, 4096);
1672 sb
->s_flags
&= ~MS_RDONLY
;
1673 ret
= btrfs_prepare_sprout(trans
, root
);
1677 device
->fs_devices
= root
->fs_info
->fs_devices
;
1680 * we don't want write_supers to jump in here with our device
1683 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1684 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1685 list_add(&device
->dev_alloc_list
,
1686 &root
->fs_info
->fs_devices
->alloc_list
);
1687 root
->fs_info
->fs_devices
->num_devices
++;
1688 root
->fs_info
->fs_devices
->open_devices
++;
1689 root
->fs_info
->fs_devices
->rw_devices
++;
1690 if (device
->can_discard
)
1691 root
->fs_info
->fs_devices
->num_can_discard
++;
1692 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1694 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1695 root
->fs_info
->fs_devices
->rotating
= 1;
1697 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1698 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1699 total_bytes
+ device
->total_bytes
);
1701 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1702 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1704 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1707 ret
= init_first_rw_device(trans
, root
, device
);
1709 ret
= btrfs_finish_sprout(trans
, root
);
1712 ret
= btrfs_add_device(trans
, root
, device
);
1716 * we've got more storage, clear any full flags on the space
1719 btrfs_clear_space_info_full(root
->fs_info
);
1721 unlock_chunks(root
);
1722 btrfs_commit_transaction(trans
, root
);
1725 mutex_unlock(&uuid_mutex
);
1726 up_write(&sb
->s_umount
);
1728 ret
= btrfs_relocate_sys_chunks(root
);
1732 mutex_unlock(&root
->fs_info
->volume_mutex
);
1735 blkdev_put(bdev
, FMODE_EXCL
);
1737 mutex_unlock(&uuid_mutex
);
1738 up_write(&sb
->s_umount
);
1743 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1744 struct btrfs_device
*device
)
1747 struct btrfs_path
*path
;
1748 struct btrfs_root
*root
;
1749 struct btrfs_dev_item
*dev_item
;
1750 struct extent_buffer
*leaf
;
1751 struct btrfs_key key
;
1753 root
= device
->dev_root
->fs_info
->chunk_root
;
1755 path
= btrfs_alloc_path();
1759 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1760 key
.type
= BTRFS_DEV_ITEM_KEY
;
1761 key
.offset
= device
->devid
;
1763 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1772 leaf
= path
->nodes
[0];
1773 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1775 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1776 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1777 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1778 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1779 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1780 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1781 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1782 btrfs_mark_buffer_dirty(leaf
);
1785 btrfs_free_path(path
);
1789 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1790 struct btrfs_device
*device
, u64 new_size
)
1792 struct btrfs_super_block
*super_copy
=
1793 &device
->dev_root
->fs_info
->super_copy
;
1794 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1795 u64 diff
= new_size
- device
->total_bytes
;
1797 if (!device
->writeable
)
1799 if (new_size
<= device
->total_bytes
)
1802 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1803 device
->fs_devices
->total_rw_bytes
+= diff
;
1805 device
->total_bytes
= new_size
;
1806 device
->disk_total_bytes
= new_size
;
1807 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1809 return btrfs_update_device(trans
, device
);
1812 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1813 struct btrfs_device
*device
, u64 new_size
)
1816 lock_chunks(device
->dev_root
);
1817 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1818 unlock_chunks(device
->dev_root
);
1822 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1823 struct btrfs_root
*root
,
1824 u64 chunk_tree
, u64 chunk_objectid
,
1828 struct btrfs_path
*path
;
1829 struct btrfs_key key
;
1831 root
= root
->fs_info
->chunk_root
;
1832 path
= btrfs_alloc_path();
1836 key
.objectid
= chunk_objectid
;
1837 key
.offset
= chunk_offset
;
1838 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1840 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1843 ret
= btrfs_del_item(trans
, root
, path
);
1845 btrfs_free_path(path
);
1849 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1852 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1853 struct btrfs_disk_key
*disk_key
;
1854 struct btrfs_chunk
*chunk
;
1861 struct btrfs_key key
;
1863 array_size
= btrfs_super_sys_array_size(super_copy
);
1865 ptr
= super_copy
->sys_chunk_array
;
1868 while (cur
< array_size
) {
1869 disk_key
= (struct btrfs_disk_key
*)ptr
;
1870 btrfs_disk_key_to_cpu(&key
, disk_key
);
1872 len
= sizeof(*disk_key
);
1874 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1875 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1876 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1877 len
+= btrfs_chunk_item_size(num_stripes
);
1882 if (key
.objectid
== chunk_objectid
&&
1883 key
.offset
== chunk_offset
) {
1884 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1886 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1895 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1896 u64 chunk_tree
, u64 chunk_objectid
,
1899 struct extent_map_tree
*em_tree
;
1900 struct btrfs_root
*extent_root
;
1901 struct btrfs_trans_handle
*trans
;
1902 struct extent_map
*em
;
1903 struct map_lookup
*map
;
1907 root
= root
->fs_info
->chunk_root
;
1908 extent_root
= root
->fs_info
->extent_root
;
1909 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1911 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1915 /* step one, relocate all the extents inside this chunk */
1916 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1920 trans
= btrfs_start_transaction(root
, 0);
1921 BUG_ON(IS_ERR(trans
));
1926 * step two, delete the device extents and the
1927 * chunk tree entries
1929 read_lock(&em_tree
->lock
);
1930 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1931 read_unlock(&em_tree
->lock
);
1933 BUG_ON(em
->start
> chunk_offset
||
1934 em
->start
+ em
->len
< chunk_offset
);
1935 map
= (struct map_lookup
*)em
->bdev
;
1937 for (i
= 0; i
< map
->num_stripes
; i
++) {
1938 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1939 map
->stripes
[i
].physical
);
1942 if (map
->stripes
[i
].dev
) {
1943 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1947 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1952 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1954 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1955 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1959 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1962 write_lock(&em_tree
->lock
);
1963 remove_extent_mapping(em_tree
, em
);
1964 write_unlock(&em_tree
->lock
);
1969 /* once for the tree */
1970 free_extent_map(em
);
1972 free_extent_map(em
);
1974 unlock_chunks(root
);
1975 btrfs_end_transaction(trans
, root
);
1979 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1981 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1982 struct btrfs_path
*path
;
1983 struct extent_buffer
*leaf
;
1984 struct btrfs_chunk
*chunk
;
1985 struct btrfs_key key
;
1986 struct btrfs_key found_key
;
1987 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1989 bool retried
= false;
1993 path
= btrfs_alloc_path();
1998 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1999 key
.offset
= (u64
)-1;
2000 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2003 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2008 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2015 leaf
= path
->nodes
[0];
2016 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2018 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2019 struct btrfs_chunk
);
2020 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2021 btrfs_release_path(path
);
2023 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2024 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2033 if (found_key
.offset
== 0)
2035 key
.offset
= found_key
.offset
- 1;
2038 if (failed
&& !retried
) {
2042 } else if (failed
&& retried
) {
2047 btrfs_free_path(path
);
2051 static u64
div_factor(u64 num
, int factor
)
2060 int btrfs_balance(struct btrfs_root
*dev_root
)
2063 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2064 struct btrfs_device
*device
;
2067 struct btrfs_path
*path
;
2068 struct btrfs_key key
;
2069 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2070 struct btrfs_trans_handle
*trans
;
2071 struct btrfs_key found_key
;
2073 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2076 if (!capable(CAP_SYS_ADMIN
))
2079 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2080 dev_root
= dev_root
->fs_info
->dev_root
;
2082 /* step one make some room on all the devices */
2083 list_for_each_entry(device
, devices
, dev_list
) {
2084 old_size
= device
->total_bytes
;
2085 size_to_free
= div_factor(old_size
, 1);
2086 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2087 if (!device
->writeable
||
2088 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2091 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2096 trans
= btrfs_start_transaction(dev_root
, 0);
2097 BUG_ON(IS_ERR(trans
));
2099 ret
= btrfs_grow_device(trans
, device
, old_size
);
2102 btrfs_end_transaction(trans
, dev_root
);
2105 /* step two, relocate all the chunks */
2106 path
= btrfs_alloc_path();
2111 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2112 key
.offset
= (u64
)-1;
2113 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2116 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2121 * this shouldn't happen, it means the last relocate
2127 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2128 BTRFS_CHUNK_ITEM_KEY
);
2132 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2134 if (found_key
.objectid
!= key
.objectid
)
2137 /* chunk zero is special */
2138 if (found_key
.offset
== 0)
2141 btrfs_release_path(path
);
2142 ret
= btrfs_relocate_chunk(chunk_root
,
2143 chunk_root
->root_key
.objectid
,
2146 if (ret
&& ret
!= -ENOSPC
)
2148 key
.offset
= found_key
.offset
- 1;
2152 btrfs_free_path(path
);
2153 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2158 * shrinking a device means finding all of the device extents past
2159 * the new size, and then following the back refs to the chunks.
2160 * The chunk relocation code actually frees the device extent
2162 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2164 struct btrfs_trans_handle
*trans
;
2165 struct btrfs_root
*root
= device
->dev_root
;
2166 struct btrfs_dev_extent
*dev_extent
= NULL
;
2167 struct btrfs_path
*path
;
2175 bool retried
= false;
2176 struct extent_buffer
*l
;
2177 struct btrfs_key key
;
2178 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2179 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2180 u64 old_size
= device
->total_bytes
;
2181 u64 diff
= device
->total_bytes
- new_size
;
2183 if (new_size
>= device
->total_bytes
)
2186 path
= btrfs_alloc_path();
2194 device
->total_bytes
= new_size
;
2195 if (device
->writeable
)
2196 device
->fs_devices
->total_rw_bytes
-= diff
;
2197 unlock_chunks(root
);
2200 key
.objectid
= device
->devid
;
2201 key
.offset
= (u64
)-1;
2202 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2205 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2209 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2214 btrfs_release_path(path
);
2219 slot
= path
->slots
[0];
2220 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2222 if (key
.objectid
!= device
->devid
) {
2223 btrfs_release_path(path
);
2227 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2228 length
= btrfs_dev_extent_length(l
, dev_extent
);
2230 if (key
.offset
+ length
<= new_size
) {
2231 btrfs_release_path(path
);
2235 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2236 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2237 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2238 btrfs_release_path(path
);
2240 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2242 if (ret
&& ret
!= -ENOSPC
)
2249 if (failed
&& !retried
) {
2253 } else if (failed
&& retried
) {
2257 device
->total_bytes
= old_size
;
2258 if (device
->writeable
)
2259 device
->fs_devices
->total_rw_bytes
+= diff
;
2260 unlock_chunks(root
);
2264 /* Shrinking succeeded, else we would be at "done". */
2265 trans
= btrfs_start_transaction(root
, 0);
2266 if (IS_ERR(trans
)) {
2267 ret
= PTR_ERR(trans
);
2273 device
->disk_total_bytes
= new_size
;
2274 /* Now btrfs_update_device() will change the on-disk size. */
2275 ret
= btrfs_update_device(trans
, device
);
2277 unlock_chunks(root
);
2278 btrfs_end_transaction(trans
, root
);
2281 WARN_ON(diff
> old_total
);
2282 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2283 unlock_chunks(root
);
2284 btrfs_end_transaction(trans
, root
);
2286 btrfs_free_path(path
);
2290 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2291 struct btrfs_root
*root
,
2292 struct btrfs_key
*key
,
2293 struct btrfs_chunk
*chunk
, int item_size
)
2295 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2296 struct btrfs_disk_key disk_key
;
2300 array_size
= btrfs_super_sys_array_size(super_copy
);
2301 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2304 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2305 btrfs_cpu_key_to_disk(&disk_key
, key
);
2306 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2307 ptr
+= sizeof(disk_key
);
2308 memcpy(ptr
, chunk
, item_size
);
2309 item_size
+= sizeof(disk_key
);
2310 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2315 * sort the devices in descending order by max_avail, total_avail
2317 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2319 const struct btrfs_device_info
*di_a
= a
;
2320 const struct btrfs_device_info
*di_b
= b
;
2322 if (di_a
->max_avail
> di_b
->max_avail
)
2324 if (di_a
->max_avail
< di_b
->max_avail
)
2326 if (di_a
->total_avail
> di_b
->total_avail
)
2328 if (di_a
->total_avail
< di_b
->total_avail
)
2333 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2334 struct btrfs_root
*extent_root
,
2335 struct map_lookup
**map_ret
,
2336 u64
*num_bytes_out
, u64
*stripe_size_out
,
2337 u64 start
, u64 type
)
2339 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2340 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2341 struct list_head
*cur
;
2342 struct map_lookup
*map
= NULL
;
2343 struct extent_map_tree
*em_tree
;
2344 struct extent_map
*em
;
2345 struct btrfs_device_info
*devices_info
= NULL
;
2347 int num_stripes
; /* total number of stripes to allocate */
2348 int sub_stripes
; /* sub_stripes info for map */
2349 int dev_stripes
; /* stripes per dev */
2350 int devs_max
; /* max devs to use */
2351 int devs_min
; /* min devs needed */
2352 int devs_increment
; /* ndevs has to be a multiple of this */
2353 int ncopies
; /* how many copies to data has */
2355 u64 max_stripe_size
;
2363 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2364 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2366 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2369 if (list_empty(&fs_devices
->alloc_list
))
2376 devs_max
= 0; /* 0 == as many as possible */
2380 * define the properties of each RAID type.
2381 * FIXME: move this to a global table and use it in all RAID
2384 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2388 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2390 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2395 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2404 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2405 max_stripe_size
= 1024 * 1024 * 1024;
2406 max_chunk_size
= 10 * max_stripe_size
;
2407 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2408 max_stripe_size
= 256 * 1024 * 1024;
2409 max_chunk_size
= max_stripe_size
;
2410 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2411 max_stripe_size
= 8 * 1024 * 1024;
2412 max_chunk_size
= 2 * max_stripe_size
;
2414 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
2419 /* we don't want a chunk larger than 10% of writeable space */
2420 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2423 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2428 cur
= fs_devices
->alloc_list
.next
;
2431 * in the first pass through the devices list, we gather information
2432 * about the available holes on each device.
2435 while (cur
!= &fs_devices
->alloc_list
) {
2436 struct btrfs_device
*device
;
2440 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2444 if (!device
->writeable
) {
2446 "btrfs: read-only device in alloc_list\n");
2451 if (!device
->in_fs_metadata
)
2454 if (device
->total_bytes
> device
->bytes_used
)
2455 total_avail
= device
->total_bytes
- device
->bytes_used
;
2459 /* If there is no space on this device, skip it. */
2460 if (total_avail
== 0)
2463 ret
= find_free_dev_extent(trans
, device
,
2464 max_stripe_size
* dev_stripes
,
2465 &dev_offset
, &max_avail
);
2466 if (ret
&& ret
!= -ENOSPC
)
2470 max_avail
= max_stripe_size
* dev_stripes
;
2472 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
2475 devices_info
[ndevs
].dev_offset
= dev_offset
;
2476 devices_info
[ndevs
].max_avail
= max_avail
;
2477 devices_info
[ndevs
].total_avail
= total_avail
;
2478 devices_info
[ndevs
].dev
= device
;
2483 * now sort the devices by hole size / available space
2485 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
2486 btrfs_cmp_device_info
, NULL
);
2488 /* round down to number of usable stripes */
2489 ndevs
-= ndevs
% devs_increment
;
2491 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
2496 if (devs_max
&& ndevs
> devs_max
)
2499 * the primary goal is to maximize the number of stripes, so use as many
2500 * devices as possible, even if the stripes are not maximum sized.
2502 stripe_size
= devices_info
[ndevs
-1].max_avail
;
2503 num_stripes
= ndevs
* dev_stripes
;
2505 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
2506 stripe_size
= max_chunk_size
* ncopies
;
2507 do_div(stripe_size
, num_stripes
);
2510 do_div(stripe_size
, dev_stripes
);
2511 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
2512 stripe_size
*= BTRFS_STRIPE_LEN
;
2514 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2519 map
->num_stripes
= num_stripes
;
2521 for (i
= 0; i
< ndevs
; ++i
) {
2522 for (j
= 0; j
< dev_stripes
; ++j
) {
2523 int s
= i
* dev_stripes
+ j
;
2524 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
2525 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
2529 map
->sector_size
= extent_root
->sectorsize
;
2530 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2531 map
->io_align
= BTRFS_STRIPE_LEN
;
2532 map
->io_width
= BTRFS_STRIPE_LEN
;
2534 map
->sub_stripes
= sub_stripes
;
2537 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
2539 *stripe_size_out
= stripe_size
;
2540 *num_bytes_out
= num_bytes
;
2542 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
2544 em
= alloc_extent_map();
2549 em
->bdev
= (struct block_device
*)map
;
2551 em
->len
= num_bytes
;
2552 em
->block_start
= 0;
2553 em
->block_len
= em
->len
;
2555 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2556 write_lock(&em_tree
->lock
);
2557 ret
= add_extent_mapping(em_tree
, em
);
2558 write_unlock(&em_tree
->lock
);
2560 free_extent_map(em
);
2562 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2563 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2567 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2568 struct btrfs_device
*device
;
2571 device
= map
->stripes
[i
].dev
;
2572 dev_offset
= map
->stripes
[i
].physical
;
2574 ret
= btrfs_alloc_dev_extent(trans
, device
,
2575 info
->chunk_root
->root_key
.objectid
,
2576 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2577 start
, dev_offset
, stripe_size
);
2581 kfree(devices_info
);
2586 kfree(devices_info
);
2590 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2591 struct btrfs_root
*extent_root
,
2592 struct map_lookup
*map
, u64 chunk_offset
,
2593 u64 chunk_size
, u64 stripe_size
)
2596 struct btrfs_key key
;
2597 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2598 struct btrfs_device
*device
;
2599 struct btrfs_chunk
*chunk
;
2600 struct btrfs_stripe
*stripe
;
2601 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2605 chunk
= kzalloc(item_size
, GFP_NOFS
);
2610 while (index
< map
->num_stripes
) {
2611 device
= map
->stripes
[index
].dev
;
2612 device
->bytes_used
+= stripe_size
;
2613 ret
= btrfs_update_device(trans
, device
);
2619 stripe
= &chunk
->stripe
;
2620 while (index
< map
->num_stripes
) {
2621 device
= map
->stripes
[index
].dev
;
2622 dev_offset
= map
->stripes
[index
].physical
;
2624 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2625 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2626 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2631 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2632 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2633 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2634 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2635 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2636 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2637 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2638 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2639 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2641 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2642 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2643 key
.offset
= chunk_offset
;
2645 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2648 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2649 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2659 * Chunk allocation falls into two parts. The first part does works
2660 * that make the new allocated chunk useable, but not do any operation
2661 * that modifies the chunk tree. The second part does the works that
2662 * require modifying the chunk tree. This division is important for the
2663 * bootstrap process of adding storage to a seed btrfs.
2665 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2666 struct btrfs_root
*extent_root
, u64 type
)
2671 struct map_lookup
*map
;
2672 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2675 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2680 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2681 &stripe_size
, chunk_offset
, type
);
2685 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2686 chunk_size
, stripe_size
);
2691 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2692 struct btrfs_root
*root
,
2693 struct btrfs_device
*device
)
2696 u64 sys_chunk_offset
;
2700 u64 sys_stripe_size
;
2702 struct map_lookup
*map
;
2703 struct map_lookup
*sys_map
;
2704 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2705 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2708 ret
= find_next_chunk(fs_info
->chunk_root
,
2709 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2713 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2714 (fs_info
->metadata_alloc_profile
&
2715 fs_info
->avail_metadata_alloc_bits
);
2716 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2718 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2719 &stripe_size
, chunk_offset
, alloc_profile
);
2722 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2724 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2725 (fs_info
->system_alloc_profile
&
2726 fs_info
->avail_system_alloc_bits
);
2727 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2729 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2730 &sys_chunk_size
, &sys_stripe_size
,
2731 sys_chunk_offset
, alloc_profile
);
2734 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2738 * Modifying chunk tree needs allocating new blocks from both
2739 * system block group and metadata block group. So we only can
2740 * do operations require modifying the chunk tree after both
2741 * block groups were created.
2743 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2744 chunk_size
, stripe_size
);
2747 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2748 sys_chunk_offset
, sys_chunk_size
,
2754 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2756 struct extent_map
*em
;
2757 struct map_lookup
*map
;
2758 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2762 read_lock(&map_tree
->map_tree
.lock
);
2763 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2764 read_unlock(&map_tree
->map_tree
.lock
);
2768 if (btrfs_test_opt(root
, DEGRADED
)) {
2769 free_extent_map(em
);
2773 map
= (struct map_lookup
*)em
->bdev
;
2774 for (i
= 0; i
< map
->num_stripes
; i
++) {
2775 if (!map
->stripes
[i
].dev
->writeable
) {
2780 free_extent_map(em
);
2784 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2786 extent_map_tree_init(&tree
->map_tree
);
2789 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2791 struct extent_map
*em
;
2794 write_lock(&tree
->map_tree
.lock
);
2795 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2797 remove_extent_mapping(&tree
->map_tree
, em
);
2798 write_unlock(&tree
->map_tree
.lock
);
2803 free_extent_map(em
);
2804 /* once for the tree */
2805 free_extent_map(em
);
2809 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2811 struct extent_map
*em
;
2812 struct map_lookup
*map
;
2813 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2816 read_lock(&em_tree
->lock
);
2817 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2818 read_unlock(&em_tree
->lock
);
2821 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2822 map
= (struct map_lookup
*)em
->bdev
;
2823 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2824 ret
= map
->num_stripes
;
2825 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2826 ret
= map
->sub_stripes
;
2829 free_extent_map(em
);
2833 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2837 if (map
->stripes
[optimal
].dev
->bdev
)
2839 for (i
= first
; i
< first
+ num
; i
++) {
2840 if (map
->stripes
[i
].dev
->bdev
)
2843 /* we couldn't find one that doesn't fail. Just return something
2844 * and the io error handling code will clean up eventually
2849 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2850 u64 logical
, u64
*length
,
2851 struct btrfs_multi_bio
**multi_ret
,
2854 struct extent_map
*em
;
2855 struct map_lookup
*map
;
2856 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2859 u64 stripe_end_offset
;
2863 int stripes_allocated
= 8;
2864 int stripes_required
= 1;
2869 struct btrfs_multi_bio
*multi
= NULL
;
2871 if (multi_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2872 stripes_allocated
= 1;
2875 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2880 atomic_set(&multi
->error
, 0);
2883 read_lock(&em_tree
->lock
);
2884 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2885 read_unlock(&em_tree
->lock
);
2888 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2889 (unsigned long long)logical
,
2890 (unsigned long long)*length
);
2894 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2895 map
= (struct map_lookup
*)em
->bdev
;
2896 offset
= logical
- em
->start
;
2898 if (mirror_num
> map
->num_stripes
)
2901 /* if our multi bio struct is too small, back off and try again */
2902 if (rw
& REQ_WRITE
) {
2903 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2904 BTRFS_BLOCK_GROUP_DUP
)) {
2905 stripes_required
= map
->num_stripes
;
2907 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2908 stripes_required
= map
->sub_stripes
;
2912 if (rw
& REQ_DISCARD
) {
2913 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2914 BTRFS_BLOCK_GROUP_RAID1
|
2915 BTRFS_BLOCK_GROUP_DUP
|
2916 BTRFS_BLOCK_GROUP_RAID10
)) {
2917 stripes_required
= map
->num_stripes
;
2920 if (multi_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2921 stripes_allocated
< stripes_required
) {
2922 stripes_allocated
= map
->num_stripes
;
2923 free_extent_map(em
);
2929 * stripe_nr counts the total number of stripes we have to stride
2930 * to get to this block
2932 do_div(stripe_nr
, map
->stripe_len
);
2934 stripe_offset
= stripe_nr
* map
->stripe_len
;
2935 BUG_ON(offset
< stripe_offset
);
2937 /* stripe_offset is the offset of this block in its stripe*/
2938 stripe_offset
= offset
- stripe_offset
;
2940 if (rw
& REQ_DISCARD
)
2941 *length
= min_t(u64
, em
->len
- offset
, *length
);
2942 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2943 BTRFS_BLOCK_GROUP_RAID1
|
2944 BTRFS_BLOCK_GROUP_RAID10
|
2945 BTRFS_BLOCK_GROUP_DUP
)) {
2946 /* we limit the length of each bio to what fits in a stripe */
2947 *length
= min_t(u64
, em
->len
- offset
,
2948 map
->stripe_len
- stripe_offset
);
2950 *length
= em
->len
- offset
;
2958 stripe_nr_orig
= stripe_nr
;
2959 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
2960 (~(map
->stripe_len
- 1));
2961 do_div(stripe_nr_end
, map
->stripe_len
);
2962 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
2964 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2965 if (rw
& REQ_DISCARD
)
2966 num_stripes
= min_t(u64
, map
->num_stripes
,
2967 stripe_nr_end
- stripe_nr_orig
);
2968 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2969 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2970 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2971 num_stripes
= map
->num_stripes
;
2972 else if (mirror_num
)
2973 stripe_index
= mirror_num
- 1;
2975 stripe_index
= find_live_mirror(map
, 0,
2977 current
->pid
% map
->num_stripes
);
2980 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2981 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2982 num_stripes
= map
->num_stripes
;
2983 else if (mirror_num
)
2984 stripe_index
= mirror_num
- 1;
2986 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2987 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2989 stripe_index
= do_div(stripe_nr
, factor
);
2990 stripe_index
*= map
->sub_stripes
;
2993 num_stripes
= map
->sub_stripes
;
2994 else if (rw
& REQ_DISCARD
)
2995 num_stripes
= min_t(u64
, map
->sub_stripes
*
2996 (stripe_nr_end
- stripe_nr_orig
),
2998 else if (mirror_num
)
2999 stripe_index
+= mirror_num
- 1;
3001 stripe_index
= find_live_mirror(map
, stripe_index
,
3002 map
->sub_stripes
, stripe_index
+
3003 current
->pid
% map
->sub_stripes
);
3007 * after this do_div call, stripe_nr is the number of stripes
3008 * on this device we have to walk to find the data, and
3009 * stripe_index is the number of our device in the stripe array
3011 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3013 BUG_ON(stripe_index
>= map
->num_stripes
);
3015 if (rw
& REQ_DISCARD
) {
3016 for (i
= 0; i
< num_stripes
; i
++) {
3017 multi
->stripes
[i
].physical
=
3018 map
->stripes
[stripe_index
].physical
+
3019 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3020 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3022 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3024 u32 last_stripe
= 0;
3027 div_u64_rem(stripe_nr_end
- 1,
3031 for (j
= 0; j
< map
->num_stripes
; j
++) {
3034 div_u64_rem(stripe_nr_end
- 1 - j
,
3035 map
->num_stripes
, &test
);
3036 if (test
== stripe_index
)
3039 stripes
= stripe_nr_end
- 1 - j
;
3040 do_div(stripes
, map
->num_stripes
);
3041 multi
->stripes
[i
].length
= map
->stripe_len
*
3042 (stripes
- stripe_nr
+ 1);
3045 multi
->stripes
[i
].length
-=
3049 if (stripe_index
== last_stripe
)
3050 multi
->stripes
[i
].length
-=
3052 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3055 int factor
= map
->num_stripes
/
3057 u32 last_stripe
= 0;
3059 div_u64_rem(stripe_nr_end
- 1,
3060 factor
, &last_stripe
);
3061 last_stripe
*= map
->sub_stripes
;
3063 for (j
= 0; j
< factor
; j
++) {
3066 div_u64_rem(stripe_nr_end
- 1 - j
,
3070 stripe_index
/ map
->sub_stripes
)
3073 stripes
= stripe_nr_end
- 1 - j
;
3074 do_div(stripes
, factor
);
3075 multi
->stripes
[i
].length
= map
->stripe_len
*
3076 (stripes
- stripe_nr
+ 1);
3078 if (i
< map
->sub_stripes
) {
3079 multi
->stripes
[i
].length
-=
3081 if (i
== map
->sub_stripes
- 1)
3084 if (stripe_index
>= last_stripe
&&
3085 stripe_index
<= (last_stripe
+
3086 map
->sub_stripes
- 1)) {
3087 multi
->stripes
[i
].length
-=
3091 multi
->stripes
[i
].length
= *length
;
3094 if (stripe_index
== map
->num_stripes
) {
3095 /* This could only happen for RAID0/10 */
3101 for (i
= 0; i
< num_stripes
; i
++) {
3102 multi
->stripes
[i
].physical
=
3103 map
->stripes
[stripe_index
].physical
+
3105 stripe_nr
* map
->stripe_len
;
3106 multi
->stripes
[i
].dev
=
3107 map
->stripes
[stripe_index
].dev
;
3113 multi
->num_stripes
= num_stripes
;
3114 multi
->max_errors
= max_errors
;
3117 free_extent_map(em
);
3121 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3122 u64 logical
, u64
*length
,
3123 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3125 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3129 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3130 u64 chunk_start
, u64 physical
, u64 devid
,
3131 u64
**logical
, int *naddrs
, int *stripe_len
)
3133 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3134 struct extent_map
*em
;
3135 struct map_lookup
*map
;
3142 read_lock(&em_tree
->lock
);
3143 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3144 read_unlock(&em_tree
->lock
);
3146 BUG_ON(!em
|| em
->start
!= chunk_start
);
3147 map
= (struct map_lookup
*)em
->bdev
;
3150 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3151 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3152 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3153 do_div(length
, map
->num_stripes
);
3155 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3158 for (i
= 0; i
< map
->num_stripes
; i
++) {
3159 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3161 if (map
->stripes
[i
].physical
> physical
||
3162 map
->stripes
[i
].physical
+ length
<= physical
)
3165 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3166 do_div(stripe_nr
, map
->stripe_len
);
3168 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3169 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3170 do_div(stripe_nr
, map
->sub_stripes
);
3171 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3172 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3174 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3175 WARN_ON(nr
>= map
->num_stripes
);
3176 for (j
= 0; j
< nr
; j
++) {
3177 if (buf
[j
] == bytenr
)
3181 WARN_ON(nr
>= map
->num_stripes
);
3188 *stripe_len
= map
->stripe_len
;
3190 free_extent_map(em
);
3194 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3196 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3197 int is_orig_bio
= 0;
3200 atomic_inc(&multi
->error
);
3202 if (bio
== multi
->orig_bio
)
3205 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3208 bio
= multi
->orig_bio
;
3210 bio
->bi_private
= multi
->private;
3211 bio
->bi_end_io
= multi
->end_io
;
3212 /* only send an error to the higher layers if it is
3213 * beyond the tolerance of the multi-bio
3215 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3219 * this bio is actually up to date, we didn't
3220 * go over the max number of errors
3222 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3227 bio_endio(bio
, err
);
3228 } else if (!is_orig_bio
) {
3233 struct async_sched
{
3236 struct btrfs_fs_info
*info
;
3237 struct btrfs_work work
;
3241 * see run_scheduled_bios for a description of why bios are collected for
3244 * This will add one bio to the pending list for a device and make sure
3245 * the work struct is scheduled.
3247 static noinline
int schedule_bio(struct btrfs_root
*root
,
3248 struct btrfs_device
*device
,
3249 int rw
, struct bio
*bio
)
3251 int should_queue
= 1;
3252 struct btrfs_pending_bios
*pending_bios
;
3254 /* don't bother with additional async steps for reads, right now */
3255 if (!(rw
& REQ_WRITE
)) {
3257 submit_bio(rw
, bio
);
3263 * nr_async_bios allows us to reliably return congestion to the
3264 * higher layers. Otherwise, the async bio makes it appear we have
3265 * made progress against dirty pages when we've really just put it
3266 * on a queue for later
3268 atomic_inc(&root
->fs_info
->nr_async_bios
);
3269 WARN_ON(bio
->bi_next
);
3270 bio
->bi_next
= NULL
;
3273 spin_lock(&device
->io_lock
);
3274 if (bio
->bi_rw
& REQ_SYNC
)
3275 pending_bios
= &device
->pending_sync_bios
;
3277 pending_bios
= &device
->pending_bios
;
3279 if (pending_bios
->tail
)
3280 pending_bios
->tail
->bi_next
= bio
;
3282 pending_bios
->tail
= bio
;
3283 if (!pending_bios
->head
)
3284 pending_bios
->head
= bio
;
3285 if (device
->running_pending
)
3288 spin_unlock(&device
->io_lock
);
3291 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3296 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3297 int mirror_num
, int async_submit
)
3299 struct btrfs_mapping_tree
*map_tree
;
3300 struct btrfs_device
*dev
;
3301 struct bio
*first_bio
= bio
;
3302 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3305 struct btrfs_multi_bio
*multi
= NULL
;
3310 length
= bio
->bi_size
;
3311 map_tree
= &root
->fs_info
->mapping_tree
;
3312 map_length
= length
;
3314 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3318 total_devs
= multi
->num_stripes
;
3319 if (map_length
< length
) {
3320 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3321 "len %llu\n", (unsigned long long)logical
,
3322 (unsigned long long)length
,
3323 (unsigned long long)map_length
);
3326 multi
->end_io
= first_bio
->bi_end_io
;
3327 multi
->private = first_bio
->bi_private
;
3328 multi
->orig_bio
= first_bio
;
3329 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3331 while (dev_nr
< total_devs
) {
3332 if (total_devs
> 1) {
3333 if (dev_nr
< total_devs
- 1) {
3334 bio
= bio_clone(first_bio
, GFP_NOFS
);
3339 bio
->bi_private
= multi
;
3340 bio
->bi_end_io
= end_bio_multi_stripe
;
3342 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3343 dev
= multi
->stripes
[dev_nr
].dev
;
3344 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3345 bio
->bi_bdev
= dev
->bdev
;
3347 schedule_bio(root
, dev
, rw
, bio
);
3349 submit_bio(rw
, bio
);
3351 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3352 bio
->bi_sector
= logical
>> 9;
3353 bio_endio(bio
, -EIO
);
3357 if (total_devs
== 1)
3362 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3365 struct btrfs_device
*device
;
3366 struct btrfs_fs_devices
*cur_devices
;
3368 cur_devices
= root
->fs_info
->fs_devices
;
3369 while (cur_devices
) {
3371 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3372 device
= __find_device(&cur_devices
->devices
,
3377 cur_devices
= cur_devices
->seed
;
3382 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3383 u64 devid
, u8
*dev_uuid
)
3385 struct btrfs_device
*device
;
3386 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3388 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3391 list_add(&device
->dev_list
,
3392 &fs_devices
->devices
);
3393 device
->dev_root
= root
->fs_info
->dev_root
;
3394 device
->devid
= devid
;
3395 device
->work
.func
= pending_bios_fn
;
3396 device
->fs_devices
= fs_devices
;
3397 device
->missing
= 1;
3398 fs_devices
->num_devices
++;
3399 fs_devices
->missing_devices
++;
3400 spin_lock_init(&device
->io_lock
);
3401 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3402 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3406 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3407 struct extent_buffer
*leaf
,
3408 struct btrfs_chunk
*chunk
)
3410 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3411 struct map_lookup
*map
;
3412 struct extent_map
*em
;
3416 u8 uuid
[BTRFS_UUID_SIZE
];
3421 logical
= key
->offset
;
3422 length
= btrfs_chunk_length(leaf
, chunk
);
3424 read_lock(&map_tree
->map_tree
.lock
);
3425 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3426 read_unlock(&map_tree
->map_tree
.lock
);
3428 /* already mapped? */
3429 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3430 free_extent_map(em
);
3433 free_extent_map(em
);
3436 em
= alloc_extent_map();
3439 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3440 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3442 free_extent_map(em
);
3446 em
->bdev
= (struct block_device
*)map
;
3447 em
->start
= logical
;
3449 em
->block_start
= 0;
3450 em
->block_len
= em
->len
;
3452 map
->num_stripes
= num_stripes
;
3453 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3454 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3455 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3456 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3457 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3458 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3459 for (i
= 0; i
< num_stripes
; i
++) {
3460 map
->stripes
[i
].physical
=
3461 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3462 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3463 read_extent_buffer(leaf
, uuid
, (unsigned long)
3464 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3466 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3468 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3470 free_extent_map(em
);
3473 if (!map
->stripes
[i
].dev
) {
3474 map
->stripes
[i
].dev
=
3475 add_missing_dev(root
, devid
, uuid
);
3476 if (!map
->stripes
[i
].dev
) {
3478 free_extent_map(em
);
3482 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3485 write_lock(&map_tree
->map_tree
.lock
);
3486 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3487 write_unlock(&map_tree
->map_tree
.lock
);
3489 free_extent_map(em
);
3494 static int fill_device_from_item(struct extent_buffer
*leaf
,
3495 struct btrfs_dev_item
*dev_item
,
3496 struct btrfs_device
*device
)
3500 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3501 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3502 device
->total_bytes
= device
->disk_total_bytes
;
3503 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3504 device
->type
= btrfs_device_type(leaf
, dev_item
);
3505 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3506 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3507 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3509 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3510 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3515 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3517 struct btrfs_fs_devices
*fs_devices
;
3520 mutex_lock(&uuid_mutex
);
3522 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3523 while (fs_devices
) {
3524 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3528 fs_devices
= fs_devices
->seed
;
3531 fs_devices
= find_fsid(fsid
);
3537 fs_devices
= clone_fs_devices(fs_devices
);
3538 if (IS_ERR(fs_devices
)) {
3539 ret
= PTR_ERR(fs_devices
);
3543 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3544 root
->fs_info
->bdev_holder
);
3548 if (!fs_devices
->seeding
) {
3549 __btrfs_close_devices(fs_devices
);
3550 free_fs_devices(fs_devices
);
3555 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3556 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3558 mutex_unlock(&uuid_mutex
);
3562 static int read_one_dev(struct btrfs_root
*root
,
3563 struct extent_buffer
*leaf
,
3564 struct btrfs_dev_item
*dev_item
)
3566 struct btrfs_device
*device
;
3569 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3570 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3572 devid
= btrfs_device_id(leaf
, dev_item
);
3573 read_extent_buffer(leaf
, dev_uuid
,
3574 (unsigned long)btrfs_device_uuid(dev_item
),
3576 read_extent_buffer(leaf
, fs_uuid
,
3577 (unsigned long)btrfs_device_fsid(dev_item
),
3580 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3581 ret
= open_seed_devices(root
, fs_uuid
);
3582 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3586 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3587 if (!device
|| !device
->bdev
) {
3588 if (!btrfs_test_opt(root
, DEGRADED
))
3592 printk(KERN_WARNING
"warning devid %llu missing\n",
3593 (unsigned long long)devid
);
3594 device
= add_missing_dev(root
, devid
, dev_uuid
);
3597 } else if (!device
->missing
) {
3599 * this happens when a device that was properly setup
3600 * in the device info lists suddenly goes bad.
3601 * device->bdev is NULL, and so we have to set
3602 * device->missing to one here
3604 root
->fs_info
->fs_devices
->missing_devices
++;
3605 device
->missing
= 1;
3609 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3610 BUG_ON(device
->writeable
);
3611 if (device
->generation
!=
3612 btrfs_device_generation(leaf
, dev_item
))
3616 fill_device_from_item(leaf
, dev_item
, device
);
3617 device
->dev_root
= root
->fs_info
->dev_root
;
3618 device
->in_fs_metadata
= 1;
3619 if (device
->writeable
)
3620 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3625 int btrfs_read_sys_array(struct btrfs_root
*root
)
3627 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3628 struct extent_buffer
*sb
;
3629 struct btrfs_disk_key
*disk_key
;
3630 struct btrfs_chunk
*chunk
;
3632 unsigned long sb_ptr
;
3638 struct btrfs_key key
;
3640 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3641 BTRFS_SUPER_INFO_SIZE
);
3644 btrfs_set_buffer_uptodate(sb
);
3645 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
3647 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3648 array_size
= btrfs_super_sys_array_size(super_copy
);
3650 ptr
= super_copy
->sys_chunk_array
;
3651 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3654 while (cur
< array_size
) {
3655 disk_key
= (struct btrfs_disk_key
*)ptr
;
3656 btrfs_disk_key_to_cpu(&key
, disk_key
);
3658 len
= sizeof(*disk_key
); ptr
+= len
;
3662 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3663 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3664 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3667 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3668 len
= btrfs_chunk_item_size(num_stripes
);
3677 free_extent_buffer(sb
);
3681 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3683 struct btrfs_path
*path
;
3684 struct extent_buffer
*leaf
;
3685 struct btrfs_key key
;
3686 struct btrfs_key found_key
;
3690 root
= root
->fs_info
->chunk_root
;
3692 path
= btrfs_alloc_path();
3696 /* first we search for all of the device items, and then we
3697 * read in all of the chunk items. This way we can create chunk
3698 * mappings that reference all of the devices that are afound
3700 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3704 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3708 leaf
= path
->nodes
[0];
3709 slot
= path
->slots
[0];
3710 if (slot
>= btrfs_header_nritems(leaf
)) {
3711 ret
= btrfs_next_leaf(root
, path
);
3718 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3719 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3720 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3722 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3723 struct btrfs_dev_item
*dev_item
;
3724 dev_item
= btrfs_item_ptr(leaf
, slot
,
3725 struct btrfs_dev_item
);
3726 ret
= read_one_dev(root
, leaf
, dev_item
);
3730 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3731 struct btrfs_chunk
*chunk
;
3732 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3733 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3739 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3741 btrfs_release_path(path
);
3746 btrfs_free_path(path
);