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);
205 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
206 ret = set_blocksize(*bdev, 4096);
208 blkdev_put(*bdev, flags);
211 invalidate_bdev(*bdev);
212 *bh = btrfs_read_dev_super(*bdev);
215 blkdev_put(*bdev, flags);
227 static void requeue_list(struct btrfs_pending_bios *pending_bios,
228 struct bio *head, struct bio *tail)
231 struct bio *old_head;
233 old_head = pending_bios->head;
234 pending_bios->head = head;
235 if (pending_bios->tail)
236 tail->bi_next = old_head;
238 pending_bios->tail = tail;
242 * we try to collect pending bios for a device so we don't get a large
243 * number of procs sending bios down to the same device. This greatly
244 * improves the schedulers ability to collect and merge the bios.
246 * But, it also turns into a long list of bios to process and that is sure
247 * to eventually make the worker thread block. The solution here is to
248 * make some progress and then put this work struct back at the end of
249 * the list if the block device is congested. This way, multiple devices
250 * can make progress from a single worker thread.
252 static noinline void run_scheduled_bios(struct btrfs_device *device)
255 struct backing_dev_info *bdi;
256 struct btrfs_fs_info *fs_info;
257 struct btrfs_pending_bios *pending_bios;
261 unsigned long num_run;
262 unsigned long batch_run = 0;
264 unsigned long last_waited = 0;
266 int sync_pending = 0;
267 struct blk_plug plug;
270 * this function runs all the bios we've collected for
271 * a particular device. We don't want to wander off to
272 * another device without first sending all of these down.
273 * So, setup a plug here and finish it off before we return
275 blk_start_plug(&plug);
277 bdi = blk_get_backing_dev_info(device->bdev);
278 fs_info = device->dev_root->fs_info;
279 limit = btrfs_async_submit_limit(fs_info);
280 limit = limit * 2 / 3;
283 spin_lock(&device->io_lock);
288 /* take all the bios off the list at once and process them
289 * later on (without the lock held). But, remember the
290 * tail and other pointers so the bios can be properly reinserted
291 * into the list if we hit congestion
293 if (!force_reg && device->pending_sync_bios.head) {
294 pending_bios = &device->pending_sync_bios;
297 pending_bios = &device->pending_bios;
301 pending = pending_bios->head;
302 tail = pending_bios->tail;
303 WARN_ON(pending && !tail);
306 * if pending was null this time around, no bios need processing
307 * at all and we can stop. Otherwise it'll loop back up again
308 * and do an additional check so no bios are missed.
310 * device->running_pending is used to synchronize with the
313 if (device->pending_sync_bios.head == NULL &&
314 device->pending_bios.head == NULL) {
316 device->running_pending = 0;
319 device->running_pending = 1;
322 pending_bios->head = NULL;
323 pending_bios->tail = NULL;
325 spin_unlock(&device->io_lock);
330 /* we want to work on both lists, but do more bios on the
331 * sync list than the regular list
334 pending_bios != &device->pending_sync_bios &&
335 device->pending_sync_bios.head) ||
336 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
337 device->pending_bios.head)) {
338 spin_lock(&device->io_lock);
339 requeue_list(pending_bios, pending, tail);
344 pending = pending->bi_next;
347 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
348 waitqueue_active(&fs_info->async_submit_wait))
349 wake_up(&fs_info->async_submit_wait);
351 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
354 * if we're doing the sync list, record that our
355 * plug has some sync requests on it
357 * If we're doing the regular list and there are
358 * sync requests sitting around, unplug before
361 if (pending_bios == &device->pending_sync_bios) {
363 } else if (sync_pending) {
364 blk_finish_plug(&plug);
365 blk_start_plug(&plug);
369 btrfsic_submit_bio(cur->bi_rw, cur);
376 * we made progress, there is more work to do and the bdi
377 * is now congested. Back off and let other work structs
380 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
381 fs_info->fs_devices->open_devices > 1) {
382 struct io_context *ioc;
384 ioc = current->io_context;
387 * the main goal here is that we don't want to
388 * block if we're going to be able to submit
389 * more requests without blocking.
391 * This code does two great things, it pokes into
392 * the elevator code from a filesystem _and_
393 * it makes assumptions about how batching works.
395 if (ioc && ioc->nr_batch_requests > 0 &&
396 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
398 ioc->last_waited == last_waited)) {
400 * we want to go through our batch of
401 * requests and stop. So, we copy out
402 * the ioc->last_waited time and test
403 * against it before looping
405 last_waited = ioc->last_waited;
409 spin_lock(&device->io_lock);
410 requeue_list(pending_bios, pending, tail);
411 device->running_pending = 1;
413 spin_unlock(&device->io_lock);
414 btrfs_queue_work(fs_info->submit_workers,
418 /* unplug every 64 requests just for good measure */
419 if (batch_run % 64 == 0) {
420 blk_finish_plug(&plug);
421 blk_start_plug(&plug);
430 spin_lock(&device->io_lock);
431 if (device->pending_bios.head || device->pending_sync_bios.head)
433 spin_unlock(&device->io_lock);
436 blk_finish_plug(&plug);
439 static void pending_bios_fn(struct btrfs_work *work)
441 struct btrfs_device *device;
443 device = container_of(work, struct btrfs_device, work);
444 run_scheduled_bios(device);
448 void btrfs_free_stale_device(struct btrfs_device *cur_dev)
450 struct btrfs_fs_devices *fs_devs;
451 struct btrfs_device *dev;
456 list_for_each_entry(fs_devs, &fs_uuids, list) {
461 if (fs_devs->seeding)
464 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
472 * Todo: This won't be enough. What if the same device
473 * comes back (with new uuid and) with its mapper path?
474 * But for now, this does help as mostly an admin will
475 * either use mapper or non mapper path throughout.
478 del = strcmp(rcu_str_deref(dev->name),
479 rcu_str_deref(cur_dev->name));
486 /* delete the stale device */
487 if (fs_devs->num_devices == 1) {
488 btrfs_sysfs_remove_fsid(fs_devs);
489 list_del(&fs_devs->list);
490 free_fs_devices(fs_devs);
492 fs_devs->num_devices--;
493 list_del(&dev->dev_list);
494 rcu_string_free(dev->name);
503 * Add new device to list of registered devices
506 * 1 - first time device is seen
507 * 0 - device already known
510 static noinline int device_list_add(const char *path,
511 struct btrfs_super_block *disk_super,
512 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
514 struct btrfs_device *device;
515 struct btrfs_fs_devices *fs_devices;
516 struct rcu_string *name;
518 u64 found_transid = btrfs_super_generation(disk_super);
520 fs_devices = find_fsid(disk_super->fsid);
522 fs_devices = alloc_fs_devices(disk_super->fsid);
523 if (IS_ERR(fs_devices))
524 return PTR_ERR(fs_devices);
526 list_add(&fs_devices->list, &fs_uuids);
530 device = __find_device(&fs_devices->devices, devid,
531 disk_super->dev_item.uuid);
535 if (fs_devices->opened)
538 device = btrfs_alloc_device(NULL, &devid,
539 disk_super->dev_item.uuid);
540 if (IS_ERR(device)) {
541 /* we can safely leave the fs_devices entry around */
542 return PTR_ERR(device);
545 name = rcu_string_strdup(path, GFP_NOFS);
550 rcu_assign_pointer(device->name, name);
552 mutex_lock(&fs_devices->device_list_mutex);
553 list_add_rcu(&device->dev_list, &fs_devices->devices);
554 fs_devices->num_devices++;
555 mutex_unlock(&fs_devices->device_list_mutex);
558 device->fs_devices = fs_devices;
559 } else if (!device->name || strcmp(device->name->str, path)) {
561 * When FS is already mounted.
562 * 1. If you are here and if the device->name is NULL that
563 * means this device was missing at time of FS mount.
564 * 2. If you are here and if the device->name is different
565 * from 'path' that means either
566 * a. The same device disappeared and reappeared with
568 * b. The missing-disk-which-was-replaced, has
571 * We must allow 1 and 2a above. But 2b would be a spurious
574 * Further in case of 1 and 2a above, the disk at 'path'
575 * would have missed some transaction when it was away and
576 * in case of 2a the stale bdev has to be updated as well.
577 * 2b must not be allowed at all time.
581 * For now, we do allow update to btrfs_fs_device through the
582 * btrfs dev scan cli after FS has been mounted. We're still
583 * tracking a problem where systems fail mount by subvolume id
584 * when we reject replacement on a mounted FS.
586 if (!fs_devices->opened && found_transid < device->generation) {
588 * That is if the FS is _not_ mounted and if you
589 * are here, that means there is more than one
590 * disk with same uuid and devid.We keep the one
591 * with larger generation number or the last-in if
592 * generation are equal.
597 name = rcu_string_strdup(path, GFP_NOFS);
600 rcu_string_free(device->name);
601 rcu_assign_pointer(device->name, name);
602 if (device->missing) {
603 fs_devices->missing_devices--;
609 * Unmount does not free the btrfs_device struct but would zero
610 * generation along with most of the other members. So just update
611 * it back. We need it to pick the disk with largest generation
614 if (!fs_devices->opened)
615 device->generation = found_transid;
618 * if there is new btrfs on an already registered device,
619 * then remove the stale device entry.
621 btrfs_free_stale_device(device);
623 *fs_devices_ret = fs_devices;
628 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
630 struct btrfs_fs_devices *fs_devices;
631 struct btrfs_device *device;
632 struct btrfs_device *orig_dev;
634 fs_devices = alloc_fs_devices(orig->fsid);
635 if (IS_ERR(fs_devices))
638 mutex_lock(&orig->device_list_mutex);
639 fs_devices->total_devices = orig->total_devices;
641 /* We have held the volume lock, it is safe to get the devices. */
642 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
643 struct rcu_string *name;
645 device = btrfs_alloc_device(NULL, &orig_dev->devid,
651 * This is ok to do without rcu read locked because we hold the
652 * uuid mutex so nothing we touch in here is going to disappear.
654 if (orig_dev->name) {
655 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
660 rcu_assign_pointer(device->name, name);
663 list_add(&device->dev_list, &fs_devices->devices);
664 device->fs_devices = fs_devices;
665 fs_devices->num_devices++;
667 mutex_unlock(&orig->device_list_mutex);
670 mutex_unlock(&orig->device_list_mutex);
671 free_fs_devices(fs_devices);
672 return ERR_PTR(-ENOMEM);
675 void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
677 struct btrfs_device *device, *next;
678 struct btrfs_device *latest_dev = NULL;
680 mutex_lock(&uuid_mutex);
682 /* This is the initialized path, it is safe to release the devices. */
683 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
684 if (device->in_fs_metadata) {
685 if (!device->is_tgtdev_for_dev_replace &&
687 device->generation > latest_dev->generation)) {
693 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
695 * In the first step, keep the device which has
696 * the correct fsid and the devid that is used
697 * for the dev_replace procedure.
698 * In the second step, the dev_replace state is
699 * read from the device tree and it is known
700 * whether the procedure is really active or
701 * not, which means whether this device is
702 * used or whether it should be removed.
704 if (step == 0 || device->is_tgtdev_for_dev_replace) {
709 blkdev_put(device->bdev, device->mode);
711 fs_devices->open_devices--;
713 if (device->writeable) {
714 list_del_init(&device->dev_alloc_list);
715 device->writeable = 0;
716 if (!device->is_tgtdev_for_dev_replace)
717 fs_devices->rw_devices--;
719 list_del_init(&device->dev_list);
720 fs_devices->num_devices--;
721 rcu_string_free(device->name);
725 if (fs_devices->seed) {
726 fs_devices = fs_devices->seed;
730 fs_devices->latest_bdev = latest_dev->bdev;
732 mutex_unlock(&uuid_mutex);
735 static void __free_device(struct work_struct *work)
737 struct btrfs_device *device;
739 device = container_of(work, struct btrfs_device, rcu_work);
742 blkdev_put(device->bdev, device->mode);
744 rcu_string_free(device->name);
748 static void free_device(struct rcu_head *head)
750 struct btrfs_device *device;
752 device = container_of(head, struct btrfs_device, rcu);
754 INIT_WORK(&device->rcu_work, __free_device);
755 schedule_work(&device->rcu_work);
758 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
760 struct btrfs_device *device, *tmp;
762 if (--fs_devices->opened > 0)
765 mutex_lock(&fs_devices->device_list_mutex);
766 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
767 btrfs_close_one_device(device);
769 mutex_unlock(&fs_devices->device_list_mutex);
771 WARN_ON(fs_devices->open_devices);
772 WARN_ON(fs_devices->rw_devices);
773 fs_devices->opened = 0;
774 fs_devices->seeding = 0;
779 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
781 struct btrfs_fs_devices *seed_devices = NULL;
784 mutex_lock(&uuid_mutex);
785 ret = __btrfs_close_devices(fs_devices);
786 if (!fs_devices->opened) {
787 seed_devices = fs_devices->seed;
788 fs_devices->seed = NULL;
790 mutex_unlock(&uuid_mutex);
792 while (seed_devices) {
793 fs_devices = seed_devices;
794 seed_devices = fs_devices->seed;
795 __btrfs_close_devices(fs_devices);
796 free_fs_devices(fs_devices);
799 * Wait for rcu kworkers under __btrfs_close_devices
800 * to finish all blkdev_puts so device is really
801 * free when umount is done.
807 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
808 fmode_t flags, void *holder)
810 struct request_queue *q;
811 struct block_device *bdev;
812 struct list_head *head = &fs_devices->devices;
813 struct btrfs_device *device;
814 struct btrfs_device *latest_dev = NULL;
815 struct buffer_head *bh;
816 struct btrfs_super_block *disk_super;
823 list_for_each_entry(device, head, dev_list) {
829 /* Just open everything we can; ignore failures here */
830 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
834 disk_super = (struct btrfs_super_block *)bh->b_data;
835 devid = btrfs_stack_device_id(&disk_super->dev_item);
836 if (devid != device->devid)
839 if (memcmp(device->uuid, disk_super->dev_item.uuid,
843 device->generation = btrfs_super_generation(disk_super);
845 device->generation > latest_dev->generation)
848 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
849 device->writeable = 0;
851 device->writeable = !bdev_read_only(bdev);
855 q = bdev_get_queue(bdev);
856 if (blk_queue_discard(q))
857 device->can_discard = 1;
860 device->in_fs_metadata = 0;
861 device->mode = flags;
863 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
864 fs_devices->rotating = 1;
866 fs_devices->open_devices++;
867 if (device->writeable &&
868 device->devid != BTRFS_DEV_REPLACE_DEVID) {
869 fs_devices->rw_devices++;
870 list_add(&device->dev_alloc_list,
871 &fs_devices->alloc_list);
878 blkdev_put(bdev, flags);
881 if (fs_devices->open_devices == 0) {
885 fs_devices->seeding = seeding;
886 fs_devices->opened = 1;
887 fs_devices->latest_bdev = latest_dev->bdev;
888 fs_devices->total_rw_bytes = 0;
893 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
894 fmode_t flags, void *holder)
898 mutex_lock(&uuid_mutex);
899 if (fs_devices->opened) {
900 fs_devices->opened++;
903 ret = __btrfs_open_devices(fs_devices, flags, holder);
905 mutex_unlock(&uuid_mutex);
910 * Look for a btrfs signature on a device. This may be called out of the mount path
911 * and we are not allowed to call set_blocksize during the scan. The superblock
912 * is read via pagecache
914 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
915 struct btrfs_fs_devices **fs_devices_ret)
917 struct btrfs_super_block *disk_super;
918 struct block_device *bdev;
929 * we would like to check all the supers, but that would make
930 * a btrfs mount succeed after a mkfs from a different FS.
931 * So, we need to add a special mount option to scan for
932 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
934 bytenr = btrfs_sb_offset(0);
936 mutex_lock(&uuid_mutex);
938 bdev = blkdev_get_by_path(path, flags, holder);
945 /* make sure our super fits in the device */
946 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
949 /* make sure our super fits in the page */
950 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
953 /* make sure our super doesn't straddle pages on disk */
954 index = bytenr >> PAGE_CACHE_SHIFT;
955 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
958 /* pull in the page with our super */
959 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
962 if (IS_ERR_OR_NULL(page))
967 /* align our pointer to the offset of the super block */
968 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
970 if (btrfs_super_bytenr(disk_super) != bytenr ||
971 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
974 devid = btrfs_stack_device_id(&disk_super->dev_item);
975 transid = btrfs_super_generation(disk_super);
976 total_devices = btrfs_super_num_devices(disk_super);
978 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
980 if (disk_super->label[0]) {
981 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
982 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
983 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
985 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
988 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
991 if (!ret && fs_devices_ret)
992 (*fs_devices_ret)->total_devices = total_devices;
996 page_cache_release(page);
999 blkdev_put(bdev, flags);
1001 mutex_unlock(&uuid_mutex);
1005 /* helper to account the used device space in the range */
1006 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1007 u64 end, u64 *length)
1009 struct btrfs_key key;
1010 struct btrfs_root *root = device->dev_root;
1011 struct btrfs_dev_extent *dev_extent;
1012 struct btrfs_path *path;
1016 struct extent_buffer *l;
1020 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
1023 path = btrfs_alloc_path();
1028 key.objectid = device->devid;
1030 key.type = BTRFS_DEV_EXTENT_KEY;
1032 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1036 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1043 slot = path->slots[0];
1044 if (slot >= btrfs_header_nritems(l)) {
1045 ret = btrfs_next_leaf(root, path);
1053 btrfs_item_key_to_cpu(l, &key, slot);
1055 if (key.objectid < device->devid)
1058 if (key.objectid > device->devid)
1061 if (key.type != BTRFS_DEV_EXTENT_KEY)
1064 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1065 extent_end = key.offset + btrfs_dev_extent_length(l,
1067 if (key.offset <= start && extent_end > end) {
1068 *length = end - start + 1;
1070 } else if (key.offset <= start && extent_end > start)
1071 *length += extent_end - start;
1072 else if (key.offset > start && extent_end <= end)
1073 *length += extent_end - key.offset;
1074 else if (key.offset > start && key.offset <= end) {
1075 *length += end - key.offset + 1;
1077 } else if (key.offset > end)
1085 btrfs_free_path(path);
1089 static int contains_pending_extent(struct btrfs_transaction *transaction,
1090 struct btrfs_device *device,
1091 u64 *start, u64 len)
1093 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
1094 struct extent_map *em;
1095 struct list_head *search_list = &fs_info->pinned_chunks;
1097 u64 physical_start = *start;
1100 search_list = &transaction->pending_chunks;
1102 list_for_each_entry(em, search_list, list) {
1103 struct map_lookup *map;
1106 map = (struct map_lookup *)em->bdev;
1107 for (i = 0; i < map->num_stripes; i++) {
1110 if (map->stripes[i].dev != device)
1112 if (map->stripes[i].physical >= physical_start + len ||
1113 map->stripes[i].physical + em->orig_block_len <=
1117 * Make sure that while processing the pinned list we do
1118 * not override our *start with a lower value, because
1119 * we can have pinned chunks that fall within this
1120 * device hole and that have lower physical addresses
1121 * than the pending chunks we processed before. If we
1122 * do not take this special care we can end up getting
1123 * 2 pending chunks that start at the same physical
1124 * device offsets because the end offset of a pinned
1125 * chunk can be equal to the start offset of some
1128 end = map->stripes[i].physical + em->orig_block_len;
1135 if (search_list != &fs_info->pinned_chunks) {
1136 search_list = &fs_info->pinned_chunks;
1145 * find_free_dev_extent_start - find free space in the specified device
1146 * @device: the device which we search the free space in
1147 * @num_bytes: the size of the free space that we need
1148 * @search_start: the position from which to begin the search
1149 * @start: store the start of the free space.
1150 * @len: the size of the free space. that we find, or the size
1151 * of the max free space if we don't find suitable free space
1153 * this uses a pretty simple search, the expectation is that it is
1154 * called very infrequently and that a given device has a small number
1157 * @start is used to store the start of the free space if we find. But if we
1158 * don't find suitable free space, it will be used to store the start position
1159 * of the max free space.
1161 * @len is used to store the size of the free space that we find.
1162 * But if we don't find suitable free space, it is used to store the size of
1163 * the max free space.
1165 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1166 struct btrfs_device *device, u64 num_bytes,
1167 u64 search_start, u64 *start, u64 *len)
1169 struct btrfs_key key;
1170 struct btrfs_root *root = device->dev_root;
1171 struct btrfs_dev_extent *dev_extent;
1172 struct btrfs_path *path;
1177 u64 search_end = device->total_bytes;
1180 struct extent_buffer *l;
1182 path = btrfs_alloc_path();
1186 max_hole_start = search_start;
1190 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1196 path->search_commit_root = 1;
1197 path->skip_locking = 1;
1199 key.objectid = device->devid;
1200 key.offset = search_start;
1201 key.type = BTRFS_DEV_EXTENT_KEY;
1203 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1207 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1214 slot = path->slots[0];
1215 if (slot >= btrfs_header_nritems(l)) {
1216 ret = btrfs_next_leaf(root, path);
1224 btrfs_item_key_to_cpu(l, &key, slot);
1226 if (key.objectid < device->devid)
1229 if (key.objectid > device->devid)
1232 if (key.type != BTRFS_DEV_EXTENT_KEY)
1235 if (key.offset > search_start) {
1236 hole_size = key.offset - search_start;
1239 * Have to check before we set max_hole_start, otherwise
1240 * we could end up sending back this offset anyway.
1242 if (contains_pending_extent(transaction, device,
1245 if (key.offset >= search_start) {
1246 hole_size = key.offset - search_start;
1253 if (hole_size > max_hole_size) {
1254 max_hole_start = search_start;
1255 max_hole_size = hole_size;
1259 * If this free space is greater than which we need,
1260 * it must be the max free space that we have found
1261 * until now, so max_hole_start must point to the start
1262 * of this free space and the length of this free space
1263 * is stored in max_hole_size. Thus, we return
1264 * max_hole_start and max_hole_size and go back to the
1267 if (hole_size >= num_bytes) {
1273 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1274 extent_end = key.offset + btrfs_dev_extent_length(l,
1276 if (extent_end > search_start)
1277 search_start = extent_end;
1284 * At this point, search_start should be the end of
1285 * allocated dev extents, and when shrinking the device,
1286 * search_end may be smaller than search_start.
1288 if (search_end > search_start) {
1289 hole_size = search_end - search_start;
1291 if (contains_pending_extent(transaction, device, &search_start,
1293 btrfs_release_path(path);
1297 if (hole_size > max_hole_size) {
1298 max_hole_start = search_start;
1299 max_hole_size = hole_size;
1304 if (max_hole_size < num_bytes)
1310 btrfs_free_path(path);
1311 *start = max_hole_start;
1313 *len = max_hole_size;
1317 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1318 struct btrfs_device *device, u64 num_bytes,
1319 u64 *start, u64 *len)
1321 struct btrfs_root *root = device->dev_root;
1324 /* FIXME use last free of some kind */
1327 * we don't want to overwrite the superblock on the drive,
1328 * so we make sure to start at an offset of at least 1MB
1330 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1331 return find_free_dev_extent_start(trans->transaction, device,
1332 num_bytes, search_start, start, len);
1335 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1336 struct btrfs_device *device,
1337 u64 start, u64 *dev_extent_len)
1340 struct btrfs_path *path;
1341 struct btrfs_root *root = device->dev_root;
1342 struct btrfs_key key;
1343 struct btrfs_key found_key;
1344 struct extent_buffer *leaf = NULL;
1345 struct btrfs_dev_extent *extent = NULL;
1347 path = btrfs_alloc_path();
1351 key.objectid = device->devid;
1353 key.type = BTRFS_DEV_EXTENT_KEY;
1355 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1357 ret = btrfs_previous_item(root, path, key.objectid,
1358 BTRFS_DEV_EXTENT_KEY);
1361 leaf = path->nodes[0];
1362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1363 extent = btrfs_item_ptr(leaf, path->slots[0],
1364 struct btrfs_dev_extent);
1365 BUG_ON(found_key.offset > start || found_key.offset +
1366 btrfs_dev_extent_length(leaf, extent) < start);
1368 btrfs_release_path(path);
1370 } else if (ret == 0) {
1371 leaf = path->nodes[0];
1372 extent = btrfs_item_ptr(leaf, path->slots[0],
1373 struct btrfs_dev_extent);
1375 btrfs_std_error(root->fs_info, ret, "Slot search failed");
1379 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1381 ret = btrfs_del_item(trans, root, path);
1383 btrfs_std_error(root->fs_info, ret,
1384 "Failed to remove dev extent item");
1386 trans->transaction->have_free_bgs = 1;
1389 btrfs_free_path(path);
1393 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1394 struct btrfs_device *device,
1395 u64 chunk_tree, u64 chunk_objectid,
1396 u64 chunk_offset, u64 start, u64 num_bytes)
1399 struct btrfs_path *path;
1400 struct btrfs_root *root = device->dev_root;
1401 struct btrfs_dev_extent *extent;
1402 struct extent_buffer *leaf;
1403 struct btrfs_key key;
1405 WARN_ON(!device->in_fs_metadata);
1406 WARN_ON(device->is_tgtdev_for_dev_replace);
1407 path = btrfs_alloc_path();
1411 key.objectid = device->devid;
1413 key.type = BTRFS_DEV_EXTENT_KEY;
1414 ret = btrfs_insert_empty_item(trans, root, path, &key,
1419 leaf = path->nodes[0];
1420 extent = btrfs_item_ptr(leaf, path->slots[0],
1421 struct btrfs_dev_extent);
1422 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1423 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1424 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1426 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1427 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
1429 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1430 btrfs_mark_buffer_dirty(leaf);
1432 btrfs_free_path(path);
1436 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1438 struct extent_map_tree *em_tree;
1439 struct extent_map *em;
1443 em_tree = &fs_info->mapping_tree.map_tree;
1444 read_lock(&em_tree->lock);
1445 n = rb_last(&em_tree->map);
1447 em = rb_entry(n, struct extent_map, rb_node);
1448 ret = em->start + em->len;
1450 read_unlock(&em_tree->lock);
1455 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1459 struct btrfs_key key;
1460 struct btrfs_key found_key;
1461 struct btrfs_path *path;
1463 path = btrfs_alloc_path();
1467 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1468 key.type = BTRFS_DEV_ITEM_KEY;
1469 key.offset = (u64)-1;
1471 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1475 BUG_ON(ret == 0); /* Corruption */
1477 ret = btrfs_previous_item(fs_info->chunk_root, path,
1478 BTRFS_DEV_ITEMS_OBJECTID,
1479 BTRFS_DEV_ITEM_KEY);
1483 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1485 *devid_ret = found_key.offset + 1;
1489 btrfs_free_path(path);
1494 * the device information is stored in the chunk root
1495 * the btrfs_device struct should be fully filled in
1497 static int btrfs_add_device(struct btrfs_trans_handle *trans,
1498 struct btrfs_root *root,
1499 struct btrfs_device *device)
1502 struct btrfs_path *path;
1503 struct btrfs_dev_item *dev_item;
1504 struct extent_buffer *leaf;
1505 struct btrfs_key key;
1508 root = root->fs_info->chunk_root;
1510 path = btrfs_alloc_path();
1514 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1515 key.type = BTRFS_DEV_ITEM_KEY;
1516 key.offset = device->devid;
1518 ret = btrfs_insert_empty_item(trans, root, path, &key,
1523 leaf = path->nodes[0];
1524 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1526 btrfs_set_device_id(leaf, dev_item, device->devid);
1527 btrfs_set_device_generation(leaf, dev_item, 0);
1528 btrfs_set_device_type(leaf, dev_item, device->type);
1529 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1530 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1531 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1532 btrfs_set_device_total_bytes(leaf, dev_item,
1533 btrfs_device_get_disk_total_bytes(device));
1534 btrfs_set_device_bytes_used(leaf, dev_item,
1535 btrfs_device_get_bytes_used(device));
1536 btrfs_set_device_group(leaf, dev_item, 0);
1537 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1538 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1539 btrfs_set_device_start_offset(leaf, dev_item, 0);
1541 ptr = btrfs_device_uuid(dev_item);
1542 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1543 ptr = btrfs_device_fsid(dev_item);
1544 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1545 btrfs_mark_buffer_dirty(leaf);
1549 btrfs_free_path(path);
1554 * Function to update ctime/mtime for a given device path.
1555 * Mainly used for ctime/mtime based probe like libblkid.
1557 static void update_dev_time(char *path_name)
1561 filp = filp_open(path_name, O_RDWR, 0);
1564 file_update_time(filp);
1565 filp_close(filp, NULL);
1569 static int btrfs_rm_dev_item(struct btrfs_root *root,
1570 struct btrfs_device *device)
1573 struct btrfs_path *path;
1574 struct btrfs_key key;
1575 struct btrfs_trans_handle *trans;
1577 root = root->fs_info->chunk_root;
1579 path = btrfs_alloc_path();
1583 trans = btrfs_start_transaction(root, 0);
1584 if (IS_ERR(trans)) {
1585 btrfs_free_path(path);
1586 return PTR_ERR(trans);
1588 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1589 key.type = BTRFS_DEV_ITEM_KEY;
1590 key.offset = device->devid;
1592 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1601 ret = btrfs_del_item(trans, root, path);
1605 btrfs_free_path(path);
1606 btrfs_commit_transaction(trans, root);
1610 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1612 struct btrfs_device *device;
1613 struct btrfs_device *next_device;
1614 struct block_device *bdev;
1615 struct buffer_head *bh = NULL;
1616 struct btrfs_super_block *disk_super;
1617 struct btrfs_fs_devices *cur_devices;
1624 bool clear_super = false;
1626 mutex_lock(&uuid_mutex);
1629 seq = read_seqbegin(&root->fs_info->profiles_lock);
1631 all_avail = root->fs_info->avail_data_alloc_bits |
1632 root->fs_info->avail_system_alloc_bits |
1633 root->fs_info->avail_metadata_alloc_bits;
1634 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1636 num_devices = root->fs_info->fs_devices->num_devices;
1637 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1638 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1639 WARN_ON(num_devices < 1);
1642 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1644 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1645 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
1649 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1650 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
1654 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1655 root->fs_info->fs_devices->rw_devices <= 2) {
1656 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
1659 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1660 root->fs_info->fs_devices->rw_devices <= 3) {
1661 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
1665 if (strcmp(device_path, "missing") == 0) {
1666 struct list_head *devices;
1667 struct btrfs_device *tmp;
1670 devices = &root->fs_info->fs_devices->devices;
1672 * It is safe to read the devices since the volume_mutex
1675 list_for_each_entry(tmp, devices, dev_list) {
1676 if (tmp->in_fs_metadata &&
1677 !tmp->is_tgtdev_for_dev_replace &&
1687 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1691 ret = btrfs_get_bdev_and_sb(device_path,
1692 FMODE_WRITE | FMODE_EXCL,
1693 root->fs_info->bdev_holder, 0,
1697 disk_super = (struct btrfs_super_block *)bh->b_data;
1698 devid = btrfs_stack_device_id(&disk_super->dev_item);
1699 dev_uuid = disk_super->dev_item.uuid;
1700 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1708 if (device->is_tgtdev_for_dev_replace) {
1709 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1713 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1714 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1718 if (device->writeable) {
1720 list_del_init(&device->dev_alloc_list);
1721 device->fs_devices->rw_devices--;
1722 unlock_chunks(root);
1726 mutex_unlock(&uuid_mutex);
1727 ret = btrfs_shrink_device(device, 0);
1728 mutex_lock(&uuid_mutex);
1733 * TODO: the superblock still includes this device in its num_devices
1734 * counter although write_all_supers() is not locked out. This
1735 * could give a filesystem state which requires a degraded mount.
1737 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1741 device->in_fs_metadata = 0;
1742 btrfs_scrub_cancel_dev(root->fs_info, device);
1745 * the device list mutex makes sure that we don't change
1746 * the device list while someone else is writing out all
1747 * the device supers. Whoever is writing all supers, should
1748 * lock the device list mutex before getting the number of
1749 * devices in the super block (super_copy). Conversely,
1750 * whoever updates the number of devices in the super block
1751 * (super_copy) should hold the device list mutex.
1754 cur_devices = device->fs_devices;
1755 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1756 list_del_rcu(&device->dev_list);
1758 device->fs_devices->num_devices--;
1759 device->fs_devices->total_devices--;
1761 if (device->missing)
1762 device->fs_devices->missing_devices--;
1764 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1765 struct btrfs_device, dev_list);
1766 if (device->bdev == root->fs_info->sb->s_bdev)
1767 root->fs_info->sb->s_bdev = next_device->bdev;
1768 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1769 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1772 device->fs_devices->open_devices--;
1773 /* remove sysfs entry */
1774 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
1777 call_rcu(&device->rcu, free_device);
1779 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1780 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1781 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1783 if (cur_devices->open_devices == 0) {
1784 struct btrfs_fs_devices *fs_devices;
1785 fs_devices = root->fs_info->fs_devices;
1786 while (fs_devices) {
1787 if (fs_devices->seed == cur_devices) {
1788 fs_devices->seed = cur_devices->seed;
1791 fs_devices = fs_devices->seed;
1793 cur_devices->seed = NULL;
1794 __btrfs_close_devices(cur_devices);
1795 free_fs_devices(cur_devices);
1798 root->fs_info->num_tolerated_disk_barrier_failures =
1799 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1802 * at this point, the device is zero sized. We want to
1803 * remove it from the devices list and zero out the old super
1805 if (clear_super && disk_super) {
1809 /* make sure this device isn't detected as part of
1812 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1813 set_buffer_dirty(bh);
1814 sync_dirty_buffer(bh);
1816 /* clear the mirror copies of super block on the disk
1817 * being removed, 0th copy is been taken care above and
1818 * the below would take of the rest
1820 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1821 bytenr = btrfs_sb_offset(i);
1822 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1823 i_size_read(bdev->bd_inode))
1827 bh = __bread(bdev, bytenr / 4096,
1828 BTRFS_SUPER_INFO_SIZE);
1832 disk_super = (struct btrfs_super_block *)bh->b_data;
1834 if (btrfs_super_bytenr(disk_super) != bytenr ||
1835 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1838 memset(&disk_super->magic, 0,
1839 sizeof(disk_super->magic));
1840 set_buffer_dirty(bh);
1841 sync_dirty_buffer(bh);
1848 /* Notify udev that device has changed */
1849 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1851 /* Update ctime/mtime for device path for libblkid */
1852 update_dev_time(device_path);
1858 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1860 mutex_unlock(&uuid_mutex);
1863 if (device->writeable) {
1865 list_add(&device->dev_alloc_list,
1866 &root->fs_info->fs_devices->alloc_list);
1867 device->fs_devices->rw_devices++;
1868 unlock_chunks(root);
1873 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1874 struct btrfs_device *srcdev)
1876 struct btrfs_fs_devices *fs_devices;
1878 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1881 * in case of fs with no seed, srcdev->fs_devices will point
1882 * to fs_devices of fs_info. However when the dev being replaced is
1883 * a seed dev it will point to the seed's local fs_devices. In short
1884 * srcdev will have its correct fs_devices in both the cases.
1886 fs_devices = srcdev->fs_devices;
1888 list_del_rcu(&srcdev->dev_list);
1889 list_del_rcu(&srcdev->dev_alloc_list);
1890 fs_devices->num_devices--;
1891 if (srcdev->missing)
1892 fs_devices->missing_devices--;
1894 if (srcdev->writeable) {
1895 fs_devices->rw_devices--;
1896 /* zero out the old super if it is writable */
1897 btrfs_scratch_superblocks(srcdev->bdev,
1898 rcu_str_deref(srcdev->name));
1902 fs_devices->open_devices--;
1905 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1906 struct btrfs_device *srcdev)
1908 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
1910 call_rcu(&srcdev->rcu, free_device);
1913 * unless fs_devices is seed fs, num_devices shouldn't go
1916 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1918 /* if this is no devs we rather delete the fs_devices */
1919 if (!fs_devices->num_devices) {
1920 struct btrfs_fs_devices *tmp_fs_devices;
1922 tmp_fs_devices = fs_info->fs_devices;
1923 while (tmp_fs_devices) {
1924 if (tmp_fs_devices->seed == fs_devices) {
1925 tmp_fs_devices->seed = fs_devices->seed;
1928 tmp_fs_devices = tmp_fs_devices->seed;
1930 fs_devices->seed = NULL;
1931 __btrfs_close_devices(fs_devices);
1932 free_fs_devices(fs_devices);
1936 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1937 struct btrfs_device *tgtdev)
1939 struct btrfs_device *next_device;
1941 mutex_lock(&uuid_mutex);
1943 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1945 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
1948 btrfs_scratch_superblocks(tgtdev->bdev,
1949 rcu_str_deref(tgtdev->name));
1950 fs_info->fs_devices->open_devices--;
1952 fs_info->fs_devices->num_devices--;
1954 next_device = list_entry(fs_info->fs_devices->devices.next,
1955 struct btrfs_device, dev_list);
1956 if (tgtdev->bdev == fs_info->sb->s_bdev)
1957 fs_info->sb->s_bdev = next_device->bdev;
1958 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1959 fs_info->fs_devices->latest_bdev = next_device->bdev;
1960 list_del_rcu(&tgtdev->dev_list);
1962 call_rcu(&tgtdev->rcu, free_device);
1964 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1965 mutex_unlock(&uuid_mutex);
1968 static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1969 struct btrfs_device **device)
1972 struct btrfs_super_block *disk_super;
1975 struct block_device *bdev;
1976 struct buffer_head *bh;
1979 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1980 root->fs_info->bdev_holder, 0, &bdev, &bh);
1983 disk_super = (struct btrfs_super_block *)bh->b_data;
1984 devid = btrfs_stack_device_id(&disk_super->dev_item);
1985 dev_uuid = disk_super->dev_item.uuid;
1986 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1991 blkdev_put(bdev, FMODE_READ);
1995 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1997 struct btrfs_device **device)
2000 if (strcmp(device_path, "missing") == 0) {
2001 struct list_head *devices;
2002 struct btrfs_device *tmp;
2004 devices = &root->fs_info->fs_devices->devices;
2006 * It is safe to read the devices since the volume_mutex
2007 * is held by the caller.
2009 list_for_each_entry(tmp, devices, dev_list) {
2010 if (tmp->in_fs_metadata && !tmp->bdev) {
2017 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2021 return btrfs_find_device_by_path(root, device_path, device);
2026 * does all the dirty work required for changing file system's UUID.
2028 static int btrfs_prepare_sprout(struct btrfs_root *root)
2030 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2031 struct btrfs_fs_devices *old_devices;
2032 struct btrfs_fs_devices *seed_devices;
2033 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
2034 struct btrfs_device *device;
2037 BUG_ON(!mutex_is_locked(&uuid_mutex));
2038 if (!fs_devices->seeding)
2041 seed_devices = __alloc_fs_devices();
2042 if (IS_ERR(seed_devices))
2043 return PTR_ERR(seed_devices);
2045 old_devices = clone_fs_devices(fs_devices);
2046 if (IS_ERR(old_devices)) {
2047 kfree(seed_devices);
2048 return PTR_ERR(old_devices);
2051 list_add(&old_devices->list, &fs_uuids);
2053 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2054 seed_devices->opened = 1;
2055 INIT_LIST_HEAD(&seed_devices->devices);
2056 INIT_LIST_HEAD(&seed_devices->alloc_list);
2057 mutex_init(&seed_devices->device_list_mutex);
2059 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2060 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2062 list_for_each_entry(device, &seed_devices->devices, dev_list)
2063 device->fs_devices = seed_devices;
2066 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2067 unlock_chunks(root);
2069 fs_devices->seeding = 0;
2070 fs_devices->num_devices = 0;
2071 fs_devices->open_devices = 0;
2072 fs_devices->missing_devices = 0;
2073 fs_devices->rotating = 0;
2074 fs_devices->seed = seed_devices;
2076 generate_random_uuid(fs_devices->fsid);
2077 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2078 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2079 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2081 super_flags = btrfs_super_flags(disk_super) &
2082 ~BTRFS_SUPER_FLAG_SEEDING;
2083 btrfs_set_super_flags(disk_super, super_flags);
2089 * strore the expected generation for seed devices in device items.
2091 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2092 struct btrfs_root *root)
2094 struct btrfs_path *path;
2095 struct extent_buffer *leaf;
2096 struct btrfs_dev_item *dev_item;
2097 struct btrfs_device *device;
2098 struct btrfs_key key;
2099 u8 fs_uuid[BTRFS_UUID_SIZE];
2100 u8 dev_uuid[BTRFS_UUID_SIZE];
2104 path = btrfs_alloc_path();
2108 root = root->fs_info->chunk_root;
2109 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2111 key.type = BTRFS_DEV_ITEM_KEY;
2114 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2118 leaf = path->nodes[0];
2120 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2121 ret = btrfs_next_leaf(root, path);
2126 leaf = path->nodes[0];
2127 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2128 btrfs_release_path(path);
2132 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2133 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2134 key.type != BTRFS_DEV_ITEM_KEY)
2137 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2138 struct btrfs_dev_item);
2139 devid = btrfs_device_id(leaf, dev_item);
2140 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2142 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2144 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2146 BUG_ON(!device); /* Logic error */
2148 if (device->fs_devices->seeding) {
2149 btrfs_set_device_generation(leaf, dev_item,
2150 device->generation);
2151 btrfs_mark_buffer_dirty(leaf);
2159 btrfs_free_path(path);
2163 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2165 struct request_queue *q;
2166 struct btrfs_trans_handle *trans;
2167 struct btrfs_device *device;
2168 struct block_device *bdev;
2169 struct list_head *devices;
2170 struct super_block *sb = root->fs_info->sb;
2171 struct rcu_string *name;
2173 int seeding_dev = 0;
2176 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
2179 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2180 root->fs_info->bdev_holder);
2182 return PTR_ERR(bdev);
2184 if (root->fs_info->fs_devices->seeding) {
2186 down_write(&sb->s_umount);
2187 mutex_lock(&uuid_mutex);
2190 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2192 devices = &root->fs_info->fs_devices->devices;
2194 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2195 list_for_each_entry(device, devices, dev_list) {
2196 if (device->bdev == bdev) {
2199 &root->fs_info->fs_devices->device_list_mutex);
2203 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2205 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2206 if (IS_ERR(device)) {
2207 /* we can safely leave the fs_devices entry around */
2208 ret = PTR_ERR(device);
2212 name = rcu_string_strdup(device_path, GFP_NOFS);
2218 rcu_assign_pointer(device->name, name);
2220 trans = btrfs_start_transaction(root, 0);
2221 if (IS_ERR(trans)) {
2222 rcu_string_free(device->name);
2224 ret = PTR_ERR(trans);
2228 q = bdev_get_queue(bdev);
2229 if (blk_queue_discard(q))
2230 device->can_discard = 1;
2231 device->writeable = 1;
2232 device->generation = trans->transid;
2233 device->io_width = root->sectorsize;
2234 device->io_align = root->sectorsize;
2235 device->sector_size = root->sectorsize;
2236 device->total_bytes = i_size_read(bdev->bd_inode);
2237 device->disk_total_bytes = device->total_bytes;
2238 device->commit_total_bytes = device->total_bytes;
2239 device->dev_root = root->fs_info->dev_root;
2240 device->bdev = bdev;
2241 device->in_fs_metadata = 1;
2242 device->is_tgtdev_for_dev_replace = 0;
2243 device->mode = FMODE_EXCL;
2244 device->dev_stats_valid = 1;
2245 set_blocksize(device->bdev, 4096);
2248 sb->s_flags &= ~MS_RDONLY;
2249 ret = btrfs_prepare_sprout(root);
2250 BUG_ON(ret); /* -ENOMEM */
2253 device->fs_devices = root->fs_info->fs_devices;
2255 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2257 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2258 list_add(&device->dev_alloc_list,
2259 &root->fs_info->fs_devices->alloc_list);
2260 root->fs_info->fs_devices->num_devices++;
2261 root->fs_info->fs_devices->open_devices++;
2262 root->fs_info->fs_devices->rw_devices++;
2263 root->fs_info->fs_devices->total_devices++;
2264 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2266 spin_lock(&root->fs_info->free_chunk_lock);
2267 root->fs_info->free_chunk_space += device->total_bytes;
2268 spin_unlock(&root->fs_info->free_chunk_lock);
2270 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2271 root->fs_info->fs_devices->rotating = 1;
2273 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
2274 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2275 tmp + device->total_bytes);
2277 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
2278 btrfs_set_super_num_devices(root->fs_info->super_copy,
2281 /* add sysfs device entry */
2282 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
2285 * we've got more storage, clear any full flags on the space
2288 btrfs_clear_space_info_full(root->fs_info);
2290 unlock_chunks(root);
2291 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2295 ret = init_first_rw_device(trans, root, device);
2296 unlock_chunks(root);
2298 btrfs_abort_transaction(trans, root, ret);
2303 ret = btrfs_add_device(trans, root, device);
2305 btrfs_abort_transaction(trans, root, ret);
2310 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2312 ret = btrfs_finish_sprout(trans, root);
2314 btrfs_abort_transaction(trans, root, ret);
2318 /* Sprouting would change fsid of the mounted root,
2319 * so rename the fsid on the sysfs
2321 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2322 root->fs_info->fsid);
2323 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
2325 btrfs_warn(root->fs_info,
2326 "sysfs: failed to create fsid for sprout");
2329 root->fs_info->num_tolerated_disk_barrier_failures =
2330 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2331 ret = btrfs_commit_transaction(trans, root);
2334 mutex_unlock(&uuid_mutex);
2335 up_write(&sb->s_umount);
2337 if (ret) /* transaction commit */
2340 ret = btrfs_relocate_sys_chunks(root);
2342 btrfs_std_error(root->fs_info, ret,
2343 "Failed to relocate sys chunks after "
2344 "device initialization. This can be fixed "
2345 "using the \"btrfs balance\" command.");
2346 trans = btrfs_attach_transaction(root);
2347 if (IS_ERR(trans)) {
2348 if (PTR_ERR(trans) == -ENOENT)
2350 return PTR_ERR(trans);
2352 ret = btrfs_commit_transaction(trans, root);
2355 /* Update ctime/mtime for libblkid */
2356 update_dev_time(device_path);
2360 btrfs_end_transaction(trans, root);
2361 rcu_string_free(device->name);
2362 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
2365 blkdev_put(bdev, FMODE_EXCL);
2367 mutex_unlock(&uuid_mutex);
2368 up_write(&sb->s_umount);
2373 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2374 struct btrfs_device *srcdev,
2375 struct btrfs_device **device_out)
2377 struct request_queue *q;
2378 struct btrfs_device *device;
2379 struct block_device *bdev;
2380 struct btrfs_fs_info *fs_info = root->fs_info;
2381 struct list_head *devices;
2382 struct rcu_string *name;
2383 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2387 if (fs_info->fs_devices->seeding) {
2388 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
2392 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2393 fs_info->bdev_holder);
2395 btrfs_err(fs_info, "target device %s is invalid!", device_path);
2396 return PTR_ERR(bdev);
2399 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2401 devices = &fs_info->fs_devices->devices;
2402 list_for_each_entry(device, devices, dev_list) {
2403 if (device->bdev == bdev) {
2404 btrfs_err(fs_info, "target device is in the filesystem!");
2411 if (i_size_read(bdev->bd_inode) <
2412 btrfs_device_get_total_bytes(srcdev)) {
2413 btrfs_err(fs_info, "target device is smaller than source device!");
2419 device = btrfs_alloc_device(NULL, &devid, NULL);
2420 if (IS_ERR(device)) {
2421 ret = PTR_ERR(device);
2425 name = rcu_string_strdup(device_path, GFP_NOFS);
2431 rcu_assign_pointer(device->name, name);
2433 q = bdev_get_queue(bdev);
2434 if (blk_queue_discard(q))
2435 device->can_discard = 1;
2436 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2437 device->writeable = 1;
2438 device->generation = 0;
2439 device->io_width = root->sectorsize;
2440 device->io_align = root->sectorsize;
2441 device->sector_size = root->sectorsize;
2442 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2443 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2444 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
2445 ASSERT(list_empty(&srcdev->resized_list));
2446 device->commit_total_bytes = srcdev->commit_total_bytes;
2447 device->commit_bytes_used = device->bytes_used;
2448 device->dev_root = fs_info->dev_root;
2449 device->bdev = bdev;
2450 device->in_fs_metadata = 1;
2451 device->is_tgtdev_for_dev_replace = 1;
2452 device->mode = FMODE_EXCL;
2453 device->dev_stats_valid = 1;
2454 set_blocksize(device->bdev, 4096);
2455 device->fs_devices = fs_info->fs_devices;
2456 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2457 fs_info->fs_devices->num_devices++;
2458 fs_info->fs_devices->open_devices++;
2459 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2461 *device_out = device;
2465 blkdev_put(bdev, FMODE_EXCL);
2469 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2470 struct btrfs_device *tgtdev)
2472 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2473 tgtdev->io_width = fs_info->dev_root->sectorsize;
2474 tgtdev->io_align = fs_info->dev_root->sectorsize;
2475 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2476 tgtdev->dev_root = fs_info->dev_root;
2477 tgtdev->in_fs_metadata = 1;
2480 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2481 struct btrfs_device *device)
2484 struct btrfs_path *path;
2485 struct btrfs_root *root;
2486 struct btrfs_dev_item *dev_item;
2487 struct extent_buffer *leaf;
2488 struct btrfs_key key;
2490 root = device->dev_root->fs_info->chunk_root;
2492 path = btrfs_alloc_path();
2496 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2497 key.type = BTRFS_DEV_ITEM_KEY;
2498 key.offset = device->devid;
2500 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2509 leaf = path->nodes[0];
2510 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2512 btrfs_set_device_id(leaf, dev_item, device->devid);
2513 btrfs_set_device_type(leaf, dev_item, device->type);
2514 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2515 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2516 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2517 btrfs_set_device_total_bytes(leaf, dev_item,
2518 btrfs_device_get_disk_total_bytes(device));
2519 btrfs_set_device_bytes_used(leaf, dev_item,
2520 btrfs_device_get_bytes_used(device));
2521 btrfs_mark_buffer_dirty(leaf);
2524 btrfs_free_path(path);
2528 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2529 struct btrfs_device *device, u64 new_size)
2531 struct btrfs_super_block *super_copy =
2532 device->dev_root->fs_info->super_copy;
2533 struct btrfs_fs_devices *fs_devices;
2537 if (!device->writeable)
2540 lock_chunks(device->dev_root);
2541 old_total = btrfs_super_total_bytes(super_copy);
2542 diff = new_size - device->total_bytes;
2544 if (new_size <= device->total_bytes ||
2545 device->is_tgtdev_for_dev_replace) {
2546 unlock_chunks(device->dev_root);
2550 fs_devices = device->dev_root->fs_info->fs_devices;
2552 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2553 device->fs_devices->total_rw_bytes += diff;
2555 btrfs_device_set_total_bytes(device, new_size);
2556 btrfs_device_set_disk_total_bytes(device, new_size);
2557 btrfs_clear_space_info_full(device->dev_root->fs_info);
2558 if (list_empty(&device->resized_list))
2559 list_add_tail(&device->resized_list,
2560 &fs_devices->resized_devices);
2561 unlock_chunks(device->dev_root);
2563 return btrfs_update_device(trans, device);
2566 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2567 struct btrfs_root *root, u64 chunk_objectid,
2571 struct btrfs_path *path;
2572 struct btrfs_key key;
2574 root = root->fs_info->chunk_root;
2575 path = btrfs_alloc_path();
2579 key.objectid = chunk_objectid;
2580 key.offset = chunk_offset;
2581 key.type = BTRFS_CHUNK_ITEM_KEY;
2583 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2586 else if (ret > 0) { /* Logic error or corruption */
2587 btrfs_std_error(root->fs_info, -ENOENT,
2588 "Failed lookup while freeing chunk.");
2593 ret = btrfs_del_item(trans, root, path);
2595 btrfs_std_error(root->fs_info, ret,
2596 "Failed to delete chunk item.");
2598 btrfs_free_path(path);
2602 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2605 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2606 struct btrfs_disk_key *disk_key;
2607 struct btrfs_chunk *chunk;
2614 struct btrfs_key key;
2617 array_size = btrfs_super_sys_array_size(super_copy);
2619 ptr = super_copy->sys_chunk_array;
2622 while (cur < array_size) {
2623 disk_key = (struct btrfs_disk_key *)ptr;
2624 btrfs_disk_key_to_cpu(&key, disk_key);
2626 len = sizeof(*disk_key);
2628 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2629 chunk = (struct btrfs_chunk *)(ptr + len);
2630 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2631 len += btrfs_chunk_item_size(num_stripes);
2636 if (key.objectid == chunk_objectid &&
2637 key.offset == chunk_offset) {
2638 memmove(ptr, ptr + len, array_size - (cur + len));
2640 btrfs_set_super_sys_array_size(super_copy, array_size);
2646 unlock_chunks(root);
2650 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2651 struct btrfs_root *root, u64 chunk_offset)
2653 struct extent_map_tree *em_tree;
2654 struct extent_map *em;
2655 struct btrfs_root *extent_root = root->fs_info->extent_root;
2656 struct map_lookup *map;
2657 u64 dev_extent_len = 0;
2658 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2662 root = root->fs_info->chunk_root;
2663 em_tree = &root->fs_info->mapping_tree.map_tree;
2665 read_lock(&em_tree->lock);
2666 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2667 read_unlock(&em_tree->lock);
2669 if (!em || em->start > chunk_offset ||
2670 em->start + em->len < chunk_offset) {
2672 * This is a logic error, but we don't want to just rely on the
2673 * user having built with ASSERT enabled, so if ASSERT doens't
2674 * do anything we still error out.
2678 free_extent_map(em);
2681 map = (struct map_lookup *)em->bdev;
2682 lock_chunks(root->fs_info->chunk_root);
2683 check_system_chunk(trans, extent_root, map->type);
2684 unlock_chunks(root->fs_info->chunk_root);
2686 for (i = 0; i < map->num_stripes; i++) {
2687 struct btrfs_device *device = map->stripes[i].dev;
2688 ret = btrfs_free_dev_extent(trans, device,
2689 map->stripes[i].physical,
2692 btrfs_abort_transaction(trans, root, ret);
2696 if (device->bytes_used > 0) {
2698 btrfs_device_set_bytes_used(device,
2699 device->bytes_used - dev_extent_len);
2700 spin_lock(&root->fs_info->free_chunk_lock);
2701 root->fs_info->free_chunk_space += dev_extent_len;
2702 spin_unlock(&root->fs_info->free_chunk_lock);
2703 btrfs_clear_space_info_full(root->fs_info);
2704 unlock_chunks(root);
2707 if (map->stripes[i].dev) {
2708 ret = btrfs_update_device(trans, map->stripes[i].dev);
2710 btrfs_abort_transaction(trans, root, ret);
2715 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
2717 btrfs_abort_transaction(trans, root, ret);
2721 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2723 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2724 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2726 btrfs_abort_transaction(trans, root, ret);
2731 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
2733 btrfs_abort_transaction(trans, extent_root, ret);
2739 free_extent_map(em);
2743 static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
2745 struct btrfs_root *extent_root;
2746 struct btrfs_trans_handle *trans;
2749 root = root->fs_info->chunk_root;
2750 extent_root = root->fs_info->extent_root;
2753 * Prevent races with automatic removal of unused block groups.
2754 * After we relocate and before we remove the chunk with offset
2755 * chunk_offset, automatic removal of the block group can kick in,
2756 * resulting in a failure when calling btrfs_remove_chunk() below.
2758 * Make sure to acquire this mutex before doing a tree search (dev
2759 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2760 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2761 * we release the path used to search the chunk/dev tree and before
2762 * the current task acquires this mutex and calls us.
2764 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2766 ret = btrfs_can_relocate(extent_root, chunk_offset);
2770 /* step one, relocate all the extents inside this chunk */
2771 btrfs_scrub_pause(root);
2772 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2773 btrfs_scrub_continue(root);
2777 trans = btrfs_start_transaction(root, 0);
2778 if (IS_ERR(trans)) {
2779 ret = PTR_ERR(trans);
2780 btrfs_std_error(root->fs_info, ret, NULL);
2785 * step two, delete the device extents and the
2786 * chunk tree entries
2788 ret = btrfs_remove_chunk(trans, root, chunk_offset);
2789 btrfs_end_transaction(trans, root);
2793 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2795 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2796 struct btrfs_path *path;
2797 struct extent_buffer *leaf;
2798 struct btrfs_chunk *chunk;
2799 struct btrfs_key key;
2800 struct btrfs_key found_key;
2802 bool retried = false;
2806 path = btrfs_alloc_path();
2811 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2812 key.offset = (u64)-1;
2813 key.type = BTRFS_CHUNK_ITEM_KEY;
2816 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
2817 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2819 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
2822 BUG_ON(ret == 0); /* Corruption */
2824 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2827 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
2833 leaf = path->nodes[0];
2834 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2836 chunk = btrfs_item_ptr(leaf, path->slots[0],
2837 struct btrfs_chunk);
2838 chunk_type = btrfs_chunk_type(leaf, chunk);
2839 btrfs_release_path(path);
2841 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2842 ret = btrfs_relocate_chunk(chunk_root,
2849 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
2851 if (found_key.offset == 0)
2853 key.offset = found_key.offset - 1;
2856 if (failed && !retried) {
2860 } else if (WARN_ON(failed && retried)) {
2864 btrfs_free_path(path);
2868 static int insert_balance_item(struct btrfs_root *root,
2869 struct btrfs_balance_control *bctl)
2871 struct btrfs_trans_handle *trans;
2872 struct btrfs_balance_item *item;
2873 struct btrfs_disk_balance_args disk_bargs;
2874 struct btrfs_path *path;
2875 struct extent_buffer *leaf;
2876 struct btrfs_key key;
2879 path = btrfs_alloc_path();
2883 trans = btrfs_start_transaction(root, 0);
2884 if (IS_ERR(trans)) {
2885 btrfs_free_path(path);
2886 return PTR_ERR(trans);
2889 key.objectid = BTRFS_BALANCE_OBJECTID;
2890 key.type = BTRFS_BALANCE_ITEM_KEY;
2893 ret = btrfs_insert_empty_item(trans, root, path, &key,
2898 leaf = path->nodes[0];
2899 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2901 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2903 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2904 btrfs_set_balance_data(leaf, item, &disk_bargs);
2905 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2906 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2907 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2908 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2910 btrfs_set_balance_flags(leaf, item, bctl->flags);
2912 btrfs_mark_buffer_dirty(leaf);
2914 btrfs_free_path(path);
2915 err = btrfs_commit_transaction(trans, root);
2921 static int del_balance_item(struct btrfs_root *root)
2923 struct btrfs_trans_handle *trans;
2924 struct btrfs_path *path;
2925 struct btrfs_key key;
2928 path = btrfs_alloc_path();
2932 trans = btrfs_start_transaction(root, 0);
2933 if (IS_ERR(trans)) {
2934 btrfs_free_path(path);
2935 return PTR_ERR(trans);
2938 key.objectid = BTRFS_BALANCE_OBJECTID;
2939 key.type = BTRFS_BALANCE_ITEM_KEY;
2942 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2950 ret = btrfs_del_item(trans, root, path);
2952 btrfs_free_path(path);
2953 err = btrfs_commit_transaction(trans, root);
2960 * This is a heuristic used to reduce the number of chunks balanced on
2961 * resume after balance was interrupted.
2963 static void update_balance_args(struct btrfs_balance_control *bctl)
2966 * Turn on soft mode for chunk types that were being converted.
2968 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2969 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2970 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2971 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2972 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2973 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2976 * Turn on usage filter if is not already used. The idea is
2977 * that chunks that we have already balanced should be
2978 * reasonably full. Don't do it for chunks that are being
2979 * converted - that will keep us from relocating unconverted
2980 * (albeit full) chunks.
2982 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2983 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2984 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2985 bctl->data.usage = 90;
2987 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2988 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2989 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2990 bctl->sys.usage = 90;
2992 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2993 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2994 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2995 bctl->meta.usage = 90;
3000 * Should be called with both balance and volume mutexes held to
3001 * serialize other volume operations (add_dev/rm_dev/resize) with
3002 * restriper. Same goes for unset_balance_control.
3004 static void set_balance_control(struct btrfs_balance_control *bctl)
3006 struct btrfs_fs_info *fs_info = bctl->fs_info;
3008 BUG_ON(fs_info->balance_ctl);
3010 spin_lock(&fs_info->balance_lock);
3011 fs_info->balance_ctl = bctl;
3012 spin_unlock(&fs_info->balance_lock);
3015 static void unset_balance_control(struct btrfs_fs_info *fs_info)
3017 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3019 BUG_ON(!fs_info->balance_ctl);
3021 spin_lock(&fs_info->balance_lock);
3022 fs_info->balance_ctl = NULL;
3023 spin_unlock(&fs_info->balance_lock);
3029 * Balance filters. Return 1 if chunk should be filtered out
3030 * (should not be balanced).
3032 static int chunk_profiles_filter(u64 chunk_type,
3033 struct btrfs_balance_args *bargs)
3035 chunk_type = chunk_to_extended(chunk_type) &
3036 BTRFS_EXTENDED_PROFILE_MASK;
3038 if (bargs->profiles & chunk_type)
3044 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3045 struct btrfs_balance_args *bargs)
3047 struct btrfs_block_group_cache *cache;
3048 u64 chunk_used, user_thresh;
3051 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3052 chunk_used = btrfs_block_group_used(&cache->item);
3054 if (bargs->usage == 0)
3056 else if (bargs->usage > 100)
3057 user_thresh = cache->key.offset;
3059 user_thresh = div_factor_fine(cache->key.offset,
3062 if (chunk_used < user_thresh)
3065 btrfs_put_block_group(cache);
3069 static int chunk_devid_filter(struct extent_buffer *leaf,
3070 struct btrfs_chunk *chunk,
3071 struct btrfs_balance_args *bargs)
3073 struct btrfs_stripe *stripe;
3074 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3077 for (i = 0; i < num_stripes; i++) {
3078 stripe = btrfs_stripe_nr(chunk, i);
3079 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3086 /* [pstart, pend) */
3087 static int chunk_drange_filter(struct extent_buffer *leaf,
3088 struct btrfs_chunk *chunk,
3090 struct btrfs_balance_args *bargs)
3092 struct btrfs_stripe *stripe;
3093 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3099 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3102 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
3103 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3104 factor = num_stripes / 2;
3105 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3106 factor = num_stripes - 1;
3107 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3108 factor = num_stripes - 2;
3110 factor = num_stripes;
3113 for (i = 0; i < num_stripes; i++) {
3114 stripe = btrfs_stripe_nr(chunk, i);
3115 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3118 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3119 stripe_length = btrfs_chunk_length(leaf, chunk);
3120 stripe_length = div_u64(stripe_length, factor);
3122 if (stripe_offset < bargs->pend &&
3123 stripe_offset + stripe_length > bargs->pstart)
3130 /* [vstart, vend) */
3131 static int chunk_vrange_filter(struct extent_buffer *leaf,
3132 struct btrfs_chunk *chunk,
3134 struct btrfs_balance_args *bargs)
3136 if (chunk_offset < bargs->vend &&
3137 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3138 /* at least part of the chunk is inside this vrange */
3144 static int chunk_soft_convert_filter(u64 chunk_type,
3145 struct btrfs_balance_args *bargs)
3147 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3150 chunk_type = chunk_to_extended(chunk_type) &
3151 BTRFS_EXTENDED_PROFILE_MASK;
3153 if (bargs->target == chunk_type)
3159 static int should_balance_chunk(struct btrfs_root *root,
3160 struct extent_buffer *leaf,
3161 struct btrfs_chunk *chunk, u64 chunk_offset)
3163 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3164 struct btrfs_balance_args *bargs = NULL;
3165 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3168 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3169 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3173 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3174 bargs = &bctl->data;
3175 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3177 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3178 bargs = &bctl->meta;
3180 /* profiles filter */
3181 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3182 chunk_profiles_filter(chunk_type, bargs)) {
3187 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3188 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3193 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3194 chunk_devid_filter(leaf, chunk, bargs)) {
3198 /* drange filter, makes sense only with devid filter */
3199 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3200 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3205 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3206 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3210 /* soft profile changing mode */
3211 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3212 chunk_soft_convert_filter(chunk_type, bargs)) {
3217 * limited by count, must be the last filter
3219 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3220 if (bargs->limit == 0)
3229 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3231 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3232 struct btrfs_root *chunk_root = fs_info->chunk_root;
3233 struct btrfs_root *dev_root = fs_info->dev_root;
3234 struct list_head *devices;
3235 struct btrfs_device *device;
3238 struct btrfs_chunk *chunk;
3239 struct btrfs_path *path;
3240 struct btrfs_key key;
3241 struct btrfs_key found_key;
3242 struct btrfs_trans_handle *trans;
3243 struct extent_buffer *leaf;
3246 int enospc_errors = 0;
3247 bool counting = true;
3248 u64 limit_data = bctl->data.limit;
3249 u64 limit_meta = bctl->meta.limit;
3250 u64 limit_sys = bctl->sys.limit;
3252 /* step one make some room on all the devices */
3253 devices = &fs_info->fs_devices->devices;
3254 list_for_each_entry(device, devices, dev_list) {
3255 old_size = btrfs_device_get_total_bytes(device);
3256 size_to_free = div_factor(old_size, 1);
3257 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
3258 if (!device->writeable ||
3259 btrfs_device_get_total_bytes(device) -
3260 btrfs_device_get_bytes_used(device) > size_to_free ||
3261 device->is_tgtdev_for_dev_replace)
3264 ret = btrfs_shrink_device(device, old_size - size_to_free);
3269 trans = btrfs_start_transaction(dev_root, 0);
3270 BUG_ON(IS_ERR(trans));
3272 ret = btrfs_grow_device(trans, device, old_size);
3275 btrfs_end_transaction(trans, dev_root);
3278 /* step two, relocate all the chunks */
3279 path = btrfs_alloc_path();
3285 /* zero out stat counters */
3286 spin_lock(&fs_info->balance_lock);
3287 memset(&bctl->stat, 0, sizeof(bctl->stat));
3288 spin_unlock(&fs_info->balance_lock);
3291 bctl->data.limit = limit_data;
3292 bctl->meta.limit = limit_meta;
3293 bctl->sys.limit = limit_sys;
3295 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3296 key.offset = (u64)-1;
3297 key.type = BTRFS_CHUNK_ITEM_KEY;
3300 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3301 atomic_read(&fs_info->balance_cancel_req)) {
3306 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3307 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3309 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3314 * this shouldn't happen, it means the last relocate
3318 BUG(); /* FIXME break ? */
3320 ret = btrfs_previous_item(chunk_root, path, 0,
3321 BTRFS_CHUNK_ITEM_KEY);
3323 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3328 leaf = path->nodes[0];
3329 slot = path->slots[0];
3330 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3332 if (found_key.objectid != key.objectid) {
3333 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3337 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3340 spin_lock(&fs_info->balance_lock);
3341 bctl->stat.considered++;
3342 spin_unlock(&fs_info->balance_lock);
3345 ret = should_balance_chunk(chunk_root, leaf, chunk,
3347 btrfs_release_path(path);
3349 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3354 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3355 spin_lock(&fs_info->balance_lock);
3356 bctl->stat.expected++;
3357 spin_unlock(&fs_info->balance_lock);
3361 ret = btrfs_relocate_chunk(chunk_root,
3363 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3364 if (ret && ret != -ENOSPC)
3366 if (ret == -ENOSPC) {
3369 spin_lock(&fs_info->balance_lock);
3370 bctl->stat.completed++;
3371 spin_unlock(&fs_info->balance_lock);
3374 if (found_key.offset == 0)
3376 key.offset = found_key.offset - 1;
3380 btrfs_release_path(path);
3385 btrfs_free_path(path);
3386 if (enospc_errors) {
3387 btrfs_info(fs_info, "%d enospc errors during balance",
3397 * alloc_profile_is_valid - see if a given profile is valid and reduced
3398 * @flags: profile to validate
3399 * @extended: if true @flags is treated as an extended profile
3401 static int alloc_profile_is_valid(u64 flags, int extended)
3403 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3404 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3406 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3408 /* 1) check that all other bits are zeroed */
3412 /* 2) see if profile is reduced */
3414 return !extended; /* "0" is valid for usual profiles */
3416 /* true if exactly one bit set */
3417 return (flags & (flags - 1)) == 0;
3420 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3422 /* cancel requested || normal exit path */
3423 return atomic_read(&fs_info->balance_cancel_req) ||
3424 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3425 atomic_read(&fs_info->balance_cancel_req) == 0);
3428 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3432 unset_balance_control(fs_info);
3433 ret = del_balance_item(fs_info->tree_root);
3435 btrfs_std_error(fs_info, ret, NULL);
3437 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3441 * Should be called with both balance and volume mutexes held
3443 int btrfs_balance(struct btrfs_balance_control *bctl,
3444 struct btrfs_ioctl_balance_args *bargs)
3446 struct btrfs_fs_info *fs_info = bctl->fs_info;
3453 if (btrfs_fs_closing(fs_info) ||
3454 atomic_read(&fs_info->balance_pause_req) ||
3455 atomic_read(&fs_info->balance_cancel_req)) {
3460 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3461 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3465 * In case of mixed groups both data and meta should be picked,
3466 * and identical options should be given for both of them.
3468 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3469 if (mixed && (bctl->flags & allowed)) {
3470 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3471 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3472 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3473 btrfs_err(fs_info, "with mixed groups data and "
3474 "metadata balance options must be the same");
3480 num_devices = fs_info->fs_devices->num_devices;
3481 btrfs_dev_replace_lock(&fs_info->dev_replace);
3482 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3483 BUG_ON(num_devices < 1);
3486 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3487 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3488 if (num_devices == 1)
3489 allowed |= BTRFS_BLOCK_GROUP_DUP;
3490 else if (num_devices > 1)
3491 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3492 if (num_devices > 2)
3493 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3494 if (num_devices > 3)
3495 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3496 BTRFS_BLOCK_GROUP_RAID6);
3497 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3498 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3499 (bctl->data.target & ~allowed))) {
3500 btrfs_err(fs_info, "unable to start balance with target "
3501 "data profile %llu",
3506 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3507 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3508 (bctl->meta.target & ~allowed))) {
3510 "unable to start balance with target metadata profile %llu",
3515 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3516 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3517 (bctl->sys.target & ~allowed))) {
3519 "unable to start balance with target system profile %llu",
3525 /* allow dup'ed data chunks only in mixed mode */
3526 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3527 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3528 btrfs_err(fs_info, "dup for data is not allowed");
3533 /* allow to reduce meta or sys integrity only if force set */
3534 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3535 BTRFS_BLOCK_GROUP_RAID10 |
3536 BTRFS_BLOCK_GROUP_RAID5 |
3537 BTRFS_BLOCK_GROUP_RAID6;
3539 seq = read_seqbegin(&fs_info->profiles_lock);
3541 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3542 (fs_info->avail_system_alloc_bits & allowed) &&
3543 !(bctl->sys.target & allowed)) ||
3544 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3545 (fs_info->avail_metadata_alloc_bits & allowed) &&
3546 !(bctl->meta.target & allowed))) {
3547 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3548 btrfs_info(fs_info, "force reducing metadata integrity");
3550 btrfs_err(fs_info, "balance will reduce metadata "
3551 "integrity, use force if you want this");
3556 } while (read_seqretry(&fs_info->profiles_lock, seq));
3558 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3559 fs_info->num_tolerated_disk_barrier_failures = min(
3560 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3561 btrfs_get_num_tolerated_disk_barrier_failures(
3565 ret = insert_balance_item(fs_info->tree_root, bctl);
3566 if (ret && ret != -EEXIST)
3569 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3570 BUG_ON(ret == -EEXIST);
3571 set_balance_control(bctl);
3573 BUG_ON(ret != -EEXIST);
3574 spin_lock(&fs_info->balance_lock);
3575 update_balance_args(bctl);
3576 spin_unlock(&fs_info->balance_lock);
3579 atomic_inc(&fs_info->balance_running);
3580 mutex_unlock(&fs_info->balance_mutex);
3582 ret = __btrfs_balance(fs_info);
3584 mutex_lock(&fs_info->balance_mutex);
3585 atomic_dec(&fs_info->balance_running);
3587 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3588 fs_info->num_tolerated_disk_barrier_failures =
3589 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3593 memset(bargs, 0, sizeof(*bargs));
3594 update_ioctl_balance_args(fs_info, 0, bargs);
3597 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3598 balance_need_close(fs_info)) {
3599 __cancel_balance(fs_info);
3602 wake_up(&fs_info->balance_wait_q);
3606 if (bctl->flags & BTRFS_BALANCE_RESUME)
3607 __cancel_balance(fs_info);
3610 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3615 static int balance_kthread(void *data)
3617 struct btrfs_fs_info *fs_info = data;
3620 mutex_lock(&fs_info->volume_mutex);
3621 mutex_lock(&fs_info->balance_mutex);
3623 if (fs_info->balance_ctl) {
3624 btrfs_info(fs_info, "continuing balance");
3625 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3628 mutex_unlock(&fs_info->balance_mutex);
3629 mutex_unlock(&fs_info->volume_mutex);
3634 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3636 struct task_struct *tsk;
3638 spin_lock(&fs_info->balance_lock);
3639 if (!fs_info->balance_ctl) {
3640 spin_unlock(&fs_info->balance_lock);
3643 spin_unlock(&fs_info->balance_lock);
3645 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3646 btrfs_info(fs_info, "force skipping balance");
3650 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3651 return PTR_ERR_OR_ZERO(tsk);
3654 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3656 struct btrfs_balance_control *bctl;
3657 struct btrfs_balance_item *item;
3658 struct btrfs_disk_balance_args disk_bargs;
3659 struct btrfs_path *path;
3660 struct extent_buffer *leaf;
3661 struct btrfs_key key;
3664 path = btrfs_alloc_path();
3668 key.objectid = BTRFS_BALANCE_OBJECTID;
3669 key.type = BTRFS_BALANCE_ITEM_KEY;
3672 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3675 if (ret > 0) { /* ret = -ENOENT; */
3680 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3686 leaf = path->nodes[0];
3687 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3689 bctl->fs_info = fs_info;
3690 bctl->flags = btrfs_balance_flags(leaf, item);
3691 bctl->flags |= BTRFS_BALANCE_RESUME;
3693 btrfs_balance_data(leaf, item, &disk_bargs);
3694 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3695 btrfs_balance_meta(leaf, item, &disk_bargs);
3696 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3697 btrfs_balance_sys(leaf, item, &disk_bargs);
3698 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3700 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3702 mutex_lock(&fs_info->volume_mutex);
3703 mutex_lock(&fs_info->balance_mutex);
3705 set_balance_control(bctl);
3707 mutex_unlock(&fs_info->balance_mutex);
3708 mutex_unlock(&fs_info->volume_mutex);
3710 btrfs_free_path(path);
3714 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3718 mutex_lock(&fs_info->balance_mutex);
3719 if (!fs_info->balance_ctl) {
3720 mutex_unlock(&fs_info->balance_mutex);
3724 if (atomic_read(&fs_info->balance_running)) {
3725 atomic_inc(&fs_info->balance_pause_req);
3726 mutex_unlock(&fs_info->balance_mutex);
3728 wait_event(fs_info->balance_wait_q,
3729 atomic_read(&fs_info->balance_running) == 0);
3731 mutex_lock(&fs_info->balance_mutex);
3732 /* we are good with balance_ctl ripped off from under us */
3733 BUG_ON(atomic_read(&fs_info->balance_running));
3734 atomic_dec(&fs_info->balance_pause_req);
3739 mutex_unlock(&fs_info->balance_mutex);
3743 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3745 if (fs_info->sb->s_flags & MS_RDONLY)
3748 mutex_lock(&fs_info->balance_mutex);
3749 if (!fs_info->balance_ctl) {
3750 mutex_unlock(&fs_info->balance_mutex);
3754 atomic_inc(&fs_info->balance_cancel_req);
3756 * if we are running just wait and return, balance item is
3757 * deleted in btrfs_balance in this case
3759 if (atomic_read(&fs_info->balance_running)) {
3760 mutex_unlock(&fs_info->balance_mutex);
3761 wait_event(fs_info->balance_wait_q,
3762 atomic_read(&fs_info->balance_running) == 0);
3763 mutex_lock(&fs_info->balance_mutex);
3765 /* __cancel_balance needs volume_mutex */
3766 mutex_unlock(&fs_info->balance_mutex);
3767 mutex_lock(&fs_info->volume_mutex);
3768 mutex_lock(&fs_info->balance_mutex);
3770 if (fs_info->balance_ctl)
3771 __cancel_balance(fs_info);
3773 mutex_unlock(&fs_info->volume_mutex);
3776 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3777 atomic_dec(&fs_info->balance_cancel_req);
3778 mutex_unlock(&fs_info->balance_mutex);
3782 static int btrfs_uuid_scan_kthread(void *data)
3784 struct btrfs_fs_info *fs_info = data;
3785 struct btrfs_root *root = fs_info->tree_root;
3786 struct btrfs_key key;
3787 struct btrfs_key max_key;
3788 struct btrfs_path *path = NULL;
3790 struct extent_buffer *eb;
3792 struct btrfs_root_item root_item;
3794 struct btrfs_trans_handle *trans = NULL;
3796 path = btrfs_alloc_path();
3803 key.type = BTRFS_ROOT_ITEM_KEY;
3806 max_key.objectid = (u64)-1;
3807 max_key.type = BTRFS_ROOT_ITEM_KEY;
3808 max_key.offset = (u64)-1;
3811 ret = btrfs_search_forward(root, &key, path, 0);
3818 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3819 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3820 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3821 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3824 eb = path->nodes[0];
3825 slot = path->slots[0];
3826 item_size = btrfs_item_size_nr(eb, slot);
3827 if (item_size < sizeof(root_item))
3830 read_extent_buffer(eb, &root_item,
3831 btrfs_item_ptr_offset(eb, slot),
3832 (int)sizeof(root_item));
3833 if (btrfs_root_refs(&root_item) == 0)
3836 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3837 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3841 btrfs_release_path(path);
3843 * 1 - subvol uuid item
3844 * 1 - received_subvol uuid item
3846 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3847 if (IS_ERR(trans)) {
3848 ret = PTR_ERR(trans);
3856 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3857 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3859 BTRFS_UUID_KEY_SUBVOL,
3862 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3868 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3869 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3870 root_item.received_uuid,
3871 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3874 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3882 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3888 btrfs_release_path(path);
3889 if (key.offset < (u64)-1) {
3891 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3893 key.type = BTRFS_ROOT_ITEM_KEY;
3894 } else if (key.objectid < (u64)-1) {
3896 key.type = BTRFS_ROOT_ITEM_KEY;
3905 btrfs_free_path(path);
3906 if (trans && !IS_ERR(trans))
3907 btrfs_end_transaction(trans, fs_info->uuid_root);
3909 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
3911 fs_info->update_uuid_tree_gen = 1;
3912 up(&fs_info->uuid_tree_rescan_sem);
3917 * Callback for btrfs_uuid_tree_iterate().
3919 * 0 check succeeded, the entry is not outdated.
3920 * < 0 if an error occured.
3921 * > 0 if the check failed, which means the caller shall remove the entry.
3923 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3924 u8 *uuid, u8 type, u64 subid)
3926 struct btrfs_key key;
3928 struct btrfs_root *subvol_root;
3930 if (type != BTRFS_UUID_KEY_SUBVOL &&
3931 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3934 key.objectid = subid;
3935 key.type = BTRFS_ROOT_ITEM_KEY;
3936 key.offset = (u64)-1;
3937 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3938 if (IS_ERR(subvol_root)) {
3939 ret = PTR_ERR(subvol_root);
3946 case BTRFS_UUID_KEY_SUBVOL:
3947 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3950 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3951 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3961 static int btrfs_uuid_rescan_kthread(void *data)
3963 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3967 * 1st step is to iterate through the existing UUID tree and
3968 * to delete all entries that contain outdated data.
3969 * 2nd step is to add all missing entries to the UUID tree.
3971 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3973 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
3974 up(&fs_info->uuid_tree_rescan_sem);
3977 return btrfs_uuid_scan_kthread(data);
3980 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3982 struct btrfs_trans_handle *trans;
3983 struct btrfs_root *tree_root = fs_info->tree_root;
3984 struct btrfs_root *uuid_root;
3985 struct task_struct *task;
3992 trans = btrfs_start_transaction(tree_root, 2);
3994 return PTR_ERR(trans);
3996 uuid_root = btrfs_create_tree(trans, fs_info,
3997 BTRFS_UUID_TREE_OBJECTID);
3998 if (IS_ERR(uuid_root)) {
3999 ret = PTR_ERR(uuid_root);
4000 btrfs_abort_transaction(trans, tree_root, ret);
4004 fs_info->uuid_root = uuid_root;
4006 ret = btrfs_commit_transaction(trans, tree_root);
4010 down(&fs_info->uuid_tree_rescan_sem);
4011 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4013 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4014 btrfs_warn(fs_info, "failed to start uuid_scan task");
4015 up(&fs_info->uuid_tree_rescan_sem);
4016 return PTR_ERR(task);
4022 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4024 struct task_struct *task;
4026 down(&fs_info->uuid_tree_rescan_sem);
4027 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4029 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4030 btrfs_warn(fs_info, "failed to start uuid_rescan task");
4031 up(&fs_info->uuid_tree_rescan_sem);
4032 return PTR_ERR(task);
4039 * shrinking a device means finding all of the device extents past
4040 * the new size, and then following the back refs to the chunks.
4041 * The chunk relocation code actually frees the device extent
4043 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4045 struct btrfs_trans_handle *trans;
4046 struct btrfs_root *root = device->dev_root;
4047 struct btrfs_dev_extent *dev_extent = NULL;
4048 struct btrfs_path *path;
4054 bool retried = false;
4055 bool checked_pending_chunks = false;
4056 struct extent_buffer *l;
4057 struct btrfs_key key;
4058 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4059 u64 old_total = btrfs_super_total_bytes(super_copy);
4060 u64 old_size = btrfs_device_get_total_bytes(device);
4061 u64 diff = old_size - new_size;
4063 if (device->is_tgtdev_for_dev_replace)
4066 path = btrfs_alloc_path();
4074 btrfs_device_set_total_bytes(device, new_size);
4075 if (device->writeable) {
4076 device->fs_devices->total_rw_bytes -= diff;
4077 spin_lock(&root->fs_info->free_chunk_lock);
4078 root->fs_info->free_chunk_space -= diff;
4079 spin_unlock(&root->fs_info->free_chunk_lock);
4081 unlock_chunks(root);
4084 key.objectid = device->devid;
4085 key.offset = (u64)-1;
4086 key.type = BTRFS_DEV_EXTENT_KEY;
4089 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
4090 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4092 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4096 ret = btrfs_previous_item(root, path, 0, key.type);
4098 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4103 btrfs_release_path(path);
4108 slot = path->slots[0];
4109 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4111 if (key.objectid != device->devid) {
4112 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4113 btrfs_release_path(path);
4117 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4118 length = btrfs_dev_extent_length(l, dev_extent);
4120 if (key.offset + length <= new_size) {
4121 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4122 btrfs_release_path(path);
4126 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4127 btrfs_release_path(path);
4129 ret = btrfs_relocate_chunk(root, chunk_offset);
4130 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
4131 if (ret && ret != -ENOSPC)
4135 } while (key.offset-- > 0);
4137 if (failed && !retried) {
4141 } else if (failed && retried) {
4146 /* Shrinking succeeded, else we would be at "done". */
4147 trans = btrfs_start_transaction(root, 0);
4148 if (IS_ERR(trans)) {
4149 ret = PTR_ERR(trans);
4156 * We checked in the above loop all device extents that were already in
4157 * the device tree. However before we have updated the device's
4158 * total_bytes to the new size, we might have had chunk allocations that
4159 * have not complete yet (new block groups attached to transaction
4160 * handles), and therefore their device extents were not yet in the
4161 * device tree and we missed them in the loop above. So if we have any
4162 * pending chunk using a device extent that overlaps the device range
4163 * that we can not use anymore, commit the current transaction and
4164 * repeat the search on the device tree - this way we guarantee we will
4165 * not have chunks using device extents that end beyond 'new_size'.
4167 if (!checked_pending_chunks) {
4168 u64 start = new_size;
4169 u64 len = old_size - new_size;
4171 if (contains_pending_extent(trans->transaction, device,
4173 unlock_chunks(root);
4174 checked_pending_chunks = true;
4177 ret = btrfs_commit_transaction(trans, root);
4184 btrfs_device_set_disk_total_bytes(device, new_size);
4185 if (list_empty(&device->resized_list))
4186 list_add_tail(&device->resized_list,
4187 &root->fs_info->fs_devices->resized_devices);
4189 WARN_ON(diff > old_total);
4190 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4191 unlock_chunks(root);
4193 /* Now btrfs_update_device() will change the on-disk size. */
4194 ret = btrfs_update_device(trans, device);
4195 btrfs_end_transaction(trans, root);
4197 btrfs_free_path(path);
4200 btrfs_device_set_total_bytes(device, old_size);
4201 if (device->writeable)
4202 device->fs_devices->total_rw_bytes += diff;
4203 spin_lock(&root->fs_info->free_chunk_lock);
4204 root->fs_info->free_chunk_space += diff;
4205 spin_unlock(&root->fs_info->free_chunk_lock);
4206 unlock_chunks(root);
4211 static int btrfs_add_system_chunk(struct btrfs_root *root,
4212 struct btrfs_key *key,
4213 struct btrfs_chunk *chunk, int item_size)
4215 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4216 struct btrfs_disk_key disk_key;
4221 array_size = btrfs_super_sys_array_size(super_copy);
4222 if (array_size + item_size + sizeof(disk_key)
4223 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4224 unlock_chunks(root);
4228 ptr = super_copy->sys_chunk_array + array_size;
4229 btrfs_cpu_key_to_disk(&disk_key, key);
4230 memcpy(ptr, &disk_key, sizeof(disk_key));
4231 ptr += sizeof(disk_key);
4232 memcpy(ptr, chunk, item_size);
4233 item_size += sizeof(disk_key);
4234 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4235 unlock_chunks(root);
4241 * sort the devices in descending order by max_avail, total_avail
4243 static int btrfs_cmp_device_info(const void *a, const void *b)
4245 const struct btrfs_device_info *di_a = a;
4246 const struct btrfs_device_info *di_b = b;
4248 if (di_a->max_avail > di_b->max_avail)
4250 if (di_a->max_avail < di_b->max_avail)
4252 if (di_a->total_avail > di_b->total_avail)
4254 if (di_a->total_avail < di_b->total_avail)
4259 static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
4260 [BTRFS_RAID_RAID10] = {
4263 .devs_max = 0, /* 0 == as many as possible */
4265 .devs_increment = 2,
4268 [BTRFS_RAID_RAID1] = {
4273 .devs_increment = 2,
4276 [BTRFS_RAID_DUP] = {
4281 .devs_increment = 1,
4284 [BTRFS_RAID_RAID0] = {
4289 .devs_increment = 1,
4292 [BTRFS_RAID_SINGLE] = {
4297 .devs_increment = 1,
4300 [BTRFS_RAID_RAID5] = {
4305 .devs_increment = 1,
4308 [BTRFS_RAID_RAID6] = {
4313 .devs_increment = 1,
4318 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4320 /* TODO allow them to set a preferred stripe size */
4324 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4326 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
4329 btrfs_set_fs_incompat(info, RAID56);
4332 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4333 - sizeof(struct btrfs_item) \
4334 - sizeof(struct btrfs_chunk)) \
4335 / sizeof(struct btrfs_stripe) + 1)
4337 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4338 - 2 * sizeof(struct btrfs_disk_key) \
4339 - 2 * sizeof(struct btrfs_chunk)) \
4340 / sizeof(struct btrfs_stripe) + 1)
4342 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4343 struct btrfs_root *extent_root, u64 start,
4346 struct btrfs_fs_info *info = extent_root->fs_info;
4347 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4348 struct list_head *cur;
4349 struct map_lookup *map = NULL;
4350 struct extent_map_tree *em_tree;
4351 struct extent_map *em;
4352 struct btrfs_device_info *devices_info = NULL;
4354 int num_stripes; /* total number of stripes to allocate */
4355 int data_stripes; /* number of stripes that count for
4357 int sub_stripes; /* sub_stripes info for map */
4358 int dev_stripes; /* stripes per dev */
4359 int devs_max; /* max devs to use */
4360 int devs_min; /* min devs needed */
4361 int devs_increment; /* ndevs has to be a multiple of this */
4362 int ncopies; /* how many copies to data has */
4364 u64 max_stripe_size;
4368 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
4374 BUG_ON(!alloc_profile_is_valid(type, 0));
4376 if (list_empty(&fs_devices->alloc_list))
4379 index = __get_raid_index(type);
4381 sub_stripes = btrfs_raid_array[index].sub_stripes;
4382 dev_stripes = btrfs_raid_array[index].dev_stripes;
4383 devs_max = btrfs_raid_array[index].devs_max;
4384 devs_min = btrfs_raid_array[index].devs_min;
4385 devs_increment = btrfs_raid_array[index].devs_increment;
4386 ncopies = btrfs_raid_array[index].ncopies;
4388 if (type & BTRFS_BLOCK_GROUP_DATA) {
4389 max_stripe_size = 1024 * 1024 * 1024;
4390 max_chunk_size = 10 * max_stripe_size;
4392 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4393 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4394 /* for larger filesystems, use larger metadata chunks */
4395 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4396 max_stripe_size = 1024 * 1024 * 1024;
4398 max_stripe_size = 256 * 1024 * 1024;
4399 max_chunk_size = max_stripe_size;
4401 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4402 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4403 max_stripe_size = 32 * 1024 * 1024;
4404 max_chunk_size = 2 * max_stripe_size;
4406 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
4408 btrfs_err(info, "invalid chunk type 0x%llx requested",
4413 /* we don't want a chunk larger than 10% of writeable space */
4414 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4417 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
4422 cur = fs_devices->alloc_list.next;
4425 * in the first pass through the devices list, we gather information
4426 * about the available holes on each device.
4429 while (cur != &fs_devices->alloc_list) {
4430 struct btrfs_device *device;
4434 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4438 if (!device->writeable) {
4440 "BTRFS: read-only device in alloc_list\n");
4444 if (!device->in_fs_metadata ||
4445 device->is_tgtdev_for_dev_replace)
4448 if (device->total_bytes > device->bytes_used)
4449 total_avail = device->total_bytes - device->bytes_used;
4453 /* If there is no space on this device, skip it. */
4454 if (total_avail == 0)
4457 ret = find_free_dev_extent(trans, device,
4458 max_stripe_size * dev_stripes,
4459 &dev_offset, &max_avail);
4460 if (ret && ret != -ENOSPC)
4464 max_avail = max_stripe_size * dev_stripes;
4466 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4469 if (ndevs == fs_devices->rw_devices) {
4470 WARN(1, "%s: found more than %llu devices\n",
4471 __func__, fs_devices->rw_devices);
4474 devices_info[ndevs].dev_offset = dev_offset;
4475 devices_info[ndevs].max_avail = max_avail;
4476 devices_info[ndevs].total_avail = total_avail;
4477 devices_info[ndevs].dev = device;
4482 * now sort the devices by hole size / available space
4484 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4485 btrfs_cmp_device_info, NULL);
4487 /* round down to number of usable stripes */
4488 ndevs -= ndevs % devs_increment;
4490 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4495 if (devs_max && ndevs > devs_max)
4498 * the primary goal is to maximize the number of stripes, so use as many
4499 * devices as possible, even if the stripes are not maximum sized.
4501 stripe_size = devices_info[ndevs-1].max_avail;
4502 num_stripes = ndevs * dev_stripes;
4505 * this will have to be fixed for RAID1 and RAID10 over
4508 data_stripes = num_stripes / ncopies;
4510 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4511 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4512 btrfs_super_stripesize(info->super_copy));
4513 data_stripes = num_stripes - 1;
4515 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4516 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4517 btrfs_super_stripesize(info->super_copy));
4518 data_stripes = num_stripes - 2;
4522 * Use the number of data stripes to figure out how big this chunk
4523 * is really going to be in terms of logical address space,
4524 * and compare that answer with the max chunk size
4526 if (stripe_size * data_stripes > max_chunk_size) {
4527 u64 mask = (1ULL << 24) - 1;
4529 stripe_size = div_u64(max_chunk_size, data_stripes);
4531 /* bump the answer up to a 16MB boundary */
4532 stripe_size = (stripe_size + mask) & ~mask;
4534 /* but don't go higher than the limits we found
4535 * while searching for free extents
4537 if (stripe_size > devices_info[ndevs-1].max_avail)
4538 stripe_size = devices_info[ndevs-1].max_avail;
4541 stripe_size = div_u64(stripe_size, dev_stripes);
4543 /* align to BTRFS_STRIPE_LEN */
4544 stripe_size = div_u64(stripe_size, raid_stripe_len);
4545 stripe_size *= raid_stripe_len;
4547 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4552 map->num_stripes = num_stripes;
4554 for (i = 0; i < ndevs; ++i) {
4555 for (j = 0; j < dev_stripes; ++j) {
4556 int s = i * dev_stripes + j;
4557 map->stripes[s].dev = devices_info[i].dev;
4558 map->stripes[s].physical = devices_info[i].dev_offset +
4562 map->sector_size = extent_root->sectorsize;
4563 map->stripe_len = raid_stripe_len;
4564 map->io_align = raid_stripe_len;
4565 map->io_width = raid_stripe_len;
4567 map->sub_stripes = sub_stripes;
4569 num_bytes = stripe_size * data_stripes;
4571 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
4573 em = alloc_extent_map();
4579 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4580 em->bdev = (struct block_device *)map;
4582 em->len = num_bytes;
4583 em->block_start = 0;
4584 em->block_len = em->len;
4585 em->orig_block_len = stripe_size;
4587 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4588 write_lock(&em_tree->lock);
4589 ret = add_extent_mapping(em_tree, em, 0);
4591 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4592 atomic_inc(&em->refs);
4594 write_unlock(&em_tree->lock);
4596 free_extent_map(em);
4600 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4601 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4604 goto error_del_extent;
4606 for (i = 0; i < map->num_stripes; i++) {
4607 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4608 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4611 spin_lock(&extent_root->fs_info->free_chunk_lock);
4612 extent_root->fs_info->free_chunk_space -= (stripe_size *
4614 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4616 free_extent_map(em);
4617 check_raid56_incompat_flag(extent_root->fs_info, type);
4619 kfree(devices_info);
4623 write_lock(&em_tree->lock);
4624 remove_extent_mapping(em_tree, em);
4625 write_unlock(&em_tree->lock);
4627 /* One for our allocation */
4628 free_extent_map(em);
4629 /* One for the tree reference */
4630 free_extent_map(em);
4631 /* One for the pending_chunks list reference */
4632 free_extent_map(em);
4634 kfree(devices_info);
4638 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4639 struct btrfs_root *extent_root,
4640 u64 chunk_offset, u64 chunk_size)
4642 struct btrfs_key key;
4643 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4644 struct btrfs_device *device;
4645 struct btrfs_chunk *chunk;
4646 struct btrfs_stripe *stripe;
4647 struct extent_map_tree *em_tree;
4648 struct extent_map *em;
4649 struct map_lookup *map;
4656 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4657 read_lock(&em_tree->lock);
4658 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4659 read_unlock(&em_tree->lock);
4662 btrfs_crit(extent_root->fs_info, "unable to find logical "
4663 "%Lu len %Lu", chunk_offset, chunk_size);
4667 if (em->start != chunk_offset || em->len != chunk_size) {
4668 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4669 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
4670 chunk_size, em->start, em->len);
4671 free_extent_map(em);
4675 map = (struct map_lookup *)em->bdev;
4676 item_size = btrfs_chunk_item_size(map->num_stripes);
4677 stripe_size = em->orig_block_len;
4679 chunk = kzalloc(item_size, GFP_NOFS);
4685 for (i = 0; i < map->num_stripes; i++) {
4686 device = map->stripes[i].dev;
4687 dev_offset = map->stripes[i].physical;
4689 ret = btrfs_update_device(trans, device);
4692 ret = btrfs_alloc_dev_extent(trans, device,
4693 chunk_root->root_key.objectid,
4694 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4695 chunk_offset, dev_offset,
4701 stripe = &chunk->stripe;
4702 for (i = 0; i < map->num_stripes; i++) {
4703 device = map->stripes[i].dev;
4704 dev_offset = map->stripes[i].physical;
4706 btrfs_set_stack_stripe_devid(stripe, device->devid);
4707 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4708 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4712 btrfs_set_stack_chunk_length(chunk, chunk_size);
4713 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4714 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4715 btrfs_set_stack_chunk_type(chunk, map->type);
4716 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4717 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4718 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4719 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4720 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4722 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4723 key.type = BTRFS_CHUNK_ITEM_KEY;
4724 key.offset = chunk_offset;
4726 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4727 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4729 * TODO: Cleanup of inserted chunk root in case of
4732 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4738 free_extent_map(em);
4743 * Chunk allocation falls into two parts. The first part does works
4744 * that make the new allocated chunk useable, but not do any operation
4745 * that modifies the chunk tree. The second part does the works that
4746 * require modifying the chunk tree. This division is important for the
4747 * bootstrap process of adding storage to a seed btrfs.
4749 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4750 struct btrfs_root *extent_root, u64 type)
4754 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
4755 chunk_offset = find_next_chunk(extent_root->fs_info);
4756 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
4759 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4760 struct btrfs_root *root,
4761 struct btrfs_device *device)
4764 u64 sys_chunk_offset;
4766 struct btrfs_fs_info *fs_info = root->fs_info;
4767 struct btrfs_root *extent_root = fs_info->extent_root;
4770 chunk_offset = find_next_chunk(fs_info);
4771 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4772 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4777 sys_chunk_offset = find_next_chunk(root->fs_info);
4778 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4779 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4784 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4788 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4789 BTRFS_BLOCK_GROUP_RAID10 |
4790 BTRFS_BLOCK_GROUP_RAID5 |
4791 BTRFS_BLOCK_GROUP_DUP)) {
4793 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4802 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4804 struct extent_map *em;
4805 struct map_lookup *map;
4806 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4811 read_lock(&map_tree->map_tree.lock);
4812 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4813 read_unlock(&map_tree->map_tree.lock);
4817 map = (struct map_lookup *)em->bdev;
4818 for (i = 0; i < map->num_stripes; i++) {
4819 if (map->stripes[i].dev->missing) {
4824 if (!map->stripes[i].dev->writeable) {
4831 * If the number of missing devices is larger than max errors,
4832 * we can not write the data into that chunk successfully, so
4835 if (miss_ndevs > btrfs_chunk_max_errors(map))
4838 free_extent_map(em);
4842 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4844 extent_map_tree_init(&tree->map_tree);
4847 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4849 struct extent_map *em;
4852 write_lock(&tree->map_tree.lock);
4853 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4855 remove_extent_mapping(&tree->map_tree, em);
4856 write_unlock(&tree->map_tree.lock);
4860 free_extent_map(em);
4861 /* once for the tree */
4862 free_extent_map(em);
4866 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4868 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4869 struct extent_map *em;
4870 struct map_lookup *map;
4871 struct extent_map_tree *em_tree = &map_tree->map_tree;
4874 read_lock(&em_tree->lock);
4875 em = lookup_extent_mapping(em_tree, logical, len);
4876 read_unlock(&em_tree->lock);
4879 * We could return errors for these cases, but that could get ugly and
4880 * we'd probably do the same thing which is just not do anything else
4881 * and exit, so return 1 so the callers don't try to use other copies.
4884 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
4889 if (em->start > logical || em->start + em->len < logical) {
4890 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
4891 "%Lu-%Lu", logical, logical+len, em->start,
4892 em->start + em->len);
4893 free_extent_map(em);
4897 map = (struct map_lookup *)em->bdev;
4898 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4899 ret = map->num_stripes;
4900 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4901 ret = map->sub_stripes;
4902 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4904 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4908 free_extent_map(em);
4910 btrfs_dev_replace_lock(&fs_info->dev_replace);
4911 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4913 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4918 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4919 struct btrfs_mapping_tree *map_tree,
4922 struct extent_map *em;
4923 struct map_lookup *map;
4924 struct extent_map_tree *em_tree = &map_tree->map_tree;
4925 unsigned long len = root->sectorsize;
4927 read_lock(&em_tree->lock);
4928 em = lookup_extent_mapping(em_tree, logical, len);
4929 read_unlock(&em_tree->lock);
4932 BUG_ON(em->start > logical || em->start + em->len < logical);
4933 map = (struct map_lookup *)em->bdev;
4934 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
4935 len = map->stripe_len * nr_data_stripes(map);
4936 free_extent_map(em);
4940 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4941 u64 logical, u64 len, int mirror_num)
4943 struct extent_map *em;
4944 struct map_lookup *map;
4945 struct extent_map_tree *em_tree = &map_tree->map_tree;
4948 read_lock(&em_tree->lock);
4949 em = lookup_extent_mapping(em_tree, logical, len);
4950 read_unlock(&em_tree->lock);
4953 BUG_ON(em->start > logical || em->start + em->len < logical);
4954 map = (struct map_lookup *)em->bdev;
4955 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
4957 free_extent_map(em);
4961 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4962 struct map_lookup *map, int first, int num,
4963 int optimal, int dev_replace_is_ongoing)
4967 struct btrfs_device *srcdev;
4969 if (dev_replace_is_ongoing &&
4970 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4971 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4972 srcdev = fs_info->dev_replace.srcdev;
4977 * try to avoid the drive that is the source drive for a
4978 * dev-replace procedure, only choose it if no other non-missing
4979 * mirror is available
4981 for (tolerance = 0; tolerance < 2; tolerance++) {
4982 if (map->stripes[optimal].dev->bdev &&
4983 (tolerance || map->stripes[optimal].dev != srcdev))
4985 for (i = first; i < first + num; i++) {
4986 if (map->stripes[i].dev->bdev &&
4987 (tolerance || map->stripes[i].dev != srcdev))
4992 /* we couldn't find one that doesn't fail. Just return something
4993 * and the io error handling code will clean up eventually
4998 static inline int parity_smaller(u64 a, u64 b)
5003 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5004 static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
5006 struct btrfs_bio_stripe s;
5013 for (i = 0; i < num_stripes - 1; i++) {
5014 if (parity_smaller(bbio->raid_map[i],
5015 bbio->raid_map[i+1])) {
5016 s = bbio->stripes[i];
5017 l = bbio->raid_map[i];
5018 bbio->stripes[i] = bbio->stripes[i+1];
5019 bbio->raid_map[i] = bbio->raid_map[i+1];
5020 bbio->stripes[i+1] = s;
5021 bbio->raid_map[i+1] = l;
5029 static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5031 struct btrfs_bio *bbio = kzalloc(
5032 /* the size of the btrfs_bio */
5033 sizeof(struct btrfs_bio) +
5034 /* plus the variable array for the stripes */
5035 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
5036 /* plus the variable array for the tgt dev */
5037 sizeof(int) * (real_stripes) +
5039 * plus the raid_map, which includes both the tgt dev
5042 sizeof(u64) * (total_stripes),
5043 GFP_NOFS|__GFP_NOFAIL);
5045 atomic_set(&bbio->error, 0);
5046 atomic_set(&bbio->refs, 1);
5051 void btrfs_get_bbio(struct btrfs_bio *bbio)
5053 WARN_ON(!atomic_read(&bbio->refs));
5054 atomic_inc(&bbio->refs);
5057 void btrfs_put_bbio(struct btrfs_bio *bbio)
5061 if (atomic_dec_and_test(&bbio->refs))
5065 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5066 u64 logical, u64 *length,
5067 struct btrfs_bio **bbio_ret,
5068 int mirror_num, int need_raid_map)
5070 struct extent_map *em;
5071 struct map_lookup *map;
5072 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
5073 struct extent_map_tree *em_tree = &map_tree->map_tree;
5076 u64 stripe_end_offset;
5086 int tgtdev_indexes = 0;
5087 struct btrfs_bio *bbio = NULL;
5088 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5089 int dev_replace_is_ongoing = 0;
5090 int num_alloc_stripes;
5091 int patch_the_first_stripe_for_dev_replace = 0;
5092 u64 physical_to_patch_in_first_stripe = 0;
5093 u64 raid56_full_stripe_start = (u64)-1;
5095 read_lock(&em_tree->lock);
5096 em = lookup_extent_mapping(em_tree, logical, *length);
5097 read_unlock(&em_tree->lock);
5100 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
5105 if (em->start > logical || em->start + em->len < logical) {
5106 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
5107 "found %Lu-%Lu", logical, em->start,
5108 em->start + em->len);
5109 free_extent_map(em);
5113 map = (struct map_lookup *)em->bdev;
5114 offset = logical - em->start;
5116 stripe_len = map->stripe_len;
5119 * stripe_nr counts the total number of stripes we have to stride
5120 * to get to this block
5122 stripe_nr = div64_u64(stripe_nr, stripe_len);
5124 stripe_offset = stripe_nr * stripe_len;
5125 BUG_ON(offset < stripe_offset);
5127 /* stripe_offset is the offset of this block in its stripe*/
5128 stripe_offset = offset - stripe_offset;
5130 /* if we're here for raid56, we need to know the stripe aligned start */
5131 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5132 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5133 raid56_full_stripe_start = offset;
5135 /* allow a write of a full stripe, but make sure we don't
5136 * allow straddling of stripes
5138 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5140 raid56_full_stripe_start *= full_stripe_len;
5143 if (rw & REQ_DISCARD) {
5144 /* we don't discard raid56 yet */
5145 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5149 *length = min_t(u64, em->len - offset, *length);
5150 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5152 /* For writes to RAID[56], allow a full stripeset across all disks.
5153 For other RAID types and for RAID[56] reads, just allow a single
5154 stripe (on a single disk). */
5155 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
5157 max_len = stripe_len * nr_data_stripes(map) -
5158 (offset - raid56_full_stripe_start);
5160 /* we limit the length of each bio to what fits in a stripe */
5161 max_len = stripe_len - stripe_offset;
5163 *length = min_t(u64, em->len - offset, max_len);
5165 *length = em->len - offset;
5168 /* This is for when we're called from btrfs_merge_bio_hook() and all
5169 it cares about is the length */
5173 btrfs_dev_replace_lock(dev_replace);
5174 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5175 if (!dev_replace_is_ongoing)
5176 btrfs_dev_replace_unlock(dev_replace);
5178 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5179 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5180 dev_replace->tgtdev != NULL) {
5182 * in dev-replace case, for repair case (that's the only
5183 * case where the mirror is selected explicitly when
5184 * calling btrfs_map_block), blocks left of the left cursor
5185 * can also be read from the target drive.
5186 * For REQ_GET_READ_MIRRORS, the target drive is added as
5187 * the last one to the array of stripes. For READ, it also
5188 * needs to be supported using the same mirror number.
5189 * If the requested block is not left of the left cursor,
5190 * EIO is returned. This can happen because btrfs_num_copies()
5191 * returns one more in the dev-replace case.
5193 u64 tmp_length = *length;
5194 struct btrfs_bio *tmp_bbio = NULL;
5195 int tmp_num_stripes;
5196 u64 srcdev_devid = dev_replace->srcdev->devid;
5197 int index_srcdev = 0;
5199 u64 physical_of_found = 0;
5201 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
5202 logical, &tmp_length, &tmp_bbio, 0, 0);
5204 WARN_ON(tmp_bbio != NULL);
5208 tmp_num_stripes = tmp_bbio->num_stripes;
5209 if (mirror_num > tmp_num_stripes) {
5211 * REQ_GET_READ_MIRRORS does not contain this
5212 * mirror, that means that the requested area
5213 * is not left of the left cursor
5216 btrfs_put_bbio(tmp_bbio);
5221 * process the rest of the function using the mirror_num
5222 * of the source drive. Therefore look it up first.
5223 * At the end, patch the device pointer to the one of the
5226 for (i = 0; i < tmp_num_stripes; i++) {
5227 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5229 * In case of DUP, in order to keep it
5230 * simple, only add the mirror with the
5231 * lowest physical address
5234 physical_of_found <=
5235 tmp_bbio->stripes[i].physical)
5240 tmp_bbio->stripes[i].physical;
5245 mirror_num = index_srcdev + 1;
5246 patch_the_first_stripe_for_dev_replace = 1;
5247 physical_to_patch_in_first_stripe = physical_of_found;
5251 btrfs_put_bbio(tmp_bbio);
5255 btrfs_put_bbio(tmp_bbio);
5256 } else if (mirror_num > map->num_stripes) {
5262 stripe_nr_orig = stripe_nr;
5263 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
5264 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
5265 stripe_end_offset = stripe_nr_end * map->stripe_len -
5268 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5269 if (rw & REQ_DISCARD)
5270 num_stripes = min_t(u64, map->num_stripes,
5271 stripe_nr_end - stripe_nr_orig);
5272 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5274 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5276 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
5277 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
5278 num_stripes = map->num_stripes;
5279 else if (mirror_num)
5280 stripe_index = mirror_num - 1;
5282 stripe_index = find_live_mirror(fs_info, map, 0,
5284 current->pid % map->num_stripes,
5285 dev_replace_is_ongoing);
5286 mirror_num = stripe_index + 1;
5289 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
5290 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
5291 num_stripes = map->num_stripes;
5292 } else if (mirror_num) {
5293 stripe_index = mirror_num - 1;
5298 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5299 u32 factor = map->num_stripes / map->sub_stripes;
5301 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5302 stripe_index *= map->sub_stripes;
5304 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5305 num_stripes = map->sub_stripes;
5306 else if (rw & REQ_DISCARD)
5307 num_stripes = min_t(u64, map->sub_stripes *
5308 (stripe_nr_end - stripe_nr_orig),
5310 else if (mirror_num)
5311 stripe_index += mirror_num - 1;
5313 int old_stripe_index = stripe_index;
5314 stripe_index = find_live_mirror(fs_info, map,
5316 map->sub_stripes, stripe_index +
5317 current->pid % map->sub_stripes,
5318 dev_replace_is_ongoing);
5319 mirror_num = stripe_index - old_stripe_index + 1;
5322 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5323 if (need_raid_map &&
5324 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5326 /* push stripe_nr back to the start of the full stripe */
5327 stripe_nr = div_u64(raid56_full_stripe_start,
5328 stripe_len * nr_data_stripes(map));
5330 /* RAID[56] write or recovery. Return all stripes */
5331 num_stripes = map->num_stripes;
5332 max_errors = nr_parity_stripes(map);
5334 *length = map->stripe_len;
5339 * Mirror #0 or #1 means the original data block.
5340 * Mirror #2 is RAID5 parity block.
5341 * Mirror #3 is RAID6 Q block.
5343 stripe_nr = div_u64_rem(stripe_nr,
5344 nr_data_stripes(map), &stripe_index);
5346 stripe_index = nr_data_stripes(map) +
5349 /* We distribute the parity blocks across stripes */
5350 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5352 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5353 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5358 * after this, stripe_nr is the number of stripes on this
5359 * device we have to walk to find the data, and stripe_index is
5360 * the number of our device in the stripe array
5362 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5364 mirror_num = stripe_index + 1;
5366 BUG_ON(stripe_index >= map->num_stripes);
5368 num_alloc_stripes = num_stripes;
5369 if (dev_replace_is_ongoing) {
5370 if (rw & (REQ_WRITE | REQ_DISCARD))
5371 num_alloc_stripes <<= 1;
5372 if (rw & REQ_GET_READ_MIRRORS)
5373 num_alloc_stripes++;
5374 tgtdev_indexes = num_stripes;
5377 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
5382 if (dev_replace_is_ongoing)
5383 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
5385 /* build raid_map */
5386 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
5387 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5392 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5393 sizeof(struct btrfs_bio_stripe) *
5395 sizeof(int) * tgtdev_indexes);
5397 /* Work out the disk rotation on this stripe-set */
5398 div_u64_rem(stripe_nr, num_stripes, &rot);
5400 /* Fill in the logical address of each stripe */
5401 tmp = stripe_nr * nr_data_stripes(map);
5402 for (i = 0; i < nr_data_stripes(map); i++)
5403 bbio->raid_map[(i+rot) % num_stripes] =
5404 em->start + (tmp + i) * map->stripe_len;
5406 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5407 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5408 bbio->raid_map[(i+rot+1) % num_stripes] =
5412 if (rw & REQ_DISCARD) {
5414 u32 sub_stripes = 0;
5415 u64 stripes_per_dev = 0;
5416 u32 remaining_stripes = 0;
5417 u32 last_stripe = 0;
5420 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5421 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5424 sub_stripes = map->sub_stripes;
5426 factor = map->num_stripes / sub_stripes;
5427 stripes_per_dev = div_u64_rem(stripe_nr_end -
5430 &remaining_stripes);
5431 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5432 last_stripe *= sub_stripes;
5435 for (i = 0; i < num_stripes; i++) {
5436 bbio->stripes[i].physical =
5437 map->stripes[stripe_index].physical +
5438 stripe_offset + stripe_nr * map->stripe_len;
5439 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5441 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5442 BTRFS_BLOCK_GROUP_RAID10)) {
5443 bbio->stripes[i].length = stripes_per_dev *
5446 if (i / sub_stripes < remaining_stripes)
5447 bbio->stripes[i].length +=
5451 * Special for the first stripe and
5454 * |-------|...|-------|
5458 if (i < sub_stripes)
5459 bbio->stripes[i].length -=
5462 if (stripe_index >= last_stripe &&
5463 stripe_index <= (last_stripe +
5465 bbio->stripes[i].length -=
5468 if (i == sub_stripes - 1)
5471 bbio->stripes[i].length = *length;
5474 if (stripe_index == map->num_stripes) {
5475 /* This could only happen for RAID0/10 */
5481 for (i = 0; i < num_stripes; i++) {
5482 bbio->stripes[i].physical =
5483 map->stripes[stripe_index].physical +
5485 stripe_nr * map->stripe_len;
5486 bbio->stripes[i].dev =
5487 map->stripes[stripe_index].dev;
5492 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5493 max_errors = btrfs_chunk_max_errors(map);
5496 sort_parity_stripes(bbio, num_stripes);
5499 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5500 dev_replace->tgtdev != NULL) {
5501 int index_where_to_add;
5502 u64 srcdev_devid = dev_replace->srcdev->devid;
5505 * duplicate the write operations while the dev replace
5506 * procedure is running. Since the copying of the old disk
5507 * to the new disk takes place at run time while the
5508 * filesystem is mounted writable, the regular write
5509 * operations to the old disk have to be duplicated to go
5510 * to the new disk as well.
5511 * Note that device->missing is handled by the caller, and
5512 * that the write to the old disk is already set up in the
5515 index_where_to_add = num_stripes;
5516 for (i = 0; i < num_stripes; i++) {
5517 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5518 /* write to new disk, too */
5519 struct btrfs_bio_stripe *new =
5520 bbio->stripes + index_where_to_add;
5521 struct btrfs_bio_stripe *old =
5524 new->physical = old->physical;
5525 new->length = old->length;
5526 new->dev = dev_replace->tgtdev;
5527 bbio->tgtdev_map[i] = index_where_to_add;
5528 index_where_to_add++;
5533 num_stripes = index_where_to_add;
5534 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5535 dev_replace->tgtdev != NULL) {
5536 u64 srcdev_devid = dev_replace->srcdev->devid;
5537 int index_srcdev = 0;
5539 u64 physical_of_found = 0;
5542 * During the dev-replace procedure, the target drive can
5543 * also be used to read data in case it is needed to repair
5544 * a corrupt block elsewhere. This is possible if the
5545 * requested area is left of the left cursor. In this area,
5546 * the target drive is a full copy of the source drive.
5548 for (i = 0; i < num_stripes; i++) {
5549 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5551 * In case of DUP, in order to keep it
5552 * simple, only add the mirror with the
5553 * lowest physical address
5556 physical_of_found <=
5557 bbio->stripes[i].physical)
5561 physical_of_found = bbio->stripes[i].physical;
5565 if (physical_of_found + map->stripe_len <=
5566 dev_replace->cursor_left) {
5567 struct btrfs_bio_stripe *tgtdev_stripe =
5568 bbio->stripes + num_stripes;
5570 tgtdev_stripe->physical = physical_of_found;
5571 tgtdev_stripe->length =
5572 bbio->stripes[index_srcdev].length;
5573 tgtdev_stripe->dev = dev_replace->tgtdev;
5574 bbio->tgtdev_map[index_srcdev] = num_stripes;
5583 bbio->map_type = map->type;
5584 bbio->num_stripes = num_stripes;
5585 bbio->max_errors = max_errors;
5586 bbio->mirror_num = mirror_num;
5587 bbio->num_tgtdevs = tgtdev_indexes;
5590 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5591 * mirror_num == num_stripes + 1 && dev_replace target drive is
5592 * available as a mirror
5594 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5595 WARN_ON(num_stripes > 1);
5596 bbio->stripes[0].dev = dev_replace->tgtdev;
5597 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5598 bbio->mirror_num = map->num_stripes + 1;
5601 if (dev_replace_is_ongoing)
5602 btrfs_dev_replace_unlock(dev_replace);
5603 free_extent_map(em);
5607 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5608 u64 logical, u64 *length,
5609 struct btrfs_bio **bbio_ret, int mirror_num)
5611 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5615 /* For Scrub/replace */
5616 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5617 u64 logical, u64 *length,
5618 struct btrfs_bio **bbio_ret, int mirror_num,
5621 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5622 mirror_num, need_raid_map);
5625 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5626 u64 chunk_start, u64 physical, u64 devid,
5627 u64 **logical, int *naddrs, int *stripe_len)
5629 struct extent_map_tree *em_tree = &map_tree->map_tree;
5630 struct extent_map *em;
5631 struct map_lookup *map;
5639 read_lock(&em_tree->lock);
5640 em = lookup_extent_mapping(em_tree, chunk_start, 1);
5641 read_unlock(&em_tree->lock);
5644 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
5649 if (em->start != chunk_start) {
5650 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5651 em->start, chunk_start);
5652 free_extent_map(em);
5655 map = (struct map_lookup *)em->bdev;
5658 rmap_len = map->stripe_len;
5660 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5661 length = div_u64(length, map->num_stripes / map->sub_stripes);
5662 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5663 length = div_u64(length, map->num_stripes);
5664 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5665 length = div_u64(length, nr_data_stripes(map));
5666 rmap_len = map->stripe_len * nr_data_stripes(map);
5669 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
5670 BUG_ON(!buf); /* -ENOMEM */
5672 for (i = 0; i < map->num_stripes; i++) {
5673 if (devid && map->stripes[i].dev->devid != devid)
5675 if (map->stripes[i].physical > physical ||
5676 map->stripes[i].physical + length <= physical)
5679 stripe_nr = physical - map->stripes[i].physical;
5680 stripe_nr = div_u64(stripe_nr, map->stripe_len);
5682 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5683 stripe_nr = stripe_nr * map->num_stripes + i;
5684 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
5685 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5686 stripe_nr = stripe_nr * map->num_stripes + i;
5687 } /* else if RAID[56], multiply by nr_data_stripes().
5688 * Alternatively, just use rmap_len below instead of
5689 * map->stripe_len */
5691 bytenr = chunk_start + stripe_nr * rmap_len;
5692 WARN_ON(nr >= map->num_stripes);
5693 for (j = 0; j < nr; j++) {
5694 if (buf[j] == bytenr)
5698 WARN_ON(nr >= map->num_stripes);
5705 *stripe_len = rmap_len;
5707 free_extent_map(em);
5711 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
5713 bio->bi_private = bbio->private;
5714 bio->bi_end_io = bbio->end_io;
5717 btrfs_put_bbio(bbio);
5720 static void btrfs_end_bio(struct bio *bio)
5722 struct btrfs_bio *bbio = bio->bi_private;
5723 int is_orig_bio = 0;
5725 if (bio->bi_error) {
5726 atomic_inc(&bbio->error);
5727 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
5728 unsigned int stripe_index =
5729 btrfs_io_bio(bio)->stripe_index;
5730 struct btrfs_device *dev;
5732 BUG_ON(stripe_index >= bbio->num_stripes);
5733 dev = bbio->stripes[stripe_index].dev;
5735 if (bio->bi_rw & WRITE)
5736 btrfs_dev_stat_inc(dev,
5737 BTRFS_DEV_STAT_WRITE_ERRS);
5739 btrfs_dev_stat_inc(dev,
5740 BTRFS_DEV_STAT_READ_ERRS);
5741 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5742 btrfs_dev_stat_inc(dev,
5743 BTRFS_DEV_STAT_FLUSH_ERRS);
5744 btrfs_dev_stat_print_on_error(dev);
5749 if (bio == bbio->orig_bio)
5752 btrfs_bio_counter_dec(bbio->fs_info);
5754 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5757 bio = bbio->orig_bio;
5760 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5761 /* only send an error to the higher layers if it is
5762 * beyond the tolerance of the btrfs bio
5764 if (atomic_read(&bbio->error) > bbio->max_errors) {
5765 bio->bi_error = -EIO;
5768 * this bio is actually up to date, we didn't
5769 * go over the max number of errors
5774 btrfs_end_bbio(bbio, bio);
5775 } else if (!is_orig_bio) {
5781 * see run_scheduled_bios for a description of why bios are collected for
5784 * This will add one bio to the pending list for a device and make sure
5785 * the work struct is scheduled.
5787 static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5788 struct btrfs_device *device,
5789 int rw, struct bio *bio)
5791 int should_queue = 1;
5792 struct btrfs_pending_bios *pending_bios;
5794 if (device->missing || !device->bdev) {
5799 /* don't bother with additional async steps for reads, right now */
5800 if (!(rw & REQ_WRITE)) {
5802 btrfsic_submit_bio(rw, bio);
5808 * nr_async_bios allows us to reliably return congestion to the
5809 * higher layers. Otherwise, the async bio makes it appear we have
5810 * made progress against dirty pages when we've really just put it
5811 * on a queue for later
5813 atomic_inc(&root->fs_info->nr_async_bios);
5814 WARN_ON(bio->bi_next);
5815 bio->bi_next = NULL;
5818 spin_lock(&device->io_lock);
5819 if (bio->bi_rw & REQ_SYNC)
5820 pending_bios = &device->pending_sync_bios;
5822 pending_bios = &device->pending_bios;
5824 if (pending_bios->tail)
5825 pending_bios->tail->bi_next = bio;
5827 pending_bios->tail = bio;
5828 if (!pending_bios->head)
5829 pending_bios->head = bio;
5830 if (device->running_pending)
5833 spin_unlock(&device->io_lock);
5836 btrfs_queue_work(root->fs_info->submit_workers,
5840 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5841 struct bio *bio, u64 physical, int dev_nr,
5844 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5846 bio->bi_private = bbio;
5847 btrfs_io_bio(bio)->stripe_index = dev_nr;
5848 bio->bi_end_io = btrfs_end_bio;
5849 bio->bi_iter.bi_sector = physical >> 9;
5852 struct rcu_string *name;
5855 name = rcu_dereference(dev->name);
5856 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5857 "(%s id %llu), size=%u\n", rw,
5858 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5859 name->str, dev->devid, bio->bi_iter.bi_size);
5863 bio->bi_bdev = dev->bdev;
5865 btrfs_bio_counter_inc_noblocked(root->fs_info);
5868 btrfs_schedule_bio(root, dev, rw, bio);
5870 btrfsic_submit_bio(rw, bio);
5873 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5875 atomic_inc(&bbio->error);
5876 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5877 /* Shoud be the original bio. */
5878 WARN_ON(bio != bbio->orig_bio);
5880 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5881 bio->bi_iter.bi_sector = logical >> 9;
5882 bio->bi_error = -EIO;
5883 btrfs_end_bbio(bbio, bio);
5887 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5888 int mirror_num, int async_submit)
5890 struct btrfs_device *dev;
5891 struct bio *first_bio = bio;
5892 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
5898 struct btrfs_bio *bbio = NULL;
5900 length = bio->bi_iter.bi_size;
5901 map_length = length;
5903 btrfs_bio_counter_inc_blocked(root->fs_info);
5904 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5907 btrfs_bio_counter_dec(root->fs_info);
5911 total_devs = bbio->num_stripes;
5912 bbio->orig_bio = first_bio;
5913 bbio->private = first_bio->bi_private;
5914 bbio->end_io = first_bio->bi_end_io;
5915 bbio->fs_info = root->fs_info;
5916 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5918 if (bbio->raid_map) {
5919 /* In this case, map_length has been set to the length of
5920 a single stripe; not the whole write */
5922 ret = raid56_parity_write(root, bio, bbio, map_length);
5924 ret = raid56_parity_recover(root, bio, bbio, map_length,
5928 btrfs_bio_counter_dec(root->fs_info);
5932 if (map_length < length) {
5933 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5934 logical, length, map_length);
5938 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
5939 dev = bbio->stripes[dev_nr].dev;
5940 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5941 bbio_error(bbio, first_bio, logical);
5945 if (dev_nr < total_devs - 1) {
5946 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
5947 BUG_ON(!bio); /* -ENOMEM */
5951 submit_stripe_bio(root, bbio, bio,
5952 bbio->stripes[dev_nr].physical, dev_nr, rw,
5955 btrfs_bio_counter_dec(root->fs_info);
5959 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5962 struct btrfs_device *device;
5963 struct btrfs_fs_devices *cur_devices;
5965 cur_devices = fs_info->fs_devices;
5966 while (cur_devices) {
5968 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5969 device = __find_device(&cur_devices->devices,
5974 cur_devices = cur_devices->seed;
5979 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5980 struct btrfs_fs_devices *fs_devices,
5981 u64 devid, u8 *dev_uuid)
5983 struct btrfs_device *device;
5985 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5989 list_add(&device->dev_list, &fs_devices->devices);
5990 device->fs_devices = fs_devices;
5991 fs_devices->num_devices++;
5993 device->missing = 1;
5994 fs_devices->missing_devices++;
6000 * btrfs_alloc_device - allocate struct btrfs_device
6001 * @fs_info: used only for generating a new devid, can be NULL if
6002 * devid is provided (i.e. @devid != NULL).
6003 * @devid: a pointer to devid for this device. If NULL a new devid
6005 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6008 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6009 * on error. Returned struct is not linked onto any lists and can be
6010 * destroyed with kfree() right away.
6012 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6016 struct btrfs_device *dev;
6019 if (WARN_ON(!devid && !fs_info))
6020 return ERR_PTR(-EINVAL);
6022 dev = __alloc_device();
6031 ret = find_next_devid(fs_info, &tmp);
6034 return ERR_PTR(ret);
6040 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6042 generate_random_uuid(dev->uuid);
6044 btrfs_init_work(&dev->work, btrfs_submit_helper,
6045 pending_bios_fn, NULL, NULL);
6050 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6051 struct extent_buffer *leaf,
6052 struct btrfs_chunk *chunk)
6054 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6055 struct map_lookup *map;
6056 struct extent_map *em;
6060 u8 uuid[BTRFS_UUID_SIZE];
6065 logical = key->offset;
6066 length = btrfs_chunk_length(leaf, chunk);
6068 read_lock(&map_tree->map_tree.lock);
6069 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
6070 read_unlock(&map_tree->map_tree.lock);
6072 /* already mapped? */
6073 if (em && em->start <= logical && em->start + em->len > logical) {
6074 free_extent_map(em);
6077 free_extent_map(em);
6080 em = alloc_extent_map();
6083 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6084 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6086 free_extent_map(em);
6090 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6091 em->bdev = (struct block_device *)map;
6092 em->start = logical;
6095 em->block_start = 0;
6096 em->block_len = em->len;
6098 map->num_stripes = num_stripes;
6099 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6100 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6101 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6102 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6103 map->type = btrfs_chunk_type(leaf, chunk);
6104 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6105 for (i = 0; i < num_stripes; i++) {
6106 map->stripes[i].physical =
6107 btrfs_stripe_offset_nr(leaf, chunk, i);
6108 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6109 read_extent_buffer(leaf, uuid, (unsigned long)
6110 btrfs_stripe_dev_uuid_nr(chunk, i),
6112 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6114 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
6115 free_extent_map(em);
6118 if (!map->stripes[i].dev) {
6119 map->stripes[i].dev =
6120 add_missing_dev(root, root->fs_info->fs_devices,
6122 if (!map->stripes[i].dev) {
6123 free_extent_map(em);
6126 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6129 map->stripes[i].dev->in_fs_metadata = 1;
6132 write_lock(&map_tree->map_tree.lock);
6133 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
6134 write_unlock(&map_tree->map_tree.lock);
6135 BUG_ON(ret); /* Tree corruption */
6136 free_extent_map(em);
6141 static void fill_device_from_item(struct extent_buffer *leaf,
6142 struct btrfs_dev_item *dev_item,
6143 struct btrfs_device *device)
6147 device->devid = btrfs_device_id(leaf, dev_item);
6148 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6149 device->total_bytes = device->disk_total_bytes;
6150 device->commit_total_bytes = device->disk_total_bytes;
6151 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6152 device->commit_bytes_used = device->bytes_used;
6153 device->type = btrfs_device_type(leaf, dev_item);
6154 device->io_align = btrfs_device_io_align(leaf, dev_item);
6155 device->io_width = btrfs_device_io_width(leaf, dev_item);
6156 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6157 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6158 device->is_tgtdev_for_dev_replace = 0;
6160 ptr = btrfs_device_uuid(dev_item);
6161 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6164 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6167 struct btrfs_fs_devices *fs_devices;
6170 BUG_ON(!mutex_is_locked(&uuid_mutex));
6172 fs_devices = root->fs_info->fs_devices->seed;
6173 while (fs_devices) {
6174 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6177 fs_devices = fs_devices->seed;
6180 fs_devices = find_fsid(fsid);
6182 if (!btrfs_test_opt(root, DEGRADED))
6183 return ERR_PTR(-ENOENT);
6185 fs_devices = alloc_fs_devices(fsid);
6186 if (IS_ERR(fs_devices))
6189 fs_devices->seeding = 1;
6190 fs_devices->opened = 1;
6194 fs_devices = clone_fs_devices(fs_devices);
6195 if (IS_ERR(fs_devices))
6198 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
6199 root->fs_info->bdev_holder);
6201 free_fs_devices(fs_devices);
6202 fs_devices = ERR_PTR(ret);
6206 if (!fs_devices->seeding) {
6207 __btrfs_close_devices(fs_devices);
6208 free_fs_devices(fs_devices);
6209 fs_devices = ERR_PTR(-EINVAL);
6213 fs_devices->seed = root->fs_info->fs_devices->seed;
6214 root->fs_info->fs_devices->seed = fs_devices;
6219 static int read_one_dev(struct btrfs_root *root,
6220 struct extent_buffer *leaf,
6221 struct btrfs_dev_item *dev_item)
6223 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6224 struct btrfs_device *device;
6227 u8 fs_uuid[BTRFS_UUID_SIZE];
6228 u8 dev_uuid[BTRFS_UUID_SIZE];
6230 devid = btrfs_device_id(leaf, dev_item);
6231 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
6233 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6236 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6237 fs_devices = open_seed_devices(root, fs_uuid);
6238 if (IS_ERR(fs_devices))
6239 return PTR_ERR(fs_devices);
6242 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
6244 if (!btrfs_test_opt(root, DEGRADED))
6247 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6250 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6253 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6256 if(!device->bdev && !device->missing) {
6258 * this happens when a device that was properly setup
6259 * in the device info lists suddenly goes bad.
6260 * device->bdev is NULL, and so we have to set
6261 * device->missing to one here
6263 device->fs_devices->missing_devices++;
6264 device->missing = 1;
6267 /* Move the device to its own fs_devices */
6268 if (device->fs_devices != fs_devices) {
6269 ASSERT(device->missing);
6271 list_move(&device->dev_list, &fs_devices->devices);
6272 device->fs_devices->num_devices--;
6273 fs_devices->num_devices++;
6275 device->fs_devices->missing_devices--;
6276 fs_devices->missing_devices++;
6278 device->fs_devices = fs_devices;
6282 if (device->fs_devices != root->fs_info->fs_devices) {
6283 BUG_ON(device->writeable);
6284 if (device->generation !=
6285 btrfs_device_generation(leaf, dev_item))
6289 fill_device_from_item(leaf, dev_item, device);
6290 device->in_fs_metadata = 1;
6291 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
6292 device->fs_devices->total_rw_bytes += device->total_bytes;
6293 spin_lock(&root->fs_info->free_chunk_lock);
6294 root->fs_info->free_chunk_space += device->total_bytes -
6296 spin_unlock(&root->fs_info->free_chunk_lock);
6302 int btrfs_read_sys_array(struct btrfs_root *root)
6304 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
6305 struct extent_buffer *sb;
6306 struct btrfs_disk_key *disk_key;
6307 struct btrfs_chunk *chunk;
6309 unsigned long sb_array_offset;
6315 struct btrfs_key key;
6317 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6319 * This will create extent buffer of nodesize, superblock size is
6320 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6321 * overallocate but we can keep it as-is, only the first page is used.
6323 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
6326 btrfs_set_buffer_uptodate(sb);
6327 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6329 * The sb extent buffer is artifical and just used to read the system array.
6330 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6331 * pages up-to-date when the page is larger: extent does not cover the
6332 * whole page and consequently check_page_uptodate does not find all
6333 * the page's extents up-to-date (the hole beyond sb),
6334 * write_extent_buffer then triggers a WARN_ON.
6336 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6337 * but sb spans only this function. Add an explicit SetPageUptodate call
6338 * to silence the warning eg. on PowerPC 64.
6340 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
6341 SetPageUptodate(sb->pages[0]);
6343 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
6344 array_size = btrfs_super_sys_array_size(super_copy);
6346 array_ptr = super_copy->sys_chunk_array;
6347 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6350 while (cur_offset < array_size) {
6351 disk_key = (struct btrfs_disk_key *)array_ptr;
6352 len = sizeof(*disk_key);
6353 if (cur_offset + len > array_size)
6354 goto out_short_read;
6356 btrfs_disk_key_to_cpu(&key, disk_key);
6359 sb_array_offset += len;
6362 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
6363 chunk = (struct btrfs_chunk *)sb_array_offset;
6365 * At least one btrfs_chunk with one stripe must be
6366 * present, exact stripe count check comes afterwards
6368 len = btrfs_chunk_item_size(1);
6369 if (cur_offset + len > array_size)
6370 goto out_short_read;
6372 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6373 len = btrfs_chunk_item_size(num_stripes);
6374 if (cur_offset + len > array_size)
6375 goto out_short_read;
6377 ret = read_one_chunk(root, &key, sb, chunk);
6385 sb_array_offset += len;
6388 free_extent_buffer(sb);
6392 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6394 free_extent_buffer(sb);
6398 int btrfs_read_chunk_tree(struct btrfs_root *root)
6400 struct btrfs_path *path;
6401 struct extent_buffer *leaf;
6402 struct btrfs_key key;
6403 struct btrfs_key found_key;
6407 root = root->fs_info->chunk_root;
6409 path = btrfs_alloc_path();
6413 mutex_lock(&uuid_mutex);
6417 * Read all device items, and then all the chunk items. All
6418 * device items are found before any chunk item (their object id
6419 * is smaller than the lowest possible object id for a chunk
6420 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6422 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6425 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6429 leaf = path->nodes[0];
6430 slot = path->slots[0];
6431 if (slot >= btrfs_header_nritems(leaf)) {
6432 ret = btrfs_next_leaf(root, path);
6439 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6440 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6441 struct btrfs_dev_item *dev_item;
6442 dev_item = btrfs_item_ptr(leaf, slot,
6443 struct btrfs_dev_item);
6444 ret = read_one_dev(root, leaf, dev_item);
6447 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6448 struct btrfs_chunk *chunk;
6449 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6450 ret = read_one_chunk(root, &found_key, leaf, chunk);
6458 unlock_chunks(root);
6459 mutex_unlock(&uuid_mutex);
6461 btrfs_free_path(path);
6465 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6467 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6468 struct btrfs_device *device;
6470 while (fs_devices) {
6471 mutex_lock(&fs_devices->device_list_mutex);
6472 list_for_each_entry(device, &fs_devices->devices, dev_list)
6473 device->dev_root = fs_info->dev_root;
6474 mutex_unlock(&fs_devices->device_list_mutex);
6476 fs_devices = fs_devices->seed;
6480 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6484 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6485 btrfs_dev_stat_reset(dev, i);
6488 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6490 struct btrfs_key key;
6491 struct btrfs_key found_key;
6492 struct btrfs_root *dev_root = fs_info->dev_root;
6493 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6494 struct extent_buffer *eb;
6497 struct btrfs_device *device;
6498 struct btrfs_path *path = NULL;
6501 path = btrfs_alloc_path();
6507 mutex_lock(&fs_devices->device_list_mutex);
6508 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6510 struct btrfs_dev_stats_item *ptr;
6513 key.type = BTRFS_DEV_STATS_KEY;
6514 key.offset = device->devid;
6515 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6517 __btrfs_reset_dev_stats(device);
6518 device->dev_stats_valid = 1;
6519 btrfs_release_path(path);
6522 slot = path->slots[0];
6523 eb = path->nodes[0];
6524 btrfs_item_key_to_cpu(eb, &found_key, slot);
6525 item_size = btrfs_item_size_nr(eb, slot);
6527 ptr = btrfs_item_ptr(eb, slot,
6528 struct btrfs_dev_stats_item);
6530 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6531 if (item_size >= (1 + i) * sizeof(__le64))
6532 btrfs_dev_stat_set(device, i,
6533 btrfs_dev_stats_value(eb, ptr, i));
6535 btrfs_dev_stat_reset(device, i);
6538 device->dev_stats_valid = 1;
6539 btrfs_dev_stat_print_on_load(device);
6540 btrfs_release_path(path);
6542 mutex_unlock(&fs_devices->device_list_mutex);
6545 btrfs_free_path(path);
6546 return ret < 0 ? ret : 0;
6549 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6550 struct btrfs_root *dev_root,
6551 struct btrfs_device *device)
6553 struct btrfs_path *path;
6554 struct btrfs_key key;
6555 struct extent_buffer *eb;
6556 struct btrfs_dev_stats_item *ptr;
6561 key.type = BTRFS_DEV_STATS_KEY;
6562 key.offset = device->devid;
6564 path = btrfs_alloc_path();
6566 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6568 btrfs_warn_in_rcu(dev_root->fs_info,
6569 "error %d while searching for dev_stats item for device %s",
6570 ret, rcu_str_deref(device->name));
6575 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6576 /* need to delete old one and insert a new one */
6577 ret = btrfs_del_item(trans, dev_root, path);
6579 btrfs_warn_in_rcu(dev_root->fs_info,
6580 "delete too small dev_stats item for device %s failed %d",
6581 rcu_str_deref(device->name), ret);
6588 /* need to insert a new item */
6589 btrfs_release_path(path);
6590 ret = btrfs_insert_empty_item(trans, dev_root, path,
6591 &key, sizeof(*ptr));
6593 btrfs_warn_in_rcu(dev_root->fs_info,
6594 "insert dev_stats item for device %s failed %d",
6595 rcu_str_deref(device->name), ret);
6600 eb = path->nodes[0];
6601 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6602 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6603 btrfs_set_dev_stats_value(eb, ptr, i,
6604 btrfs_dev_stat_read(device, i));
6605 btrfs_mark_buffer_dirty(eb);
6608 btrfs_free_path(path);
6613 * called from commit_transaction. Writes all changed device stats to disk.
6615 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6616 struct btrfs_fs_info *fs_info)
6618 struct btrfs_root *dev_root = fs_info->dev_root;
6619 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6620 struct btrfs_device *device;
6624 mutex_lock(&fs_devices->device_list_mutex);
6625 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6626 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
6629 stats_cnt = atomic_read(&device->dev_stats_ccnt);
6630 ret = update_dev_stat_item(trans, dev_root, device);
6632 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
6634 mutex_unlock(&fs_devices->device_list_mutex);
6639 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6641 btrfs_dev_stat_inc(dev, index);
6642 btrfs_dev_stat_print_on_error(dev);
6645 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
6647 if (!dev->dev_stats_valid)
6649 btrfs_err_rl_in_rcu(dev->dev_root->fs_info,
6650 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
6651 rcu_str_deref(dev->name),
6652 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6653 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6654 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6655 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6656 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6659 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6663 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6664 if (btrfs_dev_stat_read(dev, i) != 0)
6666 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6667 return; /* all values == 0, suppress message */
6669 btrfs_info_in_rcu(dev->dev_root->fs_info,
6670 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
6671 rcu_str_deref(dev->name),
6672 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6673 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6674 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6675 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6676 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6679 int btrfs_get_dev_stats(struct btrfs_root *root,
6680 struct btrfs_ioctl_get_dev_stats *stats)
6682 struct btrfs_device *dev;
6683 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6686 mutex_lock(&fs_devices->device_list_mutex);
6687 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
6688 mutex_unlock(&fs_devices->device_list_mutex);
6691 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
6693 } else if (!dev->dev_stats_valid) {
6694 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
6696 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
6697 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6698 if (stats->nr_items > i)
6700 btrfs_dev_stat_read_and_reset(dev, i);
6702 btrfs_dev_stat_reset(dev, i);
6705 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6706 if (stats->nr_items > i)
6707 stats->values[i] = btrfs_dev_stat_read(dev, i);
6709 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6710 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6714 void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path)
6716 struct buffer_head *bh;
6717 struct btrfs_super_block *disk_super;
6723 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
6726 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
6729 disk_super = (struct btrfs_super_block *)bh->b_data;
6731 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6732 set_buffer_dirty(bh);
6733 sync_dirty_buffer(bh);
6737 /* Notify udev that device has changed */
6738 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
6740 /* Update ctime/mtime for device path for libblkid */
6741 update_dev_time(device_path);
6745 * Update the size of all devices, which is used for writing out the
6748 void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6750 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6751 struct btrfs_device *curr, *next;
6753 if (list_empty(&fs_devices->resized_devices))
6756 mutex_lock(&fs_devices->device_list_mutex);
6757 lock_chunks(fs_info->dev_root);
6758 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6760 list_del_init(&curr->resized_list);
6761 curr->commit_total_bytes = curr->disk_total_bytes;
6763 unlock_chunks(fs_info->dev_root);
6764 mutex_unlock(&fs_devices->device_list_mutex);
6767 /* Must be invoked during the transaction commit */
6768 void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6769 struct btrfs_transaction *transaction)
6771 struct extent_map *em;
6772 struct map_lookup *map;
6773 struct btrfs_device *dev;
6776 if (list_empty(&transaction->pending_chunks))
6779 /* In order to kick the device replace finish process */
6781 list_for_each_entry(em, &transaction->pending_chunks, list) {
6782 map = (struct map_lookup *)em->bdev;
6784 for (i = 0; i < map->num_stripes; i++) {
6785 dev = map->stripes[i].dev;
6786 dev->commit_bytes_used = dev->bytes_used;
6789 unlock_chunks(root);
6792 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6794 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6795 while (fs_devices) {
6796 fs_devices->fs_info = fs_info;
6797 fs_devices = fs_devices->seed;
6801 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6803 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6804 while (fs_devices) {
6805 fs_devices->fs_info = NULL;
6806 fs_devices = fs_devices->seed;
6810 void btrfs_close_one_device(struct btrfs_device *device)
6812 struct btrfs_fs_devices *fs_devices = device->fs_devices;
6813 struct btrfs_device *new_device;
6814 struct rcu_string *name;
6817 fs_devices->open_devices--;
6819 if (device->writeable &&
6820 device->devid != BTRFS_DEV_REPLACE_DEVID) {
6821 list_del_init(&device->dev_alloc_list);
6822 fs_devices->rw_devices--;
6825 if (device->missing)
6826 fs_devices->missing_devices--;
6828 new_device = btrfs_alloc_device(NULL, &device->devid,
6830 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
6832 /* Safe because we are under uuid_mutex */
6834 name = rcu_string_strdup(device->name->str, GFP_NOFS);
6835 BUG_ON(!name); /* -ENOMEM */
6836 rcu_assign_pointer(new_device->name, name);
6839 list_replace_rcu(&device->dev_list, &new_device->dev_list);
6840 new_device->fs_devices = device->fs_devices;
6842 call_rcu(&device->rcu, free_device);