linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace
@ 2016-04-18 11:31 Anand Jain
  2016-04-18 11:31 ` [PATCH 01/13] btrfs: Introduce a new function to check if all chunks a OK for degraded mount Anand Jain
                   ` (14 more replies)
  0 siblings, 15 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

Thanks for various comments, tests and feedback.

Background: Spare device and Auto replace:
 Spare device is predominately used to mitigate or narrow the time
 window of a degraded raid mode, as because during which any further
 disk failure would lead to a catastrophic data loss. Data center
 storage generally will have couple of disks reserved as spares
 on their storage, so that it will automatically kickin to resilver
 the storage pool so that the pool is back to a healthy state.
 Mainly this is an storage feature rather than a FS feature,
 I believe people acquainted with enterprise storage use cases
 will appreciate the need of it, and so most/all of the enterprise
 storage has spare device feature.

Btrfs device states:
 This patch-set adds 'failed' state and makes provision to use
 'offline' state as two new device states. So to summarize
 various device states and their meanings..

 /* missing: device wasn't found at the time of mount */
 int missing;

 /*
  * failed: device confirmed to have experienced critical
  * io failure
  */
 int failed;

 /*
  * offline: When there is no confirmation that a disk has
  * failed. But an interim communication breakdown
  * and not necessarily a candidate for the device replace.
  * Device might be online after user intervention or after
  * block transport layer error recovery.
  */
 int offline;


Device state transition tuning and visualization:
 Sysfs interfaces are planned to provide the required tuning for
 device state transition, sensitivities and visualization of device
 states. However sysfs framework which could provide such an interface
 is being reviewed/tested and not yet ready as of now. So for the
 testing and debug of these features here I have used an update
 version of the procfs patch which is in the ML.

  [PATCH] btrfs: debug: procfs-devlist: introduce procfs interface for
the device list for debugging

 I find the above patch very useful, easy to use (as compared to
 sysfs to visualize the device state) and stable.

This patch set does not depend on any of the sysfs patches as such.

Backward compatibility:
 Adds a new incompatibility feature flags
 (BTRFS_FEATURE_INCOMPAT_SPARE_DEV) to manage the spare device
 when older kernels are used. So it is tested to be work fine
 with older kernel/prog versions.


Auto replace:
 Replace happens automatically, that is when there is any write
 failed or flush failed, the device will be marked as failed, which
 will stop any further IO attempt to that device. And in the next
 commit cycle the auto replace will pick the spare device to
 replace the failed device. And so the btrfs volume is back to a
 healthy state.

Per FSID spare vs Global spare:
 As of now only global spare is supported, that is spare(s)
 are for all the btrfs FS in the system. However future there will
 be a fs_info->no_auto_replace tunable which can be tuned by the user
 to limit the use of global spare.


Example use case:
 Here below is an example use case of the spare setup.

 Add a spare device:
        btrfs spare add /dev/sde -f

 If there is a spare device which is already added before the,
 just run

        btrfs dev scan [/dev/sde]

 Which will register the spare device to the kernel.

        btrfs fi show
         Label: none uuid: 52f170c1-725c-457d-8cfd-d57090460091
          Total devices 2 FS bytes used 112.00KiB
          devid 1 size 2.00GiB used 417.50MiB path /dev/sdc
          devid 2 size 2.00GiB used 417.50MiB path /dev/sdd

        Global spare
          device size 3.00GiB path /dev/sde


Patches:

Kernel:
 First, it needs, Qu's per chunk missing device patchset, which is
 part of the set.

 Next patches 6-9 adds support for Spare device. For kernel without
 spare feature the spare device is kept away. And when the kernel
 supports the spare device, it will inhibit from mounting it. Further
 these patch set provides helper function to pick a spare device and
 release a spare device back to the spare device pool.

 Patch 10 provides helper function to auto replace.
 Patch 11 provides helper function to bring a device to failed state.
 Patch 12 marks a device as failed based on flush and write errors,
  and avoids any further IO to it.
 Last 13 triggers auto replace.

Progs:
 Needs below 4 patches which will add sub cli 'spare' to manage
 the spare device. As of now deleting a spare device has to be
 managed using wipefs. However in the long run we would a proper
 btrfs command to do that job.



Changelog:
---------

v4->v5:
Kernel:
  a. Originally we had bugs as fixed in the patches below
     [PATCH] btrfs: s_bdev is not null after missing replace
     [PATCH] btrfs: cleanup assigning next active device with a check
     Incorporate those changes at force close device.

  b. Fixup
      btrfs: Introduce a new function to check if all chunks a OK for degraded mount
     as in
      [PATCH] btrfs: fix btrfs_check_degradable() to free extent map

Progs:
  None.

v3->v4:
Kernel:
 a.
  Mainly bug fixes. Thanks to Yauhen for the bug reports.
  Fixed the issue of bdev not being null. Also fixed the
  issue where auto replace didn't check for
  mutually_exclusive_operation_running. In this process,
  the function force_device_close() is changed quite a
  bit, mainly bdev is copied and nulled within the lock
  context, and later close on the copied bdev is called.
 b.
  changed the wording hot spare to spare device, as some of
  the legacy raid setup would need a perticular device
  order for some reasons. So the hot spare would copy
  back the replace target to the replaced disk. However
  we don't need such a setup in modern hw and btrfs won't
  do that way. To avoid any confusion I won't use the term
  hot spare here.

progs:
 No change. Same as v2.

V2->V3:
Kernel:
  Thanks to Yauhen and Austin for the review comments.
  Again split Patch 11 and 12 which was merged in V2 for better.
  Patch numbers are reordered (sorry about that) but for better.
  Fix rcu issue in btrfs_get_spare_device(), we don't need rcu
   as its under uuid_mutex
  Fix rcu issue and to check for replace lock at
   btrfs_auto_replace_start()
  Cleanup old: casualty_kthread() new: health_kthread() with
    changes as per
    838fe188 'btrfs: cleaner_kthread() doesn't need explicit freeze'
    (thanks Yauhen)
  Yauhen reported this issue:
	When a disk is removed through the virtualbox interface.
	BUG: unable to handle kernel NULL pointer dereference at 0000000000000548
	IP: generic_make_request_checks+0x4d/0x910
	::
 	bvec_alloc+0x5e/0x100
	generic_make_request+0x24/0x290
	submit_bio+0x67/0x140
	finish_rmw+0x409/0x570 [btrfs]
	full_stripe_write+0xa5/0xb0 [btrfs]
	raid56_parity_write+0xf5/0x180 [btrfs]
	btrfs_map_bio+0x105/0x300 [btrfs]
	btrfs_get_extent+0x83/0xb20 [btrfs]

	Status: So far the raid group profile would adapt to lower suitable
	group profile when device is missing/failed. This appears to
	be not happening with RAID56 OR there are stale IO which wasn't
	flushed out. Anyway to have this fixed I am moving the patch
	  btrfs: introduce device dynamic state transition to offline or failed
	to the top in v3,
	But firstly we need a reliable test case, or a very carefully
	crafted test case which can create this situation.

Progs:
  No change, same as V2.

V1->V2:
Kernel:
 (Based on tests and commets provided in the ML)
 a. Now transition_kthread() wakes up the casualty_kthread to check
    for device states. Instead of doing that in the transition_kthread()
    itself. Cleaner and less pressure on transition_kthread().
 b. Dropped
     [PATCH 05/15] btrfs: optimize btrfs_check_degradable() for calls outside of barrier
    as it was wrong patch and the optimization was incomplete.
 c. Merged patches
    btrfs: check for failed device and hot replace
      to
    btrfs: check device for critical errors and mark failed
    in an effort to make the changes as in a above.

Progs:
 a. Added to call btrfs_register_one_device() when doing btrfs
    spare add

Anand Jain (8):
  btrfs: introduce BTRFS_FEATURE_INCOMPAT_SPARE_DEV
  btrfs: add check not to mount a spare device
  btrfs: support btrfs dev scan for spare device
  btrfs: provide framework to get and put a spare device
  btrfs: introduce helper functions to perform hot replace
  btrfs: introduce device dynamic state transition to offline or failed
  btrfs: check device for critical errors and mark failed
  btrfs: check for failed device and hot replace

Qu Wenruo (5):
  btrfs: Introduce a new function to check if all chunks a OK for
    degraded mount
  btrfs: Do per-chunk check for mount time check
  btrfs: Do per-chunk degraded check for remount
  btrfs: Allow barrier_all_devices to do per-chunk device check
  btrfs: Cleanup num_tolerated_disk_barrier_failures

 fs/btrfs/ctree.h       |  11 +-
 fs/btrfs/dev-replace.c |  43 ++++++++
 fs/btrfs/dev-replace.h |   1 +
 fs/btrfs/disk-io.c     | 231 ++++++++++++++++++++++++++++-------------
 fs/btrfs/disk-io.h     |   2 -
 fs/btrfs/super.c       |  16 ++-
 fs/btrfs/volumes.c     | 277 ++++++++++++++++++++++++++++++++++++++++++++++---
 fs/btrfs/volumes.h     |  27 +++++
 8 files changed, 509 insertions(+), 99 deletions(-)

-- 
2.7.0


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 01/13] btrfs: Introduce a new function to check if all chunks a OK for degraded mount
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 02/13] btrfs: Do per-chunk check for mount time check Anand Jain
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Qu Wenruo <quwenruo@cn.fujitsu.com>

Introduce a new function, btrfs_check_degradable(), to judge if all chunks
in btrfs is OK for degraded mount.

