Skip to contentSkip to conversation

xfsprogs: handle symlinks etc in fs_table_initialise_mounts()

PATCH In review Eric Sandeen 3 participants 3 messages Sep 30, 2013 – Oct 18, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfsprogs: handle symlinks etc in fs_table_initialise_mounts()

Commit:

6a23747d xfs_quota: support relative path as `path' arguments

used realpath() on the supplied pathname to handle things like relative pathnames and pathnames ending in "/" which otherwise caused the getmntent scanning to fail.

However, this regressed cases where a path in mtab was a symlink; realpath() resolves this to the target, and so no match is found.

This causes i.e.:

# xfs_quota -x -c report /dev/mapper/testvg-testlv

to fail with:

xfs_quota: cannot setup path for mount /dev/mapper/testvg-testlv: No such device or address

because the scanning looks for /dev/dm-3, but the long symlink name is what exists in mtab, and no match is found.

Fix this, but keep the intended enhancements, by testing *both* the user-specified path (which might be relative, or contain a trailing slash on a mountpoint) and the realpath-resolved path (which turns a relative mountpoint into a full path, and removes trailing slashes), to determine whether the user-specified path is an xfs mountpoint or device.

While we're at it, add a few comments, and go back to the testing of "path" not "rpath"; whether or not path is passed to the function is what determines control flow. If path is specified, and realpath succeeds, we're guaranteed to have rpath as well, so there is no need to retest that. rpath is initialized to NULL, so an unconditional free(rpath) is safe as well.

---

libxcmd/paths.c +28 -16
index bd84cde..7b0e434 100644
@@ -266,6 +266,10 @@ out_nomem:
 	return ENOMEM;
 }
 
+/*
+ * If *path is NULL, initialize the fs table with all xfs mount points in mtab
+ * If *path is specified, search for that path in mtab
+ */
 static int
 fs_table_initialise_mounts(
 	char		*path)
@@ -288,6 +292,7 @@ fs_table_initialise_mounts(
 	if ((mtp = setmntent(mtab_file, "r")) == NULL)
 		return ENOENT;
 
+	/* Use realpath to resolve symlinks, relative paths, etc */
 	if (path)
 		if ((rpath = realpath(path, NULL)) == NULL)
 			return ENOENT;
@@ -295,31 +300,37 @@ fs_table_initialise_mounts(
 	while ((mnt = getmntent(mtp)) != NULL) {
 		if (strcmp(mnt->mnt_type, "xfs") != 0)
 			continue;
-		if (rpath &&
-		    ((strcmp(rpath, mnt->mnt_dir) != 0) &&
+		if (path &&
+		    ((strcmp(path, mnt->mnt_dir) != 0) &&
+		     (strcmp(path, mnt->mnt_fsname) != 0) &&
+		     (strcmp(rpath, mnt->mnt_dir) != 0) &&
 		     (strcmp(rpath, mnt->mnt_fsname) != 0)))
 			continue;
 		if (fs_extract_mount_options(mnt, &fslog, &fsrt))
 			continue;
 		(void) fs_table_insert(mnt->mnt_dir, 0, FS_MOUNT_POINT,
 					mnt->mnt_fsname, fslog, fsrt);
-		if (rpath) {
+		if (path) {
 			found = 1;
 			break;
 		}
 	}
 	endmntent(mtp);
-	if (rpath) {
-		free(rpath);
-		if (!found)
-			error = ENXIO;
-	}
+	free(rpath);
+
+	if (path && !found)
+		error = ENXIO;
+
 	return error;
 }
 
 #elif defined(HAVE_GETMNTINFO)
 #include <sys/mount.h>
 
+/*
+ * If *path is NULL, initialize the fs table with all xfs mount points in mtab
+ * If *path is specified, search for that path in mtab
+ */
 static int
 fs_table_initialise_mounts(
 	char		*path)
@@ -335,6 +346,7 @@ fs_table_initialise_mounts(
 		return 0;
 	}
 
+	/* Use realpath to resolve symlinks, relative paths, etc */
 	if (path)
 		if ((rpath = realpath(path, NULL)) == NULL)
 			return ENOENT;
@@ -342,24 +354,24 @@ fs_table_initialise_mounts(
 	for (i = 0; i < count; i++) {
 		if (strcmp(stats[i].f_fstypename, "xfs") != 0)
 			continue;
-		if (rpath &&
-		    ((strcmp(rpath, stats[i].f_mntonname) != 0) &&
+		if (path &&
+		    ((strcmp(path, stats[i].f_mntonname) != 0) &&
+		     (strcmp(path, stats[i].f_mntfromname) != 0) &&
+		     (strcmp(rpath, stats[i].f_mntonname) != 0) &&
 		     (strcmp(rpath, stats[i].f_mntfromname) != 0)))
 			continue;
 		/* TODO: external log and realtime device? */
 		(void) fs_table_insert(stats[i].f_mntonname, 0,
 					FS_MOUNT_POINT, stats[i].f_mntfromname,
 					NULL, NULL);
-		if (rpath) {
+		if (path) {
 			found = 1;
 			break;
 		}
 	}
-	if (rpath) {
-		free(rpath);
-		if (!found)
-			error = ENXIO;
-	}
+	free(rpath);
+	if (path && !found)
+		error = ENXIO;
 
 	return error;
 }

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Eric Sandeen

Commit:

6a23747d xfs_quota: support relative path as `path' arguments

used realpath() on the supplied pathname to handle things like relative pathnames and pathnames ending in "/" which otherwise caused the getmntent scanning to fail.

However, this regressed cases where a path in mtab was a symlink; realpath() resolves this to the target, and so no match is found.

This causes i.e.:

# xfs_quota -x -c report /dev/mapper/testvg-testlv

to fail with:

xfs_quota: cannot setup path for mount /dev/mapper/testvg-testlv: No such device or address

because the scanning looks for /dev/dm-3, but the long symlink name is what exists in mtab, and no match is found.

Fix this, but keep the intended enhancements, by testing *both* the user-specified path (which might be relative, or contain a trailing slash on a mountpoint) and the realpath-resolved path (which turns a relative mountpoint into a full path, and removes trailing slashes), to determine whether the user-specified path is an xfs mountpoint or device.

While we're at it, add a few comments, and go back to the testing of "path" not "rpath"; whether or not path is passed to the function is what determines control flow. If path is specified, and realpath succeeds, we're guaranteed to have rpath as well, so there is no need to retest that. rpath is initialized to NULL, so an unconditional free(rpath) is safe as well.

Quoted 2 lines

---

Looks good. There's a bunch of minor cleanups that could be done to this code, but save that for a rainy day...

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs