aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2011-05-23 06:30:52 -0400
committerChris Mason <chris.mason@oracle.com>2011-05-23 06:30:52 -0400
commit712673339a0d085358fd1cd3a6477cc7979bb69f (patch)
treeb2e268685e3cbba19a4adb2a47d853e4e461fcfc /fs/btrfs
parentaa2dfb372a2a647beedac163ce6f8b0fcbefac29 (diff)
parent8628764e1a5e1998a42b9713e9edea7753653d01 (diff)
Merge branch 'for-chris' of git://git.kernel.org/pub/scm/linux/kernel/git/arne/btrfs-unstable-arne into inode_numbers
Conflicts: fs/btrfs/Makefile fs/btrfs/ctree.h fs/btrfs/volumes.h Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/Makefile2
-rw-r--r--fs/btrfs/ctree.h42
-rw-r--r--fs/btrfs/disk-io.c12
-rw-r--r--fs/btrfs/file-item.c8
-rw-r--r--fs/btrfs/inode.c2
-rw-r--r--fs/btrfs/ioctl.c131
-rw-r--r--fs/btrfs/ioctl.h76
-rw-r--r--fs/btrfs/relocation.c2
-rw-r--r--fs/btrfs/scrub.c1368
-rw-r--r--fs/btrfs/transaction.c3
-rw-r--r--fs/btrfs/tree-log.c6
-rw-r--r--fs/btrfs/volumes.c4
-rw-r--r--fs/btrfs/volumes.h6
13 files changed, 1649 insertions, 13 deletions
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile
index a8411c22313d..9b72dcf1cd25 100644
--- a/fs/btrfs/Makefile
+++ b/fs/btrfs/Makefile
@@ -7,4 +7,4 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
7 extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \ 7 extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \
8 extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ 8 extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \
9 export.o tree-log.o acl.o free-space-cache.o zlib.o lzo.o \ 9 export.o tree-log.o acl.o free-space-cache.o zlib.o lzo.o \
10 compression.o delayed-ref.o relocation.o delayed-inode.o 10 compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index e7d40791ec9f..0d50c57caf5d 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -23,6 +23,7 @@
23#include <linux/mm.h> 23#include <linux/mm.h>
24#include <linux/highmem.h> 24#include <linux/highmem.h>
25#include <linux/fs.h> 25#include <linux/fs.h>
26#include <linux/rwsem.h>
26#include <linux/completion.h> 27#include <linux/completion.h>
27#include <linux/backing-dev.h> 28#include <linux/backing-dev.h>
28#include <linux/wait.h> 29#include <linux/wait.h>
@@ -33,6 +34,7 @@
33#include "extent_io.h" 34#include "extent_io.h"
34#include "extent_map.h" 35#include "extent_map.h"
35#include "async-thread.h" 36#include "async-thread.h"
37#include "ioctl.h"
36 38
37struct btrfs_trans_handle; 39struct btrfs_trans_handle;
38struct btrfs_transaction; 40struct btrfs_transaction;
@@ -193,7 +195,6 @@ struct btrfs_mapping_tree {
193 struct extent_map_tree map_tree; 195 struct extent_map_tree map_tree;
194}; 196};
195 197
196#define BTRFS_UUID_SIZE 16
197struct btrfs_dev_item { 198struct btrfs_dev_item {
198 /* the internal btrfs device id */ 199 /* the internal btrfs device id */
199 __le64 devid; 200 __le64 devid;
@@ -300,7 +301,6 @@ static inline unsigned long btrfs_chunk_item_size(int num_stripes)
300 sizeof(struct btrfs_stripe) * (num_stripes - 1); 301 sizeof(struct btrfs_stripe) * (num_stripes - 1);
301} 302}
302 303
303#define BTRFS_FSID_SIZE 16
304#define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0) 304#define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0)
305#define BTRFS_HEADER_FLAG_RELOC (1ULL << 1) 305#define BTRFS_HEADER_FLAG_RELOC (1ULL << 1)
306 306
@@ -516,6 +516,12 @@ struct btrfs_extent_item_v0 {
516/* use full backrefs for extent pointers in the block */ 516/* use full backrefs for extent pointers in the block */
517#define BTRFS_BLOCK_FLAG_FULL_BACKREF (1ULL << 8) 517#define BTRFS_BLOCK_FLAG_FULL_BACKREF (1ULL << 8)
518 518
519/*
520 * this flag is only used internally by scrub and may be changed at any time
521 * it is only declared here to avoid collisions
522 */
523#define BTRFS_EXTENT_FLAG_SUPER (1ULL << 48)
524
519struct btrfs_tree_block_info { 525struct btrfs_tree_block_info {
520 struct btrfs_disk_key key; 526 struct btrfs_disk_key key;
521 u8 level; 527 u8 level;
@@ -1083,6 +1089,17 @@ struct btrfs_fs_info {
1083 1089
1084 void *bdev_holder; 1090 void *bdev_holder;
1085 1091
1092 /* private scrub information */
1093 struct mutex scrub_lock;
1094 atomic_t scrubs_running;
1095 atomic_t scrub_pause_req;
1096 atomic_t scrubs_paused;
1097 atomic_t scrub_cancel_req;
1098 wait_queue_head_t scrub_pause_wait;
1099 struct rw_semaphore scrub_super_lock;
1100 int scrub_workers_refcnt;
1101 struct btrfs_workers scrub_workers;
1102
1086 /* filesystem state */ 1103 /* filesystem state */
1087 u64 fs_state; 1104 u64 fs_state;
1088 1105
@@ -2422,8 +2439,11 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
2422 struct btrfs_root *root, 2439 struct btrfs_root *root,
2423 struct btrfs_path *path, 2440 struct btrfs_path *path,
2424 u64 bytenr, int cow); 2441 u64 bytenr, int cow);
2425int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, 2442int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
2426 u64 end, struct list_head *list); 2443 struct btrfs_root *root, struct btrfs_path *path,
2444 u64 isize);
2445int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
2446 struct list_head *list, int search_commit);
2427/* inode.c */ 2447/* inode.c */
2428 2448
2429/* RHEL and EL kernels have a patch that renames PG_checked to FsMisc */ 2449/* RHEL and EL kernels have a patch that renames PG_checked to FsMisc */
@@ -2577,4 +2597,18 @@ void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans,
2577 u64 *bytes_to_reserve); 2597 u64 *bytes_to_reserve);
2578void btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, 2598void btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
2579 struct btrfs_pending_snapshot *pending); 2599 struct btrfs_pending_snapshot *pending);
2600
2601/* scrub.c */
2602int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
2603 struct btrfs_scrub_progress *progress, int readonly);
2604int btrfs_scrub_pause(struct btrfs_root *root);
2605int btrfs_scrub_pause_super(struct btrfs_root *root);
2606int btrfs_scrub_continue(struct btrfs_root *root);
2607int btrfs_scrub_continue_super(struct btrfs_root *root);
2608int btrfs_scrub_cancel(struct btrfs_root *root);
2609int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev);
2610int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid);
2611int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
2612 struct btrfs_scrub_progress *progress);
2613
2580#endif 2614#endif
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index a2eb3a3755db..29a2475b9b14 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1654,6 +1654,17 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1654 } 1654 }
1655 btrfs_init_delayed_root(fs_info->delayed_root); 1655 btrfs_init_delayed_root(fs_info->delayed_root);
1656 1656
1657 mutex_init(&fs_info->scrub_lock);
1658 atomic_set(&fs_info->scrubs_running, 0);
1659 atomic_set(&fs_info->scrub_pause_req, 0);
1660 atomic_set(&fs_info->scrubs_paused, 0);
1661 atomic_set(&fs_info->scrub_cancel_req, 0);
1662 init_waitqueue_head(&fs_info->scrub_pause_wait);
1663 init_rwsem(&fs_info->scrub_super_lock);
1664 fs_info->scrub_workers_refcnt = 0;
1665 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
1666 fs_info->thread_pool_size, &fs_info->generic_worker);
1667
1657 sb->s_blocksize = 4096; 1668 sb->s_blocksize = 4096;
1658 sb->s_blocksize_bits = blksize_bits(4096); 1669 sb->s_blocksize_bits = blksize_bits(4096);
1659 sb->s_bdi = &fs_info->bdi; 1670 sb->s_bdi = &fs_info->bdi;
@@ -2488,6 +2499,7 @@ int close_ctree(struct btrfs_root *root)
2488 fs_info->closing = 1; 2499 fs_info->closing = 1;
2489 smp_mb(); 2500 smp_mb();
2490 2501
2502 btrfs_scrub_cancel(root);
2491 btrfs_put_block_group_cache(fs_info); 2503 btrfs_put_block_group_cache(fs_info);
2492 2504
2493 /* 2505 /*
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 29e014984f60..b437cc790bfb 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -267,7 +267,7 @@ int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
267} 267}
268 268
269int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 269int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
270 struct list_head *list) 270 struct list_head *list, int search_commit)
271{ 271{
272 struct btrfs_key key; 272 struct btrfs_key key;
273 struct btrfs_path *path; 273 struct btrfs_path *path;
@@ -284,6 +284,12 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
284 path = btrfs_alloc_path(); 284 path = btrfs_alloc_path();
285 BUG_ON(!path); 285 BUG_ON(!path);
286 286
287 if (search_commit) {
288 path->skip_locking = 1;
289 path->reada = 2;
290 path->search_commit_root = 1;
291 }
292
287 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 293 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
288 key.offset = start; 294 key.offset = start;
289 key.type = BTRFS_EXTENT_CSUM_KEY; 295 key.type = BTRFS_EXTENT_CSUM_KEY;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 8ae72c3eedb1..4d8ef95abf40 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1019,7 +1019,7 @@ static noinline int csum_exist_in_range(struct btrfs_root *root,
1019 LIST_HEAD(list); 1019 LIST_HEAD(list);
1020 1020
1021 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, 1021 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1022 bytenr + num_bytes - 1, &list); 1022 bytenr + num_bytes - 1, &list, 0);
1023 if (ret == 0 && list_empty(&list)) 1023 if (ret == 0 && list_empty(&list))
1024 return 0; 1024 return 0;
1025 1025
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index ed8c055ab70f..e0a061db4353 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1809,6 +1809,75 @@ static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1809 return ret; 1809 return ret;
1810} 1810}
1811 1811
1812static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
1813{
1814 struct btrfs_ioctl_fs_info_args fi_args;
1815 struct btrfs_device *device;
1816 struct btrfs_device *next;
1817 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1818
1819 if (!capable(CAP_SYS_ADMIN))
1820 return -EPERM;
1821
1822 fi_args.num_devices = fs_devices->num_devices;
1823 fi_args.max_id = 0;
1824 memcpy(&fi_args.fsid, root->fs_info->fsid, sizeof(fi_args.fsid));
1825
1826 mutex_lock(&fs_devices->device_list_mutex);
1827 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
1828 if (device->devid > fi_args.max_id)
1829 fi_args.max_id = device->devid;
1830 }
1831 mutex_unlock(&fs_devices->device_list_mutex);
1832
1833 if (copy_to_user(arg, &fi_args, sizeof(fi_args)))
1834 return -EFAULT;
1835
1836 return 0;
1837}
1838
1839static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
1840{
1841 struct btrfs_ioctl_dev_info_args *di_args;
1842 struct btrfs_device *dev;
1843 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1844 int ret = 0;
1845 char *s_uuid = NULL;
1846 char empty_uuid[BTRFS_UUID_SIZE] = {0};
1847
1848 if (!capable(CAP_SYS_ADMIN))
1849 return -EPERM;
1850
1851 di_args = memdup_user(arg, sizeof(*di_args));
1852 if (IS_ERR(di_args))
1853 return PTR_ERR(di_args);
1854
1855 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
1856 s_uuid = di_args->uuid;
1857
1858 mutex_lock(&fs_devices->device_list_mutex);
1859 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
1860 mutex_unlock(&fs_devices->device_list_mutex);
1861
1862 if (!dev) {
1863 ret = -ENODEV;
1864 goto out;
1865 }
1866
1867 di_args->devid = dev->devid;
1868 di_args->bytes_used = dev->bytes_used;
1869 di_args->total_bytes = dev->total_bytes;
1870 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
1871 strncpy(di_args->path, dev->name, sizeof(di_args->path));
1872
1873out:
1874 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
1875 ret = -EFAULT;
1876
1877 kfree(di_args);
1878 return ret;
1879}
1880
1812static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 1881static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1813 u64 off, u64 olen, u64 destoff) 1882 u64 off, u64 olen, u64 destoff)
1814{ 1883{
@@ -2471,6 +2540,58 @@ static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2471 return btrfs_wait_for_commit(root, transid); 2540 return btrfs_wait_for_commit(root, transid);
2472} 2541}
2473 2542
2543static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2544{
2545 int ret;
2546 struct btrfs_ioctl_scrub_args *sa;
2547
2548 if (!capable(CAP_SYS_ADMIN))
2549 return -EPERM;
2550
2551 sa = memdup_user(arg, sizeof(*sa));
2552 if (IS_ERR(sa))
2553 return PTR_ERR(sa);
2554
2555 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
2556 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
2557
2558 if (copy_to_user(arg, sa, sizeof(*sa)))
2559 ret = -EFAULT;
2560
2561 kfree(sa);
2562 return ret;
2563}
2564
2565static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2566{
2567 if (!capable(CAP_SYS_ADMIN))
2568 return -EPERM;
2569
2570 return btrfs_scrub_cancel(root);
2571}
2572
2573static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2574 void __user *arg)
2575{
2576 struct btrfs_ioctl_scrub_args *sa;
2577 int ret;
2578
2579 if (!capable(CAP_SYS_ADMIN))
2580 return -EPERM;
2581
2582 sa = memdup_user(arg, sizeof(*sa));
2583 if (IS_ERR(sa))
2584 return PTR_ERR(sa);
2585
2586 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2587
2588 if (copy_to_user(arg, sa, sizeof(*sa)))
2589 ret = -EFAULT;
2590
2591 kfree(sa);
2592 return ret;
2593}
2594
2474long btrfs_ioctl(struct file *file, unsigned int 2595long btrfs_ioctl(struct file *file, unsigned int
2475 cmd, unsigned long arg) 2596 cmd, unsigned long arg)
2476{ 2597{
@@ -2510,6 +2631,10 @@ long btrfs_ioctl(struct file *file, unsigned int
2510 return btrfs_ioctl_add_dev(root, argp); 2631 return btrfs_ioctl_add_dev(root, argp);
2511 case BTRFS_IOC_RM_DEV: 2632 case BTRFS_IOC_RM_DEV:
2512 return btrfs_ioctl_rm_dev(root, argp); 2633 return btrfs_ioctl_rm_dev(root, argp);
2634 case BTRFS_IOC_FS_INFO:
2635 return btrfs_ioctl_fs_info(root, argp);
2636 case BTRFS_IOC_DEV_INFO:
2637 return btrfs_ioctl_dev_info(root, argp);
2513 case BTRFS_IOC_BALANCE: 2638 case BTRFS_IOC_BALANCE:
2514 return btrfs_balance(root->fs_info->dev_root); 2639 return btrfs_balance(root->fs_info->dev_root);
2515 case BTRFS_IOC_CLONE: 2640 case BTRFS_IOC_CLONE:
@@ -2533,6 +2658,12 @@ long btrfs_ioctl(struct file *file, unsigned int
2533 return btrfs_ioctl_start_sync(file, argp); 2658 return btrfs_ioctl_start_sync(file, argp);
2534 case BTRFS_IOC_WAIT_SYNC: 2659 case BTRFS_IOC_WAIT_SYNC:
2535 return btrfs_ioctl_wait_sync(file, argp); 2660 return btrfs_ioctl_wait_sync(file, argp);
2661 case BTRFS_IOC_SCRUB:
2662 return btrfs_ioctl_scrub(root, argp);
2663 case BTRFS_IOC_SCRUB_CANCEL:
2664 return btrfs_ioctl_scrub_cancel(root, argp);
2665 case BTRFS_IOC_SCRUB_PROGRESS:
2666 return btrfs_ioctl_scrub_progress(root, argp);
2536 } 2667 }
2537 2668
2538 return -ENOTTY; 2669 return -ENOTTY;
diff --git a/fs/btrfs/ioctl.h b/fs/btrfs/ioctl.h
index 8fb382167b13..e5e0ee2cad4e 100644
--- a/fs/btrfs/ioctl.h
+++ b/fs/btrfs/ioctl.h
@@ -32,6 +32,8 @@ struct btrfs_ioctl_vol_args {
32 32
33#define BTRFS_SUBVOL_CREATE_ASYNC (1ULL << 0) 33#define BTRFS_SUBVOL_CREATE_ASYNC (1ULL << 0)
34#define BTRFS_SUBVOL_RDONLY (1ULL << 1) 34#define BTRFS_SUBVOL_RDONLY (1ULL << 1)
35#define BTRFS_FSID_SIZE 16
36#define BTRFS_UUID_SIZE 16
35 37
36#define BTRFS_SUBVOL_NAME_MAX 4039 38#define BTRFS_SUBVOL_NAME_MAX 4039
37struct btrfs_ioctl_vol_args_v2 { 39struct btrfs_ioctl_vol_args_v2 {
@@ -42,6 +44,71 @@ struct btrfs_ioctl_vol_args_v2 {
42 char name[BTRFS_SUBVOL_NAME_MAX + 1]; 44 char name[BTRFS_SUBVOL_NAME_MAX + 1];
43}; 45};
44 46
47/*
48 * structure to report errors and progress to userspace, either as a
49 * result of a finished scrub, a canceled scrub or a progress inquiry
50 */
51struct btrfs_scrub_progress {
52 __u64 data_extents_scrubbed; /* # of data extents scrubbed */
53 __u64 tree_extents_scrubbed; /* # of tree extents scrubbed */
54 __u64 data_bytes_scrubbed; /* # of data bytes scrubbed */
55 __u64 tree_bytes_scrubbed; /* # of tree bytes scrubbed */
56 __u64 read_errors; /* # of read errors encountered (EIO) */
57 __u64 csum_errors; /* # of failed csum checks */
58 __u64 verify_errors; /* # of occurences, where the metadata
59 * of a tree block did not match the
60 * expected values, like generation or
61 * logical */
62 __u64 no_csum; /* # of 4k data block for which no csum
63 * is present, probably the result of
64 * data written with nodatasum */
65 __u64 csum_discards; /* # of csum for which no data was found
66 * in the extent tree. */
67 __u64 super_errors; /* # of bad super blocks encountered */
68 __u64 malloc_errors; /* # of internal kmalloc errors. These
69 * will likely cause an incomplete
70 * scrub */
71 __u64 uncorrectable_errors; /* # of errors where either no intact
72 * copy was found or the writeback
73 * failed */
74 __u64 corrected_errors; /* # of errors corrected */
75 __u64 last_physical; /* last physical address scrubbed. In
76 * case a scrub was aborted, this can
77 * be used to restart the scrub */
78 __u64 unverified_errors; /* # of occurences where a read for a
79 * full (64k) bio failed, but the re-
80 * check succeeded for each 4k piece.
81 * Intermittent error. */
82};
83
84#define BTRFS_SCRUB_READONLY 1
85struct btrfs_ioctl_scrub_args {
86 __u64 devid; /* in */
87 __u64 start; /* in */
88 __u64 end; /* in */
89 __u64 flags; /* in */
90 struct btrfs_scrub_progress progress; /* out */
91 /* pad to 1k */
92 __u64 unused[(1024-32-sizeof(struct btrfs_scrub_progress))/8];
93};
94
95#define BTRFS_DEVICE_PATH_NAME_MAX 1024
96struct btrfs_ioctl_dev_info_args {
97 __u64 devid; /* in/out */
98 __u8 uuid[BTRFS_UUID_SIZE]; /* in/out */
99 __u64 bytes_used; /* out */
100 __u64 total_bytes; /* out */
101 __u64 unused[379]; /* pad to 4k */
102 __u8 path[BTRFS_DEVICE_PATH_NAME_MAX]; /* out */
103};
104
105struct btrfs_ioctl_fs_info_args {
106 __u64 max_id; /* out */
107 __u64 num_devices; /* out */
108 __u8 fsid[BTRFS_FSID_SIZE]; /* out */
109 __u64 reserved[124]; /* pad to 1k */
110};
111
45#define BTRFS_INO_LOOKUP_PATH_MAX 4080 112#define BTRFS_INO_LOOKUP_PATH_MAX 4080
46struct btrfs_ioctl_ino_lookup_args { 113struct btrfs_ioctl_ino_lookup_args {
47 __u64 treeid; 114 __u64 treeid;
@@ -203,4 +270,13 @@ struct btrfs_ioctl_space_args {
203 struct btrfs_ioctl_vol_args_v2) 270 struct btrfs_ioctl_vol_args_v2)
204#define BTRFS_IOC_SUBVOL_GETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 25, __u64) 271#define BTRFS_IOC_SUBVOL_GETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 25, __u64)
205#define BTRFS_IOC_SUBVOL_SETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 26, __u64) 272#define BTRFS_IOC_SUBVOL_SETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 26, __u64)
273#define BTRFS_IOC_SCRUB _IOWR(BTRFS_IOCTL_MAGIC, 27, \
274 struct btrfs_ioctl_scrub_args)
275#define BTRFS_IOC_SCRUB_CANCEL _IO(BTRFS_IOCTL_MAGIC, 28)
276#define BTRFS_IOC_SCRUB_PROGRESS _IOWR(BTRFS_IOCTL_MAGIC, 29, \
277 struct btrfs_ioctl_scrub_args)
278#define BTRFS_IOC_DEV_INFO _IOWR(BTRFS_IOCTL_MAGIC, 30, \
279 struct btrfs_ioctl_dev_info_args)
280#define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
281 struct btrfs_ioctl_fs_info_args)
206#endif 282#endif
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 051992c7fcc9..fa2c5d87f219 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -4245,7 +4245,7 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4245 4245
4246 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; 4246 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4247 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, 4247 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4248 disk_bytenr + len - 1, &list); 4248 disk_bytenr + len - 1, &list, 0);
4249 4249
4250 while (!list_empty(&list)) { 4250 while (!list_empty(&list)) {
4251 sums = list_entry(list.next, struct btrfs_ordered_sum, list); 4251 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
new file mode 100644
index 000000000000..87a2f1273136
--- /dev/null
+++ b/fs/btrfs/scrub.c
@@ -0,0 +1,1368 @@
1/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
19#include <linux/sched.h>
20#include <linux/pagemap.h>
21#include <linux/writeback.h>
22#include <linux/blkdev.h>
23#include <linux/rbtree.h>
24#include <linux/slab.h>
25#include <linux/workqueue.h>
26#include "ctree.h"
27#include "volumes.h"
28#include "disk-io.h"
29#include "ordered-data.h"
30
31/*
32 * This is only the first step towards a full-features scrub. It reads all
33 * extent and super block and verifies the checksums. In case a bad checksum
34 * is found or the extent cannot be read, good data will be written back if
35 * any can be found.
36 *
37 * Future enhancements:
38 * - To enhance the performance, better read-ahead strategies for the
39 * extent-tree can be employed.
40 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
42 * - In case of a read error on files with nodatasum, map the file and read
43 * the extent to trigger a writeback of the good copy
44 * - track and record media errors, throw out bad devices
45 * - add a mode to also read unallocated space
46 * - make the prefetch cancellable
47 */
48
49struct scrub_bio;
50struct scrub_page;
51struct scrub_dev;
52static void scrub_bio_end_io(struct bio *bio, int err);
53static void scrub_checksum(struct btrfs_work *work);
54static int scrub_checksum_data(struct scrub_dev *sdev,
55 struct scrub_page *spag, void *buffer);
56static int scrub_checksum_tree_block(struct scrub_dev *sdev,
57 struct scrub_page *spag, u64 logical,
58 void *buffer);
59static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer);
60static int scrub_fixup_check(struct scrub_bio *sbio, int ix);
61static void scrub_fixup_end_io(struct bio *bio, int err);
62static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
63 struct page *page);
64static void scrub_fixup(struct scrub_bio *sbio, int ix);
65
66#define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
67#define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
68
69struct scrub_page {
70 u64 flags; /* extent flags */
71 u64 generation;
72 u64 mirror_num;
73 int have_csum;
74 u8 csum[BTRFS_CSUM_SIZE];
75};
76
77struct scrub_bio {
78 int index;
79 struct scrub_dev *sdev;
80 struct bio *bio;
81 int err;
82 u64 logical;
83 u64 physical;
84 struct scrub_page spag[SCRUB_PAGES_PER_BIO];
85 u64 count;
86 int next_free;
87 struct btrfs_work work;
88};
89
90struct scrub_dev {
91 struct scrub_bio *bios[SCRUB_BIOS_PER_DEV];
92 struct btrfs_device *dev;
93 int first_free;
94 int curr;
95 atomic_t in_flight;
96 spinlock_t list_lock;
97 wait_queue_head_t list_wait;
98 u16 csum_size;
99 struct list_head csum_list;
100 atomic_t cancel_req;
101 int readonly;
102 /*
103 * statistics
104 */
105 struct btrfs_scrub_progress stat;
106 spinlock_t stat_lock;
107};
108
109static void scrub_free_csums(struct scrub_dev *sdev)
110{
111 while (!list_empty(&sdev->csum_list)) {
112 struct btrfs_ordered_sum *sum;
113 sum = list_first_entry(&sdev->csum_list,
114 struct btrfs_ordered_sum, list);
115 list_del(&sum->list);
116 kfree(sum);
117 }
118}
119
120static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev)
121{
122 int i;
123 int j;
124 struct page *last_page;
125
126 if (!sdev)
127 return;
128
129 for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
130 struct scrub_bio *sbio = sdev->bios[i];
131 struct bio *bio;
132
133 if (!sbio)
134 break;
135
136 bio = sbio->bio;
137 if (bio) {
138 last_page = NULL;
139 for (j = 0; j < bio->bi_vcnt; ++j) {
140 if (bio->bi_io_vec[j].bv_page == last_page)
141 continue;
142 last_page = bio->bi_io_vec[j].bv_page;
143 __free_page(last_page);
144 }
145 bio_put(bio);
146 }
147 kfree(sbio);
148 }
149
150 scrub_free_csums(sdev);
151 kfree(sdev);
152}
153
154static noinline_for_stack
155struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev)
156{
157 struct scrub_dev *sdev;
158 int i;
159 int j;
160 int ret;
161 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
162
163 sdev = kzalloc(sizeof(*sdev), GFP_NOFS);
164 if (!sdev)
165 goto nomem;
166 sdev->dev = dev;
167 for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
168 struct bio *bio;
169 struct scrub_bio *sbio;
170
171 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
172 if (!sbio)
173 goto nomem;
174 sdev->bios[i] = sbio;
175
176 bio = bio_kmalloc(GFP_NOFS, SCRUB_PAGES_PER_BIO);
177 if (!bio)
178 goto nomem;
179
180 sbio->index = i;
181 sbio->sdev = sdev;
182 sbio->bio = bio;
183 sbio->count = 0;
184 sbio->work.func = scrub_checksum;
185 bio->bi_private = sdev->bios[i];
186 bio->bi_end_io = scrub_bio_end_io;
187 bio->bi_sector = 0;
188 bio->bi_bdev = dev->bdev;
189 bio->bi_size = 0;
190
191 for (j = 0; j < SCRUB_PAGES_PER_BIO; ++j) {
192 struct page *page;
193 page = alloc_page(GFP_NOFS);
194 if (!page)
195 goto nomem;
196
197 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
198 if (!ret)
199 goto nomem;
200 }
201 WARN_ON(bio->bi_vcnt != SCRUB_PAGES_PER_BIO);
202
203 if (i != SCRUB_BIOS_PER_DEV-1)
204 sdev->bios[i]->next_free = i + 1;
205 else
206 sdev->bios[i]->next_free = -1;
207 }
208 sdev->first_free = 0;
209 sdev->curr = -1;
210 atomic_set(&sdev->in_flight, 0);
211 atomic_set(&sdev->cancel_req, 0);
212 sdev->csum_size = btrfs_super_csum_size(&fs_info->super_copy);
213 INIT_LIST_HEAD(&sdev->csum_list);
214
215 spin_lock_init(&sdev->list_lock);
216 spin_lock_init(&sdev->stat_lock);
217 init_waitqueue_head(&sdev->list_wait);
218 return sdev;
219
220nomem:
221 scrub_free_dev(sdev);
222 return ERR_PTR(-ENOMEM);
223}
224
225/*
226 * scrub_recheck_error gets called when either verification of the page
227 * failed or the bio failed to read, e.g. with EIO. In the latter case,
228 * recheck_error gets called for every page in the bio, even though only
229 * one may be bad
230 */
231static void scrub_recheck_error(struct scrub_bio *sbio, int ix)
232{
233 if (sbio->err) {
234 if (scrub_fixup_io(READ, sbio->sdev->dev->bdev,
235 (sbio->physical + ix * PAGE_SIZE) >> 9,
236 sbio->bio->bi_io_vec[ix].bv_page) == 0) {
237 if (scrub_fixup_check(sbio, ix) == 0)
238 return;
239 }
240 }
241
242 scrub_fixup(sbio, ix);
243}
244
245static int scrub_fixup_check(struct scrub_bio *sbio, int ix)
246{
247 int ret = 1;
248 struct page *page;
249 void *buffer;
250 u64 flags = sbio->spag[ix].flags;
251
252 page = sbio->bio->bi_io_vec[ix].bv_page;
253 buffer = kmap_atomic(page, KM_USER0);
254 if (flags & BTRFS_EXTENT_FLAG_DATA) {
255 ret = scrub_checksum_data(sbio->sdev,
256 sbio->spag + ix, buffer);
257 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
258 ret = scrub_checksum_tree_block(sbio->sdev,
259 sbio->spag + ix,
260 sbio->logical + ix * PAGE_SIZE,
261 buffer);
262 } else {
263 WARN_ON(1);
264 }
265 kunmap_atomic(buffer, KM_USER0);
266
267 return ret;
268}
269
270static void scrub_fixup_end_io(struct bio *bio, int err)
271{
272 complete((struct completion *)bio->bi_private);
273}
274
275static void scrub_fixup(struct scrub_bio *sbio, int ix)
276{
277 struct scrub_dev *sdev = sbio->sdev;
278 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
279 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
280 struct btrfs_multi_bio *multi = NULL;
281 u64 logical = sbio->logical + ix * PAGE_SIZE;
282 u64 length;
283 int i;
284 int ret;
285 DECLARE_COMPLETION_ONSTACK(complete);
286
287 if ((sbio->spag[ix].flags & BTRFS_EXTENT_FLAG_DATA) &&
288 (sbio->spag[ix].have_csum == 0)) {
289 /*
290 * nodatasum, don't try to fix anything
291 * FIXME: we can do better, open the inode and trigger a
292 * writeback
293 */
294 goto uncorrectable;
295 }
296
297 length = PAGE_SIZE;
298 ret = btrfs_map_block(map_tree, REQ_WRITE, logical, &length,
299 &multi, 0);
300 if (ret || !multi || length < PAGE_SIZE) {
301 printk(KERN_ERR
302 "scrub_fixup: btrfs_map_block failed us for %llu\n",
303 (unsigned long long)logical);
304 WARN_ON(1);
305 return;
306 }
307
308 if (multi->num_stripes == 1)
309 /* there aren't any replicas */
310 goto uncorrectable;
311
312 /*
313 * first find a good copy
314 */
315 for (i = 0; i < multi->num_stripes; ++i) {
316 if (i == sbio->spag[ix].mirror_num)
317 continue;
318
319 if (scrub_fixup_io(READ, multi->stripes[i].dev->bdev,
320 multi->stripes[i].physical >> 9,
321 sbio->bio->bi_io_vec[ix].bv_page)) {
322 /* I/O-error, this is not a good copy */
323 continue;
324 }
325
326 if (scrub_fixup_check(sbio, ix) == 0)
327 break;
328 }
329 if (i == multi->num_stripes)
330 goto uncorrectable;
331
332 if (!sdev->readonly) {
333 /*
334 * bi_io_vec[ix].bv_page now contains good data, write it back
335 */
336 if (scrub_fixup_io(WRITE, sdev->dev->bdev,
337 (sbio->physical + ix * PAGE_SIZE) >> 9,
338 sbio->bio->bi_io_vec[ix].bv_page)) {
339 /* I/O-error, writeback failed, give up */
340 goto uncorrectable;
341 }
342 }
343
344 kfree(multi);
345 spin_lock(&sdev->stat_lock);
346 ++sdev->stat.corrected_errors;
347 spin_unlock(&sdev->stat_lock);
348
349 if (printk_ratelimit())
350 printk(KERN_ERR "btrfs: fixed up at %llu\n",
351 (unsigned long long)logical);
352 return;
353
354uncorrectable:
355 kfree(multi);
356 spin_lock(&sdev->stat_lock);
357 ++sdev->stat.uncorrectable_errors;
358 spin_unlock(&sdev->stat_lock);
359
360 if (printk_ratelimit())
361 printk(KERN_ERR "btrfs: unable to fixup at %llu\n",
362 (unsigned long long)logical);
363}
364
365static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
366 struct page *page)
367{
368 struct bio *bio = NULL;
369 int ret;
370 DECLARE_COMPLETION_ONSTACK(complete);
371
372 /* we are going to wait on this IO */
373 rw |= REQ_SYNC;
374
375 bio = bio_alloc(GFP_NOFS, 1);
376 bio->bi_bdev = bdev;
377 bio->bi_sector = sector;
378 bio_add_page(bio, page, PAGE_SIZE, 0);
379 bio->bi_end_io = scrub_fixup_end_io;
380 bio->bi_private = &complete;
381 submit_bio(rw, bio);
382
383 wait_for_completion(&complete);
384
385 ret = !test_bit(BIO_UPTODATE, &bio->bi_flags);
386 bio_put(bio);
387 return ret;
388}
389
390static void scrub_bio_end_io(struct bio *bio, int err)
391{
392 struct scrub_bio *sbio = bio->bi_private;
393 struct scrub_dev *sdev = sbio->sdev;
394 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
395
396 sbio->err = err;
397
398 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
399}
400
401static void scrub_checksum(struct btrfs_work *work)
402{
403 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
404 struct scrub_dev *sdev = sbio->sdev;
405 struct page *page;
406 void *buffer;
407 int i;
408 u64 flags;
409 u64 logical;
410 int ret;
411
412 if (sbio->err) {
413 for (i = 0; i < sbio->count; ++i)
414 scrub_recheck_error(sbio, i);
415
416 sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1);
417 sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
418 sbio->bio->bi_phys_segments = 0;
419 sbio->bio->bi_idx = 0;
420
421 for (i = 0; i < sbio->count; i++) {
422 struct bio_vec *bi;
423 bi = &sbio->bio->bi_io_vec[i];
424 bi->bv_offset = 0;
425 bi->bv_len = PAGE_SIZE;
426 }
427
428 spin_lock(&sdev->stat_lock);
429 ++sdev->stat.read_errors;
430 spin_unlock(&sdev->stat_lock);
431 goto out;
432 }
433 for (i = 0; i < sbio->count; ++i) {
434 page = sbio->bio->bi_io_vec[i].bv_page;
435 buffer = kmap_atomic(page, KM_USER0);
436 flags = sbio->spag[i].flags;
437 logical = sbio->logical + i * PAGE_SIZE;
438 ret = 0;
439 if (flags & BTRFS_EXTENT_FLAG_DATA) {
440 ret = scrub_checksum_data(sdev, sbio->spag + i, buffer);
441 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
442 ret = scrub_checksum_tree_block(sdev, sbio->spag + i,
443 logical, buffer);
444 } else if (flags & BTRFS_EXTENT_FLAG_SUPER) {
445 BUG_ON(i);
446 (void)scrub_checksum_super(sbio, buffer);
447 } else {
448 WARN_ON(1);
449 }
450 kunmap_atomic(buffer, KM_USER0);
451 if (ret)
452 scrub_recheck_error(sbio, i);
453 }
454
455out:
456 spin_lock(&sdev->list_lock);
457 sbio->next_free = sdev->first_free;
458 sdev->first_free = sbio->index;
459 spin_unlock(&sdev->list_lock);
460 atomic_dec(&sdev->in_flight);
461 wake_up(&sdev->list_wait);
462}
463
464static int scrub_checksum_data(struct scrub_dev *sdev,
465 struct scrub_page *spag, void *buffer)
466{
467 u8 csum[BTRFS_CSUM_SIZE];
468 u32 crc = ~(u32)0;
469 int fail = 0;
470 struct btrfs_root *root = sdev->dev->dev_root;
471
472 if (!spag->have_csum)
473 return 0;
474
475 crc = btrfs_csum_data(root, buffer, crc, PAGE_SIZE);
476 btrfs_csum_final(crc, csum);
477 if (memcmp(csum, spag->csum, sdev->csum_size))
478 fail = 1;
479
480 spin_lock(&sdev->stat_lock);
481 ++sdev->stat.data_extents_scrubbed;
482 sdev->stat.data_bytes_scrubbed += PAGE_SIZE;
483 if (fail)
484 ++sdev->stat.csum_errors;
485 spin_unlock(&sdev->stat_lock);
486
487 return fail;
488}
489
490static int scrub_checksum_tree_block(struct scrub_dev *sdev,
491 struct scrub_page *spag, u64 logical,
492 void *buffer)
493{
494 struct btrfs_header *h;
495 struct btrfs_root *root = sdev->dev->dev_root;
496 struct btrfs_fs_info *fs_info = root->fs_info;
497 u8 csum[BTRFS_CSUM_SIZE];
498 u32 crc = ~(u32)0;
499 int fail = 0;
500 int crc_fail = 0;
501
502 /*
503 * we don't use the getter functions here, as we
504 * a) don't have an extent buffer and
505 * b) the page is already kmapped
506 */
507 h = (struct btrfs_header *)buffer;
508
509 if (logical != le64_to_cpu(h->bytenr))
510 ++fail;
511
512 if (spag->generation != le64_to_cpu(h->generation))
513 ++fail;
514
515 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
516 ++fail;
517
518 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
519 BTRFS_UUID_SIZE))
520 ++fail;
521
522 crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
523 PAGE_SIZE - BTRFS_CSUM_SIZE);
524 btrfs_csum_final(crc, csum);
525 if (memcmp(csum, h->csum, sdev->csum_size))
526 ++crc_fail;
527
528 spin_lock(&sdev->stat_lock);
529 ++sdev->stat.tree_extents_scrubbed;
530 sdev->stat.tree_bytes_scrubbed += PAGE_SIZE;
531 if (crc_fail)
532 ++sdev->stat.csum_errors;
533 if (fail)
534 ++sdev->stat.verify_errors;
535 spin_unlock(&sdev->stat_lock);
536
537 return fail || crc_fail;
538}
539
540static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer)
541{
542 struct btrfs_super_block *s;
543 u64 logical;
544 struct scrub_dev *sdev = sbio->sdev;
545 struct btrfs_root *root = sdev->dev->dev_root;
546 struct btrfs_fs_info *fs_info = root->fs_info;
547 u8 csum[BTRFS_CSUM_SIZE];
548 u32 crc = ~(u32)0;
549 int fail = 0;
550
551 s = (struct btrfs_super_block *)buffer;
552 logical = sbio->logical;
553
554 if (logical != le64_to_cpu(s->bytenr))
555 ++fail;
556
557 if (sbio->spag[0].generation != le64_to_cpu(s->generation))
558 ++fail;
559
560 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
561 ++fail;
562
563 crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
564 PAGE_SIZE - BTRFS_CSUM_SIZE);
565 btrfs_csum_final(crc, csum);
566 if (memcmp(csum, s->csum, sbio->sdev->csum_size))
567 ++fail;
568
569 if (fail) {
570 /*
571 * if we find an error in a super block, we just report it.
572 * They will get written with the next transaction commit
573 * anyway
574 */
575 spin_lock(&sdev->stat_lock);
576 ++sdev->stat.super_errors;
577 spin_unlock(&sdev->stat_lock);
578 }
579
580 return fail;
581}
582
583static int scrub_submit(struct scrub_dev *sdev)
584{
585 struct scrub_bio *sbio;
586
587 if (sdev->curr == -1)
588 return 0;
589
590 sbio = sdev->bios[sdev->curr];
591
592 sbio->bio->bi_sector = sbio->physical >> 9;
593 sbio->bio->bi_size = sbio->count * PAGE_SIZE;
594 sbio->bio->bi_next = NULL;
595 sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
596 sbio->bio->bi_comp_cpu = -1;
597 sbio->bio->bi_bdev = sdev->dev->bdev;
598 sbio->err = 0;
599 sdev->curr = -1;
600 atomic_inc(&sdev->in_flight);
601
602 submit_bio(0, sbio->bio);
603
604 return 0;
605}
606
607static int scrub_page(struct scrub_dev *sdev, u64 logical, u64 len,
608 u64 physical, u64 flags, u64 gen, u64 mirror_num,
609 u8 *csum, int force)
610{
611 struct scrub_bio *sbio;
612
613again:
614 /*
615 * grab a fresh bio or wait for one to become available
616 */
617 while (sdev->curr == -1) {
618 spin_lock(&sdev->list_lock);
619 sdev->curr = sdev->first_free;
620 if (sdev->curr != -1) {
621 sdev->first_free = sdev->bios[sdev->curr]->next_free;
622 sdev->bios[sdev->curr]->next_free = -1;
623 sdev->bios[sdev->curr]->count = 0;
624 spin_unlock(&sdev->list_lock);
625 } else {
626 spin_unlock(&sdev->list_lock);
627 wait_event(sdev->list_wait, sdev->first_free != -1);
628 }
629 }
630 sbio = sdev->bios[sdev->curr];
631 if (sbio->count == 0) {
632 sbio->physical = physical;
633 sbio->logical = logical;
634 } else if (sbio->physical + sbio->count * PAGE_SIZE != physical) {
635 scrub_submit(sdev);
636 goto again;
637 }
638 sbio->spag[sbio->count].flags = flags;
639 sbio->spag[sbio->count].generation = gen;
640 sbio->spag[sbio->count].have_csum = 0;
641 sbio->spag[sbio->count].mirror_num = mirror_num;
642 if (csum) {
643 sbio->spag[sbio->count].have_csum = 1;
644 memcpy(sbio->spag[sbio->count].csum, csum, sdev->csum_size);
645 }
646 ++sbio->count;
647 if (sbio->count == SCRUB_PAGES_PER_BIO || force)
648 scrub_submit(sdev);
649
650 return 0;
651}
652
653static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,
654 u8 *csum)
655{
656 struct btrfs_ordered_sum *sum = NULL;
657 int ret = 0;
658 unsigned long i;
659 unsigned long num_sectors;
660 u32 sectorsize = sdev->dev->dev_root->sectorsize;
661
662 while (!list_empty(&sdev->csum_list)) {
663 sum = list_first_entry(&sdev->csum_list,
664 struct btrfs_ordered_sum, list);
665 if (sum->bytenr > logical)
666 return 0;
667 if (sum->bytenr + sum->len > logical)
668 break;
669
670 ++sdev->stat.csum_discards;
671 list_del(&sum->list);
672 kfree(sum);
673 sum = NULL;
674 }
675 if (!sum)
676 return 0;
677
678 num_sectors = sum->len / sectorsize;
679 for (i = 0; i < num_sectors; ++i) {
680 if (sum->sums[i].bytenr == logical) {
681 memcpy(csum, &sum->sums[i].sum, sdev->csum_size);
682 ret = 1;
683 break;
684 }
685 }
686 if (ret && i == num_sectors - 1) {
687 list_del(&sum->list);
688 kfree(sum);
689 }
690 return ret;
691}
692
693/* scrub extent tries to collect up to 64 kB for each bio */
694static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len,
695 u64 physical, u64 flags, u64 gen, u64 mirror_num)
696{
697 int ret;
698 u8 csum[BTRFS_CSUM_SIZE];
699
700 while (len) {
701 u64 l = min_t(u64, len, PAGE_SIZE);
702 int have_csum = 0;
703
704 if (flags & BTRFS_EXTENT_FLAG_DATA) {
705 /* push csums to sbio */
706 have_csum = scrub_find_csum(sdev, logical, l, csum);
707 if (have_csum == 0)
708 ++sdev->stat.no_csum;
709 }
710 ret = scrub_page(sdev, logical, l, physical, flags, gen,
711 mirror_num, have_csum ? csum : NULL, 0);
712 if (ret)
713 return ret;
714 len -= l;
715 logical += l;
716 physical += l;
717 }
718 return 0;
719}
720
721static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,
722 struct map_lookup *map, int num, u64 base, u64 length)
723{
724 struct btrfs_path *path;
725 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
726 struct btrfs_root *root = fs_info->extent_root;
727 struct btrfs_root *csum_root = fs_info->csum_root;
728 struct btrfs_extent_item *extent;
729 u64 flags;
730 int ret;
731 int slot;
732 int i;
733 u64 nstripes;
734 int start_stripe;
735 struct extent_buffer *l;
736 struct btrfs_key key;
737 u64 physical;
738 u64 logical;
739 u64 generation;
740 u64 mirror_num;
741
742 u64 increment = map->stripe_len;
743 u64 offset;
744
745 nstripes = length;
746 offset = 0;
747 do_div(nstripes, map->stripe_len);
748 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
749 offset = map->stripe_len * num;
750 increment = map->stripe_len * map->num_stripes;
751 mirror_num = 0;
752 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
753 int factor = map->num_stripes / map->sub_stripes;
754 offset = map->stripe_len * (num / map->sub_stripes);
755 increment = map->stripe_len * factor;
756 mirror_num = num % map->sub_stripes;
757 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
758 increment = map->stripe_len;
759 mirror_num = num % map->num_stripes;
760 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
761 increment = map->stripe_len;
762 mirror_num = num % map->num_stripes;
763 } else {
764 increment = map->stripe_len;
765 mirror_num = 0;
766 }
767
768 path = btrfs_alloc_path();
769 if (!path)
770 return -ENOMEM;
771
772 path->reada = 2;
773 path->search_commit_root = 1;
774 path->skip_locking = 1;
775
776 /*
777 * find all extents for each stripe and just read them to get
778 * them into the page cache
779 * FIXME: we can do better. build a more intelligent prefetching
780 */
781 logical = base + offset;
782 physical = map->stripes[num].physical;
783 ret = 0;
784 for (i = 0; i < nstripes; ++i) {
785 key.objectid = logical;
786 key.type = BTRFS_EXTENT_ITEM_KEY;
787 key.offset = (u64)0;
788
789 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
790 if (ret < 0)
791 goto out;
792
793 l = path->nodes[0];
794 slot = path->slots[0];
795 btrfs_item_key_to_cpu(l, &key, slot);
796 if (key.objectid != logical) {
797 ret = btrfs_previous_item(root, path, 0,
798 BTRFS_EXTENT_ITEM_KEY);
799 if (ret < 0)
800 goto out;
801 }
802
803 while (1) {
804 l = path->nodes[0];
805 slot = path->slots[0];
806 if (slot >= btrfs_header_nritems(l)) {
807 ret = btrfs_next_leaf(root, path);
808 if (ret == 0)
809 continue;
810 if (ret < 0)
811 goto out;
812
813 break;
814 }
815 btrfs_item_key_to_cpu(l, &key, slot);
816
817 if (key.objectid >= logical + map->stripe_len)
818 break;
819
820 path->slots[0]++;
821 }
822 btrfs_release_path(path);
823 logical += increment;
824 physical += map->stripe_len;
825 cond_resched();
826 }
827
828 /*
829 * collect all data csums for the stripe to avoid seeking during
830 * the scrub. This might currently (crc32) end up to be about 1MB
831 */
832 start_stripe = 0;
833again:
834 logical = base + offset + start_stripe * increment;
835 for (i = start_stripe; i < nstripes; ++i) {
836 ret = btrfs_lookup_csums_range(csum_root, logical,
837 logical + map->stripe_len - 1,
838 &sdev->csum_list, 1);
839 if (ret)
840 goto out;
841
842 logical += increment;
843 cond_resched();
844 }
845 /*
846 * now find all extents for each stripe and scrub them
847 */
848 logical = base + offset + start_stripe * increment;
849 physical = map->stripes[num].physical + start_stripe * map->stripe_len;
850 ret = 0;
851 for (i = start_stripe; i < nstripes; ++i) {
852 /*
853 * canceled?
854 */
855 if (atomic_read(&fs_info->scrub_cancel_req) ||
856 atomic_read(&sdev->cancel_req)) {
857 ret = -ECANCELED;
858 goto out;
859 }
860 /*
861 * check to see if we have to pause
862 */
863 if (atomic_read(&fs_info->scrub_pause_req)) {
864 /* push queued extents */
865 scrub_submit(sdev);
866 wait_event(sdev->list_wait,
867 atomic_read(&sdev->in_flight) == 0);
868 atomic_inc(&fs_info->scrubs_paused);
869 wake_up(&fs_info->scrub_pause_wait);
870 mutex_lock(&fs_info->scrub_lock);
871 while (atomic_read(&fs_info->scrub_pause_req)) {
872 mutex_unlock(&fs_info->scrub_lock);
873 wait_event(fs_info->scrub_pause_wait,
874 atomic_read(&fs_info->scrub_pause_req) == 0);
875 mutex_lock(&fs_info->scrub_lock);
876 }
877 atomic_dec(&fs_info->scrubs_paused);
878 mutex_unlock(&fs_info->scrub_lock);
879 wake_up(&fs_info->scrub_pause_wait);
880 scrub_free_csums(sdev);
881 start_stripe = i;
882 goto again;
883 }
884
885 key.objectid = logical;
886 key.type = BTRFS_EXTENT_ITEM_KEY;
887 key.offset = (u64)0;
888
889 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
890 if (ret < 0)
891 goto out;
892
893 l = path->nodes[0];
894 slot = path->slots[0];
895 btrfs_item_key_to_cpu(l, &key, slot);
896 if (key.objectid != logical) {
897 ret = btrfs_previous_item(root, path, 0,
898 BTRFS_EXTENT_ITEM_KEY);
899 if (ret < 0)
900 goto out;
901 }
902
903 while (1) {
904 l = path->nodes[0];
905 slot = path->slots[0];
906 if (slot >= btrfs_header_nritems(l)) {
907 ret = btrfs_next_leaf(root, path);
908 if (ret == 0)
909 continue;
910 if (ret < 0)
911 goto out;
912
913 break;
914 }
915 btrfs_item_key_to_cpu(l, &key, slot);
916
917 if (key.objectid + key.offset <= logical)
918 goto next;
919
920 if (key.objectid >= logical + map->stripe_len)
921 break;
922
923 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
924 goto next;
925
926 extent = btrfs_item_ptr(l, slot,
927 struct btrfs_extent_item);
928 flags = btrfs_extent_flags(l, extent);
929 generation = btrfs_extent_generation(l, extent);
930
931 if (key.objectid < logical &&
932 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
933 printk(KERN_ERR
934 "btrfs scrub: tree block %llu spanning "
935 "stripes, ignored. logical=%llu\n",
936 (unsigned long long)key.objectid,
937 (unsigned long long)logical);
938 goto next;
939 }
940
941 /*
942 * trim extent to this stripe
943 */
944 if (key.objectid < logical) {
945 key.offset -= logical - key.objectid;
946 key.objectid = logical;
947 }
948 if (key.objectid + key.offset >
949 logical + map->stripe_len) {
950 key.offset = logical + map->stripe_len -
951 key.objectid;
952 }
953
954 ret = scrub_extent(sdev, key.objectid, key.offset,
955 key.objectid - logical + physical,
956 flags, generation, mirror_num);
957 if (ret)
958 goto out;
959
960next:
961 path->slots[0]++;
962 }
963 btrfs_release_path(path);
964 logical += increment;
965 physical += map->stripe_len;
966 spin_lock(&sdev->stat_lock);
967 sdev->stat.last_physical = physical;
968 spin_unlock(&sdev->stat_lock);
969 }
970 /* push queued extents */
971 scrub_submit(sdev);
972
973out:
974 btrfs_free_path(path);
975 return ret < 0 ? ret : 0;
976}
977
978static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
979 u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length)
980{
981 struct btrfs_mapping_tree *map_tree =
982 &sdev->dev->dev_root->fs_info->mapping_tree;
983 struct map_lookup *map;
984 struct extent_map *em;
985 int i;
986 int ret = -EINVAL;
987
988 read_lock(&map_tree->map_tree.lock);
989 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
990 read_unlock(&map_tree->map_tree.lock);
991
992 if (!em)
993 return -EINVAL;
994
995 map = (struct map_lookup *)em->bdev;
996 if (em->start != chunk_offset)
997 goto out;
998
999 if (em->len < length)
1000 goto out;
1001
1002 for (i = 0; i < map->num_stripes; ++i) {
1003 if (map->stripes[i].dev == sdev->dev) {
1004 ret = scrub_stripe(sdev, map, i, chunk_offset, length);
1005 if (ret)
1006 goto out;
1007 }
1008 }
1009out:
1010 free_extent_map(em);
1011
1012 return ret;
1013}
1014
1015static noinline_for_stack
1016int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
1017{
1018 struct btrfs_dev_extent *dev_extent = NULL;
1019 struct btrfs_path *path;
1020 struct btrfs_root *root = sdev->dev->dev_root;
1021 struct btrfs_fs_info *fs_info = root->fs_info;
1022 u64 length;
1023 u64 chunk_tree;
1024 u64 chunk_objectid;
1025 u64 chunk_offset;
1026 int ret;
1027 int slot;
1028 struct extent_buffer *l;
1029 struct btrfs_key key;
1030 struct btrfs_key found_key;
1031 struct btrfs_block_group_cache *cache;
1032
1033 path = btrfs_alloc_path();
1034 if (!path)
1035 return -ENOMEM;
1036
1037 path->reada = 2;
1038 path->search_commit_root = 1;
1039 path->skip_locking = 1;
1040
1041 key.objectid = sdev->dev->devid;
1042 key.offset = 0ull;
1043 key.type = BTRFS_DEV_EXTENT_KEY;
1044
1045
1046 while (1) {
1047 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1048 if (ret < 0)
1049 goto out;
1050 ret = 0;
1051
1052 l = path->nodes[0];
1053 slot = path->slots[0];
1054
1055 btrfs_item_key_to_cpu(l, &found_key, slot);
1056
1057 if (found_key.objectid != sdev->dev->devid)
1058 break;
1059
1060 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1061 break;
1062
1063 if (found_key.offset >= end)
1064 break;
1065
1066 if (found_key.offset < key.offset)
1067 break;
1068
1069 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1070 length = btrfs_dev_extent_length(l, dev_extent);
1071
1072 if (found_key.offset + length <= start) {
1073 key.offset = found_key.offset + length;
1074 btrfs_release_path(path);
1075 continue;
1076 }
1077
1078 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1079 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1080 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1081
1082 /*
1083 * get a reference on the corresponding block group to prevent
1084 * the chunk from going away while we scrub it
1085 */
1086 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
1087 if (!cache) {
1088 ret = -ENOENT;
1089 goto out;
1090 }
1091 ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
1092 chunk_offset, length);
1093 btrfs_put_block_group(cache);
1094 if (ret)
1095 break;
1096
1097 key.offset = found_key.offset + length;
1098 btrfs_release_path(path);
1099 }
1100
1101out:
1102 btrfs_free_path(path);
1103 return ret;
1104}
1105
1106static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
1107{
1108 int i;
1109 u64 bytenr;
1110 u64 gen;
1111 int ret;
1112 struct btrfs_device *device = sdev->dev;
1113 struct btrfs_root *root = device->dev_root;
1114
1115 gen = root->fs_info->last_trans_committed;
1116
1117 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1118 bytenr = btrfs_sb_offset(i);
1119 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
1120 break;
1121
1122 ret = scrub_page(sdev, bytenr, PAGE_SIZE, bytenr,
1123 BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
1124 if (ret)
1125 return ret;
1126 }
1127 wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
1128
1129 return 0;
1130}
1131
1132/*
1133 * get a reference count on fs_info->scrub_workers. start worker if necessary
1134 */
1135static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
1136{
1137 struct btrfs_fs_info *fs_info = root->fs_info;
1138
1139 mutex_lock(&fs_info->scrub_lock);
1140 if (fs_info->scrub_workers_refcnt == 0)
1141 btrfs_start_workers(&fs_info->scrub_workers, 1);
1142 ++fs_info->scrub_workers_refcnt;
1143 mutex_unlock(&fs_info->scrub_lock);
1144
1145 return 0;
1146}
1147
1148static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
1149{
1150 struct btrfs_fs_info *fs_info = root->fs_info;
1151
1152 mutex_lock(&fs_info->scrub_lock);
1153 if (--fs_info->scrub_workers_refcnt == 0)
1154 btrfs_stop_workers(&fs_info->scrub_workers);
1155 WARN_ON(fs_info->scrub_workers_refcnt < 0);
1156 mutex_unlock(&fs_info->scrub_lock);
1157}
1158
1159
1160int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
1161 struct btrfs_scrub_progress *progress, int readonly)
1162{
1163 struct scrub_dev *sdev;
1164 struct btrfs_fs_info *fs_info = root->fs_info;
1165 int ret;
1166 struct btrfs_device *dev;
1167
1168 if (root->fs_info->closing)
1169 return -EINVAL;
1170
1171 /*
1172 * check some assumptions
1173 */
1174 if (root->sectorsize != PAGE_SIZE ||
1175 root->sectorsize != root->leafsize ||
1176 root->sectorsize != root->nodesize) {
1177 printk(KERN_ERR "btrfs_scrub: size assumptions fail\n");
1178 return -EINVAL;
1179 }
1180
1181 ret = scrub_workers_get(root);
1182 if (ret)
1183 return ret;
1184
1185 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1186 dev = btrfs_find_device(root, devid, NULL, NULL);
1187 if (!dev || dev->missing) {
1188 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1189 scrub_workers_put(root);
1190 return -ENODEV;
1191 }
1192 mutex_lock(&fs_info->scrub_lock);
1193
1194 if (!dev->in_fs_metadata) {
1195 mutex_unlock(&fs_info->scrub_lock);
1196 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1197 scrub_workers_put(root);
1198 return -ENODEV;
1199 }
1200
1201 if (dev->scrub_device) {
1202 mutex_unlock(&fs_info->scrub_lock);
1203 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1204 scrub_workers_put(root);
1205 return -EINPROGRESS;
1206 }
1207 sdev = scrub_setup_dev(dev);
1208 if (IS_ERR(sdev)) {
1209 mutex_unlock(&fs_info->scrub_lock);
1210 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1211 scrub_workers_put(root);
1212 return PTR_ERR(sdev);
1213 }
1214 sdev->readonly = readonly;
1215 dev->scrub_device = sdev;
1216
1217 atomic_inc(&fs_info->scrubs_running);
1218 mutex_unlock(&fs_info->scrub_lock);
1219 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1220
1221 down_read(&fs_info->scrub_super_lock);
1222 ret = scrub_supers(sdev);
1223 up_read(&fs_info->scrub_super_lock);
1224
1225 if (!ret)
1226 ret = scrub_enumerate_chunks(sdev, start, end);
1227
1228 wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
1229
1230 atomic_dec(&fs_info->scrubs_running);
1231 wake_up(&fs_info->scrub_pause_wait);
1232
1233 if (progress)
1234 memcpy(progress, &sdev->stat, sizeof(*progress));
1235
1236 mutex_lock(&fs_info->scrub_lock);
1237 dev->scrub_device = NULL;
1238 mutex_unlock(&fs_info->scrub_lock);
1239
1240 scrub_free_dev(sdev);
1241 scrub_workers_put(root);
1242
1243 return ret;
1244}
1245
1246int btrfs_scrub_pause(struct btrfs_root *root)
1247{
1248 struct btrfs_fs_info *fs_info = root->fs_info;
1249
1250 mutex_lock(&fs_info->scrub_lock);
1251 atomic_inc(&fs_info->scrub_pause_req);
1252 while (atomic_read(&fs_info->scrubs_paused) !=
1253 atomic_read(&fs_info->scrubs_running)) {
1254 mutex_unlock(&fs_info->scrub_lock);
1255 wait_event(fs_info->scrub_pause_wait,
1256 atomic_read(&fs_info->scrubs_paused) ==
1257 atomic_read(&fs_info->scrubs_running));
1258 mutex_lock(&fs_info->scrub_lock);
1259 }
1260 mutex_unlock(&fs_info->scrub_lock);
1261
1262 return 0;
1263}
1264
1265int btrfs_scrub_continue(struct btrfs_root *root)
1266{
1267 struct btrfs_fs_info *fs_info = root->fs_info;
1268
1269 atomic_dec(&fs_info->scrub_pause_req);
1270 wake_up(&fs_info->scrub_pause_wait);
1271 return 0;
1272}
1273
1274int btrfs_scrub_pause_super(struct btrfs_root *root)
1275{
1276 down_write(&root->fs_info->scrub_super_lock);
1277 return 0;
1278}
1279
1280int btrfs_scrub_continue_super(struct btrfs_root *root)
1281{
1282 up_write(&root->fs_info->scrub_super_lock);
1283 return 0;
1284}
1285
1286int btrfs_scrub_cancel(struct btrfs_root *root)
1287{
1288 struct btrfs_fs_info *fs_info = root->fs_info;
1289
1290 mutex_lock(&fs_info->scrub_lock);
1291 if (!atomic_read(&fs_info->scrubs_running)) {
1292 mutex_unlock(&fs_info->scrub_lock);
1293 return -ENOTCONN;
1294 }
1295
1296 atomic_inc(&fs_info->scrub_cancel_req);
1297 while (atomic_read(&fs_info->scrubs_running)) {
1298 mutex_unlock(&fs_info->scrub_lock);
1299 wait_event(fs_info->scrub_pause_wait,
1300 atomic_read(&fs_info->scrubs_running) == 0);
1301 mutex_lock(&fs_info->scrub_lock);
1302 }
1303 atomic_dec(&fs_info->scrub_cancel_req);
1304 mutex_unlock(&fs_info->scrub_lock);
1305
1306 return 0;
1307}
1308
1309int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
1310{
1311 struct btrfs_fs_info *fs_info = root->fs_info;
1312 struct scrub_dev *sdev;
1313
1314 mutex_lock(&fs_info->scrub_lock);
1315 sdev = dev->scrub_device;
1316 if (!sdev) {
1317 mutex_unlock(&fs_info->scrub_lock);
1318 return -ENOTCONN;
1319 }
1320 atomic_inc(&sdev->cancel_req);
1321 while (dev->scrub_device) {
1322 mutex_unlock(&fs_info->scrub_lock);
1323 wait_event(fs_info->scrub_pause_wait,
1324 dev->scrub_device == NULL);
1325 mutex_lock(&fs_info->scrub_lock);
1326 }
1327 mutex_unlock(&fs_info->scrub_lock);
1328
1329 return 0;
1330}
1331int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
1332{
1333 struct btrfs_fs_info *fs_info = root->fs_info;
1334 struct btrfs_device *dev;
1335 int ret;
1336
1337 /*
1338 * we have to hold the device_list_mutex here so the device
1339 * does not go away in cancel_dev. FIXME: find a better solution
1340 */
1341 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1342 dev = btrfs_find_device(root, devid, NULL, NULL);
1343 if (!dev) {
1344 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1345 return -ENODEV;
1346 }
1347 ret = btrfs_scrub_cancel_dev(root, dev);
1348 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1349
1350 return ret;
1351}
1352
1353int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
1354 struct btrfs_scrub_progress *progress)
1355{
1356 struct btrfs_device *dev;
1357 struct scrub_dev *sdev = NULL;
1358
1359 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1360 dev = btrfs_find_device(root, devid, NULL, NULL);
1361 if (dev)
1362 sdev = dev->scrub_device;
1363 if (sdev)
1364 memcpy(progress, &sdev->stat, sizeof(*progress));
1365 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1366
1367 return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;
1368}
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 33679fc710c6..dc80f7156923 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -1228,6 +1228,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1228 1228
1229 WARN_ON(cur_trans != trans->transaction); 1229 WARN_ON(cur_trans != trans->transaction);
1230 1230
1231 btrfs_scrub_pause(root);
1231 /* btrfs_commit_tree_roots is responsible for getting the 1232 /* btrfs_commit_tree_roots is responsible for getting the
1232 * various roots consistent with each other. Every pointer 1233 * various roots consistent with each other. Every pointer
1233 * in the tree of tree roots has to point to the most up to date 1234 * in the tree of tree roots has to point to the most up to date
@@ -1312,6 +1313,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1312 1313
1313 mutex_unlock(&root->fs_info->trans_mutex); 1314 mutex_unlock(&root->fs_info->trans_mutex);
1314 1315
1316 btrfs_scrub_continue(root);
1317
1315 if (current->journal_info == trans) 1318 if (current->journal_info == trans)
1316 current->journal_info = NULL; 1319 current->journal_info = NULL;
1317 1320
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index a794b9f60138..b4c191d6c774 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -614,7 +614,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
614 614
615 ret = btrfs_lookup_csums_range(root->log_root, 615 ret = btrfs_lookup_csums_range(root->log_root,
616 csum_start, csum_end - 1, 616 csum_start, csum_end - 1,
617 &ordered_sums); 617 &ordered_sums, 0);
618 BUG_ON(ret); 618 BUG_ON(ret);
619 while (!list_empty(&ordered_sums)) { 619 while (!list_empty(&ordered_sums)) {
620 struct btrfs_ordered_sum *sums; 620 struct btrfs_ordered_sum *sums;
@@ -2094,7 +2094,9 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2094 * the running transaction open, so a full commit can't hop 2094 * the running transaction open, so a full commit can't hop
2095 * in and cause problems either. 2095 * in and cause problems either.
2096 */ 2096 */
2097 btrfs_scrub_pause_super(root);
2097 write_ctree_super(trans, root->fs_info->tree_root, 1); 2098 write_ctree_super(trans, root->fs_info->tree_root, 1);
2099 btrfs_scrub_continue_super(root);
2098 ret = 0; 2100 ret = 0;
2099 2101
2100 mutex_lock(&root->log_mutex); 2102 mutex_lock(&root->log_mutex);
@@ -2689,7 +2691,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
2689 ret = btrfs_lookup_csums_range( 2691 ret = btrfs_lookup_csums_range(
2690 log->fs_info->csum_root, 2692 log->fs_info->csum_root,
2691 ds + cs, ds + cs + cl - 1, 2693 ds + cs, ds + cs + cl - 1,
2692 &ordered_sums); 2694 &ordered_sums, 0);
2693 BUG_ON(ret); 2695 BUG_ON(ret);
2694 } 2696 }
2695 } 2697 }
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index f777145203df..b8fc2fa91fdf 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -38,9 +38,6 @@ static int init_first_rw_device(struct btrfs_trans_handle *trans,
38 struct btrfs_device *device); 38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root); 39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40 40
41#define map_lookup_size(n) (sizeof(struct map_lookup) + \
42 (sizeof(struct btrfs_bio_stripe) * (n)))
43
44static DEFINE_MUTEX(uuid_mutex); 41static DEFINE_MUTEX(uuid_mutex);
45static LIST_HEAD(fs_uuids); 42static LIST_HEAD(fs_uuids);
46 43
@@ -1287,6 +1284,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1287 goto error_undo; 1284 goto error_undo;
1288 1285
1289 device->in_fs_metadata = 0; 1286 device->in_fs_metadata = 0;
1287 btrfs_scrub_cancel_dev(root, device);
1290 1288
1291 /* 1289 /*
1292 * the device list mutex makes sure that we don't change 1290 * the device list mutex makes sure that we don't change
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 05d5d199381a..7c58a7b2d99f 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -85,6 +85,9 @@ struct btrfs_device {
85 /* physical drive uuid (or lvm uuid) */ 85 /* physical drive uuid (or lvm uuid) */
86 u8 uuid[BTRFS_UUID_SIZE]; 86 u8 uuid[BTRFS_UUID_SIZE];
87 87
88 /* per-device scrub information */
89 struct scrub_dev *scrub_device;
90
88 struct btrfs_work work; 91 struct btrfs_work work;
89}; 92};
90 93
@@ -158,6 +161,9 @@ struct map_lookup {
158 struct btrfs_bio_stripe stripes[]; 161 struct btrfs_bio_stripe stripes[];
159}; 162};
160 163
164#define map_lookup_size(n) (sizeof(struct map_lookup) + \
165 (sizeof(struct btrfs_bio_stripe) * (n)))
166
161int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, 167int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
162 u64 end, u64 *length); 168 u64 end, u64 *length);
163 169