Skip to contentSkip to conversation

ceph: account for name and fsid in new device spec

PATCH Superseded Hu Weiwen 4 participants 24 messages May 7, 2023 – Jun 30, 2023

Subsystems (2)
lore
Share & tools
lore

Hu Weiwen

author
#0 lore raw

cover ceph: account for name and fsid in new device spec

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device spec format, but kernel just discard them. Instead of relying on the mount.ceph helper, we should do this directly in kernel to ensure the options and device spec are consistent. And also avoid any confusion.

Hu Weiwen (3):
ceph: refactor mds_namespace comparing
ceph: save name and fsid in mount source
libceph: reject mismatching name and fsid

 fs/ceph/super.c        | 51 +++++++++++++++++++++++++-----------------
 net/ceph/ceph_common.c | 26 ++++++++++++++++-----
 2 files changed, 52 insertions(+), 25 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/3 ceph: refactor mds_namespace comparing

From: Hu Weiwen <[email protected]>

Same logic, slightly less code. Make the following changes easier.

---
 fs/ceph/super.c | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)
fs/ceph/super.c +14 -20
index 3fc48b43cab0..4e1f4031e888 100644
@@ -235,18 +235,10 @@ static void canonicalize_path(char *path)
 	path[j] = '\0';
 }
 
-/*
- * Check if the mds namespace in ceph_mount_options matches
- * the passed in namespace string. First time match (when
- * ->mds_namespace is NULL) is treated specially, since
- * ->mds_namespace needs to be initialized by the caller.
- */
-static int namespace_equals(struct ceph_mount_options *fsopt,
-			    const char *namespace, size_t len)
+/* check if s1 (null terminated) equals to s2 (with length len2) */
+static int strstrn_equals(const char *s1, const char *s2, size_t len2)
 {
-	return !(fsopt->mds_namespace &&
-		 (strlen(fsopt->mds_namespace) != len ||
-		  strncmp(fsopt->mds_namespace, namespace, len)));
+	return !strncmp(s1, s2, len2) && strlen(s1) == len2;
 }
 
 static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,
@@ -297,12 +289,13 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 	++fs_name_start; /* start of file system name */
 	len = dev_name_end - fs_name_start;
 
-	if (!namespace_equals(fsopt, fs_name_start, len))
+	if (!fsopt->mds_namespace) {
+		fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
+		if (!fsopt->mds_namespace)
+			return -ENOMEM;
+	} else if (!strstrn_equals(fsopt->mds_namespace, fs_name_start, len)) {
 		return invalfc(fc, "Mismatching mds_namespace");
-	kfree(fsopt->mds_namespace);
-	fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
-	if (!fsopt->mds_namespace)
-		return -ENOMEM;
+	}
 	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
 
 	fsopt->new_dev_syntax = true;
@@ -417,11 +410,12 @@ static int ceph_parse_mount_param(struct fs_context *fc,
 		param->string = NULL;
 		break;
 	case Opt_mds_namespace:
-		if (!namespace_equals(fsopt, param->string, strlen(param->string)))
+		if (!fsopt->mds_namespace) {
+			fsopt->mds_namespace = param->string;
+			param->string = NULL;
+		} else if (strcmp(fsopt->mds_namespace, param->string)) {
 			return invalfc(fc, "Mismatching mds_namespace");
-		kfree(fsopt->mds_namespace);
-		fsopt->mds_namespace = param->string;
-		param->string = NULL;
+		}
 		break;
 	case Opt_recover_session:
 		mode = result.uint_32;
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/3] ceph: refactor mds_namespace comparing

Quoted 23 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

Same logic, slightly less code. Make the following changes easier.

---
  fs/ceph/super.c | 34 ++++++++++++++--------------------
  1 file changed, 14 insertions(+), 20 deletions(-)
fs/ceph/super.c +0 -6
index 3fc48b43cab0..4e1f4031e888 100644
@@ -235,18 +235,10 @@ static void canonicalize_path(char *path)
  	path[j] = '\0';
  }
 
-/*
- * Check if the mds namespace in ceph_mount_options matches
- * the passed in namespace string. First time match (when
- * ->mds_namespace is NULL) is treated specially, since
- * ->mds_namespace needs to be initialized by the caller.
- */

fs/ceph/super.c static void canonicalize_path(char *path) · line 242

Quoted 10 lines
fs/ceph/super.c +3 -5
-static int namespace_equals(struct ceph_mount_options *fsopt,
-			    const char *namespace, size_t len)
+/* check if s1 (null terminated) equals to s2 (with length len2) */
+static int strstrn_equals(const char *s1, const char *s2, size_t len2)
  {
-	return !(fsopt->mds_namespace &&
-		 (strlen(fsopt->mds_namespace) != len ||
-		  strncmp(fsopt->mds_namespace, namespace, len)));
+	return !strncmp(s1, s2, len2) && strlen(s1) == len2;
  }

Could this helper be defined as inline explicitly ?

Quoted 38 lines
fs/ceph/super.c +11 -9
  static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,
@@ -297,12 +289,13 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  	++fs_name_start; /* start of file system name */
  	len = dev_name_end - fs_name_start;
 
-	if (!namespace_equals(fsopt, fs_name_start, len))
+	if (!fsopt->mds_namespace) {
+		fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
+		if (!fsopt->mds_namespace)
+			return -ENOMEM;
+	} else if (!strstrn_equals(fsopt->mds_namespace, fs_name_start, len)) {
  		return invalfc(fc, "Mismatching mds_namespace");
-	kfree(fsopt->mds_namespace);
-	fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
-	if (!fsopt->mds_namespace)
-		return -ENOMEM;
+	}
  	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
 
  	fsopt->new_dev_syntax = true;
