diff mbox series

[2/2] fs-tests: integck: Refactor: split out common remount logic

Message ID 20240815091225.3237097-2-csokas.bence@prolan.hu
State New
Headers show
Series [1/2] fs-tests: integck: Refactor: split out common remount logic | expand

Commit Message

Csókás, Bence Aug. 15, 2024, 9:12 a.m. UTC
remount_tested_fs() and recover_tested_fs() both have
almost the same code for remounting the target FS
RO then RW. Split this sequence into a new
function called remount_ro_rw().

Signed-off-by: Csókás, Bence <csokas.bence@prolan.hu>
---
 tests/fs-tests/integrity/integck.c | 85 ++++++++++++------------------
 1 file changed, 35 insertions(+), 50 deletions(-)

Comments

Zhihao Cheng Aug. 16, 2024, 12:49 p.m. UTC | #1
在 2024/8/15 17:12, Csókás, Bence 写道:
> remount_tested_fs() and recover_tested_fs() both have
> almost the same code for remounting the target FS
> RO then RW. Split this sequence into a new
> function called remount_ro_rw().
> 
> Signed-off-by: Csókás, Bence <csokas.bence@prolan.hu>
> ---
>   tests/fs-tests/integrity/integck.c | 85 ++++++++++++------------------
>   1 file changed, 35 insertions(+), 50 deletions(-)

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> 
> diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c
> index 6ce8c9d..edd09c0 100644
> --- a/tests/fs-tests/integrity/integck.c
> +++ b/tests/fs-tests/integrity/integck.c
> @@ -2668,6 +2668,35 @@ static int umount_and_remount(int mounted, int reatt, int um_rorw)
>   	return 0;
>   }
>   
> +/**
> + * Remount the test file-system RO first, then RW.
> + */
> +static int remount_ro_rw(const char *tries)
> +{
> +	int ret;
> +	unsigned long flags;
> +
> +	flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
> +	ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> +		    flags, fsinfo.mount_opts);
> +	if (ret) {
> +		pcv("cannot remount %s R/O%s", tries, fsinfo.mount_point);
> +		return -1;
> +	}
> +
> +	flags = fsinfo.mount_flags | MS_REMOUNT;
> +	flags &= ~((unsigned long)MS_RDONLY);
> +	ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> +		    flags, fsinfo.mount_opts);
> +	if (ret) {
> +		pcv("remounted %s R/O%s, but cannot re-mount it R/W",
> +		    tries, fsinfo.mount_point);
> +		return -1;
> +	}
> +
> +	return 0;
> +}
> +
>   /**
>    * Re-mount the test file-system. This function randomly select how to
>    * re-mount.
> @@ -2692,24 +2721,9 @@ static int remount_tested_fs(void)
>   		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);
> -		if (ret) {
> -			pcv("cannot remount %s R/O (1)",
> -			    fsinfo.mount_point);
> -			return -1;
> -		}
> -
> -		flags = fsinfo.mount_flags | MS_REMOUNT;
> -		flags &= ~((unsigned long)MS_RDONLY);
> -		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> -			    flags, fsinfo.mount_opts);
> -		if (ret) {
> -			pcv("remounted %s R/O (1), but cannot re-mount it R/W",
> -			    fsinfo.mount_point);
> +		ret = remount_ro_rw(" (1)");
> +		if (ret)
>   			return -1;
> -		}
>   	}
>   
>   	if (um) {
> @@ -2730,23 +2744,9 @@ static int remount_tested_fs(void)
>   	}
>   
>   	if (rorw2) {
> -		flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
> -		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> -			    flags, fsinfo.mount_opts);
> -		if (ret) {
> -			pcv("cannot re-mount %s R/O (3)", fsinfo.mount_point);
> -			return -1;
> -		}
> -
> -		flags = fsinfo.mount_flags | MS_REMOUNT;
> -		flags &= ~((unsigned long)MS_RDONLY);
> -		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> -			    flags, fsinfo.mount_opts);
> -		if (ret) {
> -			pcv("remounted %s R/O (3), but cannot re-mount it back R/W",
> -			     fsinfo.mount_point);
> +		ret = remount_ro_rw(" (3)");
> +		if (ret)
>   			return -1;
> -		}
>   	}
>   
>   	CHECK(chdir(fsinfo.mount_point) == 0);
> @@ -3209,7 +3209,6 @@ static void free_fs_info(struct dir_info *dir)
>   static int recover_tested_fs(void)
>   {
>   	int ret;
> -	unsigned long flags;
>   	unsigned int  um_rorw, rorw2;
>   	struct mntent *mntent;
>   
> @@ -3231,23 +3230,9 @@ static int recover_tested_fs(void)
>   		return -1;
>   
>   	if (rorw2) {
> -		flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
> -		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> -			    flags, fsinfo.mount_opts);
> -		if (ret) {
> -			pcv("cannot re-mount %s R/O", fsinfo.mount_point);
> -			return -1;
> -		}
> -
> -		flags = fsinfo.mount_flags | MS_REMOUNT;
> -		flags &= ~((unsigned long)MS_RDONLY);
> -		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
> -			    flags, fsinfo.mount_opts);
> -		if (ret) {
> -			pcv("remounted %s R/O, but cannot re-mount it back R/W",
> -			     fsinfo.mount_point);
> +		ret = remount_ro_rw("");
> +		if (ret)
>   			return -1;
> -		}
>   	}
>   
>   	return 0;
>
diff mbox series

Patch

diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c
index 6ce8c9d..edd09c0 100644
--- a/tests/fs-tests/integrity/integck.c
+++ b/tests/fs-tests/integrity/integck.c
@@ -2668,6 +2668,35 @@  static int umount_and_remount(int mounted, int reatt, int um_rorw)
 	return 0;
 }
 
+/**
+ * Remount the test file-system RO first, then RW.
+ */
+static int remount_ro_rw(const char *tries)
+{
+	int ret;
+	unsigned long flags;
+
+	flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
+	ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
+		    flags, fsinfo.mount_opts);
+	if (ret) {
+		pcv("cannot remount %s R/O%s", tries, fsinfo.mount_point);
+		return -1;
+	}
+
+	flags = fsinfo.mount_flags | MS_REMOUNT;
+	flags &= ~((unsigned long)MS_RDONLY);
+	ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
+		    flags, fsinfo.mount_opts);
+	if (ret) {
+		pcv("remounted %s R/O%s, but cannot re-mount it R/W",
+		    tries, fsinfo.mount_point);
+		return -1;
+	}
+
+	return 0;
+}
+
 /**
  * Re-mount the test file-system. This function randomly select how to
  * re-mount.
@@ -2692,24 +2721,9 @@  static int remount_tested_fs(void)
 		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);
-		if (ret) {
-			pcv("cannot remount %s R/O (1)",
-			    fsinfo.mount_point);
-			return -1;
-		}
-
-		flags = fsinfo.mount_flags | MS_REMOUNT;
-		flags &= ~((unsigned long)MS_RDONLY);
-		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
-			    flags, fsinfo.mount_opts);
-		if (ret) {
-			pcv("remounted %s R/O (1), but cannot re-mount it R/W",
-			    fsinfo.mount_point);
+		ret = remount_ro_rw(" (1)");
+		if (ret)
 			return -1;
-		}
 	}
 
 	if (um) {
@@ -2730,23 +2744,9 @@  static int remount_tested_fs(void)
 	}
 
 	if (rorw2) {
-		flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
-		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
-			    flags, fsinfo.mount_opts);
-		if (ret) {
-			pcv("cannot re-mount %s R/O (3)", fsinfo.mount_point);
-			return -1;
-		}
-
-		flags = fsinfo.mount_flags | MS_REMOUNT;
-		flags &= ~((unsigned long)MS_RDONLY);
-		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
-			    flags, fsinfo.mount_opts);
-		if (ret) {
-			pcv("remounted %s R/O (3), but cannot re-mount it back R/W",
-			     fsinfo.mount_point);
+		ret = remount_ro_rw(" (3)");
+		if (ret)
 			return -1;
-		}
 	}
 
 	CHECK(chdir(fsinfo.mount_point) == 0);
@@ -3209,7 +3209,6 @@  static void free_fs_info(struct dir_info *dir)
 static int recover_tested_fs(void)
 {
 	int ret;
-	unsigned long flags;
 	unsigned int  um_rorw, rorw2;
 	struct mntent *mntent;
 
@@ -3231,23 +3230,9 @@  static int recover_tested_fs(void)
 		return -1;
 
 	if (rorw2) {
-		flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;
-		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
-			    flags, fsinfo.mount_opts);
-		if (ret) {
-			pcv("cannot re-mount %s R/O", fsinfo.mount_point);
-			return -1;
-		}
-
-		flags = fsinfo.mount_flags | MS_REMOUNT;
-		flags &= ~((unsigned long)MS_RDONLY);
-		ret = mount(fsinfo.fsdev, fsinfo.mount_point, fsinfo.fstype,
-			    flags, fsinfo.mount_opts);
-		if (ret) {
-			pcv("remounted %s R/O, but cannot re-mount it back R/W",
-			     fsinfo.mount_point);
+		ret = remount_ro_rw("");
+		if (ret)
 			return -1;
-		}
 	}
 
 	return 0;