Blame SOURCES/autofs-5.1.6-only-add-expire-alarm-for-active-mounts.patch

49b67f
autofs-5.1.6 - only add expire alarm for active mounts
49b67f
49b67f
From: Ian Kent <raven@themaw.net>
49b67f
49b67f
There's no need to add expire alarms for mount points that have no
49b67f
expirable mounts.
49b67f
49b67f
Signed-off-by: Ian Kent <raven@themaw.net>
49b67f
---
49b67f
 CHANGELOG           |    1 
49b67f
 daemon/automount.c  |    9 +-------
49b67f
 daemon/direct.c     |    3 ++
49b67f
 daemon/indirect.c   |    3 ++
49b67f
 daemon/state.c      |   25 +++++++++++-----------
49b67f
 include/automount.h |    1 
49b67f
 include/mounts.h    |    1 
49b67f
 lib/alarm.c         |   57 ++++++++++++++++++++++++++++++++++++++++++++++++----
49b67f
 lib/mounts.c        |   19 +++++++++++++++++
49b67f
 9 files changed, 96 insertions(+), 23 deletions(-)
49b67f
49b67f
--- autofs-5.1.4.orig/CHANGELOG
49b67f
+++ autofs-5.1.4/CHANGELOG
49b67f
@@ -131,6 +131,7 @@ xx/xx/2018 autofs-5.1.5
49b67f
 - use struct mnt_list to track mounted mounts.
49b67f
 - use struct mnt_list mounted list for expire.
49b67f
 - remove unused function tree_get_mnt_list().
49b67f
+- only add expre alarm for active mounts.
49b67f
 
49b67f
 19/12/2017 autofs-5.1.4
49b67f
 - fix spec file url.
