diff options
author | Jaegeuk Kim <jaegeuk.kim@samsung.com> | 2013-08-02 17:03:10 +0900 |
---|---|---|
committer | Jaegeuk Kim <jaegeuk.kim@samsung.com> | 2013-08-02 17:06:26 +0900 |
commit | 2c877a862d6d22ff5324e90b726c7d6a26febfb7 (patch) | |
tree | 8aa019f7b9589cb3ec4b7745ae045eef9fff1269 /lib | |
parent | 57baa23a3279a4b9e9df0ab92ee20a2c79b839d8 (diff) | |
download | android_external_f2fs-tools-2c877a862d6d22ff5324e90b726c7d6a26febfb7.tar.gz android_external_f2fs-tools-2c877a862d6d22ff5324e90b726c7d6a26febfb7.tar.bz2 android_external_f2fs-tools-2c877a862d6d22ff5324e90b726c7d6a26febfb7.zip |
libf2fs: check more conditions on mounted filesystem
In the case of lazy umount, "umount -l", some processes are able to use the
file system even if its mountpoint was disconnected.
At this moment, we should not allow mkfs.f2fs.
This patch adds this condition check.
Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libf2fs.c | 60 |
1 files changed, 44 insertions, 16 deletions
diff --git a/lib/libf2fs.c b/lib/libf2fs.c index b02072f..6947425 100644 --- a/lib/libf2fs.c +++ b/lib/libf2fs.c @@ -13,6 +13,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <errno.h> #include <unistd.h> #include <fcntl.h> #include <mntent.h> @@ -365,31 +366,58 @@ void f2fs_init_configuration(struct f2fs_configuration *c) c->device_name = NULL; } -int f2fs_dev_is_mounted(struct f2fs_configuration *c) +static int is_mounted(const char *mpt, const char *device) { FILE *file = NULL; struct mntent *mnt = NULL; - file = setmntent(MOUNTED, "r"); - if (file == NULL) { - /* if failed due to /etc/mtab file not present - try with /proc/mounts */ - file = setmntent("/proc/mounts", "r"); - if (file == NULL) - return 0; - } + file = setmntent(mpt, "r"); + if (file == NULL) + return 0; - while (1) { - mnt = getmntent(file); - if (mnt == NULL) + while ((mnt = getmntent(file)) != NULL) { + if (!strcmp(device, mnt->mnt_fsname)) break; - if (!strcmp(c->device_name, mnt->mnt_fsname)) { - endmntent(file); - MSG(0, "\tError: Not available on mounted device!\n"); + } + endmntent(file); + return mnt ? 1 : 0; +} + +int f2fs_dev_is_umounted(struct f2fs_configuration *c) +{ + struct stat st_buf; + int ret = 0; + + ret = is_mounted(MOUNTED, c->device_name); + if (ret) { + MSG(0, "\tError: Not available on mounted device!\n"); + return -1; + } + + /* + * if failed due to /etc/mtab file not present + * try with /proc/mounts. + */ + ret = is_mounted("/proc/mounts", c->device_name); + if (ret) { + MSG(0, "\tError: Not available on mounted device!\n"); + return -1; + } + + /* + * If f2fs is umounted with -l, the process can still use + * the file system. In this case, we should not format. + */ + if (stat(c->device_name, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) { + int fd = open(c->device_name, O_RDONLY | O_EXCL); + + if (fd >= 0) { + close(fd); + } else if (errno == EBUSY) { + MSG(0, "\tError: In use by the system!\n"); return -1; } } - endmntent(file); return 0; } |