Home | History | Annotate | Line # | Download | only in kern
kern_fileassoc.c revision 1.22
      1  1.22      elad /* $NetBSD: kern_fileassoc.c,v 1.22 2007/02/06 01:09:48 elad 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.19      elad  * 3. The name of the author may not be used to endorse or promote products
     16   1.1      elad  *    derived from this software without specific prior written permission.
     17   1.1      elad  *
     18   1.1      elad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1      elad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1      elad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1      elad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1      elad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1      elad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1      elad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1      elad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1      elad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1      elad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1      elad  */
     29   1.1      elad 
     30   1.7   xtraeme #include <sys/cdefs.h>
     31  1.22      elad __KERNEL_RCSID(0, "$NetBSD: kern_fileassoc.c,v 1.22 2007/02/06 01:09:48 elad Exp $");
     32  1.13      yamt 
     33  1.13      yamt #include "opt_fileassoc.h"
     34   1.7   xtraeme 
     35   1.1      elad #include <sys/param.h>
     36   1.1      elad #include <sys/mount.h>
     37   1.1      elad #include <sys/queue.h>
     38   1.1      elad #include <sys/malloc.h>
     39   1.1      elad #include <sys/vnode.h>
     40   1.1      elad #include <sys/namei.h>
     41   1.1      elad #include <sys/exec.h>
     42   1.1      elad #include <sys/proc.h>
     43   1.1      elad #include <sys/inttypes.h>
     44   1.1      elad #include <sys/errno.h>
     45   1.1      elad #include <sys/fileassoc.h>
     46  1.14      yamt #include <sys/specificdata.h>
     47   1.3      elad #include <sys/hash.h>
     48   1.9     blymn #include <sys/fstypes.h>
     49  1.14      yamt #include <sys/kmem.h>
     50  1.14      yamt #include <sys/once.h>
     51  1.13      yamt 
     52  1.22      elad #define	FILEASSOC_INITIAL_TABLESIZE	128
     53  1.22      elad 
     54   1.9     blymn static struct fileassoc_hash_entry *
     55   1.9     blymn fileassoc_file_lookup(struct vnode *, fhandle_t *);
     56   1.9     blymn static struct fileassoc_hash_entry *
     57   1.9     blymn fileassoc_file_add(struct vnode *, fhandle_t *);
     58  1.22      elad static struct fileassoc_table *fileassoc_table_resize(struct fileassoc_table *);
     59  1.22      elad 
     60   1.1      elad 
     61  1.14      yamt static specificdata_domain_t fileassoc_domain;
     62  1.16      yamt static specificdata_key_t fileassoc_mountspecific_key;
     63  1.14      yamt 
     64   1.3      elad /*
     65   1.3      elad  * Hook entry.
     66   1.3      elad  * Includes the hook name for identification and private hook clear callback.
     67   1.3      elad  */
     68  1.14      yamt struct fileassoc {
     69  1.14      yamt 	LIST_ENTRY(fileassoc) list;
     70  1.14      yamt 	const char *name;			/* name. */
     71  1.14      yamt 	fileassoc_cleanup_cb_t cleanup_cb;	/* clear callback. */
     72  1.14      yamt 	specificdata_key_t key;
     73   1.3      elad };
     74   1.3      elad 
     75  1.14      yamt static LIST_HEAD(, fileassoc) fileassoc_list;
     76  1.14      yamt 
     77  1.20      elad /* An entry in the per-mount hash table. */
     78   1.3      elad struct fileassoc_hash_entry {
     79   1.9     blymn 	fhandle_t *handle;				/* File handle */
     80  1.14      yamt 	specificdata_reference data;			/* Hooks. */
     81   1.3      elad 	LIST_ENTRY(fileassoc_hash_entry) entries;	/* List pointer. */
     82   1.3      elad };
     83   1.3      elad 
     84   1.3      elad LIST_HEAD(fileassoc_hashhead, fileassoc_hash_entry);
     85   1.3      elad 
     86   1.3      elad struct fileassoc_table {
     87   1.3      elad 	struct fileassoc_hashhead *hash_tbl;
     88   1.3      elad 	size_t hash_size;				/* Number of slots. */
     89   1.3      elad 	u_long hash_mask;
     90  1.22      elad 	size_t hash_used;				/* # of used slots. */
     91  1.14      yamt 	specificdata_reference data;
     92   1.3      elad };
     93   1.8     blymn 
     94   1.1      elad /*
     95   1.9     blymn  * Hashing function: Takes a number modulus the mask to give back an
     96   1.9     blymn  * index into the hash table.
     97   1.1      elad  */
     98   1.9     blymn #define FILEASSOC_HASH(tbl, handle)	\
     99  1.10     blymn 	(hash32_buf((handle), FHANDLE_SIZE(handle), HASH32_BUF_INIT) \
    100   1.1      elad 	 & ((tbl)->hash_mask))
    101   1.1      elad 
    102  1.14      yamt static void *
    103  1.14      yamt file_getdata(struct fileassoc_hash_entry *e, const struct fileassoc *assoc)
    104  1.14      yamt {
    105  1.14      yamt 
    106  1.14      yamt 	return specificdata_getspecific(fileassoc_domain, &e->data,
    107  1.14      yamt 	    assoc->key);
    108  1.14      yamt }
    109  1.14      yamt 
    110  1.14      yamt static void
    111  1.14      yamt file_setdata(struct fileassoc_hash_entry *e, const struct fileassoc *assoc,
    112  1.14      yamt     void *data)
    113  1.14      yamt {
    114  1.14      yamt 
    115  1.14      yamt 	specificdata_setspecific(fileassoc_domain, &e->data, assoc->key,
    116  1.14      yamt 	    data);
    117  1.14      yamt }
    118  1.14      yamt 
    119  1.14      yamt static void
    120  1.14      yamt file_cleanup(struct fileassoc_hash_entry *e, const struct fileassoc *assoc)
    121  1.14      yamt {
    122  1.14      yamt 	fileassoc_cleanup_cb_t cb;
    123  1.14      yamt 	void *data;
    124  1.14      yamt 
    125  1.14      yamt 	cb = assoc->cleanup_cb;
    126  1.14      yamt 	if (cb == NULL) {
    127  1.14      yamt 		return;
    128  1.14      yamt 	}
    129  1.14      yamt 	data = file_getdata(e, assoc);
    130  1.17      yamt 	(*cb)(data);
    131  1.14      yamt }
    132  1.14      yamt 
    133  1.14      yamt static void
    134  1.14      yamt file_free(struct fileassoc_hash_entry *e)
    135  1.14      yamt {
    136  1.14      yamt 	struct fileassoc *assoc;
    137  1.14      yamt 
    138  1.14      yamt 	LIST_REMOVE(e, entries);
    139  1.14      yamt 
    140  1.14      yamt 	LIST_FOREACH(assoc, &fileassoc_list, list) {
    141  1.14      yamt 		file_cleanup(e, assoc);
    142  1.14      yamt 	}
    143  1.14      yamt 	vfs_composefh_free(e->handle);
    144  1.14      yamt 	specificdata_fini(fileassoc_domain, &e->data);
    145  1.14      yamt 	kmem_free(e, sizeof(*e));
    146  1.14      yamt }
    147  1.14      yamt 
    148  1.16      yamt static void
    149  1.16      yamt table_dtor(void *vp)
    150  1.16      yamt {
    151  1.16      yamt 	struct fileassoc_table *tbl = vp;
    152  1.16      yamt 	struct fileassoc_hashhead *hh;
    153  1.16      yamt 	u_long i;
    154  1.16      yamt 
    155  1.16      yamt 	/* Remove all entries from the table and lists */
    156  1.16      yamt 	hh = tbl->hash_tbl;
    157  1.16      yamt 	for (i = 0; i < tbl->hash_size; i++) {
    158  1.16      yamt 		struct fileassoc_hash_entry *mhe;
    159  1.16      yamt 
    160  1.16      yamt 		while ((mhe = LIST_FIRST(&hh[i])) != NULL) {
    161  1.16      yamt 			file_free(mhe);
    162  1.16      yamt 		}
    163  1.16      yamt 	}
    164  1.16      yamt 
    165  1.16      yamt 	/* Remove hash table and sysctl node */
    166  1.16      yamt 	hashdone(tbl->hash_tbl, M_TEMP);
    167  1.16      yamt 	specificdata_fini(fileassoc_domain, &tbl->data);
    168  1.16      yamt 	kmem_free(tbl, sizeof(*tbl));
    169  1.16      yamt }
    170  1.16      yamt 
    171   1.1      elad /*
    172   1.1      elad  * Initialize the fileassoc subsystem.
    173   1.1      elad  */
    174  1.14      yamt static int
    175   1.1      elad fileassoc_init(void)
    176   1.1      elad {
    177  1.16      yamt 	int error;
    178  1.14      yamt 
    179  1.16      yamt 	error = mount_specific_key_create(&fileassoc_mountspecific_key,
    180  1.16      yamt 	    table_dtor);
    181  1.16      yamt 	if (error) {
    182  1.16      yamt 		return error;
    183  1.16      yamt 	}
    184  1.14      yamt 	fileassoc_domain = specificdata_domain_create();
    185  1.14      yamt 
    186  1.14      yamt 	return 0;
    187   1.1      elad }
    188   1.1      elad 
    189   1.1      elad /*
    190   1.1      elad  * Register a new hook.
    191   1.1      elad  */
    192  1.14      yamt int
    193  1.14      yamt fileassoc_register(const char *name, fileassoc_cleanup_cb_t cleanup_cb,
    194  1.14      yamt     fileassoc_t *result)
    195   1.1      elad {
    196  1.14      yamt 	int error;
    197  1.14      yamt 	specificdata_key_t key;
    198  1.14      yamt 	struct fileassoc *assoc;
    199  1.14      yamt 	static ONCE_DECL(control);
    200  1.14      yamt 
    201  1.16      yamt 	error = RUN_ONCE(&control, fileassoc_init);
    202  1.16      yamt 	if (error) {
    203  1.16      yamt 		return error;
    204  1.16      yamt 	}
    205  1.14      yamt 	error = specificdata_key_create(fileassoc_domain, &key, NULL);
    206  1.14      yamt 	if (error) {
    207  1.14      yamt 		return error;
    208  1.14      yamt 	}
    209  1.14      yamt 	assoc = kmem_alloc(sizeof(*assoc), KM_SLEEP);
    210  1.14      yamt 	assoc->name = name;
    211  1.14      yamt 	assoc->cleanup_cb = cleanup_cb;
    212  1.14      yamt 	assoc->key = key;
    213  1.14      yamt 	LIST_INSERT_HEAD(&fileassoc_list, assoc, list);
    214  1.14      yamt 	*result = assoc;
    215   1.1      elad 
    216  1.14      yamt 	return 0;
    217   1.1      elad }
    218   1.1      elad 
    219   1.1      elad /*
    220   1.1      elad  * Deregister a hook.
    221   1.1      elad  */
    222   1.1      elad int
    223  1.14      yamt fileassoc_deregister(fileassoc_t assoc)
    224   1.1      elad {
    225   1.1      elad 
    226  1.14      yamt 	LIST_REMOVE(assoc, list);
    227  1.21      elad 	specificdata_key_delete(fileassoc_domain, assoc->key);
    228  1.14      yamt 	kmem_free(assoc, sizeof(*assoc));
    229   1.1      elad 
    230  1.14      yamt 	return 0;
    231   1.1      elad }
    232   1.1      elad 
    233   1.1      elad /*
    234   1.1      elad  * Get the hash table for the specified device.
    235   1.1      elad  */
    236   1.3      elad static struct fileassoc_table *
    237   1.1      elad fileassoc_table_lookup(struct mount *mp)
    238   1.1      elad {
    239   1.1      elad 
    240  1.16      yamt 	return mount_getspecific(mp, fileassoc_mountspecific_key);
    241   1.1      elad }
    242   1.1      elad 
    243   1.1      elad /*
    244   1.8     blymn  * Perform a lookup on a hash table.  If hint is non-zero then use the value
    245   1.8     blymn  * of the hint as the identifier instead of performing a lookup for the
    246   1.8     blymn  * fileid.
    247   1.1      elad  */
    248   1.3      elad static struct fileassoc_hash_entry *
    249   1.9     blymn fileassoc_file_lookup(struct vnode *vp, fhandle_t *hint)
    250   1.1      elad {
    251   1.1      elad 	struct fileassoc_table *tbl;
    252   1.1      elad 	struct fileassoc_hashhead *tble;
    253   1.1      elad 	struct fileassoc_hash_entry *e;
    254   1.1      elad 	size_t indx;
    255   1.9     blymn 	fhandle_t *th;
    256   1.1      elad 	int error;
    257   1.1      elad 
    258  1.16      yamt 	tbl = fileassoc_table_lookup(vp->v_mount);
    259  1.16      yamt 	if (tbl == NULL) {
    260  1.16      yamt 		return NULL;
    261  1.16      yamt 	}
    262  1.16      yamt 
    263   1.9     blymn 	if (hint == NULL) {
    264   1.9     blymn 		error = vfs_composefh_alloc(vp, &th);
    265   1.8     blymn 		if (error)
    266   1.8     blymn 			return (NULL);
    267  1.16      yamt 	} else {
    268   1.8     blymn 		th = hint;
    269  1.10     blymn 	}
    270   1.1      elad 
    271   1.8     blymn 	indx = FILEASSOC_HASH(tbl, th);
    272   1.9     blymn 	tble = &(tbl->hash_tbl[indx]);
    273   1.1      elad 
    274   1.1      elad 	LIST_FOREACH(e, tble, entries) {
    275  1.16      yamt 		if (((FHANDLE_FILEID(e->handle)->fid_len ==
    276  1.10     blymn 		     FHANDLE_FILEID(th)->fid_len)) &&
    277   1.9     blymn 		    (memcmp(FHANDLE_FILEID(e->handle), FHANDLE_FILEID(th),
    278  1.11      elad 			   (FHANDLE_FILEID(th))->fid_len) == 0)) {
    279  1.16      yamt 			break;
    280  1.11      elad 		}
    281   1.1      elad 	}
    282   1.1      elad 
    283  1.10     blymn 	if (hint == NULL)
    284  1.10     blymn 		vfs_composefh_free(th);
    285  1.10     blymn 
    286  1.16      yamt 	return e;
    287   1.1      elad }
    288   1.1      elad 
    289   1.1      elad /*
    290   1.1      elad  * Return hook data associated with a vnode.
    291   1.1      elad  */
    292   1.1      elad void *
    293  1.14      yamt fileassoc_lookup(struct vnode *vp, fileassoc_t assoc)
    294   1.1      elad {
    295   1.9     blymn         struct fileassoc_hash_entry *mhe;
    296   1.8     blymn 
    297   1.9     blymn         mhe = fileassoc_file_lookup(vp, NULL);
    298   1.9     blymn         if (mhe == NULL)
    299   1.9     blymn                 return (NULL);
    300   1.1      elad 
    301  1.14      yamt         return file_getdata(mhe, assoc);
    302   1.1      elad }
    303   1.1      elad 
    304  1.22      elad static struct fileassoc_table *
    305  1.22      elad fileassoc_table_resize(struct fileassoc_table *tbl)
    306  1.22      elad {
    307  1.22      elad 	struct fileassoc_table *newtbl;
    308  1.22      elad 	struct fileassoc_hashhead *hh;
    309  1.22      elad 	u_long i;
    310  1.22      elad 
    311  1.22      elad 	/*
    312  1.22      elad 	 * Allocate a new table. Like the condition in fileassoc_file_add(),
    313  1.22      elad 	 * this is also temporary -- just double the number of slots.
    314  1.22      elad 	 */
    315  1.22      elad 	newtbl = kmem_zalloc(sizeof(*newtbl), KM_SLEEP);
    316  1.22      elad 	newtbl->hash_size = (tbl->hash_size * 2);
    317  1.22      elad 	if (newtbl->hash_size < tbl->hash_size)
    318  1.22      elad 		newtbl->hash_size = tbl->hash_size;
    319  1.22      elad 	newtbl->hash_tbl = hashinit(newtbl->hash_size, HASH_LIST, M_TEMP,
    320  1.22      elad 	    M_WAITOK | M_ZERO, &newtbl->hash_mask);
    321  1.22      elad 	newtbl->hash_used = 0;
    322  1.22      elad 	specificdata_init(fileassoc_domain, &newtbl->data);
    323  1.22      elad 
    324  1.22      elad 	/* XXX we need to make sure nothing uses fileassoc here! */
    325  1.22      elad 
    326  1.22      elad 	hh = tbl->hash_tbl;
    327  1.22      elad 	for (i = 0; i < tbl->hash_size; i++) {
    328  1.22      elad 		struct fileassoc_hash_entry *mhe;
    329  1.22      elad 
    330  1.22      elad 		while ((mhe = LIST_FIRST(&hh[i])) != NULL) {
    331  1.22      elad 			struct fileassoc_hashhead *vhh;
    332  1.22      elad 			size_t indx;
    333  1.22      elad 
    334  1.22      elad 			LIST_REMOVE(mhe, entries);
    335  1.22      elad 
    336  1.22      elad 			indx = FILEASSOC_HASH(newtbl, mhe->handle);
    337  1.22      elad 			vhh = &(newtbl->hash_tbl[indx]);
    338  1.22      elad 
    339  1.22      elad 			LIST_INSERT_HEAD(vhh, mhe, entries);
    340  1.22      elad 
    341  1.22      elad 			newtbl->hash_used++;
    342  1.22      elad 		}
    343  1.22      elad 	}
    344  1.22      elad 
    345  1.22      elad 	if (tbl->hash_used != newtbl->hash_used)
    346  1.22      elad 		panic("fileassoc_table_resize: inconsistency detected! "
    347  1.22      elad 		    "needed %zu entries, got %zu", tbl->hash_used,
    348  1.22      elad 		    newtbl->hash_used);
    349  1.22      elad 
    350  1.22      elad 	hashdone(tbl->hash_tbl, M_TEMP);
    351  1.22      elad 	specificdata_fini(fileassoc_domain, &tbl->data);
    352  1.22      elad 	kmem_free(tbl, sizeof(*tbl));
    353  1.22      elad 
    354  1.22      elad 	return (newtbl);
    355  1.22      elad }
    356  1.22      elad 
    357   1.1      elad /*
    358   1.1      elad  * Create a new fileassoc table.
    359   1.1      elad  */
    360  1.22      elad static int
    361  1.22      elad fileassoc_table_add(struct mount *mp)
    362   1.1      elad {
    363   1.1      elad 	struct fileassoc_table *tbl;
    364   1.1      elad 
    365   1.1      elad 	/* Check for existing table for device. */
    366   1.1      elad 	if (fileassoc_table_lookup(mp) != NULL)
    367   1.1      elad 		return (EEXIST);
    368   1.1      elad 
    369  1.18      elad 	/* Allocate and initialize a table. */
    370  1.14      yamt 	tbl = kmem_zalloc(sizeof(*tbl), KM_SLEEP);
    371  1.22      elad 	tbl->hash_size = FILEASSOC_INITIAL_TABLESIZE;
    372  1.22      elad 	tbl->hash_tbl = hashinit(tbl->hash_size, HASH_LIST, M_TEMP,
    373  1.22      elad 	    M_WAITOK | M_ZERO, &tbl->hash_mask);
    374  1.22      elad 	tbl->hash_used = 0;
    375  1.14      yamt 	specificdata_init(fileassoc_domain, &tbl->data);
    376   1.1      elad 
    377  1.16      yamt 	mount_setspecific(mp, fileassoc_mountspecific_key, tbl);
    378   1.1      elad 
    379   1.1      elad 	return (0);
    380   1.1      elad }
    381   1.1      elad 
    382   1.1      elad /*
    383   1.1      elad  * Delete a table.
    384   1.1      elad  */
    385   1.1      elad int
    386   1.1      elad fileassoc_table_delete(struct mount *mp)
    387   1.1      elad {
    388   1.1      elad 	struct fileassoc_table *tbl;
    389   1.1      elad 
    390   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    391   1.1      elad 	if (tbl == NULL)
    392   1.1      elad 		return (EEXIST);
    393   1.1      elad 
    394  1.16      yamt 	mount_setspecific(mp, fileassoc_mountspecific_key, NULL);
    395  1.16      yamt 	table_dtor(tbl);
    396   1.1      elad 
    397   1.1      elad 	return (0);
    398   1.1      elad }
    399   1.1      elad 
    400   1.1      elad /*
    401   1.6  christos  * Run a callback for each hook entry in a table.
    402   1.6  christos  */
    403   1.6  christos int
    404  1.14      yamt fileassoc_table_run(struct mount *mp, fileassoc_t assoc, fileassoc_cb_t cb)
    405   1.6  christos {
    406   1.6  christos 	struct fileassoc_table *tbl;
    407   1.6  christos 	struct fileassoc_hashhead *hh;
    408   1.6  christos 	u_long i;
    409   1.6  christos 
    410   1.6  christos 	tbl = fileassoc_table_lookup(mp);
    411   1.6  christos 	if (tbl == NULL)
    412   1.6  christos 		return (EEXIST);
    413   1.6  christos 
    414   1.6  christos 	hh = tbl->hash_tbl;
    415   1.6  christos 	for (i = 0; i < tbl->hash_size; i++) {
    416   1.6  christos 		struct fileassoc_hash_entry *mhe;
    417   1.6  christos 
    418   1.6  christos 		LIST_FOREACH(mhe, &hh[i], entries) {
    419  1.14      yamt 			void *data;
    420  1.14      yamt 
    421  1.14      yamt 			data = file_getdata(mhe, assoc);
    422  1.14      yamt 			if (data != NULL)
    423  1.14      yamt 				cb(data);
    424   1.6  christos 		}
    425   1.6  christos 	}
    426   1.6  christos 
    427   1.6  christos 	return (0);
    428   1.6  christos }
    429   1.6  christos 
    430   1.6  christos /*
    431   1.1      elad  * Clear a table for a given hook.
    432   1.1      elad  */
    433   1.1      elad int
    434  1.14      yamt fileassoc_table_clear(struct mount *mp, fileassoc_t assoc)
    435   1.1      elad {
    436   1.1      elad 	struct fileassoc_table *tbl;
    437   1.1      elad 	struct fileassoc_hashhead *hh;
    438   1.1      elad 	u_long i;
    439   1.1      elad 
    440   1.1      elad 	tbl = fileassoc_table_lookup(mp);
    441   1.1      elad 	if (tbl == NULL)
    442   1.1      elad 		return (EEXIST);
    443   1.1      elad 
    444   1.1      elad 	hh = tbl->hash_tbl;
    445   1.1      elad 	for (i = 0; i < tbl->hash_size; i++) {
    446   1.1      elad 		struct fileassoc_hash_entry *mhe;
    447   1.1      elad 
    448   1.1      elad 		LIST_FOREACH(mhe, &hh[i], entries) {
    449  1.14      yamt 			file_cleanup(mhe, assoc);
    450  1.14      yamt 			file_setdata(mhe, assoc, NULL);
    451   1.1      elad 		}
    452   1.1      elad 	}
    453   1.1      elad 
    454   1.1      elad 	return (0);
    455   1.1      elad }
    456   1.1      elad 
    457   1.1      elad /*
    458   1.1      elad  * Add a file entry to a table.
    459   1.1      elad  */
    460   1.3      elad static struct fileassoc_hash_entry *
    461   1.9     blymn fileassoc_file_add(struct vnode *vp, fhandle_t *hint)
    462   1.1      elad {
    463   1.1      elad 	struct fileassoc_table *tbl;
    464   1.1      elad 	struct fileassoc_hashhead *vhh;
    465   1.1      elad 	struct fileassoc_hash_entry *e;
    466   1.1      elad 	size_t indx;
    467   1.9     blymn 	fhandle_t *th;
    468   1.1      elad 	int error;
    469   1.1      elad 
    470  1.12      elad 	if (hint == NULL) {
    471   1.9     blymn 		error = vfs_composefh_alloc(vp, &th);
    472   1.8     blymn 		if (error)
    473   1.8     blymn 			return (NULL);
    474   1.8     blymn 	} else
    475   1.9     blymn 		th = hint;
    476   1.1      elad 
    477   1.9     blymn 	e = fileassoc_file_lookup(vp, th);
    478  1.10     blymn 	if (e != NULL) {
    479  1.10     blymn 		if (hint == NULL)
    480  1.10     blymn 			vfs_composefh_free(th);
    481  1.10     blymn 
    482   1.1      elad 		return (e);
    483  1.10     blymn 	}
    484   1.1      elad 
    485   1.1      elad 	tbl = fileassoc_table_lookup(vp->v_mount);
    486  1.10     blymn 	if (tbl == NULL) {
    487  1.22      elad 		fileassoc_table_add(vp->v_mount);
    488  1.22      elad 
    489  1.10     blymn 		if (hint == NULL)
    490  1.10     blymn 			vfs_composefh_free(th);
    491  1.10     blymn 
    492   1.1      elad 		return (NULL);
    493  1.10     blymn 	}
    494   1.1      elad 
    495   1.9     blymn 	indx = FILEASSOC_HASH(tbl, th);
    496   1.9     blymn 	vhh = &(tbl->hash_tbl[indx]);
    497   1.1      elad 
    498  1.14      yamt 	e = kmem_zalloc(sizeof(*e), KM_SLEEP);
    499   1.9     blymn 	e->handle = th;
    500  1.14      yamt 	specificdata_init(fileassoc_domain, &e->data);
    501   1.1      elad 	LIST_INSERT_HEAD(vhh, e, entries);
    502   1.1      elad 
    503  1.22      elad 	/*
    504  1.22      elad 	 * This decides when we need to resize the table. For now,
    505  1.22      elad 	 * resize it whenever we "filled" up the number of slots it
    506  1.22      elad 	 * has. That's not really true unless of course we had zero
    507  1.22      elad 	 * collisions. Think positive! :)
    508  1.22      elad 	 */
    509  1.22      elad 	if (++(tbl->hash_used) == tbl->hash_size) {
    510  1.22      elad 		struct fileassoc_table *newtbl;
    511  1.22      elad 
    512  1.22      elad 		newtbl = fileassoc_table_resize(tbl);
    513  1.22      elad 		mount_setspecific(vp->v_mount, fileassoc_mountspecific_key,
    514  1.22      elad 		    newtbl);
    515  1.22      elad 	}
    516  1.22      elad 
    517   1.1      elad 	return (e);
    518   1.1      elad }
    519   1.1      elad 
    520   1.1      elad /*
    521   1.1      elad  * Delete a file entry from a table.
    522   1.1      elad  */
    523   1.1      elad int
    524   1.1      elad fileassoc_file_delete(struct vnode *vp)
    525   1.1      elad {
    526   1.1      elad 	struct fileassoc_hash_entry *mhe;
    527   1.1      elad 
    528   1.9     blymn 	mhe = fileassoc_file_lookup(vp, NULL);
    529   1.1      elad 	if (mhe == NULL)
    530   1.1      elad 		return (ENOENT);
    531   1.1      elad 
    532  1.14      yamt 	file_free(mhe);
    533   1.1      elad 
    534   1.1      elad 	return (0);
    535   1.1      elad }
    536   1.1      elad 
    537   1.1      elad /*
    538   1.9     blymn  * Add a hook to a vnode.
    539   1.1      elad  */
    540   1.1      elad int
    541  1.14      yamt fileassoc_add(struct vnode *vp, fileassoc_t assoc, void *data)
    542   1.1      elad {
    543   1.1      elad 	struct fileassoc_hash_entry *e;
    544  1.14      yamt 	void *olddata;
    545   1.1      elad 
    546   1.9     blymn 	e = fileassoc_file_lookup(vp, NULL);
    547   1.1      elad 	if (e == NULL) {
    548   1.9     blymn 		e = fileassoc_file_add(vp, NULL);
    549   1.1      elad 		if (e == NULL)
    550   1.1      elad 			return (ENOTDIR);
    551   1.1      elad 	}
    552   1.1      elad 
    553  1.14      yamt 	olddata = file_getdata(e, assoc);
    554  1.14      yamt 	if (olddata != NULL)
    555   1.1      elad 		return (EEXIST);
    556   1.1      elad 
    557  1.14      yamt 	file_setdata(e, assoc, data);
    558   1.1      elad 
    559   1.1      elad 	return (0);
    560   1.1      elad }
    561   1.1      elad 
    562   1.1      elad /*
    563   1.1      elad  * Clear a hook from a vnode.
    564   1.1      elad  */
    565   1.1      elad int
    566  1.14      yamt fileassoc_clear(struct vnode *vp, fileassoc_t assoc)
    567   1.1      elad {
    568   1.1      elad 	struct fileassoc_hash_entry *mhe;
    569   1.1      elad 
    570   1.9     blymn 	mhe = fileassoc_file_lookup(vp, NULL);
    571   1.1      elad 	if (mhe == NULL)
    572   1.1      elad 		return (ENOENT);
    573   1.1      elad 
    574  1.14      yamt 	file_cleanup(mhe, assoc);
    575  1.14      yamt 	file_setdata(mhe, assoc, NULL);
    576   1.1      elad 
    577   1.1      elad 	return (0);
    578   1.1      elad }
    579