@@ -417,11 +410,12 @@ static int ceph_parse_mount_param(struct fs_context *fc,
  		param->string = NULL;
  		break;
  	case Opt_mds_namespace:
-		if (!namespace_equals(fsopt, param->string, strlen(param->string)))
+		if (!fsopt->mds_namespace) {
+			fsopt->mds_namespace = param->string;
+			param->string = NULL;
+		} else if (strcmp(fsopt->mds_namespace, param->string)) {
  			return invalfc(fc, "Mismatching mds_namespace");
-		kfree(fsopt->mds_namespace);
-		fsopt->mds_namespace = param->string;
-		param->string = NULL;
+		}
  		break;
  	case Opt_recover_session:
  		mode = result.uint_32;
#3 lore raw

Re: [PATCH 1/3] ceph: refactor mds_namespace comparing

Quoted 28 lines · Xiubo Li
Quoted 26 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

Same logic, slightly less code.  Make the following changes easier.

---
  fs/ceph/super.c | 34 ++++++++++++++--------------------

  1 file changed, 14 insertions(+), 20 deletions(-)
fs/ceph/super.c +0 -0
index 3fc48b43cab0..4e1f4031e888 100644
@@ -235,18 +235,10 @@ static void canonicalize_path(char *path)
       path[j] = '\0';
   }

  -/*
- * Check if the mds namespace in ceph_mount_options matches
- * the passed in namespace string. First time match (when
- * ->mds_namespace is NULL) is treated specially, since
- * ->mds_namespace needs to be initialized by the caller.
- */

fs/ceph/super.c +2 -2
-static int namespace_equals(struct ceph_mount_options *fsopt,
-                const char *namespace, size_t len)
+/* check if s1 (null terminated) equals to s2 (with length len2) */
+static int strstrn_equals(const char *s1, const char *s2, size_t len2)
Quoted 9 lines
Quoted 6 lines

  {
-    return !(fsopt->mds_namespace &&
-         (strlen(fsopt->mds_namespace) != len || -          strncmp(fsopt->mds_namespace, namespace, len))); +    return !strncmp(s1, s2, len2) && strlen(s1) == len2; }

Could this helper be defined as inline explicitly ?

Please ignore this, I misreaded and it's not in the header file.

Quoted 40 lines
Quoted 40 lines

    static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,

fs/ceph/super.c +0 -0
@@ -297,12 +289,13 @@ static int ceph_parse_new_source(const char 
 *dev_name, const char *dev_name_end,

      ++fs_name_start; /* start of file system name */
      len = dev_name_end - fs_name_start;
  -    if (!namespace_equals(fsopt, fs_name_start, len))
+    if (!fsopt->mds_namespace) {
+        fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
+        if (!fsopt->mds_namespace)
+            return -ENOMEM;
+    } else if (!strstrn_equals(fsopt->mds_namespace, fs_name_start, len)) {
          return invalfc(fc, "Mismatching mds_namespace");
-    kfree(fsopt->mds_namespace);
-    fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL); -    if (!fsopt->mds_namespace)
-        return -ENOMEM;
+    }
      dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace); fsopt->new_dev_syntax = true;

fs/ceph/super.c +0 -0
@@ -417,11 +410,12 @@ static int ceph_parse_mount_param(struct 
 fs_context *fc,
           param->string = NULL;
           break;

      case Opt_mds_namespace:
-        if (!namespace_equals(fsopt, param->string, strlen(param->string)))
+        if (!fsopt->mds_namespace) {
+            fsopt->mds_namespace = param->string;
+            param->string = NULL;
+        } else if (strcmp(fsopt->mds_namespace, param->string)) { return invalfc(fc, "Mismatching mds_namespace"); -        kfree(fsopt->mds_namespace);
-        fsopt->mds_namespace = param->string;
-        param->string = NULL;
+        }
          break;
      case Opt_recover_session:
          mode = result.uint_32;

Reviewed-by
#4 lore raw

Re: [PATCH 1/3] ceph: refactor mds_namespace comparing

Looks good to me.

Quoted 85 lines · Xiubo Li
Quoted 28 lines · Xiubo Li
Quoted 26 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

Same logic, slightly less code. Make the following changes easier.

---
  fs/ceph/super.c | 34 ++++++++++++++--------------------
  1 file changed, 14 insertions(+), 20 deletions(-)
fs/ceph/super.c +2 -7
index 3fc48b43cab0..4e1f4031e888 100644
@@ -235,18 +235,10 @@ static void canonicalize_path(char *path)
      path[j] = '\0';
  }
  -/*
- * Check if the mds namespace in ceph_mount_options matches
- * the passed in namespace string. First time match (when
- * ->mds_namespace is NULL) is treated specially, since
- * ->mds_namespace needs to be initialized by the caller.
- */
-static int namespace_equals(struct ceph_mount_options *fsopt,
-                const char *namespace, size_t len)
+/* check if s1 (null terminated) equals to s2 (with length len2) */
+static int strstrn_equals(const char *s1, const char *s2, size_t len2)
Quoted 9 lines

fs/ceph/super.c static void canonicalize_path(char *path) · line 242

Quoted 6 lines
fs/ceph/super.c +1 -3
  {
-    return !(fsopt->mds_namespace &&
-         (strlen(fsopt->mds_namespace) != len ||
-          strncmp(fsopt->mds_namespace, namespace, len)));
+    return !strncmp(s1, s2, len2) && strlen(s1) == len2;
  }

Could this helper be defined as inline explicitly ?

Please ignore this, I misreaded and it's not in the header file.

