Blob Blame History Raw
From c72e9f343f3967fcb936e1131430a6853f1e1d33 Mon Sep 17 00:00:00 2001
From: Dave Chiluk <chiluk@canonical.com>
Date: Mon, 31 Aug 2015 16:07:58 -0500
Subject: [PATCH 1/2] mountlist: add me_mntroot field on Linux machines

* lib/mountlist.c (read_file_system_list): Populate me_mntroot in
mount_entry so Linux machines based on /proc/self/mountinfo can
distinguish between bind mounts and original mounts.  In reality bind
mounts aren't treated differently than mountroot=/ mounts by the
kernel, but the user often wants these bind mounts distinguished.
* lib/mountlist.h (struct mount_entry): Add me_mntroot element.
More details at https://pad.lv/1432871

Upstream-commit: c6148bca89e9465fd6ba3a10d273ec4cb58c2dbe
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 lib/mountlist.c | 25 ++++++++++++++++++++++---
 lib/mountlist.h |  2 ++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/lib/mountlist.c b/lib/mountlist.c
index 6f04f55..08ade6f 100644
--- a/lib/mountlist.c
+++ b/lib/mountlist.c
@@ -447,6 +447,7 @@ read_file_system_list (bool need_fs_type)
         me = xmalloc (sizeof *me);
         me->me_devname = xstrdup (mnt->mnt_fsname);
         me->me_mountdir = xstrdup (mnt->mnt_dir);
