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>
28 #include <linux/raid/pq.h>
29 #include <linux/semaphore.h>
30 #include <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
45 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
46 struct btrfs_root
*root
,
47 struct btrfs_device
*device
);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
49 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
50 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
51 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
53 static DEFINE_MUTEX(uuid_mutex
);
54 static LIST_HEAD(fs_uuids
);
56 static void lock_chunks(struct btrfs_root
*root
)
58 mutex_lock(&root
->fs_info
->chunk_mutex
);
61 static void unlock_chunks(struct btrfs_root
*root
)
63 mutex_unlock(&root
->fs_info
->chunk_mutex
);
66 static struct btrfs_fs_devices
*__alloc_fs_devices(void)
68 struct btrfs_fs_devices
*fs_devs
;
70 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_NOFS
);
72 return ERR_PTR(-ENOMEM
);
74 mutex_init(&fs_devs
->device_list_mutex
);
76 INIT_LIST_HEAD(&fs_devs
->devices
);
77 INIT_LIST_HEAD(&fs_devs
->resized_devices
);
78 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
79 INIT_LIST_HEAD(&fs_devs
->list
);
85 * alloc_fs_devices - allocate struct btrfs_fs_devices
86 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
89 * Return: a pointer to a new &struct btrfs_fs_devices on success;
90 * ERR_PTR() on error. Returned struct is not linked onto any lists and
91 * can be destroyed with kfree() right away.
93 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
)
95 struct btrfs_fs_devices
*fs_devs
;
97 fs_devs
= __alloc_fs_devices();
102 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
104 generate_random_uuid(fs_devs
->fsid
);
109 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
111 struct btrfs_device
*device
;
112 WARN_ON(fs_devices
->opened
);
113 while (!list_empty(&fs_devices
->devices
)) {
114 device
= list_entry(fs_devices
->devices
.next
,
115 struct btrfs_device
, dev_list
);
116 list_del(&device
->dev_list
);
117 rcu_string_free(device
->name
);
123 static void btrfs_kobject_uevent(struct block_device
*bdev
,
124 enum kobject_action action
)
128 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
130 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
132 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
133 &disk_to_dev(bdev
->bd_disk
)->kobj
);
136 void btrfs_cleanup_fs_uuids(void)
138 struct btrfs_fs_devices
*fs_devices
;
140 while (!list_empty(&fs_uuids
)) {
141 fs_devices
= list_entry(fs_uuids
.next
,
142 struct btrfs_fs_devices
, list
);
143 list_del(&fs_devices
->list
);
144 free_fs_devices(fs_devices
);
148 static struct btrfs_device
*__alloc_device(void)
150 struct btrfs_device
*dev
;
152 dev
= kzalloc(sizeof(*dev
), GFP_NOFS
);
154 return ERR_PTR(-ENOMEM
);
156 INIT_LIST_HEAD(&dev
->dev_list
);
157 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
158 INIT_LIST_HEAD(&dev
->resized_list
);
160 spin_lock_init(&dev
->io_lock
);
162 spin_lock_init(&dev
->reada_lock
);
163 atomic_set(&dev
->reada_in_flight
, 0);
164 atomic_set(&dev
->dev_stats_ccnt
, 0);
165 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
166 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
171 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
174 struct btrfs_device
*dev
;
176 list_for_each_entry(dev
, head
, dev_list
) {
177 if (dev
->devid
== devid
&&
178 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
185 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
187 struct btrfs_fs_devices
*fs_devices
;
189 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
190 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
197 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
198 int flush
, struct block_device
**bdev
,
199 struct buffer_head
**bh
)
203 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
206 ret
= PTR_ERR(*bdev
);
207 printk(KERN_INFO
"BTRFS: open %s failed\n", device_path
);
212 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
213 ret
= set_blocksize(*bdev
, 4096);
215 blkdev_put(*bdev
, flags
);
218 invalidate_bdev(*bdev
);
219 *bh
= btrfs_read_dev_super(*bdev
);
222 blkdev_put(*bdev
, flags
);
234 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
235 struct bio
*head
, struct bio
*tail
)
238 struct bio
*old_head
;
240 old_head
= pending_bios
->head
;
241 pending_bios
->head
= head
;
242 if (pending_bios
->tail
)
243 tail
->bi_next
= old_head
;
245 pending_bios
->tail
= tail
;
249 * we try to collect pending bios for a device so we don't get a large
250 * number of procs sending bios down to the same device. This greatly
251 * improves the schedulers ability to collect and merge the bios.
253 * But, it also turns into a long list of bios to process and that is sure
254 * to eventually make the worker thread block. The solution here is to
255 * make some progress and then put this work struct back at the end of
256 * the list if the block device is congested. This way, multiple devices
257 * can make progress from a single worker thread.
259 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
262 struct backing_dev_info
*bdi
;
263 struct btrfs_fs_info
*fs_info
;
264 struct btrfs_pending_bios
*pending_bios
;
268 unsigned long num_run
;
269 unsigned long batch_run
= 0;
271 unsigned long last_waited
= 0;
273 int sync_pending
= 0;
274 struct blk_plug plug
;
277 * this function runs all the bios we've collected for
278 * a particular device. We don't want to wander off to
279 * another device without first sending all of these down.
280 * So, setup a plug here and finish it off before we return
282 blk_start_plug(&plug
);
284 bdi
= blk_get_backing_dev_info(device
->bdev
);
285 fs_info
= device
->dev_root
->fs_info
;
286 limit
= btrfs_async_submit_limit(fs_info
);
287 limit
= limit
* 2 / 3;
290 spin_lock(&device
->io_lock
);
295 /* take all the bios off the list at once and process them
296 * later on (without the lock held). But, remember the
297 * tail and other pointers so the bios can be properly reinserted
298 * into the list if we hit congestion
300 if (!force_reg
&& device
->pending_sync_bios
.head
) {
301 pending_bios
= &device
->pending_sync_bios
;
304 pending_bios
= &device
->pending_bios
;
308 pending
= pending_bios
->head
;
309 tail
= pending_bios
->tail
;
310 WARN_ON(pending
&& !tail
);
313 * if pending was null this time around, no bios need processing
314 * at all and we can stop. Otherwise it'll loop back up again
315 * and do an additional check so no bios are missed.
317 * device->running_pending is used to synchronize with the
320 if (device
->pending_sync_bios
.head
== NULL
&&
321 device
->pending_bios
.head
== NULL
) {
323 device
->running_pending
= 0;
326 device
->running_pending
= 1;
329 pending_bios
->head
= NULL
;
330 pending_bios
->tail
= NULL
;
332 spin_unlock(&device
->io_lock
);
337 /* we want to work on both lists, but do more bios on the
338 * sync list than the regular list
341 pending_bios
!= &device
->pending_sync_bios
&&
342 device
->pending_sync_bios
.head
) ||
343 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
344 device
->pending_bios
.head
)) {
345 spin_lock(&device
->io_lock
);
346 requeue_list(pending_bios
, pending
, tail
);
351 pending
= pending
->bi_next
;
354 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
355 waitqueue_active(&fs_info
->async_submit_wait
))
356 wake_up(&fs_info
->async_submit_wait
);
358 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
361 * if we're doing the sync list, record that our
362 * plug has some sync requests on it
364 * If we're doing the regular list and there are
365 * sync requests sitting around, unplug before
368 if (pending_bios
== &device
->pending_sync_bios
) {
370 } else if (sync_pending
) {
371 blk_finish_plug(&plug
);
372 blk_start_plug(&plug
);
376 btrfsic_submit_bio(cur
->bi_rw
, cur
);
383 * we made progress, there is more work to do and the bdi
384 * is now congested. Back off and let other work structs
387 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
388 fs_info
->fs_devices
->open_devices
> 1) {
389 struct io_context
*ioc
;
391 ioc
= current
->io_context
;
394 * the main goal here is that we don't want to
395 * block if we're going to be able to submit
396 * more requests without blocking.
398 * This code does two great things, it pokes into
399 * the elevator code from a filesystem _and_
400 * it makes assumptions about how batching works.
402 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
403 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
405 ioc
->last_waited
== last_waited
)) {
407 * we want to go through our batch of
408 * requests and stop. So, we copy out
409 * the ioc->last_waited time and test
410 * against it before looping
412 last_waited
= ioc
->last_waited
;
417 spin_lock(&device
->io_lock
);
418 requeue_list(pending_bios
, pending
, tail
);
419 device
->running_pending
= 1;
421 spin_unlock(&device
->io_lock
);
422 btrfs_queue_work(fs_info
->submit_workers
,
426 /* unplug every 64 requests just for good measure */
427 if (batch_run
% 64 == 0) {
428 blk_finish_plug(&plug
);
429 blk_start_plug(&plug
);
438 spin_lock(&device
->io_lock
);
439 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
441 spin_unlock(&device
->io_lock
);
444 blk_finish_plug(&plug
);
447 static void pending_bios_fn(struct btrfs_work
*work
)
449 struct btrfs_device
*device
;
451 device
= container_of(work
, struct btrfs_device
, work
);
452 run_scheduled_bios(device
);
456 * Add new device to list of registered devices
459 * 1 - first time device is seen
460 * 0 - device already known
463 static noinline
int device_list_add(const char *path
,
464 struct btrfs_super_block
*disk_super
,
465 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
467 struct btrfs_device
*device
;
468 struct btrfs_fs_devices
*fs_devices
;
469 struct rcu_string
*name
;
471 u64 found_transid
= btrfs_super_generation(disk_super
);
473 fs_devices
= find_fsid(disk_super
->fsid
);
475 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
476 if (IS_ERR(fs_devices
))
477 return PTR_ERR(fs_devices
);
479 list_add(&fs_devices
->list
, &fs_uuids
);
483 device
= __find_device(&fs_devices
->devices
, devid
,
484 disk_super
->dev_item
.uuid
);
488 if (fs_devices
->opened
)
491 device
= btrfs_alloc_device(NULL
, &devid
,
492 disk_super
->dev_item
.uuid
);
493 if (IS_ERR(device
)) {
494 /* we can safely leave the fs_devices entry around */
495 return PTR_ERR(device
);
498 name
= rcu_string_strdup(path
, GFP_NOFS
);
503 rcu_assign_pointer(device
->name
, name
);
505 mutex_lock(&fs_devices
->device_list_mutex
);
506 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
507 fs_devices
->num_devices
++;
508 mutex_unlock(&fs_devices
->device_list_mutex
);
511 device
->fs_devices
= fs_devices
;
512 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
514 * When FS is already mounted.
515 * 1. If you are here and if the device->name is NULL that
516 * means this device was missing at time of FS mount.
517 * 2. If you are here and if the device->name is different
518 * from 'path' that means either
519 * a. The same device disappeared and reappeared with
521 * b. The missing-disk-which-was-replaced, has
524 * We must allow 1 and 2a above. But 2b would be a spurious
527 * Further in case of 1 and 2a above, the disk at 'path'
528 * would have missed some transaction when it was away and
529 * in case of 2a the stale bdev has to be updated as well.
530 * 2b must not be allowed at all time.
534 * As of now don't allow update to btrfs_fs_device through
535 * the btrfs dev scan cli, after FS has been mounted.
537 if (fs_devices
->opened
) {
541 * That is if the FS is _not_ mounted and if you
542 * are here, that means there is more than one
543 * disk with same uuid and devid.We keep the one
544 * with larger generation number or the last-in if
545 * generation are equal.
547 if (found_transid
< device
->generation
)
551 name
= rcu_string_strdup(path
, GFP_NOFS
);
554 rcu_string_free(device
->name
);
555 rcu_assign_pointer(device
->name
, name
);
556 if (device
->missing
) {
557 fs_devices
->missing_devices
--;
563 * Unmount does not free the btrfs_device struct but would zero
564 * generation along with most of the other members. So just update
565 * it back. We need it to pick the disk with largest generation
568 if (!fs_devices
->opened
)
569 device
->generation
= found_transid
;
571 *fs_devices_ret
= fs_devices
;
576 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
578 struct btrfs_fs_devices
*fs_devices
;
579 struct btrfs_device
*device
;
580 struct btrfs_device
*orig_dev
;
582 fs_devices
= alloc_fs_devices(orig
->fsid
);
583 if (IS_ERR(fs_devices
))
586 fs_devices
->total_devices
= orig
->total_devices
;
588 /* We have held the volume lock, it is safe to get the devices. */
589 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
590 struct rcu_string
*name
;
592 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
598 * This is ok to do without rcu read locked because we hold the
599 * uuid mutex so nothing we touch in here is going to disappear.
601 if (orig_dev
->name
) {
602 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
607 rcu_assign_pointer(device
->name
, name
);
610 list_add(&device
->dev_list
, &fs_devices
->devices
);
611 device
->fs_devices
= fs_devices
;
612 fs_devices
->num_devices
++;
616 free_fs_devices(fs_devices
);
617 return ERR_PTR(-ENOMEM
);
620 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
621 struct btrfs_fs_devices
*fs_devices
, int step
)
623 struct btrfs_device
*device
, *next
;
624 struct btrfs_device
*latest_dev
= NULL
;
626 mutex_lock(&uuid_mutex
);
628 /* This is the initialized path, it is safe to release the devices. */
629 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
630 if (device
->in_fs_metadata
) {
631 if (!device
->is_tgtdev_for_dev_replace
&&
633 device
->generation
> latest_dev
->generation
)) {
639 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
641 * In the first step, keep the device which has
642 * the correct fsid and the devid that is used
643 * for the dev_replace procedure.
644 * In the second step, the dev_replace state is
645 * read from the device tree and it is known
646 * whether the procedure is really active or
647 * not, which means whether this device is
648 * used or whether it should be removed.
650 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
655 blkdev_put(device
->bdev
, device
->mode
);
657 fs_devices
->open_devices
--;
659 if (device
->writeable
) {
660 list_del_init(&device
->dev_alloc_list
);
661 device
->writeable
= 0;
662 if (!device
->is_tgtdev_for_dev_replace
)
663 fs_devices
->rw_devices
--;
665 list_del_init(&device
->dev_list
);
666 fs_devices
->num_devices
--;
667 rcu_string_free(device
->name
);
671 if (fs_devices
->seed
) {
672 fs_devices
= fs_devices
->seed
;
676 fs_devices
->latest_bdev
= latest_dev
->bdev
;
678 mutex_unlock(&uuid_mutex
);
681 static void __free_device(struct work_struct
*work
)
683 struct btrfs_device
*device
;
685 device
= container_of(work
, struct btrfs_device
, rcu_work
);
688 blkdev_put(device
->bdev
, device
->mode
);
690 rcu_string_free(device
->name
);
694 static void free_device(struct rcu_head
*head
)
696 struct btrfs_device
*device
;
698 device
= container_of(head
, struct btrfs_device
, rcu
);
700 INIT_WORK(&device
->rcu_work
, __free_device
);
701 schedule_work(&device
->rcu_work
);
704 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
706 struct btrfs_device
*device
;
708 if (--fs_devices
->opened
> 0)
711 mutex_lock(&fs_devices
->device_list_mutex
);
712 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
713 struct btrfs_device
*new_device
;
714 struct rcu_string
*name
;
717 fs_devices
->open_devices
--;
719 if (device
->writeable
&&
720 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
721 list_del_init(&device
->dev_alloc_list
);
722 fs_devices
->rw_devices
--;
726 fs_devices
->missing_devices
--;
728 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
730 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
732 /* Safe because we are under uuid_mutex */
734 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
735 BUG_ON(!name
); /* -ENOMEM */
736 rcu_assign_pointer(new_device
->name
, name
);
739 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
740 new_device
->fs_devices
= device
->fs_devices
;
742 call_rcu(&device
->rcu
, free_device
);
744 mutex_unlock(&fs_devices
->device_list_mutex
);
746 WARN_ON(fs_devices
->open_devices
);
747 WARN_ON(fs_devices
->rw_devices
);
748 fs_devices
->opened
= 0;
749 fs_devices
->seeding
= 0;
754 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
756 struct btrfs_fs_devices
*seed_devices
= NULL
;
759 mutex_lock(&uuid_mutex
);
760 ret
= __btrfs_close_devices(fs_devices
);
761 if (!fs_devices
->opened
) {
762 seed_devices
= fs_devices
->seed
;
763 fs_devices
->seed
= NULL
;
765 mutex_unlock(&uuid_mutex
);
767 while (seed_devices
) {
768 fs_devices
= seed_devices
;
769 seed_devices
= fs_devices
->seed
;
770 __btrfs_close_devices(fs_devices
);
771 free_fs_devices(fs_devices
);
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
782 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
783 fmode_t flags
, void *holder
)
785 struct request_queue
*q
;
786 struct block_device
*bdev
;
787 struct list_head
*head
= &fs_devices
->devices
;
788 struct btrfs_device
*device
;
789 struct btrfs_device
*latest_dev
= NULL
;
790 struct buffer_head
*bh
;
791 struct btrfs_super_block
*disk_super
;
798 list_for_each_entry(device
, head
, dev_list
) {
804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
809 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
810 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
811 if (devid
!= device
->devid
)
814 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
818 device
->generation
= btrfs_super_generation(disk_super
);
820 device
->generation
> latest_dev
->generation
)
823 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
824 device
->writeable
= 0;
826 device
->writeable
= !bdev_read_only(bdev
);
830 q
= bdev_get_queue(bdev
);
831 if (blk_queue_discard(q
))
832 device
->can_discard
= 1;
835 device
->in_fs_metadata
= 0;
836 device
->mode
= flags
;
838 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
839 fs_devices
->rotating
= 1;
841 fs_devices
->open_devices
++;
842 if (device
->writeable
&&
843 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
844 fs_devices
->rw_devices
++;
845 list_add(&device
->dev_alloc_list
,
846 &fs_devices
->alloc_list
);
853 blkdev_put(bdev
, flags
);
856 if (fs_devices
->open_devices
== 0) {
860 fs_devices
->seeding
= seeding
;
861 fs_devices
->opened
= 1;
862 fs_devices
->latest_bdev
= latest_dev
->bdev
;
863 fs_devices
->total_rw_bytes
= 0;
868 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
869 fmode_t flags
, void *holder
)
873 mutex_lock(&uuid_mutex
);
874 if (fs_devices
->opened
) {
875 fs_devices
->opened
++;
878 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
880 mutex_unlock(&uuid_mutex
);
885 * Look for a btrfs signature on a device. This may be called out of the mount path
886 * and we are not allowed to call set_blocksize during the scan. The superblock
887 * is read via pagecache
889 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
890 struct btrfs_fs_devices
**fs_devices_ret
)
892 struct btrfs_super_block
*disk_super
;
893 struct block_device
*bdev
;
904 * we would like to check all the supers, but that would make
905 * a btrfs mount succeed after a mkfs from a different FS.
906 * So, we need to add a special mount option to scan for
907 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
909 bytenr
= btrfs_sb_offset(0);
911 mutex_lock(&uuid_mutex
);
913 bdev
= blkdev_get_by_path(path
, flags
, holder
);
920 /* make sure our super fits in the device */
921 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
924 /* make sure our super fits in the page */
925 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
928 /* make sure our super doesn't straddle pages on disk */
929 index
= bytenr
>> PAGE_CACHE_SHIFT
;
930 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
933 /* pull in the page with our super */
934 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
937 if (IS_ERR_OR_NULL(page
))
942 /* align our pointer to the offset of the super block */
943 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
945 if (btrfs_super_bytenr(disk_super
) != bytenr
||
946 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
)
949 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
950 transid
= btrfs_super_generation(disk_super
);
951 total_devices
= btrfs_super_num_devices(disk_super
);
953 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
955 if (disk_super
->label
[0]) {
956 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
957 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
958 printk(KERN_INFO
"BTRFS: device label %s ", disk_super
->label
);
960 printk(KERN_INFO
"BTRFS: device fsid %pU ", disk_super
->fsid
);
963 printk(KERN_CONT
"devid %llu transid %llu %s\n", devid
, transid
, path
);
966 if (!ret
&& fs_devices_ret
)
967 (*fs_devices_ret
)->total_devices
= total_devices
;
971 page_cache_release(page
);
974 blkdev_put(bdev
, flags
);
976 mutex_unlock(&uuid_mutex
);
980 /* helper to account the used device space in the range */
981 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
982 u64 end
, u64
*length
)
984 struct btrfs_key key
;
985 struct btrfs_root
*root
= device
->dev_root
;
986 struct btrfs_dev_extent
*dev_extent
;
987 struct btrfs_path
*path
;
991 struct extent_buffer
*l
;
995 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
998 path
= btrfs_alloc_path();
1003 key
.objectid
= device
->devid
;
1005 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1007 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1011 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1018 slot
= path
->slots
[0];
1019 if (slot
>= btrfs_header_nritems(l
)) {
1020 ret
= btrfs_next_leaf(root
, path
);
1028 btrfs_item_key_to_cpu(l
, &key
, slot
);
1030 if (key
.objectid
< device
->devid
)
1033 if (key
.objectid
> device
->devid
)
1036 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1039 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1040 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1042 if (key
.offset
<= start
&& extent_end
> end
) {
1043 *length
= end
- start
+ 1;
1045 } else if (key
.offset
<= start
&& extent_end
> start
)
1046 *length
+= extent_end
- start
;
1047 else if (key
.offset
> start
&& extent_end
<= end
)
1048 *length
+= extent_end
- key
.offset
;
1049 else if (key
.offset
> start
&& key
.offset
<= end
) {
1050 *length
+= end
- key
.offset
+ 1;
1052 } else if (key
.offset
> end
)
1060 btrfs_free_path(path
);
1064 static int contains_pending_extent(struct btrfs_trans_handle
*trans
,
1065 struct btrfs_device
*device
,
1066 u64
*start
, u64 len
)
1068 struct extent_map
*em
;
1071 list_for_each_entry(em
, &trans
->transaction
->pending_chunks
, list
) {
1072 struct map_lookup
*map
;
1075 map
= (struct map_lookup
*)em
->bdev
;
1076 for (i
= 0; i
< map
->num_stripes
; i
++) {
1077 if (map
->stripes
[i
].dev
!= device
)
1079 if (map
->stripes
[i
].physical
>= *start
+ len
||
1080 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1083 *start
= map
->stripes
[i
].physical
+
1094 * find_free_dev_extent - find free space in the specified device
1095 * @device: the device which we search the free space in
1096 * @num_bytes: the size of the free space that we need
1097 * @start: store the start of the free space.
1098 * @len: the size of the free space. that we find, or the size of the max
1099 * free space if we don't find suitable free space
1101 * this uses a pretty simple search, the expectation is that it is
1102 * called very infrequently and that a given device has a small number
1105 * @start is used to store the start of the free space if we find. But if we
1106 * don't find suitable free space, it will be used to store the start position
1107 * of the max free space.
1109 * @len is used to store the size of the free space that we find.
1110 * But if we don't find suitable free space, it is used to store the size of
1111 * the max free space.
1113 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1114 struct btrfs_device
*device
, u64 num_bytes
,
1115 u64
*start
, u64
*len
)
1117 struct btrfs_key key
;
1118 struct btrfs_root
*root
= device
->dev_root
;
1119 struct btrfs_dev_extent
*dev_extent
;
1120 struct btrfs_path
*path
;
1126 u64 search_end
= device
->total_bytes
;
1129 struct extent_buffer
*l
;
1131 /* FIXME use last free of some kind */
1133 /* we don't want to overwrite the superblock on the drive,
1134 * so we make sure to start at an offset of at least 1MB
1136 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1138 path
= btrfs_alloc_path();
1142 max_hole_start
= search_start
;
1146 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1152 path
->search_commit_root
= 1;
1153 path
->skip_locking
= 1;
1155 key
.objectid
= device
->devid
;
1156 key
.offset
= search_start
;
1157 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1159 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1163 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1170 slot
= path
->slots
[0];
1171 if (slot
>= btrfs_header_nritems(l
)) {
1172 ret
= btrfs_next_leaf(root
, path
);
1180 btrfs_item_key_to_cpu(l
, &key
, slot
);
1182 if (key
.objectid
< device
->devid
)
1185 if (key
.objectid
> device
->devid
)
1188 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1191 if (key
.offset
> search_start
) {
1192 hole_size
= key
.offset
- search_start
;
1195 * Have to check before we set max_hole_start, otherwise
1196 * we could end up sending back this offset anyway.
1198 if (contains_pending_extent(trans
, device
,
1203 if (hole_size
> max_hole_size
) {
1204 max_hole_start
= search_start
;
1205 max_hole_size
= hole_size
;
1209 * If this free space is greater than which we need,
1210 * it must be the max free space that we have found
1211 * until now, so max_hole_start must point to the start
1212 * of this free space and the length of this free space
1213 * is stored in max_hole_size. Thus, we return
1214 * max_hole_start and max_hole_size and go back to the
1217 if (hole_size
>= num_bytes
) {
1223 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1224 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1226 if (extent_end
> search_start
)
1227 search_start
= extent_end
;
1234 * At this point, search_start should be the end of
1235 * allocated dev extents, and when shrinking the device,
1236 * search_end may be smaller than search_start.
1238 if (search_end
> search_start
)
1239 hole_size
= search_end
- search_start
;
1241 if (hole_size
> max_hole_size
) {
1242 max_hole_start
= search_start
;
1243 max_hole_size
= hole_size
;
1246 if (contains_pending_extent(trans
, device
, &search_start
, hole_size
)) {
1247 btrfs_release_path(path
);
1252 if (hole_size
< num_bytes
)
1258 btrfs_free_path(path
);
1259 *start
= max_hole_start
;
1261 *len
= max_hole_size
;
1265 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1266 struct btrfs_device
*device
,
1270 struct btrfs_path
*path
;
1271 struct btrfs_root
*root
= device
->dev_root
;
1272 struct btrfs_key key
;
1273 struct btrfs_key found_key
;
1274 struct extent_buffer
*leaf
= NULL
;
1275 struct btrfs_dev_extent
*extent
= NULL
;
1277 path
= btrfs_alloc_path();
1281 key
.objectid
= device
->devid
;
1283 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1285 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1287 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1288 BTRFS_DEV_EXTENT_KEY
);
1291 leaf
= path
->nodes
[0];
1292 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1293 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1294 struct btrfs_dev_extent
);
1295 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1296 btrfs_dev_extent_length(leaf
, extent
) < start
);
1298 btrfs_release_path(path
);
1300 } else if (ret
== 0) {
1301 leaf
= path
->nodes
[0];
1302 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1303 struct btrfs_dev_extent
);
1305 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1309 if (device
->bytes_used
> 0) {
1310 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1311 device
->bytes_used
-= len
;
1312 spin_lock(&root
->fs_info
->free_chunk_lock
);
1313 root
->fs_info
->free_chunk_space
+= len
;
1314 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1316 ret
= btrfs_del_item(trans
, root
, path
);
1318 btrfs_error(root
->fs_info
, ret
,
1319 "Failed to remove dev extent item");
1322 btrfs_free_path(path
);
1326 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1327 struct btrfs_device
*device
,
1328 u64 chunk_tree
, u64 chunk_objectid
,
1329 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1332 struct btrfs_path
*path
;
1333 struct btrfs_root
*root
= device
->dev_root
;
1334 struct btrfs_dev_extent
*extent
;
1335 struct extent_buffer
*leaf
;
1336 struct btrfs_key key
;
1338 WARN_ON(!device
->in_fs_metadata
);
1339 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1340 path
= btrfs_alloc_path();
1344 key
.objectid
= device
->devid
;
1346 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1347 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1352 leaf
= path
->nodes
[0];
1353 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1354 struct btrfs_dev_extent
);
1355 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1356 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1357 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1359 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1360 btrfs_dev_extent_chunk_tree_uuid(extent
), BTRFS_UUID_SIZE
);
1362 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1363 btrfs_mark_buffer_dirty(leaf
);
1365 btrfs_free_path(path
);
1369 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1371 struct extent_map_tree
*em_tree
;
1372 struct extent_map
*em
;
1376 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1377 read_lock(&em_tree
->lock
);
1378 n
= rb_last(&em_tree
->map
);
1380 em
= rb_entry(n
, struct extent_map
, rb_node
);
1381 ret
= em
->start
+ em
->len
;
1383 read_unlock(&em_tree
->lock
);
1388 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1392 struct btrfs_key key
;
1393 struct btrfs_key found_key
;
1394 struct btrfs_path
*path
;
1396 path
= btrfs_alloc_path();
1400 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1401 key
.type
= BTRFS_DEV_ITEM_KEY
;
1402 key
.offset
= (u64
)-1;
1404 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1408 BUG_ON(ret
== 0); /* Corruption */
1410 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1411 BTRFS_DEV_ITEMS_OBJECTID
,
1412 BTRFS_DEV_ITEM_KEY
);
1416 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1418 *devid_ret
= found_key
.offset
+ 1;
1422 btrfs_free_path(path
);
1427 * the device information is stored in the chunk root
1428 * the btrfs_device struct should be fully filled in
1430 static int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1431 struct btrfs_root
*root
,
1432 struct btrfs_device
*device
)
1435 struct btrfs_path
*path
;
1436 struct btrfs_dev_item
*dev_item
;
1437 struct extent_buffer
*leaf
;
1438 struct btrfs_key key
;
1441 root
= root
->fs_info
->chunk_root
;
1443 path
= btrfs_alloc_path();
1447 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1448 key
.type
= BTRFS_DEV_ITEM_KEY
;
1449 key
.offset
= device
->devid
;
1451 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1456 leaf
= path
->nodes
[0];
1457 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1459 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1460 btrfs_set_device_generation(leaf
, dev_item
, 0);
1461 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1462 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1463 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1464 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1465 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1466 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1467 btrfs_set_device_group(leaf
, dev_item
, 0);
1468 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1469 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1470 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1472 ptr
= btrfs_device_uuid(dev_item
);
1473 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1474 ptr
= btrfs_device_fsid(dev_item
);
1475 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1476 btrfs_mark_buffer_dirty(leaf
);
1480 btrfs_free_path(path
);
1485 * Function to update ctime/mtime for a given device path.
1486 * Mainly used for ctime/mtime based probe like libblkid.
1488 static void update_dev_time(char *path_name
)
1492 filp
= filp_open(path_name
, O_RDWR
, 0);
1495 file_update_time(filp
);
1496 filp_close(filp
, NULL
);
1500 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1501 struct btrfs_device
*device
)
1504 struct btrfs_path
*path
;
1505 struct btrfs_key key
;
1506 struct btrfs_trans_handle
*trans
;
1508 root
= root
->fs_info
->chunk_root
;
1510 path
= btrfs_alloc_path();
1514 trans
= btrfs_start_transaction(root
, 0);
1515 if (IS_ERR(trans
)) {
1516 btrfs_free_path(path
);
1517 return PTR_ERR(trans
);
1519 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1520 key
.type
= BTRFS_DEV_ITEM_KEY
;
1521 key
.offset
= device
->devid
;
1524 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1533 ret
= btrfs_del_item(trans
, root
, path
);
1537 btrfs_free_path(path
);
1538 unlock_chunks(root
);
1539 btrfs_commit_transaction(trans
, root
);
1543 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1545 struct btrfs_device
*device
;
1546 struct btrfs_device
*next_device
;
1547 struct block_device
*bdev
;
1548 struct buffer_head
*bh
= NULL
;
1549 struct btrfs_super_block
*disk_super
;
1550 struct btrfs_fs_devices
*cur_devices
;
1557 bool clear_super
= false;
1559 mutex_lock(&uuid_mutex
);
1562 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
1564 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1565 root
->fs_info
->avail_system_alloc_bits
|
1566 root
->fs_info
->avail_metadata_alloc_bits
;
1567 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
1569 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1570 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1571 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1572 WARN_ON(num_devices
< 1);
1575 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1577 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1578 ret
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
;
1582 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1583 ret
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
;
1587 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1588 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1589 ret
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
;
1592 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1593 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1594 ret
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
;
1598 if (strcmp(device_path
, "missing") == 0) {
1599 struct list_head
*devices
;
1600 struct btrfs_device
*tmp
;
1603 devices
= &root
->fs_info
->fs_devices
->devices
;
1605 * It is safe to read the devices since the volume_mutex
1608 list_for_each_entry(tmp
, devices
, dev_list
) {
1609 if (tmp
->in_fs_metadata
&&
1610 !tmp
->is_tgtdev_for_dev_replace
&&
1620 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
1624 ret
= btrfs_get_bdev_and_sb(device_path
,
1625 FMODE_WRITE
| FMODE_EXCL
,
1626 root
->fs_info
->bdev_holder
, 0,
1630 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1631 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1632 dev_uuid
= disk_super
->dev_item
.uuid
;
1633 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1641 if (device
->is_tgtdev_for_dev_replace
) {
1642 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1646 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1647 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1651 if (device
->writeable
) {
1653 list_del_init(&device
->dev_alloc_list
);
1654 unlock_chunks(root
);
1655 root
->fs_info
->fs_devices
->rw_devices
--;
1659 mutex_unlock(&uuid_mutex
);
1660 ret
= btrfs_shrink_device(device
, 0);
1661 mutex_lock(&uuid_mutex
);
1666 * TODO: the superblock still includes this device in its num_devices
1667 * counter although write_all_supers() is not locked out. This
1668 * could give a filesystem state which requires a degraded mount.
1670 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1674 spin_lock(&root
->fs_info
->free_chunk_lock
);
1675 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1677 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1679 device
->in_fs_metadata
= 0;
1680 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1683 * the device list mutex makes sure that we don't change
1684 * the device list while someone else is writing out all
1685 * the device supers. Whoever is writing all supers, should
1686 * lock the device list mutex before getting the number of
1687 * devices in the super block (super_copy). Conversely,
1688 * whoever updates the number of devices in the super block
1689 * (super_copy) should hold the device list mutex.
1692 cur_devices
= device
->fs_devices
;
1693 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1694 list_del_rcu(&device
->dev_list
);
1696 device
->fs_devices
->num_devices
--;
1697 device
->fs_devices
->total_devices
--;
1699 if (device
->missing
)
1700 device
->fs_devices
->missing_devices
--;
1702 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1703 struct btrfs_device
, dev_list
);
1704 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1705 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1706 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1707 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1710 device
->fs_devices
->open_devices
--;
1711 /* remove sysfs entry */
1712 btrfs_kobj_rm_device(root
->fs_info
, device
);
1715 call_rcu(&device
->rcu
, free_device
);
1717 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1718 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1719 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1721 if (cur_devices
->open_devices
== 0) {
1722 struct btrfs_fs_devices
*fs_devices
;
1723 fs_devices
= root
->fs_info
->fs_devices
;
1724 while (fs_devices
) {
1725 if (fs_devices
->seed
== cur_devices
) {
1726 fs_devices
->seed
= cur_devices
->seed
;
1729 fs_devices
= fs_devices
->seed
;
1731 cur_devices
->seed
= NULL
;
1733 __btrfs_close_devices(cur_devices
);
1734 unlock_chunks(root
);
1735 free_fs_devices(cur_devices
);
1738 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1739 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1742 * at this point, the device is zero sized. We want to
1743 * remove it from the devices list and zero out the old super
1745 if (clear_super
&& disk_super
) {
1749 /* make sure this device isn't detected as part of
1752 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1753 set_buffer_dirty(bh
);
1754 sync_dirty_buffer(bh
);
1756 /* clear the mirror copies of super block on the disk
1757 * being removed, 0th copy is been taken care above and
1758 * the below would take of the rest
1760 for (i
= 1; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1761 bytenr
= btrfs_sb_offset(i
);
1762 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>=
1763 i_size_read(bdev
->bd_inode
))
1767 bh
= __bread(bdev
, bytenr
/ 4096,
1768 BTRFS_SUPER_INFO_SIZE
);
1772 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1774 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1775 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
) {
1778 memset(&disk_super
->magic
, 0,
1779 sizeof(disk_super
->magic
));
1780 set_buffer_dirty(bh
);
1781 sync_dirty_buffer(bh
);
1788 /* Notify udev that device has changed */
1789 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1791 /* Update ctime/mtime for device path for libblkid */
1792 update_dev_time(device_path
);
1798 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1800 mutex_unlock(&uuid_mutex
);
1803 if (device
->writeable
) {
1805 list_add(&device
->dev_alloc_list
,
1806 &root
->fs_info
->fs_devices
->alloc_list
);
1807 unlock_chunks(root
);
1808 root
->fs_info
->fs_devices
->rw_devices
++;
1813 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1814 struct btrfs_device
*srcdev
)
1816 struct btrfs_fs_devices
*fs_devices
;
1818 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1821 * in case of fs with no seed, srcdev->fs_devices will point
1822 * to fs_devices of fs_info. However when the dev being replaced is
1823 * a seed dev it will point to the seed's local fs_devices. In short
1824 * srcdev will have its correct fs_devices in both the cases.
1826 fs_devices
= srcdev
->fs_devices
;
1828 list_del_rcu(&srcdev
->dev_list
);
1829 list_del_rcu(&srcdev
->dev_alloc_list
);
1830 fs_devices
->num_devices
--;
1831 if (srcdev
->missing
) {
1832 fs_devices
->missing_devices
--;
1833 if (!fs_devices
->seeding
)
1834 fs_devices
->rw_devices
++;
1838 fs_devices
->open_devices
--;
1841 * zero out the old super if it is not writable
1842 * (e.g. seed device)
1844 if (srcdev
->writeable
)
1845 btrfs_scratch_superblock(srcdev
);
1848 call_rcu(&srcdev
->rcu
, free_device
);
1851 * unless fs_devices is seed fs, num_devices shouldn't go
1854 BUG_ON(!fs_devices
->num_devices
&& !fs_devices
->seeding
);
1856 /* if this is no devs we rather delete the fs_devices */
1857 if (!fs_devices
->num_devices
) {
1858 struct btrfs_fs_devices
*tmp_fs_devices
;
1860 tmp_fs_devices
= fs_info
->fs_devices
;
1861 while (tmp_fs_devices
) {
1862 if (tmp_fs_devices
->seed
== fs_devices
) {
1863 tmp_fs_devices
->seed
= fs_devices
->seed
;
1866 tmp_fs_devices
= tmp_fs_devices
->seed
;
1868 fs_devices
->seed
= NULL
;
1869 __btrfs_close_devices(fs_devices
);
1870 free_fs_devices(fs_devices
);
1874 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1875 struct btrfs_device
*tgtdev
)
1877 struct btrfs_device
*next_device
;
1880 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1882 btrfs_scratch_superblock(tgtdev
);
1883 fs_info
->fs_devices
->open_devices
--;
1885 fs_info
->fs_devices
->num_devices
--;
1887 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1888 struct btrfs_device
, dev_list
);
1889 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1890 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1891 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1892 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1893 list_del_rcu(&tgtdev
->dev_list
);
1895 call_rcu(&tgtdev
->rcu
, free_device
);
1897 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1900 static int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1901 struct btrfs_device
**device
)
1904 struct btrfs_super_block
*disk_super
;
1907 struct block_device
*bdev
;
1908 struct buffer_head
*bh
;
1911 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1912 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1915 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1916 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1917 dev_uuid
= disk_super
->dev_item
.uuid
;
1918 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1923 blkdev_put(bdev
, FMODE_READ
);
1927 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1929 struct btrfs_device
**device
)
1932 if (strcmp(device_path
, "missing") == 0) {
1933 struct list_head
*devices
;
1934 struct btrfs_device
*tmp
;
1936 devices
= &root
->fs_info
->fs_devices
->devices
;
1938 * It is safe to read the devices since the volume_mutex
1939 * is held by the caller.
1941 list_for_each_entry(tmp
, devices
, dev_list
) {
1942 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1949 btrfs_err(root
->fs_info
, "no missing device found");
1955 return btrfs_find_device_by_path(root
, device_path
, device
);
1960 * does all the dirty work required for changing file system's UUID.
1962 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1964 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1965 struct btrfs_fs_devices
*old_devices
;
1966 struct btrfs_fs_devices
*seed_devices
;
1967 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1968 struct btrfs_device
*device
;
1971 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1972 if (!fs_devices
->seeding
)
1975 seed_devices
= __alloc_fs_devices();
1976 if (IS_ERR(seed_devices
))
1977 return PTR_ERR(seed_devices
);
1979 old_devices
= clone_fs_devices(fs_devices
);
1980 if (IS_ERR(old_devices
)) {
1981 kfree(seed_devices
);
1982 return PTR_ERR(old_devices
);
1985 list_add(&old_devices
->list
, &fs_uuids
);
1987 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1988 seed_devices
->opened
= 1;
1989 INIT_LIST_HEAD(&seed_devices
->devices
);
1990 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1991 mutex_init(&seed_devices
->device_list_mutex
);
1993 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1994 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1997 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1998 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1999 device
->fs_devices
= seed_devices
;
2002 fs_devices
->seeding
= 0;
2003 fs_devices
->num_devices
= 0;
2004 fs_devices
->open_devices
= 0;
2005 fs_devices
->missing_devices
= 0;
2006 fs_devices
->rotating
= 0;
2007 fs_devices
->seed
= seed_devices
;
2009 generate_random_uuid(fs_devices
->fsid
);
2010 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2011 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2012 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2014 super_flags
= btrfs_super_flags(disk_super
) &
2015 ~BTRFS_SUPER_FLAG_SEEDING
;
2016 btrfs_set_super_flags(disk_super
, super_flags
);
2022 * strore the expected generation for seed devices in device items.
2024 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
2025 struct btrfs_root
*root
)
2027 struct btrfs_path
*path
;
2028 struct extent_buffer
*leaf
;
2029 struct btrfs_dev_item
*dev_item
;
2030 struct btrfs_device
*device
;
2031 struct btrfs_key key
;
2032 u8 fs_uuid
[BTRFS_UUID_SIZE
];
2033 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2037 path
= btrfs_alloc_path();
2041 root
= root
->fs_info
->chunk_root
;
2042 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2044 key
.type
= BTRFS_DEV_ITEM_KEY
;
2047 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2051 leaf
= path
->nodes
[0];
2053 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2054 ret
= btrfs_next_leaf(root
, path
);
2059 leaf
= path
->nodes
[0];
2060 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2061 btrfs_release_path(path
);
2065 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2066 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2067 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2070 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2071 struct btrfs_dev_item
);
2072 devid
= btrfs_device_id(leaf
, dev_item
);
2073 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2075 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2077 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
2079 BUG_ON(!device
); /* Logic error */
2081 if (device
->fs_devices
->seeding
) {
2082 btrfs_set_device_generation(leaf
, dev_item
,
2083 device
->generation
);
2084 btrfs_mark_buffer_dirty(leaf
);
2092 btrfs_free_path(path
);
2096 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
2098 struct request_queue
*q
;
2099 struct btrfs_trans_handle
*trans
;
2100 struct btrfs_device
*device
;
2101 struct block_device
*bdev
;
2102 struct list_head
*devices
;
2103 struct super_block
*sb
= root
->fs_info
->sb
;
2104 struct rcu_string
*name
;
2106 int seeding_dev
= 0;
2109 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
2112 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2113 root
->fs_info
->bdev_holder
);
2115 return PTR_ERR(bdev
);
2117 if (root
->fs_info
->fs_devices
->seeding
) {
2119 down_write(&sb
->s_umount
);
2120 mutex_lock(&uuid_mutex
);
2123 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2125 devices
= &root
->fs_info
->fs_devices
->devices
;
2127 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2128 list_for_each_entry(device
, devices
, dev_list
) {
2129 if (device
->bdev
== bdev
) {
2132 &root
->fs_info
->fs_devices
->device_list_mutex
);
2136 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2138 device
= btrfs_alloc_device(root
->fs_info
, NULL
, NULL
);
2139 if (IS_ERR(device
)) {
2140 /* we can safely leave the fs_devices entry around */
2141 ret
= PTR_ERR(device
);
2145 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2151 rcu_assign_pointer(device
->name
, name
);
2153 trans
= btrfs_start_transaction(root
, 0);
2154 if (IS_ERR(trans
)) {
2155 rcu_string_free(device
->name
);
2157 ret
= PTR_ERR(trans
);
2163 q
= bdev_get_queue(bdev
);
2164 if (blk_queue_discard(q
))
2165 device
->can_discard
= 1;
2166 device
->writeable
= 1;
2167 device
->generation
= trans
->transid
;
2168 device
->io_width
= root
->sectorsize
;
2169 device
->io_align
= root
->sectorsize
;
2170 device
->sector_size
= root
->sectorsize
;
2171 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2172 device
->disk_total_bytes
= device
->total_bytes
;
2173 device
->commit_total_bytes
= device
->total_bytes
;
2174 device
->dev_root
= root
->fs_info
->dev_root
;
2175 device
->bdev
= bdev
;
2176 device
->in_fs_metadata
= 1;
2177 device
->is_tgtdev_for_dev_replace
= 0;
2178 device
->mode
= FMODE_EXCL
;
2179 device
->dev_stats_valid
= 1;
2180 set_blocksize(device
->bdev
, 4096);
2183 sb
->s_flags
&= ~MS_RDONLY
;
2184 ret
= btrfs_prepare_sprout(root
);
2185 BUG_ON(ret
); /* -ENOMEM */
2188 device
->fs_devices
= root
->fs_info
->fs_devices
;
2190 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2191 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
2192 list_add(&device
->dev_alloc_list
,
2193 &root
->fs_info
->fs_devices
->alloc_list
);
2194 root
->fs_info
->fs_devices
->num_devices
++;
2195 root
->fs_info
->fs_devices
->open_devices
++;
2196 root
->fs_info
->fs_devices
->rw_devices
++;
2197 root
->fs_info
->fs_devices
->total_devices
++;
2198 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2200 spin_lock(&root
->fs_info
->free_chunk_lock
);
2201 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
2202 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2204 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2205 root
->fs_info
->fs_devices
->rotating
= 1;
2207 tmp
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2208 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2209 tmp
+ device
->total_bytes
);
2211 tmp
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2212 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2215 /* add sysfs device entry */
2216 btrfs_kobj_add_device(root
->fs_info
, device
);
2218 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2221 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2222 ret
= init_first_rw_device(trans
, root
, device
);
2224 btrfs_abort_transaction(trans
, root
, ret
);
2227 ret
= btrfs_finish_sprout(trans
, root
);
2229 btrfs_abort_transaction(trans
, root
, ret
);
2233 /* Sprouting would change fsid of the mounted root,
2234 * so rename the fsid on the sysfs
2236 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2237 root
->fs_info
->fsid
);
2238 if (kobject_rename(&root
->fs_info
->super_kobj
, fsid_buf
))
2241 ret
= btrfs_add_device(trans
, root
, device
);
2243 btrfs_abort_transaction(trans
, root
, ret
);
2249 * we've got more storage, clear any full flags on the space
2252 btrfs_clear_space_info_full(root
->fs_info
);
2254 unlock_chunks(root
);
2255 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2256 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2257 ret
= btrfs_commit_transaction(trans
, root
);
2260 mutex_unlock(&uuid_mutex
);
2261 up_write(&sb
->s_umount
);
2263 if (ret
) /* transaction commit */
2266 ret
= btrfs_relocate_sys_chunks(root
);
2268 btrfs_error(root
->fs_info
, ret
,
2269 "Failed to relocate sys chunks after "
2270 "device initialization. This can be fixed "
2271 "using the \"btrfs balance\" command.");
2272 trans
= btrfs_attach_transaction(root
);
2273 if (IS_ERR(trans
)) {
2274 if (PTR_ERR(trans
) == -ENOENT
)
2276 return PTR_ERR(trans
);
2278 ret
= btrfs_commit_transaction(trans
, root
);
2281 /* Update ctime/mtime for libblkid */
2282 update_dev_time(device_path
);
2286 unlock_chunks(root
);
2287 btrfs_end_transaction(trans
, root
);
2288 rcu_string_free(device
->name
);
2289 btrfs_kobj_rm_device(root
->fs_info
, device
);
2292 blkdev_put(bdev
, FMODE_EXCL
);
2294 mutex_unlock(&uuid_mutex
);
2295 up_write(&sb
->s_umount
);
2300 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2301 struct btrfs_device
*srcdev
,
2302 struct btrfs_device
**device_out
)
2304 struct request_queue
*q
;
2305 struct btrfs_device
*device
;
2306 struct block_device
*bdev
;
2307 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2308 struct list_head
*devices
;
2309 struct rcu_string
*name
;
2310 u64 devid
= BTRFS_DEV_REPLACE_DEVID
;
2314 if (fs_info
->fs_devices
->seeding
) {
2315 btrfs_err(fs_info
, "the filesystem is a seed filesystem!");
2319 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2320 fs_info
->bdev_holder
);
2322 btrfs_err(fs_info
, "target device %s is invalid!", device_path
);
2323 return PTR_ERR(bdev
);
2326 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2328 devices
= &fs_info
->fs_devices
->devices
;
2329 list_for_each_entry(device
, devices
, dev_list
) {
2330 if (device
->bdev
== bdev
) {
2331 btrfs_err(fs_info
, "target device is in the filesystem!");
2338 if (i_size_read(bdev
->bd_inode
) < srcdev
->total_bytes
) {
2339 btrfs_err(fs_info
, "target device is smaller than source device!");
2345 device
= btrfs_alloc_device(NULL
, &devid
, NULL
);
2346 if (IS_ERR(device
)) {
2347 ret
= PTR_ERR(device
);
2351 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2357 rcu_assign_pointer(device
->name
, name
);
2359 q
= bdev_get_queue(bdev
);
2360 if (blk_queue_discard(q
))
2361 device
->can_discard
= 1;
2362 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2363 device
->writeable
= 1;
2364 device
->generation
= 0;
2365 device
->io_width
= root
->sectorsize
;
2366 device
->io_align
= root
->sectorsize
;
2367 device
->sector_size
= root
->sectorsize
;
2368 device
->total_bytes
= srcdev
->total_bytes
;
2369 device
->disk_total_bytes
= srcdev
->disk_total_bytes
;
2370 ASSERT(list_empty(&srcdev
->resized_list
));
2371 device
->commit_total_bytes
= srcdev
->commit_total_bytes
;
2372 device
->bytes_used
= srcdev
->bytes_used
;
2373 device
->dev_root
= fs_info
->dev_root
;
2374 device
->bdev
= bdev
;
2375 device
->in_fs_metadata
= 1;
2376 device
->is_tgtdev_for_dev_replace
= 1;
2377 device
->mode
= FMODE_EXCL
;
2378 device
->dev_stats_valid
= 1;
2379 set_blocksize(device
->bdev
, 4096);
2380 device
->fs_devices
= fs_info
->fs_devices
;
2381 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2382 fs_info
->fs_devices
->num_devices
++;
2383 fs_info
->fs_devices
->open_devices
++;
2384 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2386 *device_out
= device
;
2390 blkdev_put(bdev
, FMODE_EXCL
);
2394 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2395 struct btrfs_device
*tgtdev
)
2397 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2398 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2399 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2400 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2401 tgtdev
->dev_root
= fs_info
->dev_root
;
2402 tgtdev
->in_fs_metadata
= 1;
2405 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2406 struct btrfs_device
*device
)
2409 struct btrfs_path
*path
;
2410 struct btrfs_root
*root
;
2411 struct btrfs_dev_item
*dev_item
;
2412 struct extent_buffer
*leaf
;
2413 struct btrfs_key key
;
2415 root
= device
->dev_root
->fs_info
->chunk_root
;
2417 path
= btrfs_alloc_path();
2421 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2422 key
.type
= BTRFS_DEV_ITEM_KEY
;
2423 key
.offset
= device
->devid
;
2425 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2434 leaf
= path
->nodes
[0];
2435 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2437 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2438 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2439 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2440 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2441 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2442 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2443 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2444 btrfs_mark_buffer_dirty(leaf
);
2447 btrfs_free_path(path
);
2451 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2452 struct btrfs_device
*device
, u64 new_size
)
2454 struct btrfs_super_block
*super_copy
=
2455 device
->dev_root
->fs_info
->super_copy
;
2456 struct btrfs_fs_devices
*fs_devices
;
2457 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2458 u64 diff
= new_size
- device
->total_bytes
;
2460 if (!device
->writeable
)
2462 if (new_size
<= device
->total_bytes
||
2463 device
->is_tgtdev_for_dev_replace
)
2466 fs_devices
= device
->dev_root
->fs_info
->fs_devices
;
2468 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2469 device
->fs_devices
->total_rw_bytes
+= diff
;
2471 device
->total_bytes
= new_size
;
2472 device
->disk_total_bytes
= new_size
;
2473 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2474 if (list_empty(&device
->resized_list
))
2475 list_add_tail(&device
->resized_list
,
2476 &fs_devices
->resized_devices
);
2478 return btrfs_update_device(trans
, device
);
2481 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2482 struct btrfs_device
*device
, u64 new_size
)
2485 lock_chunks(device
->dev_root
);
2486 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2487 unlock_chunks(device
->dev_root
);
2491 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2492 struct btrfs_root
*root
,
2493 u64 chunk_tree
, u64 chunk_objectid
,
2497 struct btrfs_path
*path
;
2498 struct btrfs_key key
;
2500 root
= root
->fs_info
->chunk_root
;
2501 path
= btrfs_alloc_path();
2505 key
.objectid
= chunk_objectid
;
2506 key
.offset
= chunk_offset
;
2507 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2509 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2512 else if (ret
> 0) { /* Logic error or corruption */
2513 btrfs_error(root
->fs_info
, -ENOENT
,
2514 "Failed lookup while freeing chunk.");
2519 ret
= btrfs_del_item(trans
, root
, path
);
2521 btrfs_error(root
->fs_info
, ret
,
2522 "Failed to delete chunk item.");
2524 btrfs_free_path(path
);
2528 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2531 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2532 struct btrfs_disk_key
*disk_key
;
2533 struct btrfs_chunk
*chunk
;
2540 struct btrfs_key key
;
2542 array_size
= btrfs_super_sys_array_size(super_copy
);
2544 ptr
= super_copy
->sys_chunk_array
;
2547 while (cur
< array_size
) {
2548 disk_key
= (struct btrfs_disk_key
*)ptr
;
2549 btrfs_disk_key_to_cpu(&key
, disk_key
);
2551 len
= sizeof(*disk_key
);
2553 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2554 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2555 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2556 len
+= btrfs_chunk_item_size(num_stripes
);
2561 if (key
.objectid
== chunk_objectid
&&
2562 key
.offset
== chunk_offset
) {
2563 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2565 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2574 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2575 u64 chunk_tree
, u64 chunk_objectid
,
2578 struct extent_map_tree
*em_tree
;
2579 struct btrfs_root
*extent_root
;
2580 struct btrfs_trans_handle
*trans
;
2581 struct extent_map
*em
;
2582 struct map_lookup
*map
;
2586 root
= root
->fs_info
->chunk_root
;
2587 extent_root
= root
->fs_info
->extent_root
;
2588 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2590 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2594 /* step one, relocate all the extents inside this chunk */
2595 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2599 trans
= btrfs_start_transaction(root
, 0);
2600 if (IS_ERR(trans
)) {
2601 ret
= PTR_ERR(trans
);
2602 btrfs_std_error(root
->fs_info
, ret
);
2609 * step two, delete the device extents and the
2610 * chunk tree entries
2612 read_lock(&em_tree
->lock
);
2613 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2614 read_unlock(&em_tree
->lock
);
2616 BUG_ON(!em
|| em
->start
> chunk_offset
||
2617 em
->start
+ em
->len
< chunk_offset
);
2618 map
= (struct map_lookup
*)em
->bdev
;
2620 for (i
= 0; i
< map
->num_stripes
; i
++) {
2621 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2622 map
->stripes
[i
].physical
);
2625 if (map
->stripes
[i
].dev
) {
2626 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2630 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2635 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2637 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2638 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2642 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2645 write_lock(&em_tree
->lock
);
2646 remove_extent_mapping(em_tree
, em
);
2647 write_unlock(&em_tree
->lock
);
2649 /* once for the tree */
2650 free_extent_map(em
);
2652 free_extent_map(em
);
2654 unlock_chunks(root
);
2655 btrfs_end_transaction(trans
, root
);
2659 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2661 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2662 struct btrfs_path
*path
;
2663 struct extent_buffer
*leaf
;
2664 struct btrfs_chunk
*chunk
;
2665 struct btrfs_key key
;
2666 struct btrfs_key found_key
;
2667 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2669 bool retried
= false;
2673 path
= btrfs_alloc_path();
2678 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2679 key
.offset
= (u64
)-1;
2680 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2683 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2686 BUG_ON(ret
== 0); /* Corruption */
2688 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2695 leaf
= path
->nodes
[0];
2696 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2698 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2699 struct btrfs_chunk
);
2700 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2701 btrfs_release_path(path
);
2703 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2704 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2713 if (found_key
.offset
== 0)
2715 key
.offset
= found_key
.offset
- 1;
2718 if (failed
&& !retried
) {
2722 } else if (WARN_ON(failed
&& retried
)) {
2726 btrfs_free_path(path
);
2730 static int insert_balance_item(struct btrfs_root
*root
,
2731 struct btrfs_balance_control
*bctl
)
2733 struct btrfs_trans_handle
*trans
;
2734 struct btrfs_balance_item
*item
;
2735 struct btrfs_disk_balance_args disk_bargs
;
2736 struct btrfs_path
*path
;
2737 struct extent_buffer
*leaf
;
2738 struct btrfs_key key
;
2741 path
= btrfs_alloc_path();
2745 trans
= btrfs_start_transaction(root
, 0);
2746 if (IS_ERR(trans
)) {
2747 btrfs_free_path(path
);
2748 return PTR_ERR(trans
);
2751 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2752 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2755 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2760 leaf
= path
->nodes
[0];
2761 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2763 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2765 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2766 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2767 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2768 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2769 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2770 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2772 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2774 btrfs_mark_buffer_dirty(leaf
);
2776 btrfs_free_path(path
);
2777 err
= btrfs_commit_transaction(trans
, root
);
2783 static int del_balance_item(struct btrfs_root
*root
)
2785 struct btrfs_trans_handle
*trans
;
2786 struct btrfs_path
*path
;
2787 struct btrfs_key key
;
2790 path
= btrfs_alloc_path();
2794 trans
= btrfs_start_transaction(root
, 0);
2795 if (IS_ERR(trans
)) {
2796 btrfs_free_path(path
);
2797 return PTR_ERR(trans
);
2800 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2801 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2804 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2812 ret
= btrfs_del_item(trans
, root
, path
);
2814 btrfs_free_path(path
);
2815 err
= btrfs_commit_transaction(trans
, root
);
2822 * This is a heuristic used to reduce the number of chunks balanced on
2823 * resume after balance was interrupted.
2825 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2828 * Turn on soft mode for chunk types that were being converted.
2830 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2831 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2832 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2833 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2834 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2835 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2838 * Turn on usage filter if is not already used. The idea is
2839 * that chunks that we have already balanced should be
2840 * reasonably full. Don't do it for chunks that are being
2841 * converted - that will keep us from relocating unconverted
2842 * (albeit full) chunks.
2844 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2845 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2846 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2847 bctl
->data
.usage
= 90;
2849 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2850 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2851 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2852 bctl
->sys
.usage
= 90;
2854 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2855 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2856 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2857 bctl
->meta
.usage
= 90;
2862 * Should be called with both balance and volume mutexes held to
2863 * serialize other volume operations (add_dev/rm_dev/resize) with
2864 * restriper. Same goes for unset_balance_control.
2866 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2868 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2870 BUG_ON(fs_info
->balance_ctl
);
2872 spin_lock(&fs_info
->balance_lock
);
2873 fs_info
->balance_ctl
= bctl
;
2874 spin_unlock(&fs_info
->balance_lock
);
2877 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2879 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2881 BUG_ON(!fs_info
->balance_ctl
);
2883 spin_lock(&fs_info
->balance_lock
);
2884 fs_info
->balance_ctl
= NULL
;
2885 spin_unlock(&fs_info
->balance_lock
);
2891 * Balance filters. Return 1 if chunk should be filtered out
2892 * (should not be balanced).
2894 static int chunk_profiles_filter(u64 chunk_type
,
2895 struct btrfs_balance_args
*bargs
)
2897 chunk_type
= chunk_to_extended(chunk_type
) &
2898 BTRFS_EXTENDED_PROFILE_MASK
;
2900 if (bargs
->profiles
& chunk_type
)
2906 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2907 struct btrfs_balance_args
*bargs
)
2909 struct btrfs_block_group_cache
*cache
;
2910 u64 chunk_used
, user_thresh
;
2913 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2914 chunk_used
= btrfs_block_group_used(&cache
->item
);
2916 if (bargs
->usage
== 0)
2918 else if (bargs
->usage
> 100)
2919 user_thresh
= cache
->key
.offset
;
2921 user_thresh
= div_factor_fine(cache
->key
.offset
,
2924 if (chunk_used
< user_thresh
)
2927 btrfs_put_block_group(cache
);
2931 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2932 struct btrfs_chunk
*chunk
,
2933 struct btrfs_balance_args
*bargs
)
2935 struct btrfs_stripe
*stripe
;
2936 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2939 for (i
= 0; i
< num_stripes
; i
++) {
2940 stripe
= btrfs_stripe_nr(chunk
, i
);
2941 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2948 /* [pstart, pend) */
2949 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2950 struct btrfs_chunk
*chunk
,
2952 struct btrfs_balance_args
*bargs
)
2954 struct btrfs_stripe
*stripe
;
2955 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2961 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2964 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2965 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
2966 factor
= num_stripes
/ 2;
2967 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
2968 factor
= num_stripes
- 1;
2969 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
2970 factor
= num_stripes
- 2;
2972 factor
= num_stripes
;
2975 for (i
= 0; i
< num_stripes
; i
++) {
2976 stripe
= btrfs_stripe_nr(chunk
, i
);
2977 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2980 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2981 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2982 do_div(stripe_length
, factor
);
2984 if (stripe_offset
< bargs
->pend
&&
2985 stripe_offset
+ stripe_length
> bargs
->pstart
)
2992 /* [vstart, vend) */
2993 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2994 struct btrfs_chunk
*chunk
,
2996 struct btrfs_balance_args
*bargs
)
2998 if (chunk_offset
< bargs
->vend
&&
2999 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
3000 /* at least part of the chunk is inside this vrange */
3006 static int chunk_soft_convert_filter(u64 chunk_type
,
3007 struct btrfs_balance_args
*bargs
)
3009 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3012 chunk_type
= chunk_to_extended(chunk_type
) &
3013 BTRFS_EXTENDED_PROFILE_MASK
;
3015 if (bargs
->target
== chunk_type
)
3021 static int should_balance_chunk(struct btrfs_root
*root
,
3022 struct extent_buffer
*leaf
,
3023 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3025 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
3026 struct btrfs_balance_args
*bargs
= NULL
;
3027 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3030 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3031 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3035 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3036 bargs
= &bctl
->data
;
3037 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3039 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3040 bargs
= &bctl
->meta
;
3042 /* profiles filter */
3043 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3044 chunk_profiles_filter(chunk_type
, bargs
)) {
3049 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3050 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
3055 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3056 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3060 /* drange filter, makes sense only with devid filter */
3061 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3062 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3067 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3068 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3072 /* soft profile changing mode */
3073 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3074 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3079 * limited by count, must be the last filter
3081 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3082 if (bargs
->limit
== 0)
3091 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3093 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3094 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3095 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3096 struct list_head
*devices
;
3097 struct btrfs_device
*device
;
3100 struct btrfs_chunk
*chunk
;
3101 struct btrfs_path
*path
;
3102 struct btrfs_key key
;
3103 struct btrfs_key found_key
;
3104 struct btrfs_trans_handle
*trans
;
3105 struct extent_buffer
*leaf
;
3108 int enospc_errors
= 0;
3109 bool counting
= true;
3110 u64 limit_data
= bctl
->data
.limit
;
3111 u64 limit_meta
= bctl
->meta
.limit
;
3112 u64 limit_sys
= bctl
->sys
.limit
;
3114 /* step one make some room on all the devices */
3115 devices
= &fs_info
->fs_devices
->devices
;
3116 list_for_each_entry(device
, devices
, dev_list
) {
3117 old_size
= device
->total_bytes
;
3118 size_to_free
= div_factor(old_size
, 1);
3119 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
3120 if (!device
->writeable
||
3121 device
->total_bytes
- device
->bytes_used
> size_to_free
||
3122 device
->is_tgtdev_for_dev_replace
)
3125 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3130 trans
= btrfs_start_transaction(dev_root
, 0);
3131 BUG_ON(IS_ERR(trans
));
3133 ret
= btrfs_grow_device(trans
, device
, old_size
);
3136 btrfs_end_transaction(trans
, dev_root
);
3139 /* step two, relocate all the chunks */
3140 path
= btrfs_alloc_path();
3146 /* zero out stat counters */
3147 spin_lock(&fs_info
->balance_lock
);
3148 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3149 spin_unlock(&fs_info
->balance_lock
);
3152 bctl
->data
.limit
= limit_data
;
3153 bctl
->meta
.limit
= limit_meta
;
3154 bctl
->sys
.limit
= limit_sys
;
3156 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3157 key
.offset
= (u64
)-1;
3158 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3161 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3162 atomic_read(&fs_info
->balance_cancel_req
)) {
3167 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3172 * this shouldn't happen, it means the last relocate
3176 BUG(); /* FIXME break ? */
3178 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3179 BTRFS_CHUNK_ITEM_KEY
);
3185 leaf
= path
->nodes
[0];
3186 slot
= path
->slots
[0];
3187 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3189 if (found_key
.objectid
!= key
.objectid
)
3192 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3195 spin_lock(&fs_info
->balance_lock
);
3196 bctl
->stat
.considered
++;
3197 spin_unlock(&fs_info
->balance_lock
);
3200 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
3202 btrfs_release_path(path
);
3207 spin_lock(&fs_info
->balance_lock
);
3208 bctl
->stat
.expected
++;
3209 spin_unlock(&fs_info
->balance_lock
);
3213 ret
= btrfs_relocate_chunk(chunk_root
,
3214 chunk_root
->root_key
.objectid
,
3217 if (ret
&& ret
!= -ENOSPC
)
3219 if (ret
== -ENOSPC
) {
3222 spin_lock(&fs_info
->balance_lock
);
3223 bctl
->stat
.completed
++;
3224 spin_unlock(&fs_info
->balance_lock
);
3227 if (found_key
.offset
== 0)
3229 key
.offset
= found_key
.offset
- 1;
3233 btrfs_release_path(path
);
3238 btrfs_free_path(path
);
3239 if (enospc_errors
) {
3240 btrfs_info(fs_info
, "%d enospc errors during balance",
3250 * alloc_profile_is_valid - see if a given profile is valid and reduced
3251 * @flags: profile to validate
3252 * @extended: if true @flags is treated as an extended profile
3254 static int alloc_profile_is_valid(u64 flags
, int extended
)
3256 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3257 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3259 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3261 /* 1) check that all other bits are zeroed */
3265 /* 2) see if profile is reduced */
3267 return !extended
; /* "0" is valid for usual profiles */
3269 /* true if exactly one bit set */
3270 return (flags
& (flags
- 1)) == 0;
3273 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3275 /* cancel requested || normal exit path */
3276 return atomic_read(&fs_info
->balance_cancel_req
) ||
3277 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3278 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3281 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3285 unset_balance_control(fs_info
);
3286 ret
= del_balance_item(fs_info
->tree_root
);
3288 btrfs_std_error(fs_info
, ret
);
3290 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3294 * Should be called with both balance and volume mutexes held
3296 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3297 struct btrfs_ioctl_balance_args
*bargs
)
3299 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3306 if (btrfs_fs_closing(fs_info
) ||
3307 atomic_read(&fs_info
->balance_pause_req
) ||
3308 atomic_read(&fs_info
->balance_cancel_req
)) {
3313 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3314 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3318 * In case of mixed groups both data and meta should be picked,
3319 * and identical options should be given for both of them.
3321 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3322 if (mixed
&& (bctl
->flags
& allowed
)) {
3323 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3324 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3325 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3326 btrfs_err(fs_info
, "with mixed groups data and "
3327 "metadata balance options must be the same");
3333 num_devices
= fs_info
->fs_devices
->num_devices
;
3334 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3335 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3336 BUG_ON(num_devices
< 1);
3339 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3340 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3341 if (num_devices
== 1)
3342 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3343 else if (num_devices
> 1)
3344 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3345 if (num_devices
> 2)
3346 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3347 if (num_devices
> 3)
3348 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3349 BTRFS_BLOCK_GROUP_RAID6
);
3350 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3351 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3352 (bctl
->data
.target
& ~allowed
))) {
3353 btrfs_err(fs_info
, "unable to start balance with target "
3354 "data profile %llu",
3359 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3360 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3361 (bctl
->meta
.target
& ~allowed
))) {
3363 "unable to start balance with target metadata profile %llu",
3368 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3369 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3370 (bctl
->sys
.target
& ~allowed
))) {
3372 "unable to start balance with target system profile %llu",
3378 /* allow dup'ed data chunks only in mixed mode */
3379 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3380 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3381 btrfs_err(fs_info
, "dup for data is not allowed");
3386 /* allow to reduce meta or sys integrity only if force set */
3387 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3388 BTRFS_BLOCK_GROUP_RAID10
|
3389 BTRFS_BLOCK_GROUP_RAID5
|
3390 BTRFS_BLOCK_GROUP_RAID6
;
3392 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3394 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3395 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3396 !(bctl
->sys
.target
& allowed
)) ||
3397 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3398 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3399 !(bctl
->meta
.target
& allowed
))) {
3400 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3401 btrfs_info(fs_info
, "force reducing metadata integrity");
3403 btrfs_err(fs_info
, "balance will reduce metadata "
3404 "integrity, use force if you want this");
3409 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3411 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3412 int num_tolerated_disk_barrier_failures
;
3413 u64 target
= bctl
->sys
.target
;
3415 num_tolerated_disk_barrier_failures
=
3416 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3417 if (num_tolerated_disk_barrier_failures
> 0 &&
3419 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3420 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3421 num_tolerated_disk_barrier_failures
= 0;
3422 else if (num_tolerated_disk_barrier_failures
> 1 &&
3424 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3425 num_tolerated_disk_barrier_failures
= 1;
3427 fs_info
->num_tolerated_disk_barrier_failures
=
3428 num_tolerated_disk_barrier_failures
;
3431 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3432 if (ret
&& ret
!= -EEXIST
)
3435 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3436 BUG_ON(ret
== -EEXIST
);
3437 set_balance_control(bctl
);
3439 BUG_ON(ret
!= -EEXIST
);
3440 spin_lock(&fs_info
->balance_lock
);
3441 update_balance_args(bctl
);
3442 spin_unlock(&fs_info
->balance_lock
);
3445 atomic_inc(&fs_info
->balance_running
);
3446 mutex_unlock(&fs_info
->balance_mutex
);
3448 ret
= __btrfs_balance(fs_info
);
3450 mutex_lock(&fs_info
->balance_mutex
);
3451 atomic_dec(&fs_info
->balance_running
);
3453 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3454 fs_info
->num_tolerated_disk_barrier_failures
=
3455 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3459 memset(bargs
, 0, sizeof(*bargs
));
3460 update_ioctl_balance_args(fs_info
, 0, bargs
);
3463 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3464 balance_need_close(fs_info
)) {
3465 __cancel_balance(fs_info
);
3468 wake_up(&fs_info
->balance_wait_q
);
3472 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3473 __cancel_balance(fs_info
);
3476 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3481 static int balance_kthread(void *data
)
3483 struct btrfs_fs_info
*fs_info
= data
;
3486 mutex_lock(&fs_info
->volume_mutex
);
3487 mutex_lock(&fs_info
->balance_mutex
);
3489 if (fs_info
->balance_ctl
) {
3490 btrfs_info(fs_info
, "continuing balance");
3491 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3494 mutex_unlock(&fs_info
->balance_mutex
);
3495 mutex_unlock(&fs_info
->volume_mutex
);
3500 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3502 struct task_struct
*tsk
;
3504 spin_lock(&fs_info
->balance_lock
);
3505 if (!fs_info
->balance_ctl
) {
3506 spin_unlock(&fs_info
->balance_lock
);
3509 spin_unlock(&fs_info
->balance_lock
);
3511 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3512 btrfs_info(fs_info
, "force skipping balance");
3516 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3517 return PTR_ERR_OR_ZERO(tsk
);
3520 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3522 struct btrfs_balance_control
*bctl
;
3523 struct btrfs_balance_item
*item
;
3524 struct btrfs_disk_balance_args disk_bargs
;
3525 struct btrfs_path
*path
;
3526 struct extent_buffer
*leaf
;
3527 struct btrfs_key key
;
3530 path
= btrfs_alloc_path();
3534 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3535 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3538 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3541 if (ret
> 0) { /* ret = -ENOENT; */
3546 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3552 leaf
= path
->nodes
[0];
3553 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3555 bctl
->fs_info
= fs_info
;
3556 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3557 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3559 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3560 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3561 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3562 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3563 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3564 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3566 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3568 mutex_lock(&fs_info
->volume_mutex
);
3569 mutex_lock(&fs_info
->balance_mutex
);
3571 set_balance_control(bctl
);
3573 mutex_unlock(&fs_info
->balance_mutex
);
3574 mutex_unlock(&fs_info
->volume_mutex
);
3576 btrfs_free_path(path
);
3580 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3584 mutex_lock(&fs_info
->balance_mutex
);
3585 if (!fs_info
->balance_ctl
) {
3586 mutex_unlock(&fs_info
->balance_mutex
);
3590 if (atomic_read(&fs_info
->balance_running
)) {
3591 atomic_inc(&fs_info
->balance_pause_req
);
3592 mutex_unlock(&fs_info
->balance_mutex
);
3594 wait_event(fs_info
->balance_wait_q
,
3595 atomic_read(&fs_info
->balance_running
) == 0);
3597 mutex_lock(&fs_info
->balance_mutex
);
3598 /* we are good with balance_ctl ripped off from under us */
3599 BUG_ON(atomic_read(&fs_info
->balance_running
));
3600 atomic_dec(&fs_info
->balance_pause_req
);
3605 mutex_unlock(&fs_info
->balance_mutex
);
3609 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3611 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3614 mutex_lock(&fs_info
->balance_mutex
);
3615 if (!fs_info
->balance_ctl
) {
3616 mutex_unlock(&fs_info
->balance_mutex
);
3620 atomic_inc(&fs_info
->balance_cancel_req
);
3622 * if we are running just wait and return, balance item is
3623 * deleted in btrfs_balance in this case
3625 if (atomic_read(&fs_info
->balance_running
)) {
3626 mutex_unlock(&fs_info
->balance_mutex
);
3627 wait_event(fs_info
->balance_wait_q
,
3628 atomic_read(&fs_info
->balance_running
) == 0);
3629 mutex_lock(&fs_info
->balance_mutex
);
3631 /* __cancel_balance needs volume_mutex */
3632 mutex_unlock(&fs_info
->balance_mutex
);
3633 mutex_lock(&fs_info
->volume_mutex
);
3634 mutex_lock(&fs_info
->balance_mutex
);
3636 if (fs_info
->balance_ctl
)
3637 __cancel_balance(fs_info
);
3639 mutex_unlock(&fs_info
->volume_mutex
);
3642 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3643 atomic_dec(&fs_info
->balance_cancel_req
);
3644 mutex_unlock(&fs_info
->balance_mutex
);
3648 static int btrfs_uuid_scan_kthread(void *data
)
3650 struct btrfs_fs_info
*fs_info
= data
;
3651 struct btrfs_root
*root
= fs_info
->tree_root
;
3652 struct btrfs_key key
;
3653 struct btrfs_key max_key
;
3654 struct btrfs_path
*path
= NULL
;
3656 struct extent_buffer
*eb
;
3658 struct btrfs_root_item root_item
;
3660 struct btrfs_trans_handle
*trans
= NULL
;
3662 path
= btrfs_alloc_path();
3669 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3672 max_key
.objectid
= (u64
)-1;
3673 max_key
.type
= BTRFS_ROOT_ITEM_KEY
;
3674 max_key
.offset
= (u64
)-1;
3677 ret
= btrfs_search_forward(root
, &key
, path
, 0);
3684 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
3685 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
3686 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
3687 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
3690 eb
= path
->nodes
[0];
3691 slot
= path
->slots
[0];
3692 item_size
= btrfs_item_size_nr(eb
, slot
);
3693 if (item_size
< sizeof(root_item
))
3696 read_extent_buffer(eb
, &root_item
,
3697 btrfs_item_ptr_offset(eb
, slot
),
3698 (int)sizeof(root_item
));
3699 if (btrfs_root_refs(&root_item
) == 0)
3702 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
3703 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3707 btrfs_release_path(path
);
3709 * 1 - subvol uuid item
3710 * 1 - received_subvol uuid item
3712 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
3713 if (IS_ERR(trans
)) {
3714 ret
= PTR_ERR(trans
);
3722 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
3723 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3725 BTRFS_UUID_KEY_SUBVOL
,
3728 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3734 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3735 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3736 root_item
.received_uuid
,
3737 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
3740 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3748 ret
= btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3754 btrfs_release_path(path
);
3755 if (key
.offset
< (u64
)-1) {
3757 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
3759 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3760 } else if (key
.objectid
< (u64
)-1) {
3762 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3771 btrfs_free_path(path
);
3772 if (trans
&& !IS_ERR(trans
))
3773 btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3775 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
3777 fs_info
->update_uuid_tree_gen
= 1;
3778 up(&fs_info
->uuid_tree_rescan_sem
);
3783 * Callback for btrfs_uuid_tree_iterate().
3785 * 0 check succeeded, the entry is not outdated.
3786 * < 0 if an error occured.
3787 * > 0 if the check failed, which means the caller shall remove the entry.
3789 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
3790 u8
*uuid
, u8 type
, u64 subid
)
3792 struct btrfs_key key
;
3794 struct btrfs_root
*subvol_root
;
3796 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
3797 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
3800 key
.objectid
= subid
;
3801 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3802 key
.offset
= (u64
)-1;
3803 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
3804 if (IS_ERR(subvol_root
)) {
3805 ret
= PTR_ERR(subvol_root
);
3812 case BTRFS_UUID_KEY_SUBVOL
:
3813 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
3816 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
3817 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
3827 static int btrfs_uuid_rescan_kthread(void *data
)
3829 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
3833 * 1st step is to iterate through the existing UUID tree and
3834 * to delete all entries that contain outdated data.
3835 * 2nd step is to add all missing entries to the UUID tree.
3837 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
3839 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
3840 up(&fs_info
->uuid_tree_rescan_sem
);
3843 return btrfs_uuid_scan_kthread(data
);
3846 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
3848 struct btrfs_trans_handle
*trans
;
3849 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
3850 struct btrfs_root
*uuid_root
;
3851 struct task_struct
*task
;
3858 trans
= btrfs_start_transaction(tree_root
, 2);
3860 return PTR_ERR(trans
);
3862 uuid_root
= btrfs_create_tree(trans
, fs_info
,
3863 BTRFS_UUID_TREE_OBJECTID
);
3864 if (IS_ERR(uuid_root
)) {
3865 btrfs_abort_transaction(trans
, tree_root
,
3866 PTR_ERR(uuid_root
));
3867 return PTR_ERR(uuid_root
);
3870 fs_info
->uuid_root
= uuid_root
;
3872 ret
= btrfs_commit_transaction(trans
, tree_root
);
3876 down(&fs_info
->uuid_tree_rescan_sem
);
3877 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
3879 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3880 btrfs_warn(fs_info
, "failed to start uuid_scan task");
3881 up(&fs_info
->uuid_tree_rescan_sem
);
3882 return PTR_ERR(task
);
3888 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
3890 struct task_struct
*task
;
3892 down(&fs_info
->uuid_tree_rescan_sem
);
3893 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
3895 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3896 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
3897 up(&fs_info
->uuid_tree_rescan_sem
);
3898 return PTR_ERR(task
);
3905 * shrinking a device means finding all of the device extents past
3906 * the new size, and then following the back refs to the chunks.
3907 * The chunk relocation code actually frees the device extent
3909 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3911 struct btrfs_trans_handle
*trans
;
3912 struct btrfs_root
*root
= device
->dev_root
;
3913 struct btrfs_dev_extent
*dev_extent
= NULL
;
3914 struct btrfs_path
*path
;
3922 bool retried
= false;
3923 struct extent_buffer
*l
;
3924 struct btrfs_key key
;
3925 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3926 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3927 u64 old_size
= device
->total_bytes
;
3928 u64 diff
= device
->total_bytes
- new_size
;
3930 if (device
->is_tgtdev_for_dev_replace
)
3933 path
= btrfs_alloc_path();
3941 device
->total_bytes
= new_size
;
3942 if (device
->writeable
) {
3943 device
->fs_devices
->total_rw_bytes
-= diff
;
3944 spin_lock(&root
->fs_info
->free_chunk_lock
);
3945 root
->fs_info
->free_chunk_space
-= diff
;
3946 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3948 unlock_chunks(root
);
3951 key
.objectid
= device
->devid
;
3952 key
.offset
= (u64
)-1;
3953 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3956 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3960 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3965 btrfs_release_path(path
);
3970 slot
= path
->slots
[0];
3971 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3973 if (key
.objectid
!= device
->devid
) {
3974 btrfs_release_path(path
);
3978 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3979 length
= btrfs_dev_extent_length(l
, dev_extent
);
3981 if (key
.offset
+ length
<= new_size
) {
3982 btrfs_release_path(path
);
3986 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3987 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3988 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3989 btrfs_release_path(path
);
3991 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3993 if (ret
&& ret
!= -ENOSPC
)
3997 } while (key
.offset
-- > 0);
3999 if (failed
&& !retried
) {
4003 } else if (failed
&& retried
) {
4007 device
->total_bytes
= old_size
;
4008 if (device
->writeable
)
4009 device
->fs_devices
->total_rw_bytes
+= diff
;
4010 spin_lock(&root
->fs_info
->free_chunk_lock
);
4011 root
->fs_info
->free_chunk_space
+= diff
;
4012 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4013 unlock_chunks(root
);
4017 /* Shrinking succeeded, else we would be at "done". */
4018 trans
= btrfs_start_transaction(root
, 0);
4019 if (IS_ERR(trans
)) {
4020 ret
= PTR_ERR(trans
);
4025 device
->disk_total_bytes
= new_size
;
4026 if (list_empty(&device
->resized_list
))
4027 list_add_tail(&device
->resized_list
,
4028 &root
->fs_info
->fs_devices
->resized_devices
);
4030 /* Now btrfs_update_device() will change the on-disk size. */
4031 ret
= btrfs_update_device(trans
, device
);
4033 unlock_chunks(root
);
4034 btrfs_end_transaction(trans
, root
);
4037 WARN_ON(diff
> old_total
);
4038 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
4039 unlock_chunks(root
);
4040 btrfs_end_transaction(trans
, root
);
4042 btrfs_free_path(path
);
4046 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
4047 struct btrfs_key
*key
,
4048 struct btrfs_chunk
*chunk
, int item_size
)
4050 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4051 struct btrfs_disk_key disk_key
;
4055 array_size
= btrfs_super_sys_array_size(super_copy
);
4056 if (array_size
+ item_size
+ sizeof(disk_key
)
4057 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
4060 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4061 btrfs_cpu_key_to_disk(&disk_key
, key
);
4062 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4063 ptr
+= sizeof(disk_key
);
4064 memcpy(ptr
, chunk
, item_size
);
4065 item_size
+= sizeof(disk_key
);
4066 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4071 * sort the devices in descending order by max_avail, total_avail
4073 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4075 const struct btrfs_device_info
*di_a
= a
;
4076 const struct btrfs_device_info
*di_b
= b
;
4078 if (di_a
->max_avail
> di_b
->max_avail
)
4080 if (di_a
->max_avail
< di_b
->max_avail
)
4082 if (di_a
->total_avail
> di_b
->total_avail
)
4084 if (di_a
->total_avail
< di_b
->total_avail
)
4089 static struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
4090 [BTRFS_RAID_RAID10
] = {
4093 .devs_max
= 0, /* 0 == as many as possible */
4095 .devs_increment
= 2,
4098 [BTRFS_RAID_RAID1
] = {
4103 .devs_increment
= 2,
4106 [BTRFS_RAID_DUP
] = {
4111 .devs_increment
= 1,
4114 [BTRFS_RAID_RAID0
] = {
4119 .devs_increment
= 1,
4122 [BTRFS_RAID_SINGLE
] = {
4127 .devs_increment
= 1,
4130 [BTRFS_RAID_RAID5
] = {
4135 .devs_increment
= 1,
4138 [BTRFS_RAID_RAID6
] = {
4143 .devs_increment
= 1,
4148 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
4150 /* TODO allow them to set a preferred stripe size */
4154 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4156 if (!(type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)))
4159 btrfs_set_fs_incompat(info
, RAID56
);
4162 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4163 - sizeof(struct btrfs_item) \
4164 - sizeof(struct btrfs_chunk)) \
4165 / sizeof(struct btrfs_stripe) + 1)
4167 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4168 - 2 * sizeof(struct btrfs_disk_key) \
4169 - 2 * sizeof(struct btrfs_chunk)) \
4170 / sizeof(struct btrfs_stripe) + 1)
4172 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4173 struct btrfs_root
*extent_root
, u64 start
,
4176 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
4177 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4178 struct list_head
*cur
;
4179 struct map_lookup
*map
= NULL
;
4180 struct extent_map_tree
*em_tree
;
4181 struct extent_map
*em
;
4182 struct btrfs_device_info
*devices_info
= NULL
;
4184 int num_stripes
; /* total number of stripes to allocate */
4185 int data_stripes
; /* number of stripes that count for
4187 int sub_stripes
; /* sub_stripes info for map */
4188 int dev_stripes
; /* stripes per dev */
4189 int devs_max
; /* max devs to use */
4190 int devs_min
; /* min devs needed */
4191 int devs_increment
; /* ndevs has to be a multiple of this */
4192 int ncopies
; /* how many copies to data has */
4194 u64 max_stripe_size
;
4198 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
4204 BUG_ON(!alloc_profile_is_valid(type
, 0));
4206 if (list_empty(&fs_devices
->alloc_list
))
4209 index
= __get_raid_index(type
);
4211 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4212 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4213 devs_max
= btrfs_raid_array
[index
].devs_max
;
4214 devs_min
= btrfs_raid_array
[index
].devs_min
;
4215 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4216 ncopies
= btrfs_raid_array
[index
].ncopies
;
4218 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4219 max_stripe_size
= 1024 * 1024 * 1024;
4220 max_chunk_size
= 10 * max_stripe_size
;
4222 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4223 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4224 /* for larger filesystems, use larger metadata chunks */
4225 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
4226 max_stripe_size
= 1024 * 1024 * 1024;
4228 max_stripe_size
= 256 * 1024 * 1024;
4229 max_chunk_size
= max_stripe_size
;
4231 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4232 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4233 max_stripe_size
= 32 * 1024 * 1024;
4234 max_chunk_size
= 2 * max_stripe_size
;
4236 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4238 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4243 /* we don't want a chunk larger than 10% of writeable space */
4244 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4247 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
4252 cur
= fs_devices
->alloc_list
.next
;
4255 * in the first pass through the devices list, we gather information
4256 * about the available holes on each device.
4259 while (cur
!= &fs_devices
->alloc_list
) {
4260 struct btrfs_device
*device
;
4264 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
4268 if (!device
->writeable
) {
4270 "BTRFS: read-only device in alloc_list\n");
4274 if (!device
->in_fs_metadata
||
4275 device
->is_tgtdev_for_dev_replace
)
4278 if (device
->total_bytes
> device
->bytes_used
)
4279 total_avail
= device
->total_bytes
- device
->bytes_used
;
4283 /* If there is no space on this device, skip it. */
4284 if (total_avail
== 0)
4287 ret
= find_free_dev_extent(trans
, device
,
4288 max_stripe_size
* dev_stripes
,
4289 &dev_offset
, &max_avail
);
4290 if (ret
&& ret
!= -ENOSPC
)
4294 max_avail
= max_stripe_size
* dev_stripes
;
4296 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
4299 if (ndevs
== fs_devices
->rw_devices
) {
4300 WARN(1, "%s: found more than %llu devices\n",
4301 __func__
, fs_devices
->rw_devices
);
4304 devices_info
[ndevs
].dev_offset
= dev_offset
;
4305 devices_info
[ndevs
].max_avail
= max_avail
;
4306 devices_info
[ndevs
].total_avail
= total_avail
;
4307 devices_info
[ndevs
].dev
= device
;
4312 * now sort the devices by hole size / available space
4314 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4315 btrfs_cmp_device_info
, NULL
);
4317 /* round down to number of usable stripes */
4318 ndevs
-= ndevs
% devs_increment
;
4320 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
4325 if (devs_max
&& ndevs
> devs_max
)
4328 * the primary goal is to maximize the number of stripes, so use as many
4329 * devices as possible, even if the stripes are not maximum sized.
4331 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4332 num_stripes
= ndevs
* dev_stripes
;
4335 * this will have to be fixed for RAID1 and RAID10 over
4338 data_stripes
= num_stripes
/ ncopies
;
4340 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
4341 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
4342 btrfs_super_stripesize(info
->super_copy
));
4343 data_stripes
= num_stripes
- 1;
4345 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
4346 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
4347 btrfs_super_stripesize(info
->super_copy
));
4348 data_stripes
= num_stripes
- 2;
4352 * Use the number of data stripes to figure out how big this chunk
4353 * is really going to be in terms of logical address space,
4354 * and compare that answer with the max chunk size
4356 if (stripe_size
* data_stripes
> max_chunk_size
) {
4357 u64 mask
= (1ULL << 24) - 1;
4358 stripe_size
= max_chunk_size
;
4359 do_div(stripe_size
, data_stripes
);
4361 /* bump the answer up to a 16MB boundary */
4362 stripe_size
= (stripe_size
+ mask
) & ~mask
;
4364 /* but don't go higher than the limits we found
4365 * while searching for free extents
4367 if (stripe_size
> devices_info
[ndevs
-1].max_avail
)
4368 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4371 do_div(stripe_size
, dev_stripes
);
4373 /* align to BTRFS_STRIPE_LEN */
4374 do_div(stripe_size
, raid_stripe_len
);
4375 stripe_size
*= raid_stripe_len
;
4377 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4382 map
->num_stripes
= num_stripes
;
4384 for (i
= 0; i
< ndevs
; ++i
) {
4385 for (j
= 0; j
< dev_stripes
; ++j
) {
4386 int s
= i
* dev_stripes
+ j
;
4387 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4388 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4392 map
->sector_size
= extent_root
->sectorsize
;
4393 map
->stripe_len
= raid_stripe_len
;
4394 map
->io_align
= raid_stripe_len
;
4395 map
->io_width
= raid_stripe_len
;
4397 map
->sub_stripes
= sub_stripes
;
4399 num_bytes
= stripe_size
* data_stripes
;
4401 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
4403 em
= alloc_extent_map();
4409 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4410 em
->bdev
= (struct block_device
*)map
;
4412 em
->len
= num_bytes
;
4413 em
->block_start
= 0;
4414 em
->block_len
= em
->len
;
4415 em
->orig_block_len
= stripe_size
;
4417 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4418 write_lock(&em_tree
->lock
);
4419 ret
= add_extent_mapping(em_tree
, em
, 0);
4421 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4422 atomic_inc(&em
->refs
);
4424 write_unlock(&em_tree
->lock
);
4426 free_extent_map(em
);
4430 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
4431 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4434 goto error_del_extent
;
4436 free_extent_map(em
);
4437 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
4439 kfree(devices_info
);
4443 write_lock(&em_tree
->lock
);
4444 remove_extent_mapping(em_tree
, em
);
4445 write_unlock(&em_tree
->lock
);
4447 /* One for our allocation */
4448 free_extent_map(em
);
4449 /* One for the tree reference */
4450 free_extent_map(em
);
4452 kfree(devices_info
);
4456 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4457 struct btrfs_root
*extent_root
,
4458 u64 chunk_offset
, u64 chunk_size
)
4460 struct btrfs_key key
;
4461 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
4462 struct btrfs_device
*device
;
4463 struct btrfs_chunk
*chunk
;
4464 struct btrfs_stripe
*stripe
;
4465 struct extent_map_tree
*em_tree
;
4466 struct extent_map
*em
;
4467 struct map_lookup
*map
;
4474 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4475 read_lock(&em_tree
->lock
);
4476 em
= lookup_extent_mapping(em_tree
, chunk_offset
, chunk_size
);
4477 read_unlock(&em_tree
->lock
);
4480 btrfs_crit(extent_root
->fs_info
, "unable to find logical "
4481 "%Lu len %Lu", chunk_offset
, chunk_size
);
4485 if (em
->start
!= chunk_offset
|| em
->len
!= chunk_size
) {
4486 btrfs_crit(extent_root
->fs_info
, "found a bad mapping, wanted"
4487 " %Lu-%Lu, found %Lu-%Lu", chunk_offset
,
4488 chunk_size
, em
->start
, em
->len
);
4489 free_extent_map(em
);
4493 map
= (struct map_lookup
*)em
->bdev
;
4494 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4495 stripe_size
= em
->orig_block_len
;
4497 chunk
= kzalloc(item_size
, GFP_NOFS
);
4503 for (i
= 0; i
< map
->num_stripes
; i
++) {
4504 device
= map
->stripes
[i
].dev
;
4505 dev_offset
= map
->stripes
[i
].physical
;
4507 device
->bytes_used
+= stripe_size
;
4508 ret
= btrfs_update_device(trans
, device
);
4511 ret
= btrfs_alloc_dev_extent(trans
, device
,
4512 chunk_root
->root_key
.objectid
,
4513 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4514 chunk_offset
, dev_offset
,
4520 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
4521 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
4523 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
4525 stripe
= &chunk
->stripe
;
4526 for (i
= 0; i
< map
->num_stripes
; i
++) {
4527 device
= map
->stripes
[i
].dev
;
4528 dev_offset
= map
->stripes
[i
].physical
;
4530 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4531 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4532 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4536 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4537 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4538 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4539 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4540 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4541 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4542 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4543 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
4544 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4546 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4547 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4548 key
.offset
= chunk_offset
;
4550 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4551 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4553 * TODO: Cleanup of inserted chunk root in case of
4556 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
4562 free_extent_map(em
);
4567 * Chunk allocation falls into two parts. The first part does works
4568 * that make the new allocated chunk useable, but not do any operation
4569 * that modifies the chunk tree. The second part does the works that
4570 * require modifying the chunk tree. This division is important for the
4571 * bootstrap process of adding storage to a seed btrfs.
4573 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4574 struct btrfs_root
*extent_root
, u64 type
)
4578 chunk_offset
= find_next_chunk(extent_root
->fs_info
);
4579 return __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
, type
);
4582 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4583 struct btrfs_root
*root
,
4584 struct btrfs_device
*device
)
4587 u64 sys_chunk_offset
;
4589 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4590 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4593 chunk_offset
= find_next_chunk(fs_info
);
4594 alloc_profile
= btrfs_get_alloc_profile(extent_root
, 0);
4595 ret
= __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
,
4600 sys_chunk_offset
= find_next_chunk(root
->fs_info
);
4601 alloc_profile
= btrfs_get_alloc_profile(fs_info
->chunk_root
, 0);
4602 ret
= __btrfs_alloc_chunk(trans
, extent_root
, sys_chunk_offset
,
4605 btrfs_abort_transaction(trans
, root
, ret
);
4609 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
4611 btrfs_abort_transaction(trans
, root
, ret
);
4616 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
4620 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4621 BTRFS_BLOCK_GROUP_RAID10
|
4622 BTRFS_BLOCK_GROUP_RAID5
|
4623 BTRFS_BLOCK_GROUP_DUP
)) {
4625 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
4634 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4636 struct extent_map
*em
;
4637 struct map_lookup
*map
;
4638 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4643 read_lock(&map_tree
->map_tree
.lock
);
4644 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4645 read_unlock(&map_tree
->map_tree
.lock
);
4649 map
= (struct map_lookup
*)em
->bdev
;
4650 for (i
= 0; i
< map
->num_stripes
; i
++) {
4651 if (map
->stripes
[i
].dev
->missing
) {
4656 if (!map
->stripes
[i
].dev
->writeable
) {
4663 * If the number of missing devices is larger than max errors,
4664 * we can not write the data into that chunk successfully, so
4667 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
4670 free_extent_map(em
);
4674 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4676 extent_map_tree_init(&tree
->map_tree
);
4679 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4681 struct extent_map
*em
;
4684 write_lock(&tree
->map_tree
.lock
);
4685 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4687 remove_extent_mapping(&tree
->map_tree
, em
);
4688 write_unlock(&tree
->map_tree
.lock
);
4692 free_extent_map(em
);
4693 /* once for the tree */
4694 free_extent_map(em
);
4698 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4700 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4701 struct extent_map
*em
;
4702 struct map_lookup
*map
;
4703 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4706 read_lock(&em_tree
->lock
);
4707 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4708 read_unlock(&em_tree
->lock
);
4711 * We could return errors for these cases, but that could get ugly and
4712 * we'd probably do the same thing which is just not do anything else
4713 * and exit, so return 1 so the callers don't try to use other copies.
4716 btrfs_crit(fs_info
, "No mapping for %Lu-%Lu", logical
,
4721 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4722 btrfs_crit(fs_info
, "Invalid mapping for %Lu-%Lu, got "
4723 "%Lu-%Lu", logical
, logical
+len
, em
->start
,
4724 em
->start
+ em
->len
);
4725 free_extent_map(em
);
4729 map
= (struct map_lookup
*)em
->bdev
;
4730 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4731 ret
= map
->num_stripes
;
4732 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4733 ret
= map
->sub_stripes
;
4734 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4736 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4740 free_extent_map(em
);
4742 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4743 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4745 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4750 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4751 struct btrfs_mapping_tree
*map_tree
,
4754 struct extent_map
*em
;
4755 struct map_lookup
*map
;
4756 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4757 unsigned long len
= root
->sectorsize
;
4759 read_lock(&em_tree
->lock
);
4760 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4761 read_unlock(&em_tree
->lock
);
4764 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4765 map
= (struct map_lookup
*)em
->bdev
;
4766 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4767 BTRFS_BLOCK_GROUP_RAID6
)) {
4768 len
= map
->stripe_len
* nr_data_stripes(map
);
4770 free_extent_map(em
);
4774 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4775 u64 logical
, u64 len
, int mirror_num
)
4777 struct extent_map
*em
;
4778 struct map_lookup
*map
;
4779 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4782 read_lock(&em_tree
->lock
);
4783 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4784 read_unlock(&em_tree
->lock
);
4787 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4788 map
= (struct map_lookup
*)em
->bdev
;
4789 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4790 BTRFS_BLOCK_GROUP_RAID6
))
4792 free_extent_map(em
);
4796 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4797 struct map_lookup
*map
, int first
, int num
,
4798 int optimal
, int dev_replace_is_ongoing
)
4802 struct btrfs_device
*srcdev
;
4804 if (dev_replace_is_ongoing
&&
4805 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4806 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4807 srcdev
= fs_info
->dev_replace
.srcdev
;
4812 * try to avoid the drive that is the source drive for a
4813 * dev-replace procedure, only choose it if no other non-missing
4814 * mirror is available
4816 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4817 if (map
->stripes
[optimal
].dev
->bdev
&&
4818 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4820 for (i
= first
; i
< first
+ num
; i
++) {
4821 if (map
->stripes
[i
].dev
->bdev
&&
4822 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4827 /* we couldn't find one that doesn't fail. Just return something
4828 * and the io error handling code will clean up eventually
4833 static inline int parity_smaller(u64 a
, u64 b
)
4838 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4839 static void sort_parity_stripes(struct btrfs_bio
*bbio
, u64
*raid_map
)
4841 struct btrfs_bio_stripe s
;
4848 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
4849 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
4850 s
= bbio
->stripes
[i
];
4852 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
4853 raid_map
[i
] = raid_map
[i
+1];
4854 bbio
->stripes
[i
+1] = s
;
4862 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4863 u64 logical
, u64
*length
,
4864 struct btrfs_bio
**bbio_ret
,
4865 int mirror_num
, u64
**raid_map_ret
)
4867 struct extent_map
*em
;
4868 struct map_lookup
*map
;
4869 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4870 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4873 u64 stripe_end_offset
;
4878 u64
*raid_map
= NULL
;
4884 struct btrfs_bio
*bbio
= NULL
;
4885 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4886 int dev_replace_is_ongoing
= 0;
4887 int num_alloc_stripes
;
4888 int patch_the_first_stripe_for_dev_replace
= 0;
4889 u64 physical_to_patch_in_first_stripe
= 0;
4890 u64 raid56_full_stripe_start
= (u64
)-1;
4892 read_lock(&em_tree
->lock
);
4893 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4894 read_unlock(&em_tree
->lock
);
4897 btrfs_crit(fs_info
, "unable to find logical %llu len %llu",
4902 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4903 btrfs_crit(fs_info
, "found a bad mapping, wanted %Lu, "
4904 "found %Lu-%Lu", logical
, em
->start
,
4905 em
->start
+ em
->len
);
4906 free_extent_map(em
);
4910 map
= (struct map_lookup
*)em
->bdev
;
4911 offset
= logical
- em
->start
;
4913 stripe_len
= map
->stripe_len
;
4916 * stripe_nr counts the total number of stripes we have to stride
4917 * to get to this block
4919 do_div(stripe_nr
, stripe_len
);
4921 stripe_offset
= stripe_nr
* stripe_len
;
4922 BUG_ON(offset
< stripe_offset
);
4924 /* stripe_offset is the offset of this block in its stripe*/
4925 stripe_offset
= offset
- stripe_offset
;
4927 /* if we're here for raid56, we need to know the stripe aligned start */
4928 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4929 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
4930 raid56_full_stripe_start
= offset
;
4932 /* allow a write of a full stripe, but make sure we don't
4933 * allow straddling of stripes
4935 do_div(raid56_full_stripe_start
, full_stripe_len
);
4936 raid56_full_stripe_start
*= full_stripe_len
;
4939 if (rw
& REQ_DISCARD
) {
4940 /* we don't discard raid56 yet */
4942 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4946 *length
= min_t(u64
, em
->len
- offset
, *length
);
4947 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4949 /* For writes to RAID[56], allow a full stripeset across all disks.
4950 For other RAID types and for RAID[56] reads, just allow a single
4951 stripe (on a single disk). */
4952 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
) &&
4954 max_len
= stripe_len
* nr_data_stripes(map
) -
4955 (offset
- raid56_full_stripe_start
);
4957 /* we limit the length of each bio to what fits in a stripe */
4958 max_len
= stripe_len
- stripe_offset
;
4960 *length
= min_t(u64
, em
->len
- offset
, max_len
);
4962 *length
= em
->len
- offset
;
4965 /* This is for when we're called from btrfs_merge_bio_hook() and all
4966 it cares about is the length */
4970 btrfs_dev_replace_lock(dev_replace
);
4971 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4972 if (!dev_replace_is_ongoing
)
4973 btrfs_dev_replace_unlock(dev_replace
);
4975 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4976 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4977 dev_replace
->tgtdev
!= NULL
) {
4979 * in dev-replace case, for repair case (that's the only
4980 * case where the mirror is selected explicitly when
4981 * calling btrfs_map_block), blocks left of the left cursor
4982 * can also be read from the target drive.
4983 * For REQ_GET_READ_MIRRORS, the target drive is added as
4984 * the last one to the array of stripes. For READ, it also
4985 * needs to be supported using the same mirror number.
4986 * If the requested block is not left of the left cursor,
4987 * EIO is returned. This can happen because btrfs_num_copies()
4988 * returns one more in the dev-replace case.
4990 u64 tmp_length
= *length
;
4991 struct btrfs_bio
*tmp_bbio
= NULL
;
4992 int tmp_num_stripes
;
4993 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4994 int index_srcdev
= 0;
4996 u64 physical_of_found
= 0;
4998 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4999 logical
, &tmp_length
, &tmp_bbio
, 0, NULL
);
5001 WARN_ON(tmp_bbio
!= NULL
);
5005 tmp_num_stripes
= tmp_bbio
->num_stripes
;
5006 if (mirror_num
> tmp_num_stripes
) {
5008 * REQ_GET_READ_MIRRORS does not contain this
5009 * mirror, that means that the requested area
5010 * is not left of the left cursor
5018 * process the rest of the function using the mirror_num
5019 * of the source drive. Therefore look it up first.
5020 * At the end, patch the device pointer to the one of the
5023 for (i
= 0; i
< tmp_num_stripes
; i
++) {
5024 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5026 * In case of DUP, in order to keep it
5027 * simple, only add the mirror with the
5028 * lowest physical address
5031 physical_of_found
<=
5032 tmp_bbio
->stripes
[i
].physical
)
5037 tmp_bbio
->stripes
[i
].physical
;
5042 mirror_num
= index_srcdev
+ 1;
5043 patch_the_first_stripe_for_dev_replace
= 1;
5044 physical_to_patch_in_first_stripe
= physical_of_found
;
5053 } else if (mirror_num
> map
->num_stripes
) {
5059 stripe_nr_orig
= stripe_nr
;
5060 stripe_nr_end
= ALIGN(offset
+ *length
, map
->stripe_len
);
5061 do_div(stripe_nr_end
, map
->stripe_len
);
5062 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5065 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5066 if (rw
& REQ_DISCARD
)
5067 num_stripes
= min_t(u64
, map
->num_stripes
,
5068 stripe_nr_end
- stripe_nr_orig
);
5069 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5070 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
5071 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
5072 num_stripes
= map
->num_stripes
;
5073 else if (mirror_num
)
5074 stripe_index
= mirror_num
- 1;
5076 stripe_index
= find_live_mirror(fs_info
, map
, 0,
5078 current
->pid
% map
->num_stripes
,
5079 dev_replace_is_ongoing
);
5080 mirror_num
= stripe_index
+ 1;
5083 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
5084 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
5085 num_stripes
= map
->num_stripes
;
5086 } else if (mirror_num
) {
5087 stripe_index
= mirror_num
- 1;
5092 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5093 int factor
= map
->num_stripes
/ map
->sub_stripes
;
5095 stripe_index
= do_div(stripe_nr
, factor
);
5096 stripe_index
*= map
->sub_stripes
;
5098 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5099 num_stripes
= map
->sub_stripes
;
5100 else if (rw
& REQ_DISCARD
)
5101 num_stripes
= min_t(u64
, map
->sub_stripes
*
5102 (stripe_nr_end
- stripe_nr_orig
),
5104 else if (mirror_num
)
5105 stripe_index
+= mirror_num
- 1;
5107 int old_stripe_index
= stripe_index
;
5108 stripe_index
= find_live_mirror(fs_info
, map
,
5110 map
->sub_stripes
, stripe_index
+
5111 current
->pid
% map
->sub_stripes
,
5112 dev_replace_is_ongoing
);
5113 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5116 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5117 BTRFS_BLOCK_GROUP_RAID6
)) {
5120 if (bbio_ret
&& ((rw
& REQ_WRITE
) || mirror_num
> 1)
5124 /* push stripe_nr back to the start of the full stripe */
5125 stripe_nr
= raid56_full_stripe_start
;
5126 do_div(stripe_nr
, stripe_len
);
5128 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5130 /* RAID[56] write or recovery. Return all stripes */
5131 num_stripes
= map
->num_stripes
;
5132 max_errors
= nr_parity_stripes(map
);
5134 raid_map
= kmalloc_array(num_stripes
, sizeof(u64
),
5141 /* Work out the disk rotation on this stripe-set */
5143 rot
= do_div(tmp
, num_stripes
);
5145 /* Fill in the logical address of each stripe */
5146 tmp
= stripe_nr
* nr_data_stripes(map
);
5147 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5148 raid_map
[(i
+rot
) % num_stripes
] =
5149 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5151 raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5152 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5153 raid_map
[(i
+rot
+1) % num_stripes
] =
5156 *length
= map
->stripe_len
;
5161 * Mirror #0 or #1 means the original data block.
5162 * Mirror #2 is RAID5 parity block.
5163 * Mirror #3 is RAID6 Q block.
5165 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5167 stripe_index
= nr_data_stripes(map
) +
5170 /* We distribute the parity blocks across stripes */
5171 tmp
= stripe_nr
+ stripe_index
;
5172 stripe_index
= do_div(tmp
, map
->num_stripes
);
5176 * after this do_div call, stripe_nr is the number of stripes
5177 * on this device we have to walk to find the data, and
5178 * stripe_index is the number of our device in the stripe array
5180 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5181 mirror_num
= stripe_index
+ 1;
5183 BUG_ON(stripe_index
>= map
->num_stripes
);
5185 num_alloc_stripes
= num_stripes
;
5186 if (dev_replace_is_ongoing
) {
5187 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
5188 num_alloc_stripes
<<= 1;
5189 if (rw
& REQ_GET_READ_MIRRORS
)
5190 num_alloc_stripes
++;
5192 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
5198 atomic_set(&bbio
->error
, 0);
5200 if (rw
& REQ_DISCARD
) {
5202 int sub_stripes
= 0;
5203 u64 stripes_per_dev
= 0;
5204 u32 remaining_stripes
= 0;
5205 u32 last_stripe
= 0;
5208 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
5209 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5212 sub_stripes
= map
->sub_stripes
;
5214 factor
= map
->num_stripes
/ sub_stripes
;
5215 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
5218 &remaining_stripes
);
5219 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5220 last_stripe
*= sub_stripes
;
5223 for (i
= 0; i
< num_stripes
; i
++) {
5224 bbio
->stripes
[i
].physical
=
5225 map
->stripes
[stripe_index
].physical
+
5226 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5227 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5229 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5230 BTRFS_BLOCK_GROUP_RAID10
)) {
5231 bbio
->stripes
[i
].length
= stripes_per_dev
*
5234 if (i
/ sub_stripes
< remaining_stripes
)
5235 bbio
->stripes
[i
].length
+=
5239 * Special for the first stripe and
5242 * |-------|...|-------|
5246 if (i
< sub_stripes
)
5247 bbio
->stripes
[i
].length
-=
5250 if (stripe_index
>= last_stripe
&&
5251 stripe_index
<= (last_stripe
+
5253 bbio
->stripes
[i
].length
-=
5256 if (i
== sub_stripes
- 1)
5259 bbio
->stripes
[i
].length
= *length
;
5262 if (stripe_index
== map
->num_stripes
) {
5263 /* This could only happen for RAID0/10 */
5269 for (i
= 0; i
< num_stripes
; i
++) {
5270 bbio
->stripes
[i
].physical
=
5271 map
->stripes
[stripe_index
].physical
+
5273 stripe_nr
* map
->stripe_len
;
5274 bbio
->stripes
[i
].dev
=
5275 map
->stripes
[stripe_index
].dev
;
5280 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5281 max_errors
= btrfs_chunk_max_errors(map
);
5283 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
5284 dev_replace
->tgtdev
!= NULL
) {
5285 int index_where_to_add
;
5286 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5289 * duplicate the write operations while the dev replace
5290 * procedure is running. Since the copying of the old disk
5291 * to the new disk takes place at run time while the
5292 * filesystem is mounted writable, the regular write
5293 * operations to the old disk have to be duplicated to go
5294 * to the new disk as well.
5295 * Note that device->missing is handled by the caller, and
5296 * that the write to the old disk is already set up in the
5299 index_where_to_add
= num_stripes
;
5300 for (i
= 0; i
< num_stripes
; i
++) {
5301 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5302 /* write to new disk, too */
5303 struct btrfs_bio_stripe
*new =
5304 bbio
->stripes
+ index_where_to_add
;
5305 struct btrfs_bio_stripe
*old
=
5308 new->physical
= old
->physical
;
5309 new->length
= old
->length
;
5310 new->dev
= dev_replace
->tgtdev
;
5311 index_where_to_add
++;
5315 num_stripes
= index_where_to_add
;
5316 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
5317 dev_replace
->tgtdev
!= NULL
) {
5318 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5319 int index_srcdev
= 0;
5321 u64 physical_of_found
= 0;
5324 * During the dev-replace procedure, the target drive can
5325 * also be used to read data in case it is needed to repair
5326 * a corrupt block elsewhere. This is possible if the
5327 * requested area is left of the left cursor. In this area,
5328 * the target drive is a full copy of the source drive.
5330 for (i
= 0; i
< num_stripes
; i
++) {
5331 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5333 * In case of DUP, in order to keep it
5334 * simple, only add the mirror with the
5335 * lowest physical address
5338 physical_of_found
<=
5339 bbio
->stripes
[i
].physical
)
5343 physical_of_found
= bbio
->stripes
[i
].physical
;
5347 u64 length
= map
->stripe_len
;
5349 if (physical_of_found
+ length
<=
5350 dev_replace
->cursor_left
) {
5351 struct btrfs_bio_stripe
*tgtdev_stripe
=
5352 bbio
->stripes
+ num_stripes
;
5354 tgtdev_stripe
->physical
= physical_of_found
;
5355 tgtdev_stripe
->length
=
5356 bbio
->stripes
[index_srcdev
].length
;
5357 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5365 bbio
->num_stripes
= num_stripes
;
5366 bbio
->max_errors
= max_errors
;
5367 bbio
->mirror_num
= mirror_num
;
5370 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5371 * mirror_num == num_stripes + 1 && dev_replace target drive is
5372 * available as a mirror
5374 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5375 WARN_ON(num_stripes
> 1);
5376 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5377 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5378 bbio
->mirror_num
= map
->num_stripes
+ 1;
5381 sort_parity_stripes(bbio
, raid_map
);
5382 *raid_map_ret
= raid_map
;
5385 if (dev_replace_is_ongoing
)
5386 btrfs_dev_replace_unlock(dev_replace
);
5387 free_extent_map(em
);
5391 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
5392 u64 logical
, u64
*length
,
5393 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5395 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
5399 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
5400 u64 chunk_start
, u64 physical
, u64 devid
,
5401 u64
**logical
, int *naddrs
, int *stripe_len
)
5403 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
5404 struct extent_map
*em
;
5405 struct map_lookup
*map
;
5413 read_lock(&em_tree
->lock
);
5414 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
5415 read_unlock(&em_tree
->lock
);
5418 printk(KERN_ERR
"BTRFS: couldn't find em for chunk %Lu\n",
5423 if (em
->start
!= chunk_start
) {
5424 printk(KERN_ERR
"BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5425 em
->start
, chunk_start
);
5426 free_extent_map(em
);
5429 map
= (struct map_lookup
*)em
->bdev
;
5432 rmap_len
= map
->stripe_len
;
5434 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5435 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
5436 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5437 do_div(length
, map
->num_stripes
);
5438 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5439 BTRFS_BLOCK_GROUP_RAID6
)) {
5440 do_div(length
, nr_data_stripes(map
));
5441 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5444 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
5445 BUG_ON(!buf
); /* -ENOMEM */
5447 for (i
= 0; i
< map
->num_stripes
; i
++) {
5448 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
5450 if (map
->stripes
[i
].physical
> physical
||
5451 map
->stripes
[i
].physical
+ length
<= physical
)
5454 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5455 do_div(stripe_nr
, map
->stripe_len
);
5457 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5458 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5459 do_div(stripe_nr
, map
->sub_stripes
);
5460 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5461 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5462 } /* else if RAID[56], multiply by nr_data_stripes().
5463 * Alternatively, just use rmap_len below instead of
5464 * map->stripe_len */
5466 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5467 WARN_ON(nr
>= map
->num_stripes
);
5468 for (j
= 0; j
< nr
; j
++) {
5469 if (buf
[j
] == bytenr
)
5473 WARN_ON(nr
>= map
->num_stripes
);
5480 *stripe_len
= rmap_len
;
5482 free_extent_map(em
);
5486 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
, int err
)
5488 if (likely(bbio
->flags
& BTRFS_BIO_ORIG_BIO_SUBMITTED
))
5489 bio_endio_nodec(bio
, err
);
5491 bio_endio(bio
, err
);
5495 static void btrfs_end_bio(struct bio
*bio
, int err
)
5497 struct btrfs_bio
*bbio
= bio
->bi_private
;
5498 struct btrfs_device
*dev
= bbio
->stripes
[0].dev
;
5499 int is_orig_bio
= 0;
5502 atomic_inc(&bbio
->error
);
5503 if (err
== -EIO
|| err
== -EREMOTEIO
) {
5504 unsigned int stripe_index
=
5505 btrfs_io_bio(bio
)->stripe_index
;
5507 BUG_ON(stripe_index
>= bbio
->num_stripes
);
5508 dev
= bbio
->stripes
[stripe_index
].dev
;
5510 if (bio
->bi_rw
& WRITE
)
5511 btrfs_dev_stat_inc(dev
,
5512 BTRFS_DEV_STAT_WRITE_ERRS
);
5514 btrfs_dev_stat_inc(dev
,
5515 BTRFS_DEV_STAT_READ_ERRS
);
5516 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
5517 btrfs_dev_stat_inc(dev
,
5518 BTRFS_DEV_STAT_FLUSH_ERRS
);
5519 btrfs_dev_stat_print_on_error(dev
);
5524 if (bio
== bbio
->orig_bio
)
5527 btrfs_bio_counter_dec(bbio
->fs_info
);
5529 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5532 bio
= bbio
->orig_bio
;
5535 bio
->bi_private
= bbio
->private;
5536 bio
->bi_end_io
= bbio
->end_io
;
5537 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5538 /* only send an error to the higher layers if it is
5539 * beyond the tolerance of the btrfs bio
5541 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
5545 * this bio is actually up to date, we didn't
5546 * go over the max number of errors
5548 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5552 btrfs_end_bbio(bbio
, bio
, err
);
5553 } else if (!is_orig_bio
) {
5559 * see run_scheduled_bios for a description of why bios are collected for
5562 * This will add one bio to the pending list for a device and make sure
5563 * the work struct is scheduled.
5565 static noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
5566 struct btrfs_device
*device
,
5567 int rw
, struct bio
*bio
)
5569 int should_queue
= 1;
5570 struct btrfs_pending_bios
*pending_bios
;
5572 if (device
->missing
|| !device
->bdev
) {
5573 bio_endio(bio
, -EIO
);
5577 /* don't bother with additional async steps for reads, right now */
5578 if (!(rw
& REQ_WRITE
)) {
5580 btrfsic_submit_bio(rw
, bio
);
5586 * nr_async_bios allows us to reliably return congestion to the
5587 * higher layers. Otherwise, the async bio makes it appear we have
5588 * made progress against dirty pages when we've really just put it
5589 * on a queue for later
5591 atomic_inc(&root
->fs_info
->nr_async_bios
);
5592 WARN_ON(bio
->bi_next
);
5593 bio
->bi_next
= NULL
;
5596 spin_lock(&device
->io_lock
);
5597 if (bio
->bi_rw
& REQ_SYNC
)
5598 pending_bios
= &device
->pending_sync_bios
;
5600 pending_bios
= &device
->pending_bios
;
5602 if (pending_bios
->tail
)
5603 pending_bios
->tail
->bi_next
= bio
;
5605 pending_bios
->tail
= bio
;
5606 if (!pending_bios
->head
)
5607 pending_bios
->head
= bio
;
5608 if (device
->running_pending
)
5611 spin_unlock(&device
->io_lock
);
5614 btrfs_queue_work(root
->fs_info
->submit_workers
,
5618 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5621 struct bio_vec
*prev
;
5622 struct request_queue
*q
= bdev_get_queue(bdev
);
5623 unsigned int max_sectors
= queue_max_sectors(q
);
5624 struct bvec_merge_data bvm
= {
5626 .bi_sector
= sector
,
5627 .bi_rw
= bio
->bi_rw
,
5630 if (WARN_ON(bio
->bi_vcnt
== 0))
5633 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5634 if (bio_sectors(bio
) > max_sectors
)
5637 if (!q
->merge_bvec_fn
)
5640 bvm
.bi_size
= bio
->bi_iter
.bi_size
- prev
->bv_len
;
5641 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5646 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5647 struct bio
*bio
, u64 physical
, int dev_nr
,
5650 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5652 bio
->bi_private
= bbio
;
5653 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
5654 bio
->bi_end_io
= btrfs_end_bio
;
5655 bio
->bi_iter
.bi_sector
= physical
>> 9;
5658 struct rcu_string
*name
;
5661 name
= rcu_dereference(dev
->name
);
5662 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5663 "(%s id %llu), size=%u\n", rw
,
5664 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5665 name
->str
, dev
->devid
, bio
->bi_size
);
5669 bio
->bi_bdev
= dev
->bdev
;
5671 btrfs_bio_counter_inc_noblocked(root
->fs_info
);
5674 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5676 btrfsic_submit_bio(rw
, bio
);
5679 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5680 struct bio
*first_bio
, struct btrfs_device
*dev
,
5681 int dev_nr
, int rw
, int async
)
5683 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5685 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5686 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5689 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5693 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5694 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5695 bvec
->bv_offset
) < bvec
->bv_len
) {
5696 u64 len
= bio
->bi_iter
.bi_size
;
5698 atomic_inc(&bbio
->stripes_pending
);
5699 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5707 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5711 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5713 atomic_inc(&bbio
->error
);
5714 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5715 /* Shoud be the original bio. */
5716 WARN_ON(bio
!= bbio
->orig_bio
);
5718 bio
->bi_private
= bbio
->private;
5719 bio
->bi_end_io
= bbio
->end_io
;
5720 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5721 bio
->bi_iter
.bi_sector
= logical
>> 9;
5723 btrfs_end_bbio(bbio
, bio
, -EIO
);
5727 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5728 int mirror_num
, int async_submit
)
5730 struct btrfs_device
*dev
;
5731 struct bio
*first_bio
= bio
;
5732 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
5735 u64
*raid_map
= NULL
;
5739 struct btrfs_bio
*bbio
= NULL
;
5741 length
= bio
->bi_iter
.bi_size
;
5742 map_length
= length
;
5744 btrfs_bio_counter_inc_blocked(root
->fs_info
);
5745 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5746 mirror_num
, &raid_map
);
5748 btrfs_bio_counter_dec(root
->fs_info
);
5752 total_devs
= bbio
->num_stripes
;
5753 bbio
->orig_bio
= first_bio
;
5754 bbio
->private = first_bio
->bi_private
;
5755 bbio
->end_io
= first_bio
->bi_end_io
;
5756 bbio
->fs_info
= root
->fs_info
;
5757 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5760 /* In this case, map_length has been set to the length of
5761 a single stripe; not the whole write */
5763 ret
= raid56_parity_write(root
, bio
, bbio
,
5764 raid_map
, map_length
);
5766 ret
= raid56_parity_recover(root
, bio
, bbio
,
5767 raid_map
, map_length
,
5771 * FIXME, replace dosen't support raid56 yet, please fix
5774 btrfs_bio_counter_dec(root
->fs_info
);
5778 if (map_length
< length
) {
5779 btrfs_crit(root
->fs_info
, "mapping failed logical %llu bio len %llu len %llu",
5780 logical
, length
, map_length
);
5784 while (dev_nr
< total_devs
) {
5785 dev
= bbio
->stripes
[dev_nr
].dev
;
5786 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5787 bbio_error(bbio
, first_bio
, logical
);
5793 * Check and see if we're ok with this bio based on it's size
5794 * and offset with the given device.
5796 if (!bio_size_ok(dev
->bdev
, first_bio
,
5797 bbio
->stripes
[dev_nr
].physical
>> 9)) {
5798 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
5799 dev_nr
, rw
, async_submit
);
5805 if (dev_nr
< total_devs
- 1) {
5806 bio
= btrfs_bio_clone(first_bio
, GFP_NOFS
);
5807 BUG_ON(!bio
); /* -ENOMEM */
5810 bbio
->flags
|= BTRFS_BIO_ORIG_BIO_SUBMITTED
;
5813 submit_stripe_bio(root
, bbio
, bio
,
5814 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
5818 btrfs_bio_counter_dec(root
->fs_info
);
5822 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
5825 struct btrfs_device
*device
;
5826 struct btrfs_fs_devices
*cur_devices
;
5828 cur_devices
= fs_info
->fs_devices
;
5829 while (cur_devices
) {
5831 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5832 device
= __find_device(&cur_devices
->devices
,
5837 cur_devices
= cur_devices
->seed
;
5842 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
5843 u64 devid
, u8
*dev_uuid
)
5845 struct btrfs_device
*device
;
5846 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5848 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
5852 list_add(&device
->dev_list
, &fs_devices
->devices
);
5853 device
->fs_devices
= fs_devices
;
5854 fs_devices
->num_devices
++;
5856 device
->missing
= 1;
5857 fs_devices
->missing_devices
++;
5863 * btrfs_alloc_device - allocate struct btrfs_device
5864 * @fs_info: used only for generating a new devid, can be NULL if
5865 * devid is provided (i.e. @devid != NULL).
5866 * @devid: a pointer to devid for this device. If NULL a new devid
5868 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5871 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5872 * on error. Returned struct is not linked onto any lists and can be
5873 * destroyed with kfree() right away.
5875 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
5879 struct btrfs_device
*dev
;
5882 if (WARN_ON(!devid
&& !fs_info
))
5883 return ERR_PTR(-EINVAL
);
5885 dev
= __alloc_device();
5894 ret
= find_next_devid(fs_info
, &tmp
);
5897 return ERR_PTR(ret
);
5903 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
5905 generate_random_uuid(dev
->uuid
);
5907 btrfs_init_work(&dev
->work
, btrfs_submit_helper
,
5908 pending_bios_fn
, NULL
, NULL
);
5913 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5914 struct extent_buffer
*leaf
,
5915 struct btrfs_chunk
*chunk
)
5917 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5918 struct map_lookup
*map
;
5919 struct extent_map
*em
;
5923 u8 uuid
[BTRFS_UUID_SIZE
];
5928 logical
= key
->offset
;
5929 length
= btrfs_chunk_length(leaf
, chunk
);
5931 read_lock(&map_tree
->map_tree
.lock
);
5932 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5933 read_unlock(&map_tree
->map_tree
.lock
);
5935 /* already mapped? */
5936 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5937 free_extent_map(em
);
5940 free_extent_map(em
);
5943 em
= alloc_extent_map();
5946 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5947 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5949 free_extent_map(em
);
5953 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
5954 em
->bdev
= (struct block_device
*)map
;
5955 em
->start
= logical
;
5958 em
->block_start
= 0;
5959 em
->block_len
= em
->len
;
5961 map
->num_stripes
= num_stripes
;
5962 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5963 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5964 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5965 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5966 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5967 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5968 for (i
= 0; i
< num_stripes
; i
++) {
5969 map
->stripes
[i
].physical
=
5970 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5971 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5972 read_extent_buffer(leaf
, uuid
, (unsigned long)
5973 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5975 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5977 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5978 free_extent_map(em
);
5981 if (!map
->stripes
[i
].dev
) {
5982 map
->stripes
[i
].dev
=
5983 add_missing_dev(root
, devid
, uuid
);
5984 if (!map
->stripes
[i
].dev
) {
5985 free_extent_map(em
);
5989 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5992 write_lock(&map_tree
->map_tree
.lock
);
5993 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
5994 write_unlock(&map_tree
->map_tree
.lock
);
5995 BUG_ON(ret
); /* Tree corruption */
5996 free_extent_map(em
);
6001 static void fill_device_from_item(struct extent_buffer
*leaf
,
6002 struct btrfs_dev_item
*dev_item
,
6003 struct btrfs_device
*device
)
6007 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6008 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6009 device
->total_bytes
= device
->disk_total_bytes
;
6010 device
->commit_total_bytes
= device
->disk_total_bytes
;
6011 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6012 device
->type
= btrfs_device_type(leaf
, dev_item
);
6013 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6014 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6015 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6016 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6017 device
->is_tgtdev_for_dev_replace
= 0;
6019 ptr
= btrfs_device_uuid(dev_item
);
6020 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6023 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
6025 struct btrfs_fs_devices
*fs_devices
;
6028 BUG_ON(!mutex_is_locked(&uuid_mutex
));
6030 fs_devices
= root
->fs_info
->fs_devices
->seed
;
6031 while (fs_devices
) {
6032 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
6036 fs_devices
= fs_devices
->seed
;
6039 fs_devices
= find_fsid(fsid
);
6045 fs_devices
= clone_fs_devices(fs_devices
);
6046 if (IS_ERR(fs_devices
)) {
6047 ret
= PTR_ERR(fs_devices
);
6051 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
6052 root
->fs_info
->bdev_holder
);
6054 free_fs_devices(fs_devices
);
6058 if (!fs_devices
->seeding
) {
6059 __btrfs_close_devices(fs_devices
);
6060 free_fs_devices(fs_devices
);
6065 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
6066 root
->fs_info
->fs_devices
->seed
= fs_devices
;
6071 static int read_one_dev(struct btrfs_root
*root
,
6072 struct extent_buffer
*leaf
,
6073 struct btrfs_dev_item
*dev_item
)
6075 struct btrfs_device
*device
;
6078 u8 fs_uuid
[BTRFS_UUID_SIZE
];
6079 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6081 devid
= btrfs_device_id(leaf
, dev_item
);
6082 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6084 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6087 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
6088 ret
= open_seed_devices(root
, fs_uuid
);
6089 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
6093 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
6094 if (!device
|| !device
->bdev
) {
6095 if (!btrfs_test_opt(root
, DEGRADED
))
6099 btrfs_warn(root
->fs_info
, "devid %llu missing", devid
);
6100 device
= add_missing_dev(root
, devid
, dev_uuid
);
6103 } else if (!device
->missing
) {
6105 * this happens when a device that was properly setup
6106 * in the device info lists suddenly goes bad.
6107 * device->bdev is NULL, and so we have to set
6108 * device->missing to one here
6110 root
->fs_info
->fs_devices
->missing_devices
++;
6111 device
->missing
= 1;
6115 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
6116 BUG_ON(device
->writeable
);
6117 if (device
->generation
!=
6118 btrfs_device_generation(leaf
, dev_item
))
6122 fill_device_from_item(leaf
, dev_item
, device
);
6123 device
->in_fs_metadata
= 1;
6124 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
6125 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6126 spin_lock(&root
->fs_info
->free_chunk_lock
);
6127 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
6129 spin_unlock(&root
->fs_info
->free_chunk_lock
);
6135 int btrfs_read_sys_array(struct btrfs_root
*root
)
6137 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
6138 struct extent_buffer
*sb
;
6139 struct btrfs_disk_key
*disk_key
;
6140 struct btrfs_chunk
*chunk
;
6142 unsigned long sb_ptr
;
6148 struct btrfs_key key
;
6150 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
6151 BTRFS_SUPER_INFO_SIZE
);
6154 btrfs_set_buffer_uptodate(sb
);
6155 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6157 * The sb extent buffer is artifical and just used to read the system array.
6158 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6159 * pages up-to-date when the page is larger: extent does not cover the
6160 * whole page and consequently check_page_uptodate does not find all
6161 * the page's extents up-to-date (the hole beyond sb),
6162 * write_extent_buffer then triggers a WARN_ON.
6164 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6165 * but sb spans only this function. Add an explicit SetPageUptodate call
6166 * to silence the warning eg. on PowerPC 64.
6168 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6169 SetPageUptodate(sb
->pages
[0]);
6171 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6172 array_size
= btrfs_super_sys_array_size(super_copy
);
6174 ptr
= super_copy
->sys_chunk_array
;
6175 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6178 while (cur
< array_size
) {
6179 disk_key
= (struct btrfs_disk_key
*)ptr
;
6180 btrfs_disk_key_to_cpu(&key
, disk_key
);
6182 len
= sizeof(*disk_key
); ptr
+= len
;
6186 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6187 chunk
= (struct btrfs_chunk
*)sb_ptr
;
6188 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
6191 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6192 len
= btrfs_chunk_item_size(num_stripes
);
6201 free_extent_buffer(sb
);
6205 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
6207 struct btrfs_path
*path
;
6208 struct extent_buffer
*leaf
;
6209 struct btrfs_key key
;
6210 struct btrfs_key found_key
;
6214 root
= root
->fs_info
->chunk_root
;
6216 path
= btrfs_alloc_path();
6220 mutex_lock(&uuid_mutex
);
6224 * Read all device items, and then all the chunk items. All
6225 * device items are found before any chunk item (their object id
6226 * is smaller than the lowest possible object id for a chunk
6227 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6229 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6232 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6236 leaf
= path
->nodes
[0];
6237 slot
= path
->slots
[0];
6238 if (slot
>= btrfs_header_nritems(leaf
)) {
6239 ret
= btrfs_next_leaf(root
, path
);
6246 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6247 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6248 struct btrfs_dev_item
*dev_item
;
6249 dev_item
= btrfs_item_ptr(leaf
, slot
,
6250 struct btrfs_dev_item
);
6251 ret
= read_one_dev(root
, leaf
, dev_item
);
6254 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6255 struct btrfs_chunk
*chunk
;
6256 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6257 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
6265 unlock_chunks(root
);
6266 mutex_unlock(&uuid_mutex
);
6268 btrfs_free_path(path
);
6272 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
6274 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6275 struct btrfs_device
*device
;
6277 while (fs_devices
) {
6278 mutex_lock(&fs_devices
->device_list_mutex
);
6279 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
6280 device
->dev_root
= fs_info
->dev_root
;
6281 mutex_unlock(&fs_devices
->device_list_mutex
);
6283 fs_devices
= fs_devices
->seed
;
6287 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
6291 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6292 btrfs_dev_stat_reset(dev
, i
);
6295 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
6297 struct btrfs_key key
;
6298 struct btrfs_key found_key
;
6299 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6300 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6301 struct extent_buffer
*eb
;
6304 struct btrfs_device
*device
;
6305 struct btrfs_path
*path
= NULL
;
6308 path
= btrfs_alloc_path();
6314 mutex_lock(&fs_devices
->device_list_mutex
);
6315 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6317 struct btrfs_dev_stats_item
*ptr
;
6320 key
.type
= BTRFS_DEV_STATS_KEY
;
6321 key
.offset
= device
->devid
;
6322 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
6324 __btrfs_reset_dev_stats(device
);
6325 device
->dev_stats_valid
= 1;
6326 btrfs_release_path(path
);
6329 slot
= path
->slots
[0];
6330 eb
= path
->nodes
[0];
6331 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
6332 item_size
= btrfs_item_size_nr(eb
, slot
);
6334 ptr
= btrfs_item_ptr(eb
, slot
,
6335 struct btrfs_dev_stats_item
);
6337 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6338 if (item_size
>= (1 + i
) * sizeof(__le64
))
6339 btrfs_dev_stat_set(device
, i
,
6340 btrfs_dev_stats_value(eb
, ptr
, i
));
6342 btrfs_dev_stat_reset(device
, i
);
6345 device
->dev_stats_valid
= 1;
6346 btrfs_dev_stat_print_on_load(device
);
6347 btrfs_release_path(path
);
6349 mutex_unlock(&fs_devices
->device_list_mutex
);
6352 btrfs_free_path(path
);
6353 return ret
< 0 ? ret
: 0;
6356 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
6357 struct btrfs_root
*dev_root
,
6358 struct btrfs_device
*device
)
6360 struct btrfs_path
*path
;
6361 struct btrfs_key key
;
6362 struct extent_buffer
*eb
;
6363 struct btrfs_dev_stats_item
*ptr
;
6368 key
.type
= BTRFS_DEV_STATS_KEY
;
6369 key
.offset
= device
->devid
;
6371 path
= btrfs_alloc_path();
6373 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
6375 printk_in_rcu(KERN_WARNING
"BTRFS: "
6376 "error %d while searching for dev_stats item for device %s!\n",
6377 ret
, rcu_str_deref(device
->name
));
6382 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
6383 /* need to delete old one and insert a new one */
6384 ret
= btrfs_del_item(trans
, dev_root
, path
);
6386 printk_in_rcu(KERN_WARNING
"BTRFS: "
6387 "delete too small dev_stats item for device %s failed %d!\n",
6388 rcu_str_deref(device
->name
), ret
);
6395 /* need to insert a new item */
6396 btrfs_release_path(path
);
6397 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
6398 &key
, sizeof(*ptr
));
6400 printk_in_rcu(KERN_WARNING
"BTRFS: "
6401 "insert dev_stats item for device %s failed %d!\n",
6402 rcu_str_deref(device
->name
), ret
);
6407 eb
= path
->nodes
[0];
6408 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
6409 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6410 btrfs_set_dev_stats_value(eb
, ptr
, i
,
6411 btrfs_dev_stat_read(device
, i
));
6412 btrfs_mark_buffer_dirty(eb
);
6415 btrfs_free_path(path
);
6420 * called from commit_transaction. Writes all changed device stats to disk.
6422 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
6423 struct btrfs_fs_info
*fs_info
)
6425 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6426 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6427 struct btrfs_device
*device
;
6431 mutex_lock(&fs_devices
->device_list_mutex
);
6432 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6433 if (!device
->dev_stats_valid
|| !btrfs_dev_stats_dirty(device
))
6436 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
6437 ret
= update_dev_stat_item(trans
, dev_root
, device
);
6439 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
6441 mutex_unlock(&fs_devices
->device_list_mutex
);
6446 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
6448 btrfs_dev_stat_inc(dev
, index
);
6449 btrfs_dev_stat_print_on_error(dev
);
6452 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
6454 if (!dev
->dev_stats_valid
)
6456 printk_ratelimited_in_rcu(KERN_ERR
"BTRFS: "
6457 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6458 rcu_str_deref(dev
->name
),
6459 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6460 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6461 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6462 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6463 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6466 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
6470 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6471 if (btrfs_dev_stat_read(dev
, i
) != 0)
6473 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
6474 return; /* all values == 0, suppress message */
6476 printk_in_rcu(KERN_INFO
"BTRFS: "
6477 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6478 rcu_str_deref(dev
->name
),
6479 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6480 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6481 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6482 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6483 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6486 int btrfs_get_dev_stats(struct btrfs_root
*root
,
6487 struct btrfs_ioctl_get_dev_stats
*stats
)
6489 struct btrfs_device
*dev
;
6490 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6493 mutex_lock(&fs_devices
->device_list_mutex
);
6494 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
6495 mutex_unlock(&fs_devices
->device_list_mutex
);
6498 btrfs_warn(root
->fs_info
, "get dev_stats failed, device not found");
6500 } else if (!dev
->dev_stats_valid
) {
6501 btrfs_warn(root
->fs_info
, "get dev_stats failed, not yet valid");
6503 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
6504 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6505 if (stats
->nr_items
> i
)
6507 btrfs_dev_stat_read_and_reset(dev
, i
);
6509 btrfs_dev_stat_reset(dev
, i
);
6512 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6513 if (stats
->nr_items
> i
)
6514 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
6516 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
6517 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
6521 int btrfs_scratch_superblock(struct btrfs_device
*device
)
6523 struct buffer_head
*bh
;
6524 struct btrfs_super_block
*disk_super
;
6526 bh
= btrfs_read_dev_super(device
->bdev
);
6529 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
6531 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
6532 set_buffer_dirty(bh
);
6533 sync_dirty_buffer(bh
);
6540 * Update the size of all devices, which is used for writing out the
6543 void btrfs_update_commit_device_size(struct btrfs_fs_info
*fs_info
)
6545 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6546 struct btrfs_device
*curr
, *next
;
6548 if (list_empty(&fs_devices
->resized_devices
))
6551 mutex_lock(&fs_devices
->device_list_mutex
);
6552 lock_chunks(fs_info
->dev_root
);
6553 list_for_each_entry_safe(curr
, next
, &fs_devices
->resized_devices
,
6555 list_del_init(&curr
->resized_list
);
6556 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
6558 unlock_chunks(fs_info
->dev_root
);
6559 mutex_unlock(&fs_devices
->device_list_mutex
);