2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
44 struct btrfs_bio_stripe stripes
[];
47 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
48 struct btrfs_root
*root
,
49 struct btrfs_device
*device
);
50 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
52 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
53 (sizeof(struct btrfs_bio_stripe) * (n)))
55 static DEFINE_MUTEX(uuid_mutex
);
56 static LIST_HEAD(fs_uuids
);
58 void btrfs_lock_volumes(void)
60 mutex_lock(&uuid_mutex
);
63 void btrfs_unlock_volumes(void)
65 mutex_unlock(&uuid_mutex
);
68 static void lock_chunks(struct btrfs_root
*root
)
70 mutex_lock(&root
->fs_info
->chunk_mutex
);
73 static void unlock_chunks(struct btrfs_root
*root
)
75 mutex_unlock(&root
->fs_info
->chunk_mutex
);
78 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
80 struct btrfs_device
*device
;
81 WARN_ON(fs_devices
->opened
);
82 while (!list_empty(&fs_devices
->devices
)) {
83 device
= list_entry(fs_devices
->devices
.next
,
84 struct btrfs_device
, dev_list
);
85 list_del(&device
->dev_list
);
92 int btrfs_cleanup_fs_uuids(void)
94 struct btrfs_fs_devices
*fs_devices
;
96 while (!list_empty(&fs_uuids
)) {
97 fs_devices
= list_entry(fs_uuids
.next
,
98 struct btrfs_fs_devices
, list
);
99 list_del(&fs_devices
->list
);
100 free_fs_devices(fs_devices
);
105 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
108 struct btrfs_device
*dev
;
110 list_for_each_entry(dev
, head
, dev_list
) {
111 if (dev
->devid
== devid
&&
112 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
119 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
121 struct btrfs_fs_devices
*fs_devices
;
123 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
124 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
130 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
131 struct bio
*head
, struct bio
*tail
)
134 struct bio
*old_head
;
136 old_head
= pending_bios
->head
;
137 pending_bios
->head
= head
;
138 if (pending_bios
->tail
)
139 tail
->bi_next
= old_head
;
141 pending_bios
->tail
= tail
;
145 * we try to collect pending bios for a device so we don't get a large
146 * number of procs sending bios down to the same device. This greatly
147 * improves the schedulers ability to collect and merge the bios.
149 * But, it also turns into a long list of bios to process and that is sure
150 * to eventually make the worker thread block. The solution here is to
151 * make some progress and then put this work struct back at the end of
152 * the list if the block device is congested. This way, multiple devices
153 * can make progress from a single worker thread.
155 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
158 struct backing_dev_info
*bdi
;
159 struct btrfs_fs_info
*fs_info
;
160 struct btrfs_pending_bios
*pending_bios
;
164 unsigned long num_run
;
165 unsigned long num_sync_run
;
166 unsigned long batch_run
= 0;
168 unsigned long last_waited
= 0;
171 bdi
= blk_get_backing_dev_info(device
->bdev
);
172 fs_info
= device
->dev_root
->fs_info
;
173 limit
= btrfs_async_submit_limit(fs_info
);
174 limit
= limit
* 2 / 3;
176 /* we want to make sure that every time we switch from the sync
177 * list to the normal list, we unplug
182 spin_lock(&device
->io_lock
);
187 /* take all the bios off the list at once and process them
188 * later on (without the lock held). But, remember the
189 * tail and other pointers so the bios can be properly reinserted
190 * into the list if we hit congestion
192 if (!force_reg
&& device
->pending_sync_bios
.head
) {
193 pending_bios
= &device
->pending_sync_bios
;
196 pending_bios
= &device
->pending_bios
;
200 pending
= pending_bios
->head
;
201 tail
= pending_bios
->tail
;
202 WARN_ON(pending
&& !tail
);
205 * if pending was null this time around, no bios need processing
206 * at all and we can stop. Otherwise it'll loop back up again
207 * and do an additional check so no bios are missed.
209 * device->running_pending is used to synchronize with the
212 if (device
->pending_sync_bios
.head
== NULL
&&
213 device
->pending_bios
.head
== NULL
) {
215 device
->running_pending
= 0;
218 device
->running_pending
= 1;
221 pending_bios
->head
= NULL
;
222 pending_bios
->tail
= NULL
;
224 spin_unlock(&device
->io_lock
);
227 * if we're doing the regular priority list, make sure we unplug
228 * for any high prio bios we've sent down
230 if (pending_bios
== &device
->pending_bios
&& num_sync_run
> 0) {
232 blk_run_backing_dev(bdi
, NULL
);
238 /* we want to work on both lists, but do more bios on the
239 * sync list than the regular list
242 pending_bios
!= &device
->pending_sync_bios
&&
243 device
->pending_sync_bios
.head
) ||
244 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
245 device
->pending_bios
.head
)) {
246 spin_lock(&device
->io_lock
);
247 requeue_list(pending_bios
, pending
, tail
);
252 pending
= pending
->bi_next
;
254 atomic_dec(&fs_info
->nr_async_bios
);
256 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
257 waitqueue_active(&fs_info
->async_submit_wait
))
258 wake_up(&fs_info
->async_submit_wait
);
260 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
262 if (cur
->bi_rw
& REQ_SYNC
)
265 submit_bio(cur
->bi_rw
, cur
);
268 if (need_resched()) {
270 blk_run_backing_dev(bdi
, NULL
);
277 * we made progress, there is more work to do and the bdi
278 * is now congested. Back off and let other work structs
281 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
282 fs_info
->fs_devices
->open_devices
> 1) {
283 struct io_context
*ioc
;
285 ioc
= current
->io_context
;
288 * the main goal here is that we don't want to
289 * block if we're going to be able to submit
290 * more requests without blocking.
292 * This code does two great things, it pokes into
293 * the elevator code from a filesystem _and_
294 * it makes assumptions about how batching works.
296 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
297 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
299 ioc
->last_waited
== last_waited
)) {
301 * we want to go through our batch of
302 * requests and stop. So, we copy out
303 * the ioc->last_waited time and test
304 * against it before looping
306 last_waited
= ioc
->last_waited
;
307 if (need_resched()) {
309 blk_run_backing_dev(bdi
, NULL
);
316 spin_lock(&device
->io_lock
);
317 requeue_list(pending_bios
, pending
, tail
);
318 device
->running_pending
= 1;
320 spin_unlock(&device
->io_lock
);
321 btrfs_requeue_work(&device
->work
);
328 blk_run_backing_dev(bdi
, NULL
);
331 * IO has already been through a long path to get here. Checksumming,
332 * async helper threads, perhaps compression. We've done a pretty
333 * good job of collecting a batch of IO and should just unplug
334 * the device right away.
336 * This will help anyone who is waiting on the IO, they might have
337 * already unplugged, but managed to do so before the bio they
338 * cared about found its way down here.
340 blk_run_backing_dev(bdi
, NULL
);
346 spin_lock(&device
->io_lock
);
347 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
349 spin_unlock(&device
->io_lock
);
355 static void pending_bios_fn(struct btrfs_work
*work
)
357 struct btrfs_device
*device
;
359 device
= container_of(work
, struct btrfs_device
, work
);
360 run_scheduled_bios(device
);
363 static noinline
int device_list_add(const char *path
,
364 struct btrfs_super_block
*disk_super
,
365 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
367 struct btrfs_device
*device
;
368 struct btrfs_fs_devices
*fs_devices
;
369 u64 found_transid
= btrfs_super_generation(disk_super
);
372 fs_devices
= find_fsid(disk_super
->fsid
);
374 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
377 INIT_LIST_HEAD(&fs_devices
->devices
);
378 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
379 list_add(&fs_devices
->list
, &fs_uuids
);
380 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
381 fs_devices
->latest_devid
= devid
;
382 fs_devices
->latest_trans
= found_transid
;
383 mutex_init(&fs_devices
->device_list_mutex
);
386 device
= __find_device(&fs_devices
->devices
, devid
,
387 disk_super
->dev_item
.uuid
);
390 if (fs_devices
->opened
)
393 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
395 /* we can safely leave the fs_devices entry around */
398 device
->devid
= devid
;
399 device
->work
.func
= pending_bios_fn
;
400 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
402 device
->barriers
= 1;
403 spin_lock_init(&device
->io_lock
);
404 device
->name
= kstrdup(path
, GFP_NOFS
);
409 INIT_LIST_HEAD(&device
->dev_alloc_list
);
411 mutex_lock(&fs_devices
->device_list_mutex
);
412 list_add(&device
->dev_list
, &fs_devices
->devices
);
413 mutex_unlock(&fs_devices
->device_list_mutex
);
415 device
->fs_devices
= fs_devices
;
416 fs_devices
->num_devices
++;
417 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
418 name
= kstrdup(path
, GFP_NOFS
);
423 if (device
->missing
) {
424 fs_devices
->missing_devices
--;
429 if (found_transid
> fs_devices
->latest_trans
) {
430 fs_devices
->latest_devid
= devid
;
431 fs_devices
->latest_trans
= found_transid
;
433 *fs_devices_ret
= fs_devices
;
437 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
439 struct btrfs_fs_devices
*fs_devices
;
440 struct btrfs_device
*device
;
441 struct btrfs_device
*orig_dev
;
443 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
445 return ERR_PTR(-ENOMEM
);
447 INIT_LIST_HEAD(&fs_devices
->devices
);
448 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
449 INIT_LIST_HEAD(&fs_devices
->list
);
450 mutex_init(&fs_devices
->device_list_mutex
);
451 fs_devices
->latest_devid
= orig
->latest_devid
;
452 fs_devices
->latest_trans
= orig
->latest_trans
;
453 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
455 mutex_lock(&orig
->device_list_mutex
);
456 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
457 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
461 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
467 device
->devid
= orig_dev
->devid
;
468 device
->work
.func
= pending_bios_fn
;
469 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
470 device
->barriers
= 1;
471 spin_lock_init(&device
->io_lock
);
472 INIT_LIST_HEAD(&device
->dev_list
);
473 INIT_LIST_HEAD(&device
->dev_alloc_list
);
475 list_add(&device
->dev_list
, &fs_devices
->devices
);
476 device
->fs_devices
= fs_devices
;
477 fs_devices
->num_devices
++;
479 mutex_unlock(&orig
->device_list_mutex
);
482 mutex_unlock(&orig
->device_list_mutex
);
483 free_fs_devices(fs_devices
);
484 return ERR_PTR(-ENOMEM
);
487 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
489 struct btrfs_device
*device
, *next
;
491 mutex_lock(&uuid_mutex
);
493 mutex_lock(&fs_devices
->device_list_mutex
);
494 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
495 if (device
->in_fs_metadata
)
499 close_bdev_exclusive(device
->bdev
, device
->mode
);
501 fs_devices
->open_devices
--;
503 if (device
->writeable
) {
504 list_del_init(&device
->dev_alloc_list
);
505 device
->writeable
= 0;
506 fs_devices
->rw_devices
--;
508 list_del_init(&device
->dev_list
);
509 fs_devices
->num_devices
--;
513 mutex_unlock(&fs_devices
->device_list_mutex
);
515 if (fs_devices
->seed
) {
516 fs_devices
= fs_devices
->seed
;
520 mutex_unlock(&uuid_mutex
);
524 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
526 struct btrfs_device
*device
;
528 if (--fs_devices
->opened
> 0)
531 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
533 close_bdev_exclusive(device
->bdev
, device
->mode
);
534 fs_devices
->open_devices
--;
536 if (device
->writeable
) {
537 list_del_init(&device
->dev_alloc_list
);
538 fs_devices
->rw_devices
--;
542 device
->writeable
= 0;
543 device
->in_fs_metadata
= 0;
545 WARN_ON(fs_devices
->open_devices
);
546 WARN_ON(fs_devices
->rw_devices
);
547 fs_devices
->opened
= 0;
548 fs_devices
->seeding
= 0;
553 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
555 struct btrfs_fs_devices
*seed_devices
= NULL
;
558 mutex_lock(&uuid_mutex
);
559 ret
= __btrfs_close_devices(fs_devices
);
560 if (!fs_devices
->opened
) {
561 seed_devices
= fs_devices
->seed
;
562 fs_devices
->seed
= NULL
;
564 mutex_unlock(&uuid_mutex
);
566 while (seed_devices
) {
567 fs_devices
= seed_devices
;
568 seed_devices
= fs_devices
->seed
;
569 __btrfs_close_devices(fs_devices
);
570 free_fs_devices(fs_devices
);
575 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
576 fmode_t flags
, void *holder
)
578 struct block_device
*bdev
;
579 struct list_head
*head
= &fs_devices
->devices
;
580 struct btrfs_device
*device
;
581 struct block_device
*latest_bdev
= NULL
;
582 struct buffer_head
*bh
;
583 struct btrfs_super_block
*disk_super
;
584 u64 latest_devid
= 0;
585 u64 latest_transid
= 0;
590 list_for_each_entry(device
, head
, dev_list
) {
596 bdev
= open_bdev_exclusive(device
->name
, flags
, holder
);
598 printk(KERN_INFO
"open %s failed\n", device
->name
);
601 set_blocksize(bdev
, 4096);
603 bh
= btrfs_read_dev_super(bdev
);
609 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
610 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
611 if (devid
!= device
->devid
)
614 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
618 device
->generation
= btrfs_super_generation(disk_super
);
619 if (!latest_transid
|| device
->generation
> latest_transid
) {
620 latest_devid
= devid
;
621 latest_transid
= device
->generation
;
625 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
626 device
->writeable
= 0;
628 device
->writeable
= !bdev_read_only(bdev
);
633 device
->in_fs_metadata
= 0;
634 device
->mode
= flags
;
636 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
637 fs_devices
->rotating
= 1;
639 fs_devices
->open_devices
++;
640 if (device
->writeable
) {
641 fs_devices
->rw_devices
++;
642 list_add(&device
->dev_alloc_list
,
643 &fs_devices
->alloc_list
);
650 close_bdev_exclusive(bdev
, FMODE_READ
);
654 if (fs_devices
->open_devices
== 0) {
658 fs_devices
->seeding
= seeding
;
659 fs_devices
->opened
= 1;
660 fs_devices
->latest_bdev
= latest_bdev
;
661 fs_devices
->latest_devid
= latest_devid
;
662 fs_devices
->latest_trans
= latest_transid
;
663 fs_devices
->total_rw_bytes
= 0;
668 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
669 fmode_t flags
, void *holder
)
673 mutex_lock(&uuid_mutex
);
674 if (fs_devices
->opened
) {
675 fs_devices
->opened
++;
678 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
680 mutex_unlock(&uuid_mutex
);
684 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
685 struct btrfs_fs_devices
**fs_devices_ret
)
687 struct btrfs_super_block
*disk_super
;
688 struct block_device
*bdev
;
689 struct buffer_head
*bh
;
694 mutex_lock(&uuid_mutex
);
696 bdev
= open_bdev_exclusive(path
, flags
, holder
);
703 ret
= set_blocksize(bdev
, 4096);
706 bh
= btrfs_read_dev_super(bdev
);
711 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
712 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
713 transid
= btrfs_super_generation(disk_super
);
714 if (disk_super
->label
[0])
715 printk(KERN_INFO
"device label %s ", disk_super
->label
);
717 /* FIXME, make a readl uuid parser */
718 printk(KERN_INFO
"device fsid %llx-%llx ",
719 *(unsigned long long *)disk_super
->fsid
,
720 *(unsigned long long *)(disk_super
->fsid
+ 8));
722 printk(KERN_CONT
"devid %llu transid %llu %s\n",
723 (unsigned long long)devid
, (unsigned long long)transid
, path
);
724 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
728 close_bdev_exclusive(bdev
, flags
);
730 mutex_unlock(&uuid_mutex
);
734 /* helper to account the used device space in the range */
735 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
736 u64 end
, u64
*length
)
738 struct btrfs_key key
;
739 struct btrfs_root
*root
= device
->dev_root
;
740 struct btrfs_dev_extent
*dev_extent
;
741 struct btrfs_path
*path
;
745 struct extent_buffer
*l
;
749 if (start
>= device
->total_bytes
)
752 path
= btrfs_alloc_path();
757 key
.objectid
= device
->devid
;
759 key
.type
= BTRFS_DEV_EXTENT_KEY
;
761 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
765 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
772 slot
= path
->slots
[0];
773 if (slot
>= btrfs_header_nritems(l
)) {
774 ret
= btrfs_next_leaf(root
, path
);
782 btrfs_item_key_to_cpu(l
, &key
, slot
);
784 if (key
.objectid
< device
->devid
)
787 if (key
.objectid
> device
->devid
)
790 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
793 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
794 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
796 if (key
.offset
<= start
&& extent_end
> end
) {
797 *length
= end
- start
+ 1;
799 } else if (key
.offset
<= start
&& extent_end
> start
)
800 *length
+= extent_end
- start
;
801 else if (key
.offset
> start
&& extent_end
<= end
)
802 *length
+= extent_end
- key
.offset
;
803 else if (key
.offset
> start
&& key
.offset
<= end
) {
804 *length
+= end
- key
.offset
+ 1;
806 } else if (key
.offset
> end
)
814 btrfs_free_path(path
);
819 * find_free_dev_extent - find free space in the specified device
820 * @trans: transaction handler
821 * @device: the device which we search the free space in
822 * @num_bytes: the size of the free space that we need
823 * @start: store the start of the free space.
824 * @len: the size of the free space. that we find, or the size of the max
825 * free space if we don't find suitable free space
827 * this uses a pretty simple search, the expectation is that it is
828 * called very infrequently and that a given device has a small number
831 * @start is used to store the start of the free space if we find. But if we
832 * don't find suitable free space, it will be used to store the start position
833 * of the max free space.
835 * @len is used to store the size of the free space that we find.
836 * But if we don't find suitable free space, it is used to store the size of
837 * the max free space.
839 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
840 struct btrfs_device
*device
, u64 num_bytes
,
841 u64
*start
, u64
*len
)
843 struct btrfs_key key
;
844 struct btrfs_root
*root
= device
->dev_root
;
845 struct btrfs_dev_extent
*dev_extent
;
846 struct btrfs_path
*path
;
852 u64 search_end
= device
->total_bytes
;
855 struct extent_buffer
*l
;
857 /* FIXME use last free of some kind */
859 /* we don't want to overwrite the superblock on the drive,
860 * so we make sure to start at an offset of at least 1MB
862 search_start
= 1024 * 1024;
864 if (root
->fs_info
->alloc_start
+ num_bytes
<= search_end
)
865 search_start
= max(root
->fs_info
->alloc_start
, search_start
);
867 max_hole_start
= search_start
;
870 if (search_start
>= search_end
) {
875 path
= btrfs_alloc_path();
882 key
.objectid
= device
->devid
;
883 key
.offset
= search_start
;
884 key
.type
= BTRFS_DEV_EXTENT_KEY
;
886 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
890 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
897 slot
= path
->slots
[0];
898 if (slot
>= btrfs_header_nritems(l
)) {
899 ret
= btrfs_next_leaf(root
, path
);
907 btrfs_item_key_to_cpu(l
, &key
, slot
);
909 if (key
.objectid
< device
->devid
)
912 if (key
.objectid
> device
->devid
)
915 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
918 if (key
.offset
> search_start
) {
919 hole_size
= key
.offset
- search_start
;
921 if (hole_size
> max_hole_size
) {
922 max_hole_start
= search_start
;
923 max_hole_size
= hole_size
;
927 * If this free space is greater than which we need,
928 * it must be the max free space that we have found
929 * until now, so max_hole_start must point to the start
930 * of this free space and the length of this free space
931 * is stored in max_hole_size. Thus, we return
932 * max_hole_start and max_hole_size and go back to the
935 if (hole_size
>= num_bytes
) {
941 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
942 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
944 if (extent_end
> search_start
)
945 search_start
= extent_end
;
951 hole_size
= search_end
- search_start
;
952 if (hole_size
> max_hole_size
) {
953 max_hole_start
= search_start
;
954 max_hole_size
= hole_size
;
958 if (hole_size
< num_bytes
)
964 btrfs_free_path(path
);
966 *start
= max_hole_start
;
968 *len
= max_hole_size
;
972 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
973 struct btrfs_device
*device
,
977 struct btrfs_path
*path
;
978 struct btrfs_root
*root
= device
->dev_root
;
979 struct btrfs_key key
;
980 struct btrfs_key found_key
;
981 struct extent_buffer
*leaf
= NULL
;
982 struct btrfs_dev_extent
*extent
= NULL
;
984 path
= btrfs_alloc_path();
988 key
.objectid
= device
->devid
;
990 key
.type
= BTRFS_DEV_EXTENT_KEY
;
992 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
994 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
995 BTRFS_DEV_EXTENT_KEY
);
997 leaf
= path
->nodes
[0];
998 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
999 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1000 struct btrfs_dev_extent
);
1001 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1002 btrfs_dev_extent_length(leaf
, extent
) < start
);
1004 } else if (ret
== 0) {
1005 leaf
= path
->nodes
[0];
1006 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1007 struct btrfs_dev_extent
);
1011 if (device
->bytes_used
> 0)
1012 device
->bytes_used
-= btrfs_dev_extent_length(leaf
, extent
);
1013 ret
= btrfs_del_item(trans
, root
, path
);
1016 btrfs_free_path(path
);
1020 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1021 struct btrfs_device
*device
,
1022 u64 chunk_tree
, u64 chunk_objectid
,
1023 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1026 struct btrfs_path
*path
;
1027 struct btrfs_root
*root
= device
->dev_root
;
1028 struct btrfs_dev_extent
*extent
;
1029 struct extent_buffer
*leaf
;
1030 struct btrfs_key key
;
1032 WARN_ON(!device
->in_fs_metadata
);
1033 path
= btrfs_alloc_path();
1037 key
.objectid
= device
->devid
;
1039 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1040 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1044 leaf
= path
->nodes
[0];
1045 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1046 struct btrfs_dev_extent
);
1047 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1048 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1049 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1051 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1052 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1055 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1056 btrfs_mark_buffer_dirty(leaf
);
1057 btrfs_free_path(path
);
1061 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1062 u64 objectid
, u64
*offset
)
1064 struct btrfs_path
*path
;
1066 struct btrfs_key key
;
1067 struct btrfs_chunk
*chunk
;
1068 struct btrfs_key found_key
;
1070 path
= btrfs_alloc_path();
1073 key
.objectid
= objectid
;
1074 key
.offset
= (u64
)-1;
1075 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1077 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1083 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1087 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1089 if (found_key
.objectid
!= objectid
)
1092 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1093 struct btrfs_chunk
);
1094 *offset
= found_key
.offset
+
1095 btrfs_chunk_length(path
->nodes
[0], chunk
);
1100 btrfs_free_path(path
);
1104 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1107 struct btrfs_key key
;
1108 struct btrfs_key found_key
;
1109 struct btrfs_path
*path
;
1111 root
= root
->fs_info
->chunk_root
;
1113 path
= btrfs_alloc_path();
1117 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1118 key
.type
= BTRFS_DEV_ITEM_KEY
;
1119 key
.offset
= (u64
)-1;
1121 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1127 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1128 BTRFS_DEV_ITEM_KEY
);
1132 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1134 *objectid
= found_key
.offset
+ 1;
1138 btrfs_free_path(path
);
1143 * the device information is stored in the chunk root
1144 * the btrfs_device struct should be fully filled in
1146 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1147 struct btrfs_root
*root
,
1148 struct btrfs_device
*device
)
1151 struct btrfs_path
*path
;
1152 struct btrfs_dev_item
*dev_item
;
1153 struct extent_buffer
*leaf
;
1154 struct btrfs_key key
;
1157 root
= root
->fs_info
->chunk_root
;
1159 path
= btrfs_alloc_path();
1163 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1164 key
.type
= BTRFS_DEV_ITEM_KEY
;
1165 key
.offset
= device
->devid
;
1167 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1172 leaf
= path
->nodes
[0];
1173 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1175 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1176 btrfs_set_device_generation(leaf
, dev_item
, 0);
1177 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1178 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1179 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1180 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1181 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1182 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1183 btrfs_set_device_group(leaf
, dev_item
, 0);
1184 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1185 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1186 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1188 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1189 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1190 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1191 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1192 btrfs_mark_buffer_dirty(leaf
);
1196 btrfs_free_path(path
);
1200 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1201 struct btrfs_device
*device
)
1204 struct btrfs_path
*path
;
1205 struct btrfs_key key
;
1206 struct btrfs_trans_handle
*trans
;
1208 root
= root
->fs_info
->chunk_root
;
1210 path
= btrfs_alloc_path();
1214 trans
= btrfs_start_transaction(root
, 0);
1215 if (IS_ERR(trans
)) {
1216 btrfs_free_path(path
);
1217 return PTR_ERR(trans
);
1219 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1220 key
.type
= BTRFS_DEV_ITEM_KEY
;
1221 key
.offset
= device
->devid
;
1224 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1233 ret
= btrfs_del_item(trans
, root
, path
);
1237 btrfs_free_path(path
);
1238 unlock_chunks(root
);
1239 btrfs_commit_transaction(trans
, root
);
1243 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1245 struct btrfs_device
*device
;
1246 struct btrfs_device
*next_device
;
1247 struct block_device
*bdev
;
1248 struct buffer_head
*bh
= NULL
;
1249 struct btrfs_super_block
*disk_super
;
1256 mutex_lock(&uuid_mutex
);
1257 mutex_lock(&root
->fs_info
->volume_mutex
);
1259 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1260 root
->fs_info
->avail_system_alloc_bits
|
1261 root
->fs_info
->avail_metadata_alloc_bits
;
1263 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1264 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1265 printk(KERN_ERR
"btrfs: unable to go below four devices "
1271 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1272 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1273 printk(KERN_ERR
"btrfs: unable to go below two "
1274 "devices on raid1\n");
1279 if (strcmp(device_path
, "missing") == 0) {
1280 struct list_head
*devices
;
1281 struct btrfs_device
*tmp
;
1284 devices
= &root
->fs_info
->fs_devices
->devices
;
1285 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1286 list_for_each_entry(tmp
, devices
, dev_list
) {
1287 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1292 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1297 printk(KERN_ERR
"btrfs: no missing devices found to "
1302 bdev
= open_bdev_exclusive(device_path
, FMODE_READ
,
1303 root
->fs_info
->bdev_holder
);
1305 ret
= PTR_ERR(bdev
);
1309 set_blocksize(bdev
, 4096);
1310 bh
= btrfs_read_dev_super(bdev
);
1315 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1316 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1317 dev_uuid
= disk_super
->dev_item
.uuid
;
1318 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1326 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1327 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1333 if (device
->writeable
) {
1334 list_del_init(&device
->dev_alloc_list
);
1335 root
->fs_info
->fs_devices
->rw_devices
--;
1338 ret
= btrfs_shrink_device(device
, 0);
1342 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1346 device
->in_fs_metadata
= 0;
1349 * the device list mutex makes sure that we don't change
1350 * the device list while someone else is writing out all
1351 * the device supers.
1353 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1354 list_del_init(&device
->dev_list
);
1355 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1357 device
->fs_devices
->num_devices
--;
1359 if (device
->missing
)
1360 root
->fs_info
->fs_devices
->missing_devices
--;
1362 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1363 struct btrfs_device
, dev_list
);
1364 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1365 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1366 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1367 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1370 close_bdev_exclusive(device
->bdev
, device
->mode
);
1371 device
->bdev
= NULL
;
1372 device
->fs_devices
->open_devices
--;
1375 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1376 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
, num_devices
);
1378 if (device
->fs_devices
->open_devices
== 0) {
1379 struct btrfs_fs_devices
*fs_devices
;
1380 fs_devices
= root
->fs_info
->fs_devices
;
1381 while (fs_devices
) {
1382 if (fs_devices
->seed
== device
->fs_devices
)
1384 fs_devices
= fs_devices
->seed
;
1386 fs_devices
->seed
= device
->fs_devices
->seed
;
1387 device
->fs_devices
->seed
= NULL
;
1388 __btrfs_close_devices(device
->fs_devices
);
1389 free_fs_devices(device
->fs_devices
);
1393 * at this point, the device is zero sized. We want to
1394 * remove it from the devices list and zero out the old super
1396 if (device
->writeable
) {
1397 /* make sure this device isn't detected as part of
1400 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1401 set_buffer_dirty(bh
);
1402 sync_dirty_buffer(bh
);
1405 kfree(device
->name
);
1413 close_bdev_exclusive(bdev
, FMODE_READ
);
1415 mutex_unlock(&root
->fs_info
->volume_mutex
);
1416 mutex_unlock(&uuid_mutex
);
1421 * does all the dirty work required for changing file system's UUID.
1423 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1424 struct btrfs_root
*root
)
1426 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1427 struct btrfs_fs_devices
*old_devices
;
1428 struct btrfs_fs_devices
*seed_devices
;
1429 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1430 struct btrfs_device
*device
;
1433 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1434 if (!fs_devices
->seeding
)
1437 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1441 old_devices
= clone_fs_devices(fs_devices
);
1442 if (IS_ERR(old_devices
)) {
1443 kfree(seed_devices
);
1444 return PTR_ERR(old_devices
);
1447 list_add(&old_devices
->list
, &fs_uuids
);
1449 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1450 seed_devices
->opened
= 1;
1451 INIT_LIST_HEAD(&seed_devices
->devices
);
1452 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1453 mutex_init(&seed_devices
->device_list_mutex
);
1454 list_splice_init(&fs_devices
->devices
, &seed_devices
->devices
);
1455 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1456 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1457 device
->fs_devices
= seed_devices
;
1460 fs_devices
->seeding
= 0;
1461 fs_devices
->num_devices
= 0;
1462 fs_devices
->open_devices
= 0;
1463 fs_devices
->seed
= seed_devices
;
1465 generate_random_uuid(fs_devices
->fsid
);
1466 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1467 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1468 super_flags
= btrfs_super_flags(disk_super
) &
1469 ~BTRFS_SUPER_FLAG_SEEDING
;
1470 btrfs_set_super_flags(disk_super
, super_flags
);
1476 * strore the expected generation for seed devices in device items.
1478 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1479 struct btrfs_root
*root
)
1481 struct btrfs_path
*path
;
1482 struct extent_buffer
*leaf
;
1483 struct btrfs_dev_item
*dev_item
;
1484 struct btrfs_device
*device
;
1485 struct btrfs_key key
;
1486 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1487 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1491 path
= btrfs_alloc_path();
1495 root
= root
->fs_info
->chunk_root
;
1496 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1498 key
.type
= BTRFS_DEV_ITEM_KEY
;
1501 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1505 leaf
= path
->nodes
[0];
1507 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1508 ret
= btrfs_next_leaf(root
, path
);
1513 leaf
= path
->nodes
[0];
1514 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1515 btrfs_release_path(root
, path
);
1519 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1520 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1521 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1524 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1525 struct btrfs_dev_item
);
1526 devid
= btrfs_device_id(leaf
, dev_item
);
1527 read_extent_buffer(leaf
, dev_uuid
,
1528 (unsigned long)btrfs_device_uuid(dev_item
),
1530 read_extent_buffer(leaf
, fs_uuid
,
1531 (unsigned long)btrfs_device_fsid(dev_item
),
1533 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1536 if (device
->fs_devices
->seeding
) {
1537 btrfs_set_device_generation(leaf
, dev_item
,
1538 device
->generation
);
1539 btrfs_mark_buffer_dirty(leaf
);
1547 btrfs_free_path(path
);
1551 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1553 struct btrfs_trans_handle
*trans
;
1554 struct btrfs_device
*device
;
1555 struct block_device
*bdev
;
1556 struct list_head
*devices
;
1557 struct super_block
*sb
= root
->fs_info
->sb
;
1559 int seeding_dev
= 0;
1562 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1565 bdev
= open_bdev_exclusive(device_path
, 0, root
->fs_info
->bdev_holder
);
1567 return PTR_ERR(bdev
);
1569 if (root
->fs_info
->fs_devices
->seeding
) {
1571 down_write(&sb
->s_umount
);
1572 mutex_lock(&uuid_mutex
);
1575 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1576 mutex_lock(&root
->fs_info
->volume_mutex
);
1578 devices
= &root
->fs_info
->fs_devices
->devices
;
1580 * we have the volume lock, so we don't need the extra
1581 * device list mutex while reading the list here.
1583 list_for_each_entry(device
, devices
, dev_list
) {
1584 if (device
->bdev
== bdev
) {
1590 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1592 /* we can safely leave the fs_devices entry around */
1597 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1598 if (!device
->name
) {
1604 ret
= find_next_devid(root
, &device
->devid
);
1610 trans
= btrfs_start_transaction(root
, 0);
1611 if (IS_ERR(trans
)) {
1613 ret
= PTR_ERR(trans
);
1619 device
->barriers
= 1;
1620 device
->writeable
= 1;
1621 device
->work
.func
= pending_bios_fn
;
1622 generate_random_uuid(device
->uuid
);
1623 spin_lock_init(&device
->io_lock
);
1624 device
->generation
= trans
->transid
;
1625 device
->io_width
= root
->sectorsize
;
1626 device
->io_align
= root
->sectorsize
;
1627 device
->sector_size
= root
->sectorsize
;
1628 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1629 device
->disk_total_bytes
= device
->total_bytes
;
1630 device
->dev_root
= root
->fs_info
->dev_root
;
1631 device
->bdev
= bdev
;
1632 device
->in_fs_metadata
= 1;
1634 set_blocksize(device
->bdev
, 4096);
1637 sb
->s_flags
&= ~MS_RDONLY
;
1638 ret
= btrfs_prepare_sprout(trans
, root
);
1642 device
->fs_devices
= root
->fs_info
->fs_devices
;
1645 * we don't want write_supers to jump in here with our device
1648 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1649 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1650 list_add(&device
->dev_alloc_list
,
1651 &root
->fs_info
->fs_devices
->alloc_list
);
1652 root
->fs_info
->fs_devices
->num_devices
++;
1653 root
->fs_info
->fs_devices
->open_devices
++;
1654 root
->fs_info
->fs_devices
->rw_devices
++;
1655 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1657 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1658 root
->fs_info
->fs_devices
->rotating
= 1;
1660 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
1661 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
,
1662 total_bytes
+ device
->total_bytes
);
1664 total_bytes
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
1665 btrfs_set_super_num_devices(&root
->fs_info
->super_copy
,
1667 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1670 ret
= init_first_rw_device(trans
, root
, device
);
1672 ret
= btrfs_finish_sprout(trans
, root
);
1675 ret
= btrfs_add_device(trans
, root
, device
);
1679 * we've got more storage, clear any full flags on the space
1682 btrfs_clear_space_info_full(root
->fs_info
);
1684 unlock_chunks(root
);
1685 btrfs_commit_transaction(trans
, root
);
1688 mutex_unlock(&uuid_mutex
);
1689 up_write(&sb
->s_umount
);
1691 ret
= btrfs_relocate_sys_chunks(root
);
1695 mutex_unlock(&root
->fs_info
->volume_mutex
);
1698 close_bdev_exclusive(bdev
, 0);
1700 mutex_unlock(&uuid_mutex
);
1701 up_write(&sb
->s_umount
);
1706 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1707 struct btrfs_device
*device
)
1710 struct btrfs_path
*path
;
1711 struct btrfs_root
*root
;
1712 struct btrfs_dev_item
*dev_item
;
1713 struct extent_buffer
*leaf
;
1714 struct btrfs_key key
;
1716 root
= device
->dev_root
->fs_info
->chunk_root
;
1718 path
= btrfs_alloc_path();
1722 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1723 key
.type
= BTRFS_DEV_ITEM_KEY
;
1724 key
.offset
= device
->devid
;
1726 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1735 leaf
= path
->nodes
[0];
1736 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1738 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1739 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1740 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1741 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1742 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1743 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1744 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1745 btrfs_mark_buffer_dirty(leaf
);
1748 btrfs_free_path(path
);
1752 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1753 struct btrfs_device
*device
, u64 new_size
)
1755 struct btrfs_super_block
*super_copy
=
1756 &device
->dev_root
->fs_info
->super_copy
;
1757 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1758 u64 diff
= new_size
- device
->total_bytes
;
1760 if (!device
->writeable
)
1762 if (new_size
<= device
->total_bytes
)
1765 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1766 device
->fs_devices
->total_rw_bytes
+= diff
;
1768 device
->total_bytes
= new_size
;
1769 device
->disk_total_bytes
= new_size
;
1770 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1772 return btrfs_update_device(trans
, device
);
1775 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1776 struct btrfs_device
*device
, u64 new_size
)
1779 lock_chunks(device
->dev_root
);
1780 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1781 unlock_chunks(device
->dev_root
);
1785 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1786 struct btrfs_root
*root
,
1787 u64 chunk_tree
, u64 chunk_objectid
,
1791 struct btrfs_path
*path
;
1792 struct btrfs_key key
;
1794 root
= root
->fs_info
->chunk_root
;
1795 path
= btrfs_alloc_path();
1799 key
.objectid
= chunk_objectid
;
1800 key
.offset
= chunk_offset
;
1801 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1803 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1806 ret
= btrfs_del_item(trans
, root
, path
);
1809 btrfs_free_path(path
);
1813 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1816 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
1817 struct btrfs_disk_key
*disk_key
;
1818 struct btrfs_chunk
*chunk
;
1825 struct btrfs_key key
;
1827 array_size
= btrfs_super_sys_array_size(super_copy
);
1829 ptr
= super_copy
->sys_chunk_array
;
1832 while (cur
< array_size
) {
1833 disk_key
= (struct btrfs_disk_key
*)ptr
;
1834 btrfs_disk_key_to_cpu(&key
, disk_key
);
1836 len
= sizeof(*disk_key
);
1838 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1839 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1840 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1841 len
+= btrfs_chunk_item_size(num_stripes
);
1846 if (key
.objectid
== chunk_objectid
&&
1847 key
.offset
== chunk_offset
) {
1848 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1850 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1859 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1860 u64 chunk_tree
, u64 chunk_objectid
,
1863 struct extent_map_tree
*em_tree
;
1864 struct btrfs_root
*extent_root
;
1865 struct btrfs_trans_handle
*trans
;
1866 struct extent_map
*em
;
1867 struct map_lookup
*map
;
1871 root
= root
->fs_info
->chunk_root
;
1872 extent_root
= root
->fs_info
->extent_root
;
1873 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1875 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1879 /* step one, relocate all the extents inside this chunk */
1880 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1884 trans
= btrfs_start_transaction(root
, 0);
1885 BUG_ON(IS_ERR(trans
));
1890 * step two, delete the device extents and the
1891 * chunk tree entries
1893 read_lock(&em_tree
->lock
);
1894 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1895 read_unlock(&em_tree
->lock
);
1897 BUG_ON(em
->start
> chunk_offset
||
1898 em
->start
+ em
->len
< chunk_offset
);
1899 map
= (struct map_lookup
*)em
->bdev
;
1901 for (i
= 0; i
< map
->num_stripes
; i
++) {
1902 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1903 map
->stripes
[i
].physical
);
1906 if (map
->stripes
[i
].dev
) {
1907 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1911 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1916 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1917 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1921 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1924 write_lock(&em_tree
->lock
);
1925 remove_extent_mapping(em_tree
, em
);
1926 write_unlock(&em_tree
->lock
);
1931 /* once for the tree */
1932 free_extent_map(em
);
1934 free_extent_map(em
);
1936 unlock_chunks(root
);
1937 btrfs_end_transaction(trans
, root
);
1941 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
1943 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1944 struct btrfs_path
*path
;
1945 struct extent_buffer
*leaf
;
1946 struct btrfs_chunk
*chunk
;
1947 struct btrfs_key key
;
1948 struct btrfs_key found_key
;
1949 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
1951 bool retried
= false;
1955 path
= btrfs_alloc_path();
1960 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
1961 key
.offset
= (u64
)-1;
1962 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1965 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
1970 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
1977 leaf
= path
->nodes
[0];
1978 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1980 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
1981 struct btrfs_chunk
);
1982 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
1983 btrfs_release_path(chunk_root
, path
);
1985 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1986 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
1995 if (found_key
.offset
== 0)
1997 key
.offset
= found_key
.offset
- 1;
2000 if (failed
&& !retried
) {
2004 } else if (failed
&& retried
) {
2009 btrfs_free_path(path
);
2013 static u64
div_factor(u64 num
, int factor
)
2022 int btrfs_balance(struct btrfs_root
*dev_root
)
2025 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2026 struct btrfs_device
*device
;
2029 struct btrfs_path
*path
;
2030 struct btrfs_key key
;
2031 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2032 struct btrfs_trans_handle
*trans
;
2033 struct btrfs_key found_key
;
2035 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2038 if (!capable(CAP_SYS_ADMIN
))
2041 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2042 dev_root
= dev_root
->fs_info
->dev_root
;
2044 /* step one make some room on all the devices */
2045 list_for_each_entry(device
, devices
, dev_list
) {
2046 old_size
= device
->total_bytes
;
2047 size_to_free
= div_factor(old_size
, 1);
2048 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2049 if (!device
->writeable
||
2050 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2053 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2058 trans
= btrfs_start_transaction(dev_root
, 0);
2059 BUG_ON(IS_ERR(trans
));
2061 ret
= btrfs_grow_device(trans
, device
, old_size
);
2064 btrfs_end_transaction(trans
, dev_root
);
2067 /* step two, relocate all the chunks */
2068 path
= btrfs_alloc_path();
2071 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2072 key
.offset
= (u64
)-1;
2073 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2076 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2081 * this shouldn't happen, it means the last relocate
2087 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2088 BTRFS_CHUNK_ITEM_KEY
);
2092 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2094 if (found_key
.objectid
!= key
.objectid
)
2097 /* chunk zero is special */
2098 if (found_key
.offset
== 0)
2101 btrfs_release_path(chunk_root
, path
);
2102 ret
= btrfs_relocate_chunk(chunk_root
,
2103 chunk_root
->root_key
.objectid
,
2106 BUG_ON(ret
&& ret
!= -ENOSPC
);
2107 key
.offset
= found_key
.offset
- 1;
2111 btrfs_free_path(path
);
2112 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2117 * shrinking a device means finding all of the device extents past
2118 * the new size, and then following the back refs to the chunks.
2119 * The chunk relocation code actually frees the device extent
2121 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2123 struct btrfs_trans_handle
*trans
;
2124 struct btrfs_root
*root
= device
->dev_root
;
2125 struct btrfs_dev_extent
*dev_extent
= NULL
;
2126 struct btrfs_path
*path
;
2134 bool retried
= false;
2135 struct extent_buffer
*l
;
2136 struct btrfs_key key
;
2137 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2138 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2139 u64 old_size
= device
->total_bytes
;
2140 u64 diff
= device
->total_bytes
- new_size
;
2142 if (new_size
>= device
->total_bytes
)
2145 path
= btrfs_alloc_path();
2153 device
->total_bytes
= new_size
;
2154 if (device
->writeable
)
2155 device
->fs_devices
->total_rw_bytes
-= diff
;
2156 unlock_chunks(root
);
2159 key
.objectid
= device
->devid
;
2160 key
.offset
= (u64
)-1;
2161 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2164 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2168 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2173 btrfs_release_path(root
, path
);
2178 slot
= path
->slots
[0];
2179 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2181 if (key
.objectid
!= device
->devid
) {
2182 btrfs_release_path(root
, path
);
2186 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2187 length
= btrfs_dev_extent_length(l
, dev_extent
);
2189 if (key
.offset
+ length
<= new_size
) {
2190 btrfs_release_path(root
, path
);
2194 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2195 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2196 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2197 btrfs_release_path(root
, path
);
2199 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2201 if (ret
&& ret
!= -ENOSPC
)
2208 if (failed
&& !retried
) {
2212 } else if (failed
&& retried
) {
2216 device
->total_bytes
= old_size
;
2217 if (device
->writeable
)
2218 device
->fs_devices
->total_rw_bytes
+= diff
;
2219 unlock_chunks(root
);
2223 /* Shrinking succeeded, else we would be at "done". */
2224 trans
= btrfs_start_transaction(root
, 0);
2225 if (IS_ERR(trans
)) {
2226 ret
= PTR_ERR(trans
);
2232 device
->disk_total_bytes
= new_size
;
2233 /* Now btrfs_update_device() will change the on-disk size. */
2234 ret
= btrfs_update_device(trans
, device
);
2236 unlock_chunks(root
);
2237 btrfs_end_transaction(trans
, root
);
2240 WARN_ON(diff
> old_total
);
2241 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2242 unlock_chunks(root
);
2243 btrfs_end_transaction(trans
, root
);
2245 btrfs_free_path(path
);
2249 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2250 struct btrfs_root
*root
,
2251 struct btrfs_key
*key
,
2252 struct btrfs_chunk
*chunk
, int item_size
)
2254 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
2255 struct btrfs_disk_key disk_key
;
2259 array_size
= btrfs_super_sys_array_size(super_copy
);
2260 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2263 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2264 btrfs_cpu_key_to_disk(&disk_key
, key
);
2265 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2266 ptr
+= sizeof(disk_key
);
2267 memcpy(ptr
, chunk
, item_size
);
2268 item_size
+= sizeof(disk_key
);
2269 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2273 static noinline u64
chunk_bytes_by_type(u64 type
, u64 calc_size
,
2274 int num_stripes
, int sub_stripes
)
2276 if (type
& (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
))
2278 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2279 return calc_size
* (num_stripes
/ sub_stripes
);
2281 return calc_size
* num_stripes
;
2284 /* Used to sort the devices by max_avail(descending sort) */
2285 int btrfs_cmp_device_free_bytes(const void *dev_info1
, const void *dev_info2
)
2287 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
2288 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2290 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
2291 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2297 static int __btrfs_calc_nstripes(struct btrfs_fs_devices
*fs_devices
, u64 type
,
2298 int *num_stripes
, int *min_stripes
,
2305 if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2306 *num_stripes
= fs_devices
->rw_devices
;
2309 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2313 if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2314 if (fs_devices
->rw_devices
< 2)
2319 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2320 *num_stripes
= fs_devices
->rw_devices
;
2321 if (*num_stripes
< 4)
2323 *num_stripes
&= ~(u32
)1;
2331 static u64
__btrfs_calc_stripe_size(struct btrfs_fs_devices
*fs_devices
,
2332 u64 proposed_size
, u64 type
,
2333 int num_stripes
, int small_stripe
)
2335 int min_stripe_size
= 1 * 1024 * 1024;
2336 u64 calc_size
= proposed_size
;
2337 u64 max_chunk_size
= calc_size
;
2340 if (type
& (BTRFS_BLOCK_GROUP_RAID1
|
2341 BTRFS_BLOCK_GROUP_DUP
|
2342 BTRFS_BLOCK_GROUP_RAID10
))
2345 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2346 max_chunk_size
= 10 * calc_size
;
2347 min_stripe_size
= 64 * 1024 * 1024;
2348 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2349 max_chunk_size
= 256 * 1024 * 1024;
2350 min_stripe_size
= 32 * 1024 * 1024;
2351 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2352 calc_size
= 8 * 1024 * 1024;
2353 max_chunk_size
= calc_size
* 2;
2354 min_stripe_size
= 1 * 1024 * 1024;
2357 /* we don't want a chunk larger than 10% of writeable space */
2358 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2361 if (calc_size
* num_stripes
> max_chunk_size
* ncopies
) {
2362 calc_size
= max_chunk_size
* ncopies
;
2363 do_div(calc_size
, num_stripes
);
2364 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2365 calc_size
*= BTRFS_STRIPE_LEN
;
2368 /* we don't want tiny stripes */
2370 calc_size
= max_t(u64
, min_stripe_size
, calc_size
);
2373 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2374 * we end up with something bigger than a stripe
2376 calc_size
= max_t(u64
, calc_size
, BTRFS_STRIPE_LEN
);
2378 do_div(calc_size
, BTRFS_STRIPE_LEN
);
2379 calc_size
*= BTRFS_STRIPE_LEN
;
2384 static struct map_lookup
*__shrink_map_lookup_stripes(struct map_lookup
*map
,
2387 struct map_lookup
*new;
2388 size_t len
= map_lookup_size(num_stripes
);
2390 BUG_ON(map
->num_stripes
< num_stripes
);
2392 if (map
->num_stripes
== num_stripes
)
2395 new = kmalloc(len
, GFP_NOFS
);
2397 /* just change map->num_stripes */
2398 map
->num_stripes
= num_stripes
;
2402 memcpy(new, map
, len
);
2403 new->num_stripes
= num_stripes
;
2409 * helper to allocate device space from btrfs_device_info, in which we stored
2410 * max free space information of every device. It is used when we can not
2411 * allocate chunks by default size.
2413 * By this helper, we can allocate a new chunk as larger as possible.
2415 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle
*trans
,
2416 struct btrfs_fs_devices
*fs_devices
,
2417 struct btrfs_device_info
*devices
,
2418 int nr_device
, u64 type
,
2419 struct map_lookup
**map_lookup
,
2420 int min_stripes
, u64
*stripe_size
)
2422 int i
, index
, sort_again
= 0;
2423 int min_devices
= min_stripes
;
2424 u64 max_avail
, min_free
;
2425 struct map_lookup
*map
= *map_lookup
;
2428 if (nr_device
< min_stripes
)
2431 btrfs_descending_sort_devices(devices
, nr_device
);
2433 max_avail
= devices
[0].max_avail
;
2437 for (i
= 0; i
< nr_device
; i
++) {
2439 * if dev_offset = 0, it means the free space of this device
2440 * is less than what we need, and we didn't search max avail
2441 * extent on this device, so do it now.
2443 if (!devices
[i
].dev_offset
) {
2444 ret
= find_free_dev_extent(trans
, devices
[i
].dev
,
2446 &devices
[i
].dev_offset
,
2447 &devices
[i
].max_avail
);
2448 if (ret
!= 0 && ret
!= -ENOSPC
)
2454 /* we update the max avail free extent of each devices, sort again */
2456 btrfs_descending_sort_devices(devices
, nr_device
);
2458 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2461 if (!devices
[min_devices
- 1].max_avail
)
2464 max_avail
= devices
[min_devices
- 1].max_avail
;
2465 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2466 do_div(max_avail
, 2);
2468 max_avail
= __btrfs_calc_stripe_size(fs_devices
, max_avail
, type
,
2470 if (type
& BTRFS_BLOCK_GROUP_DUP
)
2471 min_free
= max_avail
* 2;
2473 min_free
= max_avail
;
2475 if (min_free
> devices
[min_devices
- 1].max_avail
)
2478 map
= __shrink_map_lookup_stripes(map
, min_stripes
);
2479 *stripe_size
= max_avail
;
2482 for (i
= 0; i
< min_stripes
; i
++) {
2483 map
->stripes
[i
].dev
= devices
[index
].dev
;
2484 map
->stripes
[i
].physical
= devices
[index
].dev_offset
;
2485 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2487 map
->stripes
[i
].dev
= devices
[index
].dev
;
2488 map
->stripes
[i
].physical
= devices
[index
].dev_offset
+
2498 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2499 struct btrfs_root
*extent_root
,
2500 struct map_lookup
**map_ret
,
2501 u64
*num_bytes
, u64
*stripe_size
,
2502 u64 start
, u64 type
)
2504 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2505 struct btrfs_device
*device
= NULL
;
2506 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2507 struct list_head
*cur
;
2508 struct map_lookup
*map
;
2509 struct extent_map_tree
*em_tree
;
2510 struct extent_map
*em
;
2511 struct btrfs_device_info
*devices_info
;
2512 struct list_head private_devs
;
2513 u64 calc_size
= 1024 * 1024 * 1024;
2520 int min_devices
; /* the min number of devices we need */
2525 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2526 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2528 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2530 if (list_empty(&fs_devices
->alloc_list
))
2533 ret
= __btrfs_calc_nstripes(fs_devices
, type
, &num_stripes
,
2534 &min_stripes
, &sub_stripes
);
2538 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2543 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2548 map
->num_stripes
= num_stripes
;
2550 cur
= fs_devices
->alloc_list
.next
;
2554 calc_size
= __btrfs_calc_stripe_size(fs_devices
, calc_size
, type
,
2557 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2558 min_free
= calc_size
* 2;
2561 min_free
= calc_size
;
2562 min_devices
= min_stripes
;
2565 INIT_LIST_HEAD(&private_devs
);
2566 while (index
< num_stripes
) {
2567 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2568 BUG_ON(!device
->writeable
);
2569 if (device
->total_bytes
> device
->bytes_used
)
2570 avail
= device
->total_bytes
- device
->bytes_used
;
2575 if (device
->in_fs_metadata
&& avail
>= min_free
) {
2576 ret
= find_free_dev_extent(trans
, device
, min_free
,
2577 &devices_info
[i
].dev_offset
,
2578 &devices_info
[i
].max_avail
);
2580 list_move_tail(&device
->dev_alloc_list
,
2582 map
->stripes
[index
].dev
= device
;
2583 map
->stripes
[index
].physical
=
2584 devices_info
[i
].dev_offset
;
2586 if (type
& BTRFS_BLOCK_GROUP_DUP
) {
2587 map
->stripes
[index
].dev
= device
;
2588 map
->stripes
[index
].physical
=
2589 devices_info
[i
].dev_offset
+
2593 } else if (ret
!= -ENOSPC
)
2596 devices_info
[i
].dev
= device
;
2598 } else if (device
->in_fs_metadata
&&
2599 avail
>= BTRFS_STRIPE_LEN
) {
2600 devices_info
[i
].dev
= device
;
2601 devices_info
[i
].max_avail
= avail
;
2605 if (cur
== &fs_devices
->alloc_list
)
2609 list_splice(&private_devs
, &fs_devices
->alloc_list
);
2610 if (index
< num_stripes
) {
2611 if (index
>= min_stripes
) {
2612 num_stripes
= index
;
2613 if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2614 num_stripes
/= sub_stripes
;
2615 num_stripes
*= sub_stripes
;
2618 map
= __shrink_map_lookup_stripes(map
, num_stripes
);
2619 } else if (i
>= min_devices
) {
2620 ret
= __btrfs_alloc_tiny_space(trans
, fs_devices
,
2621 devices_info
, i
, type
,
2631 map
->sector_size
= extent_root
->sectorsize
;
2632 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2633 map
->io_align
= BTRFS_STRIPE_LEN
;
2634 map
->io_width
= BTRFS_STRIPE_LEN
;
2636 map
->sub_stripes
= sub_stripes
;
2639 *stripe_size
= calc_size
;
2640 *num_bytes
= chunk_bytes_by_type(type
, calc_size
,
2641 map
->num_stripes
, sub_stripes
);
2643 em
= alloc_extent_map(GFP_NOFS
);
2648 em
->bdev
= (struct block_device
*)map
;
2650 em
->len
= *num_bytes
;
2651 em
->block_start
= 0;
2652 em
->block_len
= em
->len
;
2654 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2655 write_lock(&em_tree
->lock
);
2656 ret
= add_extent_mapping(em_tree
, em
);
2657 write_unlock(&em_tree
->lock
);
2659 free_extent_map(em
);
2661 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2662 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2667 while (index
< map
->num_stripes
) {
2668 device
= map
->stripes
[index
].dev
;
2669 dev_offset
= map
->stripes
[index
].physical
;
2671 ret
= btrfs_alloc_dev_extent(trans
, device
,
2672 info
->chunk_root
->root_key
.objectid
,
2673 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2674 start
, dev_offset
, calc_size
);
2679 kfree(devices_info
);
2684 kfree(devices_info
);
2688 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2689 struct btrfs_root
*extent_root
,
2690 struct map_lookup
*map
, u64 chunk_offset
,
2691 u64 chunk_size
, u64 stripe_size
)
2694 struct btrfs_key key
;
2695 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2696 struct btrfs_device
*device
;
2697 struct btrfs_chunk
*chunk
;
2698 struct btrfs_stripe
*stripe
;
2699 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2703 chunk
= kzalloc(item_size
, GFP_NOFS
);
2708 while (index
< map
->num_stripes
) {
2709 device
= map
->stripes
[index
].dev
;
2710 device
->bytes_used
+= stripe_size
;
2711 ret
= btrfs_update_device(trans
, device
);
2717 stripe
= &chunk
->stripe
;
2718 while (index
< map
->num_stripes
) {
2719 device
= map
->stripes
[index
].dev
;
2720 dev_offset
= map
->stripes
[index
].physical
;
2722 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2723 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2724 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2729 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2730 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2731 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2732 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2733 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2734 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2735 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2736 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2737 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2739 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2740 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2741 key
.offset
= chunk_offset
;
2743 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2746 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2747 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2756 * Chunk allocation falls into two parts. The first part does works
2757 * that make the new allocated chunk useable, but not do any operation
2758 * that modifies the chunk tree. The second part does the works that
2759 * require modifying the chunk tree. This division is important for the
2760 * bootstrap process of adding storage to a seed btrfs.
2762 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2763 struct btrfs_root
*extent_root
, u64 type
)
2768 struct map_lookup
*map
;
2769 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2772 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2777 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2778 &stripe_size
, chunk_offset
, type
);
2782 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2783 chunk_size
, stripe_size
);
2788 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2789 struct btrfs_root
*root
,
2790 struct btrfs_device
*device
)
2793 u64 sys_chunk_offset
;
2797 u64 sys_stripe_size
;
2799 struct map_lookup
*map
;
2800 struct map_lookup
*sys_map
;
2801 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2802 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2805 ret
= find_next_chunk(fs_info
->chunk_root
,
2806 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2809 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2810 (fs_info
->metadata_alloc_profile
&
2811 fs_info
->avail_metadata_alloc_bits
);
2812 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2814 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2815 &stripe_size
, chunk_offset
, alloc_profile
);
2818 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2820 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2821 (fs_info
->system_alloc_profile
&
2822 fs_info
->avail_system_alloc_bits
);
2823 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2825 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2826 &sys_chunk_size
, &sys_stripe_size
,
2827 sys_chunk_offset
, alloc_profile
);
2830 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2834 * Modifying chunk tree needs allocating new blocks from both
2835 * system block group and metadata block group. So we only can
2836 * do operations require modifying the chunk tree after both
2837 * block groups were created.
2839 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2840 chunk_size
, stripe_size
);
2843 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2844 sys_chunk_offset
, sys_chunk_size
,
2850 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2852 struct extent_map
*em
;
2853 struct map_lookup
*map
;
2854 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2858 read_lock(&map_tree
->map_tree
.lock
);
2859 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2860 read_unlock(&map_tree
->map_tree
.lock
);
2864 if (btrfs_test_opt(root
, DEGRADED
)) {
2865 free_extent_map(em
);
2869 map
= (struct map_lookup
*)em
->bdev
;
2870 for (i
= 0; i
< map
->num_stripes
; i
++) {
2871 if (!map
->stripes
[i
].dev
->writeable
) {
2876 free_extent_map(em
);
2880 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2882 extent_map_tree_init(&tree
->map_tree
, GFP_NOFS
);
2885 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2887 struct extent_map
*em
;
2890 write_lock(&tree
->map_tree
.lock
);
2891 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2893 remove_extent_mapping(&tree
->map_tree
, em
);
2894 write_unlock(&tree
->map_tree
.lock
);
2899 free_extent_map(em
);
2900 /* once for the tree */
2901 free_extent_map(em
);
2905 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2907 struct extent_map
*em
;
2908 struct map_lookup
*map
;
2909 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2912 read_lock(&em_tree
->lock
);
2913 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2914 read_unlock(&em_tree
->lock
);
2917 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2918 map
= (struct map_lookup
*)em
->bdev
;
2919 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2920 ret
= map
->num_stripes
;
2921 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2922 ret
= map
->sub_stripes
;
2925 free_extent_map(em
);
2929 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2933 if (map
->stripes
[optimal
].dev
->bdev
)
2935 for (i
= first
; i
< first
+ num
; i
++) {
2936 if (map
->stripes
[i
].dev
->bdev
)
2939 /* we couldn't find one that doesn't fail. Just return something
2940 * and the io error handling code will clean up eventually
2945 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2946 u64 logical
, u64
*length
,
2947 struct btrfs_multi_bio
**multi_ret
,
2948 int mirror_num
, struct page
*unplug_page
)
2950 struct extent_map
*em
;
2951 struct map_lookup
*map
;
2952 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2956 int stripes_allocated
= 8;
2957 int stripes_required
= 1;
2962 struct btrfs_multi_bio
*multi
= NULL
;
2964 if (multi_ret
&& !(rw
& REQ_WRITE
))
2965 stripes_allocated
= 1;
2968 multi
= kzalloc(btrfs_multi_bio_size(stripes_allocated
),
2973 atomic_set(&multi
->error
, 0);
2976 read_lock(&em_tree
->lock
);
2977 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2978 read_unlock(&em_tree
->lock
);
2980 if (!em
&& unplug_page
) {
2986 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2987 (unsigned long long)logical
,
2988 (unsigned long long)*length
);
2992 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2993 map
= (struct map_lookup
*)em
->bdev
;
2994 offset
= logical
- em
->start
;
2996 if (mirror_num
> map
->num_stripes
)
2999 /* if our multi bio struct is too small, back off and try again */
3000 if (rw
& REQ_WRITE
) {
3001 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
3002 BTRFS_BLOCK_GROUP_DUP
)) {
3003 stripes_required
= map
->num_stripes
;
3005 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3006 stripes_required
= map
->sub_stripes
;
3010 if (multi_ret
&& (rw
& REQ_WRITE
) &&
3011 stripes_allocated
< stripes_required
) {
3012 stripes_allocated
= map
->num_stripes
;
3013 free_extent_map(em
);
3019 * stripe_nr counts the total number of stripes we have to stride
3020 * to get to this block
3022 do_div(stripe_nr
, map
->stripe_len
);
3024 stripe_offset
= stripe_nr
* map
->stripe_len
;
3025 BUG_ON(offset
< stripe_offset
);
3027 /* stripe_offset is the offset of this block in its stripe*/
3028 stripe_offset
= offset
- stripe_offset
;
3030 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
3031 BTRFS_BLOCK_GROUP_RAID10
|
3032 BTRFS_BLOCK_GROUP_DUP
)) {
3033 /* we limit the length of each bio to what fits in a stripe */
3034 *length
= min_t(u64
, em
->len
- offset
,
3035 map
->stripe_len
- stripe_offset
);
3037 *length
= em
->len
- offset
;
3040 if (!multi_ret
&& !unplug_page
)
3045 if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3046 if (unplug_page
|| (rw
& REQ_WRITE
))
3047 num_stripes
= map
->num_stripes
;
3048 else if (mirror_num
)
3049 stripe_index
= mirror_num
- 1;
3051 stripe_index
= find_live_mirror(map
, 0,
3053 current
->pid
% map
->num_stripes
);
3056 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3058 num_stripes
= map
->num_stripes
;
3059 else if (mirror_num
)
3060 stripe_index
= mirror_num
- 1;
3062 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3063 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3065 stripe_index
= do_div(stripe_nr
, factor
);
3066 stripe_index
*= map
->sub_stripes
;
3068 if (unplug_page
|| (rw
& REQ_WRITE
))
3069 num_stripes
= map
->sub_stripes
;
3070 else if (mirror_num
)
3071 stripe_index
+= mirror_num
- 1;
3073 stripe_index
= find_live_mirror(map
, stripe_index
,
3074 map
->sub_stripes
, stripe_index
+
3075 current
->pid
% map
->sub_stripes
);
3079 * after this do_div call, stripe_nr is the number of stripes
3080 * on this device we have to walk to find the data, and
3081 * stripe_index is the number of our device in the stripe array
3083 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3085 BUG_ON(stripe_index
>= map
->num_stripes
);
3087 for (i
= 0; i
< num_stripes
; i
++) {
3089 struct btrfs_device
*device
;
3090 struct backing_dev_info
*bdi
;
3092 device
= map
->stripes
[stripe_index
].dev
;
3094 bdi
= blk_get_backing_dev_info(device
->bdev
);
3095 if (bdi
->unplug_io_fn
)
3096 bdi
->unplug_io_fn(bdi
, unplug_page
);
3099 multi
->stripes
[i
].physical
=
3100 map
->stripes
[stripe_index
].physical
+
3101 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3102 multi
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3108 multi
->num_stripes
= num_stripes
;
3109 multi
->max_errors
= max_errors
;
3112 free_extent_map(em
);
3116 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3117 u64 logical
, u64
*length
,
3118 struct btrfs_multi_bio
**multi_ret
, int mirror_num
)
3120 return __btrfs_map_block(map_tree
, rw
, logical
, length
, multi_ret
,
3124 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3125 u64 chunk_start
, u64 physical
, u64 devid
,
3126 u64
**logical
, int *naddrs
, int *stripe_len
)
3128 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3129 struct extent_map
*em
;
3130 struct map_lookup
*map
;
3137 read_lock(&em_tree
->lock
);
3138 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3139 read_unlock(&em_tree
->lock
);
3141 BUG_ON(!em
|| em
->start
!= chunk_start
);
3142 map
= (struct map_lookup
*)em
->bdev
;
3145 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3146 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3147 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3148 do_div(length
, map
->num_stripes
);
3150 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3153 for (i
= 0; i
< map
->num_stripes
; i
++) {
3154 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3156 if (map
->stripes
[i
].physical
> physical
||
3157 map
->stripes
[i
].physical
+ length
<= physical
)
3160 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3161 do_div(stripe_nr
, map
->stripe_len
);
3163 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3164 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3165 do_div(stripe_nr
, map
->sub_stripes
);
3166 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3167 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3169 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3170 WARN_ON(nr
>= map
->num_stripes
);
3171 for (j
= 0; j
< nr
; j
++) {
3172 if (buf
[j
] == bytenr
)
3176 WARN_ON(nr
>= map
->num_stripes
);
3183 *stripe_len
= map
->stripe_len
;
3185 free_extent_map(em
);
3189 int btrfs_unplug_page(struct btrfs_mapping_tree
*map_tree
,
3190 u64 logical
, struct page
*page
)
3192 u64 length
= PAGE_CACHE_SIZE
;
3193 return __btrfs_map_block(map_tree
, READ
, logical
, &length
,
3197 static void end_bio_multi_stripe(struct bio
*bio
, int err
)
3199 struct btrfs_multi_bio
*multi
= bio
->bi_private
;
3200 int is_orig_bio
= 0;
3203 atomic_inc(&multi
->error
);
3205 if (bio
== multi
->orig_bio
)
3208 if (atomic_dec_and_test(&multi
->stripes_pending
)) {
3211 bio
= multi
->orig_bio
;
3213 bio
->bi_private
= multi
->private;
3214 bio
->bi_end_io
= multi
->end_io
;
3215 /* only send an error to the higher layers if it is
3216 * beyond the tolerance of the multi-bio
3218 if (atomic_read(&multi
->error
) > multi
->max_errors
) {
3222 * this bio is actually up to date, we didn't
3223 * go over the max number of errors
3225 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3230 bio_endio(bio
, err
);
3231 } else if (!is_orig_bio
) {
3236 struct async_sched
{
3239 struct btrfs_fs_info
*info
;
3240 struct btrfs_work work
;
3244 * see run_scheduled_bios for a description of why bios are collected for
3247 * This will add one bio to the pending list for a device and make sure
3248 * the work struct is scheduled.
3250 static noinline
int schedule_bio(struct btrfs_root
*root
,
3251 struct btrfs_device
*device
,
3252 int rw
, struct bio
*bio
)
3254 int should_queue
= 1;
3255 struct btrfs_pending_bios
*pending_bios
;
3257 /* don't bother with additional async steps for reads, right now */
3258 if (!(rw
& REQ_WRITE
)) {
3260 submit_bio(rw
, bio
);
3266 * nr_async_bios allows us to reliably return congestion to the
3267 * higher layers. Otherwise, the async bio makes it appear we have
3268 * made progress against dirty pages when we've really just put it
3269 * on a queue for later
3271 atomic_inc(&root
->fs_info
->nr_async_bios
);
3272 WARN_ON(bio
->bi_next
);
3273 bio
->bi_next
= NULL
;
3276 spin_lock(&device
->io_lock
);
3277 if (bio
->bi_rw
& REQ_SYNC
)
3278 pending_bios
= &device
->pending_sync_bios
;
3280 pending_bios
= &device
->pending_bios
;
3282 if (pending_bios
->tail
)
3283 pending_bios
->tail
->bi_next
= bio
;
3285 pending_bios
->tail
= bio
;
3286 if (!pending_bios
->head
)
3287 pending_bios
->head
= bio
;
3288 if (device
->running_pending
)
3291 spin_unlock(&device
->io_lock
);
3294 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3299 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3300 int mirror_num
, int async_submit
)
3302 struct btrfs_mapping_tree
*map_tree
;
3303 struct btrfs_device
*dev
;
3304 struct bio
*first_bio
= bio
;
3305 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3308 struct btrfs_multi_bio
*multi
= NULL
;
3313 length
= bio
->bi_size
;
3314 map_tree
= &root
->fs_info
->mapping_tree
;
3315 map_length
= length
;
3317 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &multi
,
3321 total_devs
= multi
->num_stripes
;
3322 if (map_length
< length
) {
3323 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3324 "len %llu\n", (unsigned long long)logical
,
3325 (unsigned long long)length
,
3326 (unsigned long long)map_length
);
3329 multi
->end_io
= first_bio
->bi_end_io
;
3330 multi
->private = first_bio
->bi_private
;
3331 multi
->orig_bio
= first_bio
;
3332 atomic_set(&multi
->stripes_pending
, multi
->num_stripes
);
3334 while (dev_nr
< total_devs
) {
3335 if (total_devs
> 1) {
3336 if (dev_nr
< total_devs
- 1) {
3337 bio
= bio_clone(first_bio
, GFP_NOFS
);
3342 bio
->bi_private
= multi
;
3343 bio
->bi_end_io
= end_bio_multi_stripe
;
3345 bio
->bi_sector
= multi
->stripes
[dev_nr
].physical
>> 9;
3346 dev
= multi
->stripes
[dev_nr
].dev
;
3347 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3348 bio
->bi_bdev
= dev
->bdev
;
3350 schedule_bio(root
, dev
, rw
, bio
);
3352 submit_bio(rw
, bio
);
3354 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3355 bio
->bi_sector
= logical
>> 9;
3356 bio_endio(bio
, -EIO
);
3360 if (total_devs
== 1)
3365 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3368 struct btrfs_device
*device
;
3369 struct btrfs_fs_devices
*cur_devices
;
3371 cur_devices
= root
->fs_info
->fs_devices
;
3372 while (cur_devices
) {
3374 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3375 device
= __find_device(&cur_devices
->devices
,
3380 cur_devices
= cur_devices
->seed
;
3385 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3386 u64 devid
, u8
*dev_uuid
)
3388 struct btrfs_device
*device
;
3389 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3391 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3394 list_add(&device
->dev_list
,
3395 &fs_devices
->devices
);
3396 device
->barriers
= 1;
3397 device
->dev_root
= root
->fs_info
->dev_root
;
3398 device
->devid
= devid
;
3399 device
->work
.func
= pending_bios_fn
;
3400 device
->fs_devices
= fs_devices
;
3401 device
->missing
= 1;
3402 fs_devices
->num_devices
++;
3403 fs_devices
->missing_devices
++;
3404 spin_lock_init(&device
->io_lock
);
3405 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3406 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3410 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3411 struct extent_buffer
*leaf
,
3412 struct btrfs_chunk
*chunk
)
3414 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3415 struct map_lookup
*map
;
3416 struct extent_map
*em
;
3420 u8 uuid
[BTRFS_UUID_SIZE
];
3425 logical
= key
->offset
;
3426 length
= btrfs_chunk_length(leaf
, chunk
);
3428 read_lock(&map_tree
->map_tree
.lock
);
3429 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3430 read_unlock(&map_tree
->map_tree
.lock
);
3432 /* already mapped? */
3433 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3434 free_extent_map(em
);
3437 free_extent_map(em
);
3440 em
= alloc_extent_map(GFP_NOFS
);
3443 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3444 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3446 free_extent_map(em
);
3450 em
->bdev
= (struct block_device
*)map
;
3451 em
->start
= logical
;
3453 em
->block_start
= 0;
3454 em
->block_len
= em
->len
;
3456 map
->num_stripes
= num_stripes
;
3457 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3458 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3459 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3460 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3461 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3462 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3463 for (i
= 0; i
< num_stripes
; i
++) {
3464 map
->stripes
[i
].physical
=
3465 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3466 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3467 read_extent_buffer(leaf
, uuid
, (unsigned long)
3468 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3470 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3472 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3474 free_extent_map(em
);
3477 if (!map
->stripes
[i
].dev
) {
3478 map
->stripes
[i
].dev
=
3479 add_missing_dev(root
, devid
, uuid
);
3480 if (!map
->stripes
[i
].dev
) {
3482 free_extent_map(em
);
3486 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3489 write_lock(&map_tree
->map_tree
.lock
);
3490 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3491 write_unlock(&map_tree
->map_tree
.lock
);
3493 free_extent_map(em
);
3498 static int fill_device_from_item(struct extent_buffer
*leaf
,
3499 struct btrfs_dev_item
*dev_item
,
3500 struct btrfs_device
*device
)
3504 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3505 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3506 device
->total_bytes
= device
->disk_total_bytes
;
3507 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3508 device
->type
= btrfs_device_type(leaf
, dev_item
);
3509 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3510 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3511 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3513 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3514 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3519 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3521 struct btrfs_fs_devices
*fs_devices
;
3524 mutex_lock(&uuid_mutex
);
3526 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3527 while (fs_devices
) {
3528 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3532 fs_devices
= fs_devices
->seed
;
3535 fs_devices
= find_fsid(fsid
);
3541 fs_devices
= clone_fs_devices(fs_devices
);
3542 if (IS_ERR(fs_devices
)) {
3543 ret
= PTR_ERR(fs_devices
);
3547 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3548 root
->fs_info
->bdev_holder
);
3552 if (!fs_devices
->seeding
) {
3553 __btrfs_close_devices(fs_devices
);
3554 free_fs_devices(fs_devices
);
3559 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3560 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3562 mutex_unlock(&uuid_mutex
);
3566 static int read_one_dev(struct btrfs_root
*root
,
3567 struct extent_buffer
*leaf
,
3568 struct btrfs_dev_item
*dev_item
)
3570 struct btrfs_device
*device
;
3573 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3574 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3576 devid
= btrfs_device_id(leaf
, dev_item
);
3577 read_extent_buffer(leaf
, dev_uuid
,
3578 (unsigned long)btrfs_device_uuid(dev_item
),
3580 read_extent_buffer(leaf
, fs_uuid
,
3581 (unsigned long)btrfs_device_fsid(dev_item
),
3584 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3585 ret
= open_seed_devices(root
, fs_uuid
);
3586 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3590 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3591 if (!device
|| !device
->bdev
) {
3592 if (!btrfs_test_opt(root
, DEGRADED
))
3596 printk(KERN_WARNING
"warning devid %llu missing\n",
3597 (unsigned long long)devid
);
3598 device
= add_missing_dev(root
, devid
, dev_uuid
);
3601 } else if (!device
->missing
) {
3603 * this happens when a device that was properly setup
3604 * in the device info lists suddenly goes bad.
3605 * device->bdev is NULL, and so we have to set
3606 * device->missing to one here
3608 root
->fs_info
->fs_devices
->missing_devices
++;
3609 device
->missing
= 1;
3613 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3614 BUG_ON(device
->writeable
);
3615 if (device
->generation
!=
3616 btrfs_device_generation(leaf
, dev_item
))
3620 fill_device_from_item(leaf
, dev_item
, device
);
3621 device
->dev_root
= root
->fs_info
->dev_root
;
3622 device
->in_fs_metadata
= 1;
3623 if (device
->writeable
)
3624 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3629 int btrfs_read_super_device(struct btrfs_root
*root
, struct extent_buffer
*buf
)
3631 struct btrfs_dev_item
*dev_item
;
3633 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
3635 return read_one_dev(root
, buf
, dev_item
);
3638 int btrfs_read_sys_array(struct btrfs_root
*root
)
3640 struct btrfs_super_block
*super_copy
= &root
->fs_info
->super_copy
;
3641 struct extent_buffer
*sb
;
3642 struct btrfs_disk_key
*disk_key
;
3643 struct btrfs_chunk
*chunk
;
3645 unsigned long sb_ptr
;
3651 struct btrfs_key key
;
3653 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3654 BTRFS_SUPER_INFO_SIZE
);
3657 btrfs_set_buffer_uptodate(sb
);
3658 btrfs_set_buffer_lockdep_class(sb
, 0);
3660 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3661 array_size
= btrfs_super_sys_array_size(super_copy
);
3663 ptr
= super_copy
->sys_chunk_array
;
3664 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3667 while (cur
< array_size
) {
3668 disk_key
= (struct btrfs_disk_key
*)ptr
;
3669 btrfs_disk_key_to_cpu(&key
, disk_key
);
3671 len
= sizeof(*disk_key
); ptr
+= len
;
3675 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3676 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3677 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3680 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3681 len
= btrfs_chunk_item_size(num_stripes
);
3690 free_extent_buffer(sb
);
3694 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3696 struct btrfs_path
*path
;
3697 struct extent_buffer
*leaf
;
3698 struct btrfs_key key
;
3699 struct btrfs_key found_key
;
3703 root
= root
->fs_info
->chunk_root
;
3705 path
= btrfs_alloc_path();
3709 /* first we search for all of the device items, and then we
3710 * read in all of the chunk items. This way we can create chunk
3711 * mappings that reference all of the devices that are afound
3713 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3717 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3721 leaf
= path
->nodes
[0];
3722 slot
= path
->slots
[0];
3723 if (slot
>= btrfs_header_nritems(leaf
)) {
3724 ret
= btrfs_next_leaf(root
, path
);
3731 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3732 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3733 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3735 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3736 struct btrfs_dev_item
*dev_item
;
3737 dev_item
= btrfs_item_ptr(leaf
, slot
,
3738 struct btrfs_dev_item
);
3739 ret
= read_one_dev(root
, leaf
, dev_item
);
3743 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3744 struct btrfs_chunk
*chunk
;
3745 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3746 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3752 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3754 btrfs_release_path(root
, path
);
3759 btrfs_free_path(path
);