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/buffer_head.h>
21 #include <linux/blkdev.h>
22 #include <linux/random.h>
23 #include <linux/iocontext.h>
24 #include <asm/div64.h>
27 #include "extent_map.h"
29 #include "transaction.h"
30 #include "print-tree.h"
32 #include "async-thread.h"
42 struct btrfs_bio_stripe stripes
[];
45 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
46 struct btrfs_root
*root
,
47 struct btrfs_device
*device
);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
50 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
51 (sizeof(struct btrfs_bio_stripe) * (n)))
53 static DEFINE_MUTEX(uuid_mutex
);
54 static LIST_HEAD(fs_uuids
);
56 void btrfs_lock_volumes(void)
58 mutex_lock(&uuid_mutex
);
61 void btrfs_unlock_volumes(void)
63 mutex_unlock(&uuid_mutex
);
66 static void lock_chunks(struct btrfs_root
*root
)
68 mutex_lock(&root
->fs_info
->chunk_mutex
);
71 static void unlock_chunks(struct btrfs_root
*root
)
73 mutex_unlock(&root
->fs_info
->chunk_mutex
);
76 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
78 struct btrfs_device
*device
;
79 WARN_ON(fs_devices
->opened
);
80 while (!list_empty(&fs_devices
->devices
)) {
81 device
= list_entry(fs_devices
->devices
.next
,
82 struct btrfs_device
, dev_list
);
83 list_del(&device
->dev_list
);
90 int btrfs_cleanup_fs_uuids(void)
92 struct btrfs_fs_devices
*fs_devices
;
94 while (!list_empty(&fs_uuids
)) {
95 fs_devices
= list_entry(fs_uuids
.next
,
96 struct btrfs_fs_devices
, list
);
97 list_del(&fs_devices
->list
);
98 free_fs_devices(fs_devices
);
103 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
106 struct btrfs_device
*dev
;
108 list_for_each_entry(dev
, head
, dev_list
) {
109 if (dev
->devid
== devid
&&
110 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
117 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
119 struct btrfs_fs_devices
*fs_devices
;
121 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
122 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
128 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
129 struct bio
*head
, struct bio
*tail
)
132 struct bio
*old_head
;
134 old_head
= pending_bios
->head
;
135 pending_bios
->head
= head
;
136 if (pending_bios
->tail
)
137 tail
->bi_next
= old_head
;
139 pending_bios
->tail
= tail
;
143 * we try to collect pending bios for a device so we don't get a large
144 * number of procs sending bios down to the same device. This greatly
145 * improves the schedulers ability to collect and merge the bios.
147 * But, it also turns into a long list of bios to process and that is sure
148 * to eventually make the worker thread block. The solution here is to
149 * make some progress and then put this work struct back at the end of
150 * the list if the block device is congested. This way, multiple devices
151 * can make progress from a single worker thread.
153 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
156 struct backing_dev_info
*bdi
;
157 struct btrfs_fs_info
*fs_info
;
158 struct btrfs_pending_bios
*pending_bios
;
162 unsigned long num_run
;
163 unsigned long num_sync_run
;
165 unsigned long last_waited
= 0;
167 bdi
= blk_get_backing_dev_info(device
->bdev
);
168 fs_info
= device
->dev_root
->fs_info
;
169 limit
= btrfs_async_submit_limit(fs_info
);
170 limit
= limit
* 2 / 3;
172 /* we want to make sure that every time we switch from the sync
173 * list to the normal list, we unplug
178 spin_lock(&device
->io_lock
);
183 /* take all the bios off the list at once and process them
184 * later on (without the lock held). But, remember the
185 * tail and other pointers so the bios can be properly reinserted
186 * into the list if we hit congestion
188 if (device
->pending_sync_bios
.head
)
189 pending_bios
= &device
->pending_sync_bios
;
191 pending_bios
= &device
->pending_bios
;
193 pending
= pending_bios
->head
;
194 tail
= pending_bios
->tail
;
195 WARN_ON(pending
&& !tail
);
198 * if pending was null this time around, no bios need processing
199 * at all and we can stop. Otherwise it'll loop back up again
200 * and do an additional check so no bios are missed.
202 * device->running_pending is used to synchronize with the
205 if (device
->pending_sync_bios
.head
== NULL
&&
206 device
->pending_bios
.head
== NULL
) {
208 device
->running_pending
= 0;
211 device
->running_pending
= 1;
214 pending_bios
->head
= NULL
;
215 pending_bios
->tail
= NULL
;
217 spin_unlock(&device
->io_lock
);
220 * if we're doing the regular priority list, make sure we unplug
221 * for any high prio bios we've sent down
223 if (pending_bios
== &device
->pending_bios
&& num_sync_run
> 0) {
225 blk_run_backing_dev(bdi
, NULL
);
231 if (pending_bios
!= &device
->pending_sync_bios
&&
232 device
->pending_sync_bios
.head
&&
235 spin_lock(&device
->io_lock
);
236 requeue_list(pending_bios
, pending
, tail
);
241 pending
= pending
->bi_next
;
243 atomic_dec(&fs_info
->nr_async_bios
);
245 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
246 waitqueue_active(&fs_info
->async_submit_wait
))
247 wake_up(&fs_info
->async_submit_wait
);
249 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
250 submit_bio(cur
->bi_rw
, cur
);
255 if (need_resched()) {
257 blk_run_backing_dev(bdi
, NULL
);
264 * we made progress, there is more work to do and the bdi
265 * is now congested. Back off and let other work structs
268 if (pending
&& bdi_write_congested(bdi
) && num_run
> 16 &&
269 fs_info
->fs_devices
->open_devices
> 1) {
270 struct io_context
*ioc
;
272 ioc
= current
->io_context
;
275 * the main goal here is that we don't want to
276 * block if we're going to be able to submit
277 * more requests without blocking.
279 * This code does two great things, it pokes into
280 * the elevator code from a filesystem _and_
281 * it makes assumptions about how batching works.
283 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
284 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
286 ioc
->last_waited
== last_waited
)) {
288 * we want to go through our batch of
289 * requests and stop. So, we copy out
290 * the ioc->last_waited time and test
291 * against it before looping
293 last_waited
= ioc
->last_waited
;
294 if (need_resched()) {
296 blk_run_backing_dev(bdi
, NULL
);
303 spin_lock(&device
->io_lock
);
304 requeue_list(pending_bios
, pending
, tail
);
305 device
->running_pending
= 1;
307 spin_unlock(&device
->io_lock
);
308 btrfs_requeue_work(&device
->work
);
315 blk_run_backing_dev(bdi
, NULL
);
322 spin_lock(&device
->io_lock
);
323 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
325 spin_unlock(&device
->io_lock
);
328 * IO has already been through a long path to get here. Checksumming,
329 * async helper threads, perhaps compression. We've done a pretty
330 * good job of collecting a batch of IO and should just unplug
331 * the device right away.
333 * This will help anyone who is waiting on the IO, they might have
334 * already unplugged, but managed to do so before the bio they
335 * cared about found its way down here.
337 blk_run_backing_dev(bdi
, NULL
);
342 static void pending_bios_fn(struct btrfs_work
*work
)
344 struct btrfs_device
*device
;
346 device
= container_of(work
, struct btrfs_device
, work
);
347 run_scheduled_bios(device
);
350 static noinline
int device_list_add(const char *path
,
351 struct btrfs_super_block
*disk_super
,
352 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
354 struct btrfs_device
*device
;
355 struct btrfs_fs_devices
*fs_devices
;
356 u64 found_transid
= btrfs_super_generation(disk_super
);
358 fs_devices
= find_fsid(disk_super
->fsid
);
360 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
363 INIT_LIST_HEAD(&fs_devices
->devices
);
364 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
365 list_add(&fs_devices
->list
, &fs_uuids
);
366 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
367 fs_devices
->latest_devid
= devid
;
368 fs_devices
->latest_trans
= found_transid
;
371 device
= __find_device(&fs_devices
->devices
, devid
,
372 disk_super
->dev_item
.uuid
);
375 if (fs_devices
->opened
)
378 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
380 /* we can safely leave the fs_devices entry around */
383 device
->devid
= devid
;
384 device
->work
.func
= pending_bios_fn
;
385 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
387 device
->barriers
= 1;
388 spin_lock_init(&device
->io_lock
);
389 device
->name
= kstrdup(path
, GFP_NOFS
);
394 INIT_LIST_HEAD(&device
->dev_alloc_list
);
395 list_add(&device
->dev_list
, &fs_devices
->devices
);
396 device
->fs_devices
= fs_devices
;
397 fs_devices
->num_devices
++;
400 if (found_transid
> fs_devices
->latest_trans
) {
401 fs_devices
->latest_devid
= devid
;
402 fs_devices
->latest_trans
= found_transid
;
404 *fs_devices_ret
= fs_devices
;
408 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
410 struct btrfs_fs_devices
*fs_devices
;
411 struct btrfs_device
*device
;
412 struct btrfs_device
*orig_dev
;
414 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
416 return ERR_PTR(-ENOMEM
);
418 INIT_LIST_HEAD(&fs_devices
->devices
);
419 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
420 INIT_LIST_HEAD(&fs_devices
->list
);
421 fs_devices
->latest_devid
= orig
->latest_devid
;
422 fs_devices
->latest_trans
= orig
->latest_trans
;
423 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
425 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
426 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
430 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
434 device
->devid
= orig_dev
->devid
;
435 device
->work
.func
= pending_bios_fn
;
436 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
437 device
->barriers
= 1;
438 spin_lock_init(&device
->io_lock
);
439 INIT_LIST_HEAD(&device
->dev_list
);
440 INIT_LIST_HEAD(&device
->dev_alloc_list
);
442 list_add(&device
->dev_list
, &fs_devices
->devices
);
443 device
->fs_devices
= fs_devices
;
444 fs_devices
->num_devices
++;
448 free_fs_devices(fs_devices
);
449 return ERR_PTR(-ENOMEM
);
452 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
454 struct btrfs_device
*device
, *next
;
456 mutex_lock(&uuid_mutex
);
458 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
459 if (device
->in_fs_metadata
)
463 close_bdev_exclusive(device
->bdev
, device
->mode
);
465 fs_devices
->open_devices
--;
467 if (device
->writeable
) {
468 list_del_init(&device
->dev_alloc_list
);
469 device
->writeable
= 0;
470 fs_devices
->rw_devices
--;
472 list_del_init(&device
->dev_list
);
473 fs_devices
->num_devices
--;
478 if (fs_devices
->seed
) {
479 fs_devices
= fs_devices
->seed
;
483 mutex_unlock(&uuid_mutex
);
487 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
489 struct btrfs_device
*device
;
491 if (--fs_devices
->opened
> 0)
494 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
496 close_bdev_exclusive(device
->bdev
, device
->mode
);
497 fs_devices
->open_devices
--;
499 if (device
->writeable
) {
500 list_del_init(&device
->dev_alloc_list
);
501 fs_devices
->rw_devices
--;
505 device
->writeable
= 0;
506 device
->in_fs_metadata
= 0;
508 WARN_ON(fs_devices
->open_devices
);
509 WARN_ON(fs_devices
->rw_devices
);
510 fs_devices
->opened
= 0;
511 fs_devices
->seeding
= 0;
516 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
518 struct btrfs_fs_devices
*seed_devices
= NULL
;
521 mutex_lock(&uuid_mutex
);
522 ret
= __btrfs_close_devices(fs_devices
);
523 if (!fs_devices
->opened
) {
524 seed_devices
= fs_devices
->seed
;
525 fs_devices
->seed
= NULL
;
527 mutex_unlock(&uuid_mutex
);
529 while (seed_devices
) {
530 fs_devices
= seed_devices
;
531 seed_devices
= fs_devices
->seed
;
532 __btrfs_close_devices(fs_devices
);
533 free_fs_devices(fs_devices
);
538 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
539 fmode_t flags
, void *holder
)
541 struct block_device
*bdev
;
542 struct list_head
*head
= &fs_devices
->devices
;
543 struct btrfs_device
*device
;
544 struct block_device
*latest_bdev
= NULL
;
545 struct buffer_head
*bh
;
546 struct btrfs_super_block
*disk_super
;
547 u64 latest_devid
= 0;
548 u64 latest_transid
= 0;
553 list_for_each_entry(device
, head
, dev_list
) {
559 bdev
= open_bdev_exclusive(device
->name
, flags
, holder
);
561 printk(KERN_INFO
"open %s failed\n", device
->name
);
564 set_blocksize(bdev
, 4096);
566 bh
= btrfs_read_dev_super(bdev
);
570 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
571 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
572 if (devid
!= device
->devid
)
575 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
579 device
->generation
= btrfs_super_generation(disk_super
);
580 if (!latest_transid
|| device
->generation
> latest_transid
) {
581 latest_devid
= devid
;
582 latest_transid
= device
->generation
;
586 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
587 device
->writeable
= 0;
589 device
->writeable
= !bdev_read_only(bdev
);
594 device
->in_fs_metadata
= 0;
595 device
->mode
= flags
;
597 fs_devices
->open_devices
++;
598 if (device
->writeable
) {
599 fs_devices
->rw_devices
++;
600 list_add(&device
->dev_alloc_list
,
601 &fs_devices
->alloc_list
);
608 close_bdev_exclusive(bdev
, FMODE_READ
);
612 if (fs_devices
->open_devices
== 0) {
616 fs_devices
->seeding
= seeding
;
617 fs_devices
->opened
= 1;
618 fs_devices
->latest_bdev
= latest_bdev
;
619 fs_devices
->latest_devid
= latest_devid
;
620 fs_devices
->latest_trans
= latest_transid
;
621 fs_devices
->total_rw_bytes
= 0;
626 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
627 fmode_t flags
, void *holder
)
631 mutex_lock(&uuid_mutex
);
632 if (fs_devices
->opened
) {
633 fs_devices
->opened
++;
636 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
638 mutex_unlock(&uuid_mutex
);
642 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
643 struct btrfs_fs_devices
**fs_devices_ret
)
645 struct btrfs_super_block
*disk_super
;
646 struct block_device
*bdev
;
647 struct buffer_head
*bh
;
652 mutex_lock(&uuid_mutex
);
654 bdev
= open_bdev_exclusive(path
, flags
, holder
);
661 ret
= set_blocksize(bdev
, 4096);
664 bh
= btrfs_read_dev_super(bdev
);
669 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
670 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
671 transid
= btrfs_super_generation(disk_super
);
672 if (disk_super
->label
[0])
673 printk(KERN_INFO
"device label %s ", disk_super
->label
);
675 /* FIXME, make a readl uuid parser */
676 printk(KERN_INFO
"device fsid %llx-%llx ",
677 *(unsigned long long *)disk_super
->fsid
,
678 *(unsigned long long *)(disk_super
->fsid
+ 8));
680 printk(KERN_CONT
"devid %llu transid %llu %s\n",
681 (unsigned long long)devid
, (unsigned long long)transid
, path
);
682 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
686 close_bdev_exclusive(bdev
, flags
);
688 mutex_unlock(&uuid_mutex
);
693 * this uses a pretty simple search, the expectation is that it is
694 * called very infrequently and that a given device has a small number
697 static noinline
int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
698 struct btrfs_device
*device
,
699 u64 num_bytes
, u64
*start
)
701 struct btrfs_key key
;
702 struct btrfs_root
*root
= device
->dev_root
;
703 struct btrfs_dev_extent
*dev_extent
= NULL
;
704 struct btrfs_path
*path
;
707 u64 search_start
= 0;
708 u64 search_end
= device
->total_bytes
;
712 struct extent_buffer
*l
;
714 path
= btrfs_alloc_path();
720 /* FIXME use last free of some kind */
722 /* we don't want to overwrite the superblock on the drive,
723 * so we make sure to start at an offset of at least 1MB
725 search_start
= max((u64
)1024 * 1024, search_start
);
727 if (root
->fs_info
->alloc_start
+ num_bytes
<= device
->total_bytes
)
728 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
730 key
.objectid
= device
->devid
;
731 key
.offset
= search_start
;
732 key
.type
= BTRFS_DEV_EXTENT_KEY
;
733 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
736 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
740 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
743 slot
= path
->slots
[0];
744 if (slot
>= btrfs_header_nritems(l
)) {
745 ret
= btrfs_next_leaf(root
, path
);
752 if (search_start
>= search_end
) {
756 *start
= search_start
;
760 *start
= last_byte
> search_start
?
761 last_byte
: search_start
;
762 if (search_end
<= *start
) {
768 btrfs_item_key_to_cpu(l
, &key
, slot
);
770 if (key
.objectid
< device
->devid
)
773 if (key
.objectid
> device
->devid
)
776 if (key
.offset
>= search_start
&& key
.offset
> last_byte
&&
778 if (last_byte
< search_start
)
779 last_byte
= search_start
;
780 hole_size
= key
.offset
- last_byte
;
781 if (key
.offset
> last_byte
&&
782 hole_size
>= num_bytes
) {
787 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
791 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
792 last_byte
= key
.offset
+ btrfs_dev_extent_length(l
, dev_extent
);
798 /* we have to make sure we didn't find an extent that has already
799 * been allocated by the map tree or the original allocation
801 BUG_ON(*start
< search_start
);
803 if (*start
+ num_bytes
> search_end
) {
807 /* check for pending inserts here */
811 btrfs_free_path(path
);
815 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
816 struct btrfs_device
*device
,
820 struct btrfs_path
*path
;
821 struct btrfs_root
*root
= device
->dev_root
;
822 struct btrfs_key key
;
823 struct btrfs_key found_key
;
824 struct extent_buffer
*leaf
= NULL
;
825 struct btrfs_dev_extent
*extent
= NULL
;
827 path
= btrfs_alloc_path();
831 key
.objectid
= device
->devid
;
833 key
.type
= BTRFS_DEV_EXTENT_KEY
;
835 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
837 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
838 BTRFS_DEV_EXTENT_KEY
);
840 leaf
= path
->nodes
[0];
841 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
842 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
843 struct btrfs_dev_extent
);
844 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
845 btrfs_dev_extent_length(leaf
, extent
) < start
);
847 } else if (ret
== 0) {
848 leaf
= path
->nodes
[0];
849 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
850 struct btrfs_dev_extent
);
854 if (device
->bytes_used
> 0)
855 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
856 ret
= btrfs_del_item(trans
, root
, path
);
859 btrfs_free_path(path
);
863 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
864 struct btrfs_device
*device
,
865 u64 chunk_tree
, u64 chunk_objectid
,
866 u64 chunk_offset
, u64 start
, u64 num_bytes
)
869 struct btrfs_path
*path
;
870 struct btrfs_root
*root
= device
->dev_root
;
871 struct btrfs_dev_extent
*extent
;
872 struct extent_buffer
*leaf
;
873 struct btrfs_key key
;
875 WARN_ON(!device
->in_fs_metadata
);
876 path
= btrfs_alloc_path();
880 key
.objectid
= device
->devid
;
882 key
.type
= BTRFS_DEV_EXTENT_KEY
;
883 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
887 leaf
= path
->nodes
[0];
888 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
889 struct btrfs_dev_extent
);
890 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
891 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
892 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
894 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
895 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
898 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
899 btrfs_mark_buffer_dirty(leaf
);
900 btrfs_free_path(path
);
904 static noinline
int find_next_chunk(struct btrfs_root
*root
,
905 u64 objectid
, u64
*offset
)
907 struct btrfs_path
*path
;
909 struct btrfs_key key
;
910 struct btrfs_chunk
*chunk
;
911 struct btrfs_key found_key
;
913 path
= btrfs_alloc_path();
916 key
.objectid
= objectid
;
917 key
.offset
= (u64
)-1;
918 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
920 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
926 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
930 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
932 if (found_key
.objectid
!= objectid
)
935 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
937 *offset
= found_key
.offset
+
938 btrfs_chunk_length(path
->nodes
[0], chunk
);
943 btrfs_free_path(path
);
947 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
950 struct btrfs_key key
;
951 struct btrfs_key found_key
;
952 struct btrfs_path
*path
;
954 root
= root
->fs_info
->chunk_root
;
956 path
= btrfs_alloc_path();
960 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
961 key
.type
= BTRFS_DEV_ITEM_KEY
;
962 key
.offset
= (u64
)-1;
964 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
970 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
975 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
977 *objectid
= found_key
.offset
+ 1;
981 btrfs_free_path(path
);
986 * the device information is stored in the chunk root
987 * the btrfs_device struct should be fully filled in
989 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
990 struct btrfs_root
*root
,
991 struct btrfs_device
*device
)
994 struct btrfs_path
*path
;
995 struct btrfs_dev_item
*dev_item
;
996 struct extent_buffer
*leaf
;
997 struct btrfs_key key
;
1000 root
= root
->fs_info
->chunk_root
;
1002 path
= btrfs_alloc_path();
1006 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1007 key
.type
= BTRFS_DEV_ITEM_KEY
;
1008 key
.offset
= device
->devid
;
1010 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1015 leaf
= path
->nodes
[0];
1016 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1018 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1019 btrfs_set_device_generation(leaf
, dev_item
, 0);
1020 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1021 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1022 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1023 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1024 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1025 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1026 btrfs_set_device_group(leaf
, dev_item
, 0);
1027 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1028 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1029 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1031 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1032 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1033 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1034 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1035 btrfs_mark_buffer_dirty(leaf
);
1039 btrfs_free_path(path
);
1043 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1044 struct btrfs_device
*device
)
1047 struct btrfs_path
*path
;
1048 struct btrfs_key key
;
1049 struct btrfs_trans_handle
*trans
;
1051 root
= root
->fs_info
->chunk_root
;
1053 path
= btrfs_alloc_path();
1057 trans
= btrfs_start_transaction(root
, 1);
1058 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1059 key
.type
= BTRFS_DEV_ITEM_KEY
;
1060 key
.offset
= device
->devid
;
1063 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1072 ret
= btrfs_del_item(trans
, root
, path
);
1076 btrfs_free_path(path
);
1077 unlock_chunks(root
);
1078 btrfs_commit_transaction(trans
, root
);
1082 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1084 struct btrfs_device
*device
;
1085 struct btrfs_device
*next_device
;
1086 struct block_device
*bdev
;
1087 struct buffer_head
*bh
= NULL
;
1088 struct btrfs_super_block
*disk_super
;
1095 mutex_lock(&uuid_mutex
);
1096 mutex_lock(&root
->fs_info
->volume_mutex
);
1098 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1099 root
->fs_info
->avail_system_alloc_bits
|
1100 root
->fs_info
->avail_metadata_alloc_bits
;
1102 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1103 root
->fs_info
->fs_devices
->rw_devices
<= 4) {
1104 printk(KERN_ERR
"btrfs: unable to go below four devices "
1110 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1111 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1112 printk(KERN_ERR
"btrfs: unable to go below two "
1113 "devices on raid1\n");
1118 if (strcmp(device_path
, "missing") == 0) {
1119 struct list_head
*devices
;
1120 struct btrfs_device
*tmp
;
1123 devices
= &root
->fs_info
->fs_devices
->devices
;
1124 list_for_each_entry(tmp
, devices
, dev_list
) {
1125 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1134 printk(KERN_ERR
"btrfs: no missing devices found to "
1139 bdev
= open_bdev_exclusive(device_path
, FMODE_READ
,
1140 root
->fs_info
->bdev_holder
);
1142 ret
= PTR_ERR(bdev
);
1146 set_blocksize(bdev
, 4096);
1147 bh
= btrfs_read_dev_super(bdev
);
1152 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1153 devid
= le64_to_cpu(disk_super
->dev_item
.devid
);
1154 dev_uuid
= disk_super
->dev_item
.uuid
;
1155 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1163 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1164 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1170 if (device
->writeable
) {
1171 list_del_init(&device
->dev_alloc_list
);
1172 root
->fs_info
->fs_devices
->rw_devices
--;
1175 ret
= btrfs_shrink_device(device
, 0);
1179 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1183 device
->in_fs_metadata
= 0;
1184 list_del_init(&device
->dev_list
);
1185 device
->fs_devices
->num_devices
--;
1187 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1188 struct btrfs_device
, dev_list
);
1189 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1190 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1191 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1192 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1195 close_bdev_exclusive(device
->bdev
, device
->mode
);
1196 device
->bdev
= NULL
;
1197 device
->fs_devices
->open_devices
--;
1200 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1201 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1203 if (device
->fs_devices
->open_devices
== 0) {
1204 struct btrfs_fs_devices
*fs_devices
;
1205 fs_devices
= root
->fs_info
->fs_devices
;
1206 while (fs_devices
) {
1207 if (fs_devices
->seed
== device
->fs_devices
)
1209 fs_devices
= fs_devices
->seed
;
1211 fs_devices
->seed
= device
->fs_devices
->seed
;
1212 device
->fs_devices
->seed
= NULL
;
1213 __btrfs_close_devices(device
->fs_devices
);
1214 free_fs_devices(device
->fs_devices
);
1218 * at this point, the device is zero sized. We want to
1219 * remove it from the devices list and zero out the old super
1221 if (device
->writeable
) {
1222 /* make sure this device isn't detected as part of
1225 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1226 set_buffer_dirty(bh
);
1227 sync_dirty_buffer(bh
);
1230 kfree(device
->name
);
1238 close_bdev_exclusive(bdev
, FMODE_READ
);
1240 mutex_unlock(&root
->fs_info
->volume_mutex
);
1241 mutex_unlock(&uuid_mutex
);
1246 * does all the dirty work required for changing file system's UUID.
1248 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1249 struct btrfs_root
*root
)
1251 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1252 struct btrfs_fs_devices
*old_devices
;
1253 struct btrfs_fs_devices
*seed_devices
;
1254 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1255 struct btrfs_device
*device
;
1258 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1259 if (!fs_devices
->seeding
)
1262 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1266 old_devices
= clone_fs_devices(fs_devices
);
1267 if (IS_ERR(old_devices
)) {
1268 kfree(seed_devices
);
1269 return PTR_ERR(old_devices
);
1272 list_add(&old_devices
->list
, &fs_uuids
);
1274 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1275 seed_devices
->opened
= 1;
1276 INIT_LIST_HEAD(&seed_devices
->devices
);
1277 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1278 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1279 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1280 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1281 device
->fs_devices
= seed_devices
;
1284 fs_devices
->seeding
= 0;
1285 fs_devices
->num_devices
= 0;
1286 fs_devices
->open_devices
= 0;
1287 fs_devices
->seed
= seed_devices
;
1289 generate_random_uuid(fs_devices
->fsid
);
1290 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1291 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1292 super_flags
= btrfs_super_flags(disk_super
) &
1293 ~BTRFS_SUPER_FLAG_SEEDING
;
1294 btrfs_set_super_flags(disk_super
, super_flags
);
1300 * strore the expected generation for seed devices in device items.
1302 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1303 struct btrfs_root
*root
)
1305 struct btrfs_path
*path
;
1306 struct extent_buffer
*leaf
;
1307 struct btrfs_dev_item
*dev_item
;
1308 struct btrfs_device
*device
;
1309 struct btrfs_key key
;
1310 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1311 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1315 path
= btrfs_alloc_path();
1319 root
= root
->fs_info
->chunk_root
;
1320 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1322 key
.type
= BTRFS_DEV_ITEM_KEY
;
1325 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1329 leaf
= path
->nodes
[0];
1331 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1332 ret
= btrfs_next_leaf(root
, path
);
1337 leaf
= path
->nodes
[0];
1338 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1339 btrfs_release_path(root
, path
);
1343 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1344 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1345 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1348 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1349 struct btrfs_dev_item
);
1350 devid
= btrfs_device_id(leaf
, dev_item
);
1351 read_extent_buffer(leaf
, dev_uuid
,
1352 (unsigned long)btrfs_device_uuid(dev_item
),
1354 read_extent_buffer(leaf
, fs_uuid
,
1355 (unsigned long)btrfs_device_fsid(dev_item
),
1357 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1360 if (device
->fs_devices
->seeding
) {
1361 btrfs_set_device_generation(leaf
, dev_item
,
1362 device
->generation
);
1363 btrfs_mark_buffer_dirty(leaf
);
1371 btrfs_free_path(path
);
1375 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1377 struct btrfs_trans_handle
*trans
;
1378 struct btrfs_device
*device
;
1379 struct block_device
*bdev
;
1380 struct list_head
*devices
;
1381 struct super_block
*sb
= root
->fs_info
->sb
;
1383 int seeding_dev
= 0;
1386 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1389 bdev
= open_bdev_exclusive(device_path
, 0, root
->fs_info
->bdev_holder
);
1393 if (root
->fs_info
->fs_devices
->seeding
) {
1395 down_write(&sb
->s_umount
);
1396 mutex_lock(&uuid_mutex
);
1399 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1400 mutex_lock(&root
->fs_info
->volume_mutex
);
1402 devices
= &root
->fs_info
->fs_devices
->devices
;
1403 list_for_each_entry(device
, devices
, dev_list
) {
1404 if (device
->bdev
== bdev
) {
1410 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1412 /* we can safely leave the fs_devices entry around */
1417 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1418 if (!device
->name
) {
1424 ret
= find_next_devid(root
, &device
->devid
);
1430 trans
= btrfs_start_transaction(root
, 1);
1433 device
->barriers
= 1;
1434 device
->writeable
= 1;
1435 device
->work
.func
= pending_bios_fn
;
1436 generate_random_uuid(device
->uuid
);
1437 spin_lock_init(&device
->io_lock
);
1438 device
->generation
= trans
->transid
;
1439 device
->io_width
= root
->sectorsize
;
1440 device
->io_align
= root
->sectorsize
;
1441 device
->sector_size
= root
->sectorsize
;
1442 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1443 device
->disk_total_bytes
= device
->total_bytes
;
1444 device
->dev_root
= root
->fs_info
->dev_root
;
1445 device
->bdev
= bdev
;
1446 device
->in_fs_metadata
= 1;
1448 set_blocksize(device
->bdev
, 4096);
1451 sb
->s_flags
&= ~MS_RDONLY
;
1452 ret
= btrfs_prepare_sprout(trans
, root
);
1456 device
->fs_devices
= root
->fs_info
->fs_devices
;
1457 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1458 list_add(&device
->dev_alloc_list
,
1459 &root
->fs_info
->fs_devices
->alloc_list
);
1460 root
->fs_info
->fs_devices
->num_devices
++;
1461 root
->fs_info
->fs_devices
->open_devices
++;
1462 root
->fs_info
->fs_devices
->rw_devices
++;
1463 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1465 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1466 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1467 total_bytes
+ device
->total_bytes
);
1469 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1470 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1474 ret
= init_first_rw_device(trans
, root
, device
);
1476 ret
= btrfs_finish_sprout(trans
, root
);
1479 ret
= btrfs_add_device(trans
, root
, device
);
1483 * we've got more storage, clear any full flags on the space
1486 btrfs_clear_space_info_full(root
->fs_info
);
1488 unlock_chunks(root
);
1489 btrfs_commit_transaction(trans
, root
);
1492 mutex_unlock(&uuid_mutex
);
1493 up_write(&sb
->s_umount
);
1495 ret
= btrfs_relocate_sys_chunks(root
);
1499 mutex_unlock(&root
->fs_info
->volume_mutex
);
1502 close_bdev_exclusive(bdev
, 0);
1504 mutex_unlock(&uuid_mutex
);
1505 up_write(&sb
->s_umount
);
1510 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1511 struct btrfs_device
*device
)
1514 struct btrfs_path
*path
;
1515 struct btrfs_root
*root
;
1516 struct btrfs_dev_item
*dev_item
;
1517 struct extent_buffer
*leaf
;
1518 struct btrfs_key key
;
1520 root
= device
->dev_root
->fs_info
->chunk_root
;
1522 path
= btrfs_alloc_path();
1526 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1527 key
.type
= BTRFS_DEV_ITEM_KEY
;
1528 key
.offset
= device
->devid
;
1530 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1539 leaf
= path
->nodes
[0];
1540 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1542 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1543 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1544 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1545 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1546 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1547 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1548 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1549 btrfs_mark_buffer_dirty(leaf
);
1552 btrfs_free_path(path
);
1556 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1557 struct btrfs_device
*device
, u64 new_size
)
1559 struct btrfs_super_block
*super_copy
=
1560 &device
->dev_root
->fs_info
->super_copy
;
1561 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1562 u64 diff
= new_size
- device
->total_bytes
;
1564 if (!device
->writeable
)
1566 if (new_size
<= device
->total_bytes
)
1569 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1570 device
->fs_devices
->total_rw_bytes
+= diff
;
1572 device
->total_bytes
= new_size
;
1573 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1575 return btrfs_update_device(trans
, device
);
1578 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1579 struct btrfs_device
*device
, u64 new_size
)
1582 lock_chunks(device
->dev_root
);
1583 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1584 unlock_chunks(device
->dev_root
);
1588 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1589 struct btrfs_root
*root
,
1590 u64 chunk_tree
, u64 chunk_objectid
,
1594 struct btrfs_path
*path
;
1595 struct btrfs_key key
;
1597 root
= root
->fs_info
->chunk_root
;
1598 path
= btrfs_alloc_path();
1602 key
.objectid
= chunk_objectid
;
1603 key
.offset
= chunk_offset
;
1604 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1606 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1609 ret
= btrfs_del_item(trans
, root
, path
);
1612 btrfs_free_path(path
);
1616 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1619 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1620 struct btrfs_disk_key
*disk_key
;
1621 struct btrfs_chunk
*chunk
;
1628 struct btrfs_key key
;
1630 array_size
= btrfs_super_sys_array_size(super_copy
);
1632 ptr
= super_copy
->sys_chunk_array
;
1635 while (cur
< array_size
) {
1636 disk_key
= (struct btrfs_disk_key
*)ptr
;
1637 btrfs_disk_key_to_cpu(&key
, disk_key
);
1639 len
= sizeof(*disk_key
);
1641 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1642 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1643 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1644 len
+= btrfs_chunk_item_size(num_stripes
);
1649 if (key
.objectid
== chunk_objectid
&&
1650 key
.offset
== chunk_offset
) {
1651 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1653 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1662 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1663 u64 chunk_tree
, u64 chunk_objectid
,
1666 struct extent_map_tree
*em_tree
;
1667 struct btrfs_root
*extent_root
;
1668 struct btrfs_trans_handle
*trans
;
1669 struct extent_map
*em
;
1670 struct map_lookup
*map
;
1674 printk(KERN_INFO
"btrfs relocating chunk %llu\n",
1675 (unsigned long long)chunk_offset
);
1676 root
= root
->fs_info
->chunk_root
;
1677 extent_root
= root
->fs_info
->extent_root
;
1678 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1680 /* step one, relocate all the extents inside this chunk */
1681 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1684 trans
= btrfs_start_transaction(root
, 1);
1690 * step two, delete the device extents and the
1691 * chunk tree entries
1693 spin_lock(&em_tree
->lock
);
1694 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1695 spin_unlock(&em_tree
->lock
);
1697 BUG_ON(em
->start
> chunk_offset
||
1698 em
->start
+ em
->len
< chunk_offset
);
1699 map
= (struct map_lookup
*)em
->bdev
;
1701 for (i
= 0; i
< map
->num_stripes
; i
++) {
1702 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1703 map
->stripes
[i
].physical
);
1706 if (map
->stripes
[i
].dev
) {
1707 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1711 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1716 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1717 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1721 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1724 spin_lock(&em_tree
->lock
);
1725 remove_extent_mapping(em_tree
, em
);
1726 spin_unlock(&em_tree
->lock
);
1731 /* once for the tree */
1732 free_extent_map(em
);
1734 free_extent_map(em
);
1736 unlock_chunks(root
);
1737 btrfs_end_transaction(trans
, root
);
1741 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1743 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1744 struct btrfs_path
*path
;
1745 struct extent_buffer
*leaf
;
1746 struct btrfs_chunk
*chunk
;
1747 struct btrfs_key key
;
1748 struct btrfs_key found_key
;
1749 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1753 path
= btrfs_alloc_path();
1757 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1758 key
.offset
= (u64
)-1;
1759 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1762 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1767 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1774 leaf
= path
->nodes
[0];
1775 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1777 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1778 struct btrfs_chunk
);
1779 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1780 btrfs_release_path(chunk_root
, path
);
1782 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1783 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1789 if (found_key
.offset
== 0)
1791 key
.offset
= found_key
.offset
- 1;
1795 btrfs_free_path(path
);
1799 static u64
div_factor(u64 num
, int factor
)
1808 int btrfs_balance(struct btrfs_root
*dev_root
)
1811 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
1812 struct btrfs_device
*device
;
1815 struct btrfs_path
*path
;
1816 struct btrfs_key key
;
1817 struct btrfs_chunk
*chunk
;
1818 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
1819 struct btrfs_trans_handle
*trans
;
1820 struct btrfs_key found_key
;
1822 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1825 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
1826 dev_root
= dev_root
->fs_info
->dev_root
;
1828 /* step one make some room on all the devices */
1829 list_for_each_entry(device
, devices
, dev_list
) {
1830 old_size
= device
->total_bytes
;
1831 size_to_free
= div_factor(old_size
, 1);
1832 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
1833 if (!device
->writeable
||
1834 device
->total_bytes
- device
->bytes_used
> size_to_free
)
1837 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
1840 trans
= btrfs_start_transaction(dev_root
, 1);
1843 ret
= btrfs_grow_device(trans
, device
, old_size
);
1846 btrfs_end_transaction(trans
, dev_root
);
1849 /* step two, relocate all the chunks */
1850 path
= btrfs_alloc_path();
1853 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1854 key
.offset
= (u64
)-1;
1855 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1858 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1863 * this shouldn't happen, it means the last relocate
1869 ret
= btrfs_previous_item(chunk_root
, path
, 0,
1870 BTRFS_CHUNK_ITEM_KEY
);
1874 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1876 if (found_key
.objectid
!= key
.objectid
)
1879 chunk
= btrfs_item_ptr(path
->nodes
[0],
1881 struct btrfs_chunk
);
1882 key
.offset
= found_key
.offset
;
1883 /* chunk zero is special */
1884 if (key
.offset
== 0)
1887 btrfs_release_path(chunk_root
, path
);
1888 ret
= btrfs_relocate_chunk(chunk_root
,
1889 chunk_root
->root_key
.objectid
,
1896 btrfs_free_path(path
);
1897 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
1902 * shrinking a device means finding all of the device extents past
1903 * the new size, and then following the back refs to the chunks.
1904 * The chunk relocation code actually frees the device extent
1906 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
1908 struct btrfs_trans_handle
*trans
;
1909 struct btrfs_root
*root
= device
->dev_root
;
1910 struct btrfs_dev_extent
*dev_extent
= NULL
;
1911 struct btrfs_path
*path
;
1918 struct extent_buffer
*l
;
1919 struct btrfs_key key
;
1920 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1921 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1922 u64 diff
= device
->total_bytes
- new_size
;
1924 if (new_size
>= device
->total_bytes
)
1927 path
= btrfs_alloc_path();
1931 trans
= btrfs_start_transaction(root
, 1);
1941 device
->total_bytes
= new_size
;
1942 if (device
->writeable
)
1943 device
->fs_devices
->total_rw_bytes
-= diff
;
1944 unlock_chunks(root
);
1945 btrfs_end_transaction(trans
, root
);
1947 key
.objectid
= device
->devid
;
1948 key
.offset
= (u64
)-1;
1949 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1952 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1956 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
1965 slot
= path
->slots
[0];
1966 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1968 if (key
.objectid
!= device
->devid
)
1971 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1972 length
= btrfs_dev_extent_length(l
, dev_extent
);
1974 if (key
.offset
+ length
<= new_size
)
1977 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1978 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1979 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1980 btrfs_release_path(root
, path
);
1982 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
1988 /* Shrinking succeeded, else we would be at "done". */
1989 trans
= btrfs_start_transaction(root
, 1);
1996 device
->disk_total_bytes
= new_size
;
1997 /* Now btrfs_update_device() will change the on-disk size. */
1998 ret
= btrfs_update_device(trans
, device
);
2000 unlock_chunks(root
);
2001 btrfs_end_transaction(trans
, root
);
2004 WARN_ON(diff
> old_total
);
2005 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2006 unlock_chunks(root
);
2007 btrfs_end_transaction(trans
, root
);
2009 btrfs_free_path(path
);
2013 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2014 struct btrfs_root
*root
,
2015 struct btrfs_key
*key
,
2016 struct btrfs_chunk
*chunk
, int item_size
)
2018 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2019 struct btrfs_disk_key disk_key
;
2023 array_size
= btrfs_super_sys_array_size(super_copy
);
2024 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2027 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2028 btrfs_cpu_key_to_disk(&disk_key
, key
);
2029 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2030 ptr
+= sizeof(disk_key
);
2031 memcpy(ptr
, chunk
, item_size
);
2032 item_size
+= sizeof(disk_key
);
2033 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2037 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2038 int num_stripes
, int sub_stripes
)
2040 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2042 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2043 return calc_size
* (num_stripes
/ sub_stripes
);
2045 return calc_size
* num_stripes
;
2048 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2049 struct btrfs_root
*extent_root
,
2050 struct map_lookup
**map_ret
,
2051 u64
*num_bytes
, u64
*stripe_size
,
2052 u64 start
, u64 type
)
2054 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2055 struct btrfs_device
*device
= NULL
;
2056 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2057 struct list_head
*cur
;
2058 struct map_lookup
*map
= NULL
;
2059 struct extent_map_tree
*em_tree
;
2060 struct extent_map
*em
;
2061 struct list_head private_devs
;
2062 int min_stripe_size
= 1 * 1024 * 1024;
2063 u64 calc_size
= 1024 * 1024 * 1024;
2064 u64 max_chunk_size
= calc_size
;
2069 int num_stripes
= 1;
2070 int min_stripes
= 1;
2071 int sub_stripes
= 0;
2075 int stripe_len
= 64 * 1024;
2077 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2078 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2080 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2082 if (list_empty(&fs_devices
->alloc_list
))
2085 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2086 num_stripes
= fs_devices
->rw_devices
;
2089 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2093 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2094 num_stripes
= min_t(u64
, 2, fs_devices
->rw_devices
);
2095 if (num_stripes
< 2)
2099 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2100 num_stripes
= fs_devices
->rw_devices
;
2101 if (num_stripes
< 4)
2103 num_stripes
&= ~(u32
)1;
2108 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2109 max_chunk_size
= 10 * calc_size
;
2110 min_stripe_size
= 64 * 1024 * 1024;
2111 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2112 max_chunk_size
= 4 * calc_size
;
2113 min_stripe_size
= 32 * 1024 * 1024;
2114 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2115 calc_size
= 8 * 1024 * 1024;
2116 max_chunk_size
= calc_size
* 2;
2117 min_stripe_size
= 1 * 1024 * 1024;
2120 /* we don't want a chunk larger than 10% of writeable space */
2121 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2125 if (!map
|| map
->num_stripes
!= num_stripes
) {
2127 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2130 map
->num_stripes
= num_stripes
;
2133 if (calc_size
* num_stripes
> max_chunk_size
) {
2134 calc_size
= max_chunk_size
;
2135 do_div(calc_size
, num_stripes
);
2136 do_div(calc_size
, stripe_len
);
2137 calc_size
*= stripe_len
;
2139 /* we don't want tiny stripes */
2140 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2142 do_div(calc_size
, stripe_len
);
2143 calc_size
*= stripe_len
;
2145 cur
= fs_devices
->alloc_list
.next
;
2148 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2149 min_free
= calc_size
* 2;
2151 min_free
= calc_size
;
2154 * we add 1MB because we never use the first 1MB of the device, unless
2155 * we've looped, then we are likely allocating the maximum amount of
2156 * space left already
2159 min_free
+= 1024 * 1024;
2161 INIT_LIST_HEAD(&private_devs
);
2162 while (index
< num_stripes
) {
2163 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2164 BUG_ON(!device
->writeable
);
2165 if (device
->total_bytes
> device
->bytes_used
)
2166 avail
= device
->total_bytes
- device
->bytes_used
;
2171 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2172 ret
= find_free_dev_extent(trans
, device
,
2173 min_free
, &dev_offset
);
2175 list_move_tail(&device
->dev_alloc_list
,
2177 map
->stripes
[index
].dev
= device
;
2178 map
->stripes
[index
].physical
= dev_offset
;
2180 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2181 map
->stripes
[index
].dev
= device
;
2182 map
->stripes
[index
].physical
=
2183 dev_offset
+ calc_size
;
2187 } else if (device
->in_fs_metadata
&& avail
> max_avail
)
2189 if (cur
== &fs_devices
->alloc_list
)
2192 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2193 if (index
< num_stripes
) {
2194 if (index
>= min_stripes
) {
2195 num_stripes
= index
;
2196 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2197 num_stripes
/= sub_stripes
;
2198 num_stripes
*= sub_stripes
;
2203 if (!looped
&& max_avail
> 0) {
2205 calc_size
= max_avail
;
2211 map
->sector_size
= extent_root
->sectorsize
;
2212 map
->stripe_len
= stripe_len
;
2213 map
->io_align
= stripe_len
;
2214 map
->io_width
= stripe_len
;
2216 map
->num_stripes
= num_stripes
;
2217 map
->sub_stripes
= sub_stripes
;
2220 *stripe_size
= calc_size
;
2221 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2222 num_stripes
, sub_stripes
);
2224 em
= alloc_extent_map(GFP_NOFS
);
2229 em
->bdev
= (struct block_device
*)map
;
2231 em
->len
= *num_bytes
;
2232 em
->block_start
= 0;
2233 em
->block_len
= em
->len
;
2235 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2236 spin_lock(&em_tree
->lock
);
2237 ret
= add_extent_mapping(em_tree
, em
);
2238 spin_unlock(&em_tree
->lock
);
2240 free_extent_map(em
);
2242 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2243 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2248 while (index
< map
->num_stripes
) {
2249 device
= map
->stripes
[index
].dev
;
2250 dev_offset
= map
->stripes
[index
].physical
;
2252 ret
= btrfs_alloc_dev_extent(trans
, device
,
2253 info
->chunk_root
->root_key
.objectid
,
2254 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2255 start
, dev_offset
, calc_size
);
2263 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2264 struct btrfs_root
*extent_root
,
2265 struct map_lookup
*map
, u64 chunk_offset
,
2266 u64 chunk_size
, u64 stripe_size
)
2269 struct btrfs_key key
;
2270 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2271 struct btrfs_device
*device
;
2272 struct btrfs_chunk
*chunk
;
2273 struct btrfs_stripe
*stripe
;
2274 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2278 chunk
= kzalloc(item_size
, GFP_NOFS
);
2283 while (index
< map
->num_stripes
) {
2284 device
= map
->stripes
[index
].dev
;
2285 device
->bytes_used
+= stripe_size
;
2286 ret
= btrfs_update_device(trans
, device
);
2292 stripe
= &chunk
->stripe
;
2293 while (index
< map
->num_stripes
) {
2294 device
= map
->stripes
[index
].dev
;
2295 dev_offset
= map
->stripes
[index
].physical
;
2297 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2298 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2299 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2304 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2305 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2306 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2307 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2308 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2309 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2310 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2311 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2312 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2314 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2315 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2316 key
.offset
= chunk_offset
;
2318 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2321 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2322 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2331 * Chunk allocation falls into two parts. The first part does works
2332 * that make the new allocated chunk useable, but not do any operation
2333 * that modifies the chunk tree. The second part does the works that
2334 * require modifying the chunk tree. This division is important for the
2335 * bootstrap process of adding storage to a seed btrfs.
2337 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2338 struct btrfs_root
*extent_root
, u64 type
)
2343 struct map_lookup
*map
;
2344 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2347 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2352 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2353 &stripe_size
, chunk_offset
, type
);
2357 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2358 chunk_size
, stripe_size
);
2363 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2364 struct btrfs_root
*root
,
2365 struct btrfs_device
*device
)
2368 u64 sys_chunk_offset
;
2372 u64 sys_stripe_size
;
2374 struct map_lookup
*map
;
2375 struct map_lookup
*sys_map
;
2376 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2377 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2380 ret
= find_next_chunk(fs_info
->chunk_root
,
2381 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2384 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2385 (fs_info
->metadata_alloc_profile
&
2386 fs_info
->avail_metadata_alloc_bits
);
2387 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2389 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2390 &stripe_size
, chunk_offset
, alloc_profile
);
2393 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2395 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2396 (fs_info
->system_alloc_profile
&
2397 fs_info
->avail_system_alloc_bits
);
2398 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2400 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2401 &sys_chunk_size
, &sys_stripe_size
,
2402 sys_chunk_offset
, alloc_profile
);
2405 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2409 * Modifying chunk tree needs allocating new blocks from both
2410 * system block group and metadata block group. So we only can
2411 * do operations require modifying the chunk tree after both
2412 * block groups were created.
2414 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2415 chunk_size
, stripe_size
);
2418 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2419 sys_chunk_offset
, sys_chunk_size
,
2425 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2427 struct extent_map
*em
;
2428 struct map_lookup
*map
;
2429 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2433 spin_lock(&map_tree
->map_tree
.lock
);
2434 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2435 spin_unlock(&map_tree
->map_tree
.lock
);
2439 map
= (struct map_lookup
*)em
->bdev
;
2440 for (i
= 0; i
< map
->num_stripes
; i
++) {
2441 if (!map
->stripes
[i
].dev
->writeable
) {
2446 free_extent_map(em
);
2450 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2452 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2455 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2457 struct extent_map
*em
;
2460 spin_lock(&tree
->map_tree
.lock
);
2461 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2463 remove_extent_mapping(&tree
->map_tree
, em
);
2464 spin_unlock(&tree
->map_tree
.lock
);
2469 free_extent_map(em
);
2470 /* once for the tree */
2471 free_extent_map(em
);
2475 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2477 struct extent_map
*em
;
2478 struct map_lookup
*map
;
2479 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2482 spin_lock(&em_tree
->lock
);
2483 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2484 spin_unlock(&em_tree
->lock
);
2487 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2488 map
= (struct map_lookup
*)em
->bdev
;
2489 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2490 ret
= map
->num_stripes
;
2491 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2492 ret
= map
->sub_stripes
;
2495 free_extent_map(em
);
2499 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2503 if (map
->stripes
[optimal
].dev
->bdev
)
2505 for (i
= first
; i
< first
+ num
; i
++) {
2506 if (map
->stripes
[i
].dev
->bdev
)
2509 /* we couldn't find one that doesn't fail. Just return something
2510 * and the io error handling code will clean up eventually
2515 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2516 u64 logical
, u64
*length
,
2517 struct btrfs_multi_bio
**multi_ret
,
2518 int mirror_num
, struct page
*unplug_page
)
2520 struct extent_map
*em
;
2521 struct map_lookup
*map
;
2522 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2526 int stripes_allocated
= 8;
2527 int stripes_required
= 1;
2532 struct btrfs_multi_bio
*multi
= NULL
;
2534 if (multi_ret
&& !(rw
& (1 << BIO_RW
)))
2535 stripes_allocated
= 1;
2538 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2543 atomic_set(&multi
->error
, 0);
2546 spin_lock(&em_tree
->lock
);
2547 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2548 spin_unlock(&em_tree
->lock
);
2550 if (!em
&& unplug_page
)
2554 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2555 (unsigned long long)logical
,
2556 (unsigned long long)*length
);
2560 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2561 map
= (struct map_lookup
*)em
->bdev
;
2562 offset
= logical
- em
->start
;
2564 if (mirror_num
> map
->num_stripes
)
2567 /* if our multi bio struct is too small, back off and try again */
2568 if (rw
& (1 << BIO_RW
)) {
2569 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2570 BTRFS_BLOCK_GROUP_DUP
)) {
2571 stripes_required
= map
->num_stripes
;
2573 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2574 stripes_required
= map
->sub_stripes
;
2578 if (multi_ret
&& (rw
& (1 << BIO_RW
)) &&
2579 stripes_allocated
< stripes_required
) {
2580 stripes_allocated
= map
->num_stripes
;
2581 free_extent_map(em
);
2587 * stripe_nr counts the total number of stripes we have to stride
2588 * to get to this block
2590 do_div(stripe_nr
, map
->stripe_len
);
2592 stripe_offset
= stripe_nr
* map
->stripe_len
;
2593 BUG_ON(offset
< stripe_offset
);
2595 /* stripe_offset is the offset of this block in its stripe*/
2596 stripe_offset
= offset
- stripe_offset
;
2598 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2599 BTRFS_BLOCK_GROUP_RAID10
|
2600 BTRFS_BLOCK_GROUP_DUP
)) {
2601 /* we limit the length of each bio to what fits in a stripe */
2602 *length
= min_t(u64
, em
->len
- offset
,
2603 map
->stripe_len
- stripe_offset
);
2605 *length
= em
->len
- offset
;
2608 if (!multi_ret
&& !unplug_page
)
2613 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
2614 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2615 num_stripes
= map
->num_stripes
;
2616 else if (mirror_num
)
2617 stripe_index
= mirror_num
- 1;
2619 stripe_index
= find_live_mirror(map
, 0,
2621 current
->pid
% map
->num_stripes
);
2624 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
2625 if (rw
& (1 << BIO_RW
))
2626 num_stripes
= map
->num_stripes
;
2627 else if (mirror_num
)
2628 stripe_index
= mirror_num
- 1;
2630 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2631 int factor
= map
->num_stripes
/ map
->sub_stripes
;
2633 stripe_index
= do_div(stripe_nr
, factor
);
2634 stripe_index
*= map
->sub_stripes
;
2636 if (unplug_page
|| (rw
& (1 << BIO_RW
)))
2637 num_stripes
= map
->sub_stripes
;
2638 else if (mirror_num
)
2639 stripe_index
+= mirror_num
- 1;
2641 stripe_index
= find_live_mirror(map
, stripe_index
,
2642 map
->sub_stripes
, stripe_index
+
2643 current
->pid
% map
->sub_stripes
);
2647 * after this do_div call, stripe_nr is the number of stripes
2648 * on this device we have to walk to find the data, and
2649 * stripe_index is the number of our device in the stripe array
2651 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
2653 BUG_ON(stripe_index
>= map
->num_stripes
);
2655 for (i
= 0; i
< num_stripes
; i
++) {
2657 struct btrfs_device
*device
;
2658 struct backing_dev_info
*bdi
;
2660 device
= map
->stripes
[stripe_index
].dev
;
2662 bdi
= blk_get_backing_dev_info(device
->bdev
);
2663 if (bdi
->unplug_io_fn
)
2664 bdi
->unplug_io_fn(bdi
, unplug_page
);
2667 multi
->stripes
[i
].physical
=
2668 map
->stripes
[stripe_index
].physical
+
2669 stripe_offset
+ stripe_nr
* map
->stripe_len
;
2670 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
2676 multi
->num_stripes
= num_stripes
;
2677 multi
->max_errors
= max_errors
;
2680 free_extent_map(em
);
2684 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2685 u64 logical
, u64
*length
,
2686 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
2688 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
2692 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
2693 u64 chunk_start
, u64 physical
, u64 devid
,
2694 u64
**logical
, int *naddrs
, int *stripe_len
)
2696 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2697 struct extent_map
*em
;
2698 struct map_lookup
*map
;
2705 spin_lock(&em_tree
->lock
);
2706 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
2707 spin_unlock(&em_tree
->lock
);
2709 BUG_ON(!em
|| em
->start
!= chunk_start
);
2710 map
= (struct map_lookup
*)em
->bdev
;
2713 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2714 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
2715 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
2716 do_div(length
, map
->num_stripes
);
2718 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
2721 for (i
= 0; i
< map
->num_stripes
; i
++) {
2722 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
2724 if (map
->stripes
[i
].physical
> physical
||
2725 map
->stripes
[i
].physical
+ length
<= physical
)
2728 stripe_nr
= physical
- map
->stripes
[i
].physical
;
2729 do_div(stripe_nr
, map
->stripe_len
);
2731 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2732 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2733 do_div(stripe_nr
, map
->sub_stripes
);
2734 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
2735 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
2737 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
2738 WARN_ON(nr
>= map
->num_stripes
);
2739 for (j
= 0; j
< nr
; j
++) {
2740 if (buf
[j
] == bytenr
)
2744 WARN_ON(nr
>= map
->num_stripes
);
2749 for (i
= 0; i
> nr
; i
++) {
2750 struct btrfs_multi_bio
*multi
;
2751 struct btrfs_bio_stripe
*stripe
;
2755 ret
= btrfs_map_block(map_tree
, WRITE
, buf
[i
],
2756 &length
, &multi
, 0);
2759 stripe
= multi
->stripes
;
2760 for (j
= 0; j
< multi
->num_stripes
; j
++) {
2761 if (stripe
->physical
>= physical
&&
2762 physical
< stripe
->physical
+ length
)
2765 BUG_ON(j
>= multi
->num_stripes
);
2771 *stripe_len
= map
->stripe_len
;
2773 free_extent_map(em
);
2777 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
2778 u64 logical
, struct page
*page
)
2780 u64 length
= PAGE_CACHE_SIZE
;
2781 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
2785 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
2787 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
2788 int is_orig_bio
= 0;
2791 atomic_inc(&multi
->error
);
2793 if (bio
== multi
->orig_bio
)
2796 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
2799 bio
= multi
->orig_bio
;
2801 bio
->bi_private
= multi
->private;
2802 bio
->bi_end_io
= multi
->end_io
;
2803 /* only send an error to the higher layers if it is
2804 * beyond the tolerance of the multi-bio
2806 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
2810 * this bio is actually up to date, we didn't
2811 * go over the max number of errors
2813 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2818 bio_endio(bio
, err
);
2819 } else if (!is_orig_bio
) {
2824 struct async_sched
{
2827 struct btrfs_fs_info
*info
;
2828 struct btrfs_work work
;
2832 * see run_scheduled_bios for a description of why bios are collected for
2835 * This will add one bio to the pending list for a device and make sure
2836 * the work struct is scheduled.
2838 static noinline
int schedule_bio(struct btrfs_root
*root
,
2839 struct btrfs_device
*device
,
2840 int rw
, struct bio
*bio
)
2842 int should_queue
= 1;
2843 struct btrfs_pending_bios
*pending_bios
;
2845 /* don't bother with additional async steps for reads, right now */
2846 if (!(rw
& (1 << BIO_RW
))) {
2848 submit_bio(rw
, bio
);
2854 * nr_async_bios allows us to reliably return congestion to the
2855 * higher layers. Otherwise, the async bio makes it appear we have
2856 * made progress against dirty pages when we've really just put it
2857 * on a queue for later
2859 atomic_inc(&root
->fs_info
->nr_async_bios
);
2860 WARN_ON(bio
->bi_next
);
2861 bio
->bi_next
= NULL
;
2864 spin_lock(&device
->io_lock
);
2866 pending_bios
= &device
->pending_sync_bios
;
2868 pending_bios
= &device
->pending_bios
;
2870 if (pending_bios
->tail
)
2871 pending_bios
->tail
->bi_next
= bio
;
2873 pending_bios
->tail
= bio
;
2874 if (!pending_bios
->head
)
2875 pending_bios
->head
= bio
;
2876 if (device
->running_pending
)
2879 spin_unlock(&device
->io_lock
);
2882 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
2887 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
2888 int mirror_num
, int async_submit
)
2890 struct btrfs_mapping_tree
*map_tree
;
2891 struct btrfs_device
*dev
;
2892 struct bio
*first_bio
= bio
;
2893 u64 logical
= (u64
)bio
->bi_sector
<< 9;
2896 struct btrfs_multi_bio
*multi
= NULL
;
2901 length
= bio
->bi_size
;
2902 map_tree
= &root
->fs_info
->mapping_tree
;
2903 map_length
= length
;
2905 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
2909 total_devs
= multi
->num_stripes
;
2910 if (map_length
< length
) {
2911 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
2912 "len %llu\n", (unsigned long long)logical
,
2913 (unsigned long long)length
,
2914 (unsigned long long)map_length
);
2917 multi
->end_io
= first_bio
->bi_end_io
;
2918 multi
->private = first_bio
->bi_private
;
2919 multi
->orig_bio
= first_bio
;
2920 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
2922 while (dev_nr
< total_devs
) {
2923 if (total_devs
> 1) {
2924 if (dev_nr
< total_devs
- 1) {
2925 bio
= bio_clone(first_bio
, GFP_NOFS
);
2930 bio
->bi_private
= multi
;
2931 bio
->bi_end_io
= end_bio_multi_stripe
;
2933 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
2934 dev
= multi
->stripes
[dev_nr
].dev
;
2935 BUG_ON(rw
== WRITE
&& !dev
->writeable
);
2936 if (dev
&& dev
->bdev
) {
2937 bio
->bi_bdev
= dev
->bdev
;
2939 schedule_bio(root
, dev
, rw
, bio
);
2941 submit_bio(rw
, bio
);
2943 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2944 bio
->bi_sector
= logical
>> 9;
2945 bio_endio(bio
, -EIO
);
2949 if (total_devs
== 1)
2954 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
2957 struct btrfs_device
*device
;
2958 struct btrfs_fs_devices
*cur_devices
;
2960 cur_devices
= root
->fs_info
->fs_devices
;
2961 while (cur_devices
) {
2963 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
2964 device
= __find_device(&cur_devices
->devices
,
2969 cur_devices
= cur_devices
->seed
;
2974 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
2975 u64 devid
, u8
*dev_uuid
)
2977 struct btrfs_device
*device
;
2978 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2980 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2983 list_add(&device
->dev_list
,
2984 &fs_devices
->devices
);
2985 device
->barriers
= 1;
2986 device
->dev_root
= root
->fs_info
->dev_root
;
2987 device
->devid
= devid
;
2988 device
->work
.func
= pending_bios_fn
;
2989 device
->fs_devices
= fs_devices
;
2990 fs_devices
->num_devices
++;
2991 spin_lock_init(&device
->io_lock
);
2992 INIT_LIST_HEAD(&device
->dev_alloc_list
);
2993 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
2997 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
2998 struct extent_buffer
*leaf
,
2999 struct btrfs_chunk
*chunk
)
3001 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3002 struct map_lookup
*map
;
3003 struct extent_map
*em
;
3007 u8 uuid
[BTRFS_UUID_SIZE
];
3012 logical
= key
->offset
;
3013 length
= btrfs_chunk_length(leaf
, chunk
);
3015 spin_lock(&map_tree
->map_tree
.lock
);
3016 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3017 spin_unlock(&map_tree
->map_tree
.lock
);
3019 /* already mapped? */
3020 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3021 free_extent_map(em
);
3024 free_extent_map(em
);
3027 em
= alloc_extent_map(GFP_NOFS
);
3030 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3031 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3033 free_extent_map(em
);
3037 em
->bdev
= (struct block_device
*)map
;
3038 em
->start
= logical
;
3040 em
->block_start
= 0;
3041 em
->block_len
= em
->len
;
3043 map
->num_stripes
= num_stripes
;
3044 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3045 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3046 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3047 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3048 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3049 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3050 for (i
= 0; i
< num_stripes
; i
++) {
3051 map
->stripes
[i
].physical
=
3052 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3053 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3054 read_extent_buffer(leaf
, uuid
, (unsigned long)
3055 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3057 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3059 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3061 free_extent_map(em
);
3064 if (!map
->stripes
[i
].dev
) {
3065 map
->stripes
[i
].dev
=
3066 add_missing_dev(root
, devid
, uuid
);
3067 if (!map
->stripes
[i
].dev
) {
3069 free_extent_map(em
);
3073 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3076 spin_lock(&map_tree
->map_tree
.lock
);
3077 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3078 spin_unlock(&map_tree
->map_tree
.lock
);
3080 free_extent_map(em
);
3085 static int fill_device_from_item(struct extent_buffer
*leaf
,
3086 struct btrfs_dev_item
*dev_item
,
3087 struct btrfs_device
*device
)
3091 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3092 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3093 device
->total_bytes
= device
->disk_total_bytes
;
3094 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3095 device
->type
= btrfs_device_type(leaf
, dev_item
);
3096 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3097 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3098 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3100 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3101 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3106 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3108 struct btrfs_fs_devices
*fs_devices
;
3111 mutex_lock(&uuid_mutex
);
3113 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3114 while (fs_devices
) {
3115 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3119 fs_devices
= fs_devices
->seed
;
3122 fs_devices
= find_fsid(fsid
);
3128 fs_devices
= clone_fs_devices(fs_devices
);
3129 if (IS_ERR(fs_devices
)) {
3130 ret
= PTR_ERR(fs_devices
);
3134 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3135 root
->fs_info
->bdev_holder
);
3139 if (!fs_devices
->seeding
) {
3140 __btrfs_close_devices(fs_devices
);
3141 free_fs_devices(fs_devices
);
3146 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3147 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3149 mutex_unlock(&uuid_mutex
);
3153 static int read_one_dev(struct btrfs_root
*root
,
3154 struct extent_buffer
*leaf
,
3155 struct btrfs_dev_item
*dev_item
)
3157 struct btrfs_device
*device
;
3160 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3161 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3163 devid
= btrfs_device_id(leaf
, dev_item
);
3164 read_extent_buffer(leaf
, dev_uuid
,
3165 (unsigned long)btrfs_device_uuid(dev_item
),
3167 read_extent_buffer(leaf
, fs_uuid
,
3168 (unsigned long)btrfs_device_fsid(dev_item
),
3171 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3172 ret
= open_seed_devices(root
, fs_uuid
);
3173 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3177 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3178 if (!device
|| !device
->bdev
) {
3179 if (!btrfs_test_opt(root
, DEGRADED
))
3183 printk(KERN_WARNING
"warning devid %llu missing\n",
3184 (unsigned long long)devid
);
3185 device
= add_missing_dev(root
, devid
, dev_uuid
);
3191 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3192 BUG_ON(device
->writeable
);
3193 if (device
->generation
!=
3194 btrfs_device_generation(leaf
, dev_item
))
3198 fill_device_from_item(leaf
, dev_item
, device
);
3199 device
->dev_root
= root
->fs_info
->dev_root
;
3200 device
->in_fs_metadata
= 1;
3201 if (device
->writeable
)
3202 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3207 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3209 struct btrfs_dev_item
*dev_item
;
3211 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3213 return read_one_dev(root
, buf
, dev_item
);
3216 int btrfs_read_sys_array(struct btrfs_root
*root
)
3218 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3219 struct extent_buffer
*sb
;
3220 struct btrfs_disk_key
*disk_key
;
3221 struct btrfs_chunk
*chunk
;
3223 unsigned long sb_ptr
;
3229 struct btrfs_key key
;
3231 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3232 BTRFS_SUPER_INFO_SIZE
);
3235 btrfs_set_buffer_uptodate(sb
);
3236 btrfs_set_buffer_lockdep_class(sb
, 0);
3238 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3239 array_size
= btrfs_super_sys_array_size(super_copy
);
3241 ptr
= super_copy
->sys_chunk_array
;
3242 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3245 while (cur
< array_size
) {
3246 disk_key
= (struct btrfs_disk_key
*)ptr
;
3247 btrfs_disk_key_to_cpu(&key
, disk_key
);
3249 len
= sizeof(*disk_key
); ptr
+= len
;
3253 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3254 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3255 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3258 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3259 len
= btrfs_chunk_item_size(num_stripes
);
3268 free_extent_buffer(sb
);
3272 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3274 struct btrfs_path
*path
;
3275 struct extent_buffer
*leaf
;
3276 struct btrfs_key key
;
3277 struct btrfs_key found_key
;
3281 root
= root
->fs_info
->chunk_root
;
3283 path
= btrfs_alloc_path();
3287 /* first we search for all of the device items, and then we
3288 * read in all of the chunk items. This way we can create chunk
3289 * mappings that reference all of the devices that are afound
3291 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3295 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3297 leaf
= path
->nodes
[0];
3298 slot
= path
->slots
[0];
3299 if (slot
>= btrfs_header_nritems(leaf
)) {
3300 ret
= btrfs_next_leaf(root
, path
);
3307 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3308 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3309 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3311 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3312 struct btrfs_dev_item
*dev_item
;
3313 dev_item
= btrfs_item_ptr(leaf
, slot
,
3314 struct btrfs_dev_item
);
3315 ret
= read_one_dev(root
, leaf
, dev_item
);
3319 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3320 struct btrfs_chunk
*chunk
;
3321 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3322 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3328 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3330 btrfs_release_path(root
, path
);
3335 btrfs_free_path(path
);