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>
33 #include "extent_map.h"
35 #include "transaction.h"
36 #include "print-tree.h"
39 #include "async-thread.h"
40 #include "check-integrity.h"
41 #include "rcu-string.h"
43 #include "dev-replace.h"
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 static DEFINE_MUTEX(uuid_mutex);
54 static LIST_HEAD(fs_uuids);
56 static void lock_chunks(struct btrfs_root *root)
58 mutex_lock(&root->fs_info->chunk_mutex);
61 static void unlock_chunks(struct btrfs_root *root)
63 mutex_unlock(&root->fs_info->chunk_mutex);
66 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
68 struct btrfs_device *device;
69 WARN_ON(fs_devices->opened);
70 while (!list_empty(&fs_devices->devices)) {
71 device = list_entry(fs_devices->devices.next,
72 struct btrfs_device, dev_list);
73 list_del(&device->dev_list);
74 rcu_string_free(device->name);
80 static void btrfs_kobject_uevent(struct block_device *bdev,
81 enum kobject_action action)
85 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
87 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
89 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
90 &disk_to_dev(bdev->bd_disk)->kobj);
93 void btrfs_cleanup_fs_uuids(void)
95 struct btrfs_fs_devices *fs_devices;
97 while (!list_empty(&fs_uuids)) {
98 fs_devices = list_entry(fs_uuids.next,
99 struct btrfs_fs_devices, list);
100 list_del(&fs_devices->list);
101 free_fs_devices(fs_devices);
105 static noinline struct btrfs_device *__find_device(struct list_head *head,
108 struct btrfs_device *dev;
110 list_for_each_entry(dev, head, dev_list) {
111 if (dev->devid == devid &&
112 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
119 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
121 struct btrfs_fs_devices *fs_devices;
123 list_for_each_entry(fs_devices, &fs_uuids, list) {
124 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
131 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
132 int flush, struct block_device **bdev,
133 struct buffer_head **bh)
137 *bdev = blkdev_get_by_path(device_path, flags, holder);
140 ret = PTR_ERR(*bdev);
141 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
146 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
147 ret = set_blocksize(*bdev, 4096);
149 blkdev_put(*bdev, flags);
152 invalidate_bdev(*bdev);
153 *bh = btrfs_read_dev_super(*bdev);
156 blkdev_put(*bdev, flags);
168 static void requeue_list(struct btrfs_pending_bios *pending_bios,
169 struct bio *head, struct bio *tail)
172 struct bio *old_head;
174 old_head = pending_bios->head;
175 pending_bios->head = head;
176 if (pending_bios->tail)
177 tail->bi_next = old_head;
179 pending_bios->tail = tail;
183 * we try to collect pending bios for a device so we don't get a large
184 * number of procs sending bios down to the same device. This greatly
185 * improves the schedulers ability to collect and merge the bios.
187 * But, it also turns into a long list of bios to process and that is sure
188 * to eventually make the worker thread block. The solution here is to
189 * make some progress and then put this work struct back at the end of
190 * the list if the block device is congested. This way, multiple devices
191 * can make progress from a single worker thread.
193 static noinline void run_scheduled_bios(struct btrfs_device *device)
196 struct backing_dev_info *bdi;
197 struct btrfs_fs_info *fs_info;
198 struct btrfs_pending_bios *pending_bios;
202 unsigned long num_run;
203 unsigned long batch_run = 0;
205 unsigned long last_waited = 0;
207 int sync_pending = 0;
208 struct blk_plug plug;
211 * this function runs all the bios we've collected for
212 * a particular device. We don't want to wander off to
213 * another device without first sending all of these down.
214 * So, setup a plug here and finish it off before we return
216 blk_start_plug(&plug);
218 bdi = blk_get_backing_dev_info(device->bdev);
219 fs_info = device->dev_root->fs_info;
220 limit = btrfs_async_submit_limit(fs_info);
221 limit = limit * 2 / 3;
224 spin_lock(&device->io_lock);
229 /* take all the bios off the list at once and process them
230 * later on (without the lock held). But, remember the
231 * tail and other pointers so the bios can be properly reinserted
232 * into the list if we hit congestion
234 if (!force_reg && device->pending_sync_bios.head) {
235 pending_bios = &device->pending_sync_bios;
238 pending_bios = &device->pending_bios;
242 pending = pending_bios->head;
243 tail = pending_bios->tail;
244 WARN_ON(pending && !tail);
247 * if pending was null this time around, no bios need processing
248 * at all and we can stop. Otherwise it'll loop back up again
249 * and do an additional check so no bios are missed.
251 * device->running_pending is used to synchronize with the
254 if (device->pending_sync_bios.head == NULL &&
255 device->pending_bios.head == NULL) {
257 device->running_pending = 0;
260 device->running_pending = 1;
263 pending_bios->head = NULL;
264 pending_bios->tail = NULL;
266 spin_unlock(&device->io_lock);
271 /* we want to work on both lists, but do more bios on the
272 * sync list than the regular list
275 pending_bios != &device->pending_sync_bios &&
276 device->pending_sync_bios.head) ||
277 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
278 device->pending_bios.head)) {
279 spin_lock(&device->io_lock);
280 requeue_list(pending_bios, pending, tail);
285 pending = pending->bi_next;
288 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
289 waitqueue_active(&fs_info->async_submit_wait))
290 wake_up(&fs_info->async_submit_wait);
292 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
295 * if we're doing the sync list, record that our
296 * plug has some sync requests on it
298 * If we're doing the regular list and there are
299 * sync requests sitting around, unplug before
302 if (pending_bios == &device->pending_sync_bios) {
304 } else if (sync_pending) {
305 blk_finish_plug(&plug);
306 blk_start_plug(&plug);
310 btrfsic_submit_bio(cur->bi_rw, cur);
317 * we made progress, there is more work to do and the bdi
318 * is now congested. Back off and let other work structs
321 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
322 fs_info->fs_devices->open_devices > 1) {
323 struct io_context *ioc;
325 ioc = current->io_context;
328 * the main goal here is that we don't want to
329 * block if we're going to be able to submit
330 * more requests without blocking.
332 * This code does two great things, it pokes into
333 * the elevator code from a filesystem _and_
334 * it makes assumptions about how batching works.
336 if (ioc && ioc->nr_batch_requests > 0 &&
337 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
339 ioc->last_waited == last_waited)) {
341 * we want to go through our batch of
342 * requests and stop. So, we copy out
343 * the ioc->last_waited time and test
344 * against it before looping
346 last_waited = ioc->last_waited;
351 spin_lock(&device->io_lock);
352 requeue_list(pending_bios, pending, tail);
353 device->running_pending = 1;
355 spin_unlock(&device->io_lock);
356 btrfs_requeue_work(&device->work);
359 /* unplug every 64 requests just for good measure */
360 if (batch_run % 64 == 0) {
361 blk_finish_plug(&plug);
362 blk_start_plug(&plug);
371 spin_lock(&device->io_lock);
372 if (device->pending_bios.head || device->pending_sync_bios.head)
374 spin_unlock(&device->io_lock);
377 blk_finish_plug(&plug);
380 static void pending_bios_fn(struct btrfs_work *work)
382 struct btrfs_device *device;
384 device = container_of(work, struct btrfs_device, work);
385 run_scheduled_bios(device);
388 static noinline int device_list_add(const char *path,
389 struct btrfs_super_block *disk_super,
390 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
392 struct btrfs_device *device;
393 struct btrfs_fs_devices *fs_devices;
394 struct rcu_string *name;
395 u64 found_transid = btrfs_super_generation(disk_super);
397 fs_devices = find_fsid(disk_super->fsid);
399 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
402 INIT_LIST_HEAD(&fs_devices->devices);
403 INIT_LIST_HEAD(&fs_devices->alloc_list);
404 list_add(&fs_devices->list, &fs_uuids);
405 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
406 fs_devices->latest_devid = devid;
407 fs_devices->latest_trans = found_transid;
408 mutex_init(&fs_devices->device_list_mutex);
411 device = __find_device(&fs_devices->devices, devid,
412 disk_super->dev_item.uuid);
415 if (fs_devices->opened)
418 device = kzalloc(sizeof(*device), GFP_NOFS);
420 /* we can safely leave the fs_devices entry around */
423 device->devid = devid;
424 device->dev_stats_valid = 0;
425 device->work.func = pending_bios_fn;
426 memcpy(device->uuid, disk_super->dev_item.uuid,
428 spin_lock_init(&device->io_lock);
430 name = rcu_string_strdup(path, GFP_NOFS);
435 rcu_assign_pointer(device->name, name);
436 INIT_LIST_HEAD(&device->dev_alloc_list);
438 /* init readahead state */
439 spin_lock_init(&device->reada_lock);
440 device->reada_curr_zone = NULL;
441 atomic_set(&device->reada_in_flight, 0);
442 device->reada_next = 0;
443 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
444 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
446 mutex_lock(&fs_devices->device_list_mutex);
447 list_add_rcu(&device->dev_list, &fs_devices->devices);
448 mutex_unlock(&fs_devices->device_list_mutex);
450 device->fs_devices = fs_devices;
451 fs_devices->num_devices++;
452 } else if (!device->name || strcmp(device->name->str, path)) {
453 name = rcu_string_strdup(path, GFP_NOFS);
456 rcu_string_free(device->name);
457 rcu_assign_pointer(device->name, name);
458 if (device->missing) {
459 fs_devices->missing_devices--;
464 if (found_transid > fs_devices->latest_trans) {
465 fs_devices->latest_devid = devid;
466 fs_devices->latest_trans = found_transid;
468 *fs_devices_ret = fs_devices;
472 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
474 struct btrfs_fs_devices *fs_devices;
475 struct btrfs_device *device;
476 struct btrfs_device *orig_dev;
478 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
480 return ERR_PTR(-ENOMEM);
482 INIT_LIST_HEAD(&fs_devices->devices);
483 INIT_LIST_HEAD(&fs_devices->alloc_list);
484 INIT_LIST_HEAD(&fs_devices->list);
485 mutex_init(&fs_devices->device_list_mutex);
486 fs_devices->latest_devid = orig->latest_devid;
487 fs_devices->latest_trans = orig->latest_trans;
488 fs_devices->total_devices = orig->total_devices;
489 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
491 /* We have held the volume lock, it is safe to get the devices. */
492 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
493 struct rcu_string *name;
495 device = kzalloc(sizeof(*device), GFP_NOFS);
500 * This is ok to do without rcu read locked because we hold the
501 * uuid mutex so nothing we touch in here is going to disappear.
503 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
508 rcu_assign_pointer(device->name, name);
510 device->devid = orig_dev->devid;
511 device->work.func = pending_bios_fn;
512 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
513 spin_lock_init(&device->io_lock);
514 INIT_LIST_HEAD(&device->dev_list);
515 INIT_LIST_HEAD(&device->dev_alloc_list);
517 list_add(&device->dev_list, &fs_devices->devices);
518 device->fs_devices = fs_devices;
519 fs_devices->num_devices++;
523 free_fs_devices(fs_devices);
524 return ERR_PTR(-ENOMEM);
527 void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
528 struct btrfs_fs_devices *fs_devices, int step)
530 struct btrfs_device *device, *next;
532 struct block_device *latest_bdev = NULL;
533 u64 latest_devid = 0;
534 u64 latest_transid = 0;
536 mutex_lock(&uuid_mutex);
538 /* This is the initialized path, it is safe to release the devices. */
539 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
540 if (device->in_fs_metadata) {
541 if (!device->is_tgtdev_for_dev_replace &&
543 device->generation > latest_transid)) {
544 latest_devid = device->devid;
545 latest_transid = device->generation;
546 latest_bdev = device->bdev;
551 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
553 * In the first step, keep the device which has
554 * the correct fsid and the devid that is used
555 * for the dev_replace procedure.
556 * In the second step, the dev_replace state is
557 * read from the device tree and it is known
558 * whether the procedure is really active or
559 * not, which means whether this device is
560 * used or whether it should be removed.
562 if (step == 0 || device->is_tgtdev_for_dev_replace) {
567 blkdev_put(device->bdev, device->mode);
569 fs_devices->open_devices--;
571 if (device->writeable) {
572 list_del_init(&device->dev_alloc_list);
573 device->writeable = 0;
574 if (!device->is_tgtdev_for_dev_replace)
575 fs_devices->rw_devices--;
577 list_del_init(&device->dev_list);
578 fs_devices->num_devices--;
579 rcu_string_free(device->name);
583 if (fs_devices->seed) {
584 fs_devices = fs_devices->seed;
588 fs_devices->latest_bdev = latest_bdev;
589 fs_devices->latest_devid = latest_devid;
590 fs_devices->latest_trans = latest_transid;
592 mutex_unlock(&uuid_mutex);
595 static void __free_device(struct work_struct *work)
597 struct btrfs_device *device;
599 device = container_of(work, struct btrfs_device, rcu_work);
602 blkdev_put(device->bdev, device->mode);
604 rcu_string_free(device->name);
608 static void free_device(struct rcu_head *head)
610 struct btrfs_device *device;
612 device = container_of(head, struct btrfs_device, rcu);
614 INIT_WORK(&device->rcu_work, __free_device);
615 schedule_work(&device->rcu_work);
618 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
620 struct btrfs_device *device;
622 if (--fs_devices->opened > 0)
625 mutex_lock(&fs_devices->device_list_mutex);
626 list_for_each_entry(device, &fs_devices->devices, dev_list) {
627 struct btrfs_device *new_device;
628 struct rcu_string *name;
631 fs_devices->open_devices--;
633 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
634 list_del_init(&device->dev_alloc_list);
635 fs_devices->rw_devices--;
638 if (device->can_discard)
639 fs_devices->num_can_discard--;
641 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
642 BUG_ON(!new_device); /* -ENOMEM */
643 memcpy(new_device, device, sizeof(*new_device));
645 /* Safe because we are under uuid_mutex */
647 name = rcu_string_strdup(device->name->str, GFP_NOFS);
648 BUG_ON(device->name && !name); /* -ENOMEM */
649 rcu_assign_pointer(new_device->name, name);
651 new_device->bdev = NULL;
652 new_device->writeable = 0;
653 new_device->in_fs_metadata = 0;
654 new_device->can_discard = 0;
655 spin_lock_init(&new_device->io_lock);
656 list_replace_rcu(&device->dev_list, &new_device->dev_list);
658 call_rcu(&device->rcu, free_device);
660 mutex_unlock(&fs_devices->device_list_mutex);
662 WARN_ON(fs_devices->open_devices);
663 WARN_ON(fs_devices->rw_devices);
664 fs_devices->opened = 0;
665 fs_devices->seeding = 0;
670 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
672 struct btrfs_fs_devices *seed_devices = NULL;
675 mutex_lock(&uuid_mutex);
676 ret = __btrfs_close_devices(fs_devices);
677 if (!fs_devices->opened) {
678 seed_devices = fs_devices->seed;
679 fs_devices->seed = NULL;
681 mutex_unlock(&uuid_mutex);
683 while (seed_devices) {
684 fs_devices = seed_devices;
685 seed_devices = fs_devices->seed;
686 __btrfs_close_devices(fs_devices);
687 free_fs_devices(fs_devices);
690 * Wait for rcu kworkers under __btrfs_close_devices
691 * to finish all blkdev_puts so device is really
692 * free when umount is done.
698 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
699 fmode_t flags, void *holder)
701 struct request_queue *q;
702 struct block_device *bdev;
703 struct list_head *head = &fs_devices->devices;
704 struct btrfs_device *device;
705 struct block_device *latest_bdev = NULL;
706 struct buffer_head *bh;
707 struct btrfs_super_block *disk_super;
708 u64 latest_devid = 0;
709 u64 latest_transid = 0;
716 list_for_each_entry(device, head, dev_list) {
722 /* Just open everything we can; ignore failures here */
723 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
727 disk_super = (struct btrfs_super_block *)bh->b_data;
728 devid = btrfs_stack_device_id(&disk_super->dev_item);
729 if (devid != device->devid)
732 if (memcmp(device->uuid, disk_super->dev_item.uuid,
736 device->generation = btrfs_super_generation(disk_super);
737 if (!latest_transid || device->generation > latest_transid) {
738 latest_devid = devid;
739 latest_transid = device->generation;
743 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
744 device->writeable = 0;
746 device->writeable = !bdev_read_only(bdev);
750 q = bdev_get_queue(bdev);
751 if (blk_queue_discard(q)) {
752 device->can_discard = 1;
753 fs_devices->num_can_discard++;
757 device->in_fs_metadata = 0;
758 device->mode = flags;
760 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
761 fs_devices->rotating = 1;
763 fs_devices->open_devices++;
764 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
765 fs_devices->rw_devices++;
766 list_add(&device->dev_alloc_list,
767 &fs_devices->alloc_list);
774 blkdev_put(bdev, flags);
777 if (fs_devices->open_devices == 0) {
781 fs_devices->seeding = seeding;
782 fs_devices->opened = 1;
783 fs_devices->latest_bdev = latest_bdev;
784 fs_devices->latest_devid = latest_devid;
785 fs_devices->latest_trans = latest_transid;
786 fs_devices->total_rw_bytes = 0;
791 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
792 fmode_t flags, void *holder)
796 mutex_lock(&uuid_mutex);
797 if (fs_devices->opened) {
798 fs_devices->opened++;
801 ret = __btrfs_open_devices(fs_devices, flags, holder);
803 mutex_unlock(&uuid_mutex);
808 * Look for a btrfs signature on a device. This may be called out of the mount path
809 * and we are not allowed to call set_blocksize during the scan. The superblock
810 * is read via pagecache
812 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
813 struct btrfs_fs_devices **fs_devices_ret)
815 struct btrfs_super_block *disk_super;
816 struct block_device *bdev;
827 * we would like to check all the supers, but that would make
828 * a btrfs mount succeed after a mkfs from a different FS.
829 * So, we need to add a special mount option to scan for
830 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
832 bytenr = btrfs_sb_offset(0);
834 mutex_lock(&uuid_mutex);
836 bdev = blkdev_get_by_path(path, flags, holder);
843 /* make sure our super fits in the device */
844 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
847 /* make sure our super fits in the page */
848 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
851 /* make sure our super doesn't straddle pages on disk */
852 index = bytenr >> PAGE_CACHE_SHIFT;
853 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
856 /* pull in the page with our super */
857 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
860 if (IS_ERR_OR_NULL(page))
865 /* align our pointer to the offset of the super block */
866 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
868 if (btrfs_super_bytenr(disk_super) != bytenr ||
869 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
872 devid = btrfs_stack_device_id(&disk_super->dev_item);
873 transid = btrfs_super_generation(disk_super);
874 total_devices = btrfs_super_num_devices(disk_super);
876 if (disk_super->label[0]) {
877 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
878 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
879 printk(KERN_INFO "device label %s ", disk_super->label);
881 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
884 printk(KERN_CONT "devid %llu transid %llu %s\n",
885 (unsigned long long)devid, (unsigned long long)transid, path);
887 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
888 if (!ret && fs_devices_ret)
889 (*fs_devices_ret)->total_devices = total_devices;
893 page_cache_release(page);
896 blkdev_put(bdev, flags);
898 mutex_unlock(&uuid_mutex);
902 /* helper to account the used device space in the range */
903 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
904 u64 end, u64 *length)
906 struct btrfs_key key;
907 struct btrfs_root *root = device->dev_root;
908 struct btrfs_dev_extent *dev_extent;
909 struct btrfs_path *path;
913 struct extent_buffer *l;
917 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
920 path = btrfs_alloc_path();
925 key.objectid = device->devid;
927 key.type = BTRFS_DEV_EXTENT_KEY;
929 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
933 ret = btrfs_previous_item(root, path, key.objectid, key.type);
940 slot = path->slots[0];
941 if (slot >= btrfs_header_nritems(l)) {
942 ret = btrfs_next_leaf(root, path);
950 btrfs_item_key_to_cpu(l, &key, slot);
952 if (key.objectid < device->devid)
955 if (key.objectid > device->devid)
958 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
961 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
962 extent_end = key.offset + btrfs_dev_extent_length(l,
964 if (key.offset <= start && extent_end > end) {
965 *length = end - start + 1;
967 } else if (key.offset <= start && extent_end > start)
968 *length += extent_end - start;
969 else if (key.offset > start && extent_end <= end)
970 *length += extent_end - key.offset;
971 else if (key.offset > start && key.offset <= end) {
972 *length += end - key.offset + 1;
974 } else if (key.offset > end)
982 btrfs_free_path(path);
986 static int contains_pending_extent(struct btrfs_trans_handle *trans,
987 struct btrfs_device *device,
990 struct extent_map *em;
993 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
994 struct map_lookup *map;
997 map = (struct map_lookup *)em->bdev;
998 for (i = 0; i < map->num_stripes; i++) {
999 if (map->stripes[i].dev != device)
1001 if (map->stripes[i].physical >= *start + len ||
1002 map->stripes[i].physical + em->orig_block_len <=
1005 *start = map->stripes[i].physical +
1016 * find_free_dev_extent - find free space in the specified device
1017 * @device: the device which we search the free space in
1018 * @num_bytes: the size of the free space that we need
1019 * @start: store the start of the free space.
1020 * @len: the size of the free space. that we find, or the size of the max
1021 * free space if we don't find suitable free space
1023 * this uses a pretty simple search, the expectation is that it is
1024 * called very infrequently and that a given device has a small number
1027 * @start is used to store the start of the free space if we find. But if we
1028 * don't find suitable free space, it will be used to store the start position
1029 * of the max free space.
1031 * @len is used to store the size of the free space that we find.
1032 * But if we don't find suitable free space, it is used to store the size of
1033 * the max free space.
1035 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1036 struct btrfs_device *device, u64 num_bytes,
1037 u64 *start, u64 *len)
1039 struct btrfs_key key;
1040 struct btrfs_root *root = device->dev_root;
1041 struct btrfs_dev_extent *dev_extent;
1042 struct btrfs_path *path;
1048 u64 search_end = device->total_bytes;
1051 struct extent_buffer *l;
1053 /* FIXME use last free of some kind */
1055 /* we don't want to overwrite the superblock on the drive,
1056 * so we make sure to start at an offset of at least 1MB
1058 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1060 path = btrfs_alloc_path();
1064 max_hole_start = search_start;
1068 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1074 path->search_commit_root = 1;
1075 path->skip_locking = 1;
1077 key.objectid = device->devid;
1078 key.offset = search_start;
1079 key.type = BTRFS_DEV_EXTENT_KEY;
1081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1085 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1092 slot = path->slots[0];
1093 if (slot >= btrfs_header_nritems(l)) {
1094 ret = btrfs_next_leaf(root, path);
1102 btrfs_item_key_to_cpu(l, &key, slot);
1104 if (key.objectid < device->devid)
1107 if (key.objectid > device->devid)
1110 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1113 if (key.offset > search_start) {
1114 hole_size = key.offset - search_start;
1117 * Have to check before we set max_hole_start, otherwise
1118 * we could end up sending back this offset anyway.
1120 if (contains_pending_extent(trans, device,
1125 if (hole_size > max_hole_size) {
1126 max_hole_start = search_start;
1127 max_hole_size = hole_size;
1131 * If this free space is greater than which we need,
1132 * it must be the max free space that we have found
1133 * until now, so max_hole_start must point to the start
1134 * of this free space and the length of this free space
1135 * is stored in max_hole_size. Thus, we return
1136 * max_hole_start and max_hole_size and go back to the
1139 if (hole_size >= num_bytes) {
1145 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1146 extent_end = key.offset + btrfs_dev_extent_length(l,
1148 if (extent_end > search_start)
1149 search_start = extent_end;
1156 * At this point, search_start should be the end of
1157 * allocated dev extents, and when shrinking the device,
1158 * search_end may be smaller than search_start.
1160 if (search_end > search_start)
1161 hole_size = search_end - search_start;
1163 if (hole_size > max_hole_size) {
1164 max_hole_start = search_start;
1165 max_hole_size = hole_size;
1168 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1169 btrfs_release_path(path);
1174 if (hole_size < num_bytes)
1180 btrfs_free_path(path);
1181 *start = max_hole_start;
1183 *len = max_hole_size;
1187 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1188 struct btrfs_device *device,
1192 struct btrfs_path *path;
1193 struct btrfs_root *root = device->dev_root;
1194 struct btrfs_key key;
1195 struct btrfs_key found_key;
1196 struct extent_buffer *leaf = NULL;
1197 struct btrfs_dev_extent *extent = NULL;
1199 path = btrfs_alloc_path();
1203 key.objectid = device->devid;
1205 key.type = BTRFS_DEV_EXTENT_KEY;
1207 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1209 ret = btrfs_previous_item(root, path, key.objectid,
1210 BTRFS_DEV_EXTENT_KEY);
1213 leaf = path->nodes[0];
1214 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1215 extent = btrfs_item_ptr(leaf, path->slots[0],
1216 struct btrfs_dev_extent);
1217 BUG_ON(found_key.offset > start || found_key.offset +
1218 btrfs_dev_extent_length(leaf, extent) < start);
1220 btrfs_release_path(path);
1222 } else if (ret == 0) {
1223 leaf = path->nodes[0];
1224 extent = btrfs_item_ptr(leaf, path->slots[0],
1225 struct btrfs_dev_extent);
1227 btrfs_error(root->fs_info, ret, "Slot search failed");
1231 if (device->bytes_used > 0) {
1232 u64 len = btrfs_dev_extent_length(leaf, extent);
1233 device->bytes_used -= len;
1234 spin_lock(&root->fs_info->free_chunk_lock);
1235 root->fs_info->free_chunk_space += len;
1236 spin_unlock(&root->fs_info->free_chunk_lock);
1238 ret = btrfs_del_item(trans, root, path);
1240 btrfs_error(root->fs_info, ret,
1241 "Failed to remove dev extent item");
1244 btrfs_free_path(path);
1248 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1249 struct btrfs_device *device,
1250 u64 chunk_tree, u64 chunk_objectid,
1251 u64 chunk_offset, u64 start, u64 num_bytes)
1254 struct btrfs_path *path;
1255 struct btrfs_root *root = device->dev_root;
1256 struct btrfs_dev_extent *extent;
1257 struct extent_buffer *leaf;
1258 struct btrfs_key key;
1260 WARN_ON(!device->in_fs_metadata);
1261 WARN_ON(device->is_tgtdev_for_dev_replace);
1262 path = btrfs_alloc_path();
1266 key.objectid = device->devid;
1268 key.type = BTRFS_DEV_EXTENT_KEY;
1269 ret = btrfs_insert_empty_item(trans, root, path, &key,
1274 leaf = path->nodes[0];
1275 extent = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_dev_extent);
1277 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1278 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1279 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1281 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1282 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1285 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1286 btrfs_mark_buffer_dirty(leaf);
1288 btrfs_free_path(path);
1292 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1294 struct extent_map_tree *em_tree;
1295 struct extent_map *em;
1299 em_tree = &fs_info->mapping_tree.map_tree;
1300 read_lock(&em_tree->lock);
1301 n = rb_last(&em_tree->map);
1303 em = rb_entry(n, struct extent_map, rb_node);
1304 ret = em->start + em->len;
1306 read_unlock(&em_tree->lock);
1311 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1314 struct btrfs_key key;
1315 struct btrfs_key found_key;
1316 struct btrfs_path *path;
1318 root = root->fs_info->chunk_root;
1320 path = btrfs_alloc_path();
1324 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1325 key.type = BTRFS_DEV_ITEM_KEY;
1326 key.offset = (u64)-1;
1328 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1332 BUG_ON(ret == 0); /* Corruption */
1334 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1335 BTRFS_DEV_ITEM_KEY);
1339 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1341 *objectid = found_key.offset + 1;
1345 btrfs_free_path(path);
1350 * the device information is stored in the chunk root
1351 * the btrfs_device struct should be fully filled in
1353 static int btrfs_add_device(struct btrfs_trans_handle *trans,
1354 struct btrfs_root *root,
1355 struct btrfs_device *device)
1358 struct btrfs_path *path;
1359 struct btrfs_dev_item *dev_item;
1360 struct extent_buffer *leaf;
1361 struct btrfs_key key;
1364 root = root->fs_info->chunk_root;
1366 path = btrfs_alloc_path();
1370 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1371 key.type = BTRFS_DEV_ITEM_KEY;
1372 key.offset = device->devid;
1374 ret = btrfs_insert_empty_item(trans, root, path, &key,
1379 leaf = path->nodes[0];
1380 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1382 btrfs_set_device_id(leaf, dev_item, device->devid);
1383 btrfs_set_device_generation(leaf, dev_item, 0);
1384 btrfs_set_device_type(leaf, dev_item, device->type);
1385 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1386 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1387 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1388 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1389 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1390 btrfs_set_device_group(leaf, dev_item, 0);
1391 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1392 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1393 btrfs_set_device_start_offset(leaf, dev_item, 0);
1395 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1396 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1397 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1398 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1399 btrfs_mark_buffer_dirty(leaf);
1403 btrfs_free_path(path);
1407 static int btrfs_rm_dev_item(struct btrfs_root *root,
1408 struct btrfs_device *device)
1411 struct btrfs_path *path;
1412 struct btrfs_key key;
1413 struct btrfs_trans_handle *trans;
1415 root = root->fs_info->chunk_root;
1417 path = btrfs_alloc_path();
1421 trans = btrfs_start_transaction(root, 0);
1422 if (IS_ERR(trans)) {
1423 btrfs_free_path(path);
1424 return PTR_ERR(trans);
1426 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1427 key.type = BTRFS_DEV_ITEM_KEY;
1428 key.offset = device->devid;
1431 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1440 ret = btrfs_del_item(trans, root, path);
1444 btrfs_free_path(path);
1445 unlock_chunks(root);
1446 btrfs_commit_transaction(trans, root);
1450 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1452 struct btrfs_device *device;
1453 struct btrfs_device *next_device;
1454 struct block_device *bdev;
1455 struct buffer_head *bh = NULL;
1456 struct btrfs_super_block *disk_super;
1457 struct btrfs_fs_devices *cur_devices;
1464 bool clear_super = false;
1466 mutex_lock(&uuid_mutex);
1469 seq = read_seqbegin(&root->fs_info->profiles_lock);
1471 all_avail = root->fs_info->avail_data_alloc_bits |
1472 root->fs_info->avail_system_alloc_bits |
1473 root->fs_info->avail_metadata_alloc_bits;
1474 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1476 num_devices = root->fs_info->fs_devices->num_devices;
1477 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1478 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1479 WARN_ON(num_devices < 1);
1482 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1484 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1485 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
1489 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1490 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
1494 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1495 root->fs_info->fs_devices->rw_devices <= 2) {
1496 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
1499 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1500 root->fs_info->fs_devices->rw_devices <= 3) {
1501 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
1505 if (strcmp(device_path, "missing") == 0) {
1506 struct list_head *devices;
1507 struct btrfs_device *tmp;
1510 devices = &root->fs_info->fs_devices->devices;
1512 * It is safe to read the devices since the volume_mutex
1515 list_for_each_entry(tmp, devices, dev_list) {
1516 if (tmp->in_fs_metadata &&
1517 !tmp->is_tgtdev_for_dev_replace &&
1527 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1531 ret = btrfs_get_bdev_and_sb(device_path,
1532 FMODE_WRITE | FMODE_EXCL,
1533 root->fs_info->bdev_holder, 0,
1537 disk_super = (struct btrfs_super_block *)bh->b_data;
1538 devid = btrfs_stack_device_id(&disk_super->dev_item);
1539 dev_uuid = disk_super->dev_item.uuid;
1540 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1548 if (device->is_tgtdev_for_dev_replace) {
1549 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1553 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1554 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1558 if (device->writeable) {
1560 list_del_init(&device->dev_alloc_list);
1561 unlock_chunks(root);
1562 root->fs_info->fs_devices->rw_devices--;
1566 mutex_unlock(&uuid_mutex);
1567 ret = btrfs_shrink_device(device, 0);
1568 mutex_lock(&uuid_mutex);
1573 * TODO: the superblock still includes this device in its num_devices
1574 * counter although write_all_supers() is not locked out. This
1575 * could give a filesystem state which requires a degraded mount.
1577 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1581 spin_lock(&root->fs_info->free_chunk_lock);
1582 root->fs_info->free_chunk_space = device->total_bytes -
1584 spin_unlock(&root->fs_info->free_chunk_lock);
1586 device->in_fs_metadata = 0;
1587 btrfs_scrub_cancel_dev(root->fs_info, device);
1590 * the device list mutex makes sure that we don't change
1591 * the device list while someone else is writing out all
1592 * the device supers.
1595 cur_devices = device->fs_devices;
1596 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1597 list_del_rcu(&device->dev_list);
1599 device->fs_devices->num_devices--;
1600 device->fs_devices->total_devices--;
1602 if (device->missing)
1603 root->fs_info->fs_devices->missing_devices--;
1605 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1606 struct btrfs_device, dev_list);
1607 if (device->bdev == root->fs_info->sb->s_bdev)
1608 root->fs_info->sb->s_bdev = next_device->bdev;
1609 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1610 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1613 device->fs_devices->open_devices--;
1615 call_rcu(&device->rcu, free_device);
1616 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1618 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1619 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1621 if (cur_devices->open_devices == 0) {
1622 struct btrfs_fs_devices *fs_devices;
1623 fs_devices = root->fs_info->fs_devices;
1624 while (fs_devices) {
1625 if (fs_devices->seed == cur_devices)
1627 fs_devices = fs_devices->seed;
1629 fs_devices->seed = cur_devices->seed;
1630 cur_devices->seed = NULL;
1632 __btrfs_close_devices(cur_devices);
1633 unlock_chunks(root);
1634 free_fs_devices(cur_devices);
1637 root->fs_info->num_tolerated_disk_barrier_failures =
1638 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1641 * at this point, the device is zero sized. We want to
1642 * remove it from the devices list and zero out the old super
1644 if (clear_super && disk_super) {
1645 /* make sure this device isn't detected as part of
1648 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1649 set_buffer_dirty(bh);
1650 sync_dirty_buffer(bh);
1655 /* Notify udev that device has changed */
1657 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1662 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1664 mutex_unlock(&uuid_mutex);
1667 if (device->writeable) {
1669 list_add(&device->dev_alloc_list,
1670 &root->fs_info->fs_devices->alloc_list);
1671 unlock_chunks(root);
1672 root->fs_info->fs_devices->rw_devices++;
1677 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1678 struct btrfs_device *srcdev)
1680 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1681 list_del_rcu(&srcdev->dev_list);
1682 list_del_rcu(&srcdev->dev_alloc_list);
1683 fs_info->fs_devices->num_devices--;
1684 if (srcdev->missing) {
1685 fs_info->fs_devices->missing_devices--;
1686 fs_info->fs_devices->rw_devices++;
1688 if (srcdev->can_discard)
1689 fs_info->fs_devices->num_can_discard--;
1691 fs_info->fs_devices->open_devices--;
1693 call_rcu(&srcdev->rcu, free_device);
1696 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1697 struct btrfs_device *tgtdev)
1699 struct btrfs_device *next_device;
1702 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1704 btrfs_scratch_superblock(tgtdev);
1705 fs_info->fs_devices->open_devices--;
1707 fs_info->fs_devices->num_devices--;
1708 if (tgtdev->can_discard)
1709 fs_info->fs_devices->num_can_discard++;
1711 next_device = list_entry(fs_info->fs_devices->devices.next,
1712 struct btrfs_device, dev_list);
1713 if (tgtdev->bdev == fs_info->sb->s_bdev)
1714 fs_info->sb->s_bdev = next_device->bdev;
1715 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1716 fs_info->fs_devices->latest_bdev = next_device->bdev;
1717 list_del_rcu(&tgtdev->dev_list);
1719 call_rcu(&tgtdev->rcu, free_device);
1721 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1724 static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1725 struct btrfs_device **device)
1728 struct btrfs_super_block *disk_super;
1731 struct block_device *bdev;
1732 struct buffer_head *bh;
1735 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1736 root->fs_info->bdev_holder, 0, &bdev, &bh);
1739 disk_super = (struct btrfs_super_block *)bh->b_data;
1740 devid = btrfs_stack_device_id(&disk_super->dev_item);
1741 dev_uuid = disk_super->dev_item.uuid;
1742 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1747 blkdev_put(bdev, FMODE_READ);
1751 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1753 struct btrfs_device **device)
1756 if (strcmp(device_path, "missing") == 0) {
1757 struct list_head *devices;
1758 struct btrfs_device *tmp;
1760 devices = &root->fs_info->fs_devices->devices;
1762 * It is safe to read the devices since the volume_mutex
1763 * is held by the caller.
1765 list_for_each_entry(tmp, devices, dev_list) {
1766 if (tmp->in_fs_metadata && !tmp->bdev) {
1773 pr_err("btrfs: no missing device found\n");
1779 return btrfs_find_device_by_path(root, device_path, device);
1784 * does all the dirty work required for changing file system's UUID.
1786 static int btrfs_prepare_sprout(struct btrfs_root *root)
1788 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1789 struct btrfs_fs_devices *old_devices;
1790 struct btrfs_fs_devices *seed_devices;
1791 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1792 struct btrfs_device *device;
1795 BUG_ON(!mutex_is_locked(&uuid_mutex));
1796 if (!fs_devices->seeding)
1799 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1803 old_devices = clone_fs_devices(fs_devices);
1804 if (IS_ERR(old_devices)) {
1805 kfree(seed_devices);
1806 return PTR_ERR(old_devices);
1809 list_add(&old_devices->list, &fs_uuids);
1811 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1812 seed_devices->opened = 1;
1813 INIT_LIST_HEAD(&seed_devices->devices);
1814 INIT_LIST_HEAD(&seed_devices->alloc_list);
1815 mutex_init(&seed_devices->device_list_mutex);
1817 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1818 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1820 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1822 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1823 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1824 device->fs_devices = seed_devices;
1827 fs_devices->seeding = 0;
1828 fs_devices->num_devices = 0;
1829 fs_devices->open_devices = 0;
1830 fs_devices->total_devices = 0;
1831 fs_devices->seed = seed_devices;
1833 generate_random_uuid(fs_devices->fsid);
1834 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1835 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1836 super_flags = btrfs_super_flags(disk_super) &
1837 ~BTRFS_SUPER_FLAG_SEEDING;
1838 btrfs_set_super_flags(disk_super, super_flags);
1844 * strore the expected generation for seed devices in device items.
1846 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1847 struct btrfs_root *root)
1849 struct btrfs_path *path;
1850 struct extent_buffer *leaf;
1851 struct btrfs_dev_item *dev_item;
1852 struct btrfs_device *device;
1853 struct btrfs_key key;
1854 u8 fs_uuid[BTRFS_UUID_SIZE];
1855 u8 dev_uuid[BTRFS_UUID_SIZE];
1859 path = btrfs_alloc_path();
1863 root = root->fs_info->chunk_root;
1864 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1866 key.type = BTRFS_DEV_ITEM_KEY;
1869 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1873 leaf = path->nodes[0];
1875 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1876 ret = btrfs_next_leaf(root, path);
1881 leaf = path->nodes[0];
1882 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1883 btrfs_release_path(path);
1887 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1888 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1889 key.type != BTRFS_DEV_ITEM_KEY)
1892 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1893 struct btrfs_dev_item);
1894 devid = btrfs_device_id(leaf, dev_item);
1895 read_extent_buffer(leaf, dev_uuid,
1896 (unsigned long)btrfs_device_uuid(dev_item),
1898 read_extent_buffer(leaf, fs_uuid,
1899 (unsigned long)btrfs_device_fsid(dev_item),
1901 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1903 BUG_ON(!device); /* Logic error */
1905 if (device->fs_devices->seeding) {
1906 btrfs_set_device_generation(leaf, dev_item,
1907 device->generation);
1908 btrfs_mark_buffer_dirty(leaf);
1916 btrfs_free_path(path);
1920 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1922 struct request_queue *q;
1923 struct btrfs_trans_handle *trans;
1924 struct btrfs_device *device;
1925 struct block_device *bdev;
1926 struct list_head *devices;
1927 struct super_block *sb = root->fs_info->sb;
1928 struct rcu_string *name;
1930 int seeding_dev = 0;
1933 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1936 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1937 root->fs_info->bdev_holder);
1939 return PTR_ERR(bdev);
1941 if (root->fs_info->fs_devices->seeding) {
1943 down_write(&sb->s_umount);
1944 mutex_lock(&uuid_mutex);
1947 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1949 devices = &root->fs_info->fs_devices->devices;
1951 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1952 list_for_each_entry(device, devices, dev_list) {
1953 if (device->bdev == bdev) {
1956 &root->fs_info->fs_devices->device_list_mutex);
1960 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1962 device = kzalloc(sizeof(*device), GFP_NOFS);
1964 /* we can safely leave the fs_devices entry around */
1969 name = rcu_string_strdup(device_path, GFP_NOFS);
1975 rcu_assign_pointer(device->name, name);
1977 ret = find_next_devid(root, &device->devid);
1979 rcu_string_free(device->name);
1984 trans = btrfs_start_transaction(root, 0);
1985 if (IS_ERR(trans)) {
1986 rcu_string_free(device->name);
1988 ret = PTR_ERR(trans);
1994 q = bdev_get_queue(bdev);
1995 if (blk_queue_discard(q))
1996 device->can_discard = 1;
1997 device->writeable = 1;
1998 device->work.func = pending_bios_fn;
1999 generate_random_uuid(device->uuid);
2000 spin_lock_init(&device->io_lock);
2001 device->generation = trans->transid;
2002 device->io_width = root->sectorsize;
2003 device->io_align = root->sectorsize;
2004 device->sector_size = root->sectorsize;
2005 device->total_bytes = i_size_read(bdev->bd_inode);
2006 device->disk_total_bytes = device->total_bytes;
2007 device->dev_root = root->fs_info->dev_root;
2008 device->bdev = bdev;
2009 device->in_fs_metadata = 1;
2010 device->is_tgtdev_for_dev_replace = 0;
2011 device->mode = FMODE_EXCL;
2012 set_blocksize(device->bdev, 4096);
2015 sb->s_flags &= ~MS_RDONLY;
2016 ret = btrfs_prepare_sprout(root);
2017 BUG_ON(ret); /* -ENOMEM */
2020 device->fs_devices = root->fs_info->fs_devices;
2022 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2023 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2024 list_add(&device->dev_alloc_list,
2025 &root->fs_info->fs_devices->alloc_list);
2026 root->fs_info->fs_devices->num_devices++;
2027 root->fs_info->fs_devices->open_devices++;
2028 root->fs_info->fs_devices->rw_devices++;
2029 root->fs_info->fs_devices->total_devices++;
2030 if (device->can_discard)
2031 root->fs_info->fs_devices->num_can_discard++;
2032 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2034 spin_lock(&root->fs_info->free_chunk_lock);
2035 root->fs_info->free_chunk_space += device->total_bytes;
2036 spin_unlock(&root->fs_info->free_chunk_lock);
2038 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2039 root->fs_info->fs_devices->rotating = 1;
2041 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2042 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2043 total_bytes + device->total_bytes);
2045 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2046 btrfs_set_super_num_devices(root->fs_info->super_copy,
2048 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2051 ret = init_first_rw_device(trans, root, device);
2053 btrfs_abort_transaction(trans, root, ret);
2056 ret = btrfs_finish_sprout(trans, root);
2058 btrfs_abort_transaction(trans, root, ret);
2062 ret = btrfs_add_device(trans, root, device);
2064 btrfs_abort_transaction(trans, root, ret);
2070 * we've got more storage, clear any full flags on the space
2073 btrfs_clear_space_info_full(root->fs_info);
2075 unlock_chunks(root);
2076 root->fs_info->num_tolerated_disk_barrier_failures =
2077 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2078 ret = btrfs_commit_transaction(trans, root);
2081 mutex_unlock(&uuid_mutex);
2082 up_write(&sb->s_umount);
2084 if (ret) /* transaction commit */
2087 ret = btrfs_relocate_sys_chunks(root);
2089 btrfs_error(root->fs_info, ret,
2090 "Failed to relocate sys chunks after "
2091 "device initialization. This can be fixed "
2092 "using the \"btrfs balance\" command.");
2093 trans = btrfs_attach_transaction(root);
2094 if (IS_ERR(trans)) {
2095 if (PTR_ERR(trans) == -ENOENT)
2097 return PTR_ERR(trans);
2099 ret = btrfs_commit_transaction(trans, root);
2105 unlock_chunks(root);
2106 btrfs_end_transaction(trans, root);
2107 rcu_string_free(device->name);
2110 blkdev_put(bdev, FMODE_EXCL);
2112 mutex_unlock(&uuid_mutex);
2113 up_write(&sb->s_umount);
2118 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2119 struct btrfs_device **device_out)
2121 struct request_queue *q;
2122 struct btrfs_device *device;
2123 struct block_device *bdev;
2124 struct btrfs_fs_info *fs_info = root->fs_info;
2125 struct list_head *devices;
2126 struct rcu_string *name;
2130 if (fs_info->fs_devices->seeding)
2133 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2134 fs_info->bdev_holder);
2136 return PTR_ERR(bdev);
2138 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2140 devices = &fs_info->fs_devices->devices;
2141 list_for_each_entry(device, devices, dev_list) {
2142 if (device->bdev == bdev) {
2148 device = kzalloc(sizeof(*device), GFP_NOFS);
2154 name = rcu_string_strdup(device_path, GFP_NOFS);
2160 rcu_assign_pointer(device->name, name);
2162 q = bdev_get_queue(bdev);
2163 if (blk_queue_discard(q))
2164 device->can_discard = 1;
2165 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2166 device->writeable = 1;
2167 device->work.func = pending_bios_fn;
2168 generate_random_uuid(device->uuid);
2169 device->devid = BTRFS_DEV_REPLACE_DEVID;
2170 spin_lock_init(&device->io_lock);
2171 device->generation = 0;
2172 device->io_width = root->sectorsize;
2173 device->io_align = root->sectorsize;
2174 device->sector_size = root->sectorsize;
2175 device->total_bytes = i_size_read(bdev->bd_inode);
2176 device->disk_total_bytes = device->total_bytes;
2177 device->dev_root = fs_info->dev_root;
2178 device->bdev = bdev;
2179 device->in_fs_metadata = 1;
2180 device->is_tgtdev_for_dev_replace = 1;
2181 device->mode = FMODE_EXCL;
2182 set_blocksize(device->bdev, 4096);
2183 device->fs_devices = fs_info->fs_devices;
2184 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2185 fs_info->fs_devices->num_devices++;
2186 fs_info->fs_devices->open_devices++;
2187 if (device->can_discard)
2188 fs_info->fs_devices->num_can_discard++;
2189 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2191 *device_out = device;
2195 blkdev_put(bdev, FMODE_EXCL);
2199 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2200 struct btrfs_device *tgtdev)
2202 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2203 tgtdev->io_width = fs_info->dev_root->sectorsize;
2204 tgtdev->io_align = fs_info->dev_root->sectorsize;
2205 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2206 tgtdev->dev_root = fs_info->dev_root;
2207 tgtdev->in_fs_metadata = 1;
2210 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2211 struct btrfs_device *device)
2214 struct btrfs_path *path;
2215 struct btrfs_root *root;
2216 struct btrfs_dev_item *dev_item;
2217 struct extent_buffer *leaf;
2218 struct btrfs_key key;
2220 root = device->dev_root->fs_info->chunk_root;
2222 path = btrfs_alloc_path();
2226 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2227 key.type = BTRFS_DEV_ITEM_KEY;
2228 key.offset = device->devid;
2230 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2239 leaf = path->nodes[0];
2240 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2242 btrfs_set_device_id(leaf, dev_item, device->devid);
2243 btrfs_set_device_type(leaf, dev_item, device->type);
2244 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2245 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2246 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2247 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
2248 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2249 btrfs_mark_buffer_dirty(leaf);
2252 btrfs_free_path(path);
2256 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
2257 struct btrfs_device *device, u64 new_size)
2259 struct btrfs_super_block *super_copy =
2260 device->dev_root->fs_info->super_copy;
2261 u64 old_total = btrfs_super_total_bytes(super_copy);
2262 u64 diff = new_size - device->total_bytes;
2264 if (!device->writeable)
2266 if (new_size <= device->total_bytes ||
2267 device->is_tgtdev_for_dev_replace)
2270 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2271 device->fs_devices->total_rw_bytes += diff;
2273 device->total_bytes = new_size;
2274 device->disk_total_bytes = new_size;
2275 btrfs_clear_space_info_full(device->dev_root->fs_info);
2277 return btrfs_update_device(trans, device);
2280 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2281 struct btrfs_device *device, u64 new_size)
2284 lock_chunks(device->dev_root);
2285 ret = __btrfs_grow_device(trans, device, new_size);
2286 unlock_chunks(device->dev_root);
2290 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2291 struct btrfs_root *root,
2292 u64 chunk_tree, u64 chunk_objectid,
2296 struct btrfs_path *path;
2297 struct btrfs_key key;
2299 root = root->fs_info->chunk_root;
2300 path = btrfs_alloc_path();
2304 key.objectid = chunk_objectid;
2305 key.offset = chunk_offset;
2306 key.type = BTRFS_CHUNK_ITEM_KEY;
2308 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2311 else if (ret > 0) { /* Logic error or corruption */
2312 btrfs_error(root->fs_info, -ENOENT,
2313 "Failed lookup while freeing chunk.");
2318 ret = btrfs_del_item(trans, root, path);
2320 btrfs_error(root->fs_info, ret,
2321 "Failed to delete chunk item.");
2323 btrfs_free_path(path);
2327 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2330 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2331 struct btrfs_disk_key *disk_key;
2332 struct btrfs_chunk *chunk;
2339 struct btrfs_key key;
2341 array_size = btrfs_super_sys_array_size(super_copy);
2343 ptr = super_copy->sys_chunk_array;
2346 while (cur < array_size) {
2347 disk_key = (struct btrfs_disk_key *)ptr;
2348 btrfs_disk_key_to_cpu(&key, disk_key);
2350 len = sizeof(*disk_key);
2352 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2353 chunk = (struct btrfs_chunk *)(ptr + len);
2354 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2355 len += btrfs_chunk_item_size(num_stripes);
2360 if (key.objectid == chunk_objectid &&
2361 key.offset == chunk_offset) {
2362 memmove(ptr, ptr + len, array_size - (cur + len));
2364 btrfs_set_super_sys_array_size(super_copy, array_size);
2373 static int btrfs_relocate_chunk(struct btrfs_root *root,
2374 u64 chunk_tree, u64 chunk_objectid,
2377 struct extent_map_tree *em_tree;
2378 struct btrfs_root *extent_root;
2379 struct btrfs_trans_handle *trans;
2380 struct extent_map *em;
2381 struct map_lookup *map;
2385 root = root->fs_info->chunk_root;
2386 extent_root = root->fs_info->extent_root;
2387 em_tree = &root->fs_info->mapping_tree.map_tree;
2389 ret = btrfs_can_relocate(extent_root, chunk_offset);
2393 /* step one, relocate all the extents inside this chunk */
2394 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2398 trans = btrfs_start_transaction(root, 0);
2399 if (IS_ERR(trans)) {
2400 ret = PTR_ERR(trans);
2401 btrfs_std_error(root->fs_info, ret);
2408 * step two, delete the device extents and the
2409 * chunk tree entries
2411 read_lock(&em_tree->lock);
2412 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2413 read_unlock(&em_tree->lock);
2415 BUG_ON(!em || em->start > chunk_offset ||
2416 em->start + em->len < chunk_offset);
2417 map = (struct map_lookup *)em->bdev;
2419 for (i = 0; i < map->num_stripes; i++) {
2420 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2421 map->stripes[i].physical);
2424 if (map->stripes[i].dev) {
2425 ret = btrfs_update_device(trans, map->stripes[i].dev);
2429 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2434 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2436 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2437 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2441 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2444 write_lock(&em_tree->lock);
2445 remove_extent_mapping(em_tree, em);
2446 write_unlock(&em_tree->lock);
2451 /* once for the tree */
2452 free_extent_map(em);
2454 free_extent_map(em);
2456 unlock_chunks(root);
2457 btrfs_end_transaction(trans, root);
2461 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2463 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2464 struct btrfs_path *path;
2465 struct extent_buffer *leaf;
2466 struct btrfs_chunk *chunk;
2467 struct btrfs_key key;
2468 struct btrfs_key found_key;
2469 u64 chunk_tree = chunk_root->root_key.objectid;
2471 bool retried = false;
2475 path = btrfs_alloc_path();
2480 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2481 key.offset = (u64)-1;
2482 key.type = BTRFS_CHUNK_ITEM_KEY;
2485 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2488 BUG_ON(ret == 0); /* Corruption */
2490 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2497 leaf = path->nodes[0];
2498 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2500 chunk = btrfs_item_ptr(leaf, path->slots[0],
2501 struct btrfs_chunk);
2502 chunk_type = btrfs_chunk_type(leaf, chunk);
2503 btrfs_release_path(path);
2505 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2506 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2515 if (found_key.offset == 0)
2517 key.offset = found_key.offset - 1;
2520 if (failed && !retried) {
2524 } else if (failed && retried) {
2529 btrfs_free_path(path);
2533 static int insert_balance_item(struct btrfs_root *root,
2534 struct btrfs_balance_control *bctl)
2536 struct btrfs_trans_handle *trans;
2537 struct btrfs_balance_item *item;
2538 struct btrfs_disk_balance_args disk_bargs;
2539 struct btrfs_path *path;
2540 struct extent_buffer *leaf;
2541 struct btrfs_key key;
2544 path = btrfs_alloc_path();
2548 trans = btrfs_start_transaction(root, 0);
2549 if (IS_ERR(trans)) {
2550 btrfs_free_path(path);
2551 return PTR_ERR(trans);
2554 key.objectid = BTRFS_BALANCE_OBJECTID;
2555 key.type = BTRFS_BALANCE_ITEM_KEY;
2558 ret = btrfs_insert_empty_item(trans, root, path, &key,
2563 leaf = path->nodes[0];
2564 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2566 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2568 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2569 btrfs_set_balance_data(leaf, item, &disk_bargs);
2570 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2571 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2572 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2573 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2575 btrfs_set_balance_flags(leaf, item, bctl->flags);
2577 btrfs_mark_buffer_dirty(leaf);
2579 btrfs_free_path(path);
2580 err = btrfs_commit_transaction(trans, root);
2586 static int del_balance_item(struct btrfs_root *root)
2588 struct btrfs_trans_handle *trans;
2589 struct btrfs_path *path;
2590 struct btrfs_key key;
2593 path = btrfs_alloc_path();
2597 trans = btrfs_start_transaction(root, 0);
2598 if (IS_ERR(trans)) {
2599 btrfs_free_path(path);
2600 return PTR_ERR(trans);
2603 key.objectid = BTRFS_BALANCE_OBJECTID;
2604 key.type = BTRFS_BALANCE_ITEM_KEY;
2607 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2615 ret = btrfs_del_item(trans, root, path);
2617 btrfs_free_path(path);
2618 err = btrfs_commit_transaction(trans, root);
2625 * This is a heuristic used to reduce the number of chunks balanced on
2626 * resume after balance was interrupted.
2628 static void update_balance_args(struct btrfs_balance_control *bctl)
2631 * Turn on soft mode for chunk types that were being converted.
2633 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2634 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2635 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2636 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2637 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2638 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2641 * Turn on usage filter if is not already used. The idea is
2642 * that chunks that we have already balanced should be
2643 * reasonably full. Don't do it for chunks that are being
2644 * converted - that will keep us from relocating unconverted
2645 * (albeit full) chunks.
2647 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2648 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2649 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2650 bctl->data.usage = 90;
2652 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2653 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2654 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2655 bctl->sys.usage = 90;
2657 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2658 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2659 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2660 bctl->meta.usage = 90;
2665 * Should be called with both balance and volume mutexes held to
2666 * serialize other volume operations (add_dev/rm_dev/resize) with
2667 * restriper. Same goes for unset_balance_control.
2669 static void set_balance_control(struct btrfs_balance_control *bctl)
2671 struct btrfs_fs_info *fs_info = bctl->fs_info;
2673 BUG_ON(fs_info->balance_ctl);
2675 spin_lock(&fs_info->balance_lock);
2676 fs_info->balance_ctl = bctl;
2677 spin_unlock(&fs_info->balance_lock);
2680 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2682 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2684 BUG_ON(!fs_info->balance_ctl);
2686 spin_lock(&fs_info->balance_lock);
2687 fs_info->balance_ctl = NULL;
2688 spin_unlock(&fs_info->balance_lock);
2694 * Balance filters. Return 1 if chunk should be filtered out
2695 * (should not be balanced).
2697 static int chunk_profiles_filter(u64 chunk_type,
2698 struct btrfs_balance_args *bargs)
2700 chunk_type = chunk_to_extended(chunk_type) &
2701 BTRFS_EXTENDED_PROFILE_MASK;
2703 if (bargs->profiles & chunk_type)
2709 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2710 struct btrfs_balance_args *bargs)
2712 struct btrfs_block_group_cache *cache;
2713 u64 chunk_used, user_thresh;
2716 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2717 chunk_used = btrfs_block_group_used(&cache->item);
2719 if (bargs->usage == 0)
2721 else if (bargs->usage > 100)
2722 user_thresh = cache->key.offset;
2724 user_thresh = div_factor_fine(cache->key.offset,
2727 if (chunk_used < user_thresh)
2730 btrfs_put_block_group(cache);
2734 static int chunk_devid_filter(struct extent_buffer *leaf,
2735 struct btrfs_chunk *chunk,
2736 struct btrfs_balance_args *bargs)
2738 struct btrfs_stripe *stripe;
2739 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2742 for (i = 0; i < num_stripes; i++) {
2743 stripe = btrfs_stripe_nr(chunk, i);
2744 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2751 /* [pstart, pend) */
2752 static int chunk_drange_filter(struct extent_buffer *leaf,
2753 struct btrfs_chunk *chunk,
2755 struct btrfs_balance_args *bargs)
2757 struct btrfs_stripe *stripe;
2758 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2764 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2767 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2768 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2769 factor = num_stripes / 2;
2770 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2771 factor = num_stripes - 1;
2772 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2773 factor = num_stripes - 2;
2775 factor = num_stripes;
2778 for (i = 0; i < num_stripes; i++) {
2779 stripe = btrfs_stripe_nr(chunk, i);
2780 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2783 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2784 stripe_length = btrfs_chunk_length(leaf, chunk);
2785 do_div(stripe_length, factor);
2787 if (stripe_offset < bargs->pend &&
2788 stripe_offset + stripe_length > bargs->pstart)
2795 /* [vstart, vend) */
2796 static int chunk_vrange_filter(struct extent_buffer *leaf,
2797 struct btrfs_chunk *chunk,
2799 struct btrfs_balance_args *bargs)
2801 if (chunk_offset < bargs->vend &&
2802 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2803 /* at least part of the chunk is inside this vrange */
2809 static int chunk_soft_convert_filter(u64 chunk_type,
2810 struct btrfs_balance_args *bargs)
2812 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2815 chunk_type = chunk_to_extended(chunk_type) &
2816 BTRFS_EXTENDED_PROFILE_MASK;
2818 if (bargs->target == chunk_type)
2824 static int should_balance_chunk(struct btrfs_root *root,
2825 struct extent_buffer *leaf,
2826 struct btrfs_chunk *chunk, u64 chunk_offset)
2828 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2829 struct btrfs_balance_args *bargs = NULL;
2830 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2833 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2834 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2838 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2839 bargs = &bctl->data;
2840 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2842 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2843 bargs = &bctl->meta;
2845 /* profiles filter */
2846 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2847 chunk_profiles_filter(chunk_type, bargs)) {
2852 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2853 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2858 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2859 chunk_devid_filter(leaf, chunk, bargs)) {
2863 /* drange filter, makes sense only with devid filter */
2864 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2865 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2870 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2871 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2875 /* soft profile changing mode */
2876 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2877 chunk_soft_convert_filter(chunk_type, bargs)) {
2884 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
2886 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2887 struct btrfs_root *chunk_root = fs_info->chunk_root;
2888 struct btrfs_root *dev_root = fs_info->dev_root;
2889 struct list_head *devices;
2890 struct btrfs_device *device;
2893 struct btrfs_chunk *chunk;
2894 struct btrfs_path *path;
2895 struct btrfs_key key;
2896 struct btrfs_key found_key;
2897 struct btrfs_trans_handle *trans;
2898 struct extent_buffer *leaf;
2901 int enospc_errors = 0;
2902 bool counting = true;
2904 /* step one make some room on all the devices */
2905 devices = &fs_info->fs_devices->devices;
2906 list_for_each_entry(device, devices, dev_list) {
2907 old_size = device->total_bytes;
2908 size_to_free = div_factor(old_size, 1);
2909 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2910 if (!device->writeable ||
2911 device->total_bytes - device->bytes_used > size_to_free ||
2912 device->is_tgtdev_for_dev_replace)
2915 ret = btrfs_shrink_device(device, old_size - size_to_free);
2920 trans = btrfs_start_transaction(dev_root, 0);
2921 BUG_ON(IS_ERR(trans));
2923 ret = btrfs_grow_device(trans, device, old_size);
2926 btrfs_end_transaction(trans, dev_root);
2929 /* step two, relocate all the chunks */
2930 path = btrfs_alloc_path();
2936 /* zero out stat counters */
2937 spin_lock(&fs_info->balance_lock);
2938 memset(&bctl->stat, 0, sizeof(bctl->stat));
2939 spin_unlock(&fs_info->balance_lock);
2941 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2942 key.offset = (u64)-1;
2943 key.type = BTRFS_CHUNK_ITEM_KEY;
2946 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
2947 atomic_read(&fs_info->balance_cancel_req)) {
2952 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2957 * this shouldn't happen, it means the last relocate
2961 BUG(); /* FIXME break ? */
2963 ret = btrfs_previous_item(chunk_root, path, 0,
2964 BTRFS_CHUNK_ITEM_KEY);
2970 leaf = path->nodes[0];
2971 slot = path->slots[0];
2972 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2974 if (found_key.objectid != key.objectid)
2977 /* chunk zero is special */
2978 if (found_key.offset == 0)
2981 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2984 spin_lock(&fs_info->balance_lock);
2985 bctl->stat.considered++;
2986 spin_unlock(&fs_info->balance_lock);
2989 ret = should_balance_chunk(chunk_root, leaf, chunk,
2991 btrfs_release_path(path);
2996 spin_lock(&fs_info->balance_lock);
2997 bctl->stat.expected++;
2998 spin_unlock(&fs_info->balance_lock);
3002 ret = btrfs_relocate_chunk(chunk_root,
3003 chunk_root->root_key.objectid,
3006 if (ret && ret != -ENOSPC)
3008 if (ret == -ENOSPC) {
3011 spin_lock(&fs_info->balance_lock);
3012 bctl->stat.completed++;
3013 spin_unlock(&fs_info->balance_lock);
3016 key.offset = found_key.offset - 1;
3020 btrfs_release_path(path);
3025 btrfs_free_path(path);
3026 if (enospc_errors) {
3027 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3037 * alloc_profile_is_valid - see if a given profile is valid and reduced
3038 * @flags: profile to validate
3039 * @extended: if true @flags is treated as an extended profile
3041 static int alloc_profile_is_valid(u64 flags, int extended)
3043 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3044 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3046 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3048 /* 1) check that all other bits are zeroed */
3052 /* 2) see if profile is reduced */
3054 return !extended; /* "0" is valid for usual profiles */
3056 /* true if exactly one bit set */
3057 return (flags & (flags - 1)) == 0;
3060 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3062 /* cancel requested || normal exit path */
3063 return atomic_read(&fs_info->balance_cancel_req) ||
3064 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3065 atomic_read(&fs_info->balance_cancel_req) == 0);
3068 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3072 unset_balance_control(fs_info);
3073 ret = del_balance_item(fs_info->tree_root);
3075 btrfs_std_error(fs_info, ret);
3077 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3081 * Should be called with both balance and volume mutexes held
3083 int btrfs_balance(struct btrfs_balance_control *bctl,
3084 struct btrfs_ioctl_balance_args *bargs)
3086 struct btrfs_fs_info *fs_info = bctl->fs_info;
3093 if (btrfs_fs_closing(fs_info) ||
3094 atomic_read(&fs_info->balance_pause_req) ||
3095 atomic_read(&fs_info->balance_cancel_req)) {
3100 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3101 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3105 * In case of mixed groups both data and meta should be picked,
3106 * and identical options should be given for both of them.
3108 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3109 if (mixed && (bctl->flags & allowed)) {
3110 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3111 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3112 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3113 printk(KERN_ERR "btrfs: with mixed groups data and "
3114 "metadata balance options must be the same\n");
3120 num_devices = fs_info->fs_devices->num_devices;
3121 btrfs_dev_replace_lock(&fs_info->dev_replace);
3122 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3123 BUG_ON(num_devices < 1);
3126 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3127 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3128 if (num_devices == 1)
3129 allowed |= BTRFS_BLOCK_GROUP_DUP;
3130 else if (num_devices > 1)
3131 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3132 if (num_devices > 2)
3133 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3134 if (num_devices > 3)
3135 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3136 BTRFS_BLOCK_GROUP_RAID6);
3137 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3138 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3139 (bctl->data.target & ~allowed))) {
3140 printk(KERN_ERR "btrfs: unable to start balance with target "
3141 "data profile %llu\n",
3142 (unsigned long long)bctl->data.target);
3146 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3147 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3148 (bctl->meta.target & ~allowed))) {
3149 printk(KERN_ERR "btrfs: unable to start balance with target "
3150 "metadata profile %llu\n",
3151 (unsigned long long)bctl->meta.target);
3155 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3156 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3157 (bctl->sys.target & ~allowed))) {
3158 printk(KERN_ERR "btrfs: unable to start balance with target "
3159 "system profile %llu\n",
3160 (unsigned long long)bctl->sys.target);
3165 /* allow dup'ed data chunks only in mixed mode */
3166 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3167 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3168 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3173 /* allow to reduce meta or sys integrity only if force set */
3174 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3175 BTRFS_BLOCK_GROUP_RAID10 |
3176 BTRFS_BLOCK_GROUP_RAID5 |
3177 BTRFS_BLOCK_GROUP_RAID6;
3179 seq = read_seqbegin(&fs_info->profiles_lock);
3181 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3182 (fs_info->avail_system_alloc_bits & allowed) &&
3183 !(bctl->sys.target & allowed)) ||
3184 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3185 (fs_info->avail_metadata_alloc_bits & allowed) &&
3186 !(bctl->meta.target & allowed))) {
3187 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3188 printk(KERN_INFO "btrfs: force reducing metadata "
3191 printk(KERN_ERR "btrfs: balance will reduce metadata "
3192 "integrity, use force if you want this\n");
3197 } while (read_seqretry(&fs_info->profiles_lock, seq));
3199 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3200 int num_tolerated_disk_barrier_failures;
3201 u64 target = bctl->sys.target;
3203 num_tolerated_disk_barrier_failures =
3204 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3205 if (num_tolerated_disk_barrier_failures > 0 &&
3207 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3208 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3209 num_tolerated_disk_barrier_failures = 0;
3210 else if (num_tolerated_disk_barrier_failures > 1 &&
3212 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3213 num_tolerated_disk_barrier_failures = 1;
3215 fs_info->num_tolerated_disk_barrier_failures =
3216 num_tolerated_disk_barrier_failures;
3219 ret = insert_balance_item(fs_info->tree_root, bctl);
3220 if (ret && ret != -EEXIST)
3223 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3224 BUG_ON(ret == -EEXIST);
3225 set_balance_control(bctl);
3227 BUG_ON(ret != -EEXIST);
3228 spin_lock(&fs_info->balance_lock);
3229 update_balance_args(bctl);
3230 spin_unlock(&fs_info->balance_lock);
3233 atomic_inc(&fs_info->balance_running);
3234 mutex_unlock(&fs_info->balance_mutex);
3236 ret = __btrfs_balance(fs_info);
3238 mutex_lock(&fs_info->balance_mutex);
3239 atomic_dec(&fs_info->balance_running);
3241 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3242 fs_info->num_tolerated_disk_barrier_failures =
3243 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3247 memset(bargs, 0, sizeof(*bargs));
3248 update_ioctl_balance_args(fs_info, 0, bargs);
3251 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3252 balance_need_close(fs_info)) {
3253 __cancel_balance(fs_info);
3256 wake_up(&fs_info->balance_wait_q);
3260 if (bctl->flags & BTRFS_BALANCE_RESUME)
3261 __cancel_balance(fs_info);
3264 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3269 static int balance_kthread(void *data)
3271 struct btrfs_fs_info *fs_info = data;
3274 mutex_lock(&fs_info->volume_mutex);
3275 mutex_lock(&fs_info->balance_mutex);
3277 if (fs_info->balance_ctl) {
3278 printk(KERN_INFO "btrfs: continuing balance\n");
3279 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3282 mutex_unlock(&fs_info->balance_mutex);
3283 mutex_unlock(&fs_info->volume_mutex);
3288 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3290 struct task_struct *tsk;
3292 spin_lock(&fs_info->balance_lock);
3293 if (!fs_info->balance_ctl) {
3294 spin_unlock(&fs_info->balance_lock);
3297 spin_unlock(&fs_info->balance_lock);
3299 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3300 printk(KERN_INFO "btrfs: force skipping balance\n");
3304 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3305 return PTR_RET(tsk);
3308 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3310 struct btrfs_balance_control *bctl;
3311 struct btrfs_balance_item *item;
3312 struct btrfs_disk_balance_args disk_bargs;
3313 struct btrfs_path *path;
3314 struct extent_buffer *leaf;
3315 struct btrfs_key key;
3318 path = btrfs_alloc_path();
3322 key.objectid = BTRFS_BALANCE_OBJECTID;
3323 key.type = BTRFS_BALANCE_ITEM_KEY;
3326 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3329 if (ret > 0) { /* ret = -ENOENT; */
3334 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3340 leaf = path->nodes[0];
3341 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3343 bctl->fs_info = fs_info;
3344 bctl->flags = btrfs_balance_flags(leaf, item);
3345 bctl->flags |= BTRFS_BALANCE_RESUME;
3347 btrfs_balance_data(leaf, item, &disk_bargs);
3348 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3349 btrfs_balance_meta(leaf, item, &disk_bargs);
3350 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3351 btrfs_balance_sys(leaf, item, &disk_bargs);
3352 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3354 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3356 mutex_lock(&fs_info->volume_mutex);
3357 mutex_lock(&fs_info->balance_mutex);
3359 set_balance_control(bctl);
3361 mutex_unlock(&fs_info->balance_mutex);
3362 mutex_unlock(&fs_info->volume_mutex);
3364 btrfs_free_path(path);
3368 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3372 mutex_lock(&fs_info->balance_mutex);
3373 if (!fs_info->balance_ctl) {
3374 mutex_unlock(&fs_info->balance_mutex);
3378 if (atomic_read(&fs_info->balance_running)) {
3379 atomic_inc(&fs_info->balance_pause_req);
3380 mutex_unlock(&fs_info->balance_mutex);
3382 wait_event(fs_info->balance_wait_q,
3383 atomic_read(&fs_info->balance_running) == 0);
3385 mutex_lock(&fs_info->balance_mutex);
3386 /* we are good with balance_ctl ripped off from under us */
3387 BUG_ON(atomic_read(&fs_info->balance_running));
3388 atomic_dec(&fs_info->balance_pause_req);
3393 mutex_unlock(&fs_info->balance_mutex);
3397 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3399 mutex_lock(&fs_info->balance_mutex);
3400 if (!fs_info->balance_ctl) {
3401 mutex_unlock(&fs_info->balance_mutex);
3405 atomic_inc(&fs_info->balance_cancel_req);
3407 * if we are running just wait and return, balance item is
3408 * deleted in btrfs_balance in this case
3410 if (atomic_read(&fs_info->balance_running)) {
3411 mutex_unlock(&fs_info->balance_mutex);
3412 wait_event(fs_info->balance_wait_q,
3413 atomic_read(&fs_info->balance_running) == 0);
3414 mutex_lock(&fs_info->balance_mutex);
3416 /* __cancel_balance needs volume_mutex */
3417 mutex_unlock(&fs_info->balance_mutex);
3418 mutex_lock(&fs_info->volume_mutex);
3419 mutex_lock(&fs_info->balance_mutex);
3421 if (fs_info->balance_ctl)
3422 __cancel_balance(fs_info);
3424 mutex_unlock(&fs_info->volume_mutex);
3427 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3428 atomic_dec(&fs_info->balance_cancel_req);
3429 mutex_unlock(&fs_info->balance_mutex);
3433 static int btrfs_uuid_scan_kthread(void *data)
3435 struct btrfs_fs_info *fs_info = data;
3436 struct btrfs_root *root = fs_info->tree_root;
3437 struct btrfs_key key;
3438 struct btrfs_key max_key;
3439 struct btrfs_path *path = NULL;
3441 struct extent_buffer *eb;
3443 struct btrfs_root_item root_item;
3445 struct btrfs_trans_handle *trans;
3447 path = btrfs_alloc_path();
3454 key.type = BTRFS_ROOT_ITEM_KEY;
3457 max_key.objectid = (u64)-1;
3458 max_key.type = BTRFS_ROOT_ITEM_KEY;
3459 max_key.offset = (u64)-1;
3461 path->keep_locks = 1;
3464 ret = btrfs_search_forward(root, &key, &max_key, path, 0);
3471 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3472 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3473 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3474 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3477 eb = path->nodes[0];
3478 slot = path->slots[0];
3479 item_size = btrfs_item_size_nr(eb, slot);
3480 if (item_size < sizeof(root_item))
3484 read_extent_buffer(eb, &root_item,
3485 btrfs_item_ptr_offset(eb, slot),
3486 (int)sizeof(root_item));
3487 if (btrfs_root_refs(&root_item) == 0)
3489 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3491 * 1 - subvol uuid item
3492 * 1 - received_subvol uuid item
3494 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3495 if (IS_ERR(trans)) {
3496 ret = PTR_ERR(trans);
3499 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3501 BTRFS_UUID_KEY_SUBVOL,
3504 pr_warn("btrfs: uuid_tree_add failed %d\n",
3506 btrfs_end_transaction(trans,
3507 fs_info->uuid_root);
3512 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3514 /* 1 - received_subvol uuid item */
3515 trans = btrfs_start_transaction(
3516 fs_info->uuid_root, 1);
3517 if (IS_ERR(trans)) {
3518 ret = PTR_ERR(trans);
3522 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3523 root_item.received_uuid,
3524 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3527 pr_warn("btrfs: uuid_tree_add failed %d\n",
3529 btrfs_end_transaction(trans,
3530 fs_info->uuid_root);
3536 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3542 btrfs_release_path(path);
3543 if (key.offset < (u64)-1) {
3545 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3547 key.type = BTRFS_ROOT_ITEM_KEY;
3548 } else if (key.objectid < (u64)-1) {
3550 key.type = BTRFS_ROOT_ITEM_KEY;
3559 btrfs_free_path(path);
3561 pr_warn("btrfs: btrfs_uuid_scan_kthread failed %d\n", ret);
3562 up(&fs_info->uuid_tree_rescan_sem);
3566 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3568 struct btrfs_trans_handle *trans;
3569 struct btrfs_root *tree_root = fs_info->tree_root;
3570 struct btrfs_root *uuid_root;
3571 struct task_struct *task;
3578 trans = btrfs_start_transaction(tree_root, 2);
3580 return PTR_ERR(trans);
3582 uuid_root = btrfs_create_tree(trans, fs_info,
3583 BTRFS_UUID_TREE_OBJECTID);
3584 if (IS_ERR(uuid_root)) {
3585 btrfs_abort_transaction(trans, tree_root,
3586 PTR_ERR(uuid_root));
3587 return PTR_ERR(uuid_root);
3590 fs_info->uuid_root = uuid_root;
3592 ret = btrfs_commit_transaction(trans, tree_root);
3596 down(&fs_info->uuid_tree_rescan_sem);
3597 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3599 pr_warn("btrfs: failed to start uuid_scan task\n");
3600 up(&fs_info->uuid_tree_rescan_sem);
3601 return PTR_ERR(task);
3608 * shrinking a device means finding all of the device extents past
3609 * the new size, and then following the back refs to the chunks.
3610 * The chunk relocation code actually frees the device extent
3612 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3614 struct btrfs_trans_handle *trans;
3615 struct btrfs_root *root = device->dev_root;
3616 struct btrfs_dev_extent *dev_extent = NULL;
3617 struct btrfs_path *path;
3625 bool retried = false;
3626 struct extent_buffer *l;
3627 struct btrfs_key key;
3628 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3629 u64 old_total = btrfs_super_total_bytes(super_copy);
3630 u64 old_size = device->total_bytes;
3631 u64 diff = device->total_bytes - new_size;
3633 if (device->is_tgtdev_for_dev_replace)
3636 path = btrfs_alloc_path();
3644 device->total_bytes = new_size;
3645 if (device->writeable) {
3646 device->fs_devices->total_rw_bytes -= diff;
3647 spin_lock(&root->fs_info->free_chunk_lock);
3648 root->fs_info->free_chunk_space -= diff;
3649 spin_unlock(&root->fs_info->free_chunk_lock);
3651 unlock_chunks(root);
3654 key.objectid = device->devid;
3655 key.offset = (u64)-1;
3656 key.type = BTRFS_DEV_EXTENT_KEY;
3659 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3663 ret = btrfs_previous_item(root, path, 0, key.type);
3668 btrfs_release_path(path);
3673 slot = path->slots[0];
3674 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3676 if (key.objectid != device->devid) {
3677 btrfs_release_path(path);
3681 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3682 length = btrfs_dev_extent_length(l, dev_extent);
3684 if (key.offset + length <= new_size) {
3685 btrfs_release_path(path);
3689 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3690 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3691 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3692 btrfs_release_path(path);
3694 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3696 if (ret && ret != -ENOSPC)
3700 } while (key.offset-- > 0);
3702 if (failed && !retried) {
3706 } else if (failed && retried) {
3710 device->total_bytes = old_size;
3711 if (device->writeable)
3712 device->fs_devices->total_rw_bytes += diff;
3713 spin_lock(&root->fs_info->free_chunk_lock);
3714 root->fs_info->free_chunk_space += diff;
3715 spin_unlock(&root->fs_info->free_chunk_lock);
3716 unlock_chunks(root);
3720 /* Shrinking succeeded, else we would be at "done". */
3721 trans = btrfs_start_transaction(root, 0);
3722 if (IS_ERR(trans)) {
3723 ret = PTR_ERR(trans);
3729 device->disk_total_bytes = new_size;
3730 /* Now btrfs_update_device() will change the on-disk size. */
3731 ret = btrfs_update_device(trans, device);
3733 unlock_chunks(root);
3734 btrfs_end_transaction(trans, root);
3737 WARN_ON(diff > old_total);
3738 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3739 unlock_chunks(root);
3740 btrfs_end_transaction(trans, root);
3742 btrfs_free_path(path);
3746 static int btrfs_add_system_chunk(struct btrfs_root *root,
3747 struct btrfs_key *key,
3748 struct btrfs_chunk *chunk, int item_size)
3750 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3751 struct btrfs_disk_key disk_key;
3755 array_size = btrfs_super_sys_array_size(super_copy);
3756 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3759 ptr = super_copy->sys_chunk_array + array_size;
3760 btrfs_cpu_key_to_disk(&disk_key, key);
3761 memcpy(ptr, &disk_key, sizeof(disk_key));
3762 ptr += sizeof(disk_key);
3763 memcpy(ptr, chunk, item_size);
3764 item_size += sizeof(disk_key);
3765 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3770 * sort the devices in descending order by max_avail, total_avail
3772 static int btrfs_cmp_device_info(const void *a, const void *b)
3774 const struct btrfs_device_info *di_a = a;
3775 const struct btrfs_device_info *di_b = b;
3777 if (di_a->max_avail > di_b->max_avail)
3779 if (di_a->max_avail < di_b->max_avail)
3781 if (di_a->total_avail > di_b->total_avail)
3783 if (di_a->total_avail < di_b->total_avail)
3788 static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
3789 [BTRFS_RAID_RAID10] = {
3792 .devs_max = 0, /* 0 == as many as possible */
3794 .devs_increment = 2,
3797 [BTRFS_RAID_RAID1] = {
3802 .devs_increment = 2,
3805 [BTRFS_RAID_DUP] = {
3810 .devs_increment = 1,
3813 [BTRFS_RAID_RAID0] = {
3818 .devs_increment = 1,
3821 [BTRFS_RAID_SINGLE] = {
3826 .devs_increment = 1,
3829 [BTRFS_RAID_RAID5] = {
3834 .devs_increment = 1,
3837 [BTRFS_RAID_RAID6] = {
3842 .devs_increment = 1,
3847 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3849 /* TODO allow them to set a preferred stripe size */
3853 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3855 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3858 btrfs_set_fs_incompat(info, RAID56);
3861 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *extent_root, u64 start,
3865 struct btrfs_fs_info *info = extent_root->fs_info;
3866 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3867 struct list_head *cur;
3868 struct map_lookup *map = NULL;
3869 struct extent_map_tree *em_tree;
3870 struct extent_map *em;
3871 struct btrfs_device_info *devices_info = NULL;
3873 int num_stripes; /* total number of stripes to allocate */
3874 int data_stripes; /* number of stripes that count for
3876 int sub_stripes; /* sub_stripes info for map */
3877 int dev_stripes; /* stripes per dev */
3878 int devs_max; /* max devs to use */
3879 int devs_min; /* min devs needed */
3880 int devs_increment; /* ndevs has to be a multiple of this */
3881 int ncopies; /* how many copies to data has */
3883 u64 max_stripe_size;
3887 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
3893 BUG_ON(!alloc_profile_is_valid(type, 0));
3895 if (list_empty(&fs_devices->alloc_list))
3898 index = __get_raid_index(type);
3900 sub_stripes = btrfs_raid_array[index].sub_stripes;
3901 dev_stripes = btrfs_raid_array[index].dev_stripes;
3902 devs_max = btrfs_raid_array[index].devs_max;
3903 devs_min = btrfs_raid_array[index].devs_min;
3904 devs_increment = btrfs_raid_array[index].devs_increment;
3905 ncopies = btrfs_raid_array[index].ncopies;
3907 if (type & BTRFS_BLOCK_GROUP_DATA) {
3908 max_stripe_size = 1024 * 1024 * 1024;
3909 max_chunk_size = 10 * max_stripe_size;
3910 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
3911 /* for larger filesystems, use larger metadata chunks */
3912 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3913 max_stripe_size = 1024 * 1024 * 1024;
3915 max_stripe_size = 256 * 1024 * 1024;
3916 max_chunk_size = max_stripe_size;
3917 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
3918 max_stripe_size = 32 * 1024 * 1024;
3919 max_chunk_size = 2 * max_stripe_size;
3921 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3926 /* we don't want a chunk larger than 10% of writeable space */
3927 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3930 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3935 cur = fs_devices->alloc_list.next;
3938 * in the first pass through the devices list, we gather information
3939 * about the available holes on each device.
3942 while (cur != &fs_devices->alloc_list) {
3943 struct btrfs_device *device;
3947 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3951 if (!device->writeable) {
3953 "btrfs: read-only device in alloc_list\n");
3957 if (!device->in_fs_metadata ||
3958 device->is_tgtdev_for_dev_replace)
3961 if (device->total_bytes > device->bytes_used)
3962 total_avail = device->total_bytes - device->bytes_used;
3966 /* If there is no space on this device, skip it. */
3967 if (total_avail == 0)
3970 ret = find_free_dev_extent(trans, device,
3971 max_stripe_size * dev_stripes,
3972 &dev_offset, &max_avail);
3973 if (ret && ret != -ENOSPC)
3977 max_avail = max_stripe_size * dev_stripes;
3979 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3982 if (ndevs == fs_devices->rw_devices) {
3983 WARN(1, "%s: found more than %llu devices\n",
3984 __func__, fs_devices->rw_devices);
3987 devices_info[ndevs].dev_offset = dev_offset;
3988 devices_info[ndevs].max_avail = max_avail;
3989 devices_info[ndevs].total_avail = total_avail;
3990 devices_info[ndevs].dev = device;
3995 * now sort the devices by hole size / available space
3997 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3998 btrfs_cmp_device_info, NULL);
4000 /* round down to number of usable stripes */
4001 ndevs -= ndevs % devs_increment;
4003 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4008 if (devs_max && ndevs > devs_max)
4011 * the primary goal is to maximize the number of stripes, so use as many
4012 * devices as possible, even if the stripes are not maximum sized.
4014 stripe_size = devices_info[ndevs-1].max_avail;
4015 num_stripes = ndevs * dev_stripes;
4018 * this will have to be fixed for RAID1 and RAID10 over
4021 data_stripes = num_stripes / ncopies;
4023 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4024 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4025 btrfs_super_stripesize(info->super_copy));
4026 data_stripes = num_stripes - 1;
4028 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4029 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4030 btrfs_super_stripesize(info->super_copy));
4031 data_stripes = num_stripes - 2;
4035 * Use the number of data stripes to figure out how big this chunk
4036 * is really going to be in terms of logical address space,
4037 * and compare that answer with the max chunk size
4039 if (stripe_size * data_stripes > max_chunk_size) {
4040 u64 mask = (1ULL << 24) - 1;
4041 stripe_size = max_chunk_size;
4042 do_div(stripe_size, data_stripes);
4044 /* bump the answer up to a 16MB boundary */
4045 stripe_size = (stripe_size + mask) & ~mask;
4047 /* but don't go higher than the limits we found
4048 * while searching for free extents
4050 if (stripe_size > devices_info[ndevs-1].max_avail)
4051 stripe_size = devices_info[ndevs-1].max_avail;
4054 do_div(stripe_size, dev_stripes);
4056 /* align to BTRFS_STRIPE_LEN */
4057 do_div(stripe_size, raid_stripe_len);
4058 stripe_size *= raid_stripe_len;
4060 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4065 map->num_stripes = num_stripes;
4067 for (i = 0; i < ndevs; ++i) {
4068 for (j = 0; j < dev_stripes; ++j) {
4069 int s = i * dev_stripes + j;
4070 map->stripes[s].dev = devices_info[i].dev;
4071 map->stripes[s].physical = devices_info[i].dev_offset +
4075 map->sector_size = extent_root->sectorsize;
4076 map->stripe_len = raid_stripe_len;
4077 map->io_align = raid_stripe_len;
4078 map->io_width = raid_stripe_len;
4080 map->sub_stripes = sub_stripes;
4082 num_bytes = stripe_size * data_stripes;
4084 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
4086 em = alloc_extent_map();
4091 em->bdev = (struct block_device *)map;
4093 em->len = num_bytes;
4094 em->block_start = 0;
4095 em->block_len = em->len;
4096 em->orig_block_len = stripe_size;
4098 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4099 write_lock(&em_tree->lock);
4100 ret = add_extent_mapping(em_tree, em, 0);
4102 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4103 atomic_inc(&em->refs);
4105 write_unlock(&em_tree->lock);
4107 free_extent_map(em);
4111 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4112 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4115 goto error_del_extent;
4117 free_extent_map(em);
4118 check_raid56_incompat_flag(extent_root->fs_info, type);
4120 kfree(devices_info);
4124 write_lock(&em_tree->lock);
4125 remove_extent_mapping(em_tree, em);
4126 write_unlock(&em_tree->lock);
4128 /* One for our allocation */
4129 free_extent_map(em);
4130 /* One for the tree reference */
4131 free_extent_map(em);
4134 kfree(devices_info);
4138 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4139 struct btrfs_root *extent_root,
4140 u64 chunk_offset, u64 chunk_size)
4142 struct btrfs_key key;
4143 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4144 struct btrfs_device *device;
4145 struct btrfs_chunk *chunk;
4146 struct btrfs_stripe *stripe;
4147 struct extent_map_tree *em_tree;
4148 struct extent_map *em;
4149 struct map_lookup *map;
4156 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4157 read_lock(&em_tree->lock);
4158 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4159 read_unlock(&em_tree->lock);
4162 btrfs_crit(extent_root->fs_info, "unable to find logical "
4163 "%Lu len %Lu", chunk_offset, chunk_size);
4167 if (em->start != chunk_offset || em->len != chunk_size) {
4168 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4169 " %Lu-%Lu, found %Lu-%Lu\n", chunk_offset,
4170 chunk_size, em->start, em->len);
4171 free_extent_map(em);
4175 map = (struct map_lookup *)em->bdev;
4176 item_size = btrfs_chunk_item_size(map->num_stripes);
4177 stripe_size = em->orig_block_len;
4179 chunk = kzalloc(item_size, GFP_NOFS);
4185 for (i = 0; i < map->num_stripes; i++) {
4186 device = map->stripes[i].dev;
4187 dev_offset = map->stripes[i].physical;
4189 device->bytes_used += stripe_size;
4190 ret = btrfs_update_device(trans, device);
4193 ret = btrfs_alloc_dev_extent(trans, device,
4194 chunk_root->root_key.objectid,
4195 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4196 chunk_offset, dev_offset,
4202 spin_lock(&extent_root->fs_info->free_chunk_lock);
4203 extent_root->fs_info->free_chunk_space -= (stripe_size *
4205 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4207 stripe = &chunk->stripe;
4208 for (i = 0; i < map->num_stripes; i++) {
4209 device = map->stripes[i].dev;
4210 dev_offset = map->stripes[i].physical;
4212 btrfs_set_stack_stripe_devid(stripe, device->devid);
4213 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4214 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4218 btrfs_set_stack_chunk_length(chunk, chunk_size);
4219 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4220 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4221 btrfs_set_stack_chunk_type(chunk, map->type);
4222 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4223 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4224 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4225 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4226 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4228 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4229 key.type = BTRFS_CHUNK_ITEM_KEY;
4230 key.offset = chunk_offset;
4232 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4233 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4235 * TODO: Cleanup of inserted chunk root in case of
4238 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4244 free_extent_map(em);
4249 * Chunk allocation falls into two parts. The first part does works
4250 * that make the new allocated chunk useable, but not do any operation
4251 * that modifies the chunk tree. The second part does the works that
4252 * require modifying the chunk tree. This division is important for the
4253 * bootstrap process of adding storage to a seed btrfs.
4255 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4256 struct btrfs_root *extent_root, u64 type)
4260 chunk_offset = find_next_chunk(extent_root->fs_info);
4261 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
4264 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4265 struct btrfs_root *root,
4266 struct btrfs_device *device)
4269 u64 sys_chunk_offset;
4271 struct btrfs_fs_info *fs_info = root->fs_info;
4272 struct btrfs_root *extent_root = fs_info->extent_root;
4275 chunk_offset = find_next_chunk(fs_info);
4276 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4277 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4282 sys_chunk_offset = find_next_chunk(root->fs_info);
4283 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4284 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4287 btrfs_abort_transaction(trans, root, ret);
4291 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
4293 btrfs_abort_transaction(trans, root, ret);
4298 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4300 struct extent_map *em;
4301 struct map_lookup *map;
4302 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4306 read_lock(&map_tree->map_tree.lock);
4307 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4308 read_unlock(&map_tree->map_tree.lock);
4312 if (btrfs_test_opt(root, DEGRADED)) {
4313 free_extent_map(em);
4317 map = (struct map_lookup *)em->bdev;
4318 for (i = 0; i < map->num_stripes; i++) {
4319 if (!map->stripes[i].dev->writeable) {
4324 free_extent_map(em);
4328 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4330 extent_map_tree_init(&tree->map_tree);
4333 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4335 struct extent_map *em;
4338 write_lock(&tree->map_tree.lock);
4339 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4341 remove_extent_mapping(&tree->map_tree, em);
4342 write_unlock(&tree->map_tree.lock);
4347 free_extent_map(em);
4348 /* once for the tree */
4349 free_extent_map(em);
4353 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4355 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4356 struct extent_map *em;
4357 struct map_lookup *map;
4358 struct extent_map_tree *em_tree = &map_tree->map_tree;
4361 read_lock(&em_tree->lock);
4362 em = lookup_extent_mapping(em_tree, logical, len);
4363 read_unlock(&em_tree->lock);
4366 * We could return errors for these cases, but that could get ugly and
4367 * we'd probably do the same thing which is just not do anything else
4368 * and exit, so return 1 so the callers don't try to use other copies.
4371 btrfs_crit(fs_info, "No mapping for %Lu-%Lu\n", logical,
4376 if (em->start > logical || em->start + em->len < logical) {
4377 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
4378 "%Lu-%Lu\n", logical, logical+len, em->start,
4379 em->start + em->len);
4383 map = (struct map_lookup *)em->bdev;
4384 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4385 ret = map->num_stripes;
4386 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4387 ret = map->sub_stripes;
4388 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4390 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4394 free_extent_map(em);
4396 btrfs_dev_replace_lock(&fs_info->dev_replace);
4397 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4399 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4404 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4405 struct btrfs_mapping_tree *map_tree,
4408 struct extent_map *em;
4409 struct map_lookup *map;
4410 struct extent_map_tree *em_tree = &map_tree->map_tree;
4411 unsigned long len = root->sectorsize;
4413 read_lock(&em_tree->lock);
4414 em = lookup_extent_mapping(em_tree, logical, len);
4415 read_unlock(&em_tree->lock);
4418 BUG_ON(em->start > logical || em->start + em->len < logical);
4419 map = (struct map_lookup *)em->bdev;
4420 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4421 BTRFS_BLOCK_GROUP_RAID6)) {
4422 len = map->stripe_len * nr_data_stripes(map);
4424 free_extent_map(em);
4428 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4429 u64 logical, u64 len, int mirror_num)
4431 struct extent_map *em;
4432 struct map_lookup *map;
4433 struct extent_map_tree *em_tree = &map_tree->map_tree;
4436 read_lock(&em_tree->lock);
4437 em = lookup_extent_mapping(em_tree, logical, len);
4438 read_unlock(&em_tree->lock);
4441 BUG_ON(em->start > logical || em->start + em->len < logical);
4442 map = (struct map_lookup *)em->bdev;
4443 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4444 BTRFS_BLOCK_GROUP_RAID6))
4446 free_extent_map(em);
4450 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4451 struct map_lookup *map, int first, int num,
4452 int optimal, int dev_replace_is_ongoing)
4456 struct btrfs_device *srcdev;
4458 if (dev_replace_is_ongoing &&
4459 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4460 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4461 srcdev = fs_info->dev_replace.srcdev;
4466 * try to avoid the drive that is the source drive for a
4467 * dev-replace procedure, only choose it if no other non-missing
4468 * mirror is available
4470 for (tolerance = 0; tolerance < 2; tolerance++) {
4471 if (map->stripes[optimal].dev->bdev &&
4472 (tolerance || map->stripes[optimal].dev != srcdev))
4474 for (i = first; i < first + num; i++) {
4475 if (map->stripes[i].dev->bdev &&
4476 (tolerance || map->stripes[i].dev != srcdev))
4481 /* we couldn't find one that doesn't fail. Just return something
4482 * and the io error handling code will clean up eventually
4487 static inline int parity_smaller(u64 a, u64 b)
4492 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4493 static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4495 struct btrfs_bio_stripe s;
4502 for (i = 0; i < bbio->num_stripes - 1; i++) {
4503 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4504 s = bbio->stripes[i];
4506 bbio->stripes[i] = bbio->stripes[i+1];
4507 raid_map[i] = raid_map[i+1];
4508 bbio->stripes[i+1] = s;
4516 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4517 u64 logical, u64 *length,
4518 struct btrfs_bio **bbio_ret,
4519 int mirror_num, u64 **raid_map_ret)
4521 struct extent_map *em;
4522 struct map_lookup *map;
4523 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4524 struct extent_map_tree *em_tree = &map_tree->map_tree;
4527 u64 stripe_end_offset;
4532 u64 *raid_map = NULL;
4538 struct btrfs_bio *bbio = NULL;
4539 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4540 int dev_replace_is_ongoing = 0;
4541 int num_alloc_stripes;
4542 int patch_the_first_stripe_for_dev_replace = 0;
4543 u64 physical_to_patch_in_first_stripe = 0;
4544 u64 raid56_full_stripe_start = (u64)-1;
4546 read_lock(&em_tree->lock);
4547 em = lookup_extent_mapping(em_tree, logical, *length);
4548 read_unlock(&em_tree->lock);
4551 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4552 (unsigned long long)logical,
4553 (unsigned long long)*length);
4557 if (em->start > logical || em->start + em->len < logical) {
4558 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4559 "found %Lu-%Lu\n", logical, em->start,
4560 em->start + em->len);
4564 map = (struct map_lookup *)em->bdev;
4565 offset = logical - em->start;
4567 stripe_len = map->stripe_len;
4570 * stripe_nr counts the total number of stripes we have to stride
4571 * to get to this block
4573 do_div(stripe_nr, stripe_len);
4575 stripe_offset = stripe_nr * stripe_len;
4576 BUG_ON(offset < stripe_offset);
4578 /* stripe_offset is the offset of this block in its stripe*/
4579 stripe_offset = offset - stripe_offset;
4581 /* if we're here for raid56, we need to know the stripe aligned start */
4582 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4583 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4584 raid56_full_stripe_start = offset;
4586 /* allow a write of a full stripe, but make sure we don't
4587 * allow straddling of stripes
4589 do_div(raid56_full_stripe_start, full_stripe_len);
4590 raid56_full_stripe_start *= full_stripe_len;
4593 if (rw & REQ_DISCARD) {
4594 /* we don't discard raid56 yet */
4596 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4600 *length = min_t(u64, em->len - offset, *length);
4601 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4603 /* For writes to RAID[56], allow a full stripeset across all disks.
4604 For other RAID types and for RAID[56] reads, just allow a single
4605 stripe (on a single disk). */
4606 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4608 max_len = stripe_len * nr_data_stripes(map) -
4609 (offset - raid56_full_stripe_start);
4611 /* we limit the length of each bio to what fits in a stripe */
4612 max_len = stripe_len - stripe_offset;
4614 *length = min_t(u64, em->len - offset, max_len);
4616 *length = em->len - offset;
4619 /* This is for when we're called from btrfs_merge_bio_hook() and all
4620 it cares about is the length */
4624 btrfs_dev_replace_lock(dev_replace);
4625 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4626 if (!dev_replace_is_ongoing)
4627 btrfs_dev_replace_unlock(dev_replace);
4629 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4630 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4631 dev_replace->tgtdev != NULL) {
4633 * in dev-replace case, for repair case (that's the only
4634 * case where the mirror is selected explicitly when
4635 * calling btrfs_map_block), blocks left of the left cursor
4636 * can also be read from the target drive.
4637 * For REQ_GET_READ_MIRRORS, the target drive is added as
4638 * the last one to the array of stripes. For READ, it also
4639 * needs to be supported using the same mirror number.
4640 * If the requested block is not left of the left cursor,
4641 * EIO is returned. This can happen because btrfs_num_copies()
4642 * returns one more in the dev-replace case.
4644 u64 tmp_length = *length;
4645 struct btrfs_bio *tmp_bbio = NULL;
4646 int tmp_num_stripes;
4647 u64 srcdev_devid = dev_replace->srcdev->devid;
4648 int index_srcdev = 0;
4650 u64 physical_of_found = 0;
4652 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
4653 logical, &tmp_length, &tmp_bbio, 0, NULL);
4655 WARN_ON(tmp_bbio != NULL);
4659 tmp_num_stripes = tmp_bbio->num_stripes;
4660 if (mirror_num > tmp_num_stripes) {
4662 * REQ_GET_READ_MIRRORS does not contain this
4663 * mirror, that means that the requested area
4664 * is not left of the left cursor
4672 * process the rest of the function using the mirror_num
4673 * of the source drive. Therefore look it up first.
4674 * At the end, patch the device pointer to the one of the
4677 for (i = 0; i < tmp_num_stripes; i++) {
4678 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4680 * In case of DUP, in order to keep it
4681 * simple, only add the mirror with the
4682 * lowest physical address
4685 physical_of_found <=
4686 tmp_bbio->stripes[i].physical)
4691 tmp_bbio->stripes[i].physical;
4696 mirror_num = index_srcdev + 1;
4697 patch_the_first_stripe_for_dev_replace = 1;
4698 physical_to_patch_in_first_stripe = physical_of_found;
4707 } else if (mirror_num > map->num_stripes) {
4713 stripe_nr_orig = stripe_nr;
4714 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
4715 do_div(stripe_nr_end, map->stripe_len);
4716 stripe_end_offset = stripe_nr_end * map->stripe_len -
4719 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4720 if (rw & REQ_DISCARD)
4721 num_stripes = min_t(u64, map->num_stripes,
4722 stripe_nr_end - stripe_nr_orig);
4723 stripe_index = do_div(stripe_nr, map->num_stripes);
4724 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
4725 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
4726 num_stripes = map->num_stripes;
4727 else if (mirror_num)
4728 stripe_index = mirror_num - 1;
4730 stripe_index = find_live_mirror(fs_info, map, 0,
4732 current->pid % map->num_stripes,
4733 dev_replace_is_ongoing);
4734 mirror_num = stripe_index + 1;
4737 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
4738 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
4739 num_stripes = map->num_stripes;
4740 } else if (mirror_num) {
4741 stripe_index = mirror_num - 1;
4746 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4747 int factor = map->num_stripes / map->sub_stripes;
4749 stripe_index = do_div(stripe_nr, factor);
4750 stripe_index *= map->sub_stripes;
4752 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
4753 num_stripes = map->sub_stripes;
4754 else if (rw & REQ_DISCARD)
4755 num_stripes = min_t(u64, map->sub_stripes *
4756 (stripe_nr_end - stripe_nr_orig),
4758 else if (mirror_num)
4759 stripe_index += mirror_num - 1;
4761 int old_stripe_index = stripe_index;
4762 stripe_index = find_live_mirror(fs_info, map,
4764 map->sub_stripes, stripe_index +
4765 current->pid % map->sub_stripes,
4766 dev_replace_is_ongoing);
4767 mirror_num = stripe_index - old_stripe_index + 1;
4770 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4771 BTRFS_BLOCK_GROUP_RAID6)) {
4774 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4778 /* push stripe_nr back to the start of the full stripe */
4779 stripe_nr = raid56_full_stripe_start;
4780 do_div(stripe_nr, stripe_len);
4782 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4784 /* RAID[56] write or recovery. Return all stripes */
4785 num_stripes = map->num_stripes;
4786 max_errors = nr_parity_stripes(map);
4788 raid_map = kmalloc(sizeof(u64) * num_stripes,
4795 /* Work out the disk rotation on this stripe-set */
4797 rot = do_div(tmp, num_stripes);
4799 /* Fill in the logical address of each stripe */
4800 tmp = stripe_nr * nr_data_stripes(map);
4801 for (i = 0; i < nr_data_stripes(map); i++)
4802 raid_map[(i+rot) % num_stripes] =
4803 em->start + (tmp + i) * map->stripe_len;
4805 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4806 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4807 raid_map[(i+rot+1) % num_stripes] =
4810 *length = map->stripe_len;
4815 * Mirror #0 or #1 means the original data block.
4816 * Mirror #2 is RAID5 parity block.
4817 * Mirror #3 is RAID6 Q block.
4819 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4821 stripe_index = nr_data_stripes(map) +
4824 /* We distribute the parity blocks across stripes */
4825 tmp = stripe_nr + stripe_index;
4826 stripe_index = do_div(tmp, map->num_stripes);
4830 * after this do_div call, stripe_nr is the number of stripes
4831 * on this device we have to walk to find the data, and
4832 * stripe_index is the number of our device in the stripe array
4834 stripe_index = do_div(stripe_nr, map->num_stripes);
4835 mirror_num = stripe_index + 1;
4837 BUG_ON(stripe_index >= map->num_stripes);
4839 num_alloc_stripes = num_stripes;
4840 if (dev_replace_is_ongoing) {
4841 if (rw & (REQ_WRITE | REQ_DISCARD))
4842 num_alloc_stripes <<= 1;
4843 if (rw & REQ_GET_READ_MIRRORS)
4844 num_alloc_stripes++;
4846 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
4852 atomic_set(&bbio->error, 0);
4854 if (rw & REQ_DISCARD) {
4856 int sub_stripes = 0;
4857 u64 stripes_per_dev = 0;
4858 u32 remaining_stripes = 0;
4859 u32 last_stripe = 0;
4862 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4863 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4866 sub_stripes = map->sub_stripes;
4868 factor = map->num_stripes / sub_stripes;
4869 stripes_per_dev = div_u64_rem(stripe_nr_end -
4872 &remaining_stripes);
4873 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4874 last_stripe *= sub_stripes;
4877 for (i = 0; i < num_stripes; i++) {
4878 bbio->stripes[i].physical =
4879 map->stripes[stripe_index].physical +
4880 stripe_offset + stripe_nr * map->stripe_len;
4881 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
4883 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4884 BTRFS_BLOCK_GROUP_RAID10)) {
4885 bbio->stripes[i].length = stripes_per_dev *
4888 if (i / sub_stripes < remaining_stripes)
4889 bbio->stripes[i].length +=
4893 * Special for the first stripe and
4896 * |-------|...|-------|
4900 if (i < sub_stripes)
4901 bbio->stripes[i].length -=
4904 if (stripe_index >= last_stripe &&
4905 stripe_index <= (last_stripe +
4907 bbio->stripes[i].length -=
4910 if (i == sub_stripes - 1)
4913 bbio->stripes[i].length = *length;
4916 if (stripe_index == map->num_stripes) {
4917 /* This could only happen for RAID0/10 */
4923 for (i = 0; i < num_stripes; i++) {
4924 bbio->stripes[i].physical =
4925 map->stripes[stripe_index].physical +
4927 stripe_nr * map->stripe_len;
4928 bbio->stripes[i].dev =
4929 map->stripes[stripe_index].dev;
4934 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
4935 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4936 BTRFS_BLOCK_GROUP_RAID10 |
4937 BTRFS_BLOCK_GROUP_RAID5 |
4938 BTRFS_BLOCK_GROUP_DUP)) {
4940 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4945 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4946 dev_replace->tgtdev != NULL) {
4947 int index_where_to_add;
4948 u64 srcdev_devid = dev_replace->srcdev->devid;
4951 * duplicate the write operations while the dev replace
4952 * procedure is running. Since the copying of the old disk
4953 * to the new disk takes place at run time while the
4954 * filesystem is mounted writable, the regular write
4955 * operations to the old disk have to be duplicated to go
4956 * to the new disk as well.
4957 * Note that device->missing is handled by the caller, and
4958 * that the write to the old disk is already set up in the
4961 index_where_to_add = num_stripes;
4962 for (i = 0; i < num_stripes; i++) {
4963 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4964 /* write to new disk, too */
4965 struct btrfs_bio_stripe *new =
4966 bbio->stripes + index_where_to_add;
4967 struct btrfs_bio_stripe *old =
4970 new->physical = old->physical;
4971 new->length = old->length;
4972 new->dev = dev_replace->tgtdev;
4973 index_where_to_add++;
4977 num_stripes = index_where_to_add;
4978 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4979 dev_replace->tgtdev != NULL) {
4980 u64 srcdev_devid = dev_replace->srcdev->devid;
4981 int index_srcdev = 0;
4983 u64 physical_of_found = 0;
4986 * During the dev-replace procedure, the target drive can
4987 * also be used to read data in case it is needed to repair
4988 * a corrupt block elsewhere. This is possible if the
4989 * requested area is left of the left cursor. In this area,
4990 * the target drive is a full copy of the source drive.
4992 for (i = 0; i < num_stripes; i++) {
4993 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4995 * In case of DUP, in order to keep it
4996 * simple, only add the mirror with the
4997 * lowest physical address
5000 physical_of_found <=
5001 bbio->stripes[i].physical)
5005 physical_of_found = bbio->stripes[i].physical;
5009 u64 length = map->stripe_len;
5011 if (physical_of_found + length <=
5012 dev_replace->cursor_left) {
5013 struct btrfs_bio_stripe *tgtdev_stripe =
5014 bbio->stripes + num_stripes;
5016 tgtdev_stripe->physical = physical_of_found;
5017 tgtdev_stripe->length =
5018 bbio->stripes[index_srcdev].length;
5019 tgtdev_stripe->dev = dev_replace->tgtdev;
5027 bbio->num_stripes = num_stripes;
5028 bbio->max_errors = max_errors;
5029 bbio->mirror_num = mirror_num;
5032 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5033 * mirror_num == num_stripes + 1 && dev_replace target drive is
5034 * available as a mirror
5036 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5037 WARN_ON(num_stripes > 1);
5038 bbio->stripes[0].dev = dev_replace->tgtdev;
5039 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5040 bbio->mirror_num = map->num_stripes + 1;
5043 sort_parity_stripes(bbio, raid_map);
5044 *raid_map_ret = raid_map;
5047 if (dev_replace_is_ongoing)
5048 btrfs_dev_replace_unlock(dev_replace);
5049 free_extent_map(em);
5053 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5054 u64 logical, u64 *length,
5055 struct btrfs_bio **bbio_ret, int mirror_num)
5057 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5061 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5062 u64 chunk_start, u64 physical, u64 devid,
5063 u64 **logical, int *naddrs, int *stripe_len)
5065 struct extent_map_tree *em_tree = &map_tree->map_tree;
5066 struct extent_map *em;
5067 struct map_lookup *map;
5075 read_lock(&em_tree->lock);
5076 em = lookup_extent_mapping(em_tree, chunk_start, 1);
5077 read_unlock(&em_tree->lock);
5080 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
5085 if (em->start != chunk_start) {
5086 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
5087 em->start, chunk_start);
5088 free_extent_map(em);
5091 map = (struct map_lookup *)em->bdev;
5094 rmap_len = map->stripe_len;
5096 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5097 do_div(length, map->num_stripes / map->sub_stripes);
5098 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5099 do_div(length, map->num_stripes);
5100 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5101 BTRFS_BLOCK_GROUP_RAID6)) {
5102 do_div(length, nr_data_stripes(map));
5103 rmap_len = map->stripe_len * nr_data_stripes(map);
5106 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
5107 BUG_ON(!buf); /* -ENOMEM */
5109 for (i = 0; i < map->num_stripes; i++) {
5110 if (devid && map->stripes[i].dev->devid != devid)
5112 if (map->stripes[i].physical > physical ||
5113 map->stripes[i].physical + length <= physical)
5116 stripe_nr = physical - map->stripes[i].physical;
5117 do_div(stripe_nr, map->stripe_len);
5119 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5120 stripe_nr = stripe_nr * map->num_stripes + i;
5121 do_div(stripe_nr, map->sub_stripes);
5122 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5123 stripe_nr = stripe_nr * map->num_stripes + i;
5124 } /* else if RAID[56], multiply by nr_data_stripes().
5125 * Alternatively, just use rmap_len below instead of
5126 * map->stripe_len */
5128 bytenr = chunk_start + stripe_nr * rmap_len;
5129 WARN_ON(nr >= map->num_stripes);
5130 for (j = 0; j < nr; j++) {
5131 if (buf[j] == bytenr)
5135 WARN_ON(nr >= map->num_stripes);
5142 *stripe_len = rmap_len;
5144 free_extent_map(em);
5148 static void btrfs_end_bio(struct bio *bio, int err)
5150 struct btrfs_bio *bbio = bio->bi_private;
5151 int is_orig_bio = 0;
5154 atomic_inc(&bbio->error);
5155 if (err == -EIO || err == -EREMOTEIO) {
5156 unsigned int stripe_index =
5157 btrfs_io_bio(bio)->stripe_index;
5158 struct btrfs_device *dev;
5160 BUG_ON(stripe_index >= bbio->num_stripes);
5161 dev = bbio->stripes[stripe_index].dev;
5163 if (bio->bi_rw & WRITE)
5164 btrfs_dev_stat_inc(dev,
5165 BTRFS_DEV_STAT_WRITE_ERRS);
5167 btrfs_dev_stat_inc(dev,
5168 BTRFS_DEV_STAT_READ_ERRS);
5169 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5170 btrfs_dev_stat_inc(dev,
5171 BTRFS_DEV_STAT_FLUSH_ERRS);
5172 btrfs_dev_stat_print_on_error(dev);
5177 if (bio == bbio->orig_bio)
5180 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5183 bio = bbio->orig_bio;
5185 bio->bi_private = bbio->private;
5186 bio->bi_end_io = bbio->end_io;
5187 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5188 /* only send an error to the higher layers if it is
5189 * beyond the tolerance of the btrfs bio
5191 if (atomic_read(&bbio->error) > bbio->max_errors) {
5195 * this bio is actually up to date, we didn't
5196 * go over the max number of errors
5198 set_bit(BIO_UPTODATE, &bio->bi_flags);
5203 bio_endio(bio, err);
5204 } else if (!is_orig_bio) {
5209 struct async_sched {
5212 struct btrfs_fs_info *info;
5213 struct btrfs_work work;
5217 * see run_scheduled_bios for a description of why bios are collected for
5220 * This will add one bio to the pending list for a device and make sure
5221 * the work struct is scheduled.
5223 static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5224 struct btrfs_device *device,
5225 int rw, struct bio *bio)
5227 int should_queue = 1;
5228 struct btrfs_pending_bios *pending_bios;
5230 if (device->missing || !device->bdev) {
5231 bio_endio(bio, -EIO);
5235 /* don't bother with additional async steps for reads, right now */
5236 if (!(rw & REQ_WRITE)) {
5238 btrfsic_submit_bio(rw, bio);
5244 * nr_async_bios allows us to reliably return congestion to the
5245 * higher layers. Otherwise, the async bio makes it appear we have
5246 * made progress against dirty pages when we've really just put it
5247 * on a queue for later
5249 atomic_inc(&root->fs_info->nr_async_bios);
5250 WARN_ON(bio->bi_next);
5251 bio->bi_next = NULL;
5254 spin_lock(&device->io_lock);
5255 if (bio->bi_rw & REQ_SYNC)
5256 pending_bios = &device->pending_sync_bios;
5258 pending_bios = &device->pending_bios;
5260 if (pending_bios->tail)
5261 pending_bios->tail->bi_next = bio;
5263 pending_bios->tail = bio;
5264 if (!pending_bios->head)
5265 pending_bios->head = bio;
5266 if (device->running_pending)
5269 spin_unlock(&device->io_lock);
5272 btrfs_queue_worker(&root->fs_info->submit_workers,
5276 static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5279 struct bio_vec *prev;
5280 struct request_queue *q = bdev_get_queue(bdev);
5281 unsigned short max_sectors = queue_max_sectors(q);
5282 struct bvec_merge_data bvm = {
5284 .bi_sector = sector,
5285 .bi_rw = bio->bi_rw,
5288 if (bio->bi_vcnt == 0) {
5293 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5294 if (bio_sectors(bio) > max_sectors)
5297 if (!q->merge_bvec_fn)
5300 bvm.bi_size = bio->bi_size - prev->bv_len;
5301 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5306 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5307 struct bio *bio, u64 physical, int dev_nr,
5310 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5312 bio->bi_private = bbio;
5313 btrfs_io_bio(bio)->stripe_index = dev_nr;
5314 bio->bi_end_io = btrfs_end_bio;
5315 bio->bi_sector = physical >> 9;
5318 struct rcu_string *name;
5321 name = rcu_dereference(dev->name);
5322 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5323 "(%s id %llu), size=%u\n", rw,
5324 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5325 name->str, dev->devid, bio->bi_size);
5329 bio->bi_bdev = dev->bdev;
5331 btrfs_schedule_bio(root, dev, rw, bio);
5333 btrfsic_submit_bio(rw, bio);
5336 static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5337 struct bio *first_bio, struct btrfs_device *dev,
5338 int dev_nr, int rw, int async)
5340 struct bio_vec *bvec = first_bio->bi_io_vec;
5342 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5343 u64 physical = bbio->stripes[dev_nr].physical;
5346 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5350 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5351 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5352 bvec->bv_offset) < bvec->bv_len) {
5353 u64 len = bio->bi_size;
5355 atomic_inc(&bbio->stripes_pending);
5356 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5364 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5368 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5370 atomic_inc(&bbio->error);
5371 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5372 bio->bi_private = bbio->private;
5373 bio->bi_end_io = bbio->end_io;
5374 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5375 bio->bi_sector = logical >> 9;
5377 bio_endio(bio, -EIO);
5381 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5382 int mirror_num, int async_submit)
5384 struct btrfs_device *dev;
5385 struct bio *first_bio = bio;
5386 u64 logical = (u64)bio->bi_sector << 9;
5389 u64 *raid_map = NULL;
5393 struct btrfs_bio *bbio = NULL;
5395 length = bio->bi_size;
5396 map_length = length;
5398 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5399 mirror_num, &raid_map);
5400 if (ret) /* -ENOMEM */
5403 total_devs = bbio->num_stripes;
5404 bbio->orig_bio = first_bio;
5405 bbio->private = first_bio->bi_private;
5406 bbio->end_io = first_bio->bi_end_io;
5407 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5410 /* In this case, map_length has been set to the length of
5411 a single stripe; not the whole write */
5413 return raid56_parity_write(root, bio, bbio,
5414 raid_map, map_length);
5416 return raid56_parity_recover(root, bio, bbio,
5417 raid_map, map_length,
5422 if (map_length < length) {
5423 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5424 (unsigned long long)logical,
5425 (unsigned long long)length,
5426 (unsigned long long)map_length);
5430 while (dev_nr < total_devs) {
5431 dev = bbio->stripes[dev_nr].dev;
5432 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5433 bbio_error(bbio, first_bio, logical);
5439 * Check and see if we're ok with this bio based on it's size
5440 * and offset with the given device.
5442 if (!bio_size_ok(dev->bdev, first_bio,
5443 bbio->stripes[dev_nr].physical >> 9)) {
5444 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5445 dev_nr, rw, async_submit);
5451 if (dev_nr < total_devs - 1) {
5452 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
5453 BUG_ON(!bio); /* -ENOMEM */
5458 submit_stripe_bio(root, bbio, bio,
5459 bbio->stripes[dev_nr].physical, dev_nr, rw,
5466 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5469 struct btrfs_device *device;
5470 struct btrfs_fs_devices *cur_devices;
5472 cur_devices = fs_info->fs_devices;
5473 while (cur_devices) {
5475 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5476 device = __find_device(&cur_devices->devices,
5481 cur_devices = cur_devices->seed;
5486 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5487 u64 devid, u8 *dev_uuid)
5489 struct btrfs_device *device;
5490 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5492 device = kzalloc(sizeof(*device), GFP_NOFS);
5495 list_add(&device->dev_list,
5496 &fs_devices->devices);
5497 device->devid = devid;
5498 device->work.func = pending_bios_fn;
5499 device->fs_devices = fs_devices;
5500 device->missing = 1;
5501 fs_devices->num_devices++;
5502 fs_devices->missing_devices++;
5503 spin_lock_init(&device->io_lock);
5504 INIT_LIST_HEAD(&device->dev_alloc_list);
5505 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5509 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5510 struct extent_buffer *leaf,
5511 struct btrfs_chunk *chunk)
5513 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5514 struct map_lookup *map;
5515 struct extent_map *em;
5519 u8 uuid[BTRFS_UUID_SIZE];
5524 logical = key->offset;
5525 length = btrfs_chunk_length(leaf, chunk);
5527 read_lock(&map_tree->map_tree.lock);
5528 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
5529 read_unlock(&map_tree->map_tree.lock);
5531 /* already mapped? */
5532 if (em && em->start <= logical && em->start + em->len > logical) {
5533 free_extent_map(em);
5536 free_extent_map(em);
5539 em = alloc_extent_map();
5542 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5543 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5545 free_extent_map(em);
5549 em->bdev = (struct block_device *)map;
5550 em->start = logical;
5553 em->block_start = 0;
5554 em->block_len = em->len;
5556 map->num_stripes = num_stripes;
5557 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5558 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5559 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5560 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5561 map->type = btrfs_chunk_type(leaf, chunk);
5562 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
5563 for (i = 0; i < num_stripes; i++) {
5564 map->stripes[i].physical =
5565 btrfs_stripe_offset_nr(leaf, chunk, i);
5566 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
5567 read_extent_buffer(leaf, uuid, (unsigned long)
5568 btrfs_stripe_dev_uuid_nr(chunk, i),
5570 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5572 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
5574 free_extent_map(em);
5577 if (!map->stripes[i].dev) {
5578 map->stripes[i].dev =
5579 add_missing_dev(root, devid, uuid);
5580 if (!map->stripes[i].dev) {
5582 free_extent_map(em);
5586 map->stripes[i].dev->in_fs_metadata = 1;
5589 write_lock(&map_tree->map_tree.lock);
5590 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
5591 write_unlock(&map_tree->map_tree.lock);
5592 BUG_ON(ret); /* Tree corruption */
5593 free_extent_map(em);
5598 static void fill_device_from_item(struct extent_buffer *leaf,
5599 struct btrfs_dev_item *dev_item,
5600 struct btrfs_device *device)
5604 device->devid = btrfs_device_id(leaf, dev_item);
5605 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5606 device->total_bytes = device->disk_total_bytes;
5607 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5608 device->type = btrfs_device_type(leaf, dev_item);
5609 device->io_align = btrfs_device_io_align(leaf, dev_item);
5610 device->io_width = btrfs_device_io_width(leaf, dev_item);
5611 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
5612 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
5613 device->is_tgtdev_for_dev_replace = 0;
5615 ptr = (unsigned long)btrfs_device_uuid(dev_item);
5616 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
5619 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5621 struct btrfs_fs_devices *fs_devices;
5624 BUG_ON(!mutex_is_locked(&uuid_mutex));
5626 fs_devices = root->fs_info->fs_devices->seed;
5627 while (fs_devices) {
5628 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5632 fs_devices = fs_devices->seed;
5635 fs_devices = find_fsid(fsid);
5641 fs_devices = clone_fs_devices(fs_devices);
5642 if (IS_ERR(fs_devices)) {
5643 ret = PTR_ERR(fs_devices);
5647 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
5648 root->fs_info->bdev_holder);
5650 free_fs_devices(fs_devices);
5654 if (!fs_devices->seeding) {
5655 __btrfs_close_devices(fs_devices);
5656 free_fs_devices(fs_devices);
5661 fs_devices->seed = root->fs_info->fs_devices->seed;
5662 root->fs_info->fs_devices->seed = fs_devices;
5667 static int read_one_dev(struct btrfs_root *root,
5668 struct extent_buffer *leaf,
5669 struct btrfs_dev_item *dev_item)
5671 struct btrfs_device *device;
5674 u8 fs_uuid[BTRFS_UUID_SIZE];
5675 u8 dev_uuid[BTRFS_UUID_SIZE];
5677 devid = btrfs_device_id(leaf, dev_item);
5678 read_extent_buffer(leaf, dev_uuid,
5679 (unsigned long)btrfs_device_uuid(dev_item),
5681 read_extent_buffer(leaf, fs_uuid,
5682 (unsigned long)btrfs_device_fsid(dev_item),
5685 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5686 ret = open_seed_devices(root, fs_uuid);
5687 if (ret && !btrfs_test_opt(root, DEGRADED))
5691 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
5692 if (!device || !device->bdev) {
5693 if (!btrfs_test_opt(root, DEGRADED))
5697 btrfs_warn(root->fs_info, "devid %llu missing",
5698 (unsigned long long)devid);
5699 device = add_missing_dev(root, devid, dev_uuid);
5702 } else if (!device->missing) {
5704 * this happens when a device that was properly setup
5705 * in the device info lists suddenly goes bad.
5706 * device->bdev is NULL, and so we have to set
5707 * device->missing to one here
5709 root->fs_info->fs_devices->missing_devices++;
5710 device->missing = 1;
5714 if (device->fs_devices != root->fs_info->fs_devices) {
5715 BUG_ON(device->writeable);
5716 if (device->generation !=
5717 btrfs_device_generation(leaf, dev_item))
5721 fill_device_from_item(leaf, dev_item, device);
5722 device->in_fs_metadata = 1;
5723 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
5724 device->fs_devices->total_rw_bytes += device->total_bytes;
5725 spin_lock(&root->fs_info->free_chunk_lock);
5726 root->fs_info->free_chunk_space += device->total_bytes -
5728 spin_unlock(&root->fs_info->free_chunk_lock);
5734 int btrfs_read_sys_array(struct btrfs_root *root)
5736 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
5737 struct extent_buffer *sb;
5738 struct btrfs_disk_key *disk_key;
5739 struct btrfs_chunk *chunk;
5741 unsigned long sb_ptr;
5747 struct btrfs_key key;
5749 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
5750 BTRFS_SUPER_INFO_SIZE);
5753 btrfs_set_buffer_uptodate(sb);
5754 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
5756 * The sb extent buffer is artifical and just used to read the system array.
5757 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5758 * pages up-to-date when the page is larger: extent does not cover the
5759 * whole page and consequently check_page_uptodate does not find all
5760 * the page's extents up-to-date (the hole beyond sb),
5761 * write_extent_buffer then triggers a WARN_ON.
5763 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5764 * but sb spans only this function. Add an explicit SetPageUptodate call
5765 * to silence the warning eg. on PowerPC 64.
5767 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
5768 SetPageUptodate(sb->pages[0]);
5770 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
5771 array_size = btrfs_super_sys_array_size(super_copy);
5773 ptr = super_copy->sys_chunk_array;
5774 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5777 while (cur < array_size) {
5778 disk_key = (struct btrfs_disk_key *)ptr;
5779 btrfs_disk_key_to_cpu(&key, disk_key);
5781 len = sizeof(*disk_key); ptr += len;
5785 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
5786 chunk = (struct btrfs_chunk *)sb_ptr;
5787 ret = read_one_chunk(root, &key, sb, chunk);
5790 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5791 len = btrfs_chunk_item_size(num_stripes);
5800 free_extent_buffer(sb);
5804 int btrfs_read_chunk_tree(struct btrfs_root *root)
5806 struct btrfs_path *path;
5807 struct extent_buffer *leaf;
5808 struct btrfs_key key;
5809 struct btrfs_key found_key;
5813 root = root->fs_info->chunk_root;
5815 path = btrfs_alloc_path();
5819 mutex_lock(&uuid_mutex);
5823 * Read all device items, and then all the chunk items. All
5824 * device items are found before any chunk item (their object id
5825 * is smaller than the lowest possible object id for a chunk
5826 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
5828 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5835 leaf = path->nodes[0];
5836 slot = path->slots[0];
5837 if (slot >= btrfs_header_nritems(leaf)) {
5838 ret = btrfs_next_leaf(root, path);
5845 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5846 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5847 struct btrfs_dev_item *dev_item;
5848 dev_item = btrfs_item_ptr(leaf, slot,
5849 struct btrfs_dev_item);
5850 ret = read_one_dev(root, leaf, dev_item);
5853 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5854 struct btrfs_chunk *chunk;
5855 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5856 ret = read_one_chunk(root, &found_key, leaf, chunk);
5864 unlock_chunks(root);
5865 mutex_unlock(&uuid_mutex);
5867 btrfs_free_path(path);
5871 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
5873 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5874 struct btrfs_device *device;
5876 mutex_lock(&fs_devices->device_list_mutex);
5877 list_for_each_entry(device, &fs_devices->devices, dev_list)
5878 device->dev_root = fs_info->dev_root;
5879 mutex_unlock(&fs_devices->device_list_mutex);
5882 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5886 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5887 btrfs_dev_stat_reset(dev, i);
5890 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5892 struct btrfs_key key;
5893 struct btrfs_key found_key;
5894 struct btrfs_root *dev_root = fs_info->dev_root;
5895 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5896 struct extent_buffer *eb;
5899 struct btrfs_device *device;
5900 struct btrfs_path *path = NULL;
5903 path = btrfs_alloc_path();
5909 mutex_lock(&fs_devices->device_list_mutex);
5910 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5912 struct btrfs_dev_stats_item *ptr;
5915 key.type = BTRFS_DEV_STATS_KEY;
5916 key.offset = device->devid;
5917 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5919 __btrfs_reset_dev_stats(device);
5920 device->dev_stats_valid = 1;
5921 btrfs_release_path(path);
5924 slot = path->slots[0];
5925 eb = path->nodes[0];
5926 btrfs_item_key_to_cpu(eb, &found_key, slot);
5927 item_size = btrfs_item_size_nr(eb, slot);
5929 ptr = btrfs_item_ptr(eb, slot,
5930 struct btrfs_dev_stats_item);
5932 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5933 if (item_size >= (1 + i) * sizeof(__le64))
5934 btrfs_dev_stat_set(device, i,
5935 btrfs_dev_stats_value(eb, ptr, i));
5937 btrfs_dev_stat_reset(device, i);
5940 device->dev_stats_valid = 1;
5941 btrfs_dev_stat_print_on_load(device);
5942 btrfs_release_path(path);
5944 mutex_unlock(&fs_devices->device_list_mutex);
5947 btrfs_free_path(path);
5948 return ret < 0 ? ret : 0;
5951 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5952 struct btrfs_root *dev_root,
5953 struct btrfs_device *device)
5955 struct btrfs_path *path;
5956 struct btrfs_key key;
5957 struct extent_buffer *eb;
5958 struct btrfs_dev_stats_item *ptr;
5963 key.type = BTRFS_DEV_STATS_KEY;
5964 key.offset = device->devid;
5966 path = btrfs_alloc_path();
5968 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5970 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5971 ret, rcu_str_deref(device->name));
5976 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5977 /* need to delete old one and insert a new one */
5978 ret = btrfs_del_item(trans, dev_root, path);
5980 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5981 rcu_str_deref(device->name), ret);
5988 /* need to insert a new item */
5989 btrfs_release_path(path);
5990 ret = btrfs_insert_empty_item(trans, dev_root, path,
5991 &key, sizeof(*ptr));
5993 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5994 rcu_str_deref(device->name), ret);
5999 eb = path->nodes[0];
6000 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6001 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6002 btrfs_set_dev_stats_value(eb, ptr, i,
6003 btrfs_dev_stat_read(device, i));
6004 btrfs_mark_buffer_dirty(eb);
6007 btrfs_free_path(path);
6012 * called from commit_transaction. Writes all changed device stats to disk.
6014 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6015 struct btrfs_fs_info *fs_info)
6017 struct btrfs_root *dev_root = fs_info->dev_root;
6018 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6019 struct btrfs_device *device;
6022 mutex_lock(&fs_devices->device_list_mutex);
6023 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6024 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6027 ret = update_dev_stat_item(trans, dev_root, device);
6029 device->dev_stats_dirty = 0;
6031 mutex_unlock(&fs_devices->device_list_mutex);
6036 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6038 btrfs_dev_stat_inc(dev, index);
6039 btrfs_dev_stat_print_on_error(dev);
6042 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
6044 if (!dev->dev_stats_valid)
6046 printk_ratelimited_in_rcu(KERN_ERR
6047 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6048 rcu_str_deref(dev->name),
6049 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6050 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6051 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6052 btrfs_dev_stat_read(dev,
6053 BTRFS_DEV_STAT_CORRUPTION_ERRS),
6054 btrfs_dev_stat_read(dev,
6055 BTRFS_DEV_STAT_GENERATION_ERRS));
6058 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6062 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6063 if (btrfs_dev_stat_read(dev, i) != 0)
6065 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6066 return; /* all values == 0, suppress message */
6068 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6069 rcu_str_deref(dev->name),
6070 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6071 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6072 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6073 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6074 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6077 int btrfs_get_dev_stats(struct btrfs_root *root,
6078 struct btrfs_ioctl_get_dev_stats *stats)
6080 struct btrfs_device *dev;
6081 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6084 mutex_lock(&fs_devices->device_list_mutex);
6085 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
6086 mutex_unlock(&fs_devices->device_list_mutex);
6090 "btrfs: get dev_stats failed, device not found\n");
6092 } else if (!dev->dev_stats_valid) {
6094 "btrfs: get dev_stats failed, not yet valid\n");
6096 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
6097 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6098 if (stats->nr_items > i)
6100 btrfs_dev_stat_read_and_reset(dev, i);
6102 btrfs_dev_stat_reset(dev, i);
6105 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6106 if (stats->nr_items > i)
6107 stats->values[i] = btrfs_dev_stat_read(dev, i);
6109 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6110 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6114 int btrfs_scratch_superblock(struct btrfs_device *device)
6116 struct buffer_head *bh;
6117 struct btrfs_super_block *disk_super;
6119 bh = btrfs_read_dev_super(device->bdev);
6122 disk_super = (struct btrfs_super_block *)bh->b_data;
6124 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6125 set_buffer_dirty(bh);
6126 sync_dirty_buffer(bh);