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);
690 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
691 fmode_t flags, void *holder)
693 struct request_queue *q;
694 struct block_device *bdev;
695 struct list_head *head = &fs_devices->devices;
696 struct btrfs_device *device;
697 struct block_device *latest_bdev = NULL;
698 struct buffer_head *bh;
699 struct btrfs_super_block *disk_super;
700 u64 latest_devid = 0;
701 u64 latest_transid = 0;
708 list_for_each_entry(device, head, dev_list) {
714 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
719 disk_super = (struct btrfs_super_block *)bh->b_data;
720 devid = btrfs_stack_device_id(&disk_super->dev_item);
721 if (devid != device->devid)
724 if (memcmp(device->uuid, disk_super->dev_item.uuid,
728 device->generation = btrfs_super_generation(disk_super);
729 if (!latest_transid || device->generation > latest_transid) {
730 latest_devid = devid;
731 latest_transid = device->generation;
735 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
736 device->writeable = 0;
738 device->writeable = !bdev_read_only(bdev);
742 q = bdev_get_queue(bdev);
743 if (blk_queue_discard(q)) {
744 device->can_discard = 1;
745 fs_devices->num_can_discard++;
749 device->in_fs_metadata = 0;
750 device->mode = flags;
752 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
753 fs_devices->rotating = 1;
755 fs_devices->open_devices++;
756 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
757 fs_devices->rw_devices++;
758 list_add(&device->dev_alloc_list,
759 &fs_devices->alloc_list);
766 blkdev_put(bdev, flags);
769 if (fs_devices->open_devices == 0) {
773 fs_devices->seeding = seeding;
774 fs_devices->opened = 1;
775 fs_devices->latest_bdev = latest_bdev;
776 fs_devices->latest_devid = latest_devid;
777 fs_devices->latest_trans = latest_transid;
778 fs_devices->total_rw_bytes = 0;
783 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
784 fmode_t flags, void *holder)
788 mutex_lock(&uuid_mutex);
789 if (fs_devices->opened) {
790 fs_devices->opened++;
793 ret = __btrfs_open_devices(fs_devices, flags, holder);
795 mutex_unlock(&uuid_mutex);
800 * Look for a btrfs signature on a device. This may be called out of the mount path
801 * and we are not allowed to call set_blocksize during the scan. The superblock
802 * is read via pagecache
804 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
805 struct btrfs_fs_devices **fs_devices_ret)
807 struct btrfs_super_block *disk_super;
808 struct block_device *bdev;
819 * we would like to check all the supers, but that would make
820 * a btrfs mount succeed after a mkfs from a different FS.
821 * So, we need to add a special mount option to scan for
822 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
824 bytenr = btrfs_sb_offset(0);
826 mutex_lock(&uuid_mutex);
828 bdev = blkdev_get_by_path(path, flags, holder);
832 printk(KERN_INFO "btrfs: open %s failed\n", path);
836 /* make sure our super fits in the device */
837 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
840 /* make sure our super fits in the page */
841 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
844 /* make sure our super doesn't straddle pages on disk */
845 index = bytenr >> PAGE_CACHE_SHIFT;
846 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
849 /* pull in the page with our super */
850 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
853 if (IS_ERR_OR_NULL(page))
858 /* align our pointer to the offset of the super block */
859 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
861 if (btrfs_super_bytenr(disk_super) != bytenr ||
862 disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
865 devid = btrfs_stack_device_id(&disk_super->dev_item);
866 transid = btrfs_super_generation(disk_super);
867 total_devices = btrfs_super_num_devices(disk_super);
869 if (disk_super->label[0]) {
870 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
871 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
872 printk(KERN_INFO "device label %s ", disk_super->label);
874 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
877 printk(KERN_CONT "devid %llu transid %llu %s\n",
878 (unsigned long long)devid, (unsigned long long)transid, path);
880 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
881 if (!ret && fs_devices_ret)
882 (*fs_devices_ret)->total_devices = total_devices;
886 page_cache_release(page);
889 blkdev_put(bdev, flags);
891 mutex_unlock(&uuid_mutex);
895 /* helper to account the used device space in the range */
896 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
897 u64 end, u64 *length)
899 struct btrfs_key key;
900 struct btrfs_root *root = device->dev_root;
901 struct btrfs_dev_extent *dev_extent;
902 struct btrfs_path *path;
906 struct extent_buffer *l;
910 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
913 path = btrfs_alloc_path();
918 key.objectid = device->devid;
920 key.type = BTRFS_DEV_EXTENT_KEY;
922 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
926 ret = btrfs_previous_item(root, path, key.objectid, key.type);
933 slot = path->slots[0];
934 if (slot >= btrfs_header_nritems(l)) {
935 ret = btrfs_next_leaf(root, path);
943 btrfs_item_key_to_cpu(l, &key, slot);
945 if (key.objectid < device->devid)
948 if (key.objectid > device->devid)
951 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
954 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
955 extent_end = key.offset + btrfs_dev_extent_length(l,
957 if (key.offset <= start && extent_end > end) {
958 *length = end - start + 1;
960 } else if (key.offset <= start && extent_end > start)
961 *length += extent_end - start;
962 else if (key.offset > start && extent_end <= end)
963 *length += extent_end - key.offset;
964 else if (key.offset > start && key.offset <= end) {
965 *length += end - key.offset + 1;
967 } else if (key.offset > end)
975 btrfs_free_path(path);
980 * find_free_dev_extent - find free space in the specified device
981 * @device: the device which we search the free space in
982 * @num_bytes: the size of the free space that we need
983 * @start: store the start of the free space.
984 * @len: the size of the free space. that we find, or the size of the max
985 * free space if we don't find suitable free space
987 * this uses a pretty simple search, the expectation is that it is
988 * called very infrequently and that a given device has a small number
991 * @start is used to store the start of the free space if we find. But if we
992 * don't find suitable free space, it will be used to store the start position
993 * of the max free space.
995 * @len is used to store the size of the free space that we find.
996 * But if we don't find suitable free space, it is used to store the size of
997 * the max free space.
999 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
1000 u64 *start, u64 *len)
1002 struct btrfs_key key;
1003 struct btrfs_root *root = device->dev_root;
1004 struct btrfs_dev_extent *dev_extent;
1005 struct btrfs_path *path;
1011 u64 search_end = device->total_bytes;
1014 struct extent_buffer *l;
1016 /* FIXME use last free of some kind */
1018 /* we don't want to overwrite the superblock on the drive,
1019 * so we make sure to start at an offset of at least 1MB
1021 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1023 max_hole_start = search_start;
1027 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1032 path = btrfs_alloc_path();
1039 key.objectid = device->devid;
1040 key.offset = search_start;
1041 key.type = BTRFS_DEV_EXTENT_KEY;
1043 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1047 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1054 slot = path->slots[0];
1055 if (slot >= btrfs_header_nritems(l)) {
1056 ret = btrfs_next_leaf(root, path);
1064 btrfs_item_key_to_cpu(l, &key, slot);
1066 if (key.objectid < device->devid)
1069 if (key.objectid > device->devid)
1072 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1075 if (key.offset > search_start) {
1076 hole_size = key.offset - search_start;
1078 if (hole_size > max_hole_size) {
1079 max_hole_start = search_start;
1080 max_hole_size = hole_size;
1084 * If this free space is greater than which we need,
1085 * it must be the max free space that we have found
1086 * until now, so max_hole_start must point to the start
1087 * of this free space and the length of this free space
1088 * is stored in max_hole_size. Thus, we return
1089 * max_hole_start and max_hole_size and go back to the
1092 if (hole_size >= num_bytes) {
1098 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1099 extent_end = key.offset + btrfs_dev_extent_length(l,
1101 if (extent_end > search_start)
1102 search_start = extent_end;
1109 * At this point, search_start should be the end of
1110 * allocated dev extents, and when shrinking the device,
1111 * search_end may be smaller than search_start.
1113 if (search_end > search_start)
1114 hole_size = search_end - search_start;
1116 if (hole_size > max_hole_size) {
1117 max_hole_start = search_start;
1118 max_hole_size = hole_size;
1122 if (hole_size < num_bytes)
1128 btrfs_free_path(path);
1130 *start = max_hole_start;
1132 *len = max_hole_size;
1136 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1137 struct btrfs_device *device,
1141 struct btrfs_path *path;
1142 struct btrfs_root *root = device->dev_root;
1143 struct btrfs_key key;
1144 struct btrfs_key found_key;
1145 struct extent_buffer *leaf = NULL;
1146 struct btrfs_dev_extent *extent = NULL;
1148 path = btrfs_alloc_path();
1152 key.objectid = device->devid;
1154 key.type = BTRFS_DEV_EXTENT_KEY;
1156 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1158 ret = btrfs_previous_item(root, path, key.objectid,
1159 BTRFS_DEV_EXTENT_KEY);
1162 leaf = path->nodes[0];
1163 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1164 extent = btrfs_item_ptr(leaf, path->slots[0],
1165 struct btrfs_dev_extent);
1166 BUG_ON(found_key.offset > start || found_key.offset +
1167 btrfs_dev_extent_length(leaf, extent) < start);
1169 btrfs_release_path(path);
1171 } else if (ret == 0) {
1172 leaf = path->nodes[0];
1173 extent = btrfs_item_ptr(leaf, path->slots[0],
1174 struct btrfs_dev_extent);
1176 btrfs_error(root->fs_info, ret, "Slot search failed");
1180 if (device->bytes_used > 0) {
1181 u64 len = btrfs_dev_extent_length(leaf, extent);
1182 device->bytes_used -= len;
1183 spin_lock(&root->fs_info->free_chunk_lock);
1184 root->fs_info->free_chunk_space += len;
1185 spin_unlock(&root->fs_info->free_chunk_lock);
1187 ret = btrfs_del_item(trans, root, path);
1189 btrfs_error(root->fs_info, ret,
1190 "Failed to remove dev extent item");
1193 btrfs_free_path(path);
1197 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1198 struct btrfs_device *device,
1199 u64 chunk_tree, u64 chunk_objectid,
1200 u64 chunk_offset, u64 start, u64 num_bytes)
1203 struct btrfs_path *path;
1204 struct btrfs_root *root = device->dev_root;
1205 struct btrfs_dev_extent *extent;
1206 struct extent_buffer *leaf;
1207 struct btrfs_key key;
1209 WARN_ON(!device->in_fs_metadata);
1210 WARN_ON(device->is_tgtdev_for_dev_replace);
1211 path = btrfs_alloc_path();
1215 key.objectid = device->devid;
1217 key.type = BTRFS_DEV_EXTENT_KEY;
1218 ret = btrfs_insert_empty_item(trans, root, path, &key,
1223 leaf = path->nodes[0];
1224 extent = btrfs_item_ptr(leaf, path->slots[0],
1225 struct btrfs_dev_extent);
1226 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1227 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1228 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1230 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1231 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1234 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1235 btrfs_mark_buffer_dirty(leaf);
1237 btrfs_free_path(path);
1241 static noinline int find_next_chunk(struct btrfs_root *root,
1242 u64 objectid, u64 *offset)
1244 struct btrfs_path *path;
1246 struct btrfs_key key;
1247 struct btrfs_chunk *chunk;
1248 struct btrfs_key found_key;
1250 path = btrfs_alloc_path();
1254 key.objectid = objectid;
1255 key.offset = (u64)-1;
1256 key.type = BTRFS_CHUNK_ITEM_KEY;
1258 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1262 BUG_ON(ret == 0); /* Corruption */
1264 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1268 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1270 if (found_key.objectid != objectid)
1273 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1274 struct btrfs_chunk);
1275 *offset = found_key.offset +
1276 btrfs_chunk_length(path->nodes[0], chunk);
1281 btrfs_free_path(path);
1285 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1288 struct btrfs_key key;
1289 struct btrfs_key found_key;
1290 struct btrfs_path *path;
1292 root = root->fs_info->chunk_root;
1294 path = btrfs_alloc_path();
1298 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1299 key.type = BTRFS_DEV_ITEM_KEY;
1300 key.offset = (u64)-1;
1302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1306 BUG_ON(ret == 0); /* Corruption */
1308 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1309 BTRFS_DEV_ITEM_KEY);
1313 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1315 *objectid = found_key.offset + 1;
1319 btrfs_free_path(path);
1324 * the device information is stored in the chunk root
1325 * the btrfs_device struct should be fully filled in
1327 int btrfs_add_device(struct btrfs_trans_handle *trans,
1328 struct btrfs_root *root,
1329 struct btrfs_device *device)
1332 struct btrfs_path *path;
1333 struct btrfs_dev_item *dev_item;
1334 struct extent_buffer *leaf;
1335 struct btrfs_key key;
1338 root = root->fs_info->chunk_root;
1340 path = btrfs_alloc_path();
1344 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1345 key.type = BTRFS_DEV_ITEM_KEY;
1346 key.offset = device->devid;
1348 ret = btrfs_insert_empty_item(trans, root, path, &key,
1353 leaf = path->nodes[0];
1354 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1356 btrfs_set_device_id(leaf, dev_item, device->devid);
1357 btrfs_set_device_generation(leaf, dev_item, 0);
1358 btrfs_set_device_type(leaf, dev_item, device->type);
1359 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1360 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1361 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1362 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1363 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1364 btrfs_set_device_group(leaf, dev_item, 0);
1365 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1366 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1367 btrfs_set_device_start_offset(leaf, dev_item, 0);
1369 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1370 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1371 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1372 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1373 btrfs_mark_buffer_dirty(leaf);
1377 btrfs_free_path(path);
1381 static int btrfs_rm_dev_item(struct btrfs_root *root,
1382 struct btrfs_device *device)
1385 struct btrfs_path *path;
1386 struct btrfs_key key;
1387 struct btrfs_trans_handle *trans;
1389 root = root->fs_info->chunk_root;
1391 path = btrfs_alloc_path();
1395 trans = btrfs_start_transaction(root, 0);
1396 if (IS_ERR(trans)) {
1397 btrfs_free_path(path);
1398 return PTR_ERR(trans);
1400 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1401 key.type = BTRFS_DEV_ITEM_KEY;
1402 key.offset = device->devid;
1405 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1414 ret = btrfs_del_item(trans, root, path);
1418 btrfs_free_path(path);
1419 unlock_chunks(root);
1420 btrfs_commit_transaction(trans, root);
1424 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1426 struct btrfs_device *device;
1427 struct btrfs_device *next_device;
1428 struct block_device *bdev;
1429 struct buffer_head *bh = NULL;
1430 struct btrfs_super_block *disk_super;
1431 struct btrfs_fs_devices *cur_devices;
1438 bool clear_super = false;
1440 mutex_lock(&uuid_mutex);
1443 seq = read_seqbegin(&root->fs_info->profiles_lock);
1445 all_avail = root->fs_info->avail_data_alloc_bits |
1446 root->fs_info->avail_system_alloc_bits |
1447 root->fs_info->avail_metadata_alloc_bits;
1448 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1450 num_devices = root->fs_info->fs_devices->num_devices;
1451 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1452 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1453 WARN_ON(num_devices < 1);
1456 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1458 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1459 printk(KERN_ERR "btrfs: unable to go below four devices "
1465 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1466 printk(KERN_ERR "btrfs: unable to go below two "
1467 "devices on raid1\n");
1472 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1473 root->fs_info->fs_devices->rw_devices <= 2) {
1474 printk(KERN_ERR "btrfs: unable to go below two "
1475 "devices on raid5\n");
1479 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1480 root->fs_info->fs_devices->rw_devices <= 3) {
1481 printk(KERN_ERR "btrfs: unable to go below three "
1482 "devices on raid6\n");
1487 if (strcmp(device_path, "missing") == 0) {
1488 struct list_head *devices;
1489 struct btrfs_device *tmp;
1492 devices = &root->fs_info->fs_devices->devices;
1494 * It is safe to read the devices since the volume_mutex
1497 list_for_each_entry(tmp, devices, dev_list) {
1498 if (tmp->in_fs_metadata &&
1499 !tmp->is_tgtdev_for_dev_replace &&
1509 printk(KERN_ERR "btrfs: no missing devices found to "
1514 ret = btrfs_get_bdev_and_sb(device_path,
1515 FMODE_WRITE | FMODE_EXCL,
1516 root->fs_info->bdev_holder, 0,
1520 disk_super = (struct btrfs_super_block *)bh->b_data;
1521 devid = btrfs_stack_device_id(&disk_super->dev_item);
1522 dev_uuid = disk_super->dev_item.uuid;
1523 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1531 if (device->is_tgtdev_for_dev_replace) {
1532 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1537 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1538 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1544 if (device->writeable) {
1546 list_del_init(&device->dev_alloc_list);
1547 unlock_chunks(root);
1548 root->fs_info->fs_devices->rw_devices--;
1552 ret = btrfs_shrink_device(device, 0);
1557 * TODO: the superblock still includes this device in its num_devices
1558 * counter although write_all_supers() is not locked out. This
1559 * could give a filesystem state which requires a degraded mount.
1561 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1565 spin_lock(&root->fs_info->free_chunk_lock);
1566 root->fs_info->free_chunk_space = device->total_bytes -
1568 spin_unlock(&root->fs_info->free_chunk_lock);
1570 device->in_fs_metadata = 0;
1571 btrfs_scrub_cancel_dev(root->fs_info, device);
1574 * the device list mutex makes sure that we don't change
1575 * the device list while someone else is writing out all
1576 * the device supers.
1579 cur_devices = device->fs_devices;
1580 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1581 list_del_rcu(&device->dev_list);
1583 device->fs_devices->num_devices--;
1584 device->fs_devices->total_devices--;
1586 if (device->missing)
1587 root->fs_info->fs_devices->missing_devices--;
1589 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1590 struct btrfs_device, dev_list);
1591 if (device->bdev == root->fs_info->sb->s_bdev)
1592 root->fs_info->sb->s_bdev = next_device->bdev;
1593 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1594 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1597 device->fs_devices->open_devices--;
1599 call_rcu(&device->rcu, free_device);
1600 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1602 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1603 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1605 if (cur_devices->open_devices == 0) {
1606 struct btrfs_fs_devices *fs_devices;
1607 fs_devices = root->fs_info->fs_devices;
1608 while (fs_devices) {
1609 if (fs_devices->seed == cur_devices)
1611 fs_devices = fs_devices->seed;
1613 fs_devices->seed = cur_devices->seed;
1614 cur_devices->seed = NULL;
1616 __btrfs_close_devices(cur_devices);
1617 unlock_chunks(root);
1618 free_fs_devices(cur_devices);
1621 root->fs_info->num_tolerated_disk_barrier_failures =
1622 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1625 * at this point, the device is zero sized. We want to
1626 * remove it from the devices list and zero out the old super
1628 if (clear_super && disk_super) {
1629 /* make sure this device isn't detected as part of
1632 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1633 set_buffer_dirty(bh);
1634 sync_dirty_buffer(bh);
1639 /* Notify udev that device has changed */
1641 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1646 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1648 mutex_unlock(&uuid_mutex);
1651 if (device->writeable) {
1653 list_add(&device->dev_alloc_list,
1654 &root->fs_info->fs_devices->alloc_list);
1655 unlock_chunks(root);
1656 root->fs_info->fs_devices->rw_devices++;
1661 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1662 struct btrfs_device *srcdev)
1664 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1665 list_del_rcu(&srcdev->dev_list);
1666 list_del_rcu(&srcdev->dev_alloc_list);
1667 fs_info->fs_devices->num_devices--;
1668 if (srcdev->missing) {
1669 fs_info->fs_devices->missing_devices--;
1670 fs_info->fs_devices->rw_devices++;
1672 if (srcdev->can_discard)
1673 fs_info->fs_devices->num_can_discard--;
1675 fs_info->fs_devices->open_devices--;
1677 call_rcu(&srcdev->rcu, free_device);
1680 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1681 struct btrfs_device *tgtdev)
1683 struct btrfs_device *next_device;
1686 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1688 btrfs_scratch_superblock(tgtdev);
1689 fs_info->fs_devices->open_devices--;
1691 fs_info->fs_devices->num_devices--;
1692 if (tgtdev->can_discard)
1693 fs_info->fs_devices->num_can_discard++;
1695 next_device = list_entry(fs_info->fs_devices->devices.next,
1696 struct btrfs_device, dev_list);
1697 if (tgtdev->bdev == fs_info->sb->s_bdev)
1698 fs_info->sb->s_bdev = next_device->bdev;
1699 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1700 fs_info->fs_devices->latest_bdev = next_device->bdev;
1701 list_del_rcu(&tgtdev->dev_list);
1703 call_rcu(&tgtdev->rcu, free_device);
1705 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1708 int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1709 struct btrfs_device **device)
1712 struct btrfs_super_block *disk_super;
1715 struct block_device *bdev;
1716 struct buffer_head *bh;
1719 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1720 root->fs_info->bdev_holder, 0, &bdev, &bh);
1723 disk_super = (struct btrfs_super_block *)bh->b_data;
1724 devid = btrfs_stack_device_id(&disk_super->dev_item);
1725 dev_uuid = disk_super->dev_item.uuid;
1726 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1731 blkdev_put(bdev, FMODE_READ);
1735 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1737 struct btrfs_device **device)
1740 if (strcmp(device_path, "missing") == 0) {
1741 struct list_head *devices;
1742 struct btrfs_device *tmp;
1744 devices = &root->fs_info->fs_devices->devices;
1746 * It is safe to read the devices since the volume_mutex
1747 * is held by the caller.
1749 list_for_each_entry(tmp, devices, dev_list) {
1750 if (tmp->in_fs_metadata && !tmp->bdev) {
1757 pr_err("btrfs: no missing device found\n");
1763 return btrfs_find_device_by_path(root, device_path, device);
1768 * does all the dirty work required for changing file system's UUID.
1770 static int btrfs_prepare_sprout(struct btrfs_root *root)
1772 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1773 struct btrfs_fs_devices *old_devices;
1774 struct btrfs_fs_devices *seed_devices;
1775 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1776 struct btrfs_device *device;
1779 BUG_ON(!mutex_is_locked(&uuid_mutex));
1780 if (!fs_devices->seeding)
1783 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1787 old_devices = clone_fs_devices(fs_devices);
1788 if (IS_ERR(old_devices)) {
1789 kfree(seed_devices);
1790 return PTR_ERR(old_devices);
1793 list_add(&old_devices->list, &fs_uuids);
1795 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1796 seed_devices->opened = 1;
1797 INIT_LIST_HEAD(&seed_devices->devices);
1798 INIT_LIST_HEAD(&seed_devices->alloc_list);
1799 mutex_init(&seed_devices->device_list_mutex);
1801 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1802 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1804 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1806 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1807 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1808 device->fs_devices = seed_devices;
1811 fs_devices->seeding = 0;
1812 fs_devices->num_devices = 0;
1813 fs_devices->open_devices = 0;
1814 fs_devices->total_devices = 0;
1815 fs_devices->seed = seed_devices;
1817 generate_random_uuid(fs_devices->fsid);
1818 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1819 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1820 super_flags = btrfs_super_flags(disk_super) &
1821 ~BTRFS_SUPER_FLAG_SEEDING;
1822 btrfs_set_super_flags(disk_super, super_flags);
1828 * strore the expected generation for seed devices in device items.
1830 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root)
1833 struct btrfs_path *path;
1834 struct extent_buffer *leaf;
1835 struct btrfs_dev_item *dev_item;
1836 struct btrfs_device *device;
1837 struct btrfs_key key;
1838 u8 fs_uuid[BTRFS_UUID_SIZE];
1839 u8 dev_uuid[BTRFS_UUID_SIZE];
1843 path = btrfs_alloc_path();
1847 root = root->fs_info->chunk_root;
1848 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1850 key.type = BTRFS_DEV_ITEM_KEY;
1853 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1857 leaf = path->nodes[0];
1859 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1860 ret = btrfs_next_leaf(root, path);
1865 leaf = path->nodes[0];
1866 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1867 btrfs_release_path(path);
1871 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1872 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1873 key.type != BTRFS_DEV_ITEM_KEY)
1876 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1877 struct btrfs_dev_item);
1878 devid = btrfs_device_id(leaf, dev_item);
1879 read_extent_buffer(leaf, dev_uuid,
1880 (unsigned long)btrfs_device_uuid(dev_item),
1882 read_extent_buffer(leaf, fs_uuid,
1883 (unsigned long)btrfs_device_fsid(dev_item),
1885 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1887 BUG_ON(!device); /* Logic error */
1889 if (device->fs_devices->seeding) {
1890 btrfs_set_device_generation(leaf, dev_item,
1891 device->generation);
1892 btrfs_mark_buffer_dirty(leaf);
1900 btrfs_free_path(path);
1904 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1906 struct request_queue *q;
1907 struct btrfs_trans_handle *trans;
1908 struct btrfs_device *device;
1909 struct block_device *bdev;
1910 struct list_head *devices;
1911 struct super_block *sb = root->fs_info->sb;
1912 struct rcu_string *name;
1914 int seeding_dev = 0;
1917 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1920 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1921 root->fs_info->bdev_holder);
1923 return PTR_ERR(bdev);
1925 if (root->fs_info->fs_devices->seeding) {
1927 down_write(&sb->s_umount);
1928 mutex_lock(&uuid_mutex);
1931 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1933 devices = &root->fs_info->fs_devices->devices;
1935 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1936 list_for_each_entry(device, devices, dev_list) {
1937 if (device->bdev == bdev) {
1940 &root->fs_info->fs_devices->device_list_mutex);
1944 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1946 device = kzalloc(sizeof(*device), GFP_NOFS);
1948 /* we can safely leave the fs_devices entry around */
1953 name = rcu_string_strdup(device_path, GFP_NOFS);
1959 rcu_assign_pointer(device->name, name);
1961 ret = find_next_devid(root, &device->devid);
1963 rcu_string_free(device->name);
1968 trans = btrfs_start_transaction(root, 0);
1969 if (IS_ERR(trans)) {
1970 rcu_string_free(device->name);
1972 ret = PTR_ERR(trans);
1978 q = bdev_get_queue(bdev);
1979 if (blk_queue_discard(q))
1980 device->can_discard = 1;
1981 device->writeable = 1;
1982 device->work.func = pending_bios_fn;
1983 generate_random_uuid(device->uuid);
1984 spin_lock_init(&device->io_lock);
1985 device->generation = trans->transid;
1986 device->io_width = root->sectorsize;
1987 device->io_align = root->sectorsize;
1988 device->sector_size = root->sectorsize;
1989 device->total_bytes = i_size_read(bdev->bd_inode);
1990 device->disk_total_bytes = device->total_bytes;
1991 device->dev_root = root->fs_info->dev_root;
1992 device->bdev = bdev;
1993 device->in_fs_metadata = 1;
1994 device->is_tgtdev_for_dev_replace = 0;
1995 device->mode = FMODE_EXCL;
1996 set_blocksize(device->bdev, 4096);
1999 sb->s_flags &= ~MS_RDONLY;
2000 ret = btrfs_prepare_sprout(root);
2001 BUG_ON(ret); /* -ENOMEM */
2004 device->fs_devices = root->fs_info->fs_devices;
2006 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2007 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2008 list_add(&device->dev_alloc_list,
2009 &root->fs_info->fs_devices->alloc_list);
2010 root->fs_info->fs_devices->num_devices++;
2011 root->fs_info->fs_devices->open_devices++;
2012 root->fs_info->fs_devices->rw_devices++;
2013 root->fs_info->fs_devices->total_devices++;
2014 if (device->can_discard)
2015 root->fs_info->fs_devices->num_can_discard++;
2016 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2018 spin_lock(&root->fs_info->free_chunk_lock);
2019 root->fs_info->free_chunk_space += device->total_bytes;
2020 spin_unlock(&root->fs_info->free_chunk_lock);
2022 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2023 root->fs_info->fs_devices->rotating = 1;
2025 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2026 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2027 total_bytes + device->total_bytes);
2029 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2030 btrfs_set_super_num_devices(root->fs_info->super_copy,
2032 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2035 ret = init_first_rw_device(trans, root, device);
2037 btrfs_abort_transaction(trans, root, ret);
2040 ret = btrfs_finish_sprout(trans, root);
2042 btrfs_abort_transaction(trans, root, ret);
2046 ret = btrfs_add_device(trans, root, device);
2048 btrfs_abort_transaction(trans, root, ret);
2054 * we've got more storage, clear any full flags on the space
2057 btrfs_clear_space_info_full(root->fs_info);
2059 unlock_chunks(root);
2060 root->fs_info->num_tolerated_disk_barrier_failures =
2061 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2062 ret = btrfs_commit_transaction(trans, root);
2065 mutex_unlock(&uuid_mutex);
2066 up_write(&sb->s_umount);
2068 if (ret) /* transaction commit */
2071 ret = btrfs_relocate_sys_chunks(root);
2073 btrfs_error(root->fs_info, ret,
2074 "Failed to relocate sys chunks after "
2075 "device initialization. This can be fixed "
2076 "using the \"btrfs balance\" command.");
2077 trans = btrfs_attach_transaction(root);
2078 if (IS_ERR(trans)) {
2079 if (PTR_ERR(trans) == -ENOENT)
2081 return PTR_ERR(trans);
2083 ret = btrfs_commit_transaction(trans, root);
2089 unlock_chunks(root);
2090 btrfs_end_transaction(trans, root);
2091 rcu_string_free(device->name);
2094 blkdev_put(bdev, FMODE_EXCL);
2096 mutex_unlock(&uuid_mutex);
2097 up_write(&sb->s_umount);
2102 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2103 struct btrfs_device **device_out)
2105 struct request_queue *q;
2106 struct btrfs_device *device;
2107 struct block_device *bdev;
2108 struct btrfs_fs_info *fs_info = root->fs_info;
2109 struct list_head *devices;
2110 struct rcu_string *name;
2114 if (fs_info->fs_devices->seeding)
2117 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2118 fs_info->bdev_holder);
2120 return PTR_ERR(bdev);
2122 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2124 devices = &fs_info->fs_devices->devices;
2125 list_for_each_entry(device, devices, dev_list) {
2126 if (device->bdev == bdev) {
2132 device = kzalloc(sizeof(*device), GFP_NOFS);
2138 name = rcu_string_strdup(device_path, GFP_NOFS);
2144 rcu_assign_pointer(device->name, name);
2146 q = bdev_get_queue(bdev);
2147 if (blk_queue_discard(q))
2148 device->can_discard = 1;
2149 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2150 device->writeable = 1;
2151 device->work.func = pending_bios_fn;
2152 generate_random_uuid(device->uuid);
2153 device->devid = BTRFS_DEV_REPLACE_DEVID;
2154 spin_lock_init(&device->io_lock);
2155 device->generation = 0;
2156 device->io_width = root->sectorsize;
2157 device->io_align = root->sectorsize;
2158 device->sector_size = root->sectorsize;
2159 device->total_bytes = i_size_read(bdev->bd_inode);
2160 device->disk_total_bytes = device->total_bytes;
2161 device->dev_root = fs_info->dev_root;
2162 device->bdev = bdev;
2163 device->in_fs_metadata = 1;
2164 device->is_tgtdev_for_dev_replace = 1;
2165 device->mode = FMODE_EXCL;
2166 set_blocksize(device->bdev, 4096);
2167 device->fs_devices = fs_info->fs_devices;
2168 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2169 fs_info->fs_devices->num_devices++;
2170 fs_info->fs_devices->open_devices++;
2171 if (device->can_discard)
2172 fs_info->fs_devices->num_can_discard++;
2173 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2175 *device_out = device;
2179 blkdev_put(bdev, FMODE_EXCL);
2183 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2184 struct btrfs_device *tgtdev)
2186 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2187 tgtdev->io_width = fs_info->dev_root->sectorsize;
2188 tgtdev->io_align = fs_info->dev_root->sectorsize;
2189 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2190 tgtdev->dev_root = fs_info->dev_root;
2191 tgtdev->in_fs_metadata = 1;
2194 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2195 struct btrfs_device *device)
2198 struct btrfs_path *path;
2199 struct btrfs_root *root;
2200 struct btrfs_dev_item *dev_item;
2201 struct extent_buffer *leaf;
2202 struct btrfs_key key;
2204 root = device->dev_root->fs_info->chunk_root;
2206 path = btrfs_alloc_path();
2210 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2211 key.type = BTRFS_DEV_ITEM_KEY;
2212 key.offset = device->devid;
2214 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2223 leaf = path->nodes[0];
2224 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2226 btrfs_set_device_id(leaf, dev_item, device->devid);
2227 btrfs_set_device_type(leaf, dev_item, device->type);
2228 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2229 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2230 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2231 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
2232 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2233 btrfs_mark_buffer_dirty(leaf);
2236 btrfs_free_path(path);
2240 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
2241 struct btrfs_device *device, u64 new_size)
2243 struct btrfs_super_block *super_copy =
2244 device->dev_root->fs_info->super_copy;
2245 u64 old_total = btrfs_super_total_bytes(super_copy);
2246 u64 diff = new_size - device->total_bytes;
2248 if (!device->writeable)
2250 if (new_size <= device->total_bytes ||
2251 device->is_tgtdev_for_dev_replace)
2254 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2255 device->fs_devices->total_rw_bytes += diff;
2257 device->total_bytes = new_size;
2258 device->disk_total_bytes = new_size;
2259 btrfs_clear_space_info_full(device->dev_root->fs_info);
2261 return btrfs_update_device(trans, device);
2264 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2265 struct btrfs_device *device, u64 new_size)
2268 lock_chunks(device->dev_root);
2269 ret = __btrfs_grow_device(trans, device, new_size);
2270 unlock_chunks(device->dev_root);
2274 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2275 struct btrfs_root *root,
2276 u64 chunk_tree, u64 chunk_objectid,
2280 struct btrfs_path *path;
2281 struct btrfs_key key;
2283 root = root->fs_info->chunk_root;
2284 path = btrfs_alloc_path();
2288 key.objectid = chunk_objectid;
2289 key.offset = chunk_offset;
2290 key.type = BTRFS_CHUNK_ITEM_KEY;
2292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2295 else if (ret > 0) { /* Logic error or corruption */
2296 btrfs_error(root->fs_info, -ENOENT,
2297 "Failed lookup while freeing chunk.");
2302 ret = btrfs_del_item(trans, root, path);
2304 btrfs_error(root->fs_info, ret,
2305 "Failed to delete chunk item.");
2307 btrfs_free_path(path);
2311 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2314 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2315 struct btrfs_disk_key *disk_key;
2316 struct btrfs_chunk *chunk;
2323 struct btrfs_key key;
2325 array_size = btrfs_super_sys_array_size(super_copy);
2327 ptr = super_copy->sys_chunk_array;
2330 while (cur < array_size) {
2331 disk_key = (struct btrfs_disk_key *)ptr;
2332 btrfs_disk_key_to_cpu(&key, disk_key);
2334 len = sizeof(*disk_key);
2336 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2337 chunk = (struct btrfs_chunk *)(ptr + len);
2338 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2339 len += btrfs_chunk_item_size(num_stripes);
2344 if (key.objectid == chunk_objectid &&
2345 key.offset == chunk_offset) {
2346 memmove(ptr, ptr + len, array_size - (cur + len));
2348 btrfs_set_super_sys_array_size(super_copy, array_size);
2357 static int btrfs_relocate_chunk(struct btrfs_root *root,
2358 u64 chunk_tree, u64 chunk_objectid,
2361 struct extent_map_tree *em_tree;
2362 struct btrfs_root *extent_root;
2363 struct btrfs_trans_handle *trans;
2364 struct extent_map *em;
2365 struct map_lookup *map;
2369 root = root->fs_info->chunk_root;
2370 extent_root = root->fs_info->extent_root;
2371 em_tree = &root->fs_info->mapping_tree.map_tree;
2373 ret = btrfs_can_relocate(extent_root, chunk_offset);
2377 /* step one, relocate all the extents inside this chunk */
2378 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2382 trans = btrfs_start_transaction(root, 0);
2383 BUG_ON(IS_ERR(trans));
2388 * step two, delete the device extents and the
2389 * chunk tree entries
2391 read_lock(&em_tree->lock);
2392 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2393 read_unlock(&em_tree->lock);
2395 BUG_ON(!em || em->start > chunk_offset ||
2396 em->start + em->len < chunk_offset);
2397 map = (struct map_lookup *)em->bdev;
2399 for (i = 0; i < map->num_stripes; i++) {
2400 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2401 map->stripes[i].physical);
2404 if (map->stripes[i].dev) {
2405 ret = btrfs_update_device(trans, map->stripes[i].dev);
2409 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2414 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2416 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2417 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2421 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2424 write_lock(&em_tree->lock);
2425 remove_extent_mapping(em_tree, em);
2426 write_unlock(&em_tree->lock);
2431 /* once for the tree */
2432 free_extent_map(em);
2434 free_extent_map(em);
2436 unlock_chunks(root);
2437 btrfs_end_transaction(trans, root);
2441 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2443 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2444 struct btrfs_path *path;
2445 struct extent_buffer *leaf;
2446 struct btrfs_chunk *chunk;
2447 struct btrfs_key key;
2448 struct btrfs_key found_key;
2449 u64 chunk_tree = chunk_root->root_key.objectid;
2451 bool retried = false;
2455 path = btrfs_alloc_path();
2460 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2461 key.offset = (u64)-1;
2462 key.type = BTRFS_CHUNK_ITEM_KEY;
2465 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2468 BUG_ON(ret == 0); /* Corruption */
2470 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2477 leaf = path->nodes[0];
2478 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2480 chunk = btrfs_item_ptr(leaf, path->slots[0],
2481 struct btrfs_chunk);
2482 chunk_type = btrfs_chunk_type(leaf, chunk);
2483 btrfs_release_path(path);
2485 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2486 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2495 if (found_key.offset == 0)
2497 key.offset = found_key.offset - 1;
2500 if (failed && !retried) {
2504 } else if (failed && retried) {
2509 btrfs_free_path(path);
2513 static int insert_balance_item(struct btrfs_root *root,
2514 struct btrfs_balance_control *bctl)
2516 struct btrfs_trans_handle *trans;
2517 struct btrfs_balance_item *item;
2518 struct btrfs_disk_balance_args disk_bargs;
2519 struct btrfs_path *path;
2520 struct extent_buffer *leaf;
2521 struct btrfs_key key;
2524 path = btrfs_alloc_path();
2528 trans = btrfs_start_transaction(root, 0);
2529 if (IS_ERR(trans)) {
2530 btrfs_free_path(path);
2531 return PTR_ERR(trans);
2534 key.objectid = BTRFS_BALANCE_OBJECTID;
2535 key.type = BTRFS_BALANCE_ITEM_KEY;
2538 ret = btrfs_insert_empty_item(trans, root, path, &key,
2543 leaf = path->nodes[0];
2544 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2546 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2548 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2549 btrfs_set_balance_data(leaf, item, &disk_bargs);
2550 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2551 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2552 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2553 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2555 btrfs_set_balance_flags(leaf, item, bctl->flags);
2557 btrfs_mark_buffer_dirty(leaf);
2559 btrfs_free_path(path);
2560 err = btrfs_commit_transaction(trans, root);
2566 static int del_balance_item(struct btrfs_root *root)
2568 struct btrfs_trans_handle *trans;
2569 struct btrfs_path *path;
2570 struct btrfs_key key;
2573 path = btrfs_alloc_path();
2577 trans = btrfs_start_transaction(root, 0);
2578 if (IS_ERR(trans)) {
2579 btrfs_free_path(path);
2580 return PTR_ERR(trans);
2583 key.objectid = BTRFS_BALANCE_OBJECTID;
2584 key.type = BTRFS_BALANCE_ITEM_KEY;
2587 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2595 ret = btrfs_del_item(trans, root, path);
2597 btrfs_free_path(path);
2598 err = btrfs_commit_transaction(trans, root);
2605 * This is a heuristic used to reduce the number of chunks balanced on
2606 * resume after balance was interrupted.
2608 static void update_balance_args(struct btrfs_balance_control *bctl)
2611 * Turn on soft mode for chunk types that were being converted.
2613 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2614 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2615 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2616 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2617 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2618 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2621 * Turn on usage filter if is not already used. The idea is
2622 * that chunks that we have already balanced should be
2623 * reasonably full. Don't do it for chunks that are being
2624 * converted - that will keep us from relocating unconverted
2625 * (albeit full) chunks.
2627 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2628 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2629 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2630 bctl->data.usage = 90;
2632 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2633 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2634 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2635 bctl->sys.usage = 90;
2637 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2638 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2639 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2640 bctl->meta.usage = 90;
2645 * Should be called with both balance and volume mutexes held to
2646 * serialize other volume operations (add_dev/rm_dev/resize) with
2647 * restriper. Same goes for unset_balance_control.
2649 static void set_balance_control(struct btrfs_balance_control *bctl)
2651 struct btrfs_fs_info *fs_info = bctl->fs_info;
2653 BUG_ON(fs_info->balance_ctl);
2655 spin_lock(&fs_info->balance_lock);
2656 fs_info->balance_ctl = bctl;
2657 spin_unlock(&fs_info->balance_lock);
2660 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2662 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2664 BUG_ON(!fs_info->balance_ctl);
2666 spin_lock(&fs_info->balance_lock);
2667 fs_info->balance_ctl = NULL;
2668 spin_unlock(&fs_info->balance_lock);
2674 * Balance filters. Return 1 if chunk should be filtered out
2675 * (should not be balanced).
2677 static int chunk_profiles_filter(u64 chunk_type,
2678 struct btrfs_balance_args *bargs)
2680 chunk_type = chunk_to_extended(chunk_type) &
2681 BTRFS_EXTENDED_PROFILE_MASK;
2683 if (bargs->profiles & chunk_type)
2689 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2690 struct btrfs_balance_args *bargs)
2692 struct btrfs_block_group_cache *cache;
2693 u64 chunk_used, user_thresh;
2696 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2697 chunk_used = btrfs_block_group_used(&cache->item);
2699 if (bargs->usage == 0)
2701 else if (bargs->usage > 100)
2702 user_thresh = cache->key.offset;
2704 user_thresh = div_factor_fine(cache->key.offset,
2707 if (chunk_used < user_thresh)
2710 btrfs_put_block_group(cache);
2714 static int chunk_devid_filter(struct extent_buffer *leaf,
2715 struct btrfs_chunk *chunk,
2716 struct btrfs_balance_args *bargs)
2718 struct btrfs_stripe *stripe;
2719 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2722 for (i = 0; i < num_stripes; i++) {
2723 stripe = btrfs_stripe_nr(chunk, i);
2724 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2731 /* [pstart, pend) */
2732 static int chunk_drange_filter(struct extent_buffer *leaf,
2733 struct btrfs_chunk *chunk,
2735 struct btrfs_balance_args *bargs)
2737 struct btrfs_stripe *stripe;
2738 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2744 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2747 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2748 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2749 factor = num_stripes / 2;
2750 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2751 factor = num_stripes - 1;
2752 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2753 factor = num_stripes - 2;
2755 factor = num_stripes;
2758 for (i = 0; i < num_stripes; i++) {
2759 stripe = btrfs_stripe_nr(chunk, i);
2760 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2763 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2764 stripe_length = btrfs_chunk_length(leaf, chunk);
2765 do_div(stripe_length, factor);
2767 if (stripe_offset < bargs->pend &&
2768 stripe_offset + stripe_length > bargs->pstart)
2775 /* [vstart, vend) */
2776 static int chunk_vrange_filter(struct extent_buffer *leaf,
2777 struct btrfs_chunk *chunk,
2779 struct btrfs_balance_args *bargs)
2781 if (chunk_offset < bargs->vend &&
2782 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2783 /* at least part of the chunk is inside this vrange */
2789 static int chunk_soft_convert_filter(u64 chunk_type,
2790 struct btrfs_balance_args *bargs)
2792 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2795 chunk_type = chunk_to_extended(chunk_type) &
2796 BTRFS_EXTENDED_PROFILE_MASK;
2798 if (bargs->target == chunk_type)
2804 static int should_balance_chunk(struct btrfs_root *root,
2805 struct extent_buffer *leaf,
2806 struct btrfs_chunk *chunk, u64 chunk_offset)
2808 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2809 struct btrfs_balance_args *bargs = NULL;
2810 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2813 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2814 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2818 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2819 bargs = &bctl->data;
2820 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2822 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2823 bargs = &bctl->meta;
2825 /* profiles filter */
2826 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2827 chunk_profiles_filter(chunk_type, bargs)) {
2832 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2833 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2838 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2839 chunk_devid_filter(leaf, chunk, bargs)) {
2843 /* drange filter, makes sense only with devid filter */
2844 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2845 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2850 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2851 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2855 /* soft profile changing mode */
2856 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2857 chunk_soft_convert_filter(chunk_type, bargs)) {
2864 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
2866 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2867 struct btrfs_root *chunk_root = fs_info->chunk_root;
2868 struct btrfs_root *dev_root = fs_info->dev_root;
2869 struct list_head *devices;
2870 struct btrfs_device *device;
2873 struct btrfs_chunk *chunk;
2874 struct btrfs_path *path;
2875 struct btrfs_key key;
2876 struct btrfs_key found_key;
2877 struct btrfs_trans_handle *trans;
2878 struct extent_buffer *leaf;
2881 int enospc_errors = 0;
2882 bool counting = true;
2884 /* step one make some room on all the devices */
2885 devices = &fs_info->fs_devices->devices;
2886 list_for_each_entry(device, devices, dev_list) {
2887 old_size = device->total_bytes;
2888 size_to_free = div_factor(old_size, 1);
2889 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2890 if (!device->writeable ||
2891 device->total_bytes - device->bytes_used > size_to_free ||
2892 device->is_tgtdev_for_dev_replace)
2895 ret = btrfs_shrink_device(device, old_size - size_to_free);
2900 trans = btrfs_start_transaction(dev_root, 0);
2901 BUG_ON(IS_ERR(trans));
2903 ret = btrfs_grow_device(trans, device, old_size);
2906 btrfs_end_transaction(trans, dev_root);
2909 /* step two, relocate all the chunks */
2910 path = btrfs_alloc_path();
2916 /* zero out stat counters */
2917 spin_lock(&fs_info->balance_lock);
2918 memset(&bctl->stat, 0, sizeof(bctl->stat));
2919 spin_unlock(&fs_info->balance_lock);
2921 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2922 key.offset = (u64)-1;
2923 key.type = BTRFS_CHUNK_ITEM_KEY;
2926 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
2927 atomic_read(&fs_info->balance_cancel_req)) {
2932 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2937 * this shouldn't happen, it means the last relocate
2941 BUG(); /* FIXME break ? */
2943 ret = btrfs_previous_item(chunk_root, path, 0,
2944 BTRFS_CHUNK_ITEM_KEY);
2950 leaf = path->nodes[0];
2951 slot = path->slots[0];
2952 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2954 if (found_key.objectid != key.objectid)
2957 /* chunk zero is special */
2958 if (found_key.offset == 0)
2961 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2964 spin_lock(&fs_info->balance_lock);
2965 bctl->stat.considered++;
2966 spin_unlock(&fs_info->balance_lock);
2969 ret = should_balance_chunk(chunk_root, leaf, chunk,
2971 btrfs_release_path(path);
2976 spin_lock(&fs_info->balance_lock);
2977 bctl->stat.expected++;
2978 spin_unlock(&fs_info->balance_lock);
2982 ret = btrfs_relocate_chunk(chunk_root,
2983 chunk_root->root_key.objectid,
2986 if (ret && ret != -ENOSPC)
2988 if (ret == -ENOSPC) {
2991 spin_lock(&fs_info->balance_lock);
2992 bctl->stat.completed++;
2993 spin_unlock(&fs_info->balance_lock);
2996 key.offset = found_key.offset - 1;
3000 btrfs_release_path(path);
3005 btrfs_free_path(path);
3006 if (enospc_errors) {
3007 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3017 * alloc_profile_is_valid - see if a given profile is valid and reduced
3018 * @flags: profile to validate
3019 * @extended: if true @flags is treated as an extended profile
3021 static int alloc_profile_is_valid(u64 flags, int extended)
3023 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3024 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3026 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3028 /* 1) check that all other bits are zeroed */
3032 /* 2) see if profile is reduced */
3034 return !extended; /* "0" is valid for usual profiles */
3036 /* true if exactly one bit set */
3037 return (flags & (flags - 1)) == 0;
3040 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3042 /* cancel requested || normal exit path */
3043 return atomic_read(&fs_info->balance_cancel_req) ||
3044 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3045 atomic_read(&fs_info->balance_cancel_req) == 0);
3048 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3052 unset_balance_control(fs_info);
3053 ret = del_balance_item(fs_info->tree_root);
3056 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3059 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3060 struct btrfs_ioctl_balance_args *bargs);
3063 * Should be called with both balance and volume mutexes held
3065 int btrfs_balance(struct btrfs_balance_control *bctl,
3066 struct btrfs_ioctl_balance_args *bargs)
3068 struct btrfs_fs_info *fs_info = bctl->fs_info;
3075 if (btrfs_fs_closing(fs_info) ||
3076 atomic_read(&fs_info->balance_pause_req) ||
3077 atomic_read(&fs_info->balance_cancel_req)) {
3082 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3083 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3087 * In case of mixed groups both data and meta should be picked,
3088 * and identical options should be given for both of them.
3090 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3091 if (mixed && (bctl->flags & allowed)) {
3092 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3093 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3094 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3095 printk(KERN_ERR "btrfs: with mixed groups data and "
3096 "metadata balance options must be the same\n");
3102 num_devices = fs_info->fs_devices->num_devices;
3103 btrfs_dev_replace_lock(&fs_info->dev_replace);
3104 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3105 BUG_ON(num_devices < 1);
3108 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3109 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3110 if (num_devices == 1)
3111 allowed |= BTRFS_BLOCK_GROUP_DUP;
3112 else if (num_devices < 4)
3113 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3115 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3116 BTRFS_BLOCK_GROUP_RAID10 |
3117 BTRFS_BLOCK_GROUP_RAID5 |
3118 BTRFS_BLOCK_GROUP_RAID6);
3120 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3121 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3122 (bctl->data.target & ~allowed))) {
3123 printk(KERN_ERR "btrfs: unable to start balance with target "
3124 "data profile %llu\n",
3125 (unsigned long long)bctl->data.target);
3129 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3130 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3131 (bctl->meta.target & ~allowed))) {
3132 printk(KERN_ERR "btrfs: unable to start balance with target "
3133 "metadata profile %llu\n",
3134 (unsigned long long)bctl->meta.target);
3138 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3139 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3140 (bctl->sys.target & ~allowed))) {
3141 printk(KERN_ERR "btrfs: unable to start balance with target "
3142 "system profile %llu\n",
3143 (unsigned long long)bctl->sys.target);
3148 /* allow dup'ed data chunks only in mixed mode */
3149 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3150 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3151 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3156 /* allow to reduce meta or sys integrity only if force set */
3157 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3158 BTRFS_BLOCK_GROUP_RAID10 |
3159 BTRFS_BLOCK_GROUP_RAID5 |
3160 BTRFS_BLOCK_GROUP_RAID6;
3162 seq = read_seqbegin(&fs_info->profiles_lock);
3164 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3165 (fs_info->avail_system_alloc_bits & allowed) &&
3166 !(bctl->sys.target & allowed)) ||
3167 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3168 (fs_info->avail_metadata_alloc_bits & allowed) &&
3169 !(bctl->meta.target & allowed))) {
3170 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3171 printk(KERN_INFO "btrfs: force reducing metadata "
3174 printk(KERN_ERR "btrfs: balance will reduce metadata "
3175 "integrity, use force if you want this\n");
3180 } while (read_seqretry(&fs_info->profiles_lock, seq));
3182 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3183 int num_tolerated_disk_barrier_failures;
3184 u64 target = bctl->sys.target;
3186 num_tolerated_disk_barrier_failures =
3187 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3188 if (num_tolerated_disk_barrier_failures > 0 &&
3190 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3191 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3192 num_tolerated_disk_barrier_failures = 0;
3193 else if (num_tolerated_disk_barrier_failures > 1 &&
3195 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3196 num_tolerated_disk_barrier_failures = 1;
3198 fs_info->num_tolerated_disk_barrier_failures =
3199 num_tolerated_disk_barrier_failures;
3202 ret = insert_balance_item(fs_info->tree_root, bctl);
3203 if (ret && ret != -EEXIST)
3206 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3207 BUG_ON(ret == -EEXIST);
3208 set_balance_control(bctl);
3210 BUG_ON(ret != -EEXIST);
3211 spin_lock(&fs_info->balance_lock);
3212 update_balance_args(bctl);
3213 spin_unlock(&fs_info->balance_lock);
3216 atomic_inc(&fs_info->balance_running);
3217 mutex_unlock(&fs_info->balance_mutex);
3219 ret = __btrfs_balance(fs_info);
3221 mutex_lock(&fs_info->balance_mutex);
3222 atomic_dec(&fs_info->balance_running);
3224 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3225 fs_info->num_tolerated_disk_barrier_failures =
3226 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3230 memset(bargs, 0, sizeof(*bargs));
3231 update_ioctl_balance_args(fs_info, 0, bargs);
3234 wake_up(&fs_info->balance_wait_q);
3238 if (bctl->flags & BTRFS_BALANCE_RESUME)
3239 __cancel_balance(fs_info);
3242 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3247 static int balance_kthread(void *data)
3249 struct btrfs_fs_info *fs_info = data;
3252 mutex_lock(&fs_info->volume_mutex);
3253 mutex_lock(&fs_info->balance_mutex);
3255 if (fs_info->balance_ctl) {
3256 printk(KERN_INFO "btrfs: continuing balance\n");
3257 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3260 mutex_unlock(&fs_info->balance_mutex);
3261 mutex_unlock(&fs_info->volume_mutex);
3266 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3268 struct task_struct *tsk;
3270 spin_lock(&fs_info->balance_lock);
3271 if (!fs_info->balance_ctl) {
3272 spin_unlock(&fs_info->balance_lock);
3275 spin_unlock(&fs_info->balance_lock);
3277 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3278 printk(KERN_INFO "btrfs: force skipping balance\n");
3282 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3284 return PTR_ERR(tsk);
3289 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3291 struct btrfs_balance_control *bctl;
3292 struct btrfs_balance_item *item;
3293 struct btrfs_disk_balance_args disk_bargs;
3294 struct btrfs_path *path;
3295 struct extent_buffer *leaf;
3296 struct btrfs_key key;
3299 path = btrfs_alloc_path();
3303 key.objectid = BTRFS_BALANCE_OBJECTID;
3304 key.type = BTRFS_BALANCE_ITEM_KEY;
3307 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3310 if (ret > 0) { /* ret = -ENOENT; */
3315 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3321 leaf = path->nodes[0];
3322 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3324 bctl->fs_info = fs_info;
3325 bctl->flags = btrfs_balance_flags(leaf, item);
3326 bctl->flags |= BTRFS_BALANCE_RESUME;
3328 btrfs_balance_data(leaf, item, &disk_bargs);
3329 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3330 btrfs_balance_meta(leaf, item, &disk_bargs);
3331 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3332 btrfs_balance_sys(leaf, item, &disk_bargs);
3333 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3335 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3337 mutex_lock(&fs_info->volume_mutex);
3338 mutex_lock(&fs_info->balance_mutex);
3340 set_balance_control(bctl);
3342 mutex_unlock(&fs_info->balance_mutex);
3343 mutex_unlock(&fs_info->volume_mutex);
3345 btrfs_free_path(path);
3349 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3353 mutex_lock(&fs_info->balance_mutex);
3354 if (!fs_info->balance_ctl) {
3355 mutex_unlock(&fs_info->balance_mutex);
3359 if (atomic_read(&fs_info->balance_running)) {
3360 atomic_inc(&fs_info->balance_pause_req);
3361 mutex_unlock(&fs_info->balance_mutex);
3363 wait_event(fs_info->balance_wait_q,
3364 atomic_read(&fs_info->balance_running) == 0);
3366 mutex_lock(&fs_info->balance_mutex);
3367 /* we are good with balance_ctl ripped off from under us */
3368 BUG_ON(atomic_read(&fs_info->balance_running));
3369 atomic_dec(&fs_info->balance_pause_req);
3374 mutex_unlock(&fs_info->balance_mutex);
3378 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3380 mutex_lock(&fs_info->balance_mutex);
3381 if (!fs_info->balance_ctl) {
3382 mutex_unlock(&fs_info->balance_mutex);
3386 atomic_inc(&fs_info->balance_cancel_req);
3388 * if we are running just wait and return, balance item is
3389 * deleted in btrfs_balance in this case
3391 if (atomic_read(&fs_info->balance_running)) {
3392 mutex_unlock(&fs_info->balance_mutex);
3393 wait_event(fs_info->balance_wait_q,
3394 atomic_read(&fs_info->balance_running) == 0);
3395 mutex_lock(&fs_info->balance_mutex);
3397 /* __cancel_balance needs volume_mutex */
3398 mutex_unlock(&fs_info->balance_mutex);
3399 mutex_lock(&fs_info->volume_mutex);
3400 mutex_lock(&fs_info->balance_mutex);
3402 if (fs_info->balance_ctl)
3403 __cancel_balance(fs_info);
3405 mutex_unlock(&fs_info->volume_mutex);
3408 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3409 atomic_dec(&fs_info->balance_cancel_req);
3410 mutex_unlock(&fs_info->balance_mutex);
3415 * shrinking a device means finding all of the device extents past
3416 * the new size, and then following the back refs to the chunks.
3417 * The chunk relocation code actually frees the device extent
3419 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3421 struct btrfs_trans_handle *trans;
3422 struct btrfs_root *root = device->dev_root;
3423 struct btrfs_dev_extent *dev_extent = NULL;
3424 struct btrfs_path *path;
3432 bool retried = false;
3433 struct extent_buffer *l;
3434 struct btrfs_key key;
3435 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3436 u64 old_total = btrfs_super_total_bytes(super_copy);
3437 u64 old_size = device->total_bytes;
3438 u64 diff = device->total_bytes - new_size;
3440 if (device->is_tgtdev_for_dev_replace)
3443 path = btrfs_alloc_path();
3451 device->total_bytes = new_size;
3452 if (device->writeable) {
3453 device->fs_devices->total_rw_bytes -= diff;
3454 spin_lock(&root->fs_info->free_chunk_lock);
3455 root->fs_info->free_chunk_space -= diff;
3456 spin_unlock(&root->fs_info->free_chunk_lock);
3458 unlock_chunks(root);
3461 key.objectid = device->devid;
3462 key.offset = (u64)-1;
3463 key.type = BTRFS_DEV_EXTENT_KEY;
3466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3470 ret = btrfs_previous_item(root, path, 0, key.type);
3475 btrfs_release_path(path);
3480 slot = path->slots[0];
3481 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3483 if (key.objectid != device->devid) {
3484 btrfs_release_path(path);
3488 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3489 length = btrfs_dev_extent_length(l, dev_extent);
3491 if (key.offset + length <= new_size) {
3492 btrfs_release_path(path);
3496 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3497 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3498 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3499 btrfs_release_path(path);
3501 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3503 if (ret && ret != -ENOSPC)
3507 } while (key.offset-- > 0);
3509 if (failed && !retried) {
3513 } else if (failed && retried) {
3517 device->total_bytes = old_size;
3518 if (device->writeable)
3519 device->fs_devices->total_rw_bytes += diff;
3520 spin_lock(&root->fs_info->free_chunk_lock);
3521 root->fs_info->free_chunk_space += diff;
3522 spin_unlock(&root->fs_info->free_chunk_lock);
3523 unlock_chunks(root);
3527 /* Shrinking succeeded, else we would be at "done". */
3528 trans = btrfs_start_transaction(root, 0);
3529 if (IS_ERR(trans)) {
3530 ret = PTR_ERR(trans);
3536 device->disk_total_bytes = new_size;
3537 /* Now btrfs_update_device() will change the on-disk size. */
3538 ret = btrfs_update_device(trans, device);
3540 unlock_chunks(root);
3541 btrfs_end_transaction(trans, root);
3544 WARN_ON(diff > old_total);
3545 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3546 unlock_chunks(root);
3547 btrfs_end_transaction(trans, root);
3549 btrfs_free_path(path);
3553 static int btrfs_add_system_chunk(struct btrfs_root *root,
3554 struct btrfs_key *key,
3555 struct btrfs_chunk *chunk, int item_size)
3557 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3558 struct btrfs_disk_key disk_key;
3562 array_size = btrfs_super_sys_array_size(super_copy);
3563 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3566 ptr = super_copy->sys_chunk_array + array_size;
3567 btrfs_cpu_key_to_disk(&disk_key, key);
3568 memcpy(ptr, &disk_key, sizeof(disk_key));
3569 ptr += sizeof(disk_key);
3570 memcpy(ptr, chunk, item_size);
3571 item_size += sizeof(disk_key);
3572 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3577 * sort the devices in descending order by max_avail, total_avail
3579 static int btrfs_cmp_device_info(const void *a, const void *b)
3581 const struct btrfs_device_info *di_a = a;
3582 const struct btrfs_device_info *di_b = b;
3584 if (di_a->max_avail > di_b->max_avail)
3586 if (di_a->max_avail < di_b->max_avail)
3588 if (di_a->total_avail > di_b->total_avail)
3590 if (di_a->total_avail < di_b->total_avail)
3595 struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
3596 [BTRFS_RAID_RAID10] = {
3599 .devs_max = 0, /* 0 == as many as possible */
3601 .devs_increment = 2,
3604 [BTRFS_RAID_RAID1] = {
3609 .devs_increment = 2,
3612 [BTRFS_RAID_DUP] = {
3617 .devs_increment = 1,
3620 [BTRFS_RAID_RAID0] = {
3625 .devs_increment = 1,
3628 [BTRFS_RAID_SINGLE] = {
3633 .devs_increment = 1,
3636 [BTRFS_RAID_RAID5] = {
3641 .devs_increment = 1,
3644 [BTRFS_RAID_RAID6] = {
3649 .devs_increment = 1,
3654 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3656 /* TODO allow them to set a preferred stripe size */
3660 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3664 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3667 features = btrfs_super_incompat_flags(info->super_copy);
3668 if (features & BTRFS_FEATURE_INCOMPAT_RAID56)
3671 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
3672 btrfs_set_super_incompat_flags(info->super_copy, features);
3673 printk(KERN_INFO "btrfs: setting RAID5/6 feature flag\n");
3676 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3677 struct btrfs_root *extent_root,
3678 struct map_lookup **map_ret,
3679 u64 *num_bytes_out, u64 *stripe_size_out,
3680 u64 start, u64 type)
3682 struct btrfs_fs_info *info = extent_root->fs_info;
3683 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3684 struct list_head *cur;
3685 struct map_lookup *map = NULL;
3686 struct extent_map_tree *em_tree;
3687 struct extent_map *em;
3688 struct btrfs_device_info *devices_info = NULL;
3690 int num_stripes; /* total number of stripes to allocate */
3691 int data_stripes; /* number of stripes that count for
3693 int sub_stripes; /* sub_stripes info for map */
3694 int dev_stripes; /* stripes per dev */
3695 int devs_max; /* max devs to use */
3696 int devs_min; /* min devs needed */
3697 int devs_increment; /* ndevs has to be a multiple of this */
3698 int ncopies; /* how many copies to data has */
3700 u64 max_stripe_size;
3704 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
3710 BUG_ON(!alloc_profile_is_valid(type, 0));
3712 if (list_empty(&fs_devices->alloc_list))
3715 index = __get_raid_index(type);
3717 sub_stripes = btrfs_raid_array[index].sub_stripes;
3718 dev_stripes = btrfs_raid_array[index].dev_stripes;
3719 devs_max = btrfs_raid_array[index].devs_max;
3720 devs_min = btrfs_raid_array[index].devs_min;
3721 devs_increment = btrfs_raid_array[index].devs_increment;
3722 ncopies = btrfs_raid_array[index].ncopies;
3724 if (type & BTRFS_BLOCK_GROUP_DATA) {
3725 max_stripe_size = 1024 * 1024 * 1024;
3726 max_chunk_size = 10 * max_stripe_size;
3727 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
3728 /* for larger filesystems, use larger metadata chunks */
3729 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3730 max_stripe_size = 1024 * 1024 * 1024;
3732 max_stripe_size = 256 * 1024 * 1024;
3733 max_chunk_size = max_stripe_size;
3734 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
3735 max_stripe_size = 32 * 1024 * 1024;
3736 max_chunk_size = 2 * max_stripe_size;
3738 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3743 /* we don't want a chunk larger than 10% of writeable space */
3744 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3747 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3752 cur = fs_devices->alloc_list.next;
3755 * in the first pass through the devices list, we gather information
3756 * about the available holes on each device.
3759 while (cur != &fs_devices->alloc_list) {
3760 struct btrfs_device *device;
3764 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3768 if (!device->writeable) {
3770 "btrfs: read-only device in alloc_list\n");
3774 if (!device->in_fs_metadata ||
3775 device->is_tgtdev_for_dev_replace)
3778 if (device->total_bytes > device->bytes_used)
3779 total_avail = device->total_bytes - device->bytes_used;
3783 /* If there is no space on this device, skip it. */
3784 if (total_avail == 0)
3787 ret = find_free_dev_extent(device,
3788 max_stripe_size * dev_stripes,
3789 &dev_offset, &max_avail);
3790 if (ret && ret != -ENOSPC)
3794 max_avail = max_stripe_size * dev_stripes;
3796 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3799 if (ndevs == fs_devices->rw_devices) {
3800 WARN(1, "%s: found more than %llu devices\n",
3801 __func__, fs_devices->rw_devices);
3804 devices_info[ndevs].dev_offset = dev_offset;
3805 devices_info[ndevs].max_avail = max_avail;
3806 devices_info[ndevs].total_avail = total_avail;
3807 devices_info[ndevs].dev = device;
3812 * now sort the devices by hole size / available space
3814 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3815 btrfs_cmp_device_info, NULL);
3817 /* round down to number of usable stripes */
3818 ndevs -= ndevs % devs_increment;
3820 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3825 if (devs_max && ndevs > devs_max)
3828 * the primary goal is to maximize the number of stripes, so use as many
3829 * devices as possible, even if the stripes are not maximum sized.
3831 stripe_size = devices_info[ndevs-1].max_avail;
3832 num_stripes = ndevs * dev_stripes;
3835 * this will have to be fixed for RAID1 and RAID10 over
3838 data_stripes = num_stripes / ncopies;
3840 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3841 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3842 btrfs_super_stripesize(info->super_copy));
3843 data_stripes = num_stripes - 1;
3845 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3846 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3847 btrfs_super_stripesize(info->super_copy));
3848 data_stripes = num_stripes - 2;
3852 * Use the number of data stripes to figure out how big this chunk
3853 * is really going to be in terms of logical address space,
3854 * and compare that answer with the max chunk size
3856 if (stripe_size * data_stripes > max_chunk_size) {
3857 u64 mask = (1ULL << 24) - 1;
3858 stripe_size = max_chunk_size;
3859 do_div(stripe_size, data_stripes);
3861 /* bump the answer up to a 16MB boundary */
3862 stripe_size = (stripe_size + mask) & ~mask;
3864 /* but don't go higher than the limits we found
3865 * while searching for free extents
3867 if (stripe_size > devices_info[ndevs-1].max_avail)
3868 stripe_size = devices_info[ndevs-1].max_avail;
3871 do_div(stripe_size, dev_stripes);
3873 /* align to BTRFS_STRIPE_LEN */
3874 do_div(stripe_size, raid_stripe_len);
3875 stripe_size *= raid_stripe_len;
3877 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3882 map->num_stripes = num_stripes;
3884 for (i = 0; i < ndevs; ++i) {
3885 for (j = 0; j < dev_stripes; ++j) {
3886 int s = i * dev_stripes + j;
3887 map->stripes[s].dev = devices_info[i].dev;
3888 map->stripes[s].physical = devices_info[i].dev_offset +
3892 map->sector_size = extent_root->sectorsize;
3893 map->stripe_len = raid_stripe_len;
3894 map->io_align = raid_stripe_len;
3895 map->io_width = raid_stripe_len;
3897 map->sub_stripes = sub_stripes;
3900 num_bytes = stripe_size * data_stripes;
3902 *stripe_size_out = stripe_size;
3903 *num_bytes_out = num_bytes;
3905 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
3907 em = alloc_extent_map();
3912 em->bdev = (struct block_device *)map;
3914 em->len = num_bytes;
3915 em->block_start = 0;
3916 em->block_len = em->len;
3918 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
3919 write_lock(&em_tree->lock);
3920 ret = add_extent_mapping(em_tree, em);
3921 write_unlock(&em_tree->lock);
3923 free_extent_map(em);
3927 for (i = 0; i < map->num_stripes; ++i) {
3928 struct btrfs_device *device;
3931 device = map->stripes[i].dev;
3932 dev_offset = map->stripes[i].physical;
3934 ret = btrfs_alloc_dev_extent(trans, device,
3935 info->chunk_root->root_key.objectid,
3936 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3937 start, dev_offset, stripe_size);
3939 goto error_dev_extent;
3942 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3943 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3946 i = map->num_stripes - 1;
3947 goto error_dev_extent;
3950 free_extent_map(em);
3951 check_raid56_incompat_flag(extent_root->fs_info, type);
3953 kfree(devices_info);
3957 for (; i >= 0; i--) {
3958 struct btrfs_device *device;
3961 device = map->stripes[i].dev;
3962 err = btrfs_free_dev_extent(trans, device, start);
3964 btrfs_abort_transaction(trans, extent_root, err);
3968 write_lock(&em_tree->lock);
3969 remove_extent_mapping(em_tree, em);
3970 write_unlock(&em_tree->lock);
3972 /* One for our allocation */
3973 free_extent_map(em);
3974 /* One for the tree reference */
3975 free_extent_map(em);
3978 kfree(devices_info);
3982 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3983 struct btrfs_root *extent_root,
3984 struct map_lookup *map, u64 chunk_offset,
3985 u64 chunk_size, u64 stripe_size)
3988 struct btrfs_key key;
3989 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3990 struct btrfs_device *device;
3991 struct btrfs_chunk *chunk;
3992 struct btrfs_stripe *stripe;
3993 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3997 chunk = kzalloc(item_size, GFP_NOFS);
4002 while (index < map->num_stripes) {
4003 device = map->stripes[index].dev;
4004 device->bytes_used += stripe_size;
4005 ret = btrfs_update_device(trans, device);
4011 spin_lock(&extent_root->fs_info->free_chunk_lock);
4012 extent_root->fs_info->free_chunk_space -= (stripe_size *
4014 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4017 stripe = &chunk->stripe;
4018 while (index < map->num_stripes) {
4019 device = map->stripes[index].dev;
4020 dev_offset = map->stripes[index].physical;
4022 btrfs_set_stack_stripe_devid(stripe, device->devid);
4023 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4024 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4029 btrfs_set_stack_chunk_length(chunk, chunk_size);
4030 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4031 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4032 btrfs_set_stack_chunk_type(chunk, map->type);
4033 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4034 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4035 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4036 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4037 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4039 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4040 key.type = BTRFS_CHUNK_ITEM_KEY;
4041 key.offset = chunk_offset;
4043 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4045 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4047 * TODO: Cleanup of inserted chunk root in case of
4050 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4060 * Chunk allocation falls into two parts. The first part does works
4061 * that make the new allocated chunk useable, but not do any operation
4062 * that modifies the chunk tree. The second part does the works that
4063 * require modifying the chunk tree. This division is important for the
4064 * bootstrap process of adding storage to a seed btrfs.
4066 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4067 struct btrfs_root *extent_root, u64 type)
4072 struct map_lookup *map;
4073 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4076 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4081 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4082 &stripe_size, chunk_offset, type);
4086 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4087 chunk_size, stripe_size);
4093 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4094 struct btrfs_root *root,
4095 struct btrfs_device *device)
4098 u64 sys_chunk_offset;
4102 u64 sys_stripe_size;
4104 struct map_lookup *map;
4105 struct map_lookup *sys_map;
4106 struct btrfs_fs_info *fs_info = root->fs_info;
4107 struct btrfs_root *extent_root = fs_info->extent_root;
4110 ret = find_next_chunk(fs_info->chunk_root,
4111 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
4115 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4116 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4117 &stripe_size, chunk_offset, alloc_profile);
4121 sys_chunk_offset = chunk_offset + chunk_size;
4123 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4124 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4125 &sys_chunk_size, &sys_stripe_size,
4126 sys_chunk_offset, alloc_profile);
4128 btrfs_abort_transaction(trans, root, ret);
4132 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
4134 btrfs_abort_transaction(trans, root, ret);
4139 * Modifying chunk tree needs allocating new blocks from both
4140 * system block group and metadata block group. So we only can
4141 * do operations require modifying the chunk tree after both
4142 * block groups were created.
4144 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4145 chunk_size, stripe_size);
4147 btrfs_abort_transaction(trans, root, ret);
4151 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4152 sys_chunk_offset, sys_chunk_size,
4155 btrfs_abort_transaction(trans, root, ret);
4162 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4164 struct extent_map *em;
4165 struct map_lookup *map;
4166 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4170 read_lock(&map_tree->map_tree.lock);
4171 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4172 read_unlock(&map_tree->map_tree.lock);
4176 if (btrfs_test_opt(root, DEGRADED)) {
4177 free_extent_map(em);
4181 map = (struct map_lookup *)em->bdev;
4182 for (i = 0; i < map->num_stripes; i++) {
4183 if (!map->stripes[i].dev->writeable) {
4188 free_extent_map(em);
4192 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4194 extent_map_tree_init(&tree->map_tree);
4197 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4199 struct extent_map *em;
4202 write_lock(&tree->map_tree.lock);
4203 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4205 remove_extent_mapping(&tree->map_tree, em);
4206 write_unlock(&tree->map_tree.lock);
4211 free_extent_map(em);
4212 /* once for the tree */
4213 free_extent_map(em);
4217 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4219 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4220 struct extent_map *em;
4221 struct map_lookup *map;
4222 struct extent_map_tree *em_tree = &map_tree->map_tree;
4225 read_lock(&em_tree->lock);
4226 em = lookup_extent_mapping(em_tree, logical, len);
4227 read_unlock(&em_tree->lock);
4230 BUG_ON(em->start > logical || em->start + em->len < logical);
4231 map = (struct map_lookup *)em->bdev;
4232 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4233 ret = map->num_stripes;
4234 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4235 ret = map->sub_stripes;
4236 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4238 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4242 free_extent_map(em);
4244 btrfs_dev_replace_lock(&fs_info->dev_replace);
4245 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4247 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4252 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4253 struct btrfs_mapping_tree *map_tree,
4256 struct extent_map *em;
4257 struct map_lookup *map;
4258 struct extent_map_tree *em_tree = &map_tree->map_tree;
4259 unsigned long len = root->sectorsize;
4261 read_lock(&em_tree->lock);
4262 em = lookup_extent_mapping(em_tree, logical, len);
4263 read_unlock(&em_tree->lock);
4266 BUG_ON(em->start > logical || em->start + em->len < logical);
4267 map = (struct map_lookup *)em->bdev;
4268 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4269 BTRFS_BLOCK_GROUP_RAID6)) {
4270 len = map->stripe_len * nr_data_stripes(map);
4272 free_extent_map(em);
4276 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4277 u64 logical, u64 len, int mirror_num)
4279 struct extent_map *em;
4280 struct map_lookup *map;
4281 struct extent_map_tree *em_tree = &map_tree->map_tree;
4284 read_lock(&em_tree->lock);
4285 em = lookup_extent_mapping(em_tree, logical, len);
4286 read_unlock(&em_tree->lock);
4289 BUG_ON(em->start > logical || em->start + em->len < logical);
4290 map = (struct map_lookup *)em->bdev;
4291 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4292 BTRFS_BLOCK_GROUP_RAID6))
4294 free_extent_map(em);
4298 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4299 struct map_lookup *map, int first, int num,
4300 int optimal, int dev_replace_is_ongoing)
4304 struct btrfs_device *srcdev;
4306 if (dev_replace_is_ongoing &&
4307 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4308 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4309 srcdev = fs_info->dev_replace.srcdev;
4314 * try to avoid the drive that is the source drive for a
4315 * dev-replace procedure, only choose it if no other non-missing
4316 * mirror is available
4318 for (tolerance = 0; tolerance < 2; tolerance++) {
4319 if (map->stripes[optimal].dev->bdev &&
4320 (tolerance || map->stripes[optimal].dev != srcdev))
4322 for (i = first; i < first + num; i++) {
4323 if (map->stripes[i].dev->bdev &&
4324 (tolerance || map->stripes[i].dev != srcdev))
4329 /* we couldn't find one that doesn't fail. Just return something
4330 * and the io error handling code will clean up eventually
4335 static inline int parity_smaller(u64 a, u64 b)
4340 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4341 static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4343 struct btrfs_bio_stripe s;
4350 for (i = 0; i < bbio->num_stripes - 1; i++) {
4351 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4352 s = bbio->stripes[i];
4354 bbio->stripes[i] = bbio->stripes[i+1];
4355 raid_map[i] = raid_map[i+1];
4356 bbio->stripes[i+1] = s;
4364 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4365 u64 logical, u64 *length,
4366 struct btrfs_bio **bbio_ret,
4367 int mirror_num, u64 **raid_map_ret)
4369 struct extent_map *em;
4370 struct map_lookup *map;
4371 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4372 struct extent_map_tree *em_tree = &map_tree->map_tree;
4375 u64 stripe_end_offset;
4380 u64 *raid_map = NULL;
4386 struct btrfs_bio *bbio = NULL;
4387 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4388 int dev_replace_is_ongoing = 0;
4389 int num_alloc_stripes;
4390 int patch_the_first_stripe_for_dev_replace = 0;
4391 u64 physical_to_patch_in_first_stripe = 0;
4392 u64 raid56_full_stripe_start = (u64)-1;
4394 read_lock(&em_tree->lock);
4395 em = lookup_extent_mapping(em_tree, logical, *length);
4396 read_unlock(&em_tree->lock);
4399 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
4400 (unsigned long long)logical,
4401 (unsigned long long)*length);
4405 BUG_ON(em->start > logical || em->start + em->len < logical);
4406 map = (struct map_lookup *)em->bdev;
4407 offset = logical - em->start;
4409 if (mirror_num > map->num_stripes)
4412 stripe_len = map->stripe_len;
4415 * stripe_nr counts the total number of stripes we have to stride
4416 * to get to this block
4418 do_div(stripe_nr, stripe_len);
4420 stripe_offset = stripe_nr * stripe_len;
4421 BUG_ON(offset < stripe_offset);
4423 /* stripe_offset is the offset of this block in its stripe*/
4424 stripe_offset = offset - stripe_offset;
4426 /* if we're here for raid56, we need to know the stripe aligned start */
4427 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4428 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4429 raid56_full_stripe_start = offset;
4431 /* allow a write of a full stripe, but make sure we don't
4432 * allow straddling of stripes
4434 do_div(raid56_full_stripe_start, full_stripe_len);
4435 raid56_full_stripe_start *= full_stripe_len;
4438 if (rw & REQ_DISCARD) {
4439 /* we don't discard raid56 yet */
4441 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4445 *length = min_t(u64, em->len - offset, *length);
4446 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4448 /* For writes to RAID[56], allow a full stripeset across all disks.
4449 For other RAID types and for RAID[56] reads, just allow a single
4450 stripe (on a single disk). */
4451 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4453 max_len = stripe_len * nr_data_stripes(map) -
4454 (offset - raid56_full_stripe_start);
4456 /* we limit the length of each bio to what fits in a stripe */
4457 max_len = stripe_len - stripe_offset;
4459 *length = min_t(u64, em->len - offset, max_len);
4461 *length = em->len - offset;
4464 /* This is for when we're called from btrfs_merge_bio_hook() and all
4465 it cares about is the length */
4469 btrfs_dev_replace_lock(dev_replace);
4470 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4471 if (!dev_replace_is_ongoing)
4472 btrfs_dev_replace_unlock(dev_replace);
4474 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4475 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4476 dev_replace->tgtdev != NULL) {
4478 * in dev-replace case, for repair case (that's the only
4479 * case where the mirror is selected explicitly when
4480 * calling btrfs_map_block), blocks left of the left cursor
4481 * can also be read from the target drive.
4482 * For REQ_GET_READ_MIRRORS, the target drive is added as
4483 * the last one to the array of stripes. For READ, it also
4484 * needs to be supported using the same mirror number.
4485 * If the requested block is not left of the left cursor,
4486 * EIO is returned. This can happen because btrfs_num_copies()
4487 * returns one more in the dev-replace case.
4489 u64 tmp_length = *length;
4490 struct btrfs_bio *tmp_bbio = NULL;
4491 int tmp_num_stripes;
4492 u64 srcdev_devid = dev_replace->srcdev->devid;
4493 int index_srcdev = 0;
4495 u64 physical_of_found = 0;
4497 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
4498 logical, &tmp_length, &tmp_bbio, 0, NULL);
4500 WARN_ON(tmp_bbio != NULL);
4504 tmp_num_stripes = tmp_bbio->num_stripes;
4505 if (mirror_num > tmp_num_stripes) {
4507 * REQ_GET_READ_MIRRORS does not contain this
4508 * mirror, that means that the requested area
4509 * is not left of the left cursor
4517 * process the rest of the function using the mirror_num
4518 * of the source drive. Therefore look it up first.
4519 * At the end, patch the device pointer to the one of the
4522 for (i = 0; i < tmp_num_stripes; i++) {
4523 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4525 * In case of DUP, in order to keep it
4526 * simple, only add the mirror with the
4527 * lowest physical address
4530 physical_of_found <=
4531 tmp_bbio->stripes[i].physical)
4536 tmp_bbio->stripes[i].physical;
4541 mirror_num = index_srcdev + 1;
4542 patch_the_first_stripe_for_dev_replace = 1;
4543 physical_to_patch_in_first_stripe = physical_of_found;
4552 } else if (mirror_num > map->num_stripes) {
4558 stripe_nr_orig = stripe_nr;
4559 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
4560 do_div(stripe_nr_end, map->stripe_len);
4561 stripe_end_offset = stripe_nr_end * map->stripe_len -
4564 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4565 if (rw & REQ_DISCARD)
4566 num_stripes = min_t(u64, map->num_stripes,
4567 stripe_nr_end - stripe_nr_orig);
4568 stripe_index = do_div(stripe_nr, map->num_stripes);
4569 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
4570 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
4571 num_stripes = map->num_stripes;
4572 else if (mirror_num)
4573 stripe_index = mirror_num - 1;
4575 stripe_index = find_live_mirror(fs_info, map, 0,
4577 current->pid % map->num_stripes,
4578 dev_replace_is_ongoing);
4579 mirror_num = stripe_index + 1;
4582 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
4583 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
4584 num_stripes = map->num_stripes;
4585 } else if (mirror_num) {
4586 stripe_index = mirror_num - 1;
4591 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4592 int factor = map->num_stripes / map->sub_stripes;
4594 stripe_index = do_div(stripe_nr, factor);
4595 stripe_index *= map->sub_stripes;
4597 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
4598 num_stripes = map->sub_stripes;
4599 else if (rw & REQ_DISCARD)
4600 num_stripes = min_t(u64, map->sub_stripes *
4601 (stripe_nr_end - stripe_nr_orig),
4603 else if (mirror_num)
4604 stripe_index += mirror_num - 1;
4606 int old_stripe_index = stripe_index;
4607 stripe_index = find_live_mirror(fs_info, map,
4609 map->sub_stripes, stripe_index +
4610 current->pid % map->sub_stripes,
4611 dev_replace_is_ongoing);
4612 mirror_num = stripe_index - old_stripe_index + 1;
4615 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4616 BTRFS_BLOCK_GROUP_RAID6)) {
4619 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4623 /* push stripe_nr back to the start of the full stripe */
4624 stripe_nr = raid56_full_stripe_start;
4625 do_div(stripe_nr, stripe_len);
4627 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4629 /* RAID[56] write or recovery. Return all stripes */
4630 num_stripes = map->num_stripes;
4631 max_errors = nr_parity_stripes(map);
4633 raid_map = kmalloc(sizeof(u64) * num_stripes,
4640 /* Work out the disk rotation on this stripe-set */
4642 rot = do_div(tmp, num_stripes);
4644 /* Fill in the logical address of each stripe */
4645 tmp = stripe_nr * nr_data_stripes(map);
4646 for (i = 0; i < nr_data_stripes(map); i++)
4647 raid_map[(i+rot) % num_stripes] =
4648 em->start + (tmp + i) * map->stripe_len;
4650 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4651 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4652 raid_map[(i+rot+1) % num_stripes] =
4655 *length = map->stripe_len;
4660 * Mirror #0 or #1 means the original data block.
4661 * Mirror #2 is RAID5 parity block.
4662 * Mirror #3 is RAID6 Q block.
4664 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4666 stripe_index = nr_data_stripes(map) +
4669 /* We distribute the parity blocks across stripes */
4670 tmp = stripe_nr + stripe_index;
4671 stripe_index = do_div(tmp, map->num_stripes);
4675 * after this do_div call, stripe_nr is the number of stripes
4676 * on this device we have to walk to find the data, and
4677 * stripe_index is the number of our device in the stripe array
4679 stripe_index = do_div(stripe_nr, map->num_stripes);
4680 mirror_num = stripe_index + 1;
4682 BUG_ON(stripe_index >= map->num_stripes);
4684 num_alloc_stripes = num_stripes;
4685 if (dev_replace_is_ongoing) {
4686 if (rw & (REQ_WRITE | REQ_DISCARD))
4687 num_alloc_stripes <<= 1;
4688 if (rw & REQ_GET_READ_MIRRORS)
4689 num_alloc_stripes++;
4691 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
4696 atomic_set(&bbio->error, 0);
4698 if (rw & REQ_DISCARD) {
4700 int sub_stripes = 0;
4701 u64 stripes_per_dev = 0;
4702 u32 remaining_stripes = 0;
4703 u32 last_stripe = 0;
4706 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4707 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4710 sub_stripes = map->sub_stripes;
4712 factor = map->num_stripes / sub_stripes;
4713 stripes_per_dev = div_u64_rem(stripe_nr_end -
4716 &remaining_stripes);
4717 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4718 last_stripe *= sub_stripes;
4721 for (i = 0; i < num_stripes; i++) {
4722 bbio->stripes[i].physical =
4723 map->stripes[stripe_index].physical +
4724 stripe_offset + stripe_nr * map->stripe_len;
4725 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
4727 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4728 BTRFS_BLOCK_GROUP_RAID10)) {
4729 bbio->stripes[i].length = stripes_per_dev *
4732 if (i / sub_stripes < remaining_stripes)
4733 bbio->stripes[i].length +=
4737 * Special for the first stripe and
4740 * |-------|...|-------|
4744 if (i < sub_stripes)
4745 bbio->stripes[i].length -=
4748 if (stripe_index >= last_stripe &&
4749 stripe_index <= (last_stripe +
4751 bbio->stripes[i].length -=
4754 if (i == sub_stripes - 1)
4757 bbio->stripes[i].length = *length;
4760 if (stripe_index == map->num_stripes) {
4761 /* This could only happen for RAID0/10 */
4767 for (i = 0; i < num_stripes; i++) {
4768 bbio->stripes[i].physical =
4769 map->stripes[stripe_index].physical +
4771 stripe_nr * map->stripe_len;
4772 bbio->stripes[i].dev =
4773 map->stripes[stripe_index].dev;
4778 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
4779 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4780 BTRFS_BLOCK_GROUP_RAID10 |
4781 BTRFS_BLOCK_GROUP_RAID5 |
4782 BTRFS_BLOCK_GROUP_DUP)) {
4784 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4789 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4790 dev_replace->tgtdev != NULL) {
4791 int index_where_to_add;
4792 u64 srcdev_devid = dev_replace->srcdev->devid;
4795 * duplicate the write operations while the dev replace
4796 * procedure is running. Since the copying of the old disk
4797 * to the new disk takes place at run time while the
4798 * filesystem is mounted writable, the regular write
4799 * operations to the old disk have to be duplicated to go
4800 * to the new disk as well.
4801 * Note that device->missing is handled by the caller, and
4802 * that the write to the old disk is already set up in the
4805 index_where_to_add = num_stripes;
4806 for (i = 0; i < num_stripes; i++) {
4807 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4808 /* write to new disk, too */
4809 struct btrfs_bio_stripe *new =
4810 bbio->stripes + index_where_to_add;
4811 struct btrfs_bio_stripe *old =
4814 new->physical = old->physical;
4815 new->length = old->length;
4816 new->dev = dev_replace->tgtdev;
4817 index_where_to_add++;
4821 num_stripes = index_where_to_add;
4822 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4823 dev_replace->tgtdev != NULL) {
4824 u64 srcdev_devid = dev_replace->srcdev->devid;
4825 int index_srcdev = 0;
4827 u64 physical_of_found = 0;
4830 * During the dev-replace procedure, the target drive can
4831 * also be used to read data in case it is needed to repair
4832 * a corrupt block elsewhere. This is possible if the
4833 * requested area is left of the left cursor. In this area,
4834 * the target drive is a full copy of the source drive.
4836 for (i = 0; i < num_stripes; i++) {
4837 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4839 * In case of DUP, in order to keep it
4840 * simple, only add the mirror with the
4841 * lowest physical address
4844 physical_of_found <=
4845 bbio->stripes[i].physical)
4849 physical_of_found = bbio->stripes[i].physical;
4853 u64 length = map->stripe_len;
4855 if (physical_of_found + length <=
4856 dev_replace->cursor_left) {
4857 struct btrfs_bio_stripe *tgtdev_stripe =
4858 bbio->stripes + num_stripes;
4860 tgtdev_stripe->physical = physical_of_found;
4861 tgtdev_stripe->length =
4862 bbio->stripes[index_srcdev].length;
4863 tgtdev_stripe->dev = dev_replace->tgtdev;
4871 bbio->num_stripes = num_stripes;
4872 bbio->max_errors = max_errors;
4873 bbio->mirror_num = mirror_num;
4876 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4877 * mirror_num == num_stripes + 1 && dev_replace target drive is
4878 * available as a mirror
4880 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4881 WARN_ON(num_stripes > 1);
4882 bbio->stripes[0].dev = dev_replace->tgtdev;
4883 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4884 bbio->mirror_num = map->num_stripes + 1;
4887 sort_parity_stripes(bbio, raid_map);
4888 *raid_map_ret = raid_map;
4891 if (dev_replace_is_ongoing)
4892 btrfs_dev_replace_unlock(dev_replace);
4893 free_extent_map(em);
4897 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4898 u64 logical, u64 *length,
4899 struct btrfs_bio **bbio_ret, int mirror_num)
4901 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
4905 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4906 u64 chunk_start, u64 physical, u64 devid,
4907 u64 **logical, int *naddrs, int *stripe_len)
4909 struct extent_map_tree *em_tree = &map_tree->map_tree;
4910 struct extent_map *em;
4911 struct map_lookup *map;
4919 read_lock(&em_tree->lock);
4920 em = lookup_extent_mapping(em_tree, chunk_start, 1);
4921 read_unlock(&em_tree->lock);
4923 BUG_ON(!em || em->start != chunk_start);
4924 map = (struct map_lookup *)em->bdev;
4927 rmap_len = map->stripe_len;
4929 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4930 do_div(length, map->num_stripes / map->sub_stripes);
4931 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4932 do_div(length, map->num_stripes);
4933 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4934 BTRFS_BLOCK_GROUP_RAID6)) {
4935 do_div(length, nr_data_stripes(map));
4936 rmap_len = map->stripe_len * nr_data_stripes(map);
4939 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
4940 BUG_ON(!buf); /* -ENOMEM */
4942 for (i = 0; i < map->num_stripes; i++) {
4943 if (devid && map->stripes[i].dev->devid != devid)
4945 if (map->stripes[i].physical > physical ||
4946 map->stripes[i].physical + length <= physical)
4949 stripe_nr = physical - map->stripes[i].physical;
4950 do_div(stripe_nr, map->stripe_len);
4952 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4953 stripe_nr = stripe_nr * map->num_stripes + i;
4954 do_div(stripe_nr, map->sub_stripes);
4955 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4956 stripe_nr = stripe_nr * map->num_stripes + i;
4957 } /* else if RAID[56], multiply by nr_data_stripes().
4958 * Alternatively, just use rmap_len below instead of
4959 * map->stripe_len */
4961 bytenr = chunk_start + stripe_nr * rmap_len;
4962 WARN_ON(nr >= map->num_stripes);
4963 for (j = 0; j < nr; j++) {
4964 if (buf[j] == bytenr)
4968 WARN_ON(nr >= map->num_stripes);
4975 *stripe_len = rmap_len;
4977 free_extent_map(em);
4981 static void *merge_stripe_index_into_bio_private(void *bi_private,
4982 unsigned int stripe_index)
4985 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4987 * The alternative solution (instead of stealing bits from the
4988 * pointer) would be to allocate an intermediate structure
4989 * that contains the old private pointer plus the stripe_index.
4991 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4992 BUG_ON(stripe_index > 3);
4993 return (void *)(((uintptr_t)bi_private) | stripe_index);
4996 static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4998 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
5001 static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
5003 return (unsigned int)((uintptr_t)bi_private) & 3;
5006 static void btrfs_end_bio(struct bio *bio, int err)
5008 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
5009 int is_orig_bio = 0;
5012 atomic_inc(&bbio->error);
5013 if (err == -EIO || err == -EREMOTEIO) {
5014 unsigned int stripe_index =
5015 extract_stripe_index_from_bio_private(
5017 struct btrfs_device *dev;
5019 BUG_ON(stripe_index >= bbio->num_stripes);
5020 dev = bbio->stripes[stripe_index].dev;
5022 if (bio->bi_rw & WRITE)
5023 btrfs_dev_stat_inc(dev,
5024 BTRFS_DEV_STAT_WRITE_ERRS);
5026 btrfs_dev_stat_inc(dev,
5027 BTRFS_DEV_STAT_READ_ERRS);
5028 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5029 btrfs_dev_stat_inc(dev,
5030 BTRFS_DEV_STAT_FLUSH_ERRS);
5031 btrfs_dev_stat_print_on_error(dev);
5036 if (bio == bbio->orig_bio)
5039 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5042 bio = bbio->orig_bio;
5044 bio->bi_private = bbio->private;
5045 bio->bi_end_io = bbio->end_io;
5046 bio->bi_bdev = (struct block_device *)
5047 (unsigned long)bbio->mirror_num;
5048 /* only send an error to the higher layers if it is
5049 * beyond the tolerance of the btrfs bio
5051 if (atomic_read(&bbio->error) > bbio->max_errors) {
5055 * this bio is actually up to date, we didn't
5056 * go over the max number of errors
5058 set_bit(BIO_UPTODATE, &bio->bi_flags);
5063 bio_endio(bio, err);
5064 } else if (!is_orig_bio) {
5069 struct async_sched {
5072 struct btrfs_fs_info *info;
5073 struct btrfs_work work;
5077 * see run_scheduled_bios for a description of why bios are collected for
5080 * This will add one bio to the pending list for a device and make sure
5081 * the work struct is scheduled.
5083 noinline void btrfs_schedule_bio(struct btrfs_root *root,
5084 struct btrfs_device *device,
5085 int rw, struct bio *bio)
5087 int should_queue = 1;
5088 struct btrfs_pending_bios *pending_bios;
5090 if (device->missing || !device->bdev) {
5091 bio_endio(bio, -EIO);
5095 /* don't bother with additional async steps for reads, right now */
5096 if (!(rw & REQ_WRITE)) {
5098 btrfsic_submit_bio(rw, bio);
5104 * nr_async_bios allows us to reliably return congestion to the
5105 * higher layers. Otherwise, the async bio makes it appear we have
5106 * made progress against dirty pages when we've really just put it
5107 * on a queue for later
5109 atomic_inc(&root->fs_info->nr_async_bios);
5110 WARN_ON(bio->bi_next);
5111 bio->bi_next = NULL;
5114 spin_lock(&device->io_lock);
5115 if (bio->bi_rw & REQ_SYNC)
5116 pending_bios = &device->pending_sync_bios;
5118 pending_bios = &device->pending_bios;
5120 if (pending_bios->tail)
5121 pending_bios->tail->bi_next = bio;
5123 pending_bios->tail = bio;
5124 if (!pending_bios->head)
5125 pending_bios->head = bio;
5126 if (device->running_pending)
5129 spin_unlock(&device->io_lock);
5132 btrfs_queue_worker(&root->fs_info->submit_workers,
5136 static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5139 struct bio_vec *prev;
5140 struct request_queue *q = bdev_get_queue(bdev);
5141 unsigned short max_sectors = queue_max_sectors(q);
5142 struct bvec_merge_data bvm = {
5144 .bi_sector = sector,
5145 .bi_rw = bio->bi_rw,
5148 if (bio->bi_vcnt == 0) {
5153 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5154 if ((bio->bi_size >> 9) > max_sectors)
5157 if (!q->merge_bvec_fn)
5160 bvm.bi_size = bio->bi_size - prev->bv_len;
5161 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5166 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5167 struct bio *bio, u64 physical, int dev_nr,
5170 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5172 bio->bi_private = bbio;
5173 bio->bi_private = merge_stripe_index_into_bio_private(
5174 bio->bi_private, (unsigned int)dev_nr);
5175 bio->bi_end_io = btrfs_end_bio;
5176 bio->bi_sector = physical >> 9;
5179 struct rcu_string *name;
5182 name = rcu_dereference(dev->name);
5183 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5184 "(%s id %llu), size=%u\n", rw,
5185 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5186 name->str, dev->devid, bio->bi_size);
5190 bio->bi_bdev = dev->bdev;
5192 btrfs_schedule_bio(root, dev, rw, bio);
5194 btrfsic_submit_bio(rw, bio);
5197 static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5198 struct bio *first_bio, struct btrfs_device *dev,
5199 int dev_nr, int rw, int async)
5201 struct bio_vec *bvec = first_bio->bi_io_vec;
5203 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5204 u64 physical = bbio->stripes[dev_nr].physical;
5207 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5211 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5212 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5213 bvec->bv_offset) < bvec->bv_len) {
5214 u64 len = bio->bi_size;
5216 atomic_inc(&bbio->stripes_pending);
5217 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5225 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5229 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5231 atomic_inc(&bbio->error);
5232 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5233 bio->bi_private = bbio->private;
5234 bio->bi_end_io = bbio->end_io;
5235 bio->bi_bdev = (struct block_device *)
5236 (unsigned long)bbio->mirror_num;
5237 bio->bi_sector = logical >> 9;
5239 bio_endio(bio, -EIO);
5243 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5244 int mirror_num, int async_submit)
5246 struct btrfs_device *dev;
5247 struct bio *first_bio = bio;
5248 u64 logical = (u64)bio->bi_sector << 9;
5251 u64 *raid_map = NULL;
5255 struct btrfs_bio *bbio = NULL;
5257 length = bio->bi_size;
5258 map_length = length;
5260 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5261 mirror_num, &raid_map);
5262 if (ret) /* -ENOMEM */
5265 total_devs = bbio->num_stripes;
5266 bbio->orig_bio = first_bio;
5267 bbio->private = first_bio->bi_private;
5268 bbio->end_io = first_bio->bi_end_io;
5269 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5272 /* In this case, map_length has been set to the length of
5273 a single stripe; not the whole write */
5275 return raid56_parity_write(root, bio, bbio,
5276 raid_map, map_length);
5278 return raid56_parity_recover(root, bio, bbio,
5279 raid_map, map_length,
5284 if (map_length < length) {
5285 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
5286 "len %llu\n", (unsigned long long)logical,
5287 (unsigned long long)length,
5288 (unsigned long long)map_length);
5292 while (dev_nr < total_devs) {
5293 dev = bbio->stripes[dev_nr].dev;
5294 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5295 bbio_error(bbio, first_bio, logical);
5301 * Check and see if we're ok with this bio based on it's size
5302 * and offset with the given device.
5304 if (!bio_size_ok(dev->bdev, first_bio,
5305 bbio->stripes[dev_nr].physical >> 9)) {
5306 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5307 dev_nr, rw, async_submit);
5313 if (dev_nr < total_devs - 1) {
5314 bio = bio_clone(first_bio, GFP_NOFS);
5315 BUG_ON(!bio); /* -ENOMEM */
5320 submit_stripe_bio(root, bbio, bio,
5321 bbio->stripes[dev_nr].physical, dev_nr, rw,
5328 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5331 struct btrfs_device *device;
5332 struct btrfs_fs_devices *cur_devices;
5334 cur_devices = fs_info->fs_devices;
5335 while (cur_devices) {
5337 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5338 device = __find_device(&cur_devices->devices,
5343 cur_devices = cur_devices->seed;
5348 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5349 u64 devid, u8 *dev_uuid)
5351 struct btrfs_device *device;
5352 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5354 device = kzalloc(sizeof(*device), GFP_NOFS);
5357 list_add(&device->dev_list,
5358 &fs_devices->devices);
5359 device->dev_root = root->fs_info->dev_root;
5360 device->devid = devid;
5361 device->work.func = pending_bios_fn;
5362 device->fs_devices = fs_devices;
5363 device->missing = 1;
5364 fs_devices->num_devices++;
5365 fs_devices->missing_devices++;
5366 spin_lock_init(&device->io_lock);
5367 INIT_LIST_HEAD(&device->dev_alloc_list);
5368 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5372 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5373 struct extent_buffer *leaf,
5374 struct btrfs_chunk *chunk)
5376 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5377 struct map_lookup *map;
5378 struct extent_map *em;
5382 u8 uuid[BTRFS_UUID_SIZE];
5387 logical = key->offset;
5388 length = btrfs_chunk_length(leaf, chunk);
5390 read_lock(&map_tree->map_tree.lock);
5391 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
5392 read_unlock(&map_tree->map_tree.lock);
5394 /* already mapped? */
5395 if (em && em->start <= logical && em->start + em->len > logical) {
5396 free_extent_map(em);
5399 free_extent_map(em);
5402 em = alloc_extent_map();
5405 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5406 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5408 free_extent_map(em);
5412 em->bdev = (struct block_device *)map;
5413 em->start = logical;
5416 em->block_start = 0;
5417 em->block_len = em->len;
5419 map->num_stripes = num_stripes;
5420 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5421 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5422 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5423 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5424 map->type = btrfs_chunk_type(leaf, chunk);
5425 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
5426 for (i = 0; i < num_stripes; i++) {
5427 map->stripes[i].physical =
5428 btrfs_stripe_offset_nr(leaf, chunk, i);
5429 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
5430 read_extent_buffer(leaf, uuid, (unsigned long)
5431 btrfs_stripe_dev_uuid_nr(chunk, i),
5433 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5435 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
5437 free_extent_map(em);
5440 if (!map->stripes[i].dev) {
5441 map->stripes[i].dev =
5442 add_missing_dev(root, devid, uuid);
5443 if (!map->stripes[i].dev) {
5445 free_extent_map(em);
5449 map->stripes[i].dev->in_fs_metadata = 1;
5452 write_lock(&map_tree->map_tree.lock);
5453 ret = add_extent_mapping(&map_tree->map_tree, em);
5454 write_unlock(&map_tree->map_tree.lock);
5455 BUG_ON(ret); /* Tree corruption */
5456 free_extent_map(em);
5461 static void fill_device_from_item(struct extent_buffer *leaf,
5462 struct btrfs_dev_item *dev_item,
5463 struct btrfs_device *device)
5467 device->devid = btrfs_device_id(leaf, dev_item);
5468 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5469 device->total_bytes = device->disk_total_bytes;
5470 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5471 device->type = btrfs_device_type(leaf, dev_item);
5472 device->io_align = btrfs_device_io_align(leaf, dev_item);
5473 device->io_width = btrfs_device_io_width(leaf, dev_item);
5474 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
5475 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
5476 device->is_tgtdev_for_dev_replace = 0;
5478 ptr = (unsigned long)btrfs_device_uuid(dev_item);
5479 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
5482 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5484 struct btrfs_fs_devices *fs_devices;
5487 BUG_ON(!mutex_is_locked(&uuid_mutex));
5489 fs_devices = root->fs_info->fs_devices->seed;
5490 while (fs_devices) {
5491 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5495 fs_devices = fs_devices->seed;
5498 fs_devices = find_fsid(fsid);
5504 fs_devices = clone_fs_devices(fs_devices);
5505 if (IS_ERR(fs_devices)) {
5506 ret = PTR_ERR(fs_devices);
5510 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
5511 root->fs_info->bdev_holder);
5513 free_fs_devices(fs_devices);
5517 if (!fs_devices->seeding) {
5518 __btrfs_close_devices(fs_devices);
5519 free_fs_devices(fs_devices);
5524 fs_devices->seed = root->fs_info->fs_devices->seed;
5525 root->fs_info->fs_devices->seed = fs_devices;
5530 static int read_one_dev(struct btrfs_root *root,
5531 struct extent_buffer *leaf,
5532 struct btrfs_dev_item *dev_item)
5534 struct btrfs_device *device;
5537 u8 fs_uuid[BTRFS_UUID_SIZE];
5538 u8 dev_uuid[BTRFS_UUID_SIZE];
5540 devid = btrfs_device_id(leaf, dev_item);
5541 read_extent_buffer(leaf, dev_uuid,
5542 (unsigned long)btrfs_device_uuid(dev_item),
5544 read_extent_buffer(leaf, fs_uuid,
5545 (unsigned long)btrfs_device_fsid(dev_item),
5548 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5549 ret = open_seed_devices(root, fs_uuid);
5550 if (ret && !btrfs_test_opt(root, DEGRADED))
5554 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
5555 if (!device || !device->bdev) {
5556 if (!btrfs_test_opt(root, DEGRADED))
5560 printk(KERN_WARNING "warning devid %llu missing\n",
5561 (unsigned long long)devid);
5562 device = add_missing_dev(root, devid, dev_uuid);
5565 } else if (!device->missing) {
5567 * this happens when a device that was properly setup
5568 * in the device info lists suddenly goes bad.
5569 * device->bdev is NULL, and so we have to set
5570 * device->missing to one here
5572 root->fs_info->fs_devices->missing_devices++;
5573 device->missing = 1;
5577 if (device->fs_devices != root->fs_info->fs_devices) {
5578 BUG_ON(device->writeable);
5579 if (device->generation !=
5580 btrfs_device_generation(leaf, dev_item))
5584 fill_device_from_item(leaf, dev_item, device);
5585 device->dev_root = root->fs_info->dev_root;
5586 device->in_fs_metadata = 1;
5587 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
5588 device->fs_devices->total_rw_bytes += device->total_bytes;
5589 spin_lock(&root->fs_info->free_chunk_lock);
5590 root->fs_info->free_chunk_space += device->total_bytes -
5592 spin_unlock(&root->fs_info->free_chunk_lock);
5598 int btrfs_read_sys_array(struct btrfs_root *root)
5600 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
5601 struct extent_buffer *sb;
5602 struct btrfs_disk_key *disk_key;
5603 struct btrfs_chunk *chunk;
5605 unsigned long sb_ptr;
5611 struct btrfs_key key;
5613 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
5614 BTRFS_SUPER_INFO_SIZE);
5617 btrfs_set_buffer_uptodate(sb);
5618 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
5620 * The sb extent buffer is artifical and just used to read the system array.
5621 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5622 * pages up-to-date when the page is larger: extent does not cover the
5623 * whole page and consequently check_page_uptodate does not find all
5624 * the page's extents up-to-date (the hole beyond sb),
5625 * write_extent_buffer then triggers a WARN_ON.
5627 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5628 * but sb spans only this function. Add an explicit SetPageUptodate call
5629 * to silence the warning eg. on PowerPC 64.
5631 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
5632 SetPageUptodate(sb->pages[0]);
5634 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
5635 array_size = btrfs_super_sys_array_size(super_copy);
5637 ptr = super_copy->sys_chunk_array;
5638 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5641 while (cur < array_size) {
5642 disk_key = (struct btrfs_disk_key *)ptr;
5643 btrfs_disk_key_to_cpu(&key, disk_key);
5645 len = sizeof(*disk_key); ptr += len;
5649 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
5650 chunk = (struct btrfs_chunk *)sb_ptr;
5651 ret = read_one_chunk(root, &key, sb, chunk);
5654 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5655 len = btrfs_chunk_item_size(num_stripes);
5664 free_extent_buffer(sb);
5668 int btrfs_read_chunk_tree(struct btrfs_root *root)
5670 struct btrfs_path *path;
5671 struct extent_buffer *leaf;
5672 struct btrfs_key key;
5673 struct btrfs_key found_key;
5677 root = root->fs_info->chunk_root;
5679 path = btrfs_alloc_path();
5683 mutex_lock(&uuid_mutex);
5686 /* first we search for all of the device items, and then we
5687 * read in all of the chunk items. This way we can create chunk
5688 * mappings that reference all of the devices that are afound
5690 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5698 leaf = path->nodes[0];
5699 slot = path->slots[0];
5700 if (slot >= btrfs_header_nritems(leaf)) {
5701 ret = btrfs_next_leaf(root, path);
5708 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5709 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5710 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5712 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5713 struct btrfs_dev_item *dev_item;
5714 dev_item = btrfs_item_ptr(leaf, slot,
5715 struct btrfs_dev_item);
5716 ret = read_one_dev(root, leaf, dev_item);
5720 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5721 struct btrfs_chunk *chunk;
5722 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5723 ret = read_one_chunk(root, &found_key, leaf, chunk);
5729 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5731 btrfs_release_path(path);
5736 unlock_chunks(root);
5737 mutex_unlock(&uuid_mutex);
5739 btrfs_free_path(path);
5743 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5747 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5748 btrfs_dev_stat_reset(dev, i);
5751 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5753 struct btrfs_key key;
5754 struct btrfs_key found_key;
5755 struct btrfs_root *dev_root = fs_info->dev_root;
5756 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5757 struct extent_buffer *eb;
5760 struct btrfs_device *device;
5761 struct btrfs_path *path = NULL;
5764 path = btrfs_alloc_path();
5770 mutex_lock(&fs_devices->device_list_mutex);
5771 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5773 struct btrfs_dev_stats_item *ptr;
5776 key.type = BTRFS_DEV_STATS_KEY;
5777 key.offset = device->devid;
5778 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5780 __btrfs_reset_dev_stats(device);
5781 device->dev_stats_valid = 1;
5782 btrfs_release_path(path);
5785 slot = path->slots[0];
5786 eb = path->nodes[0];
5787 btrfs_item_key_to_cpu(eb, &found_key, slot);
5788 item_size = btrfs_item_size_nr(eb, slot);
5790 ptr = btrfs_item_ptr(eb, slot,
5791 struct btrfs_dev_stats_item);
5793 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5794 if (item_size >= (1 + i) * sizeof(__le64))
5795 btrfs_dev_stat_set(device, i,
5796 btrfs_dev_stats_value(eb, ptr, i));
5798 btrfs_dev_stat_reset(device, i);
5801 device->dev_stats_valid = 1;
5802 btrfs_dev_stat_print_on_load(device);
5803 btrfs_release_path(path);
5805 mutex_unlock(&fs_devices->device_list_mutex);
5808 btrfs_free_path(path);
5809 return ret < 0 ? ret : 0;
5812 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5813 struct btrfs_root *dev_root,
5814 struct btrfs_device *device)
5816 struct btrfs_path *path;
5817 struct btrfs_key key;
5818 struct extent_buffer *eb;
5819 struct btrfs_dev_stats_item *ptr;
5824 key.type = BTRFS_DEV_STATS_KEY;
5825 key.offset = device->devid;
5827 path = btrfs_alloc_path();
5829 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5831 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5832 ret, rcu_str_deref(device->name));
5837 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5838 /* need to delete old one and insert a new one */
5839 ret = btrfs_del_item(trans, dev_root, path);
5841 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5842 rcu_str_deref(device->name), ret);
5849 /* need to insert a new item */
5850 btrfs_release_path(path);
5851 ret = btrfs_insert_empty_item(trans, dev_root, path,
5852 &key, sizeof(*ptr));
5854 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5855 rcu_str_deref(device->name), ret);
5860 eb = path->nodes[0];
5861 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5862 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5863 btrfs_set_dev_stats_value(eb, ptr, i,
5864 btrfs_dev_stat_read(device, i));
5865 btrfs_mark_buffer_dirty(eb);
5868 btrfs_free_path(path);
5873 * called from commit_transaction. Writes all changed device stats to disk.
5875 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5876 struct btrfs_fs_info *fs_info)
5878 struct btrfs_root *dev_root = fs_info->dev_root;
5879 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5880 struct btrfs_device *device;
5883 mutex_lock(&fs_devices->device_list_mutex);
5884 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5885 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5888 ret = update_dev_stat_item(trans, dev_root, device);
5890 device->dev_stats_dirty = 0;
5892 mutex_unlock(&fs_devices->device_list_mutex);
5897 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5899 btrfs_dev_stat_inc(dev, index);
5900 btrfs_dev_stat_print_on_error(dev);
5903 void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5905 if (!dev->dev_stats_valid)
5907 printk_ratelimited_in_rcu(KERN_ERR
5908 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5909 rcu_str_deref(dev->name),
5910 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5911 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5912 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5913 btrfs_dev_stat_read(dev,
5914 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5915 btrfs_dev_stat_read(dev,
5916 BTRFS_DEV_STAT_GENERATION_ERRS));
5919 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5923 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5924 if (btrfs_dev_stat_read(dev, i) != 0)
5926 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5927 return; /* all values == 0, suppress message */
5929 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5930 rcu_str_deref(dev->name),
5931 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5932 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5933 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5934 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5935 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5938 int btrfs_get_dev_stats(struct btrfs_root *root,
5939 struct btrfs_ioctl_get_dev_stats *stats)
5941 struct btrfs_device *dev;
5942 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5945 mutex_lock(&fs_devices->device_list_mutex);
5946 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
5947 mutex_unlock(&fs_devices->device_list_mutex);
5951 "btrfs: get dev_stats failed, device not found\n");
5953 } else if (!dev->dev_stats_valid) {
5955 "btrfs: get dev_stats failed, not yet valid\n");
5957 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
5958 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5959 if (stats->nr_items > i)
5961 btrfs_dev_stat_read_and_reset(dev, i);
5963 btrfs_dev_stat_reset(dev, i);
5966 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5967 if (stats->nr_items > i)
5968 stats->values[i] = btrfs_dev_stat_read(dev, i);
5970 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5971 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5975 int btrfs_scratch_superblock(struct btrfs_device *device)
5977 struct buffer_head *bh;
5978 struct btrfs_super_block *disk_super;
5980 bh = btrfs_read_dev_super(device->bdev);
5983 disk_super = (struct btrfs_super_block *)bh->b_data;
5985 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5986 set_buffer_dirty(bh);
5987 sync_dirty_buffer(bh);