2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 struct btrfs_device
*device
);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
41 static DEFINE_MUTEX(uuid_mutex
);
42 static LIST_HEAD(fs_uuids
);
44 static void lock_chunks(struct btrfs_root
*root
)
46 mutex_lock(&root
->fs_info
->chunk_mutex
);
49 static void unlock_chunks(struct btrfs_root
*root
)
51 mutex_unlock(&root
->fs_info
->chunk_mutex
);
54 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
56 struct btrfs_device
*device
;
57 WARN_ON(fs_devices
->opened
);
58 while (!list_empty(&fs_devices
->devices
)) {
59 device
= list_entry(fs_devices
->devices
.next
,
60 struct btrfs_device
, dev_list
);
61 list_del(&device
->dev_list
);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices
*fs_devices
;
72 while (!list_empty(&fs_uuids
)) {
73 fs_devices
= list_entry(fs_uuids
.next
,
74 struct btrfs_fs_devices
, list
);
75 list_del(&fs_devices
->list
);
76 free_fs_devices(fs_devices
);
81 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
84 struct btrfs_device
*dev
;
86 list_for_each_entry(dev
, head
, dev_list
) {
87 if (dev
->devid
== devid
&&
88 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
95 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
97 struct btrfs_fs_devices
*fs_devices
;
99 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
100 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
106 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
107 struct bio
*head
, struct bio
*tail
)
110 struct bio
*old_head
;
112 old_head
= pending_bios
->head
;
113 pending_bios
->head
= head
;
114 if (pending_bios
->tail
)
115 tail
->bi_next
= old_head
;
117 pending_bios
->tail
= tail
;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
134 struct backing_dev_info
*bdi
;
135 struct btrfs_fs_info
*fs_info
;
136 struct btrfs_pending_bios
*pending_bios
;
140 unsigned long num_run
;
141 unsigned long batch_run
= 0;
143 unsigned long last_waited
= 0;
145 struct blk_plug plug
;
148 * this function runs all the bios we've collected for
149 * a particular device. We don't want to wander off to
150 * another device without first sending all of these down.
151 * So, setup a plug here and finish it off before we return
153 blk_start_plug(&plug
);
155 bdi
= blk_get_backing_dev_info(device
->bdev
);
156 fs_info
= device
->dev_root
->fs_info
;
157 limit
= btrfs_async_submit_limit(fs_info
);
158 limit
= limit
* 2 / 3;
161 spin_lock(&device
->io_lock
);
166 /* take all the bios off the list at once and process them
167 * later on (without the lock held). But, remember the
168 * tail and other pointers so the bios can be properly reinserted
169 * into the list if we hit congestion
171 if (!force_reg
&& device
->pending_sync_bios
.head
) {
172 pending_bios
= &device
->pending_sync_bios
;
175 pending_bios
= &device
->pending_bios
;
179 pending
= pending_bios
->head
;
180 tail
= pending_bios
->tail
;
181 WARN_ON(pending
&& !tail
);
184 * if pending was null this time around, no bios need processing
185 * at all and we can stop. Otherwise it'll loop back up again
186 * and do an additional check so no bios are missed.
188 * device->running_pending is used to synchronize with the
191 if (device
->pending_sync_bios
.head
== NULL
&&
192 device
->pending_bios
.head
== NULL
) {
194 device
->running_pending
= 0;
197 device
->running_pending
= 1;
200 pending_bios
->head
= NULL
;
201 pending_bios
->tail
= NULL
;
203 spin_unlock(&device
->io_lock
);
208 /* we want to work on both lists, but do more bios on the
209 * sync list than the regular list
212 pending_bios
!= &device
->pending_sync_bios
&&
213 device
->pending_sync_bios
.head
) ||
214 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
215 device
->pending_bios
.head
)) {
216 spin_lock(&device
->io_lock
);
217 requeue_list(pending_bios
, pending
, tail
);
222 pending
= pending
->bi_next
;
224 atomic_dec(&fs_info
->nr_async_bios
);
226 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
227 waitqueue_active(&fs_info
->async_submit_wait
))
228 wake_up(&fs_info
->async_submit_wait
);
230 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
232 submit_bio(cur
->bi_rw
, cur
);
239 * we made progress, there is more work to do and the bdi
240 * is now congested. Back off and let other work structs
243 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
244 fs_info
->fs_devices
->open_devices
> 1) {
245 struct io_context
*ioc
;
247 ioc
= current
->io_context
;
250 * the main goal here is that we don't want to
251 * block if we're going to be able to submit
252 * more requests without blocking.
254 * This code does two great things, it pokes into
255 * the elevator code from a filesystem _and_
256 * it makes assumptions about how batching works.
258 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
259 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
261 ioc
->last_waited
== last_waited
)) {
263 * we want to go through our batch of
264 * requests and stop. So, we copy out
265 * the ioc->last_waited time and test
266 * against it before looping
268 last_waited
= ioc
->last_waited
;
273 spin_lock(&device
->io_lock
);
274 requeue_list(pending_bios
, pending
, tail
);
275 device
->running_pending
= 1;
277 spin_unlock(&device
->io_lock
);
278 btrfs_requeue_work(&device
->work
);
287 spin_lock(&device
->io_lock
);
288 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
290 spin_unlock(&device
->io_lock
);
293 blk_finish_plug(&plug
);
297 static void pending_bios_fn(struct btrfs_work
*work
)
299 struct btrfs_device
*device
;
301 device
= container_of(work
, struct btrfs_device
, work
);
302 run_scheduled_bios(device
);
305 static noinline
int device_list_add(const char *path
,
306 struct btrfs_super_block
*disk_super
,
307 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
309 struct btrfs_device
*device
;
310 struct btrfs_fs_devices
*fs_devices
;
311 u64 found_transid
= btrfs_super_generation(disk_super
);
314 fs_devices
= find_fsid(disk_super
->fsid
);
316 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
319 INIT_LIST_HEAD(&fs_devices
->devices
);
320 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
321 list_add(&fs_devices
->list
, &fs_uuids
);
322 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
323 fs_devices
->latest_devid
= devid
;
324 fs_devices
->latest_trans
= found_transid
;
325 mutex_init(&fs_devices
->device_list_mutex
);
328 device
= __find_device(&fs_devices
->devices
, devid
,
329 disk_super
->dev_item
.uuid
);
332 if (fs_devices
->opened
)
335 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
337 /* we can safely leave the fs_devices entry around */
340 device
->devid
= devid
;
341 device
->work
.func
= pending_bios_fn
;
342 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
344 spin_lock_init(&device
->io_lock
);
345 device
->name
= kstrdup(path
, GFP_NOFS
);
350 INIT_LIST_HEAD(&device
->dev_alloc_list
);
352 mutex_lock(&fs_devices
->device_list_mutex
);
353 list_add(&device
->dev_list
, &fs_devices
->devices
);
354 mutex_unlock(&fs_devices
->device_list_mutex
);
356 device
->fs_devices
= fs_devices
;
357 fs_devices
->num_devices
++;
358 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
359 name
= kstrdup(path
, GFP_NOFS
);
364 if (device
->missing
) {
365 fs_devices
->missing_devices
--;
370 if (found_transid
> fs_devices
->latest_trans
) {
371 fs_devices
->latest_devid
= devid
;
372 fs_devices
->latest_trans
= found_transid
;
374 *fs_devices_ret
= fs_devices
;
378 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
380 struct btrfs_fs_devices
*fs_devices
;
381 struct btrfs_device
*device
;
382 struct btrfs_device
*orig_dev
;
384 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
386 return ERR_PTR(-ENOMEM
);
388 INIT_LIST_HEAD(&fs_devices
->devices
);
389 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
390 INIT_LIST_HEAD(&fs_devices
->list
);
391 mutex_init(&fs_devices
->device_list_mutex
);
392 fs_devices
->latest_devid
= orig
->latest_devid
;
393 fs_devices
->latest_trans
= orig
->latest_trans
;
394 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
396 mutex_lock(&orig
->device_list_mutex
);
397 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
398 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
402 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
408 device
->devid
= orig_dev
->devid
;
409 device
->work
.func
= pending_bios_fn
;
410 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
411 spin_lock_init(&device
->io_lock
);
412 INIT_LIST_HEAD(&device
->dev_list
);
413 INIT_LIST_HEAD(&device
->dev_alloc_list
);
415 list_add(&device
->dev_list
, &fs_devices
->devices
);
416 device
->fs_devices
= fs_devices
;
417 fs_devices
->num_devices
++;
419 mutex_unlock(&orig
->device_list_mutex
);
422 mutex_unlock(&orig
->device_list_mutex
);
423 free_fs_devices(fs_devices
);
424 return ERR_PTR(-ENOMEM
);
427 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
429 struct btrfs_device
*device
, *next
;
431 mutex_lock(&uuid_mutex
);
433 mutex_lock(&fs_devices
->device_list_mutex
);
434 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
435 if (device
->in_fs_metadata
)
439 blkdev_put(device
->bdev
, device
->mode
);
441 fs_devices
->open_devices
--;
443 if (device
->writeable
) {
444 list_del_init(&device
->dev_alloc_list
);
445 device
->writeable
= 0;
446 fs_devices
->rw_devices
--;
448 list_del_init(&device
->dev_list
);
449 fs_devices
->num_devices
--;
453 mutex_unlock(&fs_devices
->device_list_mutex
);
455 if (fs_devices
->seed
) {
456 fs_devices
= fs_devices
->seed
;
460 mutex_unlock(&uuid_mutex
);
464 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
466 struct btrfs_device
*device
;
468 if (--fs_devices
->opened
> 0)
471 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
473 blkdev_put(device
->bdev
, device
->mode
);
474 fs_devices
->open_devices
--;
476 if (device
->writeable
) {
477 list_del_init(&device
->dev_alloc_list
);
478 fs_devices
->rw_devices
--;
482 device
->writeable
= 0;
483 device
->in_fs_metadata
= 0;
485 WARN_ON(fs_devices
->open_devices
);
486 WARN_ON(fs_devices
->rw_devices
);
487 fs_devices
->opened
= 0;
488 fs_devices
->seeding
= 0;
493 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
495 struct btrfs_fs_devices
*seed_devices
= NULL
;
498 mutex_lock(&uuid_mutex
);
499 ret
= __btrfs_close_devices(fs_devices
);
500 if (!fs_devices
->opened
) {
501 seed_devices
= fs_devices
->seed
;
502 fs_devices
->seed
= NULL
;
504 mutex_unlock(&uuid_mutex
);
506 while (seed_devices
) {
507 fs_devices
= seed_devices
;
508 seed_devices
= fs_devices
->seed
;
509 __btrfs_close_devices(fs_devices
);
510 free_fs_devices(fs_devices
);
515 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
516 fmode_t flags
, void *holder
)
518 struct block_device
*bdev
;
519 struct list_head
*head
= &fs_devices
->devices
;
520 struct btrfs_device
*device
;
521 struct block_device
*latest_bdev
= NULL
;
522 struct buffer_head
*bh
;
523 struct btrfs_super_block
*disk_super
;
524 u64 latest_devid
= 0;
525 u64 latest_transid
= 0;
532 list_for_each_entry(device
, head
, dev_list
) {
538 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
540 printk(KERN_INFO
"open %s failed\n", device
->name
);
543 set_blocksize(bdev
, 4096);
545 bh
= btrfs_read_dev_super(bdev
);
551 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
552 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
553 if (devid
!= device
->devid
)
556 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
560 device
->generation
= btrfs_super_generation(disk_super
);
561 if (!latest_transid
|| device
->generation
> latest_transid
) {
562 latest_devid
= devid
;
563 latest_transid
= device
->generation
;
567 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
568 device
->writeable
= 0;
570 device
->writeable
= !bdev_read_only(bdev
);
575 device
->in_fs_metadata
= 0;
576 device
->mode
= flags
;
578 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
579 fs_devices
->rotating
= 1;
581 fs_devices
->open_devices
++;
582 if (device
->writeable
) {
583 fs_devices
->rw_devices
++;
584 list_add(&device
->dev_alloc_list
,
585 &fs_devices
->alloc_list
);
592 blkdev_put(bdev
, flags
);
596 if (fs_devices
->open_devices
== 0) {
600 fs_devices
->seeding
= seeding
;
601 fs_devices
->opened
= 1;
602 fs_devices
->latest_bdev
= latest_bdev
;
603 fs_devices
->latest_devid
= latest_devid
;
604 fs_devices
->latest_trans
= latest_transid
;
605 fs_devices
->total_rw_bytes
= 0;
610 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
611 fmode_t flags
, void *holder
)
615 mutex_lock(&uuid_mutex
);
616 if (fs_devices
->opened
) {
617 fs_devices
->opened
++;
620 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
622 mutex_unlock(&uuid_mutex
);
626 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
627 struct btrfs_fs_devices
**fs_devices_ret
)
629 struct btrfs_super_block
*disk_super
;
630 struct block_device
*bdev
;
631 struct buffer_head
*bh
;
636 mutex_lock(&uuid_mutex
);
639 bdev
= blkdev_get_by_path(path
, flags
, holder
);
646 ret
= set_blocksize(bdev
, 4096);
649 bh
= btrfs_read_dev_super(bdev
);
654 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
655 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
656 transid
= btrfs_super_generation(disk_super
);
657 if (disk_super
->label
[0])
658 printk(KERN_INFO
"device label %s ", disk_super
->label
);
660 /* FIXME, make a readl uuid parser */
661 printk(KERN_INFO
"device fsid %llx-%llx ",
662 *(unsigned long long *)disk_super
->fsid
,
663 *(unsigned long long *)(disk_super
->fsid
+ 8));
665 printk(KERN_CONT
"devid %llu transid %llu %s\n",
666 (unsigned long long)devid
, (unsigned long long)transid
, path
);
667 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
671 blkdev_put(bdev
, flags
);
673 mutex_unlock(&uuid_mutex
);
677 /* helper to account the used device space in the range */
678 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
679 u64 end
, u64
*length
)
681 struct btrfs_key key
;
682 struct btrfs_root
*root
= device
->dev_root
;
683 struct btrfs_dev_extent
*dev_extent
;
684 struct btrfs_path
*path
;
688 struct extent_buffer
*l
;
692 if (start
>= device
->total_bytes
)
695 path
= btrfs_alloc_path();
700 key
.objectid
= device
->devid
;
702 key
.type
= BTRFS_DEV_EXTENT_KEY
;
704 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
708 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
715 slot
= path
->slots
[0];
716 if (slot
>= btrfs_header_nritems(l
)) {
717 ret
= btrfs_next_leaf(root
, path
);
725 btrfs_item_key_to_cpu(l
, &key
, slot
);
727 if (key
.objectid
< device
->devid
)
730 if (key
.objectid
> device
->devid
)
733 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
736 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
737 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
739 if (key
.offset
<= start
&& extent_end
> end
) {
740 *length
= end
- start
+ 1;
742 } else if (key
.offset
<= start
&& extent_end
> start
)
743 *length
+= extent_end
- start
;
744 else if (key
.offset
> start
&& extent_end
<= end
)
745 *length
+= extent_end
- key
.offset
;
746 else if (key
.offset
> start
&& key
.offset
<= end
) {
747 *length
+= end
- key
.offset
+ 1;
749 } else if (key
.offset
> end
)
757 btrfs_free_path(path
);
762 * find_free_dev_extent - find free space in the specified device
763 * @trans: transaction handler
764 * @device: the device which we search the free space in
765 * @num_bytes: the size of the free space that we need
766 * @start: store the start of the free space.
767 * @len: the size of the free space. that we find, or the size of the max
768 * free space if we don't find suitable free space
770 * this uses a pretty simple search, the expectation is that it is
771 * called very infrequently and that a given device has a small number
774 * @start is used to store the start of the free space if we find. But if we
775 * don't find suitable free space, it will be used to store the start position
776 * of the max free space.
778 * @len is used to store the size of the free space that we find.
779 * But if we don't find suitable free space, it is used to store the size of
780 * the max free space.
782 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
783 struct btrfs_device
*device
, u64 num_bytes
,
784 u64
*start
, u64
*len
)
786 struct btrfs_key key
;
787 struct btrfs_root
*root
= device
->dev_root
;
788 struct btrfs_dev_extent
*dev_extent
;
789 struct btrfs_path
*path
;
795 u64 search_end
= device
->total_bytes
;
798 struct extent_buffer
*l
;
800 /* FIXME use last free of some kind */
802 /* we don't want to overwrite the superblock on the drive,
803 * so we make sure to start at an offset of at least 1MB
805 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
807 max_hole_start
= search_start
;
810 if (search_start
>= search_end
) {
815 path
= btrfs_alloc_path();
822 key
.objectid
= device
->devid
;
823 key
.offset
= search_start
;
824 key
.type
= BTRFS_DEV_EXTENT_KEY
;
826 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
830 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
837 slot
= path
->slots
[0];
838 if (slot
>= btrfs_header_nritems(l
)) {
839 ret
= btrfs_next_leaf(root
, path
);
847 btrfs_item_key_to_cpu(l
, &key
, slot
);
849 if (key
.objectid
< device
->devid
)
852 if (key
.objectid
> device
->devid
)
855 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
858 if (key
.offset
> search_start
) {
859 hole_size
= key
.offset
- search_start
;
861 if (hole_size
> max_hole_size
) {
862 max_hole_start
= search_start
;
863 max_hole_size
= hole_size
;
867 * If this free space is greater than which we need,
868 * it must be the max free space that we have found
869 * until now, so max_hole_start must point to the start
870 * of this free space and the length of this free space
871 * is stored in max_hole_size. Thus, we return
872 * max_hole_start and max_hole_size and go back to the
875 if (hole_size
>= num_bytes
) {
881 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
882 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
884 if (extent_end
> search_start
)
885 search_start
= extent_end
;
891 hole_size
= search_end
- search_start
;
892 if (hole_size
> max_hole_size
) {
893 max_hole_start
= search_start
;
894 max_hole_size
= hole_size
;
898 if (hole_size
< num_bytes
)
904 btrfs_free_path(path
);
906 *start
= max_hole_start
;
908 *len
= max_hole_size
;
912 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
913 struct btrfs_device
*device
,
917 struct btrfs_path
*path
;
918 struct btrfs_root
*root
= device
->dev_root
;
919 struct btrfs_key key
;
920 struct btrfs_key found_key
;
921 struct extent_buffer
*leaf
= NULL
;
922 struct btrfs_dev_extent
*extent
= NULL
;
924 path
= btrfs_alloc_path();
928 key
.objectid
= device
->devid
;
930 key
.type
= BTRFS_DEV_EXTENT_KEY
;
932 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
934 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
935 BTRFS_DEV_EXTENT_KEY
);
937 leaf
= path
->nodes
[0];
938 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
939 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
940 struct btrfs_dev_extent
);
941 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
942 btrfs_dev_extent_length(leaf
, extent
) < start
);
944 } else if (ret
== 0) {
945 leaf
= path
->nodes
[0];
946 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
947 struct btrfs_dev_extent
);
951 if (device
->bytes_used
> 0)
952 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
953 ret
= btrfs_del_item(trans
, root
, path
);
956 btrfs_free_path(path
);
960 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
961 struct btrfs_device
*device
,
962 u64 chunk_tree
, u64 chunk_objectid
,
963 u64 chunk_offset
, u64 start
, u64 num_bytes
)
966 struct btrfs_path
*path
;
967 struct btrfs_root
*root
= device
->dev_root
;
968 struct btrfs_dev_extent
*extent
;
969 struct extent_buffer
*leaf
;
970 struct btrfs_key key
;
972 WARN_ON(!device
->in_fs_metadata
);
973 path
= btrfs_alloc_path();
977 key
.objectid
= device
->devid
;
979 key
.type
= BTRFS_DEV_EXTENT_KEY
;
980 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
984 leaf
= path
->nodes
[0];
985 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
986 struct btrfs_dev_extent
);
987 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
988 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
989 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
991 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
992 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
995 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
996 btrfs_mark_buffer_dirty(leaf
);
997 btrfs_free_path(path
);
1001 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1002 u64 objectid
, u64
*offset
)
1004 struct btrfs_path
*path
;
1006 struct btrfs_key key
;
1007 struct btrfs_chunk
*chunk
;
1008 struct btrfs_key found_key
;
1010 path
= btrfs_alloc_path();
1013 key
.objectid
= objectid
;
1014 key
.offset
= (u64
)-1;
1015 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1017 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1023 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1027 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1029 if (found_key
.objectid
!= objectid
)
1032 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1033 struct btrfs_chunk
);
1034 *offset
= found_key
.offset
+
1035 btrfs_chunk_length(path
->nodes
[0], chunk
);
1040 btrfs_free_path(path
);
1044 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1047 struct btrfs_key key
;
1048 struct btrfs_key found_key
;
1049 struct btrfs_path
*path
;
1051 root
= root
->fs_info
->chunk_root
;
1053 path
= btrfs_alloc_path();
1057 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1058 key
.type
= BTRFS_DEV_ITEM_KEY
;
1059 key
.offset
= (u64
)-1;
1061 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1067 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1068 BTRFS_DEV_ITEM_KEY
);
1072 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1074 *objectid
= found_key
.offset
+ 1;
1078 btrfs_free_path(path
);
1083 * the device information is stored in the chunk root
1084 * the btrfs_device struct should be fully filled in
1086 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1087 struct btrfs_root
*root
,
1088 struct btrfs_device
*device
)
1091 struct btrfs_path
*path
;
1092 struct btrfs_dev_item
*dev_item
;
1093 struct extent_buffer
*leaf
;
1094 struct btrfs_key key
;
1097 root
= root
->fs_info
->chunk_root
;
1099 path
= btrfs_alloc_path();
1103 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1104 key
.type
= BTRFS_DEV_ITEM_KEY
;
1105 key
.offset
= device
->devid
;
1107 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1112 leaf
= path
->nodes
[0];
1113 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1115 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1116 btrfs_set_device_generation(leaf
, dev_item
, 0);
1117 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1118 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1119 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1120 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1121 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1122 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1123 btrfs_set_device_group(leaf
, dev_item
, 0);
1124 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1125 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1126 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1128 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1129 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1130 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1131 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1132 btrfs_mark_buffer_dirty(leaf
);
1136 btrfs_free_path(path
);
1140 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1141 struct btrfs_device
*device
)
1144 struct btrfs_path
*path
;
1145 struct btrfs_key key
;
1146 struct btrfs_trans_handle
*trans
;
1148 root
= root
->fs_info
->chunk_root
;
1150 path
= btrfs_alloc_path();
1154 trans
= btrfs_start_transaction(root
, 0);
1155 if (IS_ERR(trans
)) {
1156 btrfs_free_path(path
);
1157 return PTR_ERR(trans
);
1159 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1160 key
.type
= BTRFS_DEV_ITEM_KEY
;
1161 key
.offset
= device
->devid
;
1164 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1173 ret
= btrfs_del_item(trans
, root
, path
);
1177 btrfs_free_path(path
);
1178 unlock_chunks(root
);
1179 btrfs_commit_transaction(trans
, root
);
1183 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1185 struct btrfs_device
*device
;
1186 struct btrfs_device
*next_device
;
1187 struct block_device
*bdev
;
1188 struct buffer_head
*bh
= NULL
;
1189 struct btrfs_super_block
*disk_super
;
1196 mutex_lock(&uuid_mutex
);
1197 mutex_lock(&root
->fs_info
->volume_mutex
);
1199 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1200 root
->fs_info
->avail_system_alloc_bits
|
1201 root
->fs_info
->avail_metadata_alloc_bits
;
1203 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1204 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1205 printk(KERN_ERR
"btrfs: unable to go below four devices "
1211 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1212 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1213 printk(KERN_ERR
"btrfs: unable to go below two "
1214 "devices on raid1\n");
1219 if (strcmp(device_path
, "missing") == 0) {
1220 struct list_head
*devices
;
1221 struct btrfs_device
*tmp
;
1224 devices
= &root
->fs_info
->fs_devices
->devices
;
1225 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1226 list_for_each_entry(tmp
, devices
, dev_list
) {
1227 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1232 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1237 printk(KERN_ERR
"btrfs: no missing devices found to "
1242 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1243 root
->fs_info
->bdev_holder
);
1245 ret
= PTR_ERR(bdev
);
1249 set_blocksize(bdev
, 4096);
1250 bh
= btrfs_read_dev_super(bdev
);
1255 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1256 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1257 dev_uuid
= disk_super
->dev_item
.uuid
;
1258 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1266 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1267 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1273 if (device
->writeable
) {
1274 list_del_init(&device
->dev_alloc_list
);
1275 root
->fs_info
->fs_devices
->rw_devices
--;
1278 ret
= btrfs_shrink_device(device
, 0);
1282 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1286 device
->in_fs_metadata
= 0;
1287 btrfs_scrub_cancel_dev(root
, device
);
1290 * the device list mutex makes sure that we don't change
1291 * the device list while someone else is writing out all
1292 * the device supers.
1294 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1295 list_del_init(&device
->dev_list
);
1296 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1298 device
->fs_devices
->num_devices
--;
1300 if (device
->missing
)
1301 root
->fs_info
->fs_devices
->missing_devices
--;
1303 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1304 struct btrfs_device
, dev_list
);
1305 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1306 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1307 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1308 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1311 blkdev_put(device
->bdev
, device
->mode
);
1312 device
->bdev
= NULL
;
1313 device
->fs_devices
->open_devices
--;
1316 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1317 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1319 if (device
->fs_devices
->open_devices
== 0) {
1320 struct btrfs_fs_devices
*fs_devices
;
1321 fs_devices
= root
->fs_info
->fs_devices
;
1322 while (fs_devices
) {
1323 if (fs_devices
->seed
== device
->fs_devices
)
1325 fs_devices
= fs_devices
->seed
;
1327 fs_devices
->seed
= device
->fs_devices
->seed
;
1328 device
->fs_devices
->seed
= NULL
;
1329 __btrfs_close_devices(device
->fs_devices
);
1330 free_fs_devices(device
->fs_devices
);
1334 * at this point, the device is zero sized. We want to
1335 * remove it from the devices list and zero out the old super
1337 if (device
->writeable
) {
1338 /* make sure this device isn't detected as part of
1341 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1342 set_buffer_dirty(bh
);
1343 sync_dirty_buffer(bh
);
1346 kfree(device
->name
);
1354 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1356 mutex_unlock(&root
->fs_info
->volume_mutex
);
1357 mutex_unlock(&uuid_mutex
);
1360 if (device
->writeable
) {
1361 list_add(&device
->dev_alloc_list
,
1362 &root
->fs_info
->fs_devices
->alloc_list
);
1363 root
->fs_info
->fs_devices
->rw_devices
++;
1369 * does all the dirty work required for changing file system's UUID.
1371 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1372 struct btrfs_root
*root
)
1374 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1375 struct btrfs_fs_devices
*old_devices
;
1376 struct btrfs_fs_devices
*seed_devices
;
1377 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1378 struct btrfs_device
*device
;
1381 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1382 if (!fs_devices
->seeding
)
1385 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1389 old_devices
= clone_fs_devices(fs_devices
);
1390 if (IS_ERR(old_devices
)) {
1391 kfree(seed_devices
);
1392 return PTR_ERR(old_devices
);
1395 list_add(&old_devices
->list
, &fs_uuids
);
1397 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1398 seed_devices
->opened
= 1;
1399 INIT_LIST_HEAD(&seed_devices
->devices
);
1400 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1401 mutex_init(&seed_devices
->device_list_mutex
);
1402 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1403 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1404 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1405 device
->fs_devices
= seed_devices
;
1408 fs_devices
->seeding
= 0;
1409 fs_devices
->num_devices
= 0;
1410 fs_devices
->open_devices
= 0;
1411 fs_devices
->seed
= seed_devices
;
1413 generate_random_uuid(fs_devices
->fsid
);
1414 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1415 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1416 super_flags
= btrfs_super_flags(disk_super
) &
1417 ~BTRFS_SUPER_FLAG_SEEDING
;
1418 btrfs_set_super_flags(disk_super
, super_flags
);
1424 * strore the expected generation for seed devices in device items.
1426 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1427 struct btrfs_root
*root
)
1429 struct btrfs_path
*path
;
1430 struct extent_buffer
*leaf
;
1431 struct btrfs_dev_item
*dev_item
;
1432 struct btrfs_device
*device
;
1433 struct btrfs_key key
;
1434 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1435 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1439 path
= btrfs_alloc_path();
1443 root
= root
->fs_info
->chunk_root
;
1444 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1446 key
.type
= BTRFS_DEV_ITEM_KEY
;
1449 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1453 leaf
= path
->nodes
[0];
1455 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1456 ret
= btrfs_next_leaf(root
, path
);
1461 leaf
= path
->nodes
[0];
1462 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1463 btrfs_release_path(path
);
1467 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1468 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1469 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1472 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1473 struct btrfs_dev_item
);
1474 devid
= btrfs_device_id(leaf
, dev_item
);
1475 read_extent_buffer(leaf
, dev_uuid
,
1476 (unsigned long)btrfs_device_uuid(dev_item
),
1478 read_extent_buffer(leaf
, fs_uuid
,
1479 (unsigned long)btrfs_device_fsid(dev_item
),
1481 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1484 if (device
->fs_devices
->seeding
) {
1485 btrfs_set_device_generation(leaf
, dev_item
,
1486 device
->generation
);
1487 btrfs_mark_buffer_dirty(leaf
);
1495 btrfs_free_path(path
);
1499 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1501 struct btrfs_trans_handle
*trans
;
1502 struct btrfs_device
*device
;
1503 struct block_device
*bdev
;
1504 struct list_head
*devices
;
1505 struct super_block
*sb
= root
->fs_info
->sb
;
1507 int seeding_dev
= 0;
1510 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1513 bdev
= blkdev_get_by_path(device_path
, FMODE_EXCL
,
1514 root
->fs_info
->bdev_holder
);
1516 return PTR_ERR(bdev
);
1518 if (root
->fs_info
->fs_devices
->seeding
) {
1520 down_write(&sb
->s_umount
);
1521 mutex_lock(&uuid_mutex
);
1524 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1525 mutex_lock(&root
->fs_info
->volume_mutex
);
1527 devices
= &root
->fs_info
->fs_devices
->devices
;
1529 * we have the volume lock, so we don't need the extra
1530 * device list mutex while reading the list here.
1532 list_for_each_entry(device
, devices
, dev_list
) {
1533 if (device
->bdev
== bdev
) {
1539 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1541 /* we can safely leave the fs_devices entry around */
1546 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1547 if (!device
->name
) {
1553 ret
= find_next_devid(root
, &device
->devid
);
1555 kfree(device
->name
);
1560 trans
= btrfs_start_transaction(root
, 0);
1561 if (IS_ERR(trans
)) {
1562 kfree(device
->name
);
1564 ret
= PTR_ERR(trans
);
1570 device
->writeable
= 1;
1571 device
->work
.func
= pending_bios_fn
;
1572 generate_random_uuid(device
->uuid
);
1573 spin_lock_init(&device
->io_lock
);
1574 device
->generation
= trans
->transid
;
1575 device
->io_width
= root
->sectorsize
;
1576 device
->io_align
= root
->sectorsize
;
1577 device
->sector_size
= root
->sectorsize
;
1578 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1579 device
->disk_total_bytes
= device
->total_bytes
;
1580 device
->dev_root
= root
->fs_info
->dev_root
;
1581 device
->bdev
= bdev
;
1582 device
->in_fs_metadata
= 1;
1583 device
->mode
= FMODE_EXCL
;
1584 set_blocksize(device
->bdev
, 4096);
1587 sb
->s_flags
&= ~MS_RDONLY
;
1588 ret
= btrfs_prepare_sprout(trans
, root
);
1592 device
->fs_devices
= root
->fs_info
->fs_devices
;
1595 * we don't want write_supers to jump in here with our device
1598 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1599 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1600 list_add(&device
->dev_alloc_list
,
1601 &root
->fs_info
->fs_devices
->alloc_list
);
1602 root
->fs_info
->fs_devices
->num_devices
++;
1603 root
->fs_info
->fs_devices
->open_devices
++;
1604 root
->fs_info
->fs_devices
->rw_devices
++;
1605 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1607 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1608 root
->fs_info
->fs_devices
->rotating
= 1;
1610 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1611 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1612 total_bytes
+ device
->total_bytes
);
1614 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1615 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1617 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1620 ret
= init_first_rw_device(trans
, root
, device
);
1622 ret
= btrfs_finish_sprout(trans
, root
);
1625 ret
= btrfs_add_device(trans
, root
, device
);
1629 * we've got more storage, clear any full flags on the space
1632 btrfs_clear_space_info_full(root
->fs_info
);
1634 unlock_chunks(root
);
1635 btrfs_commit_transaction(trans
, root
);
1638 mutex_unlock(&uuid_mutex
);
1639 up_write(&sb
->s_umount
);
1641 ret
= btrfs_relocate_sys_chunks(root
);
1645 mutex_unlock(&root
->fs_info
->volume_mutex
);
1648 blkdev_put(bdev
, FMODE_EXCL
);
1650 mutex_unlock(&uuid_mutex
);
1651 up_write(&sb
->s_umount
);
1656 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1657 struct btrfs_device
*device
)
1660 struct btrfs_path
*path
;
1661 struct btrfs_root
*root
;
1662 struct btrfs_dev_item
*dev_item
;
1663 struct extent_buffer
*leaf
;
1664 struct btrfs_key key
;
1666 root
= device
->dev_root
->fs_info
->chunk_root
;
1668 path
= btrfs_alloc_path();
1672 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1673 key
.type
= BTRFS_DEV_ITEM_KEY
;
1674 key
.offset
= device
->devid
;
1676 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1685 leaf
= path
->nodes
[0];
1686 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1688 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1689 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1690 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1691 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1692 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1693 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1694 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1695 btrfs_mark_buffer_dirty(leaf
);
1698 btrfs_free_path(path
);
1702 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1703 struct btrfs_device
*device
, u64 new_size
)
1705 struct btrfs_super_block
*super_copy
=
1706 &device
->dev_root
->fs_info
->super_copy
;
1707 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1708 u64 diff
= new_size
- device
->total_bytes
;
1710 if (!device
->writeable
)
1712 if (new_size
<= device
->total_bytes
)
1715 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1716 device
->fs_devices
->total_rw_bytes
+= diff
;
1718 device
->total_bytes
= new_size
;
1719 device
->disk_total_bytes
= new_size
;
1720 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1722 return btrfs_update_device(trans
, device
);
1725 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1726 struct btrfs_device
*device
, u64 new_size
)
1729 lock_chunks(device
->dev_root
);
1730 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1731 unlock_chunks(device
->dev_root
);
1735 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1736 struct btrfs_root
*root
,
1737 u64 chunk_tree
, u64 chunk_objectid
,
1741 struct btrfs_path
*path
;
1742 struct btrfs_key key
;
1744 root
= root
->fs_info
->chunk_root
;
1745 path
= btrfs_alloc_path();
1749 key
.objectid
= chunk_objectid
;
1750 key
.offset
= chunk_offset
;
1751 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1753 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1756 ret
= btrfs_del_item(trans
, root
, path
);
1759 btrfs_free_path(path
);
1763 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1766 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1767 struct btrfs_disk_key
*disk_key
;
1768 struct btrfs_chunk
*chunk
;
1775 struct btrfs_key key
;
1777 array_size
= btrfs_super_sys_array_size(super_copy
);
1779 ptr
= super_copy
->sys_chunk_array
;
1782 while (cur
< array_size
) {
1783 disk_key
= (struct btrfs_disk_key
*)ptr
;
1784 btrfs_disk_key_to_cpu(&key
, disk_key
);
1786 len
= sizeof(*disk_key
);
1788 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1789 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1790 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1791 len
+= btrfs_chunk_item_size(num_stripes
);
1796 if (key
.objectid
== chunk_objectid
&&
1797 key
.offset
== chunk_offset
) {
1798 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1800 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1809 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1810 u64 chunk_tree
, u64 chunk_objectid
,
1813 struct extent_map_tree
*em_tree
;
1814 struct btrfs_root
*extent_root
;
1815 struct btrfs_trans_handle
*trans
;
1816 struct extent_map
*em
;
1817 struct map_lookup
*map
;
1821 root
= root
->fs_info
->chunk_root
;
1822 extent_root
= root
->fs_info
->extent_root
;
1823 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1825 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1829 /* step one, relocate all the extents inside this chunk */
1830 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1834 trans
= btrfs_start_transaction(root
, 0);
1835 BUG_ON(IS_ERR(trans
));
1840 * step two, delete the device extents and the
1841 * chunk tree entries
1843 read_lock(&em_tree
->lock
);
1844 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1845 read_unlock(&em_tree
->lock
);
1847 BUG_ON(em
->start
> chunk_offset
||
1848 em
->start
+ em
->len
< chunk_offset
);
1849 map
= (struct map_lookup
*)em
->bdev
;
1851 for (i
= 0; i
< map
->num_stripes
; i
++) {
1852 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1853 map
->stripes
[i
].physical
);
1856 if (map
->stripes
[i
].dev
) {
1857 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1861 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1866 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1868 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1869 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1873 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1876 write_lock(&em_tree
->lock
);
1877 remove_extent_mapping(em_tree
, em
);
1878 write_unlock(&em_tree
->lock
);
1883 /* once for the tree */
1884 free_extent_map(em
);
1886 free_extent_map(em
);
1888 unlock_chunks(root
);
1889 btrfs_end_transaction(trans
, root
);
1893 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1895 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1896 struct btrfs_path
*path
;
1897 struct extent_buffer
*leaf
;
1898 struct btrfs_chunk
*chunk
;
1899 struct btrfs_key key
;
1900 struct btrfs_key found_key
;
1901 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1903 bool retried
= false;
1907 path
= btrfs_alloc_path();
1912 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1913 key
.offset
= (u64
)-1;
1914 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1917 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1922 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1929 leaf
= path
->nodes
[0];
1930 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1932 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1933 struct btrfs_chunk
);
1934 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1935 btrfs_release_path(path
);
1937 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1938 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1947 if (found_key
.offset
== 0)
1949 key
.offset
= found_key
.offset
- 1;
1952 if (failed
&& !retried
) {
1956 } else if (failed
&& retried
) {
1961 btrfs_free_path(path
);
1965 static u64
div_factor(u64 num
, int factor
)
1974 int btrfs_balance(struct btrfs_root
*dev_root
)
1977 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1978 struct btrfs_device
*device
;
1981 struct btrfs_path
*path
;
1982 struct btrfs_key key
;
1983 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1984 struct btrfs_trans_handle
*trans
;
1985 struct btrfs_key found_key
;
1987 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1990 if (!capable(CAP_SYS_ADMIN
))
1993 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1994 dev_root
= dev_root
->fs_info
->dev_root
;
1996 /* step one make some room on all the devices */
1997 list_for_each_entry(device
, devices
, dev_list
) {
1998 old_size
= device
->total_bytes
;
1999 size_to_free
= div_factor(old_size
, 1);
2000 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2001 if (!device
->writeable
||
2002 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2005 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2010 trans
= btrfs_start_transaction(dev_root
, 0);
2011 BUG_ON(IS_ERR(trans
));
2013 ret
= btrfs_grow_device(trans
, device
, old_size
);
2016 btrfs_end_transaction(trans
, dev_root
);
2019 /* step two, relocate all the chunks */
2020 path
= btrfs_alloc_path();
2023 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2024 key
.offset
= (u64
)-1;
2025 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2028 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2033 * this shouldn't happen, it means the last relocate
2039 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2040 BTRFS_CHUNK_ITEM_KEY
);
2044 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2046 if (found_key
.objectid
!= key
.objectid
)
2049 /* chunk zero is special */
2050 if (found_key
.offset
== 0)
2053 btrfs_release_path(path
);
2054 ret
= btrfs_relocate_chunk(chunk_root
,
2055 chunk_root
->root_key
.objectid
,
2058 BUG_ON(ret
&& ret
!= -ENOSPC
);
2059 key
.offset
= found_key
.offset
- 1;
2063 btrfs_free_path(path
);
2064 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2069 * shrinking a device means finding all of the device extents past
2070 * the new size, and then following the back refs to the chunks.
2071 * The chunk relocation code actually frees the device extent
2073 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2075 struct btrfs_trans_handle
*trans
;
2076 struct btrfs_root
*root
= device
->dev_root
;
2077 struct btrfs_dev_extent
*dev_extent
= NULL
;
2078 struct btrfs_path
*path
;
2086 bool retried
= false;
2087 struct extent_buffer
*l
;
2088 struct btrfs_key key
;
2089 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2090 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2091 u64 old_size
= device
->total_bytes
;
2092 u64 diff
= device
->total_bytes
- new_size
;
2094 if (new_size
>= device
->total_bytes
)
2097 path
= btrfs_alloc_path();
2105 device
->total_bytes
= new_size
;
2106 if (device
->writeable
)
2107 device
->fs_devices
->total_rw_bytes
-= diff
;
2108 unlock_chunks(root
);
2111 key
.objectid
= device
->devid
;
2112 key
.offset
= (u64
)-1;
2113 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2116 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2120 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2125 btrfs_release_path(path
);
2130 slot
= path
->slots
[0];
2131 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2133 if (key
.objectid
!= device
->devid
) {
2134 btrfs_release_path(path
);
2138 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2139 length
= btrfs_dev_extent_length(l
, dev_extent
);
2141 if (key
.offset
+ length
<= new_size
) {
2142 btrfs_release_path(path
);
2146 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2147 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2148 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2149 btrfs_release_path(path
);
2151 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2153 if (ret
&& ret
!= -ENOSPC
)
2160 if (failed
&& !retried
) {
2164 } else if (failed
&& retried
) {
2168 device
->total_bytes
= old_size
;
2169 if (device
->writeable
)
2170 device
->fs_devices
->total_rw_bytes
+= diff
;
2171 unlock_chunks(root
);
2175 /* Shrinking succeeded, else we would be at "done". */
2176 trans
= btrfs_start_transaction(root
, 0);
2177 if (IS_ERR(trans
)) {
2178 ret
= PTR_ERR(trans
);
2184 device
->disk_total_bytes
= new_size
;
2185 /* Now btrfs_update_device() will change the on-disk size. */
2186 ret
= btrfs_update_device(trans
, device
);
2188 unlock_chunks(root
);
2189 btrfs_end_transaction(trans
, root
);
2192 WARN_ON(diff
> old_total
);
2193 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2194 unlock_chunks(root
);
2195 btrfs_end_transaction(trans
, root
);
2197 btrfs_free_path(path
);
2201 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2202 struct btrfs_root
*root
,
2203 struct btrfs_key
*key
,
2204 struct btrfs_chunk
*chunk
, int item_size
)
2206 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2207 struct btrfs_disk_key disk_key
;
2211 array_size
= btrfs_super_sys_array_size(super_copy
);
2212 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2215 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2216 btrfs_cpu_key_to_disk(&disk_key
, key
);
2217 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2218 ptr
+= sizeof(disk_key
);
2219 memcpy(ptr
, chunk
, item_size
);
2220 item_size
+= sizeof(disk_key
);
2221 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2226 * sort the devices in descending order by max_avail, total_avail
2228 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2230 const struct btrfs_device_info
*di_a
= a
;
2231 const struct btrfs_device_info
*di_b
= b
;
2233 if (di_a
->max_avail
> di_b
->max_avail
)
2235 if (di_a
->max_avail
< di_b
->max_avail
)
2237 if (di_a
->total_avail
> di_b
->total_avail
)
2239 if (di_a
->total_avail
< di_b
->total_avail
)
2244 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2245 struct btrfs_root
*extent_root
,
2246 struct map_lookup
**map_ret
,
2247 u64
*num_bytes_out
, u64
*stripe_size_out
,
2248 u64 start
, u64 type
)
2250 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2251 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2252 struct list_head
*cur
;
2253 struct map_lookup
*map
= NULL
;
2254 struct extent_map_tree
*em_tree
;
2255 struct extent_map
*em
;
2256 struct btrfs_device_info
*devices_info
= NULL
;
2258 int num_stripes
; /* total number of stripes to allocate */
2259 int sub_stripes
; /* sub_stripes info for map */
2260 int dev_stripes
; /* stripes per dev */
2261 int devs_max
; /* max devs to use */
2262 int devs_min
; /* min devs needed */
2263 int devs_increment
; /* ndevs has to be a multiple of this */
2264 int ncopies
; /* how many copies to data has */
2266 u64 max_stripe_size
;
2274 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2275 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2277 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2280 if (list_empty(&fs_devices
->alloc_list
))
2287 devs_max
= 0; /* 0 == as many as possible */
2291 * define the properties of each RAID type.
2292 * FIXME: move this to a global table and use it in all RAID
2295 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2299 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2301 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2306 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2315 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2316 max_stripe_size
= 1024 * 1024 * 1024;
2317 max_chunk_size
= 10 * max_stripe_size
;
2318 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2319 max_stripe_size
= 256 * 1024 * 1024;
2320 max_chunk_size
= max_stripe_size
;
2321 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2322 max_stripe_size
= 8 * 1024 * 1024;
2323 max_chunk_size
= 2 * max_stripe_size
;
2325 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
2330 /* we don't want a chunk larger than 10% of writeable space */
2331 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2334 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2339 cur
= fs_devices
->alloc_list
.next
;
2342 * in the first pass through the devices list, we gather information
2343 * about the available holes on each device.
2346 while (cur
!= &fs_devices
->alloc_list
) {
2347 struct btrfs_device
*device
;
2351 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2355 if (!device
->writeable
) {
2357 "btrfs: read-only device in alloc_list\n");
2362 if (!device
->in_fs_metadata
)
2365 if (device
->total_bytes
> device
->bytes_used
)
2366 total_avail
= device
->total_bytes
- device
->bytes_used
;
2369 /* avail is off by max(alloc_start, 1MB), but that is the same
2370 * for all devices, so it doesn't hurt the sorting later on
2373 ret
= find_free_dev_extent(trans
, device
,
2374 max_stripe_size
* dev_stripes
,
2375 &dev_offset
, &max_avail
);
2376 if (ret
&& ret
!= -ENOSPC
)
2380 max_avail
= max_stripe_size
* dev_stripes
;
2382 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
2385 devices_info
[ndevs
].dev_offset
= dev_offset
;
2386 devices_info
[ndevs
].max_avail
= max_avail
;
2387 devices_info
[ndevs
].total_avail
= total_avail
;
2388 devices_info
[ndevs
].dev
= device
;
2393 * now sort the devices by hole size / available space
2395 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
2396 btrfs_cmp_device_info
, NULL
);
2398 /* round down to number of usable stripes */
2399 ndevs
-= ndevs
% devs_increment
;
2401 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
2406 if (devs_max
&& ndevs
> devs_max
)
2409 * the primary goal is to maximize the number of stripes, so use as many
2410 * devices as possible, even if the stripes are not maximum sized.
2412 stripe_size
= devices_info
[ndevs
-1].max_avail
;
2413 num_stripes
= ndevs
* dev_stripes
;
2415 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
2416 stripe_size
= max_chunk_size
* ncopies
;
2417 do_div(stripe_size
, num_stripes
);
2420 do_div(stripe_size
, dev_stripes
);
2421 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
2422 stripe_size
*= BTRFS_STRIPE_LEN
;
2424 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2429 map
->num_stripes
= num_stripes
;
2431 for (i
= 0; i
< ndevs
; ++i
) {
2432 for (j
= 0; j
< dev_stripes
; ++j
) {
2433 int s
= i
* dev_stripes
+ j
;
2434 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
2435 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
2439 map
->sector_size
= extent_root
->sectorsize
;
2440 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2441 map
->io_align
= BTRFS_STRIPE_LEN
;
2442 map
->io_width
= BTRFS_STRIPE_LEN
;
2444 map
->sub_stripes
= sub_stripes
;
2447 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
2449 *stripe_size_out
= stripe_size
;
2450 *num_bytes_out
= num_bytes
;
2452 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
2454 em
= alloc_extent_map();
2459 em
->bdev
= (struct block_device
*)map
;
2461 em
->len
= num_bytes
;
2462 em
->block_start
= 0;
2463 em
->block_len
= em
->len
;
2465 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2466 write_lock(&em_tree
->lock
);
2467 ret
= add_extent_mapping(em_tree
, em
);
2468 write_unlock(&em_tree
->lock
);
2470 free_extent_map(em
);
2472 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2473 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2477 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2478 struct btrfs_device
*device
;
2481 device
= map
->stripes
[i
].dev
;
2482 dev_offset
= map
->stripes
[i
].physical
;
2484 ret
= btrfs_alloc_dev_extent(trans
, device
,
2485 info
->chunk_root
->root_key
.objectid
,
2486 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2487 start
, dev_offset
, stripe_size
);
2491 kfree(devices_info
);
2496 kfree(devices_info
);
2500 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2501 struct btrfs_root
*extent_root
,
2502 struct map_lookup
*map
, u64 chunk_offset
,
2503 u64 chunk_size
, u64 stripe_size
)
2506 struct btrfs_key key
;
2507 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2508 struct btrfs_device
*device
;
2509 struct btrfs_chunk
*chunk
;
2510 struct btrfs_stripe
*stripe
;
2511 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2515 chunk
= kzalloc(item_size
, GFP_NOFS
);
2520 while (index
< map
->num_stripes
) {
2521 device
= map
->stripes
[index
].dev
;
2522 device
->bytes_used
+= stripe_size
;
2523 ret
= btrfs_update_device(trans
, device
);
2529 stripe
= &chunk
->stripe
;
2530 while (index
< map
->num_stripes
) {
2531 device
= map
->stripes
[index
].dev
;
2532 dev_offset
= map
->stripes
[index
].physical
;
2534 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2535 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2536 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2541 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2542 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2543 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2544 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2545 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2546 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2547 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2548 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2549 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2551 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2552 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2553 key
.offset
= chunk_offset
;
2555 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2558 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2559 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2569 * Chunk allocation falls into two parts. The first part does works
2570 * that make the new allocated chunk useable, but not do any operation
2571 * that modifies the chunk tree. The second part does the works that
2572 * require modifying the chunk tree. This division is important for the
2573 * bootstrap process of adding storage to a seed btrfs.
2575 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2576 struct btrfs_root
*extent_root
, u64 type
)
2581 struct map_lookup
*map
;
2582 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2585 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2590 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2591 &stripe_size
, chunk_offset
, type
);
2595 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2596 chunk_size
, stripe_size
);
2601 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2602 struct btrfs_root
*root
,
2603 struct btrfs_device
*device
)
2606 u64 sys_chunk_offset
;
2610 u64 sys_stripe_size
;
2612 struct map_lookup
*map
;
2613 struct map_lookup
*sys_map
;
2614 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2615 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2618 ret
= find_next_chunk(fs_info
->chunk_root
,
2619 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2622 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2623 (fs_info
->metadata_alloc_profile
&
2624 fs_info
->avail_metadata_alloc_bits
);
2625 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2627 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2628 &stripe_size
, chunk_offset
, alloc_profile
);
2631 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2633 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2634 (fs_info
->system_alloc_profile
&
2635 fs_info
->avail_system_alloc_bits
);
2636 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2638 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2639 &sys_chunk_size
, &sys_stripe_size
,
2640 sys_chunk_offset
, alloc_profile
);
2643 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2647 * Modifying chunk tree needs allocating new blocks from both
2648 * system block group and metadata block group. So we only can
2649 * do operations require modifying the chunk tree after both
2650 * block groups were created.
2652 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2653 chunk_size
, stripe_size
);
2656 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2657 sys_chunk_offset
, sys_chunk_size
,
2663 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2665 struct extent_map
*em
;
2666 struct map_lookup
*map
;
2667 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2671 read_lock(&map_tree
->map_tree
.lock
);
2672 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2673 read_unlock(&map_tree
->map_tree
.lock
);
2677 if (btrfs_test_opt(root
, DEGRADED
)) {
2678 free_extent_map(em
);
2682 map
= (struct map_lookup
*)em
->bdev
;
2683 for (i
= 0; i
< map
->num_stripes
; i
++) {
2684 if (!map
->stripes
[i
].dev
->writeable
) {
2689 free_extent_map(em
);
2693 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2695 extent_map_tree_init(&tree
->map_tree
);
2698 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2700 struct extent_map
*em
;
2703 write_lock(&tree
->map_tree
.lock
);
2704 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2706 remove_extent_mapping(&tree
->map_tree
, em
);
2707 write_unlock(&tree
->map_tree
.lock
);
2712 free_extent_map(em
);
2713 /* once for the tree */
2714 free_extent_map(em
);
2718 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2720 struct extent_map
*em
;
2721 struct map_lookup
*map
;
2722 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2725 read_lock(&em_tree
->lock
);
2726 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2727 read_unlock(&em_tree
->lock
);
2730 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2731 map
= (struct map_lookup
*)em
->bdev
;
2732 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2733 ret
= map
->num_stripes
;
2734 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2735 ret
= map
->sub_stripes
;
2738 free_extent_map(em
);
2742 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2746 if (map
->stripes
[optimal
].dev
->bdev
)
2748 for (i
= first
; i
< first
+ num
; i
++) {
2749 if (map
->stripes
[i
].dev
->bdev
)
2752 /* we couldn't find one that doesn't fail. Just return something
2753 * and the io error handling code will clean up eventually
2758 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2759 u64 logical
, u64
*length
,
2760 struct btrfs_multi_bio
**multi_ret
,
2763 struct extent_map
*em
;
2764 struct map_lookup
*map
;
2765 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2768 u64 stripe_end_offset
;
2772 int stripes_allocated
= 8;
2773 int stripes_required
= 1;
2778 struct btrfs_multi_bio
*multi
= NULL
;
2780 if (multi_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2781 stripes_allocated
= 1;
2784 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2789 atomic_set(&multi
->error
, 0);
2792 read_lock(&em_tree
->lock
);
2793 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2794 read_unlock(&em_tree
->lock
);
2797 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2798 (unsigned long long)logical
,
2799 (unsigned long long)*length
);
2803 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2804 map
= (struct map_lookup
*)em
->bdev
;
2805 offset
= logical
- em
->start
;
2807 if (mirror_num
> map
->num_stripes
)
2810 /* if our multi bio struct is too small, back off and try again */
2811 if (rw
& REQ_WRITE
) {
2812 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2813 BTRFS_BLOCK_GROUP_DUP
)) {
2814 stripes_required
= map
->num_stripes
;
2816 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2817 stripes_required
= map
->sub_stripes
;
2821 if (rw
& REQ_DISCARD
) {
2822 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2823 BTRFS_BLOCK_GROUP_RAID1
|
2824 BTRFS_BLOCK_GROUP_DUP
|
2825 BTRFS_BLOCK_GROUP_RAID10
)) {
2826 stripes_required
= map
->num_stripes
;
2829 if (multi_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2830 stripes_allocated
< stripes_required
) {
2831 stripes_allocated
= map
->num_stripes
;
2832 free_extent_map(em
);
2838 * stripe_nr counts the total number of stripes we have to stride
2839 * to get to this block
2841 do_div(stripe_nr
, map
->stripe_len
);
2843 stripe_offset
= stripe_nr
* map
->stripe_len
;
2844 BUG_ON(offset
< stripe_offset
);
2846 /* stripe_offset is the offset of this block in its stripe*/
2847 stripe_offset
= offset
- stripe_offset
;
2849 if (rw
& REQ_DISCARD
)
2850 *length
= min_t(u64
, em
->len
- offset
, *length
);
2851 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2852 BTRFS_BLOCK_GROUP_RAID1
|
2853 BTRFS_BLOCK_GROUP_RAID10
|
2854 BTRFS_BLOCK_GROUP_DUP
)) {
2855 /* we limit the length of each bio to what fits in a stripe */
2856 *length
= min_t(u64
, em
->len
- offset
,
2857 map
->stripe_len
- stripe_offset
);
2859 *length
= em
->len
- offset
;
2867 stripe_nr_orig
= stripe_nr
;
2868 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
2869 (~(map
->stripe_len
- 1));
2870 do_div(stripe_nr_end
, map
->stripe_len
);
2871 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
2873 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2874 if (rw
& REQ_DISCARD
)
2875 num_stripes
= min_t(u64
, map
->num_stripes
,
2876 stripe_nr_end
- stripe_nr_orig
);
2877 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2878 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2879 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2880 num_stripes
= map
->num_stripes
;
2881 else if (mirror_num
)
2882 stripe_index
= mirror_num
- 1;
2884 stripe_index
= find_live_mirror(map
, 0,
2886 current
->pid
% map
->num_stripes
);
2889 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2890 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
2891 num_stripes
= map
->num_stripes
;
2892 else if (mirror_num
)
2893 stripe_index
= mirror_num
- 1;
2895 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2896 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2898 stripe_index
= do_div(stripe_nr
, factor
);
2899 stripe_index
*= map
->sub_stripes
;
2902 num_stripes
= map
->sub_stripes
;
2903 else if (rw
& REQ_DISCARD
)
2904 num_stripes
= min_t(u64
, map
->sub_stripes
*
2905 (stripe_nr_end
- stripe_nr_orig
),
2907 else if (mirror_num
)
2908 stripe_index
+= mirror_num
- 1;
2910 stripe_index
= find_live_mirror(map
, stripe_index
,
2911 map
->sub_stripes
, stripe_index
+
2912 current
->pid
% map
->sub_stripes
);
2916 * after this do_div call, stripe_nr is the number of stripes
2917 * on this device we have to walk to find the data, and
2918 * stripe_index is the number of our device in the stripe array
2920 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2922 BUG_ON(stripe_index
>= map
->num_stripes
);
2924 if (rw
& REQ_DISCARD
) {
2925 for (i
= 0; i
< num_stripes
; i
++) {
2926 multi
->stripes
[i
].physical
=
2927 map
->stripes
[stripe_index
].physical
+
2928 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2929 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2931 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2933 u32 last_stripe
= 0;
2936 div_u64_rem(stripe_nr_end
- 1,
2940 for (j
= 0; j
< map
->num_stripes
; j
++) {
2943 div_u64_rem(stripe_nr_end
- 1 - j
,
2944 map
->num_stripes
, &test
);
2945 if (test
== stripe_index
)
2948 stripes
= stripe_nr_end
- 1 - j
;
2949 do_div(stripes
, map
->num_stripes
);
2950 multi
->stripes
[i
].length
= map
->stripe_len
*
2951 (stripes
- stripe_nr
+ 1);
2954 multi
->stripes
[i
].length
-=
2958 if (stripe_index
== last_stripe
)
2959 multi
->stripes
[i
].length
-=
2961 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2964 int factor
= map
->num_stripes
/
2966 u32 last_stripe
= 0;
2968 div_u64_rem(stripe_nr_end
- 1,
2969 factor
, &last_stripe
);
2970 last_stripe
*= map
->sub_stripes
;
2972 for (j
= 0; j
< factor
; j
++) {
2975 div_u64_rem(stripe_nr_end
- 1 - j
,
2979 stripe_index
/ map
->sub_stripes
)
2982 stripes
= stripe_nr_end
- 1 - j
;
2983 do_div(stripes
, factor
);
2984 multi
->stripes
[i
].length
= map
->stripe_len
*
2985 (stripes
- stripe_nr
+ 1);
2987 if (i
< map
->sub_stripes
) {
2988 multi
->stripes
[i
].length
-=
2990 if (i
== map
->sub_stripes
- 1)
2993 if (stripe_index
>= last_stripe
&&
2994 stripe_index
<= (last_stripe
+
2995 map
->sub_stripes
- 1)) {
2996 multi
->stripes
[i
].length
-=
3000 multi
->stripes
[i
].length
= *length
;
3003 if (stripe_index
== map
->num_stripes
) {
3004 /* This could only happen for RAID0/10 */
3010 for (i
= 0; i
< num_stripes
; i
++) {
3011 multi
->stripes
[i
].physical
=
3012 map
->stripes
[stripe_index
].physical
+
3014 stripe_nr
* map
->stripe_len
;
3015 multi
->stripes
[i
].dev
=
3016 map
->stripes
[stripe_index
].dev
;
3022 multi
->num_stripes
= num_stripes
;
3023 multi
->max_errors
= max_errors
;
3026 free_extent_map(em
);
3030 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3031 u64 logical
, u64
*length
,
3032 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3034 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3038 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3039 u64 chunk_start
, u64 physical
, u64 devid
,
3040 u64
**logical
, int *naddrs
, int *stripe_len
)
3042 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3043 struct extent_map
*em
;
3044 struct map_lookup
*map
;
3051 read_lock(&em_tree
->lock
);
3052 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3053 read_unlock(&em_tree
->lock
);
3055 BUG_ON(!em
|| em
->start
!= chunk_start
);
3056 map
= (struct map_lookup
*)em
->bdev
;
3059 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3060 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3061 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3062 do_div(length
, map
->num_stripes
);
3064 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3067 for (i
= 0; i
< map
->num_stripes
; i
++) {
3068 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3070 if (map
->stripes
[i
].physical
> physical
||
3071 map
->stripes
[i
].physical
+ length
<= physical
)
3074 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3075 do_div(stripe_nr
, map
->stripe_len
);
3077 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3078 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3079 do_div(stripe_nr
, map
->sub_stripes
);
3080 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3081 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3083 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3084 WARN_ON(nr
>= map
->num_stripes
);
3085 for (j
= 0; j
< nr
; j
++) {
3086 if (buf
[j
] == bytenr
)
3090 WARN_ON(nr
>= map
->num_stripes
);
3097 *stripe_len
= map
->stripe_len
;
3099 free_extent_map(em
);
3103 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3105 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3106 int is_orig_bio
= 0;
3109 atomic_inc(&multi
->error
);
3111 if (bio
== multi
->orig_bio
)
3114 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3117 bio
= multi
->orig_bio
;
3119 bio
->bi_private
= multi
->private;
3120 bio
->bi_end_io
= multi
->end_io
;
3121 /* only send an error to the higher layers if it is
3122 * beyond the tolerance of the multi-bio
3124 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3128 * this bio is actually up to date, we didn't
3129 * go over the max number of errors
3131 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3136 bio_endio(bio
, err
);
3137 } else if (!is_orig_bio
) {
3142 struct async_sched
{
3145 struct btrfs_fs_info
*info
;
3146 struct btrfs_work work
;
3150 * see run_scheduled_bios for a description of why bios are collected for
3153 * This will add one bio to the pending list for a device and make sure
3154 * the work struct is scheduled.
3156 static noinline
int schedule_bio(struct btrfs_root
*root
,
3157 struct btrfs_device
*device
,
3158 int rw
, struct bio
*bio
)
3160 int should_queue
= 1;
3161 struct btrfs_pending_bios
*pending_bios
;
3163 /* don't bother with additional async steps for reads, right now */
3164 if (!(rw
& REQ_WRITE
)) {
3166 submit_bio(rw
, bio
);
3172 * nr_async_bios allows us to reliably return congestion to the
3173 * higher layers. Otherwise, the async bio makes it appear we have
3174 * made progress against dirty pages when we've really just put it
3175 * on a queue for later
3177 atomic_inc(&root
->fs_info
->nr_async_bios
);
3178 WARN_ON(bio
->bi_next
);
3179 bio
->bi_next
= NULL
;
3182 spin_lock(&device
->io_lock
);
3183 if (bio
->bi_rw
& REQ_SYNC
)
3184 pending_bios
= &device
->pending_sync_bios
;
3186 pending_bios
= &device
->pending_bios
;
3188 if (pending_bios
->tail
)
3189 pending_bios
->tail
->bi_next
= bio
;
3191 pending_bios
->tail
= bio
;
3192 if (!pending_bios
->head
)
3193 pending_bios
->head
= bio
;
3194 if (device
->running_pending
)
3197 spin_unlock(&device
->io_lock
);
3200 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3205 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3206 int mirror_num
, int async_submit
)
3208 struct btrfs_mapping_tree
*map_tree
;
3209 struct btrfs_device
*dev
;
3210 struct bio
*first_bio
= bio
;
3211 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3214 struct btrfs_multi_bio
*multi
= NULL
;
3219 length
= bio
->bi_size
;
3220 map_tree
= &root
->fs_info
->mapping_tree
;
3221 map_length
= length
;
3223 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3227 total_devs
= multi
->num_stripes
;
3228 if (map_length
< length
) {
3229 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3230 "len %llu\n", (unsigned long long)logical
,
3231 (unsigned long long)length
,
3232 (unsigned long long)map_length
);
3235 multi
->end_io
= first_bio
->bi_end_io
;
3236 multi
->private = first_bio
->bi_private
;
3237 multi
->orig_bio
= first_bio
;
3238 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3240 while (dev_nr
< total_devs
) {
3241 if (total_devs
> 1) {
3242 if (dev_nr
< total_devs
- 1) {
3243 bio
= bio_clone(first_bio
, GFP_NOFS
);
3248 bio
->bi_private
= multi
;
3249 bio
->bi_end_io
= end_bio_multi_stripe
;
3251 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3252 dev
= multi
->stripes
[dev_nr
].dev
;
3253 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3254 bio
->bi_bdev
= dev
->bdev
;
3256 schedule_bio(root
, dev
, rw
, bio
);
3258 submit_bio(rw
, bio
);
3260 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3261 bio
->bi_sector
= logical
>> 9;
3262 bio_endio(bio
, -EIO
);
3266 if (total_devs
== 1)
3271 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3274 struct btrfs_device
*device
;
3275 struct btrfs_fs_devices
*cur_devices
;
3277 cur_devices
= root
->fs_info
->fs_devices
;
3278 while (cur_devices
) {
3280 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3281 device
= __find_device(&cur_devices
->devices
,
3286 cur_devices
= cur_devices
->seed
;
3291 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3292 u64 devid
, u8
*dev_uuid
)
3294 struct btrfs_device
*device
;
3295 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3297 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3300 list_add(&device
->dev_list
,
3301 &fs_devices
->devices
);
3302 device
->dev_root
= root
->fs_info
->dev_root
;
3303 device
->devid
= devid
;
3304 device
->work
.func
= pending_bios_fn
;
3305 device
->fs_devices
= fs_devices
;
3306 device
->missing
= 1;
3307 fs_devices
->num_devices
++;
3308 fs_devices
->missing_devices
++;
3309 spin_lock_init(&device
->io_lock
);
3310 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3311 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3315 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3316 struct extent_buffer
*leaf
,
3317 struct btrfs_chunk
*chunk
)
3319 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3320 struct map_lookup
*map
;
3321 struct extent_map
*em
;
3325 u8 uuid
[BTRFS_UUID_SIZE
];
3330 logical
= key
->offset
;
3331 length
= btrfs_chunk_length(leaf
, chunk
);
3333 read_lock(&map_tree
->map_tree
.lock
);
3334 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3335 read_unlock(&map_tree
->map_tree
.lock
);
3337 /* already mapped? */
3338 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3339 free_extent_map(em
);
3342 free_extent_map(em
);
3345 em
= alloc_extent_map();
3348 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3349 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3351 free_extent_map(em
);
3355 em
->bdev
= (struct block_device
*)map
;
3356 em
->start
= logical
;
3358 em
->block_start
= 0;
3359 em
->block_len
= em
->len
;
3361 map
->num_stripes
= num_stripes
;
3362 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3363 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3364 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3365 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3366 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3367 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3368 for (i
= 0; i
< num_stripes
; i
++) {
3369 map
->stripes
[i
].physical
=
3370 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3371 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3372 read_extent_buffer(leaf
, uuid
, (unsigned long)
3373 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3375 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3377 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3379 free_extent_map(em
);
3382 if (!map
->stripes
[i
].dev
) {
3383 map
->stripes
[i
].dev
=
3384 add_missing_dev(root
, devid
, uuid
);
3385 if (!map
->stripes
[i
].dev
) {
3387 free_extent_map(em
);
3391 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3394 write_lock(&map_tree
->map_tree
.lock
);
3395 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3396 write_unlock(&map_tree
->map_tree
.lock
);
3398 free_extent_map(em
);
3403 static int fill_device_from_item(struct extent_buffer
*leaf
,
3404 struct btrfs_dev_item
*dev_item
,
3405 struct btrfs_device
*device
)
3409 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3410 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3411 device
->total_bytes
= device
->disk_total_bytes
;
3412 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3413 device
->type
= btrfs_device_type(leaf
, dev_item
);
3414 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3415 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3416 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3418 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3419 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3424 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3426 struct btrfs_fs_devices
*fs_devices
;
3429 mutex_lock(&uuid_mutex
);
3431 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3432 while (fs_devices
) {
3433 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3437 fs_devices
= fs_devices
->seed
;
3440 fs_devices
= find_fsid(fsid
);
3446 fs_devices
= clone_fs_devices(fs_devices
);
3447 if (IS_ERR(fs_devices
)) {
3448 ret
= PTR_ERR(fs_devices
);
3452 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3453 root
->fs_info
->bdev_holder
);
3457 if (!fs_devices
->seeding
) {
3458 __btrfs_close_devices(fs_devices
);
3459 free_fs_devices(fs_devices
);
3464 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3465 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3467 mutex_unlock(&uuid_mutex
);
3471 static int read_one_dev(struct btrfs_root
*root
,
3472 struct extent_buffer
*leaf
,
3473 struct btrfs_dev_item
*dev_item
)
3475 struct btrfs_device
*device
;
3478 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3479 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3481 devid
= btrfs_device_id(leaf
, dev_item
);
3482 read_extent_buffer(leaf
, dev_uuid
,
3483 (unsigned long)btrfs_device_uuid(dev_item
),
3485 read_extent_buffer(leaf
, fs_uuid
,
3486 (unsigned long)btrfs_device_fsid(dev_item
),
3489 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3490 ret
= open_seed_devices(root
, fs_uuid
);
3491 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3495 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3496 if (!device
|| !device
->bdev
) {
3497 if (!btrfs_test_opt(root
, DEGRADED
))
3501 printk(KERN_WARNING
"warning devid %llu missing\n",
3502 (unsigned long long)devid
);
3503 device
= add_missing_dev(root
, devid
, dev_uuid
);
3506 } else if (!device
->missing
) {
3508 * this happens when a device that was properly setup
3509 * in the device info lists suddenly goes bad.
3510 * device->bdev is NULL, and so we have to set
3511 * device->missing to one here
3513 root
->fs_info
->fs_devices
->missing_devices
++;
3514 device
->missing
= 1;
3518 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3519 BUG_ON(device
->writeable
);
3520 if (device
->generation
!=
3521 btrfs_device_generation(leaf
, dev_item
))
3525 fill_device_from_item(leaf
, dev_item
, device
);
3526 device
->dev_root
= root
->fs_info
->dev_root
;
3527 device
->in_fs_metadata
= 1;
3528 if (device
->writeable
)
3529 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3534 int btrfs_read_sys_array(struct btrfs_root
*root
)
3536 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3537 struct extent_buffer
*sb
;
3538 struct btrfs_disk_key
*disk_key
;
3539 struct btrfs_chunk
*chunk
;
3541 unsigned long sb_ptr
;
3547 struct btrfs_key key
;
3549 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3550 BTRFS_SUPER_INFO_SIZE
);
3553 btrfs_set_buffer_uptodate(sb
);
3554 btrfs_set_buffer_lockdep_class(sb
, 0);
3556 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3557 array_size
= btrfs_super_sys_array_size(super_copy
);
3559 ptr
= super_copy
->sys_chunk_array
;
3560 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3563 while (cur
< array_size
) {
3564 disk_key
= (struct btrfs_disk_key
*)ptr
;
3565 btrfs_disk_key_to_cpu(&key
, disk_key
);
3567 len
= sizeof(*disk_key
); ptr
+= len
;
3571 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3572 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3573 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3576 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3577 len
= btrfs_chunk_item_size(num_stripes
);
3586 free_extent_buffer(sb
);
3590 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3592 struct btrfs_path
*path
;
3593 struct extent_buffer
*leaf
;
3594 struct btrfs_key key
;
3595 struct btrfs_key found_key
;
3599 root
= root
->fs_info
->chunk_root
;
3601 path
= btrfs_alloc_path();
3605 /* first we search for all of the device items, and then we
3606 * read in all of the chunk items. This way we can create chunk
3607 * mappings that reference all of the devices that are afound
3609 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3613 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3617 leaf
= path
->nodes
[0];
3618 slot
= path
->slots
[0];
3619 if (slot
>= btrfs_header_nritems(leaf
)) {
3620 ret
= btrfs_next_leaf(root
, path
);
3627 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3628 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3629 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3631 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3632 struct btrfs_dev_item
*dev_item
;
3633 dev_item
= btrfs_item_ptr(leaf
, slot
,
3634 struct btrfs_dev_item
);
3635 ret
= read_one_dev(root
, leaf
, dev_item
);
3639 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3640 struct btrfs_chunk
*chunk
;
3641 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3642 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3648 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3650 btrfs_release_path(path
);
3655 btrfs_free_path(path
);