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>
30 #include "extent_map.h"
32 #include "transaction.h"
33 #include "print-tree.h"
35 #include "async-thread.h"
36 #include "check-integrity.h"
37 #include "rcu-string.h"
40 static int init_first_rw_device(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct btrfs_device *device);
43 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
44 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
45 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
47 static DEFINE_MUTEX(uuid_mutex);
48 static LIST_HEAD(fs_uuids);
50 static void lock_chunks(struct btrfs_root *root)
52 mutex_lock(&root->fs_info->chunk_mutex);
55 static void unlock_chunks(struct btrfs_root *root)
57 mutex_unlock(&root->fs_info->chunk_mutex);
60 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
62 struct btrfs_device *device;
63 WARN_ON(fs_devices->opened);
64 while (!list_empty(&fs_devices->devices)) {
65 device = list_entry(fs_devices->devices.next,
66 struct btrfs_device, dev_list);
67 list_del(&device->dev_list);
68 rcu_string_free(device->name);
74 void btrfs_cleanup_fs_uuids(void)
76 struct btrfs_fs_devices *fs_devices;
78 while (!list_empty(&fs_uuids)) {
79 fs_devices = list_entry(fs_uuids.next,
80 struct btrfs_fs_devices, list);
81 list_del(&fs_devices->list);
82 free_fs_devices(fs_devices);
86 static noinline struct btrfs_device *__find_device(struct list_head *head,
89 struct btrfs_device *dev;
91 list_for_each_entry(dev, head, dev_list) {
92 if (dev->devid == devid &&
93 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
100 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
102 struct btrfs_fs_devices *fs_devices;
104 list_for_each_entry(fs_devices, &fs_uuids, list) {
105 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
112 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
113 int flush, struct block_device **bdev,
114 struct buffer_head **bh)
118 *bdev = blkdev_get_by_path(device_path, flags, holder);
121 ret = PTR_ERR(*bdev);
122 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
127 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
128 ret = set_blocksize(*bdev, 4096);
130 blkdev_put(*bdev, flags);
133 invalidate_bdev(*bdev);
134 *bh = btrfs_read_dev_super(*bdev);
137 blkdev_put(*bdev, flags);
149 static void requeue_list(struct btrfs_pending_bios *pending_bios,
150 struct bio *head, struct bio *tail)
153 struct bio *old_head;
155 old_head = pending_bios->head;
156 pending_bios->head = head;
157 if (pending_bios->tail)
158 tail->bi_next = old_head;
160 pending_bios->tail = tail;
164 * we try to collect pending bios for a device so we don't get a large
165 * number of procs sending bios down to the same device. This greatly
166 * improves the schedulers ability to collect and merge the bios.
168 * But, it also turns into a long list of bios to process and that is sure
169 * to eventually make the worker thread block. The solution here is to
170 * make some progress and then put this work struct back at the end of
171 * the list if the block device is congested. This way, multiple devices
172 * can make progress from a single worker thread.
174 static noinline void run_scheduled_bios(struct btrfs_device *device)
177 struct backing_dev_info *bdi;
178 struct btrfs_fs_info *fs_info;
179 struct btrfs_pending_bios *pending_bios;
183 unsigned long num_run;
184 unsigned long batch_run = 0;
186 unsigned long last_waited = 0;
188 int sync_pending = 0;
189 struct blk_plug plug;
192 * this function runs all the bios we've collected for
193 * a particular device. We don't want to wander off to
194 * another device without first sending all of these down.
195 * So, setup a plug here and finish it off before we return
197 blk_start_plug(&plug);
199 bdi = blk_get_backing_dev_info(device->bdev);
200 fs_info = device->dev_root->fs_info;
201 limit = btrfs_async_submit_limit(fs_info);
202 limit = limit * 2 / 3;
205 spin_lock(&device->io_lock);
210 /* take all the bios off the list at once and process them
211 * later on (without the lock held). But, remember the
212 * tail and other pointers so the bios can be properly reinserted
213 * into the list if we hit congestion
215 if (!force_reg && device->pending_sync_bios.head) {
216 pending_bios = &device->pending_sync_bios;
219 pending_bios = &device->pending_bios;
223 pending = pending_bios->head;
224 tail = pending_bios->tail;
225 WARN_ON(pending && !tail);
228 * if pending was null this time around, no bios need processing
229 * at all and we can stop. Otherwise it'll loop back up again
230 * and do an additional check so no bios are missed.
232 * device->running_pending is used to synchronize with the
235 if (device->pending_sync_bios.head == NULL &&
236 device->pending_bios.head == NULL) {
238 device->running_pending = 0;
241 device->running_pending = 1;
244 pending_bios->head = NULL;
245 pending_bios->tail = NULL;
247 spin_unlock(&device->io_lock);
252 /* we want to work on both lists, but do more bios on the
253 * sync list than the regular list
256 pending_bios != &device->pending_sync_bios &&
257 device->pending_sync_bios.head) ||
258 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
259 device->pending_bios.head)) {
260 spin_lock(&device->io_lock);
261 requeue_list(pending_bios, pending, tail);
266 pending = pending->bi_next;
269 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
270 waitqueue_active(&fs_info->async_submit_wait))
271 wake_up(&fs_info->async_submit_wait);
273 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
276 * if we're doing the sync list, record that our
277 * plug has some sync requests on it
279 * If we're doing the regular list and there are
280 * sync requests sitting around, unplug before
283 if (pending_bios == &device->pending_sync_bios) {
285 } else if (sync_pending) {
286 blk_finish_plug(&plug);
287 blk_start_plug(&plug);
291 btrfsic_submit_bio(cur->bi_rw, cur);
298 * we made progress, there is more work to do and the bdi
299 * is now congested. Back off and let other work structs
302 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
303 fs_info->fs_devices->open_devices > 1) {
304 struct io_context *ioc;
306 ioc = current->io_context;
309 * the main goal here is that we don't want to
310 * block if we're going to be able to submit
311 * more requests without blocking.
313 * This code does two great things, it pokes into
314 * the elevator code from a filesystem _and_
315 * it makes assumptions about how batching works.
317 if (ioc && ioc->nr_batch_requests > 0 &&
318 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
320 ioc->last_waited == last_waited)) {
322 * we want to go through our batch of
323 * requests and stop. So, we copy out
324 * the ioc->last_waited time and test
325 * against it before looping
327 last_waited = ioc->last_waited;
332 spin_lock(&device->io_lock);
333 requeue_list(pending_bios, pending, tail);
334 device->running_pending = 1;
336 spin_unlock(&device->io_lock);
337 btrfs_requeue_work(&device->work);
340 /* unplug every 64 requests just for good measure */
341 if (batch_run % 64 == 0) {
342 blk_finish_plug(&plug);
343 blk_start_plug(&plug);
352 spin_lock(&device->io_lock);
353 if (device->pending_bios.head || device->pending_sync_bios.head)
355 spin_unlock(&device->io_lock);
358 blk_finish_plug(&plug);
361 static void pending_bios_fn(struct btrfs_work *work)
363 struct btrfs_device *device;
365 device = container_of(work, struct btrfs_device, work);
366 run_scheduled_bios(device);
369 static noinline int device_list_add(const char *path,
370 struct btrfs_super_block *disk_super,
371 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
373 struct btrfs_device *device;
374 struct btrfs_fs_devices *fs_devices;
375 struct rcu_string *name;
376 u64 found_transid = btrfs_super_generation(disk_super);
378 fs_devices = find_fsid(disk_super->fsid);
380 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
383 INIT_LIST_HEAD(&fs_devices->devices);
384 INIT_LIST_HEAD(&fs_devices->alloc_list);
385 list_add(&fs_devices->list, &fs_uuids);
386 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
387 fs_devices->latest_devid = devid;
388 fs_devices->latest_trans = found_transid;
389 mutex_init(&fs_devices->device_list_mutex);
392 device = __find_device(&fs_devices->devices, devid,
393 disk_super->dev_item.uuid);
396 if (fs_devices->opened)
399 device = kzalloc(sizeof(*device), GFP_NOFS);
401 /* we can safely leave the fs_devices entry around */
404 device->devid = devid;
405 device->dev_stats_valid = 0;
406 device->work.func = pending_bios_fn;
407 memcpy(device->uuid, disk_super->dev_item.uuid,
409 spin_lock_init(&device->io_lock);
411 name = rcu_string_strdup(path, GFP_NOFS);
416 rcu_assign_pointer(device->name, name);
417 INIT_LIST_HEAD(&device->dev_alloc_list);
419 /* init readahead state */
420 spin_lock_init(&device->reada_lock);
421 device->reada_curr_zone = NULL;
422 atomic_set(&device->reada_in_flight, 0);
423 device->reada_next = 0;
424 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
425 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
427 mutex_lock(&fs_devices->device_list_mutex);
428 list_add_rcu(&device->dev_list, &fs_devices->devices);
429 mutex_unlock(&fs_devices->device_list_mutex);
431 device->fs_devices = fs_devices;
432 fs_devices->num_devices++;
433 } else if (!device->name || strcmp(device->name->str, path)) {
434 name = rcu_string_strdup(path, GFP_NOFS);
437 rcu_string_free(device->name);
438 rcu_assign_pointer(device->name, name);
439 if (device->missing) {
440 fs_devices->missing_devices--;
445 if (found_transid > fs_devices->latest_trans) {
446 fs_devices->latest_devid = devid;
447 fs_devices->latest_trans = found_transid;
449 *fs_devices_ret = fs_devices;
453 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
455 struct btrfs_fs_devices *fs_devices;
456 struct btrfs_device *device;
457 struct btrfs_device *orig_dev;
459 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
461 return ERR_PTR(-ENOMEM);
463 INIT_LIST_HEAD(&fs_devices->devices);
464 INIT_LIST_HEAD(&fs_devices->alloc_list);
465 INIT_LIST_HEAD(&fs_devices->list);
466 mutex_init(&fs_devices->device_list_mutex);
467 fs_devices->latest_devid = orig->latest_devid;
468 fs_devices->latest_trans = orig->latest_trans;
469 fs_devices->total_devices = orig->total_devices;
470 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
472 /* We have held the volume lock, it is safe to get the devices. */
473 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
474 struct rcu_string *name;
476 device = kzalloc(sizeof(*device), GFP_NOFS);
481 * This is ok to do without rcu read locked because we hold the
482 * uuid mutex so nothing we touch in here is going to disappear.
484 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
489 rcu_assign_pointer(device->name, name);
491 device->devid = orig_dev->devid;
492 device->work.func = pending_bios_fn;
493 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
494 spin_lock_init(&device->io_lock);
495 INIT_LIST_HEAD(&device->dev_list);
496 INIT_LIST_HEAD(&device->dev_alloc_list);
498 list_add(&device->dev_list, &fs_devices->devices);
499 device->fs_devices = fs_devices;
500 fs_devices->num_devices++;
504 free_fs_devices(fs_devices);
505 return ERR_PTR(-ENOMEM);
508 void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
510 struct btrfs_device *device, *next;
512 struct block_device *latest_bdev = NULL;
513 u64 latest_devid = 0;
514 u64 latest_transid = 0;
516 mutex_lock(&uuid_mutex);
518 /* This is the initialized path, it is safe to release the devices. */
519 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
520 if (device->in_fs_metadata) {
521 if (!device->is_tgtdev_for_dev_replace &&
523 device->generation > latest_transid)) {
524 latest_devid = device->devid;
525 latest_transid = device->generation;
526 latest_bdev = device->bdev;
532 blkdev_put(device->bdev, device->mode);
534 fs_devices->open_devices--;
536 if (device->writeable) {
537 list_del_init(&device->dev_alloc_list);
538 device->writeable = 0;
539 fs_devices->rw_devices--;
541 list_del_init(&device->dev_list);
542 fs_devices->num_devices--;
543 rcu_string_free(device->name);
547 if (fs_devices->seed) {
548 fs_devices = fs_devices->seed;
552 fs_devices->latest_bdev = latest_bdev;
553 fs_devices->latest_devid = latest_devid;
554 fs_devices->latest_trans = latest_transid;
556 mutex_unlock(&uuid_mutex);
559 static void __free_device(struct work_struct *work)
561 struct btrfs_device *device;
563 device = container_of(work, struct btrfs_device, rcu_work);
566 blkdev_put(device->bdev, device->mode);
568 rcu_string_free(device->name);
572 static void free_device(struct rcu_head *head)
574 struct btrfs_device *device;
576 device = container_of(head, struct btrfs_device, rcu);
578 INIT_WORK(&device->rcu_work, __free_device);
579 schedule_work(&device->rcu_work);
582 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
584 struct btrfs_device *device;
586 if (--fs_devices->opened > 0)
589 mutex_lock(&fs_devices->device_list_mutex);
590 list_for_each_entry(device, &fs_devices->devices, dev_list) {
591 struct btrfs_device *new_device;
592 struct rcu_string *name;
595 fs_devices->open_devices--;
597 if (device->writeable) {
598 list_del_init(&device->dev_alloc_list);
599 fs_devices->rw_devices--;
602 if (device->can_discard)
603 fs_devices->num_can_discard--;
605 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
606 BUG_ON(!new_device); /* -ENOMEM */
607 memcpy(new_device, device, sizeof(*new_device));
609 /* Safe because we are under uuid_mutex */
611 name = rcu_string_strdup(device->name->str, GFP_NOFS);
612 BUG_ON(device->name && !name); /* -ENOMEM */
613 rcu_assign_pointer(new_device->name, name);
615 new_device->bdev = NULL;
616 new_device->writeable = 0;
617 new_device->in_fs_metadata = 0;
618 new_device->can_discard = 0;
619 list_replace_rcu(&device->dev_list, &new_device->dev_list);
621 call_rcu(&device->rcu, free_device);
623 mutex_unlock(&fs_devices->device_list_mutex);
625 WARN_ON(fs_devices->open_devices);
626 WARN_ON(fs_devices->rw_devices);
627 fs_devices->opened = 0;
628 fs_devices->seeding = 0;
633 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
635 struct btrfs_fs_devices *seed_devices = NULL;
638 mutex_lock(&uuid_mutex);
639 ret = __btrfs_close_devices(fs_devices);
640 if (!fs_devices->opened) {
641 seed_devices = fs_devices->seed;
642 fs_devices->seed = NULL;
644 mutex_unlock(&uuid_mutex);
646 while (seed_devices) {
647 fs_devices = seed_devices;
648 seed_devices = fs_devices->seed;
649 __btrfs_close_devices(fs_devices);
650 free_fs_devices(fs_devices);
655 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
656 fmode_t flags, void *holder)
658 struct request_queue *q;
659 struct block_device *bdev;
660 struct list_head *head = &fs_devices->devices;
661 struct btrfs_device *device;
662 struct block_device *latest_bdev = NULL;
663 struct buffer_head *bh;
664 struct btrfs_super_block *disk_super;
665 u64 latest_devid = 0;
666 u64 latest_transid = 0;
673 list_for_each_entry(device, head, dev_list) {
679 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
684 disk_super = (struct btrfs_super_block *)bh->b_data;
685 devid = btrfs_stack_device_id(&disk_super->dev_item);
686 if (devid != device->devid)
689 if (memcmp(device->uuid, disk_super->dev_item.uuid,
693 device->generation = btrfs_super_generation(disk_super);
694 if (!latest_transid || device->generation > latest_transid) {
695 latest_devid = devid;
696 latest_transid = device->generation;
700 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
701 device->writeable = 0;
703 device->writeable = !bdev_read_only(bdev);
707 q = bdev_get_queue(bdev);
708 if (blk_queue_discard(q)) {
709 device->can_discard = 1;
710 fs_devices->num_can_discard++;
714 device->in_fs_metadata = 0;
715 device->mode = flags;
717 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
718 fs_devices->rotating = 1;
720 fs_devices->open_devices++;
721 if (device->writeable) {
722 fs_devices->rw_devices++;
723 list_add(&device->dev_alloc_list,
724 &fs_devices->alloc_list);
731 blkdev_put(bdev, flags);
734 if (fs_devices->open_devices == 0) {
738 fs_devices->seeding = seeding;
739 fs_devices->opened = 1;
740 fs_devices->latest_bdev = latest_bdev;
741 fs_devices->latest_devid = latest_devid;
742 fs_devices->latest_trans = latest_transid;
743 fs_devices->total_rw_bytes = 0;
748 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
749 fmode_t flags, void *holder)
753 mutex_lock(&uuid_mutex);
754 if (fs_devices->opened) {
755 fs_devices->opened++;
758 ret = __btrfs_open_devices(fs_devices, flags, holder);
760 mutex_unlock(&uuid_mutex);
764 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
765 struct btrfs_fs_devices **fs_devices_ret)
767 struct btrfs_super_block *disk_super;
768 struct block_device *bdev;
769 struct buffer_head *bh;
776 mutex_lock(&uuid_mutex);
777 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
780 disk_super = (struct btrfs_super_block *)bh->b_data;
781 devid = btrfs_stack_device_id(&disk_super->dev_item);
782 transid = btrfs_super_generation(disk_super);
783 total_devices = btrfs_super_num_devices(disk_super);
784 if (disk_super->label[0]) {
785 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
786 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
787 printk(KERN_INFO "device label %s ", disk_super->label);
789 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
791 printk(KERN_CONT "devid %llu transid %llu %s\n",
792 (unsigned long long)devid, (unsigned long long)transid, path);
793 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
794 if (!ret && fs_devices_ret)
795 (*fs_devices_ret)->total_devices = total_devices;
797 blkdev_put(bdev, flags);
799 mutex_unlock(&uuid_mutex);
803 /* helper to account the used device space in the range */
804 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
805 u64 end, u64 *length)
807 struct btrfs_key key;
808 struct btrfs_root *root = device->dev_root;
809 struct btrfs_dev_extent *dev_extent;
810 struct btrfs_path *path;
814 struct extent_buffer *l;
818 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
821 path = btrfs_alloc_path();
826 key.objectid = device->devid;
828 key.type = BTRFS_DEV_EXTENT_KEY;
830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
834 ret = btrfs_previous_item(root, path, key.objectid, key.type);
841 slot = path->slots[0];
842 if (slot >= btrfs_header_nritems(l)) {
843 ret = btrfs_next_leaf(root, path);
851 btrfs_item_key_to_cpu(l, &key, slot);
853 if (key.objectid < device->devid)
856 if (key.objectid > device->devid)
859 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
862 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
863 extent_end = key.offset + btrfs_dev_extent_length(l,
865 if (key.offset <= start && extent_end > end) {
866 *length = end - start + 1;
868 } else if (key.offset <= start && extent_end > start)
869 *length += extent_end - start;
870 else if (key.offset > start && extent_end <= end)
871 *length += extent_end - key.offset;
872 else if (key.offset > start && key.offset <= end) {
873 *length += end - key.offset + 1;
875 } else if (key.offset > end)
883 btrfs_free_path(path);
888 * find_free_dev_extent - find free space in the specified device
889 * @device: the device which we search the free space in
890 * @num_bytes: the size of the free space that we need
891 * @start: store the start of the free space.
892 * @len: the size of the free space. that we find, or the size of the max
893 * free space if we don't find suitable free space
895 * this uses a pretty simple search, the expectation is that it is
896 * called very infrequently and that a given device has a small number
899 * @start is used to store the start of the free space if we find. But if we
900 * don't find suitable free space, it will be used to store the start position
901 * of the max free space.
903 * @len is used to store the size of the free space that we find.
904 * But if we don't find suitable free space, it is used to store the size of
905 * the max free space.
907 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
908 u64 *start, u64 *len)
910 struct btrfs_key key;
911 struct btrfs_root *root = device->dev_root;
912 struct btrfs_dev_extent *dev_extent;
913 struct btrfs_path *path;
919 u64 search_end = device->total_bytes;
922 struct extent_buffer *l;
924 /* FIXME use last free of some kind */
926 /* we don't want to overwrite the superblock on the drive,
927 * so we make sure to start at an offset of at least 1MB
929 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
931 max_hole_start = search_start;
935 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
940 path = btrfs_alloc_path();
947 key.objectid = device->devid;
948 key.offset = search_start;
949 key.type = BTRFS_DEV_EXTENT_KEY;
951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
955 ret = btrfs_previous_item(root, path, key.objectid, key.type);
962 slot = path->slots[0];
963 if (slot >= btrfs_header_nritems(l)) {
964 ret = btrfs_next_leaf(root, path);
972 btrfs_item_key_to_cpu(l, &key, slot);
974 if (key.objectid < device->devid)
977 if (key.objectid > device->devid)
980 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
983 if (key.offset > search_start) {
984 hole_size = key.offset - search_start;
986 if (hole_size > max_hole_size) {
987 max_hole_start = search_start;
988 max_hole_size = hole_size;
992 * If this free space is greater than which we need,
993 * it must be the max free space that we have found
994 * until now, so max_hole_start must point to the start
995 * of this free space and the length of this free space
996 * is stored in max_hole_size. Thus, we return
997 * max_hole_start and max_hole_size and go back to the
1000 if (hole_size >= num_bytes) {
1006 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1007 extent_end = key.offset + btrfs_dev_extent_length(l,
1009 if (extent_end > search_start)
1010 search_start = extent_end;
1017 * At this point, search_start should be the end of
1018 * allocated dev extents, and when shrinking the device,
1019 * search_end may be smaller than search_start.
1021 if (search_end > search_start)
1022 hole_size = search_end - search_start;
1024 if (hole_size > max_hole_size) {
1025 max_hole_start = search_start;
1026 max_hole_size = hole_size;
1030 if (hole_size < num_bytes)
1036 btrfs_free_path(path);
1038 *start = max_hole_start;
1040 *len = max_hole_size;
1044 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1045 struct btrfs_device *device,
1049 struct btrfs_path *path;
1050 struct btrfs_root *root = device->dev_root;
1051 struct btrfs_key key;
1052 struct btrfs_key found_key;
1053 struct extent_buffer *leaf = NULL;
1054 struct btrfs_dev_extent *extent = NULL;
1056 path = btrfs_alloc_path();
1060 key.objectid = device->devid;
1062 key.type = BTRFS_DEV_EXTENT_KEY;
1064 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1066 ret = btrfs_previous_item(root, path, key.objectid,
1067 BTRFS_DEV_EXTENT_KEY);
1070 leaf = path->nodes[0];
1071 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1072 extent = btrfs_item_ptr(leaf, path->slots[0],
1073 struct btrfs_dev_extent);
1074 BUG_ON(found_key.offset > start || found_key.offset +
1075 btrfs_dev_extent_length(leaf, extent) < start);
1077 btrfs_release_path(path);
1079 } else if (ret == 0) {
1080 leaf = path->nodes[0];
1081 extent = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_dev_extent);
1084 btrfs_error(root->fs_info, ret, "Slot search failed");
1088 if (device->bytes_used > 0) {
1089 u64 len = btrfs_dev_extent_length(leaf, extent);
1090 device->bytes_used -= len;
1091 spin_lock(&root->fs_info->free_chunk_lock);
1092 root->fs_info->free_chunk_space += len;
1093 spin_unlock(&root->fs_info->free_chunk_lock);
1095 ret = btrfs_del_item(trans, root, path);
1097 btrfs_error(root->fs_info, ret,
1098 "Failed to remove dev extent item");
1101 btrfs_free_path(path);
1105 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1106 struct btrfs_device *device,
1107 u64 chunk_tree, u64 chunk_objectid,
1108 u64 chunk_offset, u64 start, u64 num_bytes)
1111 struct btrfs_path *path;
1112 struct btrfs_root *root = device->dev_root;
1113 struct btrfs_dev_extent *extent;
1114 struct extent_buffer *leaf;
1115 struct btrfs_key key;
1117 WARN_ON(!device->in_fs_metadata);
1118 WARN_ON(device->is_tgtdev_for_dev_replace);
1119 path = btrfs_alloc_path();
1123 key.objectid = device->devid;
1125 key.type = BTRFS_DEV_EXTENT_KEY;
1126 ret = btrfs_insert_empty_item(trans, root, path, &key,
1131 leaf = path->nodes[0];
1132 extent = btrfs_item_ptr(leaf, path->slots[0],
1133 struct btrfs_dev_extent);
1134 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1135 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1136 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1138 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1139 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1142 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1143 btrfs_mark_buffer_dirty(leaf);
1145 btrfs_free_path(path);
1149 static noinline int find_next_chunk(struct btrfs_root *root,
1150 u64 objectid, u64 *offset)
1152 struct btrfs_path *path;
1154 struct btrfs_key key;
1155 struct btrfs_chunk *chunk;
1156 struct btrfs_key found_key;
1158 path = btrfs_alloc_path();
1162 key.objectid = objectid;
1163 key.offset = (u64)-1;
1164 key.type = BTRFS_CHUNK_ITEM_KEY;
1166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1170 BUG_ON(ret == 0); /* Corruption */
1172 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1176 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1178 if (found_key.objectid != objectid)
1181 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1182 struct btrfs_chunk);
1183 *offset = found_key.offset +
1184 btrfs_chunk_length(path->nodes[0], chunk);
1189 btrfs_free_path(path);
1193 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1196 struct btrfs_key key;
1197 struct btrfs_key found_key;
1198 struct btrfs_path *path;
1200 root = root->fs_info->chunk_root;
1202 path = btrfs_alloc_path();
1206 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1207 key.type = BTRFS_DEV_ITEM_KEY;
1208 key.offset = (u64)-1;
1210 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1214 BUG_ON(ret == 0); /* Corruption */
1216 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1217 BTRFS_DEV_ITEM_KEY);
1221 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1223 *objectid = found_key.offset + 1;
1227 btrfs_free_path(path);
1232 * the device information is stored in the chunk root
1233 * the btrfs_device struct should be fully filled in
1235 int btrfs_add_device(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root,
1237 struct btrfs_device *device)
1240 struct btrfs_path *path;
1241 struct btrfs_dev_item *dev_item;
1242 struct extent_buffer *leaf;
1243 struct btrfs_key key;
1246 root = root->fs_info->chunk_root;
1248 path = btrfs_alloc_path();
1252 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1253 key.type = BTRFS_DEV_ITEM_KEY;
1254 key.offset = device->devid;
1256 ret = btrfs_insert_empty_item(trans, root, path, &key,
1261 leaf = path->nodes[0];
1262 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1264 btrfs_set_device_id(leaf, dev_item, device->devid);
1265 btrfs_set_device_generation(leaf, dev_item, 0);
1266 btrfs_set_device_type(leaf, dev_item, device->type);
1267 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1268 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1269 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1270 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1271 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1272 btrfs_set_device_group(leaf, dev_item, 0);
1273 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1274 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1275 btrfs_set_device_start_offset(leaf, dev_item, 0);
1277 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1278 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1279 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1280 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1281 btrfs_mark_buffer_dirty(leaf);
1285 btrfs_free_path(path);
1289 static int btrfs_rm_dev_item(struct btrfs_root *root,
1290 struct btrfs_device *device)
1293 struct btrfs_path *path;
1294 struct btrfs_key key;
1295 struct btrfs_trans_handle *trans;
1297 root = root->fs_info->chunk_root;
1299 path = btrfs_alloc_path();
1303 trans = btrfs_start_transaction(root, 0);
1304 if (IS_ERR(trans)) {
1305 btrfs_free_path(path);
1306 return PTR_ERR(trans);
1308 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1309 key.type = BTRFS_DEV_ITEM_KEY;
1310 key.offset = device->devid;
1313 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1322 ret = btrfs_del_item(trans, root, path);
1326 btrfs_free_path(path);
1327 unlock_chunks(root);
1328 btrfs_commit_transaction(trans, root);
1332 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1334 struct btrfs_device *device;
1335 struct btrfs_device *next_device;
1336 struct block_device *bdev;
1337 struct buffer_head *bh = NULL;
1338 struct btrfs_super_block *disk_super;
1339 struct btrfs_fs_devices *cur_devices;
1345 bool clear_super = false;
1347 mutex_lock(&uuid_mutex);
1349 all_avail = root->fs_info->avail_data_alloc_bits |
1350 root->fs_info->avail_system_alloc_bits |
1351 root->fs_info->avail_metadata_alloc_bits;
1353 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1354 root->fs_info->fs_devices->num_devices <= 4) {
1355 printk(KERN_ERR "btrfs: unable to go below four devices "
1361 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1362 root->fs_info->fs_devices->num_devices <= 2) {
1363 printk(KERN_ERR "btrfs: unable to go below two "
1364 "devices on raid1\n");
1369 if (strcmp(device_path, "missing") == 0) {
1370 struct list_head *devices;
1371 struct btrfs_device *tmp;
1374 devices = &root->fs_info->fs_devices->devices;
1376 * It is safe to read the devices since the volume_mutex
1379 list_for_each_entry(tmp, devices, dev_list) {
1380 if (tmp->in_fs_metadata &&
1381 !tmp->is_tgtdev_for_dev_replace &&
1391 printk(KERN_ERR "btrfs: no missing devices found to "
1396 ret = btrfs_get_bdev_and_sb(device_path,
1397 FMODE_READ | FMODE_EXCL,
1398 root->fs_info->bdev_holder, 0,
1402 disk_super = (struct btrfs_super_block *)bh->b_data;
1403 devid = btrfs_stack_device_id(&disk_super->dev_item);
1404 dev_uuid = disk_super->dev_item.uuid;
1405 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1413 if (device->is_tgtdev_for_dev_replace) {
1414 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1419 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1420 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1426 if (device->writeable) {
1428 list_del_init(&device->dev_alloc_list);
1429 unlock_chunks(root);
1430 root->fs_info->fs_devices->rw_devices--;
1434 ret = btrfs_shrink_device(device, 0);
1439 * TODO: the superblock still includes this device in its num_devices
1440 * counter although write_all_supers() is not locked out. This
1441 * could give a filesystem state which requires a degraded mount.
1443 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1447 spin_lock(&root->fs_info->free_chunk_lock);
1448 root->fs_info->free_chunk_space = device->total_bytes -
1450 spin_unlock(&root->fs_info->free_chunk_lock);
1452 device->in_fs_metadata = 0;
1453 btrfs_scrub_cancel_dev(root->fs_info, device);
1456 * the device list mutex makes sure that we don't change
1457 * the device list while someone else is writing out all
1458 * the device supers.
1461 cur_devices = device->fs_devices;
1462 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1463 list_del_rcu(&device->dev_list);
1465 device->fs_devices->num_devices--;
1466 device->fs_devices->total_devices--;
1468 if (device->missing)
1469 root->fs_info->fs_devices->missing_devices--;
1471 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1472 struct btrfs_device, dev_list);
1473 if (device->bdev == root->fs_info->sb->s_bdev)
1474 root->fs_info->sb->s_bdev = next_device->bdev;
1475 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1476 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1479 device->fs_devices->open_devices--;
1481 call_rcu(&device->rcu, free_device);
1482 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1484 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1485 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1487 if (cur_devices->open_devices == 0) {
1488 struct btrfs_fs_devices *fs_devices;
1489 fs_devices = root->fs_info->fs_devices;
1490 while (fs_devices) {
1491 if (fs_devices->seed == cur_devices)
1493 fs_devices = fs_devices->seed;
1495 fs_devices->seed = cur_devices->seed;
1496 cur_devices->seed = NULL;
1498 __btrfs_close_devices(cur_devices);
1499 unlock_chunks(root);
1500 free_fs_devices(cur_devices);
1503 root->fs_info->num_tolerated_disk_barrier_failures =
1504 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1507 * at this point, the device is zero sized. We want to
1508 * remove it from the devices list and zero out the old super
1510 if (clear_super && disk_super) {
1511 /* make sure this device isn't detected as part of
1514 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1515 set_buffer_dirty(bh);
1516 sync_dirty_buffer(bh);
1525 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1527 mutex_unlock(&uuid_mutex);
1530 if (device->writeable) {
1532 list_add(&device->dev_alloc_list,
1533 &root->fs_info->fs_devices->alloc_list);
1534 unlock_chunks(root);
1535 root->fs_info->fs_devices->rw_devices++;
1540 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1541 struct btrfs_device *srcdev)
1543 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1544 list_del_rcu(&srcdev->dev_list);
1545 list_del_rcu(&srcdev->dev_alloc_list);
1546 fs_info->fs_devices->num_devices--;
1547 if (srcdev->missing) {
1548 fs_info->fs_devices->missing_devices--;
1549 fs_info->fs_devices->rw_devices++;
1551 if (srcdev->can_discard)
1552 fs_info->fs_devices->num_can_discard--;
1554 fs_info->fs_devices->open_devices--;
1556 call_rcu(&srcdev->rcu, free_device);
1559 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1560 struct btrfs_device *tgtdev)
1562 struct btrfs_device *next_device;
1565 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1567 btrfs_scratch_superblock(tgtdev);
1568 fs_info->fs_devices->open_devices--;
1570 fs_info->fs_devices->num_devices--;
1571 if (tgtdev->can_discard)
1572 fs_info->fs_devices->num_can_discard++;
1574 next_device = list_entry(fs_info->fs_devices->devices.next,
1575 struct btrfs_device, dev_list);
1576 if (tgtdev->bdev == fs_info->sb->s_bdev)
1577 fs_info->sb->s_bdev = next_device->bdev;
1578 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1579 fs_info->fs_devices->latest_bdev = next_device->bdev;
1580 list_del_rcu(&tgtdev->dev_list);
1582 call_rcu(&tgtdev->rcu, free_device);
1584 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1587 int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1588 struct btrfs_device **device)
1591 struct btrfs_super_block *disk_super;
1594 struct block_device *bdev;
1595 struct buffer_head *bh;
1598 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1599 root->fs_info->bdev_holder, 0, &bdev, &bh);
1602 disk_super = (struct btrfs_super_block *)bh->b_data;
1603 devid = btrfs_stack_device_id(&disk_super->dev_item);
1604 dev_uuid = disk_super->dev_item.uuid;
1605 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1610 blkdev_put(bdev, FMODE_READ);
1614 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1616 struct btrfs_device **device)
1619 if (strcmp(device_path, "missing") == 0) {
1620 struct list_head *devices;
1621 struct btrfs_device *tmp;
1623 devices = &root->fs_info->fs_devices->devices;
1625 * It is safe to read the devices since the volume_mutex
1626 * is held by the caller.
1628 list_for_each_entry(tmp, devices, dev_list) {
1629 if (tmp->in_fs_metadata && !tmp->bdev) {
1636 pr_err("btrfs: no missing device found\n");
1642 return btrfs_find_device_by_path(root, device_path, device);
1647 * does all the dirty work required for changing file system's UUID.
1649 static int btrfs_prepare_sprout(struct btrfs_root *root)
1651 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1652 struct btrfs_fs_devices *old_devices;
1653 struct btrfs_fs_devices *seed_devices;
1654 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1655 struct btrfs_device *device;
1658 BUG_ON(!mutex_is_locked(&uuid_mutex));
1659 if (!fs_devices->seeding)
1662 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1666 old_devices = clone_fs_devices(fs_devices);
1667 if (IS_ERR(old_devices)) {
1668 kfree(seed_devices);
1669 return PTR_ERR(old_devices);
1672 list_add(&old_devices->list, &fs_uuids);
1674 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1675 seed_devices->opened = 1;
1676 INIT_LIST_HEAD(&seed_devices->devices);
1677 INIT_LIST_HEAD(&seed_devices->alloc_list);
1678 mutex_init(&seed_devices->device_list_mutex);
1680 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1681 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1683 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1685 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1686 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1687 device->fs_devices = seed_devices;
1690 fs_devices->seeding = 0;
1691 fs_devices->num_devices = 0;
1692 fs_devices->open_devices = 0;
1693 fs_devices->total_devices = 0;
1694 fs_devices->seed = seed_devices;
1696 generate_random_uuid(fs_devices->fsid);
1697 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1698 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1699 super_flags = btrfs_super_flags(disk_super) &
1700 ~BTRFS_SUPER_FLAG_SEEDING;
1701 btrfs_set_super_flags(disk_super, super_flags);
1707 * strore the expected generation for seed devices in device items.
1709 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root)
1712 struct btrfs_path *path;
1713 struct extent_buffer *leaf;
1714 struct btrfs_dev_item *dev_item;
1715 struct btrfs_device *device;
1716 struct btrfs_key key;
1717 u8 fs_uuid[BTRFS_UUID_SIZE];
1718 u8 dev_uuid[BTRFS_UUID_SIZE];
1722 path = btrfs_alloc_path();
1726 root = root->fs_info->chunk_root;
1727 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1729 key.type = BTRFS_DEV_ITEM_KEY;
1732 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1736 leaf = path->nodes[0];
1738 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1739 ret = btrfs_next_leaf(root, path);
1744 leaf = path->nodes[0];
1745 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1746 btrfs_release_path(path);
1750 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1751 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1752 key.type != BTRFS_DEV_ITEM_KEY)
1755 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1756 struct btrfs_dev_item);
1757 devid = btrfs_device_id(leaf, dev_item);
1758 read_extent_buffer(leaf, dev_uuid,
1759 (unsigned long)btrfs_device_uuid(dev_item),
1761 read_extent_buffer(leaf, fs_uuid,
1762 (unsigned long)btrfs_device_fsid(dev_item),
1764 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1766 BUG_ON(!device); /* Logic error */
1768 if (device->fs_devices->seeding) {
1769 btrfs_set_device_generation(leaf, dev_item,
1770 device->generation);
1771 btrfs_mark_buffer_dirty(leaf);
1779 btrfs_free_path(path);
1783 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1785 struct request_queue *q;
1786 struct btrfs_trans_handle *trans;
1787 struct btrfs_device *device;
1788 struct block_device *bdev;
1789 struct list_head *devices;
1790 struct super_block *sb = root->fs_info->sb;
1791 struct rcu_string *name;
1793 int seeding_dev = 0;
1796 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1799 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1800 root->fs_info->bdev_holder);
1802 return PTR_ERR(bdev);
1804 if (root->fs_info->fs_devices->seeding) {
1806 down_write(&sb->s_umount);
1807 mutex_lock(&uuid_mutex);
1810 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1812 devices = &root->fs_info->fs_devices->devices;
1814 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1815 list_for_each_entry(device, devices, dev_list) {
1816 if (device->bdev == bdev) {
1819 &root->fs_info->fs_devices->device_list_mutex);
1823 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1825 device = kzalloc(sizeof(*device), GFP_NOFS);
1827 /* we can safely leave the fs_devices entry around */
1832 name = rcu_string_strdup(device_path, GFP_NOFS);
1838 rcu_assign_pointer(device->name, name);
1840 ret = find_next_devid(root, &device->devid);
1842 rcu_string_free(device->name);
1847 trans = btrfs_start_transaction(root, 0);
1848 if (IS_ERR(trans)) {
1849 rcu_string_free(device->name);
1851 ret = PTR_ERR(trans);
1857 q = bdev_get_queue(bdev);
1858 if (blk_queue_discard(q))
1859 device->can_discard = 1;
1860 device->writeable = 1;
1861 device->work.func = pending_bios_fn;
1862 generate_random_uuid(device->uuid);
1863 spin_lock_init(&device->io_lock);
1864 device->generation = trans->transid;
1865 device->io_width = root->sectorsize;
1866 device->io_align = root->sectorsize;
1867 device->sector_size = root->sectorsize;
1868 device->total_bytes = i_size_read(bdev->bd_inode);
1869 device->disk_total_bytes = device->total_bytes;
1870 device->dev_root = root->fs_info->dev_root;
1871 device->bdev = bdev;
1872 device->in_fs_metadata = 1;
1873 device->is_tgtdev_for_dev_replace = 0;
1874 device->mode = FMODE_EXCL;
1875 set_blocksize(device->bdev, 4096);
1878 sb->s_flags &= ~MS_RDONLY;
1879 ret = btrfs_prepare_sprout(root);
1880 BUG_ON(ret); /* -ENOMEM */
1883 device->fs_devices = root->fs_info->fs_devices;
1885 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1886 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1887 list_add(&device->dev_alloc_list,
1888 &root->fs_info->fs_devices->alloc_list);
1889 root->fs_info->fs_devices->num_devices++;
1890 root->fs_info->fs_devices->open_devices++;
1891 root->fs_info->fs_devices->rw_devices++;
1892 root->fs_info->fs_devices->total_devices++;
1893 if (device->can_discard)
1894 root->fs_info->fs_devices->num_can_discard++;
1895 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1897 spin_lock(&root->fs_info->free_chunk_lock);
1898 root->fs_info->free_chunk_space += device->total_bytes;
1899 spin_unlock(&root->fs_info->free_chunk_lock);
1901 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1902 root->fs_info->fs_devices->rotating = 1;
1904 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1905 btrfs_set_super_total_bytes(root->fs_info->super_copy,
1906 total_bytes + device->total_bytes);
1908 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1909 btrfs_set_super_num_devices(root->fs_info->super_copy,
1911 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1914 ret = init_first_rw_device(trans, root, device);
1916 btrfs_abort_transaction(trans, root, ret);
1919 ret = btrfs_finish_sprout(trans, root);
1921 btrfs_abort_transaction(trans, root, ret);
1925 ret = btrfs_add_device(trans, root, device);
1927 btrfs_abort_transaction(trans, root, ret);
1933 * we've got more storage, clear any full flags on the space
1936 btrfs_clear_space_info_full(root->fs_info);
1938 unlock_chunks(root);
1939 root->fs_info->num_tolerated_disk_barrier_failures =
1940 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1941 ret = btrfs_commit_transaction(trans, root);
1944 mutex_unlock(&uuid_mutex);
1945 up_write(&sb->s_umount);
1947 if (ret) /* transaction commit */
1950 ret = btrfs_relocate_sys_chunks(root);
1952 btrfs_error(root->fs_info, ret,
1953 "Failed to relocate sys chunks after "
1954 "device initialization. This can be fixed "
1955 "using the \"btrfs balance\" command.");
1956 trans = btrfs_attach_transaction(root);
1957 if (IS_ERR(trans)) {
1958 if (PTR_ERR(trans) == -ENOENT)
1960 return PTR_ERR(trans);
1962 ret = btrfs_commit_transaction(trans, root);
1968 unlock_chunks(root);
1969 btrfs_end_transaction(trans, root);
1970 rcu_string_free(device->name);
1973 blkdev_put(bdev, FMODE_EXCL);
1975 mutex_unlock(&uuid_mutex);
1976 up_write(&sb->s_umount);
1981 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
1982 struct btrfs_device **device_out)
1984 struct request_queue *q;
1985 struct btrfs_device *device;
1986 struct block_device *bdev;
1987 struct btrfs_fs_info *fs_info = root->fs_info;
1988 struct list_head *devices;
1989 struct rcu_string *name;
1993 if (fs_info->fs_devices->seeding)
1996 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1997 fs_info->bdev_holder);
1999 return PTR_ERR(bdev);
2001 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2003 devices = &fs_info->fs_devices->devices;
2004 list_for_each_entry(device, devices, dev_list) {
2005 if (device->bdev == bdev) {
2011 device = kzalloc(sizeof(*device), GFP_NOFS);
2017 name = rcu_string_strdup(device_path, GFP_NOFS);
2023 rcu_assign_pointer(device->name, name);
2025 q = bdev_get_queue(bdev);
2026 if (blk_queue_discard(q))
2027 device->can_discard = 1;
2028 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2029 device->writeable = 1;
2030 device->work.func = pending_bios_fn;
2031 generate_random_uuid(device->uuid);
2032 device->devid = BTRFS_DEV_REPLACE_DEVID;
2033 spin_lock_init(&device->io_lock);
2034 device->generation = 0;
2035 device->io_width = root->sectorsize;
2036 device->io_align = root->sectorsize;
2037 device->sector_size = root->sectorsize;
2038 device->total_bytes = i_size_read(bdev->bd_inode);
2039 device->disk_total_bytes = device->total_bytes;
2040 device->dev_root = fs_info->dev_root;
2041 device->bdev = bdev;
2042 device->in_fs_metadata = 1;
2043 device->is_tgtdev_for_dev_replace = 1;
2044 device->mode = FMODE_EXCL;
2045 set_blocksize(device->bdev, 4096);
2046 device->fs_devices = fs_info->fs_devices;
2047 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2048 fs_info->fs_devices->num_devices++;
2049 fs_info->fs_devices->open_devices++;
2050 if (device->can_discard)
2051 fs_info->fs_devices->num_can_discard++;
2052 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2054 *device_out = device;
2058 blkdev_put(bdev, FMODE_EXCL);
2062 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2063 struct btrfs_device *tgtdev)
2065 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2066 tgtdev->io_width = fs_info->dev_root->sectorsize;
2067 tgtdev->io_align = fs_info->dev_root->sectorsize;
2068 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2069 tgtdev->dev_root = fs_info->dev_root;
2070 tgtdev->in_fs_metadata = 1;
2073 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2074 struct btrfs_device *device)
2077 struct btrfs_path *path;
2078 struct btrfs_root *root;
2079 struct btrfs_dev_item *dev_item;
2080 struct extent_buffer *leaf;
2081 struct btrfs_key key;
2083 root = device->dev_root->fs_info->chunk_root;
2085 path = btrfs_alloc_path();
2089 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2090 key.type = BTRFS_DEV_ITEM_KEY;
2091 key.offset = device->devid;
2093 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2102 leaf = path->nodes[0];
2103 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2105 btrfs_set_device_id(leaf, dev_item, device->devid);
2106 btrfs_set_device_type(leaf, dev_item, device->type);
2107 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2108 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2109 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2110 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
2111 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2112 btrfs_mark_buffer_dirty(leaf);
2115 btrfs_free_path(path);
2119 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
2120 struct btrfs_device *device, u64 new_size)
2122 struct btrfs_super_block *super_copy =
2123 device->dev_root->fs_info->super_copy;
2124 u64 old_total = btrfs_super_total_bytes(super_copy);
2125 u64 diff = new_size - device->total_bytes;
2127 if (!device->writeable)
2129 if (new_size <= device->total_bytes ||
2130 device->is_tgtdev_for_dev_replace)
2133 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2134 device->fs_devices->total_rw_bytes += diff;
2136 device->total_bytes = new_size;
2137 device->disk_total_bytes = new_size;
2138 btrfs_clear_space_info_full(device->dev_root->fs_info);
2140 return btrfs_update_device(trans, device);
2143 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2144 struct btrfs_device *device, u64 new_size)
2147 lock_chunks(device->dev_root);
2148 ret = __btrfs_grow_device(trans, device, new_size);
2149 unlock_chunks(device->dev_root);
2153 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2154 struct btrfs_root *root,
2155 u64 chunk_tree, u64 chunk_objectid,
2159 struct btrfs_path *path;
2160 struct btrfs_key key;
2162 root = root->fs_info->chunk_root;
2163 path = btrfs_alloc_path();
2167 key.objectid = chunk_objectid;
2168 key.offset = chunk_offset;
2169 key.type = BTRFS_CHUNK_ITEM_KEY;
2171 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2174 else if (ret > 0) { /* Logic error or corruption */
2175 btrfs_error(root->fs_info, -ENOENT,
2176 "Failed lookup while freeing chunk.");
2181 ret = btrfs_del_item(trans, root, path);
2183 btrfs_error(root->fs_info, ret,
2184 "Failed to delete chunk item.");
2186 btrfs_free_path(path);
2190 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2193 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2194 struct btrfs_disk_key *disk_key;
2195 struct btrfs_chunk *chunk;
2202 struct btrfs_key key;
2204 array_size = btrfs_super_sys_array_size(super_copy);
2206 ptr = super_copy->sys_chunk_array;
2209 while (cur < array_size) {
2210 disk_key = (struct btrfs_disk_key *)ptr;
2211 btrfs_disk_key_to_cpu(&key, disk_key);
2213 len = sizeof(*disk_key);
2215 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2216 chunk = (struct btrfs_chunk *)(ptr + len);
2217 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2218 len += btrfs_chunk_item_size(num_stripes);
2223 if (key.objectid == chunk_objectid &&
2224 key.offset == chunk_offset) {
2225 memmove(ptr, ptr + len, array_size - (cur + len));
2227 btrfs_set_super_sys_array_size(super_copy, array_size);
2236 static int btrfs_relocate_chunk(struct btrfs_root *root,
2237 u64 chunk_tree, u64 chunk_objectid,
2240 struct extent_map_tree *em_tree;
2241 struct btrfs_root *extent_root;
2242 struct btrfs_trans_handle *trans;
2243 struct extent_map *em;
2244 struct map_lookup *map;
2248 root = root->fs_info->chunk_root;
2249 extent_root = root->fs_info->extent_root;
2250 em_tree = &root->fs_info->mapping_tree.map_tree;
2252 ret = btrfs_can_relocate(extent_root, chunk_offset);
2256 /* step one, relocate all the extents inside this chunk */
2257 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2261 trans = btrfs_start_transaction(root, 0);
2262 BUG_ON(IS_ERR(trans));
2267 * step two, delete the device extents and the
2268 * chunk tree entries
2270 read_lock(&em_tree->lock);
2271 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2272 read_unlock(&em_tree->lock);
2274 BUG_ON(!em || em->start > chunk_offset ||
2275 em->start + em->len < chunk_offset);
2276 map = (struct map_lookup *)em->bdev;
2278 for (i = 0; i < map->num_stripes; i++) {
2279 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2280 map->stripes[i].physical);
2283 if (map->stripes[i].dev) {
2284 ret = btrfs_update_device(trans, map->stripes[i].dev);
2288 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2293 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2295 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2296 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2300 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2303 write_lock(&em_tree->lock);
2304 remove_extent_mapping(em_tree, em);
2305 write_unlock(&em_tree->lock);
2310 /* once for the tree */
2311 free_extent_map(em);
2313 free_extent_map(em);
2315 unlock_chunks(root);
2316 btrfs_end_transaction(trans, root);
2320 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2322 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2323 struct btrfs_path *path;
2324 struct extent_buffer *leaf;
2325 struct btrfs_chunk *chunk;
2326 struct btrfs_key key;
2327 struct btrfs_key found_key;
2328 u64 chunk_tree = chunk_root->root_key.objectid;
2330 bool retried = false;
2334 path = btrfs_alloc_path();
2339 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2340 key.offset = (u64)-1;
2341 key.type = BTRFS_CHUNK_ITEM_KEY;
2344 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2347 BUG_ON(ret == 0); /* Corruption */
2349 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2356 leaf = path->nodes[0];
2357 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2359 chunk = btrfs_item_ptr(leaf, path->slots[0],
2360 struct btrfs_chunk);
2361 chunk_type = btrfs_chunk_type(leaf, chunk);
2362 btrfs_release_path(path);
2364 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2365 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2374 if (found_key.offset == 0)
2376 key.offset = found_key.offset - 1;
2379 if (failed && !retried) {
2383 } else if (failed && retried) {
2388 btrfs_free_path(path);
2392 static int insert_balance_item(struct btrfs_root *root,
2393 struct btrfs_balance_control *bctl)
2395 struct btrfs_trans_handle *trans;
2396 struct btrfs_balance_item *item;
2397 struct btrfs_disk_balance_args disk_bargs;
2398 struct btrfs_path *path;
2399 struct extent_buffer *leaf;
2400 struct btrfs_key key;
2403 path = btrfs_alloc_path();
2407 trans = btrfs_start_transaction(root, 0);
2408 if (IS_ERR(trans)) {
2409 btrfs_free_path(path);
2410 return PTR_ERR(trans);
2413 key.objectid = BTRFS_BALANCE_OBJECTID;
2414 key.type = BTRFS_BALANCE_ITEM_KEY;
2417 ret = btrfs_insert_empty_item(trans, root, path, &key,
2422 leaf = path->nodes[0];
2423 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2425 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2427 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2428 btrfs_set_balance_data(leaf, item, &disk_bargs);
2429 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2430 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2431 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2432 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2434 btrfs_set_balance_flags(leaf, item, bctl->flags);
2436 btrfs_mark_buffer_dirty(leaf);
2438 btrfs_free_path(path);
2439 err = btrfs_commit_transaction(trans, root);
2445 static int del_balance_item(struct btrfs_root *root)
2447 struct btrfs_trans_handle *trans;
2448 struct btrfs_path *path;
2449 struct btrfs_key key;
2452 path = btrfs_alloc_path();
2456 trans = btrfs_start_transaction(root, 0);
2457 if (IS_ERR(trans)) {
2458 btrfs_free_path(path);
2459 return PTR_ERR(trans);
2462 key.objectid = BTRFS_BALANCE_OBJECTID;
2463 key.type = BTRFS_BALANCE_ITEM_KEY;
2466 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2474 ret = btrfs_del_item(trans, root, path);
2476 btrfs_free_path(path);
2477 err = btrfs_commit_transaction(trans, root);
2484 * This is a heuristic used to reduce the number of chunks balanced on
2485 * resume after balance was interrupted.
2487 static void update_balance_args(struct btrfs_balance_control *bctl)
2490 * Turn on soft mode for chunk types that were being converted.
2492 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2493 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2494 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2495 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2496 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2497 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2500 * Turn on usage filter if is not already used. The idea is
2501 * that chunks that we have already balanced should be
2502 * reasonably full. Don't do it for chunks that are being
2503 * converted - that will keep us from relocating unconverted
2504 * (albeit full) chunks.
2506 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2507 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2508 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2509 bctl->data.usage = 90;
2511 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2512 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2513 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2514 bctl->sys.usage = 90;
2516 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2517 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2518 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2519 bctl->meta.usage = 90;
2524 * Should be called with both balance and volume mutexes held to
2525 * serialize other volume operations (add_dev/rm_dev/resize) with
2526 * restriper. Same goes for unset_balance_control.
2528 static void set_balance_control(struct btrfs_balance_control *bctl)
2530 struct btrfs_fs_info *fs_info = bctl->fs_info;
2532 BUG_ON(fs_info->balance_ctl);
2534 spin_lock(&fs_info->balance_lock);
2535 fs_info->balance_ctl = bctl;
2536 spin_unlock(&fs_info->balance_lock);
2539 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2541 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2543 BUG_ON(!fs_info->balance_ctl);
2545 spin_lock(&fs_info->balance_lock);
2546 fs_info->balance_ctl = NULL;
2547 spin_unlock(&fs_info->balance_lock);
2553 * Balance filters. Return 1 if chunk should be filtered out
2554 * (should not be balanced).
2556 static int chunk_profiles_filter(u64 chunk_type,
2557 struct btrfs_balance_args *bargs)
2559 chunk_type = chunk_to_extended(chunk_type) &
2560 BTRFS_EXTENDED_PROFILE_MASK;
2562 if (bargs->profiles & chunk_type)
2568 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2569 struct btrfs_balance_args *bargs)
2571 struct btrfs_block_group_cache *cache;
2572 u64 chunk_used, user_thresh;
2575 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2576 chunk_used = btrfs_block_group_used(&cache->item);
2578 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2579 if (chunk_used < user_thresh)
2582 btrfs_put_block_group(cache);
2586 static int chunk_devid_filter(struct extent_buffer *leaf,
2587 struct btrfs_chunk *chunk,
2588 struct btrfs_balance_args *bargs)
2590 struct btrfs_stripe *stripe;
2591 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2594 for (i = 0; i < num_stripes; i++) {
2595 stripe = btrfs_stripe_nr(chunk, i);
2596 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2603 /* [pstart, pend) */
2604 static int chunk_drange_filter(struct extent_buffer *leaf,
2605 struct btrfs_chunk *chunk,
2607 struct btrfs_balance_args *bargs)
2609 struct btrfs_stripe *stripe;
2610 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2616 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2619 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2620 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2624 factor = num_stripes / factor;
2626 for (i = 0; i < num_stripes; i++) {
2627 stripe = btrfs_stripe_nr(chunk, i);
2628 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2631 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2632 stripe_length = btrfs_chunk_length(leaf, chunk);
2633 do_div(stripe_length, factor);
2635 if (stripe_offset < bargs->pend &&
2636 stripe_offset + stripe_length > bargs->pstart)
2643 /* [vstart, vend) */
2644 static int chunk_vrange_filter(struct extent_buffer *leaf,
2645 struct btrfs_chunk *chunk,
2647 struct btrfs_balance_args *bargs)
2649 if (chunk_offset < bargs->vend &&
2650 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2651 /* at least part of the chunk is inside this vrange */
2657 static int chunk_soft_convert_filter(u64 chunk_type,
2658 struct btrfs_balance_args *bargs)
2660 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2663 chunk_type = chunk_to_extended(chunk_type) &
2664 BTRFS_EXTENDED_PROFILE_MASK;
2666 if (bargs->target == chunk_type)
2672 static int should_balance_chunk(struct btrfs_root *root,
2673 struct extent_buffer *leaf,
2674 struct btrfs_chunk *chunk, u64 chunk_offset)
2676 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2677 struct btrfs_balance_args *bargs = NULL;
2678 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2681 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2682 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2686 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2687 bargs = &bctl->data;
2688 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2690 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2691 bargs = &bctl->meta;
2693 /* profiles filter */
2694 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2695 chunk_profiles_filter(chunk_type, bargs)) {
2700 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2701 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2706 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2707 chunk_devid_filter(leaf, chunk, bargs)) {
2711 /* drange filter, makes sense only with devid filter */
2712 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2713 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2718 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2719 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2723 /* soft profile changing mode */
2724 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2725 chunk_soft_convert_filter(chunk_type, bargs)) {
2732 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
2734 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2735 struct btrfs_root *chunk_root = fs_info->chunk_root;
2736 struct btrfs_root *dev_root = fs_info->dev_root;
2737 struct list_head *devices;
2738 struct btrfs_device *device;
2741 struct btrfs_chunk *chunk;
2742 struct btrfs_path *path;
2743 struct btrfs_key key;
2744 struct btrfs_key found_key;
2745 struct btrfs_trans_handle *trans;
2746 struct extent_buffer *leaf;
2749 int enospc_errors = 0;
2750 bool counting = true;
2752 /* step one make some room on all the devices */
2753 devices = &fs_info->fs_devices->devices;
2754 list_for_each_entry(device, devices, dev_list) {
2755 old_size = device->total_bytes;
2756 size_to_free = div_factor(old_size, 1);
2757 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2758 if (!device->writeable ||
2759 device->total_bytes - device->bytes_used > size_to_free ||
2760 device->is_tgtdev_for_dev_replace)
2763 ret = btrfs_shrink_device(device, old_size - size_to_free);
2768 trans = btrfs_start_transaction(dev_root, 0);
2769 BUG_ON(IS_ERR(trans));
2771 ret = btrfs_grow_device(trans, device, old_size);
2774 btrfs_end_transaction(trans, dev_root);
2777 /* step two, relocate all the chunks */
2778 path = btrfs_alloc_path();
2784 /* zero out stat counters */
2785 spin_lock(&fs_info->balance_lock);
2786 memset(&bctl->stat, 0, sizeof(bctl->stat));
2787 spin_unlock(&fs_info->balance_lock);
2789 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2790 key.offset = (u64)-1;
2791 key.type = BTRFS_CHUNK_ITEM_KEY;
2794 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
2795 atomic_read(&fs_info->balance_cancel_req)) {
2800 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2805 * this shouldn't happen, it means the last relocate
2809 BUG(); /* FIXME break ? */
2811 ret = btrfs_previous_item(chunk_root, path, 0,
2812 BTRFS_CHUNK_ITEM_KEY);
2818 leaf = path->nodes[0];
2819 slot = path->slots[0];
2820 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2822 if (found_key.objectid != key.objectid)
2825 /* chunk zero is special */
2826 if (found_key.offset == 0)
2829 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2832 spin_lock(&fs_info->balance_lock);
2833 bctl->stat.considered++;
2834 spin_unlock(&fs_info->balance_lock);
2837 ret = should_balance_chunk(chunk_root, leaf, chunk,
2839 btrfs_release_path(path);
2844 spin_lock(&fs_info->balance_lock);
2845 bctl->stat.expected++;
2846 spin_unlock(&fs_info->balance_lock);
2850 ret = btrfs_relocate_chunk(chunk_root,
2851 chunk_root->root_key.objectid,
2854 if (ret && ret != -ENOSPC)
2856 if (ret == -ENOSPC) {
2859 spin_lock(&fs_info->balance_lock);
2860 bctl->stat.completed++;
2861 spin_unlock(&fs_info->balance_lock);
2864 key.offset = found_key.offset - 1;
2868 btrfs_release_path(path);
2873 btrfs_free_path(path);
2874 if (enospc_errors) {
2875 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2885 * alloc_profile_is_valid - see if a given profile is valid and reduced
2886 * @flags: profile to validate
2887 * @extended: if true @flags is treated as an extended profile
2889 static int alloc_profile_is_valid(u64 flags, int extended)
2891 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2892 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2894 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2896 /* 1) check that all other bits are zeroed */
2900 /* 2) see if profile is reduced */
2902 return !extended; /* "0" is valid for usual profiles */
2904 /* true if exactly one bit set */
2905 return (flags & (flags - 1)) == 0;
2908 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2910 /* cancel requested || normal exit path */
2911 return atomic_read(&fs_info->balance_cancel_req) ||
2912 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2913 atomic_read(&fs_info->balance_cancel_req) == 0);
2916 static void __cancel_balance(struct btrfs_fs_info *fs_info)
2920 unset_balance_control(fs_info);
2921 ret = del_balance_item(fs_info->tree_root);
2925 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
2926 struct btrfs_ioctl_balance_args *bargs);
2929 * Should be called with both balance and volume mutexes held
2931 int btrfs_balance(struct btrfs_balance_control *bctl,
2932 struct btrfs_ioctl_balance_args *bargs)
2934 struct btrfs_fs_info *fs_info = bctl->fs_info;
2939 if (btrfs_fs_closing(fs_info) ||
2940 atomic_read(&fs_info->balance_pause_req) ||
2941 atomic_read(&fs_info->balance_cancel_req)) {
2946 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2947 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2951 * In case of mixed groups both data and meta should be picked,
2952 * and identical options should be given for both of them.
2954 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2955 if (mixed && (bctl->flags & allowed)) {
2956 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2957 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2958 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2959 printk(KERN_ERR "btrfs: with mixed groups data and "
2960 "metadata balance options must be the same\n");
2966 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2967 if (fs_info->fs_devices->num_devices == 1)
2968 allowed |= BTRFS_BLOCK_GROUP_DUP;
2969 else if (fs_info->fs_devices->num_devices < 4)
2970 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2972 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2973 BTRFS_BLOCK_GROUP_RAID10);
2975 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2976 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2977 (bctl->data.target & ~allowed))) {
2978 printk(KERN_ERR "btrfs: unable to start balance with target "
2979 "data profile %llu\n",
2980 (unsigned long long)bctl->data.target);
2984 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2985 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2986 (bctl->meta.target & ~allowed))) {
2987 printk(KERN_ERR "btrfs: unable to start balance with target "
2988 "metadata profile %llu\n",
2989 (unsigned long long)bctl->meta.target);
2993 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2994 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2995 (bctl->sys.target & ~allowed))) {
2996 printk(KERN_ERR "btrfs: unable to start balance with target "
2997 "system profile %llu\n",
2998 (unsigned long long)bctl->sys.target);
3003 /* allow dup'ed data chunks only in mixed mode */
3004 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3005 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3006 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3011 /* allow to reduce meta or sys integrity only if force set */
3012 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3013 BTRFS_BLOCK_GROUP_RAID10;
3014 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3015 (fs_info->avail_system_alloc_bits & allowed) &&
3016 !(bctl->sys.target & allowed)) ||
3017 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3018 (fs_info->avail_metadata_alloc_bits & allowed) &&
3019 !(bctl->meta.target & allowed))) {
3020 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3021 printk(KERN_INFO "btrfs: force reducing metadata "
3024 printk(KERN_ERR "btrfs: balance will reduce metadata "
3025 "integrity, use force if you want this\n");
3031 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3032 int num_tolerated_disk_barrier_failures;
3033 u64 target = bctl->sys.target;
3035 num_tolerated_disk_barrier_failures =
3036 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3037 if (num_tolerated_disk_barrier_failures > 0 &&
3039 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3040 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3041 num_tolerated_disk_barrier_failures = 0;
3042 else if (num_tolerated_disk_barrier_failures > 1 &&
3044 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3045 num_tolerated_disk_barrier_failures = 1;
3047 fs_info->num_tolerated_disk_barrier_failures =
3048 num_tolerated_disk_barrier_failures;
3051 ret = insert_balance_item(fs_info->tree_root, bctl);
3052 if (ret && ret != -EEXIST)
3055 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3056 BUG_ON(ret == -EEXIST);
3057 set_balance_control(bctl);
3059 BUG_ON(ret != -EEXIST);
3060 spin_lock(&fs_info->balance_lock);
3061 update_balance_args(bctl);
3062 spin_unlock(&fs_info->balance_lock);
3065 atomic_inc(&fs_info->balance_running);
3066 mutex_unlock(&fs_info->balance_mutex);
3068 ret = __btrfs_balance(fs_info);
3070 mutex_lock(&fs_info->balance_mutex);
3071 atomic_dec(&fs_info->balance_running);
3074 memset(bargs, 0, sizeof(*bargs));
3075 update_ioctl_balance_args(fs_info, 0, bargs);
3078 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3079 balance_need_close(fs_info)) {
3080 __cancel_balance(fs_info);
3083 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3084 fs_info->num_tolerated_disk_barrier_failures =
3085 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3088 wake_up(&fs_info->balance_wait_q);
3092 if (bctl->flags & BTRFS_BALANCE_RESUME)
3093 __cancel_balance(fs_info);
3099 static int balance_kthread(void *data)
3101 struct btrfs_fs_info *fs_info = data;
3104 mutex_lock(&fs_info->volume_mutex);
3105 mutex_lock(&fs_info->balance_mutex);
3107 if (fs_info->balance_ctl) {
3108 printk(KERN_INFO "btrfs: continuing balance\n");
3109 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3112 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3113 mutex_unlock(&fs_info->balance_mutex);
3114 mutex_unlock(&fs_info->volume_mutex);
3119 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3121 struct task_struct *tsk;
3123 spin_lock(&fs_info->balance_lock);
3124 if (!fs_info->balance_ctl) {
3125 spin_unlock(&fs_info->balance_lock);
3128 spin_unlock(&fs_info->balance_lock);
3130 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3131 printk(KERN_INFO "btrfs: force skipping balance\n");
3135 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3136 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3138 return PTR_ERR(tsk);
3143 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3145 struct btrfs_balance_control *bctl;
3146 struct btrfs_balance_item *item;
3147 struct btrfs_disk_balance_args disk_bargs;
3148 struct btrfs_path *path;
3149 struct extent_buffer *leaf;
3150 struct btrfs_key key;
3153 path = btrfs_alloc_path();
3157 key.objectid = BTRFS_BALANCE_OBJECTID;
3158 key.type = BTRFS_BALANCE_ITEM_KEY;
3161 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3164 if (ret > 0) { /* ret = -ENOENT; */
3169 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3175 leaf = path->nodes[0];
3176 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3178 bctl->fs_info = fs_info;
3179 bctl->flags = btrfs_balance_flags(leaf, item);
3180 bctl->flags |= BTRFS_BALANCE_RESUME;
3182 btrfs_balance_data(leaf, item, &disk_bargs);
3183 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3184 btrfs_balance_meta(leaf, item, &disk_bargs);
3185 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3186 btrfs_balance_sys(leaf, item, &disk_bargs);
3187 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3189 mutex_lock(&fs_info->volume_mutex);
3190 mutex_lock(&fs_info->balance_mutex);
3192 set_balance_control(bctl);
3194 mutex_unlock(&fs_info->balance_mutex);
3195 mutex_unlock(&fs_info->volume_mutex);
3197 btrfs_free_path(path);
3201 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3205 mutex_lock(&fs_info->balance_mutex);
3206 if (!fs_info->balance_ctl) {
3207 mutex_unlock(&fs_info->balance_mutex);
3211 if (atomic_read(&fs_info->balance_running)) {
3212 atomic_inc(&fs_info->balance_pause_req);
3213 mutex_unlock(&fs_info->balance_mutex);
3215 wait_event(fs_info->balance_wait_q,
3216 atomic_read(&fs_info->balance_running) == 0);
3218 mutex_lock(&fs_info->balance_mutex);
3219 /* we are good with balance_ctl ripped off from under us */
3220 BUG_ON(atomic_read(&fs_info->balance_running));
3221 atomic_dec(&fs_info->balance_pause_req);
3226 mutex_unlock(&fs_info->balance_mutex);
3230 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3232 mutex_lock(&fs_info->balance_mutex);
3233 if (!fs_info->balance_ctl) {
3234 mutex_unlock(&fs_info->balance_mutex);
3238 atomic_inc(&fs_info->balance_cancel_req);
3240 * if we are running just wait and return, balance item is
3241 * deleted in btrfs_balance in this case
3243 if (atomic_read(&fs_info->balance_running)) {
3244 mutex_unlock(&fs_info->balance_mutex);
3245 wait_event(fs_info->balance_wait_q,
3246 atomic_read(&fs_info->balance_running) == 0);
3247 mutex_lock(&fs_info->balance_mutex);
3249 /* __cancel_balance needs volume_mutex */
3250 mutex_unlock(&fs_info->balance_mutex);
3251 mutex_lock(&fs_info->volume_mutex);
3252 mutex_lock(&fs_info->balance_mutex);
3254 if (fs_info->balance_ctl)
3255 __cancel_balance(fs_info);
3257 mutex_unlock(&fs_info->volume_mutex);
3260 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3261 atomic_dec(&fs_info->balance_cancel_req);
3262 mutex_unlock(&fs_info->balance_mutex);
3267 * shrinking a device means finding all of the device extents past
3268 * the new size, and then following the back refs to the chunks.
3269 * The chunk relocation code actually frees the device extent
3271 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3273 struct btrfs_trans_handle *trans;
3274 struct btrfs_root *root = device->dev_root;
3275 struct btrfs_dev_extent *dev_extent = NULL;
3276 struct btrfs_path *path;
3284 bool retried = false;
3285 struct extent_buffer *l;
3286 struct btrfs_key key;
3287 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3288 u64 old_total = btrfs_super_total_bytes(super_copy);
3289 u64 old_size = device->total_bytes;
3290 u64 diff = device->total_bytes - new_size;
3292 if (device->is_tgtdev_for_dev_replace)
3295 path = btrfs_alloc_path();
3303 device->total_bytes = new_size;
3304 if (device->writeable) {
3305 device->fs_devices->total_rw_bytes -= diff;
3306 spin_lock(&root->fs_info->free_chunk_lock);
3307 root->fs_info->free_chunk_space -= diff;
3308 spin_unlock(&root->fs_info->free_chunk_lock);
3310 unlock_chunks(root);
3313 key.objectid = device->devid;
3314 key.offset = (u64)-1;
3315 key.type = BTRFS_DEV_EXTENT_KEY;
3318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3322 ret = btrfs_previous_item(root, path, 0, key.type);
3327 btrfs_release_path(path);
3332 slot = path->slots[0];
3333 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3335 if (key.objectid != device->devid) {
3336 btrfs_release_path(path);
3340 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3341 length = btrfs_dev_extent_length(l, dev_extent);
3343 if (key.offset + length <= new_size) {
3344 btrfs_release_path(path);
3348 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3349 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3350 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3351 btrfs_release_path(path);
3353 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3355 if (ret && ret != -ENOSPC)
3359 } while (key.offset-- > 0);
3361 if (failed && !retried) {
3365 } else if (failed && retried) {
3369 device->total_bytes = old_size;
3370 if (device->writeable)
3371 device->fs_devices->total_rw_bytes += diff;
3372 spin_lock(&root->fs_info->free_chunk_lock);
3373 root->fs_info->free_chunk_space += diff;
3374 spin_unlock(&root->fs_info->free_chunk_lock);
3375 unlock_chunks(root);
3379 /* Shrinking succeeded, else we would be at "done". */
3380 trans = btrfs_start_transaction(root, 0);
3381 if (IS_ERR(trans)) {
3382 ret = PTR_ERR(trans);
3388 device->disk_total_bytes = new_size;
3389 /* Now btrfs_update_device() will change the on-disk size. */
3390 ret = btrfs_update_device(trans, device);
3392 unlock_chunks(root);
3393 btrfs_end_transaction(trans, root);
3396 WARN_ON(diff > old_total);
3397 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3398 unlock_chunks(root);
3399 btrfs_end_transaction(trans, root);
3401 btrfs_free_path(path);
3405 static int btrfs_add_system_chunk(struct btrfs_root *root,
3406 struct btrfs_key *key,
3407 struct btrfs_chunk *chunk, int item_size)
3409 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3410 struct btrfs_disk_key disk_key;
3414 array_size = btrfs_super_sys_array_size(super_copy);
3415 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3418 ptr = super_copy->sys_chunk_array + array_size;
3419 btrfs_cpu_key_to_disk(&disk_key, key);
3420 memcpy(ptr, &disk_key, sizeof(disk_key));
3421 ptr += sizeof(disk_key);
3422 memcpy(ptr, chunk, item_size);
3423 item_size += sizeof(disk_key);
3424 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3429 * sort the devices in descending order by max_avail, total_avail
3431 static int btrfs_cmp_device_info(const void *a, const void *b)
3433 const struct btrfs_device_info *di_a = a;
3434 const struct btrfs_device_info *di_b = b;
3436 if (di_a->max_avail > di_b->max_avail)
3438 if (di_a->max_avail < di_b->max_avail)
3440 if (di_a->total_avail > di_b->total_avail)
3442 if (di_a->total_avail < di_b->total_avail)
3447 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3448 struct btrfs_root *extent_root,
3449 struct map_lookup **map_ret,
3450 u64 *num_bytes_out, u64 *stripe_size_out,
3451 u64 start, u64 type)
3453 struct btrfs_fs_info *info = extent_root->fs_info;
3454 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3455 struct list_head *cur;
3456 struct map_lookup *map = NULL;
3457 struct extent_map_tree *em_tree;
3458 struct extent_map *em;
3459 struct btrfs_device_info *devices_info = NULL;
3461 int num_stripes; /* total number of stripes to allocate */
3462 int sub_stripes; /* sub_stripes info for map */
3463 int dev_stripes; /* stripes per dev */
3464 int devs_max; /* max devs to use */
3465 int devs_min; /* min devs needed */
3466 int devs_increment; /* ndevs has to be a multiple of this */
3467 int ncopies; /* how many copies to data has */
3469 u64 max_stripe_size;
3477 BUG_ON(!alloc_profile_is_valid(type, 0));
3479 if (list_empty(&fs_devices->alloc_list))
3486 devs_max = 0; /* 0 == as many as possible */
3490 * define the properties of each RAID type.
3491 * FIXME: move this to a global table and use it in all RAID
3494 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3498 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3500 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3505 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3514 if (type & BTRFS_BLOCK_GROUP_DATA) {
3515 max_stripe_size = 1024 * 1024 * 1024;
3516 max_chunk_size = 10 * max_stripe_size;
3517 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
3518 /* for larger filesystems, use larger metadata chunks */
3519 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3520 max_stripe_size = 1024 * 1024 * 1024;
3522 max_stripe_size = 256 * 1024 * 1024;
3523 max_chunk_size = max_stripe_size;
3524 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
3525 max_stripe_size = 32 * 1024 * 1024;
3526 max_chunk_size = 2 * max_stripe_size;
3528 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3533 /* we don't want a chunk larger than 10% of writeable space */
3534 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3537 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3542 cur = fs_devices->alloc_list.next;
3545 * in the first pass through the devices list, we gather information
3546 * about the available holes on each device.
3549 while (cur != &fs_devices->alloc_list) {
3550 struct btrfs_device *device;
3554 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3558 if (!device->writeable) {
3560 "btrfs: read-only device in alloc_list\n");
3564 if (!device->in_fs_metadata ||
3565 device->is_tgtdev_for_dev_replace)
3568 if (device->total_bytes > device->bytes_used)
3569 total_avail = device->total_bytes - device->bytes_used;
3573 /* If there is no space on this device, skip it. */
3574 if (total_avail == 0)
3577 ret = find_free_dev_extent(device,
3578 max_stripe_size * dev_stripes,
3579 &dev_offset, &max_avail);
3580 if (ret && ret != -ENOSPC)
3584 max_avail = max_stripe_size * dev_stripes;
3586 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3589 devices_info[ndevs].dev_offset = dev_offset;
3590 devices_info[ndevs].max_avail = max_avail;
3591 devices_info[ndevs].total_avail = total_avail;
3592 devices_info[ndevs].dev = device;
3597 * now sort the devices by hole size / available space
3599 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3600 btrfs_cmp_device_info, NULL);
3602 /* round down to number of usable stripes */
3603 ndevs -= ndevs % devs_increment;
3605 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3610 if (devs_max && ndevs > devs_max)
3613 * the primary goal is to maximize the number of stripes, so use as many
3614 * devices as possible, even if the stripes are not maximum sized.
3616 stripe_size = devices_info[ndevs-1].max_avail;
3617 num_stripes = ndevs * dev_stripes;
3619 if (stripe_size * ndevs > max_chunk_size * ncopies) {
3620 stripe_size = max_chunk_size * ncopies;
3621 do_div(stripe_size, ndevs);
3624 do_div(stripe_size, dev_stripes);
3626 /* align to BTRFS_STRIPE_LEN */
3627 do_div(stripe_size, BTRFS_STRIPE_LEN);
3628 stripe_size *= BTRFS_STRIPE_LEN;
3630 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3635 map->num_stripes = num_stripes;
3637 for (i = 0; i < ndevs; ++i) {
3638 for (j = 0; j < dev_stripes; ++j) {
3639 int s = i * dev_stripes + j;
3640 map->stripes[s].dev = devices_info[i].dev;
3641 map->stripes[s].physical = devices_info[i].dev_offset +
3645 map->sector_size = extent_root->sectorsize;
3646 map->stripe_len = BTRFS_STRIPE_LEN;
3647 map->io_align = BTRFS_STRIPE_LEN;
3648 map->io_width = BTRFS_STRIPE_LEN;
3650 map->sub_stripes = sub_stripes;
3653 num_bytes = stripe_size * (num_stripes / ncopies);
3655 *stripe_size_out = stripe_size;
3656 *num_bytes_out = num_bytes;
3658 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
3660 em = alloc_extent_map();
3665 em->bdev = (struct block_device *)map;
3667 em->len = num_bytes;
3668 em->block_start = 0;
3669 em->block_len = em->len;
3671 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
3672 write_lock(&em_tree->lock);
3673 ret = add_extent_mapping(em_tree, em);
3674 write_unlock(&em_tree->lock);
3675 free_extent_map(em);
3679 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3680 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3685 for (i = 0; i < map->num_stripes; ++i) {
3686 struct btrfs_device *device;
3689 device = map->stripes[i].dev;
3690 dev_offset = map->stripes[i].physical;
3692 ret = btrfs_alloc_dev_extent(trans, device,
3693 info->chunk_root->root_key.objectid,
3694 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3695 start, dev_offset, stripe_size);
3697 btrfs_abort_transaction(trans, extent_root, ret);
3702 kfree(devices_info);
3707 kfree(devices_info);
3711 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3712 struct btrfs_root *extent_root,
3713 struct map_lookup *map, u64 chunk_offset,
3714 u64 chunk_size, u64 stripe_size)
3717 struct btrfs_key key;
3718 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3719 struct btrfs_device *device;
3720 struct btrfs_chunk *chunk;
3721 struct btrfs_stripe *stripe;
3722 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3726 chunk = kzalloc(item_size, GFP_NOFS);
3731 while (index < map->num_stripes) {
3732 device = map->stripes[index].dev;
3733 device->bytes_used += stripe_size;
3734 ret = btrfs_update_device(trans, device);
3740 spin_lock(&extent_root->fs_info->free_chunk_lock);
3741 extent_root->fs_info->free_chunk_space -= (stripe_size *
3743 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3746 stripe = &chunk->stripe;
3747 while (index < map->num_stripes) {
3748 device = map->stripes[index].dev;
3749 dev_offset = map->stripes[index].physical;
3751 btrfs_set_stack_stripe_devid(stripe, device->devid);
3752 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3753 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3758 btrfs_set_stack_chunk_length(chunk, chunk_size);
3759 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3760 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3761 btrfs_set_stack_chunk_type(chunk, map->type);
3762 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3763 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3764 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3765 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3766 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3768 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3769 key.type = BTRFS_CHUNK_ITEM_KEY;
3770 key.offset = chunk_offset;
3772 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3774 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3776 * TODO: Cleanup of inserted chunk root in case of
3779 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
3789 * Chunk allocation falls into two parts. The first part does works
3790 * that make the new allocated chunk useable, but not do any operation
3791 * that modifies the chunk tree. The second part does the works that
3792 * require modifying the chunk tree. This division is important for the
3793 * bootstrap process of adding storage to a seed btrfs.
3795 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3796 struct btrfs_root *extent_root, u64 type)
3801 struct map_lookup *map;
3802 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3805 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3810 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3811 &stripe_size, chunk_offset, type);
3815 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3816 chunk_size, stripe_size);
3822 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
3823 struct btrfs_root *root,
3824 struct btrfs_device *device)
3827 u64 sys_chunk_offset;
3831 u64 sys_stripe_size;
3833 struct map_lookup *map;
3834 struct map_lookup *sys_map;
3835 struct btrfs_fs_info *fs_info = root->fs_info;
3836 struct btrfs_root *extent_root = fs_info->extent_root;
3839 ret = find_next_chunk(fs_info->chunk_root,
3840 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
3844 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
3845 fs_info->avail_metadata_alloc_bits;
3846 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3848 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3849 &stripe_size, chunk_offset, alloc_profile);
3853 sys_chunk_offset = chunk_offset + chunk_size;
3855 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
3856 fs_info->avail_system_alloc_bits;
3857 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3859 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3860 &sys_chunk_size, &sys_stripe_size,
3861 sys_chunk_offset, alloc_profile);
3863 btrfs_abort_transaction(trans, root, ret);
3867 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3869 btrfs_abort_transaction(trans, root, ret);
3874 * Modifying chunk tree needs allocating new blocks from both
3875 * system block group and metadata block group. So we only can
3876 * do operations require modifying the chunk tree after both
3877 * block groups were created.
3879 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3880 chunk_size, stripe_size);
3882 btrfs_abort_transaction(trans, root, ret);
3886 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3887 sys_chunk_offset, sys_chunk_size,
3890 btrfs_abort_transaction(trans, root, ret);
3897 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3899 struct extent_map *em;
3900 struct map_lookup *map;
3901 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3905 read_lock(&map_tree->map_tree.lock);
3906 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3907 read_unlock(&map_tree->map_tree.lock);
3911 if (btrfs_test_opt(root, DEGRADED)) {
3912 free_extent_map(em);
3916 map = (struct map_lookup *)em->bdev;
3917 for (i = 0; i < map->num_stripes; i++) {
3918 if (!map->stripes[i].dev->writeable) {
3923 free_extent_map(em);
3927 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3929 extent_map_tree_init(&tree->map_tree);
3932 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3934 struct extent_map *em;
3937 write_lock(&tree->map_tree.lock);
3938 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3940 remove_extent_mapping(&tree->map_tree, em);
3941 write_unlock(&tree->map_tree.lock);
3946 free_extent_map(em);
3947 /* once for the tree */
3948 free_extent_map(em);
3952 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
3954 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
3955 struct extent_map *em;
3956 struct map_lookup *map;
3957 struct extent_map_tree *em_tree = &map_tree->map_tree;
3960 read_lock(&em_tree->lock);
3961 em = lookup_extent_mapping(em_tree, logical, len);
3962 read_unlock(&em_tree->lock);
3965 BUG_ON(em->start > logical || em->start + em->len < logical);
3966 map = (struct map_lookup *)em->bdev;
3967 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3968 ret = map->num_stripes;
3969 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3970 ret = map->sub_stripes;
3973 free_extent_map(em);
3977 static int find_live_mirror(struct map_lookup *map, int first, int num,
3981 if (map->stripes[optimal].dev->bdev)
3983 for (i = first; i < first + num; i++) {
3984 if (map->stripes[i].dev->bdev)
3987 /* we couldn't find one that doesn't fail. Just return something
3988 * and the io error handling code will clean up eventually
3993 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
3994 u64 logical, u64 *length,
3995 struct btrfs_bio **bbio_ret,
3998 struct extent_map *em;
3999 struct map_lookup *map;
4000 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4001 struct extent_map_tree *em_tree = &map_tree->map_tree;
4004 u64 stripe_end_offset;
4013 struct btrfs_bio *bbio = NULL;
4015 read_lock(&em_tree->lock);
4016 em = lookup_extent_mapping(em_tree, logical, *length);
4017 read_unlock(&em_tree->lock);
4020 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
4021 (unsigned long long)logical,
4022 (unsigned long long)*length);
4026 BUG_ON(em->start > logical || em->start + em->len < logical);
4027 map = (struct map_lookup *)em->bdev;
4028 offset = logical - em->start;
4030 if (mirror_num > map->num_stripes)
4035 * stripe_nr counts the total number of stripes we have to stride
4036 * to get to this block
4038 do_div(stripe_nr, map->stripe_len);
4040 stripe_offset = stripe_nr * map->stripe_len;
4041 BUG_ON(offset < stripe_offset);
4043 /* stripe_offset is the offset of this block in its stripe*/
4044 stripe_offset = offset - stripe_offset;
4046 if (rw & REQ_DISCARD)
4047 *length = min_t(u64, em->len - offset, *length);
4048 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4049 /* we limit the length of each bio to what fits in a stripe */
4050 *length = min_t(u64, em->len - offset,
4051 map->stripe_len - stripe_offset);
4053 *length = em->len - offset;
4061 stripe_nr_orig = stripe_nr;
4062 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
4063 (~(map->stripe_len - 1));
4064 do_div(stripe_nr_end, map->stripe_len);
4065 stripe_end_offset = stripe_nr_end * map->stripe_len -
4067 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4068 if (rw & REQ_DISCARD)
4069 num_stripes = min_t(u64, map->num_stripes,
4070 stripe_nr_end - stripe_nr_orig);
4071 stripe_index = do_div(stripe_nr, map->num_stripes);
4072 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
4073 if (rw & (REQ_WRITE | REQ_DISCARD))
4074 num_stripes = map->num_stripes;
4075 else if (mirror_num)
4076 stripe_index = mirror_num - 1;
4078 stripe_index = find_live_mirror(map, 0,
4080 current->pid % map->num_stripes);
4081 mirror_num = stripe_index + 1;
4084 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
4085 if (rw & (REQ_WRITE | REQ_DISCARD)) {
4086 num_stripes = map->num_stripes;
4087 } else if (mirror_num) {
4088 stripe_index = mirror_num - 1;
4093 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4094 int factor = map->num_stripes / map->sub_stripes;
4096 stripe_index = do_div(stripe_nr, factor);
4097 stripe_index *= map->sub_stripes;
4100 num_stripes = map->sub_stripes;
4101 else if (rw & REQ_DISCARD)
4102 num_stripes = min_t(u64, map->sub_stripes *
4103 (stripe_nr_end - stripe_nr_orig),
4105 else if (mirror_num)
4106 stripe_index += mirror_num - 1;
4108 int old_stripe_index = stripe_index;
4109 stripe_index = find_live_mirror(map, stripe_index,
4110 map->sub_stripes, stripe_index +
4111 current->pid % map->sub_stripes);
4112 mirror_num = stripe_index - old_stripe_index + 1;
4116 * after this do_div call, stripe_nr is the number of stripes
4117 * on this device we have to walk to find the data, and
4118 * stripe_index is the number of our device in the stripe array
4120 stripe_index = do_div(stripe_nr, map->num_stripes);
4121 mirror_num = stripe_index + 1;
4123 BUG_ON(stripe_index >= map->num_stripes);
4125 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
4130 atomic_set(&bbio->error, 0);
4132 if (rw & REQ_DISCARD) {
4134 int sub_stripes = 0;
4135 u64 stripes_per_dev = 0;
4136 u32 remaining_stripes = 0;
4137 u32 last_stripe = 0;
4140 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4141 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4144 sub_stripes = map->sub_stripes;
4146 factor = map->num_stripes / sub_stripes;
4147 stripes_per_dev = div_u64_rem(stripe_nr_end -
4150 &remaining_stripes);
4151 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4152 last_stripe *= sub_stripes;
4155 for (i = 0; i < num_stripes; i++) {
4156 bbio->stripes[i].physical =
4157 map->stripes[stripe_index].physical +
4158 stripe_offset + stripe_nr * map->stripe_len;
4159 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
4161 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4162 BTRFS_BLOCK_GROUP_RAID10)) {
4163 bbio->stripes[i].length = stripes_per_dev *
4166 if (i / sub_stripes < remaining_stripes)
4167 bbio->stripes[i].length +=
4171 * Special for the first stripe and
4174 * |-------|...|-------|
4178 if (i < sub_stripes)
4179 bbio->stripes[i].length -=
4182 if (stripe_index >= last_stripe &&
4183 stripe_index <= (last_stripe +
4185 bbio->stripes[i].length -=
4188 if (i == sub_stripes - 1)
4191 bbio->stripes[i].length = *length;
4194 if (stripe_index == map->num_stripes) {
4195 /* This could only happen for RAID0/10 */
4201 for (i = 0; i < num_stripes; i++) {
4202 bbio->stripes[i].physical =
4203 map->stripes[stripe_index].physical +
4205 stripe_nr * map->stripe_len;
4206 bbio->stripes[i].dev =
4207 map->stripes[stripe_index].dev;
4212 if (rw & REQ_WRITE) {
4213 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4214 BTRFS_BLOCK_GROUP_RAID10 |
4215 BTRFS_BLOCK_GROUP_DUP)) {
4221 bbio->num_stripes = num_stripes;
4222 bbio->max_errors = max_errors;
4223 bbio->mirror_num = mirror_num;
4225 free_extent_map(em);
4229 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4230 u64 logical, u64 *length,
4231 struct btrfs_bio **bbio_ret, int mirror_num)
4233 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
4237 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4238 u64 chunk_start, u64 physical, u64 devid,
4239 u64 **logical, int *naddrs, int *stripe_len)
4241 struct extent_map_tree *em_tree = &map_tree->map_tree;
4242 struct extent_map *em;
4243 struct map_lookup *map;
4250 read_lock(&em_tree->lock);
4251 em = lookup_extent_mapping(em_tree, chunk_start, 1);
4252 read_unlock(&em_tree->lock);
4254 BUG_ON(!em || em->start != chunk_start);
4255 map = (struct map_lookup *)em->bdev;
4258 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4259 do_div(length, map->num_stripes / map->sub_stripes);
4260 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4261 do_div(length, map->num_stripes);
4263 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
4264 BUG_ON(!buf); /* -ENOMEM */
4266 for (i = 0; i < map->num_stripes; i++) {
4267 if (devid && map->stripes[i].dev->devid != devid)
4269 if (map->stripes[i].physical > physical ||
4270 map->stripes[i].physical + length <= physical)
4273 stripe_nr = physical - map->stripes[i].physical;
4274 do_div(stripe_nr, map->stripe_len);
4276 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4277 stripe_nr = stripe_nr * map->num_stripes + i;
4278 do_div(stripe_nr, map->sub_stripes);
4279 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4280 stripe_nr = stripe_nr * map->num_stripes + i;
4282 bytenr = chunk_start + stripe_nr * map->stripe_len;
4283 WARN_ON(nr >= map->num_stripes);
4284 for (j = 0; j < nr; j++) {
4285 if (buf[j] == bytenr)
4289 WARN_ON(nr >= map->num_stripes);
4296 *stripe_len = map->stripe_len;
4298 free_extent_map(em);
4302 static void *merge_stripe_index_into_bio_private(void *bi_private,
4303 unsigned int stripe_index)
4306 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4308 * The alternative solution (instead of stealing bits from the
4309 * pointer) would be to allocate an intermediate structure
4310 * that contains the old private pointer plus the stripe_index.
4312 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4313 BUG_ON(stripe_index > 3);
4314 return (void *)(((uintptr_t)bi_private) | stripe_index);
4317 static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4319 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4322 static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4324 return (unsigned int)((uintptr_t)bi_private) & 3;
4327 static void btrfs_end_bio(struct bio *bio, int err)
4329 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
4330 int is_orig_bio = 0;
4333 atomic_inc(&bbio->error);
4334 if (err == -EIO || err == -EREMOTEIO) {
4335 unsigned int stripe_index =
4336 extract_stripe_index_from_bio_private(
4338 struct btrfs_device *dev;
4340 BUG_ON(stripe_index >= bbio->num_stripes);
4341 dev = bbio->stripes[stripe_index].dev;
4343 if (bio->bi_rw & WRITE)
4344 btrfs_dev_stat_inc(dev,
4345 BTRFS_DEV_STAT_WRITE_ERRS);
4347 btrfs_dev_stat_inc(dev,
4348 BTRFS_DEV_STAT_READ_ERRS);
4349 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4350 btrfs_dev_stat_inc(dev,
4351 BTRFS_DEV_STAT_FLUSH_ERRS);
4352 btrfs_dev_stat_print_on_error(dev);
4357 if (bio == bbio->orig_bio)
4360 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4363 bio = bbio->orig_bio;
4365 bio->bi_private = bbio->private;
4366 bio->bi_end_io = bbio->end_io;
4367 bio->bi_bdev = (struct block_device *)
4368 (unsigned long)bbio->mirror_num;
4369 /* only send an error to the higher layers if it is
4370 * beyond the tolerance of the multi-bio
4372 if (atomic_read(&bbio->error) > bbio->max_errors) {
4376 * this bio is actually up to date, we didn't
4377 * go over the max number of errors
4379 set_bit(BIO_UPTODATE, &bio->bi_flags);
4384 bio_endio(bio, err);
4385 } else if (!is_orig_bio) {
4390 struct async_sched {
4393 struct btrfs_fs_info *info;
4394 struct btrfs_work work;
4398 * see run_scheduled_bios for a description of why bios are collected for
4401 * This will add one bio to the pending list for a device and make sure
4402 * the work struct is scheduled.
4404 static noinline void schedule_bio(struct btrfs_root *root,
4405 struct btrfs_device *device,
4406 int rw, struct bio *bio)
4408 int should_queue = 1;
4409 struct btrfs_pending_bios *pending_bios;
4411 /* don't bother with additional async steps for reads, right now */
4412 if (!(rw & REQ_WRITE)) {
4414 btrfsic_submit_bio(rw, bio);
4420 * nr_async_bios allows us to reliably return congestion to the
4421 * higher layers. Otherwise, the async bio makes it appear we have
4422 * made progress against dirty pages when we've really just put it
4423 * on a queue for later
4425 atomic_inc(&root->fs_info->nr_async_bios);
4426 WARN_ON(bio->bi_next);
4427 bio->bi_next = NULL;
4430 spin_lock(&device->io_lock);
4431 if (bio->bi_rw & REQ_SYNC)
4432 pending_bios = &device->pending_sync_bios;
4434 pending_bios = &device->pending_bios;
4436 if (pending_bios->tail)
4437 pending_bios->tail->bi_next = bio;
4439 pending_bios->tail = bio;
4440 if (!pending_bios->head)
4441 pending_bios->head = bio;
4442 if (device->running_pending)
4445 spin_unlock(&device->io_lock);
4448 btrfs_queue_worker(&root->fs_info->submit_workers,
4452 static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4455 struct bio_vec *prev;
4456 struct request_queue *q = bdev_get_queue(bdev);
4457 unsigned short max_sectors = queue_max_sectors(q);
4458 struct bvec_merge_data bvm = {
4460 .bi_sector = sector,
4461 .bi_rw = bio->bi_rw,
4464 if (bio->bi_vcnt == 0) {
4469 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4470 if ((bio->bi_size >> 9) > max_sectors)
4473 if (!q->merge_bvec_fn)
4476 bvm.bi_size = bio->bi_size - prev->bv_len;
4477 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4482 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4483 struct bio *bio, u64 physical, int dev_nr,
4486 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4488 bio->bi_private = bbio;
4489 bio->bi_private = merge_stripe_index_into_bio_private(
4490 bio->bi_private, (unsigned int)dev_nr);
4491 bio->bi_end_io = btrfs_end_bio;
4492 bio->bi_sector = physical >> 9;
4495 struct rcu_string *name;
4498 name = rcu_dereference(dev->name);
4499 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
4500 "(%s id %llu), size=%u\n", rw,
4501 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4502 name->str, dev->devid, bio->bi_size);
4506 bio->bi_bdev = dev->bdev;
4508 schedule_bio(root, dev, rw, bio);
4510 btrfsic_submit_bio(rw, bio);
4513 static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4514 struct bio *first_bio, struct btrfs_device *dev,
4515 int dev_nr, int rw, int async)
4517 struct bio_vec *bvec = first_bio->bi_io_vec;
4519 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4520 u64 physical = bbio->stripes[dev_nr].physical;
4523 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4527 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4528 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4529 bvec->bv_offset) < bvec->bv_len) {
4530 u64 len = bio->bi_size;
4532 atomic_inc(&bbio->stripes_pending);
4533 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4541 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4545 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4547 atomic_inc(&bbio->error);
4548 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4549 bio->bi_private = bbio->private;
4550 bio->bi_end_io = bbio->end_io;
4551 bio->bi_bdev = (struct block_device *)
4552 (unsigned long)bbio->mirror_num;
4553 bio->bi_sector = logical >> 9;
4555 bio_endio(bio, -EIO);
4559 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
4560 int mirror_num, int async_submit)
4562 struct btrfs_device *dev;
4563 struct bio *first_bio = bio;
4564 u64 logical = (u64)bio->bi_sector << 9;
4570 struct btrfs_bio *bbio = NULL;
4572 length = bio->bi_size;
4573 map_length = length;
4575 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
4580 total_devs = bbio->num_stripes;
4581 if (map_length < length) {
4582 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
4583 "len %llu\n", (unsigned long long)logical,
4584 (unsigned long long)length,
4585 (unsigned long long)map_length);
4589 bbio->orig_bio = first_bio;
4590 bbio->private = first_bio->bi_private;
4591 bbio->end_io = first_bio->bi_end_io;
4592 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
4594 while (dev_nr < total_devs) {
4595 dev = bbio->stripes[dev_nr].dev;
4596 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4597 bbio_error(bbio, first_bio, logical);
4603 * Check and see if we're ok with this bio based on it's size
4604 * and offset with the given device.
4606 if (!bio_size_ok(dev->bdev, first_bio,
4607 bbio->stripes[dev_nr].physical >> 9)) {
4608 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4609 dev_nr, rw, async_submit);
4615 if (dev_nr < total_devs - 1) {
4616 bio = bio_clone(first_bio, GFP_NOFS);
4617 BUG_ON(!bio); /* -ENOMEM */
4622 submit_stripe_bio(root, bbio, bio,
4623 bbio->stripes[dev_nr].physical, dev_nr, rw,
4630 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
4633 struct btrfs_device *device;
4634 struct btrfs_fs_devices *cur_devices;
4636 cur_devices = fs_info->fs_devices;
4637 while (cur_devices) {
4639 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4640 device = __find_device(&cur_devices->devices,
4645 cur_devices = cur_devices->seed;
4650 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4651 u64 devid, u8 *dev_uuid)
4653 struct btrfs_device *device;
4654 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4656 device = kzalloc(sizeof(*device), GFP_NOFS);
4659 list_add(&device->dev_list,
4660 &fs_devices->devices);
4661 device->dev_root = root->fs_info->dev_root;
4662 device->devid = devid;
4663 device->work.func = pending_bios_fn;
4664 device->fs_devices = fs_devices;
4665 device->missing = 1;
4666 fs_devices->num_devices++;
4667 fs_devices->missing_devices++;
4668 spin_lock_init(&device->io_lock);
4669 INIT_LIST_HEAD(&device->dev_alloc_list);
4670 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4674 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4675 struct extent_buffer *leaf,
4676 struct btrfs_chunk *chunk)
4678 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4679 struct map_lookup *map;
4680 struct extent_map *em;
4684 u8 uuid[BTRFS_UUID_SIZE];
4689 logical = key->offset;
4690 length = btrfs_chunk_length(leaf, chunk);
4692 read_lock(&map_tree->map_tree.lock);
4693 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
4694 read_unlock(&map_tree->map_tree.lock);
4696 /* already mapped? */
4697 if (em && em->start <= logical && em->start + em->len > logical) {
4698 free_extent_map(em);
4701 free_extent_map(em);
4704 em = alloc_extent_map();
4707 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4708 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4710 free_extent_map(em);
4714 em->bdev = (struct block_device *)map;
4715 em->start = logical;
4717 em->block_start = 0;
4718 em->block_len = em->len;
4720 map->num_stripes = num_stripes;
4721 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4722 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4723 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4724 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4725 map->type = btrfs_chunk_type(leaf, chunk);
4726 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
4727 for (i = 0; i < num_stripes; i++) {
4728 map->stripes[i].physical =
4729 btrfs_stripe_offset_nr(leaf, chunk, i);
4730 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
4731 read_extent_buffer(leaf, uuid, (unsigned long)
4732 btrfs_stripe_dev_uuid_nr(chunk, i),
4734 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
4736 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
4738 free_extent_map(em);
4741 if (!map->stripes[i].dev) {
4742 map->stripes[i].dev =
4743 add_missing_dev(root, devid, uuid);
4744 if (!map->stripes[i].dev) {
4746 free_extent_map(em);
4750 map->stripes[i].dev->in_fs_metadata = 1;
4753 write_lock(&map_tree->map_tree.lock);
4754 ret = add_extent_mapping(&map_tree->map_tree, em);
4755 write_unlock(&map_tree->map_tree.lock);
4756 BUG_ON(ret); /* Tree corruption */
4757 free_extent_map(em);
4762 static void fill_device_from_item(struct extent_buffer *leaf,
4763 struct btrfs_dev_item *dev_item,
4764 struct btrfs_device *device)
4768 device->devid = btrfs_device_id(leaf, dev_item);
4769 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4770 device->total_bytes = device->disk_total_bytes;
4771 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4772 device->type = btrfs_device_type(leaf, dev_item);
4773 device->io_align = btrfs_device_io_align(leaf, dev_item);
4774 device->io_width = btrfs_device_io_width(leaf, dev_item);
4775 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
4776 device->is_tgtdev_for_dev_replace = 0;
4778 ptr = (unsigned long)btrfs_device_uuid(dev_item);
4779 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
4782 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4784 struct btrfs_fs_devices *fs_devices;
4787 BUG_ON(!mutex_is_locked(&uuid_mutex));
4789 fs_devices = root->fs_info->fs_devices->seed;
4790 while (fs_devices) {
4791 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4795 fs_devices = fs_devices->seed;
4798 fs_devices = find_fsid(fsid);
4804 fs_devices = clone_fs_devices(fs_devices);
4805 if (IS_ERR(fs_devices)) {
4806 ret = PTR_ERR(fs_devices);
4810 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
4811 root->fs_info->bdev_holder);
4813 free_fs_devices(fs_devices);
4817 if (!fs_devices->seeding) {
4818 __btrfs_close_devices(fs_devices);
4819 free_fs_devices(fs_devices);
4824 fs_devices->seed = root->fs_info->fs_devices->seed;
4825 root->fs_info->fs_devices->seed = fs_devices;
4830 static int read_one_dev(struct btrfs_root *root,
4831 struct extent_buffer *leaf,
4832 struct btrfs_dev_item *dev_item)
4834 struct btrfs_device *device;
4837 u8 fs_uuid[BTRFS_UUID_SIZE];
4838 u8 dev_uuid[BTRFS_UUID_SIZE];
4840 devid = btrfs_device_id(leaf, dev_item);
4841 read_extent_buffer(leaf, dev_uuid,
4842 (unsigned long)btrfs_device_uuid(dev_item),
4844 read_extent_buffer(leaf, fs_uuid,
4845 (unsigned long)btrfs_device_fsid(dev_item),
4848 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4849 ret = open_seed_devices(root, fs_uuid);
4850 if (ret && !btrfs_test_opt(root, DEGRADED))
4854 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
4855 if (!device || !device->bdev) {
4856 if (!btrfs_test_opt(root, DEGRADED))
4860 printk(KERN_WARNING "warning devid %llu missing\n",
4861 (unsigned long long)devid);
4862 device = add_missing_dev(root, devid, dev_uuid);
4865 } else if (!device->missing) {
4867 * this happens when a device that was properly setup
4868 * in the device info lists suddenly goes bad.
4869 * device->bdev is NULL, and so we have to set
4870 * device->missing to one here
4872 root->fs_info->fs_devices->missing_devices++;
4873 device->missing = 1;
4877 if (device->fs_devices != root->fs_info->fs_devices) {
4878 BUG_ON(device->writeable);
4879 if (device->generation !=
4880 btrfs_device_generation(leaf, dev_item))
4884 fill_device_from_item(leaf, dev_item, device);
4885 device->dev_root = root->fs_info->dev_root;
4886 device->in_fs_metadata = 1;
4887 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
4888 device->fs_devices->total_rw_bytes += device->total_bytes;
4889 spin_lock(&root->fs_info->free_chunk_lock);
4890 root->fs_info->free_chunk_space += device->total_bytes -
4892 spin_unlock(&root->fs_info->free_chunk_lock);
4898 int btrfs_read_sys_array(struct btrfs_root *root)
4900 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4901 struct extent_buffer *sb;
4902 struct btrfs_disk_key *disk_key;
4903 struct btrfs_chunk *chunk;
4905 unsigned long sb_ptr;
4911 struct btrfs_key key;
4913 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
4914 BTRFS_SUPER_INFO_SIZE);
4917 btrfs_set_buffer_uptodate(sb);
4918 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
4920 * The sb extent buffer is artifical and just used to read the system array.
4921 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4922 * pages up-to-date when the page is larger: extent does not cover the
4923 * whole page and consequently check_page_uptodate does not find all
4924 * the page's extents up-to-date (the hole beyond sb),
4925 * write_extent_buffer then triggers a WARN_ON.
4927 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4928 * but sb spans only this function. Add an explicit SetPageUptodate call
4929 * to silence the warning eg. on PowerPC 64.
4931 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
4932 SetPageUptodate(sb->pages[0]);
4934 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
4935 array_size = btrfs_super_sys_array_size(super_copy);
4937 ptr = super_copy->sys_chunk_array;
4938 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4941 while (cur < array_size) {
4942 disk_key = (struct btrfs_disk_key *)ptr;
4943 btrfs_disk_key_to_cpu(&key, disk_key);
4945 len = sizeof(*disk_key); ptr += len;
4949 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4950 chunk = (struct btrfs_chunk *)sb_ptr;
4951 ret = read_one_chunk(root, &key, sb, chunk);
4954 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4955 len = btrfs_chunk_item_size(num_stripes);
4964 free_extent_buffer(sb);
4968 int btrfs_read_chunk_tree(struct btrfs_root *root)
4970 struct btrfs_path *path;
4971 struct extent_buffer *leaf;
4972 struct btrfs_key key;
4973 struct btrfs_key found_key;
4977 root = root->fs_info->chunk_root;
4979 path = btrfs_alloc_path();
4983 mutex_lock(&uuid_mutex);
4986 /* first we search for all of the device items, and then we
4987 * read in all of the chunk items. This way we can create chunk
4988 * mappings that reference all of the devices that are afound
4990 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4994 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4998 leaf = path->nodes[0];
4999 slot = path->slots[0];
5000 if (slot >= btrfs_header_nritems(leaf)) {
5001 ret = btrfs_next_leaf(root, path);
5008 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5009 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5010 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5012 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5013 struct btrfs_dev_item *dev_item;
5014 dev_item = btrfs_item_ptr(leaf, slot,
5015 struct btrfs_dev_item);
5016 ret = read_one_dev(root, leaf, dev_item);
5020 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5021 struct btrfs_chunk *chunk;
5022 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5023 ret = read_one_chunk(root, &found_key, leaf, chunk);
5029 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5031 btrfs_release_path(path);
5036 unlock_chunks(root);
5037 mutex_unlock(&uuid_mutex);
5039 btrfs_free_path(path);
5043 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5047 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5048 btrfs_dev_stat_reset(dev, i);
5051 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5053 struct btrfs_key key;
5054 struct btrfs_key found_key;
5055 struct btrfs_root *dev_root = fs_info->dev_root;
5056 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5057 struct extent_buffer *eb;
5060 struct btrfs_device *device;
5061 struct btrfs_path *path = NULL;
5064 path = btrfs_alloc_path();
5070 mutex_lock(&fs_devices->device_list_mutex);
5071 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5073 struct btrfs_dev_stats_item *ptr;
5076 key.type = BTRFS_DEV_STATS_KEY;
5077 key.offset = device->devid;
5078 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5080 __btrfs_reset_dev_stats(device);
5081 device->dev_stats_valid = 1;
5082 btrfs_release_path(path);
5085 slot = path->slots[0];
5086 eb = path->nodes[0];
5087 btrfs_item_key_to_cpu(eb, &found_key, slot);
5088 item_size = btrfs_item_size_nr(eb, slot);
5090 ptr = btrfs_item_ptr(eb, slot,
5091 struct btrfs_dev_stats_item);
5093 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5094 if (item_size >= (1 + i) * sizeof(__le64))
5095 btrfs_dev_stat_set(device, i,
5096 btrfs_dev_stats_value(eb, ptr, i));
5098 btrfs_dev_stat_reset(device, i);
5101 device->dev_stats_valid = 1;
5102 btrfs_dev_stat_print_on_load(device);
5103 btrfs_release_path(path);
5105 mutex_unlock(&fs_devices->device_list_mutex);
5108 btrfs_free_path(path);
5109 return ret < 0 ? ret : 0;
5112 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5113 struct btrfs_root *dev_root,
5114 struct btrfs_device *device)
5116 struct btrfs_path *path;
5117 struct btrfs_key key;
5118 struct extent_buffer *eb;
5119 struct btrfs_dev_stats_item *ptr;
5124 key.type = BTRFS_DEV_STATS_KEY;
5125 key.offset = device->devid;
5127 path = btrfs_alloc_path();
5129 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5131 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5132 ret, rcu_str_deref(device->name));
5137 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5138 /* need to delete old one and insert a new one */
5139 ret = btrfs_del_item(trans, dev_root, path);
5141 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5142 rcu_str_deref(device->name), ret);
5149 /* need to insert a new item */
5150 btrfs_release_path(path);
5151 ret = btrfs_insert_empty_item(trans, dev_root, path,
5152 &key, sizeof(*ptr));
5154 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5155 rcu_str_deref(device->name), ret);
5160 eb = path->nodes[0];
5161 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5162 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5163 btrfs_set_dev_stats_value(eb, ptr, i,
5164 btrfs_dev_stat_read(device, i));
5165 btrfs_mark_buffer_dirty(eb);
5168 btrfs_free_path(path);
5173 * called from commit_transaction. Writes all changed device stats to disk.
5175 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5176 struct btrfs_fs_info *fs_info)
5178 struct btrfs_root *dev_root = fs_info->dev_root;
5179 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5180 struct btrfs_device *device;
5183 mutex_lock(&fs_devices->device_list_mutex);
5184 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5185 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5188 ret = update_dev_stat_item(trans, dev_root, device);
5190 device->dev_stats_dirty = 0;
5192 mutex_unlock(&fs_devices->device_list_mutex);
5197 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5199 btrfs_dev_stat_inc(dev, index);
5200 btrfs_dev_stat_print_on_error(dev);
5203 void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5205 if (!dev->dev_stats_valid)
5207 printk_ratelimited_in_rcu(KERN_ERR
5208 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5209 rcu_str_deref(dev->name),
5210 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5211 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5212 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5213 btrfs_dev_stat_read(dev,
5214 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5215 btrfs_dev_stat_read(dev,
5216 BTRFS_DEV_STAT_GENERATION_ERRS));
5219 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5223 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5224 if (btrfs_dev_stat_read(dev, i) != 0)
5226 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5227 return; /* all values == 0, suppress message */
5229 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5230 rcu_str_deref(dev->name),
5231 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5232 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5233 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5234 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5235 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5238 int btrfs_get_dev_stats(struct btrfs_root *root,
5239 struct btrfs_ioctl_get_dev_stats *stats)
5241 struct btrfs_device *dev;
5242 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5245 mutex_lock(&fs_devices->device_list_mutex);
5246 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
5247 mutex_unlock(&fs_devices->device_list_mutex);
5251 "btrfs: get dev_stats failed, device not found\n");
5253 } else if (!dev->dev_stats_valid) {
5255 "btrfs: get dev_stats failed, not yet valid\n");
5257 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
5258 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5259 if (stats->nr_items > i)
5261 btrfs_dev_stat_read_and_reset(dev, i);
5263 btrfs_dev_stat_reset(dev, i);
5266 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5267 if (stats->nr_items > i)
5268 stats->values[i] = btrfs_dev_stat_read(dev, i);
5270 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5271 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5275 int btrfs_scratch_superblock(struct btrfs_device *device)
5277 struct buffer_head *bh;
5278 struct btrfs_super_block *disk_super;
5280 bh = btrfs_read_dev_super(device->bdev);
5283 disk_super = (struct btrfs_super_block *)bh->b_data;
5285 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5286 set_buffer_dirty(bh);
5287 sync_dirty_buffer(bh);