zdtm: check ghost restores on readonly fs if it is also a ghost in other writable bind

Submitted by Pavel Tikhomirov on Jan. 12, 2018, 3:08 p.m.

Details

Message ID 20180112150804.18206-1-ptikhomirov@virtuozzo.com
State New
Series "files: make convert_path_from_another_mp always return relative path"
Headers show

Commit Message

Pavel Tikhomirov Jan. 12, 2018, 3:08 p.m.
It is a test for convert_path_from_another_mp fix, it is a bit tricky as
we don't fully support ghosts on readonly fs, but only if the ghost can
be remaped on some _other_ bindmount (luckily we have same ghost on other
bind). Moreover wrong absolute path generated with old convert_path_from
_another_mp for lnkat don't always fail, only in case we want to do
linkat on mount in _other_ mountns and absolute path makes us do it in
local mountns and local path is readonly and we fail. =)

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 test/zdtm/static/Makefile           |   1 +
 test/zdtm/static/mntns_ghost01.c    | 126 ++++++++++++++++++++++++++++++++++++
 test/zdtm/static/mntns_ghost01.desc |   1 +
 3 files changed, 128 insertions(+)
 create mode 100644 test/zdtm/static/mntns_ghost01.c
 create mode 100644 test/zdtm/static/mntns_ghost01.desc

Patch hide | download patch | download mbox

diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index ff2473125..7b5e4c759 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -317,6 +317,7 @@  TST_DIR		=				\
 		mntns_open			\
 		mntns_link_remap		\
 		mntns_ghost			\
+		mntns_ghost01			\
 		mntns_ro_root			\
 		mntns_link_ghost		\
 		mntns_shared_bind		\
diff --git a/test/zdtm/static/mntns_ghost01.c b/test/zdtm/static/mntns_ghost01.c
new file mode 100644
index 000000000..ebb9af72c
--- /dev/null
+++ b/test/zdtm/static/mntns_ghost01.c
@@ -0,0 +1,126 @@ 
+#include <stdbool.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sched.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <sys/mount.h>
+#include <dirent.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc	= "Check ghost file is restored on readonly fs if it was ghost-remaped on writable bind";
+const char *test_author	= "Pavel Tikhomirov <ptikhomirov@virtuozzo.com>";
+
+char *dirname;
+TEST_OPTION(dirname, string, "directory name", 1);
+
+
+int main(int argc, char **argv)
+{
+	char ghost_path[PATH_MAX];
+	task_waiter_t lock;
+	pid_t pid = -1;
+	int status = 1;
+	int pfd;
+
+	test_init(argc, argv);
+	task_waiter_init(&lock);
+
+	if (mkdir(dirname, 0600) < 0) {
+		pr_perror("mkdir");
+		return 1;
+	}
+
+	snprintf(ghost_path, PATH_MAX, "%s/test.ghost", dirname);
+
+	pfd = open(ghost_path, O_CREAT | O_WRONLY, 0600);
+	if (pfd < 0) {
+		pr_perror("open");
+		return 1;
+	}
+	close(pfd);
+
+	pfd = open(ghost_path, O_RDONLY);
+	if (pfd < 0) {
+		pr_perror("open");
+		return 1;
+	}
+
+	pid = fork();
+	if (pid < 0) {
+		pr_perror("fork");
+		return 1;
+	}
+
+	if (pid == 0) {
+		int fd;
+
+		if (unshare(CLONE_NEWNS)) {
+			pr_perror("unshare");
+			return 1;
+		}
+
+		if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL)) {
+			pr_perror("mount");
+			return 1;
+		}
+
+		if (mount(dirname, dirname, NULL, MS_BIND, NULL)) {
+			pr_perror("mount");
+			return 1;
+		}
+
+		if (mount(NULL, dirname, NULL, MS_RDONLY|MS_REMOUNT|MS_BIND, NULL)) {
+			pr_perror("remount");
+			return 1;
+		}
+
+		fd = open(ghost_path, O_RDONLY);
+		if (fd < 0) {
+			pr_perror("open");
+			return 1;
+		}
+
+		task_waiter_complete(&lock, 1);
+		test_waitsig();
+
+		if (close(fd)) {
+			pr_perror("close");
+			return 1;
+		}
+
+		return 0;
+	}
+
+	task_waiter_wait4(&lock, 1);
+
+	if (unlink(ghost_path)) {
+		pr_perror("unlink");
+		return 1;
+	}
+
+	test_daemon();
+	test_waitsig();
+
+	if (close(pfd)) {
+		pr_perror("close");
+		return 1;
+	}
+
+	kill(pid, SIGTERM);
+	wait(&status);
+	if (status) {
+		fail("Test died");
+		return 1;
+	}
+	pass();
+
+	return 0;
+}
diff --git a/test/zdtm/static/mntns_ghost01.desc b/test/zdtm/static/mntns_ghost01.desc
new file mode 100644
index 000000000..8a1ef8a3d
--- /dev/null
+++ b/test/zdtm/static/mntns_ghost01.desc
@@ -0,0 +1 @@ 
+{'flavor': 'ns uns', 'flags': 'suid', 'feature': 'mnt_id', 'opts': '--link-remap'}