It provides the new basis for accurate btrfs mount/remount and even
runtime degraded mount check other than old one-size-fit-all method.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 fs/btrfs/volumes.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h |  1 +
 2 files changed, 68 insertions(+)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 4ff716d9b16c..f5fa5f88263c 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -7065,3 +7065,70 @@ static void btrfs_close_one_device(struct btrfs_device *device)
 
 	call_rcu(&device->rcu, free_device);
 }
+
+/*
+ * Check if all chunks in the fs is OK for degraded mount
+ * Caller itself should do extra check if DEGRADED mount option is given
+ * for >0 return value.
+ *
+ * Return 0 if all chunks are OK.
+ * Return >0 if all chunks are degradable but not all OK.
+ * Return <0 if any chunk is not degradable or other bug.
+ */
+int btrfs_check_degradable(struct btrfs_fs_info *fs_info, unsigned flags)
+{
+	struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
+	struct extent_map *em;
+	u64 next_start = 0;
+	int ret = 0;
+
+	if (flags & MS_RDONLY)
+		return 0;
+
+	read_lock(&map_tree->map_tree.lock);
+	em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)(-1));
+	read_unlock(&map_tree->map_tree.lock);
+	/* No any chunk? Should be a huge bug */
+	if (!em) {
+		ret = -ENOENT;
+		goto out;
+	}
+
+	while (em) {
+		struct map_lookup *map;
+		int missing = 0;
+		int max_tolerated;
+		int i;
+
+		map = (struct map_lookup *) em->bdev;
+		max_tolerated =
+			btrfs_get_num_tolerated_disk_barrier_failures(
+					map->type);
+		for (i = 0; i < map->num_stripes; i++) {
+			if (map->stripes[i].dev->missing)
+				missing++;
+		}
+		if (missing > max_tolerated) {
+			ret = -EIO;
+			btrfs_warn(fs_info,
+				   "missing devices(%d) exceeds the limit(%d), writeable mount is not allowed",
+				   missing, max_tolerated);
+			goto out;
+		} else if (missing)
+			ret = 1;
+		next_start = extent_map_end(em);
+
+		/*
+		 * Alwasy search range [next_start, (u64)-1) to find the next
+		 * chunk map
+		 */
+		free_extent_map(em);
+		read_lock(&map_tree->map_tree.lock);
+		em = lookup_extent_mapping(&map_tree->map_tree, next_start,
+					   (u64)(-1) - next_start);
+		read_unlock(&map_tree->map_tree.lock);
+	}
+out:
+	free_extent_map(em);
+	return ret;
+}
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 93e0a2560366..ae9d552c93bf 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -568,5 +568,6 @@ static inline void unlock_chunks(struct btrfs_root *root)
 struct list_head *btrfs_get_fs_uuids(void);
 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
