2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
37 struct btrfs_root
*root
,
38 struct btrfs_device
*device
);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
41 static DEFINE_MUTEX(uuid_mutex
);
42 static LIST_HEAD(fs_uuids
);
44 static void lock_chunks(struct btrfs_root
*root
)
46 mutex_lock(&root
->fs_info
->chunk_mutex
);
49 static void unlock_chunks(struct btrfs_root
*root
)
51 mutex_unlock(&root
->fs_info
->chunk_mutex
);
54 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
56 struct btrfs_device
*device
;
57 WARN_ON(fs_devices
->opened
);
58 while (!list_empty(&fs_devices
->devices
)) {
59 device
= list_entry(fs_devices
->devices
.next
,
60 struct btrfs_device
, dev_list
);
61 list_del(&device
->dev_list
);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices
*fs_devices
;
72 while (!list_empty(&fs_uuids
)) {
73 fs_devices
= list_entry(fs_uuids
.next
,
74 struct btrfs_fs_devices
, list
);
75 list_del(&fs_devices
->list
);
76 free_fs_devices(fs_devices
);
81 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
84 struct btrfs_device
*dev
;
86 list_for_each_entry(dev
, head
, dev_list
) {
87 if (dev
->devid
== devid
&&
88 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
95 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
97 struct btrfs_fs_devices
*fs_devices
;
99 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
100 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
106 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
107 struct bio
*head
, struct bio
*tail
)
110 struct bio
*old_head
;
112 old_head
= pending_bios
->head
;
113 pending_bios
->head
= head
;
114 if (pending_bios
->tail
)
115 tail
->bi_next
= old_head
;
117 pending_bios
->tail
= tail
;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
134 struct backing_dev_info
*bdi
;
135 struct btrfs_fs_info
*fs_info
;
136 struct btrfs_pending_bios
*pending_bios
;
140 unsigned long num_run
;
141 unsigned long batch_run
= 0;
143 unsigned long last_waited
= 0;
145 int sync_pending
= 0;
146 struct blk_plug plug
;
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
154 blk_start_plug(&plug
);
156 bdi
= blk_get_backing_dev_info(device
->bdev
);
157 fs_info
= device
->dev_root
->fs_info
;
158 limit
= btrfs_async_submit_limit(fs_info
);
159 limit
= limit
* 2 / 3;
162 spin_lock(&device
->io_lock
);
167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
172 if (!force_reg
&& device
->pending_sync_bios
.head
) {
173 pending_bios
= &device
->pending_sync_bios
;
176 pending_bios
= &device
->pending_bios
;
180 pending
= pending_bios
->head
;
181 tail
= pending_bios
->tail
;
182 WARN_ON(pending
&& !tail
);
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
189 * device->running_pending is used to synchronize with the
192 if (device
->pending_sync_bios
.head
== NULL
&&
193 device
->pending_bios
.head
== NULL
) {
195 device
->running_pending
= 0;
198 device
->running_pending
= 1;
201 pending_bios
->head
= NULL
;
202 pending_bios
->tail
= NULL
;
204 spin_unlock(&device
->io_lock
);
209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
213 pending_bios
!= &device
->pending_sync_bios
&&
214 device
->pending_sync_bios
.head
) ||
215 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
216 device
->pending_bios
.head
)) {
217 spin_lock(&device
->io_lock
);
218 requeue_list(pending_bios
, pending
, tail
);
223 pending
= pending
->bi_next
;
225 atomic_dec(&fs_info
->nr_async_bios
);
227 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
228 waitqueue_active(&fs_info
->async_submit_wait
))
229 wake_up(&fs_info
->async_submit_wait
);
231 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
241 if (pending_bios
== &device
->pending_sync_bios
) {
243 } else if (sync_pending
) {
244 blk_finish_plug(&plug
);
245 blk_start_plug(&plug
);
249 submit_bio(cur
->bi_rw
, cur
);
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
260 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
261 fs_info
->fs_devices
->open_devices
> 1) {
262 struct io_context
*ioc
;
264 ioc
= current
->io_context
;
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
275 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
276 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
278 ioc
->last_waited
== last_waited
)) {
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
285 last_waited
= ioc
->last_waited
;
290 spin_lock(&device
->io_lock
);
291 requeue_list(pending_bios
, pending
, tail
);
292 device
->running_pending
= 1;
294 spin_unlock(&device
->io_lock
);
295 btrfs_requeue_work(&device
->work
);
298 /* unplug every 64 requests just for good measure */
299 if (batch_run
% 64 == 0) {
300 blk_finish_plug(&plug
);
301 blk_start_plug(&plug
);
310 spin_lock(&device
->io_lock
);
311 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
313 spin_unlock(&device
->io_lock
);
316 blk_finish_plug(&plug
);
320 static void pending_bios_fn(struct btrfs_work
*work
)
322 struct btrfs_device
*device
;
324 device
= container_of(work
, struct btrfs_device
, work
);
325 run_scheduled_bios(device
);
328 static noinline
int device_list_add(const char *path
,
329 struct btrfs_super_block
*disk_super
,
330 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
332 struct btrfs_device
*device
;
333 struct btrfs_fs_devices
*fs_devices
;
334 u64 found_transid
= btrfs_super_generation(disk_super
);
337 fs_devices
= find_fsid(disk_super
->fsid
);
339 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
342 INIT_LIST_HEAD(&fs_devices
->devices
);
343 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
344 list_add(&fs_devices
->list
, &fs_uuids
);
345 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
346 fs_devices
->latest_devid
= devid
;
347 fs_devices
->latest_trans
= found_transid
;
348 mutex_init(&fs_devices
->device_list_mutex
);
351 device
= __find_device(&fs_devices
->devices
, devid
,
352 disk_super
->dev_item
.uuid
);
355 if (fs_devices
->opened
)
358 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
360 /* we can safely leave the fs_devices entry around */
363 device
->devid
= devid
;
364 device
->work
.func
= pending_bios_fn
;
365 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
367 spin_lock_init(&device
->io_lock
);
368 device
->name
= kstrdup(path
, GFP_NOFS
);
373 INIT_LIST_HEAD(&device
->dev_alloc_list
);
375 /* init readahead state */
376 spin_lock_init(&device
->reada_lock
);
377 device
->reada_curr_zone
= NULL
;
378 atomic_set(&device
->reada_in_flight
, 0);
379 device
->reada_next
= 0;
380 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
381 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
383 mutex_lock(&fs_devices
->device_list_mutex
);
384 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
385 mutex_unlock(&fs_devices
->device_list_mutex
);
387 device
->fs_devices
= fs_devices
;
388 fs_devices
->num_devices
++;
389 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
390 name
= kstrdup(path
, GFP_NOFS
);
395 if (device
->missing
) {
396 fs_devices
->missing_devices
--;
401 if (found_transid
> fs_devices
->latest_trans
) {
402 fs_devices
->latest_devid
= devid
;
403 fs_devices
->latest_trans
= found_transid
;
405 *fs_devices_ret
= fs_devices
;
409 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
411 struct btrfs_fs_devices
*fs_devices
;
412 struct btrfs_device
*device
;
413 struct btrfs_device
*orig_dev
;
415 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
417 return ERR_PTR(-ENOMEM
);
419 INIT_LIST_HEAD(&fs_devices
->devices
);
420 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
421 INIT_LIST_HEAD(&fs_devices
->list
);
422 mutex_init(&fs_devices
->device_list_mutex
);
423 fs_devices
->latest_devid
= orig
->latest_devid
;
424 fs_devices
->latest_trans
= orig
->latest_trans
;
425 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
427 /* We have held the volume lock, it is safe to get the devices. */
428 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
429 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
433 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
439 device
->devid
= orig_dev
->devid
;
440 device
->work
.func
= pending_bios_fn
;
441 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
442 spin_lock_init(&device
->io_lock
);
443 INIT_LIST_HEAD(&device
->dev_list
);
444 INIT_LIST_HEAD(&device
->dev_alloc_list
);
446 list_add(&device
->dev_list
, &fs_devices
->devices
);
447 device
->fs_devices
= fs_devices
;
448 fs_devices
->num_devices
++;
452 free_fs_devices(fs_devices
);
453 return ERR_PTR(-ENOMEM
);
456 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
458 struct btrfs_device
*device
, *next
;
460 mutex_lock(&uuid_mutex
);
462 /* This is the initialized path, it is safe to release the devices. */
463 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
464 if (device
->in_fs_metadata
)
468 blkdev_put(device
->bdev
, device
->mode
);
470 fs_devices
->open_devices
--;
472 if (device
->writeable
) {
473 list_del_init(&device
->dev_alloc_list
);
474 device
->writeable
= 0;
475 fs_devices
->rw_devices
--;
477 list_del_init(&device
->dev_list
);
478 fs_devices
->num_devices
--;
483 if (fs_devices
->seed
) {
484 fs_devices
= fs_devices
->seed
;
488 mutex_unlock(&uuid_mutex
);
492 static void __free_device(struct work_struct
*work
)
494 struct btrfs_device
*device
;
496 device
= container_of(work
, struct btrfs_device
, rcu_work
);
499 blkdev_put(device
->bdev
, device
->mode
);
505 static void free_device(struct rcu_head
*head
)
507 struct btrfs_device
*device
;
509 device
= container_of(head
, struct btrfs_device
, rcu
);
511 INIT_WORK(&device
->rcu_work
, __free_device
);
512 schedule_work(&device
->rcu_work
);
515 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
517 struct btrfs_device
*device
;
519 if (--fs_devices
->opened
> 0)
522 mutex_lock(&fs_devices
->device_list_mutex
);
523 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
524 struct btrfs_device
*new_device
;
527 fs_devices
->open_devices
--;
529 if (device
->writeable
) {
530 list_del_init(&device
->dev_alloc_list
);
531 fs_devices
->rw_devices
--;
534 if (device
->can_discard
)
535 fs_devices
->num_can_discard
--;
537 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
539 memcpy(new_device
, device
, sizeof(*new_device
));
540 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
541 BUG_ON(device
->name
&& !new_device
->name
);
542 new_device
->bdev
= NULL
;
543 new_device
->writeable
= 0;
544 new_device
->in_fs_metadata
= 0;
545 new_device
->can_discard
= 0;
546 spin_lock_init(&new_device
->io_lock
);
547 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
549 call_rcu(&device
->rcu
, free_device
);
551 mutex_unlock(&fs_devices
->device_list_mutex
);
553 WARN_ON(fs_devices
->open_devices
);
554 WARN_ON(fs_devices
->rw_devices
);
555 fs_devices
->opened
= 0;
556 fs_devices
->seeding
= 0;
561 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
563 struct btrfs_fs_devices
*seed_devices
= NULL
;
566 mutex_lock(&uuid_mutex
);
567 ret
= __btrfs_close_devices(fs_devices
);
568 if (!fs_devices
->opened
) {
569 seed_devices
= fs_devices
->seed
;
570 fs_devices
->seed
= NULL
;
572 mutex_unlock(&uuid_mutex
);
574 while (seed_devices
) {
575 fs_devices
= seed_devices
;
576 seed_devices
= fs_devices
->seed
;
577 __btrfs_close_devices(fs_devices
);
578 free_fs_devices(fs_devices
);
581 * Wait for rcu kworkers under __btrfs_close_devices
582 * to finish all blkdev_puts so device is really
583 * free when umount is done.
589 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
590 fmode_t flags
, void *holder
)
592 struct request_queue
*q
;
593 struct block_device
*bdev
;
594 struct list_head
*head
= &fs_devices
->devices
;
595 struct btrfs_device
*device
;
596 struct block_device
*latest_bdev
= NULL
;
597 struct buffer_head
*bh
;
598 struct btrfs_super_block
*disk_super
;
599 u64 latest_devid
= 0;
600 u64 latest_transid
= 0;
607 list_for_each_entry(device
, head
, dev_list
) {
613 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
615 printk(KERN_INFO
"open %s failed\n", device
->name
);
618 set_blocksize(bdev
, 4096);
620 bh
= btrfs_read_dev_super(bdev
);
624 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
625 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
626 if (devid
!= device
->devid
)
629 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
633 device
->generation
= btrfs_super_generation(disk_super
);
634 if (!latest_transid
|| device
->generation
> latest_transid
) {
635 latest_devid
= devid
;
636 latest_transid
= device
->generation
;
640 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
641 device
->writeable
= 0;
643 device
->writeable
= !bdev_read_only(bdev
);
647 q
= bdev_get_queue(bdev
);
648 if (blk_queue_discard(q
)) {
649 device
->can_discard
= 1;
650 fs_devices
->num_can_discard
++;
654 device
->in_fs_metadata
= 0;
655 device
->mode
= flags
;
657 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
658 fs_devices
->rotating
= 1;
660 fs_devices
->open_devices
++;
661 if (device
->writeable
) {
662 fs_devices
->rw_devices
++;
663 list_add(&device
->dev_alloc_list
,
664 &fs_devices
->alloc_list
);
672 blkdev_put(bdev
, flags
);
676 if (fs_devices
->open_devices
== 0) {
680 fs_devices
->seeding
= seeding
;
681 fs_devices
->opened
= 1;
682 fs_devices
->latest_bdev
= latest_bdev
;
683 fs_devices
->latest_devid
= latest_devid
;
684 fs_devices
->latest_trans
= latest_transid
;
685 fs_devices
->total_rw_bytes
= 0;
690 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
691 fmode_t flags
, void *holder
)
695 mutex_lock(&uuid_mutex
);
696 if (fs_devices
->opened
) {
697 fs_devices
->opened
++;
700 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
702 mutex_unlock(&uuid_mutex
);
706 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
707 struct btrfs_fs_devices
**fs_devices_ret
)
709 struct btrfs_super_block
*disk_super
;
710 struct block_device
*bdev
;
711 struct buffer_head
*bh
;
716 mutex_lock(&uuid_mutex
);
719 bdev
= blkdev_get_by_path(path
, flags
, holder
);
726 ret
= set_blocksize(bdev
, 4096);
729 bh
= btrfs_read_dev_super(bdev
);
734 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
735 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
736 transid
= btrfs_super_generation(disk_super
);
737 if (disk_super
->label
[0])
738 printk(KERN_INFO
"device label %s ", disk_super
->label
);
740 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
741 printk(KERN_CONT
"devid %llu transid %llu %s\n",
742 (unsigned long long)devid
, (unsigned long long)transid
, path
);
743 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
747 blkdev_put(bdev
, flags
);
749 mutex_unlock(&uuid_mutex
);
753 /* helper to account the used device space in the range */
754 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
755 u64 end
, u64
*length
)
757 struct btrfs_key key
;
758 struct btrfs_root
*root
= device
->dev_root
;
759 struct btrfs_dev_extent
*dev_extent
;
760 struct btrfs_path
*path
;
764 struct extent_buffer
*l
;
768 if (start
>= device
->total_bytes
)
771 path
= btrfs_alloc_path();
776 key
.objectid
= device
->devid
;
778 key
.type
= BTRFS_DEV_EXTENT_KEY
;
780 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
784 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
791 slot
= path
->slots
[0];
792 if (slot
>= btrfs_header_nritems(l
)) {
793 ret
= btrfs_next_leaf(root
, path
);
801 btrfs_item_key_to_cpu(l
, &key
, slot
);
803 if (key
.objectid
< device
->devid
)
806 if (key
.objectid
> device
->devid
)
809 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
812 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
813 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
815 if (key
.offset
<= start
&& extent_end
> end
) {
816 *length
= end
- start
+ 1;
818 } else if (key
.offset
<= start
&& extent_end
> start
)
819 *length
+= extent_end
- start
;
820 else if (key
.offset
> start
&& extent_end
<= end
)
821 *length
+= extent_end
- key
.offset
;
822 else if (key
.offset
> start
&& key
.offset
<= end
) {
823 *length
+= end
- key
.offset
+ 1;
825 } else if (key
.offset
> end
)
833 btrfs_free_path(path
);
838 * find_free_dev_extent - find free space in the specified device
839 * @trans: transaction handler
840 * @device: the device which we search the free space in
841 * @num_bytes: the size of the free space that we need
842 * @start: store the start of the free space.
843 * @len: the size of the free space. that we find, or the size of the max
844 * free space if we don't find suitable free space
846 * this uses a pretty simple search, the expectation is that it is
847 * called very infrequently and that a given device has a small number
850 * @start is used to store the start of the free space if we find. But if we
851 * don't find suitable free space, it will be used to store the start position
852 * of the max free space.
854 * @len is used to store the size of the free space that we find.
855 * But if we don't find suitable free space, it is used to store the size of
856 * the max free space.
858 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
859 struct btrfs_device
*device
, u64 num_bytes
,
860 u64
*start
, u64
*len
)
862 struct btrfs_key key
;
863 struct btrfs_root
*root
= device
->dev_root
;
864 struct btrfs_dev_extent
*dev_extent
;
865 struct btrfs_path
*path
;
871 u64 search_end
= device
->total_bytes
;
874 struct extent_buffer
*l
;
876 /* FIXME use last free of some kind */
878 /* we don't want to overwrite the superblock on the drive,
879 * so we make sure to start at an offset of at least 1MB
881 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
883 max_hole_start
= search_start
;
887 if (search_start
>= search_end
) {
892 path
= btrfs_alloc_path();
899 key
.objectid
= device
->devid
;
900 key
.offset
= search_start
;
901 key
.type
= BTRFS_DEV_EXTENT_KEY
;
903 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
907 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
914 slot
= path
->slots
[0];
915 if (slot
>= btrfs_header_nritems(l
)) {
916 ret
= btrfs_next_leaf(root
, path
);
924 btrfs_item_key_to_cpu(l
, &key
, slot
);
926 if (key
.objectid
< device
->devid
)
929 if (key
.objectid
> device
->devid
)
932 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
935 if (key
.offset
> search_start
) {
936 hole_size
= key
.offset
- search_start
;
938 if (hole_size
> max_hole_size
) {
939 max_hole_start
= search_start
;
940 max_hole_size
= hole_size
;
944 * If this free space is greater than which we need,
945 * it must be the max free space that we have found
946 * until now, so max_hole_start must point to the start
947 * of this free space and the length of this free space
948 * is stored in max_hole_size. Thus, we return
949 * max_hole_start and max_hole_size and go back to the
952 if (hole_size
>= num_bytes
) {
958 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
959 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
961 if (extent_end
> search_start
)
962 search_start
= extent_end
;
969 * At this point, search_start should be the end of
970 * allocated dev extents, and when shrinking the device,
971 * search_end may be smaller than search_start.
973 if (search_end
> search_start
)
974 hole_size
= search_end
- search_start
;
976 if (hole_size
> max_hole_size
) {
977 max_hole_start
= search_start
;
978 max_hole_size
= hole_size
;
982 if (hole_size
< num_bytes
)
988 btrfs_free_path(path
);
990 *start
= max_hole_start
;
992 *len
= max_hole_size
;
996 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
997 struct btrfs_device
*device
,
1001 struct btrfs_path
*path
;
1002 struct btrfs_root
*root
= device
->dev_root
;
1003 struct btrfs_key key
;
1004 struct btrfs_key found_key
;
1005 struct extent_buffer
*leaf
= NULL
;
1006 struct btrfs_dev_extent
*extent
= NULL
;
1008 path
= btrfs_alloc_path();
1012 key
.objectid
= device
->devid
;
1014 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1016 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1018 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1019 BTRFS_DEV_EXTENT_KEY
);
1022 leaf
= path
->nodes
[0];
1023 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1024 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1025 struct btrfs_dev_extent
);
1026 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1027 btrfs_dev_extent_length(leaf
, extent
) < start
);
1029 btrfs_release_path(path
);
1031 } else if (ret
== 0) {
1032 leaf
= path
->nodes
[0];
1033 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1034 struct btrfs_dev_extent
);
1038 if (device
->bytes_used
> 0) {
1039 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1040 device
->bytes_used
-= len
;
1041 spin_lock(&root
->fs_info
->free_chunk_lock
);
1042 root
->fs_info
->free_chunk_space
+= len
;
1043 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1045 ret
= btrfs_del_item(trans
, root
, path
);
1048 btrfs_free_path(path
);
1052 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1053 struct btrfs_device
*device
,
1054 u64 chunk_tree
, u64 chunk_objectid
,
1055 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1058 struct btrfs_path
*path
;
1059 struct btrfs_root
*root
= device
->dev_root
;
1060 struct btrfs_dev_extent
*extent
;
1061 struct extent_buffer
*leaf
;
1062 struct btrfs_key key
;
1064 WARN_ON(!device
->in_fs_metadata
);
1065 path
= btrfs_alloc_path();
1069 key
.objectid
= device
->devid
;
1071 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1072 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1076 leaf
= path
->nodes
[0];
1077 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1078 struct btrfs_dev_extent
);
1079 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1080 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1081 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1083 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1084 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1087 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1088 btrfs_mark_buffer_dirty(leaf
);
1089 btrfs_free_path(path
);
1093 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1094 u64 objectid
, u64
*offset
)
1096 struct btrfs_path
*path
;
1098 struct btrfs_key key
;
1099 struct btrfs_chunk
*chunk
;
1100 struct btrfs_key found_key
;
1102 path
= btrfs_alloc_path();
1106 key
.objectid
= objectid
;
1107 key
.offset
= (u64
)-1;
1108 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1110 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1116 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1120 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1122 if (found_key
.objectid
!= objectid
)
1125 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1126 struct btrfs_chunk
);
1127 *offset
= found_key
.offset
+
1128 btrfs_chunk_length(path
->nodes
[0], chunk
);
1133 btrfs_free_path(path
);
1137 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1140 struct btrfs_key key
;
1141 struct btrfs_key found_key
;
1142 struct btrfs_path
*path
;
1144 root
= root
->fs_info
->chunk_root
;
1146 path
= btrfs_alloc_path();
1150 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1151 key
.type
= BTRFS_DEV_ITEM_KEY
;
1152 key
.offset
= (u64
)-1;
1154 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1160 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1161 BTRFS_DEV_ITEM_KEY
);
1165 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1167 *objectid
= found_key
.offset
+ 1;
1171 btrfs_free_path(path
);
1176 * the device information is stored in the chunk root
1177 * the btrfs_device struct should be fully filled in
1179 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1180 struct btrfs_root
*root
,
1181 struct btrfs_device
*device
)
1184 struct btrfs_path
*path
;
1185 struct btrfs_dev_item
*dev_item
;
1186 struct extent_buffer
*leaf
;
1187 struct btrfs_key key
;
1190 root
= root
->fs_info
->chunk_root
;
1192 path
= btrfs_alloc_path();
1196 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1197 key
.type
= BTRFS_DEV_ITEM_KEY
;
1198 key
.offset
= device
->devid
;
1200 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1205 leaf
= path
->nodes
[0];
1206 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1208 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1209 btrfs_set_device_generation(leaf
, dev_item
, 0);
1210 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1211 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1212 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1213 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1214 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1215 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1216 btrfs_set_device_group(leaf
, dev_item
, 0);
1217 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1218 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1219 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1221 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1222 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1223 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1224 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1225 btrfs_mark_buffer_dirty(leaf
);
1229 btrfs_free_path(path
);
1233 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1234 struct btrfs_device
*device
)
1237 struct btrfs_path
*path
;
1238 struct btrfs_key key
;
1239 struct btrfs_trans_handle
*trans
;
1241 root
= root
->fs_info
->chunk_root
;
1243 path
= btrfs_alloc_path();
1247 trans
= btrfs_start_transaction(root
, 0);
1248 if (IS_ERR(trans
)) {
1249 btrfs_free_path(path
);
1250 return PTR_ERR(trans
);
1252 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1253 key
.type
= BTRFS_DEV_ITEM_KEY
;
1254 key
.offset
= device
->devid
;
1257 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1266 ret
= btrfs_del_item(trans
, root
, path
);
1270 btrfs_free_path(path
);
1271 unlock_chunks(root
);
1272 btrfs_commit_transaction(trans
, root
);
1276 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1278 struct btrfs_device
*device
;
1279 struct btrfs_device
*next_device
;
1280 struct block_device
*bdev
;
1281 struct buffer_head
*bh
= NULL
;
1282 struct btrfs_super_block
*disk_super
;
1283 struct btrfs_fs_devices
*cur_devices
;
1289 bool clear_super
= false;
1291 mutex_lock(&uuid_mutex
);
1292 mutex_lock(&root
->fs_info
->volume_mutex
);
1294 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1295 root
->fs_info
->avail_system_alloc_bits
|
1296 root
->fs_info
->avail_metadata_alloc_bits
;
1298 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1299 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1300 printk(KERN_ERR
"btrfs: unable to go below four devices "
1306 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1307 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1308 printk(KERN_ERR
"btrfs: unable to go below two "
1309 "devices on raid1\n");
1314 if (strcmp(device_path
, "missing") == 0) {
1315 struct list_head
*devices
;
1316 struct btrfs_device
*tmp
;
1319 devices
= &root
->fs_info
->fs_devices
->devices
;
1321 * It is safe to read the devices since the volume_mutex
1324 list_for_each_entry(tmp
, devices
, dev_list
) {
1325 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1334 printk(KERN_ERR
"btrfs: no missing devices found to "
1339 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1340 root
->fs_info
->bdev_holder
);
1342 ret
= PTR_ERR(bdev
);
1346 set_blocksize(bdev
, 4096);
1347 bh
= btrfs_read_dev_super(bdev
);
1352 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1353 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1354 dev_uuid
= disk_super
->dev_item
.uuid
;
1355 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1363 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1364 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1370 if (device
->writeable
) {
1372 list_del_init(&device
->dev_alloc_list
);
1373 unlock_chunks(root
);
1374 root
->fs_info
->fs_devices
->rw_devices
--;
1378 ret
= btrfs_shrink_device(device
, 0);
1382 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1386 spin_lock(&root
->fs_info
->free_chunk_lock
);
1387 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1389 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1391 device
->in_fs_metadata
= 0;
1392 btrfs_scrub_cancel_dev(root
, device
);
1395 * the device list mutex makes sure that we don't change
1396 * the device list while someone else is writing out all
1397 * the device supers.
1400 cur_devices
= device
->fs_devices
;
1401 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1402 list_del_rcu(&device
->dev_list
);
1404 device
->fs_devices
->num_devices
--;
1406 if (device
->missing
)
1407 root
->fs_info
->fs_devices
->missing_devices
--;
1409 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1410 struct btrfs_device
, dev_list
);
1411 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1412 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1413 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1414 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1417 device
->fs_devices
->open_devices
--;
1419 call_rcu(&device
->rcu
, free_device
);
1420 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1422 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1423 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1425 if (cur_devices
->open_devices
== 0) {
1426 struct btrfs_fs_devices
*fs_devices
;
1427 fs_devices
= root
->fs_info
->fs_devices
;
1428 while (fs_devices
) {
1429 if (fs_devices
->seed
== cur_devices
)
1431 fs_devices
= fs_devices
->seed
;
1433 fs_devices
->seed
= cur_devices
->seed
;
1434 cur_devices
->seed
= NULL
;
1436 __btrfs_close_devices(cur_devices
);
1437 unlock_chunks(root
);
1438 free_fs_devices(cur_devices
);
1442 * at this point, the device is zero sized. We want to
1443 * remove it from the devices list and zero out the old super
1446 /* make sure this device isn't detected as part of
1449 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1450 set_buffer_dirty(bh
);
1451 sync_dirty_buffer(bh
);
1460 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1462 mutex_unlock(&root
->fs_info
->volume_mutex
);
1463 mutex_unlock(&uuid_mutex
);
1466 if (device
->writeable
) {
1468 list_add(&device
->dev_alloc_list
,
1469 &root
->fs_info
->fs_devices
->alloc_list
);
1470 unlock_chunks(root
);
1471 root
->fs_info
->fs_devices
->rw_devices
++;
1477 * does all the dirty work required for changing file system's UUID.
1479 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1480 struct btrfs_root
*root
)
1482 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1483 struct btrfs_fs_devices
*old_devices
;
1484 struct btrfs_fs_devices
*seed_devices
;
1485 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1486 struct btrfs_device
*device
;
1489 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1490 if (!fs_devices
->seeding
)
1493 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1497 old_devices
= clone_fs_devices(fs_devices
);
1498 if (IS_ERR(old_devices
)) {
1499 kfree(seed_devices
);
1500 return PTR_ERR(old_devices
);
1503 list_add(&old_devices
->list
, &fs_uuids
);
1505 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1506 seed_devices
->opened
= 1;
1507 INIT_LIST_HEAD(&seed_devices
->devices
);
1508 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1509 mutex_init(&seed_devices
->device_list_mutex
);
1511 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1512 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1514 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1516 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1517 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1518 device
->fs_devices
= seed_devices
;
1521 fs_devices
->seeding
= 0;
1522 fs_devices
->num_devices
= 0;
1523 fs_devices
->open_devices
= 0;
1524 fs_devices
->seed
= seed_devices
;
1526 generate_random_uuid(fs_devices
->fsid
);
1527 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1528 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1529 super_flags
= btrfs_super_flags(disk_super
) &
1530 ~BTRFS_SUPER_FLAG_SEEDING
;
1531 btrfs_set_super_flags(disk_super
, super_flags
);
1537 * strore the expected generation for seed devices in device items.
1539 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1540 struct btrfs_root
*root
)
1542 struct btrfs_path
*path
;
1543 struct extent_buffer
*leaf
;
1544 struct btrfs_dev_item
*dev_item
;
1545 struct btrfs_device
*device
;
1546 struct btrfs_key key
;
1547 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1548 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1552 path
= btrfs_alloc_path();
1556 root
= root
->fs_info
->chunk_root
;
1557 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1559 key
.type
= BTRFS_DEV_ITEM_KEY
;
1562 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1566 leaf
= path
->nodes
[0];
1568 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1569 ret
= btrfs_next_leaf(root
, path
);
1574 leaf
= path
->nodes
[0];
1575 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1576 btrfs_release_path(path
);
1580 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1581 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1582 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1585 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1586 struct btrfs_dev_item
);
1587 devid
= btrfs_device_id(leaf
, dev_item
);
1588 read_extent_buffer(leaf
, dev_uuid
,
1589 (unsigned long)btrfs_device_uuid(dev_item
),
1591 read_extent_buffer(leaf
, fs_uuid
,
1592 (unsigned long)btrfs_device_fsid(dev_item
),
1594 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1597 if (device
->fs_devices
->seeding
) {
1598 btrfs_set_device_generation(leaf
, dev_item
,
1599 device
->generation
);
1600 btrfs_mark_buffer_dirty(leaf
);
1608 btrfs_free_path(path
);
1612 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1614 struct request_queue
*q
;
1615 struct btrfs_trans_handle
*trans
;
1616 struct btrfs_device
*device
;
1617 struct block_device
*bdev
;
1618 struct list_head
*devices
;
1619 struct super_block
*sb
= root
->fs_info
->sb
;
1621 int seeding_dev
= 0;
1624 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1627 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1628 root
->fs_info
->bdev_holder
);
1630 return PTR_ERR(bdev
);
1632 if (root
->fs_info
->fs_devices
->seeding
) {
1634 down_write(&sb
->s_umount
);
1635 mutex_lock(&uuid_mutex
);
1638 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1639 mutex_lock(&root
->fs_info
->volume_mutex
);
1641 devices
= &root
->fs_info
->fs_devices
->devices
;
1643 * we have the volume lock, so we don't need the extra
1644 * device list mutex while reading the list here.
1646 list_for_each_entry(device
, devices
, dev_list
) {
1647 if (device
->bdev
== bdev
) {
1653 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1655 /* we can safely leave the fs_devices entry around */
1660 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1661 if (!device
->name
) {
1667 ret
= find_next_devid(root
, &device
->devid
);
1669 kfree(device
->name
);
1674 trans
= btrfs_start_transaction(root
, 0);
1675 if (IS_ERR(trans
)) {
1676 kfree(device
->name
);
1678 ret
= PTR_ERR(trans
);
1684 q
= bdev_get_queue(bdev
);
1685 if (blk_queue_discard(q
))
1686 device
->can_discard
= 1;
1687 device
->writeable
= 1;
1688 device
->work
.func
= pending_bios_fn
;
1689 generate_random_uuid(device
->uuid
);
1690 spin_lock_init(&device
->io_lock
);
1691 device
->generation
= trans
->transid
;
1692 device
->io_width
= root
->sectorsize
;
1693 device
->io_align
= root
->sectorsize
;
1694 device
->sector_size
= root
->sectorsize
;
1695 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1696 device
->disk_total_bytes
= device
->total_bytes
;
1697 device
->dev_root
= root
->fs_info
->dev_root
;
1698 device
->bdev
= bdev
;
1699 device
->in_fs_metadata
= 1;
1700 device
->mode
= FMODE_EXCL
;
1701 set_blocksize(device
->bdev
, 4096);
1704 sb
->s_flags
&= ~MS_RDONLY
;
1705 ret
= btrfs_prepare_sprout(trans
, root
);
1709 device
->fs_devices
= root
->fs_info
->fs_devices
;
1712 * we don't want write_supers to jump in here with our device
1715 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1716 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1717 list_add(&device
->dev_alloc_list
,
1718 &root
->fs_info
->fs_devices
->alloc_list
);
1719 root
->fs_info
->fs_devices
->num_devices
++;
1720 root
->fs_info
->fs_devices
->open_devices
++;
1721 root
->fs_info
->fs_devices
->rw_devices
++;
1722 if (device
->can_discard
)
1723 root
->fs_info
->fs_devices
->num_can_discard
++;
1724 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1726 spin_lock(&root
->fs_info
->free_chunk_lock
);
1727 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1728 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1730 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1731 root
->fs_info
->fs_devices
->rotating
= 1;
1733 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1734 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1735 total_bytes
+ device
->total_bytes
);
1737 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1738 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1740 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1743 ret
= init_first_rw_device(trans
, root
, device
);
1745 ret
= btrfs_finish_sprout(trans
, root
);
1748 ret
= btrfs_add_device(trans
, root
, device
);
1752 * we've got more storage, clear any full flags on the space
1755 btrfs_clear_space_info_full(root
->fs_info
);
1757 unlock_chunks(root
);
1758 btrfs_commit_transaction(trans
, root
);
1761 mutex_unlock(&uuid_mutex
);
1762 up_write(&sb
->s_umount
);
1764 ret
= btrfs_relocate_sys_chunks(root
);
1768 mutex_unlock(&root
->fs_info
->volume_mutex
);
1771 blkdev_put(bdev
, FMODE_EXCL
);
1773 mutex_unlock(&uuid_mutex
);
1774 up_write(&sb
->s_umount
);
1779 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1780 struct btrfs_device
*device
)
1783 struct btrfs_path
*path
;
1784 struct btrfs_root
*root
;
1785 struct btrfs_dev_item
*dev_item
;
1786 struct extent_buffer
*leaf
;
1787 struct btrfs_key key
;
1789 root
= device
->dev_root
->fs_info
->chunk_root
;
1791 path
= btrfs_alloc_path();
1795 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1796 key
.type
= BTRFS_DEV_ITEM_KEY
;
1797 key
.offset
= device
->devid
;
1799 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1808 leaf
= path
->nodes
[0];
1809 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1811 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1812 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1813 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1814 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1815 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1816 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1817 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1818 btrfs_mark_buffer_dirty(leaf
);
1821 btrfs_free_path(path
);
1825 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1826 struct btrfs_device
*device
, u64 new_size
)
1828 struct btrfs_super_block
*super_copy
=
1829 device
->dev_root
->fs_info
->super_copy
;
1830 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1831 u64 diff
= new_size
- device
->total_bytes
;
1833 if (!device
->writeable
)
1835 if (new_size
<= device
->total_bytes
)
1838 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1839 device
->fs_devices
->total_rw_bytes
+= diff
;
1841 device
->total_bytes
= new_size
;
1842 device
->disk_total_bytes
= new_size
;
1843 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1845 return btrfs_update_device(trans
, device
);
1848 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1849 struct btrfs_device
*device
, u64 new_size
)
1852 lock_chunks(device
->dev_root
);
1853 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1854 unlock_chunks(device
->dev_root
);
1858 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1859 struct btrfs_root
*root
,
1860 u64 chunk_tree
, u64 chunk_objectid
,
1864 struct btrfs_path
*path
;
1865 struct btrfs_key key
;
1867 root
= root
->fs_info
->chunk_root
;
1868 path
= btrfs_alloc_path();
1872 key
.objectid
= chunk_objectid
;
1873 key
.offset
= chunk_offset
;
1874 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1876 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1879 ret
= btrfs_del_item(trans
, root
, path
);
1881 btrfs_free_path(path
);
1885 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1888 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1889 struct btrfs_disk_key
*disk_key
;
1890 struct btrfs_chunk
*chunk
;
1897 struct btrfs_key key
;
1899 array_size
= btrfs_super_sys_array_size(super_copy
);
1901 ptr
= super_copy
->sys_chunk_array
;
1904 while (cur
< array_size
) {
1905 disk_key
= (struct btrfs_disk_key
*)ptr
;
1906 btrfs_disk_key_to_cpu(&key
, disk_key
);
1908 len
= sizeof(*disk_key
);
1910 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1911 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1912 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1913 len
+= btrfs_chunk_item_size(num_stripes
);
1918 if (key
.objectid
== chunk_objectid
&&
1919 key
.offset
== chunk_offset
) {
1920 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1922 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1931 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1932 u64 chunk_tree
, u64 chunk_objectid
,
1935 struct extent_map_tree
*em_tree
;
1936 struct btrfs_root
*extent_root
;
1937 struct btrfs_trans_handle
*trans
;
1938 struct extent_map
*em
;
1939 struct map_lookup
*map
;
1943 root
= root
->fs_info
->chunk_root
;
1944 extent_root
= root
->fs_info
->extent_root
;
1945 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1947 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1951 /* step one, relocate all the extents inside this chunk */
1952 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1956 trans
= btrfs_start_transaction(root
, 0);
1957 BUG_ON(IS_ERR(trans
));
1962 * step two, delete the device extents and the
1963 * chunk tree entries
1965 read_lock(&em_tree
->lock
);
1966 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1967 read_unlock(&em_tree
->lock
);
1969 BUG_ON(em
->start
> chunk_offset
||
1970 em
->start
+ em
->len
< chunk_offset
);
1971 map
= (struct map_lookup
*)em
->bdev
;
1973 for (i
= 0; i
< map
->num_stripes
; i
++) {
1974 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1975 map
->stripes
[i
].physical
);
1978 if (map
->stripes
[i
].dev
) {
1979 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1983 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1988 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1990 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1991 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1995 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1998 write_lock(&em_tree
->lock
);
1999 remove_extent_mapping(em_tree
, em
);
2000 write_unlock(&em_tree
->lock
);
2005 /* once for the tree */
2006 free_extent_map(em
);
2008 free_extent_map(em
);
2010 unlock_chunks(root
);
2011 btrfs_end_transaction(trans
, root
);
2015 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2017 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2018 struct btrfs_path
*path
;
2019 struct extent_buffer
*leaf
;
2020 struct btrfs_chunk
*chunk
;
2021 struct btrfs_key key
;
2022 struct btrfs_key found_key
;
2023 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2025 bool retried
= false;
2029 path
= btrfs_alloc_path();
2034 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2035 key
.offset
= (u64
)-1;
2036 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2039 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2044 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2051 leaf
= path
->nodes
[0];
2052 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2054 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2055 struct btrfs_chunk
);
2056 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2057 btrfs_release_path(path
);
2059 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2060 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2069 if (found_key
.offset
== 0)
2071 key
.offset
= found_key
.offset
- 1;
2074 if (failed
&& !retried
) {
2078 } else if (failed
&& retried
) {
2083 btrfs_free_path(path
);
2087 static u64
div_factor(u64 num
, int factor
)
2096 int btrfs_balance(struct btrfs_root
*dev_root
)
2099 struct list_head
*devices
= &dev_root
->fs_info
->fs_devices
->devices
;
2100 struct btrfs_device
*device
;
2103 struct btrfs_path
*path
;
2104 struct btrfs_key key
;
2105 struct btrfs_root
*chunk_root
= dev_root
->fs_info
->chunk_root
;
2106 struct btrfs_trans_handle
*trans
;
2107 struct btrfs_key found_key
;
2109 if (dev_root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2112 if (!capable(CAP_SYS_ADMIN
))
2115 mutex_lock(&dev_root
->fs_info
->volume_mutex
);
2116 dev_root
= dev_root
->fs_info
->dev_root
;
2118 /* step one make some room on all the devices */
2119 list_for_each_entry(device
, devices
, dev_list
) {
2120 old_size
= device
->total_bytes
;
2121 size_to_free
= div_factor(old_size
, 1);
2122 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2123 if (!device
->writeable
||
2124 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2127 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2132 trans
= btrfs_start_transaction(dev_root
, 0);
2133 BUG_ON(IS_ERR(trans
));
2135 ret
= btrfs_grow_device(trans
, device
, old_size
);
2138 btrfs_end_transaction(trans
, dev_root
);
2141 /* step two, relocate all the chunks */
2142 path
= btrfs_alloc_path();
2147 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2148 key
.offset
= (u64
)-1;
2149 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2152 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2157 * this shouldn't happen, it means the last relocate
2163 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2164 BTRFS_CHUNK_ITEM_KEY
);
2168 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2170 if (found_key
.objectid
!= key
.objectid
)
2173 /* chunk zero is special */
2174 if (found_key
.offset
== 0)
2177 btrfs_release_path(path
);
2178 ret
= btrfs_relocate_chunk(chunk_root
,
2179 chunk_root
->root_key
.objectid
,
2182 if (ret
&& ret
!= -ENOSPC
)
2184 key
.offset
= found_key
.offset
- 1;
2188 btrfs_free_path(path
);
2189 mutex_unlock(&dev_root
->fs_info
->volume_mutex
);
2194 * shrinking a device means finding all of the device extents past
2195 * the new size, and then following the back refs to the chunks.
2196 * The chunk relocation code actually frees the device extent
2198 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2200 struct btrfs_trans_handle
*trans
;
2201 struct btrfs_root
*root
= device
->dev_root
;
2202 struct btrfs_dev_extent
*dev_extent
= NULL
;
2203 struct btrfs_path
*path
;
2211 bool retried
= false;
2212 struct extent_buffer
*l
;
2213 struct btrfs_key key
;
2214 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2215 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2216 u64 old_size
= device
->total_bytes
;
2217 u64 diff
= device
->total_bytes
- new_size
;
2219 if (new_size
>= device
->total_bytes
)
2222 path
= btrfs_alloc_path();
2230 device
->total_bytes
= new_size
;
2231 if (device
->writeable
) {
2232 device
->fs_devices
->total_rw_bytes
-= diff
;
2233 spin_lock(&root
->fs_info
->free_chunk_lock
);
2234 root
->fs_info
->free_chunk_space
-= diff
;
2235 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2237 unlock_chunks(root
);
2240 key
.objectid
= device
->devid
;
2241 key
.offset
= (u64
)-1;
2242 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2245 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2249 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2254 btrfs_release_path(path
);
2259 slot
= path
->slots
[0];
2260 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2262 if (key
.objectid
!= device
->devid
) {
2263 btrfs_release_path(path
);
2267 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2268 length
= btrfs_dev_extent_length(l
, dev_extent
);
2270 if (key
.offset
+ length
<= new_size
) {
2271 btrfs_release_path(path
);
2275 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2276 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2277 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2278 btrfs_release_path(path
);
2280 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2282 if (ret
&& ret
!= -ENOSPC
)
2289 if (failed
&& !retried
) {
2293 } else if (failed
&& retried
) {
2297 device
->total_bytes
= old_size
;
2298 if (device
->writeable
)
2299 device
->fs_devices
->total_rw_bytes
+= diff
;
2300 spin_lock(&root
->fs_info
->free_chunk_lock
);
2301 root
->fs_info
->free_chunk_space
+= diff
;
2302 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2303 unlock_chunks(root
);
2307 /* Shrinking succeeded, else we would be at "done". */
2308 trans
= btrfs_start_transaction(root
, 0);
2309 if (IS_ERR(trans
)) {
2310 ret
= PTR_ERR(trans
);
2316 device
->disk_total_bytes
= new_size
;
2317 /* Now btrfs_update_device() will change the on-disk size. */
2318 ret
= btrfs_update_device(trans
, device
);
2320 unlock_chunks(root
);
2321 btrfs_end_transaction(trans
, root
);
2324 WARN_ON(diff
> old_total
);
2325 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2326 unlock_chunks(root
);
2327 btrfs_end_transaction(trans
, root
);
2329 btrfs_free_path(path
);
2333 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2334 struct btrfs_root
*root
,
2335 struct btrfs_key
*key
,
2336 struct btrfs_chunk
*chunk
, int item_size
)
2338 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2339 struct btrfs_disk_key disk_key
;
2343 array_size
= btrfs_super_sys_array_size(super_copy
);
2344 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2347 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2348 btrfs_cpu_key_to_disk(&disk_key
, key
);
2349 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2350 ptr
+= sizeof(disk_key
);
2351 memcpy(ptr
, chunk
, item_size
);
2352 item_size
+= sizeof(disk_key
);
2353 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2358 * sort the devices in descending order by max_avail, total_avail
2360 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2362 const struct btrfs_device_info
*di_a
= a
;
2363 const struct btrfs_device_info
*di_b
= b
;
2365 if (di_a
->max_avail
> di_b
->max_avail
)
2367 if (di_a
->max_avail
< di_b
->max_avail
)
2369 if (di_a
->total_avail
> di_b
->total_avail
)
2371 if (di_a
->total_avail
< di_b
->total_avail
)
2376 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2377 struct btrfs_root
*extent_root
,
2378 struct map_lookup
**map_ret
,
2379 u64
*num_bytes_out
, u64
*stripe_size_out
,
2380 u64 start
, u64 type
)
2382 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2383 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2384 struct list_head
*cur
;
2385 struct map_lookup
*map
= NULL
;
2386 struct extent_map_tree
*em_tree
;
2387 struct extent_map
*em
;
2388 struct btrfs_device_info
*devices_info
= NULL
;
2390 int num_stripes
; /* total number of stripes to allocate */
2391 int sub_stripes
; /* sub_stripes info for map */
2392 int dev_stripes
; /* stripes per dev */
2393 int devs_max
; /* max devs to use */
2394 int devs_min
; /* min devs needed */
2395 int devs_increment
; /* ndevs has to be a multiple of this */
2396 int ncopies
; /* how many copies to data has */
2398 u64 max_stripe_size
;
2406 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
2407 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
2409 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
2412 if (list_empty(&fs_devices
->alloc_list
))
2419 devs_max
= 0; /* 0 == as many as possible */
2423 * define the properties of each RAID type.
2424 * FIXME: move this to a global table and use it in all RAID
2427 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
2431 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
2433 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
2438 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
2447 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
2448 max_stripe_size
= 1024 * 1024 * 1024;
2449 max_chunk_size
= 10 * max_stripe_size
;
2450 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
2451 max_stripe_size
= 256 * 1024 * 1024;
2452 max_chunk_size
= max_stripe_size
;
2453 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2454 max_stripe_size
= 8 * 1024 * 1024;
2455 max_chunk_size
= 2 * max_stripe_size
;
2457 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
2462 /* we don't want a chunk larger than 10% of writeable space */
2463 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
2466 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
2471 cur
= fs_devices
->alloc_list
.next
;
2474 * in the first pass through the devices list, we gather information
2475 * about the available holes on each device.
2478 while (cur
!= &fs_devices
->alloc_list
) {
2479 struct btrfs_device
*device
;
2483 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
2487 if (!device
->writeable
) {
2489 "btrfs: read-only device in alloc_list\n");
2494 if (!device
->in_fs_metadata
)
2497 if (device
->total_bytes
> device
->bytes_used
)
2498 total_avail
= device
->total_bytes
- device
->bytes_used
;
2502 /* If there is no space on this device, skip it. */
2503 if (total_avail
== 0)
2506 ret
= find_free_dev_extent(trans
, device
,
2507 max_stripe_size
* dev_stripes
,
2508 &dev_offset
, &max_avail
);
2509 if (ret
&& ret
!= -ENOSPC
)
2513 max_avail
= max_stripe_size
* dev_stripes
;
2515 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
2518 devices_info
[ndevs
].dev_offset
= dev_offset
;
2519 devices_info
[ndevs
].max_avail
= max_avail
;
2520 devices_info
[ndevs
].total_avail
= total_avail
;
2521 devices_info
[ndevs
].dev
= device
;
2526 * now sort the devices by hole size / available space
2528 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
2529 btrfs_cmp_device_info
, NULL
);
2531 /* round down to number of usable stripes */
2532 ndevs
-= ndevs
% devs_increment
;
2534 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
2539 if (devs_max
&& ndevs
> devs_max
)
2542 * the primary goal is to maximize the number of stripes, so use as many
2543 * devices as possible, even if the stripes are not maximum sized.
2545 stripe_size
= devices_info
[ndevs
-1].max_avail
;
2546 num_stripes
= ndevs
* dev_stripes
;
2548 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
2549 stripe_size
= max_chunk_size
* ncopies
;
2550 do_div(stripe_size
, num_stripes
);
2553 do_div(stripe_size
, dev_stripes
);
2554 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
2555 stripe_size
*= BTRFS_STRIPE_LEN
;
2557 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
2562 map
->num_stripes
= num_stripes
;
2564 for (i
= 0; i
< ndevs
; ++i
) {
2565 for (j
= 0; j
< dev_stripes
; ++j
) {
2566 int s
= i
* dev_stripes
+ j
;
2567 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
2568 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
2572 map
->sector_size
= extent_root
->sectorsize
;
2573 map
->stripe_len
= BTRFS_STRIPE_LEN
;
2574 map
->io_align
= BTRFS_STRIPE_LEN
;
2575 map
->io_width
= BTRFS_STRIPE_LEN
;
2577 map
->sub_stripes
= sub_stripes
;
2580 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
2582 *stripe_size_out
= stripe_size
;
2583 *num_bytes_out
= num_bytes
;
2585 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
2587 em
= alloc_extent_map();
2592 em
->bdev
= (struct block_device
*)map
;
2594 em
->len
= num_bytes
;
2595 em
->block_start
= 0;
2596 em
->block_len
= em
->len
;
2598 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
2599 write_lock(&em_tree
->lock
);
2600 ret
= add_extent_mapping(em_tree
, em
);
2601 write_unlock(&em_tree
->lock
);
2603 free_extent_map(em
);
2605 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
2606 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2610 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2611 struct btrfs_device
*device
;
2614 device
= map
->stripes
[i
].dev
;
2615 dev_offset
= map
->stripes
[i
].physical
;
2617 ret
= btrfs_alloc_dev_extent(trans
, device
,
2618 info
->chunk_root
->root_key
.objectid
,
2619 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2620 start
, dev_offset
, stripe_size
);
2624 kfree(devices_info
);
2629 kfree(devices_info
);
2633 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
2634 struct btrfs_root
*extent_root
,
2635 struct map_lookup
*map
, u64 chunk_offset
,
2636 u64 chunk_size
, u64 stripe_size
)
2639 struct btrfs_key key
;
2640 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2641 struct btrfs_device
*device
;
2642 struct btrfs_chunk
*chunk
;
2643 struct btrfs_stripe
*stripe
;
2644 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
2648 chunk
= kzalloc(item_size
, GFP_NOFS
);
2653 while (index
< map
->num_stripes
) {
2654 device
= map
->stripes
[index
].dev
;
2655 device
->bytes_used
+= stripe_size
;
2656 ret
= btrfs_update_device(trans
, device
);
2661 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
2662 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
2664 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
2667 stripe
= &chunk
->stripe
;
2668 while (index
< map
->num_stripes
) {
2669 device
= map
->stripes
[index
].dev
;
2670 dev_offset
= map
->stripes
[index
].physical
;
2672 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
2673 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
2674 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
2679 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
2680 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
2681 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
2682 btrfs_set_stack_chunk_type(chunk
, map
->type
);
2683 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
2684 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
2685 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
2686 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
2687 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
2689 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2690 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2691 key
.offset
= chunk_offset
;
2693 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
2696 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2697 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
2707 * Chunk allocation falls into two parts. The first part does works
2708 * that make the new allocated chunk useable, but not do any operation
2709 * that modifies the chunk tree. The second part does the works that
2710 * require modifying the chunk tree. This division is important for the
2711 * bootstrap process of adding storage to a seed btrfs.
2713 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2714 struct btrfs_root
*extent_root
, u64 type
)
2719 struct map_lookup
*map
;
2720 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
2723 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
2728 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2729 &stripe_size
, chunk_offset
, type
);
2733 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2734 chunk_size
, stripe_size
);
2739 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
2740 struct btrfs_root
*root
,
2741 struct btrfs_device
*device
)
2744 u64 sys_chunk_offset
;
2748 u64 sys_stripe_size
;
2750 struct map_lookup
*map
;
2751 struct map_lookup
*sys_map
;
2752 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2753 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
2756 ret
= find_next_chunk(fs_info
->chunk_root
,
2757 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
2761 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
2762 (fs_info
->metadata_alloc_profile
&
2763 fs_info
->avail_metadata_alloc_bits
);
2764 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2766 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
2767 &stripe_size
, chunk_offset
, alloc_profile
);
2770 sys_chunk_offset
= chunk_offset
+ chunk_size
;
2772 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
2773 (fs_info
->system_alloc_profile
&
2774 fs_info
->avail_system_alloc_bits
);
2775 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
2777 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
2778 &sys_chunk_size
, &sys_stripe_size
,
2779 sys_chunk_offset
, alloc_profile
);
2782 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
2786 * Modifying chunk tree needs allocating new blocks from both
2787 * system block group and metadata block group. So we only can
2788 * do operations require modifying the chunk tree after both
2789 * block groups were created.
2791 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
2792 chunk_size
, stripe_size
);
2795 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
2796 sys_chunk_offset
, sys_chunk_size
,
2802 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
2804 struct extent_map
*em
;
2805 struct map_lookup
*map
;
2806 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
2810 read_lock(&map_tree
->map_tree
.lock
);
2811 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2812 read_unlock(&map_tree
->map_tree
.lock
);
2816 if (btrfs_test_opt(root
, DEGRADED
)) {
2817 free_extent_map(em
);
2821 map
= (struct map_lookup
*)em
->bdev
;
2822 for (i
= 0; i
< map
->num_stripes
; i
++) {
2823 if (!map
->stripes
[i
].dev
->writeable
) {
2828 free_extent_map(em
);
2832 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
2834 extent_map_tree_init(&tree
->map_tree
);
2837 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
2839 struct extent_map
*em
;
2842 write_lock(&tree
->map_tree
.lock
);
2843 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
2845 remove_extent_mapping(&tree
->map_tree
, em
);
2846 write_unlock(&tree
->map_tree
.lock
);
2851 free_extent_map(em
);
2852 /* once for the tree */
2853 free_extent_map(em
);
2857 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
2859 struct extent_map
*em
;
2860 struct map_lookup
*map
;
2861 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2864 read_lock(&em_tree
->lock
);
2865 em
= lookup_extent_mapping(em_tree
, logical
, len
);
2866 read_unlock(&em_tree
->lock
);
2869 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2870 map
= (struct map_lookup
*)em
->bdev
;
2871 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
2872 ret
= map
->num_stripes
;
2873 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
2874 ret
= map
->sub_stripes
;
2877 free_extent_map(em
);
2881 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
2885 if (map
->stripes
[optimal
].dev
->bdev
)
2887 for (i
= first
; i
< first
+ num
; i
++) {
2888 if (map
->stripes
[i
].dev
->bdev
)
2891 /* we couldn't find one that doesn't fail. Just return something
2892 * and the io error handling code will clean up eventually
2897 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
2898 u64 logical
, u64
*length
,
2899 struct btrfs_bio
**bbio_ret
,
2902 struct extent_map
*em
;
2903 struct map_lookup
*map
;
2904 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
2907 u64 stripe_end_offset
;
2911 int stripes_allocated
= 8;
2912 int stripes_required
= 1;
2917 struct btrfs_bio
*bbio
= NULL
;
2919 if (bbio_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
2920 stripes_allocated
= 1;
2923 bbio
= kzalloc(btrfs_bio_size(stripes_allocated
),
2928 atomic_set(&bbio
->error
, 0);
2931 read_lock(&em_tree
->lock
);
2932 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
2933 read_unlock(&em_tree
->lock
);
2936 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
2937 (unsigned long long)logical
,
2938 (unsigned long long)*length
);
2942 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
2943 map
= (struct map_lookup
*)em
->bdev
;
2944 offset
= logical
- em
->start
;
2946 if (mirror_num
> map
->num_stripes
)
2949 /* if our btrfs_bio struct is too small, back off and try again */
2950 if (rw
& REQ_WRITE
) {
2951 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
2952 BTRFS_BLOCK_GROUP_DUP
)) {
2953 stripes_required
= map
->num_stripes
;
2955 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
2956 stripes_required
= map
->sub_stripes
;
2960 if (rw
& REQ_DISCARD
) {
2961 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2962 BTRFS_BLOCK_GROUP_RAID1
|
2963 BTRFS_BLOCK_GROUP_DUP
|
2964 BTRFS_BLOCK_GROUP_RAID10
)) {
2965 stripes_required
= map
->num_stripes
;
2968 if (bbio_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
2969 stripes_allocated
< stripes_required
) {
2970 stripes_allocated
= map
->num_stripes
;
2971 free_extent_map(em
);
2977 * stripe_nr counts the total number of stripes we have to stride
2978 * to get to this block
2980 do_div(stripe_nr
, map
->stripe_len
);
2982 stripe_offset
= stripe_nr
* map
->stripe_len
;
2983 BUG_ON(offset
< stripe_offset
);
2985 /* stripe_offset is the offset of this block in its stripe*/
2986 stripe_offset
= offset
- stripe_offset
;
2988 if (rw
& REQ_DISCARD
)
2989 *length
= min_t(u64
, em
->len
- offset
, *length
);
2990 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
2991 BTRFS_BLOCK_GROUP_RAID1
|
2992 BTRFS_BLOCK_GROUP_RAID10
|
2993 BTRFS_BLOCK_GROUP_DUP
)) {
2994 /* we limit the length of each bio to what fits in a stripe */
2995 *length
= min_t(u64
, em
->len
- offset
,
2996 map
->stripe_len
- stripe_offset
);
2998 *length
= em
->len
- offset
;
3006 stripe_nr_orig
= stripe_nr
;
3007 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3008 (~(map
->stripe_len
- 1));
3009 do_div(stripe_nr_end
, map
->stripe_len
);
3010 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3012 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3013 if (rw
& REQ_DISCARD
)
3014 num_stripes
= min_t(u64
, map
->num_stripes
,
3015 stripe_nr_end
- stripe_nr_orig
);
3016 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3017 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3018 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3019 num_stripes
= map
->num_stripes
;
3020 else if (mirror_num
)
3021 stripe_index
= mirror_num
- 1;
3023 stripe_index
= find_live_mirror(map
, 0,
3025 current
->pid
% map
->num_stripes
);
3026 mirror_num
= stripe_index
+ 1;
3029 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3030 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3031 num_stripes
= map
->num_stripes
;
3032 } else if (mirror_num
) {
3033 stripe_index
= mirror_num
- 1;
3038 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3039 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3041 stripe_index
= do_div(stripe_nr
, factor
);
3042 stripe_index
*= map
->sub_stripes
;
3045 num_stripes
= map
->sub_stripes
;
3046 else if (rw
& REQ_DISCARD
)
3047 num_stripes
= min_t(u64
, map
->sub_stripes
*
3048 (stripe_nr_end
- stripe_nr_orig
),
3050 else if (mirror_num
)
3051 stripe_index
+= mirror_num
- 1;
3053 stripe_index
= find_live_mirror(map
, stripe_index
,
3054 map
->sub_stripes
, stripe_index
+
3055 current
->pid
% map
->sub_stripes
);
3056 mirror_num
= stripe_index
+ 1;
3060 * after this do_div call, stripe_nr is the number of stripes
3061 * on this device we have to walk to find the data, and
3062 * stripe_index is the number of our device in the stripe array
3064 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3065 mirror_num
= stripe_index
+ 1;
3067 BUG_ON(stripe_index
>= map
->num_stripes
);
3069 if (rw
& REQ_DISCARD
) {
3070 for (i
= 0; i
< num_stripes
; i
++) {
3071 bbio
->stripes
[i
].physical
=
3072 map
->stripes
[stripe_index
].physical
+
3073 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3074 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3076 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3078 u32 last_stripe
= 0;
3081 div_u64_rem(stripe_nr_end
- 1,
3085 for (j
= 0; j
< map
->num_stripes
; j
++) {
3088 div_u64_rem(stripe_nr_end
- 1 - j
,
3089 map
->num_stripes
, &test
);
3090 if (test
== stripe_index
)
3093 stripes
= stripe_nr_end
- 1 - j
;
3094 do_div(stripes
, map
->num_stripes
);
3095 bbio
->stripes
[i
].length
= map
->stripe_len
*
3096 (stripes
- stripe_nr
+ 1);
3099 bbio
->stripes
[i
].length
-=
3103 if (stripe_index
== last_stripe
)
3104 bbio
->stripes
[i
].length
-=
3106 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3109 int factor
= map
->num_stripes
/
3111 u32 last_stripe
= 0;
3113 div_u64_rem(stripe_nr_end
- 1,
3114 factor
, &last_stripe
);
3115 last_stripe
*= map
->sub_stripes
;
3117 for (j
= 0; j
< factor
; j
++) {
3120 div_u64_rem(stripe_nr_end
- 1 - j
,
3124 stripe_index
/ map
->sub_stripes
)
3127 stripes
= stripe_nr_end
- 1 - j
;
3128 do_div(stripes
, factor
);
3129 bbio
->stripes
[i
].length
= map
->stripe_len
*
3130 (stripes
- stripe_nr
+ 1);
3132 if (i
< map
->sub_stripes
) {
3133 bbio
->stripes
[i
].length
-=
3135 if (i
== map
->sub_stripes
- 1)
3138 if (stripe_index
>= last_stripe
&&
3139 stripe_index
<= (last_stripe
+
3140 map
->sub_stripes
- 1)) {
3141 bbio
->stripes
[i
].length
-=
3145 bbio
->stripes
[i
].length
= *length
;
3148 if (stripe_index
== map
->num_stripes
) {
3149 /* This could only happen for RAID0/10 */
3155 for (i
= 0; i
< num_stripes
; i
++) {
3156 bbio
->stripes
[i
].physical
=
3157 map
->stripes
[stripe_index
].physical
+
3159 stripe_nr
* map
->stripe_len
;
3160 bbio
->stripes
[i
].dev
=
3161 map
->stripes
[stripe_index
].dev
;
3167 bbio
->num_stripes
= num_stripes
;
3168 bbio
->max_errors
= max_errors
;
3169 bbio
->mirror_num
= mirror_num
;
3172 free_extent_map(em
);
3176 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3177 u64 logical
, u64
*length
,
3178 struct btrfs_bio
**bbio_ret
, int mirror_num
)
3180 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
3184 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3185 u64 chunk_start
, u64 physical
, u64 devid
,
3186 u64
**logical
, int *naddrs
, int *stripe_len
)
3188 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3189 struct extent_map
*em
;
3190 struct map_lookup
*map
;
3197 read_lock(&em_tree
->lock
);
3198 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3199 read_unlock(&em_tree
->lock
);
3201 BUG_ON(!em
|| em
->start
!= chunk_start
);
3202 map
= (struct map_lookup
*)em
->bdev
;
3205 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3206 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3207 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3208 do_div(length
, map
->num_stripes
);
3210 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3213 for (i
= 0; i
< map
->num_stripes
; i
++) {
3214 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3216 if (map
->stripes
[i
].physical
> physical
||
3217 map
->stripes
[i
].physical
+ length
<= physical
)
3220 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3221 do_div(stripe_nr
, map
->stripe_len
);
3223 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3224 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3225 do_div(stripe_nr
, map
->sub_stripes
);
3226 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3227 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3229 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3230 WARN_ON(nr
>= map
->num_stripes
);
3231 for (j
= 0; j
< nr
; j
++) {
3232 if (buf
[j
] == bytenr
)
3236 WARN_ON(nr
>= map
->num_stripes
);
3243 *stripe_len
= map
->stripe_len
;
3245 free_extent_map(em
);
3249 static void btrfs_end_bio(struct bio
*bio
, int err
)
3251 struct btrfs_bio
*bbio
= bio
->bi_private
;
3252 int is_orig_bio
= 0;
3255 atomic_inc(&bbio
->error
);
3257 if (bio
== bbio
->orig_bio
)
3260 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
3263 bio
= bbio
->orig_bio
;
3265 bio
->bi_private
= bbio
->private;
3266 bio
->bi_end_io
= bbio
->end_io
;
3267 bio
->bi_bdev
= (struct block_device
*)
3268 (unsigned long)bbio
->mirror_num
;
3269 /* only send an error to the higher layers if it is
3270 * beyond the tolerance of the multi-bio
3272 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
3276 * this bio is actually up to date, we didn't
3277 * go over the max number of errors
3279 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3284 bio_endio(bio
, err
);
3285 } else if (!is_orig_bio
) {
3290 struct async_sched
{
3293 struct btrfs_fs_info
*info
;
3294 struct btrfs_work work
;
3298 * see run_scheduled_bios for a description of why bios are collected for
3301 * This will add one bio to the pending list for a device and make sure
3302 * the work struct is scheduled.
3304 static noinline
int schedule_bio(struct btrfs_root
*root
,
3305 struct btrfs_device
*device
,
3306 int rw
, struct bio
*bio
)
3308 int should_queue
= 1;
3309 struct btrfs_pending_bios
*pending_bios
;
3311 /* don't bother with additional async steps for reads, right now */
3312 if (!(rw
& REQ_WRITE
)) {
3314 submit_bio(rw
, bio
);
3320 * nr_async_bios allows us to reliably return congestion to the
3321 * higher layers. Otherwise, the async bio makes it appear we have
3322 * made progress against dirty pages when we've really just put it
3323 * on a queue for later
3325 atomic_inc(&root
->fs_info
->nr_async_bios
);
3326 WARN_ON(bio
->bi_next
);
3327 bio
->bi_next
= NULL
;
3330 spin_lock(&device
->io_lock
);
3331 if (bio
->bi_rw
& REQ_SYNC
)
3332 pending_bios
= &device
->pending_sync_bios
;
3334 pending_bios
= &device
->pending_bios
;
3336 if (pending_bios
->tail
)
3337 pending_bios
->tail
->bi_next
= bio
;
3339 pending_bios
->tail
= bio
;
3340 if (!pending_bios
->head
)
3341 pending_bios
->head
= bio
;
3342 if (device
->running_pending
)
3345 spin_unlock(&device
->io_lock
);
3348 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3353 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3354 int mirror_num
, int async_submit
)
3356 struct btrfs_mapping_tree
*map_tree
;
3357 struct btrfs_device
*dev
;
3358 struct bio
*first_bio
= bio
;
3359 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3365 struct btrfs_bio
*bbio
= NULL
;
3367 length
= bio
->bi_size
;
3368 map_tree
= &root
->fs_info
->mapping_tree
;
3369 map_length
= length
;
3371 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
3375 total_devs
= bbio
->num_stripes
;
3376 if (map_length
< length
) {
3377 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3378 "len %llu\n", (unsigned long long)logical
,
3379 (unsigned long long)length
,
3380 (unsigned long long)map_length
);
3384 bbio
->orig_bio
= first_bio
;
3385 bbio
->private = first_bio
->bi_private
;
3386 bbio
->end_io
= first_bio
->bi_end_io
;
3387 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
3389 while (dev_nr
< total_devs
) {
3390 if (dev_nr
< total_devs
- 1) {
3391 bio
= bio_clone(first_bio
, GFP_NOFS
);
3396 bio
->bi_private
= bbio
;
3397 bio
->bi_end_io
= btrfs_end_bio
;
3398 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
3399 dev
= bbio
->stripes
[dev_nr
].dev
;
3400 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3401 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3402 "(%s id %llu), size=%u\n", rw
,
3403 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
3404 dev
->name
, dev
->devid
, bio
->bi_size
);
3405 bio
->bi_bdev
= dev
->bdev
;
3407 schedule_bio(root
, dev
, rw
, bio
);
3409 submit_bio(rw
, bio
);
3411 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3412 bio
->bi_sector
= logical
>> 9;
3413 bio_endio(bio
, -EIO
);
3420 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
3423 struct btrfs_device
*device
;
3424 struct btrfs_fs_devices
*cur_devices
;
3426 cur_devices
= root
->fs_info
->fs_devices
;
3427 while (cur_devices
) {
3429 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3430 device
= __find_device(&cur_devices
->devices
,
3435 cur_devices
= cur_devices
->seed
;
3440 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
3441 u64 devid
, u8
*dev_uuid
)
3443 struct btrfs_device
*device
;
3444 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
3446 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
3449 list_add(&device
->dev_list
,
3450 &fs_devices
->devices
);
3451 device
->dev_root
= root
->fs_info
->dev_root
;
3452 device
->devid
= devid
;
3453 device
->work
.func
= pending_bios_fn
;
3454 device
->fs_devices
= fs_devices
;
3455 device
->missing
= 1;
3456 fs_devices
->num_devices
++;
3457 fs_devices
->missing_devices
++;
3458 spin_lock_init(&device
->io_lock
);
3459 INIT_LIST_HEAD(&device
->dev_alloc_list
);
3460 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
3464 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
3465 struct extent_buffer
*leaf
,
3466 struct btrfs_chunk
*chunk
)
3468 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3469 struct map_lookup
*map
;
3470 struct extent_map
*em
;
3474 u8 uuid
[BTRFS_UUID_SIZE
];
3479 logical
= key
->offset
;
3480 length
= btrfs_chunk_length(leaf
, chunk
);
3482 read_lock(&map_tree
->map_tree
.lock
);
3483 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
3484 read_unlock(&map_tree
->map_tree
.lock
);
3486 /* already mapped? */
3487 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
3488 free_extent_map(em
);
3491 free_extent_map(em
);
3494 em
= alloc_extent_map();
3497 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3498 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3500 free_extent_map(em
);
3504 em
->bdev
= (struct block_device
*)map
;
3505 em
->start
= logical
;
3507 em
->block_start
= 0;
3508 em
->block_len
= em
->len
;
3510 map
->num_stripes
= num_stripes
;
3511 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
3512 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
3513 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
3514 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
3515 map
->type
= btrfs_chunk_type(leaf
, chunk
);
3516 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
3517 for (i
= 0; i
< num_stripes
; i
++) {
3518 map
->stripes
[i
].physical
=
3519 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
3520 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
3521 read_extent_buffer(leaf
, uuid
, (unsigned long)
3522 btrfs_stripe_dev_uuid_nr(chunk
, i
),
3524 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
3526 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
3528 free_extent_map(em
);
3531 if (!map
->stripes
[i
].dev
) {
3532 map
->stripes
[i
].dev
=
3533 add_missing_dev(root
, devid
, uuid
);
3534 if (!map
->stripes
[i
].dev
) {
3536 free_extent_map(em
);
3540 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
3543 write_lock(&map_tree
->map_tree
.lock
);
3544 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
3545 write_unlock(&map_tree
->map_tree
.lock
);
3547 free_extent_map(em
);
3552 static int fill_device_from_item(struct extent_buffer
*leaf
,
3553 struct btrfs_dev_item
*dev_item
,
3554 struct btrfs_device
*device
)
3558 device
->devid
= btrfs_device_id(leaf
, dev_item
);
3559 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
3560 device
->total_bytes
= device
->disk_total_bytes
;
3561 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
3562 device
->type
= btrfs_device_type(leaf
, dev_item
);
3563 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
3564 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
3565 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
3567 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
3568 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
3573 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
3575 struct btrfs_fs_devices
*fs_devices
;
3578 mutex_lock(&uuid_mutex
);
3580 fs_devices
= root
->fs_info
->fs_devices
->seed
;
3581 while (fs_devices
) {
3582 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
3586 fs_devices
= fs_devices
->seed
;
3589 fs_devices
= find_fsid(fsid
);
3595 fs_devices
= clone_fs_devices(fs_devices
);
3596 if (IS_ERR(fs_devices
)) {
3597 ret
= PTR_ERR(fs_devices
);
3601 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
3602 root
->fs_info
->bdev_holder
);
3606 if (!fs_devices
->seeding
) {
3607 __btrfs_close_devices(fs_devices
);
3608 free_fs_devices(fs_devices
);
3613 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
3614 root
->fs_info
->fs_devices
->seed
= fs_devices
;
3616 mutex_unlock(&uuid_mutex
);
3620 static int read_one_dev(struct btrfs_root
*root
,
3621 struct extent_buffer
*leaf
,
3622 struct btrfs_dev_item
*dev_item
)
3624 struct btrfs_device
*device
;
3627 u8 fs_uuid
[BTRFS_UUID_SIZE
];
3628 u8 dev_uuid
[BTRFS_UUID_SIZE
];
3630 devid
= btrfs_device_id(leaf
, dev_item
);
3631 read_extent_buffer(leaf
, dev_uuid
,
3632 (unsigned long)btrfs_device_uuid(dev_item
),
3634 read_extent_buffer(leaf
, fs_uuid
,
3635 (unsigned long)btrfs_device_fsid(dev_item
),
3638 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
3639 ret
= open_seed_devices(root
, fs_uuid
);
3640 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
3644 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
3645 if (!device
|| !device
->bdev
) {
3646 if (!btrfs_test_opt(root
, DEGRADED
))
3650 printk(KERN_WARNING
"warning devid %llu missing\n",
3651 (unsigned long long)devid
);
3652 device
= add_missing_dev(root
, devid
, dev_uuid
);
3655 } else if (!device
->missing
) {
3657 * this happens when a device that was properly setup
3658 * in the device info lists suddenly goes bad.
3659 * device->bdev is NULL, and so we have to set
3660 * device->missing to one here
3662 root
->fs_info
->fs_devices
->missing_devices
++;
3663 device
->missing
= 1;
3667 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
3668 BUG_ON(device
->writeable
);
3669 if (device
->generation
!=
3670 btrfs_device_generation(leaf
, dev_item
))
3674 fill_device_from_item(leaf
, dev_item
, device
);
3675 device
->dev_root
= root
->fs_info
->dev_root
;
3676 device
->in_fs_metadata
= 1;
3677 if (device
->writeable
) {
3678 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
3679 spin_lock(&root
->fs_info
->free_chunk_lock
);
3680 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
3682 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3688 int btrfs_read_sys_array(struct btrfs_root
*root
)
3690 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3691 struct extent_buffer
*sb
;
3692 struct btrfs_disk_key
*disk_key
;
3693 struct btrfs_chunk
*chunk
;
3695 unsigned long sb_ptr
;
3701 struct btrfs_key key
;
3703 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
3704 BTRFS_SUPER_INFO_SIZE
);
3707 btrfs_set_buffer_uptodate(sb
);
3708 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
3710 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
3711 array_size
= btrfs_super_sys_array_size(super_copy
);
3713 ptr
= super_copy
->sys_chunk_array
;
3714 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
3717 while (cur
< array_size
) {
3718 disk_key
= (struct btrfs_disk_key
*)ptr
;
3719 btrfs_disk_key_to_cpu(&key
, disk_key
);
3721 len
= sizeof(*disk_key
); ptr
+= len
;
3725 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3726 chunk
= (struct btrfs_chunk
*)sb_ptr
;
3727 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
3730 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
3731 len
= btrfs_chunk_item_size(num_stripes
);
3740 free_extent_buffer(sb
);
3744 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
3746 struct btrfs_path
*path
;
3747 struct extent_buffer
*leaf
;
3748 struct btrfs_key key
;
3749 struct btrfs_key found_key
;
3753 root
= root
->fs_info
->chunk_root
;
3755 path
= btrfs_alloc_path();
3759 /* first we search for all of the device items, and then we
3760 * read in all of the chunk items. This way we can create chunk
3761 * mappings that reference all of the devices that are afound
3763 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
3767 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3771 leaf
= path
->nodes
[0];
3772 slot
= path
->slots
[0];
3773 if (slot
>= btrfs_header_nritems(leaf
)) {
3774 ret
= btrfs_next_leaf(root
, path
);
3781 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3782 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3783 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
3785 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
3786 struct btrfs_dev_item
*dev_item
;
3787 dev_item
= btrfs_item_ptr(leaf
, slot
,
3788 struct btrfs_dev_item
);
3789 ret
= read_one_dev(root
, leaf
, dev_item
);
3793 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
3794 struct btrfs_chunk
*chunk
;
3795 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3796 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
3802 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
3804 btrfs_release_path(path
);
3809 btrfs_free_path(path
);