Quoted 41 lines
Quoted 41 lines
fs/ceph/super.c +11 -8
    static int ceph_parse_old_source(const char *dev_name, const char
 *dev_name_end,
@@ -297,12 +289,13 @@ static int ceph_parse_new_source(const char
 *dev_name, const char *dev_name_end,
      ++fs_name_start; /* start of file system name */
      len = dev_name_end - fs_name_start;
  -    if (!namespace_equals(fsopt, fs_name_start, len))
+    if (!fsopt->mds_namespace) {
+        fsopt->mds_namespace = kstrndup(fs_name_start, len,
 GFP_KERNEL);
+        if (!fsopt->mds_namespace)
+            return -ENOMEM;
+    } else if (!strstrn_equals(fsopt->mds_namespace, fs_name_start,
 len)) {
          return invalfc(fc, "Mismatching mds_namespace");
-    kfree(fsopt->mds_namespace);
-    fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
-    if (!fsopt->mds_namespace)
-        return -ENOMEM;
+    }
      dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
        fsopt->new_dev_syntax = true;
@@ -417,11 +410,12 @@ static int ceph_parse_mount_param(struct
 fs_context *fc,
          param->string = NULL;
          break;
      case Opt_mds_namespace:
-        if (!namespace_equals(fsopt, param->string,
 strlen(param->string)))
+        if (!fsopt->mds_namespace) {
+            fsopt->mds_namespace = param->string;
+            param->string = NULL;
+        } else if (strcmp(fsopt->mds_namespace, param->string)) {
              return invalfc(fc, "Mismatching mds_namespace");
-        kfree(fsopt->mds_namespace);
-        fsopt->mds_namespace = param->string;
-        param->string = NULL;
+        }
          break;
      case Opt_recover_session:
          mode = result.uint_32;
Signature · 1 line
Milind
author
#5 lore raw

PATCH 2/3 ceph: save name and fsid in mount source

In reply to Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
 fs/ceph/super.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
fs/ceph/super.c +17 -0
index 4e1f4031e888..74636b9383b8 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 	struct ceph_fsid fsid;
 	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
 	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
 	char *fsid_start, *fs_name_start;
 
 	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 
 	if (ceph_parse_fsid(fsid_start, &fsid))
 		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID");
+	}
 
 	++fs_name_start; /* start of file system name */
 	len = dev_name_end - fs_name_start;
@@ -298,6 +305,16 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 	}
 	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
 
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
 	fsopt->new_dev_syntax = true;
 	return 0;
 }
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH 2/3] ceph: save name and fsid in mount source

Quoted 42 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
  fs/ceph/super.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
fs/ceph/super.c +7 -0
index 4e1f4031e888..74636b9383b8 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  	struct ceph_fsid fsid;
  	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
  	char *fsid_start, *fs_name_start;
 
  	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 
  	if (ceph_parse_fsid(fsid_start, &fsid))
  		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID");
+	}
 
  	++fs_name_start; /* start of file system name */
  	len = dev_name_end - fs_name_start;

fs/ceph/super.c static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end, · line 312

Quoted 8 lines
fs/ceph/super.c +5 -0
@@ -298,6 +305,16 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  	}
  	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
 
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;

Shouldn't we kfree the 'copts->mds_namespace' here ?

Quoted 8 lines
fs/ceph/super.c +5 -0
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
  	fsopt->new_dev_syntax = true;
  	return 0;
  }
author
#7 lore raw

Re: [PATCH 2/3] ceph: save name and fsid in mount source

Quoted 42 lines · Xiubo Li
Quoted 40 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
  fs/ceph/super.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
fs/ceph/super.c +7 -0
index 4e1f4031e888..74636b9383b8 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  	struct ceph_fsid fsid;
  	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
  	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
  	char *fsid_start, *fs_name_start;
  	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  	if (ceph_parse_fsid(fsid_start, &fsid))
  		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID");
+	}
  	++fs_name_start; /* start of file system name */
  	len = dev_name_end - fs_name_start;
@@ -298,6 +305,16 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  	}
Quoted 8 lines

fs/ceph/super.c static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end, · line 312

Quoted 6 lines
fs/ceph/super.c +5 -0
  	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;

Shouldn't we kfree the 'copts->mds_namespace' here ?

Seems not necessary. ceph_free_fc() will take care of releasing the whole 'struct ceph_parse_opts_ctx', including 'copts->mds_namespace'. Besides, the mds_namespace may already be set before we parse the source here.

ceph_free_fc() is called from:
put_fs_context
do_new_mount
do_mount

Quoted 9 lines
Quoted 8 lines
fs/ceph/super.c +5 -0
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
  	fsopt->new_dev_syntax = true;
  	return 0;
  }
#8 lore raw

Re: [PATCH 2/3] ceph: save name and fsid in mount source

Quoted 49 lines · 胡玮文
Quoted 40 lines · Xiubo Li
Quoted 39 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
   fs/ceph/super.c | 17 +++++++++++++++++
   1 file changed, 17 insertions(+)
fs/ceph/super.c +7 -0
index 4e1f4031e888..74636b9383b8 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
   	struct ceph_fsid fsid;
   	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
   	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
   	char *fsid_start, *fs_name_start;
   	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
   	if (ceph_parse_fsid(fsid_start, &fsid))
   		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID");
+	}
   	++fs_name_start; /* start of file system name */
   	len = dev_name_end - fs_name_start;
@@ -298,6 +305,16 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
Quoted 8 lines

fs/ceph/super.c static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end, · line 312

Quoted 7 lines
fs/ceph/super.c +5 -0
   	}
   	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;

Shouldn't we kfree the 'copts->mds_namespace' here ?

Quoted 7 lines

Seems not necessary. ceph_free_fc() will take care of releasing the whole 'struct ceph_parse_opts_ctx', including 'copts->mds_namespace'. Besides, the mds_namespace may already be set before we parse the source here.

ceph_free_fc() is called from:
put_fs_context

457 void put_fs_context(struct fs_context *fc) 458 {
459         struct super_block *sb;
460
461         if (fc->root) {
462                 sb = fc->root->d_sb;
463                 dput(fc->root);
464                 fc->root = NULL;
465                 deactivate_super(sb);
466         }
467
468         if (fc->need_free && fc->ops && fc->ops->free) 469                 fc->ops->free(fc);

But are u sure the 'fc->need_free' is correctly set ?

It seems not from my reading if I didn't miss something.

Thanks

- Xiubo

Quoted 11 lines

do_new_mount
do_mount

Quoted 8 lines
Quoted 8 lines
fs/ceph/super.c +5 -0
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
   	fsopt->new_dev_syntax = true;
   	return 0;
   }
author
#9 lore raw

Re: [PATCH 2/3] ceph: save name and fsid in mount source

Quoted 73 lines · Xiubo Li
Quoted 49 lines · 胡玮文
Quoted 40 lines · Xiubo Li
Quoted 39 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
   fs/ceph/super.c | 17 +++++++++++++++++
   1 file changed, 17 insertions(+)
