Skip to contentSkip to conversation

virtiofsd: simplify setup_remount_slave

PATCH In review Peng Tao 4 participants 4 messages Aug 14, 2019

lore
Share & tools
lore

Peng Tao

author
#0 lore raw

PATCH 1/1 virtiofsd: simplify setup_remount_slave

We can just use MS_REC to propagate MS_SLAVE. This would also allow us to close a race window between reading proc mountinfo and calling mount(MS_SLAVE) for each entry, vs. others umounting the mount point in parallel.

---
 contrib/virtiofsd/passthrough_ll.c | 53 ++----------------------------
 1 file changed, 3 insertions(+), 50 deletions(-)
contrib/virtiofsd/passthrough_ll.c +3 -50
index 6662cc237a..d1ce369071 100644
@@ -2032,55 +2032,6 @@ static struct fuse_lowlevel_ops lo_oper = {
         .removemapping  = lo_removemapping,
 };
 
-/* Remount with MS_SLAVE so our mounts don't affect the outside world */
-static void setup_remount_slave(void)
-{
-	gchar *mountinfo = NULL;
-	gchar *line;
-	gchar *nextline;
-
-	if (!g_file_get_contents("/proc/self/mountinfo", &mountinfo, NULL, NULL)) {
-		fprintf(stderr, "unable to read /proc/self/mountinfo\n");
-		exit(EXIT_FAILURE);
-	}
-
-	for (line = mountinfo; line; line = nextline) {
-		gchar **fields = NULL;
-		char *eol;
-
-		nextline = NULL;
-
-		eol = strchr(line, '\n');
-		if (eol) {
-			*eol = '\0';
-			nextline = eol + 1;
-		}
-
-		/*
-		 * The line format is:
-		 * 442 441 253:4 / / rw,relatime shared:1 - xfs /dev/sda1 rw
-		 */
-		fields = g_strsplit(line, " ", -1);
-		if (!fields[0] || !fields[1] || !fields[2] || !fields[3] ||
-		    !fields[4] || !fields[5] || !fields[6]) {
-			goto next; /* parsing failed, skip line */
-		}
-
-		if (!strstr(fields[6], "shared")) {
-			goto next; /* not shared, skip line */
-		}
-
-		if (mount(NULL, fields[4], NULL, MS_SLAVE, NULL) < 0) {
-			err(1, "mount(%s, MS_SLAVE)", fields[4]);
-		}
-
-next:
-		g_strfreev(fields);
-	}
-
-	g_free(mountinfo);
-}
-
 /* This magic is based on lxc's lxc_pivot_root() */
 static void setup_pivot_root(const char *source)
 {
@@ -2135,7 +2086,9 @@ static void setup_mount_namespace(const char *source)
 		err(1, "unshare(CLONE_NEWNS)");
 	}
 
-	setup_remount_slave();
+	if (mount(NULL, "/", NULL, MS_REC|MS_SLAVE, NULL) < 0) {
+		err(1, "mount(/, MS_REC|MS_PRIVATE)");
+	}
 
 	if (mount(source, source, NULL, MS_BIND, NULL) < 0) {
 		err(1, "mount(%s, %s, MS_BIND)", source, source);
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Peng Tao

We can just use MS_REC to propagate MS_SLAVE. This would also allow us to close a race window between reading proc mountinfo and calling mount(MS_SLAVE) for each entry, vs. others umounting the mount point in parallel.

thanks,
-liubo

Quoted 78 lines
---
 contrib/virtiofsd/passthrough_ll.c | 53 ++----------------------------
 1 file changed, 3 insertions(+), 50 deletions(-)
contrib/virtiofsd/passthrough_ll.c +3 -50
index 6662cc237a..d1ce369071 100644
@@ -2032,55 +2032,6 @@ static struct fuse_lowlevel_ops lo_oper = {
         .removemapping  = lo_removemapping,
 };
 
-/* Remount with MS_SLAVE so our mounts don't affect the outside world */
-static void setup_remount_slave(void)
-{
-	gchar *mountinfo = NULL;
-	gchar *line;
-	gchar *nextline;
-
-	if (!g_file_get_contents("/proc/self/mountinfo", &mountinfo, NULL, NULL)) {
-		fprintf(stderr, "unable to read /proc/self/mountinfo\n");
-		exit(EXIT_FAILURE);
-	}
-
-	for (line = mountinfo; line; line = nextline) {
-		gchar **fields = NULL;
-		char *eol;
-
-		nextline = NULL;
-
-		eol = strchr(line, '\n');
-		if (eol) {
-			*eol = '\0';
-			nextline = eol + 1;
-		}
-
-		/*
-		 * The line format is:
-		 * 442 441 253:4 / / rw,relatime shared:1 - xfs /dev/sda1 rw
-		 */
-		fields = g_strsplit(line, " ", -1);
-		if (!fields[0] || !fields[1] || !fields[2] || !fields[3] ||
-		    !fields[4] || !fields[5] || !fields[6]) {
-			goto next; /* parsing failed, skip line */
-		}
-
-		if (!strstr(fields[6], "shared")) {
-			goto next; /* not shared, skip line */
-		}
-
-		if (mount(NULL, fields[4], NULL, MS_SLAVE, NULL) < 0) {
-			err(1, "mount(%s, MS_SLAVE)", fields[4]);
-		}
-
-next:
-		g_strfreev(fields);
-	}
-
-	g_free(mountinfo);
-}
-
 /* This magic is based on lxc's lxc_pivot_root() */
 static void setup_pivot_root(const char *source)
 {
@@ -2135,7 +2086,9 @@ static void setup_mount_namespace(const char *source)
 		err(1, "unshare(CLONE_NEWNS)");
 	}
 
-	setup_remount_slave();
+	if (mount(NULL, "/", NULL, MS_REC|MS_SLAVE, NULL) < 0) {
+		err(1, "mount(/, MS_REC|MS_PRIVATE)");
+	}
 
 	if (mount(source, source, NULL, MS_BIND, NULL) < 0) {
 		err(1, "mount(%s, %s, MS_BIND)", source, source);
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peng Tao
Quoted 9 lines · Peng Tao

We can just use MS_REC to propagate MS_SLAVE. This would also allow us to close a race window between reading proc mountinfo and calling mount(MS_SLAVE) for each entry, vs. others umounting the mount point in parallel.

---
 contrib/virtiofsd/passthrough_ll.c | 53 ++----------------------------
 1 file changed, 3 insertions(+), 50 deletions(-)

Let's try it!

#3 lore raw
Quoted 6 lines · * Stefan Hajnoczi ([email protected]) wrote:
Quoted 5 lines · Peng Tao

We can just use MS_REC to propagate MS_SLAVE. This would also allow us to close a race window between reading proc mountinfo and calling mount(MS_SLAVE) for each entry, vs. others umounting the mount point in parallel.

Quoted 8 lines
Quoted 4 lines
---
 contrib/virtiofsd/passthrough_ll.c | 53 ++----------------------------
 1 file changed, 3 insertions(+), 50 deletions(-)

Let's try it!

OK thanks, merged in my local world

Quoted 4 lines

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK