05ad79
From 0f66811659aa8fd8b14ade8a80bfecd580962b2d Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Tue, 10 Mar 2015 12:51:44 +0100
05ad79
Subject: [PATCH 71/84] findmnt: don't rely on st_dev for --target
05ad79
05ad79
The overlay filesystem does not provide usable st_dev (in traditional
05ad79
UNIX way). It's necessary to search in /proc/self/mountinfo to detect
05ad79
which path element is mountpoint.
05ad79
05ad79
$ findmnt --target /mnt/merged/dir-a/foo
05ad79
TARGET      SOURCE  FSTYPE  OPTIONS
05ad79
/mnt/merged overlay overlay rw,relatime,lowerdir=/mnt/low,upperdir=/mnt/high/data,workdir=/mnt/high/work
05ad79
05ad79
Upstream: http://github.com/karelzak/util-linux/commit/cd41b385a06dde70bb45c3143d3459157bda58f8
05ad79
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=587393
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 libmount/src/utils.c |  6 +++++-
05ad79
 misc-utils/findmnt.8 |  8 ++++++--
05ad79
 misc-utils/findmnt.c | 17 +++++++++++------
05ad79
 3 files changed, 22 insertions(+), 9 deletions(-)
05ad79
05ad79
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
05ad79
index 7c6f5b1..5783d88 100644
05ad79
--- a/libmount/src/utils.c
05ad79
+++ b/libmount/src/utils.c
05ad79
@@ -859,7 +859,11 @@ int mnt_open_uniq_filename(const char *filename, char **name)
05ad79
  * This function finds the mountpoint that a given path resides in. @path
05ad79
  * should be canonicalized. The returned pointer should be freed by the caller.
05ad79
  *
05ad79
- * Returns: allocated string with target of the mounted device or NULL on error
05ad79
+ * WARNING: the function compares st_dev of the @path elements. This traditional
05ad79
+ * way maybe be insufficient on filesystems like Linux "overlay". See also
05ad79
+ * mnt_table_find_target().
05ad79
+ *
05ad79
+ * Returns: allocated string with the target of the mounted device or NULL on error
05ad79
  */
05ad79
 char *mnt_get_mountpoint(const char *path)
05ad79
 {
05ad79
diff --git a/misc-utils/findmnt.8 b/misc-utils/findmnt.8
05ad79
index 407636e..54739b7 100644
05ad79
--- a/misc-utils/findmnt.8
05ad79
+++ b/misc-utils/findmnt.8
05ad79
@@ -179,8 +179,12 @@ Search in
05ad79
 .IR /etc/fstab .
05ad79
 The output is in the list format (see \fB--list\fR).
05ad79
 .TP
05ad79
-.BR \-T , " \-\-target \fIdir\fP"
05ad79
-Explicitly define the mount target (mountpoint directory).
05ad79
+.BR \-T , " \-\-target \fIpath\fP"
05ad79
+Explicitly define the mount target (mountpoint directory). If the \fIpath\fR
05ad79
+is not a mountpoint file or directory than
05ad79
+.B findmnt
05ad79
+checks \fIpath\fR elements in reverse order for get the mountpoint (this feature is 
05ad79
+supported only if search in kernel files and unsupported for \fB\-\-fstab\fP).
05ad79
 .TP
05ad79
 .BR \-t , " \-\-types \fIlist\fP"
05ad79
 Limit the set of printed filesystems.  More than one type may be
05ad79
diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c
05ad79
index f16da91..fe899db 100644
05ad79
--- a/misc-utils/findmnt.c
05ad79
+++ b/misc-utils/findmnt.c
05ad79
@@ -236,9 +236,12 @@ static void set_source_match(const char *data)
05ad79
 		set_match(COL_SOURCE, data);
05ad79
 }
05ad79
 
05ad79
-static void enable_extra_target_match(void)
05ad79
+/* @tb has to be from kernel (so no fstab or so)! */
05ad79
+static void enable_extra_target_match(struct libmnt_table *tb)
05ad79
 {
05ad79
-	char *cn = NULL, *mnt = NULL;
05ad79
+	char *cn = NULL;
05ad79
+	const char *tgt = NULL, *mnt = NULL;
05ad79
+	struct libmnt_fs *fs;
05ad79
 
05ad79
 	/*
05ad79
 	 * Check if match pattern is mountpoint, if not use the
05ad79
@@ -248,9 +251,11 @@ static void enable_extra_target_match(void)
05ad79
 	if (!cn)
05ad79
 		return;
05ad79
 
05ad79
-	mnt = mnt_get_mountpoint(cn);
05ad79
-	if (!mnt || strcmp(mnt, cn) == 0)
05ad79
-		return;
05ad79
+	fs = mnt_table_find_mountpoint(tb, tgt, MNT_ITER_BACKWARD);
05ad79
+	if (fs)
05ad79
+		mnt = mnt_fs_get_target(fs);
05ad79
+	if (mnt && strcmp(mnt, tgt) != 0)
05ad79
+		set_match(COL_TARGET, xstrdup(mnt));	/* replace the current setting */
05ad79
 
05ad79
 	/* replace the current setting with the real mountpoint */
05ad79
 	set_match(COL_TARGET, mnt);
05ad79
@@ -1484,7 +1489,7 @@ int main(int argc, char *argv[])
05ad79
 			 * try it again with extra functionality for target
05ad79
 			 * match
05ad79
 			 */
05ad79
-			enable_extra_target_match();
05ad79
+			enable_extra_target_match(tb);
05ad79
 			rc = add_matching_lines(tb, tt, direction);
05ad79
 		}
05ad79
 	}
05ad79
-- 
05ad79
2.7.4
05ad79