All of lore.kernel.org
 help / color / mirror / Atom feed
From: Artem Bityutskiy <dedekind1@gmail.com>
To: MTD list <linux-mtd@lists.infradead.org>
Cc: Adrian Hunter <adrian.hunter@nokia.com>
Subject: [PATCH 22/27] fs-tests: integck: do not use tests_remount
Date: Wed, 13 Apr 2011 18:19:02 +0300	[thread overview]
Message-ID: <1302707947-6143-23-git-send-email-dedekind1@gmail.com> (raw)
In-Reply-To: <1302707947-6143-1-git-send-email-dedekind1@gmail.com>

From: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

Implement our own version of 'tests_remount()' instead of depending
on the common implementation.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
---
 tests/fs-tests/integrity/integck.c |  167 +++++++++++++++++++++++++++++++++++-
 1 files changed, 165 insertions(+), 2 deletions(-)

diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c
index 7a4bcdc..9bf42af 100644
--- a/tests/fs-tests/integrity/integck.c
+++ b/tests/fs-tests/integrity/integck.c
@@ -67,6 +67,11 @@ static struct {
  * can_mmap: file-system supports share writable 'mmap()' operation
  * is_rootfs: the tested file-system the root file-system
  * fstype: file-system type (e.g., "ubifs")
+ * fsdev: the underlying device mounted by the tested file-system
+ * mount_opts: non-standard mount options of the tested file-system (non-standard
+ *             options are stored in string form as a comma-separated list)
+ * mount_flags: standard mount options of the tested file-system (standard
+ *              options as stored as a set of flags)
  * mount_point: tested file-system mount point path
  * test_dir: the directory on the tested file-system where we test
  */
@@ -78,6 +83,9 @@ static struct {
 	unsigned int can_mmap:1;
 	unsigned int is_rootfs:1;
 	const char *fstype;
+	const char *fsdev;
+	const char *mount_opts;
+	unsigned long mount_flags;
 	const char *mount_point;
 	const char *test_dir;
 } fsinfo = {
@@ -2011,6 +2019,98 @@ static void update_test_data(void)
 		do_an_operation();
 }
 
+/**
+ * Re-mount the test file-system. This function randomly select how to
+ * re-mount.
+ */
+void remount_tested_fs(void)
+{
+	char *wd_save;
+	int ret;
+	unsigned long flags;
+	unsigned int rorw1, um, um_ro, um_rorw, rorw2;
+
+	/* Save current working directory */
+	wd_save = malloc(PATH_MAX + 1);
+	CHECK(wd_save != NULL);
+	CHECK(getcwd(wd_save, PATH_MAX + 1) != NULL);
+
+	/* Temporarily change working directory to '/' */
+	CHECK(chdir("/") == 0);
+
+	/* Choose what to do */
+	rorw1 = rand() & 1;
+	um = rand() & 1;
+	um_ro = rand() & 1;
+	um_rorw = rand() & 1;
+	rorw2 = rand() & 1;
+
+	if (rorw1 + um + rorw2 == 0)
+		um = 1;
+
+	if (rorw1) {
+		flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
+		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
+			    flags, fsinfo.mount_opts);
+		CHECK(ret == 0);
+
+		flags = fsinfo.mount_flags | MS_REMOUNT;
+		flags &= ~((unsigned long)MS_RDONLY);
+		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
+			    flags, fsinfo.mount_opts);
+		CHECK(ret == 0);
+	}
+
+	if (um) {
+		if (um_ro) {
+			flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
+			ret = mount(fsinfo.fsdev, fsinfo.mount_point,
+				    fsinfo.fstype, flags, fsinfo.mount_opts);
+			CHECK(ret == 0);
+		}
+
+		CHECK(umount(fsinfo.mount_point) != -1);
+
+		if (!um_rorw) {
+			ret = mount(fsinfo.fsdev, fsinfo.mount_point,
+				    fsinfo.fstype, fsinfo.mount_flags,
+				    fsinfo.mount_opts);
+			CHECK(ret == 0);
+		} else {
+			ret = mount(fsinfo.fsdev, fsinfo.mount_point,
+				    fsinfo.fstype, fsinfo.mount_flags | MS_RDONLY,
+				    fsinfo.mount_opts);
+			CHECK(ret == 0);
+
+			flags = fsinfo.mount_flags | MS_REMOUNT;
+			flags &= ~((unsigned long)MS_RDONLY);
+			ret = mount(fsinfo.fsdev, fsinfo.mount_point,
+				    fsinfo.fstype, flags, fsinfo.mount_opts);
+			CHECK(ret == 0);
+		}
+	}
+
+	if (rorw2) {
+		flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
+		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
+			    flags, fsinfo.mount_opts);
+		CHECK(ret == 0);
+
+		flags = fsinfo.mount_flags | MS_REMOUNT;
+		flags &= ~((unsigned long)MS_RDONLY);
+		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
+			    flags, fsinfo.mount_opts);
+		CHECK(ret == 0);
+	}
+
+	/* Restore the previous working directory */
+	CHECK(chdir(wd_save) == 0);
+	free(wd_save);
+}
+
+/*
+ * Perform the test. Returns zero on success and -1 on failure.
+ */
 static int integck(void)
 {
 	int64_t rpt;
@@ -2030,7 +2130,7 @@ static int integck(void)
 
 	if (fsinfo.is_rootfs) {
 		close_open_files();
-		tests_remount(); /* Requires root access */
+		remount_tested_fs();
 	}
 
 	/* Check everything */
@@ -2043,7 +2143,7 @@ static int integck(void)
 
 		if (!fsinfo.is_rootfs) {
 			close_open_files();
-			tests_remount(); /* Requires root access */
+			remount_tested_fs();
 		}
 
 		/* Check everything */
@@ -2060,6 +2160,67 @@ static int integck(void)
 }
 
 /*
+ * This is a helper function for 'get_tested_fs_info()'. It parses file-system
+ * mount options string, extracts standard mount options from there, and saves
+ * them in the 'fsinfo.mount_flags' variable, and non-standard mount options
+ * are saved in the 'fsinfo.mount_opts' variable. The reason for this is that
+ * we want to preserve mount options when unmounting the file-system and
+ * mounting it again. This is because we cannot pass standard mount optins
+ * (like sync, ro, etc) as a string to the 'mount()' function, because it
+ * fails. It accepts standard mount options only as flags. And only the
+ * FS-specific mount options are accepted in form of a string.
+ */
+static void parse_mount_options(const char *mount_opts)
+{
+	char *tmp, *opts, *p;
+	const char *opt;
+
+	/*
+	 * We are going to use 'strtok()' which modifies the original string,
+	 * so duplicate it.
+	 */
+	tmp = dup_string(mount_opts);
+	p = opts = calloc(1, strlen(mount_opts) + 1);
+	CHECK(opts != NULL);
+
+	opt = strtok(tmp, ",");
+	while (opt) {
+		if (!strcmp(opt, "rw"))
+			;
+		else if (!strcmp(opt, "ro"))
+			fsinfo.mount_flags |= MS_RDONLY;
+		else if (!strcmp(opt, "dirsync"))
+			fsinfo.mount_flags |= MS_DIRSYNC;
+		else if (!strcmp(opt, "noatime"))
+			fsinfo.mount_flags |= MS_NOATIME;
+		else if (!strcmp(opt, "nodiratime"))
+			fsinfo.mount_flags |= MS_NODIRATIME;
+		else if (!strcmp(opt, "noexec"))
+			fsinfo.mount_flags |= MS_NOEXEC;
+		else if (!strcmp(opt, "nosuid"))
+			fsinfo.mount_flags |= MS_NOSUID;
+		else if (!strcmp(opt, "relatime"))
+			fsinfo.mount_flags |= MS_RELATIME;
+		else if (!strcmp(opt, "sync"))
+			fsinfo.mount_flags |= MS_SYNCHRONOUS;
+		else {
+			int len = strlen(opt);
+
+			if (p != opts)
+				*p++ = ',';
+			memcpy(p, opt, len);
+			p += len;
+			*p = '\0';
+		}
+
+		opt = strtok(NULL, ",");
+	}
+
+	free(tmp);
+	fsinfo.mount_opts = opts;
+}
+
+/*
  * Fill 'fsinfo' with information about the tested file-system.
  */
 static void get_tested_fs_info(void)
