2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <linux/kthread.h>
27 #include <asm/div64.h>
30 #include "extent_map.h"
32 #include "transaction.h"
33 #include "print-tree.h"
35 #include "async-thread.h"
36 #include "check-integrity.h"
38 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
39 struct btrfs_root
*root
,
40 struct btrfs_device
*device
);
41 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
43 static DEFINE_MUTEX(uuid_mutex
);
44 static LIST_HEAD(fs_uuids
);
46 static void lock_chunks(struct btrfs_root
*root
)
48 mutex_lock(&root
->fs_info
->chunk_mutex
);
51 static void unlock_chunks(struct btrfs_root
*root
)
53 mutex_unlock(&root
->fs_info
->chunk_mutex
);
56 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
58 struct btrfs_device
*device
;
59 WARN_ON(fs_devices
->opened
);
60 while (!list_empty(&fs_devices
->devices
)) {
61 device
= list_entry(fs_devices
->devices
.next
,
62 struct btrfs_device
, dev_list
);
63 list_del(&device
->dev_list
);
70 void btrfs_cleanup_fs_uuids(void)
72 struct btrfs_fs_devices
*fs_devices
;
74 while (!list_empty(&fs_uuids
)) {
75 fs_devices
= list_entry(fs_uuids
.next
,
76 struct btrfs_fs_devices
, list
);
77 list_del(&fs_devices
->list
);
78 free_fs_devices(fs_devices
);
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
void 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 btrfsic_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
);
320 static void pending_bios_fn(struct btrfs_work
*work
)
322 struct btrfs_device
*device
;
324 device
= container_of(work
, struct btrfs_device
, work
);
325 run_scheduled_bios(device
);
328 static noinline
int device_list_add(const char *path
,
329 struct btrfs_super_block
*disk_super
,
330 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
332 struct btrfs_device
*device
;
333 struct btrfs_fs_devices
*fs_devices
;
334 u64 found_transid
= btrfs_super_generation(disk_super
);
337 fs_devices
= find_fsid(disk_super
->fsid
);
339 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
342 INIT_LIST_HEAD(&fs_devices
->devices
);
343 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
344 list_add(&fs_devices
->list
, &fs_uuids
);
345 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
346 fs_devices
->latest_devid
= devid
;
347 fs_devices
->latest_trans
= found_transid
;
348 mutex_init(&fs_devices
->device_list_mutex
);
351 device
= __find_device(&fs_devices
->devices
, devid
,
352 disk_super
->dev_item
.uuid
);
355 if (fs_devices
->opened
)
358 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
360 /* we can safely leave the fs_devices entry around */
363 device
->devid
= devid
;
364 device
->work
.func
= pending_bios_fn
;
365 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
367 spin_lock_init(&device
->io_lock
);
368 device
->name
= kstrdup(path
, GFP_NOFS
);
373 INIT_LIST_HEAD(&device
->dev_alloc_list
);
375 /* init readahead state */
376 spin_lock_init(&device
->reada_lock
);
377 device
->reada_curr_zone
= NULL
;
378 atomic_set(&device
->reada_in_flight
, 0);
379 device
->reada_next
= 0;
380 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
381 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
383 mutex_lock(&fs_devices
->device_list_mutex
);
384 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
385 mutex_unlock(&fs_devices
->device_list_mutex
);
387 device
->fs_devices
= fs_devices
;
388 fs_devices
->num_devices
++;
389 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
390 name
= kstrdup(path
, GFP_NOFS
);
395 if (device
->missing
) {
396 fs_devices
->missing_devices
--;
401 if (found_transid
> fs_devices
->latest_trans
) {
402 fs_devices
->latest_devid
= devid
;
403 fs_devices
->latest_trans
= found_transid
;
405 *fs_devices_ret
= fs_devices
;
409 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
411 struct btrfs_fs_devices
*fs_devices
;
412 struct btrfs_device
*device
;
413 struct btrfs_device
*orig_dev
;
415 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
417 return ERR_PTR(-ENOMEM
);
419 INIT_LIST_HEAD(&fs_devices
->devices
);
420 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
421 INIT_LIST_HEAD(&fs_devices
->list
);
422 mutex_init(&fs_devices
->device_list_mutex
);
423 fs_devices
->latest_devid
= orig
->latest_devid
;
424 fs_devices
->latest_trans
= orig
->latest_trans
;
425 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
427 /* We have held the volume lock, it is safe to get the devices. */
428 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
429 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
433 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
439 device
->devid
= orig_dev
->devid
;
440 device
->work
.func
= pending_bios_fn
;
441 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
442 spin_lock_init(&device
->io_lock
);
443 INIT_LIST_HEAD(&device
->dev_list
);
444 INIT_LIST_HEAD(&device
->dev_alloc_list
);
446 list_add(&device
->dev_list
, &fs_devices
->devices
);
447 device
->fs_devices
= fs_devices
;
448 fs_devices
->num_devices
++;
452 free_fs_devices(fs_devices
);
453 return ERR_PTR(-ENOMEM
);
456 void btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
458 struct btrfs_device
*device
, *next
;
460 struct block_device
*latest_bdev
= NULL
;
461 u64 latest_devid
= 0;
462 u64 latest_transid
= 0;
464 mutex_lock(&uuid_mutex
);
466 /* This is the initialized path, it is safe to release the devices. */
467 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
468 if (device
->in_fs_metadata
) {
469 if (!latest_transid
||
470 device
->generation
> latest_transid
) {
471 latest_devid
= device
->devid
;
472 latest_transid
= device
->generation
;
473 latest_bdev
= device
->bdev
;
479 blkdev_put(device
->bdev
, device
->mode
);
481 fs_devices
->open_devices
--;
483 if (device
->writeable
) {
484 list_del_init(&device
->dev_alloc_list
);
485 device
->writeable
= 0;
486 fs_devices
->rw_devices
--;
488 list_del_init(&device
->dev_list
);
489 fs_devices
->num_devices
--;
494 if (fs_devices
->seed
) {
495 fs_devices
= fs_devices
->seed
;
499 fs_devices
->latest_bdev
= latest_bdev
;
500 fs_devices
->latest_devid
= latest_devid
;
501 fs_devices
->latest_trans
= latest_transid
;
503 mutex_unlock(&uuid_mutex
);
506 static void __free_device(struct work_struct
*work
)
508 struct btrfs_device
*device
;
510 device
= container_of(work
, struct btrfs_device
, rcu_work
);
513 blkdev_put(device
->bdev
, device
->mode
);
519 static void free_device(struct rcu_head
*head
)
521 struct btrfs_device
*device
;
523 device
= container_of(head
, struct btrfs_device
, rcu
);
525 INIT_WORK(&device
->rcu_work
, __free_device
);
526 schedule_work(&device
->rcu_work
);
529 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
531 struct btrfs_device
*device
;
533 if (--fs_devices
->opened
> 0)
536 mutex_lock(&fs_devices
->device_list_mutex
);
537 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
538 struct btrfs_device
*new_device
;
541 fs_devices
->open_devices
--;
543 if (device
->writeable
) {
544 list_del_init(&device
->dev_alloc_list
);
545 fs_devices
->rw_devices
--;
548 if (device
->can_discard
)
549 fs_devices
->num_can_discard
--;
551 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
553 memcpy(new_device
, device
, sizeof(*new_device
));
554 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
555 BUG_ON(device
->name
&& !new_device
->name
);
556 new_device
->bdev
= NULL
;
557 new_device
->writeable
= 0;
558 new_device
->in_fs_metadata
= 0;
559 new_device
->can_discard
= 0;
560 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
562 call_rcu(&device
->rcu
, free_device
);
564 mutex_unlock(&fs_devices
->device_list_mutex
);
566 WARN_ON(fs_devices
->open_devices
);
567 WARN_ON(fs_devices
->rw_devices
);
568 fs_devices
->opened
= 0;
569 fs_devices
->seeding
= 0;
574 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
576 struct btrfs_fs_devices
*seed_devices
= NULL
;
579 mutex_lock(&uuid_mutex
);
580 ret
= __btrfs_close_devices(fs_devices
);
581 if (!fs_devices
->opened
) {
582 seed_devices
= fs_devices
->seed
;
583 fs_devices
->seed
= NULL
;
585 mutex_unlock(&uuid_mutex
);
587 while (seed_devices
) {
588 fs_devices
= seed_devices
;
589 seed_devices
= fs_devices
->seed
;
590 __btrfs_close_devices(fs_devices
);
591 free_fs_devices(fs_devices
);
596 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
597 fmode_t flags
, void *holder
)
599 struct request_queue
*q
;
600 struct block_device
*bdev
;
601 struct list_head
*head
= &fs_devices
->devices
;
602 struct btrfs_device
*device
;
603 struct block_device
*latest_bdev
= NULL
;
604 struct buffer_head
*bh
;
605 struct btrfs_super_block
*disk_super
;
606 u64 latest_devid
= 0;
607 u64 latest_transid
= 0;
614 list_for_each_entry(device
, head
, dev_list
) {
620 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
622 printk(KERN_INFO
"open %s failed\n", device
->name
);
625 set_blocksize(bdev
, 4096);
627 bh
= btrfs_read_dev_super(bdev
);
631 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
632 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
633 if (devid
!= device
->devid
)
636 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
640 device
->generation
= btrfs_super_generation(disk_super
);
641 if (!latest_transid
|| device
->generation
> latest_transid
) {
642 latest_devid
= devid
;
643 latest_transid
= device
->generation
;
647 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
648 device
->writeable
= 0;
650 device
->writeable
= !bdev_read_only(bdev
);
654 q
= bdev_get_queue(bdev
);
655 if (blk_queue_discard(q
)) {
656 device
->can_discard
= 1;
657 fs_devices
->num_can_discard
++;
661 device
->in_fs_metadata
= 0;
662 device
->mode
= flags
;
664 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
665 fs_devices
->rotating
= 1;
667 fs_devices
->open_devices
++;
668 if (device
->writeable
) {
669 fs_devices
->rw_devices
++;
670 list_add(&device
->dev_alloc_list
,
671 &fs_devices
->alloc_list
);
679 blkdev_put(bdev
, flags
);
683 if (fs_devices
->open_devices
== 0) {
687 fs_devices
->seeding
= seeding
;
688 fs_devices
->opened
= 1;
689 fs_devices
->latest_bdev
= latest_bdev
;
690 fs_devices
->latest_devid
= latest_devid
;
691 fs_devices
->latest_trans
= latest_transid
;
692 fs_devices
->total_rw_bytes
= 0;
697 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
698 fmode_t flags
, void *holder
)
702 mutex_lock(&uuid_mutex
);
703 if (fs_devices
->opened
) {
704 fs_devices
->opened
++;
707 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
709 mutex_unlock(&uuid_mutex
);
713 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
714 struct btrfs_fs_devices
**fs_devices_ret
)
716 struct btrfs_super_block
*disk_super
;
717 struct block_device
*bdev
;
718 struct buffer_head
*bh
;
724 bdev
= blkdev_get_by_path(path
, flags
, holder
);
731 mutex_lock(&uuid_mutex
);
732 ret
= set_blocksize(bdev
, 4096);
735 bh
= btrfs_read_dev_super(bdev
);
740 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
741 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
742 transid
= btrfs_super_generation(disk_super
);
743 if (disk_super
->label
[0])
744 printk(KERN_INFO
"device label %s ", disk_super
->label
);
746 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
747 printk(KERN_CONT
"devid %llu transid %llu %s\n",
748 (unsigned long long)devid
, (unsigned long long)transid
, path
);
749 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
753 mutex_unlock(&uuid_mutex
);
754 blkdev_put(bdev
, flags
);
759 /* helper to account the used device space in the range */
760 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
761 u64 end
, u64
*length
)
763 struct btrfs_key key
;
764 struct btrfs_root
*root
= device
->dev_root
;
765 struct btrfs_dev_extent
*dev_extent
;
766 struct btrfs_path
*path
;
770 struct extent_buffer
*l
;
774 if (start
>= device
->total_bytes
)
777 path
= btrfs_alloc_path();
782 key
.objectid
= device
->devid
;
784 key
.type
= BTRFS_DEV_EXTENT_KEY
;
786 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
790 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
797 slot
= path
->slots
[0];
798 if (slot
>= btrfs_header_nritems(l
)) {
799 ret
= btrfs_next_leaf(root
, path
);
807 btrfs_item_key_to_cpu(l
, &key
, slot
);
809 if (key
.objectid
< device
->devid
)
812 if (key
.objectid
> device
->devid
)
815 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
818 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
819 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
821 if (key
.offset
<= start
&& extent_end
> end
) {
822 *length
= end
- start
+ 1;
824 } else if (key
.offset
<= start
&& extent_end
> start
)
825 *length
+= extent_end
- start
;
826 else if (key
.offset
> start
&& extent_end
<= end
)
827 *length
+= extent_end
- key
.offset
;
828 else if (key
.offset
> start
&& key
.offset
<= end
) {
829 *length
+= end
- key
.offset
+ 1;
831 } else if (key
.offset
> end
)
839 btrfs_free_path(path
);
844 * find_free_dev_extent - find free space in the specified device
845 * @device: the device which we search the free space in
846 * @num_bytes: the size of the free space that we need
847 * @start: store the start of the free space.
848 * @len: the size of the free space. that we find, or the size of the max
849 * free space if we don't find suitable free space
851 * this uses a pretty simple search, the expectation is that it is
852 * called very infrequently and that a given device has a small number
855 * @start is used to store the start of the free space if we find. But if we
856 * don't find suitable free space, it will be used to store the start position
857 * of the max free space.
859 * @len is used to store the size of the free space that we find.
860 * But if we don't find suitable free space, it is used to store the size of
861 * the max free space.
863 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
864 u64
*start
, u64
*len
)
866 struct btrfs_key key
;
867 struct btrfs_root
*root
= device
->dev_root
;
868 struct btrfs_dev_extent
*dev_extent
;
869 struct btrfs_path
*path
;
875 u64 search_end
= device
->total_bytes
;
878 struct extent_buffer
*l
;
880 /* FIXME use last free of some kind */
882 /* we don't want to overwrite the superblock on the drive,
883 * so we make sure to start at an offset of at least 1MB
885 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
887 max_hole_start
= search_start
;
891 if (search_start
>= search_end
) {
896 path
= btrfs_alloc_path();
903 key
.objectid
= device
->devid
;
904 key
.offset
= search_start
;
905 key
.type
= BTRFS_DEV_EXTENT_KEY
;
907 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
911 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
918 slot
= path
->slots
[0];
919 if (slot
>= btrfs_header_nritems(l
)) {
920 ret
= btrfs_next_leaf(root
, path
);
928 btrfs_item_key_to_cpu(l
, &key
, slot
);
930 if (key
.objectid
< device
->devid
)
933 if (key
.objectid
> device
->devid
)
936 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
939 if (key
.offset
> search_start
) {
940 hole_size
= key
.offset
- search_start
;
942 if (hole_size
> max_hole_size
) {
943 max_hole_start
= search_start
;
944 max_hole_size
= hole_size
;
948 * If this free space is greater than which we need,
949 * it must be the max free space that we have found
950 * until now, so max_hole_start must point to the start
951 * of this free space and the length of this free space
952 * is stored in max_hole_size. Thus, we return
953 * max_hole_start and max_hole_size and go back to the
956 if (hole_size
>= num_bytes
) {
962 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
963 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
965 if (extent_end
> search_start
)
966 search_start
= extent_end
;
973 * At this point, search_start should be the end of
974 * allocated dev extents, and when shrinking the device,
975 * search_end may be smaller than search_start.
977 if (search_end
> search_start
)
978 hole_size
= search_end
- search_start
;
980 if (hole_size
> max_hole_size
) {
981 max_hole_start
= search_start
;
982 max_hole_size
= hole_size
;
986 if (hole_size
< num_bytes
)
992 btrfs_free_path(path
);
994 *start
= max_hole_start
;
996 *len
= max_hole_size
;
1000 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1001 struct btrfs_device
*device
,
1005 struct btrfs_path
*path
;
1006 struct btrfs_root
*root
= device
->dev_root
;
1007 struct btrfs_key key
;
1008 struct btrfs_key found_key
;
1009 struct extent_buffer
*leaf
= NULL
;
1010 struct btrfs_dev_extent
*extent
= NULL
;
1012 path
= btrfs_alloc_path();
1016 key
.objectid
= device
->devid
;
1018 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1020 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1022 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1023 BTRFS_DEV_EXTENT_KEY
);
1026 leaf
= path
->nodes
[0];
1027 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1028 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1029 struct btrfs_dev_extent
);
1030 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1031 btrfs_dev_extent_length(leaf
, extent
) < start
);
1033 btrfs_release_path(path
);
1035 } else if (ret
== 0) {
1036 leaf
= path
->nodes
[0];
1037 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1038 struct btrfs_dev_extent
);
1042 if (device
->bytes_used
> 0) {
1043 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1044 device
->bytes_used
-= len
;
1045 spin_lock(&root
->fs_info
->free_chunk_lock
);
1046 root
->fs_info
->free_chunk_space
+= len
;
1047 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1049 ret
= btrfs_del_item(trans
, root
, path
);
1052 btrfs_free_path(path
);
1056 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1057 struct btrfs_device
*device
,
1058 u64 chunk_tree
, u64 chunk_objectid
,
1059 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1062 struct btrfs_path
*path
;
1063 struct btrfs_root
*root
= device
->dev_root
;
1064 struct btrfs_dev_extent
*extent
;
1065 struct extent_buffer
*leaf
;
1066 struct btrfs_key key
;
1068 WARN_ON(!device
->in_fs_metadata
);
1069 path
= btrfs_alloc_path();
1073 key
.objectid
= device
->devid
;
1075 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1076 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1080 leaf
= path
->nodes
[0];
1081 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1082 struct btrfs_dev_extent
);
1083 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1084 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1085 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1087 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1088 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1091 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1092 btrfs_mark_buffer_dirty(leaf
);
1093 btrfs_free_path(path
);
1097 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1098 u64 objectid
, u64
*offset
)
1100 struct btrfs_path
*path
;
1102 struct btrfs_key key
;
1103 struct btrfs_chunk
*chunk
;
1104 struct btrfs_key found_key
;
1106 path
= btrfs_alloc_path();
1110 key
.objectid
= objectid
;
1111 key
.offset
= (u64
)-1;
1112 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1114 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1120 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1124 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1126 if (found_key
.objectid
!= objectid
)
1129 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1130 struct btrfs_chunk
);
1131 *offset
= found_key
.offset
+
1132 btrfs_chunk_length(path
->nodes
[0], chunk
);
1137 btrfs_free_path(path
);
1141 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1144 struct btrfs_key key
;
1145 struct btrfs_key found_key
;
1146 struct btrfs_path
*path
;
1148 root
= root
->fs_info
->chunk_root
;
1150 path
= btrfs_alloc_path();
1154 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1155 key
.type
= BTRFS_DEV_ITEM_KEY
;
1156 key
.offset
= (u64
)-1;
1158 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1164 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1165 BTRFS_DEV_ITEM_KEY
);
1169 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1171 *objectid
= found_key
.offset
+ 1;
1175 btrfs_free_path(path
);
1180 * the device information is stored in the chunk root
1181 * the btrfs_device struct should be fully filled in
1183 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1184 struct btrfs_root
*root
,
1185 struct btrfs_device
*device
)
1188 struct btrfs_path
*path
;
1189 struct btrfs_dev_item
*dev_item
;
1190 struct extent_buffer
*leaf
;
1191 struct btrfs_key key
;
1194 root
= root
->fs_info
->chunk_root
;
1196 path
= btrfs_alloc_path();
1200 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1201 key
.type
= BTRFS_DEV_ITEM_KEY
;
1202 key
.offset
= device
->devid
;
1204 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1209 leaf
= path
->nodes
[0];
1210 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1212 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1213 btrfs_set_device_generation(leaf
, dev_item
, 0);
1214 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1215 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1216 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1217 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1218 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1219 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1220 btrfs_set_device_group(leaf
, dev_item
, 0);
1221 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1222 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1223 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1225 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1226 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1227 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1228 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1229 btrfs_mark_buffer_dirty(leaf
);
1233 btrfs_free_path(path
);
1237 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1238 struct btrfs_device
*device
)
1241 struct btrfs_path
*path
;
1242 struct btrfs_key key
;
1243 struct btrfs_trans_handle
*trans
;
1245 root
= root
->fs_info
->chunk_root
;
1247 path
= btrfs_alloc_path();
1251 trans
= btrfs_start_transaction(root
, 0);
1252 if (IS_ERR(trans
)) {
1253 btrfs_free_path(path
);
1254 return PTR_ERR(trans
);
1256 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1257 key
.type
= BTRFS_DEV_ITEM_KEY
;
1258 key
.offset
= device
->devid
;
1261 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1270 ret
= btrfs_del_item(trans
, root
, path
);
1274 btrfs_free_path(path
);
1275 unlock_chunks(root
);
1276 btrfs_commit_transaction(trans
, root
);
1280 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1282 struct btrfs_device
*device
;
1283 struct btrfs_device
*next_device
;
1284 struct block_device
*bdev
;
1285 struct buffer_head
*bh
= NULL
;
1286 struct btrfs_super_block
*disk_super
;
1287 struct btrfs_fs_devices
*cur_devices
;
1293 bool clear_super
= false;
1295 mutex_lock(&uuid_mutex
);
1297 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1298 root
->fs_info
->avail_system_alloc_bits
|
1299 root
->fs_info
->avail_metadata_alloc_bits
;
1301 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1302 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1303 printk(KERN_ERR
"btrfs: unable to go below four devices "
1309 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1310 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1311 printk(KERN_ERR
"btrfs: unable to go below two "
1312 "devices on raid1\n");
1317 if (strcmp(device_path
, "missing") == 0) {
1318 struct list_head
*devices
;
1319 struct btrfs_device
*tmp
;
1322 devices
= &root
->fs_info
->fs_devices
->devices
;
1324 * It is safe to read the devices since the volume_mutex
1327 list_for_each_entry(tmp
, devices
, dev_list
) {
1328 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1337 printk(KERN_ERR
"btrfs: no missing devices found to "
1342 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1343 root
->fs_info
->bdev_holder
);
1345 ret
= PTR_ERR(bdev
);
1349 set_blocksize(bdev
, 4096);
1350 bh
= btrfs_read_dev_super(bdev
);
1355 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1356 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1357 dev_uuid
= disk_super
->dev_item
.uuid
;
1358 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1366 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1367 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1373 if (device
->writeable
) {
1375 list_del_init(&device
->dev_alloc_list
);
1376 unlock_chunks(root
);
1377 root
->fs_info
->fs_devices
->rw_devices
--;
1381 ret
= btrfs_shrink_device(device
, 0);
1385 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1389 spin_lock(&root
->fs_info
->free_chunk_lock
);
1390 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1392 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1394 device
->in_fs_metadata
= 0;
1395 btrfs_scrub_cancel_dev(root
, device
);
1398 * the device list mutex makes sure that we don't change
1399 * the device list while someone else is writing out all
1400 * the device supers.
1403 cur_devices
= device
->fs_devices
;
1404 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1405 list_del_rcu(&device
->dev_list
);
1407 device
->fs_devices
->num_devices
--;
1409 if (device
->missing
)
1410 root
->fs_info
->fs_devices
->missing_devices
--;
1412 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1413 struct btrfs_device
, dev_list
);
1414 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1415 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1416 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1417 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1420 device
->fs_devices
->open_devices
--;
1422 call_rcu(&device
->rcu
, free_device
);
1423 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1425 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1426 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1428 if (cur_devices
->open_devices
== 0) {
1429 struct btrfs_fs_devices
*fs_devices
;
1430 fs_devices
= root
->fs_info
->fs_devices
;
1431 while (fs_devices
) {
1432 if (fs_devices
->seed
== cur_devices
)
1434 fs_devices
= fs_devices
->seed
;
1436 fs_devices
->seed
= cur_devices
->seed
;
1437 cur_devices
->seed
= NULL
;
1439 __btrfs_close_devices(cur_devices
);
1440 unlock_chunks(root
);
1441 free_fs_devices(cur_devices
);
1445 * at this point, the device is zero sized. We want to
1446 * remove it from the devices list and zero out the old super
1449 /* make sure this device isn't detected as part of
1452 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1453 set_buffer_dirty(bh
);
1454 sync_dirty_buffer(bh
);
1463 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1465 mutex_unlock(&uuid_mutex
);
1468 if (device
->writeable
) {
1470 list_add(&device
->dev_alloc_list
,
1471 &root
->fs_info
->fs_devices
->alloc_list
);
1472 unlock_chunks(root
);
1473 root
->fs_info
->fs_devices
->rw_devices
++;
1479 * does all the dirty work required for changing file system's UUID.
1481 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1483 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1484 struct btrfs_fs_devices
*old_devices
;
1485 struct btrfs_fs_devices
*seed_devices
;
1486 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1487 struct btrfs_device
*device
;
1490 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1491 if (!fs_devices
->seeding
)
1494 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1498 old_devices
= clone_fs_devices(fs_devices
);
1499 if (IS_ERR(old_devices
)) {
1500 kfree(seed_devices
);
1501 return PTR_ERR(old_devices
);
1504 list_add(&old_devices
->list
, &fs_uuids
);
1506 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1507 seed_devices
->opened
= 1;
1508 INIT_LIST_HEAD(&seed_devices
->devices
);
1509 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1510 mutex_init(&seed_devices
->device_list_mutex
);
1512 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1513 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1515 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1517 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1518 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1519 device
->fs_devices
= seed_devices
;
1522 fs_devices
->seeding
= 0;
1523 fs_devices
->num_devices
= 0;
1524 fs_devices
->open_devices
= 0;
1525 fs_devices
->seed
= seed_devices
;
1527 generate_random_uuid(fs_devices
->fsid
);
1528 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1529 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1530 super_flags
= btrfs_super_flags(disk_super
) &
1531 ~BTRFS_SUPER_FLAG_SEEDING
;
1532 btrfs_set_super_flags(disk_super
, super_flags
);
1538 * strore the expected generation for seed devices in device items.
1540 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1541 struct btrfs_root
*root
)
1543 struct btrfs_path
*path
;
1544 struct extent_buffer
*leaf
;
1545 struct btrfs_dev_item
*dev_item
;
1546 struct btrfs_device
*device
;
1547 struct btrfs_key key
;
1548 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1549 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1553 path
= btrfs_alloc_path();
1557 root
= root
->fs_info
->chunk_root
;
1558 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1560 key
.type
= BTRFS_DEV_ITEM_KEY
;
1563 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1567 leaf
= path
->nodes
[0];
1569 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1570 ret
= btrfs_next_leaf(root
, path
);
1575 leaf
= path
->nodes
[0];
1576 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1577 btrfs_release_path(path
);
1581 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1582 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1583 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1586 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1587 struct btrfs_dev_item
);
1588 devid
= btrfs_device_id(leaf
, dev_item
);
1589 read_extent_buffer(leaf
, dev_uuid
,
1590 (unsigned long)btrfs_device_uuid(dev_item
),
1592 read_extent_buffer(leaf
, fs_uuid
,
1593 (unsigned long)btrfs_device_fsid(dev_item
),
1595 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1598 if (device
->fs_devices
->seeding
) {
1599 btrfs_set_device_generation(leaf
, dev_item
,
1600 device
->generation
);
1601 btrfs_mark_buffer_dirty(leaf
);
1609 btrfs_free_path(path
);
1613 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1615 struct request_queue
*q
;
1616 struct btrfs_trans_handle
*trans
;
1617 struct btrfs_device
*device
;
1618 struct block_device
*bdev
;
1619 struct list_head
*devices
;
1620 struct super_block
*sb
= root
->fs_info
->sb
;
1622 int seeding_dev
= 0;
1625 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1628 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1629 root
->fs_info
->bdev_holder
);
1631 return PTR_ERR(bdev
);
1633 if (root
->fs_info
->fs_devices
->seeding
) {
1635 down_write(&sb
->s_umount
);
1636 mutex_lock(&uuid_mutex
);
1639 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1641 devices
= &root
->fs_info
->fs_devices
->devices
;
1643 * we have the volume lock, so we don't need the extra
1644 * device list mutex while reading the list here.
1646 list_for_each_entry(device
, devices
, dev_list
) {
1647 if (device
->bdev
== bdev
) {
1653 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1655 /* we can safely leave the fs_devices entry around */
1660 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1661 if (!device
->name
) {
1667 ret
= find_next_devid(root
, &device
->devid
);
1669 kfree(device
->name
);
1674 trans
= btrfs_start_transaction(root
, 0);
1675 if (IS_ERR(trans
)) {
1676 kfree(device
->name
);
1678 ret
= PTR_ERR(trans
);
1684 q
= bdev_get_queue(bdev
);
1685 if (blk_queue_discard(q
))
1686 device
->can_discard
= 1;
1687 device
->writeable
= 1;
1688 device
->work
.func
= pending_bios_fn
;
1689 generate_random_uuid(device
->uuid
);
1690 spin_lock_init(&device
->io_lock
);
1691 device
->generation
= trans
->transid
;
1692 device
->io_width
= root
->sectorsize
;
1693 device
->io_align
= root
->sectorsize
;
1694 device
->sector_size
= root
->sectorsize
;
1695 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1696 device
->disk_total_bytes
= device
->total_bytes
;
1697 device
->dev_root
= root
->fs_info
->dev_root
;
1698 device
->bdev
= bdev
;
1699 device
->in_fs_metadata
= 1;
1700 device
->mode
= FMODE_EXCL
;
1701 set_blocksize(device
->bdev
, 4096);
1704 sb
->s_flags
&= ~MS_RDONLY
;
1705 ret
= btrfs_prepare_sprout(root
);
1709 device
->fs_devices
= root
->fs_info
->fs_devices
;
1712 * we don't want write_supers to jump in here with our device
1715 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1716 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1717 list_add(&device
->dev_alloc_list
,
1718 &root
->fs_info
->fs_devices
->alloc_list
);
1719 root
->fs_info
->fs_devices
->num_devices
++;
1720 root
->fs_info
->fs_devices
->open_devices
++;
1721 root
->fs_info
->fs_devices
->rw_devices
++;
1722 if (device
->can_discard
)
1723 root
->fs_info
->fs_devices
->num_can_discard
++;
1724 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1726 spin_lock(&root
->fs_info
->free_chunk_lock
);
1727 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1728 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1730 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1731 root
->fs_info
->fs_devices
->rotating
= 1;
1733 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1734 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1735 total_bytes
+ device
->total_bytes
);
1737 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1738 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1740 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1743 ret
= init_first_rw_device(trans
, root
, device
);
1745 ret
= btrfs_finish_sprout(trans
, root
);
1748 ret
= btrfs_add_device(trans
, root
, device
);
1752 * we've got more storage, clear any full flags on the space
1755 btrfs_clear_space_info_full(root
->fs_info
);
1757 unlock_chunks(root
);
1758 btrfs_commit_transaction(trans
, root
);
1761 mutex_unlock(&uuid_mutex
);
1762 up_write(&sb
->s_umount
);
1764 ret
= btrfs_relocate_sys_chunks(root
);
1770 blkdev_put(bdev
, FMODE_EXCL
);
1772 mutex_unlock(&uuid_mutex
);
1773 up_write(&sb
->s_umount
);
1778 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1779 struct btrfs_device
*device
)
1782 struct btrfs_path
*path
;
1783 struct btrfs_root
*root
;
1784 struct btrfs_dev_item
*dev_item
;
1785 struct extent_buffer
*leaf
;
1786 struct btrfs_key key
;
1788 root
= device
->dev_root
->fs_info
->chunk_root
;
1790 path
= btrfs_alloc_path();
1794 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1795 key
.type
= BTRFS_DEV_ITEM_KEY
;
1796 key
.offset
= device
->devid
;
1798 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1807 leaf
= path
->nodes
[0];
1808 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1810 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1811 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1812 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1813 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1814 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1815 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1816 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1817 btrfs_mark_buffer_dirty(leaf
);
1820 btrfs_free_path(path
);
1824 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1825 struct btrfs_device
*device
, u64 new_size
)
1827 struct btrfs_super_block
*super_copy
=
1828 device
->dev_root
->fs_info
->super_copy
;
1829 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1830 u64 diff
= new_size
- device
->total_bytes
;
1832 if (!device
->writeable
)
1834 if (new_size
<= device
->total_bytes
)
1837 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1838 device
->fs_devices
->total_rw_bytes
+= diff
;
1840 device
->total_bytes
= new_size
;
1841 device
->disk_total_bytes
= new_size
;
1842 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1844 return btrfs_update_device(trans
, device
);
1847 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1848 struct btrfs_device
*device
, u64 new_size
)
1851 lock_chunks(device
->dev_root
);
1852 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1853 unlock_chunks(device
->dev_root
);
1857 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1858 struct btrfs_root
*root
,
1859 u64 chunk_tree
, u64 chunk_objectid
,
1863 struct btrfs_path
*path
;
1864 struct btrfs_key key
;
1866 root
= root
->fs_info
->chunk_root
;
1867 path
= btrfs_alloc_path();
1871 key
.objectid
= chunk_objectid
;
1872 key
.offset
= chunk_offset
;
1873 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1875 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1878 ret
= btrfs_del_item(trans
, root
, path
);
1880 btrfs_free_path(path
);
1884 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1887 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1888 struct btrfs_disk_key
*disk_key
;
1889 struct btrfs_chunk
*chunk
;
1896 struct btrfs_key key
;
1898 array_size
= btrfs_super_sys_array_size(super_copy
);
1900 ptr
= super_copy
->sys_chunk_array
;
1903 while (cur
< array_size
) {
1904 disk_key
= (struct btrfs_disk_key
*)ptr
;
1905 btrfs_disk_key_to_cpu(&key
, disk_key
);
1907 len
= sizeof(*disk_key
);
1909 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1910 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1911 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1912 len
+= btrfs_chunk_item_size(num_stripes
);
1917 if (key
.objectid
== chunk_objectid
&&
1918 key
.offset
== chunk_offset
) {
1919 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1921 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1930 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1931 u64 chunk_tree
, u64 chunk_objectid
,
1934 struct extent_map_tree
*em_tree
;
1935 struct btrfs_root
*extent_root
;
1936 struct btrfs_trans_handle
*trans
;
1937 struct extent_map
*em
;
1938 struct map_lookup
*map
;
1942 root
= root
->fs_info
->chunk_root
;
1943 extent_root
= root
->fs_info
->extent_root
;
1944 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1946 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1950 /* step one, relocate all the extents inside this chunk */
1951 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1955 trans
= btrfs_start_transaction(root
, 0);
1956 BUG_ON(IS_ERR(trans
));
1961 * step two, delete the device extents and the
1962 * chunk tree entries
1964 read_lock(&em_tree
->lock
);
1965 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
1966 read_unlock(&em_tree
->lock
);
1968 BUG_ON(!em
|| em
->start
> chunk_offset
||
1969 em
->start
+ em
->len
< chunk_offset
);
1970 map
= (struct map_lookup
*)em
->bdev
;
1972 for (i
= 0; i
< map
->num_stripes
; i
++) {
1973 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
1974 map
->stripes
[i
].physical
);
1977 if (map
->stripes
[i
].dev
) {
1978 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
1982 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
1987 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
1989 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
1990 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
1994 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
1997 write_lock(&em_tree
->lock
);
1998 remove_extent_mapping(em_tree
, em
);
1999 write_unlock(&em_tree
->lock
);
2004 /* once for the tree */
2005 free_extent_map(em
);
2007 free_extent_map(em
);
2009 unlock_chunks(root
);
2010 btrfs_end_transaction(trans
, root
);
2014 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2016 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2017 struct btrfs_path
*path
;
2018 struct extent_buffer
*leaf
;
2019 struct btrfs_chunk
*chunk
;
2020 struct btrfs_key key
;
2021 struct btrfs_key found_key
;
2022 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2024 bool retried
= false;
2028 path
= btrfs_alloc_path();
2033 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2034 key
.offset
= (u64
)-1;
2035 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2038 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2043 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2050 leaf
= path
->nodes
[0];
2051 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2053 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2054 struct btrfs_chunk
);
2055 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2056 btrfs_release_path(path
);
2058 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2059 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2068 if (found_key
.offset
== 0)
2070 key
.offset
= found_key
.offset
- 1;
2073 if (failed
&& !retried
) {
2077 } else if (failed
&& retried
) {
2082 btrfs_free_path(path
);
2086 static int insert_balance_item(struct btrfs_root
*root
,
2087 struct btrfs_balance_control
*bctl
)
2089 struct btrfs_trans_handle
*trans
;
2090 struct btrfs_balance_item
*item
;
2091 struct btrfs_disk_balance_args disk_bargs
;
2092 struct btrfs_path
*path
;
2093 struct extent_buffer
*leaf
;
2094 struct btrfs_key key
;
2097 path
= btrfs_alloc_path();
2101 trans
= btrfs_start_transaction(root
, 0);
2102 if (IS_ERR(trans
)) {
2103 btrfs_free_path(path
);
2104 return PTR_ERR(trans
);
2107 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2108 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2111 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2116 leaf
= path
->nodes
[0];
2117 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2119 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2121 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2122 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2123 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2124 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2125 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2126 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2128 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2130 btrfs_mark_buffer_dirty(leaf
);
2132 btrfs_free_path(path
);
2133 err
= btrfs_commit_transaction(trans
, root
);
2139 static int del_balance_item(struct btrfs_root
*root
)
2141 struct btrfs_trans_handle
*trans
;
2142 struct btrfs_path
*path
;
2143 struct btrfs_key key
;
2146 path
= btrfs_alloc_path();
2150 trans
= btrfs_start_transaction(root
, 0);
2151 if (IS_ERR(trans
)) {
2152 btrfs_free_path(path
);
2153 return PTR_ERR(trans
);
2156 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2157 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2160 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2168 ret
= btrfs_del_item(trans
, root
, path
);
2170 btrfs_free_path(path
);
2171 err
= btrfs_commit_transaction(trans
, root
);
2178 * This is a heuristic used to reduce the number of chunks balanced on
2179 * resume after balance was interrupted.
2181 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2184 * Turn on soft mode for chunk types that were being converted.
2186 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2187 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2188 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2189 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2190 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2191 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2194 * Turn on usage filter if is not already used. The idea is
2195 * that chunks that we have already balanced should be
2196 * reasonably full. Don't do it for chunks that are being
2197 * converted - that will keep us from relocating unconverted
2198 * (albeit full) chunks.
2200 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2201 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2202 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2203 bctl
->data
.usage
= 90;
2205 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2206 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2207 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2208 bctl
->sys
.usage
= 90;
2210 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2211 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2212 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2213 bctl
->meta
.usage
= 90;
2218 * Should be called with both balance and volume mutexes held to
2219 * serialize other volume operations (add_dev/rm_dev/resize) with
2220 * restriper. Same goes for unset_balance_control.
2222 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2224 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2226 BUG_ON(fs_info
->balance_ctl
);
2228 spin_lock(&fs_info
->balance_lock
);
2229 fs_info
->balance_ctl
= bctl
;
2230 spin_unlock(&fs_info
->balance_lock
);
2233 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2235 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2237 BUG_ON(!fs_info
->balance_ctl
);
2239 spin_lock(&fs_info
->balance_lock
);
2240 fs_info
->balance_ctl
= NULL
;
2241 spin_unlock(&fs_info
->balance_lock
);
2247 * Balance filters. Return 1 if chunk should be filtered out
2248 * (should not be balanced).
2250 static int chunk_profiles_filter(u64 chunk_profile
,
2251 struct btrfs_balance_args
*bargs
)
2253 chunk_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
2255 if (chunk_profile
== 0)
2256 chunk_profile
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2258 if (bargs
->profiles
& chunk_profile
)
2264 static u64
div_factor_fine(u64 num
, int factor
)
2276 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2277 struct btrfs_balance_args
*bargs
)
2279 struct btrfs_block_group_cache
*cache
;
2280 u64 chunk_used
, user_thresh
;
2283 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2284 chunk_used
= btrfs_block_group_used(&cache
->item
);
2286 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2287 if (chunk_used
< user_thresh
)
2290 btrfs_put_block_group(cache
);
2294 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2295 struct btrfs_chunk
*chunk
,
2296 struct btrfs_balance_args
*bargs
)
2298 struct btrfs_stripe
*stripe
;
2299 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2302 for (i
= 0; i
< num_stripes
; i
++) {
2303 stripe
= btrfs_stripe_nr(chunk
, i
);
2304 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2311 /* [pstart, pend) */
2312 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2313 struct btrfs_chunk
*chunk
,
2315 struct btrfs_balance_args
*bargs
)
2317 struct btrfs_stripe
*stripe
;
2318 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2324 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2327 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2328 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2332 factor
= num_stripes
/ factor
;
2334 for (i
= 0; i
< num_stripes
; i
++) {
2335 stripe
= btrfs_stripe_nr(chunk
, i
);
2336 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2339 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2340 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2341 do_div(stripe_length
, factor
);
2343 if (stripe_offset
< bargs
->pend
&&
2344 stripe_offset
+ stripe_length
> bargs
->pstart
)
2351 /* [vstart, vend) */
2352 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2353 struct btrfs_chunk
*chunk
,
2355 struct btrfs_balance_args
*bargs
)
2357 if (chunk_offset
< bargs
->vend
&&
2358 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2359 /* at least part of the chunk is inside this vrange */
2365 static int chunk_soft_convert_filter(u64 chunk_profile
,
2366 struct btrfs_balance_args
*bargs
)
2368 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2371 chunk_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
2373 if (chunk_profile
== 0)
2374 chunk_profile
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2376 if (bargs
->target
& chunk_profile
)
2382 static int should_balance_chunk(struct btrfs_root
*root
,
2383 struct extent_buffer
*leaf
,
2384 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2386 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2387 struct btrfs_balance_args
*bargs
= NULL
;
2388 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2391 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2392 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2396 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2397 bargs
= &bctl
->data
;
2398 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2400 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2401 bargs
= &bctl
->meta
;
2403 /* profiles filter */
2404 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2405 chunk_profiles_filter(chunk_type
, bargs
)) {
2410 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2411 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2416 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2417 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2421 /* drange filter, makes sense only with devid filter */
2422 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2423 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2428 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2429 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2433 /* soft profile changing mode */
2434 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2435 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2442 static u64
div_factor(u64 num
, int factor
)
2451 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2453 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2454 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2455 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2456 struct list_head
*devices
;
2457 struct btrfs_device
*device
;
2460 struct btrfs_chunk
*chunk
;
2461 struct btrfs_path
*path
;
2462 struct btrfs_key key
;
2463 struct btrfs_key found_key
;
2464 struct btrfs_trans_handle
*trans
;
2465 struct extent_buffer
*leaf
;
2468 int enospc_errors
= 0;
2469 bool counting
= true;
2471 /* step one make some room on all the devices */
2472 devices
= &fs_info
->fs_devices
->devices
;
2473 list_for_each_entry(device
, devices
, dev_list
) {
2474 old_size
= device
->total_bytes
;
2475 size_to_free
= div_factor(old_size
, 1);
2476 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2477 if (!device
->writeable
||
2478 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2481 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2486 trans
= btrfs_start_transaction(dev_root
, 0);
2487 BUG_ON(IS_ERR(trans
));
2489 ret
= btrfs_grow_device(trans
, device
, old_size
);
2492 btrfs_end_transaction(trans
, dev_root
);
2495 /* step two, relocate all the chunks */
2496 path
= btrfs_alloc_path();
2502 /* zero out stat counters */
2503 spin_lock(&fs_info
->balance_lock
);
2504 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2505 spin_unlock(&fs_info
->balance_lock
);
2507 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2508 key
.offset
= (u64
)-1;
2509 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2512 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2513 atomic_read(&fs_info
->balance_cancel_req
)) {
2518 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2523 * this shouldn't happen, it means the last relocate
2527 BUG(); /* FIXME break ? */
2529 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2530 BTRFS_CHUNK_ITEM_KEY
);
2536 leaf
= path
->nodes
[0];
2537 slot
= path
->slots
[0];
2538 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2540 if (found_key
.objectid
!= key
.objectid
)
2543 /* chunk zero is special */
2544 if (found_key
.offset
== 0)
2547 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2550 spin_lock(&fs_info
->balance_lock
);
2551 bctl
->stat
.considered
++;
2552 spin_unlock(&fs_info
->balance_lock
);
2555 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2557 btrfs_release_path(path
);
2562 spin_lock(&fs_info
->balance_lock
);
2563 bctl
->stat
.expected
++;
2564 spin_unlock(&fs_info
->balance_lock
);
2568 ret
= btrfs_relocate_chunk(chunk_root
,
2569 chunk_root
->root_key
.objectid
,
2572 if (ret
&& ret
!= -ENOSPC
)
2574 if (ret
== -ENOSPC
) {
2577 spin_lock(&fs_info
->balance_lock
);
2578 bctl
->stat
.completed
++;
2579 spin_unlock(&fs_info
->balance_lock
);
2582 key
.offset
= found_key
.offset
- 1;
2586 btrfs_release_path(path
);
2591 btrfs_free_path(path
);
2592 if (enospc_errors
) {
2593 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2602 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2604 /* cancel requested || normal exit path */
2605 return atomic_read(&fs_info
->balance_cancel_req
) ||
2606 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2607 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2610 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2614 unset_balance_control(fs_info
);
2615 ret
= del_balance_item(fs_info
->tree_root
);
2619 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2620 struct btrfs_ioctl_balance_args
*bargs
);
2623 * Should be called with both balance and volume mutexes held
2625 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2626 struct btrfs_ioctl_balance_args
*bargs
)
2628 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2632 if (btrfs_fs_closing(fs_info
) ||
2633 atomic_read(&fs_info
->balance_pause_req
) ||
2634 atomic_read(&fs_info
->balance_cancel_req
)) {
2640 * In case of mixed groups both data and meta should be picked,
2641 * and identical options should be given for both of them.
2643 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2644 if ((allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
) &&
2645 (bctl
->flags
& (BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
))) {
2646 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2647 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2648 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2649 printk(KERN_ERR
"btrfs: with mixed groups data and "
2650 "metadata balance options must be the same\n");
2657 * Profile changing sanity checks. Skip them if a simple
2658 * balance is requested.
2660 if (!((bctl
->data
.flags
| bctl
->sys
.flags
| bctl
->meta
.flags
) &
2661 BTRFS_BALANCE_ARGS_CONVERT
))
2664 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2665 if (fs_info
->fs_devices
->num_devices
== 1)
2666 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2667 else if (fs_info
->fs_devices
->num_devices
< 4)
2668 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
2670 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2671 BTRFS_BLOCK_GROUP_RAID10
);
2673 if (!profile_is_valid(bctl
->data
.target
, 1) ||
2674 bctl
->data
.target
& ~allowed
) {
2675 printk(KERN_ERR
"btrfs: unable to start balance with target "
2676 "data profile %llu\n",
2677 (unsigned long long)bctl
->data
.target
);
2681 if (!profile_is_valid(bctl
->meta
.target
, 1) ||
2682 bctl
->meta
.target
& ~allowed
) {
2683 printk(KERN_ERR
"btrfs: unable to start balance with target "
2684 "metadata profile %llu\n",
2685 (unsigned long long)bctl
->meta
.target
);
2689 if (!profile_is_valid(bctl
->sys
.target
, 1) ||
2690 bctl
->sys
.target
& ~allowed
) {
2691 printk(KERN_ERR
"btrfs: unable to start balance with target "
2692 "system profile %llu\n",
2693 (unsigned long long)bctl
->sys
.target
);
2698 if (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
) {
2699 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
2704 /* allow to reduce meta or sys integrity only if force set */
2705 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2706 BTRFS_BLOCK_GROUP_RAID10
;
2707 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2708 (fs_info
->avail_system_alloc_bits
& allowed
) &&
2709 !(bctl
->sys
.target
& allowed
)) ||
2710 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2711 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
2712 !(bctl
->meta
.target
& allowed
))) {
2713 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
2714 printk(KERN_INFO
"btrfs: force reducing metadata "
2717 printk(KERN_ERR
"btrfs: balance will reduce metadata "
2718 "integrity, use force if you want this\n");
2725 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
2726 if (ret
&& ret
!= -EEXIST
)
2729 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
2730 BUG_ON(ret
== -EEXIST
);
2731 set_balance_control(bctl
);
2733 BUG_ON(ret
!= -EEXIST
);
2734 spin_lock(&fs_info
->balance_lock
);
2735 update_balance_args(bctl
);
2736 spin_unlock(&fs_info
->balance_lock
);
2739 atomic_inc(&fs_info
->balance_running
);
2740 mutex_unlock(&fs_info
->balance_mutex
);
2742 ret
= __btrfs_balance(fs_info
);
2744 mutex_lock(&fs_info
->balance_mutex
);
2745 atomic_dec(&fs_info
->balance_running
);
2748 memset(bargs
, 0, sizeof(*bargs
));
2749 update_ioctl_balance_args(fs_info
, 0, bargs
);
2752 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
2753 balance_need_close(fs_info
)) {
2754 __cancel_balance(fs_info
);
2757 wake_up(&fs_info
->balance_wait_q
);
2761 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
2762 __cancel_balance(fs_info
);
2768 static int balance_kthread(void *data
)
2770 struct btrfs_balance_control
*bctl
=
2771 (struct btrfs_balance_control
*)data
;
2772 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2775 mutex_lock(&fs_info
->volume_mutex
);
2776 mutex_lock(&fs_info
->balance_mutex
);
2778 set_balance_control(bctl
);
2780 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
2781 printk(KERN_INFO
"btrfs: force skipping balance\n");
2783 printk(KERN_INFO
"btrfs: continuing balance\n");
2784 ret
= btrfs_balance(bctl
, NULL
);
2787 mutex_unlock(&fs_info
->balance_mutex
);
2788 mutex_unlock(&fs_info
->volume_mutex
);
2792 int btrfs_recover_balance(struct btrfs_root
*tree_root
)
2794 struct task_struct
*tsk
;
2795 struct btrfs_balance_control
*bctl
;
2796 struct btrfs_balance_item
*item
;
2797 struct btrfs_disk_balance_args disk_bargs
;
2798 struct btrfs_path
*path
;
2799 struct extent_buffer
*leaf
;
2800 struct btrfs_key key
;
2803 path
= btrfs_alloc_path();
2807 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
2813 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2814 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2817 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, path
, 0, 0);
2820 if (ret
> 0) { /* ret = -ENOENT; */
2825 leaf
= path
->nodes
[0];
2826 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2828 bctl
->fs_info
= tree_root
->fs_info
;
2829 bctl
->flags
= btrfs_balance_flags(leaf
, item
) | BTRFS_BALANCE_RESUME
;
2831 btrfs_balance_data(leaf
, item
, &disk_bargs
);
2832 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
2833 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
2834 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
2835 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
2836 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
2838 tsk
= kthread_run(balance_kthread
, bctl
, "btrfs-balance");
2847 btrfs_free_path(path
);
2851 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
2855 mutex_lock(&fs_info
->balance_mutex
);
2856 if (!fs_info
->balance_ctl
) {
2857 mutex_unlock(&fs_info
->balance_mutex
);
2861 if (atomic_read(&fs_info
->balance_running
)) {
2862 atomic_inc(&fs_info
->balance_pause_req
);
2863 mutex_unlock(&fs_info
->balance_mutex
);
2865 wait_event(fs_info
->balance_wait_q
,
2866 atomic_read(&fs_info
->balance_running
) == 0);
2868 mutex_lock(&fs_info
->balance_mutex
);
2869 /* we are good with balance_ctl ripped off from under us */
2870 BUG_ON(atomic_read(&fs_info
->balance_running
));
2871 atomic_dec(&fs_info
->balance_pause_req
);
2876 mutex_unlock(&fs_info
->balance_mutex
);
2880 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
2882 mutex_lock(&fs_info
->balance_mutex
);
2883 if (!fs_info
->balance_ctl
) {
2884 mutex_unlock(&fs_info
->balance_mutex
);
2888 atomic_inc(&fs_info
->balance_cancel_req
);
2890 * if we are running just wait and return, balance item is
2891 * deleted in btrfs_balance in this case
2893 if (atomic_read(&fs_info
->balance_running
)) {
2894 mutex_unlock(&fs_info
->balance_mutex
);
2895 wait_event(fs_info
->balance_wait_q
,
2896 atomic_read(&fs_info
->balance_running
) == 0);
2897 mutex_lock(&fs_info
->balance_mutex
);
2899 /* __cancel_balance needs volume_mutex */
2900 mutex_unlock(&fs_info
->balance_mutex
);
2901 mutex_lock(&fs_info
->volume_mutex
);
2902 mutex_lock(&fs_info
->balance_mutex
);
2904 if (fs_info
->balance_ctl
)
2905 __cancel_balance(fs_info
);
2907 mutex_unlock(&fs_info
->volume_mutex
);
2910 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
2911 atomic_dec(&fs_info
->balance_cancel_req
);
2912 mutex_unlock(&fs_info
->balance_mutex
);
2917 * shrinking a device means finding all of the device extents past
2918 * the new size, and then following the back refs to the chunks.
2919 * The chunk relocation code actually frees the device extent
2921 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2923 struct btrfs_trans_handle
*trans
;
2924 struct btrfs_root
*root
= device
->dev_root
;
2925 struct btrfs_dev_extent
*dev_extent
= NULL
;
2926 struct btrfs_path
*path
;
2934 bool retried
= false;
2935 struct extent_buffer
*l
;
2936 struct btrfs_key key
;
2937 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2938 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2939 u64 old_size
= device
->total_bytes
;
2940 u64 diff
= device
->total_bytes
- new_size
;
2942 if (new_size
>= device
->total_bytes
)
2945 path
= btrfs_alloc_path();
2953 device
->total_bytes
= new_size
;
2954 if (device
->writeable
) {
2955 device
->fs_devices
->total_rw_bytes
-= diff
;
2956 spin_lock(&root
->fs_info
->free_chunk_lock
);
2957 root
->fs_info
->free_chunk_space
-= diff
;
2958 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2960 unlock_chunks(root
);
2963 key
.objectid
= device
->devid
;
2964 key
.offset
= (u64
)-1;
2965 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2968 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2972 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
2977 btrfs_release_path(path
);
2982 slot
= path
->slots
[0];
2983 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
2985 if (key
.objectid
!= device
->devid
) {
2986 btrfs_release_path(path
);
2990 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2991 length
= btrfs_dev_extent_length(l
, dev_extent
);
2993 if (key
.offset
+ length
<= new_size
) {
2994 btrfs_release_path(path
);
2998 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2999 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3000 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3001 btrfs_release_path(path
);
3003 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3005 if (ret
&& ret
!= -ENOSPC
)
3012 if (failed
&& !retried
) {
3016 } else if (failed
&& retried
) {
3020 device
->total_bytes
= old_size
;
3021 if (device
->writeable
)
3022 device
->fs_devices
->total_rw_bytes
+= diff
;
3023 spin_lock(&root
->fs_info
->free_chunk_lock
);
3024 root
->fs_info
->free_chunk_space
+= diff
;
3025 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3026 unlock_chunks(root
);
3030 /* Shrinking succeeded, else we would be at "done". */
3031 trans
= btrfs_start_transaction(root
, 0);
3032 if (IS_ERR(trans
)) {
3033 ret
= PTR_ERR(trans
);
3039 device
->disk_total_bytes
= new_size
;
3040 /* Now btrfs_update_device() will change the on-disk size. */
3041 ret
= btrfs_update_device(trans
, device
);
3043 unlock_chunks(root
);
3044 btrfs_end_transaction(trans
, root
);
3047 WARN_ON(diff
> old_total
);
3048 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3049 unlock_chunks(root
);
3050 btrfs_end_transaction(trans
, root
);
3052 btrfs_free_path(path
);
3056 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3057 struct btrfs_key
*key
,
3058 struct btrfs_chunk
*chunk
, int item_size
)
3060 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3061 struct btrfs_disk_key disk_key
;
3065 array_size
= btrfs_super_sys_array_size(super_copy
);
3066 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3069 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3070 btrfs_cpu_key_to_disk(&disk_key
, key
);
3071 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3072 ptr
+= sizeof(disk_key
);
3073 memcpy(ptr
, chunk
, item_size
);
3074 item_size
+= sizeof(disk_key
);
3075 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3080 * sort the devices in descending order by max_avail, total_avail
3082 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3084 const struct btrfs_device_info
*di_a
= a
;
3085 const struct btrfs_device_info
*di_b
= b
;
3087 if (di_a
->max_avail
> di_b
->max_avail
)
3089 if (di_a
->max_avail
< di_b
->max_avail
)
3091 if (di_a
->total_avail
> di_b
->total_avail
)
3093 if (di_a
->total_avail
< di_b
->total_avail
)
3098 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3099 struct btrfs_root
*extent_root
,
3100 struct map_lookup
**map_ret
,
3101 u64
*num_bytes_out
, u64
*stripe_size_out
,
3102 u64 start
, u64 type
)
3104 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3105 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3106 struct list_head
*cur
;
3107 struct map_lookup
*map
= NULL
;
3108 struct extent_map_tree
*em_tree
;
3109 struct extent_map
*em
;
3110 struct btrfs_device_info
*devices_info
= NULL
;
3112 int num_stripes
; /* total number of stripes to allocate */
3113 int sub_stripes
; /* sub_stripes info for map */
3114 int dev_stripes
; /* stripes per dev */
3115 int devs_max
; /* max devs to use */
3116 int devs_min
; /* min devs needed */
3117 int devs_increment
; /* ndevs has to be a multiple of this */
3118 int ncopies
; /* how many copies to data has */
3120 u64 max_stripe_size
;
3128 if ((type
& BTRFS_BLOCK_GROUP_RAID1
) &&
3129 (type
& BTRFS_BLOCK_GROUP_DUP
)) {
3131 type
&= ~BTRFS_BLOCK_GROUP_DUP
;
3134 if (list_empty(&fs_devices
->alloc_list
))
3141 devs_max
= 0; /* 0 == as many as possible */
3145 * define the properties of each RAID type.
3146 * FIXME: move this to a global table and use it in all RAID
3149 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3153 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3155 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3160 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3169 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3170 max_stripe_size
= 1024 * 1024 * 1024;
3171 max_chunk_size
= 10 * max_stripe_size
;
3172 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3173 /* for larger filesystems, use larger metadata chunks */
3174 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3175 max_stripe_size
= 1024 * 1024 * 1024;
3177 max_stripe_size
= 256 * 1024 * 1024;
3178 max_chunk_size
= max_stripe_size
;
3179 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3180 max_stripe_size
= 32 * 1024 * 1024;
3181 max_chunk_size
= 2 * max_stripe_size
;
3183 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3188 /* we don't want a chunk larger than 10% of writeable space */
3189 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3192 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3197 cur
= fs_devices
->alloc_list
.next
;
3200 * in the first pass through the devices list, we gather information
3201 * about the available holes on each device.
3204 while (cur
!= &fs_devices
->alloc_list
) {
3205 struct btrfs_device
*device
;
3209 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3213 if (!device
->writeable
) {
3215 "btrfs: read-only device in alloc_list\n");
3220 if (!device
->in_fs_metadata
)
3223 if (device
->total_bytes
> device
->bytes_used
)
3224 total_avail
= device
->total_bytes
- device
->bytes_used
;
3228 /* If there is no space on this device, skip it. */
3229 if (total_avail
== 0)
3232 ret
= find_free_dev_extent(device
,
3233 max_stripe_size
* dev_stripes
,
3234 &dev_offset
, &max_avail
);
3235 if (ret
&& ret
!= -ENOSPC
)
3239 max_avail
= max_stripe_size
* dev_stripes
;
3241 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3244 devices_info
[ndevs
].dev_offset
= dev_offset
;
3245 devices_info
[ndevs
].max_avail
= max_avail
;
3246 devices_info
[ndevs
].total_avail
= total_avail
;
3247 devices_info
[ndevs
].dev
= device
;
3252 * now sort the devices by hole size / available space
3254 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3255 btrfs_cmp_device_info
, NULL
);
3257 /* round down to number of usable stripes */
3258 ndevs
-= ndevs
% devs_increment
;
3260 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3265 if (devs_max
&& ndevs
> devs_max
)
3268 * the primary goal is to maximize the number of stripes, so use as many
3269 * devices as possible, even if the stripes are not maximum sized.
3271 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3272 num_stripes
= ndevs
* dev_stripes
;
3274 if (stripe_size
* num_stripes
> max_chunk_size
* ncopies
) {
3275 stripe_size
= max_chunk_size
* ncopies
;
3276 do_div(stripe_size
, num_stripes
);
3279 do_div(stripe_size
, dev_stripes
);
3280 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3281 stripe_size
*= BTRFS_STRIPE_LEN
;
3283 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3288 map
->num_stripes
= num_stripes
;
3290 for (i
= 0; i
< ndevs
; ++i
) {
3291 for (j
= 0; j
< dev_stripes
; ++j
) {
3292 int s
= i
* dev_stripes
+ j
;
3293 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3294 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3298 map
->sector_size
= extent_root
->sectorsize
;
3299 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3300 map
->io_align
= BTRFS_STRIPE_LEN
;
3301 map
->io_width
= BTRFS_STRIPE_LEN
;
3303 map
->sub_stripes
= sub_stripes
;
3306 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3308 *stripe_size_out
= stripe_size
;
3309 *num_bytes_out
= num_bytes
;
3311 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3313 em
= alloc_extent_map();
3318 em
->bdev
= (struct block_device
*)map
;
3320 em
->len
= num_bytes
;
3321 em
->block_start
= 0;
3322 em
->block_len
= em
->len
;
3324 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3325 write_lock(&em_tree
->lock
);
3326 ret
= add_extent_mapping(em_tree
, em
);
3327 write_unlock(&em_tree
->lock
);
3329 free_extent_map(em
);
3331 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3332 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3336 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3337 struct btrfs_device
*device
;
3340 device
= map
->stripes
[i
].dev
;
3341 dev_offset
= map
->stripes
[i
].physical
;
3343 ret
= btrfs_alloc_dev_extent(trans
, device
,
3344 info
->chunk_root
->root_key
.objectid
,
3345 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3346 start
, dev_offset
, stripe_size
);
3350 kfree(devices_info
);
3355 kfree(devices_info
);
3359 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3360 struct btrfs_root
*extent_root
,
3361 struct map_lookup
*map
, u64 chunk_offset
,
3362 u64 chunk_size
, u64 stripe_size
)
3365 struct btrfs_key key
;
3366 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3367 struct btrfs_device
*device
;
3368 struct btrfs_chunk
*chunk
;
3369 struct btrfs_stripe
*stripe
;
3370 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3374 chunk
= kzalloc(item_size
, GFP_NOFS
);
3379 while (index
< map
->num_stripes
) {
3380 device
= map
->stripes
[index
].dev
;
3381 device
->bytes_used
+= stripe_size
;
3382 ret
= btrfs_update_device(trans
, device
);
3387 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3388 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3390 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3393 stripe
= &chunk
->stripe
;
3394 while (index
< map
->num_stripes
) {
3395 device
= map
->stripes
[index
].dev
;
3396 dev_offset
= map
->stripes
[index
].physical
;
3398 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3399 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3400 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3405 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3406 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3407 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3408 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3409 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3410 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3411 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3412 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3413 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3415 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3416 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3417 key
.offset
= chunk_offset
;
3419 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3422 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3423 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3433 * Chunk allocation falls into two parts. The first part does works
3434 * that make the new allocated chunk useable, but not do any operation
3435 * that modifies the chunk tree. The second part does the works that
3436 * require modifying the chunk tree. This division is important for the
3437 * bootstrap process of adding storage to a seed btrfs.
3439 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3440 struct btrfs_root
*extent_root
, u64 type
)
3445 struct map_lookup
*map
;
3446 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3449 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3454 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3455 &stripe_size
, chunk_offset
, type
);
3459 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3460 chunk_size
, stripe_size
);
3465 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3466 struct btrfs_root
*root
,
3467 struct btrfs_device
*device
)
3470 u64 sys_chunk_offset
;
3474 u64 sys_stripe_size
;
3476 struct map_lookup
*map
;
3477 struct map_lookup
*sys_map
;
3478 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3479 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3482 ret
= find_next_chunk(fs_info
->chunk_root
,
3483 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3487 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3488 fs_info
->avail_metadata_alloc_bits
;
3489 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3491 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3492 &stripe_size
, chunk_offset
, alloc_profile
);
3495 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3497 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3498 fs_info
->avail_system_alloc_bits
;
3499 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3501 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3502 &sys_chunk_size
, &sys_stripe_size
,
3503 sys_chunk_offset
, alloc_profile
);
3506 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3510 * Modifying chunk tree needs allocating new blocks from both
3511 * system block group and metadata block group. So we only can
3512 * do operations require modifying the chunk tree after both
3513 * block groups were created.
3515 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3516 chunk_size
, stripe_size
);
3519 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3520 sys_chunk_offset
, sys_chunk_size
,
3526 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3528 struct extent_map
*em
;
3529 struct map_lookup
*map
;
3530 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3534 read_lock(&map_tree
->map_tree
.lock
);
3535 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3536 read_unlock(&map_tree
->map_tree
.lock
);
3540 if (btrfs_test_opt(root
, DEGRADED
)) {
3541 free_extent_map(em
);
3545 map
= (struct map_lookup
*)em
->bdev
;
3546 for (i
= 0; i
< map
->num_stripes
; i
++) {
3547 if (!map
->stripes
[i
].dev
->writeable
) {
3552 free_extent_map(em
);
3556 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3558 extent_map_tree_init(&tree
->map_tree
);
3561 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3563 struct extent_map
*em
;
3566 write_lock(&tree
->map_tree
.lock
);
3567 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3569 remove_extent_mapping(&tree
->map_tree
, em
);
3570 write_unlock(&tree
->map_tree
.lock
);
3575 free_extent_map(em
);
3576 /* once for the tree */
3577 free_extent_map(em
);
3581 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
3583 struct extent_map
*em
;
3584 struct map_lookup
*map
;
3585 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3588 read_lock(&em_tree
->lock
);
3589 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3590 read_unlock(&em_tree
->lock
);
3593 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3594 map
= (struct map_lookup
*)em
->bdev
;
3595 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
3596 ret
= map
->num_stripes
;
3597 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3598 ret
= map
->sub_stripes
;
3601 free_extent_map(em
);
3605 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
3609 if (map
->stripes
[optimal
].dev
->bdev
)
3611 for (i
= first
; i
< first
+ num
; i
++) {
3612 if (map
->stripes
[i
].dev
->bdev
)
3615 /* we couldn't find one that doesn't fail. Just return something
3616 * and the io error handling code will clean up eventually
3621 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3622 u64 logical
, u64
*length
,
3623 struct btrfs_bio
**bbio_ret
,
3626 struct extent_map
*em
;
3627 struct map_lookup
*map
;
3628 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3631 u64 stripe_end_offset
;
3640 struct btrfs_bio
*bbio
= NULL
;
3642 read_lock(&em_tree
->lock
);
3643 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
3644 read_unlock(&em_tree
->lock
);
3647 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
3648 (unsigned long long)logical
,
3649 (unsigned long long)*length
);
3653 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3654 map
= (struct map_lookup
*)em
->bdev
;
3655 offset
= logical
- em
->start
;
3657 if (mirror_num
> map
->num_stripes
)
3662 * stripe_nr counts the total number of stripes we have to stride
3663 * to get to this block
3665 do_div(stripe_nr
, map
->stripe_len
);
3667 stripe_offset
= stripe_nr
* map
->stripe_len
;
3668 BUG_ON(offset
< stripe_offset
);
3670 /* stripe_offset is the offset of this block in its stripe*/
3671 stripe_offset
= offset
- stripe_offset
;
3673 if (rw
& REQ_DISCARD
)
3674 *length
= min_t(u64
, em
->len
- offset
, *length
);
3675 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
3676 /* we limit the length of each bio to what fits in a stripe */
3677 *length
= min_t(u64
, em
->len
- offset
,
3678 map
->stripe_len
- stripe_offset
);
3680 *length
= em
->len
- offset
;
3688 stripe_nr_orig
= stripe_nr
;
3689 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3690 (~(map
->stripe_len
- 1));
3691 do_div(stripe_nr_end
, map
->stripe_len
);
3692 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3694 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3695 if (rw
& REQ_DISCARD
)
3696 num_stripes
= min_t(u64
, map
->num_stripes
,
3697 stripe_nr_end
- stripe_nr_orig
);
3698 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3699 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3700 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3701 num_stripes
= map
->num_stripes
;
3702 else if (mirror_num
)
3703 stripe_index
= mirror_num
- 1;
3705 stripe_index
= find_live_mirror(map
, 0,
3707 current
->pid
% map
->num_stripes
);
3708 mirror_num
= stripe_index
+ 1;
3711 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3712 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3713 num_stripes
= map
->num_stripes
;
3714 } else if (mirror_num
) {
3715 stripe_index
= mirror_num
- 1;
3720 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3721 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3723 stripe_index
= do_div(stripe_nr
, factor
);
3724 stripe_index
*= map
->sub_stripes
;
3727 num_stripes
= map
->sub_stripes
;
3728 else if (rw
& REQ_DISCARD
)
3729 num_stripes
= min_t(u64
, map
->sub_stripes
*
3730 (stripe_nr_end
- stripe_nr_orig
),
3732 else if (mirror_num
)
3733 stripe_index
+= mirror_num
- 1;
3735 stripe_index
= find_live_mirror(map
, stripe_index
,
3736 map
->sub_stripes
, stripe_index
+
3737 current
->pid
% map
->sub_stripes
);
3738 mirror_num
= stripe_index
+ 1;
3742 * after this do_div call, stripe_nr is the number of stripes
3743 * on this device we have to walk to find the data, and
3744 * stripe_index is the number of our device in the stripe array
3746 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3747 mirror_num
= stripe_index
+ 1;
3749 BUG_ON(stripe_index
>= map
->num_stripes
);
3751 bbio
= kzalloc(btrfs_bio_size(num_stripes
), GFP_NOFS
);
3756 atomic_set(&bbio
->error
, 0);
3758 if (rw
& REQ_DISCARD
) {
3760 int sub_stripes
= 0;
3761 u64 stripes_per_dev
= 0;
3762 u32 remaining_stripes
= 0;
3765 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
3766 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3769 sub_stripes
= map
->sub_stripes
;
3771 factor
= map
->num_stripes
/ sub_stripes
;
3772 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
3775 &remaining_stripes
);
3778 for (i
= 0; i
< num_stripes
; i
++) {
3779 bbio
->stripes
[i
].physical
=
3780 map
->stripes
[stripe_index
].physical
+
3781 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3782 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3784 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
3785 BTRFS_BLOCK_GROUP_RAID10
)) {
3786 bbio
->stripes
[i
].length
= stripes_per_dev
*
3788 if (i
/ sub_stripes
< remaining_stripes
)
3789 bbio
->stripes
[i
].length
+=
3791 if (i
< sub_stripes
)
3792 bbio
->stripes
[i
].length
-=
3794 if ((i
/ sub_stripes
+ 1) %
3795 sub_stripes
== remaining_stripes
)
3796 bbio
->stripes
[i
].length
-=
3798 if (i
== sub_stripes
- 1)
3801 bbio
->stripes
[i
].length
= *length
;
3804 if (stripe_index
== map
->num_stripes
) {
3805 /* This could only happen for RAID0/10 */
3811 for (i
= 0; i
< num_stripes
; i
++) {
3812 bbio
->stripes
[i
].physical
=
3813 map
->stripes
[stripe_index
].physical
+
3815 stripe_nr
* map
->stripe_len
;
3816 bbio
->stripes
[i
].dev
=
3817 map
->stripes
[stripe_index
].dev
;
3822 if (rw
& REQ_WRITE
) {
3823 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
3824 BTRFS_BLOCK_GROUP_RAID10
|
3825 BTRFS_BLOCK_GROUP_DUP
)) {
3831 bbio
->num_stripes
= num_stripes
;
3832 bbio
->max_errors
= max_errors
;
3833 bbio
->mirror_num
= mirror_num
;
3835 free_extent_map(em
);
3839 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3840 u64 logical
, u64
*length
,
3841 struct btrfs_bio
**bbio_ret
, int mirror_num
)
3843 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
3847 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3848 u64 chunk_start
, u64 physical
, u64 devid
,
3849 u64
**logical
, int *naddrs
, int *stripe_len
)
3851 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3852 struct extent_map
*em
;
3853 struct map_lookup
*map
;
3860 read_lock(&em_tree
->lock
);
3861 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3862 read_unlock(&em_tree
->lock
);
3864 BUG_ON(!em
|| em
->start
!= chunk_start
);
3865 map
= (struct map_lookup
*)em
->bdev
;
3868 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3869 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3870 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3871 do_div(length
, map
->num_stripes
);
3873 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3876 for (i
= 0; i
< map
->num_stripes
; i
++) {
3877 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3879 if (map
->stripes
[i
].physical
> physical
||
3880 map
->stripes
[i
].physical
+ length
<= physical
)
3883 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3884 do_div(stripe_nr
, map
->stripe_len
);
3886 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3887 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3888 do_div(stripe_nr
, map
->sub_stripes
);
3889 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3890 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3892 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3893 WARN_ON(nr
>= map
->num_stripes
);
3894 for (j
= 0; j
< nr
; j
++) {
3895 if (buf
[j
] == bytenr
)
3899 WARN_ON(nr
>= map
->num_stripes
);
3906 *stripe_len
= map
->stripe_len
;
3908 free_extent_map(em
);
3912 static void btrfs_end_bio(struct bio
*bio
, int err
)
3914 struct btrfs_bio
*bbio
= bio
->bi_private
;
3915 int is_orig_bio
= 0;
3918 atomic_inc(&bbio
->error
);
3920 if (bio
== bbio
->orig_bio
)
3923 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
3926 bio
= bbio
->orig_bio
;
3928 bio
->bi_private
= bbio
->private;
3929 bio
->bi_end_io
= bbio
->end_io
;
3930 bio
->bi_bdev
= (struct block_device
*)
3931 (unsigned long)bbio
->mirror_num
;
3932 /* only send an error to the higher layers if it is
3933 * beyond the tolerance of the multi-bio
3935 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
3939 * this bio is actually up to date, we didn't
3940 * go over the max number of errors
3942 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3947 bio_endio(bio
, err
);
3948 } else if (!is_orig_bio
) {
3953 struct async_sched
{
3956 struct btrfs_fs_info
*info
;
3957 struct btrfs_work work
;
3961 * see run_scheduled_bios for a description of why bios are collected for
3964 * This will add one bio to the pending list for a device and make sure
3965 * the work struct is scheduled.
3967 static noinline
void schedule_bio(struct btrfs_root
*root
,
3968 struct btrfs_device
*device
,
3969 int rw
, struct bio
*bio
)
3971 int should_queue
= 1;
3972 struct btrfs_pending_bios
*pending_bios
;
3974 /* don't bother with additional async steps for reads, right now */
3975 if (!(rw
& REQ_WRITE
)) {
3977 btrfsic_submit_bio(rw
, bio
);
3983 * nr_async_bios allows us to reliably return congestion to the
3984 * higher layers. Otherwise, the async bio makes it appear we have
3985 * made progress against dirty pages when we've really just put it
3986 * on a queue for later
3988 atomic_inc(&root
->fs_info
->nr_async_bios
);
3989 WARN_ON(bio
->bi_next
);
3990 bio
->bi_next
= NULL
;
3993 spin_lock(&device
->io_lock
);
3994 if (bio
->bi_rw
& REQ_SYNC
)
3995 pending_bios
= &device
->pending_sync_bios
;
3997 pending_bios
= &device
->pending_bios
;
3999 if (pending_bios
->tail
)
4000 pending_bios
->tail
->bi_next
= bio
;
4002 pending_bios
->tail
= bio
;
4003 if (!pending_bios
->head
)
4004 pending_bios
->head
= bio
;
4005 if (device
->running_pending
)
4008 spin_unlock(&device
->io_lock
);
4011 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4015 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4016 int mirror_num
, int async_submit
)
4018 struct btrfs_mapping_tree
*map_tree
;
4019 struct btrfs_device
*dev
;
4020 struct bio
*first_bio
= bio
;
4021 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4027 struct btrfs_bio
*bbio
= NULL
;
4029 length
= bio
->bi_size
;
4030 map_tree
= &root
->fs_info
->mapping_tree
;
4031 map_length
= length
;
4033 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
4037 total_devs
= bbio
->num_stripes
;
4038 if (map_length
< length
) {
4039 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
4040 "len %llu\n", (unsigned long long)logical
,
4041 (unsigned long long)length
,
4042 (unsigned long long)map_length
);
4046 bbio
->orig_bio
= first_bio
;
4047 bbio
->private = first_bio
->bi_private
;
4048 bbio
->end_io
= first_bio
->bi_end_io
;
4049 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4051 while (dev_nr
< total_devs
) {
4052 if (dev_nr
< total_devs
- 1) {
4053 bio
= bio_clone(first_bio
, GFP_NOFS
);
4058 bio
->bi_private
= bbio
;
4059 bio
->bi_end_io
= btrfs_end_bio
;
4060 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
4061 dev
= bbio
->stripes
[dev_nr
].dev
;
4062 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
4063 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4064 "(%s id %llu), size=%u\n", rw
,
4065 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4066 dev
->name
, dev
->devid
, bio
->bi_size
);
4067 bio
->bi_bdev
= dev
->bdev
;
4069 schedule_bio(root
, dev
, rw
, bio
);
4071 btrfsic_submit_bio(rw
, bio
);
4073 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4074 bio
->bi_sector
= logical
>> 9;
4075 bio_endio(bio
, -EIO
);
4082 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
4085 struct btrfs_device
*device
;
4086 struct btrfs_fs_devices
*cur_devices
;
4088 cur_devices
= root
->fs_info
->fs_devices
;
4089 while (cur_devices
) {
4091 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4092 device
= __find_device(&cur_devices
->devices
,
4097 cur_devices
= cur_devices
->seed
;
4102 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4103 u64 devid
, u8
*dev_uuid
)
4105 struct btrfs_device
*device
;
4106 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4108 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4111 list_add(&device
->dev_list
,
4112 &fs_devices
->devices
);
4113 device
->dev_root
= root
->fs_info
->dev_root
;
4114 device
->devid
= devid
;
4115 device
->work
.func
= pending_bios_fn
;
4116 device
->fs_devices
= fs_devices
;
4117 device
->missing
= 1;
4118 fs_devices
->num_devices
++;
4119 fs_devices
->missing_devices
++;
4120 spin_lock_init(&device
->io_lock
);
4121 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4122 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4126 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4127 struct extent_buffer
*leaf
,
4128 struct btrfs_chunk
*chunk
)
4130 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4131 struct map_lookup
*map
;
4132 struct extent_map
*em
;
4136 u8 uuid
[BTRFS_UUID_SIZE
];
4141 logical
= key
->offset
;
4142 length
= btrfs_chunk_length(leaf
, chunk
);
4144 read_lock(&map_tree
->map_tree
.lock
);
4145 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4146 read_unlock(&map_tree
->map_tree
.lock
);
4148 /* already mapped? */
4149 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4150 free_extent_map(em
);
4153 free_extent_map(em
);
4156 em
= alloc_extent_map();
4159 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4160 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4162 free_extent_map(em
);
4166 em
->bdev
= (struct block_device
*)map
;
4167 em
->start
= logical
;
4169 em
->block_start
= 0;
4170 em
->block_len
= em
->len
;
4172 map
->num_stripes
= num_stripes
;
4173 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4174 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4175 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4176 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4177 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4178 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4179 for (i
= 0; i
< num_stripes
; i
++) {
4180 map
->stripes
[i
].physical
=
4181 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4182 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4183 read_extent_buffer(leaf
, uuid
, (unsigned long)
4184 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4186 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
4188 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4190 free_extent_map(em
);
4193 if (!map
->stripes
[i
].dev
) {
4194 map
->stripes
[i
].dev
=
4195 add_missing_dev(root
, devid
, uuid
);
4196 if (!map
->stripes
[i
].dev
) {
4198 free_extent_map(em
);
4202 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4205 write_lock(&map_tree
->map_tree
.lock
);
4206 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4207 write_unlock(&map_tree
->map_tree
.lock
);
4209 free_extent_map(em
);
4214 static void fill_device_from_item(struct extent_buffer
*leaf
,
4215 struct btrfs_dev_item
*dev_item
,
4216 struct btrfs_device
*device
)
4220 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4221 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4222 device
->total_bytes
= device
->disk_total_bytes
;
4223 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4224 device
->type
= btrfs_device_type(leaf
, dev_item
);
4225 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4226 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4227 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4229 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4230 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4233 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4235 struct btrfs_fs_devices
*fs_devices
;
4238 BUG_ON(!mutex_is_locked(&uuid_mutex
));
4240 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4241 while (fs_devices
) {
4242 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4246 fs_devices
= fs_devices
->seed
;
4249 fs_devices
= find_fsid(fsid
);
4255 fs_devices
= clone_fs_devices(fs_devices
);
4256 if (IS_ERR(fs_devices
)) {
4257 ret
= PTR_ERR(fs_devices
);
4261 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4262 root
->fs_info
->bdev_holder
);
4266 if (!fs_devices
->seeding
) {
4267 __btrfs_close_devices(fs_devices
);
4268 free_fs_devices(fs_devices
);
4273 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4274 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4279 static int read_one_dev(struct btrfs_root
*root
,
4280 struct extent_buffer
*leaf
,
4281 struct btrfs_dev_item
*dev_item
)
4283 struct btrfs_device
*device
;
4286 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4287 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4289 devid
= btrfs_device_id(leaf
, dev_item
);
4290 read_extent_buffer(leaf
, dev_uuid
,
4291 (unsigned long)btrfs_device_uuid(dev_item
),
4293 read_extent_buffer(leaf
, fs_uuid
,
4294 (unsigned long)btrfs_device_fsid(dev_item
),
4297 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4298 ret
= open_seed_devices(root
, fs_uuid
);
4299 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4303 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
4304 if (!device
|| !device
->bdev
) {
4305 if (!btrfs_test_opt(root
, DEGRADED
))
4309 printk(KERN_WARNING
"warning devid %llu missing\n",
4310 (unsigned long long)devid
);
4311 device
= add_missing_dev(root
, devid
, dev_uuid
);
4314 } else if (!device
->missing
) {
4316 * this happens when a device that was properly setup
4317 * in the device info lists suddenly goes bad.
4318 * device->bdev is NULL, and so we have to set
4319 * device->missing to one here
4321 root
->fs_info
->fs_devices
->missing_devices
++;
4322 device
->missing
= 1;
4326 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4327 BUG_ON(device
->writeable
);
4328 if (device
->generation
!=
4329 btrfs_device_generation(leaf
, dev_item
))
4333 fill_device_from_item(leaf
, dev_item
, device
);
4334 device
->dev_root
= root
->fs_info
->dev_root
;
4335 device
->in_fs_metadata
= 1;
4336 if (device
->writeable
) {
4337 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4338 spin_lock(&root
->fs_info
->free_chunk_lock
);
4339 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4341 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4347 int btrfs_read_sys_array(struct btrfs_root
*root
)
4349 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4350 struct extent_buffer
*sb
;
4351 struct btrfs_disk_key
*disk_key
;
4352 struct btrfs_chunk
*chunk
;
4354 unsigned long sb_ptr
;
4360 struct btrfs_key key
;
4362 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4363 BTRFS_SUPER_INFO_SIZE
);
4366 btrfs_set_buffer_uptodate(sb
);
4367 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4369 * The sb extent buffer is artifical and just used to read the system array.
4370 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4371 * pages up-to-date when the page is larger: extent does not cover the
4372 * whole page and consequently check_page_uptodate does not find all
4373 * the page's extents up-to-date (the hole beyond sb),
4374 * write_extent_buffer then triggers a WARN_ON.
4376 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4377 * but sb spans only this function. Add an explicit SetPageUptodate call
4378 * to silence the warning eg. on PowerPC 64.
4380 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
4381 SetPageUptodate(sb
->first_page
);
4383 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4384 array_size
= btrfs_super_sys_array_size(super_copy
);
4386 ptr
= super_copy
->sys_chunk_array
;
4387 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4390 while (cur
< array_size
) {
4391 disk_key
= (struct btrfs_disk_key
*)ptr
;
4392 btrfs_disk_key_to_cpu(&key
, disk_key
);
4394 len
= sizeof(*disk_key
); ptr
+= len
;
4398 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4399 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4400 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4403 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4404 len
= btrfs_chunk_item_size(num_stripes
);
4413 free_extent_buffer(sb
);
4417 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
4419 struct btrfs_path
*path
;
4420 struct extent_buffer
*leaf
;
4421 struct btrfs_key key
;
4422 struct btrfs_key found_key
;
4426 root
= root
->fs_info
->chunk_root
;
4428 path
= btrfs_alloc_path();
4432 mutex_lock(&uuid_mutex
);
4435 /* first we search for all of the device items, and then we
4436 * read in all of the chunk items. This way we can create chunk
4437 * mappings that reference all of the devices that are afound
4439 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
4443 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4447 leaf
= path
->nodes
[0];
4448 slot
= path
->slots
[0];
4449 if (slot
>= btrfs_header_nritems(leaf
)) {
4450 ret
= btrfs_next_leaf(root
, path
);
4457 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4458 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4459 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
4461 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
4462 struct btrfs_dev_item
*dev_item
;
4463 dev_item
= btrfs_item_ptr(leaf
, slot
,
4464 struct btrfs_dev_item
);
4465 ret
= read_one_dev(root
, leaf
, dev_item
);
4469 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4470 struct btrfs_chunk
*chunk
;
4471 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
4472 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
4478 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4480 btrfs_release_path(path
);
4485 unlock_chunks(root
);
4486 mutex_unlock(&uuid_mutex
);
4488 btrfs_free_path(path
);