+int btrfs_check_degradable(struct btrfs_fs_info *fs_info, unsigned flags);
 
 #endif
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 02/13] btrfs: Do per-chunk check for mount time check
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
  2016-04-18 11:31 ` [PATCH 01/13] btrfs: Introduce a new function to check if all chunks a OK for degraded mount Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 03/13] btrfs: Do per-chunk degraded check for remount Anand Jain
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Qu Wenruo <quwenruo@cn.fujitsu.com>

Now use the btrfs_check_degraded() to do mount time degraded check.

With this patch, now we can mount with the following case:
 # mkfs.btrfs -f -m raid1 -d single /dev/sdb /dev/sdc
 # wipefs -a /dev/sdc
 # mount /dev/sdb /mnt/btrfs -o degraded
 As the single data chunk is only in sdb, so it's OK to mount as degraded,
 as missing one device is OK for RAID1.

But still fail with the following case as expected:
 # mkfs.btrfs -f -m raid1 -d single /dev/sdb /dev/sdc
 # wipefs -a /dev/sdb
 # mount /dev/sdc /mnt/btrfs -o degraded
 As the data chunk is only in sdb, so it's not OK to mount it as degraded.

Reported-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Reported-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>

[Btrfs: use btrfs_error instead of btrfs_err during mount]
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/disk-io.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index d01f89d130e0..4f91a049fbca 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2885,6 +2885,16 @@ int open_ctree(struct super_block *sb,
 		goto fail_tree_roots;
 	}
 
+	ret = btrfs_check_degradable(fs_info, fs_info->sb->s_flags);
+	if (ret < 0) {
+		btrfs_err(fs_info, "degraded writable mount failed %d", ret);
+		goto fail_tree_roots;
+	} else if (ret > 0 && !btrfs_test_opt(chunk_root, DEGRADED)) {
+		btrfs_warn(fs_info,
+			"Some device missing, but still degraded mountable, please mount with -o degraded option");
+		ret = -EACCES;
+		goto fail_tree_roots;
+	}
 	/*
 	 * keep the device that is marked to be the target device for the
 	 * dev_replace procedure
@@ -2988,14 +2998,6 @@ retry_root_backup:
 	}
 	fs_info->num_tolerated_disk_barrier_failures =
 		btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
-	if (fs_info->fs_devices->missing_devices >
-	     fs_info->num_tolerated_disk_barrier_failures &&
-	    !(sb->s_flags & MS_RDONLY)) {
-		pr_warn("BTRFS: missing devices(%llu) exceeds the limit(%d), writeable mount is not allowed\n",
-			fs_info->fs_devices->missing_devices,
-			fs_info->num_tolerated_disk_barrier_failures);
-		goto fail_sysfs;
-	}
 
 	fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
 					       "btrfs-cleaner");
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 03/13] btrfs: Do per-chunk degraded check for remount
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
  2016-04-18 11:31 ` [PATCH 01/13] btrfs: Introduce a new function to check if all chunks a OK for degraded mount Anand Jain
  2016-04-18 11:31 ` [PATCH 02/13] btrfs: Do per-chunk check for mount time check Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 04/13] btrfs: Allow barrier_all_devices to do per-chunk device check Anand Jain
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Qu Wenruo <quwenruo@cn.fujitsu.com>

Just the same for mount time check, use new btrfs_check_degraded() to do
per chunk check.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>

Btrfs: use btrfs_error instead of btrfs_err during remount

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/super.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 00b8f37cc306..87639fa53b10 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1767,11 +1767,14 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 			goto restore;
 		}
 
-		if (fs_info->fs_devices->missing_devices >
-		     fs_info->num_tolerated_disk_barrier_failures &&
-		    !(*flags & MS_RDONLY)) {
+		ret = btrfs_check_degradable(fs_info, *flags);
+		if (ret < 0) {
+			btrfs_err(fs_info,
+				"degraded writable remount failed %d", ret);
+			goto restore;
+		} else if (ret > 0 && !btrfs_test_opt(root, DEGRADED)) {
 			btrfs_warn(fs_info,
-				"too many missing devices, writeable remount is not allowed");
+				"some device missing, but still degraded mountable, please remount with -o degraded option");
 			ret = -EACCES;
 			goto restore;
 		}
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 04/13] btrfs: Allow barrier_all_devices to do per-chunk device check
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (2 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 03/13] btrfs: Do per-chunk degraded check for remount Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 05/13] btrfs: Cleanup num_tolerated_disk_barrier_failures Anand Jain
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Qu Wenruo <quwenruo@cn.fujitsu.com>

The last user of num_tolerated_disk_barrier_failures is
barrier_all_devices(). But it's can be easily changed to new per-chunk
degradable check framework.

Now btrfs_device will have two extra members, representing send/wait
error, set at write_dev_flush() time. And then check it in a similar but
more accurate behavior than old code.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 fs/btrfs/disk-io.c | 13 +++++--------
 fs/btrfs/volumes.c |  6 +++++-
 fs/btrfs/volumes.h |  4 ++++
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 4f91a049fbca..9ad3667f5e71 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3496,8 +3496,6 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 {
 	struct list_head *head;
 	struct btrfs_device *dev;
-	int errors_send = 0;
-	int errors_wait = 0;
 	int ret;
 
 	/* send down all the barriers */
@@ -3506,7 +3504,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 		if (dev->missing)
 			continue;
 		if (!dev->bdev) {
-			errors_send++;
+			dev->err_send = 1;
 			continue;
 		}
 		if (!dev->in_fs_metadata || !dev->writeable)
@@ -3514,7 +3512,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 
 		ret = write_dev_flush(dev, 0);
 		if (ret)
-			errors_send++;
+			dev->err_send = 1;
 	}
 
 	/* wait for all the barriers */
@@ -3522,7 +3520,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 		if (dev->missing)
 			continue;
 		if (!dev->bdev) {
-			errors_wait++;
+			dev->err_wait = 1;
 			continue;
 		}
 		if (!dev->in_fs_metadata || !dev->writeable)
@@ -3530,10 +3528,9 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 
 		ret = write_dev_flush(dev, 1);
 		if (ret)
-			errors_wait++;
+			dev->err_wait = 1;
 	}
-	if (errors_send > info->num_tolerated_disk_barrier_failures ||
-	    errors_wait > info->num_tolerated_disk_barrier_failures)
+	if (btrfs_check_degradable(info, info->sb->s_flags) < 0)
 		return -EIO;
 	return 0;
 }
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index f5fa5f88263c..639ae20cc5e6 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -7105,8 +7105,12 @@ int btrfs_check_degradable(struct btrfs_fs_info *fs_info, unsigned flags)
 			btrfs_get_num_tolerated_disk_barrier_failures(
 					map->type);
 		for (i = 0; i < map->num_stripes; i++) {
-			if (map->stripes[i].dev->missing)
+			if (map->stripes[i].dev->missing ||
+			    map->stripes[i].dev->err_wait ||
+			    map->stripes[i].dev->err_send)
 				missing++;
+			map->stripes[i].dev->err_wait = 0;
+			map->stripes[i].dev->err_send = 0;
 		}
 		if (missing > max_tolerated) {
 			ret = -EIO;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index ae9d552c93bf..101e5db2dd63 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -76,6 +76,10 @@ struct btrfs_device {
 	int can_discard;
 	int is_tgtdev_for_dev_replace;
 
+	/* for barrier_all_devices() check */
+	int err_send;
+	int err_wait;
+
 #ifdef __BTRFS_NEED_DEVICE_DATA_ORDERED
 	seqcount_t data_seqcount;
 #endif
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 05/13] btrfs: Cleanup num_tolerated_disk_barrier_failures
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (3 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 04/13] btrfs: Allow barrier_all_devices to do per-chunk device check Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 06/13] btrfs: introduce BTRFS_FEATURE_INCOMPAT_SPARE_DEV Anand Jain
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Qu Wenruo <quwenruo@cn.fujitsu.com>

As we use per-chunk degradable check, now the global
num_tolerated_disk_barrier_failures is of no use. So cleanup it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>

[Btrfs: resolve conflict to apply 'btrfs: Cleanup num_tolerated_disk_barrier_failures']
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/ctree.h   |  2 --
 fs/btrfs/disk-io.c | 56 ------------------------------------------------------
 fs/btrfs/disk-io.h |  2 --
 fs/btrfs/volumes.c | 17 -----------------
 4 files changed, 77 deletions(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 84a6a5b3384a..e0a50f478e01 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1829,8 +1829,6 @@ struct btrfs_fs_info {
 	/* next backup root to be overwritten */
 	int backup_root_index;
 
-	int num_tolerated_disk_barrier_failures;
-
 	/* device replace state */
 	struct btrfs_dev_replace dev_replace;
 
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 9ad3667f5e71..65c9f19d8017 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2996,8 +2996,6 @@ retry_root_backup:
 		printk(KERN_ERR "BTRFS: Failed to read block groups: %d\n", ret);
 		goto fail_sysfs;
 	}
-	fs_info->num_tolerated_disk_barrier_failures =
-		btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
 
 	fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
 					       "btrfs-cleaner");
@@ -3564,60 +3562,6 @@ int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
 	return min_tolerated;
 }
 
-int btrfs_calc_num_tolerated_disk_barrier_failures(
-	struct btrfs_fs_info *fs_info)
-{
-	struct btrfs_ioctl_space_info space;
-	struct btrfs_space_info *sinfo;
-	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
-		       BTRFS_BLOCK_GROUP_SYSTEM,
-		       BTRFS_BLOCK_GROUP_METADATA,
-		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
-	int i;
-	int c;
-	int num_tolerated_disk_barrier_failures =
-		(int)fs_info->fs_devices->num_devices;
-
-	for (i = 0; i < ARRAY_SIZE(types); i++) {
-		struct btrfs_space_info *tmp;
-
-		sinfo = NULL;
-		rcu_read_lock();
-		list_for_each_entry_rcu(tmp, &fs_info->space_info, list) {
-			if (tmp->flags == types[i]) {
-				sinfo = tmp;
-				break;
-			}
-		}
-		rcu_read_unlock();
-
-		if (!sinfo)
-			continue;
-
-		down_read(&sinfo->groups_sem);
-		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
-			u64 flags;
-
-			if (list_empty(&sinfo->block_groups[c]))
-				continue;
-
-			btrfs_get_block_group_info(&sinfo->block_groups[c],
-						   &space);
-			if (space.total_bytes == 0 || space.used_bytes == 0)
-				continue;
-			flags = space.flags;
-
-			num_tolerated_disk_barrier_failures = min(
-				num_tolerated_disk_barrier_failures,
-				btrfs_get_num_tolerated_disk_barrier_failures(
-					flags));
-		}
-		up_read(&sinfo->groups_sem);
-	}
-
-	return num_tolerated_disk_barrier_failures;
-}
-
 static int write_all_supers(struct btrfs_root *root, int max_mirrors)
 {
 	struct list_head *head;
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index 8e79d0070bcf..dd155621f95f 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -141,8 +141,6 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
 int btree_lock_page_hook(struct page *page, void *data,
 				void (*flush_fn)(void *));
 int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags);
-int btrfs_calc_num_tolerated_disk_barrier_failures(
-	struct btrfs_fs_info *fs_info);
 int __init btrfs_end_io_wq_init(void);
 void btrfs_end_io_wq_exit(void);
 
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 639ae20cc5e6..cf0d65dd443b 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1897,9 +1897,6 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
 		free_fs_devices(cur_devices);
 	}
 
-	root->fs_info->num_tolerated_disk_barrier_failures =
-		btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
-
 	/*
 	 * at this point, the device is zero sized.  We want to
 	 * remove it from the devices list and zero out the old super
@@ -2428,8 +2425,6 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
 				"sysfs: failed to create fsid for sprout");
 	}
 
-	root->fs_info->num_tolerated_disk_barrier_failures =
-		btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
 	ret = btrfs_commit_transaction(trans, root);
 
 	if (seeding_dev) {
@@ -3780,13 +3775,6 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
 			bctl->meta.target, bctl->data.target);
 	}
 
-	if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
-		fs_info->num_tolerated_disk_barrier_failures = min(
-			btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
-			btrfs_get_num_tolerated_disk_barrier_failures(
-				bctl->sys.target));
-	}
-
 	ret = insert_balance_item(fs_info->tree_root, bctl);
 	if (ret && ret != -EEXIST)
 		goto out;
@@ -3809,11 +3797,6 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
 	mutex_lock(&fs_info->balance_mutex);
 	atomic_dec(&fs_info->balance_running);
 
-	if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
-		fs_info->num_tolerated_disk_barrier_failures =
-			btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
-	}
-
 	if (bargs) {
 		memset(bargs, 0, sizeof(*bargs));
 		update_ioctl_balance_args(fs_info, 0, bargs);
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 06/13] btrfs: introduce BTRFS_FEATURE_INCOMPAT_SPARE_DEV
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (4 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 05/13] btrfs: Cleanup num_tolerated_disk_barrier_failures Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 07/13] btrfs: add check not to mount a spare device Anand Jain
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

Add BTRFS_FEATURE_INCOMPAT_SPARE_DEV (400) flag to identify
a spare device.

Along with this it checks in the mount context that a spare
device will fail to mount.  As spare devices aren't mountable.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/ctree.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index e0a50f478e01..2c185a8e92f0 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -531,6 +531,7 @@ struct btrfs_super_block {
 #define BTRFS_FEATURE_INCOMPAT_RAID56		(1ULL << 7)
 #define BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA	(1ULL << 8)
 #define BTRFS_FEATURE_INCOMPAT_NO_HOLES		(1ULL << 9)
+#define BTRFS_FEATURE_INCOMPAT_SPARE_DEV	(1ULL << 10)
 
 #define BTRFS_FEATURE_COMPAT_SUPP		0ULL
 #define BTRFS_FEATURE_COMPAT_SAFE_SET		0ULL
@@ -551,7 +552,8 @@ struct btrfs_super_block {
 	 BTRFS_FEATURE_INCOMPAT_RAID56 |		\
 	 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |		\
 	 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |	\
-	 BTRFS_FEATURE_INCOMPAT_NO_HOLES)
+	 BTRFS_FEATURE_INCOMPAT_NO_HOLES |		\
+	 BTRFS_FEATURE_INCOMPAT_SPARE_DEV)
 
 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET			\
 	(BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 07/13] btrfs: add check not to mount a spare device
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (5 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 06/13] btrfs: introduce BTRFS_FEATURE_INCOMPAT_SPARE_DEV Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 08/13] btrfs: support btrfs dev scan for " Anand Jain
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

Spare devices can be scanned but shouldn't be mountable.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/disk-io.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 65c9f19d8017..e9fca3bc7e42 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2811,6 +2811,14 @@ int open_ctree(struct super_block *sb,
 		goto fail_alloc;
 	}
 
+	if (btrfs_super_incompat_flags(disk_super) &
+			BTRFS_FEATURE_INCOMPAT_SPARE_DEV) {
+		/*You can only scan a spare device but not mount*/
+		printk(KERN_ERR "BTRFS: You can't mount a spare device\n");
+		err = -ENOTSUPP;
+		goto fail_alloc;
+	}
+
 	/*
 	 * Needn't use the lock because there is no other task which will
 	 * update the flag.
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 08/13] btrfs: support btrfs dev scan for spare device
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (6 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 07/13] btrfs: add check not to mount a spare device Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 09/13] btrfs: provide framework to get and put a " Anand Jain
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

When the user or system calls the BTRFS_IOC_SCAN_DEV,
ioctl this patch will make sure it is added to the device
list and set it as spare.

This operation will be same when BTRFS_IOC_DEVICES_READY
as well since BTRFS_IOC_DEVICES_READY ioctl has been doing
that by legacy.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/volumes.c | 4 ++++
 fs/btrfs/volumes.h | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index cf0d65dd443b..fdbad75506df 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -605,6 +605,10 @@ static noinline int device_list_add(const char *path,
 		if (IS_ERR(fs_devices))
 			return PTR_ERR(fs_devices);
 
+		if (btrfs_super_incompat_flags(disk_super) &
+				BTRFS_FEATURE_INCOMPAT_SPARE_DEV)
+			fs_devices->spare = 1;
+
 		list_add(&fs_devices->list, &fs_uuids);
 
 		device = NULL;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 101e5db2dd63..65a1d8a45c2d 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -263,6 +263,8 @@ struct btrfs_fs_devices {
 	struct kobject fsid_kobj;
 	struct kobject *device_dir_kobj;
 	struct completion kobj_unregister;
+
+	int spare;
 };
 
 #define BTRFS_BIO_INLINE_CSUM_SIZE	64
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 09/13] btrfs: provide framework to get and put a spare device
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (7 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 08/13] btrfs: support btrfs dev scan for " Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 10/13] btrfs: introduce helper functions to perform hot replace Anand Jain
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

This adds functions to get and put a spare device from the list.
So that hot repace code can pick a spare device when needed.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/ctree.h   |  1 +
 fs/btrfs/super.c   |  5 +++++
 fs/btrfs/volumes.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h |  2 ++
 4 files changed, 61 insertions(+)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 2c185a8e92f0..aa693cfdc9f0 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -4185,6 +4185,7 @@ void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
 
 /* super.c */
+struct file_system_type *btrfs_get_fs_type(void);
 int btrfs_parse_options(struct btrfs_root *root, char *options,
 			unsigned long new_flags);
 int btrfs_sync_fs(struct super_block *sb, int wait);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 87639fa53b10..49ba899b2d36 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -69,6 +69,11 @@ static struct file_system_type btrfs_fs_type;
 
 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
 
+struct file_system_type *btrfs_get_fs_type()
+{
+	return &btrfs_fs_type;
+}
+
 const char *btrfs_decode_error(int errno)
 {
 	char *errstr = "unknown";
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index fdbad75506df..86f4efbf2374 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -525,6 +525,59 @@ static void pending_bios_fn(struct btrfs_work *work)
 	run_scheduled_bios(device);
 }
 
+int btrfs_get_spare_device(char **path)
+{
+	int ret = 1;
+	struct btrfs_fs_devices *fs_devices;
+	struct btrfs_device *device;
+	struct list_head *fs_uuids = btrfs_get_fs_uuids();
+
+	mutex_lock(&uuid_mutex);
+	list_for_each_entry(fs_devices, fs_uuids, list) {
+		if (!fs_devices->spare)
+			continue;
+
+		/* as of now there is only one device in the spare fs_devices */
+		device = list_entry(fs_devices->devices.next,
+					struct btrfs_device, dev_list);
+
+		if (!device || !device->name)
+			continue;
+
+		fs_devices->spare = 0;
+		/*
+		 * Its under uuid_mutex and there is one spare per fsid
+		 * so rcu lock is actually not required
+		 */
+		*path = kstrdup(device->name->str, GFP_KERNEL);
+		if (*path)
+			ret = 0;
+		else
+			ret = -ENOMEM;
+		break;
+	}
+
+	if (!ret) {
+		btrfs_sysfs_remove_fsid(fs_devices);
+		list_del(&fs_devices->list);
+		free_fs_devices(fs_devices);
+	}
+	mutex_unlock(&uuid_mutex);
+
+	return ret;
+}
+
+void btrfs_put_spare_device(char *path)
+{
+	struct file_system_type *btrfs_fs_type;
+	struct btrfs_fs_devices *fs_devices;
+
+	btrfs_fs_type = btrfs_get_fs_type();
+
+	if (btrfs_scan_one_device(path, FMODE_READ,
+				    btrfs_fs_type, &fs_devices))
+		printk(KERN_INFO "failed to return spare device\n");
+}
 
 void btrfs_free_stale_device(struct btrfs_device *cur_dev)
 {
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 65a1d8a45c2d..12e08261b4c8 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -471,6 +471,8 @@ int btrfs_init_new_device(struct btrfs_root *root, char *path);
 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
 				  struct btrfs_device *srcdev,
 				  struct btrfs_device **device_out);
+int btrfs_get_spare_device(char **path);
+void btrfs_put_spare_device(char *path);
 int btrfs_balance(struct btrfs_balance_control *bctl,
 		  struct btrfs_ioctl_balance_args *bargs);
 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 10/13] btrfs: introduce helper functions to perform hot replace
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (8 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 09/13] btrfs: provide framework to get and put a " Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 11/13] btrfs: introduce device dynamic state transition to offline or failed Anand Jain
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

Hot replace / auto replace is important volume manager feature
and is critical to the data center operations, so that the degraded
volume can be brought back to a healthy state at the earliest and
without manual intervention.

This modifies the existing replace code to suite the need of auto
replace, in the long run I hope both the codes to be merged.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/dev-replace.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/dev-replace.h |  1 +
 2 files changed, 44 insertions(+)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index be0958de9f2d..db323f2bb1ef 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -956,3 +956,46 @@ void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
 				     &fs_info->fs_state));
 	}
 }
+
+int btrfs_auto_replace_start(struct btrfs_root *root, u64 src_devid)
+{
+	int ret;
+	char *tgt_path;
+	struct btrfs_fs_info *fs_info = root->fs_info;
+
+	if (!src_devid)
+		return -EINVAL;
+
+	if (fs_info->sb->s_flags & MS_RDONLY)
+		return -EROFS;
+
+	btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
+	if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
+		btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
+		return -EBUSY;
+	}
+	btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
+
+	if (btrfs_get_spare_device(&tgt_path)) {
+		btrfs_err(root->fs_info,
+			"No spare device found/configured in the kernel");
+		return -EINVAL;
+	}
+
+	if (atomic_xchg(
+		&root->fs_info->mutually_exclusive_operation_running, 1)) {
+		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
+	} else {
+		ret = btrfs_dev_replace_start(root, tgt_path, src_devid, NULL,
+		BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS);
+		atomic_set(
+			&root->fs_info->mutually_exclusive_operation_running, 0);
+	}
+
+	if (ret)
+		btrfs_put_spare_device(tgt_path);
+
+	kfree(tgt_path);
+
+	return ret;
+}
diff --git a/fs/btrfs/dev-replace.h b/fs/btrfs/dev-replace.h
index e922b42d91df..54b0812c8ba4 100644
--- a/fs/btrfs/dev-replace.h
+++ b/fs/btrfs/dev-replace.h
@@ -46,4 +46,5 @@ static inline void btrfs_dev_replace_stats_inc(atomic64_t *stat_value)
 {
 	atomic64_inc(stat_value);
 }
+int btrfs_auto_replace_start(struct btrfs_root *root, u64 src_devid);
 #endif
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 11/13] btrfs: introduce device dynamic state transition to offline or failed
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (9 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 10/13] btrfs: introduce helper functions to perform hot replace Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 12:59   ` kbuild test robot
  2016-04-18 11:31 ` [PATCH 12/13] btrfs: check device for critical errors and mark failed Anand Jain
                   ` (3 subsequent siblings)
  14 siblings, 1 reply; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