@@ -2104,6 +2265,8 @@ static void get_tested_fs_info(void)
         fclose(f);
 
 	fsinfo.fstype = dup_string(mntent->mnt_type);
+	fsinfo.fsdev = strdup(mntent->mnt_fsname);
+	parse_mount_options(mntent->mnt_opts);
 
 	/* Get memory page size for 'mmap()' */
 	fsinfo.page_size = sysconf(_SC_PAGE_SIZE);
-- 
1.7.2.3

  parent reply	other threads:[~2011-04-13 15:16 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-13 15:18 [PATCH 00/27] mtd-utils: make integck test independent Artem Bityutskiy
2011-04-13 15:16 ` Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 01/27] fs-tests: integck: shrink dir_entry_info structure size Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 02/27] fs-tests: integck: shrink file_info " Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 03/27] fs-tests: integck: srink file_info structure even more Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 04/27] fs-tests: integck: abuse random_offset field nicer Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 05/27] fs-tests: integck: shrink write_info even more Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 06/27] fs-tests: integck: change tests defaults Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 07/27] fs-tests: integck: implement own parameters parsing Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 08/27] fs-tests: integck: clean-up copy_string Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 09/27] fs-tests: integck: get rid of tests_check_test_file_system Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 10/27] fs-tests: integck: make integck function return error Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 11/27] fs-tests: integck: move mem_page_size to fsinfo Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 12/27] fs-tests: integck: move more variables " Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 13/27] fs-tests: integck: add own get_free_space function Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 14/27] fs-tests: integck: move log10_initial_free to fsinfo Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 15/27] fs-tests: integck: remove trailing backslashes from mount point Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 16/27] fs-tests: integck: do not use tests_cat_pid Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 17/27] fs-tests: integck: clean up test directory creation Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 18/27] fs-tests: integck: do not use tests_clear_dir Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 19/27] fs-tests: integck: do not use space after cast Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 20/27] fs-tests: integck: prefer to check for success Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 21/27] fs-tests: integck: do not use tests_fs_is_rootfs Artem Bityutskiy
2011-04-13 15:19 ` Artem Bityutskiy [this message]
2011-04-13 15:19 ` [PATCH 23/27] fs-tests: integck: do not use tests_get_total_space Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 24/27] fs-tests: integck: do not use global common variables Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 25/27] fs-tests: integck: do not use tests_random_no Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 26/27] fs-tests: integck: implement own version of CHECK Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 27/27] fs-tests: integck: do not expect max name length to be 256 Artem Bityutskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1302707947-6143-23-git-send-email-dedekind1@gmail.com \
    --to=dedekind1@gmail.com \
    --cc=adrian.hunter@nokia.com \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.