]> www.infradead.org Git - mtd-utils.git/commitdiff
fs-tests: integck: Refactor: split out common remount logic
authorCsókás, Bence <csokas.bence@prolan.hu>
Mon, 26 Aug 2024 11:24:57 +0000 (13:24 +0200)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Wed, 25 Sep 2024 05:49:28 +0000 (07:49 +0200)
remount_tested_fs() and recover_tested_fs() both have
almost the same code for umount'ing the target FS and
mount'ing it back. Split this sequence into a new
function called umount_and_remount().

Signed-off-by: Csókás, Bence <csokas.bence@prolan.hu>
Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
tests/fs-tests/integrity/integck.c

index 0a7f142e8c41b8f9804f221cd01aeffc5e680448..ad076b5ad5788a6f7f0359fb8a3982e14d277c57 100644 (file)
@@ -2577,6 +2577,93 @@ static int rm_minus_rf_dir(const char *dir_name)
        return 0;
 }
 
+/*
+ * Detach the MTD device from UBI and attach it back. This function is used
+ * whed performing emulated power cut testing andthe power cuts are amulated by
+ * UBI, not by UBIFS. In this case, to recover from the emulated power cut we
+ * have to unmount UBIFS and re-attach the MTD device.
+ */
+static int reattach(void)
+{
+       int err = 0;
+       libubi_t libubi;
+       struct ubi_attach_request req;
+
+       libubi = libubi_open();
+       if (!libubi) {
+               if (errno == 0)
+                       return errmsg("UBI is not present in the system");
+               return sys_errmsg("cannot open libubi");
+       }
+
+       err = ubi_detach_mtd(libubi, "/dev/ubi_ctrl", args.mtdn);
+       if (err) {
+               sys_errmsg("cannot detach mtd%d", args.mtdn);
+               goto out;
+       }
+
+       req.dev_num = UBI_DEV_NUM_AUTO;
+       req.mtd_num = args.mtdn;
+       req.vid_hdr_offset = 0;
+       req.mtd_dev_node = NULL;
+       req.max_beb_per1024 = 0;
+
+       err = ubi_attach(libubi, "/dev/ubi_ctrl", &req);
+       if (err)
+               sys_errmsg("cannot attach mtd%d", args.mtdn);
+
+out:
+       libubi_close(libubi);
+       return err;
+}
+
+/**
+ * Unmount and mount back the test file-system.
+ */
+static int umount_and_remount(int mounted, int reatt, int um_rorw)
+{
+       int ret = 0;
+       unsigned long flags;
+
+       if (mounted)
+               CHECK(umount(fsinfo.mount_point) != -1);
+
+       if (reatt)
+               CHECK(reattach() == 0);
+
+       if (!um_rorw) {
+               ret = mount(fsinfo.fsdev, fsinfo.mount_point,
+                           fsinfo.fstype, fsinfo.mount_flags,
+                           fsinfo.mount_opts);
+               if (ret) {
+                       pcv("unmounted %s, but cannot mount it back R/W",
+                           fsinfo.mount_point);
+                       return -1;
+               }
+       } else {
+               ret = mount(fsinfo.fsdev, fsinfo.mount_point,
+                           fsinfo.fstype, fsinfo.mount_flags | MS_RDONLY,
+                           fsinfo.mount_opts);
+               if (ret) {
+                       pcv("unmounted %s, but cannot mount it back 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("unmounted %s, mounted R/O, but cannot re-mount it R/W",
+                            fsinfo.mount_point);
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
 /**
  * Re-mount the test file-system. This function randomly select how to
  * re-mount.
@@ -2633,41 +2720,9 @@ static int remount_tested_fs(void)
                        }
                }
 
-               ret = umount(fsinfo.mount_point);
-               if (ret) {
-                       pcv("cannot unmount %s", fsinfo.mount_point);
+               ret = umount_and_remount(1, 0, um_rorw);
+               if (ret)
                        return -1;
-               }
-
-               if (!um_rorw) {
-                       ret = mount(fsinfo.fsdev, fsinfo.mount_point,
-                                   fsinfo.fstype, fsinfo.mount_flags,
-                                   fsinfo.mount_opts);
-                       if (ret) {
-                               pcv("unmounted %s, but cannot mount it back R/W",
-                                   fsinfo.mount_point);
-                               return -1;
-                       }
-               } else {
-                       ret = mount(fsinfo.fsdev, fsinfo.mount_point,
-                                   fsinfo.fstype, fsinfo.mount_flags | MS_RDONLY,
-                                   fsinfo.mount_opts);
-                       if (ret) {
-                               pcv("unmounted %s, but cannot mount it back 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("unmounted %s, mounted R/O, but cannot re-mount it R/W",
-                                    fsinfo.mount_point);
-                               return -1;
-                       }
-               }
        }
 
        if (rorw2) {
@@ -3143,46 +3198,6 @@ static void free_fs_info(struct dir_info *dir)
        }
 }
 
-/*
- * Detach the MTD device from UBI and attach it back. This function is used
- * whed performing emulated power cut testing andthe power cuts are amulated by
- * UBI, not by UBIFS. In this case, to recover from the emulated power cut we
- * have to unmount UBIFS and re-attach the MTD device.
- */
-static int reattach(void)
-{
-       int err = 0;
-       libubi_t libubi;
-       struct ubi_attach_request req;
-
-       libubi = libubi_open();
-       if (!libubi) {
-               if (errno == 0)
-                       return errmsg("UBI is not present in the system");
-               return sys_errmsg("cannot open libubi");
-       }
-
-       err = ubi_detach_mtd(libubi, "/dev/ubi_ctrl", args.mtdn);
-       if (err) {
-               sys_errmsg("cannot detach mtd%d", args.mtdn);
-               goto out;
-       }
-
-       req.dev_num = UBI_DEV_NUM_AUTO;
-       req.mtd_num = args.mtdn;
-       req.vid_hdr_offset = 0;
-       req.mtd_dev_node = NULL;
-       req.max_beb_per1024 = 0;
-
-       err = ubi_attach(libubi, "/dev/ubi_ctrl", &req);
-       if (err)
-               sys_errmsg("cannot attach mtd%d", args.mtdn);
-
-out:
-       libubi_close(libubi);
-       return err;
-}
-
 /*
  * Recover the tested file-system from an emulated power cut failure by
  * unmounting it and mounting it again.
@@ -3206,41 +3221,10 @@ static int recover_tested_fs(void)
         * while mounting in 'remount_tested_fs()'.
         */
        mntent = get_tested_fs_mntent();
-       if (mntent)
-               CHECK(umount(fsinfo.mount_point) != -1);
-
-       if (args.reattach)
-               CHECK(reattach() == 0);
-
-       if (!um_rorw) {
-               ret = mount(fsinfo.fsdev, fsinfo.mount_point,
-                           fsinfo.fstype, fsinfo.mount_flags,
-                           fsinfo.mount_opts);
-               if (ret) {
-                       pcv("unmounted %s, but cannot mount it back R/W",
-                           fsinfo.mount_point);
-                       return -1;
-               }
-       } else {
-               ret = mount(fsinfo.fsdev, fsinfo.mount_point,
-                           fsinfo.fstype, fsinfo.mount_flags | MS_RDONLY,
-                           fsinfo.mount_opts);
-               if (ret) {
-                       pcv("unmounted %s, but cannot mount it back 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("unmounted %s, mounted R/O, but cannot re-mount it R/W",
-                            fsinfo.mount_point);
-                       return -1;
-               }
-       }
+       ret = umount_and_remount(!!mntent, args.reattach, um_rorw);
+       if (ret)
+               return -1;
 
        if (rorw2) {
                flags = fsinfo.mount_flags | MS_RDONLY | MS_REMOUNT;