autofs-5.1.9 - improve handling of missing map entry for expire request

From: Ian Kent <raven@themaw.net>

It makes sense to cover the case where a map entry has been removed but
the entry still has a real mount associated with it. Make it about
umounting the real mount only and let a subsequent mount request trigger
the cleanup.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG       |    1 
 daemon/direct.c |  114 +++++++++++++++++++++++++++++++++----------------------
 2 files changed, 69 insertions(+), 46 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index be7f1347a..57ff94633 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -99,6 +99,7 @@
 - remove mount point directory at umount if we created it.
 - remove directory cleanup umount_multi().
 - always set ap for mnt_list entries.
+- improve handling of missing map entry for expire request.
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/direct.c b/daemon/direct.c
index 537dc46fa..95e6d6553 100644
--- a/daemon/direct.c
+++ b/daemon/direct.c
@@ -1006,6 +1006,8 @@ static void *do_expire_direct(void *arg)
 	else {
 		struct mapent *me;
 
+		if (!mt.mc)
+			goto done;
 		cache_writelock(mt.mc);
 		me = cache_lookup_distinct(mt.mc, mt.name);
 		if (me) {
@@ -1030,6 +1032,7 @@ static void *do_expire_direct(void *arg)
 			}
 		}
 		cache_unlock(mt.mc);
+done:
 		ops->send_ready(ap->logopt, mt.ioctlfd, mt.wait_queue_token);
 		ops->close(ap->logopt, mt.ioctlfd);
 	}
@@ -1040,6 +1043,33 @@ static void *do_expire_direct(void *arg)
 	return NULL;
 }
 
+static char *do_table_lookup_ino(struct autofs_point *ap, dev_t dev, ino_t ino, char *buf, size_t len, int *fd)
+{
+	char *path;
+
+	/* If the map entry wasn't found it must have been deleted
+	 * from the map but a trigger mount is still mounted because
+	 * it has sent us this request. So use the mount table for a
+	 * brute force lookup to get the path and open a file handle
+	 * for it so we can return a not found status to the kernel.
+	 */
+	path = table_lookup_ino(ap, dev, ino, buf, len, fd);
+	if (!path) {
+		/* This could be cuased by an inability to open a file
+		 * handle but generally that doesn't happen. The mount
+		 * has to exist and be pinned becuase we got this request
+		 * so it can't be umounted. Therefore it's very unlikely
+		 * this case will happen. If it does happen it's fatal,
+		 * the waiter will hang and there's nothing we can do
+		 * about it.
+		 */
+		logerr("can't find mount for (%lu,%lu)",
+			(unsigned long) dev, (unsigned long) ino);
+		/* TODO:  how do we clear wait q in kernel ?? */
+	}
+	return path;
+}
+
 int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_direct_t *pkt)
 {
 	struct ioctl_ops *ops = get_ioctl_ops();
@@ -1051,6 +1081,11 @@ int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_di
 	pthread_t thid;
 	struct timespec wait;
 	int status, state;
+	int ioctlfd;
+	dev_t dev = pkt->dev;
+	ino_t ino = pkt->ino;
+	char tmp[PATH_MAX + 1];
+	char *path;
 
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state);
 
@@ -1069,37 +1104,42 @@ int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_di
 	while (map) {
 		mc = map->mc;
 		cache_writelock(mc);
-		me = cache_lookup_ino(mc, pkt->dev, pkt->ino);
-		if (me)
+		me = cache_lookup_ino(mc, dev, ino);
+		if (me) {
+			path = me->key;
+			ioctlfd = me->ioctlfd;
 			break;
+		}
 		cache_unlock(mc);
+		mc = NULL;
 		map = map->next;
 	}
 
