linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: introduce BTRFS_IOC_CHECK_DEV_EXCL_OPS ioctl to check dev excl op
@ 2013-08-21 13:10 Anand Jain
  2013-08-21 13:10 ` [PATCH] btrfs-progs: replace fails start but in the background Anand Jain
  2013-08-21 13:58 ` [PATCH] btrfs: introduce BTRFS_IOC_CHECK_DEV_EXCL_OPS ioctl to check dev excl op Stefan Behrens
  0 siblings, 2 replies; 10+ messages in thread
From: Anand Jain @ 2013-08-21 13:10 UTC (permalink / raw)
  To: linux-btrfs

This patch provides an ioctl to check if the FS is performing
any device exclusive operations like device add remove balance
etc. Basically any operation which will set
fs_info->mutually_exclusive_operation_running to true

This will be necessary for any user cli which has to know
the state before putting long running commands to background.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/ioctl.c           |   10 ++++++++++
 include/uapi/linux/btrfs.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 89f346c..3544a90 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4420,6 +4420,14 @@ out_unlock:
 	return ret;
 }
 
+static int btrfs_ioctl_check_dev_excl_op(struct btrfs_root *root,
+		void __user *arg)
+{
+	if (atomic_read(&root->fs_info->mutually_exclusive_operation_running))
+		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
+	return 0;
+}
+
 long btrfs_ioctl(struct file *file, unsigned int
 		cmd, unsigned long arg)
 {
@@ -4532,6 +4540,8 @@ long btrfs_ioctl(struct file *file, unsigned int
 		return btrfs_ioctl_set_fslabel(file, argp);
 	case BTRFS_IOC_FILE_EXTENT_SAME:
 		return btrfs_ioctl_file_extent_same(file, argp);
+	case BTRFS_IOC_CHECK_DEV_EXCL_OPS:
+		return btrfs_ioctl_check_dev_excl_op(root, NULL);
 	}
 
 	return -ENOTTY;
diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h
index 90d7bd9..a4fa6eb 100644
--- a/include/uapi/linux/btrfs.h
+++ b/include/uapi/linux/btrfs.h
@@ -606,5 +606,6 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code)
 				    struct btrfs_ioctl_dev_replace_args)
 #define BTRFS_IOC_FILE_EXTENT_SAME _IOWR(BTRFS_IOCTL_MAGIC, 54, \
 					 struct btrfs_ioctl_same_args)
+#define BTRFS_IOC_CHECK_DEV_EXCL_OPS _IO(BTRFS_IOCTL_MAGIC, 56)
 
 #endif /* _UAPI_LINUX_BTRFS_H */
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread
* [PATCH] btrfs-progs: replace fails start but in the background
@ 2013-09-13 11:37 Anand Jain
  0 siblings, 0 replies; 10+ messages in thread
From: Anand Jain @ 2013-09-13 11:37 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

when the balance is running, the replace start ioctl
fails (for the right reasons). but since the cli has
put ioctl thread to background (for right reasons)
the user won't know that cli failed to start.

so before cli goes to the background, it should check
if mutually_exclusive_operation_running is not held.

this is done by newly introduced ioctl
 BTRFS_IOC_CHECK_DEV_EXCL_OPS by the following kernel patch:

btrfs: introduce BTRFS_IOC_CHECK_DEV_EXCL_OPS ioctl to check dev excl op

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-replace.c | 14 ++++++++++++++
 ioctl.h        |  1 +
 utils.c        | 15 +++++++++++++++
 utils.h        |  1 +
 4 files changed, 31 insertions(+)

diff --git a/cmds-replace.c b/cmds-replace.c
index a31d77e..f69c5ce 100644
--- a/cmds-replace.c
+++ b/cmds-replace.c
@@ -203,6 +203,20 @@ static int cmd_start_replace(int argc, char **argv)
 		goto leave_with_error;
 	}
 
+	/* check if there is some other device exclusive
+	 * operation running in the FS which won't let this replace
+	 * to run. And ENOTTY is when older kernel doesn't support
+	 * lock checking ioctl
+	 */
+	ret = is_dev_excl_op_free(fdmnt);
+	if (ret && ret != -ENOTTY) {
+		fprintf(stderr,
+			"ERROR: replace start failed on \"%s\" - %s\n",
+			path,
+			ret > 0 ? btrfs_err_str(ret) : strerror(-ret));
+		goto leave_with_error;
+	}
+
 	srcdev = argv[optind];
 	dstdev = argv[optind + 1];
 
diff --git a/ioctl.h b/ioctl.h
index c0dcc06..7df7fc9 100644
--- a/ioctl.h
+++ b/ioctl.h
@@ -598,6 +598,7 @@ struct btrfs_ioctl_clone_range_args {
 #define BTRFS_IOC_DEV_REPLACE _IOWR(BTRFS_IOCTL_MAGIC, 53, \
 				    struct btrfs_ioctl_dev_replace_args)
 #define BTRFS_IOC_DEDUP_CTL _IOW(BTRFS_IOCTL_MAGIC, 55, int)
+#define BTRFS_IOC_CHECK_DEV_EXCL_OPS _IO(BTRFS_IOCTL_MAGIC, 56)
 
 #ifdef __cplusplus
 }
diff --git a/utils.c b/utils.c
index 02a2658..22c3310 100644
--- a/utils.c
+++ b/utils.c
@@ -2004,3 +2004,18 @@ int is_vol_small(char *file)
 		return 0;
 	}
 }
+
+/* Returns:
+ * BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS:
+ *	If the device is locked to prevent other device operations
+ *	from the user cli like device add remove replace balance etc..
+ * < 0:
+ *	For any other error including if kernel don't support the
+ *	ioctl (-ENOTTY)
+ */
+int is_dev_excl_op_free(int fd)
+{
+	int ret;
+	ret = ioctl(fd, BTRFS_IOC_CHECK_DEV_EXCL_OPS, NULL);
+	return ret > 0 ? ret : -errno;
+}
diff --git a/utils.h b/utils.h
index 616bae1..6952d34 100644
--- a/utils.h
+++ b/utils.h
@@ -85,4 +85,5 @@ int is_vol_small(char *file);
 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
 			   int verify);
 int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
+int is_dev_excl_op_free(int fd);
 #endif
-- 
1.8.4.rc4.1.g0d8beaa


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

end of thread, other threads:[~2013-09-13 11:30 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-21 13:10 [PATCH] btrfs: introduce BTRFS_IOC_CHECK_DEV_EXCL_OPS ioctl to check dev excl op Anand Jain
2013-08-21 13:10 ` [PATCH] btrfs-progs: replace fails start but in the background Anand Jain
2013-09-02  2:55   ` [PATCH] btrfs-progs: replace fails start but in the background v2 Anand Jain
2013-09-06  9:39     ` Anand Jain
2013-09-06  9:38   ` [PATCH] btrfs-progs: replace fails start but in the background Anand Jain
2013-08-21 13:58 ` [PATCH] btrfs: introduce BTRFS_IOC_CHECK_DEV_EXCL_OPS ioctl to check dev excl op Stefan Behrens
2013-08-22  9:51   ` anand jain
2013-08-22 10:36     ` Stefan Behrens
2013-08-23  6:45       ` Anand Jain
  -- strict thread matches above, loose matches on Subject: below --
2013-09-13 11:37 [PATCH] btrfs-progs: replace fails start but in the background 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).