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 <linux/kthread.h>
27 #include <asm/div64.h>
30 #include "extent_map.h"
32 #include "transaction.h"
33 #include "print-tree.h"
35 #include "async-thread.h"
36 #include "check-integrity.h"
38 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
39 struct btrfs_root
*root
,
40 struct btrfs_device
*device
);
41 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
43 static DEFINE_MUTEX(uuid_mutex
);
44 static LIST_HEAD(fs_uuids
);
46 static void lock_chunks(struct btrfs_root
*root
)
48 mutex_lock(&root
->fs_info
->chunk_mutex
);
51 static void unlock_chunks(struct btrfs_root
*root
)
53 mutex_unlock(&root
->fs_info
->chunk_mutex
);
56 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
58 struct btrfs_device
*device
;
59 WARN_ON(fs_devices
->opened
);
60 while (!list_empty(&fs_devices
->devices
)) {
61 device
= list_entry(fs_devices
->devices
.next
,
62 struct btrfs_device
, dev_list
);
63 list_del(&device
->dev_list
);
70 int btrfs_cleanup_fs_uuids(void)
72 struct btrfs_fs_devices
*fs_devices
;
74 while (!list_empty(&fs_uuids
)) {
75 fs_devices
= list_entry(fs_uuids
.next
,
76 struct btrfs_fs_devices
, list
);
77 list_del(&fs_devices
->list
);
78 free_fs_devices(fs_devices
);
83 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
86 struct btrfs_device
*dev
;
88 list_for_each_entry(dev
, head
, dev_list
) {
89 if (dev
->devid
== devid
&&
90 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
97 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
99 struct btrfs_fs_devices
*fs_devices
;
101 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
102 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
108 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
109 struct bio
*head
, struct bio
*tail
)
112 struct bio
*old_head
;
114 old_head
= pending_bios
->head
;
115 pending_bios
->head
= head
;
116 if (pending_bios
->tail
)
117 tail
->bi_next
= old_head
;
119 pending_bios
->tail
= tail
;
123 * we try to collect pending bios for a device so we don't get a large
124 * number of procs sending bios down to the same device. This greatly
125 * improves the schedulers ability to collect and merge the bios.
127 * But, it also turns into a long list of bios to process and that is sure
128 * to eventually make the worker thread block. The solution here is to
129 * make some progress and then put this work struct back at the end of
130 * the list if the block device is congested. This way, multiple devices
131 * can make progress from a single worker thread.
133 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
136 struct backing_dev_info
*bdi
;
137 struct btrfs_fs_info
*fs_info
;
138 struct btrfs_pending_bios
*pending_bios
;
142 unsigned long num_run
;
143 unsigned long batch_run
= 0;
145 unsigned long last_waited
= 0;
147 int sync_pending
= 0;
148 struct blk_plug plug
;
151 * this function runs all the bios we've collected for
152 * a particular device. We don't want to wander off to
153 * another device without first sending all of these down.
154 * So, setup a plug here and finish it off before we return
156 blk_start_plug(&plug
);
158 bdi
= blk_get_backing_dev_info(device
->bdev
);
159 fs_info
= device
->dev_root
->fs_info
;
160 limit
= btrfs_async_submit_limit(fs_info
);
161 limit
= limit
* 2 / 3;
164 spin_lock(&device
->io_lock
);
169 /* take all the bios off the list at once and process them
170 * later on (without the lock held). But, remember the
171 * tail and other pointers so the bios can be properly reinserted
172 * into the list if we hit congestion
174 if (!force_reg
&& device
->pending_sync_bios
.head
) {
175 pending_bios
= &device
->pending_sync_bios
;
178 pending_bios
= &device
->pending_bios
;
182 pending
= pending_bios
->head
;
183 tail
= pending_bios
->tail
;
184 WARN_ON(pending
&& !tail
);
187 * if pending was null this time around, no bios need processing
188 * at all and we can stop. Otherwise it'll loop back up again
189 * and do an additional check so no bios are missed.
191 * device->running_pending is used to synchronize with the
194 if (device
->pending_sync_bios
.head
== NULL
&&
195 device
->pending_bios
.head
== NULL
) {
197 device
->running_pending
= 0;
200 device
->running_pending
= 1;
203 pending_bios
->head
= NULL
;
204 pending_bios
->tail
= NULL
;
206 spin_unlock(&device
->io_lock
);
211 /* we want to work on both lists, but do more bios on the
212 * sync list than the regular list
215 pending_bios
!= &device
->pending_sync_bios
&&
216 device
->pending_sync_bios
.head
) ||
217 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
218 device
->pending_bios
.head
)) {
219 spin_lock(&device
->io_lock
);
220 requeue_list(pending_bios
, pending
, tail
);
225 pending
= pending
->bi_next
;
227 atomic_dec(&fs_info
->nr_async_bios
);
229 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
230 waitqueue_active(&fs_info
->async_submit_wait
))
231 wake_up(&fs_info
->async_submit_wait
);
233 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
236 * if we're doing the sync list, record that our
237 * plug has some sync requests on it
239 * If we're doing the regular list and there are
240 * sync requests sitting around, unplug before
243 if (pending_bios
== &device
->pending_sync_bios
) {
245 } else if (sync_pending
) {
246 blk_finish_plug(&plug
);
247 blk_start_plug(&plug
);
251 btrfsic_submit_bio(cur
->bi_rw
, cur
);
258 * we made progress, there is more work to do and the bdi
259 * is now congested. Back off and let other work structs
262 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
263 fs_info
->fs_devices
->open_devices
> 1) {
264 struct io_context
*ioc
;
266 ioc
= current
->io_context
;
269 * the main goal here is that we don't want to
270 * block if we're going to be able to submit
271 * more requests without blocking.
273 * This code does two great things, it pokes into
274 * the elevator code from a filesystem _and_
275 * it makes assumptions about how batching works.
277 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
278 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
280 ioc
->last_waited
== last_waited
)) {
282 * we want to go through our batch of
283 * requests and stop. So, we copy out
284 * the ioc->last_waited time and test
285 * against it before looping
287 last_waited
= ioc
->last_waited
;
292 spin_lock(&device
->io_lock
);
293 requeue_list(pending_bios
, pending
, tail
);
294 device
->running_pending
= 1;
296 spin_unlock(&device
->io_lock
);
297 btrfs_requeue_work(&device
->work
);
300 /* unplug every 64 requests just for good measure */
301 if (batch_run
% 64 == 0) {
302 blk_finish_plug(&plug
);
303 blk_start_plug(&plug
);
312 spin_lock(&device
->io_lock
);
313 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
315 spin_unlock(&device
->io_lock
);
318 blk_finish_plug(&plug
);
322 static void pending_bios_fn(struct btrfs_work
*work
)
324 struct btrfs_device
*device
;
326 device
= container_of(work
, struct btrfs_device
, work
);
327 run_scheduled_bios(device
);
330 static noinline
int device_list_add(const char *path
,
331 struct btrfs_super_block
*disk_super
,
332 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
334 struct btrfs_device
*device
;
335 struct btrfs_fs_devices
*fs_devices
;
336 u64 found_transid
= btrfs_super_generation(disk_super
);
339 fs_devices
= find_fsid(disk_super
->fsid
);
341 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
344 INIT_LIST_HEAD(&fs_devices
->devices
);
345 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
346 list_add(&fs_devices
->list
, &fs_uuids
);
347 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
348 fs_devices
->latest_devid
= devid
;
349 fs_devices
->latest_trans
= found_transid
;
350 mutex_init(&fs_devices
->device_list_mutex
);
353 device
= __find_device(&fs_devices
->devices
, devid
,
354 disk_super
->dev_item
.uuid
);
357 if (fs_devices
->opened
)
360 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
362 /* we can safely leave the fs_devices entry around */
365 device
->devid
= devid
;
366 device
->work
.func
= pending_bios_fn
;
367 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
369 spin_lock_init(&device
->io_lock
);
370 device
->name
= kstrdup(path
, GFP_NOFS
);
375 INIT_LIST_HEAD(&device
->dev_alloc_list
);
377 /* init readahead state */
378 spin_lock_init(&device
->reada_lock
);
379 device
->reada_curr_zone
= NULL
;
380 atomic_set(&device
->reada_in_flight
, 0);
381 device
->reada_next
= 0;
382 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
383 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
385 mutex_lock(&fs_devices
->device_list_mutex
);
386 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
387 mutex_unlock(&fs_devices
->device_list_mutex
);
389 device
->fs_devices
= fs_devices
;
390 fs_devices
->num_devices
++;
391 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
392 name
= kstrdup(path
, GFP_NOFS
);
397 if (device
->missing
) {
398 fs_devices
->missing_devices
--;
403 if (found_transid
> fs_devices
->latest_trans
) {
404 fs_devices
->latest_devid
= devid
;
405 fs_devices
->latest_trans
= found_transid
;
407 *fs_devices_ret
= fs_devices
;
411 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
413 struct btrfs_fs_devices
*fs_devices
;
414 struct btrfs_device
*device
;
415 struct btrfs_device
*orig_dev
;
417 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
419 return ERR_PTR(-ENOMEM
);
421 INIT_LIST_HEAD(&fs_devices
->devices
);
422 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
423 INIT_LIST_HEAD(&fs_devices
->list
);
424 mutex_init(&fs_devices
->device_list_mutex
);
425 fs_devices
->latest_devid
= orig
->latest_devid
;
426 fs_devices
->latest_trans
= orig
->latest_trans
;
427 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
429 /* We have held the volume lock, it is safe to get the devices. */
430 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
431 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
435 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
441 device
->devid
= orig_dev
->devid
;
442 device
->work
.func
= pending_bios_fn
;
443 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
444 spin_lock_init(&device
->io_lock
);
445 INIT_LIST_HEAD(&device
->dev_list
);
446 INIT_LIST_HEAD(&device
->dev_alloc_list
);
448 list_add(&device
->dev_list
, &fs_devices
->devices
);
449 device
->fs_devices
= fs_devices
;
450 fs_devices
->num_devices
++;
454 free_fs_devices(fs_devices
);
455 return ERR_PTR(-ENOMEM
);
458 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
460 struct btrfs_device
*device
, *next
;
462 mutex_lock(&uuid_mutex
);
464 /* This is the initialized path, it is safe to release the devices. */
465 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
466 if (device
->in_fs_metadata
)
470 blkdev_put(device
->bdev
, device
->mode
);
472 fs_devices
->open_devices
--;
474 if (device
->writeable
) {
475 list_del_init(&device
->dev_alloc_list
);
476 device
->writeable
= 0;
477 fs_devices
->rw_devices
--;
479 list_del_init(&device
->dev_list
);
480 fs_devices
->num_devices
--;
485 if (fs_devices
->seed
) {
486 fs_devices
= fs_devices
->seed
;
490 mutex_unlock(&uuid_mutex
);
494 static void __free_device(struct work_struct
*work
)
496 struct btrfs_device
*device
;
498 device
= container_of(work
, struct btrfs_device
, rcu_work
);
501 blkdev_put(device
->bdev
, device
->mode
);
507 static void free_device(struct rcu_head
*head
)
509 struct btrfs_device
*device
;
511 device
= container_of(head
, struct btrfs_device
, rcu
);
513 INIT_WORK(&device
->rcu_work
, __free_device
);
514 schedule_work(&device
->rcu_work
);
517 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
519 struct btrfs_device
*device
;
521 if (--fs_devices
->opened
> 0)
524 mutex_lock(&fs_devices
->device_list_mutex
);
525 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
526 struct btrfs_device
*new_device
;
529 fs_devices
->open_devices
--;
531 if (device
->writeable
) {
532 list_del_init(&device
->dev_alloc_list
);
533 fs_devices
->rw_devices
--;
536 if (device
->can_discard
)
537 fs_devices
->num_can_discard
--;
539 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
541 memcpy(new_device
, device
, sizeof(*new_device
));
542 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
543 BUG_ON(device
->name
&& !new_device
->name
);
544 new_device
->bdev
= NULL
;
545 new_device
->writeable
= 0;
546 new_device
->in_fs_metadata
= 0;
547 new_device
->can_discard
= 0;
548 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
550 call_rcu(&device
->rcu
, free_device
);
552 mutex_unlock(&fs_devices
->device_list_mutex
);
554 WARN_ON(fs_devices
->open_devices
);
555 WARN_ON(fs_devices
->rw_devices
);
556 fs_devices
->opened
= 0;
557 fs_devices
->seeding
= 0;
562 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
564 struct btrfs_fs_devices
*seed_devices
= NULL
;
567 mutex_lock(&uuid_mutex
);
568 ret
= __btrfs_close_devices(fs_devices
);
569 if (!fs_devices
->opened
) {
570 seed_devices
= fs_devices
->seed
;
571 fs_devices
->seed
= NULL
;
573 mutex_unlock(&uuid_mutex
);
575 while (seed_devices
) {
576 fs_devices
= seed_devices
;
577 seed_devices
= fs_devices
->seed
;
578 __btrfs_close_devices(fs_devices
);
579 free_fs_devices(fs_devices
);
584 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
585 fmode_t flags
, void *holder
)
587 struct request_queue
*q
;
588 struct block_device
*bdev
;
589 struct list_head
*head
= &fs_devices
->devices
;
590 struct btrfs_device
*device
;
591 struct block_device
*latest_bdev
= NULL
;
592 struct buffer_head
*bh
;
593 struct btrfs_super_block
*disk_super
;
594 u64 latest_devid
= 0;
595 u64 latest_transid
= 0;
602 list_for_each_entry(device
, head
, dev_list
) {
608 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
610 printk(KERN_INFO
"open %s failed\n", device
->name
);
613 set_blocksize(bdev
, 4096);
615 bh
= btrfs_read_dev_super(bdev
);
619 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
620 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
621 if (devid
!= device
->devid
)
624 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
628 device
->generation
= btrfs_super_generation(disk_super
);
629 if (!latest_transid
|| device
->generation
> latest_transid
) {
630 latest_devid
= devid
;
631 latest_transid
= device
->generation
;
635 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
636 device
->writeable
= 0;
638 device
->writeable
= !bdev_read_only(bdev
);
642 q
= bdev_get_queue(bdev
);
643 if (blk_queue_discard(q
)) {
644 device
->can_discard
= 1;
645 fs_devices
->num_can_discard
++;
649 device
->in_fs_metadata
= 0;
650 device
->mode
= flags
;
652 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
653 fs_devices
->rotating
= 1;
655 fs_devices
->open_devices
++;
656 if (device
->writeable
) {
657 fs_devices
->rw_devices
++;
658 list_add(&device
->dev_alloc_list
,
659 &fs_devices
->alloc_list
);
667 blkdev_put(bdev
, flags
);
671 if (fs_devices
->open_devices
== 0) {
675 fs_devices
->seeding
= seeding
;
676 fs_devices
->opened
= 1;
677 fs_devices
->latest_bdev
= latest_bdev
;
678 fs_devices
->latest_devid
= latest_devid
;
679 fs_devices
->latest_trans
= latest_transid
;
680 fs_devices
->total_rw_bytes
= 0;
685 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
686 fmode_t flags
, void *holder
)
690 mutex_lock(&uuid_mutex
);
691 if (fs_devices
->opened
) {
692 fs_devices
->opened
++;
695 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
697 mutex_unlock(&uuid_mutex
);
701 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
702 struct btrfs_fs_devices
**fs_devices_ret
)
704 struct btrfs_super_block
*disk_super
;
705 struct block_device
*bdev
;
706 struct buffer_head
*bh
;
712 bdev
= blkdev_get_by_path(path
, flags
, holder
);
719 mutex_lock(&uuid_mutex
);
720 ret
= set_blocksize(bdev
, 4096);
723 bh
= btrfs_read_dev_super(bdev
);
728 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
729 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
730 transid
= btrfs_super_generation(disk_super
);
731 if (disk_super
->label
[0])
732 printk(KERN_INFO
"device label %s ", disk_super
->label
);
734 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
735 printk(KERN_CONT
"devid %llu transid %llu %s\n",
736 (unsigned long long)devid
, (unsigned long long)transid
, path
);
737 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
741 mutex_unlock(&uuid_mutex
);
742 blkdev_put(bdev
, flags
);
747 /* helper to account the used device space in the range */
748 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
749 u64 end
, u64
*length
)
751 struct btrfs_key key
;
752 struct btrfs_root
*root
= device
->dev_root
;
753 struct btrfs_dev_extent
*dev_extent
;
754 struct btrfs_path
*path
;
758 struct extent_buffer
*l
;
762 if (start
>= device
->total_bytes
)
765 path
= btrfs_alloc_path();
770 key
.objectid
= device
->devid
;
772 key
.type
= BTRFS_DEV_EXTENT_KEY
;
774 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
778 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
785 slot
= path
->slots
[0];
786 if (slot
>= btrfs_header_nritems(l
)) {
787 ret
= btrfs_next_leaf(root
, path
);
795 btrfs_item_key_to_cpu(l
, &key
, slot
);
797 if (key
.objectid
< device
->devid
)
800 if (key
.objectid
> device
->devid
)
803 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
806 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
807 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
809 if (key
.offset
<= start
&& extent_end
> end
) {
810 *length
= end
- start
+ 1;
812 } else if (key
.offset
<= start
&& extent_end
> start
)
813 *length
+= extent_end
- start
;
814 else if (key
.offset
> start
&& extent_end
<= end
)
815 *length
+= extent_end
- key
.offset
;
816 else if (key
.offset
> start
&& key
.offset
<= end
) {
817 *length
+= end
- key
.offset
+ 1;
819 } else if (key
.offset
> end
)
827 btrfs_free_path(path
);
832 * find_free_dev_extent - find free space in the specified device
833 * @device: the device which we search the free space in
834 * @num_bytes: the size of the free space that we need
835 * @start: store the start of the free space.
836 * @len: the size of the free space. that we find, or the size of the max
837 * free space if we don't find suitable free space
839 * this uses a pretty simple search, the expectation is that it is
840 * called very infrequently and that a given device has a small number
843 * @start is used to store the start of the free space if we find. But if we
844 * don't find suitable free space, it will be used to store the start position
845 * of the max free space.
847 * @len is used to store the size of the free space that we find.
848 * But if we don't find suitable free space, it is used to store the size of
849 * the max free space.
851 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
852 u64
*start
, u64
*len
)
854 struct btrfs_key key
;
855 struct btrfs_root
*root
= device
->dev_root
;
856 struct btrfs_dev_extent
*dev_extent
;
857 struct btrfs_path
*path
;
863 u64 search_end
= device
->total_bytes
;
866 struct extent_buffer
*l
;
868 /* FIXME use last free of some kind */
870 /* we don't want to overwrite the superblock on the drive,
871 * so we make sure to start at an offset of at least 1MB
873 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
875 max_hole_start
= search_start
;
879 if (search_start
>= search_end
) {
884 path
= btrfs_alloc_path();
891 key
.objectid
= device
->devid
;
892 key
.offset
= search_start
;
893 key
.type
= BTRFS_DEV_EXTENT_KEY
;
895 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
899 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
906 slot
= path
->slots
[0];
907 if (slot
>= btrfs_header_nritems(l
)) {
908 ret
= btrfs_next_leaf(root
, path
);
916 btrfs_item_key_to_cpu(l
, &key
, slot
);
918 if (key
.objectid
< device
->devid
)
921 if (key
.objectid
> device
->devid
)
924 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
927 if (key
.offset
> search_start
) {
928 hole_size
= key
.offset
- search_start
;
930 if (hole_size
> max_hole_size
) {
931 max_hole_start
= search_start
;
932 max_hole_size
= hole_size
;
936 * If this free space is greater than which we need,
937 * it must be the max free space that we have found
938 * until now, so max_hole_start must point to the start
939 * of this free space and the length of this free space
940 * is stored in max_hole_size. Thus, we return
941 * max_hole_start and max_hole_size and go back to the
944 if (hole_size
>= num_bytes
) {
950 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
951 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
953 if (extent_end
> search_start
)
954 search_start
= extent_end
;
961 * At this point, search_start should be the end of
962 * allocated dev extents, and when shrinking the device,
963 * search_end may be smaller than search_start.
965 if (search_end
> search_start
)
966 hole_size
= search_end
- search_start
;
968 if (hole_size
> max_hole_size
) {
969 max_hole_start
= search_start
;
970 max_hole_size
= hole_size
;
974 if (hole_size
< num_bytes
)
980 btrfs_free_path(path
);
982 *start
= max_hole_start
;
984 *len
= max_hole_size
;
988 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
989 struct btrfs_device
*device
,
993 struct btrfs_path
*path
;
994 struct btrfs_root
*root
= device
->dev_root
;
995 struct btrfs_key key
;
996 struct btrfs_key found_key
;
997 struct extent_buffer
*leaf
= NULL
;
998 struct btrfs_dev_extent
*extent
= NULL
;
1000 path
= btrfs_alloc_path();
1004 key
.objectid
= device
->devid
;
1006 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1008 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1010 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1011 BTRFS_DEV_EXTENT_KEY
);
1014 leaf
= path
->nodes
[0];
1015 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1016 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1017 struct btrfs_dev_extent
);
1018 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1019 btrfs_dev_extent_length(leaf
, extent
) < start
);
1021 btrfs_release_path(path
);
1023 } else if (ret
== 0) {
1024 leaf
= path
->nodes
[0];
1025 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1026 struct btrfs_dev_extent
);
1030 if (device
->bytes_used
> 0) {
1031 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1032 device
->bytes_used
-= len
;
1033 spin_lock(&root
->fs_info
->free_chunk_lock
);
1034 root
->fs_info
->free_chunk_space
+= len
;
1035 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1037 ret
= btrfs_del_item(trans
, root
, path
);
1040 btrfs_free_path(path
);
1044 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1045 struct btrfs_device
*device
,
1046 u64 chunk_tree
, u64 chunk_objectid
,
1047 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1050 struct btrfs_path
*path
;
1051 struct btrfs_root
*root
= device
->dev_root
;
1052 struct btrfs_dev_extent
*extent
;
1053 struct extent_buffer
*leaf
;
1054 struct btrfs_key key
;
1056 WARN_ON(!device
->in_fs_metadata
);
1057 path
= btrfs_alloc_path();
1061 key
.objectid
= device
->devid
;
1063 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1064 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1068 leaf
= path
->nodes
[0];
1069 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1070 struct btrfs_dev_extent
);
1071 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1072 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1073 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1075 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1076 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1079 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1080 btrfs_mark_buffer_dirty(leaf
);
1081 btrfs_free_path(path
);
1085 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1086 u64 objectid
, u64
*offset
)
1088 struct btrfs_path
*path
;
1090 struct btrfs_key key
;
1091 struct btrfs_chunk
*chunk
;
1092 struct btrfs_key found_key
;
1094 path
= btrfs_alloc_path();
1098 key
.objectid
= objectid
;
1099 key
.offset
= (u64
)-1;
1100 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1102 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1108 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1112 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1114 if (found_key
.objectid
!= objectid
)
1117 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1118 struct btrfs_chunk
);
1119 *offset
= found_key
.offset
+
1120 btrfs_chunk_length(path
->nodes
[0], chunk
);
1125 btrfs_free_path(path
);
1129 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1132 struct btrfs_key key
;
1133 struct btrfs_key found_key
;
1134 struct btrfs_path
*path
;
1136 root
= root
->fs_info
->chunk_root
;
1138 path
= btrfs_alloc_path();
1142 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1143 key
.type
= BTRFS_DEV_ITEM_KEY
;
1144 key
.offset
= (u64
)-1;
1146 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1152 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1153 BTRFS_DEV_ITEM_KEY
);
1157 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1159 *objectid
= found_key
.offset
+ 1;
1163 btrfs_free_path(path
);
1168 * the device information is stored in the chunk root
1169 * the btrfs_device struct should be fully filled in
1171 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1172 struct btrfs_root
*root
,
1173 struct btrfs_device
*device
)
1176 struct btrfs_path
*path
;
1177 struct btrfs_dev_item
*dev_item
;
1178 struct extent_buffer
*leaf
;
1179 struct btrfs_key key
;
1182 root
= root
->fs_info
->chunk_root
;
1184 path
= btrfs_alloc_path();
1188 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1189 key
.type
= BTRFS_DEV_ITEM_KEY
;
1190 key
.offset
= device
->devid
;
1192 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1197 leaf
= path
->nodes
[0];
1198 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1200 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1201 btrfs_set_device_generation(leaf
, dev_item
, 0);
1202 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1203 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1204 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1205 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1206 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1207 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1208 btrfs_set_device_group(leaf
, dev_item
, 0);
1209 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1210 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1211 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1213 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1214 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1215 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1216 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1217 btrfs_mark_buffer_dirty(leaf
);
1221 btrfs_free_path(path
);
1225 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1226 struct btrfs_device
*device
)
1229 struct btrfs_path
*path
;
1230 struct btrfs_key key
;
1231 struct btrfs_trans_handle
*trans
;
1233 root
= root
->fs_info
->chunk_root
;
1235 path
= btrfs_alloc_path();
1239 trans
= btrfs_start_transaction(root
, 0);
1240 if (IS_ERR(trans
)) {
1241 btrfs_free_path(path
);
1242 return PTR_ERR(trans
);
1244 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1245 key
.type
= BTRFS_DEV_ITEM_KEY
;
1246 key
.offset
= device
->devid
;
1249 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1258 ret
= btrfs_del_item(trans
, root
, path
);
1262 btrfs_free_path(path
);
1263 unlock_chunks(root
);
1264 btrfs_commit_transaction(trans
, root
);
1268 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1270 struct btrfs_device
*device
;
1271 struct btrfs_device
*next_device
;
1272 struct block_device
*bdev
;
1273 struct buffer_head
*bh
= NULL
;
1274 struct btrfs_super_block
*disk_super
;
1275 struct btrfs_fs_devices
*cur_devices
;
1281 bool clear_super
= false;
1283 mutex_lock(&uuid_mutex
);
1285 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1286 root
->fs_info
->avail_system_alloc_bits
|
1287 root
->fs_info
->avail_metadata_alloc_bits
;
1289 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1290 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1291 printk(KERN_ERR
"btrfs: unable to go below four devices "
1297 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1298 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1299 printk(KERN_ERR
"btrfs: unable to go below two "
1300 "devices on raid1\n");
1305 if (strcmp(device_path
, "missing") == 0) {
1306 struct list_head
*devices
;
1307 struct btrfs_device
*tmp
;
1310 devices
= &root
->fs_info
->fs_devices
->devices
;
1312 * It is safe to read the devices since the volume_mutex
1315 list_for_each_entry(tmp
, devices
, dev_list
) {
1316 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1325 printk(KERN_ERR
"btrfs: no missing devices found to "
1330 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1331 root
->fs_info
->bdev_holder
);
1333 ret
= PTR_ERR(bdev
);
1337 set_blocksize(bdev
, 4096);
1338 bh
= btrfs_read_dev_super(bdev
);
1343 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1344 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1345 dev_uuid
= disk_super
->dev_item
.uuid
;
1346 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1354 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1355 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1361 if (device
->writeable
) {
1363 list_del_init(&device
->dev_alloc_list
);
1364 unlock_chunks(root
);
1365 root
->fs_info
->fs_devices
->rw_devices
--;
1369 ret
= btrfs_shrink_device(device
, 0);
1373 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1377 spin_lock(&root
->fs_info
->free_chunk_lock
);
1378 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1380 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1382 device
->in_fs_metadata
= 0;
1383 btrfs_scrub_cancel_dev(root
, device
);
1386 * the device list mutex makes sure that we don't change
1387 * the device list while someone else is writing out all
1388 * the device supers.
1391 cur_devices
= device
->fs_devices
;
1392 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1393 list_del_rcu(&device
->dev_list
);
1395 device
->fs_devices
->num_devices
--;
1397 if (device
->missing
)
1398 root
->fs_info
->fs_devices
->missing_devices
--;
1400 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1401 struct btrfs_device
, dev_list
);
1402 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1403 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1404 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1405 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1408 device
->fs_devices
->open_devices
--;
1410 call_rcu(&device
->rcu
, free_device
);
1411 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1413 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1414 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1416 if (cur_devices
->open_devices
== 0) {
1417 struct btrfs_fs_devices
*fs_devices
;
1418 fs_devices
= root
->fs_info
->fs_devices
;
1419 while (fs_devices
) {
1420 if (fs_devices
->seed
== cur_devices
)
1422 fs_devices
= fs_devices
->seed
;
1424 fs_devices
->seed
= cur_devices
->seed
;
1425 cur_devices
->seed
= NULL
;
1427 __btrfs_close_devices(cur_devices
);
1428 unlock_chunks(root
);
1429 free_fs_devices(cur_devices
);
1433 * at this point, the device is zero sized. We want to
1434 * remove it from the devices list and zero out the old super
1437 /* make sure this device isn't detected as part of
1440 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1441 set_buffer_dirty(bh
);
1442 sync_dirty_buffer(bh
);
1451 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1453 mutex_unlock(&uuid_mutex
);
1456 if (device
->writeable
) {
1458 list_add(&device
->dev_alloc_list
,
1459 &root
->fs_info
->fs_devices
->alloc_list
);
1460 unlock_chunks(root
);
1461 root
->fs_info
->fs_devices
->rw_devices
++;
1467 * does all the dirty work required for changing file system's UUID.
1469 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1471 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1472 struct btrfs_fs_devices
*old_devices
;
1473 struct btrfs_fs_devices
*seed_devices
;
1474 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1475 struct btrfs_device
*device
;
1478 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1479 if (!fs_devices
->seeding
)
1482 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1486 old_devices
= clone_fs_devices(fs_devices
);
1487 if (IS_ERR(old_devices
)) {
1488 kfree(seed_devices
);
1489 return PTR_ERR(old_devices
);
1492 list_add(&old_devices
->list
, &fs_uuids
);
1494 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1495 seed_devices
->opened
= 1;
1496 INIT_LIST_HEAD(&seed_devices
->devices
);
1497 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1498 mutex_init(&seed_devices
->device_list_mutex
);
1500 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1501 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1503 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1505 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1506 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1507 device
->fs_devices
= seed_devices
;
1510 fs_devices
->seeding
= 0;
1511 fs_devices
->num_devices
= 0;
1512 fs_devices
->open_devices
= 0;
1513 fs_devices
->seed
= seed_devices
;
1515 generate_random_uuid(fs_devices
->fsid
);
1516 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1517 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1518 super_flags
= btrfs_super_flags(disk_super
) &
1519 ~BTRFS_SUPER_FLAG_SEEDING
;
1520 btrfs_set_super_flags(disk_super
, super_flags
);
1526 * strore the expected generation for seed devices in device items.
1528 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1529 struct btrfs_root
*root
)
1531 struct btrfs_path
*path
;
1532 struct extent_buffer
*leaf
;
1533 struct btrfs_dev_item
*dev_item
;
1534 struct btrfs_device
*device
;
1535 struct btrfs_key key
;
1536 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1537 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1541 path
= btrfs_alloc_path();
1545 root
= root
->fs_info
->chunk_root
;
1546 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1548 key
.type
= BTRFS_DEV_ITEM_KEY
;
1551 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1555 leaf
= path
->nodes
[0];
1557 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1558 ret
= btrfs_next_leaf(root
, path
);
1563 leaf
= path
->nodes
[0];
1564 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1565 btrfs_release_path(path
);
1569 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1570 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1571 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1574 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1575 struct btrfs_dev_item
);
1576 devid
= btrfs_device_id(leaf
, dev_item
);
1577 read_extent_buffer(leaf
, dev_uuid
,
1578 (unsigned long)btrfs_device_uuid(dev_item
),
1580 read_extent_buffer(leaf
, fs_uuid
,
1581 (unsigned long)btrfs_device_fsid(dev_item
),
1583 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1586 if (device
->fs_devices
->seeding
) {
1587 btrfs_set_device_generation(leaf
, dev_item
,
1588 device
->generation
);
1589 btrfs_mark_buffer_dirty(leaf
);
1597 btrfs_free_path(path
);
1601 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1603 struct request_queue
*q
;
1604 struct btrfs_trans_handle
*trans
;
1605 struct btrfs_device
*device
;
1606 struct block_device
*bdev
;
1607 struct list_head
*devices
;
1608 struct super_block
*sb
= root
->fs_info
->sb
;
1610 int seeding_dev
= 0;
1613 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1616 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1617 root
->fs_info
->bdev_holder
);
1619 return PTR_ERR(bdev
);
1621 if (root
->fs_info
->fs_devices
->seeding
) {
1623 down_write(&sb
->s_umount
);
1624 mutex_lock(&uuid_mutex
);
1627 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1629 devices
= &root
->fs_info
->fs_devices
->devices
;
1631 * we have the volume lock, so we don't need the extra
1632 * device list mutex while reading the list here.
1634 list_for_each_entry(device
, devices
, dev_list
) {
1635 if (device
->bdev
== bdev
) {
1641 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1643 /* we can safely leave the fs_devices entry around */
1648 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1649 if (!device
->name
) {
1655 ret
= find_next_devid(root
, &device
->devid
);
1657 kfree(device
->name
);
1662 trans
= btrfs_start_transaction(root
, 0);
1663 if (IS_ERR(trans
)) {
1664 kfree(device
->name
);
1666 ret
= PTR_ERR(trans
);
1672 q
= bdev_get_queue(bdev
);
1673 if (blk_queue_discard(q
))
1674 device
->can_discard
= 1;
1675 device
->writeable
= 1;
1676 device
->work
.func
= pending_bios_fn
;
1677 generate_random_uuid(device
->uuid
);
1678 spin_lock_init(&device
->io_lock
);
1679 device
->generation
= trans
->transid
;
1680 device
->io_width
= root
->sectorsize
;
1681 device
->io_align
= root
->sectorsize
;
1682 device
->sector_size
= root
->sectorsize
;
1683 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1684 device
->disk_total_bytes
= device
->total_bytes
;
1685 device
->dev_root
= root
->fs_info
->dev_root
;
1686 device
->bdev
= bdev
;
1687 device
->in_fs_metadata
= 1;
1688 device
->mode
= FMODE_EXCL
;
1689 set_blocksize(device
->bdev
, 4096);
1692 sb
->s_flags
&= ~MS_RDONLY
;
1693 ret
= btrfs_prepare_sprout(root
);
1697 device
->fs_devices
= root
->fs_info
->fs_devices
;
1700 * we don't want write_supers to jump in here with our device
1703 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1704 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1705 list_add(&device
->dev_alloc_list
,
1706 &root
->fs_info
->fs_devices
->alloc_list
);
1707 root
->fs_info
->fs_devices
->num_devices
++;
1708 root
->fs_info
->fs_devices
->open_devices
++;
1709 root
->fs_info
->fs_devices
->rw_devices
++;
1710 if (device
->can_discard
)
1711 root
->fs_info
->fs_devices
->num_can_discard
++;
1712 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1714 spin_lock(&root
->fs_info
->free_chunk_lock
);
1715 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1716 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1718 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1719 root
->fs_info
->fs_devices
->rotating
= 1;
1721 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1722 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1723 total_bytes
+ device
->total_bytes
);
1725 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1726 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1728 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1731 ret
= init_first_rw_device(trans
, root
, device
);
1733 ret
= btrfs_finish_sprout(trans
, root
);
1736 ret
= btrfs_add_device(trans
, root
, device
);
1740 * we've got more storage, clear any full flags on the space
1743 btrfs_clear_space_info_full(root
->fs_info
);
1745 unlock_chunks(root
);
1746 btrfs_commit_transaction(trans
, root
);
1749 mutex_unlock(&uuid_mutex
);
1750 up_write(&sb
->s_umount
);
1752 ret
= btrfs_relocate_sys_chunks(root
);
1758 blkdev_put(bdev
, FMODE_EXCL
);
1760 mutex_unlock(&uuid_mutex
);
1761 up_write(&sb
->s_umount
);
1766 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1767 struct btrfs_device
*device
)
1770 struct btrfs_path
*path
;
1771 struct btrfs_root
*root
;
1772 struct btrfs_dev_item
*dev_item
;
1773 struct extent_buffer
*leaf
;
1774 struct btrfs_key key
;
1776 root
= device
->dev_root
->fs_info
->chunk_root
;
1778 path
= btrfs_alloc_path();
1782 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1783 key
.type
= BTRFS_DEV_ITEM_KEY
;
1784 key
.offset
= device
->devid
;
1786 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1795 leaf
= path
->nodes
[0];
1796 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1798 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1799 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1800 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1801 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1802 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1803 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1804 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1805 btrfs_mark_buffer_dirty(leaf
);
1808 btrfs_free_path(path
);
1812 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1813 struct btrfs_device
*device
, u64 new_size
)
1815 struct btrfs_super_block
*super_copy
=
1816 device
->dev_root
->fs_info
->super_copy
;
1817 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1818 u64 diff
= new_size
- device
->total_bytes
;
1820 if (!device
->writeable
)
1822 if (new_size
<= device
->total_bytes
)
1825 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1826 device
->fs_devices
->total_rw_bytes
+= diff
;
1828 device
->total_bytes
= new_size
;
1829 device
->disk_total_bytes
= new_size
;
1830 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1832 return btrfs_update_device(trans
, device
);
1835 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1836 struct btrfs_device
*device
, u64 new_size
)
1839 lock_chunks(device
->dev_root
);
1840 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1841 unlock_chunks(device
->dev_root
);
1845 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1846 struct btrfs_root
*root
,
1847 u64 chunk_tree
, u64 chunk_objectid
,
1851 struct btrfs_path
*path
;
1852 struct btrfs_key key
;
1854 root
= root
->fs_info
->chunk_root
;
1855 path
= btrfs_alloc_path();
1859 key
.objectid
= chunk_objectid
;
1860 key
.offset
= chunk_offset
;
1861 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1863 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1866 ret
= btrfs_del_item(trans
, root
, path
);
1868 btrfs_free_path(path
);
1872 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1875 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1876 struct btrfs_disk_key
*disk_key
;
1877 struct btrfs_chunk
*chunk
;
1884 struct btrfs_key key
;
1886 array_size
= btrfs_super_sys_array_size(super_copy
);
1888 ptr
= super_copy
->sys_chunk_array
;
1891 while (cur
< array_size
) {
1892 disk_key
= (struct btrfs_disk_key
*)ptr
;
1893 btrfs_disk_key_to_cpu(&key
, disk_key
);
1895 len
= sizeof(*disk_key
);
1897 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1898 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1899 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1900 len
+= btrfs_chunk_item_size(num_stripes
);
1905 if (key
.objectid
== chunk_objectid
&&
1906 key
.offset
== chunk_offset
) {
1907 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1909 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1918 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1919 u64 chunk_tree
, u64 chunk_objectid
,
1922 struct extent_map_tree
*em_tree
;
1923 struct btrfs_root
*extent_root
;
1924 struct btrfs_trans_handle
*trans
;
1925 struct extent_map
*em
;
1926 struct map_lookup
*map
;
1930 root
= root
->fs_info
->chunk_root
;
1931 extent_root
= root
->fs_info
->extent_root
;
1932 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1934 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1938 /* step one, relocate all the extents inside this chunk */
1939 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1943 trans
= btrfs_start_transaction(root
, 0);
1944 BUG_ON(IS_ERR(trans
));
1949 * step two, delete the device extents and the
1950 * chunk tree entries
1952 read_lock(&em_tree
->lock
);
1953 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1954 read_unlock(&em_tree
->lock
);
1956 BUG_ON(em
->start
> chunk_offset
||
1957 em
->start
+ em
->len
< chunk_offset
);
1958 map
= (struct map_lookup
*)em
->bdev
;
1960 for (i
= 0; i
< map
->num_stripes
; i
++) {
1961 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1962 map
->stripes
[i
].physical
);
1965 if (map
->stripes
[i
].dev
) {
1966 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1970 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1975 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1977 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1978 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1982 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1985 write_lock(&em_tree
->lock
);
1986 remove_extent_mapping(em_tree
, em
);
1987 write_unlock(&em_tree
->lock
);
1992 /* once for the tree */
1993 free_extent_map(em
);
1995 free_extent_map(em
);
1997 unlock_chunks(root
);
1998 btrfs_end_transaction(trans
, root
);
2002 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2004 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2005 struct btrfs_path
*path
;
2006 struct extent_buffer
*leaf
;
2007 struct btrfs_chunk
*chunk
;
2008 struct btrfs_key key
;
2009 struct btrfs_key found_key
;
2010 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2012 bool retried
= false;
2016 path
= btrfs_alloc_path();
2021 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2022 key
.offset
= (u64
)-1;
2023 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2026 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2031 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2038 leaf
= path
->nodes
[0];
2039 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2041 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2042 struct btrfs_chunk
);
2043 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2044 btrfs_release_path(path
);
2046 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2047 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2056 if (found_key
.offset
== 0)
2058 key
.offset
= found_key
.offset
- 1;
2061 if (failed
&& !retried
) {
2065 } else if (failed
&& retried
) {
2070 btrfs_free_path(path
);
2074 static int insert_balance_item(struct btrfs_root
*root
,
2075 struct btrfs_balance_control
*bctl
)
2077 struct btrfs_trans_handle
*trans
;
2078 struct btrfs_balance_item
*item
;
2079 struct btrfs_disk_balance_args disk_bargs
;
2080 struct btrfs_path
*path
;
2081 struct extent_buffer
*leaf
;
2082 struct btrfs_key key
;
2085 path
= btrfs_alloc_path();
2089 trans
= btrfs_start_transaction(root
, 0);
2090 if (IS_ERR(trans
)) {
2091 btrfs_free_path(path
);
2092 return PTR_ERR(trans
);
2095 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2096 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2099 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2104 leaf
= path
->nodes
[0];
2105 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2107 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2109 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2110 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2111 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2112 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2113 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2114 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2116 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2118 btrfs_mark_buffer_dirty(leaf
);
2120 btrfs_free_path(path
);
2121 err
= btrfs_commit_transaction(trans
, root
);
2127 static int del_balance_item(struct btrfs_root
*root
)
2129 struct btrfs_trans_handle
*trans
;
2130 struct btrfs_path
*path
;
2131 struct btrfs_key key
;
2134 path
= btrfs_alloc_path();
2138 trans
= btrfs_start_transaction(root
, 0);
2139 if (IS_ERR(trans
)) {
2140 btrfs_free_path(path
);
2141 return PTR_ERR(trans
);
2144 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2145 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2148 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2156 ret
= btrfs_del_item(trans
, root
, path
);
2158 btrfs_free_path(path
);
2159 err
= btrfs_commit_transaction(trans
, root
);
2166 * This is a heuristic used to reduce the number of chunks balanced on
2167 * resume after balance was interrupted.
2169 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2172 * Turn on soft mode for chunk types that were being converted.
2174 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2175 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2176 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2177 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2178 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2179 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2182 * Turn on usage filter if is not already used. The idea is
2183 * that chunks that we have already balanced should be
2184 * reasonably full. Don't do it for chunks that are being
2185 * converted - that will keep us from relocating unconverted
2186 * (albeit full) chunks.
2188 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2189 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2190 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2191 bctl
->data
.usage
= 90;
2193 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2194 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2195 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2196 bctl
->sys
.usage
= 90;
2198 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2199 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2200 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2201 bctl
->meta
.usage
= 90;
2206 * Should be called with both balance and volume mutexes held to
2207 * serialize other volume operations (add_dev/rm_dev/resize) with
2208 * restriper. Same goes for unset_balance_control.
2210 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2212 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2214 BUG_ON(fs_info
->balance_ctl
);
2216 spin_lock(&fs_info
->balance_lock
);
2217 fs_info
->balance_ctl
= bctl
;
2218 spin_unlock(&fs_info
->balance_lock
);
2221 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2223 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2225 BUG_ON(!fs_info
->balance_ctl
);
2227 spin_lock(&fs_info
->balance_lock
);
2228 fs_info
->balance_ctl
= NULL
;
2229 spin_unlock(&fs_info
->balance_lock
);
2235 * Balance filters. Return 1 if chunk should be filtered out
2236 * (should not be balanced).
2238 static int chunk_profiles_filter(u64 chunk_profile
,
2239 struct btrfs_balance_args
*bargs
)
2241 chunk_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
2243 if (chunk_profile
== 0)
2244 chunk_profile
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2246 if (bargs
->profiles
& chunk_profile
)
2252 static u64
div_factor_fine(u64 num
, int factor
)
2264 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2265 struct btrfs_balance_args
*bargs
)
2267 struct btrfs_block_group_cache
*cache
;
2268 u64 chunk_used
, user_thresh
;
2271 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2272 chunk_used
= btrfs_block_group_used(&cache
->item
);
2274 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2275 if (chunk_used
< user_thresh
)
2278 btrfs_put_block_group(cache
);
2282 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2283 struct btrfs_chunk
*chunk
,
2284 struct btrfs_balance_args
*bargs
)
2286 struct btrfs_stripe
*stripe
;
2287 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2290 for (i
= 0; i
< num_stripes
; i
++) {
2291 stripe
= btrfs_stripe_nr(chunk
, i
);
2292 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2299 /* [pstart, pend) */
2300 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2301 struct btrfs_chunk
*chunk
,
2303 struct btrfs_balance_args
*bargs
)
2305 struct btrfs_stripe
*stripe
;
2306 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2312 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2315 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2316 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2320 factor
= num_stripes
/ factor
;
2322 for (i
= 0; i
< num_stripes
; i
++) {
2323 stripe
= btrfs_stripe_nr(chunk
, i
);
2324 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2327 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2328 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2329 do_div(stripe_length
, factor
);
2331 if (stripe_offset
< bargs
->pend
&&
2332 stripe_offset
+ stripe_length
> bargs
->pstart
)
2339 /* [vstart, vend) */
2340 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2341 struct btrfs_chunk
*chunk
,
2343 struct btrfs_balance_args
*bargs
)
2345 if (chunk_offset
< bargs
->vend
&&
2346 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2347 /* at least part of the chunk is inside this vrange */
2353 static int chunk_soft_convert_filter(u64 chunk_profile
,
2354 struct btrfs_balance_args
*bargs
)
2356 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2359 chunk_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
2361 if (chunk_profile
== 0)
2362 chunk_profile
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2364 if (bargs
->target
& chunk_profile
)
2370 static int should_balance_chunk(struct btrfs_root
*root
,
2371 struct extent_buffer
*leaf
,
2372 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2374 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2375 struct btrfs_balance_args
*bargs
= NULL
;
2376 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2379 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2380 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2384 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2385 bargs
= &bctl
->data
;
2386 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2388 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2389 bargs
= &bctl
->meta
;
2391 /* profiles filter */
2392 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2393 chunk_profiles_filter(chunk_type
, bargs
)) {
2398 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2399 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2404 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2405 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2409 /* drange filter, makes sense only with devid filter */
2410 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2411 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2416 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2417 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2421 /* soft profile changing mode */
2422 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2423 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2430 static u64
div_factor(u64 num
, int factor
)
2439 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2441 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2442 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2443 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2444 struct list_head
*devices
;
2445 struct btrfs_device
*device
;
2448 struct btrfs_chunk
*chunk
;
2449 struct btrfs_path
*path
;
2450 struct btrfs_key key
;
2451 struct btrfs_key found_key
;
2452 struct btrfs_trans_handle
*trans
;
2453 struct extent_buffer
*leaf
;
2456 int enospc_errors
= 0;
2457 bool counting
= true;
2459 /* step one make some room on all the devices */
2460 devices
= &fs_info
->fs_devices
->devices
;
2461 list_for_each_entry(device
, devices
, dev_list
) {
2462 old_size
= device
->total_bytes
;
2463 size_to_free
= div_factor(old_size
, 1);
2464 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2465 if (!device
->writeable
||
2466 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2469 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2474 trans
= btrfs_start_transaction(dev_root
, 0);
2475 BUG_ON(IS_ERR(trans
));
2477 ret
= btrfs_grow_device(trans
, device
, old_size
);
2480 btrfs_end_transaction(trans
, dev_root
);
2483 /* step two, relocate all the chunks */
2484 path
= btrfs_alloc_path();
2490 /* zero out stat counters */
2491 spin_lock(&fs_info
->balance_lock
);
2492 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2493 spin_unlock(&fs_info
->balance_lock
);
2495 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2496 key
.offset
= (u64
)-1;
2497 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2500 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2501 atomic_read(&fs_info
->balance_cancel_req
)) {
2506 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2511 * this shouldn't happen, it means the last relocate
2515 BUG(); /* FIXME break ? */
2517 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2518 BTRFS_CHUNK_ITEM_KEY
);
2524 leaf
= path
->nodes
[0];
2525 slot
= path
->slots
[0];
2526 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2528 if (found_key
.objectid
!= key
.objectid
)
2531 /* chunk zero is special */
2532 if (found_key
.offset
== 0)
2535 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2538 spin_lock(&fs_info
->balance_lock
);
2539 bctl
->stat
.considered
++;
2540 spin_unlock(&fs_info
->balance_lock
);
2543 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2545 btrfs_release_path(path
);
2550 spin_lock(&fs_info
->balance_lock
);
2551 bctl
->stat
.expected
++;
2552 spin_unlock(&fs_info
->balance_lock
);
2556 ret
= btrfs_relocate_chunk(chunk_root
,
2557 chunk_root
->root_key
.objectid
,
2560 if (ret
&& ret
!= -ENOSPC
)
2562 if (ret
== -ENOSPC
) {
2565 spin_lock(&fs_info
->balance_lock
);
2566 bctl
->stat
.completed
++;
2567 spin_unlock(&fs_info
->balance_lock
);
2570 key
.offset
= found_key
.offset
- 1;
2574 btrfs_release_path(path
);
2579 btrfs_free_path(path
);
2580 if (enospc_errors
) {
2581 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2590 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2592 /* cancel requested || normal exit path */
2593 return atomic_read(&fs_info
->balance_cancel_req
) ||
2594 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2595 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2598 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2602 unset_balance_control(fs_info
);
2603 ret
= del_balance_item(fs_info
->tree_root
);
2607 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2608 struct btrfs_ioctl_balance_args
*bargs
);
2611 * Should be called with both balance and volume mutexes held
2613 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2614 struct btrfs_ioctl_balance_args
*bargs
)
2616 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2620 if (btrfs_fs_closing(fs_info
) ||
2621 atomic_read(&fs_info
->balance_pause_req
) ||
2622 atomic_read(&fs_info
->balance_cancel_req
)) {
2628 * In case of mixed groups both data and meta should be picked,
2629 * and identical options should be given for both of them.
2631 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2632 if ((allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
) &&
2633 (bctl
->flags
& (BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
))) {
2634 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2635 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2636 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2637 printk(KERN_ERR
"btrfs: with mixed groups data and "
2638 "metadata balance options must be the same\n");
2645 * Profile changing sanity checks. Skip them if a simple
2646 * balance is requested.
2648 if (!((bctl
->data
.flags
| bctl
->sys
.flags
| bctl
->meta
.flags
) &
2649 BTRFS_BALANCE_ARGS_CONVERT
))
2652 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2653 if (fs_info
->fs_devices
->num_devices
== 1)
2654 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2655 else if (fs_info
->fs_devices
->num_devices
< 4)
2656 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
2658 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2659 BTRFS_BLOCK_GROUP_RAID10
);
2661 if (!profile_is_valid(bctl
->data
.target
, 1) ||
2662 bctl
->data
.target
& ~allowed
) {
2663 printk(KERN_ERR
"btrfs: unable to start balance with target "
2664 "data profile %llu\n",
2665 (unsigned long long)bctl
->data
.target
);
2669 if (!profile_is_valid(bctl
->meta
.target
, 1) ||
2670 bctl
->meta
.target
& ~allowed
) {
2671 printk(KERN_ERR
"btrfs: unable to start balance with target "
2672 "metadata profile %llu\n",
2673 (unsigned long long)bctl
->meta
.target
);
2677 if (!profile_is_valid(bctl
->sys
.target
, 1) ||
2678 bctl
->sys
.target
& ~allowed
) {
2679 printk(KERN_ERR
"btrfs: unable to start balance with target "
2680 "system profile %llu\n",
2681 (unsigned long long)bctl
->sys
.target
);
2686 if (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
) {
2687 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
2692 /* allow to reduce meta or sys integrity only if force set */
2693 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2694 BTRFS_BLOCK_GROUP_RAID10
;
2695 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2696 (fs_info
->avail_system_alloc_bits
& allowed
) &&
2697 !(bctl
->sys
.target
& allowed
)) ||
2698 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2699 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
2700 !(bctl
->meta
.target
& allowed
))) {
2701 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
2702 printk(KERN_INFO
"btrfs: force reducing metadata "
2705 printk(KERN_ERR
"btrfs: balance will reduce metadata "
2706 "integrity, use force if you want this\n");
2713 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
2714 if (ret
&& ret
!= -EEXIST
)
2717 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
2718 BUG_ON(ret
== -EEXIST
);
2719 set_balance_control(bctl
);
2721 BUG_ON(ret
!= -EEXIST
);
2722 spin_lock(&fs_info
->balance_lock
);
2723 update_balance_args(bctl
);
2724 spin_unlock(&fs_info
->balance_lock
);
2727 atomic_inc(&fs_info
->balance_running
);
2728 mutex_unlock(&fs_info
->balance_mutex
);
2730 ret
= __btrfs_balance(fs_info
);
2732 mutex_lock(&fs_info
->balance_mutex
);
2733 atomic_dec(&fs_info
->balance_running
);
2736 memset(bargs
, 0, sizeof(*bargs
));
2737 update_ioctl_balance_args(fs_info
, 0, bargs
);
2740 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
2741 balance_need_close(fs_info
)) {
2742 __cancel_balance(fs_info
);
2745 wake_up(&fs_info
->balance_wait_q
);
2749 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
2750 __cancel_balance(fs_info
);
2756 static int balance_kthread(void *data
)
2758 struct btrfs_balance_control
*bctl
=
2759 (struct btrfs_balance_control
*)data
;
2760 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2763 mutex_lock(&fs_info
->volume_mutex
);
2764 mutex_lock(&fs_info
->balance_mutex
);
2766 set_balance_control(bctl
);
2768 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
2769 printk(KERN_INFO
"btrfs: force skipping balance\n");
2771 printk(KERN_INFO
"btrfs: continuing balance\n");
2772 ret
= btrfs_balance(bctl
, NULL
);
2775 mutex_unlock(&fs_info
->balance_mutex
);
2776 mutex_unlock(&fs_info
->volume_mutex
);
2780 int btrfs_recover_balance(struct btrfs_root
*tree_root
)
2782 struct task_struct
*tsk
;
2783 struct btrfs_balance_control
*bctl
;
2784 struct btrfs_balance_item
*item
;
2785 struct btrfs_disk_balance_args disk_bargs
;
2786 struct btrfs_path
*path
;
2787 struct extent_buffer
*leaf
;
2788 struct btrfs_key key
;
2791 path
= btrfs_alloc_path();
2795 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
2801 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2802 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2805 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, path
, 0, 0);
2808 if (ret
> 0) { /* ret = -ENOENT; */
2813 leaf
= path
->nodes
[0];
2814 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2816 bctl
->fs_info
= tree_root
->fs_info
;
2817 bctl
->flags
= btrfs_balance_flags(leaf
, item
) | BTRFS_BALANCE_RESUME
;
2819 btrfs_balance_data(leaf
, item
, &disk_bargs
);
2820 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
2821 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
2822 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
2823 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
2824 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
2826 tsk
= kthread_run(balance_kthread
, bctl
, "btrfs-balance");
2835 btrfs_free_path(path
);
2839 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
2843 mutex_lock(&fs_info
->balance_mutex
);
2844 if (!fs_info
->balance_ctl
) {
2845 mutex_unlock(&fs_info
->balance_mutex
);
2849 if (atomic_read(&fs_info
->balance_running
)) {
2850 atomic_inc(&fs_info
->balance_pause_req
);
2851 mutex_unlock(&fs_info
->balance_mutex
);
2853 wait_event(fs_info
->balance_wait_q
,
2854 atomic_read(&fs_info
->balance_running
) == 0);
2856 mutex_lock(&fs_info
->balance_mutex
);
2857 /* we are good with balance_ctl ripped off from under us */
2858 BUG_ON(atomic_read(&fs_info
->balance_running
));
2859 atomic_dec(&fs_info
->balance_pause_req
);
2864 mutex_unlock(&fs_info
->balance_mutex
);
2868 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
2870 mutex_lock(&fs_info
->balance_mutex
);
2871 if (!fs_info
->balance_ctl
) {
2872 mutex_unlock(&fs_info
->balance_mutex
);
2876 atomic_inc(&fs_info
->balance_cancel_req
);
2878 * if we are running just wait and return, balance item is
2879 * deleted in btrfs_balance in this case
2881 if (atomic_read(&fs_info
->balance_running
)) {
2882 mutex_unlock(&fs_info
->balance_mutex
);
2883 wait_event(fs_info
->balance_wait_q
,
2884 atomic_read(&fs_info
->balance_running
) == 0);
2885 mutex_lock(&fs_info
->balance_mutex
);
2887 /* __cancel_balance needs volume_mutex */
2888 mutex_unlock(&fs_info
->balance_mutex
);
2889 mutex_lock(&fs_info
->volume_mutex
);
2890 mutex_lock(&fs_info
->balance_mutex
);
2892 if (fs_info
->balance_ctl
)
2893 __cancel_balance(fs_info
);
2895 mutex_unlock(&fs_info
->volume_mutex
);
2898 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
2899 atomic_dec(&fs_info
->balance_cancel_req
);
2900 mutex_unlock(&fs_info
->balance_mutex
);
2905 * shrinking a device means finding all of the device extents past
2906 * the new size, and then following the back refs to the chunks.
2907 * The chunk relocation code actually frees the device extent
2909 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2911 struct btrfs_trans_handle
*trans
;
2912 struct btrfs_root
*root
= device
->dev_root
;
2913 struct btrfs_dev_extent
*dev_extent
= NULL
;
2914 struct btrfs_path
*path
;
2922 bool retried
= false;
2923 struct extent_buffer
*l
;
2924 struct btrfs_key key
;
2925 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2926 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2927 u64 old_size
= device
->total_bytes
;
2928 u64 diff
= device
->total_bytes
- new_size
;
2930 if (new_size
>= device
->total_bytes
)
2933 path
= btrfs_alloc_path();
2941 device
->total_bytes
= new_size
;
2942 if (device
->writeable
) {
2943 device
->fs_devices
->total_rw_bytes
-= diff
;
2944 spin_lock(&root
->fs_info
->free_chunk_lock
);
2945 root
->fs_info
->free_chunk_space
-= diff
;
2946 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2948 unlock_chunks(root
);
2951 key
.objectid
= device
->devid
;
2952 key
.offset
= (u64
)-1;
2953 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2956 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2960 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2965 btrfs_release_path(path
);
2970 slot
= path
->slots
[0];
2971 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2973 if (key
.objectid
!= device
->devid
) {
2974 btrfs_release_path(path
);
2978 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2979 length
= btrfs_dev_extent_length(l
, dev_extent
);
2981 if (key
.offset
+ length
<= new_size
) {
2982 btrfs_release_path(path
);
2986 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2987 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2988 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2989 btrfs_release_path(path
);
2991 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2993 if (ret
&& ret
!= -ENOSPC
)
3000 if (failed
&& !retried
) {
3004 } else if (failed
&& retried
) {
3008 device
->total_bytes
= old_size
;
3009 if (device
->writeable
)
3010 device
->fs_devices
->total_rw_bytes
+= diff
;
3011 spin_lock(&root
->fs_info
->free_chunk_lock
);
3012 root
->fs_info
->free_chunk_space
+= diff
;
3013 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3014 unlock_chunks(root
);
3018 /* Shrinking succeeded, else we would be at "done". */
3019 trans
= btrfs_start_transaction(root
, 0);
3020 if (IS_ERR(trans
)) {
3021 ret
= PTR_ERR(trans
);
3027 device
->disk_total_bytes
= new_size
;
3028 /* Now btrfs_update_device() will change the on-disk size. */
3029 ret
= btrfs_update_device(trans
, device
);
3031 unlock_chunks(root
);
3032 btrfs_end_transaction(trans
, root
);
3035 WARN_ON(diff
> old_total
);
3036 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3037 unlock_chunks(root
);
3038 btrfs_end_transaction(trans
, root
);
3040 btrfs_free_path(path
);
3044 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3045 struct btrfs_key
*key
,
3046 struct btrfs_chunk
*chunk
, int item_size
)
3048 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3049 struct btrfs_disk_key disk_key
;
3053 array_size
= btrfs_super_sys_array_size(super_copy
);
3054 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3057 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3058 btrfs_cpu_key_to_disk(&disk_key
, key
);
3059 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3060 ptr
+= sizeof(disk_key
);
3061 memcpy(ptr
, chunk
, item_size
);
3062 item_size
+= sizeof(disk_key
);
3063 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3068 * sort the devices in descending order by max_avail, total_avail
3070 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3072 const struct btrfs_device_info
*di_a
= a
;
3073 const struct btrfs_device_info
*di_b
= b
;
3075 if (di_a
->max_avail
> di_b
->max_avail
)
3077 if (di_a
->max_avail
< di_b
->max_avail
)
3079 if (di_a
->total_avail
> di_b
->total_avail
)
3081 if (di_a
->total_avail
< di_b
->total_avail
)
3086 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3087 struct btrfs_root
*extent_root
,
3088 struct map_lookup
**map_ret
,
3089 u64
*num_bytes_out
, u64
*stripe_size_out
,
3090 u64 start
, u64 type
)
3092 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3093 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3094 struct list_head
*cur
;
3095 struct map_lookup
*map
= NULL
;
3096 struct extent_map_tree
*em_tree
;
3097 struct extent_map
*em
;
3098 struct btrfs_device_info
*devices_info
= NULL
;
3100 int num_stripes
; /* total number of stripes to allocate */
3101 int sub_stripes
; /* sub_stripes info for map */
3102 int dev_stripes
; /* stripes per dev */
3103 int devs_max
; /* max devs to use */
3104 int devs_min
; /* min devs needed */
3105 int devs_increment
; /* ndevs has to be a multiple of this */
3106 int ncopies
; /* how many copies to data has */
3108 u64 max_stripe_size
;
3116 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
3117 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
3119 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
3122 if (list_empty(&fs_devices
->alloc_list
))
3129 devs_max
= 0; /* 0 == as many as possible */
3133 * define the properties of each RAID type.
3134 * FIXME: move this to a global table and use it in all RAID
3137 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3141 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3143 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3148 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3157 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3158 max_stripe_size
= 1024 * 1024 * 1024;
3159 max_chunk_size
= 10 * max_stripe_size
;
3160 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3161 /* for larger filesystems, use larger metadata chunks */
3162 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3163 max_stripe_size
= 1024 * 1024 * 1024;
3165 max_stripe_size
= 256 * 1024 * 1024;
3166 max_chunk_size
= max_stripe_size
;
3167 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3168 max_stripe_size
= 32 * 1024 * 1024;
3169 max_chunk_size
= 2 * max_stripe_size
;
3171 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3176 /* we don't want a chunk larger than 10% of writeable space */
3177 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3180 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3185 cur
= fs_devices
->alloc_list
.next
;
3188 * in the first pass through the devices list, we gather information
3189 * about the available holes on each device.
3192 while (cur
!= &fs_devices
->alloc_list
) {
3193 struct btrfs_device
*device
;
3197 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3201 if (!device
->writeable
) {
3203 "btrfs: read-only device in alloc_list\n");
3208 if (!device
->in_fs_metadata
)
3211 if (device
->total_bytes
> device
->bytes_used
)
3212 total_avail
= device
->total_bytes
- device
->bytes_used
;
3216 /* If there is no space on this device, skip it. */
3217 if (total_avail
== 0)
3220 ret
= find_free_dev_extent(device
,
3221 max_stripe_size
* dev_stripes
,
3222 &dev_offset
, &max_avail
);
3223 if (ret
&& ret
!= -ENOSPC
)
3227 max_avail
= max_stripe_size
* dev_stripes
;
3229 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3232 devices_info
[ndevs
].dev_offset
= dev_offset
;
3233 devices_info
[ndevs
].max_avail
= max_avail
;
3234 devices_info
[ndevs
].total_avail
= total_avail
;
3235 devices_info
[ndevs
].dev
= device
;
3240 * now sort the devices by hole size / available space
3242 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3243 btrfs_cmp_device_info
, NULL
);
3245 /* round down to number of usable stripes */
3246 ndevs
-= ndevs
% devs_increment
;
3248 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3253 if (devs_max
&& ndevs
> devs_max
)
3256 * the primary goal is to maximize the number of stripes, so use as many
3257 * devices as possible, even if the stripes are not maximum sized.
3259 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3260 num_stripes
= ndevs
* dev_stripes
;
3262 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
3263 stripe_size
= max_chunk_size
* ncopies
;
3264 do_div(stripe_size
, num_stripes
);
3267 do_div(stripe_size
, dev_stripes
);
3268 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3269 stripe_size
*= BTRFS_STRIPE_LEN
;
3271 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3276 map
->num_stripes
= num_stripes
;
3278 for (i
= 0; i
< ndevs
; ++i
) {
3279 for (j
= 0; j
< dev_stripes
; ++j
) {
3280 int s
= i
* dev_stripes
+ j
;
3281 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3282 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3286 map
->sector_size
= extent_root
->sectorsize
;
3287 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3288 map
->io_align
= BTRFS_STRIPE_LEN
;
3289 map
->io_width
= BTRFS_STRIPE_LEN
;
3291 map
->sub_stripes
= sub_stripes
;
3294 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3296 *stripe_size_out
= stripe_size
;
3297 *num_bytes_out
= num_bytes
;
3299 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3301 em
= alloc_extent_map();
3306 em
->bdev
= (struct block_device
*)map
;
3308 em
->len
= num_bytes
;
3309 em
->block_start
= 0;
3310 em
->block_len
= em
->len
;
3312 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3313 write_lock(&em_tree
->lock
);
3314 ret
= add_extent_mapping(em_tree
, em
);
3315 write_unlock(&em_tree
->lock
);
3317 free_extent_map(em
);
3319 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3320 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3324 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3325 struct btrfs_device
*device
;
3328 device
= map
->stripes
[i
].dev
;
3329 dev_offset
= map
->stripes
[i
].physical
;
3331 ret
= btrfs_alloc_dev_extent(trans
, device
,
3332 info
->chunk_root
->root_key
.objectid
,
3333 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3334 start
, dev_offset
, stripe_size
);
3338 kfree(devices_info
);
3343 kfree(devices_info
);
3347 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3348 struct btrfs_root
*extent_root
,
3349 struct map_lookup
*map
, u64 chunk_offset
,
3350 u64 chunk_size
, u64 stripe_size
)
3353 struct btrfs_key key
;
3354 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3355 struct btrfs_device
*device
;
3356 struct btrfs_chunk
*chunk
;
3357 struct btrfs_stripe
*stripe
;
3358 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3362 chunk
= kzalloc(item_size
, GFP_NOFS
);
3367 while (index
< map
->num_stripes
) {
3368 device
= map
->stripes
[index
].dev
;
3369 device
->bytes_used
+= stripe_size
;
3370 ret
= btrfs_update_device(trans
, device
);
3375 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3376 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3378 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3381 stripe
= &chunk
->stripe
;
3382 while (index
< map
->num_stripes
) {
3383 device
= map
->stripes
[index
].dev
;
3384 dev_offset
= map
->stripes
[index
].physical
;
3386 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3387 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3388 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3393 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3394 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3395 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3396 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3397 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3398 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3399 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3400 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3401 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3403 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3404 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3405 key
.offset
= chunk_offset
;
3407 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3410 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3411 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3421 * Chunk allocation falls into two parts. The first part does works
3422 * that make the new allocated chunk useable, but not do any operation
3423 * that modifies the chunk tree. The second part does the works that
3424 * require modifying the chunk tree. This division is important for the
3425 * bootstrap process of adding storage to a seed btrfs.
3427 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3428 struct btrfs_root
*extent_root
, u64 type
)
3433 struct map_lookup
*map
;
3434 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3437 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3442 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3443 &stripe_size
, chunk_offset
, type
);
3447 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3448 chunk_size
, stripe_size
);
3453 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3454 struct btrfs_root
*root
,
3455 struct btrfs_device
*device
)
3458 u64 sys_chunk_offset
;
3462 u64 sys_stripe_size
;
3464 struct map_lookup
*map
;
3465 struct map_lookup
*sys_map
;
3466 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3467 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3470 ret
= find_next_chunk(fs_info
->chunk_root
,
3471 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3475 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3476 fs_info
->avail_metadata_alloc_bits
;
3477 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3479 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3480 &stripe_size
, chunk_offset
, alloc_profile
);
3483 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3485 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3486 fs_info
->avail_system_alloc_bits
;
3487 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3489 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3490 &sys_chunk_size
, &sys_stripe_size
,
3491 sys_chunk_offset
, alloc_profile
);
3494 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3498 * Modifying chunk tree needs allocating new blocks from both
3499 * system block group and metadata block group. So we only can
3500 * do operations require modifying the chunk tree after both
3501 * block groups were created.
3503 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3504 chunk_size
, stripe_size
);
3507 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3508 sys_chunk_offset
, sys_chunk_size
,
3514 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3516 struct extent_map
*em
;
3517 struct map_lookup
*map
;
3518 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3522 read_lock(&map_tree
->map_tree
.lock
);
3523 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3524 read_unlock(&map_tree
->map_tree
.lock
);
3528 if (btrfs_test_opt(root
, DEGRADED
)) {
3529 free_extent_map(em
);
3533 map
= (struct map_lookup
*)em
->bdev
;
3534 for (i
= 0; i
< map
->num_stripes
; i
++) {
3535 if (!map
->stripes
[i
].dev
->writeable
) {
3540 free_extent_map(em
);
3544 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3546 extent_map_tree_init(&tree
->map_tree
);
3549 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3551 struct extent_map
*em
;
3554 write_lock(&tree
->map_tree
.lock
);
3555 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3557 remove_extent_mapping(&tree
->map_tree
, em
);
3558 write_unlock(&tree
->map_tree
.lock
);
3563 free_extent_map(em
);
3564 /* once for the tree */
3565 free_extent_map(em
);
3569 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
3571 struct extent_map
*em
;
3572 struct map_lookup
*map
;
3573 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3576 read_lock(&em_tree
->lock
);
3577 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3578 read_unlock(&em_tree
->lock
);
3581 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3582 map
= (struct map_lookup
*)em
->bdev
;
3583 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
3584 ret
= map
->num_stripes
;
3585 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3586 ret
= map
->sub_stripes
;
3589 free_extent_map(em
);
3593 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
3597 if (map
->stripes
[optimal
].dev
->bdev
)
3599 for (i
= first
; i
< first
+ num
; i
++) {
3600 if (map
->stripes
[i
].dev
->bdev
)
3603 /* we couldn't find one that doesn't fail. Just return something
3604 * and the io error handling code will clean up eventually
3609 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3610 u64 logical
, u64
*length
,
3611 struct btrfs_bio
**bbio_ret
,
3614 struct extent_map
*em
;
3615 struct map_lookup
*map
;
3616 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3619 u64 stripe_end_offset
;
3628 struct btrfs_bio
*bbio
= NULL
;
3630 read_lock(&em_tree
->lock
);
3631 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
3632 read_unlock(&em_tree
->lock
);
3635 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
3636 (unsigned long long)logical
,
3637 (unsigned long long)*length
);
3641 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3642 map
= (struct map_lookup
*)em
->bdev
;
3643 offset
= logical
- em
->start
;
3645 if (mirror_num
> map
->num_stripes
)
3650 * stripe_nr counts the total number of stripes we have to stride
3651 * to get to this block
3653 do_div(stripe_nr
, map
->stripe_len
);
3655 stripe_offset
= stripe_nr
* map
->stripe_len
;
3656 BUG_ON(offset
< stripe_offset
);
3658 /* stripe_offset is the offset of this block in its stripe*/
3659 stripe_offset
= offset
- stripe_offset
;
3661 if (rw
& REQ_DISCARD
)
3662 *length
= min_t(u64
, em
->len
- offset
, *length
);
3663 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
3664 /* we limit the length of each bio to what fits in a stripe */
3665 *length
= min_t(u64
, em
->len
- offset
,
3666 map
->stripe_len
- stripe_offset
);
3668 *length
= em
->len
- offset
;
3676 stripe_nr_orig
= stripe_nr
;
3677 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3678 (~(map
->stripe_len
- 1));
3679 do_div(stripe_nr_end
, map
->stripe_len
);
3680 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3682 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3683 if (rw
& REQ_DISCARD
)
3684 num_stripes
= min_t(u64
, map
->num_stripes
,
3685 stripe_nr_end
- stripe_nr_orig
);
3686 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3687 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3688 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3689 num_stripes
= map
->num_stripes
;
3690 else if (mirror_num
)
3691 stripe_index
= mirror_num
- 1;
3693 stripe_index
= find_live_mirror(map
, 0,
3695 current
->pid
% map
->num_stripes
);
3696 mirror_num
= stripe_index
+ 1;
3699 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3700 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3701 num_stripes
= map
->num_stripes
;
3702 } else if (mirror_num
) {
3703 stripe_index
= mirror_num
- 1;
3708 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3709 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3711 stripe_index
= do_div(stripe_nr
, factor
);
3712 stripe_index
*= map
->sub_stripes
;
3715 num_stripes
= map
->sub_stripes
;
3716 else if (rw
& REQ_DISCARD
)
3717 num_stripes
= min_t(u64
, map
->sub_stripes
*
3718 (stripe_nr_end
- stripe_nr_orig
),
3720 else if (mirror_num
)
3721 stripe_index
+= mirror_num
- 1;
3723 stripe_index
= find_live_mirror(map
, stripe_index
,
3724 map
->sub_stripes
, stripe_index
+
3725 current
->pid
% map
->sub_stripes
);
3726 mirror_num
= stripe_index
+ 1;
3730 * after this do_div call, stripe_nr is the number of stripes
3731 * on this device we have to walk to find the data, and
3732 * stripe_index is the number of our device in the stripe array
3734 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3735 mirror_num
= stripe_index
+ 1;
3737 BUG_ON(stripe_index
>= map
->num_stripes
);
3739 bbio
= kzalloc(btrfs_bio_size(num_stripes
), GFP_NOFS
);
3744 atomic_set(&bbio
->error
, 0);
3746 if (rw
& REQ_DISCARD
) {
3748 int sub_stripes
= 0;
3749 u64 stripes_per_dev
= 0;
3750 u32 remaining_stripes
= 0;
3753 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
3754 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3757 sub_stripes
= map
->sub_stripes
;
3759 factor
= map
->num_stripes
/ sub_stripes
;
3760 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
3763 &remaining_stripes
);
3766 for (i
= 0; i
< num_stripes
; i
++) {
3767 bbio
->stripes
[i
].physical
=
3768 map
->stripes
[stripe_index
].physical
+
3769 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3770 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3772 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
3773 BTRFS_BLOCK_GROUP_RAID10
)) {
3774 bbio
->stripes
[i
].length
= stripes_per_dev
*
3776 if (i
/ sub_stripes
< remaining_stripes
)
3777 bbio
->stripes
[i
].length
+=
3779 if (i
< sub_stripes
)
3780 bbio
->stripes
[i
].length
-=
3782 if ((i
/ sub_stripes
+ 1) %
3783 sub_stripes
== remaining_stripes
)
3784 bbio
->stripes
[i
].length
-=
3786 if (i
== sub_stripes
- 1)
3789 bbio
->stripes
[i
].length
= *length
;
3792 if (stripe_index
== map
->num_stripes
) {
3793 /* This could only happen for RAID0/10 */
3799 for (i
= 0; i
< num_stripes
; i
++) {
3800 bbio
->stripes
[i
].physical
=
3801 map
->stripes
[stripe_index
].physical
+
3803 stripe_nr
* map
->stripe_len
;
3804 bbio
->stripes
[i
].dev
=
3805 map
->stripes
[stripe_index
].dev
;
3810 if (rw
& REQ_WRITE
) {
3811 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
3812 BTRFS_BLOCK_GROUP_RAID10
|
3813 BTRFS_BLOCK_GROUP_DUP
)) {
3819 bbio
->num_stripes
= num_stripes
;
3820 bbio
->max_errors
= max_errors
;
3821 bbio
->mirror_num
= mirror_num
;
3823 free_extent_map(em
);
3827 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3828 u64 logical
, u64
*length
,
3829 struct btrfs_bio
**bbio_ret
, int mirror_num
)
3831 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
3835 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3836 u64 chunk_start
, u64 physical
, u64 devid
,
3837 u64
**logical
, int *naddrs
, int *stripe_len
)
3839 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3840 struct extent_map
*em
;
3841 struct map_lookup
*map
;
3848 read_lock(&em_tree
->lock
);
3849 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3850 read_unlock(&em_tree
->lock
);
3852 BUG_ON(!em
|| em
->start
!= chunk_start
);
3853 map
= (struct map_lookup
*)em
->bdev
;
3856 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3857 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3858 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3859 do_div(length
, map
->num_stripes
);
3861 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3864 for (i
= 0; i
< map
->num_stripes
; i
++) {
3865 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3867 if (map
->stripes
[i
].physical
> physical
||
3868 map
->stripes
[i
].physical
+ length
<= physical
)
3871 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3872 do_div(stripe_nr
, map
->stripe_len
);
3874 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3875 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3876 do_div(stripe_nr
, map
->sub_stripes
);
3877 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3878 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3880 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3881 WARN_ON(nr
>= map
->num_stripes
);
3882 for (j
= 0; j
< nr
; j
++) {
3883 if (buf
[j
] == bytenr
)
3887 WARN_ON(nr
>= map
->num_stripes
);
3894 *stripe_len
= map
->stripe_len
;
3896 free_extent_map(em
);
3900 static void btrfs_end_bio(struct bio
*bio
, int err
)
3902 struct btrfs_bio
*bbio
= bio
->bi_private
;
3903 int is_orig_bio
= 0;
3906 atomic_inc(&bbio
->error
);
3908 if (bio
== bbio
->orig_bio
)
3911 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
3914 bio
= bbio
->orig_bio
;
3916 bio
->bi_private
= bbio
->private;
3917 bio
->bi_end_io
= bbio
->end_io
;
3918 bio
->bi_bdev
= (struct block_device
*)
3919 (unsigned long)bbio
->mirror_num
;
3920 /* only send an error to the higher layers if it is
3921 * beyond the tolerance of the multi-bio
3923 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
3927 * this bio is actually up to date, we didn't
3928 * go over the max number of errors
3930 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3935 bio_endio(bio
, err
);
3936 } else if (!is_orig_bio
) {
3941 struct async_sched
{
3944 struct btrfs_fs_info
*info
;
3945 struct btrfs_work work
;
3949 * see run_scheduled_bios for a description of why bios are collected for
3952 * This will add one bio to the pending list for a device and make sure
3953 * the work struct is scheduled.
3955 static noinline
int schedule_bio(struct btrfs_root
*root
,
3956 struct btrfs_device
*device
,
3957 int rw
, struct bio
*bio
)
3959 int should_queue
= 1;
3960 struct btrfs_pending_bios
*pending_bios
;
3962 /* don't bother with additional async steps for reads, right now */
3963 if (!(rw
& REQ_WRITE
)) {
3965 btrfsic_submit_bio(rw
, bio
);
3971 * nr_async_bios allows us to reliably return congestion to the
3972 * higher layers. Otherwise, the async bio makes it appear we have
3973 * made progress against dirty pages when we've really just put it
3974 * on a queue for later
3976 atomic_inc(&root
->fs_info
->nr_async_bios
);
3977 WARN_ON(bio
->bi_next
);
3978 bio
->bi_next
= NULL
;
3981 spin_lock(&device
->io_lock
);
3982 if (bio
->bi_rw
& REQ_SYNC
)
3983 pending_bios
= &device
->pending_sync_bios
;
3985 pending_bios
= &device
->pending_bios
;
3987 if (pending_bios
->tail
)
3988 pending_bios
->tail
->bi_next
= bio
;
3990 pending_bios
->tail
= bio
;
3991 if (!pending_bios
->head
)
3992 pending_bios
->head
= bio
;
3993 if (device
->running_pending
)
3996 spin_unlock(&device
->io_lock
);
3999 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4004 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4005 int mirror_num
, int async_submit
)
4007 struct btrfs_mapping_tree
*map_tree
;
4008 struct btrfs_device
*dev
;
4009 struct bio
*first_bio
= bio
;
4010 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4016 struct btrfs_bio
*bbio
= NULL
;
4018 length
= bio
->bi_size
;
4019 map_tree
= &root
->fs_info
->mapping_tree
;
4020 map_length
= length
;
4022 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
4026 total_devs
= bbio
->num_stripes
;
4027 if (map_length
< length
) {
4028 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
4029 "len %llu\n", (unsigned long long)logical
,
4030 (unsigned long long)length
,
4031 (unsigned long long)map_length
);
4035 bbio
->orig_bio
= first_bio
;
4036 bbio
->private = first_bio
->bi_private
;
4037 bbio
->end_io
= first_bio
->bi_end_io
;
4038 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4040 while (dev_nr
< total_devs
) {
4041 if (dev_nr
< total_devs
- 1) {
4042 bio
= bio_clone(first_bio
, GFP_NOFS
);
4047 bio
->bi_private
= bbio
;
4048 bio
->bi_end_io
= btrfs_end_bio
;
4049 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
4050 dev
= bbio
->stripes
[dev_nr
].dev
;
4051 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
4052 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4053 "(%s id %llu), size=%u\n", rw
,
4054 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4055 dev
->name
, dev
->devid
, bio
->bi_size
);
4056 bio
->bi_bdev
= dev
->bdev
;
4058 schedule_bio(root
, dev
, rw
, bio
);
4060 btrfsic_submit_bio(rw
, bio
);
4062 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4063 bio
->bi_sector
= logical
>> 9;
4064 bio_endio(bio
, -EIO
);
4071 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
4074 struct btrfs_device
*device
;
4075 struct btrfs_fs_devices
*cur_devices
;
4077 cur_devices
= root
->fs_info
->fs_devices
;
4078 while (cur_devices
) {
4080 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4081 device
= __find_device(&cur_devices
->devices
,
4086 cur_devices
= cur_devices
->seed
;
4091 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4092 u64 devid
, u8
*dev_uuid
)
4094 struct btrfs_device
*device
;
4095 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4097 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4100 list_add(&device
->dev_list
,
4101 &fs_devices
->devices
);
4102 device
->dev_root
= root
->fs_info
->dev_root
;
4103 device
->devid
= devid
;
4104 device
->work
.func
= pending_bios_fn
;
4105 device
->fs_devices
= fs_devices
;
4106 device
->missing
= 1;
4107 fs_devices
->num_devices
++;
4108 fs_devices
->missing_devices
++;
4109 spin_lock_init(&device
->io_lock
);
4110 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4111 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4115 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4116 struct extent_buffer
*leaf
,
4117 struct btrfs_chunk
*chunk
)
4119 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4120 struct map_lookup
*map
;
4121 struct extent_map
*em
;
4125 u8 uuid
[BTRFS_UUID_SIZE
];
4130 logical
= key
->offset
;
4131 length
= btrfs_chunk_length(leaf
, chunk
);
4133 read_lock(&map_tree
->map_tree
.lock
);
4134 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4135 read_unlock(&map_tree
->map_tree
.lock
);
4137 /* already mapped? */
4138 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4139 free_extent_map(em
);
4142 free_extent_map(em
);
4145 em
= alloc_extent_map();
4148 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4149 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4151 free_extent_map(em
);
4155 em
->bdev
= (struct block_device
*)map
;
4156 em
->start
= logical
;
4158 em
->block_start
= 0;
4159 em
->block_len
= em
->len
;
4161 map
->num_stripes
= num_stripes
;
4162 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4163 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4164 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4165 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4166 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4167 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4168 for (i
= 0; i
< num_stripes
; i
++) {
4169 map
->stripes
[i
].physical
=
4170 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4171 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4172 read_extent_buffer(leaf
, uuid
, (unsigned long)
4173 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4175 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
4177 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4179 free_extent_map(em
);
4182 if (!map
->stripes
[i
].dev
) {
4183 map
->stripes
[i
].dev
=
4184 add_missing_dev(root
, devid
, uuid
);
4185 if (!map
->stripes
[i
].dev
) {
4187 free_extent_map(em
);
4191 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4194 write_lock(&map_tree
->map_tree
.lock
);
4195 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4196 write_unlock(&map_tree
->map_tree
.lock
);
4198 free_extent_map(em
);
4203 static int fill_device_from_item(struct extent_buffer
*leaf
,
4204 struct btrfs_dev_item
*dev_item
,
4205 struct btrfs_device
*device
)
4209 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4210 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4211 device
->total_bytes
= device
->disk_total_bytes
;
4212 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4213 device
->type
= btrfs_device_type(leaf
, dev_item
);
4214 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4215 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4216 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4218 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4219 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4224 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4226 struct btrfs_fs_devices
*fs_devices
;
4229 BUG_ON(!mutex_is_locked(&uuid_mutex
));
4231 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4232 while (fs_devices
) {
4233 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4237 fs_devices
= fs_devices
->seed
;
4240 fs_devices
= find_fsid(fsid
);
4246 fs_devices
= clone_fs_devices(fs_devices
);
4247 if (IS_ERR(fs_devices
)) {
4248 ret
= PTR_ERR(fs_devices
);
4252 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4253 root
->fs_info
->bdev_holder
);
4257 if (!fs_devices
->seeding
) {
4258 __btrfs_close_devices(fs_devices
);
4259 free_fs_devices(fs_devices
);
4264 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4265 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4270 static int read_one_dev(struct btrfs_root
*root
,
4271 struct extent_buffer
*leaf
,
4272 struct btrfs_dev_item
*dev_item
)
4274 struct btrfs_device
*device
;
4277 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4278 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4280 devid
= btrfs_device_id(leaf
, dev_item
);
4281 read_extent_buffer(leaf
, dev_uuid
,
4282 (unsigned long)btrfs_device_uuid(dev_item
),
4284 read_extent_buffer(leaf
, fs_uuid
,
4285 (unsigned long)btrfs_device_fsid(dev_item
),
4288 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4289 ret
= open_seed_devices(root
, fs_uuid
);
4290 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4294 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
4295 if (!device
|| !device
->bdev
) {
4296 if (!btrfs_test_opt(root
, DEGRADED
))
4300 printk(KERN_WARNING
"warning devid %llu missing\n",
4301 (unsigned long long)devid
);
4302 device
= add_missing_dev(root
, devid
, dev_uuid
);
4305 } else if (!device
->missing
) {
4307 * this happens when a device that was properly setup
4308 * in the device info lists suddenly goes bad.
4309 * device->bdev is NULL, and so we have to set
4310 * device->missing to one here
4312 root
->fs_info
->fs_devices
->missing_devices
++;
4313 device
->missing
= 1;
4317 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4318 BUG_ON(device
->writeable
);
4319 if (device
->generation
!=
4320 btrfs_device_generation(leaf
, dev_item
))
4324 fill_device_from_item(leaf
, dev_item
, device
);
4325 device
->dev_root
= root
->fs_info
->dev_root
;
4326 device
->in_fs_metadata
= 1;
4327 if (device
->writeable
) {
4328 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4329 spin_lock(&root
->fs_info
->free_chunk_lock
);
4330 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4332 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4338 int btrfs_read_sys_array(struct btrfs_root
*root
)
4340 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4341 struct extent_buffer
*sb
;
4342 struct btrfs_disk_key
*disk_key
;
4343 struct btrfs_chunk
*chunk
;
4345 unsigned long sb_ptr
;
4351 struct btrfs_key key
;
4353 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4354 BTRFS_SUPER_INFO_SIZE
);
4357 btrfs_set_buffer_uptodate(sb
);
4358 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4360 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4361 array_size
= btrfs_super_sys_array_size(super_copy
);
4363 ptr
= super_copy
->sys_chunk_array
;
4364 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4367 while (cur
< array_size
) {
4368 disk_key
= (struct btrfs_disk_key
*)ptr
;
4369 btrfs_disk_key_to_cpu(&key
, disk_key
);
4371 len
= sizeof(*disk_key
); ptr
+= len
;
4375 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4376 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4377 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4380 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4381 len
= btrfs_chunk_item_size(num_stripes
);
4390 free_extent_buffer(sb
);
4394 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
4396 struct btrfs_path
*path
;
4397 struct extent_buffer
*leaf
;
4398 struct btrfs_key key
;
4399 struct btrfs_key found_key
;
4403 root
= root
->fs_info
->chunk_root
;
4405 path
= btrfs_alloc_path();
4409 mutex_lock(&uuid_mutex
);
4412 /* first we search for all of the device items, and then we
4413 * read in all of the chunk items. This way we can create chunk
4414 * mappings that reference all of the devices that are afound
4416 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
4420 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4424 leaf
= path
->nodes
[0];
4425 slot
= path
->slots
[0];
4426 if (slot
>= btrfs_header_nritems(leaf
)) {
4427 ret
= btrfs_next_leaf(root
, path
);
4434 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4435 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4436 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
4438 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
4439 struct btrfs_dev_item
*dev_item
;
4440 dev_item
= btrfs_item_ptr(leaf
, slot
,
4441 struct btrfs_dev_item
);
4442 ret
= read_one_dev(root
, leaf
, dev_item
);
4446 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4447 struct btrfs_chunk
*chunk
;
4448 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
4449 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
4455 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4457 btrfs_release_path(path
);
4462 unlock_chunks(root
);
4463 mutex_unlock(&uuid_mutex
);
4465 btrfs_free_path(path
);