Home | History | Annotate | Line # | Download | only in kern
kern_fileassoc.c revision 1.13
      1  1.13      yamt /* $NetBSD: kern_fileassoc.c,v 1.13 2006/12/08 13:23:22 yamt Exp $ */
      2   1.1      elad 
      3   1.1      elad /*-
      4   1.1      elad  * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
      5   1.1      elad  * All rights reserved.
      6   1.1      elad  *
      7   1.1      elad  * Redistribution and use in source and binary forms, with or without
      8   1.1      elad  * modification, are permitted provided that the following conditions
      9   1.1      elad  * are met:
     10   1.1      elad  * 1. Redistributions of source code must retain the above copyright
     11   1.1      elad  *    notice, this list of conditions and the following disclaimer.
     12   1.1      elad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      elad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      elad  *    documentation and/or other materials provided with the distribution.
     15   1.1      elad  * 3. All advertising materials mentioning features or use of this software
     16   1.1      elad  *    must display the following acknowledgement:
     17   1.1      elad  *      This product includes software developed by Elad Efrat.
     18   1.1      elad  * 4. The name of the author may not be used to endorse or promote products
     19   1.1      elad  *    derived from this software without specific prior written permission.
     20   1.1      elad  *
     21   1.1      elad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1      elad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1      elad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1      elad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1      elad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1      elad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1      elad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1      elad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1      elad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1      elad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1      elad  */
     32   1.1      elad 
     33   1.7   xtraeme #include <sys/cdefs.h>
     34  1.13      yamt __KERNEL_RCSID(0, "$NetBSD: kern_fileassoc.c,v 1.13 2006/12/08 13:23:22 yamt Exp $");
     35  1.13      yamt 
     36  1.13      yamt #include "opt_fileassoc.h"
     37   1.7   xtraeme 
     38   1.1      elad #include <sys/param.h>
     39   1.1      elad #include <sys/mount.h>
     40   1.1      elad #include <sys/queue.h>
     41   1.1      elad #include <sys/malloc.h>
     42   1.1      elad #include <sys/vnode.h>
     43   1.1      elad #include <sys/namei.h>
     44   1.1      elad #include <sys/exec.h>
     45   1.1      elad #include <sys/proc.h>
     46   1.1      elad #include <sys/inttypes.h>
     47   1.1      elad #include <sys/errno.h>
     48   1.1      elad #include <sys/fileassoc.h>
     49   1.3      elad #include <sys/hash.h>
     50   1.9     blymn #include <sys/fstypes.h>
     51   1.9     blymn 
     52  1.13      yamt /* Max. number of hooks. */
     53  1.13      yamt #ifndef FILEASSOC_NHOOKS
     54  1.13      yamt #define	FILEASSOC_NHOOKS	4
     55  1.13      yamt #endif /* !FILEASSOC_NHOOKS */
     56  1.13      yamt 
     57   1.9     blymn static struct fileassoc_hash_entry *
     58   1.9     blymn fileassoc_file_lookup(struct vnode *, fhandle_t *);
     59   1.9     blymn static struct fileassoc_hash_entry *
     60   1.9     blymn fileassoc_file_add(struct vnode *, fhandle_t *);
     61   1.1      elad 
     62   1.3      elad /*
     63   1.3      elad  * Hook entry.
     64   1.3      elad  * Includes the hook name for identification and private hook clear callback.
     65   1.3      elad  */
     66   1.3      elad struct fileassoc_hook {
     67   1.3      elad 	const char *hook_name;			/* Hook name. */
     68   1.3      elad 	fileassoc_cleanup_cb_t hook_cleanup_cb;	/* Hook clear callback. */
     69   1.3      elad };
     70   1.3      elad 
     71   1.3      elad /* An entry in the per-device hash table. */
     72   1.3      elad struct fileassoc_hash_entry {
     73   1.9     blymn 	fhandle_t *handle;				/* File handle */
     74   1.3      elad 	void *hooks[FILEASSOC_NHOOKS];			/* Hooks. */
     75   1.3      elad 	LIST_ENTRY(fileassoc_hash_entry) entries;	/* List pointer. */
     76   1.3      elad };
     77   1.3      elad 
     78   1.3      elad LIST_HEAD(fileassoc_hashhead, fileassoc_hash_entry);
     79   1.3      elad 
     80   1.3      elad struct fileassoc_table {
     81   1.3      elad 	struct fileassoc_hashhead *hash_tbl;
     82   1.3      elad 	size_t hash_size;				/* Number of slots. */
     83   1.3      elad 	struct mount *tbl_mntpt;
     84   1.3      elad 	u_long hash_mask;
     85   1.3      elad 	void *tables[FILEASSOC_NHOOKS];
     86   1.3      elad 	LIST_ENTRY(fileassoc_table) hash_list;		/* List pointer. */
     87   1.3      elad };
     88   1.8     blymn 
     89   1.4      cube struct fileassoc_hook fileassoc_hooks[FILEASSOC_NHOOKS];
     90   1.4      cube int fileassoc_nhooks;
     91   1.4      cube 
     92   1.1      elad /* Global list of hash tables, one per device. */
     93   1.1      elad LIST_HEAD(, fileassoc_table) fileassoc_tables;
     94   1.8     blymn 
     95   1.1      elad /*
     96   1.9     blymn  * Hashing function: Takes a number modulus the mask to give back an
     97   1.9     blymn  * index into the hash table.
     98   1.1      elad  */
     99   1.9     blymn #define FILEASSOC_HASH(tbl, handle)	\
    100  1.10     blymn 	(hash32_buf((handle), FHANDLE_SIZE(handle), HASH32_BUF_INIT) \
    101   1.1      elad 	 & ((tbl)->hash_mask))
    102   1.1      elad 
    103   1.1      elad /*
    104   1.1      elad  * Initialize the fileassoc subsystem.
    105   1.1      elad  */
    106   1.1      elad void
    107   1.1      elad fileassoc_init(void)
    108   1.1      elad {
    109   1.1      elad 	memset(fileassoc_hooks, 0, sizeof(fileassoc_hooks));
    110   1.1      elad 	fileassoc_nhooks = 0;
    111   1.1      elad }
    112   1.1      elad 
    113   1.1      elad /*
    114   1.1      elad  * Register a new hook.
    115   1.1      elad  */
    116   1.1      elad fileassoc_t
    117   1.1      elad fileassoc_register(const char *name, fileassoc_cleanup_cb_t cleanup_cb)
    118   1.1      elad {
    119   1.1      elad 	int i;
    120   1.1      elad 
    121   1.1      elad 	if (fileassoc_nhooks >= FILEASSOC_NHOOKS)
    122   1.1      elad 		return (-1);
    123   1.1      elad 
    124   1.1      elad 	for (i = 0; i < FILEASSOC_NHOOKS; i++)
    125   1.1      elad 		if (fileassoc_hooks[i].hook_name == NULL)
    126   1.1      elad 			break;
    127   1.1      elad 
    128   1.1      elad 	fileassoc_hooks[i].hook_name = name;
    129   1.1      elad 	fileassoc_hooks[i].hook_cleanup_cb = cleanup_cb;
    130   1.1      elad 
    131   1.1      elad 	fileassoc_nhooks++;
    132   1.1      elad 
    133   1.1      elad 	return (i);
    134   1.1      elad }
    135   1.1      elad 
    136   1.1      elad /*
    137   1.1      elad  * Deregister a hook.
    138   1.1      elad  */
    139   1.1      elad int
    140   1.1      elad fileassoc_deregister(fileassoc_t id)
    141   1.1      elad {
    142   1.1      elad 	if (id < 0 || id >= FILEASSOC_NHOOKS)
    143   1.1      elad 		return (EINVAL);
    144   1.1      elad 
    145   1.1      elad 	fileassoc_hooks[id].hook_name = NULL;
    146   1.1      elad 	fileassoc_hooks[id].hook_cleanup_cb = NULL;
    147   1.1      elad 
    148   1.1      elad 	fileassoc_nhooks--;
    149   1.1      elad 
    150   1.1      elad 	return (0);
    151   1.1      elad }
    152   1.1      elad 
    153   1.1      elad /*
    154   1.1      elad  * Get the hash table for the specified device.
    155   1.1      elad  */
    156   1.3      elad static struct fileassoc_table *
    157   1.1      elad fileassoc_table_lookup(struct mount *mp)
    158   1.1      elad {
    159   1.1      elad 	struct fileassoc_table *tbl;
    160   1.1      elad 
    161   1.1      elad 	LIST_FOREACH(tbl, &fileassoc_tables, hash_list) {
    162   1.1      elad 		if (tbl->tbl_mntpt == mp)
    163   1.1      elad 			return (tbl);
    164   1.1      elad 	}
    165   1.1      elad 
    166   1.1      elad 	return (NULL);
    167   1.1      elad }
    168   1.1      elad 
    169   1.1      elad /*
    170   1.8     blymn  * Perform a lookup on a hash table.  If hint is non-zero then use the value
    171   1.8     blymn  * of the hint as the identifier instead of performing a lookup for the
    172   1.8     blymn  * fileid.
    173   1.1      elad  */
    174   1.3      elad static struct fileassoc_hash_entry *
    175   1.9     blymn fileassoc_file_lookup(struct vnode *vp, fhandle_t *hint)
    176   1.1      elad {
    177   1.1      elad 	struct fileassoc_table *tbl;
    178   1.1      elad 	struct fileassoc_hashhead *tble;
    179   1.1      elad 	struct fileassoc_hash_entry *e;
    180   1.1      elad 	size_t indx;
    181   1.9     blymn 	fhandle_t *th;
    182   1.1      elad 	int error;
    183   1.1      elad 
    184   1.9     blymn 	if (hint == NULL) {
    185   1.9     blymn 		error = vfs_composefh_alloc(vp, &th);
    186   1.8     blymn 		if (error)
    187   1.8     blymn 			return (NULL);
    188   1.8     blymn 	} else
    189   1.8     blymn 		th = hint;
    190   1.1      elad 
    191   1.1      elad 	tbl = fileassoc_table_lookup(vp->v_mount);
    192  1.10     blymn 	if (tbl == NULL) {
    193  1.10     blymn 		if (hint == NULL)
    194  1.10     blymn 			vfs_composefh_free(th);
    195  1.10     blymn 
    196   1.1      elad 		return (NULL);
    197  1.10     blymn 	}
    198   1.1      elad 
    199   1.8     blymn 	indx = FILEASSOC_HASH(tbl, th);
    200   1.9     blymn 	tble = &(tbl->hash_tbl[indx]);
    201   1.1      elad 
    202   1.1      elad 	LIST_FOREACH(e, tble, entries) {
    203   1.9     blymn 		if ((e != NULL) &&
    204  1.10     blymn 		    ((FHANDLE_FILEID(e->handle)->fid_len ==
    205  1.10     blymn 		     FHANDLE_FILEID(th)->fid_len)) &&
    206   1.9     blymn 		    (memcmp(FHANDLE_FILEID(e->handle), FHANDLE_FILEID(th),
    207  1.11      elad 			   (FHANDLE_FILEID(th))->fid_len) == 0)) {
    208  1.11      elad 			if (hint == NULL)
    209  1.11      elad 				vfs_composefh_free(th);
    210  1.11      elad 
    211   1.1      elad 			return (e);
    212  1.11      elad 		}
    213   1.1      elad 	}
    214   1.1      elad 
    215  1.10     blymn 	if (hint == NULL)
    216  1.10     blymn 		vfs_composefh_free(th);
    217  1.10     blymn 
    218   1.1      elad 	return (NULL);
    219   1.1      elad }
    220   1.1      elad 
    221   1.1      elad /*
    222   1.1      elad  * Return hook data associated with a vnode.
    223   1.1      elad  */
    224   1.1      elad void *
    225   1.1      elad fileassoc_lookup(struct vnode *vp, fileassoc_t id)
    226   1.1      elad {
    227   1.9     blymn         struct fileassoc_hash_entry *mhe;
    228   1.8     blymn 
    229   1.9     blymn         mhe = fileassoc_file_lookup(vp, NULL);
    230   1.9     blymn         if (mhe == NULL)
    231   1.9     blymn                 return (NULL);
    232   1.1      elad 
    233   1.9     blymn         return (mhe->hooks[id]);
    234   1.1      elad }
    235   1.1      elad 
    236   1.1      elad /*
    237   1.1      elad  * Create a new fileassoc table.
    238   1.1      elad  */
    239   1.1      elad int
    240   1.1      elad fileassoc_table_add(struct mount *mp, size_t size)
    241   1.1      elad {
    242   1.1      elad 	struct fileassoc_table *tbl;
    243   1.1      elad 
    244   1.1      elad 	/* Check for existing table for device. */
    245   1.1      elad 	if (fileassoc_table_lookup(mp) != NULL)
    246   1.1      elad 		return (EEXIST);
    247   1.1      elad 
    248   1.1      elad 	/* Allocate and initialize a Veriexec hash table. */
    249   1.1      elad 	tbl = malloc(sizeof(*tbl), M_TEMP, M_WAITOK | M_ZERO);
    250   1.1      elad 	tbl->hash_size = size;
    251   1.1      elad 	tbl->tbl_mntpt = mp;
    252   1.1      elad 	tbl->hash_tbl = hashinit(size, HASH_LIST, M_TEMP,
    253   1.1      elad 				 M_WAITOK | M_ZERO, &tbl->hash_mask);
    254   1.1      elad 
    255   1.1      elad 	LIST_INSERT_HEAD(&fileassoc_tables, tbl, hash_list);
    256   1.1      elad 
    257   1.1      elad 	return (0);
    258   1.1      elad }
    259   1.1      elad 
    260   1.1      elad /*
    261   1.1      elad  * Delete a table.
    262   1.1      elad  */
    263   1.1      elad int
    264   1.1      elad fileassoc_table_delete(struct mount *mp)
    265   1.1      elad {
    266   1.1      elad 	struct fileassoc_table *tbl;
    267   1.1      elad 	struct fileassoc_hashhead *hh;
    268   1.1      elad 	u_long i;
    269   1.1      elad 	int j;
    270   1.1      elad 
    271   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    272   1.1      elad 	if (tbl == NULL)
    273   1.1      elad 		return (EEXIST);
    274   1.1      elad 
    275   1.1      elad 	/* Remove all entries from the table and lists */
    276   1.1      elad 	hh = tbl->hash_tbl;
    277   1.1      elad 	for (i = 0; i < tbl->hash_size; i++) {
    278   1.1      elad 		struct fileassoc_hash_entry *mhe;
    279   1.1      elad 
    280   1.1      elad 		while (LIST_FIRST(&hh[i]) != NULL) {
    281   1.1      elad 			mhe = LIST_FIRST(&hh[i]);
    282   1.1      elad 			LIST_REMOVE(mhe, entries);
    283   1.1      elad 
    284   1.1      elad 			for (j = 0; j < fileassoc_nhooks; j++)
    285   1.1      elad 				if (fileassoc_hooks[j].hook_cleanup_cb != NULL)
    286   1.2      elad 					(fileassoc_hooks[j].hook_cleanup_cb)
    287   1.2      elad 					    (mhe->hooks[j],
    288   1.2      elad 					    FILEASSOC_CLEANUP_FILE);
    289   1.1      elad 
    290  1.10     blymn 			vfs_composefh_free(mhe->handle);
    291   1.1      elad 			free(mhe, M_TEMP);
    292   1.1      elad 		}
    293   1.1      elad 	}
    294   1.1      elad 
    295   1.1      elad 	for (j = 0; j < fileassoc_nhooks; j++)
    296   1.1      elad 		if (fileassoc_hooks[j].hook_cleanup_cb != NULL)
    297   1.2      elad 			(fileassoc_hooks[j].hook_cleanup_cb)(tbl->tables[j],
    298   1.2      elad 			    FILEASSOC_CLEANUP_TABLE);
    299   1.1      elad 
    300   1.1      elad 	/* Remove hash table and sysctl node */
    301   1.1      elad 	hashdone(tbl->hash_tbl, M_TEMP);
    302   1.1      elad 	LIST_REMOVE(tbl, hash_list);
    303   1.1      elad 
    304   1.1      elad 	return (0);
    305   1.1      elad }
    306   1.1      elad 
    307   1.1      elad /*
    308   1.6  christos  * Run a callback for each hook entry in a table.
    309   1.6  christos  */
    310   1.6  christos int
    311   1.6  christos fileassoc_table_run(struct mount *mp, fileassoc_t id, fileassoc_cb_t cb)
    312   1.6  christos {
    313   1.6  christos 	struct fileassoc_table *tbl;
    314   1.6  christos 	struct fileassoc_hashhead *hh;
    315   1.6  christos 	u_long i;
    316   1.6  christos 
    317   1.6  christos 	tbl = fileassoc_table_lookup(mp);
    318   1.6  christos 	if (tbl == NULL)
    319   1.6  christos 		return (EEXIST);
    320   1.6  christos 
    321   1.6  christos 	hh = tbl->hash_tbl;
    322   1.6  christos 	for (i = 0; i < tbl->hash_size; i++) {
    323   1.6  christos 		struct fileassoc_hash_entry *mhe;
    324   1.6  christos 
    325   1.6  christos 		LIST_FOREACH(mhe, &hh[i], entries) {
    326   1.6  christos 			if (mhe->hooks[id] != NULL)
    327   1.6  christos 				cb(mhe->hooks[id]);
    328   1.6  christos 		}
    329   1.6  christos 	}
    330   1.6  christos 
    331   1.6  christos 	return (0);
    332   1.6  christos }
    333   1.6  christos 
    334   1.6  christos /*
    335   1.1      elad  * Clear a table for a given hook.
    336   1.1      elad  */
    337   1.1      elad int
    338   1.1      elad fileassoc_table_clear(struct mount *mp, fileassoc_t id)
    339   1.1      elad {
    340   1.1      elad 	struct fileassoc_table *tbl;
    341   1.1      elad 	struct fileassoc_hashhead *hh;
    342   1.1      elad 	fileassoc_cleanup_cb_t cleanup_cb;
    343   1.1      elad 	u_long i;
    344   1.1      elad 
    345   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    346   1.1      elad 	if (tbl == NULL)
    347   1.1      elad 		return (EEXIST);
    348   1.1      elad 
    349   1.1      elad 	cleanup_cb = fileassoc_hooks[id].hook_cleanup_cb;
    350   1.1      elad 
    351   1.1      elad 	hh = tbl->hash_tbl;
    352   1.1      elad 	for (i = 0; i < tbl->hash_size; i++) {
    353   1.1      elad 		struct fileassoc_hash_entry *mhe;
    354   1.1      elad 
    355   1.1      elad 		LIST_FOREACH(mhe, &hh[i], entries) {
    356   1.1      elad 			if ((mhe->hooks[id] != NULL) && cleanup_cb != NULL)
    357   1.2      elad 				cleanup_cb(mhe->hooks[id],
    358   1.2      elad 				    FILEASSOC_CLEANUP_FILE);
    359   1.1      elad 
    360   1.1      elad 			mhe->hooks[id] = NULL;
    361   1.1      elad 		}
    362   1.1      elad 	}
    363   1.1      elad 
    364   1.1      elad 	if ((tbl->tables[id] != NULL) && cleanup_cb != NULL)
    365   1.1      elad 		cleanup_cb(tbl->tables[id], FILEASSOC_CLEANUP_TABLE);
    366   1.1      elad 
    367   1.1      elad 	tbl->tables[id] = NULL;
    368   1.1      elad 
    369   1.1      elad 	return (0);
    370   1.1      elad }
    371   1.1      elad 
    372   1.1      elad /*
    373   1.1      elad  * Add hook-specific data on a fileassoc table.
    374   1.1      elad  */
    375   1.1      elad int
    376   1.1      elad fileassoc_tabledata_add(struct mount *mp, fileassoc_t id, void *data)
    377   1.1      elad {
    378   1.1      elad 	struct fileassoc_table *tbl;
    379   1.1      elad 
    380   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    381   1.1      elad 	if (tbl == NULL)
    382   1.1      elad 		return (EFAULT);
    383   1.1      elad 
    384   1.1      elad 	tbl->tables[id] = data;
    385   1.1      elad 
    386   1.1      elad 	return (0);
    387   1.1      elad }
    388   1.1      elad 
    389   1.1      elad /*
    390   1.1      elad  * Clear hook-specific data on a fileassoc table.
    391   1.1      elad  */
    392   1.1      elad int
    393   1.1      elad fileassoc_tabledata_clear(struct mount *mp, fileassoc_t id)
    394   1.1      elad {
    395   1.1      elad 	struct fileassoc_table *tbl;
    396   1.1      elad 
    397   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    398   1.1      elad 	if (tbl == NULL)
    399   1.1      elad 		return (EFAULT);
    400   1.1      elad 
    401   1.1      elad 	tbl->tables[id] = NULL;
    402   1.1      elad 
    403   1.1      elad 	return (0);
    404   1.1      elad }
    405   1.1      elad 
    406   1.1      elad /*
    407   1.1      elad  * Retrieve hook-specific data from a fileassoc table.
    408   1.1      elad  */
    409   1.1      elad void *
    410   1.1      elad fileassoc_tabledata_lookup(struct mount *mp, fileassoc_t id)
    411   1.1      elad {
    412   1.1      elad 	struct fileassoc_table *tbl;
    413   1.1      elad 
    414   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    415   1.1      elad 	if (tbl == NULL)
    416   1.1      elad 		return (NULL);
    417   1.1      elad 
    418   1.1      elad 	return (tbl->tables[id]);
    419   1.1      elad }
    420   1.1      elad 
    421   1.1      elad /*
    422   1.1      elad  * Add a file entry to a table.
    423   1.1      elad  */
    424   1.3      elad static struct fileassoc_hash_entry *
    425   1.9     blymn fileassoc_file_add(struct vnode *vp, fhandle_t *hint)
    426   1.1      elad {
    427   1.1      elad 	struct fileassoc_table *tbl;
    428   1.1      elad 	struct fileassoc_hashhead *vhh;
    429   1.1      elad 	struct fileassoc_hash_entry *e;
    430   1.1      elad 	size_t indx;
    431   1.9     blymn 	fhandle_t *th;
    432   1.1      elad 	int error;
    433   1.1      elad 
    434  1.12      elad 	if (hint == NULL) {
    435   1.9     blymn 		error = vfs_composefh_alloc(vp, &th);
    436   1.8     blymn 		if (error)
    437   1.8     blymn 			return (NULL);
    438   1.8     blymn 	} else
    439   1.9     blymn 		th = hint;
    440   1.1      elad 
    441   1.9     blymn 	e = fileassoc_file_lookup(vp, th);
    442  1.10     blymn 	if (e != NULL) {
    443  1.10     blymn 		if (hint == NULL)
    444  1.10     blymn 			vfs_composefh_free(th);
    445  1.10     blymn 
    446   1.1      elad 		return (e);
    447  1.10     blymn 	}
    448   1.1      elad 
    449   1.1      elad 	tbl = fileassoc_table_lookup(vp->v_mount);
    450  1.10     blymn 	if (tbl == NULL) {
    451  1.10     blymn 		if (hint == NULL)
    452  1.10     blymn 			vfs_composefh_free(th);
    453  1.10     blymn 
    454   1.1      elad 		return (NULL);
    455  1.10     blymn 	}
    456   1.1      elad 
    457   1.9     blymn 	indx = FILEASSOC_HASH(tbl, th);
    458   1.9     blymn 	vhh = &(tbl->hash_tbl[indx]);
    459   1.1      elad 
    460   1.1      elad 	e = malloc(sizeof(*e), M_TEMP, M_WAITOK | M_ZERO);
    461   1.9     blymn 	e->handle = th;
    462   1.1      elad 	LIST_INSERT_HEAD(vhh, e, entries);
    463   1.1      elad 
    464   1.1      elad 	return (e);
    465   1.1      elad }
    466   1.1      elad 
    467   1.1      elad /*
    468   1.1      elad  * Delete a file entry from a table.
    469   1.1      elad  */
    470   1.1      elad int
    471   1.1      elad fileassoc_file_delete(struct vnode *vp)
    472   1.1      elad {
    473   1.1      elad 	struct fileassoc_hash_entry *mhe;
    474   1.1      elad 	int i;
    475   1.1      elad 
    476   1.9     blymn 	mhe = fileassoc_file_lookup(vp, NULL);
    477   1.1      elad 	if (mhe == NULL)
    478   1.1      elad 		return (ENOENT);
    479   1.1      elad 
    480   1.1      elad 	LIST_REMOVE(mhe, entries);
    481   1.1      elad 
    482   1.1      elad 	for (i = 0; i < fileassoc_nhooks; i++)
    483   1.1      elad 		if (fileassoc_hooks[i].hook_cleanup_cb != NULL)
    484   1.1      elad 			(fileassoc_hooks[i].hook_cleanup_cb)(mhe->hooks[i],
    485   1.1      elad 			    FILEASSOC_CLEANUP_FILE);
    486   1.1      elad 
    487   1.1      elad 	free(mhe, M_TEMP);
    488   1.1      elad 
    489   1.1      elad 	return (0);
    490   1.1      elad }
    491   1.1      elad 
    492   1.1      elad /*
    493   1.9     blymn  * Add a hook to a vnode.
    494   1.1      elad  */
    495   1.1      elad int
    496   1.9     blymn fileassoc_add(struct vnode *vp, fileassoc_t id, void *data)
    497   1.1      elad {
    498   1.1      elad 	struct fileassoc_hash_entry *e;
    499   1.1      elad 
    500   1.9     blymn 	e = fileassoc_file_lookup(vp, NULL);
    501   1.1      elad 	if (e == NULL) {
    502   1.9     blymn 		e = fileassoc_file_add(vp, NULL);
    503   1.1      elad 		if (e == NULL)
    504   1.1      elad 			return (ENOTDIR);
    505   1.1      elad 	}
    506   1.1      elad 
    507   1.1      elad 	if (e->hooks[id] != NULL)
    508   1.1      elad 		return (EEXIST);
    509   1.1      elad 
    510   1.1      elad 	e->hooks[id] = data;
    511   1.1      elad 
    512   1.1      elad 	return (0);
    513   1.1      elad }
    514   1.1      elad 
    515   1.1      elad /*
    516   1.1      elad  * Clear a hook from a vnode.
    517   1.1      elad  */
    518   1.1      elad int
    519   1.1      elad fileassoc_clear(struct vnode *vp, fileassoc_t id)
    520   1.1      elad {
    521   1.1      elad 	struct fileassoc_hash_entry *mhe;
    522   1.1      elad 	fileassoc_cleanup_cb_t cleanup_cb;
    523   1.1      elad 
    524   1.9     blymn 	mhe = fileassoc_file_lookup(vp, NULL);
    525   1.1      elad 	if (mhe == NULL)
    526   1.1      elad 		return (ENOENT);
    527   1.1      elad 
    528   1.1      elad 	cleanup_cb = fileassoc_hooks[id].hook_cleanup_cb;
    529   1.1      elad 	if ((mhe->hooks[id] != NULL) && cleanup_cb != NULL)
    530   1.1      elad 		cleanup_cb(mhe->hooks[id], FILEASSOC_CLEANUP_FILE);
    531   1.1      elad 
    532   1.1      elad 	mhe->hooks[id] = NULL;
    533   1.1      elad 
    534   1.1      elad 	return (0);
    535   1.1      elad }
    536