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

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