2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/iocontext.h>
24 #include <linux/capability.h>
25 #include <linux/ratelimit.h>
26 #include <linux/kthread.h>
27 #include <linux/raid/pq.h>
28 #include <linux/semaphore.h>
29 #include <linux/uuid.h>
30 #include <linux/list_sort.h>
31 #include <asm/div64.h>
33 #include "extent_map.h"
35 #include "transaction.h"
36 #include "print-tree.h"
39 #include "async-thread.h"
40 #include "check-integrity.h"
41 #include "rcu-string.h"
43 #include "dev-replace.h"
46 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
47 [BTRFS_RAID_RAID10] = {
50 .devs_max = 0, /* 0 == as many as possible */
52 .tolerated_failures = 1,
56 [BTRFS_RAID_RAID1] = {
61 .tolerated_failures = 1,
70 .tolerated_failures = 0,
74 [BTRFS_RAID_RAID0] = {
79 .tolerated_failures = 0,
83 [BTRFS_RAID_SINGLE] = {
88 .tolerated_failures = 0,
92 [BTRFS_RAID_RAID5] = {
97 .tolerated_failures = 1,
101 [BTRFS_RAID_RAID6] = {
106 .tolerated_failures = 2,
112 const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
113 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
114 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
115 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
116 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
117 [BTRFS_RAID_SINGLE] = 0,
118 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
119 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
123 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
124 * condition is not met. Zero means there's no corresponding
125 * BTRFS_ERROR_DEV_*_NOT_MET value.
127 const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
128 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
129 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
130 [BTRFS_RAID_DUP] = 0,
131 [BTRFS_RAID_RAID0] = 0,
132 [BTRFS_RAID_SINGLE] = 0,
133 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
134 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
137 static int init_first_rw_device(struct btrfs_trans_handle *trans,
138 struct btrfs_fs_info *fs_info);
139 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
140 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
141 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
142 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
143 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
144 enum btrfs_map_op op,
145 u64 logical, u64 *length,
146 struct btrfs_bio **bbio_ret,
147 int mirror_num, int need_raid_map);
153 * There are several mutexes that protect manipulation of devices and low-level
154 * structures like chunks but not block groups, extents or files
156 * uuid_mutex (global lock)
157 * ------------------------
158 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
159 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
160 * device) or requested by the device= mount option
162 * the mutex can be very coarse and can cover long-running operations
164 * protects: updates to fs_devices counters like missing devices, rw devices,
165 * seeding, structure cloning, openning/closing devices at mount/umount time
167 * global::fs_devs - add, remove, updates to the global list
169 * does not protect: manipulation of the fs_devices::devices list!
171 * btrfs_device::name - renames (write side), read is RCU
173 * fs_devices::device_list_mutex (per-fs, with RCU)
174 * ------------------------------------------------
175 * protects updates to fs_devices::devices, ie. adding and deleting
177 * simple list traversal with read-only actions can be done with RCU protection
179 * may be used to exclude some operations from running concurrently without any
180 * modifications to the list (see write_all_supers)
184 * coarse lock owned by a mounted filesystem; used to exclude some operations
185 * that cannot run in parallel and affect the higher-level properties of the
186 * filesystem like: device add/deleting/resize/replace, or balance
190 * protects balance structures (status, state) and context accessed from
191 * several places (internally, ioctl)
195 * protects chunks, adding or removing during allocation, trim or when a new
196 * device is added/removed
200 * a big lock that is held by the cleaner thread and prevents running subvolume
201 * cleaning together with relocation or delayed iputs
214 DEFINE_MUTEX(uuid_mutex);
215 static LIST_HEAD(fs_uuids);
216 struct list_head *btrfs_get_fs_uuids(void)
222 * alloc_fs_devices - allocate struct btrfs_fs_devices
223 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
225 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
226 * The returned struct is not linked onto any lists and can be destroyed with
227 * kfree() right away.
229 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
231 struct btrfs_fs_devices *fs_devs;
233 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
235 return ERR_PTR(-ENOMEM);
237 mutex_init(&fs_devs->device_list_mutex);
239 INIT_LIST_HEAD(&fs_devs->devices);
240 INIT_LIST_HEAD(&fs_devs->resized_devices);
241 INIT_LIST_HEAD(&fs_devs->alloc_list);
242 INIT_LIST_HEAD(&fs_devs->list);
244 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
249 static void free_device(struct btrfs_device *device)
251 rcu_string_free(device->name);
252 bio_put(device->flush_bio);
256 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
258 struct btrfs_device *device;
259 WARN_ON(fs_devices->opened);
260 while (!list_empty(&fs_devices->devices)) {
261 device = list_entry(fs_devices->devices.next,
262 struct btrfs_device, dev_list);
263 list_del(&device->dev_list);
269 static void btrfs_kobject_uevent(struct block_device *bdev,
270 enum kobject_action action)
274 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
276 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
278 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
279 &disk_to_dev(bdev->bd_disk)->kobj);
282 void btrfs_cleanup_fs_uuids(void)
284 struct btrfs_fs_devices *fs_devices;
286 while (!list_empty(&fs_uuids)) {
287 fs_devices = list_entry(fs_uuids.next,
288 struct btrfs_fs_devices, list);
289 list_del(&fs_devices->list);
290 free_fs_devices(fs_devices);
295 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
296 * Returned struct is not linked onto any lists and must be destroyed using
299 static struct btrfs_device *__alloc_device(void)
301 struct btrfs_device *dev;
303 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
305 return ERR_PTR(-ENOMEM);
308 * Preallocate a bio that's always going to be used for flushing device
309 * barriers and matches the device lifespan
311 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
312 if (!dev->flush_bio) {
314 return ERR_PTR(-ENOMEM);
317 INIT_LIST_HEAD(&dev->dev_list);
318 INIT_LIST_HEAD(&dev->dev_alloc_list);
319 INIT_LIST_HEAD(&dev->resized_list);
321 spin_lock_init(&dev->io_lock);
323 atomic_set(&dev->reada_in_flight, 0);
324 atomic_set(&dev->dev_stats_ccnt, 0);
325 btrfs_device_data_ordered_init(dev);
326 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
327 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
333 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
336 * If devid and uuid are both specified, the match must be exact, otherwise
337 * only devid is used.
339 static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
340 u64 devid, const u8 *uuid)
342 struct list_head *head = &fs_devices->devices;
343 struct btrfs_device *dev;
345 list_for_each_entry(dev, head, dev_list) {
346 if (dev->devid == devid &&
347 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
354 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
356 struct btrfs_fs_devices *fs_devices;
358 list_for_each_entry(fs_devices, &fs_uuids, list) {
359 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
366 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
367 int flush, struct block_device **bdev,
368 struct buffer_head **bh)
372 *bdev = blkdev_get_by_path(device_path, flags, holder);
375 ret = PTR_ERR(*bdev);
380 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
381 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
383 blkdev_put(*bdev, flags);
386 invalidate_bdev(*bdev);
387 *bh = btrfs_read_dev_super(*bdev);
390 blkdev_put(*bdev, flags);
402 static void requeue_list(struct btrfs_pending_bios *pending_bios,
403 struct bio *head, struct bio *tail)
406 struct bio *old_head;
408 old_head = pending_bios->head;
409 pending_bios->head = head;
410 if (pending_bios->tail)
411 tail->bi_next = old_head;
413 pending_bios->tail = tail;
417 * we try to collect pending bios for a device so we don't get a large
418 * number of procs sending bios down to the same device. This greatly
419 * improves the schedulers ability to collect and merge the bios.
421 * But, it also turns into a long list of bios to process and that is sure
422 * to eventually make the worker thread block. The solution here is to
423 * make some progress and then put this work struct back at the end of
424 * the list if the block device is congested. This way, multiple devices
425 * can make progress from a single worker thread.
427 static noinline void run_scheduled_bios(struct btrfs_device *device)
429 struct btrfs_fs_info *fs_info = device->fs_info;
431 struct backing_dev_info *bdi;
432 struct btrfs_pending_bios *pending_bios;
436 unsigned long num_run;
437 unsigned long batch_run = 0;
438 unsigned long last_waited = 0;
440 int sync_pending = 0;
441 struct blk_plug plug;
444 * this function runs all the bios we've collected for
445 * a particular device. We don't want to wander off to
446 * another device without first sending all of these down.
447 * So, setup a plug here and finish it off before we return
449 blk_start_plug(&plug);
451 bdi = device->bdev->bd_bdi;
454 spin_lock(&device->io_lock);
459 /* take all the bios off the list at once and process them
460 * later on (without the lock held). But, remember the
461 * tail and other pointers so the bios can be properly reinserted
462 * into the list if we hit congestion
464 if (!force_reg && device->pending_sync_bios.head) {
465 pending_bios = &device->pending_sync_bios;
468 pending_bios = &device->pending_bios;
472 pending = pending_bios->head;
473 tail = pending_bios->tail;
474 WARN_ON(pending && !tail);
477 * if pending was null this time around, no bios need processing
478 * at all and we can stop. Otherwise it'll loop back up again
479 * and do an additional check so no bios are missed.
481 * device->running_pending is used to synchronize with the
484 if (device->pending_sync_bios.head == NULL &&
485 device->pending_bios.head == NULL) {
487 device->running_pending = 0;
490 device->running_pending = 1;
493 pending_bios->head = NULL;
494 pending_bios->tail = NULL;
496 spin_unlock(&device->io_lock);
501 /* we want to work on both lists, but do more bios on the
502 * sync list than the regular list
505 pending_bios != &device->pending_sync_bios &&
506 device->pending_sync_bios.head) ||
507 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
508 device->pending_bios.head)) {
509 spin_lock(&device->io_lock);
510 requeue_list(pending_bios, pending, tail);
515 pending = pending->bi_next;
518 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
521 * if we're doing the sync list, record that our
522 * plug has some sync requests on it
524 * If we're doing the regular list and there are
525 * sync requests sitting around, unplug before
528 if (pending_bios == &device->pending_sync_bios) {
530 } else if (sync_pending) {
531 blk_finish_plug(&plug);
532 blk_start_plug(&plug);
536 btrfsic_submit_bio(cur);
543 * we made progress, there is more work to do and the bdi
544 * is now congested. Back off and let other work structs
547 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
548 fs_info->fs_devices->open_devices > 1) {
549 struct io_context *ioc;
551 ioc = current->io_context;
554 * the main goal here is that we don't want to
555 * block if we're going to be able to submit
556 * more requests without blocking.
558 * This code does two great things, it pokes into
559 * the elevator code from a filesystem _and_
560 * it makes assumptions about how batching works.
562 if (ioc && ioc->nr_batch_requests > 0 &&
563 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
565 ioc->last_waited == last_waited)) {
567 * we want to go through our batch of
568 * requests and stop. So, we copy out
569 * the ioc->last_waited time and test
570 * against it before looping
572 last_waited = ioc->last_waited;
576 spin_lock(&device->io_lock);
577 requeue_list(pending_bios, pending, tail);
578 device->running_pending = 1;
580 spin_unlock(&device->io_lock);
581 btrfs_queue_work(fs_info->submit_workers,
591 spin_lock(&device->io_lock);
592 if (device->pending_bios.head || device->pending_sync_bios.head)
594 spin_unlock(&device->io_lock);
597 blk_finish_plug(&plug);
600 static void pending_bios_fn(struct btrfs_work *work)
602 struct btrfs_device *device;
604 device = container_of(work, struct btrfs_device, work);
605 run_scheduled_bios(device);
609 * Search and remove all stale (devices which are not mounted) devices.
610 * When both inputs are NULL, it will search and release all stale devices.
611 * path: Optional. When provided will it release all unmounted devices
612 * matching this path only.
613 * skip_dev: Optional. Will skip this device when searching for the stale
616 static void btrfs_free_stale_devices(const char *path,
617 struct btrfs_device *skip_dev)
619 struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
620 struct btrfs_device *dev, *tmp_dev;
622 list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, list) {
627 list_for_each_entry_safe(dev, tmp_dev,
628 &fs_devs->devices, dev_list) {
631 if (skip_dev && skip_dev == dev)
633 if (path && !dev->name)
638 not_found = strcmp(rcu_str_deref(dev->name),
644 /* delete the stale device */
645 if (fs_devs->num_devices == 1) {
646 btrfs_sysfs_remove_fsid(fs_devs);
647 list_del(&fs_devs->list);
648 free_fs_devices(fs_devs);
651 fs_devs->num_devices--;
652 list_del(&dev->dev_list);
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(&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)
735 struct btrfs_device *device;
736 struct btrfs_fs_devices *fs_devices;
737 struct rcu_string *name;
738 u64 found_transid = btrfs_super_generation(disk_super);
739 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
741 fs_devices = find_fsid(disk_super->fsid);
743 fs_devices = alloc_fs_devices(disk_super->fsid);
744 if (IS_ERR(fs_devices))
745 return ERR_CAST(fs_devices);
747 list_add(&fs_devices->list, &fs_uuids);
751 device = find_device(fs_devices, devid,
752 disk_super->dev_item.uuid);
756 if (fs_devices->opened)
757 return ERR_PTR(-EBUSY);
759 device = btrfs_alloc_device(NULL, &devid,
760 disk_super->dev_item.uuid);
761 if (IS_ERR(device)) {
762 /* we can safely leave the fs_devices entry around */
766 name = rcu_string_strdup(path, GFP_NOFS);
769 return ERR_PTR(-ENOMEM);
771 rcu_assign_pointer(device->name, name);
773 mutex_lock(&fs_devices->device_list_mutex);
774 list_add_rcu(&device->dev_list, &fs_devices->devices);
775 fs_devices->num_devices++;
776 mutex_unlock(&fs_devices->device_list_mutex);
778 device->fs_devices = fs_devices;
779 btrfs_free_stale_devices(path, device);
781 if (disk_super->label[0])
782 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
783 disk_super->label, devid, found_transid, path);
785 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
786 disk_super->fsid, devid, found_transid, path);
788 } else if (!device->name || strcmp(device->name->str, path)) {
790 * When FS is already mounted.
791 * 1. If you are here and if the device->name is NULL that
792 * means this device was missing at time of FS mount.
793 * 2. If you are here and if the device->name is different
794 * from 'path' that means either
795 * a. The same device disappeared and reappeared with
797 * b. The missing-disk-which-was-replaced, has
800 * We must allow 1 and 2a above. But 2b would be a spurious
803 * Further in case of 1 and 2a above, the disk at 'path'
804 * would have missed some transaction when it was away and
805 * in case of 2a the stale bdev has to be updated as well.
806 * 2b must not be allowed at all time.
810 * For now, we do allow update to btrfs_fs_device through the
811 * btrfs dev scan cli after FS has been mounted. We're still
812 * tracking a problem where systems fail mount by subvolume id
813 * when we reject replacement on a mounted FS.
815 if (!fs_devices->opened && found_transid < device->generation) {
817 * That is if the FS is _not_ mounted and if you
818 * are here, that means there is more than one
819 * disk with same uuid and devid.We keep the one
820 * with larger generation number or the last-in if
821 * generation are equal.
823 return ERR_PTR(-EEXIST);
826 name = rcu_string_strdup(path, GFP_NOFS);
828 return ERR_PTR(-ENOMEM);
829 rcu_string_free(device->name);
830 rcu_assign_pointer(device->name, name);
831 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
832 fs_devices->missing_devices--;
833 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
838 * Unmount does not free the btrfs_device struct but would zero
839 * generation along with most of the other members. So just update
840 * it back. We need it to pick the disk with largest generation
843 if (!fs_devices->opened)
844 device->generation = found_transid;
846 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
851 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
853 struct btrfs_fs_devices *fs_devices;
854 struct btrfs_device *device;
855 struct btrfs_device *orig_dev;
857 fs_devices = alloc_fs_devices(orig->fsid);
858 if (IS_ERR(fs_devices))
861 mutex_lock(&orig->device_list_mutex);
862 fs_devices->total_devices = orig->total_devices;
864 /* We have held the volume lock, it is safe to get the devices. */
865 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
866 struct rcu_string *name;
868 device = btrfs_alloc_device(NULL, &orig_dev->devid,
874 * This is ok to do without rcu read locked because we hold the
875 * uuid mutex so nothing we touch in here is going to disappear.
877 if (orig_dev->name) {
878 name = rcu_string_strdup(orig_dev->name->str,
884 rcu_assign_pointer(device->name, name);
887 list_add(&device->dev_list, &fs_devices->devices);
888 device->fs_devices = fs_devices;
889 fs_devices->num_devices++;
891 mutex_unlock(&orig->device_list_mutex);
894 mutex_unlock(&orig->device_list_mutex);
895 free_fs_devices(fs_devices);
896 return ERR_PTR(-ENOMEM);
899 void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
901 struct btrfs_device *device, *next;
902 struct btrfs_device *latest_dev = NULL;
904 mutex_lock(&uuid_mutex);
906 /* This is the initialized path, it is safe to release the devices. */
907 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
908 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
909 &device->dev_state)) {
910 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
911 &device->dev_state) &&
913 device->generation > latest_dev->generation)) {
919 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
921 * In the first step, keep the device which has
922 * the correct fsid and the devid that is used
923 * for the dev_replace procedure.
924 * In the second step, the dev_replace state is
925 * read from the device tree and it is known
926 * whether the procedure is really active or
927 * not, which means whether this device is
928 * used or whether it should be removed.
930 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
931 &device->dev_state)) {
936 blkdev_put(device->bdev, device->mode);
938 fs_devices->open_devices--;
940 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
941 list_del_init(&device->dev_alloc_list);
942 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
943 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
945 fs_devices->rw_devices--;
947 list_del_init(&device->dev_list);
948 fs_devices->num_devices--;
952 if (fs_devices->seed) {
953 fs_devices = fs_devices->seed;
957 fs_devices->latest_bdev = latest_dev->bdev;
959 mutex_unlock(&uuid_mutex);
962 static void free_device_rcu(struct rcu_head *head)
964 struct btrfs_device *device;
966 device = container_of(head, struct btrfs_device, rcu);
970 static void btrfs_close_bdev(struct btrfs_device *device)
975 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
976 sync_blockdev(device->bdev);
977 invalidate_bdev(device->bdev);
980 blkdev_put(device->bdev, device->mode);
983 static void btrfs_prepare_close_one_device(struct btrfs_device *device)
985 struct btrfs_fs_devices *fs_devices = device->fs_devices;
986 struct btrfs_device *new_device;
987 struct rcu_string *name;
990 fs_devices->open_devices--;
992 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
993 device->devid != BTRFS_DEV_REPLACE_DEVID) {
994 list_del_init(&device->dev_alloc_list);
995 fs_devices->rw_devices--;
998 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
999 fs_devices->missing_devices--;
1001 new_device = btrfs_alloc_device(NULL, &device->devid,
1003 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1005 /* Safe because we are under uuid_mutex */
1007 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1008 BUG_ON(!name); /* -ENOMEM */
1009 rcu_assign_pointer(new_device->name, name);
1012 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1013 new_device->fs_devices = device->fs_devices;
1016 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1018 struct btrfs_device *device, *tmp;
1019 struct list_head pending_put;
1021 INIT_LIST_HEAD(&pending_put);
1023 if (--fs_devices->opened > 0)
1026 mutex_lock(&fs_devices->device_list_mutex);
1027 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1028 btrfs_prepare_close_one_device(device);
1029 list_add(&device->dev_list, &pending_put);
1031 mutex_unlock(&fs_devices->device_list_mutex);
1034 * btrfs_show_devname() is using the device_list_mutex,
1035 * sometimes call to blkdev_put() leads vfs calling
1036 * into this func. So do put outside of device_list_mutex,
1039 while (!list_empty(&pending_put)) {
1040 device = list_first_entry(&pending_put,
1041 struct btrfs_device, dev_list);
1042 list_del(&device->dev_list);
1043 btrfs_close_bdev(device);
1044 call_rcu(&device->rcu, free_device_rcu);
1047 WARN_ON(fs_devices->open_devices);
1048 WARN_ON(fs_devices->rw_devices);
1049 fs_devices->opened = 0;
1050 fs_devices->seeding = 0;
1055 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1057 struct btrfs_fs_devices *seed_devices = NULL;
1060 mutex_lock(&uuid_mutex);
1061 ret = __btrfs_close_devices(fs_devices);
1062 if (!fs_devices->opened) {
1063 seed_devices = fs_devices->seed;
1064 fs_devices->seed = NULL;
1066 mutex_unlock(&uuid_mutex);
1068 while (seed_devices) {
1069 fs_devices = seed_devices;
1070 seed_devices = fs_devices->seed;
1071 __btrfs_close_devices(fs_devices);
1072 free_fs_devices(fs_devices);
1077 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1078 fmode_t flags, void *holder)
1080 struct list_head *head = &fs_devices->devices;
1081 struct btrfs_device *device;
1082 struct btrfs_device *latest_dev = NULL;
1085 flags |= FMODE_EXCL;
1087 list_for_each_entry(device, head, dev_list) {
1088 /* Just open everything we can; ignore failures here */
1089 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1093 device->generation > latest_dev->generation)
1094 latest_dev = device;
1096 if (fs_devices->open_devices == 0) {
1100 fs_devices->opened = 1;
1101 fs_devices->latest_bdev = latest_dev->bdev;
1102 fs_devices->total_rw_bytes = 0;
1107 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1109 struct btrfs_device *dev1, *dev2;
1111 dev1 = list_entry(a, struct btrfs_device, dev_list);
1112 dev2 = list_entry(b, struct btrfs_device, dev_list);
1114 if (dev1->devid < dev2->devid)
1116 else if (dev1->devid > dev2->devid)
1121 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1122 fmode_t flags, void *holder)
1126 mutex_lock(&uuid_mutex);
1127 if (fs_devices->opened) {
1128 fs_devices->opened++;
1131 list_sort(NULL, &fs_devices->devices, devid_cmp);
1132 ret = __btrfs_open_devices(fs_devices, flags, holder);
1134 mutex_unlock(&uuid_mutex);
1138 static void btrfs_release_disk_super(struct page *page)
1144 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1146 struct btrfs_super_block **disk_super)
1151 /* make sure our super fits in the device */
1152 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1155 /* make sure our super fits in the page */
1156 if (sizeof(**disk_super) > PAGE_SIZE)
1159 /* make sure our super doesn't straddle pages on disk */
1160 index = bytenr >> PAGE_SHIFT;
1161 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1164 /* pull in the page with our super */
1165 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1168 if (IS_ERR_OR_NULL(*page))
1173 /* align our pointer to the offset of the super block */
1174 *disk_super = p + (bytenr & ~PAGE_MASK);
1176 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1177 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1178 btrfs_release_disk_super(*page);
1182 if ((*disk_super)->label[0] &&
1183 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1184 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1190 * Look for a btrfs signature on a device. This may be called out of the mount path
1191 * and we are not allowed to call set_blocksize during the scan. The superblock
1192 * is read via pagecache
1194 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
1195 struct btrfs_fs_devices **fs_devices_ret)
1197 struct btrfs_super_block *disk_super;
1198 struct btrfs_device *device;
1199 struct block_device *bdev;
1205 * we would like to check all the supers, but that would make
1206 * a btrfs mount succeed after a mkfs from a different FS.
1207 * So, we need to add a special mount option to scan for
1208 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1210 bytenr = btrfs_sb_offset(0);
1211 flags |= FMODE_EXCL;
1212 mutex_lock(&uuid_mutex);
1214 bdev = blkdev_get_by_path(path, flags, holder);
1216 ret = PTR_ERR(bdev);
1220 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1222 goto error_bdev_put;
1225 device = device_list_add(path, disk_super);
1227 ret = PTR_ERR(device);
1229 *fs_devices_ret = device->fs_devices;
1231 btrfs_release_disk_super(page);
1234 blkdev_put(bdev, flags);
1236 mutex_unlock(&uuid_mutex);
1240 /* helper to account the used device space in the range */
1241 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1242 u64 end, u64 *length)
1244 struct btrfs_key key;
1245 struct btrfs_root *root = device->fs_info->dev_root;
1246 struct btrfs_dev_extent *dev_extent;
1247 struct btrfs_path *path;
1251 struct extent_buffer *l;
1255 if (start >= device->total_bytes ||
1256 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
1259 path = btrfs_alloc_path();
1262 path->reada = READA_FORWARD;
1264 key.objectid = device->devid;
1266 key.type = BTRFS_DEV_EXTENT_KEY;
1268 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1272 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1279 slot = path->slots[0];
1280 if (slot >= btrfs_header_nritems(l)) {
1281 ret = btrfs_next_leaf(root, path);
1289 btrfs_item_key_to_cpu(l, &key, slot);
1291 if (key.objectid < device->devid)
1294 if (key.objectid > device->devid)
1297 if (key.type != BTRFS_DEV_EXTENT_KEY)
1300 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1301 extent_end = key.offset + btrfs_dev_extent_length(l,
1303 if (key.offset <= start && extent_end > end) {
1304 *length = end - start + 1;
1306 } else if (key.offset <= start && extent_end > start)
1307 *length += extent_end - start;
1308 else if (key.offset > start && extent_end <= end)
1309 *length += extent_end - key.offset;
1310 else if (key.offset > start && key.offset <= end) {
1311 *length += end - key.offset + 1;
1313 } else if (key.offset > end)
1321 btrfs_free_path(path);
1325 static int contains_pending_extent(struct btrfs_transaction *transaction,
1326 struct btrfs_device *device,
1327 u64 *start, u64 len)
1329 struct btrfs_fs_info *fs_info = device->fs_info;
1330 struct extent_map *em;
1331 struct list_head *search_list = &fs_info->pinned_chunks;
1333 u64 physical_start = *start;
1336 search_list = &transaction->pending_chunks;
1338 list_for_each_entry(em, search_list, list) {
1339 struct map_lookup *map;
1342 map = em->map_lookup;
1343 for (i = 0; i < map->num_stripes; i++) {
1346 if (map->stripes[i].dev != device)
1348 if (map->stripes[i].physical >= physical_start + len ||
1349 map->stripes[i].physical + em->orig_block_len <=
1353 * Make sure that while processing the pinned list we do
1354 * not override our *start with a lower value, because
1355 * we can have pinned chunks that fall within this
1356 * device hole and that have lower physical addresses
1357 * than the pending chunks we processed before. If we
1358 * do not take this special care we can end up getting
1359 * 2 pending chunks that start at the same physical
1360 * device offsets because the end offset of a pinned
1361 * chunk can be equal to the start offset of some
1364 end = map->stripes[i].physical + em->orig_block_len;
1371 if (search_list != &fs_info->pinned_chunks) {
1372 search_list = &fs_info->pinned_chunks;
1381 * find_free_dev_extent_start - find free space in the specified device
1382 * @device: the device which we search the free space in
1383 * @num_bytes: the size of the free space that we need
1384 * @search_start: the position from which to begin the search
1385 * @start: store the start of the free space.
1386 * @len: the size of the free space. that we find, or the size
1387 * of the max free space if we don't find suitable free space
1389 * this uses a pretty simple search, the expectation is that it is
1390 * called very infrequently and that a given device has a small number
1393 * @start is used to store the start of the free space if we find. But if we
1394 * don't find suitable free space, it will be used to store the start position
1395 * of the max free space.
1397 * @len is used to store the size of the free space that we find.
1398 * But if we don't find suitable free space, it is used to store the size of
1399 * the max free space.
1401 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1402 struct btrfs_device *device, u64 num_bytes,
1403 u64 search_start, u64 *start, u64 *len)
1405 struct btrfs_fs_info *fs_info = device->fs_info;
1406 struct btrfs_root *root = fs_info->dev_root;
1407 struct btrfs_key key;
1408 struct btrfs_dev_extent *dev_extent;
1409 struct btrfs_path *path;
1414 u64 search_end = device->total_bytes;
1417 struct extent_buffer *l;
1420 * We don't want to overwrite the superblock on the drive nor any area
1421 * used by the boot loader (grub for example), so we make sure to start
1422 * at an offset of at least 1MB.
1424 search_start = max_t(u64, search_start, SZ_1M);
1426 path = btrfs_alloc_path();
1430 max_hole_start = search_start;
1434 if (search_start >= search_end ||
1435 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1440 path->reada = READA_FORWARD;
1441 path->search_commit_root = 1;
1442 path->skip_locking = 1;
1444 key.objectid = device->devid;
1445 key.offset = search_start;
1446 key.type = BTRFS_DEV_EXTENT_KEY;
1448 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1452 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1459 slot = path->slots[0];
1460 if (slot >= btrfs_header_nritems(l)) {
1461 ret = btrfs_next_leaf(root, path);
1469 btrfs_item_key_to_cpu(l, &key, slot);
1471 if (key.objectid < device->devid)
1474 if (key.objectid > device->devid)
1477 if (key.type != BTRFS_DEV_EXTENT_KEY)
1480 if (key.offset > search_start) {
1481 hole_size = key.offset - search_start;
1484 * Have to check before we set max_hole_start, otherwise
1485 * we could end up sending back this offset anyway.
1487 if (contains_pending_extent(transaction, device,
1490 if (key.offset >= search_start) {
1491 hole_size = key.offset - search_start;
1498 if (hole_size > max_hole_size) {
1499 max_hole_start = search_start;
1500 max_hole_size = hole_size;
1504 * If this free space is greater than which we need,
1505 * it must be the max free space that we have found
1506 * until now, so max_hole_start must point to the start
1507 * of this free space and the length of this free space
1508 * is stored in max_hole_size. Thus, we return
1509 * max_hole_start and max_hole_size and go back to the
1512 if (hole_size >= num_bytes) {
1518 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1519 extent_end = key.offset + btrfs_dev_extent_length(l,
1521 if (extent_end > search_start)
1522 search_start = extent_end;
1529 * At this point, search_start should be the end of
1530 * allocated dev extents, and when shrinking the device,
1531 * search_end may be smaller than search_start.
1533 if (search_end > search_start) {
1534 hole_size = search_end - search_start;
1536 if (contains_pending_extent(transaction, device, &search_start,
1538 btrfs_release_path(path);
1542 if (hole_size > max_hole_size) {
1543 max_hole_start = search_start;
1544 max_hole_size = hole_size;
1549 if (max_hole_size < num_bytes)
1555 btrfs_free_path(path);
1556 *start = max_hole_start;
1558 *len = max_hole_size;
1562 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1563 struct btrfs_device *device, u64 num_bytes,
1564 u64 *start, u64 *len)
1566 /* FIXME use last free of some kind */
1567 return find_free_dev_extent_start(trans->transaction, device,
1568 num_bytes, 0, start, len);
1571 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1572 struct btrfs_device *device,
1573 u64 start, u64 *dev_extent_len)
1575 struct btrfs_fs_info *fs_info = device->fs_info;
1576 struct btrfs_root *root = fs_info->dev_root;
1578 struct btrfs_path *path;
1579 struct btrfs_key key;
1580 struct btrfs_key found_key;
1581 struct extent_buffer *leaf = NULL;
1582 struct btrfs_dev_extent *extent = NULL;
1584 path = btrfs_alloc_path();
1588 key.objectid = device->devid;
1590 key.type = BTRFS_DEV_EXTENT_KEY;
1592 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1594 ret = btrfs_previous_item(root, path, key.objectid,
1595 BTRFS_DEV_EXTENT_KEY);
1598 leaf = path->nodes[0];
1599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1600 extent = btrfs_item_ptr(leaf, path->slots[0],
1601 struct btrfs_dev_extent);
1602 BUG_ON(found_key.offset > start || found_key.offset +
1603 btrfs_dev_extent_length(leaf, extent) < start);
1605 btrfs_release_path(path);
1607 } else if (ret == 0) {
1608 leaf = path->nodes[0];
1609 extent = btrfs_item_ptr(leaf, path->slots[0],
1610 struct btrfs_dev_extent);
1612 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1616 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1618 ret = btrfs_del_item(trans, root, path);
1620 btrfs_handle_fs_error(fs_info, ret,
1621 "Failed to remove dev extent item");
1623 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1626 btrfs_free_path(path);
1630 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1631 struct btrfs_device *device,
1632 u64 chunk_offset, u64 start, u64 num_bytes)
1635 struct btrfs_path *path;
1636 struct btrfs_fs_info *fs_info = device->fs_info;
1637 struct btrfs_root *root = fs_info->dev_root;
1638 struct btrfs_dev_extent *extent;
1639 struct extent_buffer *leaf;
1640 struct btrfs_key key;
1642 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1643 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1644 path = btrfs_alloc_path();
1648 key.objectid = device->devid;
1650 key.type = BTRFS_DEV_EXTENT_KEY;
1651 ret = btrfs_insert_empty_item(trans, root, path, &key,
1656 leaf = path->nodes[0];
1657 extent = btrfs_item_ptr(leaf, path->slots[0],
1658 struct btrfs_dev_extent);
1659 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1660 BTRFS_CHUNK_TREE_OBJECTID);
1661 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1662 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1663 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1665 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1666 btrfs_mark_buffer_dirty(leaf);
1668 btrfs_free_path(path);
1672 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1674 struct extent_map_tree *em_tree;
1675 struct extent_map *em;
1679 em_tree = &fs_info->mapping_tree.map_tree;
1680 read_lock(&em_tree->lock);
1681 n = rb_last(&em_tree->map);
1683 em = rb_entry(n, struct extent_map, rb_node);
1684 ret = em->start + em->len;
1686 read_unlock(&em_tree->lock);
1691 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1695 struct btrfs_key key;
1696 struct btrfs_key found_key;
1697 struct btrfs_path *path;
1699 path = btrfs_alloc_path();
1703 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1704 key.type = BTRFS_DEV_ITEM_KEY;
1705 key.offset = (u64)-1;
1707 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1711 BUG_ON(ret == 0); /* Corruption */
1713 ret = btrfs_previous_item(fs_info->chunk_root, path,
1714 BTRFS_DEV_ITEMS_OBJECTID,
1715 BTRFS_DEV_ITEM_KEY);
1719 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1721 *devid_ret = found_key.offset + 1;
1725 btrfs_free_path(path);
1730 * the device information is stored in the chunk root
1731 * the btrfs_device struct should be fully filled in
1733 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1734 struct btrfs_fs_info *fs_info,
1735 struct btrfs_device *device)
1737 struct btrfs_root *root = fs_info->chunk_root;
1739 struct btrfs_path *path;
1740 struct btrfs_dev_item *dev_item;
1741 struct extent_buffer *leaf;
1742 struct btrfs_key key;
1745 path = btrfs_alloc_path();
1749 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1750 key.type = BTRFS_DEV_ITEM_KEY;
1751 key.offset = device->devid;
1753 ret = btrfs_insert_empty_item(trans, root, path, &key,
1758 leaf = path->nodes[0];
1759 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1761 btrfs_set_device_id(leaf, dev_item, device->devid);
1762 btrfs_set_device_generation(leaf, dev_item, 0);
1763 btrfs_set_device_type(leaf, dev_item, device->type);
1764 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1765 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1766 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1767 btrfs_set_device_total_bytes(leaf, dev_item,
1768 btrfs_device_get_disk_total_bytes(device));
1769 btrfs_set_device_bytes_used(leaf, dev_item,
1770 btrfs_device_get_bytes_used(device));
1771 btrfs_set_device_group(leaf, dev_item, 0);
1772 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1773 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1774 btrfs_set_device_start_offset(leaf, dev_item, 0);
1776 ptr = btrfs_device_uuid(dev_item);
1777 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1778 ptr = btrfs_device_fsid(dev_item);
1779 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
1780 btrfs_mark_buffer_dirty(leaf);
1784 btrfs_free_path(path);
1789 * Function to update ctime/mtime for a given device path.
1790 * Mainly used for ctime/mtime based probe like libblkid.
1792 static void update_dev_time(const char *path_name)
1796 filp = filp_open(path_name, O_RDWR, 0);
1799 file_update_time(filp);
1800 filp_close(filp, NULL);
1803 static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
1804 struct btrfs_device *device)
1806 struct btrfs_root *root = fs_info->chunk_root;
1808 struct btrfs_path *path;
1809 struct btrfs_key key;
1810 struct btrfs_trans_handle *trans;
1812 path = btrfs_alloc_path();
1816 trans = btrfs_start_transaction(root, 0);
1817 if (IS_ERR(trans)) {
1818 btrfs_free_path(path);
1819 return PTR_ERR(trans);
1821 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1822 key.type = BTRFS_DEV_ITEM_KEY;
1823 key.offset = device->devid;
1825 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1829 btrfs_abort_transaction(trans, ret);
1830 btrfs_end_transaction(trans);
1834 ret = btrfs_del_item(trans, root, path);
1836 btrfs_abort_transaction(trans, ret);
1837 btrfs_end_transaction(trans);
1841 btrfs_free_path(path);
1843 ret = btrfs_commit_transaction(trans);
1848 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1849 * filesystem. It's up to the caller to adjust that number regarding eg. device
1852 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1860 seq = read_seqbegin(&fs_info->profiles_lock);
1862 all_avail = fs_info->avail_data_alloc_bits |
1863 fs_info->avail_system_alloc_bits |
1864 fs_info->avail_metadata_alloc_bits;
1865 } while (read_seqretry(&fs_info->profiles_lock, seq));
1867 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1868 if (!(all_avail & btrfs_raid_group[i]))
1871 if (num_devices < btrfs_raid_array[i].devs_min) {
1872 int ret = btrfs_raid_mindev_error[i];
1882 static struct btrfs_device * btrfs_find_next_active_device(
1883 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
1885 struct btrfs_device *next_device;
1887 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1888 if (next_device != device &&
1889 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1890 && next_device->bdev)
1898 * Helper function to check if the given device is part of s_bdev / latest_bdev
1899 * and replace it with the provided or the next active device, in the context
1900 * where this function called, there should be always be another device (or
1901 * this_dev) which is active.
1903 void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1904 struct btrfs_device *device, struct btrfs_device *this_dev)
1906 struct btrfs_device *next_device;
1909 next_device = this_dev;
1911 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1913 ASSERT(next_device);
1915 if (fs_info->sb->s_bdev &&
1916 (fs_info->sb->s_bdev == device->bdev))
1917 fs_info->sb->s_bdev = next_device->bdev;
1919 if (fs_info->fs_devices->latest_bdev == device->bdev)
1920 fs_info->fs_devices->latest_bdev = next_device->bdev;
1923 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1926 struct btrfs_device *device;
1927 struct btrfs_fs_devices *cur_devices;
1931 mutex_lock(&fs_info->volume_mutex);
1932 mutex_lock(&uuid_mutex);
1934 num_devices = fs_info->fs_devices->num_devices;
1935 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1936 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1937 WARN_ON(num_devices < 1);
1940 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
1942 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1946 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1951 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1952 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1956 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1957 fs_info->fs_devices->rw_devices == 1) {
1958 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1962 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1963 mutex_lock(&fs_info->chunk_mutex);
1964 list_del_init(&device->dev_alloc_list);
1965 device->fs_devices->rw_devices--;
1966 mutex_unlock(&fs_info->chunk_mutex);
1969 mutex_unlock(&uuid_mutex);
1970 ret = btrfs_shrink_device(device, 0);
1971 mutex_lock(&uuid_mutex);
1976 * TODO: the superblock still includes this device in its num_devices
1977 * counter although write_all_supers() is not locked out. This
1978 * could give a filesystem state which requires a degraded mount.
1980 ret = btrfs_rm_dev_item(fs_info, device);
1984 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1985 btrfs_scrub_cancel_dev(fs_info, device);
1988 * the device list mutex makes sure that we don't change
1989 * the device list while someone else is writing out all
1990 * the device supers. Whoever is writing all supers, should
1991 * lock the device list mutex before getting the number of
1992 * devices in the super block (super_copy). Conversely,
1993 * whoever updates the number of devices in the super block
1994 * (super_copy) should hold the device list mutex.
1997 cur_devices = device->fs_devices;
1998 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1999 list_del_rcu(&device->dev_list);
2001 device->fs_devices->num_devices--;
2002 device->fs_devices->total_devices--;
2004 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
2005 device->fs_devices->missing_devices--;
2007 btrfs_assign_next_active_device(fs_info, device, NULL);
2010 device->fs_devices->open_devices--;
2011 /* remove sysfs entry */
2012 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
2015 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2016 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2017 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2020 * at this point, the device is zero sized and detached from
2021 * the devices list. All that's left is to zero out the old
2022 * supers and free the device.
2024 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2025 btrfs_scratch_superblocks(device->bdev, device->name->str);
2027 btrfs_close_bdev(device);
2028 call_rcu(&device->rcu, free_device_rcu);
2030 if (cur_devices->open_devices == 0) {
2031 struct btrfs_fs_devices *fs_devices;
2032 fs_devices = fs_info->fs_devices;
2033 while (fs_devices) {
2034 if (fs_devices->seed == cur_devices) {
2035 fs_devices->seed = cur_devices->seed;
2038 fs_devices = fs_devices->seed;
2040 cur_devices->seed = NULL;
2041 __btrfs_close_devices(cur_devices);
2042 free_fs_devices(cur_devices);
2046 mutex_unlock(&uuid_mutex);
2047 mutex_unlock(&fs_info->volume_mutex);
2051 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2052 mutex_lock(&fs_info->chunk_mutex);
2053 list_add(&device->dev_alloc_list,
2054 &fs_info->fs_devices->alloc_list);
2055 device->fs_devices->rw_devices++;
2056 mutex_unlock(&fs_info->chunk_mutex);
2061 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2062 struct btrfs_device *srcdev)
2064 struct btrfs_fs_devices *fs_devices;
2066 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
2069 * in case of fs with no seed, srcdev->fs_devices will point
2070 * to fs_devices of fs_info. However when the dev being replaced is
2071 * a seed dev it will point to the seed's local fs_devices. In short
2072 * srcdev will have its correct fs_devices in both the cases.
2074 fs_devices = srcdev->fs_devices;
2076 list_del_rcu(&srcdev->dev_list);
2077 list_del(&srcdev->dev_alloc_list);
2078 fs_devices->num_devices--;
2079 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2080 fs_devices->missing_devices--;
2082 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2083 fs_devices->rw_devices--;
2086 fs_devices->open_devices--;
2089 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2090 struct btrfs_device *srcdev)
2092 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2094 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2095 /* zero out the old super if it is writable */
2096 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2099 btrfs_close_bdev(srcdev);
2100 call_rcu(&srcdev->rcu, free_device_rcu);
2102 /* if this is no devs we rather delete the fs_devices */
2103 if (!fs_devices->num_devices) {
2104 struct btrfs_fs_devices *tmp_fs_devices;
2107 * On a mounted FS, num_devices can't be zero unless it's a
2108 * seed. In case of a seed device being replaced, the replace
2109 * target added to the sprout FS, so there will be no more
2110 * device left under the seed FS.
2112 ASSERT(fs_devices->seeding);
2114 tmp_fs_devices = fs_info->fs_devices;
2115 while (tmp_fs_devices) {
2116 if (tmp_fs_devices->seed == fs_devices) {
2117 tmp_fs_devices->seed = fs_devices->seed;
2120 tmp_fs_devices = tmp_fs_devices->seed;
2122 fs_devices->seed = NULL;
2123 __btrfs_close_devices(fs_devices);
2124 free_fs_devices(fs_devices);
2128 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2129 struct btrfs_device *tgtdev)
2131 mutex_lock(&uuid_mutex);
2133 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2135 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
2138 fs_info->fs_devices->open_devices--;
2140 fs_info->fs_devices->num_devices--;
2142 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
2144 list_del_rcu(&tgtdev->dev_list);
2146 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2147 mutex_unlock(&uuid_mutex);
2150 * The update_dev_time() with in btrfs_scratch_superblocks()
2151 * may lead to a call to btrfs_show_devname() which will try
2152 * to hold device_list_mutex. And here this device
2153 * is already out of device list, so we don't have to hold
2154 * the device_list_mutex lock.
2156 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2158 btrfs_close_bdev(tgtdev);
2159 call_rcu(&tgtdev->rcu, free_device_rcu);
2162 static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
2163 const char *device_path,
2164 struct btrfs_device **device)
2167 struct btrfs_super_block *disk_super;
2170 struct block_device *bdev;
2171 struct buffer_head *bh;
2174 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2175 fs_info->bdev_holder, 0, &bdev, &bh);
2178 disk_super = (struct btrfs_super_block *)bh->b_data;
2179 devid = btrfs_stack_device_id(&disk_super->dev_item);
2180 dev_uuid = disk_super->dev_item.uuid;
2181 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2185 blkdev_put(bdev, FMODE_READ);
2189 int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
2190 const char *device_path,
2191 struct btrfs_device **device)
2194 if (strcmp(device_path, "missing") == 0) {
2195 struct list_head *devices;
2196 struct btrfs_device *tmp;
2198 devices = &fs_info->fs_devices->devices;
2200 * It is safe to read the devices since the volume_mutex
2201 * is held by the caller.
2203 list_for_each_entry(tmp, devices, dev_list) {
2204 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2205 &tmp->dev_state) && !tmp->bdev) {
2212 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2216 return btrfs_find_device_by_path(fs_info, device_path, device);
2221 * Lookup a device given by device id, or the path if the id is 0.
2223 int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
2224 const char *devpath,
2225 struct btrfs_device **device)
2231 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
2235 if (!devpath || !devpath[0])
2238 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
2245 * does all the dirty work required for changing file system's UUID.
2247 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2249 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2250 struct btrfs_fs_devices *old_devices;
2251 struct btrfs_fs_devices *seed_devices;
2252 struct btrfs_super_block *disk_super = fs_info->super_copy;
2253 struct btrfs_device *device;
2256 BUG_ON(!mutex_is_locked(&uuid_mutex));
2257 if (!fs_devices->seeding)
2260 seed_devices = alloc_fs_devices(NULL);
2261 if (IS_ERR(seed_devices))
2262 return PTR_ERR(seed_devices);
2264 old_devices = clone_fs_devices(fs_devices);
2265 if (IS_ERR(old_devices)) {
2266 kfree(seed_devices);
2267 return PTR_ERR(old_devices);
2270 list_add(&old_devices->list, &fs_uuids);
2272 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2273 seed_devices->opened = 1;
2274 INIT_LIST_HEAD(&seed_devices->devices);
2275 INIT_LIST_HEAD(&seed_devices->alloc_list);
2276 mutex_init(&seed_devices->device_list_mutex);
2278 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2279 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2281 list_for_each_entry(device, &seed_devices->devices, dev_list)
2282 device->fs_devices = seed_devices;
2284 mutex_lock(&fs_info->chunk_mutex);
2285 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2286 mutex_unlock(&fs_info->chunk_mutex);
2288 fs_devices->seeding = 0;
2289 fs_devices->num_devices = 0;
2290 fs_devices->open_devices = 0;
2291 fs_devices->missing_devices = 0;
2292 fs_devices->rotating = 0;
2293 fs_devices->seed = seed_devices;
2295 generate_random_uuid(fs_devices->fsid);
2296 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2297 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2298 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2300 super_flags = btrfs_super_flags(disk_super) &
2301 ~BTRFS_SUPER_FLAG_SEEDING;
2302 btrfs_set_super_flags(disk_super, super_flags);
2308 * Store the expected generation for seed devices in device items.
2310 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2311 struct btrfs_fs_info *fs_info)
2313 struct btrfs_root *root = fs_info->chunk_root;
2314 struct btrfs_path *path;
2315 struct extent_buffer *leaf;
2316 struct btrfs_dev_item *dev_item;
2317 struct btrfs_device *device;
2318 struct btrfs_key key;
2319 u8 fs_uuid[BTRFS_FSID_SIZE];
2320 u8 dev_uuid[BTRFS_UUID_SIZE];
2324 path = btrfs_alloc_path();
2328 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2330 key.type = BTRFS_DEV_ITEM_KEY;
2333 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2337 leaf = path->nodes[0];
2339 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2340 ret = btrfs_next_leaf(root, path);
2345 leaf = path->nodes[0];
2346 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2347 btrfs_release_path(path);
2351 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2352 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2353 key.type != BTRFS_DEV_ITEM_KEY)
2356 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2357 struct btrfs_dev_item);
2358 devid = btrfs_device_id(leaf, dev_item);
2359 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2361 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2363 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
2364 BUG_ON(!device); /* Logic error */
2366 if (device->fs_devices->seeding) {
2367 btrfs_set_device_generation(leaf, dev_item,
2368 device->generation);
2369 btrfs_mark_buffer_dirty(leaf);
2377 btrfs_free_path(path);
2381 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2383 struct btrfs_root *root = fs_info->dev_root;
2384 struct request_queue *q;
2385 struct btrfs_trans_handle *trans;
2386 struct btrfs_device *device;
2387 struct block_device *bdev;
2388 struct list_head *devices;
2389 struct super_block *sb = fs_info->sb;
2390 struct rcu_string *name;
2392 int seeding_dev = 0;
2394 bool unlocked = false;
2396 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
2399 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2400 fs_info->bdev_holder);
2402 return PTR_ERR(bdev);
2404 if (fs_info->fs_devices->seeding) {
2406 down_write(&sb->s_umount);
2407 mutex_lock(&uuid_mutex);
2410 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2412 devices = &fs_info->fs_devices->devices;
2414 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2415 list_for_each_entry(device, devices, dev_list) {
2416 if (device->bdev == bdev) {
2419 &fs_info->fs_devices->device_list_mutex);
2423 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2425 device = btrfs_alloc_device(fs_info, NULL, NULL);
2426 if (IS_ERR(device)) {
2427 /* we can safely leave the fs_devices entry around */
2428 ret = PTR_ERR(device);
2432 name = rcu_string_strdup(device_path, GFP_KERNEL);
2435 goto error_free_device;
2437 rcu_assign_pointer(device->name, name);
2439 trans = btrfs_start_transaction(root, 0);
2440 if (IS_ERR(trans)) {
2441 ret = PTR_ERR(trans);
2442 goto error_free_device;
2445 q = bdev_get_queue(bdev);
2446 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2447 device->generation = trans->transid;
2448 device->io_width = fs_info->sectorsize;
2449 device->io_align = fs_info->sectorsize;
2450 device->sector_size = fs_info->sectorsize;
2451 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2452 fs_info->sectorsize);
2453 device->disk_total_bytes = device->total_bytes;
2454 device->commit_total_bytes = device->total_bytes;
2455 device->fs_info = fs_info;
2456 device->bdev = bdev;
2457 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2458 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2459 device->mode = FMODE_EXCL;
2460 device->dev_stats_valid = 1;
2461 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2464 sb->s_flags &= ~SB_RDONLY;
2465 ret = btrfs_prepare_sprout(fs_info);
2467 btrfs_abort_transaction(trans, ret);
2472 device->fs_devices = fs_info->fs_devices;
2474 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2475 mutex_lock(&fs_info->chunk_mutex);
2476 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
2477 list_add(&device->dev_alloc_list,
2478 &fs_info->fs_devices->alloc_list);
2479 fs_info->fs_devices->num_devices++;
2480 fs_info->fs_devices->open_devices++;
2481 fs_info->fs_devices->rw_devices++;
2482 fs_info->fs_devices->total_devices++;
2483 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2485 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2487 if (!blk_queue_nonrot(q))
2488 fs_info->fs_devices->rotating = 1;
2490 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2491 btrfs_set_super_total_bytes(fs_info->super_copy,
2492 round_down(tmp + device->total_bytes, fs_info->sectorsize));
2494 tmp = btrfs_super_num_devices(fs_info->super_copy);
2495 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
2497 /* add sysfs device entry */
2498 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
2501 * we've got more storage, clear any full flags on the space
2504 btrfs_clear_space_info_full(fs_info);
2506 mutex_unlock(&fs_info->chunk_mutex);
2507 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2510 mutex_lock(&fs_info->chunk_mutex);
2511 ret = init_first_rw_device(trans, fs_info);
2512 mutex_unlock(&fs_info->chunk_mutex);
2514 btrfs_abort_transaction(trans, ret);
2519 ret = btrfs_add_dev_item(trans, fs_info, device);
2521 btrfs_abort_transaction(trans, ret);
2526 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2528 ret = btrfs_finish_sprout(trans, fs_info);
2530 btrfs_abort_transaction(trans, ret);
2534 /* Sprouting would change fsid of the mounted root,
2535 * so rename the fsid on the sysfs
2537 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2539 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2541 "sysfs: failed to create fsid for sprout");
2544 ret = btrfs_commit_transaction(trans);
2547 mutex_unlock(&uuid_mutex);
2548 up_write(&sb->s_umount);
2551 if (ret) /* transaction commit */
2554 ret = btrfs_relocate_sys_chunks(fs_info);
2556 btrfs_handle_fs_error(fs_info, ret,
2557 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2558 trans = btrfs_attach_transaction(root);
2559 if (IS_ERR(trans)) {
2560 if (PTR_ERR(trans) == -ENOENT)
2562 ret = PTR_ERR(trans);
2566 ret = btrfs_commit_transaction(trans);
2569 /* Update ctime/mtime for libblkid */
2570 update_dev_time(device_path);
2574 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
2577 sb->s_flags |= SB_RDONLY;
2579 btrfs_end_transaction(trans);
2581 free_device(device);
2583 blkdev_put(bdev, FMODE_EXCL);
2584 if (seeding_dev && !unlocked) {
2585 mutex_unlock(&uuid_mutex);
2586 up_write(&sb->s_umount);
2591 int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2592 const char *device_path,
2593 struct btrfs_device *srcdev,
2594 struct btrfs_device **device_out)
2596 struct btrfs_device *device;
2597 struct block_device *bdev;
2598 struct list_head *devices;
2599 struct rcu_string *name;
2600 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2604 if (fs_info->fs_devices->seeding) {
2605 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
2609 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2610 fs_info->bdev_holder);
2612 btrfs_err(fs_info, "target device %s is invalid!", device_path);
2613 return PTR_ERR(bdev);
2616 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2618 devices = &fs_info->fs_devices->devices;
2619 list_for_each_entry(device, devices, dev_list) {
2620 if (device->bdev == bdev) {
2622 "target device is in the filesystem!");
2629 if (i_size_read(bdev->bd_inode) <
2630 btrfs_device_get_total_bytes(srcdev)) {
2632 "target device is smaller than source device!");
2638 device = btrfs_alloc_device(NULL, &devid, NULL);
2639 if (IS_ERR(device)) {
2640 ret = PTR_ERR(device);
2644 name = rcu_string_strdup(device_path, GFP_KERNEL);
2646 free_device(device);
2650 rcu_assign_pointer(device->name, name);
2652 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2653 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2654 device->generation = 0;
2655 device->io_width = fs_info->sectorsize;
2656 device->io_align = fs_info->sectorsize;
2657 device->sector_size = fs_info->sectorsize;
2658 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2659 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2660 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
2661 ASSERT(list_empty(&srcdev->resized_list));
2662 device->commit_total_bytes = srcdev->commit_total_bytes;
2663 device->commit_bytes_used = device->bytes_used;
2664 device->fs_info = fs_info;
2665 device->bdev = bdev;
2666 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2667 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2668 device->mode = FMODE_EXCL;
2669 device->dev_stats_valid = 1;
2670 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2671 device->fs_devices = fs_info->fs_devices;
2672 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2673 fs_info->fs_devices->num_devices++;
2674 fs_info->fs_devices->open_devices++;
2675 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2677 *device_out = device;
2681 blkdev_put(bdev, FMODE_EXCL);
2685 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2686 struct btrfs_device *device)
2689 struct btrfs_path *path;
2690 struct btrfs_root *root = device->fs_info->chunk_root;
2691 struct btrfs_dev_item *dev_item;
2692 struct extent_buffer *leaf;
2693 struct btrfs_key key;
2695 path = btrfs_alloc_path();
2699 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2700 key.type = BTRFS_DEV_ITEM_KEY;
2701 key.offset = device->devid;
2703 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2712 leaf = path->nodes[0];
2713 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2715 btrfs_set_device_id(leaf, dev_item, device->devid);
2716 btrfs_set_device_type(leaf, dev_item, device->type);
2717 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2718 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2719 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2720 btrfs_set_device_total_bytes(leaf, dev_item,
2721 btrfs_device_get_disk_total_bytes(device));
2722 btrfs_set_device_bytes_used(leaf, dev_item,
2723 btrfs_device_get_bytes_used(device));
2724 btrfs_mark_buffer_dirty(leaf);
2727 btrfs_free_path(path);
2731 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2732 struct btrfs_device *device, u64 new_size)
2734 struct btrfs_fs_info *fs_info = device->fs_info;
2735 struct btrfs_super_block *super_copy = fs_info->super_copy;
2736 struct btrfs_fs_devices *fs_devices;
2740 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2743 new_size = round_down(new_size, fs_info->sectorsize);
2745 mutex_lock(&fs_info->chunk_mutex);
2746 old_total = btrfs_super_total_bytes(super_copy);
2747 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2749 if (new_size <= device->total_bytes ||
2750 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2751 mutex_unlock(&fs_info->chunk_mutex);
2755 fs_devices = fs_info->fs_devices;
2757 btrfs_set_super_total_bytes(super_copy,
2758 round_down(old_total + diff, fs_info->sectorsize));
2759 device->fs_devices->total_rw_bytes += diff;
2761 btrfs_device_set_total_bytes(device, new_size);
2762 btrfs_device_set_disk_total_bytes(device, new_size);
2763 btrfs_clear_space_info_full(device->fs_info);
2764 if (list_empty(&device->resized_list))
2765 list_add_tail(&device->resized_list,
2766 &fs_devices->resized_devices);
2767 mutex_unlock(&fs_info->chunk_mutex);
2769 return btrfs_update_device(trans, device);
2772 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2773 struct btrfs_fs_info *fs_info, u64 chunk_offset)
2775 struct btrfs_root *root = fs_info->chunk_root;
2777 struct btrfs_path *path;
2778 struct btrfs_key key;
2780 path = btrfs_alloc_path();
2784 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2785 key.offset = chunk_offset;
2786 key.type = BTRFS_CHUNK_ITEM_KEY;
2788 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2791 else if (ret > 0) { /* Logic error or corruption */
2792 btrfs_handle_fs_error(fs_info, -ENOENT,
2793 "Failed lookup while freeing chunk.");
2798 ret = btrfs_del_item(trans, root, path);
2800 btrfs_handle_fs_error(fs_info, ret,
2801 "Failed to delete chunk item.");
2803 btrfs_free_path(path);
2807 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2809 struct btrfs_super_block *super_copy = fs_info->super_copy;
2810 struct btrfs_disk_key *disk_key;
2811 struct btrfs_chunk *chunk;
2818 struct btrfs_key key;
2820 mutex_lock(&fs_info->chunk_mutex);
2821 array_size = btrfs_super_sys_array_size(super_copy);
2823 ptr = super_copy->sys_chunk_array;
2826 while (cur < array_size) {
2827 disk_key = (struct btrfs_disk_key *)ptr;
2828 btrfs_disk_key_to_cpu(&key, disk_key);
2830 len = sizeof(*disk_key);
2832 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2833 chunk = (struct btrfs_chunk *)(ptr + len);
2834 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2835 len += btrfs_chunk_item_size(num_stripes);
2840 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2841 key.offset == chunk_offset) {
2842 memmove(ptr, ptr + len, array_size - (cur + len));
2844 btrfs_set_super_sys_array_size(super_copy, array_size);
2850 mutex_unlock(&fs_info->chunk_mutex);
2854 static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2855 u64 logical, u64 length)
2857 struct extent_map_tree *em_tree;
2858 struct extent_map *em;
2860 em_tree = &fs_info->mapping_tree.map_tree;
2861 read_lock(&em_tree->lock);
2862 em = lookup_extent_mapping(em_tree, logical, length);
2863 read_unlock(&em_tree->lock);
2866 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2868 return ERR_PTR(-EINVAL);
2871 if (em->start > logical || em->start + em->len < logical) {
2873 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2874 logical, length, em->start, em->start + em->len);
2875 free_extent_map(em);
2876 return ERR_PTR(-EINVAL);
2879 /* callers are responsible for dropping em's ref. */
2883 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2884 struct btrfs_fs_info *fs_info, u64 chunk_offset)
2886 struct extent_map *em;
2887 struct map_lookup *map;
2888 u64 dev_extent_len = 0;
2890 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2892 em = get_chunk_map(fs_info, chunk_offset, 1);
2895 * This is a logic error, but we don't want to just rely on the
2896 * user having built with ASSERT enabled, so if ASSERT doesn't
2897 * do anything we still error out.
2902 map = em->map_lookup;
2903 mutex_lock(&fs_info->chunk_mutex);
2904 check_system_chunk(trans, fs_info, map->type);
2905 mutex_unlock(&fs_info->chunk_mutex);
2908 * Take the device list mutex to prevent races with the final phase of
2909 * a device replace operation that replaces the device object associated
2910 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2912 mutex_lock(&fs_devices->device_list_mutex);
2913 for (i = 0; i < map->num_stripes; i++) {
2914 struct btrfs_device *device = map->stripes[i].dev;
2915 ret = btrfs_free_dev_extent(trans, device,
2916 map->stripes[i].physical,
2919 mutex_unlock(&fs_devices->device_list_mutex);
2920 btrfs_abort_transaction(trans, ret);
2924 if (device->bytes_used > 0) {
2925 mutex_lock(&fs_info->chunk_mutex);
2926 btrfs_device_set_bytes_used(device,
2927 device->bytes_used - dev_extent_len);
2928 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
2929 btrfs_clear_space_info_full(fs_info);
2930 mutex_unlock(&fs_info->chunk_mutex);
2933 if (map->stripes[i].dev) {
2934 ret = btrfs_update_device(trans, map->stripes[i].dev);
2936 mutex_unlock(&fs_devices->device_list_mutex);
2937 btrfs_abort_transaction(trans, ret);
2942 mutex_unlock(&fs_devices->device_list_mutex);
2944 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
2946 btrfs_abort_transaction(trans, ret);
2950 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
2952 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2953 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
2955 btrfs_abort_transaction(trans, ret);
2960 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
2962 btrfs_abort_transaction(trans, ret);
2968 free_extent_map(em);
2972 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2974 struct btrfs_root *root = fs_info->chunk_root;
2975 struct btrfs_trans_handle *trans;
2979 * Prevent races with automatic removal of unused block groups.
2980 * After we relocate and before we remove the chunk with offset
2981 * chunk_offset, automatic removal of the block group can kick in,
2982 * resulting in a failure when calling btrfs_remove_chunk() below.
2984 * Make sure to acquire this mutex before doing a tree search (dev
2985 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2986 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2987 * we release the path used to search the chunk/dev tree and before
2988 * the current task acquires this mutex and calls us.
2990 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
2992 ret = btrfs_can_relocate(fs_info, chunk_offset);
2996 /* step one, relocate all the extents inside this chunk */
2997 btrfs_scrub_pause(fs_info);
2998 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2999 btrfs_scrub_continue(fs_info);
3003 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3005 if (IS_ERR(trans)) {
3006 ret = PTR_ERR(trans);
3007 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3012 * step two, delete the device extents and the
3013 * chunk tree entries
3015 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
3016 btrfs_end_transaction(trans);
3020 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
3022 struct btrfs_root *chunk_root = fs_info->chunk_root;
3023 struct btrfs_path *path;
3024 struct extent_buffer *leaf;
3025 struct btrfs_chunk *chunk;
3026 struct btrfs_key key;
3027 struct btrfs_key found_key;
3029 bool retried = false;
3033 path = btrfs_alloc_path();
3038 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3039 key.offset = (u64)-1;
3040 key.type = BTRFS_CHUNK_ITEM_KEY;
3043 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3044 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3046 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3049 BUG_ON(ret == 0); /* Corruption */
3051 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3054 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3060 leaf = path->nodes[0];
3061 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3063 chunk = btrfs_item_ptr(leaf, path->slots[0],
3064 struct btrfs_chunk);
3065 chunk_type = btrfs_chunk_type(leaf, chunk);
3066 btrfs_release_path(path);
3068 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
3069 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3075 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3077 if (found_key.offset == 0)
3079 key.offset = found_key.offset - 1;
3082 if (failed && !retried) {
3086 } else if (WARN_ON(failed && retried)) {
3090 btrfs_free_path(path);
3095 * return 1 : allocate a data chunk successfully,
3096 * return <0: errors during allocating a data chunk,
3097 * return 0 : no need to allocate a data chunk.
3099 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3102 struct btrfs_block_group_cache *cache;
3106 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3108 chunk_type = cache->flags;
3109 btrfs_put_block_group(cache);
3111 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3112 spin_lock(&fs_info->data_sinfo->lock);
3113 bytes_used = fs_info->data_sinfo->bytes_used;
3114 spin_unlock(&fs_info->data_sinfo->lock);
3117 struct btrfs_trans_handle *trans;
3120 trans = btrfs_join_transaction(fs_info->tree_root);
3122 return PTR_ERR(trans);
3124 ret = btrfs_force_chunk_alloc(trans, fs_info,
3125 BTRFS_BLOCK_GROUP_DATA);
3126 btrfs_end_transaction(trans);
3136 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3137 struct btrfs_balance_control *bctl)
3139 struct btrfs_root *root = fs_info->tree_root;
3140 struct btrfs_trans_handle *trans;
3141 struct btrfs_balance_item *item;
3142 struct btrfs_disk_balance_args disk_bargs;
3143 struct btrfs_path *path;
3144 struct extent_buffer *leaf;
3145 struct btrfs_key key;
3148 path = btrfs_alloc_path();
3152 trans = btrfs_start_transaction(root, 0);
3153 if (IS_ERR(trans)) {
3154 btrfs_free_path(path);
3155 return PTR_ERR(trans);
3158 key.objectid = BTRFS_BALANCE_OBJECTID;
3159 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3162 ret = btrfs_insert_empty_item(trans, root, path, &key,
3167 leaf = path->nodes[0];
3168 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3170 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3172 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3173 btrfs_set_balance_data(leaf, item, &disk_bargs);
3174 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3175 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3176 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3177 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3179 btrfs_set_balance_flags(leaf, item, bctl->flags);
3181 btrfs_mark_buffer_dirty(leaf);
3183 btrfs_free_path(path);
3184 err = btrfs_commit_transaction(trans);
3190 static int del_balance_item(struct btrfs_fs_info *fs_info)
3192 struct btrfs_root *root = fs_info->tree_root;
3193 struct btrfs_trans_handle *trans;
3194 struct btrfs_path *path;
3195 struct btrfs_key key;
3198 path = btrfs_alloc_path();
3202 trans = btrfs_start_transaction(root, 0);
3203 if (IS_ERR(trans)) {
3204 btrfs_free_path(path);
3205 return PTR_ERR(trans);
3208 key.objectid = BTRFS_BALANCE_OBJECTID;
3209 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3212 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3220 ret = btrfs_del_item(trans, root, path);
3222 btrfs_free_path(path);
3223 err = btrfs_commit_transaction(trans);
3230 * This is a heuristic used to reduce the number of chunks balanced on
3231 * resume after balance was interrupted.
3233 static void update_balance_args(struct btrfs_balance_control *bctl)
3236 * Turn on soft mode for chunk types that were being converted.
3238 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3239 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3240 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3241 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3242 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3243 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;