From patchwork Thu Feb 29 07:41:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?6buE5L2z55S3?= X-Patchwork-Id: 13576803 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 5B8B8C54798 for ; Thu, 29 Feb 2024 07:57:56 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1rfbIg-0000kT-7p; Thu, 29 Feb 2024 07:57:54 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rfbIf-0000kN-2Z for linux-f2fs-devel@lists.sourceforge.net; Thu, 29 Feb 2024 07:57:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=e0v+Dw9t4iXrpyyPXEqT7w0KFqKIsPZlbexCTS9gvZY=; b=CE86/jS9vE2uFt2wEDdV7NkKWh O8M7HQzoTIbKgpLVvcUAChZtryg/SWSVdOz4p82iNdc9eFKA0/VZ4rMt5LKC141pIZCFPd+qWYjUX 0LiGEKLnyJdvTbCbWUZAqa15W/vBQfaViyJh18KAtHndolSzCr2CO5ZB+dlt7OUr3bjU=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=e0v+Dw9t4iXrpyyPXEqT7w0KFqKIsPZlbexCTS9gvZY=; b=HXn9jSKbG/y0QTflx4DIDRLBS0 8mqXVXRpDtHE5oTmoQzc2hru7B2MhhccEdDSTyJouMDvlXX6bgw6Zzcr0jHc3Xau5fcZDHTQy0P33 l+Y0P11fdpckIR4TA1X9pkH4gwSqw52prYLentDlm7rMrQ1NSbHXkTPMQh0Y5dyF1Rh0=; Received: from outboundhk.mxmail.xiaomi.com ([207.226.244.122]) by sfi-mx-2.v28.lw.sourceforge.com with esmtp (Exim 4.95) id 1rfbIV-0007Pp-6c for linux-f2fs-devel@lists.sourceforge.net; Thu, 29 Feb 2024 07:57:53 +0000 X-IronPort-AV: E=Sophos;i="6.06,192,1705334400"; d="scan'208";a="104878469" To: , , Date: Thu, 29 Feb 2024 15:41:41 +0800 Message-ID: <20240229074141.66395-1-huangjianan@xiaomi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240229071853.62884-1-huangjianan@xiaomi.com> References: <20240229071853.62884-1-huangjianan@xiaomi.com> MIME-Version: 1.0 X-Originating-IP: [10.237.8.11] X-ClientProxiedBy: bj-mbx11.mioffice.cn (10.237.8.131) To YZ-MBX05.mioffice.cn (10.237.88.125) X-Headers-End: 1rfbIV-0007Pp-6c Subject: [f2fs-dev] [PATCH v2] f2fs-tools: fix to check loop device for non-root users 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: , X-Patchwork-Original-From: Huang Jianan via Linux-f2fs-devel From: =?utf-8?b?6buE5L2z55S3?= Reply-To: Huang Jianan Cc: jnhuang95@gmail.com, wanghui33@xiaomi.com Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net Currently mkfs/fsck gets the following error when executed by non-root users: Info: open /dev/loop0 failed errno:13 Error: Not available on mounted device! Let's fix it by reading the backing file from sysfs. Fixes: 14197d546b93 ("f2fs-tools: fix to check loop device") Signed-off-by: Huang Jianan --- v2: - Remove use of LOOP_GET_STATUS64. lib/libf2fs.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/libf2fs.c b/lib/libf2fs.c index d51e485..92faff9 100644 --- a/lib/libf2fs.c +++ b/lib/libf2fs.c @@ -832,7 +832,7 @@ int f2fs_dev_is_umounted(char *path) } } 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) +#if defined(HAVE_LINUX_MAJOR_H) struct mntent *mnt; struct stat st_loop; FILE *f; @@ -840,8 +840,9 @@ int f2fs_dev_is_umounted(char *path) f = setmntent("/proc/mounts", "r"); while ((mnt = getmntent(f)) != NULL) { - struct loop_info64 loopinfo = {0, }; - int loop_fd, err; + struct stat st_back; + int sysfs_fd, rc; + char buf[PATH_MAX + 1]; if (mnt->mnt_fsname[0] != '/') continue; @@ -852,23 +853,37 @@ int f2fs_dev_is_umounted(char *path) if (major(st_loop.st_rdev) != LOOP_MAJOR) continue; - loop_fd = open(mnt->mnt_fsname, O_RDONLY); - if (loop_fd < 0) { + snprintf(buf, PATH_MAX, + "/sys/dev/block/%d:%d/loop/backing_file", + major(st_loop.st_rdev), minor(st_loop.st_rdev)); + + sysfs_fd = open(buf, O_RDONLY); + if (sysfs_fd < 0) { MSG(0, "Info: open %s failed errno:%d\n", - mnt->mnt_fsname, errno); + buf, 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); + memset(buf, 0, PATH_MAX + 1); + rc = read(sysfs_fd, buf, PATH_MAX); + if (rc < 0) { + MSG(0, "Info: read %s failed errno:%d\n", + buf, errno); + return -1; + } + + if (rc > 0 && *(buf + rc - 1) == '\n') + --rc; + buf[rc] = '\0'; + + if (stat(buf, &st_back) != 0) { + MSG(0, "Info: stat %s failed errno:%d\n", + buf, errno); return -1; } - if (st_buf.st_dev == loopinfo.lo_device && - st_buf.st_ino == loopinfo.lo_inode) { + if (st_buf.st_dev == st_back.st_dev && + st_buf.st_ino == st_back.st_ino) { MSG(0, "\tError: In use by loop device!\n"); return -EBUSY; }