+        me->me_mntroot = NULL;
         me->me_type = xstrdup (mnt->mnt_type);
         me->me_type_malloced = 1;
         me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -478,7 +479,8 @@ read_file_system_list (bool need_fs_type)
         while (getline (&line, &buf_size, fp) != -1)
           {
             unsigned int devmaj, devmin;
-            int target_s, target_e, type_s, type_e, source_s, source_e;
+            int target_s, target_e, type_s, type_e;
+            int source_s, source_e, mntroot_s, mntroot_e;
             char test;
             char *dash;
             int rc;
@@ -486,13 +488,15 @@ read_file_system_list (bool need_fs_type)
             rc = sscanf(line, "%*u "        /* id - discarded  */
                               "%*u "        /* parent - discarded */
                               "%u:%u "      /* dev major:minor  */
-                              "%*s "        /* mountroot - discarded  */
+                              "%n%*s%n "    /* mountroot */
                               "%n%*s%n"     /* target, start and end  */
                               "%c",         /* more data...  */
                               &devmaj, &devmin,
+                              &mntroot_s, &mntroot_e,
                               &target_s, &target_e,
                               &test);
-            if (rc != 3 && rc != 5)  /* 5 if %n included in count.  */
+
+            if (rc != 3 && rc != 7)  /* 7 if %n included in count.  */
               continue;
 
             /* skip optional fields, terminated by " - "  */
@@ -511,16 +515,19 @@ read_file_system_list (bool need_fs_type)
               continue;
 
             /* manipulate the sub-strings in place.  */
+            line[mntroot_e] = '\0';
             line[target_e] = '\0';
             dash[type_e] = '\0';
             dash[source_e] = '\0';
             unescape_tab (dash + source_s);
             unescape_tab (line + target_s);
+            unescape_tab (line + mntroot_s);
 
             me = xmalloc (sizeof *me);
 
             me->me_devname = xstrdup (dash + source_s);
             me->me_mountdir = xstrdup (line + target_s);
+            me->me_mntroot = xstrdup (line + mntroot_s);
             me->me_type = xstrdup (dash + type_s);
             me->me_type_malloced = 1;
             me->me_dev = makedev (devmaj, devmin);
@@ -566,6 +573,7 @@ read_file_system_list (bool need_fs_type)
             me = xmalloc (sizeof *me);
             me->me_devname = xstrdup (mnt->mnt_fsname);
             me->me_mountdir = xstrdup (mnt->mnt_dir);
+            me->me_mntroot = NULL;
             me->me_type = xstrdup (mnt->mnt_type);
             me->me_type_malloced = 1;
             me->me_dummy = ME_DUMMY (me->me_devname, me->me_type, bind);
@@ -598,6 +606,7 @@ read_file_system_list (bool need_fs_type)
         me = xmalloc (sizeof *me);
         me->me_devname = xstrdup (fsp->f_mntfromname);
         me->me_mountdir = xstrdup (fsp->f_mntonname);
+        me->me_mntroot = NULL;
         me->me_type = fs_type;
         me->me_type_malloced = 0;
         me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -624,6 +633,7 @@ read_file_system_list (bool need_fs_type)
         me = xmalloc (sizeof *me);
         me->me_devname = xstrdup (fsp->f_mntfromname);
         me->me_mountdir = xstrdup (fsp->f_mntonname);
+        me->me_mntroot = NULL;
         me->me_type = xstrdup (fsp->f_fstypename);
         me->me_type_malloced = 1;
         me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -650,6 +660,7 @@ read_file_system_list (bool need_fs_type)
         me = xmalloc (sizeof *me);
         me->me_devname = xstrdup (fsd.fd_req.devname);
         me->me_mountdir = xstrdup (fsd.fd_req.path);
+        me->me_mntroot = NULL;
         me->me_type = gt_names[fsd.fd_req.fstype];
         me->me_type_malloced = 0;
         me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -748,6 +759,7 @@ read_file_system_list (bool need_fs_type)
           me->me_devname = xstrdup (fi.device_name[0] != '\0'
                                     ? fi.device_name : fi.fsh_name);
           me->me_mountdir = xstrdup (re != NULL ? re->name : fi.fsh_name);
+          me->me_mntroot = NULL;
           me->me_type = xstrdup (fi.fsh_name);
           me->me_type_malloced = 1;
           me->me_dev = fi.dev;
@@ -797,6 +809,7 @@ read_file_system_list (bool need_fs_type)
         me = xmalloc (sizeof *me);
         me->me_devname = xstrdup (stats[counter].f_mntfromname);
         me->me_mountdir = xstrdup (stats[counter].f_mntonname);
+        me->me_mntroot = NULL;
         me->me_type = xstrdup (FS_TYPE (stats[counter]));
         me->me_type_malloced = 1;
         me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -833,6 +846,7 @@ read_file_system_list (bool need_fs_type)
         strcpy (me->me_devname + 5, mnt.mt_dev);
 # endif
         me->me_mountdir = xstrdup (mnt.mt_filsys);
+        me->me_mntroot = NULL;
         me->me_dev = (dev_t) -1;        /* Magic; means not known yet. */
         me->me_type = "";
         me->me_type_malloced = 0;
@@ -880,6 +894,7 @@ read_file_system_list (bool need_fs_type)
         me = xmalloc (sizeof *me);
         me->me_devname = xstrdup ((*ent)->mt_resource);
         me->me_mountdir = xstrdup ((*ent)->mt_directory);
+        me->me_mntroot = NULL;
         me->me_type = xstrdup ((*ent)->mt_fstype);
         me->me_type_malloced = 1;
         me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -942,6 +957,7 @@ read_file_system_list (bool need_fs_type)
             me = xmalloc (sizeof *me);
             me->me_devname = xstrdup (mnt.mnt_special);
             me->me_mountdir = xstrdup (mnt.mnt_mountp);
+            me->me_mntroot = NULL;
             me->me_type = xstrdup (mnt.mnt_fstype);
             me->me_type_malloced = 1;
             me->me_dummy = MNT_IGNORE (&mnt) != 0;
@@ -1020,6 +1036,7 @@ read_file_system_list (bool need_fs_type)
                                       vmp->vmt_data[VMT_OBJECT].vmt_off);
           }
         me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
+        me->me_mntroot = NULL;
         me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
         me->me_type_malloced = 1;
         options = thisent + vmp->vmt_data[VMT_ARGS].vmt_off;
