autofs-5.1.9 - improve function mnt_find_submount_by_devid()

From: Ian Kent <raven@themaw.net>

Function mnt_find_submount_by_devid() checks only top level automounts
and first level submounts when trying to locate a mount with a given
devid.

Improve this by recursively checking submounts at all levels.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG        |    1 +
 daemon/master.c  |    2 +-
 include/mounts.h |    2 +-
 lib/mounts.c     |   39 +++++++++++++++++++++++++--------------
 4 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 4c05a33d7..20c076172 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -110,6 +110,7 @@
 - fix master_notify_submount() return.
 - fix thinko in __mnt_find_submount_by_devid().
 - fix setting submount autofs_point in mnts_add_submount().
+- improve function mnt_find_submount_by_devid().
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/master.c b/daemon/master.c
index 7e075e761..2734996f9 100644
--- a/daemon/master.c
+++ b/daemon/master.c
@@ -776,7 +776,7 @@ struct autofs_point *master_find_mapent_by_devid(dev_t devid)
 			break;
 		}
 
-		ap = mnt_find_submount_by_devid(&entry->ap->submounts, devid);
+		ap = mnt_find_submount_by_devid(&entry->ap->mounts, &entry->ap->submounts, devid);
 		if (ap)
 			break;
 	}
diff --git a/include/mounts.h b/include/mounts.h
index 39ca99965..639e4d181 100644
--- a/include/mounts.h
+++ b/include/mounts.h
@@ -165,7 +165,7 @@ int ext_mount_inuse(const char *);
 struct mnt_list *mnts_lookup_mount(const char *mp);
 void mnts_put_mount(struct mnt_list *mnt);
 struct mnt_list *mnts_find_submount(const char *path);
-struct autofs_point *mnt_find_submount_by_devid(struct list_head *submounts, dev_t devid);
+struct autofs_point *mnt_find_submount_by_devid(struct list_head *mounts, struct list_head *submounts, dev_t devid);
 struct mnt_list *mnts_add_submount(struct autofs_point *ap);
 void mnts_remove_submount(const char *mp);
 struct mnt_list *mnts_find_amdmount(const char *path);
diff --git a/lib/mounts.c b/lib/mounts.c
index 3db674a34..cec838b4e 100644
--- a/lib/mounts.c
+++ b/lib/mounts.c
@@ -1113,35 +1113,46 @@ struct mnt_list *mnts_find_submount(const char *path)
 	return NULL;
 }
 
-static struct autofs_point *__mnt_find_submount_by_devid(struct list_head *submounts, dev_t devid)
+static struct autofs_point *__mnt_find_submount_by_devid(struct list_head *mounts, struct list_head *submounts, dev_t devid)
 {
 	struct autofs_point *ap = NULL;
+	struct mnt_list *this;
 	struct list_head *p;
 
-	list_for_each(p, submounts) {
-		struct mnt_list *this;
-
-		this = list_entry(p, struct mnt_list, submount);
+	if (mounts) {
+		list_for_each(p, mounts) {
+			this = list_entry(p, struct mnt_list, mount);
 
-		if (this->ap->dev == devid) {
-			ap = this->ap;
-			break;
+			ap = __mnt_find_submount_by_devid(&this->ap->mounts, &this->ap->submounts, devid);
+			if (ap)
+				goto out;
 		}
-
-		ap = __mnt_find_submount_by_devid(&this->ap->submounts, devid);
-		if (ap)
-			break;
 	}
 
+	if (submounts) {
+		list_for_each(p, submounts) {
+			this = list_entry(p, struct mnt_list, submount);
+
+			if (this->ap->dev == devid) {
+				ap = this->ap;
+				break;
+			}
+
+			ap = __mnt_find_submount_by_devid(&this->ap->mounts, &this->ap->submounts, devid);
+			if (ap)
+				goto out;
+		}
+	}
+out:
 	return ap;
 }
 
-struct autofs_point *mnt_find_submount_by_devid(struct list_head *submounts, dev_t devid)
+struct autofs_point *mnt_find_submount_by_devid(struct list_head *mounts, struct list_head *submounts, dev_t devid)
 {
 	struct autofs_point *ap = NULL;
 
 	mnts_hash_mutex_lock();
-	ap = __mnt_find_submount_by_devid(submounts, devid);
+	ap = __mnt_find_submount_by_devid(mounts, submounts, devid);
 	mnts_hash_mutex_unlock();
 
 	return ap;
