Subject: Pending entries support for fileassoc(9)
To: None <tech-kern@netbsd.org>
From: Elad Efrat <elad@bsd.org.il>
List: tech-kern
Date: 12/02/2007 19:49:38
This is a multi-part message in MIME format.
--------------090505000503030803010606
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Hi,

Attached is a simple diff adding support for "pending entries" to
fileassoc(9). Pending entries are entries for files that do not
necessarily exist yet, or in other words, live on file-systems that
are not mounted.

There are several reasons why this pesudo-persistent meta-data storage
support is helpful; one is that Veriexec can be made to load
fingerprints before any file-systems are mounted, rather than after all
of them are.

The implementation is very simple: the fileassoc(9) KPI gets a new
function, fileassoc_add_pending(), that takes filename, fileassoc_id,
and data to attach. It adds an entry to a list that's traversed each
time a new file-system is mounted.

Until there's a proper event notification system, there's a simple
hook called fileassoc_mount_notify() to let fileassoc(9) know a new
file-system was mounted.

This has been manually tested with Veriexec.

Veriexec support will come later, probably in the form of a new keyword
in the config file, allowing entries to be loaded as "pending" if they
don't exist.

Comments? :)

-e

--------------090505000503030803010606
Content-Type: text/plain;
 name="fileassoc_pending.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="fileassoc_pending.diff"

Index: kern/kern_fileassoc.c
===================================================================
RCS file: /usr/cvs/src/sys/kern/kern_fileassoc.c,v
retrieving revision 1.29
diff -u -p -r1.29 kern_fileassoc.c
--- kern/kern_fileassoc.c	15 May 2007 19:47:45 -0000	1.29
+++ kern/kern_fileassoc.c	2 Dec 2007 15:16:40 -0000
@@ -92,6 +92,16 @@ struct fileassoc_table {
 	specificdata_reference data;
 };
 
+static TAILQ_HEAD(, fileassoc_pending_entry) fileassoc_pending =
+    TAILQ_HEAD_INITIALIZER(fileassoc_pending);
+
+struct fileassoc_pending_entry {
+	char *pe_filename;
+	fileassoc_t pe_assoc;
+	void *pe_data;
+	TAILQ_ENTRY(fileassoc_pending_entry) pe_queue;
+};
+
 /*
  * Hashing function: Takes a number modulus the mask to give back an
  * index into the hash table.
@@ -586,3 +596,90 @@ fileassoc_clear(struct vnode *vp, fileas
 
 	return (0);
 }
+
+/*
+ * Add a pending entry.
+ *
+ * This routine allows a subsystem to create a pending fileassoc entry for a
+ * file that does not yet exist, ie., resides on a file-system that hasn't
+ * been mounted.
+ */
+int
+fileassoc_add_pending(const char *filename, fileassoc_t id, void *data)
+{
+	struct fileassoc_pending_entry *pe;
+	int error = 0;
+
+	/* Allocate and initialize a new entry. */
+	pe = kmem_zalloc(sizeof(*pe), KM_SLEEP);
+	pe->pe_filename = PNBUF_GET();
+	if (strlcpy(pe->pe_filename, filename, MAXPATHLEN) >= MAXPATHLEN) {
+		error = ENAMETOOLONG;
+		goto out;
+	}
+	pe->pe_assoc = id;
+	pe->pe_data = data;
+
+	/* Add it to the pending queue. */
+	TAILQ_INSERT_TAIL(&fileassoc_pending, pe, pe_queue);
+
+ out:
+	if (error) {
+		PNBUF_PUT(pe->pe_filename);
+		kmem_free(pe, sizeof(*pe));
+	}
+
+	return (error);
+}
+
+/*
+ * This is a temporary wrapper until there's a better way to notify a new
+ * file-system was just mounted.
+ *
+ * Called whenever a file-system is mounted. Traverses the pending list,
+ * performing a lookup on each entry. Entries that are found are removed and
+ * added properly; otherwise they remain inside the pending queue.
+ */
+void
+fileassoc_mount_notify(struct mount *mp)
+{
+	struct fileassoc_pending_entry *pe;
+	const char *mountpoint = NULL;
+	size_t len = 0;
+
+	/*
+	 * XXX Should we hold a lock here to prevent new file-systems from
+	 * XXX being added while we traverse the list? (mountlist_lock)
+	 */
+
+	mountpoint = mp->mnt_stat.f_mntonname;
+	len = strlen(mountpoint);
+
+	TAILQ_FOREACH(pe, &fileassoc_pending, pe_queue) {
+		struct nameidata nid;
+		int error;
+
+		/* Skip entries not on the file-system mounted. */
+		if (strncmp(mountpoint, pe->pe_filename, len) != 0) {
+			continue;
+		}
+
+		NDINIT(&nid, LOOKUP, FOLLOW, UIO_SYSSPACE, pe->pe_filename, curlwp);
+		error = namei(&nid);
+		if (error)
+			continue;
+
+		error = fileassoc_add(nid.ni_vp, pe->pe_assoc, pe->pe_data);
+		if (error) {
+			/*
+			 * XXX Errors here usually indicate memory shortage.
+			 */
+		} else {
+			PNBUF_PUT(pe->pe_filename);
+			kmem_free(pe, sizeof(*pe));
+			TAILQ_REMOVE(&fileassoc_pending, pe, pe_queue);
+		}
+
+		vrele(nid.ni_vp);
+	}
+}
Index: kern/vfs_syscalls.c
===================================================================
RCS file: /usr/cvs/src/sys/kern/vfs_syscalls.c,v
retrieving revision 1.331
diff -u -p -r1.331 vfs_syscalls.c
--- kern/vfs_syscalls.c	24 Oct 2007 15:28:55 -0000	1.331
+++ kern/vfs_syscalls.c	2 Dec 2007 12:08:26 -0000
@@ -356,6 +356,9 @@ mount_domount(struct lwp *l, struct vnod
 	if (error)
 		vrele(vp);
 	*vpp = NULL;
+#ifdef FILEASSOC
+	fileassoc_mount_notify(mp);
+#endif /* FILEASSOC */
 	return error;
 }
 