@@ -1063,6 +1080,7 @@ read_file_system_list (bool need_fs_type)
             me = xmalloc (sizeof *me);
             me->me_devname = xstrdup (dev.f_mntfromname);
             me->me_mountdir = xstrdup (dev.f_mntonname);
+            me->me_mntroot = NULL;
             me->me_type = xstrdup (dev.f_fstypename);
             me->me_type_malloced = 1;
             me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
@@ -1105,6 +1123,7 @@ void free_mount_entry (struct mount_entry *me)
 {
   free (me->me_devname);
   free (me->me_mountdir);
+  free (me->me_mntroot);
   if (me->me_type_malloced)
     free (me->me_type);
   free (me);
diff --git a/lib/mountlist.h b/lib/mountlist.h
index 735776b..9ce3137 100644
--- a/lib/mountlist.h
+++ b/lib/mountlist.h
@@ -27,6 +27,8 @@ struct mount_entry
 {
   char *me_devname;             /* Device node name, including "/dev/". */
   char *me_mountdir;            /* Mount point directory name. */
+  char *me_mntroot;             /* Directory on filesystem of device used */
+                                /* as root for the (bind) mount. */
   char *me_type;                /* "nfs", "4.2", etc. */
   dev_t me_dev;                 /* Device number of me_mountdir. */
   unsigned int me_dummy : 1;    /* Nonzero for dummy file systems. */
-- 
2.5.5


From aff5e38d43f8477ccf07eb9e87a849b6c3211302 Mon Sep 17 00:00:00 2001
From: Dave Chiluk <chiluk@canonical.com>
Date: Mon, 21 Sep 2015 15:04:11 -0500
Subject: [PATCH 2/2] df: prioritize mounts nearer the device root

In the presence of bind mounts of a device, the 4th "mount root" field
from /proc/self/mountinfo is now considered, so as to prefer mount
points closer to the root of the device.  Note on older systems with
an /etc/mtab file, the source device was listed as the originating
directory, and so this was not an issue.
Details at http://pad.lv/1432871

* src/df.c (filter_mount_list): When deduplicating mount entries,
only prefer sources nearer or at the root of the device, when the
target is nearer the root of the device.
* NEWS: Mention the change in behavior.

Upstream-commit: 3babaf83875ceac896c8dd3a64248e955dfecef9
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 src/df.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/df.c b/src/df.c
index 0dfd554..e390298 100644
--- a/src/df.c
+++ b/src/df.c
@@ -649,6 +649,13 @@ filter_mount_list (bool devices_only)
 
           if (devlist)
             {
+              bool target_nearer_root = strlen (devlist->me->me_mountdir)
+                                        > strlen (me->me_mountdir);
+              /* With bind mounts, prefer items nearer the root of the source */
+              bool source_below_root = devlist->me->me_mntroot != NULL
+                                       && me->me_mntroot != NULL
+                                       && (strlen (devlist->me->me_mntroot)
+                                           < strlen (me->me_mntroot));
               if (! print_grand_total && me->me_remote && devlist->me->me_remote
                   && ! STREQ (devlist->me->me_devname, me->me_devname))
                 {
@@ -660,9 +667,8 @@ filter_mount_list (bool devices_only)
               else if ((strchr (me->me_devname, '/')
                        /* let "real" devices with '/' in the name win.  */
                         && ! strchr (devlist->me->me_devname, '/'))
-                       /* let a shorter mountdir win.  */
-                       || (strlen (devlist->me->me_mountdir)
-                           > strlen (me->me_mountdir))
+                       /* let points towards the root of the device win.  */
+                       || (target_nearer_root && ! source_below_root)
                        /* let an entry overmounted on a new device win...  */
                        || (! STREQ (devlist->me->me_devname, me->me_devname)
                            /* ... but only when matching an existing mnt point,
-- 
2.5.5