2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <linux/raid/pq.h>
29 #include <linux/semaphore.h>
30 #include <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
45 static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
50 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
51 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
53 DEFINE_MUTEX(uuid_mutex);
54 static LIST_HEAD(fs_uuids);
55 struct list_head *btrfs_get_fs_uuids(void)
60 static struct btrfs_fs_devices *__alloc_fs_devices(void)
62 struct btrfs_fs_devices *fs_devs;
64 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
66 return ERR_PTR(-ENOMEM);
68 mutex_init(&fs_devs->device_list_mutex);
70 INIT_LIST_HEAD(&fs_devs->devices);
71 INIT_LIST_HEAD(&fs_devs->resized_devices);
72 INIT_LIST_HEAD(&fs_devs->alloc_list);
73 INIT_LIST_HEAD(&fs_devs->list);
79 * alloc_fs_devices - allocate struct btrfs_fs_devices
80 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
83 * Return: a pointer to a new &struct btrfs_fs_devices on success;
84 * ERR_PTR() on error. Returned struct is not linked onto any lists and
85 * can be destroyed with kfree() right away.
87 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
89 struct btrfs_fs_devices *fs_devs;
91 fs_devs = __alloc_fs_devices();
96 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
98 generate_random_uuid(fs_devs->fsid);
103 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
105 struct btrfs_device *device;
106 WARN_ON(fs_devices->opened);
107 while (!list_empty(&fs_devices->devices)) {
108 device = list_entry(fs_devices->devices.next,
109 struct btrfs_device, dev_list);
110 list_del(&device->dev_list);
111 rcu_string_free(device->name);
117 static void btrfs_kobject_uevent(struct block_device *bdev,
118 enum kobject_action action)
122 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
124 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
126 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
127 &disk_to_dev(bdev->bd_disk)->kobj);
130 void btrfs_cleanup_fs_uuids(void)
132 struct btrfs_fs_devices *fs_devices;
134 while (!list_empty(&fs_uuids)) {
135 fs_devices = list_entry(fs_uuids.next,
136 struct btrfs_fs_devices, list);
137 list_del(&fs_devices->list);
138 free_fs_devices(fs_devices);
142 static struct btrfs_device *__alloc_device(void)
144 struct btrfs_device *dev;
146 dev = kzalloc(sizeof(*dev), GFP_NOFS);
148 return ERR_PTR(-ENOMEM);
150 INIT_LIST_HEAD(&dev->dev_list);
151 INIT_LIST_HEAD(&dev->dev_alloc_list);
152 INIT_LIST_HEAD(&dev->resized_list);
154 spin_lock_init(&dev->io_lock);
156 spin_lock_init(&dev->reada_lock);
157 atomic_set(&dev->reada_in_flight, 0);
158 atomic_set(&dev->dev_stats_ccnt, 0);
159 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
160 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
165 static noinline struct btrfs_device *__find_device(struct list_head *head,
168 struct btrfs_device *dev;
170 list_for_each_entry(dev, head, dev_list) {
171 if (dev->devid == devid &&
172 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
179 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
181 struct btrfs_fs_devices *fs_devices;
183 list_for_each_entry(fs_devices, &fs_uuids, list) {
184 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
191 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
192 int flush, struct block_device **bdev,
193 struct buffer_head **bh)
197 *bdev = blkdev_get_by_path(device_path, flags, holder);
200 ret = PTR_ERR(*bdev);
201 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
206 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
207 ret = set_blocksize(*bdev, 4096);
209 blkdev_put(*bdev, flags);
212 invalidate_bdev(*bdev);
213 *bh = btrfs_read_dev_super(*bdev);
216 blkdev_put(*bdev, flags);
228 static void requeue_list(struct btrfs_pending_bios *pending_bios,
229 struct bio *head, struct bio *tail)
232 struct bio *old_head;
234 old_head = pending_bios->head;
235 pending_bios->head = head;
236 if (pending_bios->tail)
237 tail->bi_next = old_head;
239 pending_bios->tail = tail;
243 * we try to collect pending bios for a device so we don't get a large
244 * number of procs sending bios down to the same device. This greatly
245 * improves the schedulers ability to collect and merge the bios.
247 * But, it also turns into a long list of bios to process and that is sure
248 * to eventually make the worker thread block. The solution here is to
249 * make some progress and then put this work struct back at the end of
250 * the list if the block device is congested. This way, multiple devices
251 * can make progress from a single worker thread.
253 static noinline void run_scheduled_bios(struct btrfs_device *device)
256 struct backing_dev_info *bdi;
257 struct btrfs_fs_info *fs_info;
258 struct btrfs_pending_bios *pending_bios;
262 unsigned long num_run;
263 unsigned long batch_run = 0;
265 unsigned long last_waited = 0;
267 int sync_pending = 0;
268 struct blk_plug plug;
271 * this function runs all the bios we've collected for
272 * a particular device. We don't want to wander off to
273 * another device without first sending all of these down.
274 * So, setup a plug here and finish it off before we return
276 blk_start_plug(&plug);
278 bdi = blk_get_backing_dev_info(device->bdev);
279 fs_info = device->dev_root->fs_info;
280 limit = btrfs_async_submit_limit(fs_info);
281 limit = limit * 2 / 3;
284 spin_lock(&device->io_lock);
289 /* take all the bios off the list at once and process them
290 * later on (without the lock held). But, remember the
291 * tail and other pointers so the bios can be properly reinserted
292 * into the list if we hit congestion
294 if (!force_reg && device->pending_sync_bios.head) {
295 pending_bios = &device->pending_sync_bios;
298 pending_bios = &device->pending_bios;
302 pending = pending_bios->head;
303 tail = pending_bios->tail;
304 WARN_ON(pending && !tail);
307 * if pending was null this time around, no bios need processing
308 * at all and we can stop. Otherwise it'll loop back up again
309 * and do an additional check so no bios are missed.
311 * device->running_pending is used to synchronize with the
314 if (device->pending_sync_bios.head == NULL &&
315 device->pending_bios.head == NULL) {
317 device->running_pending = 0;
320 device->running_pending = 1;
323 pending_bios->head = NULL;
324 pending_bios->tail = NULL;
326 spin_unlock(&device->io_lock);
331 /* we want to work on both lists, but do more bios on the
332 * sync list than the regular list
335 pending_bios != &device->pending_sync_bios &&
336 device->pending_sync_bios.head) ||
337 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
338 device->pending_bios.head)) {
339 spin_lock(&device->io_lock);
340 requeue_list(pending_bios, pending, tail);
345 pending = pending->bi_next;
349 * atomic_dec_return implies a barrier for waitqueue_active
351 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
352 waitqueue_active(&fs_info->async_submit_wait))
353 wake_up(&fs_info->async_submit_wait);
355 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
358 * if we're doing the sync list, record that our
359 * plug has some sync requests on it
361 * If we're doing the regular list and there are
362 * sync requests sitting around, unplug before
365 if (pending_bios == &device->pending_sync_bios) {
367 } else if (sync_pending) {
368 blk_finish_plug(&plug);
369 blk_start_plug(&plug);
373 btrfsic_submit_bio(cur->bi_rw, cur);
380 * we made progress, there is more work to do and the bdi
381 * is now congested. Back off and let other work structs
384 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
385 fs_info->fs_devices->open_devices > 1) {
386 struct io_context *ioc;
388 ioc = current->io_context;
391 * the main goal here is that we don't want to
392 * block if we're going to be able to submit
393 * more requests without blocking.
395 * This code does two great things, it pokes into
396 * the elevator code from a filesystem _and_
397 * it makes assumptions about how batching works.
399 if (ioc && ioc->nr_batch_requests > 0 &&
400 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
402 ioc->last_waited == last_waited)) {
404 * we want to go through our batch of
405 * requests and stop. So, we copy out
406 * the ioc->last_waited time and test
407 * against it before looping
409 last_waited = ioc->last_waited;
413 spin_lock(&device->io_lock);
414 requeue_list(pending_bios, pending, tail);
415 device->running_pending = 1;
417 spin_unlock(&device->io_lock);
418 btrfs_queue_work(fs_info->submit_workers,
422 /* unplug every 64 requests just for good measure */
423 if (batch_run % 64 == 0) {
424 blk_finish_plug(&plug);
425 blk_start_plug(&plug);
434 spin_lock(&device->io_lock);
435 if (device->pending_bios.head || device->pending_sync_bios.head)
437 spin_unlock(&device->io_lock);
440 blk_finish_plug(&plug);
443 static void pending_bios_fn(struct btrfs_work *work)
445 struct btrfs_device *device;
447 device = container_of(work, struct btrfs_device, work);
448 run_scheduled_bios(device);
452 void btrfs_free_stale_device(struct btrfs_device *cur_dev)
454 struct btrfs_fs_devices *fs_devs;
455 struct btrfs_device *dev;
460 list_for_each_entry(fs_devs, &fs_uuids, list) {
465 if (fs_devs->seeding)
468 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
476 * Todo: This won't be enough. What if the same device
477 * comes back (with new uuid and) with its mapper path?
478 * But for now, this does help as mostly an admin will
479 * either use mapper or non mapper path throughout.
482 del = strcmp(rcu_str_deref(dev->name),
483 rcu_str_deref(cur_dev->name));
490 /* delete the stale device */
491 if (fs_devs->num_devices == 1) {
492 btrfs_sysfs_remove_fsid(fs_devs);
493 list_del(&fs_devs->list);
494 free_fs_devices(fs_devs);
496 fs_devs->num_devices--;
497 list_del(&dev->dev_list);
498 rcu_string_free(dev->name);
507 * Add new device to list of registered devices
510 * 1 - first time device is seen
511 * 0 - device already known
514 static noinline int device_list_add(const char *path,
515 struct btrfs_super_block *disk_super,
516 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
518 struct btrfs_device *device;
519 struct btrfs_fs_devices *fs_devices;
520 struct rcu_string *name;
522 u64 found_transid = btrfs_super_generation(disk_super);
524 fs_devices = find_fsid(disk_super->fsid);
526 fs_devices = alloc_fs_devices(disk_super->fsid);
527 if (IS_ERR(fs_devices))
528 return PTR_ERR(fs_devices);
530 list_add(&fs_devices->list, &fs_uuids);
534 device = __find_device(&fs_devices->devices, devid,
535 disk_super->dev_item.uuid);
539 if (fs_devices->opened)
542 device = btrfs_alloc_device(NULL, &devid,
543 disk_super->dev_item.uuid);
544 if (IS_ERR(device)) {
545 /* we can safely leave the fs_devices entry around */
546 return PTR_ERR(device);
549 name = rcu_string_strdup(path, GFP_NOFS);
554 rcu_assign_pointer(device->name, name);
556 mutex_lock(&fs_devices->device_list_mutex);
557 list_add_rcu(&device->dev_list, &fs_devices->devices);
558 fs_devices->num_devices++;
559 mutex_unlock(&fs_devices->device_list_mutex);
562 device->fs_devices = fs_devices;
563 } else if (!device->name || strcmp(device->name->str, path)) {
565 * When FS is already mounted.
566 * 1. If you are here and if the device->name is NULL that
567 * means this device was missing at time of FS mount.
568 * 2. If you are here and if the device->name is different
569 * from 'path' that means either
570 * a. The same device disappeared and reappeared with
572 * b. The missing-disk-which-was-replaced, has
575 * We must allow 1 and 2a above. But 2b would be a spurious
578 * Further in case of 1 and 2a above, the disk at 'path'
579 * would have missed some transaction when it was away and
580 * in case of 2a the stale bdev has to be updated as well.
581 * 2b must not be allowed at all time.
585 * For now, we do allow update to btrfs_fs_device through the
586 * btrfs dev scan cli after FS has been mounted. We're still
587 * tracking a problem where systems fail mount by subvolume id
588 * when we reject replacement on a mounted FS.
590 if (!fs_devices->opened && found_transid < device->generation) {
592 * That is if the FS is _not_ mounted and if you
593 * are here, that means there is more than one
594 * disk with same uuid and devid.We keep the one
595 * with larger generation number or the last-in if
596 * generation are equal.
601 name = rcu_string_strdup(path, GFP_NOFS);
604 rcu_string_free(device->name);
605 rcu_assign_pointer(device->name, name);
606 if (device->missing) {
607 fs_devices->missing_devices--;
613 * Unmount does not free the btrfs_device struct but would zero
614 * generation along with most of the other members. So just update
615 * it back. We need it to pick the disk with largest generation
618 if (!fs_devices->opened)
619 device->generation = found_transid;
622 * if there is new btrfs on an already registered device,
623 * then remove the stale device entry.
625 btrfs_free_stale_device(device);
627 *fs_devices_ret = fs_devices;
632 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
634 struct btrfs_fs_devices *fs_devices;
635 struct btrfs_device *device;
636 struct btrfs_device *orig_dev;
638 fs_devices = alloc_fs_devices(orig->fsid);
639 if (IS_ERR(fs_devices))
642 mutex_lock(&orig->device_list_mutex);
643 fs_devices->total_devices = orig->total_devices;
645 /* We have held the volume lock, it is safe to get the devices. */
646 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
647 struct rcu_string *name;
649 device = btrfs_alloc_device(NULL, &orig_dev->devid,
655 * This is ok to do without rcu read locked because we hold the
656 * uuid mutex so nothing we touch in here is going to disappear.
658 if (orig_dev->name) {
659 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
664 rcu_assign_pointer(device->name, name);
667 list_add(&device->dev_list, &fs_devices->devices);
668 device->fs_devices = fs_devices;
669 fs_devices->num_devices++;
671 mutex_unlock(&orig->device_list_mutex);
674 mutex_unlock(&orig->device_list_mutex);
675 free_fs_devices(fs_devices);
676 return ERR_PTR(-ENOMEM);
679 void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
681 struct btrfs_device *device, *next;
682 struct btrfs_device *latest_dev = NULL;
684 mutex_lock(&uuid_mutex);
686 /* This is the initialized path, it is safe to release the devices. */
687 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
688 if (device->in_fs_metadata) {
689 if (!device->is_tgtdev_for_dev_replace &&
691 device->generation > latest_dev->generation)) {
697 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
699 * In the first step, keep the device which has
700 * the correct fsid and the devid that is used
701 * for the dev_replace procedure.
702 * In the second step, the dev_replace state is
703 * read from the device tree and it is known
704 * whether the procedure is really active or
705 * not, which means whether this device is
706 * used or whether it should be removed.
708 if (step == 0 || device->is_tgtdev_for_dev_replace) {
713 blkdev_put(device->bdev, device->mode);
715 fs_devices->open_devices--;
717 if (device->writeable) {
718 list_del_init(&device->dev_alloc_list);
719 device->writeable = 0;
720 if (!device->is_tgtdev_for_dev_replace)
721 fs_devices->rw_devices--;
723 list_del_init(&device->dev_list);
724 fs_devices->num_devices--;
725 rcu_string_free(device->name);
729 if (fs_devices->seed) {
730 fs_devices = fs_devices->seed;
734 fs_devices->latest_bdev = latest_dev->bdev;
736 mutex_unlock(&uuid_mutex);
739 static void __free_device(struct work_struct *work)
741 struct btrfs_device *device;
743 device = container_of(work, struct btrfs_device, rcu_work);
746 blkdev_put(device->bdev, device->mode);
748 rcu_string_free(device->name);
752 static void free_device(struct rcu_head *head)
754 struct btrfs_device *device;
756 device = container_of(head, struct btrfs_device, rcu);
758 INIT_WORK(&device->rcu_work, __free_device);
759 schedule_work(&device->rcu_work);
762 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
764 struct btrfs_device *device, *tmp;
766 if (--fs_devices->opened > 0)
769 mutex_lock(&fs_devices->device_list_mutex);
770 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
771 struct btrfs_device *new_device;
772 struct rcu_string *name;
775 fs_devices->open_devices--;
777 if (device->writeable &&
778 device->devid != BTRFS_DEV_REPLACE_DEVID) {
779 list_del_init(&device->dev_alloc_list);
780 fs_devices->rw_devices--;
784 fs_devices->missing_devices--;
786 new_device = btrfs_alloc_device(NULL, &device->devid,
788 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
790 /* Safe because we are under uuid_mutex */
792 name = rcu_string_strdup(device->name->str, GFP_NOFS);
793 BUG_ON(!name); /* -ENOMEM */
794 rcu_assign_pointer(new_device->name, name);
797 list_replace_rcu(&device->dev_list, &new_device->dev_list);
798 new_device->fs_devices = device->fs_devices;
800 call_rcu(&device->rcu, free_device);
802 mutex_unlock(&fs_devices->device_list_mutex);
804 WARN_ON(fs_devices->open_devices);
805 WARN_ON(fs_devices->rw_devices);
806 fs_devices->opened = 0;
807 fs_devices->seeding = 0;
812 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
814 struct btrfs_fs_devices *seed_devices = NULL;
817 mutex_lock(&uuid_mutex);
818 ret = __btrfs_close_devices(fs_devices);
819 if (!fs_devices->opened) {
820 seed_devices = fs_devices->seed;
821 fs_devices->seed = NULL;
823 mutex_unlock(&uuid_mutex);
825 while (seed_devices) {
826 fs_devices = seed_devices;
827 seed_devices = fs_devices->seed;
828 __btrfs_close_devices(fs_devices);
829 free_fs_devices(fs_devices);
832 * Wait for rcu kworkers under __btrfs_close_devices
833 * to finish all blkdev_puts so device is really
834 * free when umount is done.
840 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
841 fmode_t flags, void *holder)
843 struct request_queue *q;
844 struct block_device *bdev;
845 struct list_head *head = &fs_devices->devices;
846 struct btrfs_device *device;
847 struct btrfs_device *latest_dev = NULL;
848 struct buffer_head *bh;
849 struct btrfs_super_block *disk_super;
856 list_for_each_entry(device, head, dev_list) {
862 /* Just open everything we can; ignore failures here */
863 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
867 disk_super = (struct btrfs_super_block *)bh->b_data;
868 devid = btrfs_stack_device_id(&disk_super->dev_item);
869 if (devid != device->devid)
872 if (memcmp(device->uuid, disk_super->dev_item.uuid,
876 device->generation = btrfs_super_generation(disk_super);
878 device->generation > latest_dev->generation)
881 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
882 device->writeable = 0;
884 device->writeable = !bdev_read_only(bdev);
888 q = bdev_get_queue(bdev);
889 if (blk_queue_discard(q))
890 device->can_discard = 1;
893 device->in_fs_metadata = 0;
894 device->mode = flags;
896 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
897 fs_devices->rotating = 1;
899 fs_devices->open_devices++;
900 if (device->writeable &&
901 device->devid != BTRFS_DEV_REPLACE_DEVID) {
902 fs_devices->rw_devices++;
903 list_add(&device->dev_alloc_list,
904 &fs_devices->alloc_list);
911 blkdev_put(bdev, flags);
914 if (fs_devices->open_devices == 0) {
918 fs_devices->seeding = seeding;
919 fs_devices->opened = 1;
920 fs_devices->latest_bdev = latest_dev->bdev;
921 fs_devices->total_rw_bytes = 0;
926 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
927 fmode_t flags, void *holder)
931 mutex_lock(&uuid_mutex);
932 if (fs_devices->opened) {
933 fs_devices->opened++;
936 ret = __btrfs_open_devices(fs_devices, flags, holder);
938 mutex_unlock(&uuid_mutex);
943 * Look for a btrfs signature on a device. This may be called out of the mount path
944 * and we are not allowed to call set_blocksize during the scan. The superblock
945 * is read via pagecache
947 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
948 struct btrfs_fs_devices **fs_devices_ret)
950 struct btrfs_super_block *disk_super;
951 struct block_device *bdev;
962 * we would like to check all the supers, but that would make
963 * a btrfs mount succeed after a mkfs from a different FS.
964 * So, we need to add a special mount option to scan for
965 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
967 bytenr = btrfs_sb_offset(0);
969 mutex_lock(&uuid_mutex);
971 bdev = blkdev_get_by_path(path, flags, holder);
978 /* make sure our super fits in the device */
979 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
982 /* make sure our super fits in the page */
983 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
986 /* make sure our super doesn't straddle pages on disk */
987 index = bytenr >> PAGE_CACHE_SHIFT;
988 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
991 /* pull in the page with our super */
992 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
995 if (IS_ERR_OR_NULL(page))
1000 /* align our pointer to the offset of the super block */
1001 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
1003 if (btrfs_super_bytenr(disk_super) != bytenr ||
1004 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
1007 devid = btrfs_stack_device_id(&disk_super->dev_item);
1008 transid = btrfs_super_generation(disk_super);
1009 total_devices = btrfs_super_num_devices(disk_super);
1011 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
1013 if (disk_super->label[0]) {
1014 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1015 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1016 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1018 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1021 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1024 if (!ret && fs_devices_ret)
1025 (*fs_devices_ret)->total_devices = total_devices;
1029 page_cache_release(page);
1032 blkdev_put(bdev, flags);
1034 mutex_unlock(&uuid_mutex);
1038 /* helper to account the used device space in the range */
1039 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1040 u64 end, u64 *length)
1042 struct btrfs_key key;
1043 struct btrfs_root *root = device->dev_root;
1044 struct btrfs_dev_extent *dev_extent;
1045 struct btrfs_path *path;
1049 struct extent_buffer *l;
1053 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
1056 path = btrfs_alloc_path();
1061 key.objectid = device->devid;
1063 key.type = BTRFS_DEV_EXTENT_KEY;
1065 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1069 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1076 slot = path->slots[0];
1077 if (slot >= btrfs_header_nritems(l)) {
1078 ret = btrfs_next_leaf(root, path);
1086 btrfs_item_key_to_cpu(l, &key, slot);
1088 if (key.objectid < device->devid)
1091 if (key.objectid > device->devid)
1094 if (key.type != BTRFS_DEV_EXTENT_KEY)
1097 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1098 extent_end = key.offset + btrfs_dev_extent_length(l,
1100 if (key.offset <= start && extent_end > end) {
1101 *length = end - start + 1;
1103 } else if (key.offset <= start && extent_end > start)
1104 *length += extent_end - start;
1105 else if (key.offset > start && extent_end <= end)
1106 *length += extent_end - key.offset;
1107 else if (key.offset > start && key.offset <= end) {
1108 *length += end - key.offset + 1;
1110 } else if (key.offset > end)
1118 btrfs_free_path(path);
1122 static int contains_pending_extent(struct btrfs_transaction *transaction,
1123 struct btrfs_device *device,
1124 u64 *start, u64 len)
1126 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
1127 struct extent_map *em;
1128 struct list_head *search_list = &fs_info->pinned_chunks;
1130 u64 physical_start = *start;
1133 search_list = &transaction->pending_chunks;
1135 list_for_each_entry(em, search_list, list) {
1136 struct map_lookup *map;
1139 map = (struct map_lookup *)em->bdev;
1140 for (i = 0; i < map->num_stripes; i++) {
1143 if (map->stripes[i].dev != device)
1145 if (map->stripes[i].physical >= physical_start + len ||
1146 map->stripes[i].physical + em->orig_block_len <=
1150 * Make sure that while processing the pinned list we do
1151 * not override our *start with a lower value, because
1152 * we can have pinned chunks that fall within this
1153 * device hole and that have lower physical addresses
1154 * than the pending chunks we processed before. If we
1155 * do not take this special care we can end up getting
1156 * 2 pending chunks that start at the same physical
1157 * device offsets because the end offset of a pinned
1158 * chunk can be equal to the start offset of some
1161 end = map->stripes[i].physical + em->orig_block_len;
1168 if (search_list != &fs_info->pinned_chunks) {
1169 search_list = &fs_info->pinned_chunks;
1178 * find_free_dev_extent_start - find free space in the specified device
1179 * @device: the device which we search the free space in
1180 * @num_bytes: the size of the free space that we need
1181 * @search_start: the position from which to begin the search
1182 * @start: store the start of the free space.
1183 * @len: the size of the free space. that we find, or the size
1184 * of the max free space if we don't find suitable free space
1186 * this uses a pretty simple search, the expectation is that it is
1187 * called very infrequently and that a given device has a small number
1190 * @start is used to store the start of the free space if we find. But if we
1191 * don't find suitable free space, it will be used to store the start position
1192 * of the max free space.
1194 * @len is used to store the size of the free space that we find.
1195 * But if we don't find suitable free space, it is used to store the size of
1196 * the max free space.
1198 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1199 struct btrfs_device *device, u64 num_bytes,
1200 u64 search_start, u64 *start, u64 *len)
1202 struct btrfs_key key;
1203 struct btrfs_root *root = device->dev_root;
1204 struct btrfs_dev_extent *dev_extent;
1205 struct btrfs_path *path;
1210 u64 search_end = device->total_bytes;
1213 struct extent_buffer *l;
1215 path = btrfs_alloc_path();
1219 max_hole_start = search_start;
1223 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1229 path->search_commit_root = 1;
1230 path->skip_locking = 1;
1232 key.objectid = device->devid;
1233 key.offset = search_start;
1234 key.type = BTRFS_DEV_EXTENT_KEY;
1236 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1240 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1247 slot = path->slots[0];
1248 if (slot >= btrfs_header_nritems(l)) {
1249 ret = btrfs_next_leaf(root, path);
1257 btrfs_item_key_to_cpu(l, &key, slot);
1259 if (key.objectid < device->devid)
1262 if (key.objectid > device->devid)
1265 if (key.type != BTRFS_DEV_EXTENT_KEY)
1268 if (key.offset > search_start) {
1269 hole_size = key.offset - search_start;
1272 * Have to check before we set max_hole_start, otherwise
1273 * we could end up sending back this offset anyway.
1275 if (contains_pending_extent(transaction, device,
1278 if (key.offset >= search_start) {
1279 hole_size = key.offset - search_start;
1286 if (hole_size > max_hole_size) {
1287 max_hole_start = search_start;
1288 max_hole_size = hole_size;
1292 * If this free space is greater than which we need,
1293 * it must be the max free space that we have found
1294 * until now, so max_hole_start must point to the start
1295 * of this free space and the length of this free space
1296 * is stored in max_hole_size. Thus, we return
1297 * max_hole_start and max_hole_size and go back to the
1300 if (hole_size >= num_bytes) {
1306 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1307 extent_end = key.offset + btrfs_dev_extent_length(l,
1309 if (extent_end > search_start)
1310 search_start = extent_end;
1317 * At this point, search_start should be the end of
1318 * allocated dev extents, and when shrinking the device,
1319 * search_end may be smaller than search_start.
1321 if (search_end > search_start) {
1322 hole_size = search_end - search_start;
1324 if (contains_pending_extent(transaction, device, &search_start,
1326 btrfs_release_path(path);
1330 if (hole_size > max_hole_size) {
1331 max_hole_start = search_start;
1332 max_hole_size = hole_size;
1337 if (max_hole_size < num_bytes)
1343 btrfs_free_path(path);
1344 *start = max_hole_start;
1346 *len = max_hole_size;
1350 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1351 struct btrfs_device *device, u64 num_bytes,
1352 u64 *start, u64 *len)
1354 struct btrfs_root *root = device->dev_root;
1357 /* FIXME use last free of some kind */
1360 * we don't want to overwrite the superblock on the drive,
1361 * so we make sure to start at an offset of at least 1MB
1363 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1364 return find_free_dev_extent_start(trans->transaction, device,
1365 num_bytes, search_start, start, len);
1368 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1369 struct btrfs_device *device,
1370 u64 start, u64 *dev_extent_len)
1373 struct btrfs_path *path;
1374 struct btrfs_root *root = device->dev_root;
1375 struct btrfs_key key;
1376 struct btrfs_key found_key;
1377 struct extent_buffer *leaf = NULL;
1378 struct btrfs_dev_extent *extent = NULL;
1380 path = btrfs_alloc_path();
1384 key.objectid = device->devid;
1386 key.type = BTRFS_DEV_EXTENT_KEY;
1388 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1390 ret = btrfs_previous_item(root, path, key.objectid,
1391 BTRFS_DEV_EXTENT_KEY);
1394 leaf = path->nodes[0];
1395 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1396 extent = btrfs_item_ptr(leaf, path->slots[0],
1397 struct btrfs_dev_extent);
1398 BUG_ON(found_key.offset > start || found_key.offset +
1399 btrfs_dev_extent_length(leaf, extent) < start);
1401 btrfs_release_path(path);
1403 } else if (ret == 0) {
1404 leaf = path->nodes[0];
1405 extent = btrfs_item_ptr(leaf, path->slots[0],
1406 struct btrfs_dev_extent);
1408 btrfs_error(root->fs_info, ret, "Slot search failed");
1412 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1414 ret = btrfs_del_item(trans, root, path);
1416 btrfs_error(root->fs_info, ret,
1417 "Failed to remove dev extent item");
1419 trans->transaction->have_free_bgs = 1;
1422 btrfs_free_path(path);
1426 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1427 struct btrfs_device *device,
1428 u64 chunk_tree, u64 chunk_objectid,
1429 u64 chunk_offset, u64 start, u64 num_bytes)
1432 struct btrfs_path *path;
1433 struct btrfs_root *root = device->dev_root;
1434 struct btrfs_dev_extent *extent;
1435 struct extent_buffer *leaf;
1436 struct btrfs_key key;
1438 WARN_ON(!device->in_fs_metadata);
1439 WARN_ON(device->is_tgtdev_for_dev_replace);
1440 path = btrfs_alloc_path();
1444 key.objectid = device->devid;
1446 key.type = BTRFS_DEV_EXTENT_KEY;
1447 ret = btrfs_insert_empty_item(trans, root, path, &key,
1452 leaf = path->nodes[0];
1453 extent = btrfs_item_ptr(leaf, path->slots[0],
1454 struct btrfs_dev_extent);
1455 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1456 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1457 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1459 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1460 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
1462 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1463 btrfs_mark_buffer_dirty(leaf);
1465 btrfs_free_path(path);
1469 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1471 struct extent_map_tree *em_tree;
1472 struct extent_map *em;
1476 em_tree = &fs_info->mapping_tree.map_tree;
1477 read_lock(&em_tree->lock);
1478 n = rb_last(&em_tree->map);
1480 em = rb_entry(n, struct extent_map, rb_node);
1481 ret = em->start + em->len;
1483 read_unlock(&em_tree->lock);
1488 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1492 struct btrfs_key key;
1493 struct btrfs_key found_key;
1494 struct btrfs_path *path;
1496 path = btrfs_alloc_path();
1500 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1501 key.type = BTRFS_DEV_ITEM_KEY;
1502 key.offset = (u64)-1;
1504 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1508 BUG_ON(ret == 0); /* Corruption */
1510 ret = btrfs_previous_item(fs_info->chunk_root, path,
1511 BTRFS_DEV_ITEMS_OBJECTID,
1512 BTRFS_DEV_ITEM_KEY);
1516 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1518 *devid_ret = found_key.offset + 1;
1522 btrfs_free_path(path);
1527 * the device information is stored in the chunk root
1528 * the btrfs_device struct should be fully filled in
1530 static int btrfs_add_device(struct btrfs_trans_handle *trans,
1531 struct btrfs_root *root,
1532 struct btrfs_device *device)
1535 struct btrfs_path *path;
1536 struct btrfs_dev_item *dev_item;
1537 struct extent_buffer *leaf;
1538 struct btrfs_key key;
1541 root = root->fs_info->chunk_root;
1543 path = btrfs_alloc_path();
1547 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1548 key.type = BTRFS_DEV_ITEM_KEY;
1549 key.offset = device->devid;
1551 ret = btrfs_insert_empty_item(trans, root, path, &key,
1556 leaf = path->nodes[0];
1557 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1559 btrfs_set_device_id(leaf, dev_item, device->devid);
1560 btrfs_set_device_generation(leaf, dev_item, 0);
1561 btrfs_set_device_type(leaf, dev_item, device->type);
1562 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1563 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1564 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1565 btrfs_set_device_total_bytes(leaf, dev_item,
1566 btrfs_device_get_disk_total_bytes(device));
1567 btrfs_set_device_bytes_used(leaf, dev_item,
1568 btrfs_device_get_bytes_used(device));
1569 btrfs_set_device_group(leaf, dev_item, 0);
1570 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1571 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1572 btrfs_set_device_start_offset(leaf, dev_item, 0);
1574 ptr = btrfs_device_uuid(dev_item);
1575 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1576 ptr = btrfs_device_fsid(dev_item);
1577 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1578 btrfs_mark_buffer_dirty(leaf);
1582 btrfs_free_path(path);
1587 * Function to update ctime/mtime for a given device path.
1588 * Mainly used for ctime/mtime based probe like libblkid.
1590 static void update_dev_time(char *path_name)
1594 filp = filp_open(path_name, O_RDWR, 0);
1597 file_update_time(filp);
1598 filp_close(filp, NULL);
1602 static int btrfs_rm_dev_item(struct btrfs_root *root,
1603 struct btrfs_device *device)
1606 struct btrfs_path *path;
1607 struct btrfs_key key;
1608 struct btrfs_trans_handle *trans;
1610 root = root->fs_info->chunk_root;
1612 path = btrfs_alloc_path();
1616 trans = btrfs_start_transaction(root, 0);
1617 if (IS_ERR(trans)) {
1618 btrfs_free_path(path);
1619 return PTR_ERR(trans);
1621 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1622 key.type = BTRFS_DEV_ITEM_KEY;
1623 key.offset = device->devid;
1625 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1634 ret = btrfs_del_item(trans, root, path);
1638 btrfs_free_path(path);
1639 btrfs_commit_transaction(trans, root);
1643 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1645 struct btrfs_device *device;
1646 struct btrfs_device *next_device;
1647 struct block_device *bdev;
1648 struct buffer_head *bh = NULL;
1649 struct btrfs_super_block *disk_super;
1650 struct btrfs_fs_devices *cur_devices;
1657 bool clear_super = false;
1659 mutex_lock(&uuid_mutex);
1662 seq = read_seqbegin(&root->fs_info->profiles_lock);
1664 all_avail = root->fs_info->avail_data_alloc_bits |
1665 root->fs_info->avail_system_alloc_bits |
1666 root->fs_info->avail_metadata_alloc_bits;
1667 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1669 num_devices = root->fs_info->fs_devices->num_devices;
1670 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1671 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1672 WARN_ON(num_devices < 1);
1675 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1677 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1678 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
1682 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1683 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
1687 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1688 root->fs_info->fs_devices->rw_devices <= 2) {
1689 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
1692 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1693 root->fs_info->fs_devices->rw_devices <= 3) {
1694 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
1698 if (strcmp(device_path, "missing") == 0) {
1699 struct list_head *devices;
1700 struct btrfs_device *tmp;
1703 devices = &root->fs_info->fs_devices->devices;
1705 * It is safe to read the devices since the volume_mutex
1708 list_for_each_entry(tmp, devices, dev_list) {
1709 if (tmp->in_fs_metadata &&
1710 !tmp->is_tgtdev_for_dev_replace &&
1720 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1724 ret = btrfs_get_bdev_and_sb(device_path,
1725 FMODE_WRITE | FMODE_EXCL,
1726 root->fs_info->bdev_holder, 0,
1730 disk_super = (struct btrfs_super_block *)bh->b_data;
1731 devid = btrfs_stack_device_id(&disk_super->dev_item);
1732 dev_uuid = disk_super->dev_item.uuid;
1733 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1741 if (device->is_tgtdev_for_dev_replace) {
1742 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1746 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1747 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1751 if (device->writeable) {
1753 list_del_init(&device->dev_alloc_list);
1754 device->fs_devices->rw_devices--;
1755 unlock_chunks(root);
1759 mutex_unlock(&uuid_mutex);
1760 ret = btrfs_shrink_device(device, 0);
1761 mutex_lock(&uuid_mutex);
1766 * TODO: the superblock still includes this device in its num_devices
1767 * counter although write_all_supers() is not locked out. This
1768 * could give a filesystem state which requires a degraded mount.
1770 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1774 device->in_fs_metadata = 0;
1775 btrfs_scrub_cancel_dev(root->fs_info, device);
1778 * the device list mutex makes sure that we don't change
1779 * the device list while someone else is writing out all
1780 * the device supers. Whoever is writing all supers, should
1781 * lock the device list mutex before getting the number of
1782 * devices in the super block (super_copy). Conversely,
1783 * whoever updates the number of devices in the super block
1784 * (super_copy) should hold the device list mutex.
1787 cur_devices = device->fs_devices;
1788 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1789 list_del_rcu(&device->dev_list);
1791 device->fs_devices->num_devices--;
1792 device->fs_devices->total_devices--;
1794 if (device->missing)
1795 device->fs_devices->missing_devices--;
1797 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1798 struct btrfs_device, dev_list);
1799 if (device->bdev == root->fs_info->sb->s_bdev)
1800 root->fs_info->sb->s_bdev = next_device->bdev;
1801 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1802 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1805 device->fs_devices->open_devices--;
1806 /* remove sysfs entry */
1807 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
1810 call_rcu(&device->rcu, free_device);
1812 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1813 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1814 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1816 if (cur_devices->open_devices == 0) {
1817 struct btrfs_fs_devices *fs_devices;
1818 fs_devices = root->fs_info->fs_devices;
1819 while (fs_devices) {
1820 if (fs_devices->seed == cur_devices) {
1821 fs_devices->seed = cur_devices->seed;
1824 fs_devices = fs_devices->seed;
1826 cur_devices->seed = NULL;
1827 __btrfs_close_devices(cur_devices);
1828 free_fs_devices(cur_devices);
1831 root->fs_info->num_tolerated_disk_barrier_failures =
1832 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1835 * at this point, the device is zero sized. We want to
1836 * remove it from the devices list and zero out the old super
1838 if (clear_super && disk_super) {
1842 /* make sure this device isn't detected as part of
1845 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1846 set_buffer_dirty(bh);
1847 sync_dirty_buffer(bh);
1849 /* clear the mirror copies of super block on the disk
1850 * being removed, 0th copy is been taken care above and
1851 * the below would take of the rest
1853 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1854 bytenr = btrfs_sb_offset(i);
1855 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1856 i_size_read(bdev->bd_inode))
1860 bh = __bread(bdev, bytenr / 4096,
1861 BTRFS_SUPER_INFO_SIZE);
1865 disk_super = (struct btrfs_super_block *)bh->b_data;
1867 if (btrfs_super_bytenr(disk_super) != bytenr ||
1868 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1871 memset(&disk_super->magic, 0,
1872 sizeof(disk_super->magic));
1873 set_buffer_dirty(bh);
1874 sync_dirty_buffer(bh);
1881 /* Notify udev that device has changed */
1882 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1884 /* Update ctime/mtime for device path for libblkid */
1885 update_dev_time(device_path);
1891 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1893 mutex_unlock(&uuid_mutex);
1896 if (device->writeable) {
1898 list_add(&device->dev_alloc_list,
1899 &root->fs_info->fs_devices->alloc_list);
1900 device->fs_devices->rw_devices++;
1901 unlock_chunks(root);
1906 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1907 struct btrfs_device *srcdev)
1909 struct btrfs_fs_devices *fs_devices;
1911 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1914 * in case of fs with no seed, srcdev->fs_devices will point
1915 * to fs_devices of fs_info. However when the dev being replaced is
1916 * a seed dev it will point to the seed's local fs_devices. In short
1917 * srcdev will have its correct fs_devices in both the cases.
1919 fs_devices = srcdev->fs_devices;
1921 list_del_rcu(&srcdev->dev_list);
1922 list_del_rcu(&srcdev->dev_alloc_list);
1923 fs_devices->num_devices--;
1924 if (srcdev->missing)
1925 fs_devices->missing_devices--;
1927 if (srcdev->writeable) {
1928 fs_devices->rw_devices--;
1929 /* zero out the old super if it is writable */
1930 btrfs_scratch_superblock(srcdev);
1934 fs_devices->open_devices--;
1937 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1938 struct btrfs_device *srcdev)
1940 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
1942 call_rcu(&srcdev->rcu, free_device);
1945 * unless fs_devices is seed fs, num_devices shouldn't go
1948 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1950 /* if this is no devs we rather delete the fs_devices */
1951 if (!fs_devices->num_devices) {
1952 struct btrfs_fs_devices *tmp_fs_devices;
1954 tmp_fs_devices = fs_info->fs_devices;
1955 while (tmp_fs_devices) {
1956 if (tmp_fs_devices->seed == fs_devices) {
1957 tmp_fs_devices->seed = fs_devices->seed;
1960 tmp_fs_devices = tmp_fs_devices->seed;
1962 fs_devices->seed = NULL;
1963 __btrfs_close_devices(fs_devices);
1964 free_fs_devices(fs_devices);
1968 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1969 struct btrfs_device *tgtdev)
1971 struct btrfs_device *next_device;
1973 mutex_lock(&uuid_mutex);
1975 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1977 btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev);
1980 btrfs_scratch_superblock(tgtdev);
1981 fs_info->fs_devices->open_devices--;
1983 fs_info->fs_devices->num_devices--;
1985 next_device = list_entry(fs_info->fs_devices->devices.next,
1986 struct btrfs_device, dev_list);
1987 if (tgtdev->bdev == fs_info->sb->s_bdev)
1988 fs_info->sb->s_bdev = next_device->bdev;
1989 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1990 fs_info->fs_devices->latest_bdev = next_device->bdev;
1991 list_del_rcu(&tgtdev->dev_list);
1993 call_rcu(&tgtdev->rcu, free_device);
1995 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1996 mutex_unlock(&uuid_mutex);
1999 static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2000 struct btrfs_device **device)
2003 struct btrfs_super_block *disk_super;
2006 struct block_device *bdev;
2007 struct buffer_head *bh;
2010 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2011 root->fs_info->bdev_holder, 0, &bdev, &bh);
2014 disk_super = (struct btrfs_super_block *)bh->b_data;
2015 devid = btrfs_stack_device_id(&disk_super->dev_item);
2016 dev_uuid = disk_super->dev_item.uuid;
2017 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2022 blkdev_put(bdev, FMODE_READ);
2026 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2028 struct btrfs_device **device)
2031 if (strcmp(device_path, "missing") == 0) {
2032 struct list_head *devices;
2033 struct btrfs_device *tmp;
2035 devices = &root->fs_info->fs_devices->devices;
2037 * It is safe to read the devices since the volume_mutex
2038 * is held by the caller.
2040 list_for_each_entry(tmp, devices, dev_list) {
2041 if (tmp->in_fs_metadata && !tmp->bdev) {
2048 btrfs_err(root->fs_info, "no missing device found");
2054 return btrfs_find_device_by_path(root, device_path, device);
2059 * does all the dirty work required for changing file system's UUID.
2061 static int btrfs_prepare_sprout(struct btrfs_root *root)
2063 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2064 struct btrfs_fs_devices *old_devices;
2065 struct btrfs_fs_devices *seed_devices;
2066 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
2067 struct btrfs_device *device;
2070 BUG_ON(!mutex_is_locked(&uuid_mutex));
2071 if (!fs_devices->seeding)
2074 seed_devices = __alloc_fs_devices();
2075 if (IS_ERR(seed_devices))
2076 return PTR_ERR(seed_devices);
2078 old_devices = clone_fs_devices(fs_devices);
2079 if (IS_ERR(old_devices)) {
2080 kfree(seed_devices);
2081 return PTR_ERR(old_devices);
2084 list_add(&old_devices->list, &fs_uuids);
2086 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2087 seed_devices->opened = 1;
2088 INIT_LIST_HEAD(&seed_devices->devices);
2089 INIT_LIST_HEAD(&seed_devices->alloc_list);
2090 mutex_init(&seed_devices->device_list_mutex);
2092 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2093 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2095 list_for_each_entry(device, &seed_devices->devices, dev_list)
2096 device->fs_devices = seed_devices;
2099 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2100 unlock_chunks(root);
2102 fs_devices->seeding = 0;
2103 fs_devices->num_devices = 0;
2104 fs_devices->open_devices = 0;
2105 fs_devices->missing_devices = 0;
2106 fs_devices->rotating = 0;
2107 fs_devices->seed = seed_devices;
2109 generate_random_uuid(fs_devices->fsid);
2110 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2111 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2112 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2114 super_flags = btrfs_super_flags(disk_super) &
2115 ~BTRFS_SUPER_FLAG_SEEDING;
2116 btrfs_set_super_flags(disk_super, super_flags);
2122 * strore the expected generation for seed devices in device items.
2124 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2125 struct btrfs_root *root)
2127 struct btrfs_path *path;
2128 struct extent_buffer *leaf;
2129 struct btrfs_dev_item *dev_item;
2130 struct btrfs_device *device;
2131 struct btrfs_key key;
2132 u8 fs_uuid[BTRFS_UUID_SIZE];
2133 u8 dev_uuid[BTRFS_UUID_SIZE];
2137 path = btrfs_alloc_path();
2141 root = root->fs_info->chunk_root;
2142 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2144 key.type = BTRFS_DEV_ITEM_KEY;
2147 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2151 leaf = path->nodes[0];
2153 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2154 ret = btrfs_next_leaf(root, path);
2159 leaf = path->nodes[0];
2160 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2161 btrfs_release_path(path);
2165 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2166 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2167 key.type != BTRFS_DEV_ITEM_KEY)
2170 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2171 struct btrfs_dev_item);
2172 devid = btrfs_device_id(leaf, dev_item);
2173 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2175 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2177 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2179 BUG_ON(!device); /* Logic error */
2181 if (device->fs_devices->seeding) {
2182 btrfs_set_device_generation(leaf, dev_item,
2183 device->generation);
2184 btrfs_mark_buffer_dirty(leaf);
2192 btrfs_free_path(path);
2196 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2198 struct request_queue *q;
2199 struct btrfs_trans_handle *trans;
2200 struct btrfs_device *device;
2201 struct block_device *bdev;
2202 struct list_head *devices;
2203 struct super_block *sb = root->fs_info->sb;
2204 struct rcu_string *name;
2206 int seeding_dev = 0;
2209 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
2212 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2213 root->fs_info->bdev_holder);
2215 return PTR_ERR(bdev);
2217 if (root->fs_info->fs_devices->seeding) {
2219 down_write(&sb->s_umount);
2220 mutex_lock(&uuid_mutex);
2223 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2225 devices = &root->fs_info->fs_devices->devices;
2227 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2228 list_for_each_entry(device, devices, dev_list) {
2229 if (device->bdev == bdev) {
2232 &root->fs_info->fs_devices->device_list_mutex);
2236 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2238 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2239 if (IS_ERR(device)) {
2240 /* we can safely leave the fs_devices entry around */
2241 ret = PTR_ERR(device);
2245 name = rcu_string_strdup(device_path, GFP_NOFS);
2251 rcu_assign_pointer(device->name, name);
2253 trans = btrfs_start_transaction(root, 0);
2254 if (IS_ERR(trans)) {
2255 rcu_string_free(device->name);
2257 ret = PTR_ERR(trans);
2261 q = bdev_get_queue(bdev);
2262 if (blk_queue_discard(q))
2263 device->can_discard = 1;
2264 device->writeable = 1;
2265 device->generation = trans->transid;
2266 device->io_width = root->sectorsize;
2267 device->io_align = root->sectorsize;
2268 device->sector_size = root->sectorsize;
2269 device->total_bytes = i_size_read(bdev->bd_inode);
2270 device->disk_total_bytes = device->total_bytes;
2271 device->commit_total_bytes = device->total_bytes;
2272 device->dev_root = root->fs_info->dev_root;
2273 device->bdev = bdev;
2274 device->in_fs_metadata = 1;
2275 device->is_tgtdev_for_dev_replace = 0;
2276 device->mode = FMODE_EXCL;
2277 device->dev_stats_valid = 1;
2278 set_blocksize(device->bdev, 4096);
2281 sb->s_flags &= ~MS_RDONLY;
2282 ret = btrfs_prepare_sprout(root);
2283 BUG_ON(ret); /* -ENOMEM */
2286 device->fs_devices = root->fs_info->fs_devices;
2288 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2290 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2291 list_add(&device->dev_alloc_list,
2292 &root->fs_info->fs_devices->alloc_list);
2293 root->fs_info->fs_devices->num_devices++;
2294 root->fs_info->fs_devices->open_devices++;
2295 root->fs_info->fs_devices->rw_devices++;
2296 root->fs_info->fs_devices->total_devices++;
2297 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2299 spin_lock(&root->fs_info->free_chunk_lock);
2300 root->fs_info->free_chunk_space += device->total_bytes;
2301 spin_unlock(&root->fs_info->free_chunk_lock);
2303 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2304 root->fs_info->fs_devices->rotating = 1;
2306 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
2307 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2308 tmp + device->total_bytes);
2310 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
2311 btrfs_set_super_num_devices(root->fs_info->super_copy,
2314 /* add sysfs device entry */
2315 btrfs_kobj_add_device(root->fs_info->fs_devices, device);
2318 * we've got more storage, clear any full flags on the space
2321 btrfs_clear_space_info_full(root->fs_info);
2323 unlock_chunks(root);
2324 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2328 ret = init_first_rw_device(trans, root, device);
2329 unlock_chunks(root);
2331 btrfs_abort_transaction(trans, root, ret);
2336 ret = btrfs_add_device(trans, root, device);
2338 btrfs_abort_transaction(trans, root, ret);
2343 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2345 ret = btrfs_finish_sprout(trans, root);
2347 btrfs_abort_transaction(trans, root, ret);
2351 /* Sprouting would change fsid of the mounted root,
2352 * so rename the fsid on the sysfs
2354 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2355 root->fs_info->fsid);
2356 if (kobject_rename(&root->fs_info->fs_devices->super_kobj,
2358 pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n");
2361 root->fs_info->num_tolerated_disk_barrier_failures =
2362 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2363 ret = btrfs_commit_transaction(trans, root);
2366 mutex_unlock(&uuid_mutex);
2367 up_write(&sb->s_umount);
2369 if (ret) /* transaction commit */
2372 ret = btrfs_relocate_sys_chunks(root);
2374 btrfs_error(root->fs_info, ret,
2375 "Failed to relocate sys chunks after "
2376 "device initialization. This can be fixed "
2377 "using the \"btrfs balance\" command.");
2378 trans = btrfs_attach_transaction(root);
2379 if (IS_ERR(trans)) {
2380 if (PTR_ERR(trans) == -ENOENT)
2382 return PTR_ERR(trans);
2384 ret = btrfs_commit_transaction(trans, root);
2387 /* Update ctime/mtime for libblkid */
2388 update_dev_time(device_path);
2392 btrfs_end_transaction(trans, root);
2393 rcu_string_free(device->name);
2394 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
2397 blkdev_put(bdev, FMODE_EXCL);
2399 mutex_unlock(&uuid_mutex);
2400 up_write(&sb->s_umount);
2405 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2406 struct btrfs_device *srcdev,
2407 struct btrfs_device **device_out)
2409 struct request_queue *q;
2410 struct btrfs_device *device;
2411 struct block_device *bdev;
2412 struct btrfs_fs_info *fs_info = root->fs_info;
2413 struct list_head *devices;
2414 struct rcu_string *name;
2415 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2419 if (fs_info->fs_devices->seeding) {
2420 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
2424 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2425 fs_info->bdev_holder);
2427 btrfs_err(fs_info, "target device %s is invalid!", device_path);
2428 return PTR_ERR(bdev);
2431 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2433 devices = &fs_info->fs_devices->devices;
2434 list_for_each_entry(device, devices, dev_list) {
2435 if (device->bdev == bdev) {
2436 btrfs_err(fs_info, "target device is in the filesystem!");
2443 if (i_size_read(bdev->bd_inode) <
2444 btrfs_device_get_total_bytes(srcdev)) {
2445 btrfs_err(fs_info, "target device is smaller than source device!");
2451 device = btrfs_alloc_device(NULL, &devid, NULL);
2452 if (IS_ERR(device)) {
2453 ret = PTR_ERR(device);
2457 name = rcu_string_strdup(device_path, GFP_NOFS);
2463 rcu_assign_pointer(device->name, name);
2465 q = bdev_get_queue(bdev);
2466 if (blk_queue_discard(q))
2467 device->can_discard = 1;
2468 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2469 device->writeable = 1;
2470 device->generation = 0;
2471 device->io_width = root->sectorsize;
2472 device->io_align = root->sectorsize;
2473 device->sector_size = root->sectorsize;
2474 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2475 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2476 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
2477 ASSERT(list_empty(&srcdev->resized_list));
2478 device->commit_total_bytes = srcdev->commit_total_bytes;
2479 device->commit_bytes_used = device->bytes_used;
2480 device->dev_root = fs_info->dev_root;
2481 device->bdev = bdev;
2482 device->in_fs_metadata = 1;
2483 device->is_tgtdev_for_dev_replace = 1;
2484 device->mode = FMODE_EXCL;
2485 device->dev_stats_valid = 1;
2486 set_blocksize(device->bdev, 4096);
2487 device->fs_devices = fs_info->fs_devices;
2488 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2489 fs_info->fs_devices->num_devices++;
2490 fs_info->fs_devices->open_devices++;
2491 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2493 *device_out = device;
2497 blkdev_put(bdev, FMODE_EXCL);
2501 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2502 struct btrfs_device *tgtdev)
2504 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2505 tgtdev->io_width = fs_info->dev_root->sectorsize;
2506 tgtdev->io_align = fs_info->dev_root->sectorsize;
2507 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2508 tgtdev->dev_root = fs_info->dev_root;
2509 tgtdev->in_fs_metadata = 1;
2512 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2513 struct btrfs_device *device)
2516 struct btrfs_path *path;
2517 struct btrfs_root *root;
2518 struct btrfs_dev_item *dev_item;
2519 struct extent_buffer *leaf;
2520 struct btrfs_key key;
2522 root = device->dev_root->fs_info->chunk_root;
2524 path = btrfs_alloc_path();
2528 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2529 key.type = BTRFS_DEV_ITEM_KEY;
2530 key.offset = device->devid;
2532 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2541 leaf = path->nodes[0];
2542 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2544 btrfs_set_device_id(leaf, dev_item, device->devid);
2545 btrfs_set_device_type(leaf, dev_item, device->type);
2546 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2547 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2548 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2549 btrfs_set_device_total_bytes(leaf, dev_item,
2550 btrfs_device_get_disk_total_bytes(device));
2551 btrfs_set_device_bytes_used(leaf, dev_item,
2552 btrfs_device_get_bytes_used(device));
2553 btrfs_mark_buffer_dirty(leaf);
2556 btrfs_free_path(path);
2560 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2561 struct btrfs_device *device, u64 new_size)
2563 struct btrfs_super_block *super_copy =
2564 device->dev_root->fs_info->super_copy;
2565 struct btrfs_fs_devices *fs_devices;
2569 if (!device->writeable)
2572 lock_chunks(device->dev_root);
2573 old_total = btrfs_super_total_bytes(super_copy);
2574 diff = new_size - device->total_bytes;
2576 if (new_size <= device->total_bytes ||
2577 device->is_tgtdev_for_dev_replace) {
2578 unlock_chunks(device->dev_root);
2582 fs_devices = device->dev_root->fs_info->fs_devices;
2584 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2585 device->fs_devices->total_rw_bytes += diff;
2587 btrfs_device_set_total_bytes(device, new_size);
2588 btrfs_device_set_disk_total_bytes(device, new_size);
2589 btrfs_clear_space_info_full(device->dev_root->fs_info);
2590 if (list_empty(&device->resized_list))
2591 list_add_tail(&device->resized_list,
2592 &fs_devices->resized_devices);
2593 unlock_chunks(device->dev_root);
2595 return btrfs_update_device(trans, device);
2598 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2599 struct btrfs_root *root, u64 chunk_objectid,
2603 struct btrfs_path *path;
2604 struct btrfs_key key;
2606 root = root->fs_info->chunk_root;
2607 path = btrfs_alloc_path();
2611 key.objectid = chunk_objectid;
2612 key.offset = chunk_offset;
2613 key.type = BTRFS_CHUNK_ITEM_KEY;
2615 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2618 else if (ret > 0) { /* Logic error or corruption */
2619 btrfs_error(root->fs_info, -ENOENT,
2620 "Failed lookup while freeing chunk.");
2625 ret = btrfs_del_item(trans, root, path);
2627 btrfs_error(root->fs_info, ret,
2628 "Failed to delete chunk item.");
2630 btrfs_free_path(path);
2634 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2637 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2638 struct btrfs_disk_key *disk_key;
2639 struct btrfs_chunk *chunk;
2646 struct btrfs_key key;
2649 array_size = btrfs_super_sys_array_size(super_copy);
2651 ptr = super_copy->sys_chunk_array;
2654 while (cur < array_size) {
2655 disk_key = (struct btrfs_disk_key *)ptr;
2656 btrfs_disk_key_to_cpu(&key, disk_key);
2658 len = sizeof(*disk_key);
2660 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2661 chunk = (struct btrfs_chunk *)(ptr + len);
2662 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2663 len += btrfs_chunk_item_size(num_stripes);
2668 if (key.objectid == chunk_objectid &&
2669 key.offset == chunk_offset) {
2670 memmove(ptr, ptr + len, array_size - (cur + len));
2672 btrfs_set_super_sys_array_size(super_copy, array_size);
2678 unlock_chunks(root);
2682 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2683 struct btrfs_root *root, u64 chunk_offset)
2685 struct extent_map_tree *em_tree;
2686 struct extent_map *em;
2687 struct btrfs_root *extent_root = root->fs_info->extent_root;
2688 struct map_lookup *map;
2689 u64 dev_extent_len = 0;
2690 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2694 root = root->fs_info->chunk_root;
2695 em_tree = &root->fs_info->mapping_tree.map_tree;
2697 read_lock(&em_tree->lock);
2698 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2699 read_unlock(&em_tree->lock);
2701 if (!em || em->start > chunk_offset ||
2702 em->start + em->len < chunk_offset) {
2704 * This is a logic error, but we don't want to just rely on the
2705 * user having built with ASSERT enabled, so if ASSERT doens't
2706 * do anything we still error out.
2710 free_extent_map(em);
2713 map = (struct map_lookup *)em->bdev;
2714 lock_chunks(root->fs_info->chunk_root);
2715 check_system_chunk(trans, extent_root, map->type);
2716 unlock_chunks(root->fs_info->chunk_root);
2718 for (i = 0; i < map->num_stripes; i++) {
2719 struct btrfs_device *device = map->stripes[i].dev;
2720 ret = btrfs_free_dev_extent(trans, device,
2721 map->stripes[i].physical,
2724 btrfs_abort_transaction(trans, root, ret);
2728 if (device->bytes_used > 0) {
2730 btrfs_device_set_bytes_used(device,
2731 device->bytes_used - dev_extent_len);
2732 spin_lock(&root->fs_info->free_chunk_lock);
2733 root->fs_info->free_chunk_space += dev_extent_len;
2734 spin_unlock(&root->fs_info->free_chunk_lock);
2735 btrfs_clear_space_info_full(root->fs_info);
2736 unlock_chunks(root);
2739 if (map->stripes[i].dev) {
2740 ret = btrfs_update_device(trans, map->stripes[i].dev);
2742 btrfs_abort_transaction(trans, root, ret);
2747 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
2749 btrfs_abort_transaction(trans, root, ret);
2753 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2755 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2756 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2758 btrfs_abort_transaction(trans, root, ret);
2763 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
2765 btrfs_abort_transaction(trans, extent_root, ret);
2771 free_extent_map(em);
2775 static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
2777 struct btrfs_root *extent_root;
2778 struct btrfs_trans_handle *trans;
2781 root = root->fs_info->chunk_root;
2782 extent_root = root->fs_info->extent_root;
2785 * Prevent races with automatic removal of unused block groups.
2786 * After we relocate and before we remove the chunk with offset
2787 * chunk_offset, automatic removal of the block group can kick in,
2788 * resulting in a failure when calling btrfs_remove_chunk() below.
2790 * Make sure to acquire this mutex before doing a tree search (dev
2791 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2792 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2793 * we release the path used to search the chunk/dev tree and before
2794 * the current task acquires this mutex and calls us.
2796 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2798 ret = btrfs_can_relocate(extent_root, chunk_offset);
2802 /* step one, relocate all the extents inside this chunk */
2803 btrfs_scrub_pause(root);
2804 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2805 btrfs_scrub_continue(root);
2809 trans = btrfs_start_transaction(root, 0);
2810 if (IS_ERR(trans)) {
2811 ret = PTR_ERR(trans);
2812 btrfs_std_error(root->fs_info, ret);
2817 * step two, delete the device extents and the
2818 * chunk tree entries
2820 ret = btrfs_remove_chunk(trans, root, chunk_offset);
2821 btrfs_end_transaction(trans, root);
2825 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2827 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2828 struct btrfs_path *path;
2829 struct extent_buffer *leaf;
2830 struct btrfs_chunk *chunk;
2831 struct btrfs_key key;
2832 struct btrfs_key found_key;
2834 bool retried = false;
2838 path = btrfs_alloc_path();
2843 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2844 key.offset = (u64)-1;
2845 key.type = BTRFS_CHUNK_ITEM_KEY;
2848 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
2849 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2851 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
2854 BUG_ON(ret == 0); /* Corruption */
2856 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2859 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
2865 leaf = path->nodes[0];
2866 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2868 chunk = btrfs_item_ptr(leaf, path->slots[0],
2869 struct btrfs_chunk);
2870 chunk_type = btrfs_chunk_type(leaf, chunk);
2871 btrfs_release_path(path);
2873 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2874 ret = btrfs_relocate_chunk(chunk_root,
2881 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
2883 if (found_key.offset == 0)
2885 key.offset = found_key.offset - 1;
2888 if (failed && !retried) {
2892 } else if (WARN_ON(failed && retried)) {
2896 btrfs_free_path(path);
2900 static int insert_balance_item(struct btrfs_root *root,
2901 struct btrfs_balance_control *bctl)
2903 struct btrfs_trans_handle *trans;
2904 struct btrfs_balance_item *item;
2905 struct btrfs_disk_balance_args disk_bargs;
2906 struct btrfs_path *path;
2907 struct extent_buffer *leaf;
2908 struct btrfs_key key;
2911 path = btrfs_alloc_path();
2915 trans = btrfs_start_transaction(root, 0);
2916 if (IS_ERR(trans)) {
2917 btrfs_free_path(path);
2918 return PTR_ERR(trans);
2921 key.objectid = BTRFS_BALANCE_OBJECTID;
2922 key.type = BTRFS_BALANCE_ITEM_KEY;
2925 ret = btrfs_insert_empty_item(trans, root, path, &key,
2930 leaf = path->nodes[0];
2931 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2933 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2935 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2936 btrfs_set_balance_data(leaf, item, &disk_bargs);
2937 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2938 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2939 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2940 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2942 btrfs_set_balance_flags(leaf, item, bctl->flags);
2944 btrfs_mark_buffer_dirty(leaf);
2946 btrfs_free_path(path);
2947 err = btrfs_commit_transaction(trans, root);
2953 static int del_balance_item(struct btrfs_root *root)
2955 struct btrfs_trans_handle *trans;
2956 struct btrfs_path *path;
2957 struct btrfs_key key;
2960 path = btrfs_alloc_path();
2964 trans = btrfs_start_transaction(root, 0);
2965 if (IS_ERR(trans)) {
2966 btrfs_free_path(path);
2967 return PTR_ERR(trans);
2970 key.objectid = BTRFS_BALANCE_OBJECTID;
2971 key.type = BTRFS_BALANCE_ITEM_KEY;
2974 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2982 ret = btrfs_del_item(trans, root, path);
2984 btrfs_free_path(path);
2985 err = btrfs_commit_transaction(trans, root);
2992 * This is a heuristic used to reduce the number of chunks balanced on
2993 * resume after balance was interrupted.
2995 static void update_balance_args(struct btrfs_balance_control *bctl)
2998 * Turn on soft mode for chunk types that were being converted.
3000 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3001 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3002 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3003 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3004 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3005 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3008 * Turn on usage filter if is not already used. The idea is
3009 * that chunks that we have already balanced should be
3010 * reasonably full. Don't do it for chunks that are being
3011 * converted - that will keep us from relocating unconverted
3012 * (albeit full) chunks.
3014 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3015 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3016 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3017 bctl->data.usage = 90;
3019 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3020 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3021 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3022 bctl->sys.usage = 90;
3024 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3025 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3026 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3027 bctl->meta.usage = 90;
3032 * Should be called with both balance and volume mutexes held to
3033 * serialize other volume operations (add_dev/rm_dev/resize) with
3034 * restriper. Same goes for unset_balance_control.
3036 static void set_balance_control(struct btrfs_balance_control *bctl)
3038 struct btrfs_fs_info *fs_info = bctl->fs_info;
3040 BUG_ON(fs_info->balance_ctl);
3042 spin_lock(&fs_info->balance_lock);
3043 fs_info->balance_ctl = bctl;
3044 spin_unlock(&fs_info->balance_lock);
3047 static void unset_balance_control(struct btrfs_fs_info *fs_info)
3049 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3051 BUG_ON(!fs_info->balance_ctl);
3053 spin_lock(&fs_info->balance_lock);
3054 fs_info->balance_ctl = NULL;
3055 spin_unlock(&fs_info->balance_lock);
3061 * Balance filters. Return 1 if chunk should be filtered out
3062 * (should not be balanced).
3064 static int chunk_profiles_filter(u64 chunk_type,
3065 struct btrfs_balance_args *bargs)
3067 chunk_type = chunk_to_extended(chunk_type) &
3068 BTRFS_EXTENDED_PROFILE_MASK;
3070 if (bargs->profiles & chunk_type)
3076 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3077 struct btrfs_balance_args *bargs)
3079 struct btrfs_block_group_cache *cache;
3080 u64 chunk_used, user_thresh;
3083 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3084 chunk_used = btrfs_block_group_used(&cache->item);
3086 if (bargs->usage == 0)
3088 else if (bargs->usage > 100)
3089 user_thresh = cache->key.offset;
3091 user_thresh = div_factor_fine(cache->key.offset,
3094 if (chunk_used < user_thresh)
3097 btrfs_put_block_group(cache);
3101 static int chunk_devid_filter(struct extent_buffer *leaf,
3102 struct btrfs_chunk *chunk,
3103 struct btrfs_balance_args *bargs)
3105 struct btrfs_stripe *stripe;
3106 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3109 for (i = 0; i < num_stripes; i++) {
3110 stripe = btrfs_stripe_nr(chunk, i);
3111 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3118 /* [pstart, pend) */
3119 static int chunk_drange_filter(struct extent_buffer *leaf,
3120 struct btrfs_chunk *chunk,
3122 struct btrfs_balance_args *bargs)
3124 struct btrfs_stripe *stripe;
3125 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3131 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3134 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
3135 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3136 factor = num_stripes / 2;
3137 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3138 factor = num_stripes - 1;
3139 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3140 factor = num_stripes - 2;
3142 factor = num_stripes;
3145 for (i = 0; i < num_stripes; i++) {
3146 stripe = btrfs_stripe_nr(chunk, i);
3147 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3150 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3151 stripe_length = btrfs_chunk_length(leaf, chunk);
3152 stripe_length = div_u64(stripe_length, factor);
3154 if (stripe_offset < bargs->pend &&
3155 stripe_offset + stripe_length > bargs->pstart)
3162 /* [vstart, vend) */
3163 static int chunk_vrange_filter(struct extent_buffer *leaf,
3164 struct btrfs_chunk *chunk,
3166 struct btrfs_balance_args *bargs)
3168 if (chunk_offset < bargs->vend &&
3169 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3170 /* at least part of the chunk is inside this vrange */
3176 static int chunk_soft_convert_filter(u64 chunk_type,
3177 struct btrfs_balance_args *bargs)
3179 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3182 chunk_type = chunk_to_extended(chunk_type) &
3183 BTRFS_EXTENDED_PROFILE_MASK;
3185 if (bargs->target == chunk_type)
3191 static int should_balance_chunk(struct btrfs_root *root,
3192 struct extent_buffer *leaf,
3193 struct btrfs_chunk *chunk, u64 chunk_offset)
3195 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3196 struct btrfs_balance_args *bargs = NULL;
3197 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3200 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3201 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3205 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3206 bargs = &bctl->data;
3207 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3209 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3210 bargs = &bctl->meta;
3212 /* profiles filter */
3213 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3214 chunk_profiles_filter(chunk_type, bargs)) {
3219 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3220 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3225 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3226 chunk_devid_filter(leaf, chunk, bargs)) {
3230 /* drange filter, makes sense only with devid filter */
3231 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3232 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3237 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3238 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3242 /* soft profile changing mode */
3243 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3244 chunk_soft_convert_filter(chunk_type, bargs)) {
3249 * limited by count, must be the last filter
3251 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3252 if (bargs->limit == 0)
3261 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3263 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3264 struct btrfs_root *chunk_root = fs_info->chunk_root;
3265 struct btrfs_root *dev_root = fs_info->dev_root;
3266 struct list_head *devices;
3267 struct btrfs_device *device;
3270 struct btrfs_chunk *chunk;
3271 struct btrfs_path *path;
3272 struct btrfs_key key;
3273 struct btrfs_key found_key;
3274 struct btrfs_trans_handle *trans;
3275 struct extent_buffer *leaf;
3278 int enospc_errors = 0;
3279 bool counting = true;
3280 u64 limit_data = bctl->data.limit;
3281 u64 limit_meta = bctl->meta.limit;
3282 u64 limit_sys = bctl->sys.limit;
3284 /* step one make some room on all the devices */
3285 devices = &fs_info->fs_devices->devices;
3286 list_for_each_entry(device, devices, dev_list) {
3287 old_size = btrfs_device_get_total_bytes(device);
3288 size_to_free = div_factor(old_size, 1);
3289 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
3290 if (!device->writeable ||
3291 btrfs_device_get_total_bytes(device) -
3292 btrfs_device_get_bytes_used(device) > size_to_free ||
3293 device->is_tgtdev_for_dev_replace)
3296 ret = btrfs_shrink_device(device, old_size - size_to_free);
3301 trans = btrfs_start_transaction(dev_root, 0);
3302 BUG_ON(IS_ERR(trans));
3304 ret = btrfs_grow_device(trans, device, old_size);
3307 btrfs_end_transaction(trans, dev_root);
3310 /* step two, relocate all the chunks */
3311 path = btrfs_alloc_path();
3317 /* zero out stat counters */
3318 spin_lock(&fs_info->balance_lock);
3319 memset(&bctl->stat, 0, sizeof(bctl->stat));
3320 spin_unlock(&fs_info->balance_lock);
3323 bctl->data.limit = limit_data;
3324 bctl->meta.limit = limit_meta;
3325 bctl->sys.limit = limit_sys;
3327 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3328 key.offset = (u64)-1;
3329 key.type = BTRFS_CHUNK_ITEM_KEY;
3332 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3333 atomic_read(&fs_info->balance_cancel_req)) {
3338 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3339 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3341 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3346 * this shouldn't happen, it means the last relocate
3350 BUG(); /* FIXME break ? */
3352 ret = btrfs_previous_item(chunk_root, path, 0,
3353 BTRFS_CHUNK_ITEM_KEY);
3355 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3360 leaf = path->nodes[0];
3361 slot = path->slots[0];
3362 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3364 if (found_key.objectid != key.objectid) {
3365 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3369 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3372 spin_lock(&fs_info->balance_lock);
3373 bctl->stat.considered++;
3374 spin_unlock(&fs_info->balance_lock);
3377 ret = should_balance_chunk(chunk_root, leaf, chunk,
3379 btrfs_release_path(path);
3381 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3386 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3387 spin_lock(&fs_info->balance_lock);
3388 bctl->stat.expected++;
3389 spin_unlock(&fs_info->balance_lock);
3393 ret = btrfs_relocate_chunk(chunk_root,
3395 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3396 if (ret && ret != -ENOSPC)
3398 if (ret == -ENOSPC) {
3401 spin_lock(&fs_info->balance_lock);
3402 bctl->stat.completed++;
3403 spin_unlock(&fs_info->balance_lock);
3406 if (found_key.offset == 0)
3408 key.offset = found_key.offset - 1;
3412 btrfs_release_path(path);
3417 btrfs_free_path(path);
3418 if (enospc_errors) {
3419 btrfs_info(fs_info, "%d enospc errors during balance",
3429 * alloc_profile_is_valid - see if a given profile is valid and reduced
3430 * @flags: profile to validate
3431 * @extended: if true @flags is treated as an extended profile
3433 static int alloc_profile_is_valid(u64 flags, int extended)
3435 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3436 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3438 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3440 /* 1) check that all other bits are zeroed */
3444 /* 2) see if profile is reduced */
3446 return !extended; /* "0" is valid for usual profiles */
3448 /* true if exactly one bit set */
3449 return (flags & (flags - 1)) == 0;
3452 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3454 /* cancel requested || normal exit path */
3455 return atomic_read(&fs_info->balance_cancel_req) ||
3456 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3457 atomic_read(&fs_info->balance_cancel_req) == 0);
3460 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3464 unset_balance_control(fs_info);
3465 ret = del_balance_item(fs_info->tree_root);
3467 btrfs_std_error(fs_info, ret);
3469 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3473 * Should be called with both balance and volume mutexes held
3475 int btrfs_balance(struct btrfs_balance_control *bctl,
3476 struct btrfs_ioctl_balance_args *bargs)
3478 struct btrfs_fs_info *fs_info = bctl->fs_info;
3485 if (btrfs_fs_closing(fs_info) ||
3486 atomic_read(&fs_info->balance_pause_req) ||
3487 atomic_read(&fs_info->balance_cancel_req)) {
3492 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3493 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3497 * In case of mixed groups both data and meta should be picked,
3498 * and identical options should be given for both of them.
3500 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3501 if (mixed && (bctl->flags & allowed)) {
3502 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3503 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3504 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3505 btrfs_err(fs_info, "with mixed groups data and "
3506 "metadata balance options must be the same");
3512 num_devices = fs_info->fs_devices->num_devices;
3513 btrfs_dev_replace_lock(&fs_info->dev_replace);
3514 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3515 BUG_ON(num_devices < 1);
3518 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3519 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3520 if (num_devices == 1)
3521 allowed |= BTRFS_BLOCK_GROUP_DUP;
3522 else if (num_devices > 1)
3523 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3524 if (num_devices > 2)
3525 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3526 if (num_devices > 3)
3527 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3528 BTRFS_BLOCK_GROUP_RAID6);
3529 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3530 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3531 (bctl->data.target & ~allowed))) {
3532 btrfs_err(fs_info, "unable to start balance with target "
3533 "data profile %llu",
3538 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3539 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3540 (bctl->meta.target & ~allowed))) {
3542 "unable to start balance with target metadata profile %llu",
3547 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3548 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3549 (bctl->sys.target & ~allowed))) {
3551 "unable to start balance with target system profile %llu",
3557 /* allow dup'ed data chunks only in mixed mode */
3558 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3559 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3560 btrfs_err(fs_info, "dup for data is not allowed");
3565 /* allow to reduce meta or sys integrity only if force set */
3566 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3567 BTRFS_BLOCK_GROUP_RAID10 |
3568 BTRFS_BLOCK_GROUP_RAID5 |
3569 BTRFS_BLOCK_GROUP_RAID6;
3571 seq = read_seqbegin(&fs_info->profiles_lock);
3573 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3574 (fs_info->avail_system_alloc_bits & allowed) &&
3575 !(bctl->sys.target & allowed)) ||
3576 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3577 (fs_info->avail_metadata_alloc_bits & allowed) &&
3578 !(bctl->meta.target & allowed))) {
3579 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3580 btrfs_info(fs_info, "force reducing metadata integrity");
3582 btrfs_err(fs_info, "balance will reduce metadata "
3583 "integrity, use force if you want this");
3588 } while (read_seqretry(&fs_info->profiles_lock, seq));
3590 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3591 fs_info->num_tolerated_disk_barrier_failures = min(
3592 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3593 btrfs_get_num_tolerated_disk_barrier_failures(
3597 ret = insert_balance_item(fs_info->tree_root, bctl);
3598 if (ret && ret != -EEXIST)
3601 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3602 BUG_ON(ret == -EEXIST);
3603 set_balance_control(bctl);
3605 BUG_ON(ret != -EEXIST);
3606 spin_lock(&fs_info->balance_lock);
3607 update_balance_args(bctl);
3608 spin_unlock(&fs_info->balance_lock);
3611 atomic_inc(&fs_info->balance_running);
3612 mutex_unlock(&fs_info->balance_mutex);
3614 ret = __btrfs_balance(fs_info);
3616 mutex_lock(&fs_info->balance_mutex);
3617 atomic_dec(&fs_info->balance_running);
3619 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3620 fs_info->num_tolerated_disk_barrier_failures =
3621 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3625 memset(bargs, 0, sizeof(*bargs));
3626 update_ioctl_balance_args(fs_info, 0, bargs);
3629 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3630 balance_need_close(fs_info)) {
3631 __cancel_balance(fs_info);
3634 wake_up(&fs_info->balance_wait_q);
3638 if (bctl->flags & BTRFS_BALANCE_RESUME)
3639 __cancel_balance(fs_info);
3642 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3647 static int balance_kthread(void *data)
3649 struct btrfs_fs_info *fs_info = data;
3652 mutex_lock(&fs_info->volume_mutex);
3653 mutex_lock(&fs_info->balance_mutex);
3655 if (fs_info->balance_ctl) {
3656 btrfs_info(fs_info, "continuing balance");
3657 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3660 mutex_unlock(&fs_info->balance_mutex);
3661 mutex_unlock(&fs_info->volume_mutex);
3666 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3668 struct task_struct *tsk;
3670 spin_lock(&fs_info->balance_lock);
3671 if (!fs_info->balance_ctl) {
3672 spin_unlock(&fs_info->balance_lock);
3675 spin_unlock(&fs_info->balance_lock);
3677 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3678 btrfs_info(fs_info, "force skipping balance");
3682 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3683 return PTR_ERR_OR_ZERO(tsk);
3686 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3688 struct btrfs_balance_control *bctl;
3689 struct btrfs_balance_item *item;
3690 struct btrfs_disk_balance_args disk_bargs;
3691 struct btrfs_path *path;
3692 struct extent_buffer *leaf;
3693 struct btrfs_key key;
3696 path = btrfs_alloc_path();
3700 key.objectid = BTRFS_BALANCE_OBJECTID;
3701 key.type = BTRFS_BALANCE_ITEM_KEY;
3704 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3707 if (ret > 0) { /* ret = -ENOENT; */
3712 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3718 leaf = path->nodes[0];
3719 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3721 bctl->fs_info = fs_info;
3722 bctl->flags = btrfs_balance_flags(leaf, item);
3723 bctl->flags |= BTRFS_BALANCE_RESUME;
3725 btrfs_balance_data(leaf, item, &disk_bargs);
3726 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3727 btrfs_balance_meta(leaf, item, &disk_bargs);
3728 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3729 btrfs_balance_sys(leaf, item, &disk_bargs);
3730 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3732 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3734 mutex_lock(&fs_info->volume_mutex);
3735 mutex_lock(&fs_info->balance_mutex);
3737 set_balance_control(bctl);
3739 mutex_unlock(&fs_info->balance_mutex);
3740 mutex_unlock(&fs_info->volume_mutex);
3742 btrfs_free_path(path);
3746 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3750 mutex_lock(&fs_info->balance_mutex);
3751 if (!fs_info->balance_ctl) {
3752 mutex_unlock(&fs_info->balance_mutex);
3756 if (atomic_read(&fs_info->balance_running)) {
3757 atomic_inc(&fs_info->balance_pause_req);
3758 mutex_unlock(&fs_info->balance_mutex);
3760 wait_event(fs_info->balance_wait_q,
3761 atomic_read(&fs_info->balance_running) == 0);
3763 mutex_lock(&fs_info->balance_mutex);
3764 /* we are good with balance_ctl ripped off from under us */
3765 BUG_ON(atomic_read(&fs_info->balance_running));
3766 atomic_dec(&fs_info->balance_pause_req);
3771 mutex_unlock(&fs_info->balance_mutex);
3775 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3777 if (fs_info->sb->s_flags & MS_RDONLY)
3780 mutex_lock(&fs_info->balance_mutex);
3781 if (!fs_info->balance_ctl) {
3782 mutex_unlock(&fs_info->balance_mutex);
3786 atomic_inc(&fs_info->balance_cancel_req);
3788 * if we are running just wait and return, balance item is
3789 * deleted in btrfs_balance in this case
3791 if (atomic_read(&fs_info->balance_running)) {
3792 mutex_unlock(&fs_info->balance_mutex);
3793 wait_event(fs_info->balance_wait_q,
3794 atomic_read(&fs_info->balance_running) == 0);
3795 mutex_lock(&fs_info->balance_mutex);
3797 /* __cancel_balance needs volume_mutex */
3798 mutex_unlock(&fs_info->balance_mutex);
3799 mutex_lock(&fs_info->volume_mutex);
3800 mutex_lock(&fs_info->balance_mutex);
3802 if (fs_info->balance_ctl)
3803 __cancel_balance(fs_info);
3805 mutex_unlock(&fs_info->volume_mutex);
3808 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3809 atomic_dec(&fs_info->balance_cancel_req);
3810 mutex_unlock(&fs_info->balance_mutex);
3814 static int btrfs_uuid_scan_kthread(void *data)
3816 struct btrfs_fs_info *fs_info = data;
3817 struct btrfs_root *root = fs_info->tree_root;
3818 struct btrfs_key key;
3819 struct btrfs_key max_key;
3820 struct btrfs_path *path = NULL;
3822 struct extent_buffer *eb;
3824 struct btrfs_root_item root_item;
3826 struct btrfs_trans_handle *trans = NULL;
3828 path = btrfs_alloc_path();
3835 key.type = BTRFS_ROOT_ITEM_KEY;
3838 max_key.objectid = (u64)-1;
3839 max_key.type = BTRFS_ROOT_ITEM_KEY;
3840 max_key.offset = (u64)-1;
3843 ret = btrfs_search_forward(root, &key, path, 0);
3850 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3851 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3852 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3853 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3856 eb = path->nodes[0];
3857 slot = path->slots[0];
3858 item_size = btrfs_item_size_nr(eb, slot);
3859 if (item_size < sizeof(root_item))
3862 read_extent_buffer(eb, &root_item,
3863 btrfs_item_ptr_offset(eb, slot),
3864 (int)sizeof(root_item));
3865 if (btrfs_root_refs(&root_item) == 0)
3868 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3869 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3873 btrfs_release_path(path);
3875 * 1 - subvol uuid item
3876 * 1 - received_subvol uuid item
3878 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3879 if (IS_ERR(trans)) {
3880 ret = PTR_ERR(trans);
3888 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3889 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3891 BTRFS_UUID_KEY_SUBVOL,
3894 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3900 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3901 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3902 root_item.received_uuid,
3903 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3906 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3914 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3920 btrfs_release_path(path);
3921 if (key.offset < (u64)-1) {
3923 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3925 key.type = BTRFS_ROOT_ITEM_KEY;
3926 } else if (key.objectid < (u64)-1) {
3928 key.type = BTRFS_ROOT_ITEM_KEY;
3937 btrfs_free_path(path);
3938 if (trans && !IS_ERR(trans))
3939 btrfs_end_transaction(trans, fs_info->uuid_root);
3941 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
3943 fs_info->update_uuid_tree_gen = 1;
3944 up(&fs_info->uuid_tree_rescan_sem);
3949 * Callback for btrfs_uuid_tree_iterate().
3951 * 0 check succeeded, the entry is not outdated.
3952 * < 0 if an error occured.
3953 * > 0 if the check failed, which means the caller shall remove the entry.
3955 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3956 u8 *uuid, u8 type, u64 subid)
3958 struct btrfs_key key;
3960 struct btrfs_root *subvol_root;
3962 if (type != BTRFS_UUID_KEY_SUBVOL &&
3963 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3966 key.objectid = subid;
3967 key.type = BTRFS_ROOT_ITEM_KEY;
3968 key.offset = (u64)-1;
3969 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3970 if (IS_ERR(subvol_root)) {
3971 ret = PTR_ERR(subvol_root);
3978 case BTRFS_UUID_KEY_SUBVOL:
3979 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3982 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3983 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3993 static int btrfs_uuid_rescan_kthread(void *data)
3995 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3999 * 1st step is to iterate through the existing UUID tree and
4000 * to delete all entries that contain outdated data.
4001 * 2nd step is to add all missing entries to the UUID tree.
4003 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4005 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
4006 up(&fs_info->uuid_tree_rescan_sem);
4009 return btrfs_uuid_scan_kthread(data);
4012 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4014 struct btrfs_trans_handle *trans;
4015 struct btrfs_root *tree_root = fs_info->tree_root;
4016 struct btrfs_root *uuid_root;
4017 struct task_struct *task;
4024 trans = btrfs_start_transaction(tree_root, 2);
4026 return PTR_ERR(trans);
4028 uuid_root = btrfs_create_tree(trans, fs_info,
4029 BTRFS_UUID_TREE_OBJECTID);
4030 if (IS_ERR(uuid_root)) {
4031 ret = PTR_ERR(uuid_root);
4032 btrfs_abort_transaction(trans, tree_root, ret);
4036 fs_info->uuid_root = uuid_root;
4038 ret = btrfs_commit_transaction(trans, tree_root);
4042 down(&fs_info->uuid_tree_rescan_sem);
4043 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4045 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4046 btrfs_warn(fs_info, "failed to start uuid_scan task");
4047 up(&fs_info->uuid_tree_rescan_sem);
4048 return PTR_ERR(task);
4054 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4056 struct task_struct *task;
4058 down(&fs_info->uuid_tree_rescan_sem);
4059 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4061 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4062 btrfs_warn(fs_info, "failed to start uuid_rescan task");
4063 up(&fs_info->uuid_tree_rescan_sem);
4064 return PTR_ERR(task);
4071 * shrinking a device means finding all of the device extents past
4072 * the new size, and then following the back refs to the chunks.
4073 * The chunk relocation code actually frees the device extent
4075 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4077 struct btrfs_trans_handle *trans;
4078 struct btrfs_root *root = device->dev_root;
4079 struct btrfs_dev_extent *dev_extent = NULL;
4080 struct btrfs_path *path;
4086 bool retried = false;
4087 bool checked_pending_chunks = false;
4088 struct extent_buffer *l;
4089 struct btrfs_key key;
4090 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4091 u64 old_total = btrfs_super_total_bytes(super_copy);
4092 u64 old_size = btrfs_device_get_total_bytes(device);
4093 u64 diff = old_size - new_size;
4095 if (device->is_tgtdev_for_dev_replace)
4098 path = btrfs_alloc_path();
4106 btrfs_device_set_total_bytes(device, new_size);
4107 if (device->writeable) {
4108 device->fs_devices->total_rw_bytes -= diff;
4109 spin_lock(&root->fs_info->free_chunk_lock);
4110 root->fs_info->free_chunk_space -= diff;
4111 spin_unlock(&root->fs_info->free_chunk_lock);
4113 unlock_chunks(root);
4116 key.objectid = device->devid;
4117 key.offset = (u64)-1;
4118 key.type = BTRFS_DEV_EXTENT_KEY;
4121 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
4122 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4124 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4128 ret = btrfs_previous_item(root, path, 0, key.type);
4130 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4135 btrfs_release_path(path);
4140 slot = path->slots[0];
4141 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4143 if (key.objectid != device->devid) {
4144 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4145 btrfs_release_path(path);
4149 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4150 length = btrfs_dev_extent_length(l, dev_extent);
4152 if (key.offset + length <= new_size) {
4153 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4154 btrfs_release_path(path);
4158 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4159 btrfs_release_path(path);
4161 ret = btrfs_relocate_chunk(root, chunk_offset);
4162 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4163 if (ret && ret != -ENOSPC)
4167 } while (key.offset-- > 0);
4169 if (failed && !retried) {
4173 } else if (failed && retried) {
4178 /* Shrinking succeeded, else we would be at "done". */
4179 trans = btrfs_start_transaction(root, 0);
4180 if (IS_ERR(trans)) {
4181 ret = PTR_ERR(trans);
4188 * We checked in the above loop all device extents that were already in
4189 * the device tree. However before we have updated the device's
4190 * total_bytes to the new size, we might have had chunk allocations that
4191 * have not complete yet (new block groups attached to transaction
4192 * handles), and therefore their device extents were not yet in the
4193 * device tree and we missed them in the loop above. So if we have any
4194 * pending chunk using a device extent that overlaps the device range
4195 * that we can not use anymore, commit the current transaction and
4196 * repeat the search on the device tree - this way we guarantee we will
4197 * not have chunks using device extents that end beyond 'new_size'.
4199 if (!checked_pending_chunks) {
4200 u64 start = new_size;
4201 u64 len = old_size - new_size;
4203 if (contains_pending_extent(trans->transaction, device,
4205 unlock_chunks(root);
4206 checked_pending_chunks = true;
4209 ret = btrfs_commit_transaction(trans, root);
4216 btrfs_device_set_disk_total_bytes(device, new_size);
4217 if (list_empty(&device->resized_list))
4218 list_add_tail(&device->resized_list,
4219 &root->fs_info->fs_devices->resized_devices);
4221 WARN_ON(diff > old_total);
4222 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4223 unlock_chunks(root);
4225 /* Now btrfs_update_device() will change the on-disk size. */
4226 ret = btrfs_update_device(trans, device);
4227 btrfs_end_transaction(trans, root);
4229 btrfs_free_path(path);
4232 btrfs_device_set_total_bytes(device, old_size);
4233 if (device->writeable)
4234 device->fs_devices->total_rw_bytes += diff;
4235 spin_lock(&root->fs_info->free_chunk_lock);
4236 root->fs_info->free_chunk_space += diff;
4237 spin_unlock(&root->fs_info->free_chunk_lock);
4238 unlock_chunks(root);
4243 static int btrfs_add_system_chunk(struct btrfs_root *root,
4244 struct btrfs_key *key,
4245 struct btrfs_chunk *chunk, int item_size)
4247 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4248 struct btrfs_disk_key disk_key;
4253 array_size = btrfs_super_sys_array_size(super_copy);
4254 if (array_size + item_size + sizeof(disk_key)
4255 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4256 unlock_chunks(root);
4260 ptr = super_copy->sys_chunk_array + array_size;
4261 btrfs_cpu_key_to_disk(&disk_key, key);
4262 memcpy(ptr, &disk_key, sizeof(disk_key));
4263 ptr += sizeof(disk_key);
4264 memcpy(ptr, chunk, item_size);
4265 item_size += sizeof(disk_key);
4266 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4267 unlock_chunks(root);
4273 * sort the devices in descending order by max_avail, total_avail
4275 static int btrfs_cmp_device_info(const void *a, const void *b)
4277 const struct btrfs_device_info *di_a = a;
4278 const struct btrfs_device_info *di_b = b;
4280 if (di_a->max_avail > di_b->max_avail)
4282 if (di_a->max_avail < di_b->max_avail)
4284 if (di_a->total_avail > di_b->total_avail)
4286 if (di_a->total_avail < di_b->total_avail)
4291 static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
4292 [BTRFS_RAID_RAID10] = {
4295 .devs_max = 0, /* 0 == as many as possible */
4297 .devs_increment = 2,
4300 [BTRFS_RAID_RAID1] = {
4305 .devs_increment = 2,
4308 [BTRFS_RAID_DUP] = {
4313 .devs_increment = 1,
4316 [BTRFS_RAID_RAID0] = {
4321 .devs_increment = 1,
4324 [BTRFS_RAID_SINGLE] = {
4329 .devs_increment = 1,
4332 [BTRFS_RAID_RAID5] = {
4337 .devs_increment = 1,
4340 [BTRFS_RAID_RAID6] = {
4345 .devs_increment = 1,
4350 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4352 /* TODO allow them to set a preferred stripe size */
4356 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4358 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
4361 btrfs_set_fs_incompat(info, RAID56);
4364 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4365 - sizeof(struct btrfs_item) \
4366 - sizeof(struct btrfs_chunk)) \
4367 / sizeof(struct btrfs_stripe) + 1)
4369 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4370 - 2 * sizeof(struct btrfs_disk_key) \
4371 - 2 * sizeof(struct btrfs_chunk)) \
4372 / sizeof(struct btrfs_stripe) + 1)
4374 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4375 struct btrfs_root *extent_root, u64 start,
4378 struct btrfs_fs_info *info = extent_root->fs_info;
4379 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4380 struct list_head *cur;
4381 struct map_lookup *map = NULL;
4382 struct extent_map_tree *em_tree;
4383 struct extent_map *em;
4384 struct btrfs_device_info *devices_info = NULL;
4386 int num_stripes; /* total number of stripes to allocate */
4387 int data_stripes; /* number of stripes that count for
4389 int sub_stripes; /* sub_stripes info for map */
4390 int dev_stripes; /* stripes per dev */
4391 int devs_max; /* max devs to use */
4392 int devs_min; /* min devs needed */
4393 int devs_increment; /* ndevs has to be a multiple of this */
4394 int ncopies; /* how many copies to data has */
4396 u64 max_stripe_size;
4400 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
4406 BUG_ON(!alloc_profile_is_valid(type, 0));
4408 if (list_empty(&fs_devices->alloc_list))
4411 index = __get_raid_index(type);
4413 sub_stripes = btrfs_raid_array[index].sub_stripes;
4414 dev_stripes = btrfs_raid_array[index].dev_stripes;
4415 devs_max = btrfs_raid_array[index].devs_max;
4416 devs_min = btrfs_raid_array[index].devs_min;
4417 devs_increment = btrfs_raid_array[index].devs_increment;
4418 ncopies = btrfs_raid_array[index].ncopies;
4420 if (type & BTRFS_BLOCK_GROUP_DATA) {
4421 max_stripe_size = 1024 * 1024 * 1024;
4422 max_chunk_size = 10 * max_stripe_size;
4424 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4425 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4426 /* for larger filesystems, use larger metadata chunks */
4427 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4428 max_stripe_size = 1024 * 1024 * 1024;
4430 max_stripe_size = 256 * 1024 * 1024;
4431 max_chunk_size = max_stripe_size;
4433 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4434 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4435 max_stripe_size = 32 * 1024 * 1024;
4436 max_chunk_size = 2 * max_stripe_size;
4438 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
4440 btrfs_err(info, "invalid chunk type 0x%llx requested",
4445 /* we don't want a chunk larger than 10% of writeable space */
4446 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4449 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
4454 cur = fs_devices->alloc_list.next;
4457 * in the first pass through the devices list, we gather information
4458 * about the available holes on each device.
4461 while (cur != &fs_devices->alloc_list) {
4462 struct btrfs_device *device;
4466 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4470 if (!device->writeable) {
4472 "BTRFS: read-only device in alloc_list\n");
4476 if (!device->in_fs_metadata ||
4477 device->is_tgtdev_for_dev_replace)
4480 if (device->total_bytes > device->bytes_used)
4481 total_avail = device->total_bytes - device->bytes_used;
4485 /* If there is no space on this device, skip it. */
4486 if (total_avail == 0)
4489 ret = find_free_dev_extent(trans, device,
4490 max_stripe_size * dev_stripes,
4491 &dev_offset, &max_avail);
4492 if (ret && ret != -ENOSPC)
4496 max_avail = max_stripe_size * dev_stripes;
4498 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4501 if (ndevs == fs_devices->rw_devices) {
4502 WARN(1, "%s: found more than %llu devices\n",
4503 __func__, fs_devices->rw_devices);
4506 devices_info[ndevs].dev_offset = dev_offset;
4507 devices_info[ndevs].max_avail = max_avail;
4508 devices_info[ndevs].total_avail = total_avail;
4509 devices_info[ndevs].dev = device;
4514 * now sort the devices by hole size / available space
4516 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4517 btrfs_cmp_device_info, NULL);
4519 /* round down to number of usable stripes */
4520 ndevs -= ndevs % devs_increment;
4522 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4527 if (devs_max && ndevs > devs_max)
4530 * the primary goal is to maximize the number of stripes, so use as many
4531 * devices as possible, even if the stripes are not maximum sized.
4533 stripe_size = devices_info[ndevs-1].max_avail;
4534 num_stripes = ndevs * dev_stripes;
4537 * this will have to be fixed for RAID1 and RAID10 over
4540 data_stripes = num_stripes / ncopies;
4542 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4543 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4544 btrfs_super_stripesize(info->super_copy));
4545 data_stripes = num_stripes - 1;
4547 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4548 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4549 btrfs_super_stripesize(info->super_copy));
4550 data_stripes = num_stripes - 2;
4554 * Use the number of data stripes to figure out how big this chunk
4555 * is really going to be in terms of logical address space,
4556 * and compare that answer with the max chunk size
4558 if (stripe_size * data_stripes > max_chunk_size) {
4559 u64 mask = (1ULL << 24) - 1;
4561 stripe_size = div_u64(max_chunk_size, data_stripes);
4563 /* bump the answer up to a 16MB boundary */
4564 stripe_size = (stripe_size + mask) & ~mask;
4566 /* but don't go higher than the limits we found
4567 * while searching for free extents
4569 if (stripe_size > devices_info[ndevs-1].max_avail)
4570 stripe_size = devices_info[ndevs-1].max_avail;
4573 stripe_size = div_u64(stripe_size, dev_stripes);
4575 /* align to BTRFS_STRIPE_LEN */
4576 stripe_size = div_u64(stripe_size, raid_stripe_len);
4577 stripe_size *= raid_stripe_len;
4579 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4584 map->num_stripes = num_stripes;
4586 for (i = 0; i < ndevs; ++i) {
4587 for (j = 0; j < dev_stripes; ++j) {
4588 int s = i * dev_stripes + j;
4589 map->stripes[s].dev = devices_info[i].dev;
4590 map->stripes[s].physical = devices_info[i].dev_offset +
4594 map->sector_size = extent_root->sectorsize;
4595 map->stripe_len = raid_stripe_len;
4596 map->io_align = raid_stripe_len;
4597 map->io_width = raid_stripe_len;
4599 map->sub_stripes = sub_stripes;
4601 num_bytes = stripe_size * data_stripes;
4603 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
4605 em = alloc_extent_map();
4611 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4612 em->bdev = (struct block_device *)map;
4614 em->len = num_bytes;
4615 em->block_start = 0;
4616 em->block_len = em->len;
4617 em->orig_block_len = stripe_size;
4619 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4620 write_lock(&em_tree->lock);
4621 ret = add_extent_mapping(em_tree, em, 0);
4623 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4624 atomic_inc(&em->refs);
4626 write_unlock(&em_tree->lock);
4628 free_extent_map(em);
4632 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4633 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4636 goto error_del_extent;
4638 for (i = 0; i < map->num_stripes; i++) {
4639 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4640 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4643 spin_lock(&extent_root->fs_info->free_chunk_lock);
4644 extent_root->fs_info->free_chunk_space -= (stripe_size *
4646 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4648 free_extent_map(em);
4649 check_raid56_incompat_flag(extent_root->fs_info, type);
4651 kfree(devices_info);
4655 write_lock(&em_tree->lock);
4656 remove_extent_mapping(em_tree, em);
4657 write_unlock(&em_tree->lock);
4659 /* One for our allocation */
4660 free_extent_map(em);
4661 /* One for the tree reference */
4662 free_extent_map(em);
4663 /* One for the pending_chunks list reference */
4664 free_extent_map(em);
4666 kfree(devices_info);
4670 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4671 struct btrfs_root *extent_root,
4672 u64 chunk_offset, u64 chunk_size)
4674 struct btrfs_key key;
4675 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4676 struct btrfs_device *device;
4677 struct btrfs_chunk *chunk;
4678 struct btrfs_stripe *stripe;
4679 struct extent_map_tree *em_tree;
4680 struct extent_map *em;
4681 struct map_lookup *map;
4688 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4689 read_lock(&em_tree->lock);
4690 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4691 read_unlock(&em_tree->lock);
4694 btrfs_crit(extent_root->fs_info, "unable to find logical "
4695 "%Lu len %Lu", chunk_offset, chunk_size);
4699 if (em->start != chunk_offset || em->len != chunk_size) {
4700 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4701 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
4702 chunk_size, em->start, em->len);
4703 free_extent_map(em);
4707 map = (struct map_lookup *)em->bdev;
4708 item_size = btrfs_chunk_item_size(map->num_stripes);
4709 stripe_size = em->orig_block_len;
4711 chunk = kzalloc(item_size, GFP_NOFS);
4717 for (i = 0; i < map->num_stripes; i++) {
4718 device = map->stripes[i].dev;
4719 dev_offset = map->stripes[i].physical;
4721 ret = btrfs_update_device(trans, device);
4724 ret = btrfs_alloc_dev_extent(trans, device,
4725 chunk_root->root_key.objectid,
4726 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4727 chunk_offset, dev_offset,
4733 stripe = &chunk->stripe;
4734 for (i = 0; i < map->num_stripes; i++) {
4735 device = map->stripes[i].dev;
4736 dev_offset = map->stripes[i].physical;
4738 btrfs_set_stack_stripe_devid(stripe, device->devid);
4739 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4740 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4744 btrfs_set_stack_chunk_length(chunk, chunk_size);
4745 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4746 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4747 btrfs_set_stack_chunk_type(chunk, map->type);
4748 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4749 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4750 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4751 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4752 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4754 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4755 key.type = BTRFS_CHUNK_ITEM_KEY;
4756 key.offset = chunk_offset;
4758 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4759 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4761 * TODO: Cleanup of inserted chunk root in case of
4764 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4770 free_extent_map(em);
4775 * Chunk allocation falls into two parts. The first part does works
4776 * that make the new allocated chunk useable, but not do any operation
4777 * that modifies the chunk tree. The second part does the works that
4778 * require modifying the chunk tree. This division is important for the
4779 * bootstrap process of adding storage to a seed btrfs.
4781 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4782 struct btrfs_root *extent_root, u64 type)
4786 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
4787 chunk_offset = find_next_chunk(extent_root->fs_info);
4788 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
4791 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4792 struct btrfs_root *root,
4793 struct btrfs_device *device)
4796 u64 sys_chunk_offset;
4798 struct btrfs_fs_info *fs_info = root->fs_info;
4799 struct btrfs_root *extent_root = fs_info->extent_root;
4802 chunk_offset = find_next_chunk(fs_info);
4803 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4804 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4809 sys_chunk_offset = find_next_chunk(root->fs_info);
4810 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4811 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4816 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4820 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4821 BTRFS_BLOCK_GROUP_RAID10 |
4822 BTRFS_BLOCK_GROUP_RAID5 |
4823 BTRFS_BLOCK_GROUP_DUP)) {
4825 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4834 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4836 struct extent_map *em;
4837 struct map_lookup *map;
4838 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4843 read_lock(&map_tree->map_tree.lock);
4844 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4845 read_unlock(&map_tree->map_tree.lock);
4849 map = (struct map_lookup *)em->bdev;
4850 for (i = 0; i < map->num_stripes; i++) {
4851 if (map->stripes[i].dev->missing) {
4856 if (!map->stripes[i].dev->writeable) {
4863 * If the number of missing devices is larger than max errors,
4864 * we can not write the data into that chunk successfully, so
4867 if (miss_ndevs > btrfs_chunk_max_errors(map))
4870 free_extent_map(em);
4874 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4876 extent_map_tree_init(&tree->map_tree);
4879 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4881 struct extent_map *em;
4884 write_lock(&tree->map_tree.lock);
4885 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4887 remove_extent_mapping(&tree->map_tree, em);
4888 write_unlock(&tree->map_tree.lock);
4892 free_extent_map(em);
4893 /* once for the tree */
4894 free_extent_map(em);
4898 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4900 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4901 struct extent_map *em;
4902 struct map_lookup *map;
4903 struct extent_map_tree *em_tree = &map_tree->map_tree;
4906 read_lock(&em_tree->lock);
4907 em = lookup_extent_mapping(em_tree, logical, len);
4908 read_unlock(&em_tree->lock);
4911 * We could return errors for these cases, but that could get ugly and
4912 * we'd probably do the same thing which is just not do anything else
4913 * and exit, so return 1 so the callers don't try to use other copies.
4916 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
4921 if (em->start > logical || em->start + em->len < logical) {
4922 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
4923 "%Lu-%Lu", logical, logical+len, em->start,
4924 em->start + em->len);
4925 free_extent_map(em);
4929 map = (struct map_lookup *)em->bdev;
4930 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4931 ret = map->num_stripes;
4932 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4933 ret = map->sub_stripes;
4934 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4936 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4940 free_extent_map(em);
4942 btrfs_dev_replace_lock(&fs_info->dev_replace);
4943 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4945 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4950 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4951 struct btrfs_mapping_tree *map_tree,
4954 struct extent_map *em;
4955 struct map_lookup *map;
4956 struct extent_map_tree *em_tree = &map_tree->map_tree;
4957 unsigned long len = root->sectorsize;
4959 read_lock(&em_tree->lock);
4960 em = lookup_extent_mapping(em_tree, logical, len);
4961 read_unlock(&em_tree->lock);
4964 BUG_ON(em->start > logical || em->start + em->len < logical);
4965 map = (struct map_lookup *)em->bdev;
4966 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
4967 len = map->stripe_len * nr_data_stripes(map);
4968 free_extent_map(em);
4972 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4973 u64 logical, u64 len, int mirror_num)
4975 struct extent_map *em;
4976 struct map_lookup *map;
4977 struct extent_map_tree *em_tree = &map_tree->map_tree;
4980 read_lock(&em_tree->lock);
4981 em = lookup_extent_mapping(em_tree, logical, len);
4982 read_unlock(&em_tree->lock);
4985 BUG_ON(em->start > logical || em->start + em->len < logical);
4986 map = (struct map_lookup *)em->bdev;
4987 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
4989 free_extent_map(em);
4993 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4994 struct map_lookup *map, int first, int num,
4995 int optimal, int dev_replace_is_ongoing)
4999 struct btrfs_device *srcdev;
5001 if (dev_replace_is_ongoing &&
5002 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5003 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5004 srcdev = fs_info->dev_replace.srcdev;
5009 * try to avoid the drive that is the source drive for a
5010 * dev-replace procedure, only choose it if no other non-missing
5011 * mirror is available
5013 for (tolerance = 0; tolerance < 2; tolerance++) {
5014 if (map->stripes[optimal].dev->bdev &&
5015 (tolerance || map->stripes[optimal].dev != srcdev))
5017 for (i = first; i < first + num; i++) {
5018 if (map->stripes[i].dev->bdev &&
5019 (tolerance || map->stripes[i].dev != srcdev))
5024 /* we couldn't find one that doesn't fail. Just return something
5025 * and the io error handling code will clean up eventually
5030 static inline int parity_smaller(u64 a, u64 b)
5035 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5036 static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
5038 struct btrfs_bio_stripe s;
5045 for (i = 0; i < num_stripes - 1; i++) {
5046 if (parity_smaller(bbio->raid_map[i],
5047 bbio->raid_map[i+1])) {
5048 s = bbio->stripes[i];
5049 l = bbio->raid_map[i];
5050 bbio->stripes[i] = bbio->stripes[i+1];
5051 bbio->raid_map[i] = bbio->raid_map[i+1];
5052 bbio->stripes[i+1] = s;
5053 bbio->raid_map[i+1] = l;
5061 static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5063 struct btrfs_bio *bbio = kzalloc(
5064 /* the size of the btrfs_bio */
5065 sizeof(struct btrfs_bio) +
5066 /* plus the variable array for the stripes */
5067 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
5068 /* plus the variable array for the tgt dev */
5069 sizeof(int) * (real_stripes) +
5071 * plus the raid_map, which includes both the tgt dev
5074 sizeof(u64) * (total_stripes),
5075 GFP_NOFS|__GFP_NOFAIL);
5077 atomic_set(&bbio->error, 0);
5078 atomic_set(&bbio->refs, 1);
5083 void btrfs_get_bbio(struct btrfs_bio *bbio)
5085 WARN_ON(!atomic_read(&bbio->refs));
5086 atomic_inc(&bbio->refs);
5089 void btrfs_put_bbio(struct btrfs_bio *bbio)
5093 if (atomic_dec_and_test(&bbio->refs))
5097 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5098 u64 logical, u64 *length,
5099 struct btrfs_bio **bbio_ret,
5100 int mirror_num, int need_raid_map)
5102 struct extent_map *em;
5103 struct map_lookup *map;
5104 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
5105 struct extent_map_tree *em_tree = &map_tree->map_tree;
5108 u64 stripe_end_offset;
5118 int tgtdev_indexes = 0;
5119 struct btrfs_bio *bbio = NULL;
5120 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5121 int dev_replace_is_ongoing = 0;
5122 int num_alloc_stripes;
5123 int patch_the_first_stripe_for_dev_replace = 0;
5124 u64 physical_to_patch_in_first_stripe = 0;
5125 u64 raid56_full_stripe_start = (u64)-1;
5127 read_lock(&em_tree->lock);
5128 em = lookup_extent_mapping(em_tree, logical, *length);
5129 read_unlock(&em_tree->lock);
5132 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
5137 if (em->start > logical || em->start + em->len < logical) {
5138 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
5139 "found %Lu-%Lu", logical, em->start,
5140 em->start + em->len);
5141 free_extent_map(em);
5145 map = (struct map_lookup *)em->bdev;
5146 offset = logical - em->start;
5148 stripe_len = map->stripe_len;
5151 * stripe_nr counts the total number of stripes we have to stride
5152 * to get to this block
5154 stripe_nr = div64_u64(stripe_nr, stripe_len);
5156 stripe_offset = stripe_nr * stripe_len;
5157 BUG_ON(offset < stripe_offset);
5159 /* stripe_offset is the offset of this block in its stripe*/
5160 stripe_offset = offset - stripe_offset;
5162 /* if we're here for raid56, we need to know the stripe aligned start */
5163 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5164 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5165 raid56_full_stripe_start = offset;
5167 /* allow a write of a full stripe, but make sure we don't
5168 * allow straddling of stripes
5170 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5172 raid56_full_stripe_start *= full_stripe_len;
5175 if (rw & REQ_DISCARD) {
5176 /* we don't discard raid56 yet */
5177 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5181 *length = min_t(u64, em->len - offset, *length);
5182 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5184 /* For writes to RAID[56], allow a full stripeset across all disks.
5185 For other RAID types and for RAID[56] reads, just allow a single
5186 stripe (on a single disk). */
5187 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
5189 max_len = stripe_len * nr_data_stripes(map) -
5190 (offset - raid56_full_stripe_start);
5192 /* we limit the length of each bio to what fits in a stripe */
5193 max_len = stripe_len - stripe_offset;
5195 *length = min_t(u64, em->len - offset, max_len);
5197 *length = em->len - offset;
5200 /* This is for when we're called from btrfs_merge_bio_hook() and all
5201 it cares about is the length */
5205 btrfs_dev_replace_lock(dev_replace);
5206 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5207 if (!dev_replace_is_ongoing)
5208 btrfs_dev_replace_unlock(dev_replace);
5210 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5211 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5212 dev_replace->tgtdev != NULL) {
5214 * in dev-replace case, for repair case (that's the only
5215 * case where the mirror is selected explicitly when
5216 * calling btrfs_map_block), blocks left of the left cursor
5217 * can also be read from the target drive.
5218 * For REQ_GET_READ_MIRRORS, the target drive is added as
5219 * the last one to the array of stripes. For READ, it also
5220 * needs to be supported using the same mirror number.
5221 * If the requested block is not left of the left cursor,
5222 * EIO is returned. This can happen because btrfs_num_copies()
5223 * returns one more in the dev-replace case.
5225 u64 tmp_length = *length;
5226 struct btrfs_bio *tmp_bbio = NULL;
5227 int tmp_num_stripes;
5228 u64 srcdev_devid = dev_replace->srcdev->devid;
5229 int index_srcdev = 0;
5231 u64 physical_of_found = 0;
5233 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
5234 logical, &tmp_length, &tmp_bbio, 0, 0);
5236 WARN_ON(tmp_bbio != NULL);
5240 tmp_num_stripes = tmp_bbio->num_stripes;
5241 if (mirror_num > tmp_num_stripes) {
5243 * REQ_GET_READ_MIRRORS does not contain this
5244 * mirror, that means that the requested area
5245 * is not left of the left cursor
5248 btrfs_put_bbio(tmp_bbio);
5253 * process the rest of the function using the mirror_num
5254 * of the source drive. Therefore look it up first.
5255 * At the end, patch the device pointer to the one of the
5258 for (i = 0; i < tmp_num_stripes; i++) {
5259 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5261 * In case of DUP, in order to keep it
5262 * simple, only add the mirror with the
5263 * lowest physical address
5266 physical_of_found <=
5267 tmp_bbio->stripes[i].physical)
5272 tmp_bbio->stripes[i].physical;
5277 mirror_num = index_srcdev + 1;
5278 patch_the_first_stripe_for_dev_replace = 1;
5279 physical_to_patch_in_first_stripe = physical_of_found;
5283 btrfs_put_bbio(tmp_bbio);
5287 btrfs_put_bbio(tmp_bbio);
5288 } else if (mirror_num > map->num_stripes) {
5294 stripe_nr_orig = stripe_nr;
5295 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
5296 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
5297 stripe_end_offset = stripe_nr_end * map->stripe_len -
5300 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5301 if (rw & REQ_DISCARD)
5302 num_stripes = min_t(u64, map->num_stripes,
5303 stripe_nr_end - stripe_nr_orig);
5304 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5306 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5308 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
5309 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
5310 num_stripes = map->num_stripes;
5311 else if (mirror_num)
5312 stripe_index = mirror_num - 1;
5314 stripe_index = find_live_mirror(fs_info, map, 0,
5316 current->pid % map->num_stripes,
5317 dev_replace_is_ongoing);
5318 mirror_num = stripe_index + 1;
5321 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
5322 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
5323 num_stripes = map->num_stripes;
5324 } else if (mirror_num) {
5325 stripe_index = mirror_num - 1;
5330 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5331 u32 factor = map->num_stripes / map->sub_stripes;
5333 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5334 stripe_index *= map->sub_stripes;
5336 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5337 num_stripes = map->sub_stripes;
5338 else if (rw & REQ_DISCARD)
5339 num_stripes = min_t(u64, map->sub_stripes *
5340 (stripe_nr_end - stripe_nr_orig),
5342 else if (mirror_num)
5343 stripe_index += mirror_num - 1;
5345 int old_stripe_index = stripe_index;
5346 stripe_index = find_live_mirror(fs_info, map,
5348 map->sub_stripes, stripe_index +
5349 current->pid % map->sub_stripes,
5350 dev_replace_is_ongoing);
5351 mirror_num = stripe_index - old_stripe_index + 1;
5354 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5355 if (need_raid_map &&
5356 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5358 /* push stripe_nr back to the start of the full stripe */
5359 stripe_nr = div_u64(raid56_full_stripe_start,
5360 stripe_len * nr_data_stripes(map));
5362 /* RAID[56] write or recovery. Return all stripes */
5363 num_stripes = map->num_stripes;
5364 max_errors = nr_parity_stripes(map);
5366 *length = map->stripe_len;
5371 * Mirror #0 or #1 means the original data block.
5372 * Mirror #2 is RAID5 parity block.
5373 * Mirror #3 is RAID6 Q block.
5375 stripe_nr = div_u64_rem(stripe_nr,
5376 nr_data_stripes(map), &stripe_index);
5378 stripe_index = nr_data_stripes(map) +
5381 /* We distribute the parity blocks across stripes */
5382 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5384 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5385 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5390 * after this, stripe_nr is the number of stripes on this
5391 * device we have to walk to find the data, and stripe_index is
5392 * the number of our device in the stripe array
5394 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5396 mirror_num = stripe_index + 1;
5398 BUG_ON(stripe_index >= map->num_stripes);
5400 num_alloc_stripes = num_stripes;
5401 if (dev_replace_is_ongoing) {
5402 if (rw & (REQ_WRITE | REQ_DISCARD))
5403 num_alloc_stripes <<= 1;
5404 if (rw & REQ_GET_READ_MIRRORS)
5405 num_alloc_stripes++;
5406 tgtdev_indexes = num_stripes;
5409 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
5414 if (dev_replace_is_ongoing)
5415 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
5417 /* build raid_map */
5418 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
5419 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5424 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5425 sizeof(struct btrfs_bio_stripe) *
5427 sizeof(int) * tgtdev_indexes);
5429 /* Work out the disk rotation on this stripe-set */
5430 div_u64_rem(stripe_nr, num_stripes, &rot);
5432 /* Fill in the logical address of each stripe */
5433 tmp = stripe_nr * nr_data_stripes(map);
5434 for (i = 0; i < nr_data_stripes(map); i++)
5435 bbio->raid_map[(i+rot) % num_stripes] =
5436 em->start + (tmp + i) * map->stripe_len;
5438 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5439 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5440 bbio->raid_map[(i+rot+1) % num_stripes] =
5444 if (rw & REQ_DISCARD) {
5446 u32 sub_stripes = 0;
5447 u64 stripes_per_dev = 0;
5448 u32 remaining_stripes = 0;
5449 u32 last_stripe = 0;
5452 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5453 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5456 sub_stripes = map->sub_stripes;
5458 factor = map->num_stripes / sub_stripes;
5459 stripes_per_dev = div_u64_rem(stripe_nr_end -
5462 &remaining_stripes);
5463 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5464 last_stripe *= sub_stripes;
5467 for (i = 0; i < num_stripes; i++) {
5468 bbio->stripes[i].physical =
5469 map->stripes[stripe_index].physical +
5470 stripe_offset + stripe_nr * map->stripe_len;
5471 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5473 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5474 BTRFS_BLOCK_GROUP_RAID10)) {
5475 bbio->stripes[i].length = stripes_per_dev *
5478 if (i / sub_stripes < remaining_stripes)
5479 bbio->stripes[i].length +=
5483 * Special for the first stripe and
5486 * |-------|...|-------|
5490 if (i < sub_stripes)
5491 bbio->stripes[i].length -=
5494 if (stripe_index >= last_stripe &&
5495 stripe_index <= (last_stripe +
5497 bbio->stripes[i].length -=
5500 if (i == sub_stripes - 1)
5503 bbio->stripes[i].length = *length;
5506 if (stripe_index == map->num_stripes) {
5507 /* This could only happen for RAID0/10 */
5513 for (i = 0; i < num_stripes; i++) {
5514 bbio->stripes[i].physical =
5515 map->stripes[stripe_index].physical +
5517 stripe_nr * map->stripe_len;
5518 bbio->stripes[i].dev =
5519 map->stripes[stripe_index].dev;
5524 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5525 max_errors = btrfs_chunk_max_errors(map);
5528 sort_parity_stripes(bbio, num_stripes);
5531 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5532 dev_replace->tgtdev != NULL) {
5533 int index_where_to_add;
5534 u64 srcdev_devid = dev_replace->srcdev->devid;
5537 * duplicate the write operations while the dev replace
5538 * procedure is running. Since the copying of the old disk
5539 * to the new disk takes place at run time while the
5540 * filesystem is mounted writable, the regular write
5541 * operations to the old disk have to be duplicated to go
5542 * to the new disk as well.
5543 * Note that device->missing is handled by the caller, and
5544 * that the write to the old disk is already set up in the
5547 index_where_to_add = num_stripes;
5548 for (i = 0; i < num_stripes; i++) {
5549 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5550 /* write to new disk, too */
5551 struct btrfs_bio_stripe *new =
5552 bbio->stripes + index_where_to_add;
5553 struct btrfs_bio_stripe *old =
5556 new->physical = old->physical;
5557 new->length = old->length;
5558 new->dev = dev_replace->tgtdev;
5559 bbio->tgtdev_map[i] = index_where_to_add;
5560 index_where_to_add++;
5565 num_stripes = index_where_to_add;
5566 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5567 dev_replace->tgtdev != NULL) {
5568 u64 srcdev_devid = dev_replace->srcdev->devid;
5569 int index_srcdev = 0;
5571 u64 physical_of_found = 0;
5574 * During the dev-replace procedure, the target drive can
5575 * also be used to read data in case it is needed to repair
5576 * a corrupt block elsewhere. This is possible if the
5577 * requested area is left of the left cursor. In this area,
5578 * the target drive is a full copy of the source drive.
5580 for (i = 0; i < num_stripes; i++) {
5581 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5583 * In case of DUP, in order to keep it
5584 * simple, only add the mirror with the
5585 * lowest physical address
5588 physical_of_found <=
5589 bbio->stripes[i].physical)
5593 physical_of_found = bbio->stripes[i].physical;
5597 if (physical_of_found + map->stripe_len <=
5598 dev_replace->cursor_left) {
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;
5615 bbio->map_type = map->type;
5616 bbio->num_stripes = num_stripes;
5617 bbio->max_errors = max_errors;
5618 bbio->mirror_num = mirror_num;
5619 bbio->num_tgtdevs = tgtdev_indexes;
5622 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5623 * mirror_num == num_stripes + 1 && dev_replace target drive is
5624 * available as a mirror
5626 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5627 WARN_ON(num_stripes > 1);
5628 bbio->stripes[0].dev = dev_replace->tgtdev;
5629 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5630 bbio->mirror_num = map->num_stripes + 1;
5633 if (dev_replace_is_ongoing)
5634 btrfs_dev_replace_unlock(dev_replace);
5635 free_extent_map(em);
5639 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5640 u64 logical, u64 *length,
5641 struct btrfs_bio **bbio_ret, int mirror_num)
5643 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5647 /* For Scrub/replace */
5648 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5649 u64 logical, u64 *length,
5650 struct btrfs_bio **bbio_ret, int mirror_num,
5653 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5654 mirror_num, need_raid_map);
5657 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5658 u64 chunk_start, u64 physical, u64 devid,
5659 u64 **logical, int *naddrs, int *stripe_len)
5661 struct extent_map_tree *em_tree = &map_tree->map_tree;
5662 struct extent_map *em;
5663 struct map_lookup *map;
5671 read_lock(&em_tree->lock);
5672 em = lookup_extent_mapping(em_tree, chunk_start, 1);
5673 read_unlock(&em_tree->lock);
5676 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
5681 if (em->start != chunk_start) {
5682 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5683 em->start, chunk_start);
5684 free_extent_map(em);
5687 map = (struct map_lookup *)em->bdev;
5690 rmap_len = map->stripe_len;
5692 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5693 length = div_u64(length, map->num_stripes / map->sub_stripes);
5694 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5695 length = div_u64(length, map->num_stripes);
5696 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5697 length = div_u64(length, nr_data_stripes(map));
5698 rmap_len = map->stripe_len * nr_data_stripes(map);
5701 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
5702 BUG_ON(!buf); /* -ENOMEM */
5704 for (i = 0; i < map->num_stripes; i++) {
5705 if (devid && map->stripes[i].dev->devid != devid)
5707 if (map->stripes[i].physical > physical ||
5708 map->stripes[i].physical + length <= physical)
5711 stripe_nr = physical - map->stripes[i].physical;
5712 stripe_nr = div_u64(stripe_nr, map->stripe_len);
5714 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5715 stripe_nr = stripe_nr * map->num_stripes + i;
5716 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
5717 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5718 stripe_nr = stripe_nr * map->num_stripes + i;
5719 } /* else if RAID[56], multiply by nr_data_stripes().
5720 * Alternatively, just use rmap_len below instead of
5721 * map->stripe_len */
5723 bytenr = chunk_start + stripe_nr * rmap_len;
5724 WARN_ON(nr >= map->num_stripes);
5725 for (j = 0; j < nr; j++) {
5726 if (buf[j] == bytenr)
5730 WARN_ON(nr >= map->num_stripes);
5737 *stripe_len = rmap_len;
5739 free_extent_map(em);
5743 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
5745 bio->bi_private = bbio->private;
5746 bio->bi_end_io = bbio->end_io;
5749 btrfs_put_bbio(bbio);
5752 static void btrfs_end_bio(struct bio *bio)
5754 struct btrfs_bio *bbio = bio->bi_private;
5755 int is_orig_bio = 0;
5757 if (bio->bi_error) {
5758 atomic_inc(&bbio->error);
5759 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
5760 unsigned int stripe_index =
5761 btrfs_io_bio(bio)->stripe_index;
5762 struct btrfs_device *dev;
5764 BUG_ON(stripe_index >= bbio->num_stripes);
5765 dev = bbio->stripes[stripe_index].dev;
5767 if (bio->bi_rw & WRITE)
5768 btrfs_dev_stat_inc(dev,
5769 BTRFS_DEV_STAT_WRITE_ERRS);
5771 btrfs_dev_stat_inc(dev,
5772 BTRFS_DEV_STAT_READ_ERRS);
5773 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5774 btrfs_dev_stat_inc(dev,
5775 BTRFS_DEV_STAT_FLUSH_ERRS);
5776 btrfs_dev_stat_print_on_error(dev);
5781 if (bio == bbio->orig_bio)
5784 btrfs_bio_counter_dec(bbio->fs_info);
5786 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5789 bio = bbio->orig_bio;
5792 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5793 /* only send an error to the higher layers if it is
5794 * beyond the tolerance of the btrfs bio
5796 if (atomic_read(&bbio->error) > bbio->max_errors) {
5797 bio->bi_error = -EIO;
5800 * this bio is actually up to date, we didn't
5801 * go over the max number of errors
5806 btrfs_end_bbio(bbio, bio);
5807 } else if (!is_orig_bio) {
5813 * see run_scheduled_bios for a description of why bios are collected for
5816 * This will add one bio to the pending list for a device and make sure
5817 * the work struct is scheduled.
5819 static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5820 struct btrfs_device *device,
5821 int rw, struct bio *bio)
5823 int should_queue = 1;
5824 struct btrfs_pending_bios *pending_bios;
5826 if (device->missing || !device->bdev) {
5831 /* don't bother with additional async steps for reads, right now */
5832 if (!(rw & REQ_WRITE)) {
5834 btrfsic_submit_bio(rw, bio);
5840 * nr_async_bios allows us to reliably return congestion to the
5841 * higher layers. Otherwise, the async bio makes it appear we have
5842 * made progress against dirty pages when we've really just put it
5843 * on a queue for later
5845 atomic_inc(&root->fs_info->nr_async_bios);
5846 WARN_ON(bio->bi_next);
5847 bio->bi_next = NULL;
5850 spin_lock(&device->io_lock);
5851 if (bio->bi_rw & REQ_SYNC)
5852 pending_bios = &device->pending_sync_bios;
5854 pending_bios = &device->pending_bios;
5856 if (pending_bios->tail)
5857 pending_bios->tail->bi_next = bio;
5859 pending_bios->tail = bio;
5860 if (!pending_bios->head)
5861 pending_bios->head = bio;
5862 if (device->running_pending)
5865 spin_unlock(&device->io_lock);
5868 btrfs_queue_work(root->fs_info->submit_workers,
5872 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5873 struct bio *bio, u64 physical, int dev_nr,
5876 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5878 bio->bi_private = bbio;
5879 btrfs_io_bio(bio)->stripe_index = dev_nr;
5880 bio->bi_end_io = btrfs_end_bio;
5881 bio->bi_iter.bi_sector = physical >> 9;
5884 struct rcu_string *name;
5887 name = rcu_dereference(dev->name);
5888 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5889 "(%s id %llu), size=%u\n", rw,
5890 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5891 name->str, dev->devid, bio->bi_iter.bi_size);
5895 bio->bi_bdev = dev->bdev;
5897 btrfs_bio_counter_inc_noblocked(root->fs_info);
5900 btrfs_schedule_bio(root, dev, rw, bio);
5902 btrfsic_submit_bio(rw, bio);
5905 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5907 atomic_inc(&bbio->error);
5908 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5909 /* Shoud be the original bio. */
5910 WARN_ON(bio != bbio->orig_bio);
5912 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5913 bio->bi_iter.bi_sector = logical >> 9;
5914 bio->bi_error = -EIO;
5915 btrfs_end_bbio(bbio, bio);
5919 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5920 int mirror_num, int async_submit)
5922 struct btrfs_device *dev;
5923 struct bio *first_bio = bio;
5924 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
5930 struct btrfs_bio *bbio = NULL;
5932 length = bio->bi_iter.bi_size;
5933 map_length = length;
5935 btrfs_bio_counter_inc_blocked(root->fs_info);
5936 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5939 btrfs_bio_counter_dec(root->fs_info);
5943 total_devs = bbio->num_stripes;
5944 bbio->orig_bio = first_bio;
5945 bbio->private = first_bio->bi_private;
5946 bbio->end_io = first_bio->bi_end_io;
5947 bbio->fs_info = root->fs_info;
5948 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5950 if (bbio->raid_map) {
5951 /* In this case, map_length has been set to the length of
5952 a single stripe; not the whole write */
5954 ret = raid56_parity_write(root, bio, bbio, map_length);
5956 ret = raid56_parity_recover(root, bio, bbio, map_length,
5960 btrfs_bio_counter_dec(root->fs_info);
5964 if (map_length < length) {
5965 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5966 logical, length, map_length);
5970 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
5971 dev = bbio->stripes[dev_nr].dev;
5972 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5973 bbio_error(bbio, first_bio, logical);
5977 if (dev_nr < total_devs - 1) {
5978 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
5979 BUG_ON(!bio); /* -ENOMEM */
5983 submit_stripe_bio(root, bbio, bio,
5984 bbio->stripes[dev_nr].physical, dev_nr, rw,
5987 btrfs_bio_counter_dec(root->fs_info);
5991 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5994 struct btrfs_device *device;
5995 struct btrfs_fs_devices *cur_devices;
5997 cur_devices = fs_info->fs_devices;
5998 while (cur_devices) {
6000 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6001 device = __find_device(&cur_devices->devices,
6006 cur_devices = cur_devices->seed;
6011 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
6012 struct btrfs_fs_devices *fs_devices,
6013 u64 devid, u8 *dev_uuid)
6015 struct btrfs_device *device;
6017 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6021 list_add(&device->dev_list, &fs_devices->devices);
6022 device->fs_devices = fs_devices;
6023 fs_devices->num_devices++;
6025 device->missing = 1;
6026 fs_devices->missing_devices++;
6032 * btrfs_alloc_device - allocate struct btrfs_device
6033 * @fs_info: used only for generating a new devid, can be NULL if
6034 * devid is provided (i.e. @devid != NULL).
6035 * @devid: a pointer to devid for this device. If NULL a new devid
6037 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6040 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6041 * on error. Returned struct is not linked onto any lists and can be
6042 * destroyed with kfree() right away.
6044 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6048 struct btrfs_device *dev;
6051 if (WARN_ON(!devid && !fs_info))
6052 return ERR_PTR(-EINVAL);
6054 dev = __alloc_device();
6063 ret = find_next_devid(fs_info, &tmp);
6066 return ERR_PTR(ret);
6072 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6074 generate_random_uuid(dev->uuid);
6076 btrfs_init_work(&dev->work, btrfs_submit_helper,
6077 pending_bios_fn, NULL, NULL);
6082 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6083 struct extent_buffer *leaf,
6084 struct btrfs_chunk *chunk)
6086 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6087 struct map_lookup *map;
6088 struct extent_map *em;
6092 u8 uuid[BTRFS_UUID_SIZE];
6097 logical = key->offset;
6098 length = btrfs_chunk_length(leaf, chunk);
6100 read_lock(&map_tree->map_tree.lock);
6101 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
6102 read_unlock(&map_tree->map_tree.lock);
6104 /* already mapped? */
6105 if (em && em->start <= logical && em->start + em->len > logical) {
6106 free_extent_map(em);
6109 free_extent_map(em);
6112 em = alloc_extent_map();
6115 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6116 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6118 free_extent_map(em);
6122 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6123 em->bdev = (struct block_device *)map;
6124 em->start = logical;
6127 em->block_start = 0;
6128 em->block_len = em->len;
6130 map->num_stripes = num_stripes;
6131 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6132 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6133 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6134 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6135 map->type = btrfs_chunk_type(leaf, chunk);
6136 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6137 for (i = 0; i < num_stripes; i++) {
6138 map->stripes[i].physical =
6139 btrfs_stripe_offset_nr(leaf, chunk, i);
6140 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6141 read_extent_buffer(leaf, uuid, (unsigned long)
6142 btrfs_stripe_dev_uuid_nr(chunk, i),
6144 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6146 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
6147 free_extent_map(em);
6150 if (!map->stripes[i].dev) {
6151 map->stripes[i].dev =
6152 add_missing_dev(root, root->fs_info->fs_devices,
6154 if (!map->stripes[i].dev) {
6155 free_extent_map(em);
6158 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6161 map->stripes[i].dev->in_fs_metadata = 1;
6164 write_lock(&map_tree->map_tree.lock);
6165 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
6166 write_unlock(&map_tree->map_tree.lock);
6167 BUG_ON(ret); /* Tree corruption */
6168 free_extent_map(em);
6173 static void fill_device_from_item(struct extent_buffer *leaf,
6174 struct btrfs_dev_item *dev_item,
6175 struct btrfs_device *device)
6179 device->devid = btrfs_device_id(leaf, dev_item);
6180 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6181 device->total_bytes = device->disk_total_bytes;
6182 device->commit_total_bytes = device->disk_total_bytes;
6183 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6184 device->commit_bytes_used = device->bytes_used;
6185 device->type = btrfs_device_type(leaf, dev_item);
6186 device->io_align = btrfs_device_io_align(leaf, dev_item);
6187 device->io_width = btrfs_device_io_width(leaf, dev_item);
6188 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6189 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6190 device->is_tgtdev_for_dev_replace = 0;
6192 ptr = btrfs_device_uuid(dev_item);
6193 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6196 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6199 struct btrfs_fs_devices *fs_devices;
6202 BUG_ON(!mutex_is_locked(&uuid_mutex));
6204 fs_devices = root->fs_info->fs_devices->seed;
6205 while (fs_devices) {
6206 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6209 fs_devices = fs_devices->seed;
6212 fs_devices = find_fsid(fsid);
6214 if (!btrfs_test_opt(root, DEGRADED))
6215 return ERR_PTR(-ENOENT);
6217 fs_devices = alloc_fs_devices(fsid);
6218 if (IS_ERR(fs_devices))
6221 fs_devices->seeding = 1;
6222 fs_devices->opened = 1;
6226 fs_devices = clone_fs_devices(fs_devices);
6227 if (IS_ERR(fs_devices))
6230 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
6231 root->fs_info->bdev_holder);
6233 free_fs_devices(fs_devices);
6234 fs_devices = ERR_PTR(ret);
6238 if (!fs_devices->seeding) {
6239 __btrfs_close_devices(fs_devices);
6240 free_fs_devices(fs_devices);
6241 fs_devices = ERR_PTR(-EINVAL);
6245 fs_devices->seed = root->fs_info->fs_devices->seed;
6246 root->fs_info->fs_devices->seed = fs_devices;
6251 static int read_one_dev(struct btrfs_root *root,
6252 struct extent_buffer *leaf,
6253 struct btrfs_dev_item *dev_item)
6255 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6256 struct btrfs_device *device;
6259 u8 fs_uuid[BTRFS_UUID_SIZE];
6260 u8 dev_uuid[BTRFS_UUID_SIZE];
6262 devid = btrfs_device_id(leaf, dev_item);
6263 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
6265 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6268 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6269 fs_devices = open_seed_devices(root, fs_uuid);
6270 if (IS_ERR(fs_devices))
6271 return PTR_ERR(fs_devices);
6274 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
6276 if (!btrfs_test_opt(root, DEGRADED))
6279 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6282 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6285 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6288 if(!device->bdev && !device->missing) {
6290 * this happens when a device that was properly setup
6291 * in the device info lists suddenly goes bad.
6292 * device->bdev is NULL, and so we have to set
6293 * device->missing to one here
6295 device->fs_devices->missing_devices++;
6296 device->missing = 1;
6299 /* Move the device to its own fs_devices */
6300 if (device->fs_devices != fs_devices) {
6301 ASSERT(device->missing);
6303 list_move(&device->dev_list, &fs_devices->devices);
6304 device->fs_devices->num_devices--;
6305 fs_devices->num_devices++;
6307 device->fs_devices->missing_devices--;
6308 fs_devices->missing_devices++;
6310 device->fs_devices = fs_devices;
6314 if (device->fs_devices != root->fs_info->fs_devices) {
6315 BUG_ON(device->writeable);
6316 if (device->generation !=
6317 btrfs_device_generation(leaf, dev_item))
6321 fill_device_from_item(leaf, dev_item, device);
6322 device->in_fs_metadata = 1;
6323 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
6324 device->fs_devices->total_rw_bytes += device->total_bytes;
6325 spin_lock(&root->fs_info->free_chunk_lock);
6326 root->fs_info->free_chunk_space += device->total_bytes -
6328 spin_unlock(&root->fs_info->free_chunk_lock);
6334 int btrfs_read_sys_array(struct btrfs_root *root)
6336 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
6337 struct extent_buffer *sb;
6338 struct btrfs_disk_key *disk_key;
6339 struct btrfs_chunk *chunk;
6341 unsigned long sb_array_offset;
6347 struct btrfs_key key;
6349 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6351 * This will create extent buffer of nodesize, superblock size is
6352 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6353 * overallocate but we can keep it as-is, only the first page is used.
6355 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
6358 btrfs_set_buffer_uptodate(sb);
6359 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6361 * The sb extent buffer is artifical and just used to read the system array.
6362 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6363 * pages up-to-date when the page is larger: extent does not cover the
6364 * whole page and consequently check_page_uptodate does not find all
6365 * the page's extents up-to-date (the hole beyond sb),
6366 * write_extent_buffer then triggers a WARN_ON.
6368 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6369 * but sb spans only this function. Add an explicit SetPageUptodate call
6370 * to silence the warning eg. on PowerPC 64.
6372 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
6373 SetPageUptodate(sb->pages[0]);
6375 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
6376 array_size = btrfs_super_sys_array_size(super_copy);
6378 array_ptr = super_copy->sys_chunk_array;
6379 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6382 while (cur_offset < array_size) {
6383 disk_key = (struct btrfs_disk_key *)array_ptr;
6384 len = sizeof(*disk_key);
6385 if (cur_offset + len > array_size)
6386 goto out_short_read;
6388 btrfs_disk_key_to_cpu(&key, disk_key);
6391 sb_array_offset += len;
6394 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
6395 chunk = (struct btrfs_chunk *)sb_array_offset;
6397 * At least one btrfs_chunk with one stripe must be
6398 * present, exact stripe count check comes afterwards
6400 len = btrfs_chunk_item_size(1);
6401 if (cur_offset + len > array_size)
6402 goto out_short_read;
6404 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6405 len = btrfs_chunk_item_size(num_stripes);
6406 if (cur_offset + len > array_size)
6407 goto out_short_read;
6409 ret = read_one_chunk(root, &key, sb, chunk);
6417 sb_array_offset += len;
6420 free_extent_buffer(sb);
6424 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6426 free_extent_buffer(sb);
6430 int btrfs_read_chunk_tree(struct btrfs_root *root)
6432 struct btrfs_path *path;
6433 struct extent_buffer *leaf;
6434 struct btrfs_key key;
6435 struct btrfs_key found_key;
6439 root = root->fs_info->chunk_root;
6441 path = btrfs_alloc_path();
6445 mutex_lock(&uuid_mutex);
6449 * Read all device items, and then all the chunk items. All
6450 * device items are found before any chunk item (their object id
6451 * is smaller than the lowest possible object id for a chunk
6452 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6454 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6457 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6461 leaf = path->nodes[0];
6462 slot = path->slots[0];
6463 if (slot >= btrfs_header_nritems(leaf)) {
6464 ret = btrfs_next_leaf(root, path);
6471 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6472 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6473 struct btrfs_dev_item *dev_item;
6474 dev_item = btrfs_item_ptr(leaf, slot,
6475 struct btrfs_dev_item);
6476 ret = read_one_dev(root, leaf, dev_item);
6479 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6480 struct btrfs_chunk *chunk;
6481 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6482 ret = read_one_chunk(root, &found_key, leaf, chunk);
6490 unlock_chunks(root);
6491 mutex_unlock(&uuid_mutex);
6493 btrfs_free_path(path);
6497 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6499 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6500 struct btrfs_device *device;
6502 while (fs_devices) {
6503 mutex_lock(&fs_devices->device_list_mutex);
6504 list_for_each_entry(device, &fs_devices->devices, dev_list)
6505 device->dev_root = fs_info->dev_root;
6506 mutex_unlock(&fs_devices->device_list_mutex);
6508 fs_devices = fs_devices->seed;
6512 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6516 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6517 btrfs_dev_stat_reset(dev, i);
6520 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6522 struct btrfs_key key;
6523 struct btrfs_key found_key;
6524 struct btrfs_root *dev_root = fs_info->dev_root;
6525 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6526 struct extent_buffer *eb;
6529 struct btrfs_device *device;
6530 struct btrfs_path *path = NULL;
6533 path = btrfs_alloc_path();
6539 mutex_lock(&fs_devices->device_list_mutex);
6540 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6542 struct btrfs_dev_stats_item *ptr;
6545 key.type = BTRFS_DEV_STATS_KEY;
6546 key.offset = device->devid;
6547 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6549 __btrfs_reset_dev_stats(device);
6550 device->dev_stats_valid = 1;
6551 btrfs_release_path(path);
6554 slot = path->slots[0];
6555 eb = path->nodes[0];
6556 btrfs_item_key_to_cpu(eb, &found_key, slot);
6557 item_size = btrfs_item_size_nr(eb, slot);
6559 ptr = btrfs_item_ptr(eb, slot,
6560 struct btrfs_dev_stats_item);
6562 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6563 if (item_size >= (1 + i) * sizeof(__le64))
6564 btrfs_dev_stat_set(device, i,
6565 btrfs_dev_stats_value(eb, ptr, i));
6567 btrfs_dev_stat_reset(device, i);
6570 device->dev_stats_valid = 1;
6571 btrfs_dev_stat_print_on_load(device);
6572 btrfs_release_path(path);
6574 mutex_unlock(&fs_devices->device_list_mutex);
6577 btrfs_free_path(path);
6578 return ret < 0 ? ret : 0;
6581 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6582 struct btrfs_root *dev_root,
6583 struct btrfs_device *device)
6585 struct btrfs_path *path;
6586 struct btrfs_key key;
6587 struct extent_buffer *eb;
6588 struct btrfs_dev_stats_item *ptr;
6593 key.type = BTRFS_DEV_STATS_KEY;
6594 key.offset = device->devid;
6596 path = btrfs_alloc_path();
6598 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6600 printk_in_rcu(KERN_WARNING "BTRFS: "
6601 "error %d while searching for dev_stats item for device %s!\n",
6602 ret, rcu_str_deref(device->name));
6607 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6608 /* need to delete old one and insert a new one */
6609 ret = btrfs_del_item(trans, dev_root, path);
6611 printk_in_rcu(KERN_WARNING "BTRFS: "
6612 "delete too small dev_stats item for device %s failed %d!\n",
6613 rcu_str_deref(device->name), ret);
6620 /* need to insert a new item */
6621 btrfs_release_path(path);
6622 ret = btrfs_insert_empty_item(trans, dev_root, path,
6623 &key, sizeof(*ptr));
6625 printk_in_rcu(KERN_WARNING "BTRFS: "
6626 "insert dev_stats item for device %s failed %d!\n",
6627 rcu_str_deref(device->name), ret);
6632 eb = path->nodes[0];
6633 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6634 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6635 btrfs_set_dev_stats_value(eb, ptr, i,
6636 btrfs_dev_stat_read(device, i));
6637 btrfs_mark_buffer_dirty(eb);
6640 btrfs_free_path(path);
6645 * called from commit_transaction. Writes all changed device stats to disk.
6647 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6648 struct btrfs_fs_info *fs_info)
6650 struct btrfs_root *dev_root = fs_info->dev_root;
6651 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6652 struct btrfs_device *device;
6656 mutex_lock(&fs_devices->device_list_mutex);
6657 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6658 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
6661 stats_cnt = atomic_read(&device->dev_stats_ccnt);
6662 ret = update_dev_stat_item(trans, dev_root, device);
6664 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
6666 mutex_unlock(&fs_devices->device_list_mutex);
6671 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6673 btrfs_dev_stat_inc(dev, index);
6674 btrfs_dev_stat_print_on_error(dev);
6677 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
6679 if (!dev->dev_stats_valid)
6681 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6682 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6683 rcu_str_deref(dev->name),
6684 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6685 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6686 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6687 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6688 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6691 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6695 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6696 if (btrfs_dev_stat_read(dev, i) != 0)
6698 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6699 return; /* all values == 0, suppress message */
6701 printk_in_rcu(KERN_INFO "BTRFS: "
6702 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6703 rcu_str_deref(dev->name),
6704 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6705 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6706 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6707 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6708 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6711 int btrfs_get_dev_stats(struct btrfs_root *root,
6712 struct btrfs_ioctl_get_dev_stats *stats)
6714 struct btrfs_device *dev;
6715 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6718 mutex_lock(&fs_devices->device_list_mutex);
6719 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
6720 mutex_unlock(&fs_devices->device_list_mutex);
6723 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
6725 } else if (!dev->dev_stats_valid) {
6726 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
6728 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
6729 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6730 if (stats->nr_items > i)
6732 btrfs_dev_stat_read_and_reset(dev, i);
6734 btrfs_dev_stat_reset(dev, i);
6737 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6738 if (stats->nr_items > i)
6739 stats->values[i] = btrfs_dev_stat_read(dev, i);
6741 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6742 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6746 int btrfs_scratch_superblock(struct btrfs_device *device)
6748 struct buffer_head *bh;
6749 struct btrfs_super_block *disk_super;
6751 bh = btrfs_read_dev_super(device->bdev);
6754 disk_super = (struct btrfs_super_block *)bh->b_data;
6756 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6757 set_buffer_dirty(bh);
6758 sync_dirty_buffer(bh);
6765 * Update the size of all devices, which is used for writing out the
6768 void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6770 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6771 struct btrfs_device *curr, *next;
6773 if (list_empty(&fs_devices->resized_devices))
6776 mutex_lock(&fs_devices->device_list_mutex);
6777 lock_chunks(fs_info->dev_root);
6778 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6780 list_del_init(&curr->resized_list);
6781 curr->commit_total_bytes = curr->disk_total_bytes;
6783 unlock_chunks(fs_info->dev_root);
6784 mutex_unlock(&fs_devices->device_list_mutex);
6787 /* Must be invoked during the transaction commit */
6788 void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6789 struct btrfs_transaction *transaction)
6791 struct extent_map *em;
6792 struct map_lookup *map;
6793 struct btrfs_device *dev;
6796 if (list_empty(&transaction->pending_chunks))
6799 /* In order to kick the device replace finish process */
6801 list_for_each_entry(em, &transaction->pending_chunks, list) {
6802 map = (struct map_lookup *)em->bdev;
6804 for (i = 0; i < map->num_stripes; i++) {
6805 dev = map->stripes[i].dev;
6806 dev->commit_bytes_used = dev->bytes_used;
6809 unlock_chunks(root);
6812 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6814 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6815 while (fs_devices) {
6816 fs_devices->fs_info = fs_info;
6817 fs_devices = fs_devices->seed;
6821 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6823 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6824 while (fs_devices) {
6825 fs_devices->fs_info = NULL;
6826 fs_devices = fs_devices->seed;