1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/ratelimit.h>
12 #include <linux/kthread.h>
13 #include <linux/raid/pq.h>
14 #include <linux/semaphore.h>
15 #include <linux/uuid.h>
16 #include <linux/list_sort.h>
18 #include "extent_map.h"
20 #include "transaction.h"
21 #include "print-tree.h"
24 #include "async-thread.h"
25 #include "check-integrity.h"
26 #include "rcu-string.h"
28 #include "dev-replace.h"
31 const struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
32 [BTRFS_RAID_RAID10
] = {
35 .devs_max
= 0, /* 0 == as many as possible */
37 .tolerated_failures
= 1,
40 .raid_name
= "raid10",
41 .bg_flag
= BTRFS_BLOCK_GROUP_RAID10
,
42 .mindev_error
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
,
44 [BTRFS_RAID_RAID1
] = {
49 .tolerated_failures
= 1,
53 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1
,
54 .mindev_error
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
,
61 .tolerated_failures
= 0,
65 .bg_flag
= BTRFS_BLOCK_GROUP_DUP
,
68 [BTRFS_RAID_RAID0
] = {
73 .tolerated_failures
= 0,
77 .bg_flag
= BTRFS_BLOCK_GROUP_RAID0
,
80 [BTRFS_RAID_SINGLE
] = {
85 .tolerated_failures
= 0,
88 .raid_name
= "single",
92 [BTRFS_RAID_RAID5
] = {
97 .tolerated_failures
= 1,
100 .raid_name
= "raid5",
101 .bg_flag
= BTRFS_BLOCK_GROUP_RAID5
,
102 .mindev_error
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
,
104 [BTRFS_RAID_RAID6
] = {
109 .tolerated_failures
= 2,
112 .raid_name
= "raid6",
113 .bg_flag
= BTRFS_BLOCK_GROUP_RAID6
,
114 .mindev_error
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
,
118 const char *get_raid_name(enum btrfs_raid_types type
)
120 if (type
>= BTRFS_NR_RAID_TYPES
)
123 return btrfs_raid_array
[type
].raid_name
;
126 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
127 struct btrfs_fs_info
*fs_info
);
128 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
);
129 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
130 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
131 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
132 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
133 enum btrfs_map_op op
,
134 u64 logical
, u64
*length
,
135 struct btrfs_bio
**bbio_ret
,
136 int mirror_num
, int need_raid_map
);
142 * There are several mutexes that protect manipulation of devices and low-level
143 * structures like chunks but not block groups, extents or files
145 * uuid_mutex (global lock)
146 * ------------------------
147 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
148 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
149 * device) or requested by the device= mount option
151 * the mutex can be very coarse and can cover long-running operations
153 * protects: updates to fs_devices counters like missing devices, rw devices,
154 * seeding, structure cloning, openning/closing devices at mount/umount time
156 * global::fs_devs - add, remove, updates to the global list
158 * does not protect: manipulation of the fs_devices::devices list!
160 * btrfs_device::name - renames (write side), read is RCU
162 * fs_devices::device_list_mutex (per-fs, with RCU)
163 * ------------------------------------------------
164 * protects updates to fs_devices::devices, ie. adding and deleting
166 * simple list traversal with read-only actions can be done with RCU protection
168 * may be used to exclude some operations from running concurrently without any
169 * modifications to the list (see write_all_supers)
173 * protects balance structures (status, state) and context accessed from
174 * several places (internally, ioctl)
178 * protects chunks, adding or removing during allocation, trim or when a new
179 * device is added/removed
183 * a big lock that is held by the cleaner thread and prevents running subvolume
184 * cleaning together with relocation or delayed iputs
197 * Exclusive operations, BTRFS_FS_EXCL_OP
198 * ======================================
200 * Maintains the exclusivity of the following operations that apply to the
201 * whole filesystem and cannot run in parallel.
206 * - Device replace (*)
209 * The device operations (as above) can be in one of the following states:
215 * Only device operations marked with (*) can go into the Paused state for the
218 * - ioctl (only Balance can be Paused through ioctl)
219 * - filesystem remounted as read-only
220 * - filesystem unmounted and mounted as read-only
221 * - system power-cycle and filesystem mounted as read-only
222 * - filesystem or device errors leading to forced read-only
224 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
225 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
226 * A device operation in Paused or Running state can be canceled or resumed
227 * either by ioctl (Balance only) or when remounted as read-write.
228 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
232 DEFINE_MUTEX(uuid_mutex
);
233 static LIST_HEAD(fs_uuids
);
234 struct list_head
*btrfs_get_fs_uuids(void)
240 * alloc_fs_devices - allocate struct btrfs_fs_devices
241 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
243 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
244 * The returned struct is not linked onto any lists and can be destroyed with
245 * kfree() right away.
247 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
)
249 struct btrfs_fs_devices
*fs_devs
;
251 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_KERNEL
);
253 return ERR_PTR(-ENOMEM
);
255 mutex_init(&fs_devs
->device_list_mutex
);
257 INIT_LIST_HEAD(&fs_devs
->devices
);
258 INIT_LIST_HEAD(&fs_devs
->resized_devices
);
259 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
260 INIT_LIST_HEAD(&fs_devs
->fs_list
);
262 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
267 void btrfs_free_device(struct btrfs_device
*device
)
269 rcu_string_free(device
->name
);
270 bio_put(device
->flush_bio
);
274 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
276 struct btrfs_device
*device
;
277 WARN_ON(fs_devices
->opened
);
278 while (!list_empty(&fs_devices
->devices
)) {
279 device
= list_entry(fs_devices
->devices
.next
,
280 struct btrfs_device
, dev_list
);
281 list_del(&device
->dev_list
);
282 btrfs_free_device(device
);
287 static void btrfs_kobject_uevent(struct block_device
*bdev
,
288 enum kobject_action action
)
292 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
294 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
296 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
297 &disk_to_dev(bdev
->bd_disk
)->kobj
);
300 void __exit
btrfs_cleanup_fs_uuids(void)
302 struct btrfs_fs_devices
*fs_devices
;
304 while (!list_empty(&fs_uuids
)) {
305 fs_devices
= list_entry(fs_uuids
.next
,
306 struct btrfs_fs_devices
, fs_list
);
307 list_del(&fs_devices
->fs_list
);
308 free_fs_devices(fs_devices
);
313 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
314 * Returned struct is not linked onto any lists and must be destroyed using
317 static struct btrfs_device
*__alloc_device(void)
319 struct btrfs_device
*dev
;
321 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
323 return ERR_PTR(-ENOMEM
);
326 * Preallocate a bio that's always going to be used for flushing device
327 * barriers and matches the device lifespan
329 dev
->flush_bio
= bio_alloc_bioset(GFP_KERNEL
, 0, NULL
);
330 if (!dev
->flush_bio
) {
332 return ERR_PTR(-ENOMEM
);
335 INIT_LIST_HEAD(&dev
->dev_list
);
336 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
337 INIT_LIST_HEAD(&dev
->resized_list
);
339 spin_lock_init(&dev
->io_lock
);
341 atomic_set(&dev
->reada_in_flight
, 0);
342 atomic_set(&dev
->dev_stats_ccnt
, 0);
343 btrfs_device_data_ordered_init(dev
);
344 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
345 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
350 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
352 struct btrfs_fs_devices
*fs_devices
;
354 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
355 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
362 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
363 int flush
, struct block_device
**bdev
,
364 struct buffer_head
**bh
)
368 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
371 ret
= PTR_ERR(*bdev
);
376 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
377 ret
= set_blocksize(*bdev
, BTRFS_BDEV_BLOCKSIZE
);
379 blkdev_put(*bdev
, flags
);
382 invalidate_bdev(*bdev
);
383 *bh
= btrfs_read_dev_super(*bdev
);
386 blkdev_put(*bdev
, flags
);
398 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
399 struct bio
*head
, struct bio
*tail
)
402 struct bio
*old_head
;
404 old_head
= pending_bios
->head
;
405 pending_bios
->head
= head
;
406 if (pending_bios
->tail
)
407 tail
->bi_next
= old_head
;
409 pending_bios
->tail
= tail
;
413 * we try to collect pending bios for a device so we don't get a large
414 * number of procs sending bios down to the same device. This greatly
415 * improves the schedulers ability to collect and merge the bios.
417 * But, it also turns into a long list of bios to process and that is sure
418 * to eventually make the worker thread block. The solution here is to
419 * make some progress and then put this work struct back at the end of
420 * the list if the block device is congested. This way, multiple devices
421 * can make progress from a single worker thread.
423 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
425 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
427 struct backing_dev_info
*bdi
;
428 struct btrfs_pending_bios
*pending_bios
;
432 unsigned long num_run
;
433 unsigned long batch_run
= 0;
434 unsigned long last_waited
= 0;
436 int sync_pending
= 0;
437 struct blk_plug plug
;
440 * this function runs all the bios we've collected for
441 * a particular device. We don't want to wander off to
442 * another device without first sending all of these down.
443 * So, setup a plug here and finish it off before we return
445 blk_start_plug(&plug
);
447 bdi
= device
->bdev
->bd_bdi
;
450 spin_lock(&device
->io_lock
);
455 /* take all the bios off the list at once and process them
456 * later on (without the lock held). But, remember the
457 * tail and other pointers so the bios can be properly reinserted
458 * into the list if we hit congestion
460 if (!force_reg
&& device
->pending_sync_bios
.head
) {
461 pending_bios
= &device
->pending_sync_bios
;
464 pending_bios
= &device
->pending_bios
;
468 pending
= pending_bios
->head
;
469 tail
= pending_bios
->tail
;
470 WARN_ON(pending
&& !tail
);
473 * if pending was null this time around, no bios need processing
474 * at all and we can stop. Otherwise it'll loop back up again
475 * and do an additional check so no bios are missed.
477 * device->running_pending is used to synchronize with the
480 if (device
->pending_sync_bios
.head
== NULL
&&
481 device
->pending_bios
.head
== NULL
) {
483 device
->running_pending
= 0;
486 device
->running_pending
= 1;
489 pending_bios
->head
= NULL
;
490 pending_bios
->tail
= NULL
;
492 spin_unlock(&device
->io_lock
);
497 /* we want to work on both lists, but do more bios on the
498 * sync list than the regular list
501 pending_bios
!= &device
->pending_sync_bios
&&
502 device
->pending_sync_bios
.head
) ||
503 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
504 device
->pending_bios
.head
)) {
505 spin_lock(&device
->io_lock
);
506 requeue_list(pending_bios
, pending
, tail
);
511 pending
= pending
->bi_next
;
514 BUG_ON(atomic_read(&cur
->__bi_cnt
) == 0);
517 * if we're doing the sync list, record that our
518 * plug has some sync requests on it
520 * If we're doing the regular list and there are
521 * sync requests sitting around, unplug before
524 if (pending_bios
== &device
->pending_sync_bios
) {
526 } else if (sync_pending
) {
527 blk_finish_plug(&plug
);
528 blk_start_plug(&plug
);
532 btrfsic_submit_bio(cur
);
539 * we made progress, there is more work to do and the bdi
540 * is now congested. Back off and let other work structs
543 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
544 fs_info
->fs_devices
->open_devices
> 1) {
545 struct io_context
*ioc
;
547 ioc
= current
->io_context
;
550 * the main goal here is that we don't want to
551 * block if we're going to be able to submit
552 * more requests without blocking.
554 * This code does two great things, it pokes into
555 * the elevator code from a filesystem _and_
556 * it makes assumptions about how batching works.
558 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
559 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
561 ioc
->last_waited
== last_waited
)) {
563 * we want to go through our batch of
564 * requests and stop. So, we copy out
565 * the ioc->last_waited time and test
566 * against it before looping
568 last_waited
= ioc
->last_waited
;
572 spin_lock(&device
->io_lock
);
573 requeue_list(pending_bios
, pending
, tail
);
574 device
->running_pending
= 1;
576 spin_unlock(&device
->io_lock
);
577 btrfs_queue_work(fs_info
->submit_workers
,
587 spin_lock(&device
->io_lock
);
588 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
590 spin_unlock(&device
->io_lock
);
593 blk_finish_plug(&plug
);
596 static void pending_bios_fn(struct btrfs_work
*work
)
598 struct btrfs_device
*device
;
600 device
= container_of(work
, struct btrfs_device
, work
);
601 run_scheduled_bios(device
);
605 * Search and remove all stale (devices which are not mounted) devices.
606 * When both inputs are NULL, it will search and release all stale devices.
607 * path: Optional. When provided will it release all unmounted devices
608 * matching this path only.
609 * skip_dev: Optional. Will skip this device when searching for the stale
612 static void btrfs_free_stale_devices(const char *path
,
613 struct btrfs_device
*skip_device
)
615 struct btrfs_fs_devices
*fs_devices
, *tmp_fs_devices
;
616 struct btrfs_device
*device
, *tmp_device
;
618 list_for_each_entry_safe(fs_devices
, tmp_fs_devices
, &fs_uuids
, fs_list
) {
619 mutex_lock(&fs_devices
->device_list_mutex
);
620 if (fs_devices
->opened
) {
621 mutex_unlock(&fs_devices
->device_list_mutex
);
625 list_for_each_entry_safe(device
, tmp_device
,
626 &fs_devices
->devices
, dev_list
) {
629 if (skip_device
&& skip_device
== device
)
631 if (path
&& !device
->name
)
636 not_found
= strcmp(rcu_str_deref(device
->name
),
642 /* delete the stale device */
643 fs_devices
->num_devices
--;
644 list_del(&device
->dev_list
);
645 btrfs_free_device(device
);
647 if (fs_devices
->num_devices
== 0)
650 mutex_unlock(&fs_devices
->device_list_mutex
);
651 if (fs_devices
->num_devices
== 0) {
652 btrfs_sysfs_remove_fsid(fs_devices
);
653 list_del(&fs_devices
->fs_list
);
654 free_fs_devices(fs_devices
);
659 static int btrfs_open_one_device(struct btrfs_fs_devices
*fs_devices
,
660 struct btrfs_device
*device
, fmode_t flags
,
663 struct request_queue
*q
;
664 struct block_device
*bdev
;
665 struct buffer_head
*bh
;
666 struct btrfs_super_block
*disk_super
;
675 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
680 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
681 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
682 if (devid
!= device
->devid
)
685 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
, BTRFS_UUID_SIZE
))
688 device
->generation
= btrfs_super_generation(disk_super
);
690 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
691 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
692 fs_devices
->seeding
= 1;
694 if (bdev_read_only(bdev
))
695 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
697 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
700 q
= bdev_get_queue(bdev
);
701 if (!blk_queue_nonrot(q
))
702 fs_devices
->rotating
= 1;
705 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
706 device
->mode
= flags
;
708 fs_devices
->open_devices
++;
709 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
710 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
711 fs_devices
->rw_devices
++;
712 list_add_tail(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
720 blkdev_put(bdev
, flags
);
726 * Add new device to list of registered devices
729 * device pointer which was just added or updated when successful
730 * error pointer when failed
732 static noinline
struct btrfs_device
*device_list_add(const char *path
,
733 struct btrfs_super_block
*disk_super
,
734 bool *new_device_added
)
736 struct btrfs_device
*device
;
737 struct btrfs_fs_devices
*fs_devices
;
738 struct rcu_string
*name
;
739 u64 found_transid
= btrfs_super_generation(disk_super
);
740 u64 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
742 fs_devices
= find_fsid(disk_super
->fsid
);
744 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
745 if (IS_ERR(fs_devices
))
746 return ERR_CAST(fs_devices
);
748 mutex_lock(&fs_devices
->device_list_mutex
);
749 list_add(&fs_devices
->fs_list
, &fs_uuids
);
753 mutex_lock(&fs_devices
->device_list_mutex
);
754 device
= btrfs_find_device(fs_devices
, devid
,
755 disk_super
->dev_item
.uuid
, NULL
, false);
759 if (fs_devices
->opened
) {
760 mutex_unlock(&fs_devices
->device_list_mutex
);
761 return ERR_PTR(-EBUSY
);
764 device
= btrfs_alloc_device(NULL
, &devid
,
765 disk_super
->dev_item
.uuid
);
766 if (IS_ERR(device
)) {
767 mutex_unlock(&fs_devices
->device_list_mutex
);
768 /* we can safely leave the fs_devices entry around */
772 name
= rcu_string_strdup(path
, GFP_NOFS
);
774 btrfs_free_device(device
);
775 mutex_unlock(&fs_devices
->device_list_mutex
);
776 return ERR_PTR(-ENOMEM
);
778 rcu_assign_pointer(device
->name
, name
);
780 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
781 fs_devices
->num_devices
++;
783 device
->fs_devices
= fs_devices
;
784 *new_device_added
= true;
786 if (disk_super
->label
[0])
787 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
788 disk_super
->label
, devid
, found_transid
, path
);
790 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
791 disk_super
->fsid
, devid
, found_transid
, path
);
793 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
795 * When FS is already mounted.
796 * 1. If you are here and if the device->name is NULL that
797 * means this device was missing at time of FS mount.
798 * 2. If you are here and if the device->name is different
799 * from 'path' that means either
800 * a. The same device disappeared and reappeared with
802 * b. The missing-disk-which-was-replaced, has
805 * We must allow 1 and 2a above. But 2b would be a spurious
808 * Further in case of 1 and 2a above, the disk at 'path'
809 * would have missed some transaction when it was away and
810 * in case of 2a the stale bdev has to be updated as well.
811 * 2b must not be allowed at all time.
815 * For now, we do allow update to btrfs_fs_device through the
816 * btrfs dev scan cli after FS has been mounted. We're still
817 * tracking a problem where systems fail mount by subvolume id
818 * when we reject replacement on a mounted FS.
820 if (!fs_devices
->opened
&& found_transid
< device
->generation
) {
822 * That is if the FS is _not_ mounted and if you
823 * are here, that means there is more than one
824 * disk with same uuid and devid.We keep the one
825 * with larger generation number or the last-in if
826 * generation are equal.
828 mutex_unlock(&fs_devices
->device_list_mutex
);
829 return ERR_PTR(-EEXIST
);
833 * We are going to replace the device path for a given devid,
834 * make sure it's the same device if the device is mounted
837 struct block_device
*path_bdev
;
839 path_bdev
= lookup_bdev(path
);
840 if (IS_ERR(path_bdev
)) {
841 mutex_unlock(&fs_devices
->device_list_mutex
);
842 return ERR_CAST(path_bdev
);
845 if (device
->bdev
!= path_bdev
) {
847 mutex_unlock(&fs_devices
->device_list_mutex
);
848 btrfs_warn_in_rcu(device
->fs_info
,
849 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
850 disk_super
->fsid
, devid
,
851 rcu_str_deref(device
->name
), path
);
852 return ERR_PTR(-EEXIST
);
855 btrfs_info_in_rcu(device
->fs_info
,
856 "device fsid %pU devid %llu moved old:%s new:%s",
857 disk_super
->fsid
, devid
,
858 rcu_str_deref(device
->name
), path
);
861 name
= rcu_string_strdup(path
, GFP_NOFS
);
863 mutex_unlock(&fs_devices
->device_list_mutex
);
864 return ERR_PTR(-ENOMEM
);
866 rcu_string_free(device
->name
);
867 rcu_assign_pointer(device
->name
, name
);
868 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
869 fs_devices
->missing_devices
--;
870 clear_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
875 * Unmount does not free the btrfs_device struct but would zero
876 * generation along with most of the other members. So just update
877 * it back. We need it to pick the disk with largest generation
880 if (!fs_devices
->opened
)
881 device
->generation
= found_transid
;
883 fs_devices
->total_devices
= btrfs_super_num_devices(disk_super
);
885 mutex_unlock(&fs_devices
->device_list_mutex
);
889 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
891 struct btrfs_fs_devices
*fs_devices
;
892 struct btrfs_device
*device
;
893 struct btrfs_device
*orig_dev
;
895 fs_devices
= alloc_fs_devices(orig
->fsid
);
896 if (IS_ERR(fs_devices
))
899 mutex_lock(&orig
->device_list_mutex
);
900 fs_devices
->total_devices
= orig
->total_devices
;
902 /* We have held the volume lock, it is safe to get the devices. */
903 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
904 struct rcu_string
*name
;
906 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
912 * This is ok to do without rcu read locked because we hold the
913 * uuid mutex so nothing we touch in here is going to disappear.
915 if (orig_dev
->name
) {
916 name
= rcu_string_strdup(orig_dev
->name
->str
,
919 btrfs_free_device(device
);
922 rcu_assign_pointer(device
->name
, name
);
925 list_add(&device
->dev_list
, &fs_devices
->devices
);
926 device
->fs_devices
= fs_devices
;
927 fs_devices
->num_devices
++;
929 mutex_unlock(&orig
->device_list_mutex
);
932 mutex_unlock(&orig
->device_list_mutex
);
933 free_fs_devices(fs_devices
);
934 return ERR_PTR(-ENOMEM
);
938 * After we have read the system tree and know devids belonging to
939 * this filesystem, remove the device which does not belong there.
941 void btrfs_free_extra_devids(struct btrfs_fs_devices
*fs_devices
, int step
)
943 struct btrfs_device
*device
, *next
;
944 struct btrfs_device
*latest_dev
= NULL
;
946 mutex_lock(&uuid_mutex
);
948 /* This is the initialized path, it is safe to release the devices. */
949 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
950 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
951 &device
->dev_state
)) {
952 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
953 &device
->dev_state
) &&
954 !test_bit(BTRFS_DEV_STATE_MISSING
,
955 &device
->dev_state
) &&
957 device
->generation
> latest_dev
->generation
)) {
963 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
965 * In the first step, keep the device which has
966 * the correct fsid and the devid that is used
967 * for the dev_replace procedure.
968 * In the second step, the dev_replace state is
969 * read from the device tree and it is known
970 * whether the procedure is really active or
971 * not, which means whether this device is
972 * used or whether it should be removed.
974 if (step
== 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
975 &device
->dev_state
)) {
980 blkdev_put(device
->bdev
, device
->mode
);
982 fs_devices
->open_devices
--;
984 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
985 list_del_init(&device
->dev_alloc_list
);
986 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
987 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
989 fs_devices
->rw_devices
--;
991 list_del_init(&device
->dev_list
);
992 fs_devices
->num_devices
--;
993 btrfs_free_device(device
);
996 if (fs_devices
->seed
) {
997 fs_devices
= fs_devices
->seed
;
1001 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1003 mutex_unlock(&uuid_mutex
);
1006 static void free_device_rcu(struct rcu_head
*head
)
1008 struct btrfs_device
*device
;
1010 device
= container_of(head
, struct btrfs_device
, rcu
);
1011 btrfs_free_device(device
);
1014 static void btrfs_close_bdev(struct btrfs_device
*device
)
1019 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1020 sync_blockdev(device
->bdev
);
1021 invalidate_bdev(device
->bdev
);
1024 blkdev_put(device
->bdev
, device
->mode
);
1027 static void btrfs_close_one_device(struct btrfs_device
*device
)
1029 struct btrfs_fs_devices
*fs_devices
= device
->fs_devices
;
1030 struct btrfs_device
*new_device
;
1031 struct rcu_string
*name
;
1034 fs_devices
->open_devices
--;
1036 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
1037 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
1038 list_del_init(&device
->dev_alloc_list
);
1039 fs_devices
->rw_devices
--;
1042 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
1043 fs_devices
->missing_devices
--;
1045 btrfs_close_bdev(device
);
1047 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
1049 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
1051 /* Safe because we are under uuid_mutex */
1053 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
1054 BUG_ON(!name
); /* -ENOMEM */
1055 rcu_assign_pointer(new_device
->name
, name
);
1058 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
1059 new_device
->fs_devices
= device
->fs_devices
;
1061 call_rcu(&device
->rcu
, free_device_rcu
);
1064 static int close_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1066 struct btrfs_device
*device
, *tmp
;
1068 if (--fs_devices
->opened
> 0)
1071 mutex_lock(&fs_devices
->device_list_mutex
);
1072 list_for_each_entry_safe(device
, tmp
, &fs_devices
->devices
, dev_list
) {
1073 btrfs_close_one_device(device
);
1075 mutex_unlock(&fs_devices
->device_list_mutex
);
1077 WARN_ON(fs_devices
->open_devices
);
1078 WARN_ON(fs_devices
->rw_devices
);
1079 fs_devices
->opened
= 0;
1080 fs_devices
->seeding
= 0;
1085 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
1087 struct btrfs_fs_devices
*seed_devices
= NULL
;
1090 mutex_lock(&uuid_mutex
);
1091 ret
= close_fs_devices(fs_devices
);
1092 if (!fs_devices
->opened
) {
1093 seed_devices
= fs_devices
->seed
;
1094 fs_devices
->seed
= NULL
;
1096 mutex_unlock(&uuid_mutex
);
1098 while (seed_devices
) {
1099 fs_devices
= seed_devices
;
1100 seed_devices
= fs_devices
->seed
;
1101 close_fs_devices(fs_devices
);
1102 free_fs_devices(fs_devices
);
1107 static int open_fs_devices(struct btrfs_fs_devices
*fs_devices
,
1108 fmode_t flags
, void *holder
)
1110 struct btrfs_device
*device
;
1111 struct btrfs_device
*latest_dev
= NULL
;
1114 flags
|= FMODE_EXCL
;
1116 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
1117 /* Just open everything we can; ignore failures here */
1118 if (btrfs_open_one_device(fs_devices
, device
, flags
, holder
))
1122 device
->generation
> latest_dev
->generation
)
1123 latest_dev
= device
;
1125 if (fs_devices
->open_devices
== 0) {
1129 fs_devices
->opened
= 1;
1130 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1131 fs_devices
->total_rw_bytes
= 0;
1136 static int devid_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1138 struct btrfs_device
*dev1
, *dev2
;
1140 dev1
= list_entry(a
, struct btrfs_device
, dev_list
);
1141 dev2
= list_entry(b
, struct btrfs_device
, dev_list
);
1143 if (dev1
->devid
< dev2
->devid
)
1145 else if (dev1
->devid
> dev2
->devid
)
1150 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
1151 fmode_t flags
, void *holder
)
1155 lockdep_assert_held(&uuid_mutex
);
1157 mutex_lock(&fs_devices
->device_list_mutex
);
1158 if (fs_devices
->opened
) {
1159 fs_devices
->opened
++;
1162 list_sort(NULL
, &fs_devices
->devices
, devid_cmp
);
1163 ret
= open_fs_devices(fs_devices
, flags
, holder
);
1165 mutex_unlock(&fs_devices
->device_list_mutex
);
1170 static void btrfs_release_disk_super(struct page
*page
)
1176 static int btrfs_read_disk_super(struct block_device
*bdev
, u64 bytenr
,
1178 struct btrfs_super_block
**disk_super
)
1183 /* make sure our super fits in the device */
1184 if (bytenr
+ PAGE_SIZE
>= i_size_read(bdev
->bd_inode
))
1187 /* make sure our super fits in the page */
1188 if (sizeof(**disk_super
) > PAGE_SIZE
)
1191 /* make sure our super doesn't straddle pages on disk */
1192 index
= bytenr
>> PAGE_SHIFT
;
1193 if ((bytenr
+ sizeof(**disk_super
) - 1) >> PAGE_SHIFT
!= index
)
1196 /* pull in the page with our super */
1197 *page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
1200 if (IS_ERR_OR_NULL(*page
))
1205 /* align our pointer to the offset of the super block */
1206 *disk_super
= p
+ (bytenr
& ~PAGE_MASK
);
1208 if (btrfs_super_bytenr(*disk_super
) != bytenr
||
1209 btrfs_super_magic(*disk_super
) != BTRFS_MAGIC
) {
1210 btrfs_release_disk_super(*page
);
1214 if ((*disk_super
)->label
[0] &&
1215 (*disk_super
)->label
[BTRFS_LABEL_SIZE
- 1])
1216 (*disk_super
)->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
1222 * Look for a btrfs signature on a device. This may be called out of the mount path
1223 * and we are not allowed to call set_blocksize during the scan. The superblock
1224 * is read via pagecache
1226 struct btrfs_device
*btrfs_scan_one_device(const char *path
, fmode_t flags
,
1229 struct btrfs_super_block
*disk_super
;
1230 bool new_device_added
= false;
1231 struct btrfs_device
*device
= NULL
;
1232 struct block_device
*bdev
;
1236 lockdep_assert_held(&uuid_mutex
);
1239 * we would like to check all the supers, but that would make
1240 * a btrfs mount succeed after a mkfs from a different FS.
1241 * So, we need to add a special mount option to scan for
1242 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1244 bytenr
= btrfs_sb_offset(0);
1245 flags
|= FMODE_EXCL
;
1247 bdev
= blkdev_get_by_path(path
, flags
, holder
);
1249 return ERR_CAST(bdev
);
1251 if (btrfs_read_disk_super(bdev
, bytenr
, &page
, &disk_super
)) {
1252 device
= ERR_PTR(-EINVAL
);
1253 goto error_bdev_put
;
1256 device
= device_list_add(path
, disk_super
, &new_device_added
);
1257 if (!IS_ERR(device
)) {
1258 if (new_device_added
)
1259 btrfs_free_stale_devices(path
, device
);
1262 btrfs_release_disk_super(page
);
1265 blkdev_put(bdev
, flags
);
1270 static int contains_pending_extent(struct btrfs_transaction
*transaction
,
1271 struct btrfs_device
*device
,
1272 u64
*start
, u64 len
)
1274 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1275 struct extent_map
*em
;
1276 struct list_head
*search_list
= &fs_info
->pinned_chunks
;
1278 u64 physical_start
= *start
;
1281 search_list
= &transaction
->pending_chunks
;
1283 list_for_each_entry(em
, search_list
, list
) {
1284 struct map_lookup
*map
;
1287 map
= em
->map_lookup
;
1288 for (i
= 0; i
< map
->num_stripes
; i
++) {
1291 if (map
->stripes
[i
].dev
!= device
)
1293 if (map
->stripes
[i
].physical
>= physical_start
+ len
||
1294 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1298 * Make sure that while processing the pinned list we do
1299 * not override our *start with a lower value, because
1300 * we can have pinned chunks that fall within this
1301 * device hole and that have lower physical addresses
1302 * than the pending chunks we processed before. If we
1303 * do not take this special care we can end up getting
1304 * 2 pending chunks that start at the same physical
1305 * device offsets because the end offset of a pinned
1306 * chunk can be equal to the start offset of some
1309 end
= map
->stripes
[i
].physical
+ em
->orig_block_len
;
1316 if (search_list
!= &fs_info
->pinned_chunks
) {
1317 search_list
= &fs_info
->pinned_chunks
;
1326 * find_free_dev_extent_start - find free space in the specified device
1327 * @device: the device which we search the free space in
1328 * @num_bytes: the size of the free space that we need
1329 * @search_start: the position from which to begin the search
1330 * @start: store the start of the free space.
1331 * @len: the size of the free space. that we find, or the size
1332 * of the max free space if we don't find suitable free space
1334 * this uses a pretty simple search, the expectation is that it is
1335 * called very infrequently and that a given device has a small number
1338 * @start is used to store the start of the free space if we find. But if we
1339 * don't find suitable free space, it will be used to store the start position
1340 * of the max free space.
1342 * @len is used to store the size of the free space that we find.
1343 * But if we don't find suitable free space, it is used to store the size of
1344 * the max free space.
1346 int find_free_dev_extent_start(struct btrfs_transaction
*transaction
,
1347 struct btrfs_device
*device
, u64 num_bytes
,
1348 u64 search_start
, u64
*start
, u64
*len
)
1350 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1351 struct btrfs_root
*root
= fs_info
->dev_root
;
1352 struct btrfs_key key
;
1353 struct btrfs_dev_extent
*dev_extent
;
1354 struct btrfs_path
*path
;
1359 u64 search_end
= device
->total_bytes
;
1362 struct extent_buffer
*l
;
1365 * We don't want to overwrite the superblock on the drive nor any area
1366 * used by the boot loader (grub for example), so we make sure to start
1367 * at an offset of at least 1MB.
1369 search_start
= max_t(u64
, search_start
, SZ_1M
);
1371 path
= btrfs_alloc_path();
1375 max_hole_start
= search_start
;
1379 if (search_start
>= search_end
||
1380 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1385 path
->reada
= READA_FORWARD
;
1386 path
->search_commit_root
= 1;
1387 path
->skip_locking
= 1;
1389 key
.objectid
= device
->devid
;
1390 key
.offset
= search_start
;
1391 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1393 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1397 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1404 slot
= path
->slots
[0];
1405 if (slot
>= btrfs_header_nritems(l
)) {
1406 ret
= btrfs_next_leaf(root
, path
);
1414 btrfs_item_key_to_cpu(l
, &key
, slot
);
1416 if (key
.objectid
< device
->devid
)
1419 if (key
.objectid
> device
->devid
)
1422 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1425 if (key
.offset
> search_start
) {
1426 hole_size
= key
.offset
- search_start
;
1429 * Have to check before we set max_hole_start, otherwise
1430 * we could end up sending back this offset anyway.
1432 if (contains_pending_extent(transaction
, device
,
1435 if (key
.offset
>= search_start
) {
1436 hole_size
= key
.offset
- search_start
;
1443 if (hole_size
> max_hole_size
) {
1444 max_hole_start
= search_start
;
1445 max_hole_size
= hole_size
;
1449 * If this free space is greater than which we need,
1450 * it must be the max free space that we have found
1451 * until now, so max_hole_start must point to the start
1452 * of this free space and the length of this free space
1453 * is stored in max_hole_size. Thus, we return
1454 * max_hole_start and max_hole_size and go back to the
1457 if (hole_size
>= num_bytes
) {
1463 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1464 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1466 if (extent_end
> search_start
)
1467 search_start
= extent_end
;
1474 * At this point, search_start should be the end of
1475 * allocated dev extents, and when shrinking the device,
1476 * search_end may be smaller than search_start.
1478 if (search_end
> search_start
) {
1479 hole_size
= search_end
- search_start
;
1481 if (contains_pending_extent(transaction
, device
, &search_start
,
1483 btrfs_release_path(path
);
1487 if (hole_size
> max_hole_size
) {
1488 max_hole_start
= search_start
;
1489 max_hole_size
= hole_size
;
1494 if (max_hole_size
< num_bytes
)
1500 btrfs_free_path(path
);
1501 *start
= max_hole_start
;
1503 *len
= max_hole_size
;
1507 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1508 struct btrfs_device
*device
, u64 num_bytes
,
1509 u64
*start
, u64
*len
)
1511 /* FIXME use last free of some kind */
1512 return find_free_dev_extent_start(trans
->transaction
, device
,
1513 num_bytes
, 0, start
, len
);
1516 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1517 struct btrfs_device
*device
,
1518 u64 start
, u64
*dev_extent_len
)
1520 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1521 struct btrfs_root
*root
= fs_info
->dev_root
;
1523 struct btrfs_path
*path
;
1524 struct btrfs_key key
;
1525 struct btrfs_key found_key
;
1526 struct extent_buffer
*leaf
= NULL
;
1527 struct btrfs_dev_extent
*extent
= NULL
;
1529 path
= btrfs_alloc_path();
1533 key
.objectid
= device
->devid
;
1535 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1537 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1539 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1540 BTRFS_DEV_EXTENT_KEY
);
1543 leaf
= path
->nodes
[0];
1544 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1545 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1546 struct btrfs_dev_extent
);
1547 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1548 btrfs_dev_extent_length(leaf
, extent
) < start
);
1550 btrfs_release_path(path
);
1552 } else if (ret
== 0) {
1553 leaf
= path
->nodes
[0];
1554 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1555 struct btrfs_dev_extent
);
1557 btrfs_handle_fs_error(fs_info
, ret
, "Slot search failed");
1561 *dev_extent_len
= btrfs_dev_extent_length(leaf
, extent
);
1563 ret
= btrfs_del_item(trans
, root
, path
);
1565 btrfs_handle_fs_error(fs_info
, ret
,
1566 "Failed to remove dev extent item");
1568 set_bit(BTRFS_TRANS_HAVE_FREE_BGS
, &trans
->transaction
->flags
);
1571 btrfs_free_path(path
);
1575 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1576 struct btrfs_device
*device
,
1577 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1580 struct btrfs_path
*path
;
1581 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1582 struct btrfs_root
*root
= fs_info
->dev_root
;
1583 struct btrfs_dev_extent
*extent
;
1584 struct extent_buffer
*leaf
;
1585 struct btrfs_key key
;
1587 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
));
1588 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
));
1589 path
= btrfs_alloc_path();
1593 key
.objectid
= device
->devid
;
1595 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1596 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1601 leaf
= path
->nodes
[0];
1602 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1603 struct btrfs_dev_extent
);
1604 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1605 BTRFS_CHUNK_TREE_OBJECTID
);
1606 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1607 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1608 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1610 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1611 btrfs_mark_buffer_dirty(leaf
);
1613 btrfs_free_path(path
);
1617 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1619 struct extent_map_tree
*em_tree
;
1620 struct extent_map
*em
;
1624 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1625 read_lock(&em_tree
->lock
);
1626 n
= rb_last(&em_tree
->map
);
1628 em
= rb_entry(n
, struct extent_map
, rb_node
);
1629 ret
= em
->start
+ em
->len
;
1631 read_unlock(&em_tree
->lock
);
1636 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1640 struct btrfs_key key
;
1641 struct btrfs_key found_key
;
1642 struct btrfs_path
*path
;
1644 path
= btrfs_alloc_path();
1648 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1649 key
.type
= BTRFS_DEV_ITEM_KEY
;
1650 key
.offset
= (u64
)-1;
1652 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1656 BUG_ON(ret
== 0); /* Corruption */
1658 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1659 BTRFS_DEV_ITEMS_OBJECTID
,
1660 BTRFS_DEV_ITEM_KEY
);
1664 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1666 *devid_ret
= found_key
.offset
+ 1;
1670 btrfs_free_path(path
);
1675 * the device information is stored in the chunk root
1676 * the btrfs_device struct should be fully filled in
1678 static int btrfs_add_dev_item(struct btrfs_trans_handle
*trans
,
1679 struct btrfs_device
*device
)
1682 struct btrfs_path
*path
;
1683 struct btrfs_dev_item
*dev_item
;
1684 struct extent_buffer
*leaf
;
1685 struct btrfs_key key
;
1688 path
= btrfs_alloc_path();
1692 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1693 key
.type
= BTRFS_DEV_ITEM_KEY
;
1694 key
.offset
= device
->devid
;
1696 ret
= btrfs_insert_empty_item(trans
, trans
->fs_info
->chunk_root
, path
,
1697 &key
, sizeof(*dev_item
));
1701 leaf
= path
->nodes
[0];
1702 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1704 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1705 btrfs_set_device_generation(leaf
, dev_item
, 0);
1706 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1707 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1708 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1709 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1710 btrfs_set_device_total_bytes(leaf
, dev_item
,
1711 btrfs_device_get_disk_total_bytes(device
));
1712 btrfs_set_device_bytes_used(leaf
, dev_item
,
1713 btrfs_device_get_bytes_used(device
));
1714 btrfs_set_device_group(leaf
, dev_item
, 0);
1715 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1716 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1717 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1719 ptr
= btrfs_device_uuid(dev_item
);
1720 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1721 ptr
= btrfs_device_fsid(dev_item
);
1722 write_extent_buffer(leaf
, trans
->fs_info
->fsid
, ptr
, BTRFS_FSID_SIZE
);
1723 btrfs_mark_buffer_dirty(leaf
);
1727 btrfs_free_path(path
);
1732 * Function to update ctime/mtime for a given device path.
1733 * Mainly used for ctime/mtime based probe like libblkid.
1735 static void update_dev_time(const char *path_name
)
1739 filp
= filp_open(path_name
, O_RDWR
, 0);
1742 file_update_time(filp
);
1743 filp_close(filp
, NULL
);
1746 static int btrfs_rm_dev_item(struct btrfs_fs_info
*fs_info
,
1747 struct btrfs_device
*device
)
1749 struct btrfs_root
*root
= fs_info
->chunk_root
;
1751 struct btrfs_path
*path
;
1752 struct btrfs_key key
;
1753 struct btrfs_trans_handle
*trans
;
1755 path
= btrfs_alloc_path();
1759 trans
= btrfs_start_transaction(root
, 0);
1760 if (IS_ERR(trans
)) {
1761 btrfs_free_path(path
);
1762 return PTR_ERR(trans
);
1764 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1765 key
.type
= BTRFS_DEV_ITEM_KEY
;
1766 key
.offset
= device
->devid
;
1768 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1772 btrfs_abort_transaction(trans
, ret
);
1773 btrfs_end_transaction(trans
);
1777 ret
= btrfs_del_item(trans
, root
, path
);
1779 btrfs_abort_transaction(trans
, ret
);
1780 btrfs_end_transaction(trans
);
1784 btrfs_free_path(path
);
1786 ret
= btrfs_commit_transaction(trans
);
1791 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1792 * filesystem. It's up to the caller to adjust that number regarding eg. device
1795 static int btrfs_check_raid_min_devices(struct btrfs_fs_info
*fs_info
,
1803 seq
= read_seqbegin(&fs_info
->profiles_lock
);
1805 all_avail
= fs_info
->avail_data_alloc_bits
|
1806 fs_info
->avail_system_alloc_bits
|
1807 fs_info
->avail_metadata_alloc_bits
;
1808 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
1810 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1811 if (!(all_avail
& btrfs_raid_array
[i
].bg_flag
))
1814 if (num_devices
< btrfs_raid_array
[i
].devs_min
) {
1815 int ret
= btrfs_raid_array
[i
].mindev_error
;
1825 static struct btrfs_device
* btrfs_find_next_active_device(
1826 struct btrfs_fs_devices
*fs_devs
, struct btrfs_device
*device
)
1828 struct btrfs_device
*next_device
;
1830 list_for_each_entry(next_device
, &fs_devs
->devices
, dev_list
) {
1831 if (next_device
!= device
&&
1832 !test_bit(BTRFS_DEV_STATE_MISSING
, &next_device
->dev_state
)
1833 && next_device
->bdev
)
1841 * Helper function to check if the given device is part of s_bdev / latest_bdev
1842 * and replace it with the provided or the next active device, in the context
1843 * where this function called, there should be always be another device (or
1844 * this_dev) which is active.
1846 void btrfs_assign_next_active_device(struct btrfs_device
*device
,
1847 struct btrfs_device
*this_dev
)
1849 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1850 struct btrfs_device
*next_device
;
1853 next_device
= this_dev
;
1855 next_device
= btrfs_find_next_active_device(fs_info
->fs_devices
,
1857 ASSERT(next_device
);
1859 if (fs_info
->sb
->s_bdev
&&
1860 (fs_info
->sb
->s_bdev
== device
->bdev
))
1861 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1863 if (fs_info
->fs_devices
->latest_bdev
== device
->bdev
)
1864 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1867 int btrfs_rm_device(struct btrfs_fs_info
*fs_info
, const char *device_path
,
1870 struct btrfs_device
*device
;
1871 struct btrfs_fs_devices
*cur_devices
;
1872 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
1876 mutex_lock(&uuid_mutex
);
1878 num_devices
= fs_devices
->num_devices
;
1879 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
1880 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
1881 WARN_ON(num_devices
< 1);
1884 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
1886 ret
= btrfs_check_raid_min_devices(fs_info
, num_devices
- 1);
1890 ret
= btrfs_find_device_by_devspec(fs_info
, devid
, device_path
,
1895 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1896 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1900 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
1901 fs_info
->fs_devices
->rw_devices
== 1) {
1902 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1906 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1907 mutex_lock(&fs_info
->chunk_mutex
);
1908 list_del_init(&device
->dev_alloc_list
);
1909 device
->fs_devices
->rw_devices
--;
1910 mutex_unlock(&fs_info
->chunk_mutex
);
1913 mutex_unlock(&uuid_mutex
);
1914 ret
= btrfs_shrink_device(device
, 0);
1915 mutex_lock(&uuid_mutex
);
1920 * TODO: the superblock still includes this device in its num_devices
1921 * counter although write_all_supers() is not locked out. This
1922 * could give a filesystem state which requires a degraded mount.
1924 ret
= btrfs_rm_dev_item(fs_info
, device
);
1928 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
1929 btrfs_scrub_cancel_dev(fs_info
, device
);
1932 * the device list mutex makes sure that we don't change
1933 * the device list while someone else is writing out all
1934 * the device supers. Whoever is writing all supers, should
1935 * lock the device list mutex before getting the number of
1936 * devices in the super block (super_copy). Conversely,
1937 * whoever updates the number of devices in the super block
1938 * (super_copy) should hold the device list mutex.
1942 * In normal cases the cur_devices == fs_devices. But in case
1943 * of deleting a seed device, the cur_devices should point to
1944 * its own fs_devices listed under the fs_devices->seed.
1946 cur_devices
= device
->fs_devices
;
1947 mutex_lock(&fs_devices
->device_list_mutex
);
1948 list_del_rcu(&device
->dev_list
);
1950 cur_devices
->num_devices
--;
1951 cur_devices
->total_devices
--;
1952 /* Update total_devices of the parent fs_devices if it's seed */
1953 if (cur_devices
!= fs_devices
)
1954 fs_devices
->total_devices
--;
1956 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
1957 cur_devices
->missing_devices
--;
1959 btrfs_assign_next_active_device(device
, NULL
);
1962 cur_devices
->open_devices
--;
1963 /* remove sysfs entry */
1964 btrfs_sysfs_rm_device_link(fs_devices
, device
);
1967 num_devices
= btrfs_super_num_devices(fs_info
->super_copy
) - 1;
1968 btrfs_set_super_num_devices(fs_info
->super_copy
, num_devices
);
1969 mutex_unlock(&fs_devices
->device_list_mutex
);
1972 * at this point, the device is zero sized and detached from
1973 * the devices list. All that's left is to zero out the old
1974 * supers and free the device.
1976 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
1977 btrfs_scratch_superblocks(device
->bdev
, device
->name
->str
);
1979 btrfs_close_bdev(device
);
1980 call_rcu(&device
->rcu
, free_device_rcu
);
1982 if (cur_devices
->open_devices
== 0) {
1983 while (fs_devices
) {
1984 if (fs_devices
->seed
== cur_devices
) {
1985 fs_devices
->seed
= cur_devices
->seed
;
1988 fs_devices
= fs_devices
->seed
;
1990 cur_devices
->seed
= NULL
;
1991 close_fs_devices(cur_devices
);
1992 free_fs_devices(cur_devices
);
1996 mutex_unlock(&uuid_mutex
);
2000 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
2001 mutex_lock(&fs_info
->chunk_mutex
);
2002 list_add(&device
->dev_alloc_list
,
2003 &fs_devices
->alloc_list
);
2004 device
->fs_devices
->rw_devices
++;
2005 mutex_unlock(&fs_info
->chunk_mutex
);
2010 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device
*srcdev
)
2012 struct btrfs_fs_devices
*fs_devices
;
2014 lockdep_assert_held(&srcdev
->fs_info
->fs_devices
->device_list_mutex
);
2017 * in case of fs with no seed, srcdev->fs_devices will point
2018 * to fs_devices of fs_info. However when the dev being replaced is
2019 * a seed dev it will point to the seed's local fs_devices. In short
2020 * srcdev will have its correct fs_devices in both the cases.
2022 fs_devices
= srcdev
->fs_devices
;
2024 list_del_rcu(&srcdev
->dev_list
);
2025 list_del(&srcdev
->dev_alloc_list
);
2026 fs_devices
->num_devices
--;
2027 if (test_bit(BTRFS_DEV_STATE_MISSING
, &srcdev
->dev_state
))
2028 fs_devices
->missing_devices
--;
2030 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
))
2031 fs_devices
->rw_devices
--;
2034 fs_devices
->open_devices
--;
2037 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info
*fs_info
,
2038 struct btrfs_device
*srcdev
)
2040 struct btrfs_fs_devices
*fs_devices
= srcdev
->fs_devices
;
2042 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
)) {
2043 /* zero out the old super if it is writable */
2044 btrfs_scratch_superblocks(srcdev
->bdev
, srcdev
->name
->str
);
2047 btrfs_close_bdev(srcdev
);
2048 call_rcu(&srcdev
->rcu
, free_device_rcu
);
2050 /* if this is no devs we rather delete the fs_devices */
2051 if (!fs_devices
->num_devices
) {
2052 struct btrfs_fs_devices
*tmp_fs_devices
;
2055 * On a mounted FS, num_devices can't be zero unless it's a
2056 * seed. In case of a seed device being replaced, the replace
2057 * target added to the sprout FS, so there will be no more
2058 * device left under the seed FS.
2060 ASSERT(fs_devices
->seeding
);
2062 tmp_fs_devices
= fs_info
->fs_devices
;
2063 while (tmp_fs_devices
) {
2064 if (tmp_fs_devices
->seed
== fs_devices
) {
2065 tmp_fs_devices
->seed
= fs_devices
->seed
;
2068 tmp_fs_devices
= tmp_fs_devices
->seed
;
2070 fs_devices
->seed
= NULL
;
2071 close_fs_devices(fs_devices
);
2072 free_fs_devices(fs_devices
);
2076 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device
*tgtdev
)
2078 struct btrfs_fs_devices
*fs_devices
= tgtdev
->fs_info
->fs_devices
;
2081 mutex_lock(&fs_devices
->device_list_mutex
);
2083 btrfs_sysfs_rm_device_link(fs_devices
, tgtdev
);
2086 fs_devices
->open_devices
--;
2088 fs_devices
->num_devices
--;
2090 btrfs_assign_next_active_device(tgtdev
, NULL
);
2092 list_del_rcu(&tgtdev
->dev_list
);
2094 mutex_unlock(&fs_devices
->device_list_mutex
);
2097 * The update_dev_time() with in btrfs_scratch_superblocks()
2098 * may lead to a call to btrfs_show_devname() which will try
2099 * to hold device_list_mutex. And here this device
2100 * is already out of device list, so we don't have to hold
2101 * the device_list_mutex lock.
2103 btrfs_scratch_superblocks(tgtdev
->bdev
, tgtdev
->name
->str
);
2105 btrfs_close_bdev(tgtdev
);
2106 call_rcu(&tgtdev
->rcu
, free_device_rcu
);
2109 static int btrfs_find_device_by_path(struct btrfs_fs_info
*fs_info
,
2110 const char *device_path
,
2111 struct btrfs_device
**device
)
2114 struct btrfs_super_block
*disk_super
;
2117 struct block_device
*bdev
;
2118 struct buffer_head
*bh
;
2121 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
2122 fs_info
->bdev_holder
, 0, &bdev
, &bh
);
2125 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
2126 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
2127 dev_uuid
= disk_super
->dev_item
.uuid
;
2128 *device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2129 disk_super
->fsid
, true);
2133 blkdev_put(bdev
, FMODE_READ
);
2137 int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info
*fs_info
,
2138 const char *device_path
,
2139 struct btrfs_device
**device
)
2142 if (strcmp(device_path
, "missing") == 0) {
2143 struct list_head
*devices
;
2144 struct btrfs_device
*tmp
;
2146 devices
= &fs_info
->fs_devices
->devices
;
2147 list_for_each_entry(tmp
, devices
, dev_list
) {
2148 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
2149 &tmp
->dev_state
) && !tmp
->bdev
) {
2156 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
2160 return btrfs_find_device_by_path(fs_info
, device_path
, device
);
2165 * Lookup a device given by device id, or the path if the id is 0.
2167 int btrfs_find_device_by_devspec(struct btrfs_fs_info
*fs_info
, u64 devid
,
2168 const char *devpath
,
2169 struct btrfs_device
**device
)
2175 *device
= btrfs_find_device(fs_info
->fs_devices
, devid
,
2180 if (!devpath
|| !devpath
[0])
2183 ret
= btrfs_find_device_missing_or_by_path(fs_info
, devpath
,
2190 * does all the dirty work required for changing file system's UUID.
2192 static int btrfs_prepare_sprout(struct btrfs_fs_info
*fs_info
)
2194 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2195 struct btrfs_fs_devices
*old_devices
;
2196 struct btrfs_fs_devices
*seed_devices
;
2197 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
2198 struct btrfs_device
*device
;
2201 lockdep_assert_held(&uuid_mutex
);
2202 if (!fs_devices
->seeding
)
2205 seed_devices
= alloc_fs_devices(NULL
);
2206 if (IS_ERR(seed_devices
))
2207 return PTR_ERR(seed_devices
);
2209 old_devices
= clone_fs_devices(fs_devices
);
2210 if (IS_ERR(old_devices
)) {
2211 kfree(seed_devices
);
2212 return PTR_ERR(old_devices
);
2215 list_add(&old_devices
->fs_list
, &fs_uuids
);
2217 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
2218 seed_devices
->opened
= 1;
2219 INIT_LIST_HEAD(&seed_devices
->devices
);
2220 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
2221 mutex_init(&seed_devices
->device_list_mutex
);
2223 mutex_lock(&fs_devices
->device_list_mutex
);
2224 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
2226 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
)
2227 device
->fs_devices
= seed_devices
;
2229 mutex_lock(&fs_info
->chunk_mutex
);
2230 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
2231 mutex_unlock(&fs_info
->chunk_mutex
);
2233 fs_devices
->seeding
= 0;
2234 fs_devices
->num_devices
= 0;
2235 fs_devices
->open_devices
= 0;
2236 fs_devices
->missing_devices
= 0;
2237 fs_devices
->rotating
= 0;
2238 fs_devices
->seed
= seed_devices
;
2240 generate_random_uuid(fs_devices
->fsid
);
2241 memcpy(fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2242 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2243 mutex_unlock(&fs_devices
->device_list_mutex
);
2245 super_flags
= btrfs_super_flags(disk_super
) &
2246 ~BTRFS_SUPER_FLAG_SEEDING
;
2247 btrfs_set_super_flags(disk_super
, super_flags
);
2253 * Store the expected generation for seed devices in device items.
2255 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
2256 struct btrfs_fs_info
*fs_info
)
2258 struct btrfs_root
*root
= fs_info
->chunk_root
;
2259 struct btrfs_path
*path
;
2260 struct extent_buffer
*leaf
;
2261 struct btrfs_dev_item
*dev_item
;
2262 struct btrfs_device
*device
;
2263 struct btrfs_key key
;
2264 u8 fs_uuid
[BTRFS_FSID_SIZE
];
2265 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2269 path
= btrfs_alloc_path();
2273 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2275 key
.type
= BTRFS_DEV_ITEM_KEY
;
2278 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2282 leaf
= path
->nodes
[0];
2284 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2285 ret
= btrfs_next_leaf(root
, path
);
2290 leaf
= path
->nodes
[0];
2291 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2292 btrfs_release_path(path
);
2296 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2297 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2298 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2301 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2302 struct btrfs_dev_item
);
2303 devid
= btrfs_device_id(leaf
, dev_item
);
2304 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2306 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2308 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2310 BUG_ON(!device
); /* Logic error */
2312 if (device
->fs_devices
->seeding
) {
2313 btrfs_set_device_generation(leaf
, dev_item
,
2314 device
->generation
);
2315 btrfs_mark_buffer_dirty(leaf
);
2323 btrfs_free_path(path
);
2327 int btrfs_init_new_device(struct btrfs_fs_info
*fs_info
, const char *device_path
)
2329 struct btrfs_root
*root
= fs_info
->dev_root
;
2330 struct request_queue
*q
;
2331 struct btrfs_trans_handle
*trans
;
2332 struct btrfs_device
*device
;
2333 struct block_device
*bdev
;
2334 struct super_block
*sb
= fs_info
->sb
;
2335 struct rcu_string
*name
;
2336 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2337 u64 orig_super_total_bytes
;
2338 u64 orig_super_num_devices
;
2339 int seeding_dev
= 0;
2341 bool unlocked
= false;
2343 if (sb_rdonly(sb
) && !fs_devices
->seeding
)
2346 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2347 fs_info
->bdev_holder
);
2349 return PTR_ERR(bdev
);
2351 if (fs_devices
->seeding
) {
2353 down_write(&sb
->s_umount
);
2354 mutex_lock(&uuid_mutex
);
2357 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2359 mutex_lock(&fs_devices
->device_list_mutex
);
2360 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
2361 if (device
->bdev
== bdev
) {
2364 &fs_devices
->device_list_mutex
);
2368 mutex_unlock(&fs_devices
->device_list_mutex
);
2370 device
= btrfs_alloc_device(fs_info
, NULL
, NULL
);
2371 if (IS_ERR(device
)) {
2372 /* we can safely leave the fs_devices entry around */
2373 ret
= PTR_ERR(device
);
2377 name
= rcu_string_strdup(device_path
, GFP_KERNEL
);
2380 goto error_free_device
;
2382 rcu_assign_pointer(device
->name
, name
);
2384 trans
= btrfs_start_transaction(root
, 0);
2385 if (IS_ERR(trans
)) {
2386 ret
= PTR_ERR(trans
);
2387 goto error_free_device
;
2390 q
= bdev_get_queue(bdev
);
2391 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
2392 device
->generation
= trans
->transid
;
2393 device
->io_width
= fs_info
->sectorsize
;
2394 device
->io_align
= fs_info
->sectorsize
;
2395 device
->sector_size
= fs_info
->sectorsize
;
2396 device
->total_bytes
= round_down(i_size_read(bdev
->bd_inode
),
2397 fs_info
->sectorsize
);
2398 device
->disk_total_bytes
= device
->total_bytes
;
2399 device
->commit_total_bytes
= device
->total_bytes
;
2400 device
->fs_info
= fs_info
;
2401 device
->bdev
= bdev
;
2402 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
2403 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
2404 device
->mode
= FMODE_EXCL
;
2405 device
->dev_stats_valid
= 1;
2406 set_blocksize(device
->bdev
, BTRFS_BDEV_BLOCKSIZE
);
2409 sb
->s_flags
&= ~SB_RDONLY
;
2410 ret
= btrfs_prepare_sprout(fs_info
);
2412 btrfs_abort_transaction(trans
, ret
);
2417 device
->fs_devices
= fs_devices
;
2419 mutex_lock(&fs_devices
->device_list_mutex
);
2420 mutex_lock(&fs_info
->chunk_mutex
);
2421 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
2422 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
2423 fs_devices
->num_devices
++;
2424 fs_devices
->open_devices
++;
2425 fs_devices
->rw_devices
++;
2426 fs_devices
->total_devices
++;
2427 fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2429 atomic64_add(device
->total_bytes
, &fs_info
->free_chunk_space
);
2431 if (!blk_queue_nonrot(q
))
2432 fs_devices
->rotating
= 1;
2434 orig_super_total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
2435 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2436 round_down(orig_super_total_bytes
+ device
->total_bytes
,
2437 fs_info
->sectorsize
));
2439 orig_super_num_devices
= btrfs_super_num_devices(fs_info
->super_copy
);
2440 btrfs_set_super_num_devices(fs_info
->super_copy
,
2441 orig_super_num_devices
+ 1);
2443 /* add sysfs device entry */
2444 btrfs_sysfs_add_device_link(fs_devices
, device
);
2447 * we've got more storage, clear any full flags on the space
2450 btrfs_clear_space_info_full(fs_info
);
2452 mutex_unlock(&fs_info
->chunk_mutex
);
2453 mutex_unlock(&fs_devices
->device_list_mutex
);
2456 mutex_lock(&fs_info
->chunk_mutex
);
2457 ret
= init_first_rw_device(trans
, fs_info
);
2458 mutex_unlock(&fs_info
->chunk_mutex
);
2460 btrfs_abort_transaction(trans
, ret
);
2465 ret
= btrfs_add_dev_item(trans
, device
);
2467 btrfs_abort_transaction(trans
, ret
);
2472 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2474 ret
= btrfs_finish_sprout(trans
, fs_info
);
2476 btrfs_abort_transaction(trans
, ret
);
2480 /* Sprouting would change fsid of the mounted root,
2481 * so rename the fsid on the sysfs
2483 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2485 if (kobject_rename(&fs_devices
->fsid_kobj
, fsid_buf
))
2487 "sysfs: failed to create fsid for sprout");
2490 ret
= btrfs_commit_transaction(trans
);
2493 mutex_unlock(&uuid_mutex
);
2494 up_write(&sb
->s_umount
);
2497 if (ret
) /* transaction commit */
2500 ret
= btrfs_relocate_sys_chunks(fs_info
);
2502 btrfs_handle_fs_error(fs_info
, ret
,
2503 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2504 trans
= btrfs_attach_transaction(root
);
2505 if (IS_ERR(trans
)) {
2506 if (PTR_ERR(trans
) == -ENOENT
)
2508 ret
= PTR_ERR(trans
);
2512 ret
= btrfs_commit_transaction(trans
);
2515 /* Update ctime/mtime for libblkid */
2516 update_dev_time(device_path
);
2520 btrfs_sysfs_rm_device_link(fs_devices
, device
);
2521 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
2522 mutex_lock(&fs_info
->chunk_mutex
);
2523 list_del_rcu(&device
->dev_list
);
2524 list_del(&device
->dev_alloc_list
);
2525 fs_info
->fs_devices
->num_devices
--;
2526 fs_info
->fs_devices
->open_devices
--;
2527 fs_info
->fs_devices
->rw_devices
--;
2528 fs_info
->fs_devices
->total_devices
--;
2529 fs_info
->fs_devices
->total_rw_bytes
-= device
->total_bytes
;
2530 atomic64_sub(device
->total_bytes
, &fs_info
->free_chunk_space
);
2531 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2532 orig_super_total_bytes
);
2533 btrfs_set_super_num_devices(fs_info
->super_copy
,
2534 orig_super_num_devices
);
2535 mutex_unlock(&fs_info
->chunk_mutex
);
2536 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
2539 sb
->s_flags
|= SB_RDONLY
;
2541 btrfs_end_transaction(trans
);
2543 btrfs_free_device(device
);
2545 blkdev_put(bdev
, FMODE_EXCL
);
2546 if (seeding_dev
&& !unlocked
) {
2547 mutex_unlock(&uuid_mutex
);
2548 up_write(&sb
->s_umount
);
2553 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2554 struct btrfs_device
*device
)
2557 struct btrfs_path
*path
;
2558 struct btrfs_root
*root
= device
->fs_info
->chunk_root
;
2559 struct btrfs_dev_item
*dev_item
;
2560 struct extent_buffer
*leaf
;
2561 struct btrfs_key key
;
2563 path
= btrfs_alloc_path();
2567 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2568 key
.type
= BTRFS_DEV_ITEM_KEY
;
2569 key
.offset
= device
->devid
;
2571 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2580 leaf
= path
->nodes
[0];
2581 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2583 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2584 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2585 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2586 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2587 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2588 btrfs_set_device_total_bytes(leaf
, dev_item
,
2589 btrfs_device_get_disk_total_bytes(device
));
2590 btrfs_set_device_bytes_used(leaf
, dev_item
,
2591 btrfs_device_get_bytes_used(device
));
2592 btrfs_mark_buffer_dirty(leaf
);
2595 btrfs_free_path(path
);
2599 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2600 struct btrfs_device
*device
, u64 new_size
)
2602 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
2603 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2604 struct btrfs_fs_devices
*fs_devices
;
2608 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
2611 new_size
= round_down(new_size
, fs_info
->sectorsize
);
2613 mutex_lock(&fs_info
->chunk_mutex
);
2614 old_total
= btrfs_super_total_bytes(super_copy
);
2615 diff
= round_down(new_size
- device
->total_bytes
, fs_info
->sectorsize
);
2617 if (new_size
<= device
->total_bytes
||
2618 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
2619 mutex_unlock(&fs_info
->chunk_mutex
);
2623 fs_devices
= fs_info
->fs_devices
;
2625 btrfs_set_super_total_bytes(super_copy
,
2626 round_down(old_total
+ diff
, fs_info
->sectorsize
));
2627 device
->fs_devices
->total_rw_bytes
+= diff
;
2629 btrfs_device_set_total_bytes(device
, new_size
);
2630 btrfs_device_set_disk_total_bytes(device
, new_size
);
2631 btrfs_clear_space_info_full(device
->fs_info
);
2632 if (list_empty(&device
->resized_list
))
2633 list_add_tail(&device
->resized_list
,
2634 &fs_devices
->resized_devices
);
2635 mutex_unlock(&fs_info
->chunk_mutex
);
2637 return btrfs_update_device(trans
, device
);
2640 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2642 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2643 struct btrfs_root
*root
= fs_info
->chunk_root
;
2645 struct btrfs_path
*path
;
2646 struct btrfs_key key
;
2648 path
= btrfs_alloc_path();
2652 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2653 key
.offset
= chunk_offset
;
2654 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2656 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2659 else if (ret
> 0) { /* Logic error or corruption */
2660 btrfs_handle_fs_error(fs_info
, -ENOENT
,
2661 "Failed lookup while freeing chunk.");
2666 ret
= btrfs_del_item(trans
, root
, path
);
2668 btrfs_handle_fs_error(fs_info
, ret
,
2669 "Failed to delete chunk item.");
2671 btrfs_free_path(path
);
2675 static int btrfs_del_sys_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2677 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2678 struct btrfs_disk_key
*disk_key
;
2679 struct btrfs_chunk
*chunk
;
2686 struct btrfs_key key
;
2688 mutex_lock(&fs_info
->chunk_mutex
);
2689 array_size
= btrfs_super_sys_array_size(super_copy
);
2691 ptr
= super_copy
->sys_chunk_array
;
2694 while (cur
< array_size
) {
2695 disk_key
= (struct btrfs_disk_key
*)ptr
;
2696 btrfs_disk_key_to_cpu(&key
, disk_key
);
2698 len
= sizeof(*disk_key
);
2700 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2701 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2702 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2703 len
+= btrfs_chunk_item_size(num_stripes
);
2708 if (key
.objectid
== BTRFS_FIRST_CHUNK_TREE_OBJECTID
&&
2709 key
.offset
== chunk_offset
) {
2710 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2712 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2718 mutex_unlock(&fs_info
->chunk_mutex
);
2722 static struct extent_map
*get_chunk_map(struct btrfs_fs_info
*fs_info
,
2723 u64 logical
, u64 length
)
2725 struct extent_map_tree
*em_tree
;
2726 struct extent_map
*em
;
2728 em_tree
= &fs_info
->mapping_tree
.map_tree
;
2729 read_lock(&em_tree
->lock
);
2730 em
= lookup_extent_mapping(em_tree
, logical
, length
);
2731 read_unlock(&em_tree
->lock
);
2734 btrfs_crit(fs_info
, "unable to find logical %llu length %llu",
2736 return ERR_PTR(-EINVAL
);
2739 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
2741 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2742 logical
, length
, em
->start
, em
->start
+ em
->len
);
2743 free_extent_map(em
);
2744 return ERR_PTR(-EINVAL
);
2747 /* callers are responsible for dropping em's ref. */
2751 int btrfs_remove_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2753 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2754 struct extent_map
*em
;
2755 struct map_lookup
*map
;
2756 u64 dev_extent_len
= 0;
2758 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2760 em
= get_chunk_map(fs_info
, chunk_offset
, 1);
2763 * This is a logic error, but we don't want to just rely on the
2764 * user having built with ASSERT enabled, so if ASSERT doesn't
2765 * do anything we still error out.
2770 map
= em
->map_lookup
;
2771 mutex_lock(&fs_info
->chunk_mutex
);
2772 check_system_chunk(trans
, map
->type
);
2773 mutex_unlock(&fs_info
->chunk_mutex
);
2776 * Take the device list mutex to prevent races with the final phase of
2777 * a device replace operation that replaces the device object associated
2778 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2780 mutex_lock(&fs_devices
->device_list_mutex
);
2781 for (i
= 0; i
< map
->num_stripes
; i
++) {
2782 struct btrfs_device
*device
= map
->stripes
[i
].dev
;
2783 ret
= btrfs_free_dev_extent(trans
, device
,
2784 map
->stripes
[i
].physical
,
2787 mutex_unlock(&fs_devices
->device_list_mutex
);
2788 btrfs_abort_transaction(trans
, ret
);
2792 if (device
->bytes_used
> 0) {
2793 mutex_lock(&fs_info
->chunk_mutex
);
2794 btrfs_device_set_bytes_used(device
,
2795 device
->bytes_used
- dev_extent_len
);
2796 atomic64_add(dev_extent_len
, &fs_info
->free_chunk_space
);
2797 btrfs_clear_space_info_full(fs_info
);
2798 mutex_unlock(&fs_info
->chunk_mutex
);
2801 if (map
->stripes
[i
].dev
) {
2802 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2804 mutex_unlock(&fs_devices
->device_list_mutex
);
2805 btrfs_abort_transaction(trans
, ret
);
2810 mutex_unlock(&fs_devices
->device_list_mutex
);
2812 ret
= btrfs_free_chunk(trans
, chunk_offset
);
2814 btrfs_abort_transaction(trans
, ret
);
2818 trace_btrfs_chunk_free(fs_info
, map
, chunk_offset
, em
->len
);
2820 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2821 ret
= btrfs_del_sys_chunk(fs_info
, chunk_offset
);
2823 btrfs_abort_transaction(trans
, ret
);
2828 ret
= btrfs_remove_block_group(trans
, chunk_offset
, em
);
2830 btrfs_abort_transaction(trans
, ret
);
2836 free_extent_map(em
);
2840 static int btrfs_relocate_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2842 struct btrfs_root
*root
= fs_info
->chunk_root
;
2843 struct btrfs_trans_handle
*trans
;
2847 * Prevent races with automatic removal of unused block groups.
2848 * After we relocate and before we remove the chunk with offset
2849 * chunk_offset, automatic removal of the block group can kick in,
2850 * resulting in a failure when calling btrfs_remove_chunk() below.
2852 * Make sure to acquire this mutex before doing a tree search (dev
2853 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2854 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2855 * we release the path used to search the chunk/dev tree and before
2856 * the current task acquires this mutex and calls us.
2858 lockdep_assert_held(&fs_info
->delete_unused_bgs_mutex
);
2860 ret
= btrfs_can_relocate(fs_info
, chunk_offset
);
2864 /* step one, relocate all the extents inside this chunk */
2865 btrfs_scrub_pause(fs_info
);
2866 ret
= btrfs_relocate_block_group(fs_info
, chunk_offset
);
2867 btrfs_scrub_continue(fs_info
);
2872 * We add the kobjects here (and after forcing data chunk creation)
2873 * since relocation is the only place we'll create chunks of a new
2874 * type at runtime. The only place where we'll remove the last
2875 * chunk of a type is the call immediately below this one. Even
2876 * so, we're protected against races with the cleaner thread since
2877 * we're covered by the delete_unused_bgs_mutex.
2879 btrfs_add_raid_kobjects(fs_info
);
2881 trans
= btrfs_start_trans_remove_block_group(root
->fs_info
,
2883 if (IS_ERR(trans
)) {
2884 ret
= PTR_ERR(trans
);
2885 btrfs_handle_fs_error(root
->fs_info
, ret
, NULL
);
2890 * step two, delete the device extents and the
2891 * chunk tree entries
2893 ret
= btrfs_remove_chunk(trans
, chunk_offset
);
2894 btrfs_end_transaction(trans
);
2898 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
)
2900 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2901 struct btrfs_path
*path
;
2902 struct extent_buffer
*leaf
;
2903 struct btrfs_chunk
*chunk
;
2904 struct btrfs_key key
;
2905 struct btrfs_key found_key
;
2907 bool retried
= false;
2911 path
= btrfs_alloc_path();
2916 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2917 key
.offset
= (u64
)-1;
2918 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2921 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
2922 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2924 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
2927 BUG_ON(ret
== 0); /* Corruption */
2929 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2932 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
2938 leaf
= path
->nodes
[0];
2939 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2941 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2942 struct btrfs_chunk
);
2943 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2944 btrfs_release_path(path
);
2946 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2947 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
2953 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
2955 if (found_key
.offset
== 0)
2957 key
.offset
= found_key
.offset
- 1;
2960 if (failed
&& !retried
) {
2964 } else if (WARN_ON(failed
&& retried
)) {
2968 btrfs_free_path(path
);
2973 * return 1 : allocate a data chunk successfully,
2974 * return <0: errors during allocating a data chunk,
2975 * return 0 : no need to allocate a data chunk.
2977 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info
*fs_info
,
2980 struct btrfs_block_group_cache
*cache
;
2984 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2986 chunk_type
= cache
->flags
;
2987 btrfs_put_block_group(cache
);
2989 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
) {
2990 spin_lock(&fs_info
->data_sinfo
->lock
);
2991 bytes_used
= fs_info
->data_sinfo
->bytes_used
;
2992 spin_unlock(&fs_info
->data_sinfo
->lock
);
2995 struct btrfs_trans_handle
*trans
;
2998 trans
= btrfs_join_transaction(fs_info
->tree_root
);
3000 return PTR_ERR(trans
);
3002 ret
= btrfs_force_chunk_alloc(trans
,
3003 BTRFS_BLOCK_GROUP_DATA
);
3004 btrfs_end_transaction(trans
);
3008 btrfs_add_raid_kobjects(fs_info
);
3016 static int insert_balance_item(struct btrfs_fs_info
*fs_info
,
3017 struct btrfs_balance_control
*bctl
)
3019 struct btrfs_root
*root
= fs_info
->tree_root
;
3020 struct btrfs_trans_handle
*trans
;
3021 struct btrfs_balance_item
*item
;
3022 struct btrfs_disk_balance_args disk_bargs
;
3023 struct btrfs_path
*path
;
3024 struct extent_buffer
*leaf
;
3025 struct btrfs_key key
;
3028 path
= btrfs_alloc_path();
3032 trans
= btrfs_start_transaction(root
, 0);
3033 if (IS_ERR(trans
)) {
3034 btrfs_free_path(path
);
3035 return PTR_ERR(trans
);
3038 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3039 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3042 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3047 leaf
= path
->nodes
[0];
3048 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3050 memzero_extent_buffer(leaf
, (unsigned long)item
, sizeof(*item
));
3052 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
3053 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
3054 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
3055 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
3056 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
3057 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
3059 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
3061 btrfs_mark_buffer_dirty(leaf
);
3063 btrfs_free_path(path
);
3064 err
= btrfs_commit_transaction(trans
);
3070 static int del_balance_item(struct btrfs_fs_info
*fs_info
)
3072 struct btrfs_root
*root
= fs_info
->tree_root
;
3073 struct btrfs_trans_handle
*trans
;
3074 struct btrfs_path
*path
;
3075 struct btrfs_key key
;
3078 path
= btrfs_alloc_path();
3082 trans
= btrfs_start_transaction(root
, 0);
3083 if (IS_ERR(trans
)) {
3084 btrfs_free_path(path
);
3085 return PTR_ERR(trans
);
3088 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3089 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3092 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3100 ret
= btrfs_del_item(trans
, root
, path
);
3102 btrfs_free_path(path
);
3103 err
= btrfs_commit_transaction(trans
);
3110 * This is a heuristic used to reduce the number of chunks balanced on
3111 * resume after balance was interrupted.
3113 static void update_balance_args(struct btrfs_balance_control
*bctl
)
3116 * Turn on soft mode for chunk types that were being converted.
3118 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3119 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3120 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3121 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3122 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3123 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3126 * Turn on usage filter if is not already used. The idea is
3127 * that chunks that we have already balanced should be
3128 * reasonably full. Don't do it for chunks that are being
3129 * converted - that will keep us from relocating unconverted
3130 * (albeit full) chunks.
3132 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3133 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3134 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3135 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3136 bctl
->data
.usage
= 90;
3138 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3139 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3140 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3141 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3142 bctl
->sys
.usage
= 90;
3144 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3145 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3146 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3147 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3148 bctl
->meta
.usage
= 90;
3153 * Clear the balance status in fs_info and delete the balance item from disk.
3155 static void reset_balance_state(struct btrfs_fs_info
*fs_info
)
3157 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3160 BUG_ON(!fs_info
->balance_ctl
);
3162 spin_lock(&fs_info
->balance_lock
);
3163 fs_info
->balance_ctl
= NULL
;
3164 spin_unlock(&fs_info
->balance_lock
);
3167 ret
= del_balance_item(fs_info
);
3169 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
3173 * Balance filters. Return 1 if chunk should be filtered out
3174 * (should not be balanced).
3176 static int chunk_profiles_filter(u64 chunk_type
,
3177 struct btrfs_balance_args
*bargs
)
3179 chunk_type
= chunk_to_extended(chunk_type
) &
3180 BTRFS_EXTENDED_PROFILE_MASK
;
3182 if (bargs
->profiles
& chunk_type
)
3188 static int chunk_usage_range_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
3189 struct btrfs_balance_args
*bargs
)
3191 struct btrfs_block_group_cache
*cache
;
3193 u64 user_thresh_min
;
3194 u64 user_thresh_max
;
3197 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3198 chunk_used
= btrfs_block_group_used(&cache
->item
);
3200 if (bargs
->usage_min
== 0)
3201 user_thresh_min
= 0;
3203 user_thresh_min
= div_factor_fine(cache
->key
.offset
,
3206 if (bargs
->usage_max
== 0)
3207 user_thresh_max
= 1;
3208 else if (bargs
->usage_max
> 100)
3209 user_thresh_max
= cache
->key
.offset
;
3211 user_thresh_max
= div_factor_fine(cache
->key
.offset
,
3214 if (user_thresh_min
<= chunk_used
&& chunk_used
< user_thresh_max
)
3217 btrfs_put_block_group(cache
);
3221 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
,
3222 u64 chunk_offset
, struct btrfs_balance_args
*bargs
)
3224 struct btrfs_block_group_cache
*cache
;
3225 u64 chunk_used
, user_thresh
;
3228 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3229 chunk_used
= btrfs_block_group_used(&cache
->item
);
3231 if (bargs
->usage_min
== 0)
3233 else if (bargs
->usage
> 100)
3234 user_thresh
= cache
->key
.offset
;
3236 user_thresh
= div_factor_fine(cache
->key
.offset
,
3239 if (chunk_used
< user_thresh
)
3242 btrfs_put_block_group(cache
);
3246 static int chunk_devid_filter(struct extent_buffer
*leaf
,
3247 struct btrfs_chunk
*chunk
,
3248 struct btrfs_balance_args
*bargs
)
3250 struct btrfs_stripe
*stripe
;
3251 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3254 for (i
= 0; i
< num_stripes
; i
++) {
3255 stripe
= btrfs_stripe_nr(chunk
, i
);
3256 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
3263 /* [pstart, pend) */
3264 static int chunk_drange_filter(struct extent_buffer
*leaf
,
3265 struct btrfs_chunk
*chunk
,
3266 struct btrfs_balance_args
*bargs
)
3268 struct btrfs_stripe
*stripe
;
3269 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3275 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
3278 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
3279 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
3280 factor
= num_stripes
/ 2;
3281 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
3282 factor
= num_stripes
- 1;
3283 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
3284 factor
= num_stripes
- 2;
3286 factor
= num_stripes
;
3289 for (i
= 0; i
< num_stripes
; i
++) {
3290 stripe
= btrfs_stripe_nr(chunk
, i
);
3291 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
3294 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
3295 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
3296 stripe_length
= div_u64(stripe_length
, factor
);
3298 if (stripe_offset
< bargs
->pend
&&
3299 stripe_offset
+ stripe_length
> bargs
->pstart
)
3306 /* [vstart, vend) */
3307 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
3308 struct btrfs_chunk
*chunk
,
3310 struct btrfs_balance_args
*bargs
)
3312 if (chunk_offset
< bargs
->vend
&&
3313 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
3314 /* at least part of the chunk is inside this vrange */
3320 static int chunk_stripes_range_filter(struct extent_buffer
*leaf
,
3321 struct btrfs_chunk
*chunk
,
3322 struct btrfs_balance_args
*bargs
)
3324 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3326 if (bargs
->stripes_min
<= num_stripes
3327 && num_stripes
<= bargs
->stripes_max
)
3333 static int chunk_soft_convert_filter(u64 chunk_type
,
3334 struct btrfs_balance_args
*bargs
)
3336 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3339 chunk_type
= chunk_to_extended(chunk_type
) &
3340 BTRFS_EXTENDED_PROFILE_MASK
;
3342 if (bargs
->target
== chunk_type
)
3348 static int should_balance_chunk(struct btrfs_fs_info
*fs_info
,
3349 struct extent_buffer
*leaf
,
3350 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3352 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3353 struct btrfs_balance_args
*bargs
= NULL
;
3354 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3357 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3358 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3362 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3363 bargs
= &bctl
->data
;
3364 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3366 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3367 bargs
= &bctl
->meta
;
3369 /* profiles filter */
3370 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3371 chunk_profiles_filter(chunk_type
, bargs
)) {
3376 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3377 chunk_usage_filter(fs_info
, chunk_offset
, bargs
)) {
3379 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3380 chunk_usage_range_filter(fs_info
, chunk_offset
, bargs
)) {
3385 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3386 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3390 /* drange filter, makes sense only with devid filter */
3391 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3392 chunk_drange_filter(leaf
, chunk
, bargs
)) {
3397 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3398 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3402 /* stripes filter */
3403 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_STRIPES_RANGE
) &&
3404 chunk_stripes_range_filter(leaf
, chunk
, bargs
)) {
3408 /* soft profile changing mode */
3409 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3410 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3415 * limited by count, must be the last filter
3417 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3418 if (bargs
->limit
== 0)
3422 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT_RANGE
)) {
3424 * Same logic as the 'limit' filter; the minimum cannot be
3425 * determined here because we do not have the global information
3426 * about the count of all chunks that satisfy the filters.
3428 if (bargs
->limit_max
== 0)
3437 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3439 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3440 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3441 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3442 struct list_head
*devices
;
3443 struct btrfs_device
*device
;
3447 struct btrfs_chunk
*chunk
;
3448 struct btrfs_path
*path
= NULL
;
3449 struct btrfs_key key
;
3450 struct btrfs_key found_key
;
3451 struct btrfs_trans_handle
*trans
;
3452 struct extent_buffer
*leaf
;
3455 int enospc_errors
= 0;
3456 bool counting
= true;
3457 /* The single value limit and min/max limits use the same bytes in the */
3458 u64 limit_data
= bctl
->data
.limit
;
3459 u64 limit_meta
= bctl
->meta
.limit
;
3460 u64 limit_sys
= bctl
->sys
.limit
;
3464 int chunk_reserved
= 0;
3466 /* step one make some room on all the devices */
3467 devices
= &fs_info
->fs_devices
->devices
;
3468 list_for_each_entry(device
, devices
, dev_list
) {
3469 old_size
= btrfs_device_get_total_bytes(device
);
3470 size_to_free
= div_factor(old_size
, 1);
3471 size_to_free
= min_t(u64
, size_to_free
, SZ_1M
);
3472 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) ||
3473 btrfs_device_get_total_bytes(device
) -
3474 btrfs_device_get_bytes_used(device
) > size_to_free
||
3475 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
3478 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3482 /* btrfs_shrink_device never returns ret > 0 */
3487 trans
= btrfs_start_transaction(dev_root
, 0);
3488 if (IS_ERR(trans
)) {
3489 ret
= PTR_ERR(trans
);
3490 btrfs_info_in_rcu(fs_info
,
3491 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3492 rcu_str_deref(device
->name
), ret
,
3493 old_size
, old_size
- size_to_free
);
3497 ret
= btrfs_grow_device(trans
, device
, old_size
);
3499 btrfs_end_transaction(trans
);
3500 /* btrfs_grow_device never returns ret > 0 */
3502 btrfs_info_in_rcu(fs_info
,
3503 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3504 rcu_str_deref(device
->name
), ret
,
3505 old_size
, old_size
- size_to_free
);
3509 btrfs_end_transaction(trans
);
3512 /* step two, relocate all the chunks */
3513 path
= btrfs_alloc_path();
3519 /* zero out stat counters */
3520 spin_lock(&fs_info
->balance_lock
);
3521 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3522 spin_unlock(&fs_info
->balance_lock
);
3526 * The single value limit and min/max limits use the same bytes
3529 bctl
->data
.limit
= limit_data
;
3530 bctl
->meta
.limit
= limit_meta
;
3531 bctl
->sys
.limit
= limit_sys
;
3533 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3534 key
.offset
= (u64
)-1;
3535 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3538 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3539 atomic_read(&fs_info
->balance_cancel_req
)) {
3544 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
3545 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3547 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3552 * this shouldn't happen, it means the last relocate
3556 BUG(); /* FIXME break ? */
3558 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3559 BTRFS_CHUNK_ITEM_KEY
);
3561 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3566 leaf
= path
->nodes
[0];
3567 slot
= path
->slots
[0];
3568 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3570 if (found_key
.objectid
!= key
.objectid
) {
3571 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3575 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3576 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3579 spin_lock(&fs_info
->balance_lock
);
3580 bctl
->stat
.considered
++;
3581 spin_unlock(&fs_info
->balance_lock
);
3584 ret
= should_balance_chunk(fs_info
, leaf
, chunk
,
3587 btrfs_release_path(path
);
3589 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3594 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3595 spin_lock(&fs_info
->balance_lock
);
3596 bctl
->stat
.expected
++;
3597 spin_unlock(&fs_info
->balance_lock
);
3599 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3601 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3603 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3610 * Apply limit_min filter, no need to check if the LIMITS
3611 * filter is used, limit_min is 0 by default
3613 if (((chunk_type
& BTRFS_BLOCK_GROUP_DATA
) &&
3614 count_data
< bctl
->data
.limit_min
)
3615 || ((chunk_type
& BTRFS_BLOCK_GROUP_METADATA
) &&
3616 count_meta
< bctl
->meta
.limit_min
)
3617 || ((chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
3618 count_sys
< bctl
->sys
.limit_min
)) {
3619 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3623 if (!chunk_reserved
) {
3625 * We may be relocating the only data chunk we have,
3626 * which could potentially end up with losing data's
3627 * raid profile, so lets allocate an empty one in
3630 ret
= btrfs_may_alloc_data_chunk(fs_info
,
3633 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3635 } else if (ret
== 1) {
3640 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
3641 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3642 if (ret
&& ret
!= -ENOSPC
)
3644 if (ret
== -ENOSPC
) {
3647 spin_lock(&fs_info
->balance_lock
);
3648 bctl
->stat
.completed
++;
3649 spin_unlock(&fs_info
->balance_lock
);
3652 if (found_key
.offset
== 0)
3654 key
.offset
= found_key
.offset
- 1;
3658 btrfs_release_path(path
);
3663 btrfs_free_path(path
);
3664 if (enospc_errors
) {
3665 btrfs_info(fs_info
, "%d enospc errors during balance",
3675 * alloc_profile_is_valid - see if a given profile is valid and reduced
3676 * @flags: profile to validate
3677 * @extended: if true @flags is treated as an extended profile
3679 static int alloc_profile_is_valid(u64 flags
, int extended
)
3681 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3682 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3684 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3686 /* 1) check that all other bits are zeroed */
3690 /* 2) see if profile is reduced */
3692 return !extended
; /* "0" is valid for usual profiles */
3694 /* true if exactly one bit set */
3695 return (flags
& (flags
- 1)) == 0;
3698 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3700 /* cancel requested || normal exit path */
3701 return atomic_read(&fs_info
->balance_cancel_req
) ||
3702 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3703 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3706 /* Non-zero return value signifies invalidity */
3707 static inline int validate_convert_profile(struct btrfs_balance_args
*bctl_arg
,
3710 return ((bctl_arg
->flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3711 (!alloc_profile_is_valid(bctl_arg
->target
, 1) ||
3712 (bctl_arg
->target
& ~allowed
)));
3716 * Should be called with balance mutexe held
3718 int btrfs_balance(struct btrfs_fs_info
*fs_info
,
3719 struct btrfs_balance_control
*bctl
,
3720 struct btrfs_ioctl_balance_args
*bargs
)
3722 u64 meta_target
, data_target
;
3728 bool reducing_integrity
;
3730 if (btrfs_fs_closing(fs_info
) ||
3731 atomic_read(&fs_info
->balance_pause_req
) ||
3732 atomic_read(&fs_info
->balance_cancel_req
)) {
3737 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3738 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3742 * In case of mixed groups both data and meta should be picked,
3743 * and identical options should be given for both of them.
3745 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3746 if (mixed
&& (bctl
->flags
& allowed
)) {
3747 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3748 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3749 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3751 "balance: mixed groups data and metadata options must be the same");
3757 num_devices
= fs_info
->fs_devices
->num_devices
;
3758 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
3759 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3760 BUG_ON(num_devices
< 1);
3763 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
3764 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
| BTRFS_BLOCK_GROUP_DUP
;
3765 if (num_devices
> 1)
3766 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3767 if (num_devices
> 2)
3768 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3769 if (num_devices
> 3)
3770 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3771 BTRFS_BLOCK_GROUP_RAID6
);
3772 if (validate_convert_profile(&bctl
->data
, allowed
)) {
3773 int index
= btrfs_bg_flags_to_raid_index(bctl
->data
.target
);
3776 "balance: invalid convert data profile %s",
3777 get_raid_name(index
));
3781 if (validate_convert_profile(&bctl
->meta
, allowed
)) {
3782 int index
= btrfs_bg_flags_to_raid_index(bctl
->meta
.target
);
3785 "balance: invalid convert metadata profile %s",
3786 get_raid_name(index
));
3790 if (validate_convert_profile(&bctl
->sys
, allowed
)) {
3791 int index
= btrfs_bg_flags_to_raid_index(bctl
->sys
.target
);
3794 "balance: invalid convert system profile %s",
3795 get_raid_name(index
));
3800 /* allow to reduce meta or sys integrity only if force set */
3801 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3802 BTRFS_BLOCK_GROUP_RAID10
|
3803 BTRFS_BLOCK_GROUP_RAID5
|
3804 BTRFS_BLOCK_GROUP_RAID6
;
3806 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3808 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3809 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3810 !(bctl
->sys
.target
& allowed
)) ||
3811 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3812 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3813 !(bctl
->meta
.target
& allowed
)))
3814 reducing_integrity
= true;
3816 reducing_integrity
= false;
3818 /* if we're not converting, the target field is uninitialized */
3819 meta_target
= (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
3820 bctl
->meta
.target
: fs_info
->avail_metadata_alloc_bits
;
3821 data_target
= (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
3822 bctl
->data
.target
: fs_info
->avail_data_alloc_bits
;
3823 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3825 if (reducing_integrity
) {
3826 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3828 "balance: force reducing metadata integrity");
3831 "balance: reduces metadata integrity, use --force if you want this");
3837 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target
) <
3838 btrfs_get_num_tolerated_disk_barrier_failures(data_target
)) {
3839 int meta_index
= btrfs_bg_flags_to_raid_index(meta_target
);
3840 int data_index
= btrfs_bg_flags_to_raid_index(data_target
);
3843 "balance: metadata profile %s has lower redundancy than data profile %s",
3844 get_raid_name(meta_index
), get_raid_name(data_index
));
3847 ret
= insert_balance_item(fs_info
, bctl
);
3848 if (ret
&& ret
!= -EEXIST
)
3851 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3852 BUG_ON(ret
== -EEXIST
);
3853 BUG_ON(fs_info
->balance_ctl
);
3854 spin_lock(&fs_info
->balance_lock
);
3855 fs_info
->balance_ctl
= bctl
;
3856 spin_unlock(&fs_info
->balance_lock
);
3858 BUG_ON(ret
!= -EEXIST
);
3859 spin_lock(&fs_info
->balance_lock
);
3860 update_balance_args(bctl
);
3861 spin_unlock(&fs_info
->balance_lock
);
3864 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
3865 set_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
3866 mutex_unlock(&fs_info
->balance_mutex
);
3868 ret
= __btrfs_balance(fs_info
);
3870 mutex_lock(&fs_info
->balance_mutex
);
3871 clear_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
3874 memset(bargs
, 0, sizeof(*bargs
));
3875 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
3878 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3879 balance_need_close(fs_info
)) {
3880 reset_balance_state(fs_info
);
3881 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3884 wake_up(&fs_info
->balance_wait_q
);
3888 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3889 reset_balance_state(fs_info
);
3892 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
3897 static int balance_kthread(void *data
)
3899 struct btrfs_fs_info
*fs_info
= data
;
3902 mutex_lock(&fs_info
->balance_mutex
);
3903 if (fs_info
->balance_ctl
) {
3904 btrfs_info(fs_info
, "balance: resuming");
3905 ret
= btrfs_balance(fs_info
, fs_info
->balance_ctl
, NULL
);
3907 mutex_unlock(&fs_info
->balance_mutex
);
3912 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3914 struct task_struct
*tsk
;
3916 mutex_lock(&fs_info
->balance_mutex
);
3917 if (!fs_info
->balance_ctl
) {
3918 mutex_unlock(&fs_info
->balance_mutex
);
3921 mutex_unlock(&fs_info
->balance_mutex
);
3923 if (btrfs_test_opt(fs_info
, SKIP_BALANCE
)) {
3924 btrfs_info(fs_info
, "balance: resume skipped");
3929 * A ro->rw remount sequence should continue with the paused balance
3930 * regardless of who pauses it, system or the user as of now, so set
3933 spin_lock(&fs_info
->balance_lock
);
3934 fs_info
->balance_ctl
->flags
|= BTRFS_BALANCE_RESUME
;
3935 spin_unlock(&fs_info
->balance_lock
);
3937 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3938 return PTR_ERR_OR_ZERO(tsk
);
3941 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3943 struct btrfs_balance_control
*bctl
;
3944 struct btrfs_balance_item
*item
;
3945 struct btrfs_disk_balance_args disk_bargs
;
3946 struct btrfs_path
*path
;
3947 struct extent_buffer
*leaf
;
3948 struct btrfs_key key
;
3951 path
= btrfs_alloc_path();
3955 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3956 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3959 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3962 if (ret
> 0) { /* ret = -ENOENT; */
3967 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3973 leaf
= path
->nodes
[0];
3974 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3976 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3977 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3979 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3980 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3981 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3982 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3983 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3984 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3987 * This should never happen, as the paused balance state is recovered
3988 * during mount without any chance of other exclusive ops to collide.
3990 * This gives the exclusive op status to balance and keeps in paused
3991 * state until user intervention (cancel or umount). If the ownership
3992 * cannot be assigned, show a message but do not fail. The balance
3993 * is in a paused state and must have fs_info::balance_ctl properly
3996 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
))
3998 "balance: cannot set exclusive op status, resume manually");
4000 mutex_lock(&fs_info
->balance_mutex
);
4001 BUG_ON(fs_info
->balance_ctl
);
4002 spin_lock(&fs_info
->balance_lock
);
4003 fs_info
->balance_ctl
= bctl
;
4004 spin_unlock(&fs_info
->balance_lock
);
4005 mutex_unlock(&fs_info
->balance_mutex
);
4007 btrfs_free_path(path
);
4011 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
4015 mutex_lock(&fs_info
->balance_mutex
);
4016 if (!fs_info
->balance_ctl
) {
4017 mutex_unlock(&fs_info
->balance_mutex
);
4021 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4022 atomic_inc(&fs_info
->balance_pause_req
);
4023 mutex_unlock(&fs_info
->balance_mutex
);
4025 wait_event(fs_info
->balance_wait_q
,
4026 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4028 mutex_lock(&fs_info
->balance_mutex
);
4029 /* we are good with balance_ctl ripped off from under us */
4030 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4031 atomic_dec(&fs_info
->balance_pause_req
);
4036 mutex_unlock(&fs_info
->balance_mutex
);
4040 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
4042 mutex_lock(&fs_info
->balance_mutex
);
4043 if (!fs_info
->balance_ctl
) {
4044 mutex_unlock(&fs_info
->balance_mutex
);
4049 * A paused balance with the item stored on disk can be resumed at
4050 * mount time if the mount is read-write. Otherwise it's still paused
4051 * and we must not allow cancelling as it deletes the item.
4053 if (sb_rdonly(fs_info
->sb
)) {
4054 mutex_unlock(&fs_info
->balance_mutex
);
4058 atomic_inc(&fs_info
->balance_cancel_req
);
4060 * if we are running just wait and return, balance item is
4061 * deleted in btrfs_balance in this case
4063 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4064 mutex_unlock(&fs_info
->balance_mutex
);
4065 wait_event(fs_info
->balance_wait_q
,
4066 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4067 mutex_lock(&fs_info
->balance_mutex
);
4069 mutex_unlock(&fs_info
->balance_mutex
);
4071 * Lock released to allow other waiters to continue, we'll
4072 * reexamine the status again.
4074 mutex_lock(&fs_info
->balance_mutex
);
4076 if (fs_info
->balance_ctl
) {
4077 reset_balance_state(fs_info
);
4078 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4079 btrfs_info(fs_info
, "balance: canceled");
4083 BUG_ON(fs_info
->balance_ctl
||
4084 test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4085 atomic_dec(&fs_info
->balance_cancel_req
);
4086 mutex_unlock(&fs_info
->balance_mutex
);
4090 static int btrfs_uuid_scan_kthread(void *data
)
4092 struct btrfs_fs_info
*fs_info
= data
;
4093 struct btrfs_root
*root
= fs_info
->tree_root
;
4094 struct btrfs_key key
;
4095 struct btrfs_path
*path
= NULL
;
4097 struct extent_buffer
*eb
;
4099 struct btrfs_root_item root_item
;
4101 struct btrfs_trans_handle
*trans
= NULL
;
4103 path
= btrfs_alloc_path();
4110 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4114 ret
= btrfs_search_forward(root
, &key
, path
,
4115 BTRFS_OLDEST_GENERATION
);
4122 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
4123 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
4124 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
4125 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
4128 eb
= path
->nodes
[0];
4129 slot
= path
->slots
[0];
4130 item_size
= btrfs_item_size_nr(eb
, slot
);
4131 if (item_size
< sizeof(root_item
))
4134 read_extent_buffer(eb
, &root_item
,
4135 btrfs_item_ptr_offset(eb
, slot
),
4136 (int)sizeof(root_item
));
4137 if (btrfs_root_refs(&root_item
) == 0)
4140 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
4141 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4145 btrfs_release_path(path
);
4147 * 1 - subvol uuid item
4148 * 1 - received_subvol uuid item
4150 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
4151 if (IS_ERR(trans
)) {
4152 ret
= PTR_ERR(trans
);
4160 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
4161 ret
= btrfs_uuid_tree_add(trans
, root_item
.uuid
,
4162 BTRFS_UUID_KEY_SUBVOL
,
4165 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4171 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4172 ret
= btrfs_uuid_tree_add(trans
,
4173 root_item
.received_uuid
,
4174 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4177 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4185 ret
= btrfs_end_transaction(trans
);
4191 btrfs_release_path(path
);
4192 if (key
.offset
< (u64
)-1) {
4194 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
4196 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4197 } else if (key
.objectid
< (u64
)-1) {
4199 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4208 btrfs_free_path(path
);
4209 if (trans
&& !IS_ERR(trans
))
4210 btrfs_end_transaction(trans
);
4212 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
4214 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN
, &fs_info
->flags
);
4215 up(&fs_info
->uuid_tree_rescan_sem
);
4220 * Callback for btrfs_uuid_tree_iterate().
4222 * 0 check succeeded, the entry is not outdated.
4223 * < 0 if an error occurred.
4224 * > 0 if the check failed, which means the caller shall remove the entry.
4226 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
4227 u8
*uuid
, u8 type
, u64 subid
)
4229 struct btrfs_key key
;
4231 struct btrfs_root
*subvol_root
;
4233 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
4234 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
4237 key
.objectid
= subid
;
4238 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4239 key
.offset
= (u64
)-1;
4240 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4241 if (IS_ERR(subvol_root
)) {
4242 ret
= PTR_ERR(subvol_root
);
4249 case BTRFS_UUID_KEY_SUBVOL
:
4250 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
4253 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
4254 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
4264 static int btrfs_uuid_rescan_kthread(void *data
)
4266 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
4270 * 1st step is to iterate through the existing UUID tree and
4271 * to delete all entries that contain outdated data.
4272 * 2nd step is to add all missing entries to the UUID tree.
4274 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
4276 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
4277 up(&fs_info
->uuid_tree_rescan_sem
);
4280 return btrfs_uuid_scan_kthread(data
);
4283 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
4285 struct btrfs_trans_handle
*trans
;
4286 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
4287 struct btrfs_root
*uuid_root
;
4288 struct task_struct
*task
;
4295 trans
= btrfs_start_transaction(tree_root
, 2);
4297 return PTR_ERR(trans
);
4299 uuid_root
= btrfs_create_tree(trans
, fs_info
,
4300 BTRFS_UUID_TREE_OBJECTID
);
4301 if (IS_ERR(uuid_root
)) {
4302 ret
= PTR_ERR(uuid_root
);
4303 btrfs_abort_transaction(trans
, ret
);
4304 btrfs_end_transaction(trans
);
4308 fs_info
->uuid_root
= uuid_root
;
4310 ret
= btrfs_commit_transaction(trans
);
4314 down(&fs_info
->uuid_tree_rescan_sem
);
4315 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
4317 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4318 btrfs_warn(fs_info
, "failed to start uuid_scan task");
4319 up(&fs_info
->uuid_tree_rescan_sem
);
4320 return PTR_ERR(task
);
4326 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
4328 struct task_struct
*task
;
4330 down(&fs_info
->uuid_tree_rescan_sem
);
4331 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
4333 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4334 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
4335 up(&fs_info
->uuid_tree_rescan_sem
);
4336 return PTR_ERR(task
);
4343 * shrinking a device means finding all of the device extents past
4344 * the new size, and then following the back refs to the chunks.
4345 * The chunk relocation code actually frees the device extent
4347 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
4349 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
4350 struct btrfs_root
*root
= fs_info
->dev_root
;
4351 struct btrfs_trans_handle
*trans
;
4352 struct btrfs_dev_extent
*dev_extent
= NULL
;
4353 struct btrfs_path
*path
;
4359 bool retried
= false;
4360 bool checked_pending_chunks
= false;
4361 struct extent_buffer
*l
;
4362 struct btrfs_key key
;
4363 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4364 u64 old_total
= btrfs_super_total_bytes(super_copy
);
4365 u64 old_size
= btrfs_device_get_total_bytes(device
);
4368 new_size
= round_down(new_size
, fs_info
->sectorsize
);
4369 diff
= round_down(old_size
- new_size
, fs_info
->sectorsize
);
4371 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4374 path
= btrfs_alloc_path();
4378 path
->reada
= READA_BACK
;
4380 mutex_lock(&fs_info
->chunk_mutex
);
4382 btrfs_device_set_total_bytes(device
, new_size
);
4383 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4384 device
->fs_devices
->total_rw_bytes
-= diff
;
4385 atomic64_sub(diff
, &fs_info
->free_chunk_space
);
4387 mutex_unlock(&fs_info
->chunk_mutex
);
4390 key
.objectid
= device
->devid
;
4391 key
.offset
= (u64
)-1;
4392 key
.type
= BTRFS_DEV_EXTENT_KEY
;
4395 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
4396 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4398 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4402 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
4404 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4409 btrfs_release_path(path
);
4414 slot
= path
->slots
[0];
4415 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
4417 if (key
.objectid
!= device
->devid
) {
4418 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4419 btrfs_release_path(path
);
4423 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
4424 length
= btrfs_dev_extent_length(l
, dev_extent
);
4426 if (key
.offset
+ length
<= new_size
) {
4427 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4428 btrfs_release_path(path
);
4432 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
4433 btrfs_release_path(path
);
4436 * We may be relocating the only data chunk we have,
4437 * which could potentially end up with losing data's
4438 * raid profile, so lets allocate an empty one in
4441 ret
= btrfs_may_alloc_data_chunk(fs_info
, chunk_offset
);
4443 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4447 ret
= btrfs_relocate_chunk(fs_info
, chunk_offset
);
4448 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4449 if (ret
&& ret
!= -ENOSPC
)
4453 } while (key
.offset
-- > 0);
4455 if (failed
&& !retried
) {
4459 } else if (failed
&& retried
) {
4464 /* Shrinking succeeded, else we would be at "done". */
4465 trans
= btrfs_start_transaction(root
, 0);
4466 if (IS_ERR(trans
)) {
4467 ret
= PTR_ERR(trans
);
4471 mutex_lock(&fs_info
->chunk_mutex
);
4474 * We checked in the above loop all device extents that were already in
4475 * the device tree. However before we have updated the device's
4476 * total_bytes to the new size, we might have had chunk allocations that
4477 * have not complete yet (new block groups attached to transaction
4478 * handles), and therefore their device extents were not yet in the
4479 * device tree and we missed them in the loop above. So if we have any
4480 * pending chunk using a device extent that overlaps the device range
4481 * that we can not use anymore, commit the current transaction and
4482 * repeat the search on the device tree - this way we guarantee we will
4483 * not have chunks using device extents that end beyond 'new_size'.
4485 if (!checked_pending_chunks
) {
4486 u64 start
= new_size
;
4487 u64 len
= old_size
- new_size
;
4489 if (contains_pending_extent(trans
->transaction
, device
,
4491 mutex_unlock(&fs_info
->chunk_mutex
);
4492 checked_pending_chunks
= true;
4495 ret
= btrfs_commit_transaction(trans
);
4502 btrfs_device_set_disk_total_bytes(device
, new_size
);
4503 if (list_empty(&device
->resized_list
))
4504 list_add_tail(&device
->resized_list
,
4505 &fs_info
->fs_devices
->resized_devices
);
4507 WARN_ON(diff
> old_total
);
4508 btrfs_set_super_total_bytes(super_copy
,
4509 round_down(old_total
- diff
, fs_info
->sectorsize
));
4510 mutex_unlock(&fs_info
->chunk_mutex
);
4512 /* Now btrfs_update_device() will change the on-disk size. */
4513 ret
= btrfs_update_device(trans
, device
);
4515 btrfs_abort_transaction(trans
, ret
);
4516 btrfs_end_transaction(trans
);
4518 ret
= btrfs_commit_transaction(trans
);
4521 btrfs_free_path(path
);
4523 mutex_lock(&fs_info
->chunk_mutex
);
4524 btrfs_device_set_total_bytes(device
, old_size
);
4525 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
4526 device
->fs_devices
->total_rw_bytes
+= diff
;
4527 atomic64_add(diff
, &fs_info
->free_chunk_space
);
4528 mutex_unlock(&fs_info
->chunk_mutex
);
4533 static int btrfs_add_system_chunk(struct btrfs_fs_info
*fs_info
,
4534 struct btrfs_key
*key
,
4535 struct btrfs_chunk
*chunk
, int item_size
)
4537 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4538 struct btrfs_disk_key disk_key
;
4542 mutex_lock(&fs_info
->chunk_mutex
);
4543 array_size
= btrfs_super_sys_array_size(super_copy
);
4544 if (array_size
+ item_size
+ sizeof(disk_key
)
4545 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
4546 mutex_unlock(&fs_info
->chunk_mutex
);
4550 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4551 btrfs_cpu_key_to_disk(&disk_key
, key
);
4552 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4553 ptr
+= sizeof(disk_key
);
4554 memcpy(ptr
, chunk
, item_size
);
4555 item_size
+= sizeof(disk_key
);
4556 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4557 mutex_unlock(&fs_info
->chunk_mutex
);
4563 * sort the devices in descending order by max_avail, total_avail
4565 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4567 const struct btrfs_device_info
*di_a
= a
;
4568 const struct btrfs_device_info
*di_b
= b
;
4570 if (di_a
->max_avail
> di_b
->max_avail
)
4572 if (di_a
->max_avail
< di_b
->max_avail
)
4574 if (di_a
->total_avail
> di_b
->total_avail
)
4576 if (di_a
->total_avail
< di_b
->total_avail
)
4581 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4583 if (!(type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
4586 btrfs_set_fs_incompat(info
, RAID56
);
4589 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
4590 - sizeof(struct btrfs_chunk)) \
4591 / sizeof(struct btrfs_stripe) + 1)
4593 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4594 - 2 * sizeof(struct btrfs_disk_key) \
4595 - 2 * sizeof(struct btrfs_chunk)) \
4596 / sizeof(struct btrfs_stripe) + 1)
4598 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4599 u64 start
, u64 type
)
4601 struct btrfs_fs_info
*info
= trans
->fs_info
;
4602 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4603 struct btrfs_device
*device
;
4604 struct map_lookup
*map
= NULL
;
4605 struct extent_map_tree
*em_tree
;
4606 struct extent_map
*em
;
4607 struct btrfs_device_info
*devices_info
= NULL
;
4609 int num_stripes
; /* total number of stripes to allocate */
4610 int data_stripes
; /* number of stripes that count for
4612 int sub_stripes
; /* sub_stripes info for map */
4613 int dev_stripes
; /* stripes per dev */
4614 int devs_max
; /* max devs to use */
4615 int devs_min
; /* min devs needed */
4616 int devs_increment
; /* ndevs has to be a multiple of this */
4617 int ncopies
; /* how many copies to data has */
4619 u64 max_stripe_size
;
4628 BUG_ON(!alloc_profile_is_valid(type
, 0));
4630 if (list_empty(&fs_devices
->alloc_list
)) {
4631 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
4632 btrfs_debug(info
, "%s: no writable device", __func__
);
4636 index
= btrfs_bg_flags_to_raid_index(type
);
4638 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4639 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4640 devs_max
= btrfs_raid_array
[index
].devs_max
;
4641 devs_min
= btrfs_raid_array
[index
].devs_min
;
4642 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4643 ncopies
= btrfs_raid_array
[index
].ncopies
;
4645 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4646 max_stripe_size
= SZ_1G
;
4647 max_chunk_size
= BTRFS_MAX_DATA_CHUNK_SIZE
;
4649 devs_max
= BTRFS_MAX_DEVS(info
);
4650 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4651 /* for larger filesystems, use larger metadata chunks */
4652 if (fs_devices
->total_rw_bytes
> 50ULL * SZ_1G
)
4653 max_stripe_size
= SZ_1G
;
4655 max_stripe_size
= SZ_256M
;
4656 max_chunk_size
= max_stripe_size
;
4658 devs_max
= BTRFS_MAX_DEVS(info
);
4659 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4660 max_stripe_size
= SZ_32M
;
4661 max_chunk_size
= 2 * max_stripe_size
;
4663 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4665 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4670 /* we don't want a chunk larger than 10% of writeable space */
4671 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4674 devices_info
= kcalloc(fs_devices
->rw_devices
, sizeof(*devices_info
),
4680 * in the first pass through the devices list, we gather information
4681 * about the available holes on each device.
4684 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
4688 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4690 "BTRFS: read-only device in alloc_list\n");
4694 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
4695 &device
->dev_state
) ||
4696 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4699 if (device
->total_bytes
> device
->bytes_used
)
4700 total_avail
= device
->total_bytes
- device
->bytes_used
;
4704 /* If there is no space on this device, skip it. */
4705 if (total_avail
== 0)
4708 ret
= find_free_dev_extent(trans
, device
,
4709 max_stripe_size
* dev_stripes
,
4710 &dev_offset
, &max_avail
);
4711 if (ret
&& ret
!= -ENOSPC
)
4715 max_avail
= max_stripe_size
* dev_stripes
;
4717 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
) {
4718 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
4720 "%s: devid %llu has no free space, have=%llu want=%u",
4721 __func__
, device
->devid
, max_avail
,
4722 BTRFS_STRIPE_LEN
* dev_stripes
);
4726 if (ndevs
== fs_devices
->rw_devices
) {
4727 WARN(1, "%s: found more than %llu devices\n",
4728 __func__
, fs_devices
->rw_devices
);
4731 devices_info
[ndevs
].dev_offset
= dev_offset
;
4732 devices_info
[ndevs
].max_avail
= max_avail
;
4733 devices_info
[ndevs
].total_avail
= total_avail
;
4734 devices_info
[ndevs
].dev
= device
;
4739 * now sort the devices by hole size / available space
4741 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4742 btrfs_cmp_device_info
, NULL
);
4744 /* round down to number of usable stripes */
4745 ndevs
= round_down(ndevs
, devs_increment
);
4747 if (ndevs
< devs_min
) {
4749 if (btrfs_test_opt(info
, ENOSPC_DEBUG
)) {
4751 "%s: not enough devices with free space: have=%d minimum required=%d",
4752 __func__
, ndevs
, devs_min
);
4757 ndevs
= min(ndevs
, devs_max
);
4760 * The primary goal is to maximize the number of stripes, so use as
4761 * many devices as possible, even if the stripes are not maximum sized.
4763 * The DUP profile stores more than one stripe per device, the
4764 * max_avail is the total size so we have to adjust.
4766 stripe_size
= div_u64(devices_info
[ndevs
- 1].max_avail
, dev_stripes
);
4767 num_stripes
= ndevs
* dev_stripes
;
4770 * this will have to be fixed for RAID1 and RAID10 over
4773 data_stripes
= num_stripes
/ ncopies
;
4775 if (type
& BTRFS_BLOCK_GROUP_RAID5
)
4776 data_stripes
= num_stripes
- 1;
4778 if (type
& BTRFS_BLOCK_GROUP_RAID6
)
4779 data_stripes
= num_stripes
- 2;
4782 * Use the number of data stripes to figure out how big this chunk
4783 * is really going to be in terms of logical address space,
4784 * and compare that answer with the max chunk size. If it's higher,
4785 * we try to reduce stripe_size.
4787 if (stripe_size
* data_stripes
> max_chunk_size
) {
4789 * Reduce stripe_size, round it up to a 16MB boundary again and
4790 * then use it, unless it ends up being even bigger than the
4791 * previous value we had already.
4793 stripe_size
= min(round_up(div_u64(max_chunk_size
,
4794 data_stripes
), SZ_16M
),
4798 /* align to BTRFS_STRIPE_LEN */
4799 stripe_size
= round_down(stripe_size
, BTRFS_STRIPE_LEN
);
4801 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4806 map
->num_stripes
= num_stripes
;
4808 for (i
= 0; i
< ndevs
; ++i
) {
4809 for (j
= 0; j
< dev_stripes
; ++j
) {
4810 int s
= i
* dev_stripes
+ j
;
4811 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4812 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4816 map
->stripe_len
= BTRFS_STRIPE_LEN
;
4817 map
->io_align
= BTRFS_STRIPE_LEN
;
4818 map
->io_width
= BTRFS_STRIPE_LEN
;
4820 map
->sub_stripes
= sub_stripes
;
4822 num_bytes
= stripe_size
* data_stripes
;
4824 trace_btrfs_chunk_alloc(info
, map
, start
, num_bytes
);
4826 em
= alloc_extent_map();
4832 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4833 em
->map_lookup
= map
;
4835 em
->len
= num_bytes
;
4836 em
->block_start
= 0;
4837 em
->block_len
= em
->len
;
4838 em
->orig_block_len
= stripe_size
;
4840 em_tree
= &info
->mapping_tree
.map_tree
;
4841 write_lock(&em_tree
->lock
);
4842 ret
= add_extent_mapping(em_tree
, em
, 0);
4844 write_unlock(&em_tree
->lock
);
4845 free_extent_map(em
);
4849 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4850 refcount_inc(&em
->refs
);
4851 write_unlock(&em_tree
->lock
);
4853 ret
= btrfs_make_block_group(trans
, 0, type
, start
, num_bytes
);
4855 goto error_del_extent
;
4857 for (i
= 0; i
< map
->num_stripes
; i
++) {
4858 num_bytes
= map
->stripes
[i
].dev
->bytes_used
+ stripe_size
;
4859 btrfs_device_set_bytes_used(map
->stripes
[i
].dev
, num_bytes
);
4860 map
->stripes
[i
].dev
->has_pending_chunks
= true;
4863 atomic64_sub(stripe_size
* map
->num_stripes
, &info
->free_chunk_space
);
4865 free_extent_map(em
);
4866 check_raid56_incompat_flag(info
, type
);
4868 kfree(devices_info
);
4872 write_lock(&em_tree
->lock
);
4873 remove_extent_mapping(em_tree
, em
);
4874 write_unlock(&em_tree
->lock
);
4876 /* One for our allocation */
4877 free_extent_map(em
);
4878 /* One for the tree reference */
4879 free_extent_map(em
);
4880 /* One for the pending_chunks list reference */
4881 free_extent_map(em
);
4883 kfree(devices_info
);
4887 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4888 u64 chunk_offset
, u64 chunk_size
)
4890 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
4891 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4892 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
4893 struct btrfs_key key
;
4894 struct btrfs_device
*device
;
4895 struct btrfs_chunk
*chunk
;
4896 struct btrfs_stripe
*stripe
;
4897 struct extent_map
*em
;
4898 struct map_lookup
*map
;
4905 em
= get_chunk_map(fs_info
, chunk_offset
, chunk_size
);
4909 map
= em
->map_lookup
;
4910 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4911 stripe_size
= em
->orig_block_len
;
4913 chunk
= kzalloc(item_size
, GFP_NOFS
);
4920 * Take the device list mutex to prevent races with the final phase of
4921 * a device replace operation that replaces the device object associated
4922 * with the map's stripes, because the device object's id can change
4923 * at any time during that final phase of the device replace operation
4924 * (dev-replace.c:btrfs_dev_replace_finishing()).
4926 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
4927 for (i
= 0; i
< map
->num_stripes
; i
++) {
4928 device
= map
->stripes
[i
].dev
;
4929 dev_offset
= map
->stripes
[i
].physical
;
4931 ret
= btrfs_update_device(trans
, device
);
4934 ret
= btrfs_alloc_dev_extent(trans
, device
, chunk_offset
,
4935 dev_offset
, stripe_size
);
4940 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4944 stripe
= &chunk
->stripe
;
4945 for (i
= 0; i
< map
->num_stripes
; i
++) {
4946 device
= map
->stripes
[i
].dev
;
4947 dev_offset
= map
->stripes
[i
].physical
;
4949 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4950 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4951 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4954 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4956 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4957 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4958 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4959 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4960 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4961 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4962 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4963 btrfs_set_stack_chunk_sector_size(chunk
, fs_info
->sectorsize
);
4964 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4966 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4967 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4968 key
.offset
= chunk_offset
;
4970 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4971 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4973 * TODO: Cleanup of inserted chunk root in case of
4976 ret
= btrfs_add_system_chunk(fs_info
, &key
, chunk
, item_size
);
4981 free_extent_map(em
);
4986 * Chunk allocation falls into two parts. The first part does works
4987 * that make the new allocated chunk useable, but not do any operation
4988 * that modifies the chunk tree. The second part does the works that
4989 * require modifying the chunk tree. This division is important for the
4990 * bootstrap process of adding storage to a seed btrfs.
4992 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
, u64 type
)
4996 lockdep_assert_held(&trans
->fs_info
->chunk_mutex
);
4997 chunk_offset
= find_next_chunk(trans
->fs_info
);
4998 return __btrfs_alloc_chunk(trans
, chunk_offset
, type
);
5001 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
5002 struct btrfs_fs_info
*fs_info
)
5005 u64 sys_chunk_offset
;
5009 chunk_offset
= find_next_chunk(fs_info
);
5010 alloc_profile
= btrfs_metadata_alloc_profile(fs_info
);
5011 ret
= __btrfs_alloc_chunk(trans
, chunk_offset
, alloc_profile
);
5015 sys_chunk_offset
= find_next_chunk(fs_info
);
5016 alloc_profile
= btrfs_system_alloc_profile(fs_info
);
5017 ret
= __btrfs_alloc_chunk(trans
, sys_chunk_offset
, alloc_profile
);
5021 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
5025 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
5026 BTRFS_BLOCK_GROUP_RAID10
|
5027 BTRFS_BLOCK_GROUP_RAID5
)) {
5029 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
5038 int btrfs_chunk_readonly(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
5040 struct extent_map
*em
;
5041 struct map_lookup
*map
;
5046 em
= get_chunk_map(fs_info
, chunk_offset
, 1);
5050 map
= em
->map_lookup
;
5051 for (i
= 0; i
< map
->num_stripes
; i
++) {
5052 if (test_bit(BTRFS_DEV_STATE_MISSING
,
5053 &map
->stripes
[i
].dev
->dev_state
)) {
5057 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
,
5058 &map
->stripes
[i
].dev
->dev_state
)) {
5065 * If the number of missing devices is larger than max errors,
5066 * we can not write the data into that chunk successfully, so
5069 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
5072 free_extent_map(em
);
5076 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
5078 extent_map_tree_init(&tree
->map_tree
);
5081 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
5083 struct extent_map
*em
;
5086 write_lock(&tree
->map_tree
.lock
);
5087 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
5089 remove_extent_mapping(&tree
->map_tree
, em
);
5090 write_unlock(&tree
->map_tree
.lock
);
5094 free_extent_map(em
);
5095 /* once for the tree */
5096 free_extent_map(em
);
5100 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5102 struct extent_map
*em
;
5103 struct map_lookup
*map
;
5106 em
= get_chunk_map(fs_info
, logical
, len
);
5109 * We could return errors for these cases, but that could get
5110 * ugly and we'd probably do the same thing which is just not do
5111 * anything else and exit, so return 1 so the callers don't try
5112 * to use other copies.
5116 map
= em
->map_lookup
;
5117 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
5118 ret
= map
->num_stripes
;
5119 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5120 ret
= map
->sub_stripes
;
5121 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
5123 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5125 * There could be two corrupted data stripes, we need
5126 * to loop retry in order to rebuild the correct data.
5128 * Fail a stripe at a time on every retry except the
5129 * stripe under reconstruction.
5131 ret
= map
->num_stripes
;
5134 free_extent_map(em
);
5136 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
5137 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
) &&
5138 fs_info
->dev_replace
.tgtdev
)
5140 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
5145 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info
*fs_info
,
5148 struct extent_map
*em
;
5149 struct map_lookup
*map
;
5150 unsigned long len
= fs_info
->sectorsize
;
5152 em
= get_chunk_map(fs_info
, logical
, len
);
5154 if (!WARN_ON(IS_ERR(em
))) {
5155 map
= em
->map_lookup
;
5156 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5157 len
= map
->stripe_len
* nr_data_stripes(map
);
5158 free_extent_map(em
);
5163 int btrfs_is_parity_mirror(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5165 struct extent_map
*em
;
5166 struct map_lookup
*map
;
5169 em
= get_chunk_map(fs_info
, logical
, len
);
5171 if(!WARN_ON(IS_ERR(em
))) {
5172 map
= em
->map_lookup
;
5173 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5175 free_extent_map(em
);
5180 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
5181 struct map_lookup
*map
, int first
,
5182 int dev_replace_is_ongoing
)
5186 int preferred_mirror
;
5188 struct btrfs_device
*srcdev
;
5191 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)));
5193 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5194 num_stripes
= map
->sub_stripes
;
5196 num_stripes
= map
->num_stripes
;
5198 preferred_mirror
= first
+ current
->pid
% num_stripes
;
5200 if (dev_replace_is_ongoing
&&
5201 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
5202 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
5203 srcdev
= fs_info
->dev_replace
.srcdev
;
5208 * try to avoid the drive that is the source drive for a
5209 * dev-replace procedure, only choose it if no other non-missing
5210 * mirror is available
5212 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
5213 if (map
->stripes
[preferred_mirror
].dev
->bdev
&&
5214 (tolerance
|| map
->stripes
[preferred_mirror
].dev
!= srcdev
))
5215 return preferred_mirror
;
5216 for (i
= first
; i
< first
+ num_stripes
; i
++) {
5217 if (map
->stripes
[i
].dev
->bdev
&&
5218 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
5223 /* we couldn't find one that doesn't fail. Just return something
5224 * and the io error handling code will clean up eventually
5226 return preferred_mirror
;
5229 static inline int parity_smaller(u64 a
, u64 b
)
5234 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5235 static void sort_parity_stripes(struct btrfs_bio
*bbio
, int num_stripes
)
5237 struct btrfs_bio_stripe s
;
5244 for (i
= 0; i
< num_stripes
- 1; i
++) {
5245 if (parity_smaller(bbio
->raid_map
[i
],
5246 bbio
->raid_map
[i
+1])) {
5247 s
= bbio
->stripes
[i
];
5248 l
= bbio
->raid_map
[i
];
5249 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
5250 bbio
->raid_map
[i
] = bbio
->raid_map
[i
+1];
5251 bbio
->stripes
[i
+1] = s
;
5252 bbio
->raid_map
[i
+1] = l
;
5260 static struct btrfs_bio
*alloc_btrfs_bio(int total_stripes
, int real_stripes
)
5262 struct btrfs_bio
*bbio
= kzalloc(
5263 /* the size of the btrfs_bio */
5264 sizeof(struct btrfs_bio
) +
5265 /* plus the variable array for the stripes */
5266 sizeof(struct btrfs_bio_stripe
) * (total_stripes
) +
5267 /* plus the variable array for the tgt dev */
5268 sizeof(int) * (real_stripes
) +
5270 * plus the raid_map, which includes both the tgt dev
5273 sizeof(u64
) * (total_stripes
),
5274 GFP_NOFS
|__GFP_NOFAIL
);
5276 atomic_set(&bbio
->error
, 0);
5277 refcount_set(&bbio
->refs
, 1);
5282 void btrfs_get_bbio(struct btrfs_bio
*bbio
)
5284 WARN_ON(!refcount_read(&bbio
->refs
));
5285 refcount_inc(&bbio
->refs
);
5288 void btrfs_put_bbio(struct btrfs_bio
*bbio
)
5292 if (refcount_dec_and_test(&bbio
->refs
))
5296 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5298 * Please note that, discard won't be sent to target device of device
5301 static int __btrfs_map_block_for_discard(struct btrfs_fs_info
*fs_info
,
5302 u64 logical
, u64 length
,
5303 struct btrfs_bio
**bbio_ret
)
5305 struct extent_map
*em
;
5306 struct map_lookup
*map
;
5307 struct btrfs_bio
*bbio
;
5311 u64 stripe_end_offset
;
5318 u32 sub_stripes
= 0;
5319 u64 stripes_per_dev
= 0;
5320 u32 remaining_stripes
= 0;
5321 u32 last_stripe
= 0;
5325 /* discard always return a bbio */
5328 em
= get_chunk_map(fs_info
, logical
, length
);
5332 map
= em
->map_lookup
;
5333 /* we don't discard raid56 yet */
5334 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5339 offset
= logical
- em
->start
;
5340 length
= min_t(u64
, em
->len
- offset
, length
);
5342 stripe_len
= map
->stripe_len
;
5344 * stripe_nr counts the total number of stripes we have to stride
5345 * to get to this block
5347 stripe_nr
= div64_u64(offset
, stripe_len
);
5349 /* stripe_offset is the offset of this block in its stripe */
5350 stripe_offset
= offset
- stripe_nr
* stripe_len
;
5352 stripe_nr_end
= round_up(offset
+ length
, map
->stripe_len
);
5353 stripe_nr_end
= div64_u64(stripe_nr_end
, map
->stripe_len
);
5354 stripe_cnt
= stripe_nr_end
- stripe_nr
;
5355 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5358 * after this, stripe_nr is the number of stripes on this
5359 * device we have to walk to find the data, and stripe_index is
5360 * the number of our device in the stripe array
5364 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5365 BTRFS_BLOCK_GROUP_RAID10
)) {
5366 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5369 sub_stripes
= map
->sub_stripes
;
5371 factor
= map
->num_stripes
/ sub_stripes
;
5372 num_stripes
= min_t(u64
, map
->num_stripes
,
5373 sub_stripes
* stripe_cnt
);
5374 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5375 stripe_index
*= sub_stripes
;
5376 stripes_per_dev
= div_u64_rem(stripe_cnt
, factor
,
5377 &remaining_stripes
);
5378 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5379 last_stripe
*= sub_stripes
;
5380 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
5381 BTRFS_BLOCK_GROUP_DUP
)) {
5382 num_stripes
= map
->num_stripes
;
5384 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5388 bbio
= alloc_btrfs_bio(num_stripes
, 0);
5394 for (i
= 0; i
< num_stripes
; i
++) {
5395 bbio
->stripes
[i
].physical
=
5396 map
->stripes
[stripe_index
].physical
+
5397 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5398 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5400 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5401 BTRFS_BLOCK_GROUP_RAID10
)) {
5402 bbio
->stripes
[i
].length
= stripes_per_dev
*
5405 if (i
/ sub_stripes
< remaining_stripes
)
5406 bbio
->stripes
[i
].length
+=
5410 * Special for the first stripe and
5413 * |-------|...|-------|
5417 if (i
< sub_stripes
)
5418 bbio
->stripes
[i
].length
-=
5421 if (stripe_index
>= last_stripe
&&
5422 stripe_index
<= (last_stripe
+
5424 bbio
->stripes
[i
].length
-=
5427 if (i
== sub_stripes
- 1)
5430 bbio
->stripes
[i
].length
= length
;
5434 if (stripe_index
== map
->num_stripes
) {
5441 bbio
->map_type
= map
->type
;
5442 bbio
->num_stripes
= num_stripes
;
5444 free_extent_map(em
);
5449 * In dev-replace case, for repair case (that's the only case where the mirror
5450 * is selected explicitly when calling btrfs_map_block), blocks left of the
5451 * left cursor can also be read from the target drive.
5453 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5455 * For READ, it also needs to be supported using the same mirror number.
5457 * If the requested block is not left of the left cursor, EIO is returned. This
5458 * can happen because btrfs_num_copies() returns one more in the dev-replace
5461 static int get_extra_mirror_from_replace(struct btrfs_fs_info
*fs_info
,
5462 u64 logical
, u64 length
,
5463 u64 srcdev_devid
, int *mirror_num
,
5466 struct btrfs_bio
*bbio
= NULL
;
5468 int index_srcdev
= 0;
5470 u64 physical_of_found
= 0;
5474 ret
= __btrfs_map_block(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
,
5475 logical
, &length
, &bbio
, 0, 0);
5477 ASSERT(bbio
== NULL
);
5481 num_stripes
= bbio
->num_stripes
;
5482 if (*mirror_num
> num_stripes
) {
5484 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5485 * that means that the requested area is not left of the left
5488 btrfs_put_bbio(bbio
);
5493 * process the rest of the function using the mirror_num of the source
5494 * drive. Therefore look it up first. At the end, patch the device
5495 * pointer to the one of the target drive.
5497 for (i
= 0; i
< num_stripes
; i
++) {
5498 if (bbio
->stripes
[i
].dev
->devid
!= srcdev_devid
)
5502 * In case of DUP, in order to keep it simple, only add the
5503 * mirror with the lowest physical address
5506 physical_of_found
<= bbio
->stripes
[i
].physical
)
5511 physical_of_found
= bbio
->stripes
[i
].physical
;
5514 btrfs_put_bbio(bbio
);
5520 *mirror_num
= index_srcdev
+ 1;
5521 *physical
= physical_of_found
;
5525 static void handle_ops_on_dev_replace(enum btrfs_map_op op
,
5526 struct btrfs_bio
**bbio_ret
,
5527 struct btrfs_dev_replace
*dev_replace
,
5528 int *num_stripes_ret
, int *max_errors_ret
)
5530 struct btrfs_bio
*bbio
= *bbio_ret
;
5531 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5532 int tgtdev_indexes
= 0;
5533 int num_stripes
= *num_stripes_ret
;
5534 int max_errors
= *max_errors_ret
;
5537 if (op
== BTRFS_MAP_WRITE
) {
5538 int index_where_to_add
;
5541 * duplicate the write operations while the dev replace
5542 * procedure is running. Since the copying of the old disk to
5543 * the new disk takes place at run time while the filesystem is
5544 * mounted writable, the regular write operations to the old
5545 * disk have to be duplicated to go to the new disk as well.
5547 * Note that device->missing is handled by the caller, and that
5548 * the write to the old disk is already set up in the stripes
5551 index_where_to_add
= num_stripes
;
5552 for (i
= 0; i
< num_stripes
; i
++) {
5553 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5554 /* write to new disk, too */
5555 struct btrfs_bio_stripe
*new =
5556 bbio
->stripes
+ index_where_to_add
;
5557 struct btrfs_bio_stripe
*old
=
5560 new->physical
= old
->physical
;
5561 new->length
= old
->length
;
5562 new->dev
= dev_replace
->tgtdev
;
5563 bbio
->tgtdev_map
[i
] = index_where_to_add
;
5564 index_where_to_add
++;
5569 num_stripes
= index_where_to_add
;
5570 } else if (op
== BTRFS_MAP_GET_READ_MIRRORS
) {
5571 int index_srcdev
= 0;
5573 u64 physical_of_found
= 0;
5576 * During the dev-replace procedure, the target drive can also
5577 * be used to read data in case it is needed to repair a corrupt
5578 * block elsewhere. This is possible if the requested area is
5579 * left of the left cursor. In this area, the target drive is a
5580 * full copy of the source drive.
5582 for (i
= 0; i
< num_stripes
; i
++) {
5583 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5585 * In case of DUP, in order to keep it simple,
5586 * only add the mirror with the lowest physical
5590 physical_of_found
<=
5591 bbio
->stripes
[i
].physical
)
5595 physical_of_found
= bbio
->stripes
[i
].physical
;
5599 struct btrfs_bio_stripe
*tgtdev_stripe
=
5600 bbio
->stripes
+ num_stripes
;
5602 tgtdev_stripe
->physical
= physical_of_found
;
5603 tgtdev_stripe
->length
=
5604 bbio
->stripes
[index_srcdev
].length
;
5605 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5606 bbio
->tgtdev_map
[index_srcdev
] = num_stripes
;
5613 *num_stripes_ret
= num_stripes
;
5614 *max_errors_ret
= max_errors
;
5615 bbio
->num_tgtdevs
= tgtdev_indexes
;
5619 static bool need_full_stripe(enum btrfs_map_op op
)
5621 return (op
== BTRFS_MAP_WRITE
|| op
== BTRFS_MAP_GET_READ_MIRRORS
);
5624 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
5625 enum btrfs_map_op op
,
5626 u64 logical
, u64
*length
,
5627 struct btrfs_bio
**bbio_ret
,
5628 int mirror_num
, int need_raid_map
)
5630 struct extent_map
*em
;
5631 struct map_lookup
*map
;
5641 int tgtdev_indexes
= 0;
5642 struct btrfs_bio
*bbio
= NULL
;
5643 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
5644 int dev_replace_is_ongoing
= 0;
5645 int num_alloc_stripes
;
5646 int patch_the_first_stripe_for_dev_replace
= 0;
5647 u64 physical_to_patch_in_first_stripe
= 0;
5648 u64 raid56_full_stripe_start
= (u64
)-1;
5650 if (op
== BTRFS_MAP_DISCARD
)
5651 return __btrfs_map_block_for_discard(fs_info
, logical
,
5654 em
= get_chunk_map(fs_info
, logical
, *length
);
5658 map
= em
->map_lookup
;
5659 offset
= logical
- em
->start
;
5661 stripe_len
= map
->stripe_len
;
5664 * stripe_nr counts the total number of stripes we have to stride
5665 * to get to this block
5667 stripe_nr
= div64_u64(stripe_nr
, stripe_len
);
5669 stripe_offset
= stripe_nr
* stripe_len
;
5670 if (offset
< stripe_offset
) {
5672 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
5673 stripe_offset
, offset
, em
->start
, logical
,
5675 free_extent_map(em
);
5679 /* stripe_offset is the offset of this block in its stripe*/
5680 stripe_offset
= offset
- stripe_offset
;
5682 /* if we're here for raid56, we need to know the stripe aligned start */
5683 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5684 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
5685 raid56_full_stripe_start
= offset
;
5687 /* allow a write of a full stripe, but make sure we don't
5688 * allow straddling of stripes
5690 raid56_full_stripe_start
= div64_u64(raid56_full_stripe_start
,
5692 raid56_full_stripe_start
*= full_stripe_len
;
5695 if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
5697 /* For writes to RAID[56], allow a full stripeset across all disks.
5698 For other RAID types and for RAID[56] reads, just allow a single
5699 stripe (on a single disk). */
5700 if ((map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
5701 (op
== BTRFS_MAP_WRITE
)) {
5702 max_len
= stripe_len
* nr_data_stripes(map
) -
5703 (offset
- raid56_full_stripe_start
);
5705 /* we limit the length of each bio to what fits in a stripe */
5706 max_len
= stripe_len
- stripe_offset
;
5708 *length
= min_t(u64
, em
->len
- offset
, max_len
);
5710 *length
= em
->len
- offset
;
5713 /* This is for when we're called from btrfs_merge_bio_hook() and all
5714 it cares about is the length */
5718 btrfs_dev_replace_read_lock(dev_replace
);
5719 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
5720 if (!dev_replace_is_ongoing
)
5721 btrfs_dev_replace_read_unlock(dev_replace
);
5723 btrfs_dev_replace_set_lock_blocking(dev_replace
);
5725 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
5726 !need_full_stripe(op
) && dev_replace
->tgtdev
!= NULL
) {
5727 ret
= get_extra_mirror_from_replace(fs_info
, logical
, *length
,
5728 dev_replace
->srcdev
->devid
,
5730 &physical_to_patch_in_first_stripe
);
5734 patch_the_first_stripe_for_dev_replace
= 1;
5735 } else if (mirror_num
> map
->num_stripes
) {
5741 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5742 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5744 if (!need_full_stripe(op
))
5746 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
5747 if (need_full_stripe(op
))
5748 num_stripes
= map
->num_stripes
;
5749 else if (mirror_num
)
5750 stripe_index
= mirror_num
- 1;
5752 stripe_index
= find_live_mirror(fs_info
, map
, 0,
5753 dev_replace_is_ongoing
);
5754 mirror_num
= stripe_index
+ 1;
5757 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
5758 if (need_full_stripe(op
)) {
5759 num_stripes
= map
->num_stripes
;
5760 } else if (mirror_num
) {
5761 stripe_index
= mirror_num
- 1;
5766 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5767 u32 factor
= map
->num_stripes
/ map
->sub_stripes
;
5769 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5770 stripe_index
*= map
->sub_stripes
;
5772 if (need_full_stripe(op
))
5773 num_stripes
= map
->sub_stripes
;
5774 else if (mirror_num
)
5775 stripe_index
+= mirror_num
- 1;
5777 int old_stripe_index
= stripe_index
;
5778 stripe_index
= find_live_mirror(fs_info
, map
,
5780 dev_replace_is_ongoing
);
5781 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5784 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5785 if (need_raid_map
&& (need_full_stripe(op
) || mirror_num
> 1)) {
5786 /* push stripe_nr back to the start of the full stripe */
5787 stripe_nr
= div64_u64(raid56_full_stripe_start
,
5788 stripe_len
* nr_data_stripes(map
));
5790 /* RAID[56] write or recovery. Return all stripes */
5791 num_stripes
= map
->num_stripes
;
5792 max_errors
= nr_parity_stripes(map
);
5794 *length
= map
->stripe_len
;
5799 * Mirror #0 or #1 means the original data block.
5800 * Mirror #2 is RAID5 parity block.
5801 * Mirror #3 is RAID6 Q block.
5803 stripe_nr
= div_u64_rem(stripe_nr
,
5804 nr_data_stripes(map
), &stripe_index
);
5806 stripe_index
= nr_data_stripes(map
) +
5809 /* We distribute the parity blocks across stripes */
5810 div_u64_rem(stripe_nr
+ stripe_index
, map
->num_stripes
,
5812 if (!need_full_stripe(op
) && mirror_num
<= 1)
5817 * after this, stripe_nr is the number of stripes on this
5818 * device we have to walk to find the data, and stripe_index is
5819 * the number of our device in the stripe array
5821 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5823 mirror_num
= stripe_index
+ 1;
5825 if (stripe_index
>= map
->num_stripes
) {
5827 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
5828 stripe_index
, map
->num_stripes
);
5833 num_alloc_stripes
= num_stripes
;
5834 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
) {
5835 if (op
== BTRFS_MAP_WRITE
)
5836 num_alloc_stripes
<<= 1;
5837 if (op
== BTRFS_MAP_GET_READ_MIRRORS
)
5838 num_alloc_stripes
++;
5839 tgtdev_indexes
= num_stripes
;
5842 bbio
= alloc_btrfs_bio(num_alloc_stripes
, tgtdev_indexes
);
5847 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
)
5848 bbio
->tgtdev_map
= (int *)(bbio
->stripes
+ num_alloc_stripes
);
5850 /* build raid_map */
5851 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
&& need_raid_map
&&
5852 (need_full_stripe(op
) || mirror_num
> 1)) {
5856 bbio
->raid_map
= (u64
*)((void *)bbio
->stripes
+
5857 sizeof(struct btrfs_bio_stripe
) *
5859 sizeof(int) * tgtdev_indexes
);
5861 /* Work out the disk rotation on this stripe-set */
5862 div_u64_rem(stripe_nr
, num_stripes
, &rot
);
5864 /* Fill in the logical address of each stripe */
5865 tmp
= stripe_nr
* nr_data_stripes(map
);
5866 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5867 bbio
->raid_map
[(i
+rot
) % num_stripes
] =
5868 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5870 bbio
->raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5871 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5872 bbio
->raid_map
[(i
+rot
+1) % num_stripes
] =
5877 for (i
= 0; i
< num_stripes
; i
++) {
5878 bbio
->stripes
[i
].physical
=
5879 map
->stripes
[stripe_index
].physical
+
5881 stripe_nr
* map
->stripe_len
;
5882 bbio
->stripes
[i
].dev
=
5883 map
->stripes
[stripe_index
].dev
;
5887 if (need_full_stripe(op
))
5888 max_errors
= btrfs_chunk_max_errors(map
);
5891 sort_parity_stripes(bbio
, num_stripes
);
5893 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
&&
5894 need_full_stripe(op
)) {
5895 handle_ops_on_dev_replace(op
, &bbio
, dev_replace
, &num_stripes
,
5900 bbio
->map_type
= map
->type
;
5901 bbio
->num_stripes
= num_stripes
;
5902 bbio
->max_errors
= max_errors
;
5903 bbio
->mirror_num
= mirror_num
;
5906 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5907 * mirror_num == num_stripes + 1 && dev_replace target drive is
5908 * available as a mirror
5910 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5911 WARN_ON(num_stripes
> 1);
5912 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5913 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5914 bbio
->mirror_num
= map
->num_stripes
+ 1;
5917 if (dev_replace_is_ongoing
) {
5918 btrfs_dev_replace_clear_lock_blocking(dev_replace
);
5919 btrfs_dev_replace_read_unlock(dev_replace
);
5921 free_extent_map(em
);
5925 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
5926 u64 logical
, u64
*length
,
5927 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5929 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
,
5933 /* For Scrub/replace */
5934 int btrfs_map_sblock(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
5935 u64 logical
, u64
*length
,
5936 struct btrfs_bio
**bbio_ret
)
5938 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
, 0, 1);
5941 int btrfs_rmap_block(struct btrfs_fs_info
*fs_info
, u64 chunk_start
,
5942 u64 physical
, u64
**logical
, int *naddrs
, int *stripe_len
)
5944 struct extent_map
*em
;
5945 struct map_lookup
*map
;
5953 em
= get_chunk_map(fs_info
, chunk_start
, 1);
5957 map
= em
->map_lookup
;
5959 rmap_len
= map
->stripe_len
;
5961 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5962 length
= div_u64(length
, map
->num_stripes
/ map
->sub_stripes
);
5963 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5964 length
= div_u64(length
, map
->num_stripes
);
5965 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5966 length
= div_u64(length
, nr_data_stripes(map
));
5967 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5970 buf
= kcalloc(map
->num_stripes
, sizeof(u64
), GFP_NOFS
);
5971 BUG_ON(!buf
); /* -ENOMEM */
5973 for (i
= 0; i
< map
->num_stripes
; i
++) {
5974 if (map
->stripes
[i
].physical
> physical
||
5975 map
->stripes
[i
].physical
+ length
<= physical
)
5978 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5979 stripe_nr
= div64_u64(stripe_nr
, map
->stripe_len
);
5981 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5982 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5983 stripe_nr
= div_u64(stripe_nr
, map
->sub_stripes
);
5984 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5985 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5986 } /* else if RAID[56], multiply by nr_data_stripes().
5987 * Alternatively, just use rmap_len below instead of
5988 * map->stripe_len */
5990 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5991 WARN_ON(nr
>= map
->num_stripes
);
5992 for (j
= 0; j
< nr
; j
++) {
5993 if (buf
[j
] == bytenr
)
5997 WARN_ON(nr
>= map
->num_stripes
);
6004 *stripe_len
= rmap_len
;
6006 free_extent_map(em
);
6010 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
)
6012 bio
->bi_private
= bbio
->private;
6013 bio
->bi_end_io
= bbio
->end_io
;
6016 btrfs_put_bbio(bbio
);
6019 static void btrfs_end_bio(struct bio
*bio
)
6021 struct btrfs_bio
*bbio
= bio
->bi_private
;
6022 int is_orig_bio
= 0;
6024 if (bio
->bi_status
) {
6025 atomic_inc(&bbio
->error
);
6026 if (bio
->bi_status
== BLK_STS_IOERR
||
6027 bio
->bi_status
== BLK_STS_TARGET
) {
6028 unsigned int stripe_index
=
6029 btrfs_io_bio(bio
)->stripe_index
;
6030 struct btrfs_device
*dev
;
6032 BUG_ON(stripe_index
>= bbio
->num_stripes
);
6033 dev
= bbio
->stripes
[stripe_index
].dev
;
6035 if (bio_op(bio
) == REQ_OP_WRITE
)
6036 btrfs_dev_stat_inc_and_print(dev
,
6037 BTRFS_DEV_STAT_WRITE_ERRS
);
6038 else if (!(bio
->bi_opf
& REQ_RAHEAD
))
6039 btrfs_dev_stat_inc_and_print(dev
,
6040 BTRFS_DEV_STAT_READ_ERRS
);
6041 if (bio
->bi_opf
& REQ_PREFLUSH
)
6042 btrfs_dev_stat_inc_and_print(dev
,
6043 BTRFS_DEV_STAT_FLUSH_ERRS
);
6048 if (bio
== bbio
->orig_bio
)
6051 btrfs_bio_counter_dec(bbio
->fs_info
);
6053 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6056 bio
= bbio
->orig_bio
;
6059 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6060 /* only send an error to the higher layers if it is
6061 * beyond the tolerance of the btrfs bio
6063 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
6064 bio
->bi_status
= BLK_STS_IOERR
;
6067 * this bio is actually up to date, we didn't
6068 * go over the max number of errors
6070 bio
->bi_status
= BLK_STS_OK
;
6073 btrfs_end_bbio(bbio
, bio
);
6074 } else if (!is_orig_bio
) {
6080 * see run_scheduled_bios for a description of why bios are collected for
6083 * This will add one bio to the pending list for a device and make sure
6084 * the work struct is scheduled.
6086 static noinline
void btrfs_schedule_bio(struct btrfs_device
*device
,
6089 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
6090 int should_queue
= 1;
6091 struct btrfs_pending_bios
*pending_bios
;
6093 /* don't bother with additional async steps for reads, right now */
6094 if (bio_op(bio
) == REQ_OP_READ
) {
6095 btrfsic_submit_bio(bio
);
6099 WARN_ON(bio
->bi_next
);
6100 bio
->bi_next
= NULL
;
6102 spin_lock(&device
->io_lock
);
6103 if (op_is_sync(bio
->bi_opf
))
6104 pending_bios
= &device
->pending_sync_bios
;
6106 pending_bios
= &device
->pending_bios
;
6108 if (pending_bios
->tail
)
6109 pending_bios
->tail
->bi_next
= bio
;
6111 pending_bios
->tail
= bio
;
6112 if (!pending_bios
->head
)
6113 pending_bios
->head
= bio
;
6114 if (device
->running_pending
)
6117 spin_unlock(&device
->io_lock
);
6120 btrfs_queue_work(fs_info
->submit_workers
, &device
->work
);
6123 static void submit_stripe_bio(struct btrfs_bio
*bbio
, struct bio
*bio
,
6124 u64 physical
, int dev_nr
, int async
)
6126 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
6127 struct btrfs_fs_info
*fs_info
= bbio
->fs_info
;
6129 bio
->bi_private
= bbio
;
6130 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
6131 bio
->bi_end_io
= btrfs_end_bio
;
6132 bio
->bi_iter
.bi_sector
= physical
>> 9;
6133 btrfs_debug_in_rcu(fs_info
,
6134 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6135 bio_op(bio
), bio
->bi_opf
, (u64
)bio
->bi_iter
.bi_sector
,
6136 (u_long
)dev
->bdev
->bd_dev
, rcu_str_deref(dev
->name
), dev
->devid
,
6137 bio
->bi_iter
.bi_size
);
6138 bio_set_dev(bio
, dev
->bdev
);
6140 btrfs_bio_counter_inc_noblocked(fs_info
);
6143 btrfs_schedule_bio(dev
, bio
);
6145 btrfsic_submit_bio(bio
);
6148 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
6150 atomic_inc(&bbio
->error
);
6151 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6152 /* Should be the original bio. */
6153 WARN_ON(bio
!= bbio
->orig_bio
);
6155 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6156 bio
->bi_iter
.bi_sector
= logical
>> 9;
6157 if (atomic_read(&bbio
->error
) > bbio
->max_errors
)
6158 bio
->bi_status
= BLK_STS_IOERR
;
6160 bio
->bi_status
= BLK_STS_OK
;
6161 btrfs_end_bbio(bbio
, bio
);
6165 blk_status_t
btrfs_map_bio(struct btrfs_fs_info
*fs_info
, struct bio
*bio
,
6166 int mirror_num
, int async_submit
)
6168 struct btrfs_device
*dev
;
6169 struct bio
*first_bio
= bio
;
6170 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
6176 struct btrfs_bio
*bbio
= NULL
;
6178 length
= bio
->bi_iter
.bi_size
;
6179 map_length
= length
;
6181 btrfs_bio_counter_inc_blocked(fs_info
);
6182 ret
= __btrfs_map_block(fs_info
, btrfs_op(bio
), logical
,
6183 &map_length
, &bbio
, mirror_num
, 1);
6185 btrfs_bio_counter_dec(fs_info
);
6186 return errno_to_blk_status(ret
);
6189 total_devs
= bbio
->num_stripes
;
6190 bbio
->orig_bio
= first_bio
;
6191 bbio
->private = first_bio
->bi_private
;
6192 bbio
->end_io
= first_bio
->bi_end_io
;
6193 bbio
->fs_info
= fs_info
;
6194 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
6196 if ((bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
6197 ((bio_op(bio
) == REQ_OP_WRITE
) || (mirror_num
> 1))) {
6198 /* In this case, map_length has been set to the length of
6199 a single stripe; not the whole write */
6200 if (bio_op(bio
) == REQ_OP_WRITE
) {
6201 ret
= raid56_parity_write(fs_info
, bio
, bbio
,
6204 ret
= raid56_parity_recover(fs_info
, bio
, bbio
,
6205 map_length
, mirror_num
, 1);
6208 btrfs_bio_counter_dec(fs_info
);
6209 return errno_to_blk_status(ret
);
6212 if (map_length
< length
) {
6214 "mapping failed logical %llu bio len %llu len %llu",
6215 logical
, length
, map_length
);
6219 for (dev_nr
= 0; dev_nr
< total_devs
; dev_nr
++) {
6220 dev
= bbio
->stripes
[dev_nr
].dev
;
6221 if (!dev
|| !dev
->bdev
|| test_bit(BTRFS_DEV_STATE_MISSING
,
6223 (bio_op(first_bio
) == REQ_OP_WRITE
&&
6224 !test_bit(BTRFS_DEV_STATE_WRITEABLE
, &dev
->dev_state
))) {
6225 bbio_error(bbio
, first_bio
, logical
);
6229 if (dev_nr
< total_devs
- 1)
6230 bio
= btrfs_bio_clone(first_bio
);
6234 submit_stripe_bio(bbio
, bio
, bbio
->stripes
[dev_nr
].physical
,
6235 dev_nr
, async_submit
);
6237 btrfs_bio_counter_dec(fs_info
);
6242 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6245 * If devid and uuid are both specified, the match must be exact, otherwise
6246 * only devid is used.
6248 * If @seed is true, traverse through the seed devices.
6250 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_devices
*fs_devices
,
6251 u64 devid
, u8
*uuid
, u8
*fsid
,
6254 struct btrfs_device
*device
;
6256 while (fs_devices
) {
6258 !memcmp(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
)) {
6259 list_for_each_entry(device
, &fs_devices
->devices
,
6261 if (device
->devid
== devid
&&
6262 (!uuid
|| memcmp(device
->uuid
, uuid
,
6263 BTRFS_UUID_SIZE
) == 0))
6268 fs_devices
= fs_devices
->seed
;
6275 static struct btrfs_device
*add_missing_dev(struct btrfs_fs_devices
*fs_devices
,
6276 u64 devid
, u8
*dev_uuid
)
6278 struct btrfs_device
*device
;
6280 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
6284 list_add(&device
->dev_list
, &fs_devices
->devices
);
6285 device
->fs_devices
= fs_devices
;
6286 fs_devices
->num_devices
++;
6288 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6289 fs_devices
->missing_devices
++;
6295 * btrfs_alloc_device - allocate struct btrfs_device
6296 * @fs_info: used only for generating a new devid, can be NULL if
6297 * devid is provided (i.e. @devid != NULL).
6298 * @devid: a pointer to devid for this device. If NULL a new devid
6300 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6303 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6304 * on error. Returned struct is not linked onto any lists and must be
6305 * destroyed with btrfs_free_device.
6307 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
6311 struct btrfs_device
*dev
;
6314 if (WARN_ON(!devid
&& !fs_info
))
6315 return ERR_PTR(-EINVAL
);
6317 dev
= __alloc_device();
6326 ret
= find_next_devid(fs_info
, &tmp
);
6328 btrfs_free_device(dev
);
6329 return ERR_PTR(ret
);
6335 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
6337 generate_random_uuid(dev
->uuid
);
6339 btrfs_init_work(&dev
->work
, btrfs_submit_helper
,
6340 pending_bios_fn
, NULL
, NULL
);
6345 /* Return -EIO if any error, otherwise return 0. */
6346 static int btrfs_check_chunk_valid(struct btrfs_fs_info
*fs_info
,
6347 struct extent_buffer
*leaf
,
6348 struct btrfs_chunk
*chunk
, u64 logical
)
6358 length
= btrfs_chunk_length(leaf
, chunk
);
6359 stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6360 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6361 sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6362 type
= btrfs_chunk_type(leaf
, chunk
);
6365 btrfs_err(fs_info
, "invalid chunk num_stripes: %u",
6369 if (!IS_ALIGNED(logical
, fs_info
->sectorsize
)) {
6370 btrfs_err(fs_info
, "invalid chunk logical %llu", logical
);
6373 if (btrfs_chunk_sector_size(leaf
, chunk
) != fs_info
->sectorsize
) {
6374 btrfs_err(fs_info
, "invalid chunk sectorsize %u",
6375 btrfs_chunk_sector_size(leaf
, chunk
));
6378 if (!length
|| !IS_ALIGNED(length
, fs_info
->sectorsize
)) {
6379 btrfs_err(fs_info
, "invalid chunk length %llu", length
);
6382 if (!is_power_of_2(stripe_len
) || stripe_len
!= BTRFS_STRIPE_LEN
) {
6383 btrfs_err(fs_info
, "invalid chunk stripe length: %llu",
6387 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK
| BTRFS_BLOCK_GROUP_PROFILE_MASK
) &
6389 btrfs_err(fs_info
, "unrecognized chunk type: %llu",
6390 ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
6391 BTRFS_BLOCK_GROUP_PROFILE_MASK
) &
6392 btrfs_chunk_type(leaf
, chunk
));
6396 if ((type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) == 0) {
6397 btrfs_err(fs_info
, "missing chunk type flag: 0x%llx", type
);
6401 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
6402 (type
& (BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
))) {
6404 "system chunk with data or metadata type: 0x%llx", type
);
6408 features
= btrfs_super_incompat_flags(fs_info
->super_copy
);
6409 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
6413 if ((type
& BTRFS_BLOCK_GROUP_METADATA
) &&
6414 (type
& BTRFS_BLOCK_GROUP_DATA
)) {
6416 "mixed chunk type in non-mixed mode: 0x%llx", type
);
6421 if ((type
& BTRFS_BLOCK_GROUP_RAID10
&& sub_stripes
!= 2) ||
6422 (type
& BTRFS_BLOCK_GROUP_RAID1
&& num_stripes
!= 2) ||
6423 (type
& BTRFS_BLOCK_GROUP_RAID5
&& num_stripes
< 2) ||
6424 (type
& BTRFS_BLOCK_GROUP_RAID6
&& num_stripes
< 3) ||
6425 (type
& BTRFS_BLOCK_GROUP_DUP
&& num_stripes
!= 2) ||
6426 ((type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) == 0 &&
6427 num_stripes
!= 1)) {
6429 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6430 num_stripes
, sub_stripes
,
6431 type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
);
6438 static void btrfs_report_missing_device(struct btrfs_fs_info
*fs_info
,
6439 u64 devid
, u8
*uuid
, bool error
)
6442 btrfs_err_rl(fs_info
, "devid %llu uuid %pU is missing",
6445 btrfs_warn_rl(fs_info
, "devid %llu uuid %pU is missing",
6449 static int read_one_chunk(struct btrfs_fs_info
*fs_info
, struct btrfs_key
*key
,
6450 struct extent_buffer
*leaf
,
6451 struct btrfs_chunk
*chunk
)
6453 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
6454 struct map_lookup
*map
;
6455 struct extent_map
*em
;
6459 u8 uuid
[BTRFS_UUID_SIZE
];
6464 logical
= key
->offset
;
6465 length
= btrfs_chunk_length(leaf
, chunk
);
6466 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6468 ret
= btrfs_check_chunk_valid(fs_info
, leaf
, chunk
, logical
);
6472 read_lock(&map_tree
->map_tree
.lock
);
6473 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
6474 read_unlock(&map_tree
->map_tree
.lock
);
6476 /* already mapped? */
6477 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
6478 free_extent_map(em
);
6481 free_extent_map(em
);
6484 em
= alloc_extent_map();
6487 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
6489 free_extent_map(em
);
6493 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
6494 em
->map_lookup
= map
;
6495 em
->start
= logical
;
6498 em
->block_start
= 0;
6499 em
->block_len
= em
->len
;
6501 map
->num_stripes
= num_stripes
;
6502 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
6503 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
6504 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6505 map
->type
= btrfs_chunk_type(leaf
, chunk
);
6506 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6507 map
->verified_stripes
= 0;
6508 for (i
= 0; i
< num_stripes
; i
++) {
6509 map
->stripes
[i
].physical
=
6510 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
6511 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
6512 read_extent_buffer(leaf
, uuid
, (unsigned long)
6513 btrfs_stripe_dev_uuid_nr(chunk
, i
),
6515 map
->stripes
[i
].dev
= btrfs_find_device(fs_info
->fs_devices
,
6516 devid
, uuid
, NULL
, true);
6517 if (!map
->stripes
[i
].dev
&&
6518 !btrfs_test_opt(fs_info
, DEGRADED
)) {
6519 free_extent_map(em
);
6520 btrfs_report_missing_device(fs_info
, devid
, uuid
, true);
6523 if (!map
->stripes
[i
].dev
) {
6524 map
->stripes
[i
].dev
=
6525 add_missing_dev(fs_info
->fs_devices
, devid
,
6527 if (IS_ERR(map
->stripes
[i
].dev
)) {
6528 free_extent_map(em
);
6530 "failed to init missing dev %llu: %ld",
6531 devid
, PTR_ERR(map
->stripes
[i
].dev
));
6532 return PTR_ERR(map
->stripes
[i
].dev
);
6534 btrfs_report_missing_device(fs_info
, devid
, uuid
, false);
6536 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
6537 &(map
->stripes
[i
].dev
->dev_state
));
6541 write_lock(&map_tree
->map_tree
.lock
);
6542 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
6543 write_unlock(&map_tree
->map_tree
.lock
);
6546 "failed to add chunk map, start=%llu len=%llu: %d",
6547 em
->start
, em
->len
, ret
);
6549 free_extent_map(em
);
6554 static void fill_device_from_item(struct extent_buffer
*leaf
,
6555 struct btrfs_dev_item
*dev_item
,
6556 struct btrfs_device
*device
)
6560 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6561 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6562 device
->total_bytes
= device
->disk_total_bytes
;
6563 device
->commit_total_bytes
= device
->disk_total_bytes
;
6564 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6565 device
->commit_bytes_used
= device
->bytes_used
;
6566 device
->type
= btrfs_device_type(leaf
, dev_item
);
6567 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6568 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6569 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6570 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6571 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
6573 ptr
= btrfs_device_uuid(dev_item
);
6574 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6577 static struct btrfs_fs_devices
*open_seed_devices(struct btrfs_fs_info
*fs_info
,
6580 struct btrfs_fs_devices
*fs_devices
;
6583 lockdep_assert_held(&uuid_mutex
);
6586 fs_devices
= fs_info
->fs_devices
->seed
;
6587 while (fs_devices
) {
6588 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
))
6591 fs_devices
= fs_devices
->seed
;
6594 fs_devices
= find_fsid(fsid
);
6596 if (!btrfs_test_opt(fs_info
, DEGRADED
))
6597 return ERR_PTR(-ENOENT
);
6599 fs_devices
= alloc_fs_devices(fsid
);
6600 if (IS_ERR(fs_devices
))
6603 fs_devices
->seeding
= 1;
6604 fs_devices
->opened
= 1;
6608 fs_devices
= clone_fs_devices(fs_devices
);
6609 if (IS_ERR(fs_devices
))
6612 ret
= open_fs_devices(fs_devices
, FMODE_READ
, fs_info
->bdev_holder
);
6614 free_fs_devices(fs_devices
);
6615 fs_devices
= ERR_PTR(ret
);
6619 if (!fs_devices
->seeding
) {
6620 close_fs_devices(fs_devices
);
6621 free_fs_devices(fs_devices
);
6622 fs_devices
= ERR_PTR(-EINVAL
);
6626 fs_devices
->seed
= fs_info
->fs_devices
->seed
;
6627 fs_info
->fs_devices
->seed
= fs_devices
;
6632 static int read_one_dev(struct btrfs_fs_info
*fs_info
,
6633 struct extent_buffer
*leaf
,
6634 struct btrfs_dev_item
*dev_item
)
6636 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6637 struct btrfs_device
*device
;
6640 u8 fs_uuid
[BTRFS_FSID_SIZE
];
6641 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6643 devid
= btrfs_device_id(leaf
, dev_item
);
6644 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6646 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6649 if (memcmp(fs_uuid
, fs_info
->fsid
, BTRFS_FSID_SIZE
)) {
6650 fs_devices
= open_seed_devices(fs_info
, fs_uuid
);
6651 if (IS_ERR(fs_devices
))
6652 return PTR_ERR(fs_devices
);
6655 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
6658 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6659 btrfs_report_missing_device(fs_info
, devid
,
6664 device
= add_missing_dev(fs_devices
, devid
, dev_uuid
);
6665 if (IS_ERR(device
)) {
6667 "failed to add missing dev %llu: %ld",
6668 devid
, PTR_ERR(device
));
6669 return PTR_ERR(device
);
6671 btrfs_report_missing_device(fs_info
, devid
, dev_uuid
, false);
6673 if (!device
->bdev
) {
6674 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6675 btrfs_report_missing_device(fs_info
,
6676 devid
, dev_uuid
, true);
6679 btrfs_report_missing_device(fs_info
, devid
,
6683 if (!device
->bdev
&&
6684 !test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
6686 * this happens when a device that was properly setup
6687 * in the device info lists suddenly goes bad.
6688 * device->bdev is NULL, and so we have to set
6689 * device->missing to one here
6691 device
->fs_devices
->missing_devices
++;
6692 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6695 /* Move the device to its own fs_devices */
6696 if (device
->fs_devices
!= fs_devices
) {
6697 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING
,
6698 &device
->dev_state
));
6700 list_move(&device
->dev_list
, &fs_devices
->devices
);
6701 device
->fs_devices
->num_devices
--;
6702 fs_devices
->num_devices
++;
6704 device
->fs_devices
->missing_devices
--;
6705 fs_devices
->missing_devices
++;
6707 device
->fs_devices
= fs_devices
;
6711 if (device
->fs_devices
!= fs_info
->fs_devices
) {
6712 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
));
6713 if (device
->generation
!=
6714 btrfs_device_generation(leaf
, dev_item
))
6718 fill_device_from_item(leaf
, dev_item
, device
);
6719 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
6720 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
6721 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
6722 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6723 atomic64_add(device
->total_bytes
- device
->bytes_used
,
6724 &fs_info
->free_chunk_space
);
6730 int btrfs_read_sys_array(struct btrfs_fs_info
*fs_info
)
6732 struct btrfs_root
*root
= fs_info
->tree_root
;
6733 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
6734 struct extent_buffer
*sb
;
6735 struct btrfs_disk_key
*disk_key
;
6736 struct btrfs_chunk
*chunk
;
6738 unsigned long sb_array_offset
;
6745 struct btrfs_key key
;
6747 ASSERT(BTRFS_SUPER_INFO_SIZE
<= fs_info
->nodesize
);
6749 * This will create extent buffer of nodesize, superblock size is
6750 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6751 * overallocate but we can keep it as-is, only the first page is used.
6753 sb
= btrfs_find_create_tree_block(fs_info
, BTRFS_SUPER_INFO_OFFSET
);
6756 set_extent_buffer_uptodate(sb
);
6757 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6759 * The sb extent buffer is artificial and just used to read the system array.
6760 * set_extent_buffer_uptodate() call does not properly mark all it's
6761 * pages up-to-date when the page is larger: extent does not cover the
6762 * whole page and consequently check_page_uptodate does not find all
6763 * the page's extents up-to-date (the hole beyond sb),
6764 * write_extent_buffer then triggers a WARN_ON.
6766 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6767 * but sb spans only this function. Add an explicit SetPageUptodate call
6768 * to silence the warning eg. on PowerPC 64.
6770 if (PAGE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6771 SetPageUptodate(sb
->pages
[0]);
6773 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6774 array_size
= btrfs_super_sys_array_size(super_copy
);
6776 array_ptr
= super_copy
->sys_chunk_array
;
6777 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6780 while (cur_offset
< array_size
) {
6781 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
6782 len
= sizeof(*disk_key
);
6783 if (cur_offset
+ len
> array_size
)
6784 goto out_short_read
;
6786 btrfs_disk_key_to_cpu(&key
, disk_key
);
6789 sb_array_offset
+= len
;
6792 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6793 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
6795 * At least one btrfs_chunk with one stripe must be
6796 * present, exact stripe count check comes afterwards
6798 len
= btrfs_chunk_item_size(1);
6799 if (cur_offset
+ len
> array_size
)
6800 goto out_short_read
;
6802 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6805 "invalid number of stripes %u in sys_array at offset %u",
6806 num_stripes
, cur_offset
);
6811 type
= btrfs_chunk_type(sb
, chunk
);
6812 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) == 0) {
6814 "invalid chunk type %llu in sys_array at offset %u",
6820 len
= btrfs_chunk_item_size(num_stripes
);
6821 if (cur_offset
+ len
> array_size
)
6822 goto out_short_read
;
6824 ret
= read_one_chunk(fs_info
, &key
, sb
, chunk
);
6829 "unexpected item type %u in sys_array at offset %u",
6830 (u32
)key
.type
, cur_offset
);
6835 sb_array_offset
+= len
;
6838 clear_extent_buffer_uptodate(sb
);
6839 free_extent_buffer_stale(sb
);
6843 btrfs_err(fs_info
, "sys_array too short to read %u bytes at offset %u",
6845 clear_extent_buffer_uptodate(sb
);
6846 free_extent_buffer_stale(sb
);
6851 * Check if all chunks in the fs are OK for read-write degraded mount
6853 * If the @failing_dev is specified, it's accounted as missing.
6855 * Return true if all chunks meet the minimal RW mount requirements.
6856 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6858 bool btrfs_check_rw_degradable(struct btrfs_fs_info
*fs_info
,
6859 struct btrfs_device
*failing_dev
)
6861 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
6862 struct extent_map
*em
;
6866 read_lock(&map_tree
->map_tree
.lock
);
6867 em
= lookup_extent_mapping(&map_tree
->map_tree
, 0, (u64
)-1);
6868 read_unlock(&map_tree
->map_tree
.lock
);
6869 /* No chunk at all? Return false anyway */
6875 struct map_lookup
*map
;
6880 map
= em
->map_lookup
;
6882 btrfs_get_num_tolerated_disk_barrier_failures(
6884 for (i
= 0; i
< map
->num_stripes
; i
++) {
6885 struct btrfs_device
*dev
= map
->stripes
[i
].dev
;
6887 if (!dev
|| !dev
->bdev
||
6888 test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
) ||
6889 dev
->last_flush_error
)
6891 else if (failing_dev
&& failing_dev
== dev
)
6894 if (missing
> max_tolerated
) {
6897 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6898 em
->start
, missing
, max_tolerated
);
6899 free_extent_map(em
);
6903 next_start
= extent_map_end(em
);
6904 free_extent_map(em
);
6906 read_lock(&map_tree
->map_tree
.lock
);
6907 em
= lookup_extent_mapping(&map_tree
->map_tree
, next_start
,
6908 (u64
)(-1) - next_start
);
6909 read_unlock(&map_tree
->map_tree
.lock
);
6915 int btrfs_read_chunk_tree(struct btrfs_fs_info
*fs_info
)
6917 struct btrfs_root
*root
= fs_info
->chunk_root
;
6918 struct btrfs_path
*path
;
6919 struct extent_buffer
*leaf
;
6920 struct btrfs_key key
;
6921 struct btrfs_key found_key
;
6926 path
= btrfs_alloc_path();
6931 * uuid_mutex is needed only if we are mounting a sprout FS
6932 * otherwise we don't need it.
6934 mutex_lock(&uuid_mutex
);
6935 mutex_lock(&fs_info
->chunk_mutex
);
6938 * Read all device items, and then all the chunk items. All
6939 * device items are found before any chunk item (their object id
6940 * is smaller than the lowest possible object id for a chunk
6941 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6943 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6946 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6950 leaf
= path
->nodes
[0];
6951 slot
= path
->slots
[0];
6952 if (slot
>= btrfs_header_nritems(leaf
)) {
6953 ret
= btrfs_next_leaf(root
, path
);
6960 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6961 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6962 struct btrfs_dev_item
*dev_item
;
6963 dev_item
= btrfs_item_ptr(leaf
, slot
,
6964 struct btrfs_dev_item
);
6965 ret
= read_one_dev(fs_info
, leaf
, dev_item
);
6969 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6970 struct btrfs_chunk
*chunk
;
6971 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6972 ret
= read_one_chunk(fs_info
, &found_key
, leaf
, chunk
);
6980 * After loading chunk tree, we've got all device information,
6981 * do another round of validation checks.
6983 if (total_dev
!= fs_info
->fs_devices
->total_devices
) {
6985 "super_num_devices %llu mismatch with num_devices %llu found here",
6986 btrfs_super_num_devices(fs_info
->super_copy
),
6991 if (btrfs_super_total_bytes(fs_info
->super_copy
) <
6992 fs_info
->fs_devices
->total_rw_bytes
) {
6994 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
6995 btrfs_super_total_bytes(fs_info
->super_copy
),
6996 fs_info
->fs_devices
->total_rw_bytes
);
7002 mutex_unlock(&fs_info
->chunk_mutex
);
7003 mutex_unlock(&uuid_mutex
);
7005 btrfs_free_path(path
);
7009 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
7011 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7012 struct btrfs_device
*device
;
7014 while (fs_devices
) {
7015 mutex_lock(&fs_devices
->device_list_mutex
);
7016 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
7017 device
->fs_info
= fs_info
;
7018 mutex_unlock(&fs_devices
->device_list_mutex
);
7020 fs_devices
= fs_devices
->seed
;
7024 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
7028 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7029 btrfs_dev_stat_reset(dev
, i
);
7032 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
7034 struct btrfs_key key
;
7035 struct btrfs_key found_key
;
7036 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7037 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7038 struct extent_buffer
*eb
;
7041 struct btrfs_device
*device
;
7042 struct btrfs_path
*path
= NULL
;
7045 path
= btrfs_alloc_path();
7051 mutex_lock(&fs_devices
->device_list_mutex
);
7052 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7054 struct btrfs_dev_stats_item
*ptr
;
7056 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7057 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7058 key
.offset
= device
->devid
;
7059 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
7061 __btrfs_reset_dev_stats(device
);
7062 device
->dev_stats_valid
= 1;
7063 btrfs_release_path(path
);
7066 slot
= path
->slots
[0];
7067 eb
= path
->nodes
[0];
7068 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
7069 item_size
= btrfs_item_size_nr(eb
, slot
);
7071 ptr
= btrfs_item_ptr(eb
, slot
,
7072 struct btrfs_dev_stats_item
);
7074 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7075 if (item_size
>= (1 + i
) * sizeof(__le64
))
7076 btrfs_dev_stat_set(device
, i
,
7077 btrfs_dev_stats_value(eb
, ptr
, i
));
7079 btrfs_dev_stat_reset(device
, i
);
7082 device
->dev_stats_valid
= 1;
7083 btrfs_dev_stat_print_on_load(device
);
7084 btrfs_release_path(path
);
7086 mutex_unlock(&fs_devices
->device_list_mutex
);
7089 btrfs_free_path(path
);
7090 return ret
< 0 ? ret
: 0;
7093 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
7094 struct btrfs_device
*device
)
7096 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7097 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7098 struct btrfs_path
*path
;
7099 struct btrfs_key key
;
7100 struct extent_buffer
*eb
;
7101 struct btrfs_dev_stats_item
*ptr
;
7105 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7106 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7107 key
.offset
= device
->devid
;
7109 path
= btrfs_alloc_path();
7112 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
7114 btrfs_warn_in_rcu(fs_info
,
7115 "error %d while searching for dev_stats item for device %s",
7116 ret
, rcu_str_deref(device
->name
));
7121 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
7122 /* need to delete old one and insert a new one */
7123 ret
= btrfs_del_item(trans
, dev_root
, path
);
7125 btrfs_warn_in_rcu(fs_info
,
7126 "delete too small dev_stats item for device %s failed %d",
7127 rcu_str_deref(device
->name
), ret
);
7134 /* need to insert a new item */
7135 btrfs_release_path(path
);
7136 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
7137 &key
, sizeof(*ptr
));
7139 btrfs_warn_in_rcu(fs_info
,
7140 "insert dev_stats item for device %s failed %d",
7141 rcu_str_deref(device
->name
), ret
);
7146 eb
= path
->nodes
[0];
7147 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
7148 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7149 btrfs_set_dev_stats_value(eb
, ptr
, i
,
7150 btrfs_dev_stat_read(device
, i
));
7151 btrfs_mark_buffer_dirty(eb
);
7154 btrfs_free_path(path
);
7159 * called from commit_transaction. Writes all changed device stats to disk.
7161 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
7162 struct btrfs_fs_info
*fs_info
)
7164 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7165 struct btrfs_device
*device
;
7169 mutex_lock(&fs_devices
->device_list_mutex
);
7170 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7171 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
7172 if (!device
->dev_stats_valid
|| stats_cnt
== 0)
7177 * There is a LOAD-LOAD control dependency between the value of
7178 * dev_stats_ccnt and updating the on-disk values which requires
7179 * reading the in-memory counters. Such control dependencies
7180 * require explicit read memory barriers.
7182 * This memory barriers pairs with smp_mb__before_atomic in
7183 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7184 * barrier implied by atomic_xchg in
7185 * btrfs_dev_stats_read_and_reset
7189 ret
= update_dev_stat_item(trans
, device
);
7191 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
7193 mutex_unlock(&fs_devices
->device_list_mutex
);
7198 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
7200 btrfs_dev_stat_inc(dev
, index
);
7201 btrfs_dev_stat_print_on_error(dev
);
7204 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
7206 if (!dev
->dev_stats_valid
)
7208 btrfs_err_rl_in_rcu(dev
->fs_info
,
7209 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7210 rcu_str_deref(dev
->name
),
7211 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7212 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7213 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7214 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7215 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7218 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
7222 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7223 if (btrfs_dev_stat_read(dev
, i
) != 0)
7225 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
7226 return; /* all values == 0, suppress message */
7228 btrfs_info_in_rcu(dev
->fs_info
,
7229 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7230 rcu_str_deref(dev
->name
),
7231 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7232 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7233 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7234 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7235 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7238 int btrfs_get_dev_stats(struct btrfs_fs_info
*fs_info
,
7239 struct btrfs_ioctl_get_dev_stats
*stats
)
7241 struct btrfs_device
*dev
;
7242 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7245 mutex_lock(&fs_devices
->device_list_mutex
);
7246 dev
= btrfs_find_device(fs_info
->fs_devices
, stats
->devid
,
7248 mutex_unlock(&fs_devices
->device_list_mutex
);
7251 btrfs_warn(fs_info
, "get dev_stats failed, device not found");
7253 } else if (!dev
->dev_stats_valid
) {
7254 btrfs_warn(fs_info
, "get dev_stats failed, not yet valid");
7256 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
7257 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7258 if (stats
->nr_items
> i
)
7260 btrfs_dev_stat_read_and_reset(dev
, i
);
7262 btrfs_dev_stat_reset(dev
, i
);
7264 btrfs_info(fs_info
, "device stats zeroed by %s (%d)",
7265 current
->comm
, task_pid_nr(current
));
7267 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7268 if (stats
->nr_items
> i
)
7269 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
7271 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
7272 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
7276 void btrfs_scratch_superblocks(struct block_device
*bdev
, const char *device_path
)
7278 struct buffer_head
*bh
;
7279 struct btrfs_super_block
*disk_super
;
7285 for (copy_num
= 0; copy_num
< BTRFS_SUPER_MIRROR_MAX
;
7288 if (btrfs_read_dev_one_super(bdev
, copy_num
, &bh
))
7291 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
7293 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
7294 set_buffer_dirty(bh
);
7295 sync_dirty_buffer(bh
);
7299 /* Notify udev that device has changed */
7300 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
7302 /* Update ctime/mtime for device path for libblkid */
7303 update_dev_time(device_path
);
7307 * Update the size of all devices, which is used for writing out the
7310 void btrfs_update_commit_device_size(struct btrfs_fs_info
*fs_info
)
7312 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7313 struct btrfs_device
*curr
, *next
;
7315 if (list_empty(&fs_devices
->resized_devices
))
7318 mutex_lock(&fs_devices
->device_list_mutex
);
7319 mutex_lock(&fs_info
->chunk_mutex
);
7320 list_for_each_entry_safe(curr
, next
, &fs_devices
->resized_devices
,
7322 list_del_init(&curr
->resized_list
);
7323 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
7325 mutex_unlock(&fs_info
->chunk_mutex
);
7326 mutex_unlock(&fs_devices
->device_list_mutex
);
7329 /* Must be invoked during the transaction commit */
7330 void btrfs_update_commit_device_bytes_used(struct btrfs_transaction
*trans
)
7332 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7333 struct extent_map
*em
;
7334 struct map_lookup
*map
;
7335 struct btrfs_device
*dev
;
7338 if (list_empty(&trans
->pending_chunks
))
7341 /* In order to kick the device replace finish process */
7342 mutex_lock(&fs_info
->chunk_mutex
);
7343 list_for_each_entry(em
, &trans
->pending_chunks
, list
) {
7344 map
= em
->map_lookup
;
7346 for (i
= 0; i
< map
->num_stripes
; i
++) {
7347 dev
= map
->stripes
[i
].dev
;
7348 dev
->commit_bytes_used
= dev
->bytes_used
;
7349 dev
->has_pending_chunks
= false;
7352 mutex_unlock(&fs_info
->chunk_mutex
);
7355 void btrfs_set_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
7357 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7358 while (fs_devices
) {
7359 fs_devices
->fs_info
= fs_info
;
7360 fs_devices
= fs_devices
->seed
;
7364 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
7366 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7367 while (fs_devices
) {
7368 fs_devices
->fs_info
= NULL
;
7369 fs_devices
= fs_devices
->seed
;
7374 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7376 int btrfs_bg_type_to_factor(u64 flags
)
7378 if (flags
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
7379 BTRFS_BLOCK_GROUP_RAID10
))
7385 static u64
calc_stripe_length(u64 type
, u64 chunk_len
, int num_stripes
)
7387 int index
= btrfs_bg_flags_to_raid_index(type
);
7388 int ncopies
= btrfs_raid_array
[index
].ncopies
;
7391 switch (type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
7392 case BTRFS_BLOCK_GROUP_RAID5
:
7393 data_stripes
= num_stripes
- 1;
7395 case BTRFS_BLOCK_GROUP_RAID6
:
7396 data_stripes
= num_stripes
- 2;
7399 data_stripes
= num_stripes
/ ncopies
;
7402 return div_u64(chunk_len
, data_stripes
);
7405 static int verify_one_dev_extent(struct btrfs_fs_info
*fs_info
,
7406 u64 chunk_offset
, u64 devid
,
7407 u64 physical_offset
, u64 physical_len
)
7409 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
.map_tree
;
7410 struct extent_map
*em
;
7411 struct map_lookup
*map
;
7412 struct btrfs_device
*dev
;
7418 read_lock(&em_tree
->lock
);
7419 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
7420 read_unlock(&em_tree
->lock
);
7424 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7425 physical_offset
, devid
);
7430 map
= em
->map_lookup
;
7431 stripe_len
= calc_stripe_length(map
->type
, em
->len
, map
->num_stripes
);
7432 if (physical_len
!= stripe_len
) {
7434 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7435 physical_offset
, devid
, em
->start
, physical_len
,
7441 for (i
= 0; i
< map
->num_stripes
; i
++) {
7442 if (map
->stripes
[i
].dev
->devid
== devid
&&
7443 map
->stripes
[i
].physical
== physical_offset
) {
7445 if (map
->verified_stripes
>= map
->num_stripes
) {
7447 "too many dev extents for chunk %llu found",
7452 map
->verified_stripes
++;
7458 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7459 physical_offset
, devid
);
7463 /* Make sure no dev extent is beyond device bondary */
7464 dev
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
, NULL
, true);
7466 btrfs_err(fs_info
, "failed to find devid %llu", devid
);
7471 /* It's possible this device is a dummy for seed device */
7472 if (dev
->disk_total_bytes
== 0) {
7473 dev
= btrfs_find_device(fs_info
->fs_devices
->seed
, devid
,
7476 btrfs_err(fs_info
, "failed to find seed devid %llu",
7483 if (physical_offset
+ physical_len
> dev
->disk_total_bytes
) {
7485 "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7486 devid
, physical_offset
, physical_len
,
7487 dev
->disk_total_bytes
);
7492 free_extent_map(em
);
7496 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info
*fs_info
)
7498 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
.map_tree
;
7499 struct extent_map
*em
;
7500 struct rb_node
*node
;
7503 read_lock(&em_tree
->lock
);
7504 for (node
= rb_first(&em_tree
->map
); node
; node
= rb_next(node
)) {
7505 em
= rb_entry(node
, struct extent_map
, rb_node
);
7506 if (em
->map_lookup
->num_stripes
!=
7507 em
->map_lookup
->verified_stripes
) {
7509 "chunk %llu has missing dev extent, have %d expect %d",
7510 em
->start
, em
->map_lookup
->verified_stripes
,
7511 em
->map_lookup
->num_stripes
);
7517 read_unlock(&em_tree
->lock
);
7522 * Ensure that all dev extents are mapped to correct chunk, otherwise
7523 * later chunk allocation/free would cause unexpected behavior.
7525 * NOTE: This will iterate through the whole device tree, which should be of
7526 * the same size level as the chunk tree. This slightly increases mount time.
7528 int btrfs_verify_dev_extents(struct btrfs_fs_info
*fs_info
)
7530 struct btrfs_path
*path
;
7531 struct btrfs_root
*root
= fs_info
->dev_root
;
7532 struct btrfs_key key
;
7534 u64 prev_dev_ext_end
= 0;
7538 key
.type
= BTRFS_DEV_EXTENT_KEY
;
7541 path
= btrfs_alloc_path();
7545 path
->reada
= READA_FORWARD
;
7546 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7550 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
7551 ret
= btrfs_next_item(root
, path
);
7554 /* No dev extents at all? Not good */
7561 struct extent_buffer
*leaf
= path
->nodes
[0];
7562 struct btrfs_dev_extent
*dext
;
7563 int slot
= path
->slots
[0];
7565 u64 physical_offset
;
7569 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7570 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
7572 devid
= key
.objectid
;
7573 physical_offset
= key
.offset
;
7575 dext
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dev_extent
);
7576 chunk_offset
= btrfs_dev_extent_chunk_offset(leaf
, dext
);
7577 physical_len
= btrfs_dev_extent_length(leaf
, dext
);
7579 /* Check if this dev extent overlaps with the previous one */
7580 if (devid
== prev_devid
&& physical_offset
< prev_dev_ext_end
) {
7582 "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7583 devid
, physical_offset
, prev_dev_ext_end
);
7588 ret
= verify_one_dev_extent(fs_info
, chunk_offset
, devid
,
7589 physical_offset
, physical_len
);
7593 prev_dev_ext_end
= physical_offset
+ physical_len
;
7595 ret
= btrfs_next_item(root
, path
);
7604 /* Ensure all chunks have corresponding dev extents */
7605 ret
= verify_chunk_dev_extent_mapping(fs_info
);
7607 btrfs_free_path(path
);