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