fs/ceph/super.c +7 -0
index 4e1f4031e888..74636b9383b8 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
   	struct ceph_fsid fsid;
   	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
   	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
   	char *fsid_start, *fs_name_start;
   	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
   	if (ceph_parse_fsid(fsid_start, &fsid))
   		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID");
+	}
   	++fs_name_start; /* start of file system name */
   	len = dev_name_end - fs_name_start;
@@ -298,6 +305,16 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
Quoted 8 lines

fs/ceph/super.c static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end, · line 312

Quoted 7 lines
fs/ceph/super.c +5 -0
   	}
   	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;

Shouldn't we kfree the 'copts->mds_namespace' here ?

Quoted 7 lines

Seems not necessary. ceph_free_fc() will take care of releasing the whole 'struct ceph_parse_opts_ctx', including 'copts->mds_namespace'. Besides, the mds_namespace may already be set before we parse the source here.

ceph_free_fc() is called from:
put_fs_context

457 void put_fs_context(struct fs_context *fc) 458 {
459         struct super_block *sb;
460
461         if (fc->root) {
462                 sb = fc->root->d_sb;
463                 dput(fc->root);
464                 fc->root = NULL;
465                 deactivate_super(sb);
466         }
467
468         if (fc->need_free && fc->ops && fc->ops->free) 469                 fc->ops->free(fc);

Quoted 3 lines

But are u sure the 'fc->need_free' is correctly set ?

It seems not from my reading if I didn't miss something.

'fc->need_free' is initialized to true just after init_fs_context() is called, see 'alloc_fs_context()'. And it is only reset to false after calling free().

I've verified with gdb that ceph_free_fc() got called if ceph_parse_new_source() returns an error.

Anyway, if ceph_free_fc() is not invoked, then we are leaking a lot of things, not just mds_namespace. The whole fs_context need to be freed unconditionally after the mount is finished.

Quoted 16 lines

Thanks

- Xiubo

Quoted 11 lines

do_new_mount
do_mount

Quoted 8 lines
Quoted 8 lines
fs/ceph/super.c +5 -0
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
   	fsopt->new_dev_syntax = true;
   	return 0;
   }
#10 lore raw

Re: [PATCH 2/3] ceph: save name and fsid in mount source

Quoted 79 lines · 胡玮文
Quoted 71 lines · Xiubo Li
Quoted 49 lines · 胡玮文
Quoted 40 lines · Xiubo Li
Quoted 39 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
    fs/ceph/super.c | 17 +++++++++++++++++
    1 file changed, 17 insertions(+)
fs/ceph/super.c +7 -0
index 4e1f4031e888..74636b9383b8 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
    	struct ceph_fsid fsid;
    	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
    	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
    	char *fsid_start, *fs_name_start;
    	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
    	if (ceph_parse_fsid(fsid_start, &fsid))
    		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID");
+	}
    	++fs_name_start; /* start of file system name */
    	len = dev_name_end - fs_name_start;
@@ -298,6 +305,16 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
Quoted 8 lines

fs/ceph/super.c static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end, · line 312

Quoted 7 lines
fs/ceph/super.c +5 -0
    	}
    	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;

Shouldn't we kfree the 'copts->mds_namespace' here ?

Quoted 7 lines

Seems not necessary. ceph_free_fc() will take care of releasing the whole 'struct ceph_parse_opts_ctx', including 'copts->mds_namespace'. Besides, the mds_namespace may already be set before we parse the source here.

ceph_free_fc() is called from:
put_fs_context

457 void put_fs_context(struct fs_context *fc) 458 {
459         struct super_block *sb;
460
461         if (fc->root) {
462                 sb = fc->root->d_sb;
463                 dput(fc->root);
464                 fc->root = NULL;
465                 deactivate_super(sb);
466         }
467
468         if (fc->need_free && fc->ops && fc->ops->free) 469                 fc->ops->free(fc);

Quoted 3 lines

But are u sure the 'fc->need_free' is correctly set ?

It seems not from my reading if I didn't miss something.

'fc->need_free' is initialized to true just after init_fs_context() is called, see 'alloc_fs_context()'. And it is only reset to false after calling free().

Quoted 6 lines

I've verified with gdb that ceph_free_fc() got called if ceph_parse_new_source() returns an error.

Anyway, if ceph_free_fc() is not invoked, then we are leaking a lot of things, not just mds_namespace. The whole fs_context need to be freed unconditionally after the mount is finished.

Okay, you are right. I just missed that.

Thanks

Quoted 15 lines
Quoted 15 lines

Thanks

- Xiubo

Quoted 11 lines

do_new_mount
do_mount

Quoted 8 lines
Quoted 8 lines
fs/ceph/super.c +5 -0
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
    	fsopt->new_dev_syntax = true;
    	return 0;
    }
author
#11 lore raw

PATCH 3/3 libceph: reject mismatching name and fsid

In reply to Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
 net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
net/ceph/ceph_common.c +21 -5
index 4c6441536d55..c59c5ccc23a8 100644
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
 		break;
 
 	case Opt_fsid:
-		err = ceph_parse_fsid(param->string, &opt->fsid);
+	{
+		struct ceph_fsid fsid;
+
+		err = ceph_parse_fsid(param->string, &fsid);
 		if (err) {
 			error_plog(&log, "Failed to parse fsid: %d", err);
 			return err;
 		}
-		opt->flags |= CEPH_OPT_FSID;
+
+		if (!(opt->flags & CEPH_OPT_FSID)) {
+			opt->fsid = fsid;
+			opt->flags |= CEPH_OPT_FSID;
+		} else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+			error_plog(&log, "fsid already set to %pU",
+				   &opt->fsid);
+			return -EINVAL;
+		}
 		break;
+	}
 	case Opt_name:
-		kfree(opt->name);
-		opt->name = param->string;
-		param->string = NULL;
+		if (!opt->name) {
+			opt->name = param->string;
+			param->string = NULL;
+		} else if (strcmp(opt->name, param->string)) {
+			error_plog(&log, "name already set to %s", opt->name);
+			return -EINVAL;
+		}
 		break;
 	case Opt_secret:
 		ceph_crypto_key_destroy(opt->key);