This patch provides helper functions to force a device to offline
or failed, and we need this device states for the following reasons,
1) a. it can be reported that device has failed when it does
   b. close the device when it goes offline so that blocklayer can
      cleanup
2) identify the candidate for the auto replace
3) avoid further commit error reported against the failing device and
4) a device in the multi device btrfs may go offline from the system
   (but as of now in in some system config btrfs gets unmounted in this
    context, which is not a correct behavior)

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/volumes.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h |  14 ++++++
 2 files changed, 145 insertions(+)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 86f4efbf2374..617e54f5fd19 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -7176,3 +7176,134 @@ out:
 	free_extent_map(em);
 	return ret;
 }
+
+static void __close_device(struct work_struct *work)
+{
+	struct btrfs_device *device;
+
+	device = container_of(work, struct btrfs_device, rcu_work);
+
+	if (device->closing_bdev)
+		blkdev_put(device->closing_bdev, device->mode);
+
+	device->closing_bdev = NULL;
+}
+
+static void close_device(struct rcu_head *head)
+{
+	struct btrfs_device *device;
+
+	device = container_of(head, struct btrfs_device, rcu);
+
+	INIT_WORK(&device->rcu_work, __close_device);
+	schedule_work(&device->rcu_work);
+}
+
+void device_force_close(struct btrfs_device *device)
+{
+	struct btrfs_fs_devices *fs_devices;
+
+	fs_devices = device->fs_devices;
+
+	mutex_lock(&fs_devices->device_list_mutex);
+	mutex_lock(&fs_devices->fs_info->chunk_mutex);
+	spin_lock(&fs_devices->fs_info->free_chunk_lock);
+
+	btrfs_assign_next_active_device(fs_devices->fs_info, device, NULL);
+
+	if (device->bdev)
+		fs_devices->open_devices--;
+
+	if (device->writeable) {
+		list_del_init(&device->dev_alloc_list);
+		fs_devices->rw_devices--;
+	}
+	device->writeable = 0;
+
+	/*
+	 * fixme: works for now, but its better to keep the state of
+	 * missing and offline different, and update rest of the
+	 * places where we check for only missing and not for failed
+	 * or offline as of now.
+	 */
+	device->missing = 1;
+	fs_devices->missing_devices++;
+	device->closing_bdev = device->bdev;
+	device->bdev = NULL;
+
+	call_rcu(&device->rcu, close_device);
+
+	spin_unlock(&fs_devices->fs_info->free_chunk_lock);
+	mutex_unlock(&fs_devices->fs_info->chunk_mutex);
+	mutex_unlock(&fs_devices->device_list_mutex);
+
+	rcu_barrier();
+}
+
+void btrfs_device_enforce_state(struct btrfs_device *dev, char *why)
+{
+	int tolerance;
+	bool degrade_option;
+	char dev_status[10];
+	char chunk_status[25];
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_fs_devices *fs_devices;
+
+	fs_devices = dev->fs_devices;
+	fs_info = fs_devices->fs_info;
+	degrade_option = btrfs_test_opt(fs_info->fs_root, DEGRADED);
+
+	/* todo: support seed later */
+	if (fs_devices->seeding)
+		return;
+
+	/* this shouldn't be called if device is already missing */
+	if (dev->missing || !dev->bdev)
+		return;
+
+	if (dev->offline || dev->failed)
+		return;
+
+	/* Only RW device is requested to force close let FS handle it*/
+	if (fs_devices->rw_devices == 1) {
+		btrfs_std_error(fs_info, -EIO,
+			"force offline last RW device");
+		return;
+	}
+
+	if (!strcmp(why, "offline"))
+		dev->offline = 1;
+	else if (!strcmp(why, "failed"))
+		dev->failed = 1;
+	else
+		return;
+
+	/*
+	 * Here after, there shouldn't any reason why can't force
+	 * close this device
+	 */
+	btrfs_sysfs_rm_device_link(fs_devices, dev);
+	device_force_close(dev);
+	strcpy(dev_status, "closed");
+
+	tolerance = btrfs_check_degradable(fs_info,
+						fs_info->sb->s_flags);
+	if (tolerance > 0) {
+		strncpy(chunk_status, "chunk(s) degraded", 25);
+	} else if(tolerance < 0) {
+		strncpy(chunk_status, "chunk(s) failed", 25);
+	} else {
+		strncpy(chunk_status, "No chunk(s) are degraded", 25);
+	}
+
+	btrfs_warn_in_rcu(fs_info, "device %s marked %s, %s, %s",
+		rcu_str_deref(dev->name), why, dev_status, chunk_status);
+	btrfs_info_in_rcu(fs_info,
+		"num_devices %llu rw_devices %llu degraded-option: %s",
+		fs_devices->num_devices, fs_devices->rw_devices,
+		degrade_option ? "set":"unset");
+
+	if (tolerance < 0)
+		btrfs_std_error(fs_info, -EIO, "devices below critical level");
+
+}
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 12e08261b4c8..cc006283a12e 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -66,13 +66,26 @@ struct btrfs_device {
 	struct btrfs_pending_bios pending_sync_bios;
 
 	struct block_device *bdev;
+	struct block_device *closing_bdev;
 
 	/* the mode sent to blkdev_get */
 	fmode_t mode;
 
 	int writeable;
 	int in_fs_metadata;
+	/* missing: device wasn't found at the time of mount */
 	int missing;
+	/* failed: device confirmed to have experienced critical io failure */
+	int failed;
+	/*
+	 * offline: system or user or block layer transport has removed
+	 * offlined the device which was once present and without going
+	 * through unmount. Implies an intriem communication break down
+	 * and not necessarily a candidate for the device replace. And
+	 * device might be online after user intervention or after
+	 * block transport layer error recovery.
+	 */
+	int offline;
 	int can_discard;
 	int is_tgtdev_for_dev_replace;
 
@@ -577,5 +590,6 @@ struct list_head *btrfs_get_fs_uuids(void);
 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
 int btrfs_check_degradable(struct btrfs_fs_info *fs_info, unsigned flags);
+void btrfs_device_enforce_state(struct btrfs_device *dev, char *why);
 
 #endif
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 12/13] btrfs: check device for critical errors and mark failed
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (10 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 11/13] btrfs: introduce device dynamic state transition to offline or failed Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-18 11:31 ` [PATCH 13/13] btrfs: check for failed device and hot replace Anand Jain
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

Write and Flush errors are considered as critical errors,
upon which the device will be brought offline and marked as
failed. Write and Flush errors are identified using device
error statistics. This is monitored using a kthread
btrfs_health.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/ctree.h   |   2 ++
 fs/btrfs/disk-io.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 fs/btrfs/volumes.c |   1 +
 fs/btrfs/volumes.h |   4 +++
 4 files changed, 107 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index aa693cfdc9f0..47e9cd9dd29a 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1569,6 +1569,7 @@ struct btrfs_fs_info {
 	struct mutex tree_log_mutex;
 	struct mutex transaction_kthread_mutex;
 	struct mutex cleaner_mutex;
+	struct mutex health_mutex;
 	struct mutex chunk_mutex;
 	struct mutex volume_mutex;
 
@@ -1686,6 +1687,7 @@ struct btrfs_fs_info {
 	struct btrfs_workqueue *extent_workers;
 	struct task_struct *transaction_kthread;
 	struct task_struct *cleaner_kthread;
+	struct task_struct *health_kthread;
 	int thread_pool_size;
 
 	struct kobject *space_info_kobj;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index e9fca3bc7e42..1deb5714cc3a 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1876,6 +1876,93 @@ sleep:
 	return 0;
 }
 
+/*
+ * returns:
+ * < 0 : Check didn't run, std error
+ *   0 : No errors found
+ * > 0 : # of devices having fatal errors
+ */
+static int btrfs_update_devices_health(struct btrfs_root *root)
+{
+	int ret = 0;
+	struct btrfs_device *device;
+	struct btrfs_fs_info *fs_info = root->fs_info;
+
+	if (btrfs_fs_closing(fs_info))
+		return -EBUSY;
+
+	/* mark disk(s) with write or flush error(s) as failed */
+	mutex_lock(&fs_info->volume_mutex);
+	list_for_each_entry_rcu(device,
+			&fs_info->fs_devices->devices, dev_list) {
+		int c_err;
+
+		if (device->failed) {
+			ret++;
+			continue;
+		}
+
+		/*
+		 * todo: replace target device's write/flush error,
+		 * skip for now
+		 */
+		if (device->is_tgtdev_for_dev_replace)
+			continue;
+
+		if (!device->dev_stats_valid)
+			continue;
+
+		c_err = atomic_read(&device->new_critical_errs);
+		atomic_sub(c_err, &device->new_critical_errs);
+		if (c_err) {
+			btrfs_crit_in_rcu(fs_info,
+				"fatal error on device %s",
+					rcu_str_deref(device->name));
+			btrfs_device_enforce_state(device, "failed");
+			ret ++;
+		}
+	}
+	mutex_unlock(&fs_info->volume_mutex);
+
+	return ret;
+}
+
+/*
+ * Devices health maintenance kthread, gets woken-up by transaction
+ * kthread, once sysfs is ready, this should publish the report
+ * through sysfs so that user land scripts and invoke actions.
+ */
+static int health_kthread(void *arg)
+{
+	struct btrfs_root *root = arg;
+
+	do {
+		if (btrfs_need_cleaner_sleep(root))
+			goto sleep;
+
+		if (!mutex_trylock(&root->fs_info->health_mutex))
+			goto sleep;
+
+		if (btrfs_need_cleaner_sleep(root)) {
+			mutex_unlock(&root->fs_info->health_mutex);
+			goto sleep;
+		}
+
+		/* Check devices health */
+		btrfs_update_devices_health(root);
+
+		mutex_unlock(&root->fs_info->health_mutex);
+
+sleep:
+		set_current_state(TASK_INTERRUPTIBLE);
+		if (!kthread_should_stop())
+			schedule();
+		__set_current_state(TASK_RUNNING);
+	} while (!kthread_should_stop());
+
+	return 0;
+}
+
 static int transaction_kthread(void *arg)
 {
 	struct btrfs_root *root = arg;
@@ -1922,6 +2009,7 @@ static int transaction_kthread(void *arg)
 			btrfs_end_transaction(trans, root);
 		}
 sleep:
+		wake_up_process(root->fs_info->health_kthread);
 		wake_up_process(root->fs_info->cleaner_kthread);
 		mutex_unlock(&root->fs_info->transaction_kthread_mutex);
 
@@ -2668,6 +2756,7 @@ int open_ctree(struct super_block *sb,
 	mutex_init(&fs_info->chunk_mutex);
 	mutex_init(&fs_info->transaction_kthread_mutex);
 	mutex_init(&fs_info->cleaner_mutex);
+	mutex_init(&fs_info->health_mutex);
 	mutex_init(&fs_info->volume_mutex);
 	mutex_init(&fs_info->ro_block_group_mutex);
 	init_rwsem(&fs_info->commit_root_sem);
@@ -3010,11 +3099,16 @@ retry_root_backup:
 	if (IS_ERR(fs_info->cleaner_kthread))
 		goto fail_sysfs;
 
+	fs_info->health_kthread = kthread_run(health_kthread, tree_root,
+					       "btrfs-health");
+	if (IS_ERR(fs_info->health_kthread))
+		goto fail_cleaner;
+
 	fs_info->transaction_kthread = kthread_run(transaction_kthread,
 						   tree_root,
 						   "btrfs-transaction");
 	if (IS_ERR(fs_info->transaction_kthread))
-		goto fail_cleaner;
+		goto fail_health;
 
 	if (!btrfs_test_opt(tree_root, SSD) &&
 	    !btrfs_test_opt(tree_root, NOSSD) &&
@@ -3178,6 +3272,10 @@ fail_trans_kthread:
 	kthread_stop(fs_info->transaction_kthread);
 	btrfs_cleanup_transaction(fs_info->tree_root);
 	btrfs_free_fs_roots(fs_info);
+
+fail_health:
+	kthread_stop(fs_info->health_kthread);
+
 fail_cleaner:
 	kthread_stop(fs_info->cleaner_kthread);
 
@@ -3833,6 +3931,7 @@ void close_ctree(struct btrfs_root *root)
 
 	kthread_stop(fs_info->transaction_kthread);
 	kthread_stop(fs_info->cleaner_kthread);
+	kthread_stop(fs_info->health_kthread);
 
 	fs_info->closing = 2;
 	smp_mb();
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 617e54f5fd19..a2023a7cc579 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -233,6 +233,7 @@ static struct btrfs_device *__alloc_device(void)
 	spin_lock_init(&dev->reada_lock);
 	atomic_set(&dev->reada_in_flight, 0);
 	atomic_set(&dev->dev_stats_ccnt, 0);
+	atomic_set(&dev->new_critical_errs, 0);
 	btrfs_device_data_ordered_init(dev);
 	INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
 	INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index cc006283a12e..f13b8a7d8248 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -167,6 +167,7 @@ struct btrfs_device {
 	/* Counter to record the change of device stats */
 	atomic_t dev_stats_ccnt;
 	atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
+	atomic_t new_critical_errs;
 };
 
 /*
@@ -539,6 +540,9 @@ static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
 	atomic_inc(dev->dev_stat_values + index);
 	smp_mb__before_atomic();
 	atomic_inc(&dev->dev_stats_ccnt);
+	if (index == BTRFS_DEV_STAT_WRITE_ERRS ||
+		index == BTRFS_DEV_STAT_FLUSH_ERRS)
+		atomic_inc(&dev->new_critical_errs);
 }
 
 static inline int btrfs_dev_stat_read(struct btrfs_device *dev,
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 13/13] btrfs: check for failed device and hot replace
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (11 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 12/13] btrfs: check device for critical errors and mark failed Anand Jain
@ 2016-04-18 11:31 ` Anand Jain
  2016-04-25 15:35 ` [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Yauhen Kharuzhy
  2016-04-28 12:10 ` Yauhen Kharuzhy
  14 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-04-18 11:31 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, yauhen.kharuzhy

