btrfs-progs: fix find_mount_root() to handle duplicated mount point correctly
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Thu, 4 Sep 2014 02:27:48 +0000 (10:27 +0800)
committerDavid Sterba <dsterba@suse.cz>
Sun, 14 Sep 2014 17:06:10 +0000 (19:06 +0200)
Original find_mount_root() will use the first mount point match and
return it.
It was OK until the following commit, which will also check the fstype:
de22c28ef31d9721606ba059 btrfs-progs: Check fstype in find_mount_root()

With fstype check, we should check the last match, not only the first
one.
Or the following mount will not pass the find_mount_root():
/dev/sdc on /mnt/test type ext4 (rw,relatime,data=ordered)
/dev/sdb on /mnt/test type btrfs (rw,relatime,space_cache)

This patch will use the last match to do the fstype check.

Reported-by: Remco Hosman <remco@yerf-it.nl>
Signed-off-by: Remco Hosman <remco@yerf-it.nl>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Reviewed-by: Omar Sandoval <osandov@osandov.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
utils.c

diff --git a/utils.c b/utils.c
index f0a3c07..c4f2a00 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -2359,8 +2359,8 @@ int find_mount_root(const char *path, char **mount_root)
        while ((ent = getmntent(mnttab))) {
                len = strlen(ent->mnt_dir);
                if (strncmp(ent->mnt_dir, path, len) == 0) {
-                       /* match found */
-                       if (longest_matchlen < len) {
+                       /* match found and use the latest match */
+                       if (longest_matchlen <= len) {
                                free(longest_match);
                                longest_matchlen = len;
                                longest_match = strdup(ent->mnt_dir);