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