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"
37 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
38 struct btrfs_root
*root
,
39 struct btrfs_device
*device
);
40 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
42 static DEFINE_MUTEX(uuid_mutex
);
43 static LIST_HEAD(fs_uuids
);
45 static void lock_chunks(struct btrfs_root
*root
)
47 mutex_lock(&root
->fs_info
->chunk_mutex
);
50 static void unlock_chunks(struct btrfs_root
*root
)
52 mutex_unlock(&root
->fs_info
->chunk_mutex
);
55 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
57 struct btrfs_device
*device
;
58 WARN_ON(fs_devices
->opened
);
59 while (!list_empty(&fs_devices
->devices
)) {
60 device
= list_entry(fs_devices
->devices
.next
,
61 struct btrfs_device
, dev_list
);
62 list_del(&device
->dev_list
);
69 int btrfs_cleanup_fs_uuids(void)
71 struct btrfs_fs_devices
*fs_devices
;
73 while (!list_empty(&fs_uuids
)) {
74 fs_devices
= list_entry(fs_uuids
.next
,
75 struct btrfs_fs_devices
, list
);
76 list_del(&fs_devices
->list
);
77 free_fs_devices(fs_devices
);
82 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
85 struct btrfs_device
*dev
;
87 list_for_each_entry(dev
, head
, dev_list
) {
88 if (dev
->devid
== devid
&&
89 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
96 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
98 struct btrfs_fs_devices
*fs_devices
;
100 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
101 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
107 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
108 struct bio
*head
, struct bio
*tail
)
111 struct bio
*old_head
;
113 old_head
= pending_bios
->head
;
114 pending_bios
->head
= head
;
115 if (pending_bios
->tail
)
116 tail
->bi_next
= old_head
;
118 pending_bios
->tail
= tail
;
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
132 static noinline
int run_scheduled_bios(struct btrfs_device
*device
)
135 struct backing_dev_info
*bdi
;
136 struct btrfs_fs_info
*fs_info
;
137 struct btrfs_pending_bios
*pending_bios
;
141 unsigned long num_run
;
142 unsigned long batch_run
= 0;
144 unsigned long last_waited
= 0;
146 int sync_pending
= 0;
147 struct blk_plug plug
;
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
155 blk_start_plug(&plug
);
157 bdi
= blk_get_backing_dev_info(device
->bdev
);
158 fs_info
= device
->dev_root
->fs_info
;
159 limit
= btrfs_async_submit_limit(fs_info
);
160 limit
= limit
* 2 / 3;
163 spin_lock(&device
->io_lock
);
168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
173 if (!force_reg
&& device
->pending_sync_bios
.head
) {
174 pending_bios
= &device
->pending_sync_bios
;
177 pending_bios
= &device
->pending_bios
;
181 pending
= pending_bios
->head
;
182 tail
= pending_bios
->tail
;
183 WARN_ON(pending
&& !tail
);
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
190 * device->running_pending is used to synchronize with the
193 if (device
->pending_sync_bios
.head
== NULL
&&
194 device
->pending_bios
.head
== NULL
) {
196 device
->running_pending
= 0;
199 device
->running_pending
= 1;
202 pending_bios
->head
= NULL
;
203 pending_bios
->tail
= NULL
;
205 spin_unlock(&device
->io_lock
);
210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
214 pending_bios
!= &device
->pending_sync_bios
&&
215 device
->pending_sync_bios
.head
) ||
216 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
217 device
->pending_bios
.head
)) {
218 spin_lock(&device
->io_lock
);
219 requeue_list(pending_bios
, pending
, tail
);
224 pending
= pending
->bi_next
;
226 atomic_dec(&fs_info
->nr_async_bios
);
228 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
229 waitqueue_active(&fs_info
->async_submit_wait
))
230 wake_up(&fs_info
->async_submit_wait
);
232 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
242 if (pending_bios
== &device
->pending_sync_bios
) {
244 } else if (sync_pending
) {
245 blk_finish_plug(&plug
);
246 blk_start_plug(&plug
);
250 submit_bio(cur
->bi_rw
, cur
);
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
261 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
262 fs_info
->fs_devices
->open_devices
> 1) {
263 struct io_context
*ioc
;
265 ioc
= current
->io_context
;
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
276 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
277 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
279 ioc
->last_waited
== last_waited
)) {
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
286 last_waited
= ioc
->last_waited
;
291 spin_lock(&device
->io_lock
);
292 requeue_list(pending_bios
, pending
, tail
);
293 device
->running_pending
= 1;
295 spin_unlock(&device
->io_lock
);
296 btrfs_requeue_work(&device
->work
);
299 /* unplug every 64 requests just for good measure */
300 if (batch_run
% 64 == 0) {
301 blk_finish_plug(&plug
);
302 blk_start_plug(&plug
);
311 spin_lock(&device
->io_lock
);
312 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
314 spin_unlock(&device
->io_lock
);
317 blk_finish_plug(&plug
);
321 static void pending_bios_fn(struct btrfs_work
*work
)
323 struct btrfs_device
*device
;
325 device
= container_of(work
, struct btrfs_device
, work
);
326 run_scheduled_bios(device
);
329 static noinline
int device_list_add(const char *path
,
330 struct btrfs_super_block
*disk_super
,
331 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
333 struct btrfs_device
*device
;
334 struct btrfs_fs_devices
*fs_devices
;
335 u64 found_transid
= btrfs_super_generation(disk_super
);
338 fs_devices
= find_fsid(disk_super
->fsid
);
340 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
343 INIT_LIST_HEAD(&fs_devices
->devices
);
344 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
345 list_add(&fs_devices
->list
, &fs_uuids
);
346 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
347 fs_devices
->latest_devid
= devid
;
348 fs_devices
->latest_trans
= found_transid
;
349 mutex_init(&fs_devices
->device_list_mutex
);
352 device
= __find_device(&fs_devices
->devices
, devid
,
353 disk_super
->dev_item
.uuid
);
356 if (fs_devices
->opened
)
359 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
361 /* we can safely leave the fs_devices entry around */
364 device
->devid
= devid
;
365 device
->work
.func
= pending_bios_fn
;
366 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
368 spin_lock_init(&device
->io_lock
);
369 device
->name
= kstrdup(path
, GFP_NOFS
);
374 INIT_LIST_HEAD(&device
->dev_alloc_list
);
376 /* init readahead state */
377 spin_lock_init(&device
->reada_lock
);
378 device
->reada_curr_zone
= NULL
;
379 atomic_set(&device
->reada_in_flight
, 0);
380 device
->reada_next
= 0;
381 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
382 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
384 mutex_lock(&fs_devices
->device_list_mutex
);
385 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
386 mutex_unlock(&fs_devices
->device_list_mutex
);
388 device
->fs_devices
= fs_devices
;
389 fs_devices
->num_devices
++;
390 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
391 name
= kstrdup(path
, GFP_NOFS
);
396 if (device
->missing
) {
397 fs_devices
->missing_devices
--;
402 if (found_transid
> fs_devices
->latest_trans
) {
403 fs_devices
->latest_devid
= devid
;
404 fs_devices
->latest_trans
= found_transid
;
406 *fs_devices_ret
= fs_devices
;
410 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
412 struct btrfs_fs_devices
*fs_devices
;
413 struct btrfs_device
*device
;
414 struct btrfs_device
*orig_dev
;
416 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
418 return ERR_PTR(-ENOMEM
);
420 INIT_LIST_HEAD(&fs_devices
->devices
);
421 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
422 INIT_LIST_HEAD(&fs_devices
->list
);
423 mutex_init(&fs_devices
->device_list_mutex
);
424 fs_devices
->latest_devid
= orig
->latest_devid
;
425 fs_devices
->latest_trans
= orig
->latest_trans
;
426 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
428 /* We have held the volume lock, it is safe to get the devices. */
429 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
430 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
434 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
440 device
->devid
= orig_dev
->devid
;
441 device
->work
.func
= pending_bios_fn
;
442 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
443 spin_lock_init(&device
->io_lock
);
444 INIT_LIST_HEAD(&device
->dev_list
);
445 INIT_LIST_HEAD(&device
->dev_alloc_list
);
447 list_add(&device
->dev_list
, &fs_devices
->devices
);
448 device
->fs_devices
= fs_devices
;
449 fs_devices
->num_devices
++;
453 free_fs_devices(fs_devices
);
454 return ERR_PTR(-ENOMEM
);
457 int btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
459 struct btrfs_device
*device
, *next
;
461 mutex_lock(&uuid_mutex
);
463 /* This is the initialized path, it is safe to release the devices. */
464 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
465 if (device
->in_fs_metadata
)
469 blkdev_put(device
->bdev
, device
->mode
);
471 fs_devices
->open_devices
--;
473 if (device
->writeable
) {
474 list_del_init(&device
->dev_alloc_list
);
475 device
->writeable
= 0;
476 fs_devices
->rw_devices
--;
478 list_del_init(&device
->dev_list
);
479 fs_devices
->num_devices
--;
484 if (fs_devices
->seed
) {
485 fs_devices
= fs_devices
->seed
;
489 mutex_unlock(&uuid_mutex
);
493 static void __free_device(struct work_struct
*work
)
495 struct btrfs_device
*device
;
497 device
= container_of(work
, struct btrfs_device
, rcu_work
);
500 blkdev_put(device
->bdev
, device
->mode
);
506 static void free_device(struct rcu_head
*head
)
508 struct btrfs_device
*device
;
510 device
= container_of(head
, struct btrfs_device
, rcu
);
512 INIT_WORK(&device
->rcu_work
, __free_device
);
513 schedule_work(&device
->rcu_work
);
516 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
518 struct btrfs_device
*device
;
520 if (--fs_devices
->opened
> 0)
523 mutex_lock(&fs_devices
->device_list_mutex
);
524 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
525 struct btrfs_device
*new_device
;
528 fs_devices
->open_devices
--;
530 if (device
->writeable
) {
531 list_del_init(&device
->dev_alloc_list
);
532 fs_devices
->rw_devices
--;
535 if (device
->can_discard
)
536 fs_devices
->num_can_discard
--;
538 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
540 memcpy(new_device
, device
, sizeof(*new_device
));
541 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
542 BUG_ON(device
->name
&& !new_device
->name
);
543 new_device
->bdev
= NULL
;
544 new_device
->writeable
= 0;
545 new_device
->in_fs_metadata
= 0;
546 new_device
->can_discard
= 0;
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
);
583 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
584 fmode_t flags
, void *holder
)
586 struct request_queue
*q
;
587 struct block_device
*bdev
;
588 struct list_head
*head
= &fs_devices
->devices
;
589 struct btrfs_device
*device
;
590 struct block_device
*latest_bdev
= NULL
;
591 struct buffer_head
*bh
;
592 struct btrfs_super_block
*disk_super
;
593 u64 latest_devid
= 0;
594 u64 latest_transid
= 0;
601 list_for_each_entry(device
, head
, dev_list
) {
607 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
609 printk(KERN_INFO
"open %s failed\n", device
->name
);
612 set_blocksize(bdev
, 4096);
614 bh
= btrfs_read_dev_super(bdev
);
618 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
619 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
620 if (devid
!= device
->devid
)
623 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
627 device
->generation
= btrfs_super_generation(disk_super
);
628 if (!latest_transid
|| device
->generation
> latest_transid
) {
629 latest_devid
= devid
;
630 latest_transid
= device
->generation
;
634 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
635 device
->writeable
= 0;
637 device
->writeable
= !bdev_read_only(bdev
);
641 q
= bdev_get_queue(bdev
);
642 if (blk_queue_discard(q
)) {
643 device
->can_discard
= 1;
644 fs_devices
->num_can_discard
++;
648 device
->in_fs_metadata
= 0;
649 device
->mode
= flags
;
651 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
652 fs_devices
->rotating
= 1;
654 fs_devices
->open_devices
++;
655 if (device
->writeable
) {
656 fs_devices
->rw_devices
++;
657 list_add(&device
->dev_alloc_list
,
658 &fs_devices
->alloc_list
);
666 blkdev_put(bdev
, flags
);
670 if (fs_devices
->open_devices
== 0) {
674 fs_devices
->seeding
= seeding
;
675 fs_devices
->opened
= 1;
676 fs_devices
->latest_bdev
= latest_bdev
;
677 fs_devices
->latest_devid
= latest_devid
;
678 fs_devices
->latest_trans
= latest_transid
;
679 fs_devices
->total_rw_bytes
= 0;
684 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
685 fmode_t flags
, void *holder
)
689 mutex_lock(&uuid_mutex
);
690 if (fs_devices
->opened
) {
691 fs_devices
->opened
++;
694 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
696 mutex_unlock(&uuid_mutex
);
700 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
701 struct btrfs_fs_devices
**fs_devices_ret
)
703 struct btrfs_super_block
*disk_super
;
704 struct block_device
*bdev
;
705 struct buffer_head
*bh
;
710 mutex_lock(&uuid_mutex
);
713 bdev
= blkdev_get_by_path(path
, flags
, holder
);
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 blkdev_put(bdev
, flags
);
743 mutex_unlock(&uuid_mutex
);
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 * @trans: transaction handler
834 * @device: the device which we search the free space in
835 * @num_bytes: the size of the free space that we need
836 * @start: store the start of the free space.
837 * @len: the size of the free space. that we find, or the size of the max
838 * free space if we don't find suitable free space
840 * this uses a pretty simple search, the expectation is that it is
841 * called very infrequently and that a given device has a small number
844 * @start is used to store the start of the free space if we find. But if we
845 * don't find suitable free space, it will be used to store the start position
846 * of the max free space.
848 * @len is used to store the size of the free space that we find.
849 * But if we don't find suitable free space, it is used to store the size of
850 * the max free space.
852 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
853 struct btrfs_device
*device
, u64 num_bytes
,
854 u64
*start
, u64
*len
)
856 struct btrfs_key key
;
857 struct btrfs_root
*root
= device
->dev_root
;
858 struct btrfs_dev_extent
*dev_extent
;
859 struct btrfs_path
*path
;
865 u64 search_end
= device
->total_bytes
;
868 struct extent_buffer
*l
;
870 /* FIXME use last free of some kind */
872 /* we don't want to overwrite the superblock on the drive,
873 * so we make sure to start at an offset of at least 1MB
875 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
877 max_hole_start
= search_start
;
881 if (search_start
>= search_end
) {
886 path
= btrfs_alloc_path();
893 key
.objectid
= device
->devid
;
894 key
.offset
= search_start
;
895 key
.type
= BTRFS_DEV_EXTENT_KEY
;
897 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
901 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
908 slot
= path
->slots
[0];
909 if (slot
>= btrfs_header_nritems(l
)) {
910 ret
= btrfs_next_leaf(root
, path
);
918 btrfs_item_key_to_cpu(l
, &key
, slot
);
920 if (key
.objectid
< device
->devid
)
923 if (key
.objectid
> device
->devid
)
926 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
929 if (key
.offset
> search_start
) {
930 hole_size
= key
.offset
- search_start
;
932 if (hole_size
> max_hole_size
) {
933 max_hole_start
= search_start
;
934 max_hole_size
= hole_size
;
938 * If this free space is greater than which we need,
939 * it must be the max free space that we have found
940 * until now, so max_hole_start must point to the start
941 * of this free space and the length of this free space
942 * is stored in max_hole_size. Thus, we return
943 * max_hole_start and max_hole_size and go back to the
946 if (hole_size
>= num_bytes
) {
952 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
953 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
955 if (extent_end
> search_start
)
956 search_start
= extent_end
;
963 * At this point, search_start should be the end of
964 * allocated dev extents, and when shrinking the device,
965 * search_end may be smaller than search_start.
967 if (search_end
> search_start
)
968 hole_size
= search_end
- search_start
;
970 if (hole_size
> max_hole_size
) {
971 max_hole_start
= search_start
;
972 max_hole_size
= hole_size
;
976 if (hole_size
< num_bytes
)
982 btrfs_free_path(path
);
984 *start
= max_hole_start
;
986 *len
= max_hole_size
;
990 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
991 struct btrfs_device
*device
,
995 struct btrfs_path
*path
;
996 struct btrfs_root
*root
= device
->dev_root
;
997 struct btrfs_key key
;
998 struct btrfs_key found_key
;
999 struct extent_buffer
*leaf
= NULL
;
1000 struct btrfs_dev_extent
*extent
= NULL
;
1002 path
= btrfs_alloc_path();
1006 key
.objectid
= device
->devid
;
1008 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1010 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1012 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1013 BTRFS_DEV_EXTENT_KEY
);
1016 leaf
= path
->nodes
[0];
1017 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1018 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1019 struct btrfs_dev_extent
);
1020 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1021 btrfs_dev_extent_length(leaf
, extent
) < start
);
1023 btrfs_release_path(path
);
1025 } else if (ret
== 0) {
1026 leaf
= path
->nodes
[0];
1027 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1028 struct btrfs_dev_extent
);
1032 if (device
->bytes_used
> 0) {
1033 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1034 device
->bytes_used
-= len
;
1035 spin_lock(&root
->fs_info
->free_chunk_lock
);
1036 root
->fs_info
->free_chunk_space
+= len
;
1037 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1039 ret
= btrfs_del_item(trans
, root
, path
);
1042 btrfs_free_path(path
);
1046 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1047 struct btrfs_device
*device
,
1048 u64 chunk_tree
, u64 chunk_objectid
,
1049 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1052 struct btrfs_path
*path
;
1053 struct btrfs_root
*root
= device
->dev_root
;
1054 struct btrfs_dev_extent
*extent
;
1055 struct extent_buffer
*leaf
;
1056 struct btrfs_key key
;
1058 WARN_ON(!device
->in_fs_metadata
);
1059 path
= btrfs_alloc_path();
1063 key
.objectid
= device
->devid
;
1065 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1066 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1070 leaf
= path
->nodes
[0];
1071 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1072 struct btrfs_dev_extent
);
1073 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1074 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1075 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1077 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1078 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1081 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1082 btrfs_mark_buffer_dirty(leaf
);
1083 btrfs_free_path(path
);
1087 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1088 u64 objectid
, u64
*offset
)
1090 struct btrfs_path
*path
;
1092 struct btrfs_key key
;
1093 struct btrfs_chunk
*chunk
;
1094 struct btrfs_key found_key
;
1096 path
= btrfs_alloc_path();
1100 key
.objectid
= objectid
;
1101 key
.offset
= (u64
)-1;
1102 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1104 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1110 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1114 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1116 if (found_key
.objectid
!= objectid
)
1119 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1120 struct btrfs_chunk
);
1121 *offset
= found_key
.offset
+
1122 btrfs_chunk_length(path
->nodes
[0], chunk
);
1127 btrfs_free_path(path
);
1131 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1134 struct btrfs_key key
;
1135 struct btrfs_key found_key
;
1136 struct btrfs_path
*path
;
1138 root
= root
->fs_info
->chunk_root
;
1140 path
= btrfs_alloc_path();
1144 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1145 key
.type
= BTRFS_DEV_ITEM_KEY
;
1146 key
.offset
= (u64
)-1;
1148 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1154 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1155 BTRFS_DEV_ITEM_KEY
);
1159 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1161 *objectid
= found_key
.offset
+ 1;
1165 btrfs_free_path(path
);
1170 * the device information is stored in the chunk root
1171 * the btrfs_device struct should be fully filled in
1173 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1174 struct btrfs_root
*root
,
1175 struct btrfs_device
*device
)
1178 struct btrfs_path
*path
;
1179 struct btrfs_dev_item
*dev_item
;
1180 struct extent_buffer
*leaf
;
1181 struct btrfs_key key
;
1184 root
= root
->fs_info
->chunk_root
;
1186 path
= btrfs_alloc_path();
1190 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1191 key
.type
= BTRFS_DEV_ITEM_KEY
;
1192 key
.offset
= device
->devid
;
1194 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1199 leaf
= path
->nodes
[0];
1200 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1202 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1203 btrfs_set_device_generation(leaf
, dev_item
, 0);
1204 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1205 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1206 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1207 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1208 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1209 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1210 btrfs_set_device_group(leaf
, dev_item
, 0);
1211 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1212 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1213 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1215 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1216 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1217 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1218 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1219 btrfs_mark_buffer_dirty(leaf
);
1223 btrfs_free_path(path
);
1227 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1228 struct btrfs_device
*device
)
1231 struct btrfs_path
*path
;
1232 struct btrfs_key key
;
1233 struct btrfs_trans_handle
*trans
;
1235 root
= root
->fs_info
->chunk_root
;
1237 path
= btrfs_alloc_path();
1241 trans
= btrfs_start_transaction(root
, 0);
1242 if (IS_ERR(trans
)) {
1243 btrfs_free_path(path
);
1244 return PTR_ERR(trans
);
1246 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1247 key
.type
= BTRFS_DEV_ITEM_KEY
;
1248 key
.offset
= device
->devid
;
1251 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1260 ret
= btrfs_del_item(trans
, root
, path
);
1264 btrfs_free_path(path
);
1265 unlock_chunks(root
);
1266 btrfs_commit_transaction(trans
, root
);
1270 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1272 struct btrfs_device
*device
;
1273 struct btrfs_device
*next_device
;
1274 struct block_device
*bdev
;
1275 struct buffer_head
*bh
= NULL
;
1276 struct btrfs_super_block
*disk_super
;
1277 struct btrfs_fs_devices
*cur_devices
;
1283 bool clear_super
= false;
1285 mutex_lock(&uuid_mutex
);
1287 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1288 root
->fs_info
->avail_system_alloc_bits
|
1289 root
->fs_info
->avail_metadata_alloc_bits
;
1291 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1292 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1293 printk(KERN_ERR
"btrfs: unable to go below four devices "
1299 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1300 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1301 printk(KERN_ERR
"btrfs: unable to go below two "
1302 "devices on raid1\n");
1307 if (strcmp(device_path
, "missing") == 0) {
1308 struct list_head
*devices
;
1309 struct btrfs_device
*tmp
;
1312 devices
= &root
->fs_info
->fs_devices
->devices
;
1314 * It is safe to read the devices since the volume_mutex
1317 list_for_each_entry(tmp
, devices
, dev_list
) {
1318 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1327 printk(KERN_ERR
"btrfs: no missing devices found to "
1332 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1333 root
->fs_info
->bdev_holder
);
1335 ret
= PTR_ERR(bdev
);
1339 set_blocksize(bdev
, 4096);
1340 bh
= btrfs_read_dev_super(bdev
);
1345 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1346 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1347 dev_uuid
= disk_super
->dev_item
.uuid
;
1348 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1356 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1357 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1363 if (device
->writeable
) {
1365 list_del_init(&device
->dev_alloc_list
);
1366 unlock_chunks(root
);
1367 root
->fs_info
->fs_devices
->rw_devices
--;
1371 ret
= btrfs_shrink_device(device
, 0);
1375 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1379 spin_lock(&root
->fs_info
->free_chunk_lock
);
1380 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1382 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1384 device
->in_fs_metadata
= 0;
1385 btrfs_scrub_cancel_dev(root
, device
);
1388 * the device list mutex makes sure that we don't change
1389 * the device list while someone else is writing out all
1390 * the device supers.
1393 cur_devices
= device
->fs_devices
;
1394 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1395 list_del_rcu(&device
->dev_list
);
1397 device
->fs_devices
->num_devices
--;
1399 if (device
->missing
)
1400 root
->fs_info
->fs_devices
->missing_devices
--;
1402 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1403 struct btrfs_device
, dev_list
);
1404 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1405 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1406 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1407 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1410 device
->fs_devices
->open_devices
--;
1412 call_rcu(&device
->rcu
, free_device
);
1413 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1415 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1416 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1418 if (cur_devices
->open_devices
== 0) {
1419 struct btrfs_fs_devices
*fs_devices
;
1420 fs_devices
= root
->fs_info
->fs_devices
;
1421 while (fs_devices
) {
1422 if (fs_devices
->seed
== cur_devices
)
1424 fs_devices
= fs_devices
->seed
;
1426 fs_devices
->seed
= cur_devices
->seed
;
1427 cur_devices
->seed
= NULL
;
1429 __btrfs_close_devices(cur_devices
);
1430 unlock_chunks(root
);
1431 free_fs_devices(cur_devices
);
1435 * at this point, the device is zero sized. We want to
1436 * remove it from the devices list and zero out the old super
1439 /* make sure this device isn't detected as part of
1442 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1443 set_buffer_dirty(bh
);
1444 sync_dirty_buffer(bh
);
1453 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1455 mutex_unlock(&uuid_mutex
);
1458 if (device
->writeable
) {
1460 list_add(&device
->dev_alloc_list
,
1461 &root
->fs_info
->fs_devices
->alloc_list
);
1462 unlock_chunks(root
);
1463 root
->fs_info
->fs_devices
->rw_devices
++;
1469 * does all the dirty work required for changing file system's UUID.
1471 static int btrfs_prepare_sprout(struct btrfs_trans_handle
*trans
,
1472 struct btrfs_root
*root
)
1474 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1475 struct btrfs_fs_devices
*old_devices
;
1476 struct btrfs_fs_devices
*seed_devices
;
1477 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1478 struct btrfs_device
*device
;
1481 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1482 if (!fs_devices
->seeding
)
1485 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1489 old_devices
= clone_fs_devices(fs_devices
);
1490 if (IS_ERR(old_devices
)) {
1491 kfree(seed_devices
);
1492 return PTR_ERR(old_devices
);
1495 list_add(&old_devices
->list
, &fs_uuids
);
1497 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1498 seed_devices
->opened
= 1;
1499 INIT_LIST_HEAD(&seed_devices
->devices
);
1500 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1501 mutex_init(&seed_devices
->device_list_mutex
);
1503 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1504 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1506 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1508 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1509 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1510 device
->fs_devices
= seed_devices
;
1513 fs_devices
->seeding
= 0;
1514 fs_devices
->num_devices
= 0;
1515 fs_devices
->open_devices
= 0;
1516 fs_devices
->seed
= seed_devices
;
1518 generate_random_uuid(fs_devices
->fsid
);
1519 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1520 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1521 super_flags
= btrfs_super_flags(disk_super
) &
1522 ~BTRFS_SUPER_FLAG_SEEDING
;
1523 btrfs_set_super_flags(disk_super
, super_flags
);
1529 * strore the expected generation for seed devices in device items.
1531 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1532 struct btrfs_root
*root
)
1534 struct btrfs_path
*path
;
1535 struct extent_buffer
*leaf
;
1536 struct btrfs_dev_item
*dev_item
;
1537 struct btrfs_device
*device
;
1538 struct btrfs_key key
;
1539 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1540 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1544 path
= btrfs_alloc_path();
1548 root
= root
->fs_info
->chunk_root
;
1549 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1551 key
.type
= BTRFS_DEV_ITEM_KEY
;
1554 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1558 leaf
= path
->nodes
[0];
1560 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1561 ret
= btrfs_next_leaf(root
, path
);
1566 leaf
= path
->nodes
[0];
1567 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1568 btrfs_release_path(path
);
1572 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1573 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1574 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1577 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1578 struct btrfs_dev_item
);
1579 devid
= btrfs_device_id(leaf
, dev_item
);
1580 read_extent_buffer(leaf
, dev_uuid
,
1581 (unsigned long)btrfs_device_uuid(dev_item
),
1583 read_extent_buffer(leaf
, fs_uuid
,
1584 (unsigned long)btrfs_device_fsid(dev_item
),
1586 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1589 if (device
->fs_devices
->seeding
) {
1590 btrfs_set_device_generation(leaf
, dev_item
,
1591 device
->generation
);
1592 btrfs_mark_buffer_dirty(leaf
);
1600 btrfs_free_path(path
);
1604 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1606 struct request_queue
*q
;
1607 struct btrfs_trans_handle
*trans
;
1608 struct btrfs_device
*device
;
1609 struct block_device
*bdev
;
1610 struct list_head
*devices
;
1611 struct super_block
*sb
= root
->fs_info
->sb
;
1613 int seeding_dev
= 0;
1616 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1619 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1620 root
->fs_info
->bdev_holder
);
1622 return PTR_ERR(bdev
);
1624 if (root
->fs_info
->fs_devices
->seeding
) {
1626 down_write(&sb
->s_umount
);
1627 mutex_lock(&uuid_mutex
);
1630 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1632 devices
= &root
->fs_info
->fs_devices
->devices
;
1634 * we have the volume lock, so we don't need the extra
1635 * device list mutex while reading the list here.
1637 list_for_each_entry(device
, devices
, dev_list
) {
1638 if (device
->bdev
== bdev
) {
1644 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1646 /* we can safely leave the fs_devices entry around */
1651 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1652 if (!device
->name
) {
1658 ret
= find_next_devid(root
, &device
->devid
);
1660 kfree(device
->name
);
1665 trans
= btrfs_start_transaction(root
, 0);
1666 if (IS_ERR(trans
)) {
1667 kfree(device
->name
);
1669 ret
= PTR_ERR(trans
);
1675 q
= bdev_get_queue(bdev
);
1676 if (blk_queue_discard(q
))
1677 device
->can_discard
= 1;
1678 device
->writeable
= 1;
1679 device
->work
.func
= pending_bios_fn
;
1680 generate_random_uuid(device
->uuid
);
1681 spin_lock_init(&device
->io_lock
);
1682 device
->generation
= trans
->transid
;
1683 device
->io_width
= root
->sectorsize
;
1684 device
->io_align
= root
->sectorsize
;
1685 device
->sector_size
= root
->sectorsize
;
1686 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1687 device
->disk_total_bytes
= device
->total_bytes
;
1688 device
->dev_root
= root
->fs_info
->dev_root
;
1689 device
->bdev
= bdev
;
1690 device
->in_fs_metadata
= 1;
1691 device
->mode
= FMODE_EXCL
;
1692 set_blocksize(device
->bdev
, 4096);
1695 sb
->s_flags
&= ~MS_RDONLY
;
1696 ret
= btrfs_prepare_sprout(trans
, root
);
1700 device
->fs_devices
= root
->fs_info
->fs_devices
;
1703 * we don't want write_supers to jump in here with our device
1706 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1707 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1708 list_add(&device
->dev_alloc_list
,
1709 &root
->fs_info
->fs_devices
->alloc_list
);
1710 root
->fs_info
->fs_devices
->num_devices
++;
1711 root
->fs_info
->fs_devices
->open_devices
++;
1712 root
->fs_info
->fs_devices
->rw_devices
++;
1713 if (device
->can_discard
)
1714 root
->fs_info
->fs_devices
->num_can_discard
++;
1715 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1717 spin_lock(&root
->fs_info
->free_chunk_lock
);
1718 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1719 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1721 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1722 root
->fs_info
->fs_devices
->rotating
= 1;
1724 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1725 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1726 total_bytes
+ device
->total_bytes
);
1728 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1729 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1731 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1734 ret
= init_first_rw_device(trans
, root
, device
);
1736 ret
= btrfs_finish_sprout(trans
, root
);
1739 ret
= btrfs_add_device(trans
, root
, device
);
1743 * we've got more storage, clear any full flags on the space
1746 btrfs_clear_space_info_full(root
->fs_info
);
1748 unlock_chunks(root
);
1749 btrfs_commit_transaction(trans
, root
);
1752 mutex_unlock(&uuid_mutex
);
1753 up_write(&sb
->s_umount
);
1755 ret
= btrfs_relocate_sys_chunks(root
);
1761 blkdev_put(bdev
, FMODE_EXCL
);
1763 mutex_unlock(&uuid_mutex
);
1764 up_write(&sb
->s_umount
);
1769 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1770 struct btrfs_device
*device
)
1773 struct btrfs_path
*path
;
1774 struct btrfs_root
*root
;
1775 struct btrfs_dev_item
*dev_item
;
1776 struct extent_buffer
*leaf
;
1777 struct btrfs_key key
;
1779 root
= device
->dev_root
->fs_info
->chunk_root
;
1781 path
= btrfs_alloc_path();
1785 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1786 key
.type
= BTRFS_DEV_ITEM_KEY
;
1787 key
.offset
= device
->devid
;
1789 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1798 leaf
= path
->nodes
[0];
1799 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1801 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1802 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1803 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1804 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1805 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1806 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1807 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1808 btrfs_mark_buffer_dirty(leaf
);
1811 btrfs_free_path(path
);
1815 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1816 struct btrfs_device
*device
, u64 new_size
)
1818 struct btrfs_super_block
*super_copy
=
1819 device
->dev_root
->fs_info
->super_copy
;
1820 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1821 u64 diff
= new_size
- device
->total_bytes
;
1823 if (!device
->writeable
)
1825 if (new_size
<= device
->total_bytes
)
1828 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1829 device
->fs_devices
->total_rw_bytes
+= diff
;
1831 device
->total_bytes
= new_size
;
1832 device
->disk_total_bytes
= new_size
;
1833 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1835 return btrfs_update_device(trans
, device
);
1838 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1839 struct btrfs_device
*device
, u64 new_size
)
1842 lock_chunks(device
->dev_root
);
1843 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1844 unlock_chunks(device
->dev_root
);
1848 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1849 struct btrfs_root
*root
,
1850 u64 chunk_tree
, u64 chunk_objectid
,
1854 struct btrfs_path
*path
;
1855 struct btrfs_key key
;
1857 root
= root
->fs_info
->chunk_root
;
1858 path
= btrfs_alloc_path();
1862 key
.objectid
= chunk_objectid
;
1863 key
.offset
= chunk_offset
;
1864 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1866 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1869 ret
= btrfs_del_item(trans
, root
, path
);
1871 btrfs_free_path(path
);
1875 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1878 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1879 struct btrfs_disk_key
*disk_key
;
1880 struct btrfs_chunk
*chunk
;
1887 struct btrfs_key key
;
1889 array_size
= btrfs_super_sys_array_size(super_copy
);
1891 ptr
= super_copy
->sys_chunk_array
;
1894 while (cur
< array_size
) {
1895 disk_key
= (struct btrfs_disk_key
*)ptr
;
1896 btrfs_disk_key_to_cpu(&key
, disk_key
);
1898 len
= sizeof(*disk_key
);
1900 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1901 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1902 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1903 len
+= btrfs_chunk_item_size(num_stripes
);
1908 if (key
.objectid
== chunk_objectid
&&
1909 key
.offset
== chunk_offset
) {
1910 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1912 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1921 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1922 u64 chunk_tree
, u64 chunk_objectid
,
1925 struct extent_map_tree
*em_tree
;
1926 struct btrfs_root
*extent_root
;
1927 struct btrfs_trans_handle
*trans
;
1928 struct extent_map
*em
;
1929 struct map_lookup
*map
;
1933 root
= root
->fs_info
->chunk_root
;
1934 extent_root
= root
->fs_info
->extent_root
;
1935 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1937 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1941 /* step one, relocate all the extents inside this chunk */
1942 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1946 trans
= btrfs_start_transaction(root
, 0);
1947 BUG_ON(IS_ERR(trans
));
1952 * step two, delete the device extents and the
1953 * chunk tree entries
1955 read_lock(&em_tree
->lock
);
1956 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1957 read_unlock(&em_tree
->lock
);
1959 BUG_ON(em
->start
> chunk_offset
||
1960 em
->start
+ em
->len
< chunk_offset
);
1961 map
= (struct map_lookup
*)em
->bdev
;
1963 for (i
= 0; i
< map
->num_stripes
; i
++) {
1964 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1965 map
->stripes
[i
].physical
);
1968 if (map
->stripes
[i
].dev
) {
1969 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1973 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1978 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1980 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1981 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1985 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1988 write_lock(&em_tree
->lock
);
1989 remove_extent_mapping(em_tree
, em
);
1990 write_unlock(&em_tree
->lock
);
1995 /* once for the tree */
1996 free_extent_map(em
);
1998 free_extent_map(em
);
2000 unlock_chunks(root
);
2001 btrfs_end_transaction(trans
, root
);
2005 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2007 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2008 struct btrfs_path
*path
;
2009 struct extent_buffer
*leaf
;
2010 struct btrfs_chunk
*chunk
;
2011 struct btrfs_key key
;
2012 struct btrfs_key found_key
;
2013 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2015 bool retried
= false;
2019 path
= btrfs_alloc_path();
2024 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2025 key
.offset
= (u64
)-1;
2026 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2029 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2034 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2041 leaf
= path
->nodes
[0];
2042 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2044 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2045 struct btrfs_chunk
);
2046 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2047 btrfs_release_path(path
);
2049 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2050 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2059 if (found_key
.offset
== 0)
2061 key
.offset
= found_key
.offset
- 1;
2064 if (failed
&& !retried
) {
2068 } else if (failed
&& retried
) {
2073 btrfs_free_path(path
);
2077 static int insert_balance_item(struct btrfs_root
*root
,
2078 struct btrfs_balance_control
*bctl
)
2080 struct btrfs_trans_handle
*trans
;
2081 struct btrfs_balance_item
*item
;
2082 struct btrfs_disk_balance_args disk_bargs
;
2083 struct btrfs_path
*path
;
2084 struct extent_buffer
*leaf
;
2085 struct btrfs_key key
;
2088 path
= btrfs_alloc_path();
2092 trans
= btrfs_start_transaction(root
, 0);
2093 if (IS_ERR(trans
)) {
2094 btrfs_free_path(path
);
2095 return PTR_ERR(trans
);
2098 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2099 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2102 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2107 leaf
= path
->nodes
[0];
2108 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2110 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2112 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2113 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2114 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2115 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2116 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2117 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2119 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2121 btrfs_mark_buffer_dirty(leaf
);
2123 btrfs_free_path(path
);
2124 err
= btrfs_commit_transaction(trans
, root
);
2130 static int del_balance_item(struct btrfs_root
*root
)
2132 struct btrfs_trans_handle
*trans
;
2133 struct btrfs_path
*path
;
2134 struct btrfs_key key
;
2137 path
= btrfs_alloc_path();
2141 trans
= btrfs_start_transaction(root
, 0);
2142 if (IS_ERR(trans
)) {
2143 btrfs_free_path(path
);
2144 return PTR_ERR(trans
);
2147 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2148 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2151 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2159 ret
= btrfs_del_item(trans
, root
, path
);
2161 btrfs_free_path(path
);
2162 err
= btrfs_commit_transaction(trans
, root
);
2169 * This is a heuristic used to reduce the number of chunks balanced on
2170 * resume after balance was interrupted.
2172 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2175 * Turn on soft mode for chunk types that were being converted.
2177 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2178 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2179 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2180 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2181 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2182 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2185 * Turn on usage filter if is not already used. The idea is
2186 * that chunks that we have already balanced should be
2187 * reasonably full. Don't do it for chunks that are being
2188 * converted - that will keep us from relocating unconverted
2189 * (albeit full) chunks.
2191 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2192 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2193 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2194 bctl
->data
.usage
= 90;
2196 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2197 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2198 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2199 bctl
->sys
.usage
= 90;
2201 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2202 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2203 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2204 bctl
->meta
.usage
= 90;
2209 * Should be called with both balance and volume mutexes held to
2210 * serialize other volume operations (add_dev/rm_dev/resize) with
2211 * restriper. Same goes for unset_balance_control.
2213 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2215 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2217 BUG_ON(fs_info
->balance_ctl
);
2219 spin_lock(&fs_info
->balance_lock
);
2220 fs_info
->balance_ctl
= bctl
;
2221 spin_unlock(&fs_info
->balance_lock
);
2224 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2226 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2228 BUG_ON(!fs_info
->balance_ctl
);
2230 spin_lock(&fs_info
->balance_lock
);
2231 fs_info
->balance_ctl
= NULL
;
2232 spin_unlock(&fs_info
->balance_lock
);
2238 * Balance filters. Return 1 if chunk should be filtered out
2239 * (should not be balanced).
2241 static int chunk_profiles_filter(u64 chunk_profile
,
2242 struct btrfs_balance_args
*bargs
)
2244 chunk_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
2246 if (chunk_profile
== 0)
2247 chunk_profile
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2249 if (bargs
->profiles
& chunk_profile
)
2255 static u64
div_factor_fine(u64 num
, int factor
)
2267 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2268 struct btrfs_balance_args
*bargs
)
2270 struct btrfs_block_group_cache
*cache
;
2271 u64 chunk_used
, user_thresh
;
2274 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2275 chunk_used
= btrfs_block_group_used(&cache
->item
);
2277 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2278 if (chunk_used
< user_thresh
)
2281 btrfs_put_block_group(cache
);
2285 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2286 struct btrfs_chunk
*chunk
,
2287 struct btrfs_balance_args
*bargs
)
2289 struct btrfs_stripe
*stripe
;
2290 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2293 for (i
= 0; i
< num_stripes
; i
++) {
2294 stripe
= btrfs_stripe_nr(chunk
, i
);
2295 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2302 /* [pstart, pend) */
2303 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2304 struct btrfs_chunk
*chunk
,
2306 struct btrfs_balance_args
*bargs
)
2308 struct btrfs_stripe
*stripe
;
2309 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2315 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2318 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2319 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2323 factor
= num_stripes
/ factor
;
2325 for (i
= 0; i
< num_stripes
; i
++) {
2326 stripe
= btrfs_stripe_nr(chunk
, i
);
2327 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2330 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2331 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2332 do_div(stripe_length
, factor
);
2334 if (stripe_offset
< bargs
->pend
&&
2335 stripe_offset
+ stripe_length
> bargs
->pstart
)
2342 /* [vstart, vend) */
2343 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2344 struct btrfs_chunk
*chunk
,
2346 struct btrfs_balance_args
*bargs
)
2348 if (chunk_offset
< bargs
->vend
&&
2349 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2350 /* at least part of the chunk is inside this vrange */
2356 static int chunk_soft_convert_filter(u64 chunk_profile
,
2357 struct btrfs_balance_args
*bargs
)
2359 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2362 chunk_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
2364 if (chunk_profile
== 0)
2365 chunk_profile
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2367 if (bargs
->target
& chunk_profile
)
2373 static int should_balance_chunk(struct btrfs_root
*root
,
2374 struct extent_buffer
*leaf
,
2375 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2377 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2378 struct btrfs_balance_args
*bargs
= NULL
;
2379 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2382 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2383 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2387 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2388 bargs
= &bctl
->data
;
2389 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2391 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2392 bargs
= &bctl
->meta
;
2394 /* profiles filter */
2395 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2396 chunk_profiles_filter(chunk_type
, bargs
)) {
2401 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2402 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2407 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2408 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2412 /* drange filter, makes sense only with devid filter */
2413 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2414 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2419 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2420 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2424 /* soft profile changing mode */
2425 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2426 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2433 static u64
div_factor(u64 num
, int factor
)
2442 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2444 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2445 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2446 struct list_head
*devices
;
2447 struct btrfs_device
*device
;
2450 struct btrfs_chunk
*chunk
;
2451 struct btrfs_path
*path
;
2452 struct btrfs_key key
;
2453 struct btrfs_key found_key
;
2454 struct btrfs_trans_handle
*trans
;
2455 struct extent_buffer
*leaf
;
2458 int enospc_errors
= 0;
2460 /* step one make some room on all the devices */
2461 devices
= &fs_info
->fs_devices
->devices
;
2462 list_for_each_entry(device
, devices
, dev_list
) {
2463 old_size
= device
->total_bytes
;
2464 size_to_free
= div_factor(old_size
, 1);
2465 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2466 if (!device
->writeable
||
2467 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2470 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2475 trans
= btrfs_start_transaction(dev_root
, 0);
2476 BUG_ON(IS_ERR(trans
));
2478 ret
= btrfs_grow_device(trans
, device
, old_size
);
2481 btrfs_end_transaction(trans
, dev_root
);
2484 /* step two, relocate all the chunks */
2485 path
= btrfs_alloc_path();
2490 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2491 key
.offset
= (u64
)-1;
2492 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2495 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2500 * this shouldn't happen, it means the last relocate
2504 BUG(); /* FIXME break ? */
2506 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2507 BTRFS_CHUNK_ITEM_KEY
);
2513 leaf
= path
->nodes
[0];
2514 slot
= path
->slots
[0];
2515 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2517 if (found_key
.objectid
!= key
.objectid
)
2520 /* chunk zero is special */
2521 if (found_key
.offset
== 0)
2524 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2526 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2528 btrfs_release_path(path
);
2532 ret
= btrfs_relocate_chunk(chunk_root
,
2533 chunk_root
->root_key
.objectid
,
2536 if (ret
&& ret
!= -ENOSPC
)
2541 key
.offset
= found_key
.offset
- 1;
2545 btrfs_free_path(path
);
2546 if (enospc_errors
) {
2547 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2556 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2560 unset_balance_control(fs_info
);
2561 ret
= del_balance_item(fs_info
->tree_root
);
2565 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
,
2566 struct btrfs_ioctl_balance_args
*bargs
);
2569 * Should be called with both balance and volume mutexes held
2571 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2572 struct btrfs_ioctl_balance_args
*bargs
)
2574 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2578 if (btrfs_fs_closing(fs_info
)) {
2584 * In case of mixed groups both data and meta should be picked,
2585 * and identical options should be given for both of them.
2587 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2588 if ((allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
) &&
2589 (bctl
->flags
& (BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
))) {
2590 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2591 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2592 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2593 printk(KERN_ERR
"btrfs: with mixed groups data and "
2594 "metadata balance options must be the same\n");
2601 * Profile changing sanity checks. Skip them if a simple
2602 * balance is requested.
2604 if (!((bctl
->data
.flags
| bctl
->sys
.flags
| bctl
->meta
.flags
) &
2605 BTRFS_BALANCE_ARGS_CONVERT
))
2608 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2609 if (fs_info
->fs_devices
->num_devices
== 1)
2610 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2611 else if (fs_info
->fs_devices
->num_devices
< 4)
2612 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
2614 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2615 BTRFS_BLOCK_GROUP_RAID10
);
2617 if (!profile_is_valid(bctl
->data
.target
, 1) ||
2618 bctl
->data
.target
& ~allowed
) {
2619 printk(KERN_ERR
"btrfs: unable to start balance with target "
2620 "data profile %llu\n",
2621 (unsigned long long)bctl
->data
.target
);
2625 if (!profile_is_valid(bctl
->meta
.target
, 1) ||
2626 bctl
->meta
.target
& ~allowed
) {
2627 printk(KERN_ERR
"btrfs: unable to start balance with target "
2628 "metadata profile %llu\n",
2629 (unsigned long long)bctl
->meta
.target
);
2633 if (!profile_is_valid(bctl
->sys
.target
, 1) ||
2634 bctl
->sys
.target
& ~allowed
) {
2635 printk(KERN_ERR
"btrfs: unable to start balance with target "
2636 "system profile %llu\n",
2637 (unsigned long long)bctl
->sys
.target
);
2642 if (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
) {
2643 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
2648 /* allow to reduce meta or sys integrity only if force set */
2649 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2650 BTRFS_BLOCK_GROUP_RAID10
;
2651 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2652 (fs_info
->avail_system_alloc_bits
& allowed
) &&
2653 !(bctl
->sys
.target
& allowed
)) ||
2654 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2655 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
2656 !(bctl
->meta
.target
& allowed
))) {
2657 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
2658 printk(KERN_INFO
"btrfs: force reducing metadata "
2661 printk(KERN_ERR
"btrfs: balance will reduce metadata "
2662 "integrity, use force if you want this\n");
2669 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
2670 if (ret
&& ret
!= -EEXIST
)
2673 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
2674 BUG_ON(ret
== -EEXIST
);
2675 set_balance_control(bctl
);
2677 BUG_ON(ret
!= -EEXIST
);
2678 spin_lock(&fs_info
->balance_lock
);
2679 update_balance_args(bctl
);
2680 spin_unlock(&fs_info
->balance_lock
);
2683 mutex_unlock(&fs_info
->balance_mutex
);
2685 ret
= __btrfs_balance(fs_info
);
2687 mutex_lock(&fs_info
->balance_mutex
);
2690 memset(bargs
, 0, sizeof(*bargs
));
2691 update_ioctl_balance_args(fs_info
, bargs
);
2694 __cancel_balance(fs_info
);
2698 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
2699 __cancel_balance(fs_info
);
2705 static int balance_kthread(void *data
)
2707 struct btrfs_balance_control
*bctl
=
2708 (struct btrfs_balance_control
*)data
;
2709 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2712 mutex_lock(&fs_info
->volume_mutex
);
2713 mutex_lock(&fs_info
->balance_mutex
);
2715 set_balance_control(bctl
);
2717 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
2718 printk(KERN_INFO
"btrfs: force skipping balance\n");
2720 printk(KERN_INFO
"btrfs: continuing balance\n");
2721 ret
= btrfs_balance(bctl
, NULL
);
2724 mutex_unlock(&fs_info
->balance_mutex
);
2725 mutex_unlock(&fs_info
->volume_mutex
);
2729 int btrfs_recover_balance(struct btrfs_root
*tree_root
)
2731 struct task_struct
*tsk
;
2732 struct btrfs_balance_control
*bctl
;
2733 struct btrfs_balance_item
*item
;
2734 struct btrfs_disk_balance_args disk_bargs
;
2735 struct btrfs_path
*path
;
2736 struct extent_buffer
*leaf
;
2737 struct btrfs_key key
;
2740 path
= btrfs_alloc_path();
2744 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
2750 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2751 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2754 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, path
, 0, 0);
2757 if (ret
> 0) { /* ret = -ENOENT; */
2762 leaf
= path
->nodes
[0];
2763 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2765 bctl
->fs_info
= tree_root
->fs_info
;
2766 bctl
->flags
= btrfs_balance_flags(leaf
, item
) | BTRFS_BALANCE_RESUME
;
2768 btrfs_balance_data(leaf
, item
, &disk_bargs
);
2769 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
2770 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
2771 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
2772 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
2773 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
2775 tsk
= kthread_run(balance_kthread
, bctl
, "btrfs-balance");
2784 btrfs_free_path(path
);
2789 * shrinking a device means finding all of the device extents past
2790 * the new size, and then following the back refs to the chunks.
2791 * The chunk relocation code actually frees the device extent
2793 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2795 struct btrfs_trans_handle
*trans
;
2796 struct btrfs_root
*root
= device
->dev_root
;
2797 struct btrfs_dev_extent
*dev_extent
= NULL
;
2798 struct btrfs_path
*path
;
2806 bool retried
= false;
2807 struct extent_buffer
*l
;
2808 struct btrfs_key key
;
2809 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2810 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2811 u64 old_size
= device
->total_bytes
;
2812 u64 diff
= device
->total_bytes
- new_size
;
2814 if (new_size
>= device
->total_bytes
)
2817 path
= btrfs_alloc_path();
2825 device
->total_bytes
= new_size
;
2826 if (device
->writeable
) {
2827 device
->fs_devices
->total_rw_bytes
-= diff
;
2828 spin_lock(&root
->fs_info
->free_chunk_lock
);
2829 root
->fs_info
->free_chunk_space
-= diff
;
2830 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2832 unlock_chunks(root
);
2835 key
.objectid
= device
->devid
;
2836 key
.offset
= (u64
)-1;
2837 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2840 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2844 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2849 btrfs_release_path(path
);
2854 slot
= path
->slots
[0];
2855 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2857 if (key
.objectid
!= device
->devid
) {
2858 btrfs_release_path(path
);
2862 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2863 length
= btrfs_dev_extent_length(l
, dev_extent
);
2865 if (key
.offset
+ length
<= new_size
) {
2866 btrfs_release_path(path
);
2870 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2871 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2872 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2873 btrfs_release_path(path
);
2875 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
2877 if (ret
&& ret
!= -ENOSPC
)
2884 if (failed
&& !retried
) {
2888 } else if (failed
&& retried
) {
2892 device
->total_bytes
= old_size
;
2893 if (device
->writeable
)
2894 device
->fs_devices
->total_rw_bytes
+= diff
;
2895 spin_lock(&root
->fs_info
->free_chunk_lock
);
2896 root
->fs_info
->free_chunk_space
+= diff
;
2897 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2898 unlock_chunks(root
);
2902 /* Shrinking succeeded, else we would be at "done". */
2903 trans
= btrfs_start_transaction(root
, 0);
2904 if (IS_ERR(trans
)) {
2905 ret
= PTR_ERR(trans
);
2911 device
->disk_total_bytes
= new_size
;
2912 /* Now btrfs_update_device() will change the on-disk size. */
2913 ret
= btrfs_update_device(trans
, device
);
2915 unlock_chunks(root
);
2916 btrfs_end_transaction(trans
, root
);
2919 WARN_ON(diff
> old_total
);
2920 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
2921 unlock_chunks(root
);
2922 btrfs_end_transaction(trans
, root
);
2924 btrfs_free_path(path
);
2928 static int btrfs_add_system_chunk(struct btrfs_trans_handle
*trans
,
2929 struct btrfs_root
*root
,
2930 struct btrfs_key
*key
,
2931 struct btrfs_chunk
*chunk
, int item_size
)
2933 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2934 struct btrfs_disk_key disk_key
;
2938 array_size
= btrfs_super_sys_array_size(super_copy
);
2939 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
2942 ptr
= super_copy
->sys_chunk_array
+ array_size
;
2943 btrfs_cpu_key_to_disk(&disk_key
, key
);
2944 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
2945 ptr
+= sizeof(disk_key
);
2946 memcpy(ptr
, chunk
, item_size
);
2947 item_size
+= sizeof(disk_key
);
2948 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
2953 * sort the devices in descending order by max_avail, total_avail
2955 static int btrfs_cmp_device_info(const void *a
, const void *b
)
2957 const struct btrfs_device_info
*di_a
= a
;
2958 const struct btrfs_device_info
*di_b
= b
;
2960 if (di_a
->max_avail
> di_b
->max_avail
)
2962 if (di_a
->max_avail
< di_b
->max_avail
)
2964 if (di_a
->total_avail
> di_b
->total_avail
)
2966 if (di_a
->total_avail
< di_b
->total_avail
)
2971 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
2972 struct btrfs_root
*extent_root
,
2973 struct map_lookup
**map_ret
,
2974 u64
*num_bytes_out
, u64
*stripe_size_out
,
2975 u64 start
, u64 type
)
2977 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
2978 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
2979 struct list_head
*cur
;
2980 struct map_lookup
*map
= NULL
;
2981 struct extent_map_tree
*em_tree
;
2982 struct extent_map
*em
;
2983 struct btrfs_device_info
*devices_info
= NULL
;
2985 int num_stripes
; /* total number of stripes to allocate */
2986 int sub_stripes
; /* sub_stripes info for map */
2987 int dev_stripes
; /* stripes per dev */
2988 int devs_max
; /* max devs to use */
2989 int devs_min
; /* min devs needed */
2990 int devs_increment
; /* ndevs has to be a multiple of this */
2991 int ncopies
; /* how many copies to data has */
2993 u64 max_stripe_size
;
3001 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
3002 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
3004 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
3007 if (list_empty(&fs_devices
->alloc_list
))
3014 devs_max
= 0; /* 0 == as many as possible */
3018 * define the properties of each RAID type.
3019 * FIXME: move this to a global table and use it in all RAID
3022 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3026 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3028 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3033 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3042 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3043 max_stripe_size
= 1024 * 1024 * 1024;
3044 max_chunk_size
= 10 * max_stripe_size
;
3045 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3046 max_stripe_size
= 256 * 1024 * 1024;
3047 max_chunk_size
= max_stripe_size
;
3048 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3049 max_stripe_size
= 8 * 1024 * 1024;
3050 max_chunk_size
= 2 * max_stripe_size
;
3052 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3057 /* we don't want a chunk larger than 10% of writeable space */
3058 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3061 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3066 cur
= fs_devices
->alloc_list
.next
;
3069 * in the first pass through the devices list, we gather information
3070 * about the available holes on each device.
3073 while (cur
!= &fs_devices
->alloc_list
) {
3074 struct btrfs_device
*device
;
3078 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3082 if (!device
->writeable
) {
3084 "btrfs: read-only device in alloc_list\n");
3089 if (!device
->in_fs_metadata
)
3092 if (device
->total_bytes
> device
->bytes_used
)
3093 total_avail
= device
->total_bytes
- device
->bytes_used
;
3097 /* If there is no space on this device, skip it. */
3098 if (total_avail
== 0)
3101 ret
= find_free_dev_extent(trans
, device
,
3102 max_stripe_size
* dev_stripes
,
3103 &dev_offset
, &max_avail
);
3104 if (ret
&& ret
!= -ENOSPC
)
3108 max_avail
= max_stripe_size
* dev_stripes
;
3110 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3113 devices_info
[ndevs
].dev_offset
= dev_offset
;
3114 devices_info
[ndevs
].max_avail
= max_avail
;
3115 devices_info
[ndevs
].total_avail
= total_avail
;
3116 devices_info
[ndevs
].dev
= device
;
3121 * now sort the devices by hole size / available space
3123 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3124 btrfs_cmp_device_info
, NULL
);
3126 /* round down to number of usable stripes */
3127 ndevs
-= ndevs
% devs_increment
;
3129 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3134 if (devs_max
&& ndevs
> devs_max
)
3137 * the primary goal is to maximize the number of stripes, so use as many
3138 * devices as possible, even if the stripes are not maximum sized.
3140 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3141 num_stripes
= ndevs
* dev_stripes
;
3143 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
3144 stripe_size
= max_chunk_size
* ncopies
;
3145 do_div(stripe_size
, num_stripes
);
3148 do_div(stripe_size
, dev_stripes
);
3149 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3150 stripe_size
*= BTRFS_STRIPE_LEN
;
3152 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3157 map
->num_stripes
= num_stripes
;
3159 for (i
= 0; i
< ndevs
; ++i
) {
3160 for (j
= 0; j
< dev_stripes
; ++j
) {
3161 int s
= i
* dev_stripes
+ j
;
3162 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3163 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3167 map
->sector_size
= extent_root
->sectorsize
;
3168 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3169 map
->io_align
= BTRFS_STRIPE_LEN
;
3170 map
->io_width
= BTRFS_STRIPE_LEN
;
3172 map
->sub_stripes
= sub_stripes
;
3175 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3177 *stripe_size_out
= stripe_size
;
3178 *num_bytes_out
= num_bytes
;
3180 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3182 em
= alloc_extent_map();
3187 em
->bdev
= (struct block_device
*)map
;
3189 em
->len
= num_bytes
;
3190 em
->block_start
= 0;
3191 em
->block_len
= em
->len
;
3193 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3194 write_lock(&em_tree
->lock
);
3195 ret
= add_extent_mapping(em_tree
, em
);
3196 write_unlock(&em_tree
->lock
);
3198 free_extent_map(em
);
3200 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3201 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3205 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3206 struct btrfs_device
*device
;
3209 device
= map
->stripes
[i
].dev
;
3210 dev_offset
= map
->stripes
[i
].physical
;
3212 ret
= btrfs_alloc_dev_extent(trans
, device
,
3213 info
->chunk_root
->root_key
.objectid
,
3214 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3215 start
, dev_offset
, stripe_size
);
3219 kfree(devices_info
);
3224 kfree(devices_info
);
3228 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3229 struct btrfs_root
*extent_root
,
3230 struct map_lookup
*map
, u64 chunk_offset
,
3231 u64 chunk_size
, u64 stripe_size
)
3234 struct btrfs_key key
;
3235 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3236 struct btrfs_device
*device
;
3237 struct btrfs_chunk
*chunk
;
3238 struct btrfs_stripe
*stripe
;
3239 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3243 chunk
= kzalloc(item_size
, GFP_NOFS
);
3248 while (index
< map
->num_stripes
) {
3249 device
= map
->stripes
[index
].dev
;
3250 device
->bytes_used
+= stripe_size
;
3251 ret
= btrfs_update_device(trans
, device
);
3256 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3257 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3259 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3262 stripe
= &chunk
->stripe
;
3263 while (index
< map
->num_stripes
) {
3264 device
= map
->stripes
[index
].dev
;
3265 dev_offset
= map
->stripes
[index
].physical
;
3267 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3268 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3269 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3274 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3275 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3276 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3277 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3278 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3279 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3280 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3281 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3282 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3284 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3285 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3286 key
.offset
= chunk_offset
;
3288 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3291 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3292 ret
= btrfs_add_system_chunk(trans
, chunk_root
, &key
, chunk
,
3302 * Chunk allocation falls into two parts. The first part does works
3303 * that make the new allocated chunk useable, but not do any operation
3304 * that modifies the chunk tree. The second part does the works that
3305 * require modifying the chunk tree. This division is important for the
3306 * bootstrap process of adding storage to a seed btrfs.
3308 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3309 struct btrfs_root
*extent_root
, u64 type
)
3314 struct map_lookup
*map
;
3315 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3318 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3323 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3324 &stripe_size
, chunk_offset
, type
);
3328 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3329 chunk_size
, stripe_size
);
3334 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3335 struct btrfs_root
*root
,
3336 struct btrfs_device
*device
)
3339 u64 sys_chunk_offset
;
3343 u64 sys_stripe_size
;
3345 struct map_lookup
*map
;
3346 struct map_lookup
*sys_map
;
3347 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3348 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3351 ret
= find_next_chunk(fs_info
->chunk_root
,
3352 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3356 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3357 fs_info
->avail_metadata_alloc_bits
;
3358 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3360 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3361 &stripe_size
, chunk_offset
, alloc_profile
);
3364 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3366 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3367 fs_info
->avail_system_alloc_bits
;
3368 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3370 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3371 &sys_chunk_size
, &sys_stripe_size
,
3372 sys_chunk_offset
, alloc_profile
);
3375 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3379 * Modifying chunk tree needs allocating new blocks from both
3380 * system block group and metadata block group. So we only can
3381 * do operations require modifying the chunk tree after both
3382 * block groups were created.
3384 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3385 chunk_size
, stripe_size
);
3388 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3389 sys_chunk_offset
, sys_chunk_size
,
3395 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3397 struct extent_map
*em
;
3398 struct map_lookup
*map
;
3399 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3403 read_lock(&map_tree
->map_tree
.lock
);
3404 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3405 read_unlock(&map_tree
->map_tree
.lock
);
3409 if (btrfs_test_opt(root
, DEGRADED
)) {
3410 free_extent_map(em
);
3414 map
= (struct map_lookup
*)em
->bdev
;
3415 for (i
= 0; i
< map
->num_stripes
; i
++) {
3416 if (!map
->stripes
[i
].dev
->writeable
) {
3421 free_extent_map(em
);
3425 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3427 extent_map_tree_init(&tree
->map_tree
);
3430 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3432 struct extent_map
*em
;
3435 write_lock(&tree
->map_tree
.lock
);
3436 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3438 remove_extent_mapping(&tree
->map_tree
, em
);
3439 write_unlock(&tree
->map_tree
.lock
);
3444 free_extent_map(em
);
3445 /* once for the tree */
3446 free_extent_map(em
);
3450 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
3452 struct extent_map
*em
;
3453 struct map_lookup
*map
;
3454 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3457 read_lock(&em_tree
->lock
);
3458 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3459 read_unlock(&em_tree
->lock
);
3462 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3463 map
= (struct map_lookup
*)em
->bdev
;
3464 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
3465 ret
= map
->num_stripes
;
3466 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3467 ret
= map
->sub_stripes
;
3470 free_extent_map(em
);
3474 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
3478 if (map
->stripes
[optimal
].dev
->bdev
)
3480 for (i
= first
; i
< first
+ num
; i
++) {
3481 if (map
->stripes
[i
].dev
->bdev
)
3484 /* we couldn't find one that doesn't fail. Just return something
3485 * and the io error handling code will clean up eventually
3490 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3491 u64 logical
, u64
*length
,
3492 struct btrfs_bio
**bbio_ret
,
3495 struct extent_map
*em
;
3496 struct map_lookup
*map
;
3497 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3500 u64 stripe_end_offset
;
3504 int stripes_allocated
= 8;
3505 int stripes_required
= 1;
3510 struct btrfs_bio
*bbio
= NULL
;
3512 if (bbio_ret
&& !(rw
& (REQ_WRITE
| REQ_DISCARD
)))
3513 stripes_allocated
= 1;
3516 bbio
= kzalloc(btrfs_bio_size(stripes_allocated
),
3521 atomic_set(&bbio
->error
, 0);
3524 read_lock(&em_tree
->lock
);
3525 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
3526 read_unlock(&em_tree
->lock
);
3529 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
3530 (unsigned long long)logical
,
3531 (unsigned long long)*length
);
3535 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3536 map
= (struct map_lookup
*)em
->bdev
;
3537 offset
= logical
- em
->start
;
3539 if (mirror_num
> map
->num_stripes
)
3542 /* if our btrfs_bio struct is too small, back off and try again */
3543 if (rw
& REQ_WRITE
) {
3544 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
3545 BTRFS_BLOCK_GROUP_DUP
)) {
3546 stripes_required
= map
->num_stripes
;
3548 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3549 stripes_required
= map
->sub_stripes
;
3553 if (rw
& REQ_DISCARD
) {
3554 if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
)
3555 stripes_required
= map
->num_stripes
;
3557 if (bbio_ret
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
3558 stripes_allocated
< stripes_required
) {
3559 stripes_allocated
= map
->num_stripes
;
3560 free_extent_map(em
);
3566 * stripe_nr counts the total number of stripes we have to stride
3567 * to get to this block
3569 do_div(stripe_nr
, map
->stripe_len
);
3571 stripe_offset
= stripe_nr
* map
->stripe_len
;
3572 BUG_ON(offset
< stripe_offset
);
3574 /* stripe_offset is the offset of this block in its stripe*/
3575 stripe_offset
= offset
- stripe_offset
;
3577 if (rw
& REQ_DISCARD
)
3578 *length
= min_t(u64
, em
->len
- offset
, *length
);
3579 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
3580 /* we limit the length of each bio to what fits in a stripe */
3581 *length
= min_t(u64
, em
->len
- offset
,
3582 map
->stripe_len
- stripe_offset
);
3584 *length
= em
->len
- offset
;
3592 stripe_nr_orig
= stripe_nr
;
3593 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3594 (~(map
->stripe_len
- 1));
3595 do_div(stripe_nr_end
, map
->stripe_len
);
3596 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3598 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3599 if (rw
& REQ_DISCARD
)
3600 num_stripes
= min_t(u64
, map
->num_stripes
,
3601 stripe_nr_end
- stripe_nr_orig
);
3602 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3603 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3604 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3605 num_stripes
= map
->num_stripes
;
3606 else if (mirror_num
)
3607 stripe_index
= mirror_num
- 1;
3609 stripe_index
= find_live_mirror(map
, 0,
3611 current
->pid
% map
->num_stripes
);
3612 mirror_num
= stripe_index
+ 1;
3615 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3616 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3617 num_stripes
= map
->num_stripes
;
3618 } else if (mirror_num
) {
3619 stripe_index
= mirror_num
- 1;
3624 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3625 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3627 stripe_index
= do_div(stripe_nr
, factor
);
3628 stripe_index
*= map
->sub_stripes
;
3631 num_stripes
= map
->sub_stripes
;
3632 else if (rw
& REQ_DISCARD
)
3633 num_stripes
= min_t(u64
, map
->sub_stripes
*
3634 (stripe_nr_end
- stripe_nr_orig
),
3636 else if (mirror_num
)
3637 stripe_index
+= mirror_num
- 1;
3639 stripe_index
= find_live_mirror(map
, stripe_index
,
3640 map
->sub_stripes
, stripe_index
+
3641 current
->pid
% map
->sub_stripes
);
3642 mirror_num
= stripe_index
+ 1;
3646 * after this do_div call, stripe_nr is the number of stripes
3647 * on this device we have to walk to find the data, and
3648 * stripe_index is the number of our device in the stripe array
3650 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3651 mirror_num
= stripe_index
+ 1;
3653 BUG_ON(stripe_index
>= map
->num_stripes
);
3655 if (rw
& REQ_DISCARD
) {
3656 for (i
= 0; i
< num_stripes
; i
++) {
3657 bbio
->stripes
[i
].physical
=
3658 map
->stripes
[stripe_index
].physical
+
3659 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3660 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3662 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3664 u32 last_stripe
= 0;
3667 div_u64_rem(stripe_nr_end
- 1,
3671 for (j
= 0; j
< map
->num_stripes
; j
++) {
3674 div_u64_rem(stripe_nr_end
- 1 - j
,
3675 map
->num_stripes
, &test
);
3676 if (test
== stripe_index
)
3679 stripes
= stripe_nr_end
- 1 - j
;
3680 do_div(stripes
, map
->num_stripes
);
3681 bbio
->stripes
[i
].length
= map
->stripe_len
*
3682 (stripes
- stripe_nr
+ 1);
3685 bbio
->stripes
[i
].length
-=
3689 if (stripe_index
== last_stripe
)
3690 bbio
->stripes
[i
].length
-=
3692 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3695 int factor
= map
->num_stripes
/
3697 u32 last_stripe
= 0;
3699 div_u64_rem(stripe_nr_end
- 1,
3700 factor
, &last_stripe
);
3701 last_stripe
*= map
->sub_stripes
;
3703 for (j
= 0; j
< factor
; j
++) {
3706 div_u64_rem(stripe_nr_end
- 1 - j
,
3710 stripe_index
/ map
->sub_stripes
)
3713 stripes
= stripe_nr_end
- 1 - j
;
3714 do_div(stripes
, factor
);
3715 bbio
->stripes
[i
].length
= map
->stripe_len
*
3716 (stripes
- stripe_nr
+ 1);
3718 if (i
< map
->sub_stripes
) {
3719 bbio
->stripes
[i
].length
-=
3721 if (i
== map
->sub_stripes
- 1)
3724 if (stripe_index
>= last_stripe
&&
3725 stripe_index
<= (last_stripe
+
3726 map
->sub_stripes
- 1)) {
3727 bbio
->stripes
[i
].length
-=
3731 bbio
->stripes
[i
].length
= *length
;
3734 if (stripe_index
== map
->num_stripes
) {
3735 /* This could only happen for RAID0/10 */
3741 for (i
= 0; i
< num_stripes
; i
++) {
3742 bbio
->stripes
[i
].physical
=
3743 map
->stripes
[stripe_index
].physical
+
3745 stripe_nr
* map
->stripe_len
;
3746 bbio
->stripes
[i
].dev
=
3747 map
->stripes
[stripe_index
].dev
;
3753 bbio
->num_stripes
= num_stripes
;
3754 bbio
->max_errors
= max_errors
;
3755 bbio
->mirror_num
= mirror_num
;
3758 free_extent_map(em
);
3762 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3763 u64 logical
, u64
*length
,
3764 struct btrfs_bio
**bbio_ret
, int mirror_num
)
3766 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
3770 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3771 u64 chunk_start
, u64 physical
, u64 devid
,
3772 u64
**logical
, int *naddrs
, int *stripe_len
)
3774 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3775 struct extent_map
*em
;
3776 struct map_lookup
*map
;
3783 read_lock(&em_tree
->lock
);
3784 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3785 read_unlock(&em_tree
->lock
);
3787 BUG_ON(!em
|| em
->start
!= chunk_start
);
3788 map
= (struct map_lookup
*)em
->bdev
;
3791 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3792 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3793 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3794 do_div(length
, map
->num_stripes
);
3796 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3799 for (i
= 0; i
< map
->num_stripes
; i
++) {
3800 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3802 if (map
->stripes
[i
].physical
> physical
||
3803 map
->stripes
[i
].physical
+ length
<= physical
)
3806 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3807 do_div(stripe_nr
, map
->stripe_len
);
3809 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3810 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3811 do_div(stripe_nr
, map
->sub_stripes
);
3812 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3813 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3815 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3816 WARN_ON(nr
>= map
->num_stripes
);
3817 for (j
= 0; j
< nr
; j
++) {
3818 if (buf
[j
] == bytenr
)
3822 WARN_ON(nr
>= map
->num_stripes
);
3829 *stripe_len
= map
->stripe_len
;
3831 free_extent_map(em
);
3835 static void btrfs_end_bio(struct bio
*bio
, int err
)
3837 struct btrfs_bio
*bbio
= bio
->bi_private
;
3838 int is_orig_bio
= 0;
3841 atomic_inc(&bbio
->error
);
3843 if (bio
== bbio
->orig_bio
)
3846 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
3849 bio
= bbio
->orig_bio
;
3851 bio
->bi_private
= bbio
->private;
3852 bio
->bi_end_io
= bbio
->end_io
;
3853 bio
->bi_bdev
= (struct block_device
*)
3854 (unsigned long)bbio
->mirror_num
;
3855 /* only send an error to the higher layers if it is
3856 * beyond the tolerance of the multi-bio
3858 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
3862 * this bio is actually up to date, we didn't
3863 * go over the max number of errors
3865 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3870 bio_endio(bio
, err
);
3871 } else if (!is_orig_bio
) {
3876 struct async_sched
{
3879 struct btrfs_fs_info
*info
;
3880 struct btrfs_work work
;
3884 * see run_scheduled_bios for a description of why bios are collected for
3887 * This will add one bio to the pending list for a device and make sure
3888 * the work struct is scheduled.
3890 static noinline
int schedule_bio(struct btrfs_root
*root
,
3891 struct btrfs_device
*device
,
3892 int rw
, struct bio
*bio
)
3894 int should_queue
= 1;
3895 struct btrfs_pending_bios
*pending_bios
;
3897 /* don't bother with additional async steps for reads, right now */
3898 if (!(rw
& REQ_WRITE
)) {
3900 submit_bio(rw
, bio
);
3906 * nr_async_bios allows us to reliably return congestion to the
3907 * higher layers. Otherwise, the async bio makes it appear we have
3908 * made progress against dirty pages when we've really just put it
3909 * on a queue for later
3911 atomic_inc(&root
->fs_info
->nr_async_bios
);
3912 WARN_ON(bio
->bi_next
);
3913 bio
->bi_next
= NULL
;
3916 spin_lock(&device
->io_lock
);
3917 if (bio
->bi_rw
& REQ_SYNC
)
3918 pending_bios
= &device
->pending_sync_bios
;
3920 pending_bios
= &device
->pending_bios
;
3922 if (pending_bios
->tail
)
3923 pending_bios
->tail
->bi_next
= bio
;
3925 pending_bios
->tail
= bio
;
3926 if (!pending_bios
->head
)
3927 pending_bios
->head
= bio
;
3928 if (device
->running_pending
)
3931 spin_unlock(&device
->io_lock
);
3934 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
3939 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
3940 int mirror_num
, int async_submit
)
3942 struct btrfs_mapping_tree
*map_tree
;
3943 struct btrfs_device
*dev
;
3944 struct bio
*first_bio
= bio
;
3945 u64 logical
= (u64
)bio
->bi_sector
<< 9;
3951 struct btrfs_bio
*bbio
= NULL
;
3953 length
= bio
->bi_size
;
3954 map_tree
= &root
->fs_info
->mapping_tree
;
3955 map_length
= length
;
3957 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
3961 total_devs
= bbio
->num_stripes
;
3962 if (map_length
< length
) {
3963 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
3964 "len %llu\n", (unsigned long long)logical
,
3965 (unsigned long long)length
,
3966 (unsigned long long)map_length
);
3970 bbio
->orig_bio
= first_bio
;
3971 bbio
->private = first_bio
->bi_private
;
3972 bbio
->end_io
= first_bio
->bi_end_io
;
3973 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
3975 while (dev_nr
< total_devs
) {
3976 if (dev_nr
< total_devs
- 1) {
3977 bio
= bio_clone(first_bio
, GFP_NOFS
);
3982 bio
->bi_private
= bbio
;
3983 bio
->bi_end_io
= btrfs_end_bio
;
3984 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
3985 dev
= bbio
->stripes
[dev_nr
].dev
;
3986 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
3987 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3988 "(%s id %llu), size=%u\n", rw
,
3989 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
3990 dev
->name
, dev
->devid
, bio
->bi_size
);
3991 bio
->bi_bdev
= dev
->bdev
;
3993 schedule_bio(root
, dev
, rw
, bio
);
3995 submit_bio(rw
, bio
);
3997 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3998 bio
->bi_sector
= logical
>> 9;
3999 bio_endio(bio
, -EIO
);
4006 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
4009 struct btrfs_device
*device
;
4010 struct btrfs_fs_devices
*cur_devices
;
4012 cur_devices
= root
->fs_info
->fs_devices
;
4013 while (cur_devices
) {
4015 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4016 device
= __find_device(&cur_devices
->devices
,
4021 cur_devices
= cur_devices
->seed
;
4026 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4027 u64 devid
, u8
*dev_uuid
)
4029 struct btrfs_device
*device
;
4030 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4032 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4035 list_add(&device
->dev_list
,
4036 &fs_devices
->devices
);
4037 device
->dev_root
= root
->fs_info
->dev_root
;
4038 device
->devid
= devid
;
4039 device
->work
.func
= pending_bios_fn
;
4040 device
->fs_devices
= fs_devices
;
4041 device
->missing
= 1;
4042 fs_devices
->num_devices
++;
4043 fs_devices
->missing_devices
++;
4044 spin_lock_init(&device
->io_lock
);
4045 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4046 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4050 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4051 struct extent_buffer
*leaf
,
4052 struct btrfs_chunk
*chunk
)
4054 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4055 struct map_lookup
*map
;
4056 struct extent_map
*em
;
4060 u8 uuid
[BTRFS_UUID_SIZE
];
4065 logical
= key
->offset
;
4066 length
= btrfs_chunk_length(leaf
, chunk
);
4068 read_lock(&map_tree
->map_tree
.lock
);
4069 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4070 read_unlock(&map_tree
->map_tree
.lock
);
4072 /* already mapped? */
4073 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4074 free_extent_map(em
);
4077 free_extent_map(em
);
4080 em
= alloc_extent_map();
4083 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4084 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4086 free_extent_map(em
);
4090 em
->bdev
= (struct block_device
*)map
;
4091 em
->start
= logical
;
4093 em
->block_start
= 0;
4094 em
->block_len
= em
->len
;
4096 map
->num_stripes
= num_stripes
;
4097 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4098 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4099 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4100 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4101 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4102 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4103 for (i
= 0; i
< num_stripes
; i
++) {
4104 map
->stripes
[i
].physical
=
4105 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4106 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4107 read_extent_buffer(leaf
, uuid
, (unsigned long)
4108 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4110 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
4112 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4114 free_extent_map(em
);
4117 if (!map
->stripes
[i
].dev
) {
4118 map
->stripes
[i
].dev
=
4119 add_missing_dev(root
, devid
, uuid
);
4120 if (!map
->stripes
[i
].dev
) {
4122 free_extent_map(em
);
4126 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4129 write_lock(&map_tree
->map_tree
.lock
);
4130 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4131 write_unlock(&map_tree
->map_tree
.lock
);
4133 free_extent_map(em
);
4138 static int fill_device_from_item(struct extent_buffer
*leaf
,
4139 struct btrfs_dev_item
*dev_item
,
4140 struct btrfs_device
*device
)
4144 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4145 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4146 device
->total_bytes
= device
->disk_total_bytes
;
4147 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4148 device
->type
= btrfs_device_type(leaf
, dev_item
);
4149 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4150 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4151 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4153 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4154 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4159 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4161 struct btrfs_fs_devices
*fs_devices
;
4164 mutex_lock(&uuid_mutex
);
4166 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4167 while (fs_devices
) {
4168 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4172 fs_devices
= fs_devices
->seed
;
4175 fs_devices
= find_fsid(fsid
);
4181 fs_devices
= clone_fs_devices(fs_devices
);
4182 if (IS_ERR(fs_devices
)) {
4183 ret
= PTR_ERR(fs_devices
);
4187 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4188 root
->fs_info
->bdev_holder
);
4192 if (!fs_devices
->seeding
) {
4193 __btrfs_close_devices(fs_devices
);
4194 free_fs_devices(fs_devices
);
4199 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4200 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4202 mutex_unlock(&uuid_mutex
);
4206 static int read_one_dev(struct btrfs_root
*root
,
4207 struct extent_buffer
*leaf
,
4208 struct btrfs_dev_item
*dev_item
)
4210 struct btrfs_device
*device
;
4213 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4214 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4216 devid
= btrfs_device_id(leaf
, dev_item
);
4217 read_extent_buffer(leaf
, dev_uuid
,
4218 (unsigned long)btrfs_device_uuid(dev_item
),
4220 read_extent_buffer(leaf
, fs_uuid
,
4221 (unsigned long)btrfs_device_fsid(dev_item
),
4224 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4225 ret
= open_seed_devices(root
, fs_uuid
);
4226 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4230 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
4231 if (!device
|| !device
->bdev
) {
4232 if (!btrfs_test_opt(root
, DEGRADED
))
4236 printk(KERN_WARNING
"warning devid %llu missing\n",
4237 (unsigned long long)devid
);
4238 device
= add_missing_dev(root
, devid
, dev_uuid
);
4241 } else if (!device
->missing
) {
4243 * this happens when a device that was properly setup
4244 * in the device info lists suddenly goes bad.
4245 * device->bdev is NULL, and so we have to set
4246 * device->missing to one here
4248 root
->fs_info
->fs_devices
->missing_devices
++;
4249 device
->missing
= 1;
4253 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4254 BUG_ON(device
->writeable
);
4255 if (device
->generation
!=
4256 btrfs_device_generation(leaf
, dev_item
))
4260 fill_device_from_item(leaf
, dev_item
, device
);
4261 device
->dev_root
= root
->fs_info
->dev_root
;
4262 device
->in_fs_metadata
= 1;
4263 if (device
->writeable
) {
4264 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4265 spin_lock(&root
->fs_info
->free_chunk_lock
);
4266 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4268 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4274 int btrfs_read_sys_array(struct btrfs_root
*root
)
4276 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4277 struct extent_buffer
*sb
;
4278 struct btrfs_disk_key
*disk_key
;
4279 struct btrfs_chunk
*chunk
;
4281 unsigned long sb_ptr
;
4287 struct btrfs_key key
;
4289 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4290 BTRFS_SUPER_INFO_SIZE
);
4293 btrfs_set_buffer_uptodate(sb
);
4294 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4296 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4297 array_size
= btrfs_super_sys_array_size(super_copy
);
4299 ptr
= super_copy
->sys_chunk_array
;
4300 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4303 while (cur
< array_size
) {
4304 disk_key
= (struct btrfs_disk_key
*)ptr
;
4305 btrfs_disk_key_to_cpu(&key
, disk_key
);
4307 len
= sizeof(*disk_key
); ptr
+= len
;
4311 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4312 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4313 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4316 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4317 len
= btrfs_chunk_item_size(num_stripes
);
4326 free_extent_buffer(sb
);
4330 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
4332 struct btrfs_path
*path
;
4333 struct extent_buffer
*leaf
;
4334 struct btrfs_key key
;
4335 struct btrfs_key found_key
;
4339 root
= root
->fs_info
->chunk_root
;
4341 path
= btrfs_alloc_path();
4345 /* first we search for all of the device items, and then we
4346 * read in all of the chunk items. This way we can create chunk
4347 * mappings that reference all of the devices that are afound
4349 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
4353 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4357 leaf
= path
->nodes
[0];
4358 slot
= path
->slots
[0];
4359 if (slot
>= btrfs_header_nritems(leaf
)) {
4360 ret
= btrfs_next_leaf(root
, path
);
4367 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4368 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4369 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
4371 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
4372 struct btrfs_dev_item
*dev_item
;
4373 dev_item
= btrfs_item_ptr(leaf
, slot
,
4374 struct btrfs_dev_item
);
4375 ret
= read_one_dev(root
, leaf
, dev_item
);
4379 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4380 struct btrfs_chunk
*chunk
;
4381 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
4382 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
4388 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4390 btrfs_release_path(path
);
4395 btrfs_free_path(path
);