diff options
author | Csókás, Bence <csokas.bence@prolan.hu> | 2024-08-26 13:24:57 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2024-09-25 07:49:28 +0200 |
commit | b14cc35e792107b40715ee3e20e82b3f7db4b068 (patch) | |
tree | 8d8ae670b685ff76d2c24fa11e4ad2c2c62a6646 /tests | |
parent | 481b459304ff461b3e93addadc246949791172bd (diff) |
fs-tests: integck: Refactor: split out common remount logic
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>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/fs-tests/integrity/integck.c | 200 |
1 files changed, 92 insertions, 108 deletions
diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c index 0a7f142..ad076b5 100644 --- a/tests/fs-tests/integrity/integck.c +++ b/tests/fs-tests/integrity/integck.c @@ -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) { @@ -3144,46 +3199,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; |