49b67f
--- autofs-5.1.4.orig/daemon/automount.c
49b67f
+++ autofs-5.1.4/daemon/automount.c
49b67f
@@ -1844,7 +1844,7 @@ int handle_mounts_exit(struct autofs_poi
49b67f
 
49b67f
 	if (ap->state != ST_SHUTDOWN) {
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		/* Return to ST_READY is done immediately */
49b67f
 		st_add_task(ap, ST_READY);
49b67f
 		if (ap->submount)
49b67f
@@ -1889,7 +1889,7 @@ int handle_mounts_exit(struct autofs_poi
49b67f
 	/* Failed shutdown returns to ready */
49b67f
 	warn(ap->logopt, "can't shutdown: filesystem %s still busy", ap->path);
49b67f
 	if (!ap->submount)
49b67f
-		alarm_add(ap, ap->exp_runfreq);
49b67f
+		conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 	/* Return to ST_READY is done immediately */
49b67f
 	st_add_task(ap, ST_READY);
49b67f
 	if (ap->submount)
49b67f
@@ -1952,11 +1952,6 @@ void *handle_mounts(void *arg)
49b67f
 	suc->status = 0;
49b67f
 	pthread_cleanup_pop(1);
49b67f
 
49b67f
-	/* We often start several automounters at the same time.  Add some
49b67f
-	   randomness so we don't all expire at the same time. */
49b67f
-	if (!ap->submount && ap->exp_runfreq)
49b67f
-		alarm_add(ap, ap->exp_runfreq + rand() % ap->exp_runfreq);
49b67f
-
49b67f
 	pthread_setcancelstate(cancel_state, NULL);
49b67f
 
49b67f
 	while (1) {
49b67f
--- autofs-5.1.4.orig/daemon/direct.c
49b67f
+++ autofs-5.1.4/daemon/direct.c
49b67f
@@ -1245,6 +1245,9 @@ static void *do_mount_direct(void *arg)
49b67f
 		info(ap->logopt, "mounted %s", mt.name);
49b67f
 
49b67f
 		mnts_set_mounted_mount(ap, mt.name);
49b67f
+
49b67f
+		if (!ap->submount)
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 	} else {
49b67f
 		/* TODO: get mount return status from lookup_nss_mount */
49b67f
 		ops->send_fail(ap->logopt,
49b67f
--- autofs-5.1.4.orig/daemon/indirect.c
49b67f
+++ autofs-5.1.4/daemon/indirect.c
49b67f
@@ -753,6 +753,9 @@ static void *do_mount_indirect(void *arg
49b67f
 		info(ap->logopt, "mounted %s", buf);
49b67f
 
49b67f
 		mnts_set_mounted_mount(ap, mt.name);
49b67f
+
49b67f
+		if (!ap->submount)
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 	} else {
49b67f
 		/* TODO: get mount return status from lookup_nss_mount */
49b67f
 		ops->send_fail(ap->logopt,
49b67f
--- autofs-5.1.4.orig/daemon/state.c
49b67f
+++ autofs-5.1.4/daemon/state.c
49b67f
@@ -155,7 +155,7 @@ void expire_cleanup(void *arg)
49b67f
 				ap->submount = 1;
49b67f
 
49b67f
 			if (ap->state == ST_EXPIRE && !ap->submount)
49b67f
-				alarm_add(ap, ap->exp_runfreq);
49b67f
+				conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 
49b67f
 			/* FALLTHROUGH */
49b67f
 
49b67f
@@ -173,7 +173,7 @@ void expire_cleanup(void *arg)
49b67f
 			if (!rv && !idle && !ap->shutdown) {
49b67f
 				next = ST_READY;
49b67f
 				if (!ap->submount)
49b67f
-					alarm_add(ap, ap->exp_runfreq);
49b67f
+					conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 				break;
49b67f
 			}
49b67f
 
49b67f
@@ -187,7 +187,7 @@ void expire_cleanup(void *arg)
49b67f
 			/* Failed shutdown returns to ready */
49b67f
 			warn(ap->logopt, "filesystem %s still busy", ap->path);
49b67f
 			if (!ap->submount)
49b67f
-				alarm_add(ap, ap->exp_runfreq);
49b67f
+				conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 			next = ST_READY;
49b67f
 			break;
49b67f
 #endif
49b67f
@@ -522,9 +522,10 @@ static void *do_readmap(void *arg)
49b67f
 		 * alarm will have been added. So add it here if there are
49b67f
 		 * now map entries.
49b67f
 		 */
49b67f
-		if (append_alarm && ap->exp_runfreq)
49b67f
-			alarm_add(ap, ap->exp_runfreq +
49b67f
-				  rand() % ap->exp_runfreq);
49b67f
+		if (append_alarm && ap->exp_runfreq) {
49b67f
+			time_t seconds = ap->exp_runfreq + rand() % ap->exp_runfreq;
49b67f
+			conditional_alarm_add(ap, seconds);
49b67f
+		}
49b67f
 
49b67f
 		pthread_cleanup_pop(1);
49b67f
 		pthread_cleanup_pop(1);
49b67f
@@ -577,7 +578,7 @@ static unsigned int st_readmap(struct au
49b67f
 		/* It didn't work: return to ready */
49b67f
 		st_ready(ap);
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		return 0;
49b67f
 	}
49b67f
 
49b67f
@@ -604,7 +605,7 @@ static unsigned int st_readmap(struct au
49b67f
 		/* It didn't work: return to ready */
49b67f
 		st_ready(ap);
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		return 0;
49b67f
 	}
49b67f
 	ap->readmap_thread = thid;
49b67f
@@ -640,7 +641,7 @@ static unsigned int st_prepare_shutdown(
49b67f
 	case EXP_PARTIAL:
49b67f
 		/* It didn't work: return to ready */
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		st_ready(ap);
49b67f
 		return 0;
49b67f
 
49b67f
@@ -666,7 +667,7 @@ static unsigned int st_force_shutdown(st
49b67f
 	case EXP_PARTIAL:
49b67f
 		/* It didn't work: return to ready */
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		st_ready(ap);
49b67f
 		return 0;
49b67f
 
49b67f
@@ -699,7 +700,7 @@ static unsigned int st_prune(struct auto
49b67f
 	case EXP_ERROR:
49b67f
 	case EXP_PARTIAL:
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		st_ready(ap);
49b67f
 		return 0;
49b67f
 
49b67f
@@ -720,7 +721,7 @@ static unsigned int st_expire(struct aut
49b67f
 	case EXP_ERROR:
49b67f
 	case EXP_PARTIAL:
49b67f
 		if (!ap->submount)
49b67f
-			alarm_add(ap, ap->exp_runfreq);
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		st_ready(ap);
49b67f
 		return 0;
49b67f
 
49b67f
--- autofs-5.1.4.orig/include/automount.h
49b67f
+++ autofs-5.1.4/include/automount.h
49b67f
@@ -637,6 +637,7 @@ static inline time_t monotonic_time(time
49b67f
 /* Expire alarm handling routines */
49b67f
 int alarm_start_handler(void);
49b67f
 int alarm_add(struct autofs_point *ap, time_t seconds);
49b67f
+int conditional_alarm_add(struct autofs_point *ap, time_t seconds);
49b67f
 void alarm_delete(struct autofs_point *ap);
49b67f
 
49b67f
 #endif
49b67f
--- autofs-5.1.4.orig/include/mounts.h
49b67f
+++ autofs-5.1.4/include/mounts.h
49b67f
@@ -125,6 +125,7 @@ void mnts_remove_amdmount(const char *mp
49b67f
 struct mnt_list *mnts_add_mount(struct autofs_point *ap, const char *name, unsigned int flags);
49b67f
 void mnts_remove_mount(const char *mp, unsigned int flags);
49b67f
 struct mnt_list *get_mnt_list(const char *path, int include);
49b67f
+unsigned int mnts_has_mounted_mounts(struct autofs_point *ap);
49b67f
 void mnts_get_expire_list(struct list_head *mnts, struct autofs_point *ap);
49b67f
 void mnts_put_expire_list(struct list_head *mnts);
49b67f
 void mnts_set_mounted_mount(struct autofs_point *ap, const char *name);
49b67f
--- autofs-5.1.4.orig/lib/alarm.c
49b67f
+++ autofs-5.1.4/lib/alarm.c
49b67f
@@ -41,7 +41,7 @@ do { \
49b67f
 } while (0)
49b67f
 
49b67f
 /* Insert alarm entry on ordered list. */
49b67f
-int alarm_add(struct autofs_point *ap, time_t seconds)
49b67f
+int __alarm_add(struct autofs_point *ap, time_t seconds)
49b67f
 {
49b67f
 	struct list_head *head;
49b67f
 	struct list_head *p;
49b67f
@@ -62,8 +62,6 @@ int alarm_add(struct autofs_point *ap, t
49b67f
 	new->cancel = 0;
49b67f
 	new->time = now + seconds;
49b67f
 
49b67f
-	alarm_lock();
49b67f
-
49b67f
 	head = &alarms;
49b67f
 
49b67f
 	/* Check if we have a pending alarm */
49b67f
@@ -97,9 +95,42 @@ int alarm_add(struct autofs_point *ap, t
49b67f
 			fatal(status);
49b67f
 	}
49b67f
 
49b67f
+	return 1;
49b67f
+}
49b67f
+
49b67f
+int alarm_add(struct autofs_point *ap, time_t seconds)
49b67f
+{
49b67f
+	int status;
49b67f
+
49b67f
+	alarm_lock();
49b67f
+	status = __alarm_add(ap, seconds);
49b67f
 	alarm_unlock();
49b67f
 
49b67f
-	return 1;
49b67f
+	return status;
49b67f
+}
49b67f
+
49b67f
+static int __alarm_exists(struct autofs_point *ap)
49b67f
+{
49b67f
+	struct list_head *head;
49b67f
+	struct list_head *p;
49b67f
+
49b67f
+	head = &alarms;
49b67f
+
49b67f
+	if (list_empty(head))
49b67f
+		return 0;
49b67f
+
49b67f
+	p = head->next;
49b67f
+	while (p != head) {
49b67f
+		struct alarm *this;
49b67f
+
49b67f
+		this = list_entry(p, struct alarm, list);
49b67f
+		p = p->next;
49b67f
+
49b67f
+		if (ap == this->ap)
49b67f
+			return 1;
49b67f
+	}
49b67f
+
49b67f
+	return 0;
49b67f
 }
49b67f
 
49b67f
 void alarm_delete(struct autofs_point *ap)
49b67f
@@ -152,6 +183,24 @@ void alarm_delete(struct autofs_point *a
49b67f
 	return;
49b67f
 }
49b67f
 
49b67f
+int conditional_alarm_add(struct autofs_point *ap, time_t seconds)
49b67f
+{
49b67f
+	int status;
49b67f
+
49b67f
+	if (!mnts_has_mounted_mounts(ap))
49b67f
+		return 1;
49b67f
+
49b67f
+	alarm_lock();
49b67f
+	if (__alarm_exists(ap)) {
49b67f
+		alarm_unlock();
49b67f
+		return 1;
49b67f
+	}
49b67f
+	status = __alarm_add(ap, seconds);
49b67f
+	alarm_unlock();
49b67f
+
49b67f
+	return status;
49b67f
+}
49b67f
+
49b67f
 static void *alarm_handler(void *arg)
49b67f
 {
49b67f
 	struct list_head *head;
49b67f
--- autofs-5.1.4.orig/lib/mounts.c
49b67f
+++ autofs-5.1.4/lib/mounts.c
49b67f
@@ -1208,6 +1208,19 @@ void mnts_set_mounted_mount(struct autof
49b67f
 	}
49b67f
 }
49b67f
 
49b67f
+unsigned int mnts_has_mounted_mounts(struct autofs_point *ap)
49b67f
+{
49b67f
+	unsigned int has_mounted_mounts = 0;
49b67f
+
49b67f
+	mnts_hash_mutex_lock();
49b67f
+	if (list_empty(&ap->mounts))
49b67f
+		goto done;
49b67f
+	has_mounted_mounts = 1;
49b67f
+done:
49b67f
+	mnts_hash_mutex_unlock();
49b67f
+	return has_mounted_mounts;
49b67f
+}
49b67f
+
49b67f
 struct node {
49b67f
 	struct mnt_list *mnt;
49b67f
 	struct node *left;
49b67f
@@ -1907,6 +1920,9 @@ static int do_remount_direct(struct auto
49b67f
 		mnts_set_mounted_mount(ap, path);
49b67f
 
49b67f
 		info(ap->logopt, "re-connected to %s", path);
49b67f
+
49b67f
+		if (!ap->submount)
49b67f
+			conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 	} else {
49b67f
 		status = REMOUNT_FAIL;
49b67f
 		info(ap->logopt, "failed to re-connect %s", path);
49b67f
@@ -1980,6 +1996,9 @@ static int do_remount_indirect(struct au
49b67f
 			mnts_set_mounted_mount(ap, buf);
49b67f
 
49b67f
 			info(ap->logopt, "re-connected to %s", buf);
49b67f
+
49b67f
+			if (!ap->submount)
49b67f
+				conditional_alarm_add(ap, ap->exp_runfreq);
49b67f
 		} else {
49b67f
 			status = REMOUNT_FAIL;
49b67f
 			info(ap->logopt, "failed to re-connect %s", buf);