From patchwork Wed Jan 31 07:34:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Yu X-Patchwork-Id: 13538854 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D8272C47258 for ; Wed, 31 Jan 2024 07:34:48 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1rV57P-0000Iq-5T; Wed, 31 Jan 2024 07:34:48 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rV57O-0000Ij-4s for linux-f2fs-devel@lists.sourceforge.net; Wed, 31 Jan 2024 07:34:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Pel17dfBgXLm0h8o3fm8ADIyoEy0CM2Yup/8T5oCA9c=; b=AtdTM2kz7OM6CxvJ6zhQg5erAm oXYNZmtdYhmhpglX1uYotoMuoaqB904lpYC91wiMKZfvIv17TAeE2jCH3qnstg7gxDBp6wlV0ZMHi 2HS4B4Bteclqyne54b1CYPRLRQZZwioKPl0XP1/exdGNPNSpaAPfny8DebS5yuJQwVxk=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=Pel17dfBgXLm0h8o3fm8ADIyoEy0CM2Yup/8T5oCA9c=; b=L 1iy7E0BOSOJInyq5d8DR7FhA0Ydfb265Q/8vElg7Uq0vSrSEIvSqLU5CEmXQEutawpJALhdS4wyJX HyXU/u5Kq31913YJiDZ+jXDKOlq3BjXiL9fa+dW6mF6YSUoEswSEp7D9MbgMSYoTQSK7/oBcj6Pjj cSxdTbkxxZ5Bf5jo=; Received: from sin.source.kernel.org ([145.40.73.55]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1rV57N-0003fU-9j for linux-f2fs-devel@lists.sourceforge.net; Wed, 31 Jan 2024 07:34:47 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 38B32CE16C5 for ; Wed, 31 Jan 2024 07:34:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56D58C433C7; Wed, 31 Jan 2024 07:34:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706686473; bh=5WEPBicbrC9l7gCU5jpmXSo2vNMolaQFK60+OOWxapA=; h=From:To:Cc:Subject:Date:From; b=TOg0Viy/sHa1mcyhcZNMkOemGAWSAncXq/QBrwzSfTm0JC84zS5pVphUIKUWxZ8+b tIZBKpHvc2o5rf+E8Z5HCfA5jhvbepCP5/ZG6DsyQWvdMl9ruoEpABYyJKURMBo6wL hZ+mVVZPjPJeAiAuzLW0JYN9aw0CIqJxGRr7iBfyDGP8GBCLcovfLUiPuRnnQ7g++K O3RwKXDS30e2FAv7zAXsnTC5HKvU7cejvSq5a+qyKyUIkErHLJ9zjisutrPkmEiVVd R650nQOHAdE2LMMv7aOh/TGT/om2B/KwboMV9vVKoCAZTxJ9le70d2vYkvI62n0zr8 dI6RcZV6mgW2A== From: Chao Yu To: jaegeuk@kernel.org Date: Wed, 31 Jan 2024 15:34:25 +0800 Message-Id: <20240131073425.4043962-1-chao@kernel.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Headers-End: 1rV57N-0003fU-9j Subject: [f2fs-dev] [PATCH] f2fs-tools: fix to check loop device X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-f2fs-devel@lists.sourceforge.net Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net Otherwise, mkfs/fsck can update backfile of moutned loop device. 1. mkfs.f2fs image 2. mount -o loop image /mnt/f2fs/ 3. mkfs.f2fs image -f Before: ... Info: format successful After: ... Error: In use by loop device! Signed-off-by: Chao Yu --- configure.ac | 2 ++ fsck/main.c | 4 +-- include/android_config.h | 2 ++ lib/libf2fs.c | 74 ++++++++++++++++++++++++++++++++++------ mkfs/f2fs_format_main.c | 7 ++-- 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/configure.ac b/configure.ac index 123ddbb..21c6391 100644 --- a/configure.ac +++ b/configure.ac @@ -144,6 +144,8 @@ AC_CHECK_HEADERS(m4_flatten([ linux/fs.h linux/hdreg.h linux/limits.h + linux/loop.h + linux/major.h linux/posix_acl.h linux/types.h linux/xattr.h diff --git a/fsck/main.c b/fsck/main.c index 1affa72..c4d0956 100644 --- a/fsck/main.c +++ b/fsck/main.c @@ -1111,8 +1111,8 @@ int main(int argc, char **argv) f2fs_parse_options(argc, argv); - if (c.func != DUMP && f2fs_devs_are_umounted() < 0) { - if (errno == EBUSY) { + if (c.func != DUMP && (ret = f2fs_devs_are_umounted()) < 0) { + if (ret == -EBUSY) { ret = -1; if (c.func == FSCK) ret = FSCK_OPERATIONAL_ERROR; diff --git a/include/android_config.h b/include/android_config.h index da8abcb..05b686e 100644 --- a/include/android_config.h +++ b/include/android_config.h @@ -6,6 +6,8 @@ #define HAVE_FSYNC 1 #define HAVE_LINUX_HDREG_H 1 #define HAVE_LINUX_LIMITS_H 1 +#define HAVE_LINUX_LOOP_H 1 +#define HAVE_LINUX_MAJOR_H 1 #define HAVE_POSIX_ACL_H 1 #define HAVE_LINUX_TYPES_H 1 #define HAVE_LINUX_XATTR_H 1 diff --git a/lib/libf2fs.c b/lib/libf2fs.c index 2451201..8570689 100644 --- a/lib/libf2fs.c +++ b/lib/libf2fs.c @@ -19,6 +19,12 @@ #endif #include #include +#ifdef HAVE_LINUX_LOOP_H +#include +#ifdef HAVE_LINUX_MAJOR_H +#include +#endif +#endif #ifdef HAVE_SYS_IOCTL_H #include #endif @@ -756,7 +762,7 @@ int f2fs_dev_is_umounted(char *path) #ifdef _WIN32 return 0; #else - struct stat *st_buf; + struct stat st_buf; int is_rootdev = 0; int ret = 0; char *rootdev_name = get_rootdev(); @@ -807,32 +813,78 @@ int f2fs_dev_is_umounted(char *path) * If f2fs is umounted with -l, the process can still use * the file system. In this case, we should not format. */ - st_buf = malloc(sizeof(struct stat)); - ASSERT(st_buf); + if (stat(path, &st_buf)) { + MSG(0, "Info: stat failed errno:%d\n", errno); + return -1; + } - if (stat(path, st_buf) == 0 && S_ISBLK(st_buf->st_mode)) { + if (S_ISBLK(st_buf.st_mode)) { int fd = open(path, O_RDONLY | O_EXCL); if (fd >= 0) { close(fd); } else if (errno == EBUSY) { MSG(0, "\tError: In use by the system!\n"); - free(st_buf); - return -1; + return -EBUSY; } + } else if (S_ISREG(st_buf.st_mode)) { + /* check whether regular is backfile of loop device */ +#if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) + struct mntent *mnt; + struct stat st_loop; + FILE *f; + + f = setmntent("/proc/mounts", "r"); + + while ((mnt = getmntent(f)) != NULL) { + struct loop_info64 loopinfo = {0, }; + int loop_fd, err; + + if (mnt->mnt_fsname[0] != '/') + continue; + if (stat(mnt->mnt_fsname, &st_loop) != 0) + continue; + if (!S_ISBLK(st_loop.st_mode)) + continue; + if (major(st_loop.st_rdev) != LOOP_MAJOR) + continue; + + loop_fd = open(mnt->mnt_fsname, O_RDONLY); + if (loop_fd < 0) { + MSG(0, "Info: open %s failed errno:%d\n", + mnt->mnt_fsname, errno); + return -1; + } + + err = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo); + close(loop_fd); + if (err < 0) { + MSG(0, "\tError: ioctl LOOP_GET_STATUS64 failed errno:%d!\n", + errno); + return -1; + } + + if (st_buf.st_dev == loopinfo.lo_device && + st_buf.st_ino == loopinfo.lo_inode) { + MSG(0, "\tError: In use by loop device!\n"); + return -EBUSY; + } + } +#endif } - free(st_buf); return ret; #endif } int f2fs_devs_are_umounted(void) { - int i; + int ret, i; - for (i = 0; i < c.ndevs; i++) - if (f2fs_dev_is_umounted((char *)c.devices[i].path)) - return -1; + for (i = 0; i < c.ndevs; i++) { + ret = f2fs_dev_is_umounted((char *)c.devices[i].path); + if (ret) + return ret; + } return 0; } diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c index 4de517a..f3316f7 100644 --- a/mkfs/f2fs_format_main.c +++ b/mkfs/f2fs_format_main.c @@ -443,6 +443,8 @@ static int f2fs_check_overwrite(void) int main(int argc, char *argv[]) { + int ret; + f2fs_init_configuration(); f2fs_parse_options(argc, argv); @@ -451,8 +453,9 @@ int main(int argc, char *argv[]) c.func = MKFS; - if (f2fs_devs_are_umounted() < 0) { - if (errno != EBUSY) + ret = f2fs_devs_are_umounted(); + if (ret) { + if (ret != -EBUSY) MSG(0, "\tError: Not available on mounted device!\n"); goto err_format; }