From: Anand Jain <Anand.Jain@oracle.com>

This patch checks for failed device and kicks out auto
replace, if when user decided to disable auto replace
it can be done by future sysfs or future ioctl interface
to set fs_info->no_auto_replace parameter to 1.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
---
 fs/btrfs/ctree.h   |  2 ++
 fs/btrfs/disk-io.c | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 47e9cd9dd29a..67bb36bb82ee 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1862,6 +1862,8 @@ struct btrfs_fs_info {
 	struct list_head pinned_chunks;
 
 	int creating_free_space_tree;
+
+	int no_auto_replace;
 };
 
 struct btrfs_subvolume_writers {
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 1deb5714cc3a..5c5c51319bec 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1876,6 +1876,39 @@ sleep:
 	return 0;
 }
 
+static int btrfs_recuperate(struct btrfs_root *root)
+{
+	int ret;
+	u64 failed_devid = 0;
+	struct btrfs_device *device;
+	struct btrfs_fs_devices *fs_devices;
+
+	fs_devices = root->fs_info->fs_devices;
+
+	/* fixme: does it need device_list_mutex */
+	mutex_lock(&fs_devices->device_list_mutex);
+	rcu_read_lock();
+	list_for_each_entry_rcu(device,
+			&fs_devices->devices, dev_list) {
+		if (device->failed) {
+			failed_devid = device->devid;
+			break;
+		}
+	}
+	rcu_read_unlock();
+	mutex_unlock(&fs_devices->device_list_mutex);
+
+	/*
+	 * We are using the replace code which should be interrupt-able
+	 * during unmount, and as of now there is no user land stop
+	 * request that we support and this will run until its complete
+	 */
+	if (failed_devid && !root->fs_info->no_auto_replace)
+		ret = btrfs_auto_replace_start(root, failed_devid);
+
+	return ret;
+}
+
 /*
  * returns:
  * < 0 : Check didn't run, std error
@@ -1951,6 +1984,8 @@ static int health_kthread(void *arg)
 		/* Check devices health */
 		btrfs_update_devices_health(root);
 