Signature · 1 line
2.25.1
#12 lore raw

Re: [PATCH 3/3] libceph: reject mismatching name and fsid

Quoted 16 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
  net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
  1 file changed, 21 insertions(+), 5 deletions(-)
net/ceph/ceph_common.c +0 -0
index 4c6441536d55..c59c5ccc23a8 100644

net/ceph/ceph_common.c int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt, · line 443

Quoted 5 lines
net/ceph/ceph_common.c +1 -1
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
  		break;
 
  	case Opt_fsid:
-		err = ceph_parse_fsid(param->string, &opt->fsid);
+	{

BTW, do we need the '{}' here ?

Quoted 33 lines
net/ceph/ceph_common.c +20 -4
+		struct ceph_fsid fsid;
+
+		err = ceph_parse_fsid(param->string, &fsid);
  		if (err) {
  			error_plog(&log, "Failed to parse fsid: %d", err);
  			return err;
  		}
-		opt->flags |= CEPH_OPT_FSID;
+
+		if (!(opt->flags & CEPH_OPT_FSID)) {
+			opt->fsid = fsid;
+			opt->flags |= CEPH_OPT_FSID;
+		} else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+			error_plog(&log, "fsid already set to %pU",
+				   &opt->fsid);
+			return -EINVAL;
+		}
  		break;
+	}
  	case Opt_name:
-		kfree(opt->name);
-		opt->name = param->string;
-		param->string = NULL;
+		if (!opt->name) {
+			opt->name = param->string;
+			param->string = NULL;
+		} else if (strcmp(opt->name, param->string)) {
+			error_plog(&log, "name already set to %s", opt->name);
+			return -EINVAL;
+		}
  		break;
  	case Opt_secret:
  		ceph_crypto_key_destroy(opt->key);
author
#13 lore raw

Re: [PATCH 3/3] libceph: reject mismatching name and fsid

Quoted 13 lines · Xiubo Li
Quoted 11 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
  net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
  1 file changed, 21 insertions(+), 5 deletions(-)
Quoted 11 lines

net/ceph/ceph_common.c int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt, · line 442

Quoted 9 lines
net/ceph/ceph_common.c +1 -1
index 4c6441536d55..c59c5ccc23a8 100644
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
  		break;
  	case Opt_fsid:
-		err = ceph_parse_fsid(param->string, &opt->fsid);
+	{

BTW, do we need the '{}' here ?

I want to declare 'fsid' variable closer to its usage. But a declaration cannot follow a case label:

error: a label can only be part of a statement and a declaration is not a statement

searching for 'case \w+:\n\s+\{' in the source tree reveals about 1400 such usage. Should be pretty common.

Quoted 34 lines
Quoted 33 lines
net/ceph/ceph_common.c +20 -4
+		struct ceph_fsid fsid;
+
+		err = ceph_parse_fsid(param->string, &fsid);
  		if (err) {
  			error_plog(&log, "Failed to parse fsid: %d", err);
  			return err;
  		}
-		opt->flags |= CEPH_OPT_FSID;
+
+		if (!(opt->flags & CEPH_OPT_FSID)) {
+			opt->fsid = fsid;
+			opt->flags |= CEPH_OPT_FSID;
+		} else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+			error_plog(&log, "fsid already set to %pU",
+				   &opt->fsid);
+			return -EINVAL;
+		}
  		break;
+	}
  	case Opt_name:
-		kfree(opt->name);
-		opt->name = param->string;
-		param->string = NULL;
+		if (!opt->name) {
+			opt->name = param->string;
+			param->string = NULL;
+		} else if (strcmp(opt->name, param->string)) {
+			error_plog(&log, "name already set to %s", opt->name);
+			return -EINVAL;
+		}
  		break;
  	case Opt_secret:
  		ceph_crypto_key_destroy(opt->key);
#14 lore raw

Re: [PATCH 3/3] libceph: reject mismatching name and fsid

Quoted 23 lines · 胡玮文
Quoted 12 lines · Xiubo Li
Quoted 11 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
   net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
   1 file changed, 21 insertions(+), 5 deletions(-)
Quoted 10 lines

net/ceph/ceph_common.c int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt, · line 442

Quoted 9 lines
net/ceph/ceph_common.c +1 -1
index 4c6441536d55..c59c5ccc23a8 100644
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
   		break;
   	case Opt_fsid:
-		err = ceph_parse_fsid(param->string, &opt->fsid);
+	{

BTW, do we need the '{}' here ?

Quoted 7 lines

I want to declare 'fsid' variable closer to its usage. But a declaration cannot follow a case label:

error: a label can only be part of a statement and a declaration is not a statement

searching for 'case \w+:\n\s+\{' in the source tree reveals about 1400 such usage. Should be pretty common.

Did you see this when compiling ? So odd I jsut remove them and it worked for me.

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
net/ceph/ceph_common.c +20 -4
+		struct ceph_fsid fsid;
+
+		err = ceph_parse_fsid(param->string, &fsid);
   		if (err) {
   			error_plog(&log, "Failed to parse fsid: %d", err);
   			return err;
   		}
-		opt->flags |= CEPH_OPT_FSID;
+
+		if (!(opt->flags & CEPH_OPT_FSID)) {
+			opt->fsid = fsid;
+			opt->flags |= CEPH_OPT_FSID;
+		} else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+			error_plog(&log, "fsid already set to %pU",
+				   &opt->fsid);
+			return -EINVAL;
+		}
   		break;
+	}
   	case Opt_name:
-		kfree(opt->name);
-		opt->name = param->string;
-		param->string = NULL;
+		if (!opt->name) {
+			opt->name = param->string;
+			param->string = NULL;
+		} else if (strcmp(opt->name, param->string)) {
+			error_plog(&log, "name already set to %s", opt->name);
+			return -EINVAL;
+		}
   		break;
   	case Opt_secret:
   		ceph_crypto_key_destroy(opt->key);
author
#15 lore raw

Re: [PATCH 3/3] libceph: reject mismatching name and fsid

