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 <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
44 static int init_first_rw_device(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_device *device);
47 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
48 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
49 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
51 static DEFINE_MUTEX(uuid_mutex);
52 static LIST_HEAD(fs_uuids);
54 static void lock_chunks(struct btrfs_root *root)
56 mutex_lock(&root->fs_info->chunk_mutex);
59 static void unlock_chunks(struct btrfs_root *root)
61 mutex_unlock(&root->fs_info->chunk_mutex);
64 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
66 struct btrfs_device *device;
67 WARN_ON(fs_devices->opened);
68 while (!list_empty(&fs_devices->devices)) {
69 device = list_entry(fs_devices->devices.next,
70 struct btrfs_device, dev_list);
71 list_del(&device->dev_list);
72 rcu_string_free(device->name);
78 static void btrfs_kobject_uevent(struct block_device *bdev,
79 enum kobject_action action)
83 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
85 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
87 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
88 &disk_to_dev(bdev->bd_disk)->kobj);
91 void btrfs_cleanup_fs_uuids(void)
93 struct btrfs_fs_devices *fs_devices;
95 while (!list_empty(&fs_uuids)) {
96 fs_devices = list_entry(fs_uuids.next,
97 struct btrfs_fs_devices, list);
98 list_del(&fs_devices->list);
99 free_fs_devices(fs_devices);
103 static noinline struct btrfs_device *__find_device(struct list_head *head,
106 struct btrfs_device *dev;
108 list_for_each_entry(dev, head, dev_list) {
109 if (dev->devid == devid &&
110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
117 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
119 struct btrfs_fs_devices *fs_devices;
121 list_for_each_entry(fs_devices, &fs_uuids, list) {
122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
129 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
130 int flush, struct block_device **bdev,
131 struct buffer_head **bh)
135 *bdev = blkdev_get_by_path(device_path, flags, holder);
138 ret = PTR_ERR(*bdev);
139 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
144 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
145 ret = set_blocksize(*bdev, 4096);
147 blkdev_put(*bdev, flags);
150 invalidate_bdev(*bdev);
151 *bh = btrfs_read_dev_super(*bdev);
154 blkdev_put(*bdev, flags);
166 static void requeue_list(struct btrfs_pending_bios *pending_bios,
167 struct bio *head, struct bio *tail)
170 struct bio *old_head;
172 old_head = pending_bios->head;
173 pending_bios->head = head;
174 if (pending_bios->tail)
175 tail->bi_next = old_head;
177 pending_bios->tail = tail;
181 * we try to collect pending bios for a device so we don't get a large
182 * number of procs sending bios down to the same device. This greatly
183 * improves the schedulers ability to collect and merge the bios.
185 * But, it also turns into a long list of bios to process and that is sure
186 * to eventually make the worker thread block. The solution here is to
187 * make some progress and then put this work struct back at the end of
188 * the list if the block device is congested. This way, multiple devices
189 * can make progress from a single worker thread.
191 static noinline void run_scheduled_bios(struct btrfs_device *device)
194 struct backing_dev_info *bdi;
195 struct btrfs_fs_info *fs_info;
196 struct btrfs_pending_bios *pending_bios;
200 unsigned long num_run;
201 unsigned long batch_run = 0;
203 unsigned long last_waited = 0;
205 int sync_pending = 0;
206 struct blk_plug plug;
209 * this function runs all the bios we've collected for
210 * a particular device. We don't want to wander off to
211 * another device without first sending all of these down.
212 * So, setup a plug here and finish it off before we return
214 blk_start_plug(&plug);
216 bdi = blk_get_backing_dev_info(device->bdev);
217 fs_info = device->dev_root->fs_info;
218 limit = btrfs_async_submit_limit(fs_info);
219 limit = limit * 2 / 3;
222 spin_lock(&device->io_lock);
227 /* take all the bios off the list at once and process them
228 * later on (without the lock held). But, remember the
229 * tail and other pointers so the bios can be properly reinserted
230 * into the list if we hit congestion
232 if (!force_reg && device->pending_sync_bios.head) {
233 pending_bios = &device->pending_sync_bios;
236 pending_bios = &device->pending_bios;
240 pending = pending_bios->head;
241 tail = pending_bios->tail;
242 WARN_ON(pending && !tail);
245 * if pending was null this time around, no bios need processing
246 * at all and we can stop. Otherwise it'll loop back up again
247 * and do an additional check so no bios are missed.
249 * device->running_pending is used to synchronize with the
252 if (device->pending_sync_bios.head == NULL &&
253 device->pending_bios.head == NULL) {
255 device->running_pending = 0;
258 device->running_pending = 1;
261 pending_bios->head = NULL;
262 pending_bios->tail = NULL;
264 spin_unlock(&device->io_lock);
269 /* we want to work on both lists, but do more bios on the
270 * sync list than the regular list
273 pending_bios != &device->pending_sync_bios &&
274 device->pending_sync_bios.head) ||
275 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
276 device->pending_bios.head)) {
277 spin_lock(&device->io_lock);
278 requeue_list(pending_bios, pending, tail);
283 pending = pending->bi_next;
286 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
287 waitqueue_active(&fs_info->async_submit_wait))
288 wake_up(&fs_info->async_submit_wait);
290 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
293 * if we're doing the sync list, record that our
294 * plug has some sync requests on it
296 * If we're doing the regular list and there are
297 * sync requests sitting around, unplug before
300 if (pending_bios == &device->pending_sync_bios) {
302 } else if (sync_pending) {
303 blk_finish_plug(&plug);
304 blk_start_plug(&plug);
308 btrfsic_submit_bio(cur->bi_rw, cur);
315 * we made progress, there is more work to do and the bdi
316 * is now congested. Back off and let other work structs
319 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
320 fs_info->fs_devices->open_devices > 1) {
321 struct io_context *ioc;
323 ioc = current->io_context;
326 * the main goal here is that we don't want to
327 * block if we're going to be able to submit
328 * more requests without blocking.
330 * This code does two great things, it pokes into
331 * the elevator code from a filesystem _and_
332 * it makes assumptions about how batching works.
334 if (ioc && ioc->nr_batch_requests > 0 &&
335 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
337 ioc->last_waited == last_waited)) {
339 * we want to go through our batch of
340 * requests and stop. So, we copy out
341 * the ioc->last_waited time and test
342 * against it before looping
344 last_waited = ioc->last_waited;
349 spin_lock(&device->io_lock);
350 requeue_list(pending_bios, pending, tail);
351 device->running_pending = 1;
353 spin_unlock(&device->io_lock);
354 btrfs_requeue_work(&device->work);
357 /* unplug every 64 requests just for good measure */
358 if (batch_run % 64 == 0) {
359 blk_finish_plug(&plug);
360 blk_start_plug(&plug);
369 spin_lock(&device->io_lock);
370 if (device->pending_bios.head || device->pending_sync_bios.head)
372 spin_unlock(&device->io_lock);
375 blk_finish_plug(&plug);
378 static void pending_bios_fn(struct btrfs_work *work)
380 struct btrfs_device *device;
382 device = container_of(work, struct btrfs_device, work);
383 run_scheduled_bios(device);
386 static noinline int device_list_add(const char *path,
387 struct btrfs_super_block *disk_super,
388 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
390 struct btrfs_device *device;
391 struct btrfs_fs_devices *fs_devices;
392 struct rcu_string *name;
393 u64 found_transid = btrfs_super_generation(disk_super);
395 fs_devices = find_fsid(disk_super->fsid);
397 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
400 INIT_LIST_HEAD(&fs_devices->devices);
401 INIT_LIST_HEAD(&fs_devices->alloc_list);
402 list_add(&fs_devices->list, &fs_uuids);
403 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
404 fs_devices->latest_devid = devid;
405 fs_devices->latest_trans = found_transid;
406 mutex_init(&fs_devices->device_list_mutex);
409 device = __find_device(&fs_devices->devices, devid,
410 disk_super->dev_item.uuid);
413 if (fs_devices->opened)
416 device = kzalloc(sizeof(*device), GFP_NOFS);
418 /* we can safely leave the fs_devices entry around */
421 device->devid = devid;
422 device->dev_stats_valid = 0;
423 device->work.func = pending_bios_fn;
424 memcpy(device->uuid, disk_super->dev_item.uuid,
426 spin_lock_init(&device->io_lock);
428 name = rcu_string_strdup(path, GFP_NOFS);
433 rcu_assign_pointer(device->name, name);
434 INIT_LIST_HEAD(&device->dev_alloc_list);
436 /* init readahead state */
437 spin_lock_init(&device->reada_lock);
438 device->reada_curr_zone = NULL;
439 atomic_set(&device->reada_in_flight, 0);
440 device->reada_next = 0;
441 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
442 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
444 mutex_lock(&fs_devices->device_list_mutex);
445 list_add_rcu(&device->dev_list, &fs_devices->devices);
446 mutex_unlock(&fs_devices->device_list_mutex);
448 device->fs_devices = fs_devices;
449 fs_devices->num_devices++;
450 } else if (!device->name || strcmp(device->name->str, path)) {
451 name = rcu_string_strdup(path, GFP_NOFS);
454 rcu_string_free(device->name);
455 rcu_assign_pointer(device->name, name);
456 if (device->missing) {
457 fs_devices->missing_devices--;
462 if (found_transid > fs_devices->latest_trans) {
463 fs_devices->latest_devid = devid;
464 fs_devices->latest_trans = found_transid;
466 *fs_devices_ret = fs_devices;
470 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
472 struct btrfs_fs_devices *fs_devices;
473 struct btrfs_device *device;
474 struct btrfs_device *orig_dev;
476 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
478 return ERR_PTR(-ENOMEM);
480 INIT_LIST_HEAD(&fs_devices->devices);
481 INIT_LIST_HEAD(&fs_devices->alloc_list);
482 INIT_LIST_HEAD(&fs_devices->list);
483 mutex_init(&fs_devices->device_list_mutex);
484 fs_devices->latest_devid = orig->latest_devid;
485 fs_devices->latest_trans = orig->latest_trans;
486 fs_devices->total_devices = orig->total_devices;
487 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
489 /* We have held the volume lock, it is safe to get the devices. */
490 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
491 struct rcu_string *name;
493 device = kzalloc(sizeof(*device), GFP_NOFS);
498 * This is ok to do without rcu read locked because we hold the
499 * uuid mutex so nothing we touch in here is going to disappear.
501 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
506 rcu_assign_pointer(device->name, name);
508 device->devid = orig_dev->devid;
509 device->work.func = pending_bios_fn;
510 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
511 spin_lock_init(&device->io_lock);
512 INIT_LIST_HEAD(&device->dev_list);
513 INIT_LIST_HEAD(&device->dev_alloc_list);
515 list_add(&device->dev_list, &fs_devices->devices);
516 device->fs_devices = fs_devices;
517 fs_devices->num_devices++;
521 free_fs_devices(fs_devices);
522 return ERR_PTR(-ENOMEM);
525 void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
526 struct btrfs_fs_devices *fs_devices, int step)
528 struct btrfs_device *device, *next;
530 struct block_device *latest_bdev = NULL;
531 u64 latest_devid = 0;
532 u64 latest_transid = 0;
534 mutex_lock(&uuid_mutex);
536 /* This is the initialized path, it is safe to release the devices. */
537 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
538 if (device->in_fs_metadata) {
539 if (!device->is_tgtdev_for_dev_replace &&
541 device->generation > latest_transid)) {
542 latest_devid = device->devid;
543 latest_transid = device->generation;
544 latest_bdev = device->bdev;
549 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
551 * In the first step, keep the device which has
552 * the correct fsid and the devid that is used
553 * for the dev_replace procedure.
554 * In the second step, the dev_replace state is
555 * read from the device tree and it is known
556 * whether the procedure is really active or
557 * not, which means whether this device is
558 * used or whether it should be removed.
560 if (step == 0 || device->is_tgtdev_for_dev_replace) {
565 blkdev_put(device->bdev, device->mode);
567 fs_devices->open_devices--;
569 if (device->writeable) {
570 list_del_init(&device->dev_alloc_list);
571 device->writeable = 0;
572 if (!device->is_tgtdev_for_dev_replace)
573 fs_devices->rw_devices--;
575 list_del_init(&device->dev_list);
576 fs_devices->num_devices--;
577 rcu_string_free(device->name);
581 if (fs_devices->seed) {
582 fs_devices = fs_devices->seed;
586 fs_devices->latest_bdev = latest_bdev;
587 fs_devices->latest_devid = latest_devid;
588 fs_devices->latest_trans = latest_transid;
590 mutex_unlock(&uuid_mutex);
593 static void __free_device(struct work_struct *work)
595 struct btrfs_device *device;
597 device = container_of(work, struct btrfs_device, rcu_work);
600 blkdev_put(device->bdev, device->mode);
602 rcu_string_free(device->name);
606 static void free_device(struct rcu_head *head)
608 struct btrfs_device *device;
610 device = container_of(head, struct btrfs_device, rcu);
612 INIT_WORK(&device->rcu_work, __free_device);
613 schedule_work(&device->rcu_work);
616 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
618 struct btrfs_device *device;
620 if (--fs_devices->opened > 0)
623 mutex_lock(&fs_devices->device_list_mutex);
624 list_for_each_entry(device, &fs_devices->devices, dev_list) {
625 struct btrfs_device *new_device;
626 struct rcu_string *name;
629 fs_devices->open_devices--;
631 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
632 list_del_init(&device->dev_alloc_list);
633 fs_devices->rw_devices--;
636 if (device->can_discard)
637 fs_devices->num_can_discard--;
639 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
640 BUG_ON(!new_device); /* -ENOMEM */
641 memcpy(new_device, device, sizeof(*new_device));
643 /* Safe because we are under uuid_mutex */
645 name = rcu_string_strdup(device->name->str, GFP_NOFS);
646 BUG_ON(device->name && !name); /* -ENOMEM */
647 rcu_assign_pointer(new_device->name, name);
649 new_device->bdev = NULL;
650 new_device->writeable = 0;
651 new_device->in_fs_metadata = 0;
652 new_device->can_discard = 0;
653 spin_lock_init(&new_device->io_lock);
654 list_replace_rcu(&device->dev_list, &new_device->dev_list);
656 call_rcu(&device->rcu, free_device);
658 mutex_unlock(&fs_devices->device_list_mutex);
660 WARN_ON(fs_devices->open_devices);
661 WARN_ON(fs_devices->rw_devices);
662 fs_devices->opened = 0;
663 fs_devices->seeding = 0;
668 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
670 struct btrfs_fs_devices *seed_devices = NULL;
673 mutex_lock(&uuid_mutex);
674 ret = __btrfs_close_devices(fs_devices);
675 if (!fs_devices->opened) {
676 seed_devices = fs_devices->seed;
677 fs_devices->seed = NULL;
679 mutex_unlock(&uuid_mutex);
681 while (seed_devices) {
682 fs_devices = seed_devices;
683 seed_devices = fs_devices->seed;
684 __btrfs_close_devices(fs_devices);
685 free_fs_devices(fs_devices);
688 * Wait for rcu kworkers under __btrfs_close_devices
689 * to finish all blkdev_puts so device is really
690 * free when umount is done.
696 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
697 fmode_t flags, void *holder)
699 struct request_queue *q;
700 struct block_device *bdev;
701 struct list_head *head = &fs_devices->devices;
702 struct btrfs_device *device;
703 struct block_device *latest_bdev = NULL;
704 struct buffer_head *bh;
705 struct btrfs_super_block *disk_super;
706 u64 latest_devid = 0;
707 u64 latest_transid = 0;
714 list_for_each_entry(device, head, dev_list) {
720 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
725 disk_super = (struct btrfs_super_block *)bh->b_data;
726 devid = btrfs_stack_device_id(&disk_super->dev_item);
727 if (devid != device->devid)
730 if (memcmp(device->uuid, disk_super->dev_item.uuid,
734 device->generation = btrfs_super_generation(disk_super);
735 if (!latest_transid || device->generation > latest_transid) {
736 latest_devid = devid;
737 latest_transid = device->generation;
741 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
742 device->writeable = 0;
744 device->writeable = !bdev_read_only(bdev);
748 q = bdev_get_queue(bdev);
749 if (blk_queue_discard(q)) {
750 device->can_discard = 1;
751 fs_devices->num_can_discard++;
755 device->in_fs_metadata = 0;
756 device->mode = flags;
758 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
759 fs_devices->rotating = 1;
761 fs_devices->open_devices++;
762 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
763 fs_devices->rw_devices++;
764 list_add(&device->dev_alloc_list,
765 &fs_devices->alloc_list);
772 blkdev_put(bdev, flags);
775 if (fs_devices->open_devices == 0) {
779 fs_devices->seeding = seeding;
780 fs_devices->opened = 1;
781 fs_devices->latest_bdev = latest_bdev;
782 fs_devices->latest_devid = latest_devid;
783 fs_devices->latest_trans = latest_transid;
784 fs_devices->total_rw_bytes = 0;
789 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
790 fmode_t flags, void *holder)
794 mutex_lock(&uuid_mutex);
795 if (fs_devices->opened) {
796 fs_devices->opened++;
799 ret = __btrfs_open_devices(fs_devices, flags, holder);
801 mutex_unlock(&uuid_mutex);
806 * Look for a btrfs signature on a device. This may be called out of the mount path
807 * and we are not allowed to call set_blocksize during the scan. The superblock
808 * is read via pagecache
810 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
811 struct btrfs_fs_devices **fs_devices_ret)
813 struct btrfs_super_block *disk_super;
814 struct block_device *bdev;
825 * we would like to check all the supers, but that would make
826 * a btrfs mount succeed after a mkfs from a different FS.
827 * So, we need to add a special mount option to scan for
828 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
830 bytenr = btrfs_sb_offset(0);
832 mutex_lock(&uuid_mutex);
834 bdev = blkdev_get_by_path(path, flags, holder);
841 /* make sure our super fits in the device */
842 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
845 /* make sure our super fits in the page */
846 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
849 /* make sure our super doesn't straddle pages on disk */
850 index = bytenr >> PAGE_CACHE_SHIFT;
851 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
854 /* pull in the page with our super */
855 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
858 if (IS_ERR_OR_NULL(page))
863 /* align our pointer to the offset of the super block */
864 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
866 if (btrfs_super_bytenr(disk_super) != bytenr ||
867 disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
870 devid = btrfs_stack_device_id(&disk_super->dev_item);
871 transid = btrfs_super_generation(disk_super);
872 total_devices = btrfs_super_num_devices(disk_super);
874 if (disk_super->label[0]) {
875 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
876 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
877 printk(KERN_INFO "device label %s ", disk_super->label);
879 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
882 printk(KERN_CONT "devid %llu transid %llu %s\n",
883 (unsigned long long)devid, (unsigned long long)transid, path);
885 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
886 if (!ret && fs_devices_ret)
887 (*fs_devices_ret)->total_devices = total_devices;
891 page_cache_release(page);
894 blkdev_put(bdev, flags);
896 mutex_unlock(&uuid_mutex);
900 /* helper to account the used device space in the range */
901 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
902 u64 end, u64 *length)
904 struct btrfs_key key;
905 struct btrfs_root *root = device->dev_root;
906 struct btrfs_dev_extent *dev_extent;
907 struct btrfs_path *path;
911 struct extent_buffer *l;
915 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
918 path = btrfs_alloc_path();
923 key.objectid = device->devid;
925 key.type = BTRFS_DEV_EXTENT_KEY;
927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
931 ret = btrfs_previous_item(root, path, key.objectid, key.type);
938 slot = path->slots[0];
939 if (slot >= btrfs_header_nritems(l)) {
940 ret = btrfs_next_leaf(root, path);
948 btrfs_item_key_to_cpu(l, &key, slot);
950 if (key.objectid < device->devid)
953 if (key.objectid > device->devid)
956 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
959 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
960 extent_end = key.offset + btrfs_dev_extent_length(l,
962 if (key.offset <= start && extent_end > end) {
963 *length = end - start + 1;
965 } else if (key.offset <= start && extent_end > start)
966 *length += extent_end - start;
967 else if (key.offset > start && extent_end <= end)
968 *length += extent_end - key.offset;
969 else if (key.offset > start && key.offset <= end) {
970 *length += end - key.offset + 1;
972 } else if (key.offset > end)
980 btrfs_free_path(path);
985 * find_free_dev_extent - find free space in the specified device
986 * @device: the device which we search the free space in
987 * @num_bytes: the size of the free space that we need
988 * @start: store the start of the free space.
989 * @len: the size of the free space. that we find, or the size of the max
990 * free space if we don't find suitable free space
992 * this uses a pretty simple search, the expectation is that it is
993 * called very infrequently and that a given device has a small number
996 * @start is used to store the start of the free space if we find. But if we
997 * don't find suitable free space, it will be used to store the start position
998 * of the max free space.
1000 * @len is used to store the size of the free space that we find.
1001 * But if we don't find suitable free space, it is used to store the size of
1002 * the max free space.
1004 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
1005 u64 *start, u64 *len)
1007 struct btrfs_key key;
1008 struct btrfs_root *root = device->dev_root;
1009 struct btrfs_dev_extent *dev_extent;
1010 struct btrfs_path *path;
1016 u64 search_end = device->total_bytes;
1019 struct extent_buffer *l;
1021 /* FIXME use last free of some kind */
1023 /* we don't want to overwrite the superblock on the drive,
1024 * so we make sure to start at an offset of at least 1MB
1026 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1028 max_hole_start = search_start;
1032 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1037 path = btrfs_alloc_path();
1044 key.objectid = device->devid;
1045 key.offset = search_start;
1046 key.type = BTRFS_DEV_EXTENT_KEY;
1048 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1052 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1059 slot = path->slots[0];
1060 if (slot >= btrfs_header_nritems(l)) {
1061 ret = btrfs_next_leaf(root, path);
1069 btrfs_item_key_to_cpu(l, &key, slot);
1071 if (key.objectid < device->devid)
1074 if (key.objectid > device->devid)
1077 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1080 if (key.offset > search_start) {
1081 hole_size = key.offset - search_start;
1083 if (hole_size > max_hole_size) {
1084 max_hole_start = search_start;
1085 max_hole_size = hole_size;
1089 * If this free space is greater than which we need,
1090 * it must be the max free space that we have found
1091 * until now, so max_hole_start must point to the start
1092 * of this free space and the length of this free space
1093 * is stored in max_hole_size. Thus, we return
1094 * max_hole_start and max_hole_size and go back to the
1097 if (hole_size >= num_bytes) {
1103 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1104 extent_end = key.offset + btrfs_dev_extent_length(l,
1106 if (extent_end > search_start)
1107 search_start = extent_end;
1114 * At this point, search_start should be the end of
1115 * allocated dev extents, and when shrinking the device,
1116 * search_end may be smaller than search_start.
1118 if (search_end > search_start)
1119 hole_size = search_end - search_start;
1121 if (hole_size > max_hole_size) {
1122 max_hole_start = search_start;
1123 max_hole_size = hole_size;
1127 if (hole_size < num_bytes)
1133 btrfs_free_path(path);
1135 *start = max_hole_start;
1137 *len = max_hole_size;
1141 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1142 struct btrfs_device *device,
1146 struct btrfs_path *path;
1147 struct btrfs_root *root = device->dev_root;
1148 struct btrfs_key key;
1149 struct btrfs_key found_key;
1150 struct extent_buffer *leaf = NULL;
1151 struct btrfs_dev_extent *extent = NULL;
1153 path = btrfs_alloc_path();
1157 key.objectid = device->devid;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
1161 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1163 ret = btrfs_previous_item(root, path, key.objectid,
1164 BTRFS_DEV_EXTENT_KEY);
1167 leaf = path->nodes[0];
1168 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1169 extent = btrfs_item_ptr(leaf, path->slots[0],
1170 struct btrfs_dev_extent);
1171 BUG_ON(found_key.offset > start || found_key.offset +
1172 btrfs_dev_extent_length(leaf, extent) < start);
1174 btrfs_release_path(path);
1176 } else if (ret == 0) {
1177 leaf = path->nodes[0];
1178 extent = btrfs_item_ptr(leaf, path->slots[0],
1179 struct btrfs_dev_extent);
1181 btrfs_error(root->fs_info, ret, "Slot search failed");
1185 if (device->bytes_used > 0) {
1186 u64 len = btrfs_dev_extent_length(leaf, extent);
1187 device->bytes_used -= len;
1188 spin_lock(&root->fs_info->free_chunk_lock);
1189 root->fs_info->free_chunk_space += len;
1190 spin_unlock(&root->fs_info->free_chunk_lock);
1192 ret = btrfs_del_item(trans, root, path);
1194 btrfs_error(root->fs_info, ret,
1195 "Failed to remove dev extent item");
1198 btrfs_free_path(path);
1202 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1203 struct btrfs_device *device,
1204 u64 chunk_tree, u64 chunk_objectid,
1205 u64 chunk_offset, u64 start, u64 num_bytes)
1208 struct btrfs_path *path;
1209 struct btrfs_root *root = device->dev_root;
1210 struct btrfs_dev_extent *extent;
1211 struct extent_buffer *leaf;
1212 struct btrfs_key key;
1214 WARN_ON(!device->in_fs_metadata);
1215 WARN_ON(device->is_tgtdev_for_dev_replace);
1216 path = btrfs_alloc_path();
1220 key.objectid = device->devid;
1222 key.type = BTRFS_DEV_EXTENT_KEY;
1223 ret = btrfs_insert_empty_item(trans, root, path, &key,
1228 leaf = path->nodes[0];
1229 extent = btrfs_item_ptr(leaf, path->slots[0],
1230 struct btrfs_dev_extent);
1231 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1232 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1233 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1235 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1236 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1239 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1240 btrfs_mark_buffer_dirty(leaf);
1242 btrfs_free_path(path);
1246 static noinline int find_next_chunk(struct btrfs_root *root,
1247 u64 objectid, u64 *offset)
1249 struct btrfs_path *path;
1251 struct btrfs_key key;
1252 struct btrfs_chunk *chunk;
1253 struct btrfs_key found_key;
1255 path = btrfs_alloc_path();
1259 key.objectid = objectid;
1260 key.offset = (u64)-1;
1261 key.type = BTRFS_CHUNK_ITEM_KEY;
1263 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1267 BUG_ON(ret == 0); /* Corruption */
1269 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1273 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1275 if (found_key.objectid != objectid)
1278 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1279 struct btrfs_chunk);
1280 *offset = found_key.offset +
1281 btrfs_chunk_length(path->nodes[0], chunk);
1286 btrfs_free_path(path);
1290 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1293 struct btrfs_key key;
1294 struct btrfs_key found_key;
1295 struct btrfs_path *path;
1297 root = root->fs_info->chunk_root;
1299 path = btrfs_alloc_path();
1303 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1304 key.type = BTRFS_DEV_ITEM_KEY;
1305 key.offset = (u64)-1;
1307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1311 BUG_ON(ret == 0); /* Corruption */
1313 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1314 BTRFS_DEV_ITEM_KEY);
1318 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1320 *objectid = found_key.offset + 1;
1324 btrfs_free_path(path);
1329 * the device information is stored in the chunk root
1330 * the btrfs_device struct should be fully filled in
1332 int btrfs_add_device(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root,
1334 struct btrfs_device *device)
1337 struct btrfs_path *path;
1338 struct btrfs_dev_item *dev_item;
1339 struct extent_buffer *leaf;
1340 struct btrfs_key key;
1343 root = root->fs_info->chunk_root;
1345 path = btrfs_alloc_path();
1349 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1350 key.type = BTRFS_DEV_ITEM_KEY;
1351 key.offset = device->devid;
1353 ret = btrfs_insert_empty_item(trans, root, path, &key,
1358 leaf = path->nodes[0];
1359 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1361 btrfs_set_device_id(leaf, dev_item, device->devid);
1362 btrfs_set_device_generation(leaf, dev_item, 0);
1363 btrfs_set_device_type(leaf, dev_item, device->type);
1364 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1365 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1366 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1367 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1368 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1369 btrfs_set_device_group(leaf, dev_item, 0);
1370 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1371 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1372 btrfs_set_device_start_offset(leaf, dev_item, 0);
1374 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1375 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1376 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1377 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1378 btrfs_mark_buffer_dirty(leaf);
1382 btrfs_free_path(path);
1386 static int btrfs_rm_dev_item(struct btrfs_root *root,
1387 struct btrfs_device *device)
1390 struct btrfs_path *path;
1391 struct btrfs_key key;
1392 struct btrfs_trans_handle *trans;
1394 root = root->fs_info->chunk_root;
1396 path = btrfs_alloc_path();
1400 trans = btrfs_start_transaction(root, 0);
1401 if (IS_ERR(trans)) {
1402 btrfs_free_path(path);
1403 return PTR_ERR(trans);
1405 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1406 key.type = BTRFS_DEV_ITEM_KEY;
1407 key.offset = device->devid;
1410 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1419 ret = btrfs_del_item(trans, root, path);
1423 btrfs_free_path(path);
1424 unlock_chunks(root);
1425 btrfs_commit_transaction(trans, root);
1429 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1431 struct btrfs_device *device;
1432 struct btrfs_device *next_device;
1433 struct block_device *bdev;
1434 struct buffer_head *bh = NULL;
1435 struct btrfs_super_block *disk_super;
1436 struct btrfs_fs_devices *cur_devices;
1443 bool clear_super = false;
1445 mutex_lock(&uuid_mutex);
1448 seq = read_seqbegin(&root->fs_info->profiles_lock);
1450 all_avail = root->fs_info->avail_data_alloc_bits |
1451 root->fs_info->avail_system_alloc_bits |
1452 root->fs_info->avail_metadata_alloc_bits;
1453 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1455 num_devices = root->fs_info->fs_devices->num_devices;
1456 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1457 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1458 WARN_ON(num_devices < 1);
1461 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1463 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1464 printk(KERN_ERR "btrfs: unable to go below four devices "
1470 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1471 printk(KERN_ERR "btrfs: unable to go below two "
1472 "devices on raid1\n");
1477 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1478 root->fs_info->fs_devices->rw_devices <= 2) {
1479 printk(KERN_ERR "btrfs: unable to go below two "
1480 "devices on raid5\n");
1484 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1485 root->fs_info->fs_devices->rw_devices <= 3) {
1486 printk(KERN_ERR "btrfs: unable to go below three "
1487 "devices on raid6\n");
1492 if (strcmp(device_path, "missing") == 0) {
1493 struct list_head *devices;
1494 struct btrfs_device *tmp;
1497 devices = &root->fs_info->fs_devices->devices;
1499 * It is safe to read the devices since the volume_mutex
1502 list_for_each_entry(tmp, devices, dev_list) {
1503 if (tmp->in_fs_metadata &&
1504 !tmp->is_tgtdev_for_dev_replace &&
1514 printk(KERN_ERR "btrfs: no missing devices found to "
1519 ret = btrfs_get_bdev_and_sb(device_path,
1520 FMODE_WRITE | FMODE_EXCL,
1521 root->fs_info->bdev_holder, 0,
1525 disk_super = (struct btrfs_super_block *)bh->b_data;
1526 devid = btrfs_stack_device_id(&disk_super->dev_item);
1527 dev_uuid = disk_super->dev_item.uuid;
1528 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1536 if (device->is_tgtdev_for_dev_replace) {
1537 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1542 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1543 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1549 if (device->writeable) {
1551 list_del_init(&device->dev_alloc_list);
1552 unlock_chunks(root);
1553 root->fs_info->fs_devices->rw_devices--;
1557 ret = btrfs_shrink_device(device, 0);
1562 * TODO: the superblock still includes this device in its num_devices
1563 * counter although write_all_supers() is not locked out. This
1564 * could give a filesystem state which requires a degraded mount.
1566 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1570 spin_lock(&root->fs_info->free_chunk_lock);
1571 root->fs_info->free_chunk_space = device->total_bytes -
1573 spin_unlock(&root->fs_info->free_chunk_lock);
1575 device->in_fs_metadata = 0;
1576 btrfs_scrub_cancel_dev(root->fs_info, device);
1579 * the device list mutex makes sure that we don't change
1580 * the device list while someone else is writing out all
1581 * the device supers.
1584 cur_devices = device->fs_devices;
1585 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1586 list_del_rcu(&device->dev_list);
1588 device->fs_devices->num_devices--;
1589 device->fs_devices->total_devices--;
1591 if (device->missing)
1592 root->fs_info->fs_devices->missing_devices--;
1594 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1595 struct btrfs_device, dev_list);
1596 if (device->bdev == root->fs_info->sb->s_bdev)
1597 root->fs_info->sb->s_bdev = next_device->bdev;
1598 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1599 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1602 device->fs_devices->open_devices--;
1604 call_rcu(&device->rcu, free_device);
1605 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1607 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1608 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1610 if (cur_devices->open_devices == 0) {
1611 struct btrfs_fs_devices *fs_devices;
1612 fs_devices = root->fs_info->fs_devices;
1613 while (fs_devices) {
1614 if (fs_devices->seed == cur_devices)
1616 fs_devices = fs_devices->seed;
1618 fs_devices->seed = cur_devices->seed;
1619 cur_devices->seed = NULL;
1621 __btrfs_close_devices(cur_devices);
1622 unlock_chunks(root);
1623 free_fs_devices(cur_devices);
1626 root->fs_info->num_tolerated_disk_barrier_failures =
1627 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1630 * at this point, the device is zero sized. We want to
1631 * remove it from the devices list and zero out the old super
1633 if (clear_super && disk_super) {
1634 /* make sure this device isn't detected as part of
1637 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1638 set_buffer_dirty(bh);
1639 sync_dirty_buffer(bh);
1644 /* Notify udev that device has changed */
1646 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1651 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1653 mutex_unlock(&uuid_mutex);
1656 if (device->writeable) {
1658 list_add(&device->dev_alloc_list,
1659 &root->fs_info->fs_devices->alloc_list);
1660 unlock_chunks(root);
1661 root->fs_info->fs_devices->rw_devices++;
1666 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1667 struct btrfs_device *srcdev)
1669 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1670 list_del_rcu(&srcdev->dev_list);
1671 list_del_rcu(&srcdev->dev_alloc_list);
1672 fs_info->fs_devices->num_devices--;
1673 if (srcdev->missing) {
1674 fs_info->fs_devices->missing_devices--;
1675 fs_info->fs_devices->rw_devices++;
1677 if (srcdev->can_discard)
1678 fs_info->fs_devices->num_can_discard--;
1680 fs_info->fs_devices->open_devices--;
1682 call_rcu(&srcdev->rcu, free_device);
1685 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1686 struct btrfs_device *tgtdev)
1688 struct btrfs_device *next_device;
1691 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1693 btrfs_scratch_superblock(tgtdev);
1694 fs_info->fs_devices->open_devices--;
1696 fs_info->fs_devices->num_devices--;
1697 if (tgtdev->can_discard)
1698 fs_info->fs_devices->num_can_discard++;
1700 next_device = list_entry(fs_info->fs_devices->devices.next,
1701 struct btrfs_device, dev_list);
1702 if (tgtdev->bdev == fs_info->sb->s_bdev)
1703 fs_info->sb->s_bdev = next_device->bdev;
1704 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1705 fs_info->fs_devices->latest_bdev = next_device->bdev;
1706 list_del_rcu(&tgtdev->dev_list);
1708 call_rcu(&tgtdev->rcu, free_device);
1710 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1713 int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1714 struct btrfs_device **device)
1717 struct btrfs_super_block *disk_super;
1720 struct block_device *bdev;
1721 struct buffer_head *bh;
1724 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1725 root->fs_info->bdev_holder, 0, &bdev, &bh);
1728 disk_super = (struct btrfs_super_block *)bh->b_data;
1729 devid = btrfs_stack_device_id(&disk_super->dev_item);
1730 dev_uuid = disk_super->dev_item.uuid;
1731 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1736 blkdev_put(bdev, FMODE_READ);
1740 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1742 struct btrfs_device **device)
1745 if (strcmp(device_path, "missing") == 0) {
1746 struct list_head *devices;
1747 struct btrfs_device *tmp;
1749 devices = &root->fs_info->fs_devices->devices;
1751 * It is safe to read the devices since the volume_mutex
1752 * is held by the caller.
1754 list_for_each_entry(tmp, devices, dev_list) {
1755 if (tmp->in_fs_metadata && !tmp->bdev) {
1762 pr_err("btrfs: no missing device found\n");
1768 return btrfs_find_device_by_path(root, device_path, device);
1773 * does all the dirty work required for changing file system's UUID.
1775 static int btrfs_prepare_sprout(struct btrfs_root *root)
1777 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1778 struct btrfs_fs_devices *old_devices;
1779 struct btrfs_fs_devices *seed_devices;
1780 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1781 struct btrfs_device *device;
1784 BUG_ON(!mutex_is_locked(&uuid_mutex));
1785 if (!fs_devices->seeding)
1788 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1792 old_devices = clone_fs_devices(fs_devices);
1793 if (IS_ERR(old_devices)) {
1794 kfree(seed_devices);
1795 return PTR_ERR(old_devices);
1798 list_add(&old_devices->list, &fs_uuids);
1800 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1801 seed_devices->opened = 1;
1802 INIT_LIST_HEAD(&seed_devices->devices);
1803 INIT_LIST_HEAD(&seed_devices->alloc_list);
1804 mutex_init(&seed_devices->device_list_mutex);
1806 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1807 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1809 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1811 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1812 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1813 device->fs_devices = seed_devices;
1816 fs_devices->seeding = 0;
1817 fs_devices->num_devices = 0;
1818 fs_devices->open_devices = 0;
1819 fs_devices->total_devices = 0;
1820 fs_devices->seed = seed_devices;
1822 generate_random_uuid(fs_devices->fsid);
1823 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1824 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1825 super_flags = btrfs_super_flags(disk_super) &
1826 ~BTRFS_SUPER_FLAG_SEEDING;
1827 btrfs_set_super_flags(disk_super, super_flags);
1833 * strore the expected generation for seed devices in device items.
1835 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1836 struct btrfs_root *root)
1838 struct btrfs_path *path;
1839 struct extent_buffer *leaf;
1840 struct btrfs_dev_item *dev_item;
1841 struct btrfs_device *device;
1842 struct btrfs_key key;
1843 u8 fs_uuid[BTRFS_UUID_SIZE];
1844 u8 dev_uuid[BTRFS_UUID_SIZE];
1848 path = btrfs_alloc_path();
1852 root = root->fs_info->chunk_root;
1853 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1855 key.type = BTRFS_DEV_ITEM_KEY;
1858 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1862 leaf = path->nodes[0];
1864 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1865 ret = btrfs_next_leaf(root, path);
1870 leaf = path->nodes[0];
1871 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1872 btrfs_release_path(path);
1876 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1877 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1878 key.type != BTRFS_DEV_ITEM_KEY)
1881 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1882 struct btrfs_dev_item);
1883 devid = btrfs_device_id(leaf, dev_item);
1884 read_extent_buffer(leaf, dev_uuid,
1885 (unsigned long)btrfs_device_uuid(dev_item),
1887 read_extent_buffer(leaf, fs_uuid,
1888 (unsigned long)btrfs_device_fsid(dev_item),
1890 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1892 BUG_ON(!device); /* Logic error */
1894 if (device->fs_devices->seeding) {
1895 btrfs_set_device_generation(leaf, dev_item,
1896 device->generation);
1897 btrfs_mark_buffer_dirty(leaf);
1905 btrfs_free_path(path);
1909 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1911 struct request_queue *q;
1912 struct btrfs_trans_handle *trans;
1913 struct btrfs_device *device;
1914 struct block_device *bdev;
1915 struct list_head *devices;
1916 struct super_block *sb = root->fs_info->sb;
1917 struct rcu_string *name;
1919 int seeding_dev = 0;
1922 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1925 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1926 root->fs_info->bdev_holder);
1928 return PTR_ERR(bdev);
1930 if (root->fs_info->fs_devices->seeding) {
1932 down_write(&sb->s_umount);
1933 mutex_lock(&uuid_mutex);
1936 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1938 devices = &root->fs_info->fs_devices->devices;
1940 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1941 list_for_each_entry(device, devices, dev_list) {
1942 if (device->bdev == bdev) {
1945 &root->fs_info->fs_devices->device_list_mutex);
1949 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1951 device = kzalloc(sizeof(*device), GFP_NOFS);
1953 /* we can safely leave the fs_devices entry around */
1958 name = rcu_string_strdup(device_path, GFP_NOFS);
1964 rcu_assign_pointer(device->name, name);
1966 ret = find_next_devid(root, &device->devid);
1968 rcu_string_free(device->name);
1973 trans = btrfs_start_transaction(root, 0);
1974 if (IS_ERR(trans)) {
1975 rcu_string_free(device->name);
1977 ret = PTR_ERR(trans);
1983 q = bdev_get_queue(bdev);
1984 if (blk_queue_discard(q))
1985 device->can_discard = 1;
1986 device->writeable = 1;
1987 device->work.func = pending_bios_fn;
1988 generate_random_uuid(device->uuid);
1989 spin_lock_init(&device->io_lock);
1990 device->generation = trans->transid;
1991 device->io_width = root->sectorsize;
1992 device->io_align = root->sectorsize;
1993 device->sector_size = root->sectorsize;
1994 device->total_bytes = i_size_read(bdev->bd_inode);
1995 device->disk_total_bytes = device->total_bytes;
1996 device->dev_root = root->fs_info->dev_root;
1997 device->bdev = bdev;
1998 device->in_fs_metadata = 1;
1999 device->is_tgtdev_for_dev_replace = 0;
2000 device->mode = FMODE_EXCL;
2001 set_blocksize(device->bdev, 4096);
2004 sb->s_flags &= ~MS_RDONLY;
2005 ret = btrfs_prepare_sprout(root);
2006 BUG_ON(ret); /* -ENOMEM */
2009 device->fs_devices = root->fs_info->fs_devices;
2011 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2012 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2013 list_add(&device->dev_alloc_list,
2014 &root->fs_info->fs_devices->alloc_list);
2015 root->fs_info->fs_devices->num_devices++;
2016 root->fs_info->fs_devices->open_devices++;
2017 root->fs_info->fs_devices->rw_devices++;
2018 root->fs_info->fs_devices->total_devices++;
2019 if (device->can_discard)
2020 root->fs_info->fs_devices->num_can_discard++;
2021 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2023 spin_lock(&root->fs_info->free_chunk_lock);
2024 root->fs_info->free_chunk_space += device->total_bytes;
2025 spin_unlock(&root->fs_info->free_chunk_lock);
2027 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2028 root->fs_info->fs_devices->rotating = 1;
2030 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2031 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2032 total_bytes + device->total_bytes);
2034 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2035 btrfs_set_super_num_devices(root->fs_info->super_copy,
2037 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2040 ret = init_first_rw_device(trans, root, device);
2042 btrfs_abort_transaction(trans, root, ret);
2045 ret = btrfs_finish_sprout(trans, root);
2047 btrfs_abort_transaction(trans, root, ret);
2051 ret = btrfs_add_device(trans, root, device);
2053 btrfs_abort_transaction(trans, root, ret);
2059 * we've got more storage, clear any full flags on the space
2062 btrfs_clear_space_info_full(root->fs_info);
2064 unlock_chunks(root);
2065 root->fs_info->num_tolerated_disk_barrier_failures =
2066 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2067 ret = btrfs_commit_transaction(trans, root);
2070 mutex_unlock(&uuid_mutex);
2071 up_write(&sb->s_umount);
2073 if (ret) /* transaction commit */
2076 ret = btrfs_relocate_sys_chunks(root);
2078 btrfs_error(root->fs_info, ret,
2079 "Failed to relocate sys chunks after "
2080 "device initialization. This can be fixed "
2081 "using the \"btrfs balance\" command.");
2082 trans = btrfs_attach_transaction(root);
2083 if (IS_ERR(trans)) {
2084 if (PTR_ERR(trans) == -ENOENT)
2086 return PTR_ERR(trans);
2088 ret = btrfs_commit_transaction(trans, root);
2094 unlock_chunks(root);
2095 btrfs_end_transaction(trans, root);
2096 rcu_string_free(device->name);
2099 blkdev_put(bdev, FMODE_EXCL);
2101 mutex_unlock(&uuid_mutex);
2102 up_write(&sb->s_umount);
2107 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2108 struct btrfs_device **device_out)
2110 struct request_queue *q;
2111 struct btrfs_device *device;
2112 struct block_device *bdev;
2113 struct btrfs_fs_info *fs_info = root->fs_info;
2114 struct list_head *devices;
2115 struct rcu_string *name;
2119 if (fs_info->fs_devices->seeding)
2122 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2123 fs_info->bdev_holder);
2125 return PTR_ERR(bdev);
2127 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2129 devices = &fs_info->fs_devices->devices;
2130 list_for_each_entry(device, devices, dev_list) {
2131 if (device->bdev == bdev) {
2137 device = kzalloc(sizeof(*device), GFP_NOFS);
2143 name = rcu_string_strdup(device_path, GFP_NOFS);
2149 rcu_assign_pointer(device->name, name);
2151 q = bdev_get_queue(bdev);
2152 if (blk_queue_discard(q))
2153 device->can_discard = 1;
2154 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2155 device->writeable = 1;
2156 device->work.func = pending_bios_fn;
2157 generate_random_uuid(device->uuid);
2158 device->devid = BTRFS_DEV_REPLACE_DEVID;
2159 spin_lock_init(&device->io_lock);
2160 device->generation = 0;
2161 device->io_width = root->sectorsize;
2162 device->io_align = root->sectorsize;
2163 device->sector_size = root->sectorsize;
2164 device->total_bytes = i_size_read(bdev->bd_inode);
2165 device->disk_total_bytes = device->total_bytes;
2166 device->dev_root = fs_info->dev_root;
2167 device->bdev = bdev;
2168 device->in_fs_metadata = 1;
2169 device->is_tgtdev_for_dev_replace = 1;
2170 device->mode = FMODE_EXCL;
2171 set_blocksize(device->bdev, 4096);
2172 device->fs_devices = fs_info->fs_devices;
2173 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2174 fs_info->fs_devices->num_devices++;
2175 fs_info->fs_devices->open_devices++;
2176 if (device->can_discard)
2177 fs_info->fs_devices->num_can_discard++;
2178 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2180 *device_out = device;
2184 blkdev_put(bdev, FMODE_EXCL);
2188 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2189 struct btrfs_device *tgtdev)
2191 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2192 tgtdev->io_width = fs_info->dev_root->sectorsize;
2193 tgtdev->io_align = fs_info->dev_root->sectorsize;
2194 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2195 tgtdev->dev_root = fs_info->dev_root;
2196 tgtdev->in_fs_metadata = 1;
2199 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2200 struct btrfs_device *device)
2203 struct btrfs_path *path;
2204 struct btrfs_root *root;
2205 struct btrfs_dev_item *dev_item;
2206 struct extent_buffer *leaf;
2207 struct btrfs_key key;
2209 root = device->dev_root->fs_info->chunk_root;
2211 path = btrfs_alloc_path();
2215 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2216 key.type = BTRFS_DEV_ITEM_KEY;
2217 key.offset = device->devid;
2219 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2228 leaf = path->nodes[0];
2229 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2231 btrfs_set_device_id(leaf, dev_item, device->devid);
2232 btrfs_set_device_type(leaf, dev_item, device->type);
2233 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2234 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2235 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2236 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
2237 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2238 btrfs_mark_buffer_dirty(leaf);
2241 btrfs_free_path(path);
2245 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
2246 struct btrfs_device *device, u64 new_size)
2248 struct btrfs_super_block *super_copy =
2249 device->dev_root->fs_info->super_copy;
2250 u64 old_total = btrfs_super_total_bytes(super_copy);
2251 u64 diff = new_size - device->total_bytes;
2253 if (!device->writeable)
2255 if (new_size <= device->total_bytes ||
2256 device->is_tgtdev_for_dev_replace)
2259 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2260 device->fs_devices->total_rw_bytes += diff;
2262 device->total_bytes = new_size;
2263 device->disk_total_bytes = new_size;
2264 btrfs_clear_space_info_full(device->dev_root->fs_info);
2266 return btrfs_update_device(trans, device);
2269 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2270 struct btrfs_device *device, u64 new_size)
2273 lock_chunks(device->dev_root);
2274 ret = __btrfs_grow_device(trans, device, new_size);
2275 unlock_chunks(device->dev_root);
2279 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2280 struct btrfs_root *root,
2281 u64 chunk_tree, u64 chunk_objectid,
2285 struct btrfs_path *path;
2286 struct btrfs_key key;
2288 root = root->fs_info->chunk_root;
2289 path = btrfs_alloc_path();
2293 key.objectid = chunk_objectid;
2294 key.offset = chunk_offset;
2295 key.type = BTRFS_CHUNK_ITEM_KEY;
2297 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2300 else if (ret > 0) { /* Logic error or corruption */
2301 btrfs_error(root->fs_info, -ENOENT,
2302 "Failed lookup while freeing chunk.");
2307 ret = btrfs_del_item(trans, root, path);
2309 btrfs_error(root->fs_info, ret,
2310 "Failed to delete chunk item.");
2312 btrfs_free_path(path);
2316 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2319 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2320 struct btrfs_disk_key *disk_key;
2321 struct btrfs_chunk *chunk;
2328 struct btrfs_key key;
2330 array_size = btrfs_super_sys_array_size(super_copy);
2332 ptr = super_copy->sys_chunk_array;
2335 while (cur < array_size) {
2336 disk_key = (struct btrfs_disk_key *)ptr;
2337 btrfs_disk_key_to_cpu(&key, disk_key);
2339 len = sizeof(*disk_key);
2341 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2342 chunk = (struct btrfs_chunk *)(ptr + len);
2343 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2344 len += btrfs_chunk_item_size(num_stripes);
2349 if (key.objectid == chunk_objectid &&
2350 key.offset == chunk_offset) {
2351 memmove(ptr, ptr + len, array_size - (cur + len));
2353 btrfs_set_super_sys_array_size(super_copy, array_size);
2362 static int btrfs_relocate_chunk(struct btrfs_root *root,
2363 u64 chunk_tree, u64 chunk_objectid,
2366 struct extent_map_tree *em_tree;
2367 struct btrfs_root *extent_root;
2368 struct btrfs_trans_handle *trans;
2369 struct extent_map *em;
2370 struct map_lookup *map;
2374 root = root->fs_info->chunk_root;
2375 extent_root = root->fs_info->extent_root;
2376 em_tree = &root->fs_info->mapping_tree.map_tree;
2378 ret = btrfs_can_relocate(extent_root, chunk_offset);
2382 /* step one, relocate all the extents inside this chunk */
2383 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2387 trans = btrfs_start_transaction(root, 0);
2388 if (IS_ERR(trans)) {
2389 ret = PTR_ERR(trans);
2390 btrfs_std_error(root->fs_info, ret);
2397 * step two, delete the device extents and the
2398 * chunk tree entries
2400 read_lock(&em_tree->lock);
2401 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2402 read_unlock(&em_tree->lock);
2404 BUG_ON(!em || em->start > chunk_offset ||
2405 em->start + em->len < chunk_offset);
2406 map = (struct map_lookup *)em->bdev;
2408 for (i = 0; i < map->num_stripes; i++) {
2409 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2410 map->stripes[i].physical);
2413 if (map->stripes[i].dev) {
2414 ret = btrfs_update_device(trans, map->stripes[i].dev);
2418 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2423 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2425 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2426 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2430 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2433 write_lock(&em_tree->lock);
2434 remove_extent_mapping(em_tree, em);
2435 write_unlock(&em_tree->lock);
2440 /* once for the tree */
2441 free_extent_map(em);
2443 free_extent_map(em);
2445 unlock_chunks(root);
2446 btrfs_end_transaction(trans, root);
2450 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2452 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2453 struct btrfs_path *path;
2454 struct extent_buffer *leaf;
2455 struct btrfs_chunk *chunk;
2456 struct btrfs_key key;
2457 struct btrfs_key found_key;
2458 u64 chunk_tree = chunk_root->root_key.objectid;
2460 bool retried = false;
2464 path = btrfs_alloc_path();
2469 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2470 key.offset = (u64)-1;
2471 key.type = BTRFS_CHUNK_ITEM_KEY;
2474 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2477 BUG_ON(ret == 0); /* Corruption */
2479 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2486 leaf = path->nodes[0];
2487 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2489 chunk = btrfs_item_ptr(leaf, path->slots[0],
2490 struct btrfs_chunk);
2491 chunk_type = btrfs_chunk_type(leaf, chunk);
2492 btrfs_release_path(path);
2494 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2495 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2504 if (found_key.offset == 0)
2506 key.offset = found_key.offset - 1;
2509 if (failed && !retried) {
2513 } else if (failed && retried) {
2518 btrfs_free_path(path);
2522 static int insert_balance_item(struct btrfs_root *root,
2523 struct btrfs_balance_control *bctl)
2525 struct btrfs_trans_handle *trans;
2526 struct btrfs_balance_item *item;
2527 struct btrfs_disk_balance_args disk_bargs;
2528 struct btrfs_path *path;
2529 struct extent_buffer *leaf;
2530 struct btrfs_key key;
2533 path = btrfs_alloc_path();
2537 trans = btrfs_start_transaction(root, 0);
2538 if (IS_ERR(trans)) {
2539 btrfs_free_path(path);
2540 return PTR_ERR(trans);
2543 key.objectid = BTRFS_BALANCE_OBJECTID;
2544 key.type = BTRFS_BALANCE_ITEM_KEY;
2547 ret = btrfs_insert_empty_item(trans, root, path, &key,
2552 leaf = path->nodes[0];
2553 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2555 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2557 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2558 btrfs_set_balance_data(leaf, item, &disk_bargs);
2559 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2560 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2561 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2562 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2564 btrfs_set_balance_flags(leaf, item, bctl->flags);
2566 btrfs_mark_buffer_dirty(leaf);
2568 btrfs_free_path(path);
2569 err = btrfs_commit_transaction(trans, root);
2575 static int del_balance_item(struct btrfs_root *root)
2577 struct btrfs_trans_handle *trans;
2578 struct btrfs_path *path;
2579 struct btrfs_key key;
2582 path = btrfs_alloc_path();
2586 trans = btrfs_start_transaction(root, 0);
2587 if (IS_ERR(trans)) {
2588 btrfs_free_path(path);
2589 return PTR_ERR(trans);
2592 key.objectid = BTRFS_BALANCE_OBJECTID;
2593 key.type = BTRFS_BALANCE_ITEM_KEY;
2596 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2604 ret = btrfs_del_item(trans, root, path);
2606 btrfs_free_path(path);
2607 err = btrfs_commit_transaction(trans, root);
2614 * This is a heuristic used to reduce the number of chunks balanced on
2615 * resume after balance was interrupted.
2617 static void update_balance_args(struct btrfs_balance_control *bctl)
2620 * Turn on soft mode for chunk types that were being converted.
2622 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2623 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2624 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2625 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2626 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2627 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2630 * Turn on usage filter if is not already used. The idea is
2631 * that chunks that we have already balanced should be
2632 * reasonably full. Don't do it for chunks that are being
2633 * converted - that will keep us from relocating unconverted
2634 * (albeit full) chunks.
2636 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2637 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2638 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2639 bctl->data.usage = 90;
2641 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2642 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2643 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2644 bctl->sys.usage = 90;
2646 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2647 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2648 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2649 bctl->meta.usage = 90;
2654 * Should be called with both balance and volume mutexes held to
2655 * serialize other volume operations (add_dev/rm_dev/resize) with
2656 * restriper. Same goes for unset_balance_control.
2658 static void set_balance_control(struct btrfs_balance_control *bctl)
2660 struct btrfs_fs_info *fs_info = bctl->fs_info;
2662 BUG_ON(fs_info->balance_ctl);
2664 spin_lock(&fs_info->balance_lock);
2665 fs_info->balance_ctl = bctl;
2666 spin_unlock(&fs_info->balance_lock);
2669 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2671 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2673 BUG_ON(!fs_info->balance_ctl);
2675 spin_lock(&fs_info->balance_lock);
2676 fs_info->balance_ctl = NULL;
2677 spin_unlock(&fs_info->balance_lock);
2683 * Balance filters. Return 1 if chunk should be filtered out
2684 * (should not be balanced).
2686 static int chunk_profiles_filter(u64 chunk_type,
2687 struct btrfs_balance_args *bargs)
2689 chunk_type = chunk_to_extended(chunk_type) &
2690 BTRFS_EXTENDED_PROFILE_MASK;
2692 if (bargs->profiles & chunk_type)
2698 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2699 struct btrfs_balance_args *bargs)
2701 struct btrfs_block_group_cache *cache;
2702 u64 chunk_used, user_thresh;
2705 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2706 chunk_used = btrfs_block_group_used(&cache->item);
2708 if (bargs->usage == 0)
2710 else if (bargs->usage > 100)
2711 user_thresh = cache->key.offset;
2713 user_thresh = div_factor_fine(cache->key.offset,
2716 if (chunk_used < user_thresh)
2719 btrfs_put_block_group(cache);
2723 static int chunk_devid_filter(struct extent_buffer *leaf,
2724 struct btrfs_chunk *chunk,
2725 struct btrfs_balance_args *bargs)
2727 struct btrfs_stripe *stripe;
2728 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2731 for (i = 0; i < num_stripes; i++) {
2732 stripe = btrfs_stripe_nr(chunk, i);
2733 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2740 /* [pstart, pend) */
2741 static int chunk_drange_filter(struct extent_buffer *leaf,
2742 struct btrfs_chunk *chunk,
2744 struct btrfs_balance_args *bargs)
2746 struct btrfs_stripe *stripe;
2747 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2753 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2756 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2757 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2758 factor = num_stripes / 2;
2759 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2760 factor = num_stripes - 1;
2761 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2762 factor = num_stripes - 2;
2764 factor = num_stripes;
2767 for (i = 0; i < num_stripes; i++) {
2768 stripe = btrfs_stripe_nr(chunk, i);
2769 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2772 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2773 stripe_length = btrfs_chunk_length(leaf, chunk);
2774 do_div(stripe_length, factor);
2776 if (stripe_offset < bargs->pend &&
2777 stripe_offset + stripe_length > bargs->pstart)
2784 /* [vstart, vend) */
2785 static int chunk_vrange_filter(struct extent_buffer *leaf,
2786 struct btrfs_chunk *chunk,
2788 struct btrfs_balance_args *bargs)
2790 if (chunk_offset < bargs->vend &&
2791 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2792 /* at least part of the chunk is inside this vrange */
2798 static int chunk_soft_convert_filter(u64 chunk_type,
2799 struct btrfs_balance_args *bargs)
2801 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2804 chunk_type = chunk_to_extended(chunk_type) &
2805 BTRFS_EXTENDED_PROFILE_MASK;
2807 if (bargs->target == chunk_type)
2813 static int should_balance_chunk(struct btrfs_root *root,
2814 struct extent_buffer *leaf,
2815 struct btrfs_chunk *chunk, u64 chunk_offset)
2817 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2818 struct btrfs_balance_args *bargs = NULL;
2819 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2822 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2823 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2827 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2828 bargs = &bctl->data;
2829 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2831 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2832 bargs = &bctl->meta;
2834 /* profiles filter */
2835 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2836 chunk_profiles_filter(chunk_type, bargs)) {
2841 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2842 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2847 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2848 chunk_devid_filter(leaf, chunk, bargs)) {
2852 /* drange filter, makes sense only with devid filter */
2853 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2854 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2859 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2860 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2864 /* soft profile changing mode */
2865 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2866 chunk_soft_convert_filter(chunk_type, bargs)) {
2873 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
2875 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2876 struct btrfs_root *chunk_root = fs_info->chunk_root;
2877 struct btrfs_root *dev_root = fs_info->dev_root;
2878 struct list_head *devices;
2879 struct btrfs_device *device;
2882 struct btrfs_chunk *chunk;
2883 struct btrfs_path *path;
2884 struct btrfs_key key;
2885 struct btrfs_key found_key;
2886 struct btrfs_trans_handle *trans;
2887 struct extent_buffer *leaf;
2890 int enospc_errors = 0;
2891 bool counting = true;
2893 /* step one make some room on all the devices */
2894 devices = &fs_info->fs_devices->devices;
2895 list_for_each_entry(device, devices, dev_list) {
2896 old_size = device->total_bytes;
2897 size_to_free = div_factor(old_size, 1);
2898 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2899 if (!device->writeable ||
2900 device->total_bytes - device->bytes_used > size_to_free ||
2901 device->is_tgtdev_for_dev_replace)
2904 ret = btrfs_shrink_device(device, old_size - size_to_free);
2909 trans = btrfs_start_transaction(dev_root, 0);
2910 BUG_ON(IS_ERR(trans));
2912 ret = btrfs_grow_device(trans, device, old_size);
2915 btrfs_end_transaction(trans, dev_root);
2918 /* step two, relocate all the chunks */
2919 path = btrfs_alloc_path();
2925 /* zero out stat counters */
2926 spin_lock(&fs_info->balance_lock);
2927 memset(&bctl->stat, 0, sizeof(bctl->stat));
2928 spin_unlock(&fs_info->balance_lock);
2930 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2931 key.offset = (u64)-1;
2932 key.type = BTRFS_CHUNK_ITEM_KEY;
2935 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
2936 atomic_read(&fs_info->balance_cancel_req)) {
2941 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2946 * this shouldn't happen, it means the last relocate
2950 BUG(); /* FIXME break ? */
2952 ret = btrfs_previous_item(chunk_root, path, 0,
2953 BTRFS_CHUNK_ITEM_KEY);
2959 leaf = path->nodes[0];
2960 slot = path->slots[0];
2961 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2963 if (found_key.objectid != key.objectid)
2966 /* chunk zero is special */
2967 if (found_key.offset == 0)
2970 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2973 spin_lock(&fs_info->balance_lock);
2974 bctl->stat.considered++;
2975 spin_unlock(&fs_info->balance_lock);
2978 ret = should_balance_chunk(chunk_root, leaf, chunk,
2980 btrfs_release_path(path);
2985 spin_lock(&fs_info->balance_lock);
2986 bctl->stat.expected++;
2987 spin_unlock(&fs_info->balance_lock);
2991 ret = btrfs_relocate_chunk(chunk_root,
2992 chunk_root->root_key.objectid,
2995 if (ret && ret != -ENOSPC)
2997 if (ret == -ENOSPC) {
3000 spin_lock(&fs_info->balance_lock);
3001 bctl->stat.completed++;
3002 spin_unlock(&fs_info->balance_lock);
3005 key.offset = found_key.offset - 1;
3009 btrfs_release_path(path);
3014 btrfs_free_path(path);
3015 if (enospc_errors) {
3016 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3026 * alloc_profile_is_valid - see if a given profile is valid and reduced
3027 * @flags: profile to validate
3028 * @extended: if true @flags is treated as an extended profile
3030 static int alloc_profile_is_valid(u64 flags, int extended)
3032 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3033 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3035 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3037 /* 1) check that all other bits are zeroed */
3041 /* 2) see if profile is reduced */
3043 return !extended; /* "0" is valid for usual profiles */
3045 /* true if exactly one bit set */
3046 return (flags & (flags - 1)) == 0;
3049 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3051 /* cancel requested || normal exit path */
3052 return atomic_read(&fs_info->balance_cancel_req) ||
3053 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3054 atomic_read(&fs_info->balance_cancel_req) == 0);
3057 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3061 unset_balance_control(fs_info);
3062 ret = del_balance_item(fs_info->tree_root);
3064 btrfs_std_error(fs_info, ret);
3066 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3069 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3070 struct btrfs_ioctl_balance_args *bargs);
3073 * Should be called with both balance and volume mutexes held
3075 int btrfs_balance(struct btrfs_balance_control *bctl,
3076 struct btrfs_ioctl_balance_args *bargs)
3078 struct btrfs_fs_info *fs_info = bctl->fs_info;
3085 if (btrfs_fs_closing(fs_info) ||
3086 atomic_read(&fs_info->balance_pause_req) ||
3087 atomic_read(&fs_info->balance_cancel_req)) {
3092 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3093 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3097 * In case of mixed groups both data and meta should be picked,
3098 * and identical options should be given for both of them.
3100 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3101 if (mixed && (bctl->flags & allowed)) {
3102 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3103 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3104 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3105 printk(KERN_ERR "btrfs: with mixed groups data and "
3106 "metadata balance options must be the same\n");
3112 num_devices = fs_info->fs_devices->num_devices;
3113 btrfs_dev_replace_lock(&fs_info->dev_replace);
3114 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3115 BUG_ON(num_devices < 1);
3118 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3119 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3120 if (num_devices == 1)
3121 allowed |= BTRFS_BLOCK_GROUP_DUP;
3122 else if (num_devices < 4)
3123 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3125 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3126 BTRFS_BLOCK_GROUP_RAID10 |
3127 BTRFS_BLOCK_GROUP_RAID5 |
3128 BTRFS_BLOCK_GROUP_RAID6);
3130 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3131 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3132 (bctl->data.target & ~allowed))) {
3133 printk(KERN_ERR "btrfs: unable to start balance with target "
3134 "data profile %llu\n",
3135 (unsigned long long)bctl->data.target);
3139 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3140 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3141 (bctl->meta.target & ~allowed))) {
3142 printk(KERN_ERR "btrfs: unable to start balance with target "
3143 "metadata profile %llu\n",
3144 (unsigned long long)bctl->meta.target);
3148 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3149 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3150 (bctl->sys.target & ~allowed))) {
3151 printk(KERN_ERR "btrfs: unable to start balance with target "
3152 "system profile %llu\n",
3153 (unsigned long long)bctl->sys.target);
3158 /* allow dup'ed data chunks only in mixed mode */
3159 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3160 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3161 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3166 /* allow to reduce meta or sys integrity only if force set */
3167 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3168 BTRFS_BLOCK_GROUP_RAID10 |
3169 BTRFS_BLOCK_GROUP_RAID5 |
3170 BTRFS_BLOCK_GROUP_RAID6;
3172 seq = read_seqbegin(&fs_info->profiles_lock);
3174 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3175 (fs_info->avail_system_alloc_bits & allowed) &&
3176 !(bctl->sys.target & allowed)) ||
3177 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3178 (fs_info->avail_metadata_alloc_bits & allowed) &&
3179 !(bctl->meta.target & allowed))) {
3180 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3181 printk(KERN_INFO "btrfs: force reducing metadata "
3184 printk(KERN_ERR "btrfs: balance will reduce metadata "
3185 "integrity, use force if you want this\n");
3190 } while (read_seqretry(&fs_info->profiles_lock, seq));
3192 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3193 int num_tolerated_disk_barrier_failures;
3194 u64 target = bctl->sys.target;
3196 num_tolerated_disk_barrier_failures =
3197 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3198 if (num_tolerated_disk_barrier_failures > 0 &&
3200 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3201 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3202 num_tolerated_disk_barrier_failures = 0;
3203 else if (num_tolerated_disk_barrier_failures > 1 &&
3205 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3206 num_tolerated_disk_barrier_failures = 1;
3208 fs_info->num_tolerated_disk_barrier_failures =
3209 num_tolerated_disk_barrier_failures;
3212 ret = insert_balance_item(fs_info->tree_root, bctl);
3213 if (ret && ret != -EEXIST)
3216 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3217 BUG_ON(ret == -EEXIST);
3218 set_balance_control(bctl);
3220 BUG_ON(ret != -EEXIST);
3221 spin_lock(&fs_info->balance_lock);
3222 update_balance_args(bctl);
3223 spin_unlock(&fs_info->balance_lock);
3226 atomic_inc(&fs_info->balance_running);
3227 mutex_unlock(&fs_info->balance_mutex);
3229 ret = __btrfs_balance(fs_info);
3231 mutex_lock(&fs_info->balance_mutex);
3232 atomic_dec(&fs_info->balance_running);
3234 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3235 fs_info->num_tolerated_disk_barrier_failures =
3236 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3240 memset(bargs, 0, sizeof(*bargs));
3241 update_ioctl_balance_args(fs_info, 0, bargs);
3244 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3245 balance_need_close(fs_info)) {
3246 __cancel_balance(fs_info);
3249 wake_up(&fs_info->balance_wait_q);
3253 if (bctl->flags & BTRFS_BALANCE_RESUME)
3254 __cancel_balance(fs_info);
3257 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3262 static int balance_kthread(void *data)
3264 struct btrfs_fs_info *fs_info = data;
3267 mutex_lock(&fs_info->volume_mutex);
3268 mutex_lock(&fs_info->balance_mutex);
3270 if (fs_info->balance_ctl) {
3271 printk(KERN_INFO "btrfs: continuing balance\n");
3272 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3275 mutex_unlock(&fs_info->balance_mutex);
3276 mutex_unlock(&fs_info->volume_mutex);
3281 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3283 struct task_struct *tsk;
3285 spin_lock(&fs_info->balance_lock);
3286 if (!fs_info->balance_ctl) {
3287 spin_unlock(&fs_info->balance_lock);
3290 spin_unlock(&fs_info->balance_lock);
3292 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3293 printk(KERN_INFO "btrfs: force skipping balance\n");
3297 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3299 return PTR_ERR(tsk);
3304 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3306 struct btrfs_balance_control *bctl;
3307 struct btrfs_balance_item *item;
3308 struct btrfs_disk_balance_args disk_bargs;
3309 struct btrfs_path *path;
3310 struct extent_buffer *leaf;
3311 struct btrfs_key key;
3314 path = btrfs_alloc_path();
3318 key.objectid = BTRFS_BALANCE_OBJECTID;
3319 key.type = BTRFS_BALANCE_ITEM_KEY;
3322 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3325 if (ret > 0) { /* ret = -ENOENT; */
3330 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3336 leaf = path->nodes[0];
3337 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3339 bctl->fs_info = fs_info;
3340 bctl->flags = btrfs_balance_flags(leaf, item);
3341 bctl->flags |= BTRFS_BALANCE_RESUME;
3343 btrfs_balance_data(leaf, item, &disk_bargs);
3344 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3345 btrfs_balance_meta(leaf, item, &disk_bargs);
3346 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3347 btrfs_balance_sys(leaf, item, &disk_bargs);
3348 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3350 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3352 mutex_lock(&fs_info->volume_mutex);
3353 mutex_lock(&fs_info->balance_mutex);
3355 set_balance_control(bctl);
3357 mutex_unlock(&fs_info->balance_mutex);
3358 mutex_unlock(&fs_info->volume_mutex);
3360 btrfs_free_path(path);
3364 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3368 mutex_lock(&fs_info->balance_mutex);
3369 if (!fs_info->balance_ctl) {
3370 mutex_unlock(&fs_info->balance_mutex);
3374 if (atomic_read(&fs_info->balance_running)) {
3375 atomic_inc(&fs_info->balance_pause_req);
3376 mutex_unlock(&fs_info->balance_mutex);
3378 wait_event(fs_info->balance_wait_q,
3379 atomic_read(&fs_info->balance_running) == 0);
3381 mutex_lock(&fs_info->balance_mutex);
3382 /* we are good with balance_ctl ripped off from under us */
3383 BUG_ON(atomic_read(&fs_info->balance_running));
3384 atomic_dec(&fs_info->balance_pause_req);
3389 mutex_unlock(&fs_info->balance_mutex);
3393 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3395 mutex_lock(&fs_info->balance_mutex);
3396 if (!fs_info->balance_ctl) {
3397 mutex_unlock(&fs_info->balance_mutex);
3401 atomic_inc(&fs_info->balance_cancel_req);
3403 * if we are running just wait and return, balance item is
3404 * deleted in btrfs_balance in this case
3406 if (atomic_read(&fs_info->balance_running)) {
3407 mutex_unlock(&fs_info->balance_mutex);
3408 wait_event(fs_info->balance_wait_q,
3409 atomic_read(&fs_info->balance_running) == 0);
3410 mutex_lock(&fs_info->balance_mutex);
3412 /* __cancel_balance needs volume_mutex */
3413 mutex_unlock(&fs_info->balance_mutex);
3414 mutex_lock(&fs_info->volume_mutex);
3415 mutex_lock(&fs_info->balance_mutex);
3417 if (fs_info->balance_ctl)
3418 __cancel_balance(fs_info);
3420 mutex_unlock(&fs_info->volume_mutex);
3423 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3424 atomic_dec(&fs_info->balance_cancel_req);
3425 mutex_unlock(&fs_info->balance_mutex);
3430 * shrinking a device means finding all of the device extents past
3431 * the new size, and then following the back refs to the chunks.
3432 * The chunk relocation code actually frees the device extent
3434 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3436 struct btrfs_trans_handle *trans;
3437 struct btrfs_root *root = device->dev_root;
3438 struct btrfs_dev_extent *dev_extent = NULL;
3439 struct btrfs_path *path;
3447 bool retried = false;
3448 struct extent_buffer *l;
3449 struct btrfs_key key;
3450 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3451 u64 old_total = btrfs_super_total_bytes(super_copy);
3452 u64 old_size = device->total_bytes;
3453 u64 diff = device->total_bytes - new_size;
3455 if (device->is_tgtdev_for_dev_replace)
3458 path = btrfs_alloc_path();
3466 device->total_bytes = new_size;
3467 if (device->writeable) {
3468 device->fs_devices->total_rw_bytes -= diff;
3469 spin_lock(&root->fs_info->free_chunk_lock);
3470 root->fs_info->free_chunk_space -= diff;
3471 spin_unlock(&root->fs_info->free_chunk_lock);
3473 unlock_chunks(root);
3476 key.objectid = device->devid;
3477 key.offset = (u64)-1;
3478 key.type = BTRFS_DEV_EXTENT_KEY;
3481 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3485 ret = btrfs_previous_item(root, path, 0, key.type);
3490 btrfs_release_path(path);
3495 slot = path->slots[0];
3496 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3498 if (key.objectid != device->devid) {
3499 btrfs_release_path(path);
3503 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3504 length = btrfs_dev_extent_length(l, dev_extent);
3506 if (key.offset + length <= new_size) {
3507 btrfs_release_path(path);
3511 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3512 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3513 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3514 btrfs_release_path(path);
3516 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3518 if (ret && ret != -ENOSPC)
3522 } while (key.offset-- > 0);
3524 if (failed && !retried) {
3528 } else if (failed && retried) {
3532 device->total_bytes = old_size;
3533 if (device->writeable)
3534 device->fs_devices->total_rw_bytes += diff;
3535 spin_lock(&root->fs_info->free_chunk_lock);
3536 root->fs_info->free_chunk_space += diff;
3537 spin_unlock(&root->fs_info->free_chunk_lock);
3538 unlock_chunks(root);
3542 /* Shrinking succeeded, else we would be at "done". */
3543 trans = btrfs_start_transaction(root, 0);
3544 if (IS_ERR(trans)) {
3545 ret = PTR_ERR(trans);
3551 device->disk_total_bytes = new_size;
3552 /* Now btrfs_update_device() will change the on-disk size. */
3553 ret = btrfs_update_device(trans, device);
3555 unlock_chunks(root);
3556 btrfs_end_transaction(trans, root);
3559 WARN_ON(diff > old_total);
3560 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3561 unlock_chunks(root);
3562 btrfs_end_transaction(trans, root);
3564 btrfs_free_path(path);
3568 static int btrfs_add_system_chunk(struct btrfs_root *root,
3569 struct btrfs_key *key,
3570 struct btrfs_chunk *chunk, int item_size)
3572 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3573 struct btrfs_disk_key disk_key;
3577 array_size = btrfs_super_sys_array_size(super_copy);
3578 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3581 ptr = super_copy->sys_chunk_array + array_size;
3582 btrfs_cpu_key_to_disk(&disk_key, key);
3583 memcpy(ptr, &disk_key, sizeof(disk_key));
3584 ptr += sizeof(disk_key);
3585 memcpy(ptr, chunk, item_size);
3586 item_size += sizeof(disk_key);
3587 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3592 * sort the devices in descending order by max_avail, total_avail
3594 static int btrfs_cmp_device_info(const void *a, const void *b)
3596 const struct btrfs_device_info *di_a = a;
3597 const struct btrfs_device_info *di_b = b;
3599 if (di_a->max_avail > di_b->max_avail)
3601 if (di_a->max_avail < di_b->max_avail)
3603 if (di_a->total_avail > di_b->total_avail)
3605 if (di_a->total_avail < di_b->total_avail)
3610 struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
3611 [BTRFS_RAID_RAID10] = {
3614 .devs_max = 0, /* 0 == as many as possible */
3616 .devs_increment = 2,
3619 [BTRFS_RAID_RAID1] = {
3624 .devs_increment = 2,
3627 [BTRFS_RAID_DUP] = {
3632 .devs_increment = 1,
3635 [BTRFS_RAID_RAID0] = {
3640 .devs_increment = 1,
3643 [BTRFS_RAID_SINGLE] = {
3648 .devs_increment = 1,
3651 [BTRFS_RAID_RAID5] = {
3656 .devs_increment = 1,
3659 [BTRFS_RAID_RAID6] = {
3664 .devs_increment = 1,
3669 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3671 /* TODO allow them to set a preferred stripe size */
3675 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3679 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3682 features = btrfs_super_incompat_flags(info->super_copy);
3683 if (features & BTRFS_FEATURE_INCOMPAT_RAID56)
3686 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
3687 btrfs_set_super_incompat_flags(info->super_copy, features);
3688 printk(KERN_INFO "btrfs: setting RAID5/6 feature flag\n");
3691 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3692 struct btrfs_root *extent_root,
3693 struct map_lookup **map_ret,
3694 u64 *num_bytes_out, u64 *stripe_size_out,
3695 u64 start, u64 type)
3697 struct btrfs_fs_info *info = extent_root->fs_info;
3698 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3699 struct list_head *cur;
3700 struct map_lookup *map = NULL;
3701 struct extent_map_tree *em_tree;
3702 struct extent_map *em;
3703 struct btrfs_device_info *devices_info = NULL;
3705 int num_stripes; /* total number of stripes to allocate */
3706 int data_stripes; /* number of stripes that count for
3708 int sub_stripes; /* sub_stripes info for map */
3709 int dev_stripes; /* stripes per dev */
3710 int devs_max; /* max devs to use */
3711 int devs_min; /* min devs needed */
3712 int devs_increment; /* ndevs has to be a multiple of this */
3713 int ncopies; /* how many copies to data has */
3715 u64 max_stripe_size;
3719 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
3725 BUG_ON(!alloc_profile_is_valid(type, 0));
3727 if (list_empty(&fs_devices->alloc_list))
3730 index = __get_raid_index(type);
3732 sub_stripes = btrfs_raid_array[index].sub_stripes;
3733 dev_stripes = btrfs_raid_array[index].dev_stripes;
3734 devs_max = btrfs_raid_array[index].devs_max;
3735 devs_min = btrfs_raid_array[index].devs_min;
3736 devs_increment = btrfs_raid_array[index].devs_increment;
3737 ncopies = btrfs_raid_array[index].ncopies;
3739 if (type & BTRFS_BLOCK_GROUP_DATA) {
3740 max_stripe_size = 1024 * 1024 * 1024;
3741 max_chunk_size = 10 * max_stripe_size;
3742 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
3743 /* for larger filesystems, use larger metadata chunks */
3744 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3745 max_stripe_size = 1024 * 1024 * 1024;
3747 max_stripe_size = 256 * 1024 * 1024;
3748 max_chunk_size = max_stripe_size;
3749 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
3750 max_stripe_size = 32 * 1024 * 1024;
3751 max_chunk_size = 2 * max_stripe_size;
3753 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3758 /* we don't want a chunk larger than 10% of writeable space */
3759 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3762 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3767 cur = fs_devices->alloc_list.next;
3770 * in the first pass through the devices list, we gather information
3771 * about the available holes on each device.
3774 while (cur != &fs_devices->alloc_list) {
3775 struct btrfs_device *device;
3779 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3783 if (!device->writeable) {
3785 "btrfs: read-only device in alloc_list\n");
3789 if (!device->in_fs_metadata ||
3790 device->is_tgtdev_for_dev_replace)
3793 if (device->total_bytes > device->bytes_used)
3794 total_avail = device->total_bytes - device->bytes_used;
3798 /* If there is no space on this device, skip it. */
3799 if (total_avail == 0)
3802 ret = find_free_dev_extent(device,
3803 max_stripe_size * dev_stripes,
3804 &dev_offset, &max_avail);
3805 if (ret && ret != -ENOSPC)
3809 max_avail = max_stripe_size * dev_stripes;
3811 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3814 if (ndevs == fs_devices->rw_devices) {
3815 WARN(1, "%s: found more than %llu devices\n",
3816 __func__, fs_devices->rw_devices);
3819 devices_info[ndevs].dev_offset = dev_offset;
3820 devices_info[ndevs].max_avail = max_avail;
3821 devices_info[ndevs].total_avail = total_avail;
3822 devices_info[ndevs].dev = device;
3827 * now sort the devices by hole size / available space
3829 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3830 btrfs_cmp_device_info, NULL);
3832 /* round down to number of usable stripes */
3833 ndevs -= ndevs % devs_increment;
3835 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3840 if (devs_max && ndevs > devs_max)
3843 * the primary goal is to maximize the number of stripes, so use as many
3844 * devices as possible, even if the stripes are not maximum sized.
3846 stripe_size = devices_info[ndevs-1].max_avail;
3847 num_stripes = ndevs * dev_stripes;
3850 * this will have to be fixed for RAID1 and RAID10 over
3853 data_stripes = num_stripes / ncopies;
3855 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3856 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3857 btrfs_super_stripesize(info->super_copy));
3858 data_stripes = num_stripes - 1;
3860 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3861 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3862 btrfs_super_stripesize(info->super_copy));
3863 data_stripes = num_stripes - 2;
3867 * Use the number of data stripes to figure out how big this chunk
3868 * is really going to be in terms of logical address space,
3869 * and compare that answer with the max chunk size
3871 if (stripe_size * data_stripes > max_chunk_size) {
3872 u64 mask = (1ULL << 24) - 1;
3873 stripe_size = max_chunk_size;
3874 do_div(stripe_size, data_stripes);
3876 /* bump the answer up to a 16MB boundary */
3877 stripe_size = (stripe_size + mask) & ~mask;
3879 /* but don't go higher than the limits we found
3880 * while searching for free extents
3882 if (stripe_size > devices_info[ndevs-1].max_avail)
3883 stripe_size = devices_info[ndevs-1].max_avail;
3886 do_div(stripe_size, dev_stripes);
3888 /* align to BTRFS_STRIPE_LEN */
3889 do_div(stripe_size, raid_stripe_len);
3890 stripe_size *= raid_stripe_len;
3892 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3897 map->num_stripes = num_stripes;
3899 for (i = 0; i < ndevs; ++i) {
3900 for (j = 0; j < dev_stripes; ++j) {
3901 int s = i * dev_stripes + j;
3902 map->stripes[s].dev = devices_info[i].dev;
3903 map->stripes[s].physical = devices_info[i].dev_offset +
3907 map->sector_size = extent_root->sectorsize;
3908 map->stripe_len = raid_stripe_len;
3909 map->io_align = raid_stripe_len;
3910 map->io_width = raid_stripe_len;
3912 map->sub_stripes = sub_stripes;
3915 num_bytes = stripe_size * data_stripes;
3917 *stripe_size_out = stripe_size;
3918 *num_bytes_out = num_bytes;
3920 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
3922 em = alloc_extent_map();
3927 em->bdev = (struct block_device *)map;
3929 em->len = num_bytes;
3930 em->block_start = 0;
3931 em->block_len = em->len;
3933 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
3934 write_lock(&em_tree->lock);
3935 ret = add_extent_mapping(em_tree, em);
3936 write_unlock(&em_tree->lock);
3938 free_extent_map(em);
3942 for (i = 0; i < map->num_stripes; ++i) {
3943 struct btrfs_device *device;
3946 device = map->stripes[i].dev;
3947 dev_offset = map->stripes[i].physical;
3949 ret = btrfs_alloc_dev_extent(trans, device,
3950 info->chunk_root->root_key.objectid,
3951 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3952 start, dev_offset, stripe_size);
3954 goto error_dev_extent;
3957 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3958 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3961 i = map->num_stripes - 1;
3962 goto error_dev_extent;
3965 free_extent_map(em);
3966 check_raid56_incompat_flag(extent_root->fs_info, type);
3968 kfree(devices_info);
3972 for (; i >= 0; i--) {
3973 struct btrfs_device *device;
3976 device = map->stripes[i].dev;
3977 err = btrfs_free_dev_extent(trans, device, start);
3979 btrfs_abort_transaction(trans, extent_root, err);
3983 write_lock(&em_tree->lock);
3984 remove_extent_mapping(em_tree, em);
3985 write_unlock(&em_tree->lock);
3987 /* One for our allocation */
3988 free_extent_map(em);
3989 /* One for the tree reference */
3990 free_extent_map(em);
3993 kfree(devices_info);
3997 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3998 struct btrfs_root *extent_root,
3999 struct map_lookup *map, u64 chunk_offset,
4000 u64 chunk_size, u64 stripe_size)
4003 struct btrfs_key key;
4004 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4005 struct btrfs_device *device;
4006 struct btrfs_chunk *chunk;
4007 struct btrfs_stripe *stripe;
4008 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
4012 chunk = kzalloc(item_size, GFP_NOFS);
4017 while (index < map->num_stripes) {
4018 device = map->stripes[index].dev;
4019 device->bytes_used += stripe_size;
4020 ret = btrfs_update_device(trans, device);
4026 spin_lock(&extent_root->fs_info->free_chunk_lock);
4027 extent_root->fs_info->free_chunk_space -= (stripe_size *
4029 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4032 stripe = &chunk->stripe;
4033 while (index < map->num_stripes) {
4034 device = map->stripes[index].dev;
4035 dev_offset = map->stripes[index].physical;
4037 btrfs_set_stack_stripe_devid(stripe, device->devid);
4038 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4039 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4044 btrfs_set_stack_chunk_length(chunk, chunk_size);
4045 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4046 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4047 btrfs_set_stack_chunk_type(chunk, map->type);
4048 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4049 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4050 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4051 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4052 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4054 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4055 key.type = BTRFS_CHUNK_ITEM_KEY;
4056 key.offset = chunk_offset;
4058 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4060 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4062 * TODO: Cleanup of inserted chunk root in case of
4065 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4075 * Chunk allocation falls into two parts. The first part does works
4076 * that make the new allocated chunk useable, but not do any operation
4077 * that modifies the chunk tree. The second part does the works that
4078 * require modifying the chunk tree. This division is important for the
4079 * bootstrap process of adding storage to a seed btrfs.
4081 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4082 struct btrfs_root *extent_root, u64 type)
4087 struct map_lookup *map;
4088 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4091 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4096 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4097 &stripe_size, chunk_offset, type);
4101 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4102 chunk_size, stripe_size);
4108 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4109 struct btrfs_root *root,
4110 struct btrfs_device *device)
4113 u64 sys_chunk_offset;
4117 u64 sys_stripe_size;
4119 struct map_lookup *map;
4120 struct map_lookup *sys_map;
4121 struct btrfs_fs_info *fs_info = root->fs_info;
4122 struct btrfs_root *extent_root = fs_info->extent_root;
4125 ret = find_next_chunk(fs_info->chunk_root,
4126 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
4130 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4131 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4132 &stripe_size, chunk_offset, alloc_profile);
4136 sys_chunk_offset = chunk_offset + chunk_size;
4138 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4139 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4140 &sys_chunk_size, &sys_stripe_size,
4141 sys_chunk_offset, alloc_profile);
4143 btrfs_abort_transaction(trans, root, ret);
4147 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
4149 btrfs_abort_transaction(trans, root, ret);
4154 * Modifying chunk tree needs allocating new blocks from both
4155 * system block group and metadata block group. So we only can
4156 * do operations require modifying the chunk tree after both
4157 * block groups were created.
4159 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4160 chunk_size, stripe_size);
4162 btrfs_abort_transaction(trans, root, ret);
4166 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4167 sys_chunk_offset, sys_chunk_size,
4170 btrfs_abort_transaction(trans, root, ret);
4177 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4179 struct extent_map *em;
4180 struct map_lookup *map;
4181 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4185 read_lock(&map_tree->map_tree.lock);
4186 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4187 read_unlock(&map_tree->map_tree.lock);
4191 if (btrfs_test_opt(root, DEGRADED)) {
4192 free_extent_map(em);
4196 map = (struct map_lookup *)em->bdev;
4197 for (i = 0; i < map->num_stripes; i++) {
4198 if (!map->stripes[i].dev->writeable) {
4203 free_extent_map(em);
4207 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4209 extent_map_tree_init(&tree->map_tree);
4212 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4214 struct extent_map *em;
4217 write_lock(&tree->map_tree.lock);
4218 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4220 remove_extent_mapping(&tree->map_tree, em);
4221 write_unlock(&tree->map_tree.lock);
4226 free_extent_map(em);
4227 /* once for the tree */
4228 free_extent_map(em);
4232 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4234 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4235 struct extent_map *em;
4236 struct map_lookup *map;
4237 struct extent_map_tree *em_tree = &map_tree->map_tree;
4240 read_lock(&em_tree->lock);
4241 em = lookup_extent_mapping(em_tree, logical, len);
4242 read_unlock(&em_tree->lock);
4245 BUG_ON(em->start > logical || em->start + em->len < logical);
4246 map = (struct map_lookup *)em->bdev;
4247 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4248 ret = map->num_stripes;
4249 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4250 ret = map->sub_stripes;
4251 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4253 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4257 free_extent_map(em);
4259 btrfs_dev_replace_lock(&fs_info->dev_replace);
4260 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4262 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4267 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4268 struct btrfs_mapping_tree *map_tree,
4271 struct extent_map *em;
4272 struct map_lookup *map;
4273 struct extent_map_tree *em_tree = &map_tree->map_tree;
4274 unsigned long len = root->sectorsize;
4276 read_lock(&em_tree->lock);
4277 em = lookup_extent_mapping(em_tree, logical, len);
4278 read_unlock(&em_tree->lock);
4281 BUG_ON(em->start > logical || em->start + em->len < logical);
4282 map = (struct map_lookup *)em->bdev;
4283 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4284 BTRFS_BLOCK_GROUP_RAID6)) {
4285 len = map->stripe_len * nr_data_stripes(map);
4287 free_extent_map(em);
4291 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4292 u64 logical, u64 len, int mirror_num)
4294 struct extent_map *em;
4295 struct map_lookup *map;
4296 struct extent_map_tree *em_tree = &map_tree->map_tree;
4299 read_lock(&em_tree->lock);
4300 em = lookup_extent_mapping(em_tree, logical, len);
4301 read_unlock(&em_tree->lock);
4304 BUG_ON(em->start > logical || em->start + em->len < logical);
4305 map = (struct map_lookup *)em->bdev;
4306 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4307 BTRFS_BLOCK_GROUP_RAID6))
4309 free_extent_map(em);
4313 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4314 struct map_lookup *map, int first, int num,
4315 int optimal, int dev_replace_is_ongoing)
4319 struct btrfs_device *srcdev;
4321 if (dev_replace_is_ongoing &&
4322 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4323 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4324 srcdev = fs_info->dev_replace.srcdev;
4329 * try to avoid the drive that is the source drive for a
4330 * dev-replace procedure, only choose it if no other non-missing
4331 * mirror is available
4333 for (tolerance = 0; tolerance < 2; tolerance++) {
4334 if (map->stripes[optimal].dev->bdev &&
4335 (tolerance || map->stripes[optimal].dev != srcdev))
4337 for (i = first; i < first + num; i++) {
4338 if (map->stripes[i].dev->bdev &&
4339 (tolerance || map->stripes[i].dev != srcdev))
4344 /* we couldn't find one that doesn't fail. Just return something
4345 * and the io error handling code will clean up eventually
4350 static inline int parity_smaller(u64 a, u64 b)
4355 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4356 static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4358 struct btrfs_bio_stripe s;
4365 for (i = 0; i < bbio->num_stripes - 1; i++) {
4366 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4367 s = bbio->stripes[i];
4369 bbio->stripes[i] = bbio->stripes[i+1];
4370 raid_map[i] = raid_map[i+1];
4371 bbio->stripes[i+1] = s;
4379 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4380 u64 logical, u64 *length,
4381 struct btrfs_bio **bbio_ret,
4382 int mirror_num, u64 **raid_map_ret)
4384 struct extent_map *em;
4385 struct map_lookup *map;
4386 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4387 struct extent_map_tree *em_tree = &map_tree->map_tree;
4390 u64 stripe_end_offset;
4395 u64 *raid_map = NULL;
4401 struct btrfs_bio *bbio = NULL;
4402 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4403 int dev_replace_is_ongoing = 0;
4404 int num_alloc_stripes;
4405 int patch_the_first_stripe_for_dev_replace = 0;
4406 u64 physical_to_patch_in_first_stripe = 0;
4407 u64 raid56_full_stripe_start = (u64)-1;
4409 read_lock(&em_tree->lock);
4410 em = lookup_extent_mapping(em_tree, logical, *length);
4411 read_unlock(&em_tree->lock);
4414 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
4415 (unsigned long long)logical,
4416 (unsigned long long)*length);
4420 BUG_ON(em->start > logical || em->start + em->len < logical);
4421 map = (struct map_lookup *)em->bdev;
4422 offset = logical - em->start;
4424 if (mirror_num > map->num_stripes)
4427 stripe_len = map->stripe_len;
4430 * stripe_nr counts the total number of stripes we have to stride
4431 * to get to this block
4433 do_div(stripe_nr, stripe_len);
4435 stripe_offset = stripe_nr * stripe_len;
4436 BUG_ON(offset < stripe_offset);
4438 /* stripe_offset is the offset of this block in its stripe*/
4439 stripe_offset = offset - stripe_offset;
4441 /* if we're here for raid56, we need to know the stripe aligned start */
4442 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4443 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4444 raid56_full_stripe_start = offset;
4446 /* allow a write of a full stripe, but make sure we don't
4447 * allow straddling of stripes
4449 do_div(raid56_full_stripe_start, full_stripe_len);
4450 raid56_full_stripe_start *= full_stripe_len;
4453 if (rw & REQ_DISCARD) {
4454 /* we don't discard raid56 yet */
4456 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4460 *length = min_t(u64, em->len - offset, *length);
4461 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4463 /* For writes to RAID[56], allow a full stripeset across all disks.
4464 For other RAID types and for RAID[56] reads, just allow a single
4465 stripe (on a single disk). */
4466 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4468 max_len = stripe_len * nr_data_stripes(map) -
4469 (offset - raid56_full_stripe_start);
4471 /* we limit the length of each bio to what fits in a stripe */
4472 max_len = stripe_len - stripe_offset;
4474 *length = min_t(u64, em->len - offset, max_len);
4476 *length = em->len - offset;
4479 /* This is for when we're called from btrfs_merge_bio_hook() and all
4480 it cares about is the length */
4484 btrfs_dev_replace_lock(dev_replace);
4485 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4486 if (!dev_replace_is_ongoing)
4487 btrfs_dev_replace_unlock(dev_replace);
4489 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4490 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4491 dev_replace->tgtdev != NULL) {
4493 * in dev-replace case, for repair case (that's the only
4494 * case where the mirror is selected explicitly when
4495 * calling btrfs_map_block), blocks left of the left cursor
4496 * can also be read from the target drive.
4497 * For REQ_GET_READ_MIRRORS, the target drive is added as
4498 * the last one to the array of stripes. For READ, it also
4499 * needs to be supported using the same mirror number.
4500 * If the requested block is not left of the left cursor,
4501 * EIO is returned. This can happen because btrfs_num_copies()
4502 * returns one more in the dev-replace case.
4504 u64 tmp_length = *length;
4505 struct btrfs_bio *tmp_bbio = NULL;
4506 int tmp_num_stripes;
4507 u64 srcdev_devid = dev_replace->srcdev->devid;
4508 int index_srcdev = 0;
4510 u64 physical_of_found = 0;
4512 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
4513 logical, &tmp_length, &tmp_bbio, 0, NULL);
4515 WARN_ON(tmp_bbio != NULL);
4519 tmp_num_stripes = tmp_bbio->num_stripes;
4520 if (mirror_num > tmp_num_stripes) {
4522 * REQ_GET_READ_MIRRORS does not contain this
4523 * mirror, that means that the requested area
4524 * is not left of the left cursor
4532 * process the rest of the function using the mirror_num
4533 * of the source drive. Therefore look it up first.
4534 * At the end, patch the device pointer to the one of the
4537 for (i = 0; i < tmp_num_stripes; i++) {
4538 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4540 * In case of DUP, in order to keep it
4541 * simple, only add the mirror with the
4542 * lowest physical address
4545 physical_of_found <=
4546 tmp_bbio->stripes[i].physical)
4551 tmp_bbio->stripes[i].physical;
4556 mirror_num = index_srcdev + 1;
4557 patch_the_first_stripe_for_dev_replace = 1;
4558 physical_to_patch_in_first_stripe = physical_of_found;
4567 } else if (mirror_num > map->num_stripes) {
4573 stripe_nr_orig = stripe_nr;
4574 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
4575 do_div(stripe_nr_end, map->stripe_len);
4576 stripe_end_offset = stripe_nr_end * map->stripe_len -
4579 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4580 if (rw & REQ_DISCARD)
4581 num_stripes = min_t(u64, map->num_stripes,
4582 stripe_nr_end - stripe_nr_orig);
4583 stripe_index = do_div(stripe_nr, map->num_stripes);
4584 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
4585 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
4586 num_stripes = map->num_stripes;
4587 else if (mirror_num)
4588 stripe_index = mirror_num - 1;
4590 stripe_index = find_live_mirror(fs_info, map, 0,
4592 current->pid % map->num_stripes,
4593 dev_replace_is_ongoing);
4594 mirror_num = stripe_index + 1;
4597 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
4598 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
4599 num_stripes = map->num_stripes;
4600 } else if (mirror_num) {
4601 stripe_index = mirror_num - 1;
4606 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4607 int factor = map->num_stripes / map->sub_stripes;
4609 stripe_index = do_div(stripe_nr, factor);
4610 stripe_index *= map->sub_stripes;
4612 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
4613 num_stripes = map->sub_stripes;
4614 else if (rw & REQ_DISCARD)
4615 num_stripes = min_t(u64, map->sub_stripes *
4616 (stripe_nr_end - stripe_nr_orig),
4618 else if (mirror_num)
4619 stripe_index += mirror_num - 1;
4621 int old_stripe_index = stripe_index;
4622 stripe_index = find_live_mirror(fs_info, map,
4624 map->sub_stripes, stripe_index +
4625 current->pid % map->sub_stripes,
4626 dev_replace_is_ongoing);
4627 mirror_num = stripe_index - old_stripe_index + 1;
4630 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4631 BTRFS_BLOCK_GROUP_RAID6)) {
4634 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4638 /* push stripe_nr back to the start of the full stripe */
4639 stripe_nr = raid56_full_stripe_start;
4640 do_div(stripe_nr, stripe_len);
4642 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4644 /* RAID[56] write or recovery. Return all stripes */
4645 num_stripes = map->num_stripes;
4646 max_errors = nr_parity_stripes(map);
4648 raid_map = kmalloc(sizeof(u64) * num_stripes,
4655 /* Work out the disk rotation on this stripe-set */
4657 rot = do_div(tmp, num_stripes);
4659 /* Fill in the logical address of each stripe */
4660 tmp = stripe_nr * nr_data_stripes(map);
4661 for (i = 0; i < nr_data_stripes(map); i++)
4662 raid_map[(i+rot) % num_stripes] =
4663 em->start + (tmp + i) * map->stripe_len;
4665 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4666 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4667 raid_map[(i+rot+1) % num_stripes] =
4670 *length = map->stripe_len;
4675 * Mirror #0 or #1 means the original data block.
4676 * Mirror #2 is RAID5 parity block.
4677 * Mirror #3 is RAID6 Q block.
4679 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4681 stripe_index = nr_data_stripes(map) +
4684 /* We distribute the parity blocks across stripes */
4685 tmp = stripe_nr + stripe_index;
4686 stripe_index = do_div(tmp, map->num_stripes);
4690 * after this do_div call, stripe_nr is the number of stripes
4691 * on this device we have to walk to find the data, and
4692 * stripe_index is the number of our device in the stripe array
4694 stripe_index = do_div(stripe_nr, map->num_stripes);
4695 mirror_num = stripe_index + 1;
4697 BUG_ON(stripe_index >= map->num_stripes);
4699 num_alloc_stripes = num_stripes;
4700 if (dev_replace_is_ongoing) {
4701 if (rw & (REQ_WRITE | REQ_DISCARD))
4702 num_alloc_stripes <<= 1;
4703 if (rw & REQ_GET_READ_MIRRORS)
4704 num_alloc_stripes++;
4706 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
4711 atomic_set(&bbio->error, 0);
4713 if (rw & REQ_DISCARD) {
4715 int sub_stripes = 0;
4716 u64 stripes_per_dev = 0;
4717 u32 remaining_stripes = 0;
4718 u32 last_stripe = 0;
4721 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4722 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4725 sub_stripes = map->sub_stripes;
4727 factor = map->num_stripes / sub_stripes;
4728 stripes_per_dev = div_u64_rem(stripe_nr_end -
4731 &remaining_stripes);
4732 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4733 last_stripe *= sub_stripes;
4736 for (i = 0; i < num_stripes; i++) {
4737 bbio->stripes[i].physical =
4738 map->stripes[stripe_index].physical +
4739 stripe_offset + stripe_nr * map->stripe_len;
4740 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
4742 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4743 BTRFS_BLOCK_GROUP_RAID10)) {
4744 bbio->stripes[i].length = stripes_per_dev *
4747 if (i / sub_stripes < remaining_stripes)
4748 bbio->stripes[i].length +=
4752 * Special for the first stripe and
4755 * |-------|...|-------|
4759 if (i < sub_stripes)
4760 bbio->stripes[i].length -=
4763 if (stripe_index >= last_stripe &&
4764 stripe_index <= (last_stripe +
4766 bbio->stripes[i].length -=
4769 if (i == sub_stripes - 1)
4772 bbio->stripes[i].length = *length;
4775 if (stripe_index == map->num_stripes) {
4776 /* This could only happen for RAID0/10 */
4782 for (i = 0; i < num_stripes; i++) {
4783 bbio->stripes[i].physical =
4784 map->stripes[stripe_index].physical +
4786 stripe_nr * map->stripe_len;
4787 bbio->stripes[i].dev =
4788 map->stripes[stripe_index].dev;
4793 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
4794 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4795 BTRFS_BLOCK_GROUP_RAID10 |
4796 BTRFS_BLOCK_GROUP_RAID5 |
4797 BTRFS_BLOCK_GROUP_DUP)) {
4799 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4804 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4805 dev_replace->tgtdev != NULL) {
4806 int index_where_to_add;
4807 u64 srcdev_devid = dev_replace->srcdev->devid;
4810 * duplicate the write operations while the dev replace
4811 * procedure is running. Since the copying of the old disk
4812 * to the new disk takes place at run time while the
4813 * filesystem is mounted writable, the regular write
4814 * operations to the old disk have to be duplicated to go
4815 * to the new disk as well.
4816 * Note that device->missing is handled by the caller, and
4817 * that the write to the old disk is already set up in the
4820 index_where_to_add = num_stripes;
4821 for (i = 0; i < num_stripes; i++) {
4822 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4823 /* write to new disk, too */
4824 struct btrfs_bio_stripe *new =
4825 bbio->stripes + index_where_to_add;
4826 struct btrfs_bio_stripe *old =
4829 new->physical = old->physical;
4830 new->length = old->length;
4831 new->dev = dev_replace->tgtdev;
4832 index_where_to_add++;
4836 num_stripes = index_where_to_add;
4837 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4838 dev_replace->tgtdev != NULL) {
4839 u64 srcdev_devid = dev_replace->srcdev->devid;
4840 int index_srcdev = 0;
4842 u64 physical_of_found = 0;
4845 * During the dev-replace procedure, the target drive can
4846 * also be used to read data in case it is needed to repair
4847 * a corrupt block elsewhere. This is possible if the
4848 * requested area is left of the left cursor. In this area,
4849 * the target drive is a full copy of the source drive.
4851 for (i = 0; i < num_stripes; i++) {
4852 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4854 * In case of DUP, in order to keep it
4855 * simple, only add the mirror with the
4856 * lowest physical address
4859 physical_of_found <=
4860 bbio->stripes[i].physical)
4864 physical_of_found = bbio->stripes[i].physical;
4868 u64 length = map->stripe_len;
4870 if (physical_of_found + length <=
4871 dev_replace->cursor_left) {
4872 struct btrfs_bio_stripe *tgtdev_stripe =
4873 bbio->stripes + num_stripes;
4875 tgtdev_stripe->physical = physical_of_found;
4876 tgtdev_stripe->length =
4877 bbio->stripes[index_srcdev].length;
4878 tgtdev_stripe->dev = dev_replace->tgtdev;
4886 bbio->num_stripes = num_stripes;
4887 bbio->max_errors = max_errors;
4888 bbio->mirror_num = mirror_num;
4891 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4892 * mirror_num == num_stripes + 1 && dev_replace target drive is
4893 * available as a mirror
4895 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4896 WARN_ON(num_stripes > 1);
4897 bbio->stripes[0].dev = dev_replace->tgtdev;
4898 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4899 bbio->mirror_num = map->num_stripes + 1;
4902 sort_parity_stripes(bbio, raid_map);
4903 *raid_map_ret = raid_map;
4906 if (dev_replace_is_ongoing)
4907 btrfs_dev_replace_unlock(dev_replace);
4908 free_extent_map(em);
4912 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4913 u64 logical, u64 *length,
4914 struct btrfs_bio **bbio_ret, int mirror_num)
4916 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
4920 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4921 u64 chunk_start, u64 physical, u64 devid,
4922 u64 **logical, int *naddrs, int *stripe_len)
4924 struct extent_map_tree *em_tree = &map_tree->map_tree;
4925 struct extent_map *em;
4926 struct map_lookup *map;
4934 read_lock(&em_tree->lock);
4935 em = lookup_extent_mapping(em_tree, chunk_start, 1);
4936 read_unlock(&em_tree->lock);
4939 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
4944 if (em->start != chunk_start) {
4945 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4946 em->start, chunk_start);
4947 free_extent_map(em);
4950 map = (struct map_lookup *)em->bdev;
4953 rmap_len = map->stripe_len;
4955 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4956 do_div(length, map->num_stripes / map->sub_stripes);
4957 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4958 do_div(length, map->num_stripes);
4959 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4960 BTRFS_BLOCK_GROUP_RAID6)) {
4961 do_div(length, nr_data_stripes(map));
4962 rmap_len = map->stripe_len * nr_data_stripes(map);
4965 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
4966 BUG_ON(!buf); /* -ENOMEM */
4968 for (i = 0; i < map->num_stripes; i++) {
4969 if (devid && map->stripes[i].dev->devid != devid)
4971 if (map->stripes[i].physical > physical ||
4972 map->stripes[i].physical + length <= physical)
4975 stripe_nr = physical - map->stripes[i].physical;
4976 do_div(stripe_nr, map->stripe_len);
4978 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4979 stripe_nr = stripe_nr * map->num_stripes + i;
4980 do_div(stripe_nr, map->sub_stripes);
4981 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4982 stripe_nr = stripe_nr * map->num_stripes + i;
4983 } /* else if RAID[56], multiply by nr_data_stripes().
4984 * Alternatively, just use rmap_len below instead of
4985 * map->stripe_len */
4987 bytenr = chunk_start + stripe_nr * rmap_len;
4988 WARN_ON(nr >= map->num_stripes);
4989 for (j = 0; j < nr; j++) {
4990 if (buf[j] == bytenr)
4994 WARN_ON(nr >= map->num_stripes);
5001 *stripe_len = rmap_len;
5003 free_extent_map(em);
5007 static void *merge_stripe_index_into_bio_private(void *bi_private,
5008 unsigned int stripe_index)
5011 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
5013 * The alternative solution (instead of stealing bits from the
5014 * pointer) would be to allocate an intermediate structure
5015 * that contains the old private pointer plus the stripe_index.
5017 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
5018 BUG_ON(stripe_index > 3);
5019 return (void *)(((uintptr_t)bi_private) | stripe_index);
5022 static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
5024 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
5027 static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
5029 return (unsigned int)((uintptr_t)bi_private) & 3;
5032 static void btrfs_end_bio(struct bio *bio, int err)
5034 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
5035 int is_orig_bio = 0;
5038 atomic_inc(&bbio->error);
5039 if (err == -EIO || err == -EREMOTEIO) {
5040 unsigned int stripe_index =
5041 extract_stripe_index_from_bio_private(
5043 struct btrfs_device *dev;
5045 BUG_ON(stripe_index >= bbio->num_stripes);
5046 dev = bbio->stripes[stripe_index].dev;
5048 if (bio->bi_rw & WRITE)
5049 btrfs_dev_stat_inc(dev,
5050 BTRFS_DEV_STAT_WRITE_ERRS);
5052 btrfs_dev_stat_inc(dev,
5053 BTRFS_DEV_STAT_READ_ERRS);
5054 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5055 btrfs_dev_stat_inc(dev,
5056 BTRFS_DEV_STAT_FLUSH_ERRS);
5057 btrfs_dev_stat_print_on_error(dev);
5062 if (bio == bbio->orig_bio)
5065 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5068 bio = bbio->orig_bio;
5070 bio->bi_private = bbio->private;
5071 bio->bi_end_io = bbio->end_io;
5072 bio->bi_bdev = (struct block_device *)
5073 (unsigned long)bbio->mirror_num;
5074 /* only send an error to the higher layers if it is
5075 * beyond the tolerance of the btrfs bio
5077 if (atomic_read(&bbio->error) > bbio->max_errors) {
5081 * this bio is actually up to date, we didn't
5082 * go over the max number of errors
5084 set_bit(BIO_UPTODATE, &bio->bi_flags);
5089 bio_endio(bio, err);
5090 } else if (!is_orig_bio) {
5095 struct async_sched {
5098 struct btrfs_fs_info *info;
5099 struct btrfs_work work;
5103 * see run_scheduled_bios for a description of why bios are collected for
5106 * This will add one bio to the pending list for a device and make sure
5107 * the work struct is scheduled.
5109 noinline void btrfs_schedule_bio(struct btrfs_root *root,
5110 struct btrfs_device *device,
5111 int rw, struct bio *bio)
5113 int should_queue = 1;
5114 struct btrfs_pending_bios *pending_bios;
5116 if (device->missing || !device->bdev) {
5117 bio_endio(bio, -EIO);
5121 /* don't bother with additional async steps for reads, right now */
5122 if (!(rw & REQ_WRITE)) {
5124 btrfsic_submit_bio(rw, bio);
5130 * nr_async_bios allows us to reliably return congestion to the
5131 * higher layers. Otherwise, the async bio makes it appear we have
5132 * made progress against dirty pages when we've really just put it
5133 * on a queue for later
5135 atomic_inc(&root->fs_info->nr_async_bios);
5136 WARN_ON(bio->bi_next);
5137 bio->bi_next = NULL;
5140 spin_lock(&device->io_lock);
5141 if (bio->bi_rw & REQ_SYNC)
5142 pending_bios = &device->pending_sync_bios;
5144 pending_bios = &device->pending_bios;
5146 if (pending_bios->tail)
5147 pending_bios->tail->bi_next = bio;
5149 pending_bios->tail = bio;
5150 if (!pending_bios->head)
5151 pending_bios->head = bio;
5152 if (device->running_pending)
5155 spin_unlock(&device->io_lock);
5158 btrfs_queue_worker(&root->fs_info->submit_workers,
5162 static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5165 struct bio_vec *prev;
5166 struct request_queue *q = bdev_get_queue(bdev);
5167 unsigned short max_sectors = queue_max_sectors(q);
5168 struct bvec_merge_data bvm = {
5170 .bi_sector = sector,
5171 .bi_rw = bio->bi_rw,
5174 if (bio->bi_vcnt == 0) {
5179 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5180 if ((bio->bi_size >> 9) > max_sectors)
5183 if (!q->merge_bvec_fn)
5186 bvm.bi_size = bio->bi_size - prev->bv_len;
5187 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5192 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5193 struct bio *bio, u64 physical, int dev_nr,
5196 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5198 bio->bi_private = bbio;
5199 bio->bi_private = merge_stripe_index_into_bio_private(
5200 bio->bi_private, (unsigned int)dev_nr);
5201 bio->bi_end_io = btrfs_end_bio;
5202 bio->bi_sector = physical >> 9;
5205 struct rcu_string *name;
5208 name = rcu_dereference(dev->name);
5209 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5210 "(%s id %llu), size=%u\n", rw,
5211 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5212 name->str, dev->devid, bio->bi_size);
5216 bio->bi_bdev = dev->bdev;
5218 btrfs_schedule_bio(root, dev, rw, bio);
5220 btrfsic_submit_bio(rw, bio);
5223 static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5224 struct bio *first_bio, struct btrfs_device *dev,
5225 int dev_nr, int rw, int async)
5227 struct bio_vec *bvec = first_bio->bi_io_vec;
5229 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5230 u64 physical = bbio->stripes[dev_nr].physical;
5233 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5237 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5238 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5239 bvec->bv_offset) < bvec->bv_len) {
5240 u64 len = bio->bi_size;
5242 atomic_inc(&bbio->stripes_pending);
5243 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5251 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5255 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5257 atomic_inc(&bbio->error);
5258 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5259 bio->bi_private = bbio->private;
5260 bio->bi_end_io = bbio->end_io;
5261 bio->bi_bdev = (struct block_device *)
5262 (unsigned long)bbio->mirror_num;
5263 bio->bi_sector = logical >> 9;
5265 bio_endio(bio, -EIO);
5269 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5270 int mirror_num, int async_submit)
5272 struct btrfs_device *dev;
5273 struct bio *first_bio = bio;
5274 u64 logical = (u64)bio->bi_sector << 9;
5277 u64 *raid_map = NULL;
5281 struct btrfs_bio *bbio = NULL;
5283 length = bio->bi_size;
5284 map_length = length;
5286 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5287 mirror_num, &raid_map);
5288 if (ret) /* -ENOMEM */
5291 total_devs = bbio->num_stripes;
5292 bbio->orig_bio = first_bio;
5293 bbio->private = first_bio->bi_private;
5294 bbio->end_io = first_bio->bi_end_io;
5295 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5298 /* In this case, map_length has been set to the length of
5299 a single stripe; not the whole write */
5301 return raid56_parity_write(root, bio, bbio,
5302 raid_map, map_length);
5304 return raid56_parity_recover(root, bio, bbio,
5305 raid_map, map_length,
5310 if (map_length < length) {
5311 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
5312 "len %llu\n", (unsigned long long)logical,
5313 (unsigned long long)length,
5314 (unsigned long long)map_length);
5318 while (dev_nr < total_devs) {
5319 dev = bbio->stripes[dev_nr].dev;
5320 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5321 bbio_error(bbio, first_bio, logical);
5327 * Check and see if we're ok with this bio based on it's size
5328 * and offset with the given device.
5330 if (!bio_size_ok(dev->bdev, first_bio,
5331 bbio->stripes[dev_nr].physical >> 9)) {
5332 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5333 dev_nr, rw, async_submit);
5339 if (dev_nr < total_devs - 1) {
5340 bio = bio_clone(first_bio, GFP_NOFS);
5341 BUG_ON(!bio); /* -ENOMEM */
5346 submit_stripe_bio(root, bbio, bio,
5347 bbio->stripes[dev_nr].physical, dev_nr, rw,
5354 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5357 struct btrfs_device *device;
5358 struct btrfs_fs_devices *cur_devices;
5360 cur_devices = fs_info->fs_devices;
5361 while (cur_devices) {
5363 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5364 device = __find_device(&cur_devices->devices,
5369 cur_devices = cur_devices->seed;
5374 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5375 u64 devid, u8 *dev_uuid)
5377 struct btrfs_device *device;
5378 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5380 device = kzalloc(sizeof(*device), GFP_NOFS);
5383 list_add(&device->dev_list,
5384 &fs_devices->devices);
5385 device->dev_root = root->fs_info->dev_root;
5386 device->devid = devid;
5387 device->work.func = pending_bios_fn;
5388 device->fs_devices = fs_devices;
5389 device->missing = 1;
5390 fs_devices->num_devices++;
5391 fs_devices->missing_devices++;
5392 spin_lock_init(&device->io_lock);
5393 INIT_LIST_HEAD(&device->dev_alloc_list);
5394 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5398 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5399 struct extent_buffer *leaf,
5400 struct btrfs_chunk *chunk)
5402 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5403 struct map_lookup *map;
5404 struct extent_map *em;
5408 u8 uuid[BTRFS_UUID_SIZE];
5413 logical = key->offset;
5414 length = btrfs_chunk_length(leaf, chunk);
5416 read_lock(&map_tree->map_tree.lock);
5417 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
5418 read_unlock(&map_tree->map_tree.lock);
5420 /* already mapped? */
5421 if (em && em->start <= logical && em->start + em->len > logical) {
5422 free_extent_map(em);
5425 free_extent_map(em);
5428 em = alloc_extent_map();
5431 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5432 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5434 free_extent_map(em);
5438 em->bdev = (struct block_device *)map;
5439 em->start = logical;
5442 em->block_start = 0;
5443 em->block_len = em->len;
5445 map->num_stripes = num_stripes;
5446 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5447 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5448 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5449 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5450 map->type = btrfs_chunk_type(leaf, chunk);
5451 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
5452 for (i = 0; i < num_stripes; i++) {
5453 map->stripes[i].physical =
5454 btrfs_stripe_offset_nr(leaf, chunk, i);
5455 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
5456 read_extent_buffer(leaf, uuid, (unsigned long)
5457 btrfs_stripe_dev_uuid_nr(chunk, i),
5459 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5461 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
5463 free_extent_map(em);
5466 if (!map->stripes[i].dev) {
5467 map->stripes[i].dev =
5468 add_missing_dev(root, devid, uuid);
5469 if (!map->stripes[i].dev) {
5471 free_extent_map(em);
5475 map->stripes[i].dev->in_fs_metadata = 1;
5478 write_lock(&map_tree->map_tree.lock);
5479 ret = add_extent_mapping(&map_tree->map_tree, em);
5480 write_unlock(&map_tree->map_tree.lock);
5481 BUG_ON(ret); /* Tree corruption */
5482 free_extent_map(em);
5487 static void fill_device_from_item(struct extent_buffer *leaf,
5488 struct btrfs_dev_item *dev_item,
5489 struct btrfs_device *device)
5493 device->devid = btrfs_device_id(leaf, dev_item);
5494 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5495 device->total_bytes = device->disk_total_bytes;
5496 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5497 device->type = btrfs_device_type(leaf, dev_item);
5498 device->io_align = btrfs_device_io_align(leaf, dev_item);
5499 device->io_width = btrfs_device_io_width(leaf, dev_item);
5500 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
5501 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
5502 device->is_tgtdev_for_dev_replace = 0;
5504 ptr = (unsigned long)btrfs_device_uuid(dev_item);
5505 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
5508 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5510 struct btrfs_fs_devices *fs_devices;
5513 BUG_ON(!mutex_is_locked(&uuid_mutex));
5515 fs_devices = root->fs_info->fs_devices->seed;
5516 while (fs_devices) {
5517 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5521 fs_devices = fs_devices->seed;
5524 fs_devices = find_fsid(fsid);
5530 fs_devices = clone_fs_devices(fs_devices);
5531 if (IS_ERR(fs_devices)) {
5532 ret = PTR_ERR(fs_devices);
5536 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
5537 root->fs_info->bdev_holder);
5539 free_fs_devices(fs_devices);
5543 if (!fs_devices->seeding) {
5544 __btrfs_close_devices(fs_devices);
5545 free_fs_devices(fs_devices);
5550 fs_devices->seed = root->fs_info->fs_devices->seed;
5551 root->fs_info->fs_devices->seed = fs_devices;
5556 static int read_one_dev(struct btrfs_root *root,
5557 struct extent_buffer *leaf,
5558 struct btrfs_dev_item *dev_item)
5560 struct btrfs_device *device;
5563 u8 fs_uuid[BTRFS_UUID_SIZE];
5564 u8 dev_uuid[BTRFS_UUID_SIZE];
5566 devid = btrfs_device_id(leaf, dev_item);
5567 read_extent_buffer(leaf, dev_uuid,
5568 (unsigned long)btrfs_device_uuid(dev_item),
5570 read_extent_buffer(leaf, fs_uuid,
5571 (unsigned long)btrfs_device_fsid(dev_item),
5574 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5575 ret = open_seed_devices(root, fs_uuid);
5576 if (ret && !btrfs_test_opt(root, DEGRADED))
5580 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
5581 if (!device || !device->bdev) {
5582 if (!btrfs_test_opt(root, DEGRADED))
5586 printk(KERN_WARNING "warning devid %llu missing\n",
5587 (unsigned long long)devid);
5588 device = add_missing_dev(root, devid, dev_uuid);
5591 } else if (!device->missing) {
5593 * this happens when a device that was properly setup
5594 * in the device info lists suddenly goes bad.
5595 * device->bdev is NULL, and so we have to set
5596 * device->missing to one here
5598 root->fs_info->fs_devices->missing_devices++;
5599 device->missing = 1;
5603 if (device->fs_devices != root->fs_info->fs_devices) {
5604 BUG_ON(device->writeable);
5605 if (device->generation !=
5606 btrfs_device_generation(leaf, dev_item))
5610 fill_device_from_item(leaf, dev_item, device);
5611 device->dev_root = root->fs_info->dev_root;
5612 device->in_fs_metadata = 1;
5613 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
5614 device->fs_devices->total_rw_bytes += device->total_bytes;
5615 spin_lock(&root->fs_info->free_chunk_lock);
5616 root->fs_info->free_chunk_space += device->total_bytes -
5618 spin_unlock(&root->fs_info->free_chunk_lock);
5624 int btrfs_read_sys_array(struct btrfs_root *root)
5626 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
5627 struct extent_buffer *sb;
5628 struct btrfs_disk_key *disk_key;
5629 struct btrfs_chunk *chunk;
5631 unsigned long sb_ptr;
5637 struct btrfs_key key;
5639 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
5640 BTRFS_SUPER_INFO_SIZE);
5643 btrfs_set_buffer_uptodate(sb);
5644 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
5646 * The sb extent buffer is artifical and just used to read the system array.
5647 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5648 * pages up-to-date when the page is larger: extent does not cover the
5649 * whole page and consequently check_page_uptodate does not find all
5650 * the page's extents up-to-date (the hole beyond sb),
5651 * write_extent_buffer then triggers a WARN_ON.
5653 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5654 * but sb spans only this function. Add an explicit SetPageUptodate call
5655 * to silence the warning eg. on PowerPC 64.
5657 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
5658 SetPageUptodate(sb->pages[0]);
5660 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
5661 array_size = btrfs_super_sys_array_size(super_copy);
5663 ptr = super_copy->sys_chunk_array;
5664 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5667 while (cur < array_size) {
5668 disk_key = (struct btrfs_disk_key *)ptr;
5669 btrfs_disk_key_to_cpu(&key, disk_key);
5671 len = sizeof(*disk_key); ptr += len;
5675 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
5676 chunk = (struct btrfs_chunk *)sb_ptr;
5677 ret = read_one_chunk(root, &key, sb, chunk);
5680 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5681 len = btrfs_chunk_item_size(num_stripes);
5690 free_extent_buffer(sb);
5694 int btrfs_read_chunk_tree(struct btrfs_root *root)
5696 struct btrfs_path *path;
5697 struct extent_buffer *leaf;
5698 struct btrfs_key key;
5699 struct btrfs_key found_key;
5703 root = root->fs_info->chunk_root;
5705 path = btrfs_alloc_path();
5709 mutex_lock(&uuid_mutex);
5712 /* first we search for all of the device items, and then we
5713 * read in all of the chunk items. This way we can create chunk
5714 * mappings that reference all of the devices that are afound
5716 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5720 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5724 leaf = path->nodes[0];
5725 slot = path->slots[0];
5726 if (slot >= btrfs_header_nritems(leaf)) {
5727 ret = btrfs_next_leaf(root, path);
5734 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5735 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5736 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5738 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5739 struct btrfs_dev_item *dev_item;
5740 dev_item = btrfs_item_ptr(leaf, slot,
5741 struct btrfs_dev_item);
5742 ret = read_one_dev(root, leaf, dev_item);
5746 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5747 struct btrfs_chunk *chunk;
5748 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5749 ret = read_one_chunk(root, &found_key, leaf, chunk);
5755 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5757 btrfs_release_path(path);
5762 unlock_chunks(root);
5763 mutex_unlock(&uuid_mutex);
5765 btrfs_free_path(path);
5769 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5773 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5774 btrfs_dev_stat_reset(dev, i);
5777 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5779 struct btrfs_key key;
5780 struct btrfs_key found_key;
5781 struct btrfs_root *dev_root = fs_info->dev_root;
5782 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5783 struct extent_buffer *eb;
5786 struct btrfs_device *device;
5787 struct btrfs_path *path = NULL;
5790 path = btrfs_alloc_path();
5796 mutex_lock(&fs_devices->device_list_mutex);
5797 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5799 struct btrfs_dev_stats_item *ptr;
5802 key.type = BTRFS_DEV_STATS_KEY;
5803 key.offset = device->devid;
5804 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5806 __btrfs_reset_dev_stats(device);
5807 device->dev_stats_valid = 1;
5808 btrfs_release_path(path);
5811 slot = path->slots[0];
5812 eb = path->nodes[0];
5813 btrfs_item_key_to_cpu(eb, &found_key, slot);
5814 item_size = btrfs_item_size_nr(eb, slot);
5816 ptr = btrfs_item_ptr(eb, slot,
5817 struct btrfs_dev_stats_item);
5819 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5820 if (item_size >= (1 + i) * sizeof(__le64))
5821 btrfs_dev_stat_set(device, i,
5822 btrfs_dev_stats_value(eb, ptr, i));
5824 btrfs_dev_stat_reset(device, i);
5827 device->dev_stats_valid = 1;
5828 btrfs_dev_stat_print_on_load(device);
5829 btrfs_release_path(path);
5831 mutex_unlock(&fs_devices->device_list_mutex);
5834 btrfs_free_path(path);
5835 return ret < 0 ? ret : 0;
5838 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5839 struct btrfs_root *dev_root,
5840 struct btrfs_device *device)
5842 struct btrfs_path *path;
5843 struct btrfs_key key;
5844 struct extent_buffer *eb;
5845 struct btrfs_dev_stats_item *ptr;
5850 key.type = BTRFS_DEV_STATS_KEY;
5851 key.offset = device->devid;
5853 path = btrfs_alloc_path();
5855 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5857 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5858 ret, rcu_str_deref(device->name));
5863 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5864 /* need to delete old one and insert a new one */
5865 ret = btrfs_del_item(trans, dev_root, path);
5867 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5868 rcu_str_deref(device->name), ret);
5875 /* need to insert a new item */
5876 btrfs_release_path(path);
5877 ret = btrfs_insert_empty_item(trans, dev_root, path,
5878 &key, sizeof(*ptr));
5880 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5881 rcu_str_deref(device->name), ret);
5886 eb = path->nodes[0];
5887 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5888 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5889 btrfs_set_dev_stats_value(eb, ptr, i,
5890 btrfs_dev_stat_read(device, i));
5891 btrfs_mark_buffer_dirty(eb);
5894 btrfs_free_path(path);
5899 * called from commit_transaction. Writes all changed device stats to disk.
5901 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5902 struct btrfs_fs_info *fs_info)
5904 struct btrfs_root *dev_root = fs_info->dev_root;
5905 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5906 struct btrfs_device *device;
5909 mutex_lock(&fs_devices->device_list_mutex);
5910 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5911 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5914 ret = update_dev_stat_item(trans, dev_root, device);
5916 device->dev_stats_dirty = 0;
5918 mutex_unlock(&fs_devices->device_list_mutex);
5923 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5925 btrfs_dev_stat_inc(dev, index);
5926 btrfs_dev_stat_print_on_error(dev);
5929 void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5931 if (!dev->dev_stats_valid)
5933 printk_ratelimited_in_rcu(KERN_ERR
5934 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5935 rcu_str_deref(dev->name),
5936 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5937 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5938 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5939 btrfs_dev_stat_read(dev,
5940 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5941 btrfs_dev_stat_read(dev,
5942 BTRFS_DEV_STAT_GENERATION_ERRS));
5945 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5949 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5950 if (btrfs_dev_stat_read(dev, i) != 0)
5952 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5953 return; /* all values == 0, suppress message */
5955 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5956 rcu_str_deref(dev->name),
5957 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5958 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5959 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5960 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5961 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5964 int btrfs_get_dev_stats(struct btrfs_root *root,
5965 struct btrfs_ioctl_get_dev_stats *stats)
5967 struct btrfs_device *dev;
5968 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5971 mutex_lock(&fs_devices->device_list_mutex);
5972 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
5973 mutex_unlock(&fs_devices->device_list_mutex);
5977 "btrfs: get dev_stats failed, device not found\n");
5979 } else if (!dev->dev_stats_valid) {
5981 "btrfs: get dev_stats failed, not yet valid\n");
5983 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
5984 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5985 if (stats->nr_items > i)
5987 btrfs_dev_stat_read_and_reset(dev, i);
5989 btrfs_dev_stat_reset(dev, i);
5992 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5993 if (stats->nr_items > i)
5994 stats->values[i] = btrfs_dev_stat_read(dev, i);
5996 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5997 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6001 int btrfs_scratch_superblock(struct btrfs_device *device)
6003 struct buffer_head *bh;
6004 struct btrfs_super_block *disk_super;
6006 bh = btrfs_read_dev_super(device->bdev);
6009 disk_super = (struct btrfs_super_block *)bh->b_data;
6011 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6012 set_buffer_dirty(bh);
6013 sync_dirty_buffer(bh);