-	if (!me || me->len >= PATH_MAX) {
-		/*
-		 * Shouldn't happen as we have been sent this following
-		 * successful thread creation and lookup.
-		 */
-		if (!me)
-			crit(ap->logopt, "can't find map entry for (%lu,%lu)",
-			    (unsigned long) pkt->dev, (unsigned long) pkt->ino);
-		else {
-			cache_unlock(mc);
-			crit(ap->logopt, "lookup key is too long");
+	if (!me) {
+		path = do_table_lookup_ino(ap, dev, ino, tmp, PATH_MAX + 1, &ioctlfd);
+		if (!path) {
+			master_source_unlock(ap->entry);
+			master_mutex_unlock();
+			pthread_setcancelstate(state, NULL);
+			return 1;
 		}
+	} else if (me->len >= PATH_MAX) {
+		/* Shouldn't happen, should be validated before this. */
+		cache_unlock(mc);
+		error(ap->logopt, "direct mount path too long %s", me->key);
+		ops->send_fail(ap->logopt,
+			       ioctlfd, pkt->wait_queue_token, -ENAMETOOLONG);
+		ops->close(ap->logopt, ioctlfd);
 		master_source_unlock(ap->entry);
 		pthread_setcancelstate(state, NULL);
 		return 1;
-	}
-
-	/* Can't expire it if it isn't mounted */
-	if (me->ioctlfd == -1) {
-		int ioctlfd;
-
+	} else if (me->ioctlfd == -1) {
+		/* Can't expire it if it isn't mounted */
 		ioctlfd = open_ioctlfd(ap, me->key, me->dev);
 		if (ioctlfd == -1) {
 			cache_unlock(mc);
+			logerr("can't open ioctlfd for %s", me->key);
+			/* TODO:  how do we clear wait q in kernel ?? */
 			master_source_unlock(ap->entry);
 			pthread_setcancelstate(state, NULL);
 			return 1;
@@ -1111,14 +1151,16 @@ int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_di
 		pthread_setcancelstate(state, NULL);
 		return 0;
 	}
+	if (mc)
+		cache_unlock(mc);
 
 	mt = malloc(sizeof(struct pending_args));
 	if (!mt) {
 		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
 		error(ap->logopt, "malloc: %s", estr);
 		ops->send_fail(ap->logopt,
-			       me->ioctlfd, pkt->wait_queue_token, -ENOMEM);
-		cache_unlock(mc);
+			       ioctlfd, pkt->wait_queue_token, -ENOMEM);
+		ops->close(ap->logopt, ioctlfd);
 		master_source_unlock(ap->entry);
 		pthread_setcancelstate(state, NULL);
 		return 1;
@@ -1131,10 +1173,10 @@ int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_di
 		fatal(status);
 
 	mt->ap = ap;
-	mt->ioctlfd = me->ioctlfd;
+	mt->ioctlfd = ioctlfd;
 	mt->mc = mc;
-	strcpy(mt->name, me->key);
-	mt->dev = me->dev;
+	strcpy(mt->name, path);
+	mt->dev = dev;
 	mt->type = NFY_EXPIRE;
 	mt->wait_queue_token = pkt->wait_queue_token;
 
@@ -1148,7 +1190,6 @@ int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_di
 		error(ap->logopt, "expire thread create failed");
 		ops->send_fail(ap->logopt,
 			       mt->ioctlfd, pkt->wait_queue_token, -status);
-		cache_unlock(mc);
 		master_source_unlock(ap->entry);
 		pending_mutex_unlock(mt);
 		pending_cond_destroy(mt);
@@ -1158,7 +1199,6 @@ int handle_packet_expire_direct(struct autofs_point *ap, autofs_packet_expire_di
 		return 1;
 	}
 
-	cache_unlock(mc);
 	master_source_unlock(ap->entry);
 
 	pthread_cleanup_push(free_pending_args, mt);
@@ -1377,26 +1417,8 @@ int handle_packet_missing_direct(struct autofs_point *ap, autofs_packet_missing_
 		char tmp[PATH_MAX + 1];
 		char *path;
 
-		/* If the map entry wasn't found it must have been deleted
-		 * from the map but a trigger mount is still mounted because
-		 * it has sent us this request. So use the mount table for a
-		 * brute force lookup to get the path and open a file handle
-		 * for it so we can return a not found status to the kernel.
-		 */
-		path = table_lookup_ino(ap, pkt->dev, pkt->ino, tmp, PATH_MAX + 1, &ioctlfd);
-		if (!path) {
-			/* This could be cuased by an inability to open a file
-			 * handle but generally that doesn't happen. The mount
-			 * has to exist and be pinned becuase we got this request
-			 * so it can't be umounted. Therefore it's very unlikely
-			 * this case will happen. If it does happen it's fatal,
-			 * the waiter will hang and there's nothing we can do
-			 * about it.
-			 */
-			logerr("can't find mount for (%lu,%lu)",
-			    (unsigned long) pkt->dev, (unsigned long) pkt->ino);
-			/* TODO:  how do we clear wait q in kernel ?? */
-		} else {
+		path = do_table_lookup_ino(ap, pkt->dev, pkt->ino, tmp, PATH_MAX + 1, &ioctlfd);
+		if (path) {
 			char buf[MAX_ERR_BUF];
 
 			/* Try and recover from this unexpecyedly missing map