Quoted 29 lines · Xiubo Li
Quoted 27 lines · 胡玮文
Quoted 12 lines · Xiubo Li
Quoted 11 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
   net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
   1 file changed, 21 insertions(+), 5 deletions(-)
Quoted 10 lines

net/ceph/ceph_common.c int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt, · line 442

Quoted 9 lines
net/ceph/ceph_common.c +1 -1
index 4c6441536d55..c59c5ccc23a8 100644
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
   		break;
   	case Opt_fsid:
-		err = ceph_parse_fsid(param->string, &opt->fsid);
+	{

BTW, do we need the '{}' here ?

I want to declare 'fsid' variable closer to its usage. But a declaration cannot follow a case label:
error: a label can only be part of a statement and a declaration is not a statement

Quoted 5 lines
Quoted 2 lines

searching for 'case \w+:\n\s+\{' in the source tree reveals about 1400 such usage. Should be pretty common.

Did you see this when compiling ? So odd I jsut remove them and it worked for me.

Yes, my compiler is gcc version 9.4.0 from ubuntu 20.04. clangd 16.0.2 also gives error.

Console output:

CC net/ceph/ceph_common.o
net/ceph/ceph_common.c: In function ‘ceph_parse_param’:
net/ceph/ceph_common.c:443:3: error: a label can only be part of a statement and a declaration is not a statement

  443 |   struct ceph_fsid fsid;
      |   ^~~~~~
Quoted 34 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
net/ceph/ceph_common.c +20 -4
+		struct ceph_fsid fsid;
+
+		err = ceph_parse_fsid(param->string, &fsid);
   		if (err) {
   			error_plog(&log, "Failed to parse fsid: %d", err);
   			return err;
   		}
-		opt->flags |= CEPH_OPT_FSID;
+
+		if (!(opt->flags & CEPH_OPT_FSID)) {
+			opt->fsid = fsid;
+			opt->flags |= CEPH_OPT_FSID;
+		} else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+			error_plog(&log, "fsid already set to %pU",
+				   &opt->fsid);
+			return -EINVAL;
+		}
   		break;
+	}
   	case Opt_name:
-		kfree(opt->name);
-		opt->name = param->string;
-		param->string = NULL;
+		if (!opt->name) {
+			opt->name = param->string;
+			param->string = NULL;
+		} else if (strcmp(opt->name, param->string)) {
+			error_plog(&log, "name already set to %s", opt->name);
+			return -EINVAL;
+		}
   		break;
   	case Opt_secret:
   		ceph_crypto_key_destroy(opt->key);
maintainer
#16 lore raw

Re: [PATCH 3/3] libceph: reject mismatching name and fsid

In reply to Hu Weiwen
Quoted 56 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
 net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
net/ceph/ceph_common.c +21 -6
index 4c6441536d55..c59c5ccc23a8 100644
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
                break;
 
        case Opt_fsid:
-               err = ceph_parse_fsid(param->string, &opt->fsid);
+       {
+               struct ceph_fsid fsid;
+
+               err = ceph_parse_fsid(param->string, &fsid);
                if (err) {
                        error_plog(&log, "Failed to parse fsid: %d", err);
                        return err;
                }
-               opt->flags |= CEPH_OPT_FSID;
+
+               if (!(opt->flags & CEPH_OPT_FSID)) {
+                       opt->fsid = fsid;
+                       opt->flags |= CEPH_OPT_FSID;
+               } else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+                       error_plog(&log, "fsid already set to %pU",
+                                  &opt->fsid);
+                       return -EINVAL;
+               }
                break;
+       }
        case Opt_name:
-               kfree(opt->name);
-               opt->name = param->string;
-               param->string = NULL;
+               if (!opt->name) {
+                       opt->name = param->string;
+                       param->string = NULL;
+               } else if (strcmp(opt->name, param->string)) {
+                       error_plog(&log, "name already set to %s", opt->name);
+                       return -EINVAL;
+               }
                break;
        case Opt_secret:
                ceph_crypto_key_destroy(opt->key);
--
Quoted 2 lines

2.25.1

Hi Hu,

I'm not following the reason for singling out "fsid" and "name" in ceph_parse_param() in net/ceph. All options are overridable in the sense that the last setting wins on purpose, this is a long-standing behavior. Allowing "key" or "secret" to be overridden while rejecting the corresponding override for "name" is weird.

If there is a desire to treat "fsid" and "name" specially in CephFS because they are coming from the device spec and aren't considered to be mount options in the usual sense, I would suggest enforcing this behavior in fs/ceph (and only when the device spec syntax is used).

Thanks,

Ilya

author
#17 lore raw

Re: [PATCH 3/3] libceph: reject mismatching name and fsid

Quoted 68 lines · Ilya Dryomov
Quoted 56 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

