autofs-5.1.9 - make negative timeout check consistent

From: Ian Kent <raven@themaw.net>

We want to make the negative caching behaviour consistent so we can see
if side effects might be introduced when changes are needed.

Add a function to check if the negative timeout has expired without
setting the cache entry positive.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG           |    1 +
 daemon/direct.c     |    2 +-
 daemon/indirect.c   |    2 +-
 include/automount.h |    1 +
 lib/cache.c         |    9 ++++++++-
 5 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 1bd8754bb..3c0b8f0fd 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -101,6 +101,7 @@
 - always set ap for mnt_list entries.
 - improve handling of missing map entry for expire request.
 - don't open code negative update in lookup.
+- make negative timeout check consistent.
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/direct.c b/daemon/direct.c
index 95e6d6553..d07cb8e3e 100644
--- a/daemon/direct.c
+++ b/daemon/direct.c
@@ -1482,7 +1482,7 @@ int handle_packet_missing_direct(struct autofs_point *ap, autofs_packet_missing_
 	}
 
 	/* Check if we recorded a mount fail for this key */
-	if (me->status >= monotonic_time(NULL)) {
+	if (!cache_negative_expired(me)) {
 		ops->send_fail(ap->logopt,
 			       ioctlfd, pkt->wait_queue_token, -ENOENT);
 		ops->close(ap->logopt, ioctlfd);
diff --git a/daemon/indirect.c b/daemon/indirect.c
index f8abaaa27..c5094ebef 100644
--- a/daemon/indirect.c
+++ b/daemon/indirect.c
@@ -812,7 +812,7 @@ int handle_packet_missing_indirect(struct autofs_point *ap, autofs_packet_missin
 	me = lookup_source_mapent(ap, pkt->name, LKP_DISTINCT);
 	if (me) {
 		/* Check if we recorded a mount fail for this key */
-		if (me->status >= monotonic_time(NULL)) {
+		if (!cache_negative_expired(me)) {
 			ops->send_fail(ap->logopt, ap->ioctlfd,
 				       pkt->wait_queue_token, -ENOENT);
 			cache_unlock(me->mc);
diff --git a/include/automount.h b/include/automount.h
index 240ab3855..bb71e53d0 100644
--- a/include/automount.h
+++ b/include/automount.h
@@ -217,6 +217,7 @@ struct mapent *cache_partial_match(struct mapent_cache *mc, const char *prefix);
 struct mapent *cache_partial_match_wild(struct mapent_cache *mc, const char *prefix);
 int cache_add(struct mapent_cache *mc, struct map_source *ms, const char *key, const char *mapent, time_t age);
 int cache_update_offset(struct mapent_cache *mc, const char *mkey, const char *key, const char *mapent, time_t age);
+int cache_negative_expired(struct mapent *me);
 int cache_lookup_negative(struct mapent *me, const char *key);
 void cache_update_negative(struct mapent_cache *mc, struct map_source *ms, const char *key, time_t timeout);
 struct mapent *cache_get_offset_parent(struct mapent_cache *mc, const char *key);
diff --git a/lib/cache.c b/lib/cache.c
index 3b5a0cbbd..d266c1ec9 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -625,12 +625,19 @@ int cache_update_offset(struct mapent_cache *mc, const char *mkey, const char *k
 	return ret; 
 }
 
+int cache_negative_expired(struct mapent *me)
+{
+	if (me->status >= monotonic_time(NULL))
+		return 0;
+	return 1;
+}
+
 /* Called with cache_lock held for map entry me and is released
  * on return.
  */
 int cache_lookup_negative(struct mapent *me, const char *key)
 {
-	if (me->status >= monotonic_time(NULL)) {
+	if (!cache_negative_expired(me)) {
 		cache_unlock(me->mc);
 		return CHE_UNAVAIL;
 	} else {
