Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] fstrim: get realpath before trim ioctl
@ 2019-04-14  3:31 Wang Shilong
  2019-04-15  9:15 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Wang Shilong @ 2019-04-14  3:31 UTC (permalink / raw)
  To: util-linux; +Cc: Wang Shilong, Andreas Dilger, Shuichi Ihara

From: Wang Shilong <wshilong@ddn.com>

Original motivation is we want to run fstrim command
on Lustre[1] osd server mount point directly, however
our server mount point doesn't export osd directory
to users, and it will cause following command fail:

 $fstrim -v /mnt/mds/

But following succeed:

 $fstrim -v /mnt/mds

We could improve this a bit by getting realpath
before trapping kernel, this also give benifits
to normal use cases.

Cc: Andreas Dilger <adilger@dilger.ca>
Cc: Shuichi Ihara <sihara@ddn.com>
[1] http://wiki.lustre.org/Main_Page
Signed-off-by: Wang Shilong <wshilong@ddn.com>
---
 sys-utils/fstrim.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index 175717114..830556458 100644
--- a/sys-utils/fstrim.c
+++ b/sys-utils/fstrim.c
@@ -67,14 +67,20 @@ struct fstrim_control {
 /* returns: 0 = success, 1 = unsupported, < 0 = error */
 static int fstrim_filesystem(struct fstrim_control *ctl, const char *path, const char *devname)
 {
-	int fd, rc;
+	int fd = -1, rc;
 	struct stat sb;
 	struct fstrim_range range;
+	char *rpath = realpath(path, NULL);
 
+	if (!rpath) {
+		warn(_("cannot get realpath: %s"), path);
+		rc = -errno;
+		goto done;
+	}
 	/* kernel modifies the range */
 	memcpy(&range, &ctl->range, sizeof(range));
 
-	fd = open(path, O_RDONLY);
+	fd = open(rpath, O_RDONLY);
 	if (fd < 0) {
 		warn(_("cannot open %s"), path);
 		rc = -errno;
@@ -129,6 +135,7 @@ static int fstrim_filesystem(struct fstrim_control *ctl, const char *path, const
 done:
 	if (fd >= 0)
 		close(fd);
+	free(rpath);
 	return rc;
 }
 
-- 
2.21.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] fstrim: get realpath before trim ioctl
  2019-04-14  3:31 [PATCH] fstrim: get realpath before trim ioctl Wang Shilong
@ 2019-04-15  9:15 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2019-04-15  9:15 UTC (permalink / raw)
  To: Wang Shilong; +Cc: util-linux, Wang Shilong, Andreas Dilger, Shuichi Ihara

On Sun, Apr 14, 2019 at 12:31:56PM +0900, Wang Shilong wrote:
>  sys-utils/fstrim.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)

Applied, thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-14  3:31 [PATCH] fstrim: get realpath before trim ioctl Wang Shilong
2019-04-15  9:15 ` Karel Zak

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org util-linux@archiver.kernel.org
	public-inbox-index util-linux


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/ public-inbox