These are present in the device spec of cephfs. So they should be treated as immutable. Also reject `mount()' calls where options and device spec are inconsistent.

---
 net/ceph/ceph_common.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
net/ceph/ceph_common.c +21 -6
index 4c6441536d55..c59c5ccc23a8 100644
@@ -440,17 +440,33 @@ int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
                break;
 
        case Opt_fsid:
-               err = ceph_parse_fsid(param->string, &opt->fsid);
+       {
+               struct ceph_fsid fsid;
+
+               err = ceph_parse_fsid(param->string, &fsid);
                if (err) {
                        error_plog(&log, "Failed to parse fsid: %d", err);
                        return err;
                }
-               opt->flags |= CEPH_OPT_FSID;
+
+               if (!(opt->flags & CEPH_OPT_FSID)) {
+                       opt->fsid = fsid;
+                       opt->flags |= CEPH_OPT_FSID;
+               } else if (ceph_fsid_compare(&opt->fsid, &fsid)) {
+                       error_plog(&log, "fsid already set to %pU",
+                                  &opt->fsid);
+                       return -EINVAL;
+               }
                break;
+       }
        case Opt_name:
-               kfree(opt->name);
-               opt->name = param->string;
-               param->string = NULL;
+               if (!opt->name) {
+                       opt->name = param->string;
+                       param->string = NULL;
+               } else if (strcmp(opt->name, param->string)) {
+                       error_plog(&log, "name already set to %s", opt->name);
+                       return -EINVAL;
+               }
                break;
        case Opt_secret:
                ceph_crypto_key_destroy(opt->key);
--
Quoted 2 lines

2.25.1

Hi Hu,

I'm not following the reason for singling out "fsid" and "name" in ceph_parse_param() in net/ceph. All options are overridable in the sense that the last setting wins on purpose, this is a long-standing behavior. Allowing "key" or "secret" to be overridden while rejecting the corresponding override for "name" is weird.

Quoted 8 lines

If there is a desire to treat "fsid" and "name" specially in CephFS because they are coming from the device spec and aren't considered to be mount options in the usual sense, I would suggest enforcing this behavior in fs/ceph (and only when the device spec syntax is used).

Thanks,

Ilya

Hi Ilya,

I agree. Thank you for your advise. Please see my patch v2.

Hu Weiwen

#18 lore raw
In reply to Hu Weiwen
Quoted 7 lines · Hu Weiwen

From: Hu Weiwen <[email protected]>

We have name and fsid in the new device spec format, but kernel just discard them. Instead of relying on the mount.ceph helper, we should do this directly in kernel to ensure the options and device spec are consistent. And also avoid any confusion.

Quoted 9 lines

Hu Weiwen (3):
ceph: refactor mds_namespace comparing
ceph: save name and fsid in mount source
libceph: reject mismatching name and fsid

  fs/ceph/super.c        | 51 +++++++++++++++++++++++++-----------------
  net/ceph/ceph_common.c | 26 ++++++++++++++++-----
  2 files changed, 52 insertions(+), 25 deletions(-)

Will apply this patch series to the testing branch and run the test.

Thanks Weiwen

- Xiubo

author
#19 lore raw

v2 cover ceph: account for name and fsid in new device spec

In reply to Hu Weiwen

We have name and fsid in the new device spec format, but kernel just discard them. Instead of relying on the mount.ceph helper, we should do this directly in kernel to ensure the options and device spec are consistent. And also avoid any confusion.

Changes since v1:
* Changed my email address, since I have graduated from university.
* A slightly more efficient `strstrn_equals' implementation.
* Make the mismatching error message more clear.
* As suggested by Ilya, rewrite patch 3. Now it does not touch code in net/ceph, I now allow options to be overridable, and check the device spec is consistent with the final resolved options.
* Added patch 4.

Hu Weiwen (4):
ceph: refactor mds_namespace comparing
ceph: save name and fsid in mount source
ceph: delay parsing of source after all options
ceph: allow mds_namespace to appear multiple times

 fs/ceph/super.c | 68 ++++++++++++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 29 deletions(-)
Signature · 1 line
2.25.1
author
#20 lore raw

PATCH v2 1/4 ceph: refactor mds_namespace comparing

Same logic, slightly less code. Make the following changes easier.

---
 fs/ceph/super.c | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)
fs/ceph/super.c +14 -20
index 3fc48b43cab0..9185ff00bb61 100644
@@ -235,18 +235,10 @@ static void canonicalize_path(char *path)
 	path[j] = '\0';
 }
 
-/*
- * Check if the mds namespace in ceph_mount_options matches
- * the passed in namespace string. First time match (when
- * ->mds_namespace is NULL) is treated specially, since
- * ->mds_namespace needs to be initialized by the caller.
- */
-static int namespace_equals(struct ceph_mount_options *fsopt,
-			    const char *namespace, size_t len)
+/* check if s1 (null terminated) equals to s2 (with length len2) */
+static int strstrn_equals(const char *s1, const char *s2, size_t len2)
 {
-	return !(fsopt->mds_namespace &&
-		 (strlen(fsopt->mds_namespace) != len ||
-		  strncmp(fsopt->mds_namespace, namespace, len)));
+	return !strncmp(s1, s2, len2) && s1[len2] == '\0';
 }
 
 static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,
@@ -297,12 +289,13 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 	++fs_name_start; /* start of file system name */
 	len = dev_name_end - fs_name_start;
 
-	if (!namespace_equals(fsopt, fs_name_start, len))
+	if (!fsopt->mds_namespace) {
+		fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
+		if (!fsopt->mds_namespace)
+			return -ENOMEM;
+	} else if (!strstrn_equals(fsopt->mds_namespace, fs_name_start, len)) {
 		return invalfc(fc, "Mismatching mds_namespace");
-	kfree(fsopt->mds_namespace);
-	fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
-	if (!fsopt->mds_namespace)
-		return -ENOMEM;
+	}
 	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
 
 	fsopt->new_dev_syntax = true;