Index: kern/init_main.c
===================================================================
RCS file: /usr/cvs/src/sys/kern/init_main.c,v
retrieving revision 1.330
diff -u -p -r1.330 init_main.c
--- kern/init_main.c	15 Nov 2007 20:12:04 -0000	1.330
+++ kern/init_main.c	2 Dec 2007 14:47:41 -0000
@@ -164,6 +164,9 @@ __KERNEL_RCSID(0, "$NetBSD: init_main.c,
 #include <sys/ktrace.h>
 #endif
 #include <sys/kauth.h>
+#ifdef FILEASSOC
+#include <sys/fileassoc.h>
+#endif /* FILEASSOC */
 #include <net80211/ieee80211_netbsd.h>
 
 #include <sys/syscall.h>
@@ -602,6 +605,8 @@ main(void)
 	VOP_UNLOCK(rootvnode, 0);
 	cwdi0.cwdi_rdir = NULL;
 
+	fileassoc_mount_notify(CIRCLEQ_FIRST(&mountlist));
+
 	/*
 	 * Now that root is mounted, we can fixup initproc's CWD
 	 * info.  All other processes are kthreads, which merely
Index: sys/fileassoc.h
===================================================================
RCS file: /usr/cvs/src/sys/sys/fileassoc.h,v
retrieving revision 1.11
diff -u -p -r1.11 fileassoc.h
--- sys/fileassoc.h	15 May 2007 19:47:44 -0000	1.11
+++ sys/fileassoc.h	2 Dec 2007 12:08:48 -0000
@@ -46,5 +46,9 @@ int fileassoc_file_delete(struct vnode *
 int fileassoc_add(struct vnode *, fileassoc_t, void *);
 int fileassoc_clear(struct vnode *, fileassoc_t);
 int fileassoc_table_run(struct mount *, fileassoc_t, fileassoc_cb_t, void *);
+int fileassoc_add_pending(const char *, fileassoc_t, void *);
+
+/* XXX elad - temporary. */
+void fileassoc_mount_notify(struct mount *);
 
 #endif /* !_SYS_FILEASSOC_H_ */

--------------090505000503030803010606--