]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fstrim: get realpath before trim ioctl
authorWang Shilong <wshilong@ddn.com>
Sun, 14 Apr 2019 03:31:56 +0000 (12:31 +0900)
committerKarel Zak <kzak@redhat.com>
Mon, 15 Apr 2019 09:12:55 +0000 (11:12 +0200)
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

index 175717114d10534ec3387cee4ff3a8a77e6b5f3b..830556458a7f392daf1ed49150b0aeaa63177d4e 100644 (file)
@@ -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;
 }