From: Malahal Naineni <malahal@us.ibm.com>
To: linux-nfs@vger.kernel.org
Subject: [PATCH] Get normalized paths for comparing NFS export paths
Date: Tue, 7 Feb 2012 14:44:01 -0600	[thread overview]
Message-ID: <20120207204401.GA31752@us.ibm.com> (raw)
In-Reply-To: <4F2E620F.5090100@panasas.com>
NFSv4 gladly accepts and mounts "hostname:path" instead of
"hostname:/path". NFS also accepts other forms for pathname, but it
doesn't use the exact pathname string used for generating /proc/mounts.
This causes mount entry mistmatch between /etc/mtab and /proc/mounts
files. The former will have the exact given pathname string but the
latter will have a modified path name.
Signed-off-by: Malahal Naineni <malahal@us.ibm.com>
---
 utils/mount/nfsumount.c |   94 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 83 insertions(+), 11 deletions(-)
diff --git a/utils/mount/nfsumount.c b/utils/mount/nfsumount.c
index 3538d88..54542a5 100644
--- a/utils/mount/nfsumount.c
+++ b/utils/mount/nfsumount.c
@@ -139,6 +139,88 @@ static int del_mtab(const char *spec, const char *node)
 	return EX_FILEIO;
 }
 
+/* 
+ * Return normalized path.
+ *
+ * Resolve "." and ".." components. Replace multiple slashes with one
+ * slash. realpath is close but doesn't work for us as the path won't
+ * exist on the client.
+ *
+ * The return string must be freed by the caller.
+ */
+static char *normpath(const char *path)
+{
+	const char *ptr, *next, *end;
+	char *norm; /* result */
+
+	if (!path)
+		return NULL;
+
+	norm = malloc(strlen(path)+1);
+	if (!norm)
+		return NULL;
+		
+	end = path+strlen(path);
+	*norm = '\0'; /* Make it a NULL string */
+	for (ptr = path; ptr < end; ptr = next+1) {
+		next = strchr(ptr, '/');
+		if (!next)
+			next = end;
+		int pclen = next - ptr; /* path component length */
+		if (strncmp(ptr, ".", pclen) == 0)
+			continue;
+		if (strncmp(ptr, "..", pclen) == 0) {
+			char *tmp = strrchr(norm, '/');
+			if (tmp)
+				*tmp = '\0';
+			continue;
+		}
+
+		/* Add the new component */
+		strncat(norm, "/", 1);
+		strncat(norm, ptr, pclen);
+	}
+
+	/*
+	 * If there was no component to copy, norm would be null.
+	 * Return "/" in that case
+	 */
+	if (*norm == '\0')
+		strcpy(norm, "/");
+
+	return norm;
+}
+
+/*
+ * Detect if the given two entries refer to the same mount entry.
+ * Usually one entry is from /etc/mtab and the other is from
+ * /proc/mounts.
+ */
+static int nfs_same_mount_entry(const struct mntentchn *mc1,
+				const struct mntentchn *mc2)
+{
+	char *host1, *host2;
+	char *path1, *path2;
+	char *norm1, *norm2;
+	int retval;
+
+	nfs_parse_devname(mc1->m.mnt_fsname, &host1, &path1);
+	nfs_parse_devname(mc2->m.mnt_fsname, &host2, &path2);
+	norm1 = normpath(path1);
+	norm2 = normpath(path2);
+
+	retval = strcmp(host1, host2) == 0 && strcmp(norm1, norm2) == 0;
+
+	free(host1);
+	free(host2);
+	free(path1);
+	free(path2);
+	free(norm1);
+	free(norm2);
+
+	return retval;
+}
+
 /*
  * Detect NFSv4 mounts.
  *
@@ -161,17 +243,7 @@ static int nfs_umount_is_vers4(const struct mntentchn *mc)
 		goto not_found;
 
 	do {
-		size_t nlen = strlen(pmc->m.mnt_fsname);
-
-		/*
-		 * It's possible the mount location string in /proc/mounts
-		 * ends with a '/'. In this case, if the entry came from
-		 * /etc/mtab, it won't have the trailing '/' so deal with
-		 * it.
-		 */
-		while (pmc->m.mnt_fsname[nlen - 1] == '/')
-			nlen--;
-		if (strncmp(pmc->m.mnt_fsname, mc->m.mnt_fsname, nlen) != 0)
+		if (!nfs_same_mount_entry(pmc, mc))
 			continue;
 
 		if (strcmp(pmc->m.mnt_type, "nfs4") == 0)
-- 
1.7.8.3
next prev parent reply	other threads:[~2012-02-07 20:44 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-03  1:42 [PATCH] Check for beginning '/' in the mount path Malahal Naineni
2012-02-03 11:15 ` Boaz Harrosh
2012-02-03 12:16   ` NeilBrown
2012-02-03 14:29     ` Malahal Naineni
2012-02-05 11:03       ` Boaz Harrosh
2012-02-06 18:11         ` Malahal Naineni
2012-02-07 20:44         ` Malahal Naineni [this message]
2012-02-16 18:09           ` [PATCH] Get normalized paths for comparing NFS export paths Malahal Naineni
2012-03-02 19:10           ` Steve Dickson
2012-03-02 19:27             ` Malahal Naineni
2012-03-02 20:57               ` Steve Dickson
2012-03-02 22:01                 ` Malahal Naineni
2012-03-03 17:39                   ` Steve Dickson
2012-03-03 19:12                     ` Myklebust, Trond
2012-03-04 22:31                       ` Steve Dickson
2012-03-04 22:46                         ` Myklebust, Trond
2012-03-04 23:08                           ` Steve Dickson
2012-03-05  4:46                           ` Malahal Naineni
2012-03-05 12:03                             ` Steve Dickson
2012-03-04 22:58                         ` Steve Dickson
2012-03-04 23:26                           ` Myklebust, Trond
2012-03-05  0:03                             ` Steve Dickson
2012-03-05  2:04                               ` Myklebust, Trond
2012-03-05  4:53                                 ` Malahal Naineni
2012-03-05 11:55                                 ` Steve Dickson
2012-03-05 14:47                                   ` Malahal Naineni
2012-03-05 15:03                                     ` Steve Dickson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=20120207204401.GA31752@us.ibm.com \
    --to=malahal@us.ibm.com \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).