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

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