+		btrfs_recuperate(root);
+
 		mutex_unlock(&root->fs_info->health_mutex);
 
 sleep:
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH 11/13] btrfs: introduce device dynamic state transition to offline or failed
  2016-04-18 11:31 ` [PATCH 11/13] btrfs: introduce device dynamic state transition to offline or failed Anand Jain
@ 2016-04-18 12:59   ` kbuild test robot
  0 siblings, 0 replies; 18+ messages in thread
From: kbuild test robot @ 2016-04-18 12:59 UTC (permalink / raw)
  To: Anand Jain; +Cc: kbuild-all, linux-btrfs, dsterba, yauhen.kharuzhy

[-- Attachment #1: Type: text/plain, Size: 1531 bytes --]

Hi Anand,

[auto build test ERROR on btrfs/next]
[also build test ERROR on v4.6-rc4 next-20160418]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Anand-Jain/Introduce-device-state-failed-spare-device-and-auto-replace/20160418-195801
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs.git next
config: i386-randconfig-x003-201616 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

   fs/btrfs/volumes.c: In function 'device_force_close':
>> fs/btrfs/volumes.c:7189:2: error: implicit declaration of function 'btrfs_assign_next_active_device' [-Werror=implicit-function-declaration]
     btrfs_assign_next_active_device(fs_devices->fs_info, device, NULL);
     ^
   cc1: some warnings being treated as errors

vim +/btrfs_assign_next_active_device +7189 fs/btrfs/volumes.c

  7183		fs_devices = device->fs_devices;
  7184	
  7185		mutex_lock(&fs_devices->device_list_mutex);
  7186		mutex_lock(&fs_devices->fs_info->chunk_mutex);
  7187		spin_lock(&fs_devices->fs_info->free_chunk_lock);
  7188	
> 7189		btrfs_assign_next_active_device(fs_devices->fs_info, device, NULL);
  7190	
  7191		if (device->bdev)
  7192			fs_devices->open_devices--;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 27427 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (12 preceding siblings ...)
  2016-04-18 11:31 ` [PATCH 13/13] btrfs: check for failed device and hot replace Anand Jain
@ 2016-04-25 15:35 ` Yauhen Kharuzhy
  2016-04-28 12:10 ` Yauhen Kharuzhy
  14 siblings, 0 replies; 18+ messages in thread
From: Yauhen Kharuzhy @ 2016-04-25 15:35 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs, dsterba

On Mon, Apr 18, 2016 at 07:31:31PM +0800, Anand Jain wrote:
> Thanks for various comments, tests and feedback.

Seems working good for me.

-- 
Yauhen Kharuzhy

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace
  2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
                   ` (13 preceding siblings ...)
  2016-04-25 15:35 ` [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Yauhen Kharuzhy
@ 2016-04-28 12:10 ` Yauhen Kharuzhy
  2016-05-02  1:32   ` Anand Jain
  14 siblings, 1 reply; 18+ messages in thread
From: Yauhen Kharuzhy @ 2016-04-28 12:10 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs, dsterba

On Mon, Apr 18, 2016 at 07:31:31PM +0800, Anand Jain wrote:
> Thanks for various comments, tests and feedback.
> 
> 
> Changelog:
> ---------
> 
> v4->v5:
> Kernel:
>   a. Originally we had bugs as fixed in the patches below
>      [PATCH] btrfs: s_bdev is not null after missing replace
>      [PATCH] btrfs: cleanup assigning next active device with a check
>      Incorporate those changes at force close device.
> 
>   b. Fixup
>       btrfs: Introduce a new function to check if all chunks a OK for degraded mount
>      as in
>       [PATCH] btrfs: fix btrfs_check_degradable() to free extent map
> 
> Progs:
>   None.
> 
> v3->v4:
> Kernel:
>  a.
>   Mainly bug fixes. Thanks to Yauhen for the bug reports.
>   Fixed the issue of bdev not being null. Also fixed the
>   issue where auto replace didn't check for
>   mutually_exclusive_operation_running. In this process,
>   the function force_device_close() is changed quite a
>   bit, mainly bdev is copied and nulled within the lock
>   context, and later close on the copied bdev is called.
>  b.
>   changed the wording hot spare to spare device, as some of
>   the legacy raid setup would need a perticular device
>   order for some reasons. So the hot spare would copy
>   back the replace target to the replaced disk. However
>   we don't need such a setup in modern hw and btrfs won't
>   do that way. To avoid any confusion I won't use the term
>   hot spare here.
> 
> progs:
>  No change. Same as v2.

Hmmm.... It seems like I have reproduced bug with null bdev fixed in v3.

Kernel 4.6-rc4, device was closed during of replacing it by other.

Sequense was:
1) create FS (RAID5), mount, write 40 MB file, sync
2) detach drive
3) start replace detached (but still not closed) drive by another
4) touch mount point, call 'btrfs fi sync' (I use udev script to initiate write immediately after drive removal)
4) <device is closed here> -> oops.

Failed line in generic_make_request_checks() seems to be

(gdb) list *(generic_make_request_checks+0x226)
0xffffffff812b9ff6 is in generic_make_request_checks (block/blk-core.c:1998).
1993
1994            if (!blkcg_bio_issue_check(q, bio))
1995                    return false;
1996
1997            trace_block_bio_queue(q, bio);
1998            return true;
1999
2000    end_io:
2001            bio->bi_error = err;
2002            bio_endio(bio);


Logs:

[  501.174109] BTRFS: device fsid 36dac817-e212-4ebd-b0a9-158511142cca devid 1 transid 3 /dev/sdc
[  501.225342] BTRFS: device fsid 36dac817-e212-4ebd-b0a9-158511142cca devid 2 transid 3 /dev/sde
[  501.248114] BTRFS: device fsid 36dac817-e212-4ebd-b0a9-158511142cca devid 3 transid 3 /dev/sdf
[  501.424316] BTRFS info (device sdf): disk space caching is enabled
[  501.437013] BTRFS: has skinny extents
[  501.440414] BTRFS: flagging fs with big metadata feature
[  501.454078] BTRFS: creating UUID tree
[  501.492933] BTRFS info (device sdf): event FSID=36dac817-e212-4ebd-b0a9-158511142cca,EVENT=state_changed,FS_STATE=good,,
[  502.047861] sd 3:0:0:0: [sdf] Synchronizing SCSI cache
[  502.065678] sd 3:0:0:0: [sdf] Stopping disk
[  502.093117] ata4.00: disabled
[  502.232151] ata4: hard resetting link
[  502.553636] ata4: SATA link up 3.0 Gbps (SStatus 123 SControl 300)
[  502.569587] ata4.00: ATA-6: VBOX HARDDISK, 1.0, max UDMA/133
[  502.573199] ata4.00: 41943040 sectors, multi 128: LBA48 NCQ (depth 31/32)
[  502.605463] ata4.00: configured for UDMA/133
[  502.625707] ata4: EH complete
[  502.658374] scsi 3:0:0:0: Direct-Access     ATA      VBOX HARDDISK    1.0  PQ: 0 ANSI: 5
[  502.742139] sd 3:0:0:0: [sdd] 41943040 512-byte logical blocks: (21.5 GB/20.0 GiB)
[  502.764534] sd 3:0:0:0: Attached scsi generic sg3 type 0
[  502.772915] sd 3:0:0:0: [sdd] Write Protect is off
[  502.780088] sd 3:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  502.834239] sd 3:0:0:0: [sdd] Attached SCSI disk
[  504.997866] BTRFS info (device sdf): dev_replace from /dev/sdf (devid 3) to /dev/sdd started
[  505.079668] BTRFS error (device sdf): bdev /dev/sdf errs: wr 0, rd 0, flush 1, corrupt 0, gen 0
[  505.090617] BTRFS warning (device sdf): lost page write due to IO error on /dev/sdf
[  505.100133] BTRFS error (device sdf): bdev /dev/sdf errs: wr 1, rd 0, flush 1, corrupt 0, gen 0
[  505.104022] BTRFS warning (device sdf): lost page write due to IO error on /dev/sdf
[  505.138437] BTRFS error (device sdf): bdev /dev/sdf errs: wr 2, rd 0, flush 1, corrupt 0, gen 0
[  505.174972] BTRFS info (device sdf): event FSID=36dac817-e212-4ebd-b0a9-158511142cca,EVENT=scrub_started,FS_STATE=replacing,,
[  505.210637] BTRFS warning (device sdf): i/o error at logical 20987904 on dev /dev/sdf, sector 2080: metadata leaf (level 0) in tree 3
[  505.245229] BTRFS warning (device sdf): i/o error at logical 20987904 on dev /dev/sdf, sector 2080: metadata leaf (level 0) in tree 3
[  505.281491] BTRFS error (device sdf): bdev /dev/sdf errs: wr 2, rd 1, flush 1, corrupt 0, gen 0
[  505.323670] BTRFS error (device sdf): fixed up error at logical 20987904 on dev /dev/sdf
[  505.396037] BTRFS error (device sdf): bdev /dev/sdf errs: wr 2, rd 1, flush 2, corrupt 0, gen 0
[  505.816820] BTRFS warning (device sdf): lost page write due to IO error on /dev/sdf
[  505.980350] BTRFS error (device sdf): bdev /dev/sdf errs: wr 3, rd 1, flush 2, corrupt 0, gen 0
[  506.060469] BTRFS warning (device sdf): lost page write due to IO error on /dev/sdf
[  506.083004] BTRFS error (device sdf): bdev /dev/sdf errs: wr 4, rd 1, flush 2, corrupt 0, gen 0
[  506.152976] BTRFS critical (device sdf): fatal error on device /dev/sdf
[  506.154907] BTRFS warning (device sdf): i/o error at logical 37765120 on dev /dev/sdf, sector 18464: metadata leaf (level 0) in tree 9
[  506.154909] BTRFS warning (device sdf): i/o error at logical 37765120 on dev /dev/sdf, sector 18464: metadata leaf (level 0) in tree 9
[  506.154912] BTRFS error (device sdf): bdev /dev/sdf errs: wr 4, rd 2, flush 2, corrupt 0, gen 0
[  506.155171] BTRFS error (device sdf): fixed up error at logical 37765120 on dev /dev/sdf
[  506.155351] BTRFS warning (device sdf): i/o error at logical 38109184 on dev /dev/sdf, sector 18752: metadata leaf (level 0) in tree 7
[  506.155353] BTRFS warning (device sdf): i/o error at logical 38109184 on dev /dev/sdf, sector 18752: metadata leaf (level 0) in tree 7
[  506.155355] BTRFS error (device sdf): bdev /dev/sdf errs: wr 4, rd 3, flush 2, corrupt 0, gen 0
[  506.155785] BTRFS error (device sdf): fixed up error at logical 38109184 on dev /dev/sdf
[  506.155811] BTRFS warning (device sdf): i/o error at logical 38125568 on dev /dev/sdf, sector 18784: metadata leaf (level 0) in tree 2
[  506.155813] BTRFS warning (device sdf): i/o error at logical 38125568 on dev /dev/sdf, sector 18784: metadata leaf (level 0) in tree 2
[  506.155815] BTRFS error (device sdf): bdev /dev/sdf errs: wr 4, rd 4, flush 2, corrupt 0, gen 0
[  506.156125] BTRFS error (device sdf): fixed up error at logical 38125568 on dev /dev/sdf
[  506.156143] BTRFS warning (device sdf): i/o error at logical 38141952 on dev /dev/sdf, sector 18816: metadata leaf (level 0) in tree 4
[  506.156144] BTRFS warning (device sdf): i/o error at logical 38141952 on dev /dev/sdf, sector 18816: metadata leaf (level 0) in tree 4
[  506.156390] BTRFS error (device sdf): fixed up error at logical 38141952 on dev /dev/sdf
[  506.157290] BTRFS warning (device sdf): i/o error at logical 38158336 on dev /dev/sdf, sector 18848: metadata leaf (level 0) in tree 1
[  506.157292] BTRFS warning (device sdf): i/o error at logical 38158336 on dev /dev/sdf, sector 18848: metadata leaf (level 0) in tree 1
[  506.159130] BTRFS error (device sdf): fixed up error at logical 38158336 on dev /dev/sdf
[  506.159155] BTRFS warning (device sdf): i/o error at logical 38174720 on dev /dev/sdf, sector 18880: metadata node (level 1) in tree 7
[  506.159167] BTRFS warning (device sdf): i/o error at logical 38174720 on dev /dev/sdf, sector 18880: metadata node (level 1) in tree 7
[  506.159314] BTRFS error (device sdf): fixed up error at logical 38174720 on dev /dev/sdf
[  506.159330] BTRFS warning (device sdf): i/o error at logical 38191104 on dev /dev/sdf, sector 18912: metadata leaf (level 0) in tree 7
[  506.159332] BTRFS warning (device sdf): i/o error at logical 38191104 on dev /dev/sdf, sector 18912: metadata leaf (level 0) in tree 7
[  506.159503] BTRFS error (device sdf): fixed up error at logical 38191104 on dev /dev/sdf
[  506.224289] BTRFS warning (device sdf): i/o error at logical 2186346496 on dev /dev/sdf, sector 2116608, root 5, inode 257, offset 65536, length 4096, links 1 (path: test.dat)
[  506.307665] BTRFS error (device sdf): fixed up error at logical 2186346496 on dev /dev/sdf
[  506.307801] BTRFS warning (device sdf): i/o error at logical 2186350592 on dev /dev/sdf, sector 2116616, root 5, inode 257, offset 69632, length 4096, links 1 (path: test.dat)
[  506.308063] BTRFS error (device sdf): fixed up error at logical 2186350592 on dev /dev/sdf
[  506.884108] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190741504 on dev /dev/sdf
[  506.986767] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190745600 on dev /dev/sdf
[  507.145938] BTRFS warning (device sdf): device /dev/sdf marked failed, closed, chunk(s) degraded
[  507.157268] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190749696 on dev /dev/sdf
[  507.157281] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190753792 on dev /dev/sdf
[  507.157288] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190757888 on dev /dev/sdf
[  507.157294] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190761984 on dev /dev/sdf
[  507.157303] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190766080 on dev /dev/sdf
[  507.157309] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190770176 on dev /dev/sdf
[  507.157314] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190774272 on dev /dev/sdf
[  507.157320] BTRFS error (device sdf): unable to fixup (regular) error at logical 2190778368 on dev /dev/sdf
[  507.186438] BUG: unable to handle kernel NULL pointer dereference at 0000000000000008
[  507.186452] IP: [<ffffffff812b9ff6>] generic_make_request_checks+0x226/0x500
[  507.186460] PGD 2af4a067 PUD 2ac1a067 PMD 0 
[  507.186462] Oops: 0000 [#1] SMP 
[  507.186528] Modules linked in: softdog cpufreq_userspace cpufreq_powersave cpufreq_stats cpufreq_conservative nfsd auth_rpcgss oid_registry nfs_acl nfs lockd grace fscache sunrpc loop ipmi_devintf ipmi_msghandler snd_pcm joydev snd_timer snd iTCO_wdt soundcore iTCO_vendor_support ohci_pci pcspkr ohci_hcd psmouse serio_raw evdev lpc_ich battery video ac mfd_core rng_core parport_pc parport 8250_fintek i2c_piix4 acpi_cpufreq button tpm_tis tpm processor ext4 crc16 jbd2 mbcache crc32c_generic btrfs xor raid6_pq dm_mod md_mod hid_generic usbhid hid xhci_pci xhci_hcd sr_mod cdrom sg sd_mod ata_generic ehci_pci ehci_hcd ata_piix ahci libahci libata usbcore usb_common scsi_mod e1000
[  507.186531] CPU: 0 PID: 5091 Comm: btrfs Not tainted 4.6.0-rc4-scst31x-debug-12+ #2
[  507.186532] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
[  507.186534] task: ffff88002aaeafc0 ti: ffff880029990000 task.ti: ffff880029990000
[  507.186537] RIP: 0010:[<ffffffff812b9ff6>]  [<ffffffff812b9ff6>] generic_make_request_checks+0x226/0x500
[  507.186538] RSP: 0018:ffff880029993718  EFLAGS: 00010202
[  507.186539] RAX: 0000000000000000 RBX: ffff88002c0fad68 RCX: ffff88002aaeafc0
[  507.186540] RDX: 0000000080000000 RSI: ffff88002c0fad68 RDI: 0000000000000000
[  507.186541] RBP: 0000000000000008 R08: ffffffff817d68e2 R09: ffffffff817d68d1
[  507.186542] R10: 0000000000000000 R11: ffffffff817d68c0 R12: ffff8800297be220
[  507.186543] R13: ffff88001d9d9900 R14: ffff880029909d80 R15: ffff8800297be000
[  507.186549] FS:  00007f3bdc2b4880(0000) GS:ffff88002fc00000(0000) knlGS:0000000000000000
[  507.186550] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  507.186551] CR2: 0000000000000008 CR3: 000000002a6a7000 CR4: 00000000000006f0
[  507.186555] Stack:
[  507.186558]  ffff88002aaeafc0 ffff88002aaeafc0 0000000000000001 0000000000000001
[  507.186565]  0000000000000000 ffff88002c0fad68 00000000ffffffff ffff8800297be220
[  507.186567]  ffff88001d9d9900 ffff880029909d80 ffff8800297be000 ffffffff812bc3ee
[  507.186568] Call Trace:
[  507.186572]  [<ffffffff812bc3ee>] ? generic_make_request+0x1e/0x1b0
[  507.186647]  [<ffffffffa027d8e8>] ? release_extent_buffer+0x28/0xc0 [btrfs]
[  507.186650]  [<ffffffff812bc5df>] ? submit_bio+0x5f/0x130
[  507.186670]  [<ffffffffa02b8549>] ? scrub_add_page_to_rd_bio+0xc9/0x280 [btrfs]
[  507.186673]  [<ffffffff811b6014>] ? alloc_pages_current+0x84/0x110
[  507.186702]  [<ffffffffa02bb306>] ? scrub_raid56_parity+0xb06/0xc80 [btrfs]
[  507.186723]  [<ffffffffa02bbfea>] ? scrub_stripe+0x92a/0x11b0 [btrfs]
[  507.186727]  [<ffffffff810596ca>] ? kvm_clock_read+0x1a/0x20
[  507.186775]  [<ffffffffa02bc97c>] ? scrub_chunk+0x10c/0x150 [btrfs]
[  507.186795]  [<ffffffffa02bcc13>] ? scrub_enumerate_chunks+0x253/0x510 [btrfs]
[  507.186799]  [<ffffffff810b2e00>] ? prepare_to_wait_event+0xd0/0xe0
[  507.186827]  [<ffffffffa02be193>] ? btrfs_scrub_dev+0x1c3/0x530 [btrfs]
[  507.186846]  [<ffffffffa025fae3>] ? start_transaction+0xa3/0x4e0 [btrfs]
[  507.186877]  [<ffffffffa02d1594>] ? btrfs_dev_replace_start+0x2a4/0x3f0 [btrfs]
[  507.186897]  [<ffffffffa02d171a>] ? btrfs_dev_replace_by_ioctl+0x3a/0x60 [btrfs]
[  507.186917]  [<ffffffffa029b0b3>] ? btrfs_ioctl+0x1d43/0x2190 [btrfs]
[  507.186920]  [<ffffffff811f6946>] ? iput+0x86/0x220
[  507.186924]  [<ffffffff812141b5>] ? __blkdev_put+0x1f5/0x250
[  507.186926]  [<ffffffff812ccec9>] ? disk_flush_events+0x39/0x60
[  507.186934]  [<ffffffff811f27b2>] ? dput+0xa2/0x210
[  507.186936]  [<ffffffff811ef02d>] ? do_vfs_ioctl+0x8d/0x5a0
[  507.186939]  [<ffffffff8108434b>] ? do_sigaction+0x17b/0x1a0
[  507.186941]  [<ffffffff811ef5b4>] ? SyS_ioctl+0x74/0x80
[  507.186944]  [<ffffffff8157b7f6>] ? entry_SYSCALL_64_fastpath+0x1e/0xa8
[  507.186964] Code: c5 e0 01 00 00 41 83 e5 10 ba ff ff ff 3f be 01 00 00 00 48 0f 44 fd e8 b9 56 05 00 66 66 66 66 90 b8 01 00 00 00 e9 8a 00 00 00 <48> 8b 47 08 48 8b 40 50 48 c1 f8 09 48 85 c0 0f 84 f2 fd ff ff 
[  507.186966] RIP  [<ffffffff812b9ff6>] generic_make_request_checks+0x226/0x500
[  507.186967]  RSP <ffff880029993718>
[  507.186968] CR2: 0000000000000008
[  507.186971] ---[ end trace 2485f8ef2600e6ab ]---
[  507.665193] sd 2:0:0:0: [sde] Synchronizing SCSI cache
[  507.665344] sd 2:0:0:0: [sde] Stopping disk
[  511.432359] BTRFS info (device sdf): num_devices 4 rw_devices 2 degraded-option: unset


-- 
Yauhen Kharuzhy

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace
  2016-04-28 12:10 ` Yauhen Kharuzhy
@ 2016-05-02  1:32   ` Anand Jain
  0 siblings, 0 replies; 18+ messages in thread
From: Anand Jain @ 2016-05-02  1:32 UTC (permalink / raw)
  To: Yauhen Kharuzhy; +Cc: linux-btrfs, dsterba


Thanks!  for report.

> Sequense was:
> 1) create FS (RAID5), mount, write 40 MB file, sync
> 2) detach drive
> 3) start replace detached (but still not closed) drive by another
> 4) touch mount point, call 'btrfs fi sync' (I use udev script to initiate write immediately after drive removal)
> 4) <device is closed here> -> oops.

  Right. It didn't handle this case. Now I have fixed this locally.
  But not sent yet, as I am also waiting to base this patch set on
  top of my recent patches.
    [PATCH 0/2] [RFC] btrfs: create degraded-RAID1 chunks

- Anand

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2016-05-02  1:32 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-18 11:31 [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Anand Jain
2016-04-18 11:31 ` [PATCH 01/13] btrfs: Introduce a new function to check if all chunks a OK for degraded mount Anand Jain
2016-04-18 11:31 ` [PATCH 02/13] btrfs: Do per-chunk check for mount time check Anand Jain
2016-04-18 11:31 ` [PATCH 03/13] btrfs: Do per-chunk degraded check for remount Anand Jain
2016-04-18 11:31 ` [PATCH 04/13] btrfs: Allow barrier_all_devices to do per-chunk device check Anand Jain
2016-04-18 11:31 ` [PATCH 05/13] btrfs: Cleanup num_tolerated_disk_barrier_failures Anand Jain
2016-04-18 11:31 ` [PATCH 06/13] btrfs: introduce BTRFS_FEATURE_INCOMPAT_SPARE_DEV Anand Jain
2016-04-18 11:31 ` [PATCH 07/13] btrfs: add check not to mount a spare device Anand Jain
2016-04-18 11:31 ` [PATCH 08/13] btrfs: support btrfs dev scan for " Anand Jain
2016-04-18 11:31 ` [PATCH 09/13] btrfs: provide framework to get and put a " Anand Jain
2016-04-18 11:31 ` [PATCH 10/13] btrfs: introduce helper functions to perform hot replace Anand Jain
2016-04-18 11:31 ` [PATCH 11/13] btrfs: introduce device dynamic state transition to offline or failed Anand Jain
2016-04-18 12:59   ` kbuild test robot
2016-04-18 11:31 ` [PATCH 12/13] btrfs: check device for critical errors and mark failed Anand Jain
2016-04-18 11:31 ` [PATCH 13/13] btrfs: check for failed device and hot replace Anand Jain
2016-04-25 15:35 ` [PATCH v5 00/13] Introduce device state 'failed', spare device and auto replace Yauhen Kharuzhy
2016-04-28 12:10 ` Yauhen Kharuzhy
2016-05-02  1:32   ` Anand Jain

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).