git: 21b8e363c4eb - main - automount: check for mounted-over autofs instances on flush

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 17 Oct 2023 16:41:40 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=21b8e363c4eb24c0a5659101603cc08a86d87759

commit 21b8e363c4eb24c0a5659101603cc08a86d87759
Author:     Andrew Gierth <andrew@tao146.riddles.org.uk>
AuthorDate: 2023-07-10 15:09:56 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-10-17 16:40:45 +0000

    automount: check for mounted-over autofs instances on flush
    
    PR:     272446
    Reviewed by:    kib
    MFC after:      1 week
    Differential revision: https://reviews.freebsd.org/D40961
---
 usr.sbin/autofs/automount.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/usr.sbin/autofs/automount.c b/usr.sbin/autofs/automount.c
index 188bbc3a040f..cee647cb4e2c 100644
--- a/usr.sbin/autofs/automount.c
+++ b/usr.sbin/autofs/automount.c
@@ -260,6 +260,7 @@ static void
 flush_caches(void)
 {
 	struct statfs *mntbuf;
+	struct statfs statbuf;
 	int i, nitems;
 
 	nitems = getmntinfo(&mntbuf, MNT_WAIT);
@@ -274,6 +275,21 @@ flush_caches(void)
 			    mntbuf[i].f_mntonname);
 			continue;
 		}
+		/*
+		 * A direct map mountpoint may have been mounted over, in
+		 * which case we can't MNT_UPDATE it. There's an obvious race
+		 * condition remaining here, but that has to be fixed in the
+		 * kernel.
+		 */
+		if (statfs(mntbuf[i].f_mntonname, &statbuf) != 0) {
+			log_err(1, "cannot statfs %s", mntbuf[i].f_mntonname);
+			continue;
+		}
+		if (strcmp(statbuf.f_fstypename, "autofs") != 0) {
+			log_debugx("skipping %s, filesystem type is not autofs",
+			    mntbuf[i].f_mntonname);
+			continue;
+		}
 
 		flush_autofs(mntbuf[i].f_mntonname);
 	}