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);
56 static struct btrfs_fs_devices *__alloc_fs_devices(void)
58 struct btrfs_fs_devices *fs_devs;
60 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
62 return ERR_PTR(-ENOMEM);
64 mutex_init(&fs_devs->device_list_mutex);
66 INIT_LIST_HEAD(&fs_devs->devices);
67 INIT_LIST_HEAD(&fs_devs->resized_devices);
68 INIT_LIST_HEAD(&fs_devs->alloc_list);
69 INIT_LIST_HEAD(&fs_devs->list);
75 * alloc_fs_devices - allocate struct btrfs_fs_devices
76 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
79 * Return: a pointer to a new &struct btrfs_fs_devices on success;
80 * ERR_PTR() on error. Returned struct is not linked onto any lists and
81 * can be destroyed with kfree() right away.
83 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
85 struct btrfs_fs_devices *fs_devs;
87 fs_devs = __alloc_fs_devices();
92 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
94 generate_random_uuid(fs_devs->fsid);
99 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
101 struct btrfs_device *device;
102 WARN_ON(fs_devices->opened);
103 while (!list_empty(&fs_devices->devices)) {
104 device = list_entry(fs_devices->devices.next,
105 struct btrfs_device, dev_list);
106 list_del(&device->dev_list);
107 rcu_string_free(device->name);
113 static void btrfs_kobject_uevent(struct block_device *bdev,
114 enum kobject_action action)
118 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
120 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
122 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
123 &disk_to_dev(bdev->bd_disk)->kobj);
126 void btrfs_cleanup_fs_uuids(void)
128 struct btrfs_fs_devices *fs_devices;
130 while (!list_empty(&fs_uuids)) {
131 fs_devices = list_entry(fs_uuids.next,
132 struct btrfs_fs_devices, list);
133 list_del(&fs_devices->list);
134 free_fs_devices(fs_devices);
138 static struct btrfs_device *__alloc_device(void)
140 struct btrfs_device *dev;
142 dev = kzalloc(sizeof(*dev), GFP_NOFS);
144 return ERR_PTR(-ENOMEM);
146 INIT_LIST_HEAD(&dev->dev_list);
147 INIT_LIST_HEAD(&dev->dev_alloc_list);
148 INIT_LIST_HEAD(&dev->resized_list);
150 spin_lock_init(&dev->io_lock);
152 spin_lock_init(&dev->reada_lock);
153 atomic_set(&dev->reada_in_flight, 0);
154 atomic_set(&dev->dev_stats_ccnt, 0);
155 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
156 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
161 static noinline struct btrfs_device *__find_device(struct list_head *head,
164 struct btrfs_device *dev;
166 list_for_each_entry(dev, head, dev_list) {
167 if (dev->devid == devid &&
168 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
175 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
177 struct btrfs_fs_devices *fs_devices;
179 list_for_each_entry(fs_devices, &fs_uuids, list) {
180 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
187 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
188 int flush, struct block_device **bdev,
189 struct buffer_head **bh)
193 *bdev = blkdev_get_by_path(device_path, flags, holder);
196 ret = PTR_ERR(*bdev);
197 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
202 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
203 ret = set_blocksize(*bdev, 4096);
205 blkdev_put(*bdev, flags);
208 invalidate_bdev(*bdev);
209 *bh = btrfs_read_dev_super(*bdev);
212 blkdev_put(*bdev, flags);
224 static void requeue_list(struct btrfs_pending_bios *pending_bios,
225 struct bio *head, struct bio *tail)
228 struct bio *old_head;
230 old_head = pending_bios->head;
231 pending_bios->head = head;
232 if (pending_bios->tail)
233 tail->bi_next = old_head;
235 pending_bios->tail = tail;
239 * we try to collect pending bios for a device so we don't get a large
240 * number of procs sending bios down to the same device. This greatly
241 * improves the schedulers ability to collect and merge the bios.
243 * But, it also turns into a long list of bios to process and that is sure
244 * to eventually make the worker thread block. The solution here is to
245 * make some progress and then put this work struct back at the end of
246 * the list if the block device is congested. This way, multiple devices
247 * can make progress from a single worker thread.
249 static noinline void run_scheduled_bios(struct btrfs_device *device)
252 struct backing_dev_info *bdi;
253 struct btrfs_fs_info *fs_info;
254 struct btrfs_pending_bios *pending_bios;
258 unsigned long num_run;
259 unsigned long batch_run = 0;
261 unsigned long last_waited = 0;
263 int sync_pending = 0;
264 struct blk_plug plug;
267 * this function runs all the bios we've collected for
268 * a particular device. We don't want to wander off to
269 * another device without first sending all of these down.
270 * So, setup a plug here and finish it off before we return
272 blk_start_plug(&plug);
274 bdi = blk_get_backing_dev_info(device->bdev);
275 fs_info = device->dev_root->fs_info;
276 limit = btrfs_async_submit_limit(fs_info);
277 limit = limit * 2 / 3;
280 spin_lock(&device->io_lock);
285 /* take all the bios off the list at once and process them
286 * later on (without the lock held). But, remember the
287 * tail and other pointers so the bios can be properly reinserted
288 * into the list if we hit congestion
290 if (!force_reg && device->pending_sync_bios.head) {
291 pending_bios = &device->pending_sync_bios;
294 pending_bios = &device->pending_bios;
298 pending = pending_bios->head;
299 tail = pending_bios->tail;
300 WARN_ON(pending && !tail);
303 * if pending was null this time around, no bios need processing
304 * at all and we can stop. Otherwise it'll loop back up again
305 * and do an additional check so no bios are missed.
307 * device->running_pending is used to synchronize with the
310 if (device->pending_sync_bios.head == NULL &&
311 device->pending_bios.head == NULL) {
313 device->running_pending = 0;
316 device->running_pending = 1;
319 pending_bios->head = NULL;
320 pending_bios->tail = NULL;
322 spin_unlock(&device->io_lock);
327 /* we want to work on both lists, but do more bios on the
328 * sync list than the regular list
331 pending_bios != &device->pending_sync_bios &&
332 device->pending_sync_bios.head) ||
333 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
334 device->pending_bios.head)) {
335 spin_lock(&device->io_lock);
336 requeue_list(pending_bios, pending, tail);
341 pending = pending->bi_next;
344 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
345 waitqueue_active(&fs_info->async_submit_wait))
346 wake_up(&fs_info->async_submit_wait);
348 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
351 * if we're doing the sync list, record that our
352 * plug has some sync requests on it
354 * If we're doing the regular list and there are
355 * sync requests sitting around, unplug before
358 if (pending_bios == &device->pending_sync_bios) {
360 } else if (sync_pending) {
361 blk_finish_plug(&plug);
362 blk_start_plug(&plug);
366 btrfsic_submit_bio(cur->bi_rw, cur);
373 * we made progress, there is more work to do and the bdi
374 * is now congested. Back off and let other work structs
377 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
378 fs_info->fs_devices->open_devices > 1) {
379 struct io_context *ioc;
381 ioc = current->io_context;
384 * the main goal here is that we don't want to
385 * block if we're going to be able to submit
386 * more requests without blocking.
388 * This code does two great things, it pokes into
389 * the elevator code from a filesystem _and_
390 * it makes assumptions about how batching works.
392 if (ioc && ioc->nr_batch_requests > 0 &&
393 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
395 ioc->last_waited == last_waited)) {
397 * we want to go through our batch of
398 * requests and stop. So, we copy out
399 * the ioc->last_waited time and test
400 * against it before looping
402 last_waited = ioc->last_waited;
407 spin_lock(&device->io_lock);
408 requeue_list(pending_bios, pending, tail);
409 device->running_pending = 1;
411 spin_unlock(&device->io_lock);
412 btrfs_queue_work(fs_info->submit_workers,
416 /* unplug every 64 requests just for good measure */
417 if (batch_run % 64 == 0) {
418 blk_finish_plug(&plug);
419 blk_start_plug(&plug);
428 spin_lock(&device->io_lock);
429 if (device->pending_bios.head || device->pending_sync_bios.head)
431 spin_unlock(&device->io_lock);
434 blk_finish_plug(&plug);
437 static void pending_bios_fn(struct btrfs_work *work)
439 struct btrfs_device *device;
441 device = container_of(work, struct btrfs_device, work);
442 run_scheduled_bios(device);
446 * Add new device to list of registered devices
449 * 1 - first time device is seen
450 * 0 - device already known
453 static noinline int device_list_add(const char *path,
454 struct btrfs_super_block *disk_super,
455 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
457 struct btrfs_device *device;
458 struct btrfs_fs_devices *fs_devices;
459 struct rcu_string *name;
461 u64 found_transid = btrfs_super_generation(disk_super);
463 fs_devices = find_fsid(disk_super->fsid);
465 fs_devices = alloc_fs_devices(disk_super->fsid);
466 if (IS_ERR(fs_devices))
467 return PTR_ERR(fs_devices);
469 list_add(&fs_devices->list, &fs_uuids);
473 device = __find_device(&fs_devices->devices, devid,
474 disk_super->dev_item.uuid);
478 if (fs_devices->opened)
481 device = btrfs_alloc_device(NULL, &devid,
482 disk_super->dev_item.uuid);
483 if (IS_ERR(device)) {
484 /* we can safely leave the fs_devices entry around */
485 return PTR_ERR(device);
488 name = rcu_string_strdup(path, GFP_NOFS);
493 rcu_assign_pointer(device->name, name);
495 mutex_lock(&fs_devices->device_list_mutex);
496 list_add_rcu(&device->dev_list, &fs_devices->devices);
497 fs_devices->num_devices++;
498 mutex_unlock(&fs_devices->device_list_mutex);
501 device->fs_devices = fs_devices;
502 } else if (!device->name || strcmp(device->name->str, path)) {
504 * When FS is already mounted.
505 * 1. If you are here and if the device->name is NULL that
506 * means this device was missing at time of FS mount.
507 * 2. If you are here and if the device->name is different
508 * from 'path' that means either
509 * a. The same device disappeared and reappeared with
511 * b. The missing-disk-which-was-replaced, has
514 * We must allow 1 and 2a above. But 2b would be a spurious
517 * Further in case of 1 and 2a above, the disk at 'path'
518 * would have missed some transaction when it was away and
519 * in case of 2a the stale bdev has to be updated as well.
520 * 2b must not be allowed at all time.
524 * For now, we do allow update to btrfs_fs_device through the
525 * btrfs dev scan cli after FS has been mounted. We're still
526 * tracking a problem where systems fail mount by subvolume id
527 * when we reject replacement on a mounted FS.
529 if (!fs_devices->opened && found_transid < device->generation) {
531 * That is if the FS is _not_ mounted and if you
532 * are here, that means there is more than one
533 * disk with same uuid and devid.We keep the one
534 * with larger generation number or the last-in if
535 * generation are equal.
540 name = rcu_string_strdup(path, GFP_NOFS);
543 rcu_string_free(device->name);
544 rcu_assign_pointer(device->name, name);
545 if (device->missing) {
546 fs_devices->missing_devices--;
552 * Unmount does not free the btrfs_device struct but would zero
553 * generation along with most of the other members. So just update
554 * it back. We need it to pick the disk with largest generation
557 if (!fs_devices->opened)
558 device->generation = found_transid;
560 *fs_devices_ret = fs_devices;
565 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
567 struct btrfs_fs_devices *fs_devices;
568 struct btrfs_device *device;
569 struct btrfs_device *orig_dev;
571 fs_devices = alloc_fs_devices(orig->fsid);
572 if (IS_ERR(fs_devices))
575 mutex_lock(&orig->device_list_mutex);
576 fs_devices->total_devices = orig->total_devices;
578 /* We have held the volume lock, it is safe to get the devices. */
579 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
580 struct rcu_string *name;
582 device = btrfs_alloc_device(NULL, &orig_dev->devid,
588 * This is ok to do without rcu read locked because we hold the
589 * uuid mutex so nothing we touch in here is going to disappear.
591 if (orig_dev->name) {
592 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
597 rcu_assign_pointer(device->name, name);
600 list_add(&device->dev_list, &fs_devices->devices);
601 device->fs_devices = fs_devices;
602 fs_devices->num_devices++;
604 mutex_unlock(&orig->device_list_mutex);
607 mutex_unlock(&orig->device_list_mutex);
608 free_fs_devices(fs_devices);
609 return ERR_PTR(-ENOMEM);
612 void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
613 struct btrfs_fs_devices *fs_devices, int step)
615 struct btrfs_device *device, *next;
616 struct btrfs_device *latest_dev = NULL;
618 mutex_lock(&uuid_mutex);
620 /* This is the initialized path, it is safe to release the devices. */
621 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
622 if (device->in_fs_metadata) {
623 if (!device->is_tgtdev_for_dev_replace &&
625 device->generation > latest_dev->generation)) {
631 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
633 * In the first step, keep the device which has
634 * the correct fsid and the devid that is used
635 * for the dev_replace procedure.
636 * In the second step, the dev_replace state is
637 * read from the device tree and it is known
638 * whether the procedure is really active or
639 * not, which means whether this device is
640 * used or whether it should be removed.
642 if (step == 0 || device->is_tgtdev_for_dev_replace) {
647 blkdev_put(device->bdev, device->mode);
649 fs_devices->open_devices--;
651 if (device->writeable) {
652 list_del_init(&device->dev_alloc_list);
653 device->writeable = 0;
654 if (!device->is_tgtdev_for_dev_replace)
655 fs_devices->rw_devices--;
657 list_del_init(&device->dev_list);
658 fs_devices->num_devices--;
659 rcu_string_free(device->name);
663 if (fs_devices->seed) {
664 fs_devices = fs_devices->seed;
668 fs_devices->latest_bdev = latest_dev->bdev;
670 mutex_unlock(&uuid_mutex);
673 static void __free_device(struct work_struct *work)
675 struct btrfs_device *device;
677 device = container_of(work, struct btrfs_device, rcu_work);
680 blkdev_put(device->bdev, device->mode);
682 rcu_string_free(device->name);
686 static void free_device(struct rcu_head *head)
688 struct btrfs_device *device;
690 device = container_of(head, struct btrfs_device, rcu);
692 INIT_WORK(&device->rcu_work, __free_device);
693 schedule_work(&device->rcu_work);
696 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
698 struct btrfs_device *device;
700 if (--fs_devices->opened > 0)
703 mutex_lock(&fs_devices->device_list_mutex);
704 list_for_each_entry(device, &fs_devices->devices, dev_list) {
705 struct btrfs_device *new_device;
706 struct rcu_string *name;
709 fs_devices->open_devices--;
711 if (device->writeable &&
712 device->devid != BTRFS_DEV_REPLACE_DEVID) {
713 list_del_init(&device->dev_alloc_list);
714 fs_devices->rw_devices--;
718 fs_devices->missing_devices--;
720 new_device = btrfs_alloc_device(NULL, &device->devid,
722 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
724 /* Safe because we are under uuid_mutex */
726 name = rcu_string_strdup(device->name->str, GFP_NOFS);
727 BUG_ON(!name); /* -ENOMEM */
728 rcu_assign_pointer(new_device->name, name);
731 list_replace_rcu(&device->dev_list, &new_device->dev_list);
732 new_device->fs_devices = device->fs_devices;
734 call_rcu(&device->rcu, free_device);
736 mutex_unlock(&fs_devices->device_list_mutex);
738 WARN_ON(fs_devices->open_devices);
739 WARN_ON(fs_devices->rw_devices);
740 fs_devices->opened = 0;
741 fs_devices->seeding = 0;
746 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
748 struct btrfs_fs_devices *seed_devices = NULL;
751 mutex_lock(&uuid_mutex);
752 ret = __btrfs_close_devices(fs_devices);
753 if (!fs_devices->opened) {
754 seed_devices = fs_devices->seed;
755 fs_devices->seed = NULL;
757 mutex_unlock(&uuid_mutex);
759 while (seed_devices) {
760 fs_devices = seed_devices;
761 seed_devices = fs_devices->seed;
762 __btrfs_close_devices(fs_devices);
763 free_fs_devices(fs_devices);
766 * Wait for rcu kworkers under __btrfs_close_devices
767 * to finish all blkdev_puts so device is really
768 * free when umount is done.
774 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
775 fmode_t flags, void *holder)
777 struct request_queue *q;
778 struct block_device *bdev;
779 struct list_head *head = &fs_devices->devices;
780 struct btrfs_device *device;
781 struct btrfs_device *latest_dev = NULL;
782 struct buffer_head *bh;
783 struct btrfs_super_block *disk_super;
790 list_for_each_entry(device, head, dev_list) {
796 /* Just open everything we can; ignore failures here */
797 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
801 disk_super = (struct btrfs_super_block *)bh->b_data;
802 devid = btrfs_stack_device_id(&disk_super->dev_item);
803 if (devid != device->devid)
806 if (memcmp(device->uuid, disk_super->dev_item.uuid,
810 device->generation = btrfs_super_generation(disk_super);
812 device->generation > latest_dev->generation)
815 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
816 device->writeable = 0;
818 device->writeable = !bdev_read_only(bdev);
822 q = bdev_get_queue(bdev);
823 if (blk_queue_discard(q))
824 device->can_discard = 1;
827 device->in_fs_metadata = 0;
828 device->mode = flags;
830 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
831 fs_devices->rotating = 1;
833 fs_devices->open_devices++;
834 if (device->writeable &&
835 device->devid != BTRFS_DEV_REPLACE_DEVID) {
836 fs_devices->rw_devices++;
837 list_add(&device->dev_alloc_list,
838 &fs_devices->alloc_list);
845 blkdev_put(bdev, flags);
848 if (fs_devices->open_devices == 0) {
852 fs_devices->seeding = seeding;
853 fs_devices->opened = 1;
854 fs_devices->latest_bdev = latest_dev->bdev;
855 fs_devices->total_rw_bytes = 0;
860 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
861 fmode_t flags, void *holder)
865 mutex_lock(&uuid_mutex);
866 if (fs_devices->opened) {
867 fs_devices->opened++;
870 ret = __btrfs_open_devices(fs_devices, flags, holder);
872 mutex_unlock(&uuid_mutex);
877 * Look for a btrfs signature on a device. This may be called out of the mount path
878 * and we are not allowed to call set_blocksize during the scan. The superblock
879 * is read via pagecache
881 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
882 struct btrfs_fs_devices **fs_devices_ret)
884 struct btrfs_super_block *disk_super;
885 struct block_device *bdev;
896 * we would like to check all the supers, but that would make
897 * a btrfs mount succeed after a mkfs from a different FS.
898 * So, we need to add a special mount option to scan for
899 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
901 bytenr = btrfs_sb_offset(0);
903 mutex_lock(&uuid_mutex);
905 bdev = blkdev_get_by_path(path, flags, holder);
912 /* make sure our super fits in the device */
913 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
916 /* make sure our super fits in the page */
917 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
920 /* make sure our super doesn't straddle pages on disk */
921 index = bytenr >> PAGE_CACHE_SHIFT;
922 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
925 /* pull in the page with our super */
926 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
929 if (IS_ERR_OR_NULL(page))
934 /* align our pointer to the offset of the super block */
935 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
937 if (btrfs_super_bytenr(disk_super) != bytenr ||
938 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
941 devid = btrfs_stack_device_id(&disk_super->dev_item);
942 transid = btrfs_super_generation(disk_super);
943 total_devices = btrfs_super_num_devices(disk_super);
945 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
947 if (disk_super->label[0]) {
948 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
949 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
950 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
952 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
955 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
958 if (!ret && fs_devices_ret)
959 (*fs_devices_ret)->total_devices = total_devices;
963 page_cache_release(page);
966 blkdev_put(bdev, flags);
968 mutex_unlock(&uuid_mutex);
972 /* helper to account the used device space in the range */
973 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
974 u64 end, u64 *length)
976 struct btrfs_key key;
977 struct btrfs_root *root = device->dev_root;
978 struct btrfs_dev_extent *dev_extent;
979 struct btrfs_path *path;
983 struct extent_buffer *l;
987 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
990 path = btrfs_alloc_path();
995 key.objectid = device->devid;
997 key.type = BTRFS_DEV_EXTENT_KEY;
999 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1003 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1010 slot = path->slots[0];
1011 if (slot >= btrfs_header_nritems(l)) {
1012 ret = btrfs_next_leaf(root, path);
1020 btrfs_item_key_to_cpu(l, &key, slot);
1022 if (key.objectid < device->devid)
1025 if (key.objectid > device->devid)
1028 if (key.type != BTRFS_DEV_EXTENT_KEY)
1031 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1032 extent_end = key.offset + btrfs_dev_extent_length(l,
1034 if (key.offset <= start && extent_end > end) {
1035 *length = end - start + 1;
1037 } else if (key.offset <= start && extent_end > start)
1038 *length += extent_end - start;
1039 else if (key.offset > start && extent_end <= end)
1040 *length += extent_end - key.offset;
1041 else if (key.offset > start && key.offset <= end) {
1042 *length += end - key.offset + 1;
1044 } else if (key.offset > end)
1052 btrfs_free_path(path);
1056 static int contains_pending_extent(struct btrfs_trans_handle *trans,
1057 struct btrfs_device *device,
1058 u64 *start, u64 len)
1060 struct extent_map *em;
1061 struct list_head *search_list = &trans->transaction->pending_chunks;
1065 list_for_each_entry(em, search_list, list) {
1066 struct map_lookup *map;
1069 map = (struct map_lookup *)em->bdev;
1070 for (i = 0; i < map->num_stripes; i++) {
1071 if (map->stripes[i].dev != device)
1073 if (map->stripes[i].physical >= *start + len ||
1074 map->stripes[i].physical + em->orig_block_len <=
1077 *start = map->stripes[i].physical +
1082 if (search_list == &trans->transaction->pending_chunks) {
1083 search_list = &trans->root->fs_info->pinned_chunks;
1092 * find_free_dev_extent - find free space in the specified device
1093 * @device: the device which we search the free space in
1094 * @num_bytes: the size of the free space that we need
1095 * @start: store the start of the free space.
1096 * @len: the size of the free space. that we find, or the size of the max
1097 * free space if we don't find suitable free space
1099 * this uses a pretty simple search, the expectation is that it is
1100 * called very infrequently and that a given device has a small number
1103 * @start is used to store the start of the free space if we find. But if we
1104 * don't find suitable free space, it will be used to store the start position
1105 * of the max free space.
1107 * @len is used to store the size of the free space that we find.
1108 * But if we don't find suitable free space, it is used to store the size of
1109 * the max free space.
1111 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1112 struct btrfs_device *device, u64 num_bytes,
1113 u64 *start, u64 *len)
1115 struct btrfs_key key;
1116 struct btrfs_root *root = device->dev_root;
1117 struct btrfs_dev_extent *dev_extent;
1118 struct btrfs_path *path;
1124 u64 search_end = device->total_bytes;
1127 struct extent_buffer *l;
1129 /* FIXME use last free of some kind */
1131 /* we don't want to overwrite the superblock on the drive,
1132 * so we make sure to start at an offset of at least 1MB
1134 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1136 path = btrfs_alloc_path();
1140 max_hole_start = search_start;
1144 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1150 path->search_commit_root = 1;
1151 path->skip_locking = 1;
1153 key.objectid = device->devid;
1154 key.offset = search_start;
1155 key.type = BTRFS_DEV_EXTENT_KEY;
1157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1161 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1168 slot = path->slots[0];
1169 if (slot >= btrfs_header_nritems(l)) {
1170 ret = btrfs_next_leaf(root, path);
1178 btrfs_item_key_to_cpu(l, &key, slot);
1180 if (key.objectid < device->devid)
1183 if (key.objectid > device->devid)
1186 if (key.type != BTRFS_DEV_EXTENT_KEY)
1189 if (key.offset > search_start) {
1190 hole_size = key.offset - search_start;
1193 * Have to check before we set max_hole_start, otherwise
1194 * we could end up sending back this offset anyway.
1196 if (contains_pending_extent(trans, device,
1201 if (hole_size > max_hole_size) {
1202 max_hole_start = search_start;
1203 max_hole_size = hole_size;
1207 * If this free space is greater than which we need,
1208 * it must be the max free space that we have found
1209 * until now, so max_hole_start must point to the start
1210 * of this free space and the length of this free space
1211 * is stored in max_hole_size. Thus, we return
1212 * max_hole_start and max_hole_size and go back to the
1215 if (hole_size >= num_bytes) {
1221 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1222 extent_end = key.offset + btrfs_dev_extent_length(l,
1224 if (extent_end > search_start)
1225 search_start = extent_end;
1232 * At this point, search_start should be the end of
1233 * allocated dev extents, and when shrinking the device,
1234 * search_end may be smaller than search_start.
1236 if (search_end > search_start)
1237 hole_size = search_end - search_start;
1239 if (hole_size > max_hole_size) {
1240 max_hole_start = search_start;
1241 max_hole_size = hole_size;
1244 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1245 btrfs_release_path(path);
1250 if (hole_size < num_bytes)
1256 btrfs_free_path(path);
1257 *start = max_hole_start;
1259 *len = max_hole_size;
1263 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1264 struct btrfs_device *device,
1265 u64 start, u64 *dev_extent_len)
1268 struct btrfs_path *path;
1269 struct btrfs_root *root = device->dev_root;
1270 struct btrfs_key key;
1271 struct btrfs_key found_key;
1272 struct extent_buffer *leaf = NULL;
1273 struct btrfs_dev_extent *extent = NULL;
1275 path = btrfs_alloc_path();
1279 key.objectid = device->devid;
1281 key.type = BTRFS_DEV_EXTENT_KEY;
1283 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1285 ret = btrfs_previous_item(root, path, key.objectid,
1286 BTRFS_DEV_EXTENT_KEY);
1289 leaf = path->nodes[0];
1290 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1291 extent = btrfs_item_ptr(leaf, path->slots[0],
1292 struct btrfs_dev_extent);
1293 BUG_ON(found_key.offset > start || found_key.offset +
1294 btrfs_dev_extent_length(leaf, extent) < start);
1296 btrfs_release_path(path);
1298 } else if (ret == 0) {
1299 leaf = path->nodes[0];
1300 extent = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_dev_extent);
1303 btrfs_error(root->fs_info, ret, "Slot search failed");
1307 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1309 ret = btrfs_del_item(trans, root, path);
1311 btrfs_error(root->fs_info, ret,
1312 "Failed to remove dev extent item");
1314 trans->transaction->have_free_bgs = 1;
1317 btrfs_free_path(path);
1321 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1322 struct btrfs_device *device,
1323 u64 chunk_tree, u64 chunk_objectid,
1324 u64 chunk_offset, u64 start, u64 num_bytes)
1327 struct btrfs_path *path;
1328 struct btrfs_root *root = device->dev_root;
1329 struct btrfs_dev_extent *extent;
1330 struct extent_buffer *leaf;
1331 struct btrfs_key key;
1333 WARN_ON(!device->in_fs_metadata);
1334 WARN_ON(device->is_tgtdev_for_dev_replace);
1335 path = btrfs_alloc_path();
1339 key.objectid = device->devid;
1341 key.type = BTRFS_DEV_EXTENT_KEY;
1342 ret = btrfs_insert_empty_item(trans, root, path, &key,
1347 leaf = path->nodes[0];
1348 extent = btrfs_item_ptr(leaf, path->slots[0],
1349 struct btrfs_dev_extent);
1350 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1351 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1352 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1354 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1355 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
1357 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1358 btrfs_mark_buffer_dirty(leaf);
1360 btrfs_free_path(path);
1364 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1366 struct extent_map_tree *em_tree;
1367 struct extent_map *em;
1371 em_tree = &fs_info->mapping_tree.map_tree;
1372 read_lock(&em_tree->lock);
1373 n = rb_last(&em_tree->map);
1375 em = rb_entry(n, struct extent_map, rb_node);
1376 ret = em->start + em->len;
1378 read_unlock(&em_tree->lock);
1383 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1387 struct btrfs_key key;
1388 struct btrfs_key found_key;
1389 struct btrfs_path *path;
1391 path = btrfs_alloc_path();
1395 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1396 key.type = BTRFS_DEV_ITEM_KEY;
1397 key.offset = (u64)-1;
1399 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1403 BUG_ON(ret == 0); /* Corruption */
1405 ret = btrfs_previous_item(fs_info->chunk_root, path,
1406 BTRFS_DEV_ITEMS_OBJECTID,
1407 BTRFS_DEV_ITEM_KEY);
1411 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1413 *devid_ret = found_key.offset + 1;
1417 btrfs_free_path(path);
1422 * the device information is stored in the chunk root
1423 * the btrfs_device struct should be fully filled in
1425 static int btrfs_add_device(struct btrfs_trans_handle *trans,
1426 struct btrfs_root *root,
1427 struct btrfs_device *device)
1430 struct btrfs_path *path;
1431 struct btrfs_dev_item *dev_item;
1432 struct extent_buffer *leaf;
1433 struct btrfs_key key;
1436 root = root->fs_info->chunk_root;
1438 path = btrfs_alloc_path();
1442 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1443 key.type = BTRFS_DEV_ITEM_KEY;
1444 key.offset = device->devid;
1446 ret = btrfs_insert_empty_item(trans, root, path, &key,
1451 leaf = path->nodes[0];
1452 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1454 btrfs_set_device_id(leaf, dev_item, device->devid);
1455 btrfs_set_device_generation(leaf, dev_item, 0);
1456 btrfs_set_device_type(leaf, dev_item, device->type);
1457 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1458 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1459 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1460 btrfs_set_device_total_bytes(leaf, dev_item,
1461 btrfs_device_get_disk_total_bytes(device));
1462 btrfs_set_device_bytes_used(leaf, dev_item,
1463 btrfs_device_get_bytes_used(device));
1464 btrfs_set_device_group(leaf, dev_item, 0);
1465 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1466 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1467 btrfs_set_device_start_offset(leaf, dev_item, 0);
1469 ptr = btrfs_device_uuid(dev_item);
1470 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1471 ptr = btrfs_device_fsid(dev_item);
1472 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1473 btrfs_mark_buffer_dirty(leaf);
1477 btrfs_free_path(path);
1482 * Function to update ctime/mtime for a given device path.
1483 * Mainly used for ctime/mtime based probe like libblkid.
1485 static void update_dev_time(char *path_name)
1489 filp = filp_open(path_name, O_RDWR, 0);
1492 file_update_time(filp);
1493 filp_close(filp, NULL);
1497 static int btrfs_rm_dev_item(struct btrfs_root *root,
1498 struct btrfs_device *device)
1501 struct btrfs_path *path;
1502 struct btrfs_key key;
1503 struct btrfs_trans_handle *trans;
1505 root = root->fs_info->chunk_root;
1507 path = btrfs_alloc_path();
1511 trans = btrfs_start_transaction(root, 0);
1512 if (IS_ERR(trans)) {
1513 btrfs_free_path(path);
1514 return PTR_ERR(trans);
1516 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1517 key.type = BTRFS_DEV_ITEM_KEY;
1518 key.offset = device->devid;
1520 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1529 ret = btrfs_del_item(trans, root, path);
1533 btrfs_free_path(path);
1534 btrfs_commit_transaction(trans, root);
1538 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1540 struct btrfs_device *device;
1541 struct btrfs_device *next_device;
1542 struct block_device *bdev;
1543 struct buffer_head *bh = NULL;
1544 struct btrfs_super_block *disk_super;
1545 struct btrfs_fs_devices *cur_devices;
1552 bool clear_super = false;
1554 mutex_lock(&uuid_mutex);
1557 seq = read_seqbegin(&root->fs_info->profiles_lock);
1559 all_avail = root->fs_info->avail_data_alloc_bits |
1560 root->fs_info->avail_system_alloc_bits |
1561 root->fs_info->avail_metadata_alloc_bits;
1562 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1564 num_devices = root->fs_info->fs_devices->num_devices;
1565 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1566 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1567 WARN_ON(num_devices < 1);
1570 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1572 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1573 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
1577 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1578 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
1582 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1583 root->fs_info->fs_devices->rw_devices <= 2) {
1584 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
1587 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1588 root->fs_info->fs_devices->rw_devices <= 3) {
1589 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
1593 if (strcmp(device_path, "missing") == 0) {
1594 struct list_head *devices;
1595 struct btrfs_device *tmp;
1598 devices = &root->fs_info->fs_devices->devices;
1600 * It is safe to read the devices since the volume_mutex
1603 list_for_each_entry(tmp, devices, dev_list) {
1604 if (tmp->in_fs_metadata &&
1605 !tmp->is_tgtdev_for_dev_replace &&
1615 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1619 ret = btrfs_get_bdev_and_sb(device_path,
1620 FMODE_WRITE | FMODE_EXCL,
1621 root->fs_info->bdev_holder, 0,
1625 disk_super = (struct btrfs_super_block *)bh->b_data;
1626 devid = btrfs_stack_device_id(&disk_super->dev_item);
1627 dev_uuid = disk_super->dev_item.uuid;
1628 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1636 if (device->is_tgtdev_for_dev_replace) {
1637 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1641 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1642 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1646 if (device->writeable) {
1648 list_del_init(&device->dev_alloc_list);
1649 device->fs_devices->rw_devices--;
1650 unlock_chunks(root);
1654 mutex_unlock(&uuid_mutex);
1655 ret = btrfs_shrink_device(device, 0);
1656 mutex_lock(&uuid_mutex);
1661 * TODO: the superblock still includes this device in its num_devices
1662 * counter although write_all_supers() is not locked out. This
1663 * could give a filesystem state which requires a degraded mount.
1665 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1669 device->in_fs_metadata = 0;
1670 btrfs_scrub_cancel_dev(root->fs_info, device);
1673 * the device list mutex makes sure that we don't change
1674 * the device list while someone else is writing out all
1675 * the device supers. Whoever is writing all supers, should
1676 * lock the device list mutex before getting the number of
1677 * devices in the super block (super_copy). Conversely,
1678 * whoever updates the number of devices in the super block
1679 * (super_copy) should hold the device list mutex.
1682 cur_devices = device->fs_devices;
1683 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1684 list_del_rcu(&device->dev_list);
1686 device->fs_devices->num_devices--;
1687 device->fs_devices->total_devices--;
1689 if (device->missing)
1690 device->fs_devices->missing_devices--;
1692 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1693 struct btrfs_device, dev_list);
1694 if (device->bdev == root->fs_info->sb->s_bdev)
1695 root->fs_info->sb->s_bdev = next_device->bdev;
1696 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1697 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1700 device->fs_devices->open_devices--;
1701 /* remove sysfs entry */
1702 btrfs_kobj_rm_device(root->fs_info, device);
1705 call_rcu(&device->rcu, free_device);
1707 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1708 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1709 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1711 if (cur_devices->open_devices == 0) {
1712 struct btrfs_fs_devices *fs_devices;
1713 fs_devices = root->fs_info->fs_devices;
1714 while (fs_devices) {
1715 if (fs_devices->seed == cur_devices) {
1716 fs_devices->seed = cur_devices->seed;
1719 fs_devices = fs_devices->seed;
1721 cur_devices->seed = NULL;
1722 __btrfs_close_devices(cur_devices);
1723 free_fs_devices(cur_devices);
1726 root->fs_info->num_tolerated_disk_barrier_failures =
1727 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1730 * at this point, the device is zero sized. We want to
1731 * remove it from the devices list and zero out the old super
1733 if (clear_super && disk_super) {
1737 /* make sure this device isn't detected as part of
1740 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1741 set_buffer_dirty(bh);
1742 sync_dirty_buffer(bh);
1744 /* clear the mirror copies of super block on the disk
1745 * being removed, 0th copy is been taken care above and
1746 * the below would take of the rest
1748 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1749 bytenr = btrfs_sb_offset(i);
1750 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1751 i_size_read(bdev->bd_inode))
1755 bh = __bread(bdev, bytenr / 4096,
1756 BTRFS_SUPER_INFO_SIZE);
1760 disk_super = (struct btrfs_super_block *)bh->b_data;
1762 if (btrfs_super_bytenr(disk_super) != bytenr ||
1763 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1766 memset(&disk_super->magic, 0,
1767 sizeof(disk_super->magic));
1768 set_buffer_dirty(bh);
1769 sync_dirty_buffer(bh);
1776 /* Notify udev that device has changed */
1777 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1779 /* Update ctime/mtime for device path for libblkid */
1780 update_dev_time(device_path);
1786 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1788 mutex_unlock(&uuid_mutex);
1791 if (device->writeable) {
1793 list_add(&device->dev_alloc_list,
1794 &root->fs_info->fs_devices->alloc_list);
1795 device->fs_devices->rw_devices++;
1796 unlock_chunks(root);
1801 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1802 struct btrfs_device *srcdev)
1804 struct btrfs_fs_devices *fs_devices;
1806 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1809 * in case of fs with no seed, srcdev->fs_devices will point
1810 * to fs_devices of fs_info. However when the dev being replaced is
1811 * a seed dev it will point to the seed's local fs_devices. In short
1812 * srcdev will have its correct fs_devices in both the cases.
1814 fs_devices = srcdev->fs_devices;
1816 list_del_rcu(&srcdev->dev_list);
1817 list_del_rcu(&srcdev->dev_alloc_list);
1818 fs_devices->num_devices--;
1819 if (srcdev->missing)
1820 fs_devices->missing_devices--;
1822 if (srcdev->writeable) {
1823 fs_devices->rw_devices--;
1824 /* zero out the old super if it is writable */
1825 btrfs_scratch_superblock(srcdev);
1829 fs_devices->open_devices--;
1832 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1833 struct btrfs_device *srcdev)
1835 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
1837 call_rcu(&srcdev->rcu, free_device);
1840 * unless fs_devices is seed fs, num_devices shouldn't go
1843 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1845 /* if this is no devs we rather delete the fs_devices */
1846 if (!fs_devices->num_devices) {
1847 struct btrfs_fs_devices *tmp_fs_devices;
1849 tmp_fs_devices = fs_info->fs_devices;
1850 while (tmp_fs_devices) {
1851 if (tmp_fs_devices->seed == fs_devices) {
1852 tmp_fs_devices->seed = fs_devices->seed;
1855 tmp_fs_devices = tmp_fs_devices->seed;
1857 fs_devices->seed = NULL;
1858 __btrfs_close_devices(fs_devices);
1859 free_fs_devices(fs_devices);
1863 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1864 struct btrfs_device *tgtdev)
1866 struct btrfs_device *next_device;
1868 mutex_lock(&uuid_mutex);
1870 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1872 btrfs_scratch_superblock(tgtdev);
1873 fs_info->fs_devices->open_devices--;
1875 fs_info->fs_devices->num_devices--;
1877 next_device = list_entry(fs_info->fs_devices->devices.next,
1878 struct btrfs_device, dev_list);
1879 if (tgtdev->bdev == fs_info->sb->s_bdev)
1880 fs_info->sb->s_bdev = next_device->bdev;
1881 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1882 fs_info->fs_devices->latest_bdev = next_device->bdev;
1883 list_del_rcu(&tgtdev->dev_list);
1885 call_rcu(&tgtdev->rcu, free_device);
1887 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1888 mutex_unlock(&uuid_mutex);
1891 static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1892 struct btrfs_device **device)
1895 struct btrfs_super_block *disk_super;
1898 struct block_device *bdev;
1899 struct buffer_head *bh;
1902 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1903 root->fs_info->bdev_holder, 0, &bdev, &bh);
1906 disk_super = (struct btrfs_super_block *)bh->b_data;
1907 devid = btrfs_stack_device_id(&disk_super->dev_item);
1908 dev_uuid = disk_super->dev_item.uuid;
1909 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1914 blkdev_put(bdev, FMODE_READ);
1918 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1920 struct btrfs_device **device)
1923 if (strcmp(device_path, "missing") == 0) {
1924 struct list_head *devices;
1925 struct btrfs_device *tmp;
1927 devices = &root->fs_info->fs_devices->devices;
1929 * It is safe to read the devices since the volume_mutex
1930 * is held by the caller.
1932 list_for_each_entry(tmp, devices, dev_list) {
1933 if (tmp->in_fs_metadata && !tmp->bdev) {
1940 btrfs_err(root->fs_info, "no missing device found");
1946 return btrfs_find_device_by_path(root, device_path, device);
1951 * does all the dirty work required for changing file system's UUID.
1953 static int btrfs_prepare_sprout(struct btrfs_root *root)
1955 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1956 struct btrfs_fs_devices *old_devices;
1957 struct btrfs_fs_devices *seed_devices;
1958 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1959 struct btrfs_device *device;
1962 BUG_ON(!mutex_is_locked(&uuid_mutex));
1963 if (!fs_devices->seeding)
1966 seed_devices = __alloc_fs_devices();
1967 if (IS_ERR(seed_devices))
1968 return PTR_ERR(seed_devices);
1970 old_devices = clone_fs_devices(fs_devices);
1971 if (IS_ERR(old_devices)) {
1972 kfree(seed_devices);
1973 return PTR_ERR(old_devices);
1976 list_add(&old_devices->list, &fs_uuids);
1978 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1979 seed_devices->opened = 1;
1980 INIT_LIST_HEAD(&seed_devices->devices);
1981 INIT_LIST_HEAD(&seed_devices->alloc_list);
1982 mutex_init(&seed_devices->device_list_mutex);
1984 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1985 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1987 list_for_each_entry(device, &seed_devices->devices, dev_list)
1988 device->fs_devices = seed_devices;
1991 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1992 unlock_chunks(root);
1994 fs_devices->seeding = 0;
1995 fs_devices->num_devices = 0;
1996 fs_devices->open_devices = 0;
1997 fs_devices->missing_devices = 0;
1998 fs_devices->rotating = 0;
1999 fs_devices->seed = seed_devices;
2001 generate_random_uuid(fs_devices->fsid);
2002 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2003 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2004 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2006 super_flags = btrfs_super_flags(disk_super) &
2007 ~BTRFS_SUPER_FLAG_SEEDING;
2008 btrfs_set_super_flags(disk_super, super_flags);
2014 * strore the expected generation for seed devices in device items.
2016 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2017 struct btrfs_root *root)
2019 struct btrfs_path *path;
2020 struct extent_buffer *leaf;
2021 struct btrfs_dev_item *dev_item;
2022 struct btrfs_device *device;
2023 struct btrfs_key key;
2024 u8 fs_uuid[BTRFS_UUID_SIZE];
2025 u8 dev_uuid[BTRFS_UUID_SIZE];
2029 path = btrfs_alloc_path();
2033 root = root->fs_info->chunk_root;
2034 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2036 key.type = BTRFS_DEV_ITEM_KEY;
2039 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2043 leaf = path->nodes[0];
2045 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2046 ret = btrfs_next_leaf(root, path);
2051 leaf = path->nodes[0];
2052 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2053 btrfs_release_path(path);
2057 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2058 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2059 key.type != BTRFS_DEV_ITEM_KEY)
2062 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2063 struct btrfs_dev_item);
2064 devid = btrfs_device_id(leaf, dev_item);
2065 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2067 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2069 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2071 BUG_ON(!device); /* Logic error */
2073 if (device->fs_devices->seeding) {
2074 btrfs_set_device_generation(leaf, dev_item,
2075 device->generation);
2076 btrfs_mark_buffer_dirty(leaf);
2084 btrfs_free_path(path);
2088 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2090 struct request_queue *q;
2091 struct btrfs_trans_handle *trans;
2092 struct btrfs_device *device;
2093 struct block_device *bdev;
2094 struct list_head *devices;
2095 struct super_block *sb = root->fs_info->sb;
2096 struct rcu_string *name;
2098 int seeding_dev = 0;
2101 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
2104 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2105 root->fs_info->bdev_holder);
2107 return PTR_ERR(bdev);
2109 if (root->fs_info->fs_devices->seeding) {
2111 down_write(&sb->s_umount);
2112 mutex_lock(&uuid_mutex);
2115 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2117 devices = &root->fs_info->fs_devices->devices;
2119 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2120 list_for_each_entry(device, devices, dev_list) {
2121 if (device->bdev == bdev) {
2124 &root->fs_info->fs_devices->device_list_mutex);
2128 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2130 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2131 if (IS_ERR(device)) {
2132 /* we can safely leave the fs_devices entry around */
2133 ret = PTR_ERR(device);
2137 name = rcu_string_strdup(device_path, GFP_NOFS);
2143 rcu_assign_pointer(device->name, name);
2145 trans = btrfs_start_transaction(root, 0);
2146 if (IS_ERR(trans)) {
2147 rcu_string_free(device->name);
2149 ret = PTR_ERR(trans);
2153 q = bdev_get_queue(bdev);
2154 if (blk_queue_discard(q))
2155 device->can_discard = 1;
2156 device->writeable = 1;
2157 device->generation = trans->transid;
2158 device->io_width = root->sectorsize;
2159 device->io_align = root->sectorsize;
2160 device->sector_size = root->sectorsize;
2161 device->total_bytes = i_size_read(bdev->bd_inode);
2162 device->disk_total_bytes = device->total_bytes;
2163 device->commit_total_bytes = device->total_bytes;
2164 device->dev_root = root->fs_info->dev_root;
2165 device->bdev = bdev;
2166 device->in_fs_metadata = 1;
2167 device->is_tgtdev_for_dev_replace = 0;
2168 device->mode = FMODE_EXCL;
2169 device->dev_stats_valid = 1;
2170 set_blocksize(device->bdev, 4096);
2173 sb->s_flags &= ~MS_RDONLY;
2174 ret = btrfs_prepare_sprout(root);
2175 BUG_ON(ret); /* -ENOMEM */
2178 device->fs_devices = root->fs_info->fs_devices;
2180 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2182 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2183 list_add(&device->dev_alloc_list,
2184 &root->fs_info->fs_devices->alloc_list);
2185 root->fs_info->fs_devices->num_devices++;
2186 root->fs_info->fs_devices->open_devices++;
2187 root->fs_info->fs_devices->rw_devices++;
2188 root->fs_info->fs_devices->total_devices++;
2189 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2191 spin_lock(&root->fs_info->free_chunk_lock);
2192 root->fs_info->free_chunk_space += device->total_bytes;
2193 spin_unlock(&root->fs_info->free_chunk_lock);
2195 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2196 root->fs_info->fs_devices->rotating = 1;
2198 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
2199 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2200 tmp + device->total_bytes);
2202 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
2203 btrfs_set_super_num_devices(root->fs_info->super_copy,
2206 /* add sysfs device entry */
2207 btrfs_kobj_add_device(root->fs_info, device);
2210 * we've got more storage, clear any full flags on the space
2213 btrfs_clear_space_info_full(root->fs_info);
2215 unlock_chunks(root);
2216 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2220 ret = init_first_rw_device(trans, root, device);
2221 unlock_chunks(root);
2223 btrfs_abort_transaction(trans, root, ret);
2228 ret = btrfs_add_device(trans, root, device);
2230 btrfs_abort_transaction(trans, root, ret);
2235 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2237 ret = btrfs_finish_sprout(trans, root);
2239 btrfs_abort_transaction(trans, root, ret);
2243 /* Sprouting would change fsid of the mounted root,
2244 * so rename the fsid on the sysfs
2246 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2247 root->fs_info->fsid);
2248 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2252 root->fs_info->num_tolerated_disk_barrier_failures =
2253 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2254 ret = btrfs_commit_transaction(trans, root);
2257 mutex_unlock(&uuid_mutex);
2258 up_write(&sb->s_umount);
2260 if (ret) /* transaction commit */
2263 ret = btrfs_relocate_sys_chunks(root);
2265 btrfs_error(root->fs_info, ret,
2266 "Failed to relocate sys chunks after "
2267 "device initialization. This can be fixed "
2268 "using the \"btrfs balance\" command.");
2269 trans = btrfs_attach_transaction(root);
2270 if (IS_ERR(trans)) {
2271 if (PTR_ERR(trans) == -ENOENT)
2273 return PTR_ERR(trans);
2275 ret = btrfs_commit_transaction(trans, root);
2278 /* Update ctime/mtime for libblkid */
2279 update_dev_time(device_path);
2283 btrfs_end_transaction(trans, root);
2284 rcu_string_free(device->name);
2285 btrfs_kobj_rm_device(root->fs_info, device);
2288 blkdev_put(bdev, FMODE_EXCL);
2290 mutex_unlock(&uuid_mutex);
2291 up_write(&sb->s_umount);
2296 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2297 struct btrfs_device *srcdev,
2298 struct btrfs_device **device_out)
2300 struct request_queue *q;
2301 struct btrfs_device *device;
2302 struct block_device *bdev;
2303 struct btrfs_fs_info *fs_info = root->fs_info;
2304 struct list_head *devices;
2305 struct rcu_string *name;
2306 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2310 if (fs_info->fs_devices->seeding) {
2311 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
2315 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2316 fs_info->bdev_holder);
2318 btrfs_err(fs_info, "target device %s is invalid!", device_path);
2319 return PTR_ERR(bdev);
2322 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2324 devices = &fs_info->fs_devices->devices;
2325 list_for_each_entry(device, devices, dev_list) {
2326 if (device->bdev == bdev) {
2327 btrfs_err(fs_info, "target device is in the filesystem!");
2334 if (i_size_read(bdev->bd_inode) <
2335 btrfs_device_get_total_bytes(srcdev)) {
2336 btrfs_err(fs_info, "target device is smaller than source device!");
2342 device = btrfs_alloc_device(NULL, &devid, NULL);
2343 if (IS_ERR(device)) {
2344 ret = PTR_ERR(device);
2348 name = rcu_string_strdup(device_path, GFP_NOFS);
2354 rcu_assign_pointer(device->name, name);
2356 q = bdev_get_queue(bdev);
2357 if (blk_queue_discard(q))
2358 device->can_discard = 1;
2359 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2360 device->writeable = 1;
2361 device->generation = 0;
2362 device->io_width = root->sectorsize;
2363 device->io_align = root->sectorsize;
2364 device->sector_size = root->sectorsize;
2365 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2366 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2367 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
2368 ASSERT(list_empty(&srcdev->resized_list));
2369 device->commit_total_bytes = srcdev->commit_total_bytes;
2370 device->commit_bytes_used = device->bytes_used;
2371 device->dev_root = fs_info->dev_root;
2372 device->bdev = bdev;
2373 device->in_fs_metadata = 1;
2374 device->is_tgtdev_for_dev_replace = 1;
2375 device->mode = FMODE_EXCL;
2376 device->dev_stats_valid = 1;
2377 set_blocksize(device->bdev, 4096);
2378 device->fs_devices = fs_info->fs_devices;
2379 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2380 fs_info->fs_devices->num_devices++;
2381 fs_info->fs_devices->open_devices++;
2382 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2384 *device_out = device;
2388 blkdev_put(bdev, FMODE_EXCL);
2392 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2393 struct btrfs_device *tgtdev)
2395 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2396 tgtdev->io_width = fs_info->dev_root->sectorsize;
2397 tgtdev->io_align = fs_info->dev_root->sectorsize;
2398 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2399 tgtdev->dev_root = fs_info->dev_root;
2400 tgtdev->in_fs_metadata = 1;
2403 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2404 struct btrfs_device *device)
2407 struct btrfs_path *path;
2408 struct btrfs_root *root;
2409 struct btrfs_dev_item *dev_item;
2410 struct extent_buffer *leaf;
2411 struct btrfs_key key;
2413 root = device->dev_root->fs_info->chunk_root;
2415 path = btrfs_alloc_path();
2419 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2420 key.type = BTRFS_DEV_ITEM_KEY;
2421 key.offset = device->devid;
2423 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2432 leaf = path->nodes[0];
2433 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2435 btrfs_set_device_id(leaf, dev_item, device->devid);
2436 btrfs_set_device_type(leaf, dev_item, device->type);
2437 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2438 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2439 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2440 btrfs_set_device_total_bytes(leaf, dev_item,
2441 btrfs_device_get_disk_total_bytes(device));
2442 btrfs_set_device_bytes_used(leaf, dev_item,
2443 btrfs_device_get_bytes_used(device));
2444 btrfs_mark_buffer_dirty(leaf);
2447 btrfs_free_path(path);
2451 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2452 struct btrfs_device *device, u64 new_size)
2454 struct btrfs_super_block *super_copy =
2455 device->dev_root->fs_info->super_copy;
2456 struct btrfs_fs_devices *fs_devices;
2460 if (!device->writeable)
2463 lock_chunks(device->dev_root);
2464 old_total = btrfs_super_total_bytes(super_copy);
2465 diff = new_size - device->total_bytes;
2467 if (new_size <= device->total_bytes ||
2468 device->is_tgtdev_for_dev_replace) {
2469 unlock_chunks(device->dev_root);
2473 fs_devices = device->dev_root->fs_info->fs_devices;
2475 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2476 device->fs_devices->total_rw_bytes += diff;
2478 btrfs_device_set_total_bytes(device, new_size);
2479 btrfs_device_set_disk_total_bytes(device, new_size);
2480 btrfs_clear_space_info_full(device->dev_root->fs_info);
2481 if (list_empty(&device->resized_list))
2482 list_add_tail(&device->resized_list,
2483 &fs_devices->resized_devices);
2484 unlock_chunks(device->dev_root);
2486 return btrfs_update_device(trans, device);
2489 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2490 struct btrfs_root *root, u64 chunk_objectid,
2494 struct btrfs_path *path;
2495 struct btrfs_key key;
2497 root = root->fs_info->chunk_root;
2498 path = btrfs_alloc_path();
2502 key.objectid = chunk_objectid;
2503 key.offset = chunk_offset;
2504 key.type = BTRFS_CHUNK_ITEM_KEY;
2506 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2509 else if (ret > 0) { /* Logic error or corruption */
2510 btrfs_error(root->fs_info, -ENOENT,
2511 "Failed lookup while freeing chunk.");
2516 ret = btrfs_del_item(trans, root, path);
2518 btrfs_error(root->fs_info, ret,
2519 "Failed to delete chunk item.");
2521 btrfs_free_path(path);
2525 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2528 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2529 struct btrfs_disk_key *disk_key;
2530 struct btrfs_chunk *chunk;
2537 struct btrfs_key key;
2540 array_size = btrfs_super_sys_array_size(super_copy);
2542 ptr = super_copy->sys_chunk_array;
2545 while (cur < array_size) {
2546 disk_key = (struct btrfs_disk_key *)ptr;
2547 btrfs_disk_key_to_cpu(&key, disk_key);
2549 len = sizeof(*disk_key);
2551 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2552 chunk = (struct btrfs_chunk *)(ptr + len);
2553 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2554 len += btrfs_chunk_item_size(num_stripes);
2559 if (key.objectid == chunk_objectid &&
2560 key.offset == chunk_offset) {
2561 memmove(ptr, ptr + len, array_size - (cur + len));
2563 btrfs_set_super_sys_array_size(super_copy, array_size);
2569 unlock_chunks(root);
2573 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2574 struct btrfs_root *root, u64 chunk_offset)
2576 struct extent_map_tree *em_tree;
2577 struct extent_map *em;
2578 struct btrfs_root *extent_root = root->fs_info->extent_root;
2579 struct map_lookup *map;
2580 u64 dev_extent_len = 0;
2581 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2585 root = root->fs_info->chunk_root;
2586 em_tree = &root->fs_info->mapping_tree.map_tree;
2588 read_lock(&em_tree->lock);
2589 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2590 read_unlock(&em_tree->lock);
2592 if (!em || em->start > chunk_offset ||
2593 em->start + em->len < chunk_offset) {
2595 * This is a logic error, but we don't want to just rely on the
2596 * user having built with ASSERT enabled, so if ASSERT doens't
2597 * do anything we still error out.
2601 free_extent_map(em);
2604 map = (struct map_lookup *)em->bdev;
2606 for (i = 0; i < map->num_stripes; i++) {
2607 struct btrfs_device *device = map->stripes[i].dev;
2608 ret = btrfs_free_dev_extent(trans, device,
2609 map->stripes[i].physical,
2612 btrfs_abort_transaction(trans, root, ret);
2616 if (device->bytes_used > 0) {
2618 btrfs_device_set_bytes_used(device,
2619 device->bytes_used - dev_extent_len);
2620 spin_lock(&root->fs_info->free_chunk_lock);
2621 root->fs_info->free_chunk_space += dev_extent_len;
2622 spin_unlock(&root->fs_info->free_chunk_lock);
2623 btrfs_clear_space_info_full(root->fs_info);
2624 unlock_chunks(root);
2627 if (map->stripes[i].dev) {
2628 ret = btrfs_update_device(trans, map->stripes[i].dev);
2630 btrfs_abort_transaction(trans, root, ret);
2635 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
2637 btrfs_abort_transaction(trans, root, ret);
2641 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2643 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2644 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2646 btrfs_abort_transaction(trans, root, ret);
2651 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
2653 btrfs_abort_transaction(trans, extent_root, ret);
2659 free_extent_map(em);
2663 static int btrfs_relocate_chunk(struct btrfs_root *root,
2667 struct btrfs_root *extent_root;
2668 struct btrfs_trans_handle *trans;
2671 root = root->fs_info->chunk_root;
2672 extent_root = root->fs_info->extent_root;
2674 ret = btrfs_can_relocate(extent_root, chunk_offset);
2678 /* step one, relocate all the extents inside this chunk */
2679 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2683 trans = btrfs_start_transaction(root, 0);
2684 if (IS_ERR(trans)) {
2685 ret = PTR_ERR(trans);
2686 btrfs_std_error(root->fs_info, ret);
2691 * step two, delete the device extents and the
2692 * chunk tree entries
2694 ret = btrfs_remove_chunk(trans, root, chunk_offset);
2695 btrfs_end_transaction(trans, root);
2699 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2701 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2702 struct btrfs_path *path;
2703 struct extent_buffer *leaf;
2704 struct btrfs_chunk *chunk;
2705 struct btrfs_key key;
2706 struct btrfs_key found_key;
2708 bool retried = false;
2712 path = btrfs_alloc_path();
2717 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2718 key.offset = (u64)-1;
2719 key.type = BTRFS_CHUNK_ITEM_KEY;
2722 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2725 BUG_ON(ret == 0); /* Corruption */
2727 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2734 leaf = path->nodes[0];
2735 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2737 chunk = btrfs_item_ptr(leaf, path->slots[0],
2738 struct btrfs_chunk);
2739 chunk_type = btrfs_chunk_type(leaf, chunk);
2740 btrfs_release_path(path);
2742 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2743 ret = btrfs_relocate_chunk(chunk_root,
2752 if (found_key.offset == 0)
2754 key.offset = found_key.offset - 1;
2757 if (failed && !retried) {
2761 } else if (WARN_ON(failed && retried)) {
2765 btrfs_free_path(path);
2769 static int insert_balance_item(struct btrfs_root *root,
2770 struct btrfs_balance_control *bctl)
2772 struct btrfs_trans_handle *trans;
2773 struct btrfs_balance_item *item;
2774 struct btrfs_disk_balance_args disk_bargs;
2775 struct btrfs_path *path;
2776 struct extent_buffer *leaf;
2777 struct btrfs_key key;
2780 path = btrfs_alloc_path();
2784 trans = btrfs_start_transaction(root, 0);
2785 if (IS_ERR(trans)) {
2786 btrfs_free_path(path);
2787 return PTR_ERR(trans);
2790 key.objectid = BTRFS_BALANCE_OBJECTID;
2791 key.type = BTRFS_BALANCE_ITEM_KEY;
2794 ret = btrfs_insert_empty_item(trans, root, path, &key,
2799 leaf = path->nodes[0];
2800 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2802 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2804 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2805 btrfs_set_balance_data(leaf, item, &disk_bargs);
2806 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2807 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2808 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2809 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2811 btrfs_set_balance_flags(leaf, item, bctl->flags);
2813 btrfs_mark_buffer_dirty(leaf);
2815 btrfs_free_path(path);
2816 err = btrfs_commit_transaction(trans, root);
2822 static int del_balance_item(struct btrfs_root *root)
2824 struct btrfs_trans_handle *trans;
2825 struct btrfs_path *path;
2826 struct btrfs_key key;
2829 path = btrfs_alloc_path();
2833 trans = btrfs_start_transaction(root, 0);
2834 if (IS_ERR(trans)) {
2835 btrfs_free_path(path);
2836 return PTR_ERR(trans);
2839 key.objectid = BTRFS_BALANCE_OBJECTID;
2840 key.type = BTRFS_BALANCE_ITEM_KEY;
2843 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2851 ret = btrfs_del_item(trans, root, path);
2853 btrfs_free_path(path);
2854 err = btrfs_commit_transaction(trans, root);
2861 * This is a heuristic used to reduce the number of chunks balanced on
2862 * resume after balance was interrupted.
2864 static void update_balance_args(struct btrfs_balance_control *bctl)
2867 * Turn on soft mode for chunk types that were being converted.
2869 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2870 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2871 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2872 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2873 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2874 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2877 * Turn on usage filter if is not already used. The idea is
2878 * that chunks that we have already balanced should be
2879 * reasonably full. Don't do it for chunks that are being
2880 * converted - that will keep us from relocating unconverted
2881 * (albeit full) chunks.
2883 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2884 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2885 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2886 bctl->data.usage = 90;
2888 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2889 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2890 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2891 bctl->sys.usage = 90;
2893 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2894 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2895 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2896 bctl->meta.usage = 90;
2901 * Should be called with both balance and volume mutexes held to
2902 * serialize other volume operations (add_dev/rm_dev/resize) with
2903 * restriper. Same goes for unset_balance_control.
2905 static void set_balance_control(struct btrfs_balance_control *bctl)
2907 struct btrfs_fs_info *fs_info = bctl->fs_info;
2909 BUG_ON(fs_info->balance_ctl);
2911 spin_lock(&fs_info->balance_lock);
2912 fs_info->balance_ctl = bctl;
2913 spin_unlock(&fs_info->balance_lock);
2916 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2918 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2920 BUG_ON(!fs_info->balance_ctl);
2922 spin_lock(&fs_info->balance_lock);
2923 fs_info->balance_ctl = NULL;
2924 spin_unlock(&fs_info->balance_lock);
2930 * Balance filters. Return 1 if chunk should be filtered out
2931 * (should not be balanced).
2933 static int chunk_profiles_filter(u64 chunk_type,
2934 struct btrfs_balance_args *bargs)
2936 chunk_type = chunk_to_extended(chunk_type) &
2937 BTRFS_EXTENDED_PROFILE_MASK;
2939 if (bargs->profiles & chunk_type)
2945 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2946 struct btrfs_balance_args *bargs)
2948 struct btrfs_block_group_cache *cache;
2949 u64 chunk_used, user_thresh;
2952 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2953 chunk_used = btrfs_block_group_used(&cache->item);
2955 if (bargs->usage == 0)
2957 else if (bargs->usage > 100)
2958 user_thresh = cache->key.offset;
2960 user_thresh = div_factor_fine(cache->key.offset,
2963 if (chunk_used < user_thresh)
2966 btrfs_put_block_group(cache);
2970 static int chunk_devid_filter(struct extent_buffer *leaf,
2971 struct btrfs_chunk *chunk,
2972 struct btrfs_balance_args *bargs)
2974 struct btrfs_stripe *stripe;
2975 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2978 for (i = 0; i < num_stripes; i++) {
2979 stripe = btrfs_stripe_nr(chunk, i);
2980 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2987 /* [pstart, pend) */
2988 static int chunk_drange_filter(struct extent_buffer *leaf,
2989 struct btrfs_chunk *chunk,
2991 struct btrfs_balance_args *bargs)
2993 struct btrfs_stripe *stripe;
2994 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3000 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3003 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
3004 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3005 factor = num_stripes / 2;
3006 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3007 factor = num_stripes - 1;
3008 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3009 factor = num_stripes - 2;
3011 factor = num_stripes;
3014 for (i = 0; i < num_stripes; i++) {
3015 stripe = btrfs_stripe_nr(chunk, i);
3016 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3019 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3020 stripe_length = btrfs_chunk_length(leaf, chunk);
3021 do_div(stripe_length, factor);
3023 if (stripe_offset < bargs->pend &&
3024 stripe_offset + stripe_length > bargs->pstart)
3031 /* [vstart, vend) */
3032 static int chunk_vrange_filter(struct extent_buffer *leaf,
3033 struct btrfs_chunk *chunk,
3035 struct btrfs_balance_args *bargs)
3037 if (chunk_offset < bargs->vend &&
3038 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3039 /* at least part of the chunk is inside this vrange */
3045 static int chunk_soft_convert_filter(u64 chunk_type,
3046 struct btrfs_balance_args *bargs)
3048 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3051 chunk_type = chunk_to_extended(chunk_type) &
3052 BTRFS_EXTENDED_PROFILE_MASK;
3054 if (bargs->target == chunk_type)
3060 static int should_balance_chunk(struct btrfs_root *root,
3061 struct extent_buffer *leaf,
3062 struct btrfs_chunk *chunk, u64 chunk_offset)
3064 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3065 struct btrfs_balance_args *bargs = NULL;
3066 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3069 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3070 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3074 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3075 bargs = &bctl->data;
3076 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3078 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3079 bargs = &bctl->meta;
3081 /* profiles filter */
3082 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3083 chunk_profiles_filter(chunk_type, bargs)) {
3088 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3089 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3094 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3095 chunk_devid_filter(leaf, chunk, bargs)) {
3099 /* drange filter, makes sense only with devid filter */
3100 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3101 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3106 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3107 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3111 /* soft profile changing mode */
3112 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3113 chunk_soft_convert_filter(chunk_type, bargs)) {
3118 * limited by count, must be the last filter
3120 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3121 if (bargs->limit == 0)
3130 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3132 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3133 struct btrfs_root *chunk_root = fs_info->chunk_root;
3134 struct btrfs_root *dev_root = fs_info->dev_root;
3135 struct list_head *devices;
3136 struct btrfs_device *device;
3139 struct btrfs_chunk *chunk;
3140 struct btrfs_path *path;
3141 struct btrfs_key key;
3142 struct btrfs_key found_key;
3143 struct btrfs_trans_handle *trans;
3144 struct extent_buffer *leaf;
3147 int enospc_errors = 0;
3148 bool counting = true;
3149 u64 limit_data = bctl->data.limit;
3150 u64 limit_meta = bctl->meta.limit;
3151 u64 limit_sys = bctl->sys.limit;
3153 /* step one make some room on all the devices */
3154 devices = &fs_info->fs_devices->devices;
3155 list_for_each_entry(device, devices, dev_list) {
3156 old_size = btrfs_device_get_total_bytes(device);
3157 size_to_free = div_factor(old_size, 1);
3158 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
3159 if (!device->writeable ||
3160 btrfs_device_get_total_bytes(device) -
3161 btrfs_device_get_bytes_used(device) > size_to_free ||
3162 device->is_tgtdev_for_dev_replace)
3165 ret = btrfs_shrink_device(device, old_size - size_to_free);
3170 trans = btrfs_start_transaction(dev_root, 0);
3171 BUG_ON(IS_ERR(trans));
3173 ret = btrfs_grow_device(trans, device, old_size);
3176 btrfs_end_transaction(trans, dev_root);
3179 /* step two, relocate all the chunks */
3180 path = btrfs_alloc_path();
3186 /* zero out stat counters */
3187 spin_lock(&fs_info->balance_lock);
3188 memset(&bctl->stat, 0, sizeof(bctl->stat));
3189 spin_unlock(&fs_info->balance_lock);
3192 bctl->data.limit = limit_data;
3193 bctl->meta.limit = limit_meta;
3194 bctl->sys.limit = limit_sys;
3196 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3197 key.offset = (u64)-1;
3198 key.type = BTRFS_CHUNK_ITEM_KEY;
3201 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3202 atomic_read(&fs_info->balance_cancel_req)) {
3207 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3212 * this shouldn't happen, it means the last relocate
3216 BUG(); /* FIXME break ? */
3218 ret = btrfs_previous_item(chunk_root, path, 0,
3219 BTRFS_CHUNK_ITEM_KEY);
3225 leaf = path->nodes[0];
3226 slot = path->slots[0];
3227 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3229 if (found_key.objectid != key.objectid)
3232 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3235 spin_lock(&fs_info->balance_lock);
3236 bctl->stat.considered++;
3237 spin_unlock(&fs_info->balance_lock);
3240 ret = should_balance_chunk(chunk_root, leaf, chunk,
3242 btrfs_release_path(path);
3247 spin_lock(&fs_info->balance_lock);
3248 bctl->stat.expected++;
3249 spin_unlock(&fs_info->balance_lock);
3253 ret = btrfs_relocate_chunk(chunk_root,
3256 if (ret && ret != -ENOSPC)
3258 if (ret == -ENOSPC) {
3261 spin_lock(&fs_info->balance_lock);
3262 bctl->stat.completed++;
3263 spin_unlock(&fs_info->balance_lock);
3266 if (found_key.offset == 0)
3268 key.offset = found_key.offset - 1;
3272 btrfs_release_path(path);
3277 btrfs_free_path(path);
3278 if (enospc_errors) {
3279 btrfs_info(fs_info, "%d enospc errors during balance",
3289 * alloc_profile_is_valid - see if a given profile is valid and reduced
3290 * @flags: profile to validate
3291 * @extended: if true @flags is treated as an extended profile
3293 static int alloc_profile_is_valid(u64 flags, int extended)
3295 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3296 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3298 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3300 /* 1) check that all other bits are zeroed */
3304 /* 2) see if profile is reduced */
3306 return !extended; /* "0" is valid for usual profiles */
3308 /* true if exactly one bit set */
3309 return (flags & (flags - 1)) == 0;
3312 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3314 /* cancel requested || normal exit path */
3315 return atomic_read(&fs_info->balance_cancel_req) ||
3316 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3317 atomic_read(&fs_info->balance_cancel_req) == 0);
3320 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3324 unset_balance_control(fs_info);
3325 ret = del_balance_item(fs_info->tree_root);
3327 btrfs_std_error(fs_info, ret);
3329 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3333 * Should be called with both balance and volume mutexes held
3335 int btrfs_balance(struct btrfs_balance_control *bctl,
3336 struct btrfs_ioctl_balance_args *bargs)
3338 struct btrfs_fs_info *fs_info = bctl->fs_info;
3345 if (btrfs_fs_closing(fs_info) ||
3346 atomic_read(&fs_info->balance_pause_req) ||
3347 atomic_read(&fs_info->balance_cancel_req)) {
3352 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3353 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3357 * In case of mixed groups both data and meta should be picked,
3358 * and identical options should be given for both of them.
3360 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3361 if (mixed && (bctl->flags & allowed)) {
3362 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3363 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3364 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3365 btrfs_err(fs_info, "with mixed groups data and "
3366 "metadata balance options must be the same");
3372 num_devices = fs_info->fs_devices->num_devices;
3373 btrfs_dev_replace_lock(&fs_info->dev_replace);
3374 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3375 BUG_ON(num_devices < 1);
3378 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3379 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3380 if (num_devices == 1)
3381 allowed |= BTRFS_BLOCK_GROUP_DUP;
3382 else if (num_devices > 1)
3383 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3384 if (num_devices > 2)
3385 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3386 if (num_devices > 3)
3387 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3388 BTRFS_BLOCK_GROUP_RAID6);
3389 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3390 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3391 (bctl->data.target & ~allowed))) {
3392 btrfs_err(fs_info, "unable to start balance with target "
3393 "data profile %llu",
3398 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3399 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3400 (bctl->meta.target & ~allowed))) {
3402 "unable to start balance with target metadata profile %llu",
3407 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3408 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3409 (bctl->sys.target & ~allowed))) {
3411 "unable to start balance with target system profile %llu",
3417 /* allow dup'ed data chunks only in mixed mode */
3418 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3419 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3420 btrfs_err(fs_info, "dup for data is not allowed");
3425 /* allow to reduce meta or sys integrity only if force set */
3426 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3427 BTRFS_BLOCK_GROUP_RAID10 |
3428 BTRFS_BLOCK_GROUP_RAID5 |
3429 BTRFS_BLOCK_GROUP_RAID6;
3431 seq = read_seqbegin(&fs_info->profiles_lock);
3433 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3434 (fs_info->avail_system_alloc_bits & allowed) &&
3435 !(bctl->sys.target & allowed)) ||
3436 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3437 (fs_info->avail_metadata_alloc_bits & allowed) &&
3438 !(bctl->meta.target & allowed))) {
3439 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3440 btrfs_info(fs_info, "force reducing metadata integrity");
3442 btrfs_err(fs_info, "balance will reduce metadata "
3443 "integrity, use force if you want this");
3448 } while (read_seqretry(&fs_info->profiles_lock, seq));
3450 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3451 int num_tolerated_disk_barrier_failures;
3452 u64 target = bctl->sys.target;
3454 num_tolerated_disk_barrier_failures =
3455 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3456 if (num_tolerated_disk_barrier_failures > 0 &&
3458 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3459 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3460 num_tolerated_disk_barrier_failures = 0;
3461 else if (num_tolerated_disk_barrier_failures > 1 &&
3463 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3464 num_tolerated_disk_barrier_failures = 1;
3466 fs_info->num_tolerated_disk_barrier_failures =
3467 num_tolerated_disk_barrier_failures;
3470 ret = insert_balance_item(fs_info->tree_root, bctl);
3471 if (ret && ret != -EEXIST)
3474 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3475 BUG_ON(ret == -EEXIST);
3476 set_balance_control(bctl);
3478 BUG_ON(ret != -EEXIST);
3479 spin_lock(&fs_info->balance_lock);
3480 update_balance_args(bctl);
3481 spin_unlock(&fs_info->balance_lock);
3484 atomic_inc(&fs_info->balance_running);
3485 mutex_unlock(&fs_info->balance_mutex);
3487 ret = __btrfs_balance(fs_info);
3489 mutex_lock(&fs_info->balance_mutex);
3490 atomic_dec(&fs_info->balance_running);
3492 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3493 fs_info->num_tolerated_disk_barrier_failures =
3494 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3498 memset(bargs, 0, sizeof(*bargs));
3499 update_ioctl_balance_args(fs_info, 0, bargs);
3502 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3503 balance_need_close(fs_info)) {
3504 __cancel_balance(fs_info);
3507 wake_up(&fs_info->balance_wait_q);
3511 if (bctl->flags & BTRFS_BALANCE_RESUME)
3512 __cancel_balance(fs_info);
3515 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3520 static int balance_kthread(void *data)
3522 struct btrfs_fs_info *fs_info = data;
3525 mutex_lock(&fs_info->volume_mutex);
3526 mutex_lock(&fs_info->balance_mutex);
3528 if (fs_info->balance_ctl) {
3529 btrfs_info(fs_info, "continuing balance");
3530 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3533 mutex_unlock(&fs_info->balance_mutex);
3534 mutex_unlock(&fs_info->volume_mutex);
3539 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3541 struct task_struct *tsk;
3543 spin_lock(&fs_info->balance_lock);
3544 if (!fs_info->balance_ctl) {
3545 spin_unlock(&fs_info->balance_lock);
3548 spin_unlock(&fs_info->balance_lock);
3550 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3551 btrfs_info(fs_info, "force skipping balance");
3555 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3556 return PTR_ERR_OR_ZERO(tsk);
3559 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3561 struct btrfs_balance_control *bctl;
3562 struct btrfs_balance_item *item;
3563 struct btrfs_disk_balance_args disk_bargs;
3564 struct btrfs_path *path;
3565 struct extent_buffer *leaf;
3566 struct btrfs_key key;
3569 path = btrfs_alloc_path();
3573 key.objectid = BTRFS_BALANCE_OBJECTID;
3574 key.type = BTRFS_BALANCE_ITEM_KEY;
3577 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3580 if (ret > 0) { /* ret = -ENOENT; */
3585 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3591 leaf = path->nodes[0];
3592 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3594 bctl->fs_info = fs_info;
3595 bctl->flags = btrfs_balance_flags(leaf, item);
3596 bctl->flags |= BTRFS_BALANCE_RESUME;
3598 btrfs_balance_data(leaf, item, &disk_bargs);
3599 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3600 btrfs_balance_meta(leaf, item, &disk_bargs);
3601 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3602 btrfs_balance_sys(leaf, item, &disk_bargs);
3603 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3605 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3607 mutex_lock(&fs_info->volume_mutex);
3608 mutex_lock(&fs_info->balance_mutex);
3610 set_balance_control(bctl);
3612 mutex_unlock(&fs_info->balance_mutex);
3613 mutex_unlock(&fs_info->volume_mutex);
3615 btrfs_free_path(path);
3619 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3623 mutex_lock(&fs_info->balance_mutex);
3624 if (!fs_info->balance_ctl) {
3625 mutex_unlock(&fs_info->balance_mutex);
3629 if (atomic_read(&fs_info->balance_running)) {
3630 atomic_inc(&fs_info->balance_pause_req);
3631 mutex_unlock(&fs_info->balance_mutex);
3633 wait_event(fs_info->balance_wait_q,
3634 atomic_read(&fs_info->balance_running) == 0);
3636 mutex_lock(&fs_info->balance_mutex);
3637 /* we are good with balance_ctl ripped off from under us */
3638 BUG_ON(atomic_read(&fs_info->balance_running));
3639 atomic_dec(&fs_info->balance_pause_req);
3644 mutex_unlock(&fs_info->balance_mutex);
3648 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3650 if (fs_info->sb->s_flags & MS_RDONLY)
3653 mutex_lock(&fs_info->balance_mutex);
3654 if (!fs_info->balance_ctl) {
3655 mutex_unlock(&fs_info->balance_mutex);
3659 atomic_inc(&fs_info->balance_cancel_req);
3661 * if we are running just wait and return, balance item is
3662 * deleted in btrfs_balance in this case
3664 if (atomic_read(&fs_info->balance_running)) {
3665 mutex_unlock(&fs_info->balance_mutex);
3666 wait_event(fs_info->balance_wait_q,
3667 atomic_read(&fs_info->balance_running) == 0);
3668 mutex_lock(&fs_info->balance_mutex);
3670 /* __cancel_balance needs volume_mutex */
3671 mutex_unlock(&fs_info->balance_mutex);
3672 mutex_lock(&fs_info->volume_mutex);
3673 mutex_lock(&fs_info->balance_mutex);
3675 if (fs_info->balance_ctl)
3676 __cancel_balance(fs_info);
3678 mutex_unlock(&fs_info->volume_mutex);
3681 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3682 atomic_dec(&fs_info->balance_cancel_req);
3683 mutex_unlock(&fs_info->balance_mutex);
3687 static int btrfs_uuid_scan_kthread(void *data)
3689 struct btrfs_fs_info *fs_info = data;
3690 struct btrfs_root *root = fs_info->tree_root;
3691 struct btrfs_key key;
3692 struct btrfs_key max_key;
3693 struct btrfs_path *path = NULL;
3695 struct extent_buffer *eb;
3697 struct btrfs_root_item root_item;
3699 struct btrfs_trans_handle *trans = NULL;
3701 path = btrfs_alloc_path();
3708 key.type = BTRFS_ROOT_ITEM_KEY;
3711 max_key.objectid = (u64)-1;
3712 max_key.type = BTRFS_ROOT_ITEM_KEY;
3713 max_key.offset = (u64)-1;
3716 ret = btrfs_search_forward(root, &key, path, 0);
3723 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3724 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3725 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3726 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3729 eb = path->nodes[0];
3730 slot = path->slots[0];
3731 item_size = btrfs_item_size_nr(eb, slot);
3732 if (item_size < sizeof(root_item))
3735 read_extent_buffer(eb, &root_item,
3736 btrfs_item_ptr_offset(eb, slot),
3737 (int)sizeof(root_item));
3738 if (btrfs_root_refs(&root_item) == 0)
3741 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3742 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3746 btrfs_release_path(path);
3748 * 1 - subvol uuid item
3749 * 1 - received_subvol uuid item
3751 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3752 if (IS_ERR(trans)) {
3753 ret = PTR_ERR(trans);
3761 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3762 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3764 BTRFS_UUID_KEY_SUBVOL,
3767 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3773 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3774 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3775 root_item.received_uuid,
3776 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3779 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3787 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3793 btrfs_release_path(path);
3794 if (key.offset < (u64)-1) {
3796 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3798 key.type = BTRFS_ROOT_ITEM_KEY;
3799 } else if (key.objectid < (u64)-1) {
3801 key.type = BTRFS_ROOT_ITEM_KEY;
3810 btrfs_free_path(path);
3811 if (trans && !IS_ERR(trans))
3812 btrfs_end_transaction(trans, fs_info->uuid_root);
3814 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
3816 fs_info->update_uuid_tree_gen = 1;
3817 up(&fs_info->uuid_tree_rescan_sem);
3822 * Callback for btrfs_uuid_tree_iterate().
3824 * 0 check succeeded, the entry is not outdated.
3825 * < 0 if an error occured.
3826 * > 0 if the check failed, which means the caller shall remove the entry.
3828 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3829 u8 *uuid, u8 type, u64 subid)
3831 struct btrfs_key key;
3833 struct btrfs_root *subvol_root;
3835 if (type != BTRFS_UUID_KEY_SUBVOL &&
3836 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3839 key.objectid = subid;
3840 key.type = BTRFS_ROOT_ITEM_KEY;
3841 key.offset = (u64)-1;
3842 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3843 if (IS_ERR(subvol_root)) {
3844 ret = PTR_ERR(subvol_root);
3851 case BTRFS_UUID_KEY_SUBVOL:
3852 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3855 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3856 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3866 static int btrfs_uuid_rescan_kthread(void *data)
3868 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3872 * 1st step is to iterate through the existing UUID tree and
3873 * to delete all entries that contain outdated data.
3874 * 2nd step is to add all missing entries to the UUID tree.
3876 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3878 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
3879 up(&fs_info->uuid_tree_rescan_sem);
3882 return btrfs_uuid_scan_kthread(data);
3885 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3887 struct btrfs_trans_handle *trans;
3888 struct btrfs_root *tree_root = fs_info->tree_root;
3889 struct btrfs_root *uuid_root;
3890 struct task_struct *task;
3897 trans = btrfs_start_transaction(tree_root, 2);
3899 return PTR_ERR(trans);
3901 uuid_root = btrfs_create_tree(trans, fs_info,
3902 BTRFS_UUID_TREE_OBJECTID);
3903 if (IS_ERR(uuid_root)) {
3904 btrfs_abort_transaction(trans, tree_root,
3905 PTR_ERR(uuid_root));
3906 return PTR_ERR(uuid_root);
3909 fs_info->uuid_root = uuid_root;
3911 ret = btrfs_commit_transaction(trans, tree_root);
3915 down(&fs_info->uuid_tree_rescan_sem);
3916 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3918 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3919 btrfs_warn(fs_info, "failed to start uuid_scan task");
3920 up(&fs_info->uuid_tree_rescan_sem);
3921 return PTR_ERR(task);
3927 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3929 struct task_struct *task;
3931 down(&fs_info->uuid_tree_rescan_sem);
3932 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3934 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3935 btrfs_warn(fs_info, "failed to start uuid_rescan task");
3936 up(&fs_info->uuid_tree_rescan_sem);
3937 return PTR_ERR(task);
3944 * shrinking a device means finding all of the device extents past
3945 * the new size, and then following the back refs to the chunks.
3946 * The chunk relocation code actually frees the device extent
3948 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3950 struct btrfs_trans_handle *trans;
3951 struct btrfs_root *root = device->dev_root;
3952 struct btrfs_dev_extent *dev_extent = NULL;
3953 struct btrfs_path *path;
3960 bool retried = false;
3961 struct extent_buffer *l;
3962 struct btrfs_key key;
3963 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3964 u64 old_total = btrfs_super_total_bytes(super_copy);
3965 u64 old_size = btrfs_device_get_total_bytes(device);
3966 u64 diff = old_size - new_size;
3968 if (device->is_tgtdev_for_dev_replace)
3971 path = btrfs_alloc_path();
3979 btrfs_device_set_total_bytes(device, new_size);
3980 if (device->writeable) {
3981 device->fs_devices->total_rw_bytes -= diff;
3982 spin_lock(&root->fs_info->free_chunk_lock);
3983 root->fs_info->free_chunk_space -= diff;
3984 spin_unlock(&root->fs_info->free_chunk_lock);
3986 unlock_chunks(root);
3989 key.objectid = device->devid;
3990 key.offset = (u64)-1;
3991 key.type = BTRFS_DEV_EXTENT_KEY;
3994 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3998 ret = btrfs_previous_item(root, path, 0, key.type);
4003 btrfs_release_path(path);
4008 slot = path->slots[0];
4009 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4011 if (key.objectid != device->devid) {
4012 btrfs_release_path(path);
4016 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4017 length = btrfs_dev_extent_length(l, dev_extent);
4019 if (key.offset + length <= new_size) {
4020 btrfs_release_path(path);
4024 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4025 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4026 btrfs_release_path(path);
4028 ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset);
4029 if (ret && ret != -ENOSPC)
4033 } while (key.offset-- > 0);
4035 if (failed && !retried) {
4039 } else if (failed && retried) {
4043 btrfs_device_set_total_bytes(device, old_size);
4044 if (device->writeable)
4045 device->fs_devices->total_rw_bytes += diff;
4046 spin_lock(&root->fs_info->free_chunk_lock);
4047 root->fs_info->free_chunk_space += diff;
4048 spin_unlock(&root->fs_info->free_chunk_lock);
4049 unlock_chunks(root);
4053 /* Shrinking succeeded, else we would be at "done". */
4054 trans = btrfs_start_transaction(root, 0);
4055 if (IS_ERR(trans)) {
4056 ret = PTR_ERR(trans);
4061 btrfs_device_set_disk_total_bytes(device, new_size);
4062 if (list_empty(&device->resized_list))
4063 list_add_tail(&device->resized_list,
4064 &root->fs_info->fs_devices->resized_devices);
4066 WARN_ON(diff > old_total);
4067 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4068 unlock_chunks(root);
4070 /* Now btrfs_update_device() will change the on-disk size. */
4071 ret = btrfs_update_device(trans, device);
4072 btrfs_end_transaction(trans, root);
4074 btrfs_free_path(path);
4078 static int btrfs_add_system_chunk(struct btrfs_root *root,
4079 struct btrfs_key *key,
4080 struct btrfs_chunk *chunk, int item_size)
4082 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4083 struct btrfs_disk_key disk_key;
4088 array_size = btrfs_super_sys_array_size(super_copy);
4089 if (array_size + item_size + sizeof(disk_key)
4090 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4091 unlock_chunks(root);
4095 ptr = super_copy->sys_chunk_array + array_size;
4096 btrfs_cpu_key_to_disk(&disk_key, key);
4097 memcpy(ptr, &disk_key, sizeof(disk_key));
4098 ptr += sizeof(disk_key);
4099 memcpy(ptr, chunk, item_size);
4100 item_size += sizeof(disk_key);
4101 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4102 unlock_chunks(root);
4108 * sort the devices in descending order by max_avail, total_avail
4110 static int btrfs_cmp_device_info(const void *a, const void *b)
4112 const struct btrfs_device_info *di_a = a;
4113 const struct btrfs_device_info *di_b = b;
4115 if (di_a->max_avail > di_b->max_avail)
4117 if (di_a->max_avail < di_b->max_avail)
4119 if (di_a->total_avail > di_b->total_avail)
4121 if (di_a->total_avail < di_b->total_avail)
4126 static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
4127 [BTRFS_RAID_RAID10] = {
4130 .devs_max = 0, /* 0 == as many as possible */
4132 .devs_increment = 2,
4135 [BTRFS_RAID_RAID1] = {
4140 .devs_increment = 2,
4143 [BTRFS_RAID_DUP] = {
4148 .devs_increment = 1,
4151 [BTRFS_RAID_RAID0] = {
4156 .devs_increment = 1,
4159 [BTRFS_RAID_SINGLE] = {
4164 .devs_increment = 1,
4167 [BTRFS_RAID_RAID5] = {
4172 .devs_increment = 1,
4175 [BTRFS_RAID_RAID6] = {
4180 .devs_increment = 1,
4185 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4187 /* TODO allow them to set a preferred stripe size */
4191 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4193 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
4196 btrfs_set_fs_incompat(info, RAID56);
4199 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4200 - sizeof(struct btrfs_item) \
4201 - sizeof(struct btrfs_chunk)) \
4202 / sizeof(struct btrfs_stripe) + 1)
4204 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4205 - 2 * sizeof(struct btrfs_disk_key) \
4206 - 2 * sizeof(struct btrfs_chunk)) \
4207 / sizeof(struct btrfs_stripe) + 1)
4209 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4210 struct btrfs_root *extent_root, u64 start,
4213 struct btrfs_fs_info *info = extent_root->fs_info;
4214 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4215 struct list_head *cur;
4216 struct map_lookup *map = NULL;
4217 struct extent_map_tree *em_tree;
4218 struct extent_map *em;
4219 struct btrfs_device_info *devices_info = NULL;
4221 int num_stripes; /* total number of stripes to allocate */
4222 int data_stripes; /* number of stripes that count for
4224 int sub_stripes; /* sub_stripes info for map */
4225 int dev_stripes; /* stripes per dev */
4226 int devs_max; /* max devs to use */
4227 int devs_min; /* min devs needed */
4228 int devs_increment; /* ndevs has to be a multiple of this */
4229 int ncopies; /* how many copies to data has */
4231 u64 max_stripe_size;
4235 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
4241 BUG_ON(!alloc_profile_is_valid(type, 0));
4243 if (list_empty(&fs_devices->alloc_list))
4246 index = __get_raid_index(type);
4248 sub_stripes = btrfs_raid_array[index].sub_stripes;
4249 dev_stripes = btrfs_raid_array[index].dev_stripes;
4250 devs_max = btrfs_raid_array[index].devs_max;
4251 devs_min = btrfs_raid_array[index].devs_min;
4252 devs_increment = btrfs_raid_array[index].devs_increment;
4253 ncopies = btrfs_raid_array[index].ncopies;
4255 if (type & BTRFS_BLOCK_GROUP_DATA) {
4256 max_stripe_size = 1024 * 1024 * 1024;
4257 max_chunk_size = 10 * max_stripe_size;
4259 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4260 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4261 /* for larger filesystems, use larger metadata chunks */
4262 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4263 max_stripe_size = 1024 * 1024 * 1024;
4265 max_stripe_size = 256 * 1024 * 1024;
4266 max_chunk_size = max_stripe_size;
4268 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4269 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4270 max_stripe_size = 32 * 1024 * 1024;
4271 max_chunk_size = 2 * max_stripe_size;
4273 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
4275 btrfs_err(info, "invalid chunk type 0x%llx requested",
4280 /* we don't want a chunk larger than 10% of writeable space */
4281 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4284 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4289 cur = fs_devices->alloc_list.next;
4292 * in the first pass through the devices list, we gather information
4293 * about the available holes on each device.
4296 while (cur != &fs_devices->alloc_list) {
4297 struct btrfs_device *device;
4301 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4305 if (!device->writeable) {
4307 "BTRFS: read-only device in alloc_list\n");
4311 if (!device->in_fs_metadata ||
4312 device->is_tgtdev_for_dev_replace)
4315 if (device->total_bytes > device->bytes_used)
4316 total_avail = device->total_bytes - device->bytes_used;
4320 /* If there is no space on this device, skip it. */
4321 if (total_avail == 0)
4324 ret = find_free_dev_extent(trans, device,
4325 max_stripe_size * dev_stripes,
4326 &dev_offset, &max_avail);
4327 if (ret && ret != -ENOSPC)
4331 max_avail = max_stripe_size * dev_stripes;
4333 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4336 if (ndevs == fs_devices->rw_devices) {
4337 WARN(1, "%s: found more than %llu devices\n",
4338 __func__, fs_devices->rw_devices);
4341 devices_info[ndevs].dev_offset = dev_offset;
4342 devices_info[ndevs].max_avail = max_avail;
4343 devices_info[ndevs].total_avail = total_avail;
4344 devices_info[ndevs].dev = device;
4349 * now sort the devices by hole size / available space
4351 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4352 btrfs_cmp_device_info, NULL);
4354 /* round down to number of usable stripes */
4355 ndevs -= ndevs % devs_increment;
4357 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4362 if (devs_max && ndevs > devs_max)
4365 * the primary goal is to maximize the number of stripes, so use as many
4366 * devices as possible, even if the stripes are not maximum sized.
4368 stripe_size = devices_info[ndevs-1].max_avail;
4369 num_stripes = ndevs * dev_stripes;
4372 * this will have to be fixed for RAID1 and RAID10 over
4375 data_stripes = num_stripes / ncopies;
4377 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4378 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4379 btrfs_super_stripesize(info->super_copy));
4380 data_stripes = num_stripes - 1;
4382 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4383 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4384 btrfs_super_stripesize(info->super_copy));
4385 data_stripes = num_stripes - 2;
4389 * Use the number of data stripes to figure out how big this chunk
4390 * is really going to be in terms of logical address space,
4391 * and compare that answer with the max chunk size
4393 if (stripe_size * data_stripes > max_chunk_size) {
4394 u64 mask = (1ULL << 24) - 1;
4395 stripe_size = max_chunk_size;
4396 do_div(stripe_size, data_stripes);
4398 /* bump the answer up to a 16MB boundary */
4399 stripe_size = (stripe_size + mask) & ~mask;
4401 /* but don't go higher than the limits we found
4402 * while searching for free extents
4404 if (stripe_size > devices_info[ndevs-1].max_avail)
4405 stripe_size = devices_info[ndevs-1].max_avail;
4408 do_div(stripe_size, dev_stripes);
4410 /* align to BTRFS_STRIPE_LEN */
4411 do_div(stripe_size, raid_stripe_len);
4412 stripe_size *= raid_stripe_len;
4414 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4419 map->num_stripes = num_stripes;
4421 for (i = 0; i < ndevs; ++i) {
4422 for (j = 0; j < dev_stripes; ++j) {
4423 int s = i * dev_stripes + j;
4424 map->stripes[s].dev = devices_info[i].dev;
4425 map->stripes[s].physical = devices_info[i].dev_offset +
4429 map->sector_size = extent_root->sectorsize;
4430 map->stripe_len = raid_stripe_len;
4431 map->io_align = raid_stripe_len;
4432 map->io_width = raid_stripe_len;
4434 map->sub_stripes = sub_stripes;
4436 num_bytes = stripe_size * data_stripes;
4438 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
4440 em = alloc_extent_map();
4446 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4447 em->bdev = (struct block_device *)map;
4449 em->len = num_bytes;
4450 em->block_start = 0;
4451 em->block_len = em->len;
4452 em->orig_block_len = stripe_size;
4454 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4455 write_lock(&em_tree->lock);
4456 ret = add_extent_mapping(em_tree, em, 0);
4458 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4459 atomic_inc(&em->refs);
4461 write_unlock(&em_tree->lock);
4463 free_extent_map(em);
4467 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4468 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4471 goto error_del_extent;
4473 for (i = 0; i < map->num_stripes; i++) {
4474 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4475 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4478 spin_lock(&extent_root->fs_info->free_chunk_lock);
4479 extent_root->fs_info->free_chunk_space -= (stripe_size *
4481 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4483 free_extent_map(em);
4484 check_raid56_incompat_flag(extent_root->fs_info, type);
4486 kfree(devices_info);
4490 write_lock(&em_tree->lock);
4491 remove_extent_mapping(em_tree, em);
4492 write_unlock(&em_tree->lock);
4494 /* One for our allocation */
4495 free_extent_map(em);
4496 /* One for the tree reference */
4497 free_extent_map(em);
4498 /* One for the pending_chunks list reference */
4499 free_extent_map(em);
4501 kfree(devices_info);
4505 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4506 struct btrfs_root *extent_root,
4507 u64 chunk_offset, u64 chunk_size)
4509 struct btrfs_key key;
4510 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4511 struct btrfs_device *device;
4512 struct btrfs_chunk *chunk;
4513 struct btrfs_stripe *stripe;
4514 struct extent_map_tree *em_tree;
4515 struct extent_map *em;
4516 struct map_lookup *map;
4523 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4524 read_lock(&em_tree->lock);
4525 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4526 read_unlock(&em_tree->lock);
4529 btrfs_crit(extent_root->fs_info, "unable to find logical "
4530 "%Lu len %Lu", chunk_offset, chunk_size);
4534 if (em->start != chunk_offset || em->len != chunk_size) {
4535 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4536 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
4537 chunk_size, em->start, em->len);
4538 free_extent_map(em);
4542 map = (struct map_lookup *)em->bdev;
4543 item_size = btrfs_chunk_item_size(map->num_stripes);
4544 stripe_size = em->orig_block_len;
4546 chunk = kzalloc(item_size, GFP_NOFS);
4552 for (i = 0; i < map->num_stripes; i++) {
4553 device = map->stripes[i].dev;
4554 dev_offset = map->stripes[i].physical;
4556 ret = btrfs_update_device(trans, device);
4559 ret = btrfs_alloc_dev_extent(trans, device,
4560 chunk_root->root_key.objectid,
4561 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4562 chunk_offset, dev_offset,
4568 stripe = &chunk->stripe;
4569 for (i = 0; i < map->num_stripes; i++) {
4570 device = map->stripes[i].dev;
4571 dev_offset = map->stripes[i].physical;
4573 btrfs_set_stack_stripe_devid(stripe, device->devid);
4574 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4575 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4579 btrfs_set_stack_chunk_length(chunk, chunk_size);
4580 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4581 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4582 btrfs_set_stack_chunk_type(chunk, map->type);
4583 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4584 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4585 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4586 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4587 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4589 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4590 key.type = BTRFS_CHUNK_ITEM_KEY;
4591 key.offset = chunk_offset;
4593 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4594 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4596 * TODO: Cleanup of inserted chunk root in case of
4599 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4605 free_extent_map(em);
4610 * Chunk allocation falls into two parts. The first part does works
4611 * that make the new allocated chunk useable, but not do any operation
4612 * that modifies the chunk tree. The second part does the works that
4613 * require modifying the chunk tree. This division is important for the
4614 * bootstrap process of adding storage to a seed btrfs.
4616 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4617 struct btrfs_root *extent_root, u64 type)
4621 chunk_offset = find_next_chunk(extent_root->fs_info);
4622 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
4625 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4626 struct btrfs_root *root,
4627 struct btrfs_device *device)
4630 u64 sys_chunk_offset;
4632 struct btrfs_fs_info *fs_info = root->fs_info;
4633 struct btrfs_root *extent_root = fs_info->extent_root;
4636 chunk_offset = find_next_chunk(fs_info);
4637 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4638 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4643 sys_chunk_offset = find_next_chunk(root->fs_info);
4644 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4645 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4650 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4654 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4655 BTRFS_BLOCK_GROUP_RAID10 |
4656 BTRFS_BLOCK_GROUP_RAID5 |
4657 BTRFS_BLOCK_GROUP_DUP)) {
4659 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4668 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4670 struct extent_map *em;
4671 struct map_lookup *map;
4672 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4677 read_lock(&map_tree->map_tree.lock);
4678 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4679 read_unlock(&map_tree->map_tree.lock);
4683 map = (struct map_lookup *)em->bdev;
4684 for (i = 0; i < map->num_stripes; i++) {
4685 if (map->stripes[i].dev->missing) {
4690 if (!map->stripes[i].dev->writeable) {
4697 * If the number of missing devices is larger than max errors,
4698 * we can not write the data into that chunk successfully, so
4701 if (miss_ndevs > btrfs_chunk_max_errors(map))
4704 free_extent_map(em);
4708 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4710 extent_map_tree_init(&tree->map_tree);
4713 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4715 struct extent_map *em;
4718 write_lock(&tree->map_tree.lock);
4719 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4721 remove_extent_mapping(&tree->map_tree, em);
4722 write_unlock(&tree->map_tree.lock);
4726 free_extent_map(em);
4727 /* once for the tree */
4728 free_extent_map(em);
4732 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4734 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4735 struct extent_map *em;
4736 struct map_lookup *map;
4737 struct extent_map_tree *em_tree = &map_tree->map_tree;
4740 read_lock(&em_tree->lock);
4741 em = lookup_extent_mapping(em_tree, logical, len);
4742 read_unlock(&em_tree->lock);
4745 * We could return errors for these cases, but that could get ugly and
4746 * we'd probably do the same thing which is just not do anything else
4747 * and exit, so return 1 so the callers don't try to use other copies.
4750 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
4755 if (em->start > logical || em->start + em->len < logical) {
4756 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
4757 "%Lu-%Lu", logical, logical+len, em->start,
4758 em->start + em->len);
4759 free_extent_map(em);
4763 map = (struct map_lookup *)em->bdev;
4764 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4765 ret = map->num_stripes;
4766 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4767 ret = map->sub_stripes;
4768 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4770 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4774 free_extent_map(em);
4776 btrfs_dev_replace_lock(&fs_info->dev_replace);
4777 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4779 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4784 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4785 struct btrfs_mapping_tree *map_tree,
4788 struct extent_map *em;
4789 struct map_lookup *map;
4790 struct extent_map_tree *em_tree = &map_tree->map_tree;
4791 unsigned long len = root->sectorsize;
4793 read_lock(&em_tree->lock);
4794 em = lookup_extent_mapping(em_tree, logical, len);
4795 read_unlock(&em_tree->lock);
4798 BUG_ON(em->start > logical || em->start + em->len < logical);
4799 map = (struct map_lookup *)em->bdev;
4800 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
4801 len = map->stripe_len * nr_data_stripes(map);
4802 free_extent_map(em);
4806 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4807 u64 logical, u64 len, int mirror_num)
4809 struct extent_map *em;
4810 struct map_lookup *map;
4811 struct extent_map_tree *em_tree = &map_tree->map_tree;
4814 read_lock(&em_tree->lock);
4815 em = lookup_extent_mapping(em_tree, logical, len);
4816 read_unlock(&em_tree->lock);
4819 BUG_ON(em->start > logical || em->start + em->len < logical);
4820 map = (struct map_lookup *)em->bdev;
4821 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
4823 free_extent_map(em);
4827 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4828 struct map_lookup *map, int first, int num,
4829 int optimal, int dev_replace_is_ongoing)
4833 struct btrfs_device *srcdev;
4835 if (dev_replace_is_ongoing &&
4836 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4837 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4838 srcdev = fs_info->dev_replace.srcdev;
4843 * try to avoid the drive that is the source drive for a
4844 * dev-replace procedure, only choose it if no other non-missing
4845 * mirror is available
4847 for (tolerance = 0; tolerance < 2; tolerance++) {
4848 if (map->stripes[optimal].dev->bdev &&
4849 (tolerance || map->stripes[optimal].dev != srcdev))
4851 for (i = first; i < first + num; i++) {
4852 if (map->stripes[i].dev->bdev &&
4853 (tolerance || map->stripes[i].dev != srcdev))
4858 /* we couldn't find one that doesn't fail. Just return something
4859 * and the io error handling code will clean up eventually
4864 static inline int parity_smaller(u64 a, u64 b)
4869 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4870 static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
4872 struct btrfs_bio_stripe s;
4879 for (i = 0; i < num_stripes - 1; i++) {
4880 if (parity_smaller(bbio->raid_map[i],
4881 bbio->raid_map[i+1])) {
4882 s = bbio->stripes[i];
4883 l = bbio->raid_map[i];
4884 bbio->stripes[i] = bbio->stripes[i+1];
4885 bbio->raid_map[i] = bbio->raid_map[i+1];
4886 bbio->stripes[i+1] = s;
4887 bbio->raid_map[i+1] = l;
4895 static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
4897 struct btrfs_bio *bbio = kzalloc(
4898 sizeof(struct btrfs_bio) +
4899 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
4900 sizeof(int) * (real_stripes) +
4901 sizeof(u64) * (real_stripes),
4906 atomic_set(&bbio->error, 0);
4907 atomic_set(&bbio->refs, 1);
4912 void btrfs_get_bbio(struct btrfs_bio *bbio)
4914 WARN_ON(!atomic_read(&bbio->refs));
4915 atomic_inc(&bbio->refs);
4918 void btrfs_put_bbio(struct btrfs_bio *bbio)
4922 if (atomic_dec_and_test(&bbio->refs))
4926 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4927 u64 logical, u64 *length,
4928 struct btrfs_bio **bbio_ret,
4929 int mirror_num, int need_raid_map)
4931 struct extent_map *em;
4932 struct map_lookup *map;
4933 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4934 struct extent_map_tree *em_tree = &map_tree->map_tree;
4937 u64 stripe_end_offset;
4947 int tgtdev_indexes = 0;
4948 struct btrfs_bio *bbio = NULL;
4949 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4950 int dev_replace_is_ongoing = 0;
4951 int num_alloc_stripes;
4952 int patch_the_first_stripe_for_dev_replace = 0;
4953 u64 physical_to_patch_in_first_stripe = 0;
4954 u64 raid56_full_stripe_start = (u64)-1;
4956 read_lock(&em_tree->lock);
4957 em = lookup_extent_mapping(em_tree, logical, *length);
4958 read_unlock(&em_tree->lock);
4961 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4966 if (em->start > logical || em->start + em->len < logical) {
4967 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4968 "found %Lu-%Lu", logical, em->start,
4969 em->start + em->len);
4970 free_extent_map(em);
4974 map = (struct map_lookup *)em->bdev;
4975 offset = logical - em->start;
4977 stripe_len = map->stripe_len;
4980 * stripe_nr counts the total number of stripes we have to stride
4981 * to get to this block
4983 do_div(stripe_nr, stripe_len);
4985 stripe_offset = stripe_nr * stripe_len;
4986 BUG_ON(offset < stripe_offset);
4988 /* stripe_offset is the offset of this block in its stripe*/
4989 stripe_offset = offset - stripe_offset;
4991 /* if we're here for raid56, we need to know the stripe aligned start */
4992 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
4993 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4994 raid56_full_stripe_start = offset;
4996 /* allow a write of a full stripe, but make sure we don't
4997 * allow straddling of stripes
4999 do_div(raid56_full_stripe_start, full_stripe_len);
5000 raid56_full_stripe_start *= full_stripe_len;
5003 if (rw & REQ_DISCARD) {
5004 /* we don't discard raid56 yet */
5005 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5009 *length = min_t(u64, em->len - offset, *length);
5010 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5012 /* For writes to RAID[56], allow a full stripeset across all disks.
5013 For other RAID types and for RAID[56] reads, just allow a single
5014 stripe (on a single disk). */
5015 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
5017 max_len = stripe_len * nr_data_stripes(map) -
5018 (offset - raid56_full_stripe_start);
5020 /* we limit the length of each bio to what fits in a stripe */
5021 max_len = stripe_len - stripe_offset;
5023 *length = min_t(u64, em->len - offset, max_len);
5025 *length = em->len - offset;
5028 /* This is for when we're called from btrfs_merge_bio_hook() and all
5029 it cares about is the length */
5033 btrfs_dev_replace_lock(dev_replace);
5034 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5035 if (!dev_replace_is_ongoing)
5036 btrfs_dev_replace_unlock(dev_replace);
5038 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5039 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5040 dev_replace->tgtdev != NULL) {
5042 * in dev-replace case, for repair case (that's the only
5043 * case where the mirror is selected explicitly when
5044 * calling btrfs_map_block), blocks left of the left cursor
5045 * can also be read from the target drive.
5046 * For REQ_GET_READ_MIRRORS, the target drive is added as
5047 * the last one to the array of stripes. For READ, it also
5048 * needs to be supported using the same mirror number.
5049 * If the requested block is not left of the left cursor,
5050 * EIO is returned. This can happen because btrfs_num_copies()
5051 * returns one more in the dev-replace case.
5053 u64 tmp_length = *length;
5054 struct btrfs_bio *tmp_bbio = NULL;
5055 int tmp_num_stripes;
5056 u64 srcdev_devid = dev_replace->srcdev->devid;
5057 int index_srcdev = 0;
5059 u64 physical_of_found = 0;
5061 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
5062 logical, &tmp_length, &tmp_bbio, 0, 0);
5064 WARN_ON(tmp_bbio != NULL);
5068 tmp_num_stripes = tmp_bbio->num_stripes;
5069 if (mirror_num > tmp_num_stripes) {
5071 * REQ_GET_READ_MIRRORS does not contain this
5072 * mirror, that means that the requested area
5073 * is not left of the left cursor
5076 btrfs_put_bbio(tmp_bbio);
5081 * process the rest of the function using the mirror_num
5082 * of the source drive. Therefore look it up first.
5083 * At the end, patch the device pointer to the one of the
5086 for (i = 0; i < tmp_num_stripes; i++) {
5087 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5089 * In case of DUP, in order to keep it
5090 * simple, only add the mirror with the
5091 * lowest physical address
5094 physical_of_found <=
5095 tmp_bbio->stripes[i].physical)
5100 tmp_bbio->stripes[i].physical;
5105 mirror_num = index_srcdev + 1;
5106 patch_the_first_stripe_for_dev_replace = 1;
5107 physical_to_patch_in_first_stripe = physical_of_found;
5111 btrfs_put_bbio(tmp_bbio);
5115 btrfs_put_bbio(tmp_bbio);
5116 } else if (mirror_num > map->num_stripes) {
5122 stripe_nr_orig = stripe_nr;
5123 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
5124 do_div(stripe_nr_end, map->stripe_len);
5125 stripe_end_offset = stripe_nr_end * map->stripe_len -
5128 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5129 if (rw & REQ_DISCARD)
5130 num_stripes = min_t(u64, map->num_stripes,
5131 stripe_nr_end - stripe_nr_orig);
5132 stripe_index = do_div(stripe_nr, map->num_stripes);
5133 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5135 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
5136 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
5137 num_stripes = map->num_stripes;
5138 else if (mirror_num)
5139 stripe_index = mirror_num - 1;
5141 stripe_index = find_live_mirror(fs_info, map, 0,
5143 current->pid % map->num_stripes,
5144 dev_replace_is_ongoing);
5145 mirror_num = stripe_index + 1;
5148 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
5149 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
5150 num_stripes = map->num_stripes;
5151 } else if (mirror_num) {
5152 stripe_index = mirror_num - 1;
5157 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5158 int factor = map->num_stripes / map->sub_stripes;
5160 stripe_index = do_div(stripe_nr, factor);
5161 stripe_index *= map->sub_stripes;
5163 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5164 num_stripes = map->sub_stripes;
5165 else if (rw & REQ_DISCARD)
5166 num_stripes = min_t(u64, map->sub_stripes *
5167 (stripe_nr_end - stripe_nr_orig),
5169 else if (mirror_num)
5170 stripe_index += mirror_num - 1;
5172 int old_stripe_index = stripe_index;
5173 stripe_index = find_live_mirror(fs_info, map,
5175 map->sub_stripes, stripe_index +
5176 current->pid % map->sub_stripes,
5177 dev_replace_is_ongoing);
5178 mirror_num = stripe_index - old_stripe_index + 1;
5181 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5182 if (need_raid_map &&
5183 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5185 /* push stripe_nr back to the start of the full stripe */
5186 stripe_nr = raid56_full_stripe_start;
5187 do_div(stripe_nr, stripe_len * nr_data_stripes(map));
5189 /* RAID[56] write or recovery. Return all stripes */
5190 num_stripes = map->num_stripes;
5191 max_errors = nr_parity_stripes(map);
5193 *length = map->stripe_len;
5200 * Mirror #0 or #1 means the original data block.
5201 * Mirror #2 is RAID5 parity block.
5202 * Mirror #3 is RAID6 Q block.
5204 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5206 stripe_index = nr_data_stripes(map) +
5209 /* We distribute the parity blocks across stripes */
5210 tmp = stripe_nr + stripe_index;
5211 stripe_index = do_div(tmp, map->num_stripes);
5212 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5213 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5218 * after this do_div call, stripe_nr is the number of stripes
5219 * on this device we have to walk to find the data, and
5220 * stripe_index is the number of our device in the stripe array
5222 stripe_index = do_div(stripe_nr, map->num_stripes);
5223 mirror_num = stripe_index + 1;
5225 BUG_ON(stripe_index >= map->num_stripes);
5227 num_alloc_stripes = num_stripes;
5228 if (dev_replace_is_ongoing) {
5229 if (rw & (REQ_WRITE | REQ_DISCARD))
5230 num_alloc_stripes <<= 1;
5231 if (rw & REQ_GET_READ_MIRRORS)
5232 num_alloc_stripes++;
5233 tgtdev_indexes = num_stripes;
5236 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
5241 if (dev_replace_is_ongoing)
5242 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
5244 /* build raid_map */
5245 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
5246 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5251 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5252 sizeof(struct btrfs_bio_stripe) *
5254 sizeof(int) * tgtdev_indexes);
5256 /* Work out the disk rotation on this stripe-set */
5258 rot = do_div(tmp, num_stripes);
5260 /* Fill in the logical address of each stripe */
5261 tmp = stripe_nr * nr_data_stripes(map);
5262 for (i = 0; i < nr_data_stripes(map); i++)
5263 bbio->raid_map[(i+rot) % num_stripes] =
5264 em->start + (tmp + i) * map->stripe_len;
5266 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5267 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5268 bbio->raid_map[(i+rot+1) % num_stripes] =
5272 if (rw & REQ_DISCARD) {
5274 int sub_stripes = 0;
5275 u64 stripes_per_dev = 0;
5276 u32 remaining_stripes = 0;
5277 u32 last_stripe = 0;
5280 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5281 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5284 sub_stripes = map->sub_stripes;
5286 factor = map->num_stripes / sub_stripes;
5287 stripes_per_dev = div_u64_rem(stripe_nr_end -
5290 &remaining_stripes);
5291 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5292 last_stripe *= sub_stripes;
5295 for (i = 0; i < num_stripes; i++) {
5296 bbio->stripes[i].physical =
5297 map->stripes[stripe_index].physical +
5298 stripe_offset + stripe_nr * map->stripe_len;
5299 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5301 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5302 BTRFS_BLOCK_GROUP_RAID10)) {
5303 bbio->stripes[i].length = stripes_per_dev *
5306 if (i / sub_stripes < remaining_stripes)
5307 bbio->stripes[i].length +=
5311 * Special for the first stripe and
5314 * |-------|...|-------|
5318 if (i < sub_stripes)
5319 bbio->stripes[i].length -=
5322 if (stripe_index >= last_stripe &&
5323 stripe_index <= (last_stripe +
5325 bbio->stripes[i].length -=
5328 if (i == sub_stripes - 1)
5331 bbio->stripes[i].length = *length;
5334 if (stripe_index == map->num_stripes) {
5335 /* This could only happen for RAID0/10 */
5341 for (i = 0; i < num_stripes; i++) {
5342 bbio->stripes[i].physical =
5343 map->stripes[stripe_index].physical +
5345 stripe_nr * map->stripe_len;
5346 bbio->stripes[i].dev =
5347 map->stripes[stripe_index].dev;
5352 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5353 max_errors = btrfs_chunk_max_errors(map);
5356 sort_parity_stripes(bbio, num_stripes);
5359 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5360 dev_replace->tgtdev != NULL) {
5361 int index_where_to_add;
5362 u64 srcdev_devid = dev_replace->srcdev->devid;
5365 * duplicate the write operations while the dev replace
5366 * procedure is running. Since the copying of the old disk
5367 * to the new disk takes place at run time while the
5368 * filesystem is mounted writable, the regular write
5369 * operations to the old disk have to be duplicated to go
5370 * to the new disk as well.
5371 * Note that device->missing is handled by the caller, and
5372 * that the write to the old disk is already set up in the
5375 index_where_to_add = num_stripes;
5376 for (i = 0; i < num_stripes; i++) {
5377 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5378 /* write to new disk, too */
5379 struct btrfs_bio_stripe *new =
5380 bbio->stripes + index_where_to_add;
5381 struct btrfs_bio_stripe *old =
5384 new->physical = old->physical;
5385 new->length = old->length;
5386 new->dev = dev_replace->tgtdev;
5387 bbio->tgtdev_map[i] = index_where_to_add;
5388 index_where_to_add++;
5393 num_stripes = index_where_to_add;
5394 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5395 dev_replace->tgtdev != NULL) {
5396 u64 srcdev_devid = dev_replace->srcdev->devid;
5397 int index_srcdev = 0;
5399 u64 physical_of_found = 0;
5402 * During the dev-replace procedure, the target drive can
5403 * also be used to read data in case it is needed to repair
5404 * a corrupt block elsewhere. This is possible if the
5405 * requested area is left of the left cursor. In this area,
5406 * the target drive is a full copy of the source drive.
5408 for (i = 0; i < num_stripes; i++) {
5409 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5411 * In case of DUP, in order to keep it
5412 * simple, only add the mirror with the
5413 * lowest physical address
5416 physical_of_found <=
5417 bbio->stripes[i].physical)
5421 physical_of_found = bbio->stripes[i].physical;
5425 u64 length = map->stripe_len;
5427 if (physical_of_found + length <=
5428 dev_replace->cursor_left) {
5429 struct btrfs_bio_stripe *tgtdev_stripe =
5430 bbio->stripes + num_stripes;
5432 tgtdev_stripe->physical = physical_of_found;
5433 tgtdev_stripe->length =
5434 bbio->stripes[index_srcdev].length;
5435 tgtdev_stripe->dev = dev_replace->tgtdev;
5436 bbio->tgtdev_map[index_srcdev] = num_stripes;
5445 bbio->map_type = map->type;
5446 bbio->num_stripes = num_stripes;
5447 bbio->max_errors = max_errors;
5448 bbio->mirror_num = mirror_num;
5449 bbio->num_tgtdevs = tgtdev_indexes;
5452 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5453 * mirror_num == num_stripes + 1 && dev_replace target drive is
5454 * available as a mirror
5456 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5457 WARN_ON(num_stripes > 1);
5458 bbio->stripes[0].dev = dev_replace->tgtdev;
5459 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5460 bbio->mirror_num = map->num_stripes + 1;
5463 if (dev_replace_is_ongoing)
5464 btrfs_dev_replace_unlock(dev_replace);
5465 free_extent_map(em);
5469 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5470 u64 logical, u64 *length,
5471 struct btrfs_bio **bbio_ret, int mirror_num)
5473 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5477 /* For Scrub/replace */
5478 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5479 u64 logical, u64 *length,
5480 struct btrfs_bio **bbio_ret, int mirror_num,
5483 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5484 mirror_num, need_raid_map);
5487 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5488 u64 chunk_start, u64 physical, u64 devid,
5489 u64 **logical, int *naddrs, int *stripe_len)
5491 struct extent_map_tree *em_tree = &map_tree->map_tree;
5492 struct extent_map *em;
5493 struct map_lookup *map;
5501 read_lock(&em_tree->lock);
5502 em = lookup_extent_mapping(em_tree, chunk_start, 1);
5503 read_unlock(&em_tree->lock);
5506 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
5511 if (em->start != chunk_start) {
5512 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5513 em->start, chunk_start);
5514 free_extent_map(em);
5517 map = (struct map_lookup *)em->bdev;
5520 rmap_len = map->stripe_len;
5522 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5523 do_div(length, map->num_stripes / map->sub_stripes);
5524 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5525 do_div(length, map->num_stripes);
5526 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5527 do_div(length, nr_data_stripes(map));
5528 rmap_len = map->stripe_len * nr_data_stripes(map);
5531 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
5532 BUG_ON(!buf); /* -ENOMEM */
5534 for (i = 0; i < map->num_stripes; i++) {
5535 if (devid && map->stripes[i].dev->devid != devid)
5537 if (map->stripes[i].physical > physical ||
5538 map->stripes[i].physical + length <= physical)
5541 stripe_nr = physical - map->stripes[i].physical;
5542 do_div(stripe_nr, map->stripe_len);
5544 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5545 stripe_nr = stripe_nr * map->num_stripes + i;
5546 do_div(stripe_nr, map->sub_stripes);
5547 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5548 stripe_nr = stripe_nr * map->num_stripes + i;
5549 } /* else if RAID[56], multiply by nr_data_stripes().
5550 * Alternatively, just use rmap_len below instead of
5551 * map->stripe_len */
5553 bytenr = chunk_start + stripe_nr * rmap_len;
5554 WARN_ON(nr >= map->num_stripes);
5555 for (j = 0; j < nr; j++) {
5556 if (buf[j] == bytenr)
5560 WARN_ON(nr >= map->num_stripes);
5567 *stripe_len = rmap_len;
5569 free_extent_map(em);
5573 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5575 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5576 bio_endio_nodec(bio, err);
5578 bio_endio(bio, err);
5579 btrfs_put_bbio(bbio);
5582 static void btrfs_end_bio(struct bio *bio, int err)
5584 struct btrfs_bio *bbio = bio->bi_private;
5585 struct btrfs_device *dev = bbio->stripes[0].dev;
5586 int is_orig_bio = 0;
5589 atomic_inc(&bbio->error);
5590 if (err == -EIO || err == -EREMOTEIO) {
5591 unsigned int stripe_index =
5592 btrfs_io_bio(bio)->stripe_index;
5594 BUG_ON(stripe_index >= bbio->num_stripes);
5595 dev = bbio->stripes[stripe_index].dev;
5597 if (bio->bi_rw & WRITE)
5598 btrfs_dev_stat_inc(dev,
5599 BTRFS_DEV_STAT_WRITE_ERRS);
5601 btrfs_dev_stat_inc(dev,
5602 BTRFS_DEV_STAT_READ_ERRS);
5603 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5604 btrfs_dev_stat_inc(dev,
5605 BTRFS_DEV_STAT_FLUSH_ERRS);
5606 btrfs_dev_stat_print_on_error(dev);
5611 if (bio == bbio->orig_bio)
5614 btrfs_bio_counter_dec(bbio->fs_info);
5616 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5619 bio = bbio->orig_bio;
5622 bio->bi_private = bbio->private;
5623 bio->bi_end_io = bbio->end_io;
5624 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5625 /* only send an error to the higher layers if it is
5626 * beyond the tolerance of the btrfs bio
5628 if (atomic_read(&bbio->error) > bbio->max_errors) {
5632 * this bio is actually up to date, we didn't
5633 * go over the max number of errors
5635 set_bit(BIO_UPTODATE, &bio->bi_flags);
5639 btrfs_end_bbio(bbio, bio, err);
5640 } else if (!is_orig_bio) {
5646 * see run_scheduled_bios for a description of why bios are collected for
5649 * This will add one bio to the pending list for a device and make sure
5650 * the work struct is scheduled.
5652 static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5653 struct btrfs_device *device,
5654 int rw, struct bio *bio)
5656 int should_queue = 1;
5657 struct btrfs_pending_bios *pending_bios;
5659 if (device->missing || !device->bdev) {
5660 bio_endio(bio, -EIO);
5664 /* don't bother with additional async steps for reads, right now */
5665 if (!(rw & REQ_WRITE)) {
5667 btrfsic_submit_bio(rw, bio);
5673 * nr_async_bios allows us to reliably return congestion to the
5674 * higher layers. Otherwise, the async bio makes it appear we have
5675 * made progress against dirty pages when we've really just put it
5676 * on a queue for later
5678 atomic_inc(&root->fs_info->nr_async_bios);
5679 WARN_ON(bio->bi_next);
5680 bio->bi_next = NULL;
5683 spin_lock(&device->io_lock);
5684 if (bio->bi_rw & REQ_SYNC)
5685 pending_bios = &device->pending_sync_bios;
5687 pending_bios = &device->pending_bios;
5689 if (pending_bios->tail)
5690 pending_bios->tail->bi_next = bio;
5692 pending_bios->tail = bio;
5693 if (!pending_bios->head)
5694 pending_bios->head = bio;
5695 if (device->running_pending)
5698 spin_unlock(&device->io_lock);
5701 btrfs_queue_work(root->fs_info->submit_workers,
5705 static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5708 struct bio_vec *prev;
5709 struct request_queue *q = bdev_get_queue(bdev);
5710 unsigned int max_sectors = queue_max_sectors(q);
5711 struct bvec_merge_data bvm = {
5713 .bi_sector = sector,
5714 .bi_rw = bio->bi_rw,
5717 if (WARN_ON(bio->bi_vcnt == 0))
5720 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5721 if (bio_sectors(bio) > max_sectors)
5724 if (!q->merge_bvec_fn)
5727 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
5728 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5733 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5734 struct bio *bio, u64 physical, int dev_nr,
5737 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5739 bio->bi_private = bbio;
5740 btrfs_io_bio(bio)->stripe_index = dev_nr;
5741 bio->bi_end_io = btrfs_end_bio;
5742 bio->bi_iter.bi_sector = physical >> 9;
5745 struct rcu_string *name;
5748 name = rcu_dereference(dev->name);
5749 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5750 "(%s id %llu), size=%u\n", rw,
5751 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5752 name->str, dev->devid, bio->bi_iter.bi_size);
5756 bio->bi_bdev = dev->bdev;
5758 btrfs_bio_counter_inc_noblocked(root->fs_info);
5761 btrfs_schedule_bio(root, dev, rw, bio);
5763 btrfsic_submit_bio(rw, bio);
5766 static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5767 struct bio *first_bio, struct btrfs_device *dev,
5768 int dev_nr, int rw, int async)
5770 struct bio_vec *bvec = first_bio->bi_io_vec;
5772 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5773 u64 physical = bbio->stripes[dev_nr].physical;
5776 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5780 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5781 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5782 bvec->bv_offset) < bvec->bv_len) {
5783 u64 len = bio->bi_iter.bi_size;
5785 atomic_inc(&bbio->stripes_pending);
5786 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5794 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5798 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5800 atomic_inc(&bbio->error);
5801 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5802 /* Shoud be the original bio. */
5803 WARN_ON(bio != bbio->orig_bio);
5805 bio->bi_private = bbio->private;
5806 bio->bi_end_io = bbio->end_io;
5807 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5808 bio->bi_iter.bi_sector = logical >> 9;
5810 btrfs_end_bbio(bbio, bio, -EIO);
5814 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5815 int mirror_num, int async_submit)
5817 struct btrfs_device *dev;
5818 struct bio *first_bio = bio;
5819 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
5825 struct btrfs_bio *bbio = NULL;
5827 length = bio->bi_iter.bi_size;
5828 map_length = length;
5830 btrfs_bio_counter_inc_blocked(root->fs_info);
5831 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5834 btrfs_bio_counter_dec(root->fs_info);
5838 total_devs = bbio->num_stripes;
5839 bbio->orig_bio = first_bio;
5840 bbio->private = first_bio->bi_private;
5841 bbio->end_io = first_bio->bi_end_io;
5842 bbio->fs_info = root->fs_info;
5843 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5845 if (bbio->raid_map) {
5846 /* In this case, map_length has been set to the length of
5847 a single stripe; not the whole write */
5849 ret = raid56_parity_write(root, bio, bbio, map_length);
5851 ret = raid56_parity_recover(root, bio, bbio, map_length,
5855 btrfs_bio_counter_dec(root->fs_info);
5859 if (map_length < length) {
5860 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5861 logical, length, map_length);
5865 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
5866 dev = bbio->stripes[dev_nr].dev;
5867 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5868 bbio_error(bbio, first_bio, logical);
5873 * Check and see if we're ok with this bio based on it's size
5874 * and offset with the given device.
5876 if (!bio_size_ok(dev->bdev, first_bio,
5877 bbio->stripes[dev_nr].physical >> 9)) {
5878 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5879 dev_nr, rw, async_submit);
5884 if (dev_nr < total_devs - 1) {
5885 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
5886 BUG_ON(!bio); /* -ENOMEM */
5889 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
5892 submit_stripe_bio(root, bbio, bio,
5893 bbio->stripes[dev_nr].physical, dev_nr, rw,
5896 btrfs_bio_counter_dec(root->fs_info);
5900 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5903 struct btrfs_device *device;
5904 struct btrfs_fs_devices *cur_devices;
5906 cur_devices = fs_info->fs_devices;
5907 while (cur_devices) {
5909 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5910 device = __find_device(&cur_devices->devices,
5915 cur_devices = cur_devices->seed;
5920 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5921 struct btrfs_fs_devices *fs_devices,
5922 u64 devid, u8 *dev_uuid)
5924 struct btrfs_device *device;
5926 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5930 list_add(&device->dev_list, &fs_devices->devices);
5931 device->fs_devices = fs_devices;
5932 fs_devices->num_devices++;
5934 device->missing = 1;
5935 fs_devices->missing_devices++;
5941 * btrfs_alloc_device - allocate struct btrfs_device
5942 * @fs_info: used only for generating a new devid, can be NULL if
5943 * devid is provided (i.e. @devid != NULL).
5944 * @devid: a pointer to devid for this device. If NULL a new devid
5946 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5949 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5950 * on error. Returned struct is not linked onto any lists and can be
5951 * destroyed with kfree() right away.
5953 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5957 struct btrfs_device *dev;
5960 if (WARN_ON(!devid && !fs_info))
5961 return ERR_PTR(-EINVAL);
5963 dev = __alloc_device();
5972 ret = find_next_devid(fs_info, &tmp);
5975 return ERR_PTR(ret);
5981 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5983 generate_random_uuid(dev->uuid);
5985 btrfs_init_work(&dev->work, btrfs_submit_helper,
5986 pending_bios_fn, NULL, NULL);
5991 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5992 struct extent_buffer *leaf,
5993 struct btrfs_chunk *chunk)
5995 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5996 struct map_lookup *map;
5997 struct extent_map *em;
6001 u8 uuid[BTRFS_UUID_SIZE];
6006 logical = key->offset;
6007 length = btrfs_chunk_length(leaf, chunk);
6009 read_lock(&map_tree->map_tree.lock);
6010 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
6011 read_unlock(&map_tree->map_tree.lock);
6013 /* already mapped? */
6014 if (em && em->start <= logical && em->start + em->len > logical) {
6015 free_extent_map(em);
6018 free_extent_map(em);
6021 em = alloc_extent_map();
6024 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6025 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6027 free_extent_map(em);
6031 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6032 em->bdev = (struct block_device *)map;
6033 em->start = logical;
6036 em->block_start = 0;
6037 em->block_len = em->len;
6039 map->num_stripes = num_stripes;
6040 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6041 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6042 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6043 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6044 map->type = btrfs_chunk_type(leaf, chunk);
6045 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6046 for (i = 0; i < num_stripes; i++) {
6047 map->stripes[i].physical =
6048 btrfs_stripe_offset_nr(leaf, chunk, i);
6049 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6050 read_extent_buffer(leaf, uuid, (unsigned long)
6051 btrfs_stripe_dev_uuid_nr(chunk, i),
6053 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6055 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
6056 free_extent_map(em);
6059 if (!map->stripes[i].dev) {
6060 map->stripes[i].dev =
6061 add_missing_dev(root, root->fs_info->fs_devices,
6063 if (!map->stripes[i].dev) {
6064 free_extent_map(em);
6068 map->stripes[i].dev->in_fs_metadata = 1;
6071 write_lock(&map_tree->map_tree.lock);
6072 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
6073 write_unlock(&map_tree->map_tree.lock);
6074 BUG_ON(ret); /* Tree corruption */
6075 free_extent_map(em);
6080 static void fill_device_from_item(struct extent_buffer *leaf,
6081 struct btrfs_dev_item *dev_item,
6082 struct btrfs_device *device)
6086 device->devid = btrfs_device_id(leaf, dev_item);
6087 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6088 device->total_bytes = device->disk_total_bytes;
6089 device->commit_total_bytes = device->disk_total_bytes;
6090 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6091 device->commit_bytes_used = device->bytes_used;
6092 device->type = btrfs_device_type(leaf, dev_item);
6093 device->io_align = btrfs_device_io_align(leaf, dev_item);
6094 device->io_width = btrfs_device_io_width(leaf, dev_item);
6095 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6096 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6097 device->is_tgtdev_for_dev_replace = 0;
6099 ptr = btrfs_device_uuid(dev_item);
6100 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6103 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6106 struct btrfs_fs_devices *fs_devices;
6109 BUG_ON(!mutex_is_locked(&uuid_mutex));
6111 fs_devices = root->fs_info->fs_devices->seed;
6112 while (fs_devices) {
6113 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6116 fs_devices = fs_devices->seed;
6119 fs_devices = find_fsid(fsid);
6121 if (!btrfs_test_opt(root, DEGRADED))
6122 return ERR_PTR(-ENOENT);
6124 fs_devices = alloc_fs_devices(fsid);
6125 if (IS_ERR(fs_devices))
6128 fs_devices->seeding = 1;
6129 fs_devices->opened = 1;
6133 fs_devices = clone_fs_devices(fs_devices);
6134 if (IS_ERR(fs_devices))
6137 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
6138 root->fs_info->bdev_holder);
6140 free_fs_devices(fs_devices);
6141 fs_devices = ERR_PTR(ret);
6145 if (!fs_devices->seeding) {
6146 __btrfs_close_devices(fs_devices);
6147 free_fs_devices(fs_devices);
6148 fs_devices = ERR_PTR(-EINVAL);
6152 fs_devices->seed = root->fs_info->fs_devices->seed;
6153 root->fs_info->fs_devices->seed = fs_devices;
6158 static int read_one_dev(struct btrfs_root *root,
6159 struct extent_buffer *leaf,
6160 struct btrfs_dev_item *dev_item)
6162 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6163 struct btrfs_device *device;
6166 u8 fs_uuid[BTRFS_UUID_SIZE];
6167 u8 dev_uuid[BTRFS_UUID_SIZE];
6169 devid = btrfs_device_id(leaf, dev_item);
6170 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
6172 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6175 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6176 fs_devices = open_seed_devices(root, fs_uuid);
6177 if (IS_ERR(fs_devices))
6178 return PTR_ERR(fs_devices);
6181 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
6183 if (!btrfs_test_opt(root, DEGRADED))
6186 btrfs_warn(root->fs_info, "devid %llu missing", devid);
6187 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6191 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6194 if(!device->bdev && !device->missing) {
6196 * this happens when a device that was properly setup
6197 * in the device info lists suddenly goes bad.
6198 * device->bdev is NULL, and so we have to set
6199 * device->missing to one here
6201 device->fs_devices->missing_devices++;
6202 device->missing = 1;
6205 /* Move the device to its own fs_devices */
6206 if (device->fs_devices != fs_devices) {
6207 ASSERT(device->missing);
6209 list_move(&device->dev_list, &fs_devices->devices);
6210 device->fs_devices->num_devices--;
6211 fs_devices->num_devices++;
6213 device->fs_devices->missing_devices--;
6214 fs_devices->missing_devices++;
6216 device->fs_devices = fs_devices;
6220 if (device->fs_devices != root->fs_info->fs_devices) {
6221 BUG_ON(device->writeable);
6222 if (device->generation !=
6223 btrfs_device_generation(leaf, dev_item))
6227 fill_device_from_item(leaf, dev_item, device);
6228 device->in_fs_metadata = 1;
6229 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
6230 device->fs_devices->total_rw_bytes += device->total_bytes;
6231 spin_lock(&root->fs_info->free_chunk_lock);
6232 root->fs_info->free_chunk_space += device->total_bytes -
6234 spin_unlock(&root->fs_info->free_chunk_lock);
6240 int btrfs_read_sys_array(struct btrfs_root *root)
6242 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
6243 struct extent_buffer *sb;
6244 struct btrfs_disk_key *disk_key;
6245 struct btrfs_chunk *chunk;
6247 unsigned long sb_array_offset;
6253 struct btrfs_key key;
6255 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6257 * This will create extent buffer of nodesize, superblock size is
6258 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6259 * overallocate but we can keep it as-is, only the first page is used.
6261 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
6264 btrfs_set_buffer_uptodate(sb);
6265 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6267 * The sb extent buffer is artifical and just used to read the system array.
6268 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6269 * pages up-to-date when the page is larger: extent does not cover the
6270 * whole page and consequently check_page_uptodate does not find all
6271 * the page's extents up-to-date (the hole beyond sb),
6272 * write_extent_buffer then triggers a WARN_ON.
6274 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6275 * but sb spans only this function. Add an explicit SetPageUptodate call
6276 * to silence the warning eg. on PowerPC 64.
6278 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
6279 SetPageUptodate(sb->pages[0]);
6281 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
6282 array_size = btrfs_super_sys_array_size(super_copy);
6284 array_ptr = super_copy->sys_chunk_array;
6285 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6288 while (cur_offset < array_size) {
6289 disk_key = (struct btrfs_disk_key *)array_ptr;
6290 len = sizeof(*disk_key);
6291 if (cur_offset + len > array_size)
6292 goto out_short_read;
6294 btrfs_disk_key_to_cpu(&key, disk_key);
6297 sb_array_offset += len;
6300 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
6301 chunk = (struct btrfs_chunk *)sb_array_offset;
6303 * At least one btrfs_chunk with one stripe must be
6304 * present, exact stripe count check comes afterwards
6306 len = btrfs_chunk_item_size(1);
6307 if (cur_offset + len > array_size)
6308 goto out_short_read;
6310 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6311 len = btrfs_chunk_item_size(num_stripes);
6312 if (cur_offset + len > array_size)
6313 goto out_short_read;
6315 ret = read_one_chunk(root, &key, sb, chunk);
6323 sb_array_offset += len;
6326 free_extent_buffer(sb);
6330 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6332 free_extent_buffer(sb);
6336 int btrfs_read_chunk_tree(struct btrfs_root *root)
6338 struct btrfs_path *path;
6339 struct extent_buffer *leaf;
6340 struct btrfs_key key;
6341 struct btrfs_key found_key;
6345 root = root->fs_info->chunk_root;
6347 path = btrfs_alloc_path();
6351 mutex_lock(&uuid_mutex);
6355 * Read all device items, and then all the chunk items. All
6356 * device items are found before any chunk item (their object id
6357 * is smaller than the lowest possible object id for a chunk
6358 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6360 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6363 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6367 leaf = path->nodes[0];
6368 slot = path->slots[0];
6369 if (slot >= btrfs_header_nritems(leaf)) {
6370 ret = btrfs_next_leaf(root, path);
6377 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6378 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6379 struct btrfs_dev_item *dev_item;
6380 dev_item = btrfs_item_ptr(leaf, slot,
6381 struct btrfs_dev_item);
6382 ret = read_one_dev(root, leaf, dev_item);
6385 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6386 struct btrfs_chunk *chunk;
6387 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6388 ret = read_one_chunk(root, &found_key, leaf, chunk);
6396 unlock_chunks(root);
6397 mutex_unlock(&uuid_mutex);
6399 btrfs_free_path(path);
6403 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6405 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6406 struct btrfs_device *device;
6408 while (fs_devices) {
6409 mutex_lock(&fs_devices->device_list_mutex);
6410 list_for_each_entry(device, &fs_devices->devices, dev_list)
6411 device->dev_root = fs_info->dev_root;
6412 mutex_unlock(&fs_devices->device_list_mutex);
6414 fs_devices = fs_devices->seed;
6418 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6422 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6423 btrfs_dev_stat_reset(dev, i);
6426 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6428 struct btrfs_key key;
6429 struct btrfs_key found_key;
6430 struct btrfs_root *dev_root = fs_info->dev_root;
6431 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6432 struct extent_buffer *eb;
6435 struct btrfs_device *device;
6436 struct btrfs_path *path = NULL;
6439 path = btrfs_alloc_path();
6445 mutex_lock(&fs_devices->device_list_mutex);
6446 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6448 struct btrfs_dev_stats_item *ptr;
6451 key.type = BTRFS_DEV_STATS_KEY;
6452 key.offset = device->devid;
6453 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6455 __btrfs_reset_dev_stats(device);
6456 device->dev_stats_valid = 1;
6457 btrfs_release_path(path);
6460 slot = path->slots[0];
6461 eb = path->nodes[0];
6462 btrfs_item_key_to_cpu(eb, &found_key, slot);
6463 item_size = btrfs_item_size_nr(eb, slot);
6465 ptr = btrfs_item_ptr(eb, slot,
6466 struct btrfs_dev_stats_item);
6468 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6469 if (item_size >= (1 + i) * sizeof(__le64))
6470 btrfs_dev_stat_set(device, i,
6471 btrfs_dev_stats_value(eb, ptr, i));
6473 btrfs_dev_stat_reset(device, i);
6476 device->dev_stats_valid = 1;
6477 btrfs_dev_stat_print_on_load(device);
6478 btrfs_release_path(path);
6480 mutex_unlock(&fs_devices->device_list_mutex);
6483 btrfs_free_path(path);
6484 return ret < 0 ? ret : 0;
6487 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6488 struct btrfs_root *dev_root,
6489 struct btrfs_device *device)
6491 struct btrfs_path *path;
6492 struct btrfs_key key;
6493 struct extent_buffer *eb;
6494 struct btrfs_dev_stats_item *ptr;
6499 key.type = BTRFS_DEV_STATS_KEY;
6500 key.offset = device->devid;
6502 path = btrfs_alloc_path();
6504 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6506 printk_in_rcu(KERN_WARNING "BTRFS: "
6507 "error %d while searching for dev_stats item for device %s!\n",
6508 ret, rcu_str_deref(device->name));
6513 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6514 /* need to delete old one and insert a new one */
6515 ret = btrfs_del_item(trans, dev_root, path);
6517 printk_in_rcu(KERN_WARNING "BTRFS: "
6518 "delete too small dev_stats item for device %s failed %d!\n",
6519 rcu_str_deref(device->name), ret);
6526 /* need to insert a new item */
6527 btrfs_release_path(path);
6528 ret = btrfs_insert_empty_item(trans, dev_root, path,
6529 &key, sizeof(*ptr));
6531 printk_in_rcu(KERN_WARNING "BTRFS: "
6532 "insert dev_stats item for device %s failed %d!\n",
6533 rcu_str_deref(device->name), ret);
6538 eb = path->nodes[0];
6539 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6540 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6541 btrfs_set_dev_stats_value(eb, ptr, i,
6542 btrfs_dev_stat_read(device, i));
6543 btrfs_mark_buffer_dirty(eb);
6546 btrfs_free_path(path);
6551 * called from commit_transaction. Writes all changed device stats to disk.
6553 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6554 struct btrfs_fs_info *fs_info)
6556 struct btrfs_root *dev_root = fs_info->dev_root;
6557 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6558 struct btrfs_device *device;
6562 mutex_lock(&fs_devices->device_list_mutex);
6563 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6564 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
6567 stats_cnt = atomic_read(&device->dev_stats_ccnt);
6568 ret = update_dev_stat_item(trans, dev_root, device);
6570 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
6572 mutex_unlock(&fs_devices->device_list_mutex);
6577 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6579 btrfs_dev_stat_inc(dev, index);
6580 btrfs_dev_stat_print_on_error(dev);
6583 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
6585 if (!dev->dev_stats_valid)
6587 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6588 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6589 rcu_str_deref(dev->name),
6590 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6591 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6592 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6593 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6594 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6597 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6601 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6602 if (btrfs_dev_stat_read(dev, i) != 0)
6604 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6605 return; /* all values == 0, suppress message */
6607 printk_in_rcu(KERN_INFO "BTRFS: "
6608 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6609 rcu_str_deref(dev->name),
6610 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6611 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6612 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6613 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6614 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6617 int btrfs_get_dev_stats(struct btrfs_root *root,
6618 struct btrfs_ioctl_get_dev_stats *stats)
6620 struct btrfs_device *dev;
6621 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6624 mutex_lock(&fs_devices->device_list_mutex);
6625 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
6626 mutex_unlock(&fs_devices->device_list_mutex);
6629 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
6631 } else if (!dev->dev_stats_valid) {
6632 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
6634 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
6635 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6636 if (stats->nr_items > i)
6638 btrfs_dev_stat_read_and_reset(dev, i);
6640 btrfs_dev_stat_reset(dev, i);
6643 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6644 if (stats->nr_items > i)
6645 stats->values[i] = btrfs_dev_stat_read(dev, i);
6647 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6648 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6652 int btrfs_scratch_superblock(struct btrfs_device *device)
6654 struct buffer_head *bh;
6655 struct btrfs_super_block *disk_super;
6657 bh = btrfs_read_dev_super(device->bdev);
6660 disk_super = (struct btrfs_super_block *)bh->b_data;
6662 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6663 set_buffer_dirty(bh);
6664 sync_dirty_buffer(bh);
6671 * Update the size of all devices, which is used for writing out the
6674 void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6676 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6677 struct btrfs_device *curr, *next;
6679 if (list_empty(&fs_devices->resized_devices))
6682 mutex_lock(&fs_devices->device_list_mutex);
6683 lock_chunks(fs_info->dev_root);
6684 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6686 list_del_init(&curr->resized_list);
6687 curr->commit_total_bytes = curr->disk_total_bytes;
6689 unlock_chunks(fs_info->dev_root);
6690 mutex_unlock(&fs_devices->device_list_mutex);
6693 /* Must be invoked during the transaction commit */
6694 void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6695 struct btrfs_transaction *transaction)
6697 struct extent_map *em;
6698 struct map_lookup *map;
6699 struct btrfs_device *dev;
6702 if (list_empty(&transaction->pending_chunks))
6705 /* In order to kick the device replace finish process */
6707 list_for_each_entry(em, &transaction->pending_chunks, list) {
6708 map = (struct map_lookup *)em->bdev;
6710 for (i = 0; i < map->num_stripes; i++) {
6711 dev = map->stripes[i].dev;
6712 dev->commit_bytes_used = dev->bytes_used;
6715 unlock_chunks(root);