@@ -417,11 +410,12 @@ static int ceph_parse_mount_param(struct fs_context *fc,
 		param->string = NULL;
 		break;
 	case Opt_mds_namespace:
-		if (!namespace_equals(fsopt, param->string, strlen(param->string)))
+		if (!fsopt->mds_namespace) {
+			fsopt->mds_namespace = param->string;
+			param->string = NULL;
+		} else if (strcmp(fsopt->mds_namespace, param->string)) {
 			return invalfc(fc, "Mismatching mds_namespace");
-		kfree(fsopt->mds_namespace);
-		fsopt->mds_namespace = param->string;
-		param->string = NULL;
+		}
 		break;
 	case Opt_recover_session:
 		mode = result.uint_32;
Signature · 1 line
2.25.1
author
#21 lore raw

PATCH v2 2/4 ceph: save name and fsid in mount source

In reply to Hu Weiwen

We have name and fsid in the new device syntax. It is confusing that the kernel accept these info but do not take them into account when connecting to the cluster.

Although the mount.ceph helper program will extract the name from device spec and pass it as name options, these changes are still useful if we don't have that program installed, or if we want to call `mount()' directly.

---
 fs/ceph/super.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
fs/ceph/super.c +18 -1
index 9185ff00bb61..3d36ee4543ed 100644
@@ -267,6 +267,7 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 	struct ceph_fsid fsid;
 	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
 	struct ceph_mount_options *fsopt = pctx->opts;
+	struct ceph_options *copts = pctx->copts;
 	char *fsid_start, *fs_name_start;
 
 	if (*dev_name_end != '=') {
@@ -285,6 +286,12 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 
 	if (ceph_parse_fsid(fsid_start, &fsid))
 		return invalfc(fc, "Invalid FSID");
+	if (!(copts->flags & CEPH_OPT_FSID)) {
+		copts->fsid = fsid;
+		copts->flags |= CEPH_OPT_FSID;
+	} else if (ceph_fsid_compare(&fsid, &copts->fsid)) {
+		return invalfc(fc, "Mismatching cluster FSID between mount source and options");
+	}
 
 	++fs_name_start; /* start of file system name */
 	len = dev_name_end - fs_name_start;
@@ -294,10 +301,20 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
 		if (!fsopt->mds_namespace)
 			return -ENOMEM;
 	} else if (!strstrn_equals(fsopt->mds_namespace, fs_name_start, len)) {
-		return invalfc(fc, "Mismatching mds_namespace");
+		return invalfc(fc, "Mismatching mds_namespace between mount source and options");
 	}
 	dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
 
+	len = fsid_start - dev_name - 1;
+	if (!copts->name) {
+		copts->name = kstrndup(dev_name, len, GFP_KERNEL);
+		if (!copts->name)
+			return -ENOMEM;
+	} else if (!strstrn_equals(copts->name, dev_name, len)) {
+		return invalfc(fc, "Mismatching cephx name between mount source and options");
+	}
+	dout("cephx name '%s'\n", copts->name);
+
 	fsopt->new_dev_syntax = true;
 	return 0;
 }
Signature · 1 line
2.25.1
author
#22 lore raw

PATCH v2 3/4 ceph: delay parsing of source after all options

In reply to Hu Weiwen

Ensure the name, fsid, and mds_namespace in source are consistent with these in options.

---
 fs/ceph/super.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
fs/ceph/super.c +12 -10
index 3d36ee4543ed..e66867efd811 100644
@@ -149,7 +149,6 @@ enum {
 	Opt_snapdirname,
 	Opt_mds_namespace,
 	Opt_recover_session,
-	Opt_source,
 	Opt_mon_addr,
 	/* string args above */
 	Opt_dirstat,
@@ -202,7 +201,6 @@ static const struct fs_parameter_spec ceph_mount_parameters[] = {
 	fsparam_flag_no ("require_active_mds",		Opt_require_active_mds),
 	fsparam_u32	("rsize",			Opt_rsize),
 	fsparam_string	("snapdirname",			Opt_snapdirname),
-	fsparam_string	("source",			Opt_source),
 	fsparam_string	("mon_addr",			Opt_mon_addr),
 	fsparam_u32	("wsize",			Opt_wsize),
 	fsparam_flag_no	("wsync",			Opt_wsync),
@@ -337,11 +335,11 @@ static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
  *     <server_spec> is <ip>[:<port>]
  *     <path> is optional, but if present must begin with '/'
  */
-static int ceph_parse_source(struct fs_parameter *param, struct fs_context *fc)
+static int ceph_parse_source(struct fs_context *fc)
 {
 	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
 	struct ceph_mount_options *fsopt = pctx->opts;
-	char *dev_name = param->string, *dev_name_end;
+	const char *dev_name = fc->source, *dev_name_end;
 	int ret;
 
 	dout("%s '%s'\n", __func__, dev_name);
@@ -383,8 +381,6 @@ static int ceph_parse_source(struct fs_parameter *param, struct fs_context *fc)
 			return ret;
 	}
 
-	fc->source = param->string;
-	param->string = NULL;
 	return 0;
 }
 
@@ -443,10 +439,6 @@ static int ceph_parse_mount_param(struct fs_context *fc,
 		else
 			BUG();
 		break;
-	case Opt_source:
-		if (fc->source)
-			return invalfc(fc, "Multiple sources specified");
-		return ceph_parse_source(param, fc);
 	case Opt_mon_addr:
 		return ceph_parse_mon_addr(param, fc);
 	case Opt_wsize:
@@ -1220,6 +1212,10 @@ static int ceph_get_tree(struct fs_context *fc)
 
 	if (!fc->source)
 		return invalfc(fc, "No source");
+	err = ceph_parse_source(fc);
+	if (err < 0)
+		return err;
+
 	if (fsopt->new_dev_syntax && !fsopt->mon_addr)
 		return invalfc(fc, "No monitor address");
 
@@ -1301,6 +1297,12 @@ static int ceph_reconfigure_fc(struct fs_context *fc)
 	struct ceph_parse_opts_ctx *pctx = fc->fs_private;
 	struct ceph_mount_options *fsopt = pctx->opts;
 	struct ceph_fs_client *fsc = ceph_sb_to_client(fc->root->d_sb);
+	int err;
+
+	/* validate source and options are still consistent */
+	err = ceph_parse_source(fc);
+	if (err < 0)
+		return err;
 
 	if (fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
 		ceph_set_mount_opt(fsc, ASYNC_DIROPS);
Signature · 1 line
2.25.1
author
#23 lore raw

PATCH v2 4/4 ceph: allow mds_namespace to appear multiple times

In reply to Hu Weiwen

To be consistent with other options. It will still be rejected if its final value is inconsistent with mount source.

---
 fs/ceph/super.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/ceph/super.c +3 -6
index e66867efd811..96aba62fdcba 100644
@@ -423,12 +423,9 @@ static int ceph_parse_mount_param(struct fs_context *fc,
 		param->string = NULL;
 		break;
 	case Opt_mds_namespace:
-		if (!fsopt->mds_namespace) {
-			fsopt->mds_namespace = param->string;
-			param->string = NULL;
-		} else if (strcmp(fsopt->mds_namespace, param->string)) {
-			return invalfc(fc, "Mismatching mds_namespace");
-		}
+		kfree(fsopt->mds_namespace);
+		fsopt->mds_namespace = param->string;
+		param->string = NULL;
 		break;
 	case Opt_recover_session:
 		mode = result.uint_32;
Signature · 1 line
2.25.1