be81ba
From 9f7fe1b83fcc508dc3e05815d03dbbb752a1cfba Mon Sep 17 00:00:00 2001
be81ba
From: Karel Zak <kzak@redhat.com>
be81ba
Date: Fri, 1 Jun 2018 12:16:19 +0200
be81ba
Subject: [PATCH 63/63] libmount: improve MS_REC usage
be81ba
be81ba
libmount allows to split one library (mount(8)) call to multiple mount(2)
be81ba
syscalls, for example
be81ba
be81ba
   --rbind --make-rslave
be81ba
be81ba
in this case we have to be careful with MS_REC because the flag is
be81ba
applied to multiple operations.
be81ba
be81ba
 # strace -e mount mount --rbind --make-rslave /mnt/A /mnt/B
be81ba
be81ba
Old version:
be81ba
be81ba
 mount("/mnt/A", "/mnt/B", 0x13ecac0, MS_MGC_VAL|MS_BIND, NULL) = 0
be81ba
 mount("none", "/mnt/B", NULL, MS_REC|MS_SLAVE, NULL) = 0
be81ba
be81ba
Fixed version:
be81ba
be81ba
 mount("/mnt/A", "/mnt/B", 0x1f22ac0, MS_MGC_VAL|MS_BIND|MS_REC, NULL) = 0
be81ba
 mount("none", "/mnt/B", NULL, MS_REC|MS_SLAVE, NULL) = 0
be81ba
be81ba
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1946921
be81ba
Upstream: http://github.com/karelzak/util-linux/commit/816773b475900909d42c2c8282a6ac50252cac22
be81ba
Signed-off-by: Karel Zak <kzak@redhat.com>
be81ba
---
be81ba
 libmount/src/context.c       |  5 +++++
be81ba
 libmount/src/context_mount.c | 25 ++++++++++++++++++++++---
be81ba
 libmount/src/optstr.c        |  9 ++++++---
be81ba
 3 files changed, 33 insertions(+), 6 deletions(-)
be81ba
be81ba
diff --git a/libmount/src/context.c b/libmount/src/context.c
be81ba
index e7f1ee934..8e00b75a9 100644
be81ba
--- a/libmount/src/context.c
be81ba
+++ b/libmount/src/context.c
be81ba
@@ -1375,6 +1375,11 @@ struct libmnt_lock *mnt_context_get_lock(struct libmnt_context *cxt)
be81ba
  *
be81ba
  * both of these calls have the same effect.
be81ba
  *
be81ba
+ * Be careful if you want to use MS_REC flag -- in this case the bit is applied
be81ba
+ * to all bind/slave/etc. options. If you want to mix more propadation flags
be81ba
+ * and/or bind, move operations than it's better to specify mount options by
be81ba
+ * strings.
be81ba
+ *
be81ba
  * Returns: 0 on success, negative number in case of error.
be81ba
  */
be81ba
 int mnt_context_set_mflags(struct libmnt_context *cxt, unsigned long flags)
be81ba
diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c
be81ba
index b88e60507..a8e84127c 100644
be81ba
--- a/libmount/src/context_mount.c
be81ba
+++ b/libmount/src/context_mount.c
be81ba
@@ -73,6 +73,7 @@ static int init_propagation(struct libmnt_context *cxt)
be81ba
 	char *opts = (char *) mnt_fs_get_vfs_options(cxt->fs);
be81ba
 	size_t namesz;
be81ba
 	struct libmnt_optmap const *maps[1];
be81ba
+	int rec_count = 0;
be81ba
 
be81ba
 	if (!opts)
be81ba
 		return 0;
be81ba
@@ -86,9 +87,19 @@ static int init_propagation(struct libmnt_context *cxt)
be81ba
 		struct libmnt_addmount *ad;
be81ba
 		int rc;
be81ba
 
be81ba
-		if (!mnt_optmap_get_entry(maps, 1, name, namesz, &ent)
be81ba
-		    || !ent
be81ba
-		    || !(ent->id & MS_PROPAGATION))
be81ba
+		if (!mnt_optmap_get_entry(maps, 1, name, namesz, &ent) || !ent)
be81ba
+			continue;
be81ba
+
be81ba
+		DBG(CXT, ul_debugobj(cxt, " checking %s", ent->name));
be81ba
+
be81ba
+		/* Note that MS_REC may be used for more flags, so we have to keep
be81ba
+		 * track about number of recursive options to keep the MS_REC in the
be81ba
+		 * mountflags if necessary.
be81ba
+		 */
be81ba
+		if (ent->id & MS_REC)
be81ba
+			rec_count++;
be81ba
+
be81ba
+		if (!(ent->id & MS_PROPAGATION))
be81ba
 			continue;
be81ba
 
be81ba
 		ad = mnt_new_addmount();
be81ba
@@ -96,13 +107,21 @@ static int init_propagation(struct libmnt_context *cxt)
be81ba
 			return -ENOMEM;
be81ba
 
be81ba
 		ad->mountflags = ent->id;
be81ba
+		DBG(CXT, ul_debugobj(cxt, " adding extra mount(2) call for %s", ent->name));
be81ba
 		rc = mnt_context_append_additional_mount(cxt, ad);
be81ba
 		if (rc)
be81ba
 			return rc;
be81ba
 
be81ba
+		DBG(CXT, ul_debugobj(cxt, " removing %s from primary mount(2) call", ent->name));
be81ba
 		cxt->mountflags &= ~ent->id;
be81ba
+
be81ba
+		if (ent->id & MS_REC)
be81ba
+			rec_count--;
be81ba
 	}
be81ba
 
be81ba
+	if (rec_count)
be81ba
+		cxt->mountflags |= MS_REC;
be81ba
+
be81ba
 	return 0;
be81ba
 }
be81ba
 
be81ba
diff --git a/libmount/src/optstr.c b/libmount/src/optstr.c
be81ba
index 8248f0dee..cc077ffd9 100644
be81ba
--- a/libmount/src/optstr.c
be81ba
+++ b/libmount/src/optstr.c
be81ba
@@ -800,14 +800,17 @@ int mnt_optstr_apply_flags(char **optstr, unsigned long flags,
be81ba
 					if (rc)
be81ba
 						goto err;
be81ba
 				}
be81ba
-				if (!(ent->mask & MNT_INVERT))
be81ba
+				if (!(ent->mask & MNT_INVERT)) {
be81ba
 					fl &= ~ent->id;
be81ba
+					if (ent->id & MS_REC)
be81ba
+						fl |= MS_REC;
be81ba
+				}
be81ba
 			}
be81ba
 		}
be81ba
 	}
be81ba
 
be81ba
-	/* add missing options */
be81ba
-	if (fl) {
be81ba
+	/* add missing options (but ignore fl if contains MS_REC only) */
be81ba
+	if (fl && fl != MS_REC) {
be81ba
 		const struct libmnt_optmap *ent;
be81ba
 		char *p;
be81ba
 
be81ba
-- 
be81ba
2.31.1
be81ba