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/ratelimit.h>
27 #include <linux/kthread.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"
37 #include "rcu-string.h"
39 #include "dev-replace.h"
41 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
42 struct btrfs_root
*root
,
43 struct btrfs_device
*device
);
44 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
45 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
46 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
48 static DEFINE_MUTEX(uuid_mutex
);
49 static LIST_HEAD(fs_uuids
);
51 static void lock_chunks(struct btrfs_root
*root
)
53 mutex_lock(&root
->fs_info
->chunk_mutex
);
56 static void unlock_chunks(struct btrfs_root
*root
)
58 mutex_unlock(&root
->fs_info
->chunk_mutex
);
61 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
63 struct btrfs_device
*device
;
64 WARN_ON(fs_devices
->opened
);
65 while (!list_empty(&fs_devices
->devices
)) {
66 device
= list_entry(fs_devices
->devices
.next
,
67 struct btrfs_device
, dev_list
);
68 list_del(&device
->dev_list
);
69 rcu_string_free(device
->name
);
75 void btrfs_cleanup_fs_uuids(void)
77 struct btrfs_fs_devices
*fs_devices
;
79 while (!list_empty(&fs_uuids
)) {
80 fs_devices
= list_entry(fs_uuids
.next
,
81 struct btrfs_fs_devices
, list
);
82 list_del(&fs_devices
->list
);
83 free_fs_devices(fs_devices
);
87 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
90 struct btrfs_device
*dev
;
92 list_for_each_entry(dev
, head
, dev_list
) {
93 if (dev
->devid
== devid
&&
94 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
101 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
103 struct btrfs_fs_devices
*fs_devices
;
105 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
106 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
113 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
114 int flush
, struct block_device
**bdev
,
115 struct buffer_head
**bh
)
119 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
122 ret
= PTR_ERR(*bdev
);
123 printk(KERN_INFO
"btrfs: open %s failed\n", device_path
);
128 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
129 ret
= set_blocksize(*bdev
, 4096);
131 blkdev_put(*bdev
, flags
);
134 invalidate_bdev(*bdev
);
135 *bh
= btrfs_read_dev_super(*bdev
);
138 blkdev_put(*bdev
, flags
);
150 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
151 struct bio
*head
, struct bio
*tail
)
154 struct bio
*old_head
;
156 old_head
= pending_bios
->head
;
157 pending_bios
->head
= head
;
158 if (pending_bios
->tail
)
159 tail
->bi_next
= old_head
;
161 pending_bios
->tail
= tail
;
165 * we try to collect pending bios for a device so we don't get a large
166 * number of procs sending bios down to the same device. This greatly
167 * improves the schedulers ability to collect and merge the bios.
169 * But, it also turns into a long list of bios to process and that is sure
170 * to eventually make the worker thread block. The solution here is to
171 * make some progress and then put this work struct back at the end of
172 * the list if the block device is congested. This way, multiple devices
173 * can make progress from a single worker thread.
175 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
178 struct backing_dev_info
*bdi
;
179 struct btrfs_fs_info
*fs_info
;
180 struct btrfs_pending_bios
*pending_bios
;
184 unsigned long num_run
;
185 unsigned long batch_run
= 0;
187 unsigned long last_waited
= 0;
189 int sync_pending
= 0;
190 struct blk_plug plug
;
193 * this function runs all the bios we've collected for
194 * a particular device. We don't want to wander off to
195 * another device without first sending all of these down.
196 * So, setup a plug here and finish it off before we return
198 blk_start_plug(&plug
);
200 bdi
= blk_get_backing_dev_info(device
->bdev
);
201 fs_info
= device
->dev_root
->fs_info
;
202 limit
= btrfs_async_submit_limit(fs_info
);
203 limit
= limit
* 2 / 3;
206 spin_lock(&device
->io_lock
);
211 /* take all the bios off the list at once and process them
212 * later on (without the lock held). But, remember the
213 * tail and other pointers so the bios can be properly reinserted
214 * into the list if we hit congestion
216 if (!force_reg
&& device
->pending_sync_bios
.head
) {
217 pending_bios
= &device
->pending_sync_bios
;
220 pending_bios
= &device
->pending_bios
;
224 pending
= pending_bios
->head
;
225 tail
= pending_bios
->tail
;
226 WARN_ON(pending
&& !tail
);
229 * if pending was null this time around, no bios need processing
230 * at all and we can stop. Otherwise it'll loop back up again
231 * and do an additional check so no bios are missed.
233 * device->running_pending is used to synchronize with the
236 if (device
->pending_sync_bios
.head
== NULL
&&
237 device
->pending_bios
.head
== NULL
) {
239 device
->running_pending
= 0;
242 device
->running_pending
= 1;
245 pending_bios
->head
= NULL
;
246 pending_bios
->tail
= NULL
;
248 spin_unlock(&device
->io_lock
);
253 /* we want to work on both lists, but do more bios on the
254 * sync list than the regular list
257 pending_bios
!= &device
->pending_sync_bios
&&
258 device
->pending_sync_bios
.head
) ||
259 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
260 device
->pending_bios
.head
)) {
261 spin_lock(&device
->io_lock
);
262 requeue_list(pending_bios
, pending
, tail
);
267 pending
= pending
->bi_next
;
270 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
271 waitqueue_active(&fs_info
->async_submit_wait
))
272 wake_up(&fs_info
->async_submit_wait
);
274 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
277 * if we're doing the sync list, record that our
278 * plug has some sync requests on it
280 * If we're doing the regular list and there are
281 * sync requests sitting around, unplug before
284 if (pending_bios
== &device
->pending_sync_bios
) {
286 } else if (sync_pending
) {
287 blk_finish_plug(&plug
);
288 blk_start_plug(&plug
);
292 btrfsic_submit_bio(cur
->bi_rw
, cur
);
299 * we made progress, there is more work to do and the bdi
300 * is now congested. Back off and let other work structs
303 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
304 fs_info
->fs_devices
->open_devices
> 1) {
305 struct io_context
*ioc
;
307 ioc
= current
->io_context
;
310 * the main goal here is that we don't want to
311 * block if we're going to be able to submit
312 * more requests without blocking.
314 * This code does two great things, it pokes into
315 * the elevator code from a filesystem _and_
316 * it makes assumptions about how batching works.
318 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
319 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
321 ioc
->last_waited
== last_waited
)) {
323 * we want to go through our batch of
324 * requests and stop. So, we copy out
325 * the ioc->last_waited time and test
326 * against it before looping
328 last_waited
= ioc
->last_waited
;
333 spin_lock(&device
->io_lock
);
334 requeue_list(pending_bios
, pending
, tail
);
335 device
->running_pending
= 1;
337 spin_unlock(&device
->io_lock
);
338 btrfs_requeue_work(&device
->work
);
341 /* unplug every 64 requests just for good measure */
342 if (batch_run
% 64 == 0) {
343 blk_finish_plug(&plug
);
344 blk_start_plug(&plug
);
353 spin_lock(&device
->io_lock
);
354 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
356 spin_unlock(&device
->io_lock
);
359 blk_finish_plug(&plug
);
362 static void pending_bios_fn(struct btrfs_work
*work
)
364 struct btrfs_device
*device
;
366 device
= container_of(work
, struct btrfs_device
, work
);
367 run_scheduled_bios(device
);
370 static noinline
int device_list_add(const char *path
,
371 struct btrfs_super_block
*disk_super
,
372 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
374 struct btrfs_device
*device
;
375 struct btrfs_fs_devices
*fs_devices
;
376 struct rcu_string
*name
;
377 u64 found_transid
= btrfs_super_generation(disk_super
);
379 fs_devices
= find_fsid(disk_super
->fsid
);
381 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
384 INIT_LIST_HEAD(&fs_devices
->devices
);
385 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
386 list_add(&fs_devices
->list
, &fs_uuids
);
387 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
388 fs_devices
->latest_devid
= devid
;
389 fs_devices
->latest_trans
= found_transid
;
390 mutex_init(&fs_devices
->device_list_mutex
);
393 device
= __find_device(&fs_devices
->devices
, devid
,
394 disk_super
->dev_item
.uuid
);
397 if (fs_devices
->opened
)
400 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
402 /* we can safely leave the fs_devices entry around */
405 device
->devid
= devid
;
406 device
->dev_stats_valid
= 0;
407 device
->work
.func
= pending_bios_fn
;
408 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
410 spin_lock_init(&device
->io_lock
);
412 name
= rcu_string_strdup(path
, GFP_NOFS
);
417 rcu_assign_pointer(device
->name
, name
);
418 INIT_LIST_HEAD(&device
->dev_alloc_list
);
420 /* init readahead state */
421 spin_lock_init(&device
->reada_lock
);
422 device
->reada_curr_zone
= NULL
;
423 atomic_set(&device
->reada_in_flight
, 0);
424 device
->reada_next
= 0;
425 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
426 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
428 mutex_lock(&fs_devices
->device_list_mutex
);
429 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
430 mutex_unlock(&fs_devices
->device_list_mutex
);
432 device
->fs_devices
= fs_devices
;
433 fs_devices
->num_devices
++;
434 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
435 name
= rcu_string_strdup(path
, GFP_NOFS
);
438 rcu_string_free(device
->name
);
439 rcu_assign_pointer(device
->name
, name
);
440 if (device
->missing
) {
441 fs_devices
->missing_devices
--;
446 if (found_transid
> fs_devices
->latest_trans
) {
447 fs_devices
->latest_devid
= devid
;
448 fs_devices
->latest_trans
= found_transid
;
450 *fs_devices_ret
= fs_devices
;
454 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
456 struct btrfs_fs_devices
*fs_devices
;
457 struct btrfs_device
*device
;
458 struct btrfs_device
*orig_dev
;
460 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
462 return ERR_PTR(-ENOMEM
);
464 INIT_LIST_HEAD(&fs_devices
->devices
);
465 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
466 INIT_LIST_HEAD(&fs_devices
->list
);
467 mutex_init(&fs_devices
->device_list_mutex
);
468 fs_devices
->latest_devid
= orig
->latest_devid
;
469 fs_devices
->latest_trans
= orig
->latest_trans
;
470 fs_devices
->total_devices
= orig
->total_devices
;
471 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
473 /* We have held the volume lock, it is safe to get the devices. */
474 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
475 struct rcu_string
*name
;
477 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
482 * This is ok to do without rcu read locked because we hold the
483 * uuid mutex so nothing we touch in here is going to disappear.
485 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
490 rcu_assign_pointer(device
->name
, name
);
492 device
->devid
= orig_dev
->devid
;
493 device
->work
.func
= pending_bios_fn
;
494 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
495 spin_lock_init(&device
->io_lock
);
496 INIT_LIST_HEAD(&device
->dev_list
);
497 INIT_LIST_HEAD(&device
->dev_alloc_list
);
499 list_add(&device
->dev_list
, &fs_devices
->devices
);
500 device
->fs_devices
= fs_devices
;
501 fs_devices
->num_devices
++;
505 free_fs_devices(fs_devices
);
506 return ERR_PTR(-ENOMEM
);
509 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
510 struct btrfs_fs_devices
*fs_devices
, int step
)
512 struct btrfs_device
*device
, *next
;
514 struct block_device
*latest_bdev
= NULL
;
515 u64 latest_devid
= 0;
516 u64 latest_transid
= 0;
518 mutex_lock(&uuid_mutex
);
520 /* This is the initialized path, it is safe to release the devices. */
521 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
522 if (device
->in_fs_metadata
) {
523 if (!device
->is_tgtdev_for_dev_replace
&&
525 device
->generation
> latest_transid
)) {
526 latest_devid
= device
->devid
;
527 latest_transid
= device
->generation
;
528 latest_bdev
= device
->bdev
;
533 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
535 * In the first step, keep the device which has
536 * the correct fsid and the devid that is used
537 * for the dev_replace procedure.
538 * In the second step, the dev_replace state is
539 * read from the device tree and it is known
540 * whether the procedure is really active or
541 * not, which means whether this device is
542 * used or whether it should be removed.
544 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
549 blkdev_put(device
->bdev
, device
->mode
);
551 fs_devices
->open_devices
--;
553 if (device
->writeable
) {
554 list_del_init(&device
->dev_alloc_list
);
555 device
->writeable
= 0;
556 if (!device
->is_tgtdev_for_dev_replace
)
557 fs_devices
->rw_devices
--;
559 list_del_init(&device
->dev_list
);
560 fs_devices
->num_devices
--;
561 rcu_string_free(device
->name
);
565 if (fs_devices
->seed
) {
566 fs_devices
= fs_devices
->seed
;
570 fs_devices
->latest_bdev
= latest_bdev
;
571 fs_devices
->latest_devid
= latest_devid
;
572 fs_devices
->latest_trans
= latest_transid
;
574 mutex_unlock(&uuid_mutex
);
577 static void __free_device(struct work_struct
*work
)
579 struct btrfs_device
*device
;
581 device
= container_of(work
, struct btrfs_device
, rcu_work
);
584 blkdev_put(device
->bdev
, device
->mode
);
586 rcu_string_free(device
->name
);
590 static void free_device(struct rcu_head
*head
)
592 struct btrfs_device
*device
;
594 device
= container_of(head
, struct btrfs_device
, rcu
);
596 INIT_WORK(&device
->rcu_work
, __free_device
);
597 schedule_work(&device
->rcu_work
);
600 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
602 struct btrfs_device
*device
;
604 if (--fs_devices
->opened
> 0)
607 mutex_lock(&fs_devices
->device_list_mutex
);
608 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
609 struct btrfs_device
*new_device
;
610 struct rcu_string
*name
;
613 fs_devices
->open_devices
--;
615 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
616 list_del_init(&device
->dev_alloc_list
);
617 fs_devices
->rw_devices
--;
620 if (device
->can_discard
)
621 fs_devices
->num_can_discard
--;
623 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
624 BUG_ON(!new_device
); /* -ENOMEM */
625 memcpy(new_device
, device
, sizeof(*new_device
));
627 /* Safe because we are under uuid_mutex */
629 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
630 BUG_ON(device
->name
&& !name
); /* -ENOMEM */
631 rcu_assign_pointer(new_device
->name
, name
);
633 new_device
->bdev
= NULL
;
634 new_device
->writeable
= 0;
635 new_device
->in_fs_metadata
= 0;
636 new_device
->can_discard
= 0;
637 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
639 call_rcu(&device
->rcu
, free_device
);
641 mutex_unlock(&fs_devices
->device_list_mutex
);
643 WARN_ON(fs_devices
->open_devices
);
644 WARN_ON(fs_devices
->rw_devices
);
645 fs_devices
->opened
= 0;
646 fs_devices
->seeding
= 0;
651 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
653 struct btrfs_fs_devices
*seed_devices
= NULL
;
656 mutex_lock(&uuid_mutex
);
657 ret
= __btrfs_close_devices(fs_devices
);
658 if (!fs_devices
->opened
) {
659 seed_devices
= fs_devices
->seed
;
660 fs_devices
->seed
= NULL
;
662 mutex_unlock(&uuid_mutex
);
664 while (seed_devices
) {
665 fs_devices
= seed_devices
;
666 seed_devices
= fs_devices
->seed
;
667 __btrfs_close_devices(fs_devices
);
668 free_fs_devices(fs_devices
);
673 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
674 fmode_t flags
, void *holder
)
676 struct request_queue
*q
;
677 struct block_device
*bdev
;
678 struct list_head
*head
= &fs_devices
->devices
;
679 struct btrfs_device
*device
;
680 struct block_device
*latest_bdev
= NULL
;
681 struct buffer_head
*bh
;
682 struct btrfs_super_block
*disk_super
;
683 u64 latest_devid
= 0;
684 u64 latest_transid
= 0;
691 list_for_each_entry(device
, head
, dev_list
) {
697 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
702 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
703 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
704 if (devid
!= device
->devid
)
707 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
711 device
->generation
= btrfs_super_generation(disk_super
);
712 if (!latest_transid
|| device
->generation
> latest_transid
) {
713 latest_devid
= devid
;
714 latest_transid
= device
->generation
;
718 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
719 device
->writeable
= 0;
721 device
->writeable
= !bdev_read_only(bdev
);
725 q
= bdev_get_queue(bdev
);
726 if (blk_queue_discard(q
)) {
727 device
->can_discard
= 1;
728 fs_devices
->num_can_discard
++;
732 device
->in_fs_metadata
= 0;
733 device
->mode
= flags
;
735 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
736 fs_devices
->rotating
= 1;
738 fs_devices
->open_devices
++;
739 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
740 fs_devices
->rw_devices
++;
741 list_add(&device
->dev_alloc_list
,
742 &fs_devices
->alloc_list
);
749 blkdev_put(bdev
, flags
);
752 if (fs_devices
->open_devices
== 0) {
756 fs_devices
->seeding
= seeding
;
757 fs_devices
->opened
= 1;
758 fs_devices
->latest_bdev
= latest_bdev
;
759 fs_devices
->latest_devid
= latest_devid
;
760 fs_devices
->latest_trans
= latest_transid
;
761 fs_devices
->total_rw_bytes
= 0;
766 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
767 fmode_t flags
, void *holder
)
771 mutex_lock(&uuid_mutex
);
772 if (fs_devices
->opened
) {
773 fs_devices
->opened
++;
776 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
778 mutex_unlock(&uuid_mutex
);
782 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
783 struct btrfs_fs_devices
**fs_devices_ret
)
785 struct btrfs_super_block
*disk_super
;
786 struct block_device
*bdev
;
787 struct buffer_head
*bh
;
794 mutex_lock(&uuid_mutex
);
795 ret
= btrfs_get_bdev_and_sb(path
, flags
, holder
, 0, &bdev
, &bh
);
798 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
799 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
800 transid
= btrfs_super_generation(disk_super
);
801 total_devices
= btrfs_super_num_devices(disk_super
);
802 if (disk_super
->label
[0]) {
803 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
804 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
805 printk(KERN_INFO
"device label %s ", disk_super
->label
);
807 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
809 printk(KERN_CONT
"devid %llu transid %llu %s\n",
810 (unsigned long long)devid
, (unsigned long long)transid
, path
);
811 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
812 if (!ret
&& fs_devices_ret
)
813 (*fs_devices_ret
)->total_devices
= total_devices
;
815 blkdev_put(bdev
, flags
);
817 mutex_unlock(&uuid_mutex
);
821 /* helper to account the used device space in the range */
822 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
823 u64 end
, u64
*length
)
825 struct btrfs_key key
;
826 struct btrfs_root
*root
= device
->dev_root
;
827 struct btrfs_dev_extent
*dev_extent
;
828 struct btrfs_path
*path
;
832 struct extent_buffer
*l
;
836 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
839 path
= btrfs_alloc_path();
844 key
.objectid
= device
->devid
;
846 key
.type
= BTRFS_DEV_EXTENT_KEY
;
848 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
852 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
859 slot
= path
->slots
[0];
860 if (slot
>= btrfs_header_nritems(l
)) {
861 ret
= btrfs_next_leaf(root
, path
);
869 btrfs_item_key_to_cpu(l
, &key
, slot
);
871 if (key
.objectid
< device
->devid
)
874 if (key
.objectid
> device
->devid
)
877 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
880 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
881 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
883 if (key
.offset
<= start
&& extent_end
> end
) {
884 *length
= end
- start
+ 1;
886 } else if (key
.offset
<= start
&& extent_end
> start
)
887 *length
+= extent_end
- start
;
888 else if (key
.offset
> start
&& extent_end
<= end
)
889 *length
+= extent_end
- key
.offset
;
890 else if (key
.offset
> start
&& key
.offset
<= end
) {
891 *length
+= end
- key
.offset
+ 1;
893 } else if (key
.offset
> end
)
901 btrfs_free_path(path
);
906 * find_free_dev_extent - find free space in the specified device
907 * @device: the device which we search the free space in
908 * @num_bytes: the size of the free space that we need
909 * @start: store the start of the free space.
910 * @len: the size of the free space. that we find, or the size of the max
911 * free space if we don't find suitable free space
913 * this uses a pretty simple search, the expectation is that it is
914 * called very infrequently and that a given device has a small number
917 * @start is used to store the start of the free space if we find. But if we
918 * don't find suitable free space, it will be used to store the start position
919 * of the max free space.
921 * @len is used to store the size of the free space that we find.
922 * But if we don't find suitable free space, it is used to store the size of
923 * the max free space.
925 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
926 u64
*start
, u64
*len
)
928 struct btrfs_key key
;
929 struct btrfs_root
*root
= device
->dev_root
;
930 struct btrfs_dev_extent
*dev_extent
;
931 struct btrfs_path
*path
;
937 u64 search_end
= device
->total_bytes
;
940 struct extent_buffer
*l
;
942 /* FIXME use last free of some kind */
944 /* we don't want to overwrite the superblock on the drive,
945 * so we make sure to start at an offset of at least 1MB
947 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
949 max_hole_start
= search_start
;
953 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
958 path
= btrfs_alloc_path();
965 key
.objectid
= device
->devid
;
966 key
.offset
= search_start
;
967 key
.type
= BTRFS_DEV_EXTENT_KEY
;
969 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
973 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
980 slot
= path
->slots
[0];
981 if (slot
>= btrfs_header_nritems(l
)) {
982 ret
= btrfs_next_leaf(root
, path
);
990 btrfs_item_key_to_cpu(l
, &key
, slot
);
992 if (key
.objectid
< device
->devid
)
995 if (key
.objectid
> device
->devid
)
998 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1001 if (key
.offset
> search_start
) {
1002 hole_size
= key
.offset
- search_start
;
1004 if (hole_size
> max_hole_size
) {
1005 max_hole_start
= search_start
;
1006 max_hole_size
= hole_size
;
1010 * If this free space is greater than which we need,
1011 * it must be the max free space that we have found
1012 * until now, so max_hole_start must point to the start
1013 * of this free space and the length of this free space
1014 * is stored in max_hole_size. Thus, we return
1015 * max_hole_start and max_hole_size and go back to the
1018 if (hole_size
>= num_bytes
) {
1024 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1025 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1027 if (extent_end
> search_start
)
1028 search_start
= extent_end
;
1035 * At this point, search_start should be the end of
1036 * allocated dev extents, and when shrinking the device,
1037 * search_end may be smaller than search_start.
1039 if (search_end
> search_start
)
1040 hole_size
= search_end
- search_start
;
1042 if (hole_size
> max_hole_size
) {
1043 max_hole_start
= search_start
;
1044 max_hole_size
= hole_size
;
1048 if (hole_size
< num_bytes
)
1054 btrfs_free_path(path
);
1056 *start
= max_hole_start
;
1058 *len
= max_hole_size
;
1062 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1063 struct btrfs_device
*device
,
1067 struct btrfs_path
*path
;
1068 struct btrfs_root
*root
= device
->dev_root
;
1069 struct btrfs_key key
;
1070 struct btrfs_key found_key
;
1071 struct extent_buffer
*leaf
= NULL
;
1072 struct btrfs_dev_extent
*extent
= NULL
;
1074 path
= btrfs_alloc_path();
1078 key
.objectid
= device
->devid
;
1080 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1082 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1084 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1085 BTRFS_DEV_EXTENT_KEY
);
1088 leaf
= path
->nodes
[0];
1089 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1090 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1091 struct btrfs_dev_extent
);
1092 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1093 btrfs_dev_extent_length(leaf
, extent
) < start
);
1095 btrfs_release_path(path
);
1097 } else if (ret
== 0) {
1098 leaf
= path
->nodes
[0];
1099 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1100 struct btrfs_dev_extent
);
1102 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1106 if (device
->bytes_used
> 0) {
1107 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1108 device
->bytes_used
-= len
;
1109 spin_lock(&root
->fs_info
->free_chunk_lock
);
1110 root
->fs_info
->free_chunk_space
+= len
;
1111 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1113 ret
= btrfs_del_item(trans
, root
, path
);
1115 btrfs_error(root
->fs_info
, ret
,
1116 "Failed to remove dev extent item");
1119 btrfs_free_path(path
);
1123 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1124 struct btrfs_device
*device
,
1125 u64 chunk_tree
, u64 chunk_objectid
,
1126 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1129 struct btrfs_path
*path
;
1130 struct btrfs_root
*root
= device
->dev_root
;
1131 struct btrfs_dev_extent
*extent
;
1132 struct extent_buffer
*leaf
;
1133 struct btrfs_key key
;
1135 WARN_ON(!device
->in_fs_metadata
);
1136 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1137 path
= btrfs_alloc_path();
1141 key
.objectid
= device
->devid
;
1143 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1144 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1149 leaf
= path
->nodes
[0];
1150 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1151 struct btrfs_dev_extent
);
1152 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1153 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1154 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1156 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1157 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1160 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1161 btrfs_mark_buffer_dirty(leaf
);
1163 btrfs_free_path(path
);
1167 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1168 u64 objectid
, u64
*offset
)
1170 struct btrfs_path
*path
;
1172 struct btrfs_key key
;
1173 struct btrfs_chunk
*chunk
;
1174 struct btrfs_key found_key
;
1176 path
= btrfs_alloc_path();
1180 key
.objectid
= objectid
;
1181 key
.offset
= (u64
)-1;
1182 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1184 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1188 BUG_ON(ret
== 0); /* Corruption */
1190 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1194 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1196 if (found_key
.objectid
!= objectid
)
1199 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1200 struct btrfs_chunk
);
1201 *offset
= found_key
.offset
+
1202 btrfs_chunk_length(path
->nodes
[0], chunk
);
1207 btrfs_free_path(path
);
1211 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1214 struct btrfs_key key
;
1215 struct btrfs_key found_key
;
1216 struct btrfs_path
*path
;
1218 root
= root
->fs_info
->chunk_root
;
1220 path
= btrfs_alloc_path();
1224 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1225 key
.type
= BTRFS_DEV_ITEM_KEY
;
1226 key
.offset
= (u64
)-1;
1228 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1232 BUG_ON(ret
== 0); /* Corruption */
1234 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1235 BTRFS_DEV_ITEM_KEY
);
1239 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1241 *objectid
= found_key
.offset
+ 1;
1245 btrfs_free_path(path
);
1250 * the device information is stored in the chunk root
1251 * the btrfs_device struct should be fully filled in
1253 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1254 struct btrfs_root
*root
,
1255 struct btrfs_device
*device
)
1258 struct btrfs_path
*path
;
1259 struct btrfs_dev_item
*dev_item
;
1260 struct extent_buffer
*leaf
;
1261 struct btrfs_key key
;
1264 root
= root
->fs_info
->chunk_root
;
1266 path
= btrfs_alloc_path();
1270 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1271 key
.type
= BTRFS_DEV_ITEM_KEY
;
1272 key
.offset
= device
->devid
;
1274 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1279 leaf
= path
->nodes
[0];
1280 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1282 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1283 btrfs_set_device_generation(leaf
, dev_item
, 0);
1284 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1285 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1286 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1287 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1288 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1289 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1290 btrfs_set_device_group(leaf
, dev_item
, 0);
1291 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1292 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1293 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1295 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1296 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1297 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1298 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1299 btrfs_mark_buffer_dirty(leaf
);
1303 btrfs_free_path(path
);
1307 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1308 struct btrfs_device
*device
)
1311 struct btrfs_path
*path
;
1312 struct btrfs_key key
;
1313 struct btrfs_trans_handle
*trans
;
1315 root
= root
->fs_info
->chunk_root
;
1317 path
= btrfs_alloc_path();
1321 trans
= btrfs_start_transaction(root
, 0);
1322 if (IS_ERR(trans
)) {
1323 btrfs_free_path(path
);
1324 return PTR_ERR(trans
);
1326 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1327 key
.type
= BTRFS_DEV_ITEM_KEY
;
1328 key
.offset
= device
->devid
;
1331 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1340 ret
= btrfs_del_item(trans
, root
, path
);
1344 btrfs_free_path(path
);
1345 unlock_chunks(root
);
1346 btrfs_commit_transaction(trans
, root
);
1350 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1352 struct btrfs_device
*device
;
1353 struct btrfs_device
*next_device
;
1354 struct block_device
*bdev
;
1355 struct buffer_head
*bh
= NULL
;
1356 struct btrfs_super_block
*disk_super
;
1357 struct btrfs_fs_devices
*cur_devices
;
1363 bool clear_super
= false;
1365 mutex_lock(&uuid_mutex
);
1367 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1368 root
->fs_info
->avail_system_alloc_bits
|
1369 root
->fs_info
->avail_metadata_alloc_bits
;
1371 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1372 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1373 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1374 WARN_ON(num_devices
< 1);
1377 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1379 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1380 printk(KERN_ERR
"btrfs: unable to go below four devices "
1386 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1387 printk(KERN_ERR
"btrfs: unable to go below two "
1388 "devices on raid1\n");
1393 if (strcmp(device_path
, "missing") == 0) {
1394 struct list_head
*devices
;
1395 struct btrfs_device
*tmp
;
1398 devices
= &root
->fs_info
->fs_devices
->devices
;
1400 * It is safe to read the devices since the volume_mutex
1403 list_for_each_entry(tmp
, devices
, dev_list
) {
1404 if (tmp
->in_fs_metadata
&&
1405 !tmp
->is_tgtdev_for_dev_replace
&&
1415 printk(KERN_ERR
"btrfs: no missing devices found to "
1420 ret
= btrfs_get_bdev_and_sb(device_path
,
1421 FMODE_READ
| FMODE_EXCL
,
1422 root
->fs_info
->bdev_holder
, 0,
1426 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1427 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1428 dev_uuid
= disk_super
->dev_item
.uuid
;
1429 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1437 if (device
->is_tgtdev_for_dev_replace
) {
1438 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1443 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1444 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1450 if (device
->writeable
) {
1452 list_del_init(&device
->dev_alloc_list
);
1453 unlock_chunks(root
);
1454 root
->fs_info
->fs_devices
->rw_devices
--;
1458 ret
= btrfs_shrink_device(device
, 0);
1463 * TODO: the superblock still includes this device in its num_devices
1464 * counter although write_all_supers() is not locked out. This
1465 * could give a filesystem state which requires a degraded mount.
1467 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1471 spin_lock(&root
->fs_info
->free_chunk_lock
);
1472 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1474 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1476 device
->in_fs_metadata
= 0;
1477 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1480 * the device list mutex makes sure that we don't change
1481 * the device list while someone else is writing out all
1482 * the device supers.
1485 cur_devices
= device
->fs_devices
;
1486 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1487 list_del_rcu(&device
->dev_list
);
1489 device
->fs_devices
->num_devices
--;
1490 device
->fs_devices
->total_devices
--;
1492 if (device
->missing
)
1493 root
->fs_info
->fs_devices
->missing_devices
--;
1495 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1496 struct btrfs_device
, dev_list
);
1497 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1498 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1499 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1500 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1503 device
->fs_devices
->open_devices
--;
1505 call_rcu(&device
->rcu
, free_device
);
1506 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1508 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1509 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1511 if (cur_devices
->open_devices
== 0) {
1512 struct btrfs_fs_devices
*fs_devices
;
1513 fs_devices
= root
->fs_info
->fs_devices
;
1514 while (fs_devices
) {
1515 if (fs_devices
->seed
== cur_devices
)
1517 fs_devices
= fs_devices
->seed
;
1519 fs_devices
->seed
= cur_devices
->seed
;
1520 cur_devices
->seed
= NULL
;
1522 __btrfs_close_devices(cur_devices
);
1523 unlock_chunks(root
);
1524 free_fs_devices(cur_devices
);
1527 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1528 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1531 * at this point, the device is zero sized. We want to
1532 * remove it from the devices list and zero out the old super
1534 if (clear_super
&& disk_super
) {
1535 /* make sure this device isn't detected as part of
1538 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1539 set_buffer_dirty(bh
);
1540 sync_dirty_buffer(bh
);
1549 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1551 mutex_unlock(&uuid_mutex
);
1554 if (device
->writeable
) {
1556 list_add(&device
->dev_alloc_list
,
1557 &root
->fs_info
->fs_devices
->alloc_list
);
1558 unlock_chunks(root
);
1559 root
->fs_info
->fs_devices
->rw_devices
++;
1564 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1565 struct btrfs_device
*srcdev
)
1567 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1568 list_del_rcu(&srcdev
->dev_list
);
1569 list_del_rcu(&srcdev
->dev_alloc_list
);
1570 fs_info
->fs_devices
->num_devices
--;
1571 if (srcdev
->missing
) {
1572 fs_info
->fs_devices
->missing_devices
--;
1573 fs_info
->fs_devices
->rw_devices
++;
1575 if (srcdev
->can_discard
)
1576 fs_info
->fs_devices
->num_can_discard
--;
1578 fs_info
->fs_devices
->open_devices
--;
1580 call_rcu(&srcdev
->rcu
, free_device
);
1583 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1584 struct btrfs_device
*tgtdev
)
1586 struct btrfs_device
*next_device
;
1589 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1591 btrfs_scratch_superblock(tgtdev
);
1592 fs_info
->fs_devices
->open_devices
--;
1594 fs_info
->fs_devices
->num_devices
--;
1595 if (tgtdev
->can_discard
)
1596 fs_info
->fs_devices
->num_can_discard
++;
1598 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1599 struct btrfs_device
, dev_list
);
1600 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1601 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1602 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1603 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1604 list_del_rcu(&tgtdev
->dev_list
);
1606 call_rcu(&tgtdev
->rcu
, free_device
);
1608 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1611 int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1612 struct btrfs_device
**device
)
1615 struct btrfs_super_block
*disk_super
;
1618 struct block_device
*bdev
;
1619 struct buffer_head
*bh
;
1622 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1623 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1626 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1627 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1628 dev_uuid
= disk_super
->dev_item
.uuid
;
1629 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1634 blkdev_put(bdev
, FMODE_READ
);
1638 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1640 struct btrfs_device
**device
)
1643 if (strcmp(device_path
, "missing") == 0) {
1644 struct list_head
*devices
;
1645 struct btrfs_device
*tmp
;
1647 devices
= &root
->fs_info
->fs_devices
->devices
;
1649 * It is safe to read the devices since the volume_mutex
1650 * is held by the caller.
1652 list_for_each_entry(tmp
, devices
, dev_list
) {
1653 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1660 pr_err("btrfs: no missing device found\n");
1666 return btrfs_find_device_by_path(root
, device_path
, device
);
1671 * does all the dirty work required for changing file system's UUID.
1673 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1675 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1676 struct btrfs_fs_devices
*old_devices
;
1677 struct btrfs_fs_devices
*seed_devices
;
1678 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1679 struct btrfs_device
*device
;
1682 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1683 if (!fs_devices
->seeding
)
1686 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1690 old_devices
= clone_fs_devices(fs_devices
);
1691 if (IS_ERR(old_devices
)) {
1692 kfree(seed_devices
);
1693 return PTR_ERR(old_devices
);
1696 list_add(&old_devices
->list
, &fs_uuids
);
1698 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1699 seed_devices
->opened
= 1;
1700 INIT_LIST_HEAD(&seed_devices
->devices
);
1701 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1702 mutex_init(&seed_devices
->device_list_mutex
);
1704 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1705 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1707 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1709 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1710 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1711 device
->fs_devices
= seed_devices
;
1714 fs_devices
->seeding
= 0;
1715 fs_devices
->num_devices
= 0;
1716 fs_devices
->open_devices
= 0;
1717 fs_devices
->total_devices
= 0;
1718 fs_devices
->seed
= seed_devices
;
1720 generate_random_uuid(fs_devices
->fsid
);
1721 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1722 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1723 super_flags
= btrfs_super_flags(disk_super
) &
1724 ~BTRFS_SUPER_FLAG_SEEDING
;
1725 btrfs_set_super_flags(disk_super
, super_flags
);
1731 * strore the expected generation for seed devices in device items.
1733 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1734 struct btrfs_root
*root
)
1736 struct btrfs_path
*path
;
1737 struct extent_buffer
*leaf
;
1738 struct btrfs_dev_item
*dev_item
;
1739 struct btrfs_device
*device
;
1740 struct btrfs_key key
;
1741 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1742 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1746 path
= btrfs_alloc_path();
1750 root
= root
->fs_info
->chunk_root
;
1751 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1753 key
.type
= BTRFS_DEV_ITEM_KEY
;
1756 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1760 leaf
= path
->nodes
[0];
1762 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1763 ret
= btrfs_next_leaf(root
, path
);
1768 leaf
= path
->nodes
[0];
1769 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1770 btrfs_release_path(path
);
1774 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1775 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1776 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1779 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1780 struct btrfs_dev_item
);
1781 devid
= btrfs_device_id(leaf
, dev_item
);
1782 read_extent_buffer(leaf
, dev_uuid
,
1783 (unsigned long)btrfs_device_uuid(dev_item
),
1785 read_extent_buffer(leaf
, fs_uuid
,
1786 (unsigned long)btrfs_device_fsid(dev_item
),
1788 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1790 BUG_ON(!device
); /* Logic error */
1792 if (device
->fs_devices
->seeding
) {
1793 btrfs_set_device_generation(leaf
, dev_item
,
1794 device
->generation
);
1795 btrfs_mark_buffer_dirty(leaf
);
1803 btrfs_free_path(path
);
1807 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1809 struct request_queue
*q
;
1810 struct btrfs_trans_handle
*trans
;
1811 struct btrfs_device
*device
;
1812 struct block_device
*bdev
;
1813 struct list_head
*devices
;
1814 struct super_block
*sb
= root
->fs_info
->sb
;
1815 struct rcu_string
*name
;
1817 int seeding_dev
= 0;
1820 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1823 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1824 root
->fs_info
->bdev_holder
);
1826 return PTR_ERR(bdev
);
1828 if (root
->fs_info
->fs_devices
->seeding
) {
1830 down_write(&sb
->s_umount
);
1831 mutex_lock(&uuid_mutex
);
1834 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1836 devices
= &root
->fs_info
->fs_devices
->devices
;
1838 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1839 list_for_each_entry(device
, devices
, dev_list
) {
1840 if (device
->bdev
== bdev
) {
1843 &root
->fs_info
->fs_devices
->device_list_mutex
);
1847 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1849 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1851 /* we can safely leave the fs_devices entry around */
1856 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1862 rcu_assign_pointer(device
->name
, name
);
1864 ret
= find_next_devid(root
, &device
->devid
);
1866 rcu_string_free(device
->name
);
1871 trans
= btrfs_start_transaction(root
, 0);
1872 if (IS_ERR(trans
)) {
1873 rcu_string_free(device
->name
);
1875 ret
= PTR_ERR(trans
);
1881 q
= bdev_get_queue(bdev
);
1882 if (blk_queue_discard(q
))
1883 device
->can_discard
= 1;
1884 device
->writeable
= 1;
1885 device
->work
.func
= pending_bios_fn
;
1886 generate_random_uuid(device
->uuid
);
1887 spin_lock_init(&device
->io_lock
);
1888 device
->generation
= trans
->transid
;
1889 device
->io_width
= root
->sectorsize
;
1890 device
->io_align
= root
->sectorsize
;
1891 device
->sector_size
= root
->sectorsize
;
1892 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1893 device
->disk_total_bytes
= device
->total_bytes
;
1894 device
->dev_root
= root
->fs_info
->dev_root
;
1895 device
->bdev
= bdev
;
1896 device
->in_fs_metadata
= 1;
1897 device
->is_tgtdev_for_dev_replace
= 0;
1898 device
->mode
= FMODE_EXCL
;
1899 set_blocksize(device
->bdev
, 4096);
1902 sb
->s_flags
&= ~MS_RDONLY
;
1903 ret
= btrfs_prepare_sprout(root
);
1904 BUG_ON(ret
); /* -ENOMEM */
1907 device
->fs_devices
= root
->fs_info
->fs_devices
;
1909 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1910 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1911 list_add(&device
->dev_alloc_list
,
1912 &root
->fs_info
->fs_devices
->alloc_list
);
1913 root
->fs_info
->fs_devices
->num_devices
++;
1914 root
->fs_info
->fs_devices
->open_devices
++;
1915 root
->fs_info
->fs_devices
->rw_devices
++;
1916 root
->fs_info
->fs_devices
->total_devices
++;
1917 if (device
->can_discard
)
1918 root
->fs_info
->fs_devices
->num_can_discard
++;
1919 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1921 spin_lock(&root
->fs_info
->free_chunk_lock
);
1922 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1923 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1925 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1926 root
->fs_info
->fs_devices
->rotating
= 1;
1928 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1929 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1930 total_bytes
+ device
->total_bytes
);
1932 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1933 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1935 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1938 ret
= init_first_rw_device(trans
, root
, device
);
1940 btrfs_abort_transaction(trans
, root
, ret
);
1943 ret
= btrfs_finish_sprout(trans
, root
);
1945 btrfs_abort_transaction(trans
, root
, ret
);
1949 ret
= btrfs_add_device(trans
, root
, device
);
1951 btrfs_abort_transaction(trans
, root
, ret
);
1957 * we've got more storage, clear any full flags on the space
1960 btrfs_clear_space_info_full(root
->fs_info
);
1962 unlock_chunks(root
);
1963 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1964 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1965 ret
= btrfs_commit_transaction(trans
, root
);
1968 mutex_unlock(&uuid_mutex
);
1969 up_write(&sb
->s_umount
);
1971 if (ret
) /* transaction commit */
1974 ret
= btrfs_relocate_sys_chunks(root
);
1976 btrfs_error(root
->fs_info
, ret
,
1977 "Failed to relocate sys chunks after "
1978 "device initialization. This can be fixed "
1979 "using the \"btrfs balance\" command.");
1980 trans
= btrfs_attach_transaction(root
);
1981 if (IS_ERR(trans
)) {
1982 if (PTR_ERR(trans
) == -ENOENT
)
1984 return PTR_ERR(trans
);
1986 ret
= btrfs_commit_transaction(trans
, root
);
1992 unlock_chunks(root
);
1993 btrfs_end_transaction(trans
, root
);
1994 rcu_string_free(device
->name
);
1997 blkdev_put(bdev
, FMODE_EXCL
);
1999 mutex_unlock(&uuid_mutex
);
2000 up_write(&sb
->s_umount
);
2005 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2006 struct btrfs_device
**device_out
)
2008 struct request_queue
*q
;
2009 struct btrfs_device
*device
;
2010 struct block_device
*bdev
;
2011 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2012 struct list_head
*devices
;
2013 struct rcu_string
*name
;
2017 if (fs_info
->fs_devices
->seeding
)
2020 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2021 fs_info
->bdev_holder
);
2023 return PTR_ERR(bdev
);
2025 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2027 devices
= &fs_info
->fs_devices
->devices
;
2028 list_for_each_entry(device
, devices
, dev_list
) {
2029 if (device
->bdev
== bdev
) {
2035 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2041 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2047 rcu_assign_pointer(device
->name
, name
);
2049 q
= bdev_get_queue(bdev
);
2050 if (blk_queue_discard(q
))
2051 device
->can_discard
= 1;
2052 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2053 device
->writeable
= 1;
2054 device
->work
.func
= pending_bios_fn
;
2055 generate_random_uuid(device
->uuid
);
2056 device
->devid
= BTRFS_DEV_REPLACE_DEVID
;
2057 spin_lock_init(&device
->io_lock
);
2058 device
->generation
= 0;
2059 device
->io_width
= root
->sectorsize
;
2060 device
->io_align
= root
->sectorsize
;
2061 device
->sector_size
= root
->sectorsize
;
2062 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2063 device
->disk_total_bytes
= device
->total_bytes
;
2064 device
->dev_root
= fs_info
->dev_root
;
2065 device
->bdev
= bdev
;
2066 device
->in_fs_metadata
= 1;
2067 device
->is_tgtdev_for_dev_replace
= 1;
2068 device
->mode
= FMODE_EXCL
;
2069 set_blocksize(device
->bdev
, 4096);
2070 device
->fs_devices
= fs_info
->fs_devices
;
2071 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2072 fs_info
->fs_devices
->num_devices
++;
2073 fs_info
->fs_devices
->open_devices
++;
2074 if (device
->can_discard
)
2075 fs_info
->fs_devices
->num_can_discard
++;
2076 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2078 *device_out
= device
;
2082 blkdev_put(bdev
, FMODE_EXCL
);
2086 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2087 struct btrfs_device
*tgtdev
)
2089 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2090 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2091 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2092 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2093 tgtdev
->dev_root
= fs_info
->dev_root
;
2094 tgtdev
->in_fs_metadata
= 1;
2097 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2098 struct btrfs_device
*device
)
2101 struct btrfs_path
*path
;
2102 struct btrfs_root
*root
;
2103 struct btrfs_dev_item
*dev_item
;
2104 struct extent_buffer
*leaf
;
2105 struct btrfs_key key
;
2107 root
= device
->dev_root
->fs_info
->chunk_root
;
2109 path
= btrfs_alloc_path();
2113 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2114 key
.type
= BTRFS_DEV_ITEM_KEY
;
2115 key
.offset
= device
->devid
;
2117 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2126 leaf
= path
->nodes
[0];
2127 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2129 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2130 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2131 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2132 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2133 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2134 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2135 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2136 btrfs_mark_buffer_dirty(leaf
);
2139 btrfs_free_path(path
);
2143 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2144 struct btrfs_device
*device
, u64 new_size
)
2146 struct btrfs_super_block
*super_copy
=
2147 device
->dev_root
->fs_info
->super_copy
;
2148 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2149 u64 diff
= new_size
- device
->total_bytes
;
2151 if (!device
->writeable
)
2153 if (new_size
<= device
->total_bytes
||
2154 device
->is_tgtdev_for_dev_replace
)
2157 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2158 device
->fs_devices
->total_rw_bytes
+= diff
;
2160 device
->total_bytes
= new_size
;
2161 device
->disk_total_bytes
= new_size
;
2162 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2164 return btrfs_update_device(trans
, device
);
2167 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2168 struct btrfs_device
*device
, u64 new_size
)
2171 lock_chunks(device
->dev_root
);
2172 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2173 unlock_chunks(device
->dev_root
);
2177 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2178 struct btrfs_root
*root
,
2179 u64 chunk_tree
, u64 chunk_objectid
,
2183 struct btrfs_path
*path
;
2184 struct btrfs_key key
;
2186 root
= root
->fs_info
->chunk_root
;
2187 path
= btrfs_alloc_path();
2191 key
.objectid
= chunk_objectid
;
2192 key
.offset
= chunk_offset
;
2193 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2195 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2198 else if (ret
> 0) { /* Logic error or corruption */
2199 btrfs_error(root
->fs_info
, -ENOENT
,
2200 "Failed lookup while freeing chunk.");
2205 ret
= btrfs_del_item(trans
, root
, path
);
2207 btrfs_error(root
->fs_info
, ret
,
2208 "Failed to delete chunk item.");
2210 btrfs_free_path(path
);
2214 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2217 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2218 struct btrfs_disk_key
*disk_key
;
2219 struct btrfs_chunk
*chunk
;
2226 struct btrfs_key key
;
2228 array_size
= btrfs_super_sys_array_size(super_copy
);
2230 ptr
= super_copy
->sys_chunk_array
;
2233 while (cur
< array_size
) {
2234 disk_key
= (struct btrfs_disk_key
*)ptr
;
2235 btrfs_disk_key_to_cpu(&key
, disk_key
);
2237 len
= sizeof(*disk_key
);
2239 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2240 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2241 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2242 len
+= btrfs_chunk_item_size(num_stripes
);
2247 if (key
.objectid
== chunk_objectid
&&
2248 key
.offset
== chunk_offset
) {
2249 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2251 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2260 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2261 u64 chunk_tree
, u64 chunk_objectid
,
2264 struct extent_map_tree
*em_tree
;
2265 struct btrfs_root
*extent_root
;
2266 struct btrfs_trans_handle
*trans
;
2267 struct extent_map
*em
;
2268 struct map_lookup
*map
;
2272 root
= root
->fs_info
->chunk_root
;
2273 extent_root
= root
->fs_info
->extent_root
;
2274 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2276 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2280 /* step one, relocate all the extents inside this chunk */
2281 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2285 trans
= btrfs_start_transaction(root
, 0);
2286 BUG_ON(IS_ERR(trans
));
2291 * step two, delete the device extents and the
2292 * chunk tree entries
2294 read_lock(&em_tree
->lock
);
2295 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2296 read_unlock(&em_tree
->lock
);
2298 BUG_ON(!em
|| em
->start
> chunk_offset
||
2299 em
->start
+ em
->len
< chunk_offset
);
2300 map
= (struct map_lookup
*)em
->bdev
;
2302 for (i
= 0; i
< map
->num_stripes
; i
++) {
2303 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2304 map
->stripes
[i
].physical
);
2307 if (map
->stripes
[i
].dev
) {
2308 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2312 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2317 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2319 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2320 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2324 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2327 write_lock(&em_tree
->lock
);
2328 remove_extent_mapping(em_tree
, em
);
2329 write_unlock(&em_tree
->lock
);
2334 /* once for the tree */
2335 free_extent_map(em
);
2337 free_extent_map(em
);
2339 unlock_chunks(root
);
2340 btrfs_end_transaction(trans
, root
);
2344 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2346 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2347 struct btrfs_path
*path
;
2348 struct extent_buffer
*leaf
;
2349 struct btrfs_chunk
*chunk
;
2350 struct btrfs_key key
;
2351 struct btrfs_key found_key
;
2352 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2354 bool retried
= false;
2358 path
= btrfs_alloc_path();
2363 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2364 key
.offset
= (u64
)-1;
2365 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2368 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2371 BUG_ON(ret
== 0); /* Corruption */
2373 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2380 leaf
= path
->nodes
[0];
2381 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2383 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2384 struct btrfs_chunk
);
2385 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2386 btrfs_release_path(path
);
2388 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2389 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2398 if (found_key
.offset
== 0)
2400 key
.offset
= found_key
.offset
- 1;
2403 if (failed
&& !retried
) {
2407 } else if (failed
&& retried
) {
2412 btrfs_free_path(path
);
2416 static int insert_balance_item(struct btrfs_root
*root
,
2417 struct btrfs_balance_control
*bctl
)
2419 struct btrfs_trans_handle
*trans
;
2420 struct btrfs_balance_item
*item
;
2421 struct btrfs_disk_balance_args disk_bargs
;
2422 struct btrfs_path
*path
;
2423 struct extent_buffer
*leaf
;
2424 struct btrfs_key key
;
2427 path
= btrfs_alloc_path();
2431 trans
= btrfs_start_transaction(root
, 0);
2432 if (IS_ERR(trans
)) {
2433 btrfs_free_path(path
);
2434 return PTR_ERR(trans
);
2437 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2438 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2441 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2446 leaf
= path
->nodes
[0];
2447 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2449 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2451 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2452 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2453 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2454 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2455 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2456 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2458 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2460 btrfs_mark_buffer_dirty(leaf
);
2462 btrfs_free_path(path
);
2463 err
= btrfs_commit_transaction(trans
, root
);
2469 static int del_balance_item(struct btrfs_root
*root
)
2471 struct btrfs_trans_handle
*trans
;
2472 struct btrfs_path
*path
;
2473 struct btrfs_key key
;
2476 path
= btrfs_alloc_path();
2480 trans
= btrfs_start_transaction(root
, 0);
2481 if (IS_ERR(trans
)) {
2482 btrfs_free_path(path
);
2483 return PTR_ERR(trans
);
2486 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2487 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2490 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2498 ret
= btrfs_del_item(trans
, root
, path
);
2500 btrfs_free_path(path
);
2501 err
= btrfs_commit_transaction(trans
, root
);
2508 * This is a heuristic used to reduce the number of chunks balanced on
2509 * resume after balance was interrupted.
2511 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2514 * Turn on soft mode for chunk types that were being converted.
2516 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2517 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2518 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2519 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2520 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2521 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2524 * Turn on usage filter if is not already used. The idea is
2525 * that chunks that we have already balanced should be
2526 * reasonably full. Don't do it for chunks that are being
2527 * converted - that will keep us from relocating unconverted
2528 * (albeit full) chunks.
2530 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2531 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2532 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2533 bctl
->data
.usage
= 90;
2535 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2536 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2537 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2538 bctl
->sys
.usage
= 90;
2540 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2541 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2542 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2543 bctl
->meta
.usage
= 90;
2548 * Should be called with both balance and volume mutexes held to
2549 * serialize other volume operations (add_dev/rm_dev/resize) with
2550 * restriper. Same goes for unset_balance_control.
2552 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2554 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2556 BUG_ON(fs_info
->balance_ctl
);
2558 spin_lock(&fs_info
->balance_lock
);
2559 fs_info
->balance_ctl
= bctl
;
2560 spin_unlock(&fs_info
->balance_lock
);
2563 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2565 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2567 BUG_ON(!fs_info
->balance_ctl
);
2569 spin_lock(&fs_info
->balance_lock
);
2570 fs_info
->balance_ctl
= NULL
;
2571 spin_unlock(&fs_info
->balance_lock
);
2577 * Balance filters. Return 1 if chunk should be filtered out
2578 * (should not be balanced).
2580 static int chunk_profiles_filter(u64 chunk_type
,
2581 struct btrfs_balance_args
*bargs
)
2583 chunk_type
= chunk_to_extended(chunk_type
) &
2584 BTRFS_EXTENDED_PROFILE_MASK
;
2586 if (bargs
->profiles
& chunk_type
)
2592 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2593 struct btrfs_balance_args
*bargs
)
2595 struct btrfs_block_group_cache
*cache
;
2596 u64 chunk_used
, user_thresh
;
2599 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2600 chunk_used
= btrfs_block_group_used(&cache
->item
);
2602 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2603 if (chunk_used
< user_thresh
)
2606 btrfs_put_block_group(cache
);
2610 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2611 struct btrfs_chunk
*chunk
,
2612 struct btrfs_balance_args
*bargs
)
2614 struct btrfs_stripe
*stripe
;
2615 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2618 for (i
= 0; i
< num_stripes
; i
++) {
2619 stripe
= btrfs_stripe_nr(chunk
, i
);
2620 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2627 /* [pstart, pend) */
2628 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2629 struct btrfs_chunk
*chunk
,
2631 struct btrfs_balance_args
*bargs
)
2633 struct btrfs_stripe
*stripe
;
2634 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2640 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2643 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2644 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2648 factor
= num_stripes
/ factor
;
2650 for (i
= 0; i
< num_stripes
; i
++) {
2651 stripe
= btrfs_stripe_nr(chunk
, i
);
2652 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2655 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2656 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2657 do_div(stripe_length
, factor
);
2659 if (stripe_offset
< bargs
->pend
&&
2660 stripe_offset
+ stripe_length
> bargs
->pstart
)
2667 /* [vstart, vend) */
2668 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2669 struct btrfs_chunk
*chunk
,
2671 struct btrfs_balance_args
*bargs
)
2673 if (chunk_offset
< bargs
->vend
&&
2674 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2675 /* at least part of the chunk is inside this vrange */
2681 static int chunk_soft_convert_filter(u64 chunk_type
,
2682 struct btrfs_balance_args
*bargs
)
2684 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2687 chunk_type
= chunk_to_extended(chunk_type
) &
2688 BTRFS_EXTENDED_PROFILE_MASK
;
2690 if (bargs
->target
== chunk_type
)
2696 static int should_balance_chunk(struct btrfs_root
*root
,
2697 struct extent_buffer
*leaf
,
2698 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2700 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2701 struct btrfs_balance_args
*bargs
= NULL
;
2702 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2705 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2706 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2710 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2711 bargs
= &bctl
->data
;
2712 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2714 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2715 bargs
= &bctl
->meta
;
2717 /* profiles filter */
2718 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2719 chunk_profiles_filter(chunk_type
, bargs
)) {
2724 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2725 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2730 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2731 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2735 /* drange filter, makes sense only with devid filter */
2736 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2737 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2742 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2743 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2747 /* soft profile changing mode */
2748 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2749 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2756 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2758 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2759 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2760 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2761 struct list_head
*devices
;
2762 struct btrfs_device
*device
;
2765 struct btrfs_chunk
*chunk
;
2766 struct btrfs_path
*path
;
2767 struct btrfs_key key
;
2768 struct btrfs_key found_key
;
2769 struct btrfs_trans_handle
*trans
;
2770 struct extent_buffer
*leaf
;
2773 int enospc_errors
= 0;
2774 bool counting
= true;
2776 /* step one make some room on all the devices */
2777 devices
= &fs_info
->fs_devices
->devices
;
2778 list_for_each_entry(device
, devices
, dev_list
) {
2779 old_size
= device
->total_bytes
;
2780 size_to_free
= div_factor(old_size
, 1);
2781 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2782 if (!device
->writeable
||
2783 device
->total_bytes
- device
->bytes_used
> size_to_free
||
2784 device
->is_tgtdev_for_dev_replace
)
2787 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2792 trans
= btrfs_start_transaction(dev_root
, 0);
2793 BUG_ON(IS_ERR(trans
));
2795 ret
= btrfs_grow_device(trans
, device
, old_size
);
2798 btrfs_end_transaction(trans
, dev_root
);
2801 /* step two, relocate all the chunks */
2802 path
= btrfs_alloc_path();
2808 /* zero out stat counters */
2809 spin_lock(&fs_info
->balance_lock
);
2810 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2811 spin_unlock(&fs_info
->balance_lock
);
2813 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2814 key
.offset
= (u64
)-1;
2815 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2818 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2819 atomic_read(&fs_info
->balance_cancel_req
)) {
2824 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2829 * this shouldn't happen, it means the last relocate
2833 BUG(); /* FIXME break ? */
2835 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2836 BTRFS_CHUNK_ITEM_KEY
);
2842 leaf
= path
->nodes
[0];
2843 slot
= path
->slots
[0];
2844 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2846 if (found_key
.objectid
!= key
.objectid
)
2849 /* chunk zero is special */
2850 if (found_key
.offset
== 0)
2853 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2856 spin_lock(&fs_info
->balance_lock
);
2857 bctl
->stat
.considered
++;
2858 spin_unlock(&fs_info
->balance_lock
);
2861 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2863 btrfs_release_path(path
);
2868 spin_lock(&fs_info
->balance_lock
);
2869 bctl
->stat
.expected
++;
2870 spin_unlock(&fs_info
->balance_lock
);
2874 ret
= btrfs_relocate_chunk(chunk_root
,
2875 chunk_root
->root_key
.objectid
,
2878 if (ret
&& ret
!= -ENOSPC
)
2880 if (ret
== -ENOSPC
) {
2883 spin_lock(&fs_info
->balance_lock
);
2884 bctl
->stat
.completed
++;
2885 spin_unlock(&fs_info
->balance_lock
);
2888 key
.offset
= found_key
.offset
- 1;
2892 btrfs_release_path(path
);
2897 btrfs_free_path(path
);
2898 if (enospc_errors
) {
2899 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2909 * alloc_profile_is_valid - see if a given profile is valid and reduced
2910 * @flags: profile to validate
2911 * @extended: if true @flags is treated as an extended profile
2913 static int alloc_profile_is_valid(u64 flags
, int extended
)
2915 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2916 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2918 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
2920 /* 1) check that all other bits are zeroed */
2924 /* 2) see if profile is reduced */
2926 return !extended
; /* "0" is valid for usual profiles */
2928 /* true if exactly one bit set */
2929 return (flags
& (flags
- 1)) == 0;
2932 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2934 /* cancel requested || normal exit path */
2935 return atomic_read(&fs_info
->balance_cancel_req
) ||
2936 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2937 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2940 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2944 unset_balance_control(fs_info
);
2945 ret
= del_balance_item(fs_info
->tree_root
);
2949 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2950 struct btrfs_ioctl_balance_args
*bargs
);
2953 * Should be called with both balance and volume mutexes held
2955 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2956 struct btrfs_ioctl_balance_args
*bargs
)
2958 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2964 if (btrfs_fs_closing(fs_info
) ||
2965 atomic_read(&fs_info
->balance_pause_req
) ||
2966 atomic_read(&fs_info
->balance_cancel_req
)) {
2971 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2972 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
2976 * In case of mixed groups both data and meta should be picked,
2977 * and identical options should be given for both of them.
2979 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
2980 if (mixed
&& (bctl
->flags
& allowed
)) {
2981 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2982 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2983 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2984 printk(KERN_ERR
"btrfs: with mixed groups data and "
2985 "metadata balance options must be the same\n");
2991 num_devices
= fs_info
->fs_devices
->num_devices
;
2992 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
2993 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
2994 BUG_ON(num_devices
< 1);
2997 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
2998 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2999 if (num_devices
== 1)
3000 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3001 else if (num_devices
< 4)
3002 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3004 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
3005 BTRFS_BLOCK_GROUP_RAID10
);
3007 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3008 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3009 (bctl
->data
.target
& ~allowed
))) {
3010 printk(KERN_ERR
"btrfs: unable to start balance with target "
3011 "data profile %llu\n",
3012 (unsigned long long)bctl
->data
.target
);
3016 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3017 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3018 (bctl
->meta
.target
& ~allowed
))) {
3019 printk(KERN_ERR
"btrfs: unable to start balance with target "
3020 "metadata profile %llu\n",
3021 (unsigned long long)bctl
->meta
.target
);
3025 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3026 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3027 (bctl
->sys
.target
& ~allowed
))) {
3028 printk(KERN_ERR
"btrfs: unable to start balance with target "
3029 "system profile %llu\n",
3030 (unsigned long long)bctl
->sys
.target
);
3035 /* allow dup'ed data chunks only in mixed mode */
3036 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3037 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3038 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
3043 /* allow to reduce meta or sys integrity only if force set */
3044 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3045 BTRFS_BLOCK_GROUP_RAID10
;
3046 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3047 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3048 !(bctl
->sys
.target
& allowed
)) ||
3049 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3050 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3051 !(bctl
->meta
.target
& allowed
))) {
3052 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3053 printk(KERN_INFO
"btrfs: force reducing metadata "
3056 printk(KERN_ERR
"btrfs: balance will reduce metadata "
3057 "integrity, use force if you want this\n");
3063 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3064 int num_tolerated_disk_barrier_failures
;
3065 u64 target
= bctl
->sys
.target
;
3067 num_tolerated_disk_barrier_failures
=
3068 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3069 if (num_tolerated_disk_barrier_failures
> 0 &&
3071 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3072 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3073 num_tolerated_disk_barrier_failures
= 0;
3074 else if (num_tolerated_disk_barrier_failures
> 1 &&
3076 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3077 num_tolerated_disk_barrier_failures
= 1;
3079 fs_info
->num_tolerated_disk_barrier_failures
=
3080 num_tolerated_disk_barrier_failures
;
3083 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3084 if (ret
&& ret
!= -EEXIST
)
3087 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3088 BUG_ON(ret
== -EEXIST
);
3089 set_balance_control(bctl
);
3091 BUG_ON(ret
!= -EEXIST
);
3092 spin_lock(&fs_info
->balance_lock
);
3093 update_balance_args(bctl
);
3094 spin_unlock(&fs_info
->balance_lock
);
3097 atomic_inc(&fs_info
->balance_running
);
3098 mutex_unlock(&fs_info
->balance_mutex
);
3100 ret
= __btrfs_balance(fs_info
);
3102 mutex_lock(&fs_info
->balance_mutex
);
3103 atomic_dec(&fs_info
->balance_running
);
3106 memset(bargs
, 0, sizeof(*bargs
));
3107 update_ioctl_balance_args(fs_info
, 0, bargs
);
3110 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3111 balance_need_close(fs_info
)) {
3112 __cancel_balance(fs_info
);
3115 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3116 fs_info
->num_tolerated_disk_barrier_failures
=
3117 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3120 wake_up(&fs_info
->balance_wait_q
);
3124 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3125 __cancel_balance(fs_info
);
3131 static int balance_kthread(void *data
)
3133 struct btrfs_fs_info
*fs_info
= data
;
3136 mutex_lock(&fs_info
->volume_mutex
);
3137 mutex_lock(&fs_info
->balance_mutex
);
3139 if (fs_info
->balance_ctl
) {
3140 printk(KERN_INFO
"btrfs: continuing balance\n");
3141 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3144 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3145 mutex_unlock(&fs_info
->balance_mutex
);
3146 mutex_unlock(&fs_info
->volume_mutex
);
3151 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3153 struct task_struct
*tsk
;
3155 spin_lock(&fs_info
->balance_lock
);
3156 if (!fs_info
->balance_ctl
) {
3157 spin_unlock(&fs_info
->balance_lock
);
3160 spin_unlock(&fs_info
->balance_lock
);
3162 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3163 printk(KERN_INFO
"btrfs: force skipping balance\n");
3167 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3168 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3170 return PTR_ERR(tsk
);
3175 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3177 struct btrfs_balance_control
*bctl
;
3178 struct btrfs_balance_item
*item
;
3179 struct btrfs_disk_balance_args disk_bargs
;
3180 struct btrfs_path
*path
;
3181 struct extent_buffer
*leaf
;
3182 struct btrfs_key key
;
3185 path
= btrfs_alloc_path();
3189 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3190 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3193 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3196 if (ret
> 0) { /* ret = -ENOENT; */
3201 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3207 leaf
= path
->nodes
[0];
3208 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3210 bctl
->fs_info
= fs_info
;
3211 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3212 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3214 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3215 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3216 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3217 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3218 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3219 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3221 mutex_lock(&fs_info
->volume_mutex
);
3222 mutex_lock(&fs_info
->balance_mutex
);
3224 set_balance_control(bctl
);
3226 mutex_unlock(&fs_info
->balance_mutex
);
3227 mutex_unlock(&fs_info
->volume_mutex
);
3229 btrfs_free_path(path
);
3233 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3237 mutex_lock(&fs_info
->balance_mutex
);
3238 if (!fs_info
->balance_ctl
) {
3239 mutex_unlock(&fs_info
->balance_mutex
);
3243 if (atomic_read(&fs_info
->balance_running
)) {
3244 atomic_inc(&fs_info
->balance_pause_req
);
3245 mutex_unlock(&fs_info
->balance_mutex
);
3247 wait_event(fs_info
->balance_wait_q
,
3248 atomic_read(&fs_info
->balance_running
) == 0);
3250 mutex_lock(&fs_info
->balance_mutex
);
3251 /* we are good with balance_ctl ripped off from under us */
3252 BUG_ON(atomic_read(&fs_info
->balance_running
));
3253 atomic_dec(&fs_info
->balance_pause_req
);
3258 mutex_unlock(&fs_info
->balance_mutex
);
3262 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3264 mutex_lock(&fs_info
->balance_mutex
);
3265 if (!fs_info
->balance_ctl
) {
3266 mutex_unlock(&fs_info
->balance_mutex
);
3270 atomic_inc(&fs_info
->balance_cancel_req
);
3272 * if we are running just wait and return, balance item is
3273 * deleted in btrfs_balance in this case
3275 if (atomic_read(&fs_info
->balance_running
)) {
3276 mutex_unlock(&fs_info
->balance_mutex
);
3277 wait_event(fs_info
->balance_wait_q
,
3278 atomic_read(&fs_info
->balance_running
) == 0);
3279 mutex_lock(&fs_info
->balance_mutex
);
3281 /* __cancel_balance needs volume_mutex */
3282 mutex_unlock(&fs_info
->balance_mutex
);
3283 mutex_lock(&fs_info
->volume_mutex
);
3284 mutex_lock(&fs_info
->balance_mutex
);
3286 if (fs_info
->balance_ctl
)
3287 __cancel_balance(fs_info
);
3289 mutex_unlock(&fs_info
->volume_mutex
);
3292 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3293 atomic_dec(&fs_info
->balance_cancel_req
);
3294 mutex_unlock(&fs_info
->balance_mutex
);
3299 * shrinking a device means finding all of the device extents past
3300 * the new size, and then following the back refs to the chunks.
3301 * The chunk relocation code actually frees the device extent
3303 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3305 struct btrfs_trans_handle
*trans
;
3306 struct btrfs_root
*root
= device
->dev_root
;
3307 struct btrfs_dev_extent
*dev_extent
= NULL
;
3308 struct btrfs_path
*path
;
3316 bool retried
= false;
3317 struct extent_buffer
*l
;
3318 struct btrfs_key key
;
3319 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3320 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3321 u64 old_size
= device
->total_bytes
;
3322 u64 diff
= device
->total_bytes
- new_size
;
3324 if (device
->is_tgtdev_for_dev_replace
)
3327 path
= btrfs_alloc_path();
3335 device
->total_bytes
= new_size
;
3336 if (device
->writeable
) {
3337 device
->fs_devices
->total_rw_bytes
-= diff
;
3338 spin_lock(&root
->fs_info
->free_chunk_lock
);
3339 root
->fs_info
->free_chunk_space
-= diff
;
3340 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3342 unlock_chunks(root
);
3345 key
.objectid
= device
->devid
;
3346 key
.offset
= (u64
)-1;
3347 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3350 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3354 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3359 btrfs_release_path(path
);
3364 slot
= path
->slots
[0];
3365 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3367 if (key
.objectid
!= device
->devid
) {
3368 btrfs_release_path(path
);
3372 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3373 length
= btrfs_dev_extent_length(l
, dev_extent
);
3375 if (key
.offset
+ length
<= new_size
) {
3376 btrfs_release_path(path
);
3380 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3381 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3382 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3383 btrfs_release_path(path
);
3385 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3387 if (ret
&& ret
!= -ENOSPC
)
3391 } while (key
.offset
-- > 0);
3393 if (failed
&& !retried
) {
3397 } else if (failed
&& retried
) {
3401 device
->total_bytes
= old_size
;
3402 if (device
->writeable
)
3403 device
->fs_devices
->total_rw_bytes
+= diff
;
3404 spin_lock(&root
->fs_info
->free_chunk_lock
);
3405 root
->fs_info
->free_chunk_space
+= diff
;
3406 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3407 unlock_chunks(root
);
3411 /* Shrinking succeeded, else we would be at "done". */
3412 trans
= btrfs_start_transaction(root
, 0);
3413 if (IS_ERR(trans
)) {
3414 ret
= PTR_ERR(trans
);
3420 device
->disk_total_bytes
= new_size
;
3421 /* Now btrfs_update_device() will change the on-disk size. */
3422 ret
= btrfs_update_device(trans
, device
);
3424 unlock_chunks(root
);
3425 btrfs_end_transaction(trans
, root
);
3428 WARN_ON(diff
> old_total
);
3429 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3430 unlock_chunks(root
);
3431 btrfs_end_transaction(trans
, root
);
3433 btrfs_free_path(path
);
3437 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3438 struct btrfs_key
*key
,
3439 struct btrfs_chunk
*chunk
, int item_size
)
3441 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3442 struct btrfs_disk_key disk_key
;
3446 array_size
= btrfs_super_sys_array_size(super_copy
);
3447 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3450 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3451 btrfs_cpu_key_to_disk(&disk_key
, key
);
3452 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3453 ptr
+= sizeof(disk_key
);
3454 memcpy(ptr
, chunk
, item_size
);
3455 item_size
+= sizeof(disk_key
);
3456 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3461 * sort the devices in descending order by max_avail, total_avail
3463 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3465 const struct btrfs_device_info
*di_a
= a
;
3466 const struct btrfs_device_info
*di_b
= b
;
3468 if (di_a
->max_avail
> di_b
->max_avail
)
3470 if (di_a
->max_avail
< di_b
->max_avail
)
3472 if (di_a
->total_avail
> di_b
->total_avail
)
3474 if (di_a
->total_avail
< di_b
->total_avail
)
3479 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3480 struct btrfs_root
*extent_root
,
3481 struct map_lookup
**map_ret
,
3482 u64
*num_bytes_out
, u64
*stripe_size_out
,
3483 u64 start
, u64 type
)
3485 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3486 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3487 struct list_head
*cur
;
3488 struct map_lookup
*map
= NULL
;
3489 struct extent_map_tree
*em_tree
;
3490 struct extent_map
*em
;
3491 struct btrfs_device_info
*devices_info
= NULL
;
3493 int num_stripes
; /* total number of stripes to allocate */
3494 int sub_stripes
; /* sub_stripes info for map */
3495 int dev_stripes
; /* stripes per dev */
3496 int devs_max
; /* max devs to use */
3497 int devs_min
; /* min devs needed */
3498 int devs_increment
; /* ndevs has to be a multiple of this */
3499 int ncopies
; /* how many copies to data has */
3501 u64 max_stripe_size
;
3509 BUG_ON(!alloc_profile_is_valid(type
, 0));
3511 if (list_empty(&fs_devices
->alloc_list
))
3518 devs_max
= 0; /* 0 == as many as possible */
3522 * define the properties of each RAID type.
3523 * FIXME: move this to a global table and use it in all RAID
3526 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3530 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3532 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3537 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3546 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3547 max_stripe_size
= 1024 * 1024 * 1024;
3548 max_chunk_size
= 10 * max_stripe_size
;
3549 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3550 /* for larger filesystems, use larger metadata chunks */
3551 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3552 max_stripe_size
= 1024 * 1024 * 1024;
3554 max_stripe_size
= 256 * 1024 * 1024;
3555 max_chunk_size
= max_stripe_size
;
3556 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3557 max_stripe_size
= 32 * 1024 * 1024;
3558 max_chunk_size
= 2 * max_stripe_size
;
3560 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3565 /* we don't want a chunk larger than 10% of writeable space */
3566 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3569 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3574 cur
= fs_devices
->alloc_list
.next
;
3577 * in the first pass through the devices list, we gather information
3578 * about the available holes on each device.
3581 while (cur
!= &fs_devices
->alloc_list
) {
3582 struct btrfs_device
*device
;
3586 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3590 if (!device
->writeable
) {
3592 "btrfs: read-only device in alloc_list\n");
3596 if (!device
->in_fs_metadata
||
3597 device
->is_tgtdev_for_dev_replace
)
3600 if (device
->total_bytes
> device
->bytes_used
)
3601 total_avail
= device
->total_bytes
- device
->bytes_used
;
3605 /* If there is no space on this device, skip it. */
3606 if (total_avail
== 0)
3609 ret
= find_free_dev_extent(device
,
3610 max_stripe_size
* dev_stripes
,
3611 &dev_offset
, &max_avail
);
3612 if (ret
&& ret
!= -ENOSPC
)
3616 max_avail
= max_stripe_size
* dev_stripes
;
3618 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3621 devices_info
[ndevs
].dev_offset
= dev_offset
;
3622 devices_info
[ndevs
].max_avail
= max_avail
;
3623 devices_info
[ndevs
].total_avail
= total_avail
;
3624 devices_info
[ndevs
].dev
= device
;
3626 WARN_ON(ndevs
> fs_devices
->rw_devices
);
3630 * now sort the devices by hole size / available space
3632 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3633 btrfs_cmp_device_info
, NULL
);
3635 /* round down to number of usable stripes */
3636 ndevs
-= ndevs
% devs_increment
;
3638 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3643 if (devs_max
&& ndevs
> devs_max
)
3646 * the primary goal is to maximize the number of stripes, so use as many
3647 * devices as possible, even if the stripes are not maximum sized.
3649 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3650 num_stripes
= ndevs
* dev_stripes
;
3652 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3653 stripe_size
= max_chunk_size
* ncopies
;
3654 do_div(stripe_size
, ndevs
);
3657 do_div(stripe_size
, dev_stripes
);
3659 /* align to BTRFS_STRIPE_LEN */
3660 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3661 stripe_size
*= BTRFS_STRIPE_LEN
;
3663 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3668 map
->num_stripes
= num_stripes
;
3670 for (i
= 0; i
< ndevs
; ++i
) {
3671 for (j
= 0; j
< dev_stripes
; ++j
) {
3672 int s
= i
* dev_stripes
+ j
;
3673 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3674 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3678 map
->sector_size
= extent_root
->sectorsize
;
3679 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3680 map
->io_align
= BTRFS_STRIPE_LEN
;
3681 map
->io_width
= BTRFS_STRIPE_LEN
;
3683 map
->sub_stripes
= sub_stripes
;
3686 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3688 *stripe_size_out
= stripe_size
;
3689 *num_bytes_out
= num_bytes
;
3691 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3693 em
= alloc_extent_map();
3698 em
->bdev
= (struct block_device
*)map
;
3700 em
->len
= num_bytes
;
3701 em
->block_start
= 0;
3702 em
->block_len
= em
->len
;
3704 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3705 write_lock(&em_tree
->lock
);
3706 ret
= add_extent_mapping(em_tree
, em
);
3707 write_unlock(&em_tree
->lock
);
3708 free_extent_map(em
);
3712 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3713 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3718 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3719 struct btrfs_device
*device
;
3722 device
= map
->stripes
[i
].dev
;
3723 dev_offset
= map
->stripes
[i
].physical
;
3725 ret
= btrfs_alloc_dev_extent(trans
, device
,
3726 info
->chunk_root
->root_key
.objectid
,
3727 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3728 start
, dev_offset
, stripe_size
);
3730 btrfs_abort_transaction(trans
, extent_root
, ret
);
3735 kfree(devices_info
);
3740 kfree(devices_info
);
3744 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3745 struct btrfs_root
*extent_root
,
3746 struct map_lookup
*map
, u64 chunk_offset
,
3747 u64 chunk_size
, u64 stripe_size
)
3750 struct btrfs_key key
;
3751 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3752 struct btrfs_device
*device
;
3753 struct btrfs_chunk
*chunk
;
3754 struct btrfs_stripe
*stripe
;
3755 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3759 chunk
= kzalloc(item_size
, GFP_NOFS
);
3764 while (index
< map
->num_stripes
) {
3765 device
= map
->stripes
[index
].dev
;
3766 device
->bytes_used
+= stripe_size
;
3767 ret
= btrfs_update_device(trans
, device
);
3773 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3774 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3776 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3779 stripe
= &chunk
->stripe
;
3780 while (index
< map
->num_stripes
) {
3781 device
= map
->stripes
[index
].dev
;
3782 dev_offset
= map
->stripes
[index
].physical
;
3784 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3785 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3786 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3791 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3792 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3793 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3794 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3795 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3796 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3797 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3798 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3799 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3801 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3802 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3803 key
.offset
= chunk_offset
;
3805 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3807 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3809 * TODO: Cleanup of inserted chunk root in case of
3812 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3822 * Chunk allocation falls into two parts. The first part does works
3823 * that make the new allocated chunk useable, but not do any operation
3824 * that modifies the chunk tree. The second part does the works that
3825 * require modifying the chunk tree. This division is important for the
3826 * bootstrap process of adding storage to a seed btrfs.
3828 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3829 struct btrfs_root
*extent_root
, u64 type
)
3834 struct map_lookup
*map
;
3835 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3838 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3843 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3844 &stripe_size
, chunk_offset
, type
);
3848 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3849 chunk_size
, stripe_size
);
3855 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3856 struct btrfs_root
*root
,
3857 struct btrfs_device
*device
)
3860 u64 sys_chunk_offset
;
3864 u64 sys_stripe_size
;
3866 struct map_lookup
*map
;
3867 struct map_lookup
*sys_map
;
3868 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3869 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3872 ret
= find_next_chunk(fs_info
->chunk_root
,
3873 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3877 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3878 fs_info
->avail_metadata_alloc_bits
;
3879 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3881 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3882 &stripe_size
, chunk_offset
, alloc_profile
);
3886 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3888 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3889 fs_info
->avail_system_alloc_bits
;
3890 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3892 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3893 &sys_chunk_size
, &sys_stripe_size
,
3894 sys_chunk_offset
, alloc_profile
);
3896 btrfs_abort_transaction(trans
, root
, ret
);
3900 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3902 btrfs_abort_transaction(trans
, root
, ret
);
3907 * Modifying chunk tree needs allocating new blocks from both
3908 * system block group and metadata block group. So we only can
3909 * do operations require modifying the chunk tree after both
3910 * block groups were created.
3912 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3913 chunk_size
, stripe_size
);
3915 btrfs_abort_transaction(trans
, root
, ret
);
3919 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3920 sys_chunk_offset
, sys_chunk_size
,
3923 btrfs_abort_transaction(trans
, root
, ret
);
3930 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3932 struct extent_map
*em
;
3933 struct map_lookup
*map
;
3934 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3938 read_lock(&map_tree
->map_tree
.lock
);
3939 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3940 read_unlock(&map_tree
->map_tree
.lock
);
3944 if (btrfs_test_opt(root
, DEGRADED
)) {
3945 free_extent_map(em
);
3949 map
= (struct map_lookup
*)em
->bdev
;
3950 for (i
= 0; i
< map
->num_stripes
; i
++) {
3951 if (!map
->stripes
[i
].dev
->writeable
) {
3956 free_extent_map(em
);
3960 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3962 extent_map_tree_init(&tree
->map_tree
);
3965 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3967 struct extent_map
*em
;
3970 write_lock(&tree
->map_tree
.lock
);
3971 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3973 remove_extent_mapping(&tree
->map_tree
, em
);
3974 write_unlock(&tree
->map_tree
.lock
);
3979 free_extent_map(em
);
3980 /* once for the tree */
3981 free_extent_map(em
);
3985 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
3987 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
3988 struct extent_map
*em
;
3989 struct map_lookup
*map
;
3990 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3993 read_lock(&em_tree
->lock
);
3994 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3995 read_unlock(&em_tree
->lock
);
3998 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3999 map
= (struct map_lookup
*)em
->bdev
;
4000 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4001 ret
= map
->num_stripes
;
4002 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4003 ret
= map
->sub_stripes
;
4006 free_extent_map(em
);
4010 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
4014 if (map
->stripes
[optimal
].dev
->bdev
)
4016 for (i
= first
; i
< first
+ num
; i
++) {
4017 if (map
->stripes
[i
].dev
->bdev
)
4020 /* we couldn't find one that doesn't fail. Just return something
4021 * and the io error handling code will clean up eventually
4026 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4027 u64 logical
, u64
*length
,
4028 struct btrfs_bio
**bbio_ret
,
4031 struct extent_map
*em
;
4032 struct map_lookup
*map
;
4033 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4034 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4037 u64 stripe_end_offset
;
4046 struct btrfs_bio
*bbio
= NULL
;
4048 read_lock(&em_tree
->lock
);
4049 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4050 read_unlock(&em_tree
->lock
);
4053 printk(KERN_CRIT
"btrfs: unable to find logical %llu len %llu\n",
4054 (unsigned long long)logical
,
4055 (unsigned long long)*length
);
4059 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4060 map
= (struct map_lookup
*)em
->bdev
;
4061 offset
= logical
- em
->start
;
4063 if (mirror_num
> map
->num_stripes
)
4068 * stripe_nr counts the total number of stripes we have to stride
4069 * to get to this block
4071 do_div(stripe_nr
, map
->stripe_len
);
4073 stripe_offset
= stripe_nr
* map
->stripe_len
;
4074 BUG_ON(offset
< stripe_offset
);
4076 /* stripe_offset is the offset of this block in its stripe*/
4077 stripe_offset
= offset
- stripe_offset
;
4079 if (rw
& REQ_DISCARD
)
4080 *length
= min_t(u64
, em
->len
- offset
, *length
);
4081 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4082 /* we limit the length of each bio to what fits in a stripe */
4083 *length
= min_t(u64
, em
->len
- offset
,
4084 map
->stripe_len
- stripe_offset
);
4086 *length
= em
->len
- offset
;
4094 stripe_nr_orig
= stripe_nr
;
4095 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
4096 (~(map
->stripe_len
- 1));
4097 do_div(stripe_nr_end
, map
->stripe_len
);
4098 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
4100 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4101 if (rw
& REQ_DISCARD
)
4102 num_stripes
= min_t(u64
, map
->num_stripes
,
4103 stripe_nr_end
- stripe_nr_orig
);
4104 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4105 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
4106 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
4107 num_stripes
= map
->num_stripes
;
4108 else if (mirror_num
)
4109 stripe_index
= mirror_num
- 1;
4111 stripe_index
= find_live_mirror(map
, 0,
4113 current
->pid
% map
->num_stripes
);
4114 mirror_num
= stripe_index
+ 1;
4117 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
4118 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
4119 num_stripes
= map
->num_stripes
;
4120 } else if (mirror_num
) {
4121 stripe_index
= mirror_num
- 1;
4126 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4127 int factor
= map
->num_stripes
/ map
->sub_stripes
;
4129 stripe_index
= do_div(stripe_nr
, factor
);
4130 stripe_index
*= map
->sub_stripes
;
4133 num_stripes
= map
->sub_stripes
;
4134 else if (rw
& REQ_DISCARD
)
4135 num_stripes
= min_t(u64
, map
->sub_stripes
*
4136 (stripe_nr_end
- stripe_nr_orig
),
4138 else if (mirror_num
)
4139 stripe_index
+= mirror_num
- 1;
4141 int old_stripe_index
= stripe_index
;
4142 stripe_index
= find_live_mirror(map
, stripe_index
,
4143 map
->sub_stripes
, stripe_index
+
4144 current
->pid
% map
->sub_stripes
);
4145 mirror_num
= stripe_index
- old_stripe_index
+ 1;
4149 * after this do_div call, stripe_nr is the number of stripes
4150 * on this device we have to walk to find the data, and
4151 * stripe_index is the number of our device in the stripe array
4153 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4154 mirror_num
= stripe_index
+ 1;
4156 BUG_ON(stripe_index
>= map
->num_stripes
);
4158 bbio
= kzalloc(btrfs_bio_size(num_stripes
), GFP_NOFS
);
4163 atomic_set(&bbio
->error
, 0);
4165 if (rw
& REQ_DISCARD
) {
4167 int sub_stripes
= 0;
4168 u64 stripes_per_dev
= 0;
4169 u32 remaining_stripes
= 0;
4170 u32 last_stripe
= 0;
4173 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
4174 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4177 sub_stripes
= map
->sub_stripes
;
4179 factor
= map
->num_stripes
/ sub_stripes
;
4180 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
4183 &remaining_stripes
);
4184 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
4185 last_stripe
*= sub_stripes
;
4188 for (i
= 0; i
< num_stripes
; i
++) {
4189 bbio
->stripes
[i
].physical
=
4190 map
->stripes
[stripe_index
].physical
+
4191 stripe_offset
+ stripe_nr
* map
->stripe_len
;
4192 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
4194 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
4195 BTRFS_BLOCK_GROUP_RAID10
)) {
4196 bbio
->stripes
[i
].length
= stripes_per_dev
*
4199 if (i
/ sub_stripes
< remaining_stripes
)
4200 bbio
->stripes
[i
].length
+=
4204 * Special for the first stripe and
4207 * |-------|...|-------|
4211 if (i
< sub_stripes
)
4212 bbio
->stripes
[i
].length
-=
4215 if (stripe_index
>= last_stripe
&&
4216 stripe_index
<= (last_stripe
+
4218 bbio
->stripes
[i
].length
-=
4221 if (i
== sub_stripes
- 1)
4224 bbio
->stripes
[i
].length
= *length
;
4227 if (stripe_index
== map
->num_stripes
) {
4228 /* This could only happen for RAID0/10 */
4234 for (i
= 0; i
< num_stripes
; i
++) {
4235 bbio
->stripes
[i
].physical
=
4236 map
->stripes
[stripe_index
].physical
+
4238 stripe_nr
* map
->stripe_len
;
4239 bbio
->stripes
[i
].dev
=
4240 map
->stripes
[stripe_index
].dev
;
4245 if (rw
& REQ_WRITE
) {
4246 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4247 BTRFS_BLOCK_GROUP_RAID10
|
4248 BTRFS_BLOCK_GROUP_DUP
)) {
4254 bbio
->num_stripes
= num_stripes
;
4255 bbio
->max_errors
= max_errors
;
4256 bbio
->mirror_num
= mirror_num
;
4258 free_extent_map(em
);
4262 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4263 u64 logical
, u64
*length
,
4264 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4266 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
4270 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4271 u64 chunk_start
, u64 physical
, u64 devid
,
4272 u64
**logical
, int *naddrs
, int *stripe_len
)
4274 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4275 struct extent_map
*em
;
4276 struct map_lookup
*map
;
4283 read_lock(&em_tree
->lock
);
4284 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4285 read_unlock(&em_tree
->lock
);
4287 BUG_ON(!em
|| em
->start
!= chunk_start
);
4288 map
= (struct map_lookup
*)em
->bdev
;
4291 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4292 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4293 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4294 do_div(length
, map
->num_stripes
);
4296 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4297 BUG_ON(!buf
); /* -ENOMEM */
4299 for (i
= 0; i
< map
->num_stripes
; i
++) {
4300 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4302 if (map
->stripes
[i
].physical
> physical
||
4303 map
->stripes
[i
].physical
+ length
<= physical
)
4306 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4307 do_div(stripe_nr
, map
->stripe_len
);
4309 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4310 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4311 do_div(stripe_nr
, map
->sub_stripes
);
4312 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4313 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4315 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
4316 WARN_ON(nr
>= map
->num_stripes
);
4317 for (j
= 0; j
< nr
; j
++) {
4318 if (buf
[j
] == bytenr
)
4322 WARN_ON(nr
>= map
->num_stripes
);
4329 *stripe_len
= map
->stripe_len
;
4331 free_extent_map(em
);
4335 static void *merge_stripe_index_into_bio_private(void *bi_private
,
4336 unsigned int stripe_index
)
4339 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4341 * The alternative solution (instead of stealing bits from the
4342 * pointer) would be to allocate an intermediate structure
4343 * that contains the old private pointer plus the stripe_index.
4345 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
4346 BUG_ON(stripe_index
> 3);
4347 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
4350 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
4352 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
4355 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
4357 return (unsigned int)((uintptr_t)bi_private
) & 3;
4360 static void btrfs_end_bio(struct bio
*bio
, int err
)
4362 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
4363 int is_orig_bio
= 0;
4366 atomic_inc(&bbio
->error
);
4367 if (err
== -EIO
|| err
== -EREMOTEIO
) {
4368 unsigned int stripe_index
=
4369 extract_stripe_index_from_bio_private(
4371 struct btrfs_device
*dev
;
4373 BUG_ON(stripe_index
>= bbio
->num_stripes
);
4374 dev
= bbio
->stripes
[stripe_index
].dev
;
4376 if (bio
->bi_rw
& WRITE
)
4377 btrfs_dev_stat_inc(dev
,
4378 BTRFS_DEV_STAT_WRITE_ERRS
);
4380 btrfs_dev_stat_inc(dev
,
4381 BTRFS_DEV_STAT_READ_ERRS
);
4382 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
4383 btrfs_dev_stat_inc(dev
,
4384 BTRFS_DEV_STAT_FLUSH_ERRS
);
4385 btrfs_dev_stat_print_on_error(dev
);
4390 if (bio
== bbio
->orig_bio
)
4393 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4396 bio
= bbio
->orig_bio
;
4398 bio
->bi_private
= bbio
->private;
4399 bio
->bi_end_io
= bbio
->end_io
;
4400 bio
->bi_bdev
= (struct block_device
*)
4401 (unsigned long)bbio
->mirror_num
;
4402 /* only send an error to the higher layers if it is
4403 * beyond the tolerance of the multi-bio
4405 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4409 * this bio is actually up to date, we didn't
4410 * go over the max number of errors
4412 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4417 bio_endio(bio
, err
);
4418 } else if (!is_orig_bio
) {
4423 struct async_sched
{
4426 struct btrfs_fs_info
*info
;
4427 struct btrfs_work work
;
4431 * see run_scheduled_bios for a description of why bios are collected for
4434 * This will add one bio to the pending list for a device and make sure
4435 * the work struct is scheduled.
4437 static noinline
void schedule_bio(struct btrfs_root
*root
,
4438 struct btrfs_device
*device
,
4439 int rw
, struct bio
*bio
)
4441 int should_queue
= 1;
4442 struct btrfs_pending_bios
*pending_bios
;
4444 /* don't bother with additional async steps for reads, right now */
4445 if (!(rw
& REQ_WRITE
)) {
4447 btrfsic_submit_bio(rw
, bio
);
4453 * nr_async_bios allows us to reliably return congestion to the
4454 * higher layers. Otherwise, the async bio makes it appear we have
4455 * made progress against dirty pages when we've really just put it
4456 * on a queue for later
4458 atomic_inc(&root
->fs_info
->nr_async_bios
);
4459 WARN_ON(bio
->bi_next
);
4460 bio
->bi_next
= NULL
;
4463 spin_lock(&device
->io_lock
);
4464 if (bio
->bi_rw
& REQ_SYNC
)
4465 pending_bios
= &device
->pending_sync_bios
;
4467 pending_bios
= &device
->pending_bios
;
4469 if (pending_bios
->tail
)
4470 pending_bios
->tail
->bi_next
= bio
;
4472 pending_bios
->tail
= bio
;
4473 if (!pending_bios
->head
)
4474 pending_bios
->head
= bio
;
4475 if (device
->running_pending
)
4478 spin_unlock(&device
->io_lock
);
4481 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4485 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
4488 struct bio_vec
*prev
;
4489 struct request_queue
*q
= bdev_get_queue(bdev
);
4490 unsigned short max_sectors
= queue_max_sectors(q
);
4491 struct bvec_merge_data bvm
= {
4493 .bi_sector
= sector
,
4494 .bi_rw
= bio
->bi_rw
,
4497 if (bio
->bi_vcnt
== 0) {
4502 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
4503 if ((bio
->bi_size
>> 9) > max_sectors
)
4506 if (!q
->merge_bvec_fn
)
4509 bvm
.bi_size
= bio
->bi_size
- prev
->bv_len
;
4510 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
4515 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4516 struct bio
*bio
, u64 physical
, int dev_nr
,
4519 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
4521 bio
->bi_private
= bbio
;
4522 bio
->bi_private
= merge_stripe_index_into_bio_private(
4523 bio
->bi_private
, (unsigned int)dev_nr
);
4524 bio
->bi_end_io
= btrfs_end_bio
;
4525 bio
->bi_sector
= physical
>> 9;
4528 struct rcu_string
*name
;
4531 name
= rcu_dereference(dev
->name
);
4532 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
4533 "(%s id %llu), size=%u\n", rw
,
4534 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4535 name
->str
, dev
->devid
, bio
->bi_size
);
4539 bio
->bi_bdev
= dev
->bdev
;
4541 schedule_bio(root
, dev
, rw
, bio
);
4543 btrfsic_submit_bio(rw
, bio
);
4546 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4547 struct bio
*first_bio
, struct btrfs_device
*dev
,
4548 int dev_nr
, int rw
, int async
)
4550 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
4552 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
4553 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
4556 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
4560 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
4561 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
4562 bvec
->bv_offset
) < bvec
->bv_len
) {
4563 u64 len
= bio
->bi_size
;
4565 atomic_inc(&bbio
->stripes_pending
);
4566 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
4574 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
4578 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
4580 atomic_inc(&bbio
->error
);
4581 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4582 bio
->bi_private
= bbio
->private;
4583 bio
->bi_end_io
= bbio
->end_io
;
4584 bio
->bi_bdev
= (struct block_device
*)
4585 (unsigned long)bbio
->mirror_num
;
4586 bio
->bi_sector
= logical
>> 9;
4588 bio_endio(bio
, -EIO
);
4592 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4593 int mirror_num
, int async_submit
)
4595 struct btrfs_device
*dev
;
4596 struct bio
*first_bio
= bio
;
4597 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4603 struct btrfs_bio
*bbio
= NULL
;
4605 length
= bio
->bi_size
;
4606 map_length
= length
;
4608 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
4613 total_devs
= bbio
->num_stripes
;
4614 if (map_length
< length
) {
4615 printk(KERN_CRIT
"btrfs: mapping failed logical %llu bio len %llu "
4616 "len %llu\n", (unsigned long long)logical
,
4617 (unsigned long long)length
,
4618 (unsigned long long)map_length
);
4622 bbio
->orig_bio
= first_bio
;
4623 bbio
->private = first_bio
->bi_private
;
4624 bbio
->end_io
= first_bio
->bi_end_io
;
4625 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4627 while (dev_nr
< total_devs
) {
4628 dev
= bbio
->stripes
[dev_nr
].dev
;
4629 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
4630 bbio_error(bbio
, first_bio
, logical
);
4636 * Check and see if we're ok with this bio based on it's size
4637 * and offset with the given device.
4639 if (!bio_size_ok(dev
->bdev
, first_bio
,
4640 bbio
->stripes
[dev_nr
].physical
>> 9)) {
4641 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
4642 dev_nr
, rw
, async_submit
);
4648 if (dev_nr
< total_devs
- 1) {
4649 bio
= bio_clone(first_bio
, GFP_NOFS
);
4650 BUG_ON(!bio
); /* -ENOMEM */
4655 submit_stripe_bio(root
, bbio
, bio
,
4656 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
4663 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
4666 struct btrfs_device
*device
;
4667 struct btrfs_fs_devices
*cur_devices
;
4669 cur_devices
= fs_info
->fs_devices
;
4670 while (cur_devices
) {
4672 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4673 device
= __find_device(&cur_devices
->devices
,
4678 cur_devices
= cur_devices
->seed
;
4683 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4684 u64 devid
, u8
*dev_uuid
)
4686 struct btrfs_device
*device
;
4687 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4689 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4692 list_add(&device
->dev_list
,
4693 &fs_devices
->devices
);
4694 device
->dev_root
= root
->fs_info
->dev_root
;
4695 device
->devid
= devid
;
4696 device
->work
.func
= pending_bios_fn
;
4697 device
->fs_devices
= fs_devices
;
4698 device
->missing
= 1;
4699 fs_devices
->num_devices
++;
4700 fs_devices
->missing_devices
++;
4701 spin_lock_init(&device
->io_lock
);
4702 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4703 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4707 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4708 struct extent_buffer
*leaf
,
4709 struct btrfs_chunk
*chunk
)
4711 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4712 struct map_lookup
*map
;
4713 struct extent_map
*em
;
4717 u8 uuid
[BTRFS_UUID_SIZE
];
4722 logical
= key
->offset
;
4723 length
= btrfs_chunk_length(leaf
, chunk
);
4725 read_lock(&map_tree
->map_tree
.lock
);
4726 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4727 read_unlock(&map_tree
->map_tree
.lock
);
4729 /* already mapped? */
4730 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4731 free_extent_map(em
);
4734 free_extent_map(em
);
4737 em
= alloc_extent_map();
4740 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4741 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4743 free_extent_map(em
);
4747 em
->bdev
= (struct block_device
*)map
;
4748 em
->start
= logical
;
4750 em
->block_start
= 0;
4751 em
->block_len
= em
->len
;
4753 map
->num_stripes
= num_stripes
;
4754 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4755 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4756 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4757 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4758 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4759 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4760 for (i
= 0; i
< num_stripes
; i
++) {
4761 map
->stripes
[i
].physical
=
4762 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4763 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4764 read_extent_buffer(leaf
, uuid
, (unsigned long)
4765 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4767 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
4769 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4771 free_extent_map(em
);
4774 if (!map
->stripes
[i
].dev
) {
4775 map
->stripes
[i
].dev
=
4776 add_missing_dev(root
, devid
, uuid
);
4777 if (!map
->stripes
[i
].dev
) {
4779 free_extent_map(em
);
4783 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4786 write_lock(&map_tree
->map_tree
.lock
);
4787 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4788 write_unlock(&map_tree
->map_tree
.lock
);
4789 BUG_ON(ret
); /* Tree corruption */
4790 free_extent_map(em
);
4795 static void fill_device_from_item(struct extent_buffer
*leaf
,
4796 struct btrfs_dev_item
*dev_item
,
4797 struct btrfs_device
*device
)
4801 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4802 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4803 device
->total_bytes
= device
->disk_total_bytes
;
4804 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4805 device
->type
= btrfs_device_type(leaf
, dev_item
);
4806 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4807 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4808 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4809 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
4810 device
->is_tgtdev_for_dev_replace
= 0;
4812 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4813 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4816 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4818 struct btrfs_fs_devices
*fs_devices
;
4821 BUG_ON(!mutex_is_locked(&uuid_mutex
));
4823 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4824 while (fs_devices
) {
4825 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4829 fs_devices
= fs_devices
->seed
;
4832 fs_devices
= find_fsid(fsid
);
4838 fs_devices
= clone_fs_devices(fs_devices
);
4839 if (IS_ERR(fs_devices
)) {
4840 ret
= PTR_ERR(fs_devices
);
4844 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4845 root
->fs_info
->bdev_holder
);
4847 free_fs_devices(fs_devices
);
4851 if (!fs_devices
->seeding
) {
4852 __btrfs_close_devices(fs_devices
);
4853 free_fs_devices(fs_devices
);
4858 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4859 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4864 static int read_one_dev(struct btrfs_root
*root
,
4865 struct extent_buffer
*leaf
,
4866 struct btrfs_dev_item
*dev_item
)
4868 struct btrfs_device
*device
;
4871 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4872 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4874 devid
= btrfs_device_id(leaf
, dev_item
);
4875 read_extent_buffer(leaf
, dev_uuid
,
4876 (unsigned long)btrfs_device_uuid(dev_item
),
4878 read_extent_buffer(leaf
, fs_uuid
,
4879 (unsigned long)btrfs_device_fsid(dev_item
),
4882 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4883 ret
= open_seed_devices(root
, fs_uuid
);
4884 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4888 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
4889 if (!device
|| !device
->bdev
) {
4890 if (!btrfs_test_opt(root
, DEGRADED
))
4894 printk(KERN_WARNING
"warning devid %llu missing\n",
4895 (unsigned long long)devid
);
4896 device
= add_missing_dev(root
, devid
, dev_uuid
);
4899 } else if (!device
->missing
) {
4901 * this happens when a device that was properly setup
4902 * in the device info lists suddenly goes bad.
4903 * device->bdev is NULL, and so we have to set
4904 * device->missing to one here
4906 root
->fs_info
->fs_devices
->missing_devices
++;
4907 device
->missing
= 1;
4911 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4912 BUG_ON(device
->writeable
);
4913 if (device
->generation
!=
4914 btrfs_device_generation(leaf
, dev_item
))
4918 fill_device_from_item(leaf
, dev_item
, device
);
4919 device
->dev_root
= root
->fs_info
->dev_root
;
4920 device
->in_fs_metadata
= 1;
4921 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
4922 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4923 spin_lock(&root
->fs_info
->free_chunk_lock
);
4924 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4926 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4932 int btrfs_read_sys_array(struct btrfs_root
*root
)
4934 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4935 struct extent_buffer
*sb
;
4936 struct btrfs_disk_key
*disk_key
;
4937 struct btrfs_chunk
*chunk
;
4939 unsigned long sb_ptr
;
4945 struct btrfs_key key
;
4947 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4948 BTRFS_SUPER_INFO_SIZE
);
4951 btrfs_set_buffer_uptodate(sb
);
4952 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4954 * The sb extent buffer is artifical and just used to read the system array.
4955 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4956 * pages up-to-date when the page is larger: extent does not cover the
4957 * whole page and consequently check_page_uptodate does not find all
4958 * the page's extents up-to-date (the hole beyond sb),
4959 * write_extent_buffer then triggers a WARN_ON.
4961 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4962 * but sb spans only this function. Add an explicit SetPageUptodate call
4963 * to silence the warning eg. on PowerPC 64.
4965 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
4966 SetPageUptodate(sb
->pages
[0]);
4968 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4969 array_size
= btrfs_super_sys_array_size(super_copy
);
4971 ptr
= super_copy
->sys_chunk_array
;
4972 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4975 while (cur
< array_size
) {
4976 disk_key
= (struct btrfs_disk_key
*)ptr
;
4977 btrfs_disk_key_to_cpu(&key
, disk_key
);
4979 len
= sizeof(*disk_key
); ptr
+= len
;
4983 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4984 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4985 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4988 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4989 len
= btrfs_chunk_item_size(num_stripes
);
4998 free_extent_buffer(sb
);
5002 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
5004 struct btrfs_path
*path
;
5005 struct extent_buffer
*leaf
;
5006 struct btrfs_key key
;
5007 struct btrfs_key found_key
;
5011 root
= root
->fs_info
->chunk_root
;
5013 path
= btrfs_alloc_path();
5017 mutex_lock(&uuid_mutex
);
5020 /* first we search for all of the device items, and then we
5021 * read in all of the chunk items. This way we can create chunk
5022 * mappings that reference all of the devices that are afound
5024 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
5028 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5032 leaf
= path
->nodes
[0];
5033 slot
= path
->slots
[0];
5034 if (slot
>= btrfs_header_nritems(leaf
)) {
5035 ret
= btrfs_next_leaf(root
, path
);
5042 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5043 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5044 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
5046 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
5047 struct btrfs_dev_item
*dev_item
;
5048 dev_item
= btrfs_item_ptr(leaf
, slot
,
5049 struct btrfs_dev_item
);
5050 ret
= read_one_dev(root
, leaf
, dev_item
);
5054 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5055 struct btrfs_chunk
*chunk
;
5056 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
5057 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
5063 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5065 btrfs_release_path(path
);
5070 unlock_chunks(root
);
5071 mutex_unlock(&uuid_mutex
);
5073 btrfs_free_path(path
);
5077 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
5081 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5082 btrfs_dev_stat_reset(dev
, i
);
5085 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
5087 struct btrfs_key key
;
5088 struct btrfs_key found_key
;
5089 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5090 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5091 struct extent_buffer
*eb
;
5094 struct btrfs_device
*device
;
5095 struct btrfs_path
*path
= NULL
;
5098 path
= btrfs_alloc_path();
5104 mutex_lock(&fs_devices
->device_list_mutex
);
5105 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5107 struct btrfs_dev_stats_item
*ptr
;
5110 key
.type
= BTRFS_DEV_STATS_KEY
;
5111 key
.offset
= device
->devid
;
5112 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
5114 __btrfs_reset_dev_stats(device
);
5115 device
->dev_stats_valid
= 1;
5116 btrfs_release_path(path
);
5119 slot
= path
->slots
[0];
5120 eb
= path
->nodes
[0];
5121 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5122 item_size
= btrfs_item_size_nr(eb
, slot
);
5124 ptr
= btrfs_item_ptr(eb
, slot
,
5125 struct btrfs_dev_stats_item
);
5127 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5128 if (item_size
>= (1 + i
) * sizeof(__le64
))
5129 btrfs_dev_stat_set(device
, i
,
5130 btrfs_dev_stats_value(eb
, ptr
, i
));
5132 btrfs_dev_stat_reset(device
, i
);
5135 device
->dev_stats_valid
= 1;
5136 btrfs_dev_stat_print_on_load(device
);
5137 btrfs_release_path(path
);
5139 mutex_unlock(&fs_devices
->device_list_mutex
);
5142 btrfs_free_path(path
);
5143 return ret
< 0 ? ret
: 0;
5146 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
5147 struct btrfs_root
*dev_root
,
5148 struct btrfs_device
*device
)
5150 struct btrfs_path
*path
;
5151 struct btrfs_key key
;
5152 struct extent_buffer
*eb
;
5153 struct btrfs_dev_stats_item
*ptr
;
5158 key
.type
= BTRFS_DEV_STATS_KEY
;
5159 key
.offset
= device
->devid
;
5161 path
= btrfs_alloc_path();
5163 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
5165 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
5166 ret
, rcu_str_deref(device
->name
));
5171 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
5172 /* need to delete old one and insert a new one */
5173 ret
= btrfs_del_item(trans
, dev_root
, path
);
5175 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
5176 rcu_str_deref(device
->name
), ret
);
5183 /* need to insert a new item */
5184 btrfs_release_path(path
);
5185 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
5186 &key
, sizeof(*ptr
));
5188 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
5189 rcu_str_deref(device
->name
), ret
);
5194 eb
= path
->nodes
[0];
5195 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
5196 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5197 btrfs_set_dev_stats_value(eb
, ptr
, i
,
5198 btrfs_dev_stat_read(device
, i
));
5199 btrfs_mark_buffer_dirty(eb
);
5202 btrfs_free_path(path
);
5207 * called from commit_transaction. Writes all changed device stats to disk.
5209 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
5210 struct btrfs_fs_info
*fs_info
)
5212 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5213 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5214 struct btrfs_device
*device
;
5217 mutex_lock(&fs_devices
->device_list_mutex
);
5218 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5219 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
5222 ret
= update_dev_stat_item(trans
, dev_root
, device
);
5224 device
->dev_stats_dirty
= 0;
5226 mutex_unlock(&fs_devices
->device_list_mutex
);
5231 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
5233 btrfs_dev_stat_inc(dev
, index
);
5234 btrfs_dev_stat_print_on_error(dev
);
5237 void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
5239 if (!dev
->dev_stats_valid
)
5241 printk_ratelimited_in_rcu(KERN_ERR
5242 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5243 rcu_str_deref(dev
->name
),
5244 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5245 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5246 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5247 btrfs_dev_stat_read(dev
,
5248 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5249 btrfs_dev_stat_read(dev
,
5250 BTRFS_DEV_STAT_GENERATION_ERRS
));
5253 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
5257 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5258 if (btrfs_dev_stat_read(dev
, i
) != 0)
5260 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
5261 return; /* all values == 0, suppress message */
5263 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5264 rcu_str_deref(dev
->name
),
5265 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5266 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5267 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5268 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5269 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
5272 int btrfs_get_dev_stats(struct btrfs_root
*root
,
5273 struct btrfs_ioctl_get_dev_stats
*stats
)
5275 struct btrfs_device
*dev
;
5276 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5279 mutex_lock(&fs_devices
->device_list_mutex
);
5280 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
5281 mutex_unlock(&fs_devices
->device_list_mutex
);
5285 "btrfs: get dev_stats failed, device not found\n");
5287 } else if (!dev
->dev_stats_valid
) {
5289 "btrfs: get dev_stats failed, not yet valid\n");
5291 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
5292 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5293 if (stats
->nr_items
> i
)
5295 btrfs_dev_stat_read_and_reset(dev
, i
);
5297 btrfs_dev_stat_reset(dev
, i
);
5300 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5301 if (stats
->nr_items
> i
)
5302 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
5304 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
5305 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
5309 int btrfs_scratch_superblock(struct btrfs_device
*device
)
5311 struct buffer_head
*bh
;
5312 struct btrfs_super_block
*disk_super
;
5314 bh
= btrfs_read_dev_super(device
->bdev
);
5317 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
5319 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
5320 set_buffer_dirty(bh
);
5321 sync_dirty_buffer(bh
);