Blame SOURCES/autofs-5.0.9-amd-lookup-add-external-mounts-tracking-functions.patch

304803
autofs-5.0.9 - amd lookup add external mounts tracking functions
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
Amd automounts can use what's called a sublink option. Using this
304803
option a single containing mount can be specified and symlinks
304803
that point to directories within the mount created instead of a
304803
mount for every directory.
304803
304803
In some cases this can greatly reduce the number of mounts needed
304803
for a map but complicates automounting when using an "in-place"
304803
automounter such as autofs.
304803
304803
Clearly we can't perform the mount and create symlinks within it
304803
so we must us a mount lookaside directory like amd. Those external
304803
mounts also need to be managed.
304803
304803
This patch adds functions to maintain a list of these mounts (much
304803
like a reference counter) so we know if an external mount is present
304803
and if it's time to umount it.
304803
---
304803
 include/mounts.h |    2 +
304803
 lib/mounts.c     |  132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
304803
 2 files changed, 134 insertions(+)
304803
304803
diff --git a/include/mounts.h b/include/mounts.h
304803
index c2f92ec..3bef086 100644
304803
--- a/include/mounts.h
304803
+++ b/include/mounts.h
304803
@@ -93,6 +93,8 @@ unsigned int get_kver_major(void);
304803
 unsigned int get_kver_minor(void);
304803
 char *make_options_string(char *path, int kernel_pipefd, const char *extra);
304803
 char *make_mnt_name_string(char *path);
304803
+int ext_mount_add(struct list_head *, const char *);
304803
+int ext_mount_remove(struct list_head *, const char *);
304803
 struct mnt_list *get_mnt_list(const char *table, const char *path, int include);
304803
 struct mnt_list *reverse_mnt_list(struct mnt_list *list);
304803
 void free_mnt_list(struct mnt_list *list);
304803
diff --git a/lib/mounts.c b/lib/mounts.c
304803
index 868dcb1..aea6691 100644
304803
--- a/lib/mounts.c
304803
+++ b/lib/mounts.c
304803
@@ -46,6 +46,17 @@ static const char mnt_name_template[]      = "automount(pid%u)";
304803
 static struct kernel_mod_version kver = {0, 0};
304803
 static const char kver_options_template[]  = "fd=%d,pgrp=%u,minproto=3,maxproto=5";
304803
 
304803
+#define EXT_MOUNTS_HASH_SIZE    50
304803
+
304803
+struct ext_mount {
304803
+	char *mountpoint;
304803
+	struct list_head mount;
304803
+	struct list_head mounts;
304803
+};
304803
+static struct list_head ext_mounts_hash[EXT_MOUNTS_HASH_SIZE];
304803
+static unsigned int ext_mounts_hash_init_done = 0;
304803
+static pthread_mutex_t ext_mount_hash_mutex = PTHREAD_MUTEX_INITIALIZER;
304803
+
304803
 unsigned int linux_version_code(void)
304803
 {
304803
 	struct utsname my_utsname;
304803
@@ -422,6 +433,127 @@ char *make_mnt_name_string(char *path)
304803
 	return mnt_name;
304803
 }
304803
 
304803
+static void ext_mounts_hash_init(void)
304803
+{
304803
+	int i;
304803
+	for (i = 0; i < EXT_MOUNTS_HASH_SIZE; i++)
304803
+		INIT_LIST_HEAD(&ext_mounts_hash[i]);
304803
+	ext_mounts_hash_init_done = 1;
304803
+}
304803
+
304803
+static struct ext_mount *ext_mount_lookup(const char *mountpoint)
304803
+{
304803
+	u_int32_t hval = hash(mountpoint, EXT_MOUNTS_HASH_SIZE);
304803
+	struct list_head *p, *head;
304803
+
304803
+	if (!ext_mounts_hash_init_done)
304803
+		ext_mounts_hash_init();
304803
+
304803
+	if (list_empty(&ext_mounts_hash[hval]))
304803
+		return NULL;
304803
+
304803
+	head = &ext_mounts_hash[hval];
304803
+	list_for_each(p, head) {
304803
+		struct ext_mount *this = list_entry(p, struct ext_mount, mount);
304803
+		if (!strcmp(this->mountpoint, mountpoint))
304803
+			return this;
304803
+	}
304803
+	return NULL;
304803
+}
304803
+
304803
+int ext_mount_add(struct list_head *entry, const char *path)
304803
+{
304803
+	struct ext_mount *em;
304803
+	char *auto_dir;
304803
+	u_int32_t hval;
304803
+	int ret = 0;
304803
+
304803
+	/* Not a mount in the external mount directory */
304803
+	auto_dir = conf_amd_get_auto_dir();
304803
+	if (strncmp(path, auto_dir, strlen(auto_dir))) {
304803
+		free(auto_dir);
304803
+		return 0;
304803
+	}
304803
+	free(auto_dir);
304803
+
304803
+	pthread_mutex_lock(&ext_mount_hash_mutex);
304803
+
304803
+	em = ext_mount_lookup(path);
304803
+	if (em) {
304803
+		struct list_head *p, *head;
304803
+		head = &em->mounts;
304803
+		list_for_each(p, head) {
304803
+			if (p == entry)
304803
+				goto done;
304803
+		}
304803
+		list_add_tail(entry, &em->mounts);
304803
+		ret = 1;
304803
+		goto done;
304803
+	}
304803
+
304803
+	em = malloc(sizeof(struct ext_mount));
304803
+	if (!em) {
304803
+		ret = -1;
304803
+		goto done;
304803
+	}
304803
+
304803
+	em->mountpoint = strdup(path);
304803
+	if (!em->mountpoint) {
304803
+		free(em);
304803
+		ret = -1;
304803
+		goto done;
304803
+	}
304803
+	INIT_LIST_HEAD(&em->mount);
304803
+	INIT_LIST_HEAD(&em->mounts);
304803
+
304803
+	hval = hash(path, EXT_MOUNTS_HASH_SIZE);
304803
+	list_add_tail(&em->mount, &ext_mounts_hash[hval]);
304803
+
304803
+	list_add_tail(entry, &em->mounts);
304803
+
304803
+	ret = 1;
304803
+done:
304803
+	pthread_mutex_unlock(&ext_mount_hash_mutex);
304803
+	return ret;
304803
+}
304803
+
304803
+int ext_mount_remove(struct list_head *entry, const char *path)
304803
+{
304803
+	struct ext_mount *em;
304803
+	char *auto_dir;
304803
+	int ret = 0;
304803
+
304803
+	/* Not a mount in the external mount directory */
304803
+	auto_dir = conf_amd_get_auto_dir();
304803
+	if (strncmp(path, auto_dir, strlen(auto_dir))) {
304803
+		free(auto_dir);
304803
+		return 0;
304803
+	}
304803
+	free(auto_dir);
304803
+
304803
+	pthread_mutex_lock(&ext_mount_hash_mutex);
304803
+
304803
+	em = ext_mount_lookup(path);
304803
+	if (!em)
304803
+		goto done;
304803
+
304803
+	list_del_init(entry);
304803
+
304803
+	if (!list_empty(&em->mounts))
304803
+		goto done;
304803
+	else {
304803
+		list_del_init(&em->mount);
304803
+		if (list_empty(&em->mount)) {
304803
+			free(em->mountpoint);
304803
+			free(em);
304803
+		}
304803
+		ret = 1;
304803
+	}
304803
+done:
304803
+	pthread_mutex_unlock(&ext_mount_hash_mutex);
304803
+	return ret;
304803
+}
304803
+
304803
 /*
304803
  * Get list of mounts under path in longest->shortest order
304803
  */