Home | History | Annotate | Line # | Download | only in kern
kern_fileassoc.c revision 1.10.4.2
      1  1.10.4.2      yamt /* $NetBSD: kern_fileassoc.c,v 1.10.4.2 2006/12/18 11:42:15 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.10.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: kern_fileassoc.c,v 1.10.4.2 2006/12/18 11:42:15 yamt Exp $");
     35  1.10.4.1      yamt 
     36  1.10.4.1      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.10.4.2      yamt #include <sys/specificdata.h>
     50       1.3      elad #include <sys/hash.h>
     51       1.9     blymn #include <sys/fstypes.h>
     52  1.10.4.2      yamt #include <sys/kmem.h>
     53  1.10.4.2      yamt #include <sys/once.h>
     54  1.10.4.1      yamt 
     55       1.9     blymn static struct fileassoc_hash_entry *
     56       1.9     blymn fileassoc_file_lookup(struct vnode *, fhandle_t *);
     57       1.9     blymn static struct fileassoc_hash_entry *
     58       1.9     blymn fileassoc_file_add(struct vnode *, fhandle_t *);
     59       1.1      elad 
     60  1.10.4.2      yamt static specificdata_domain_t fileassoc_domain;
     61  1.10.4.2      yamt static specificdata_key_t fileassoc_mountspecific_key;
     62  1.10.4.2      yamt 
     63       1.3      elad /*
     64       1.3      elad  * Hook entry.
     65       1.3      elad  * Includes the hook name for identification and private hook clear callback.
     66       1.3      elad  */
     67  1.10.4.2      yamt struct fileassoc {
     68  1.10.4.2      yamt 	LIST_ENTRY(fileassoc) list;
     69  1.10.4.2      yamt 	const char *name;			/* name. */
     70  1.10.4.2      yamt 	fileassoc_cleanup_cb_t cleanup_cb;	/* clear callback. */
     71  1.10.4.2      yamt 	specificdata_key_t key;
     72       1.3      elad };
     73       1.3      elad 
     74  1.10.4.2      yamt static LIST_HEAD(, fileassoc) fileassoc_list;
     75  1.10.4.2      yamt 
     76       1.3      elad /* An entry in the per-device hash table. */
     77       1.3      elad struct fileassoc_hash_entry {
     78       1.9     blymn 	fhandle_t *handle;				/* File handle */
     79  1.10.4.2      yamt 	specificdata_reference data;			/* Hooks. */
     80       1.3      elad 	LIST_ENTRY(fileassoc_hash_entry) entries;	/* List pointer. */
     81       1.3      elad };
     82       1.3      elad 
     83       1.3      elad LIST_HEAD(fileassoc_hashhead, fileassoc_hash_entry);
     84       1.3      elad 
     85       1.3      elad struct fileassoc_table {
     86       1.3      elad 	struct fileassoc_hashhead *hash_tbl;
     87       1.3      elad 	size_t hash_size;				/* Number of slots. */
     88       1.3      elad 	u_long hash_mask;
     89  1.10.4.2      yamt 	specificdata_reference data;
     90       1.3      elad };
     91       1.8     blymn 
     92       1.1      elad /*
     93       1.9     blymn  * Hashing function: Takes a number modulus the mask to give back an
     94       1.9     blymn  * index into the hash table.
     95       1.1      elad  */
     96       1.9     blymn #define FILEASSOC_HASH(tbl, handle)	\
     97      1.10     blymn 	(hash32_buf((handle), FHANDLE_SIZE(handle), HASH32_BUF_INIT) \
     98       1.1      elad 	 & ((tbl)->hash_mask))
     99       1.1      elad 
    100  1.10.4.2      yamt static void *
    101  1.10.4.2      yamt table_getdata(struct fileassoc_table *tbl, const struct fileassoc *assoc)
    102  1.10.4.2      yamt {
    103  1.10.4.2      yamt 
    104  1.10.4.2      yamt 	return specificdata_getspecific(fileassoc_domain, &tbl->data,
    105  1.10.4.2      yamt 	    assoc->key);
    106  1.10.4.2      yamt }
    107  1.10.4.2      yamt 
    108  1.10.4.2      yamt static void
    109  1.10.4.2      yamt table_setdata(struct fileassoc_table *tbl, const struct fileassoc *assoc,
    110  1.10.4.2      yamt     void *data)
    111  1.10.4.2      yamt {
    112  1.10.4.2      yamt 
    113  1.10.4.2      yamt 	specificdata_setspecific(fileassoc_domain, &tbl->data, assoc->key,
    114  1.10.4.2      yamt 	    data);
    115  1.10.4.2      yamt }
    116  1.10.4.2      yamt 
    117  1.10.4.2      yamt static void
    118  1.10.4.2      yamt table_cleanup(struct fileassoc_table *tbl, const struct fileassoc *assoc)
    119  1.10.4.2      yamt {
    120  1.10.4.2      yamt 	fileassoc_cleanup_cb_t cb;
    121  1.10.4.2      yamt 	void *data;
    122  1.10.4.2      yamt 
    123  1.10.4.2      yamt 	cb = assoc->cleanup_cb;
    124  1.10.4.2      yamt 	if (cb == NULL) {
    125  1.10.4.2      yamt 		return;
    126  1.10.4.2      yamt 	}
    127  1.10.4.2      yamt 	data = table_getdata(tbl, assoc);
    128  1.10.4.2      yamt 	(*cb)(data, FILEASSOC_CLEANUP_TABLE);
    129  1.10.4.2      yamt }
    130  1.10.4.2      yamt 
    131  1.10.4.2      yamt static void *
    132  1.10.4.2      yamt file_getdata(struct fileassoc_hash_entry *e, const struct fileassoc *assoc)
    133  1.10.4.2      yamt {
    134  1.10.4.2      yamt 
    135  1.10.4.2      yamt 	return specificdata_getspecific(fileassoc_domain, &e->data,
    136  1.10.4.2      yamt 	    assoc->key);
    137  1.10.4.2      yamt }
    138  1.10.4.2      yamt 
    139  1.10.4.2      yamt static void
    140  1.10.4.2      yamt file_setdata(struct fileassoc_hash_entry *e, const struct fileassoc *assoc,
    141  1.10.4.2      yamt     void *data)
    142  1.10.4.2      yamt {
    143  1.10.4.2      yamt 
    144  1.10.4.2      yamt 	specificdata_setspecific(fileassoc_domain, &e->data, assoc->key,
    145  1.10.4.2      yamt 	    data);
    146  1.10.4.2      yamt }
    147  1.10.4.2      yamt 
    148  1.10.4.2      yamt static void
    149  1.10.4.2      yamt file_cleanup(struct fileassoc_hash_entry *e, const struct fileassoc *assoc)
    150  1.10.4.2      yamt {
    151  1.10.4.2      yamt 	fileassoc_cleanup_cb_t cb;
    152  1.10.4.2      yamt 	void *data;
    153  1.10.4.2      yamt 
    154  1.10.4.2      yamt 	cb = assoc->cleanup_cb;
    155  1.10.4.2      yamt 	if (cb == NULL) {
    156  1.10.4.2      yamt 		return;
    157  1.10.4.2      yamt 	}
    158  1.10.4.2      yamt 	data = file_getdata(e, assoc);
    159  1.10.4.2      yamt 	(*cb)(data, FILEASSOC_CLEANUP_FILE);
    160  1.10.4.2      yamt }
    161  1.10.4.2      yamt 
    162  1.10.4.2      yamt static void
    163  1.10.4.2      yamt file_free(struct fileassoc_hash_entry *e)
    164  1.10.4.2      yamt {
    165  1.10.4.2      yamt 	struct fileassoc *assoc;
    166  1.10.4.2      yamt 
    167  1.10.4.2      yamt 	LIST_REMOVE(e, entries);
    168  1.10.4.2      yamt 
    169  1.10.4.2      yamt 	LIST_FOREACH(assoc, &fileassoc_list, list) {
    170  1.10.4.2      yamt 		file_cleanup(e, assoc);
    171  1.10.4.2      yamt 	}
    172  1.10.4.2      yamt 	vfs_composefh_free(e->handle);
    173  1.10.4.2      yamt 	specificdata_fini(fileassoc_domain, &e->data);
    174  1.10.4.2      yamt 	kmem_free(e, sizeof(*e));
    175  1.10.4.2      yamt }
    176  1.10.4.2      yamt 
    177  1.10.4.2      yamt static void
    178  1.10.4.2      yamt table_dtor(void *vp)
    179  1.10.4.2      yamt {
    180  1.10.4.2      yamt 	struct fileassoc_table *tbl = vp;
    181  1.10.4.2      yamt 	const struct fileassoc *assoc;
    182  1.10.4.2      yamt 	struct fileassoc_hashhead *hh;
    183  1.10.4.2      yamt 	u_long i;
    184  1.10.4.2      yamt 
    185  1.10.4.2      yamt 	/* Remove all entries from the table and lists */
    186  1.10.4.2      yamt 	hh = tbl->hash_tbl;
    187  1.10.4.2      yamt 	for (i = 0; i < tbl->hash_size; i++) {
    188  1.10.4.2      yamt 		struct fileassoc_hash_entry *mhe;
    189  1.10.4.2      yamt 
    190  1.10.4.2      yamt 		while ((mhe = LIST_FIRST(&hh[i])) != NULL) {
    191  1.10.4.2      yamt 			file_free(mhe);
    192  1.10.4.2      yamt 		}
    193  1.10.4.2      yamt 	}
    194  1.10.4.2      yamt 
    195  1.10.4.2      yamt 	LIST_FOREACH(assoc, &fileassoc_list, list) {
    196  1.10.4.2      yamt 		table_cleanup(tbl, assoc);
    197  1.10.4.2      yamt 	}
    198  1.10.4.2      yamt 
    199  1.10.4.2      yamt 	/* Remove hash table and sysctl node */
    200  1.10.4.2      yamt 	hashdone(tbl->hash_tbl, M_TEMP);
    201  1.10.4.2      yamt 	specificdata_fini(fileassoc_domain, &tbl->data);
    202  1.10.4.2      yamt 	kmem_free(tbl, sizeof(*tbl));
    203  1.10.4.2      yamt }
    204  1.10.4.2      yamt 
    205       1.1      elad /*
    206       1.1      elad  * Initialize the fileassoc subsystem.
    207       1.1      elad  */
    208  1.10.4.2      yamt static int
    209       1.1      elad fileassoc_init(void)
    210       1.1      elad {
    211  1.10.4.2      yamt 	int error;
    212  1.10.4.2      yamt 
    213  1.10.4.2      yamt 	error = mount_specific_key_create(&fileassoc_mountspecific_key,
    214  1.10.4.2      yamt 	    table_dtor);
    215  1.10.4.2      yamt 	if (error) {
    216  1.10.4.2      yamt 		return error;
    217  1.10.4.2      yamt 	}
    218  1.10.4.2      yamt 	fileassoc_domain = specificdata_domain_create();
    219  1.10.4.2      yamt 
    220  1.10.4.2      yamt 	return 0;
    221       1.1      elad }
    222       1.1      elad 
    223       1.1      elad /*
    224       1.1      elad  * Register a new hook.
    225       1.1      elad  */
    226  1.10.4.2      yamt int
    227  1.10.4.2      yamt fileassoc_register(const char *name, fileassoc_cleanup_cb_t cleanup_cb,
    228  1.10.4.2      yamt     fileassoc_t *result)
    229       1.1      elad {
    230  1.10.4.2      yamt 	int error;
    231  1.10.4.2      yamt 	specificdata_key_t key;
    232  1.10.4.2      yamt 	struct fileassoc *assoc;
    233  1.10.4.2      yamt 	static ONCE_DECL(control);
    234  1.10.4.2      yamt 
    235  1.10.4.2      yamt 	error = RUN_ONCE(&control, fileassoc_init);
    236  1.10.4.2      yamt 	if (error) {
    237  1.10.4.2      yamt 		return error;
    238  1.10.4.2      yamt 	}
    239  1.10.4.2      yamt 	error = specificdata_key_create(fileassoc_domain, &key, NULL);
    240  1.10.4.2      yamt 	if (error) {
    241  1.10.4.2      yamt 		return error;
    242  1.10.4.2      yamt 	}
    243  1.10.4.2      yamt 	assoc = kmem_alloc(sizeof(*assoc), KM_SLEEP);
    244  1.10.4.2      yamt 	assoc->name = name;
    245  1.10.4.2      yamt 	assoc->cleanup_cb = cleanup_cb;
    246  1.10.4.2      yamt 	assoc->key = key;
    247  1.10.4.2      yamt 	LIST_INSERT_HEAD(&fileassoc_list, assoc, list);
    248  1.10.4.2      yamt 	*result = assoc;
    249       1.1      elad 
    250  1.10.4.2      yamt 	return 0;
    251       1.1      elad }
    252       1.1      elad 
    253       1.1      elad /*
    254       1.1      elad  * Deregister a hook.
    255       1.1      elad  */
    256       1.1      elad int
    257  1.10.4.2      yamt fileassoc_deregister(fileassoc_t assoc)
    258       1.1      elad {
    259       1.1      elad 
    260  1.10.4.2      yamt 	LIST_REMOVE(assoc, list);
    261  1.10.4.2      yamt 	kmem_free(assoc, sizeof(*assoc));
    262       1.1      elad 
    263  1.10.4.2      yamt 	return 0;
    264       1.1      elad }
    265       1.1      elad 
    266       1.1      elad /*
    267       1.1      elad  * Get the hash table for the specified device.
    268       1.1      elad  */
    269       1.3      elad static struct fileassoc_table *
    270       1.1      elad fileassoc_table_lookup(struct mount *mp)
    271       1.1      elad {
    272       1.1      elad 
    273  1.10.4.2      yamt 	return mount_getspecific(mp, fileassoc_mountspecific_key);
    274       1.1      elad }
    275       1.1      elad 
    276       1.1      elad /*
    277       1.8     blymn  * Perform a lookup on a hash table.  If hint is non-zero then use the value
    278       1.8     blymn  * of the hint as the identifier instead of performing a lookup for the
    279       1.8     blymn  * fileid.
    280       1.1      elad  */
    281       1.3      elad static struct fileassoc_hash_entry *
    282       1.9     blymn fileassoc_file_lookup(struct vnode *vp, fhandle_t *hint)
    283       1.1      elad {
    284       1.1      elad 	struct fileassoc_table *tbl;
    285       1.1      elad 	struct fileassoc_hashhead *tble;
    286       1.1      elad 	struct fileassoc_hash_entry *e;
    287       1.1      elad 	size_t indx;
    288       1.9     blymn 	fhandle_t *th;
    289       1.1      elad 	int error;
    290       1.1      elad 
    291  1.10.4.2      yamt 	tbl = fileassoc_table_lookup(vp->v_mount);
    292  1.10.4.2      yamt 	if (tbl == NULL) {
    293  1.10.4.2      yamt 		return NULL;
    294  1.10.4.2      yamt 	}
    295  1.10.4.2      yamt 
    296       1.9     blymn 	if (hint == NULL) {
    297       1.9     blymn 		error = vfs_composefh_alloc(vp, &th);
    298       1.8     blymn 		if (error)
    299       1.8     blymn 			return (NULL);
    300  1.10.4.2      yamt 	} else {
    301       1.8     blymn 		th = hint;
    302      1.10     blymn 	}
    303       1.1      elad 
    304       1.8     blymn 	indx = FILEASSOC_HASH(tbl, th);
    305       1.9     blymn 	tble = &(tbl->hash_tbl[indx]);
    306       1.1      elad 
    307       1.1      elad 	LIST_FOREACH(e, tble, entries) {
    308  1.10.4.2      yamt 		if (((FHANDLE_FILEID(e->handle)->fid_len ==
    309      1.10     blymn 		     FHANDLE_FILEID(th)->fid_len)) &&
    310       1.9     blymn 		    (memcmp(FHANDLE_FILEID(e->handle), FHANDLE_FILEID(th),
    311  1.10.4.1      yamt 			   (FHANDLE_FILEID(th))->fid_len) == 0)) {
    312  1.10.4.2      yamt 			break;
    313  1.10.4.1      yamt 		}
    314       1.1      elad 	}
    315       1.1      elad 
    316      1.10     blymn 	if (hint == NULL)
    317      1.10     blymn 		vfs_composefh_free(th);
    318      1.10     blymn 
    319  1.10.4.2      yamt 	return e;
    320       1.1      elad }
    321       1.1      elad 
    322       1.1      elad /*
    323       1.1      elad  * Return hook data associated with a vnode.
    324       1.1      elad  */
    325       1.1      elad void *
    326  1.10.4.2      yamt fileassoc_lookup(struct vnode *vp, fileassoc_t assoc)
    327       1.1      elad {
    328       1.9     blymn         struct fileassoc_hash_entry *mhe;
    329       1.8     blymn 
    330       1.9     blymn         mhe = fileassoc_file_lookup(vp, NULL);
    331       1.9     blymn         if (mhe == NULL)
    332       1.9     blymn                 return (NULL);
    333       1.1      elad 
    334  1.10.4.2      yamt         return file_getdata(mhe, assoc);
    335       1.1      elad }
    336       1.1      elad 
    337       1.1      elad /*
    338       1.1      elad  * Create a new fileassoc table.
    339       1.1      elad  */
    340       1.1      elad int
    341       1.1      elad fileassoc_table_add(struct mount *mp, size_t size)
    342       1.1      elad {
    343       1.1      elad 	struct fileassoc_table *tbl;
    344       1.1      elad 
    345       1.1      elad 	/* Check for existing table for device. */
    346       1.1      elad 	if (fileassoc_table_lookup(mp) != NULL)
    347       1.1      elad 		return (EEXIST);
    348       1.1      elad 
    349       1.1      elad 	/* Allocate and initialize a Veriexec hash table. */
    350  1.10.4.2      yamt 	tbl = kmem_zalloc(sizeof(*tbl), KM_SLEEP);
    351       1.1      elad 	tbl->hash_size = size;
    352       1.1      elad 	tbl->hash_tbl = hashinit(size, HASH_LIST, M_TEMP,
    353       1.1      elad 				 M_WAITOK | M_ZERO, &tbl->hash_mask);
    354  1.10.4.2      yamt 	specificdata_init(fileassoc_domain, &tbl->data);
    355       1.1      elad 
    356  1.10.4.2      yamt 	mount_setspecific(mp, fileassoc_mountspecific_key, tbl);
    357       1.1      elad 
    358       1.1      elad 	return (0);
    359       1.1      elad }
    360       1.1      elad 
    361       1.1      elad /*
    362       1.1      elad  * Delete a table.
    363       1.1      elad  */
    364       1.1      elad int
    365       1.1      elad fileassoc_table_delete(struct mount *mp)
    366       1.1      elad {
    367       1.1      elad 	struct fileassoc_table *tbl;
    368       1.1      elad 
    369       1.1      elad 	tbl = fileassoc_table_lookup(mp);
    370       1.1      elad 	if (tbl == NULL)
    371       1.1      elad 		return (EEXIST);
    372       1.1      elad 
    373  1.10.4.2      yamt 	mount_setspecific(mp, fileassoc_mountspecific_key, NULL);
    374  1.10.4.2      yamt 	table_dtor(tbl);
    375       1.1      elad 
    376       1.1      elad 	return (0);
    377       1.1      elad }
    378       1.1      elad 
    379       1.1      elad /*
    380       1.6  christos  * Run a callback for each hook entry in a table.
    381       1.6  christos  */
    382       1.6  christos int
    383  1.10.4.2      yamt fileassoc_table_run(struct mount *mp, fileassoc_t assoc, fileassoc_cb_t cb)
    384       1.6  christos {
    385       1.6  christos 	struct fileassoc_table *tbl;
    386       1.6  christos 	struct fileassoc_hashhead *hh;
    387       1.6  christos 	u_long i;
    388       1.6  christos 
    389       1.6  christos 	tbl = fileassoc_table_lookup(mp);
    390       1.6  christos 	if (tbl == NULL)
    391       1.6  christos 		return (EEXIST);
    392       1.6  christos 
    393       1.6  christos 	hh = tbl->hash_tbl;
    394       1.6  christos 	for (i = 0; i < tbl->hash_size; i++) {
    395       1.6  christos 		struct fileassoc_hash_entry *mhe;
    396       1.6  christos 
    397       1.6  christos 		LIST_FOREACH(mhe, &hh[i], entries) {
    398  1.10.4.2      yamt 			void *data;
    399  1.10.4.2      yamt 
    400  1.10.4.2      yamt 			data = file_getdata(mhe, assoc);
    401  1.10.4.2      yamt 			if (data != NULL)
    402  1.10.4.2      yamt 				cb(data);
    403       1.6  christos 		}
    404       1.6  christos 	}
    405       1.6  christos 
    406       1.6  christos 	return (0);
    407       1.6  christos }
    408       1.6  christos 
    409       1.6  christos /*
    410       1.1      elad  * Clear a table for a given hook.
    411       1.1      elad  */
    412       1.1      elad int
    413  1.10.4.2      yamt fileassoc_table_clear(struct mount *mp, fileassoc_t assoc)
    414       1.1      elad {
    415       1.1      elad 	struct fileassoc_table *tbl;
    416       1.1      elad 	struct fileassoc_hashhead *hh;
    417       1.1      elad 	u_long i;
    418       1.1      elad 
    419       1.1      elad 	tbl = fileassoc_table_lookup(mp);
    420       1.1      elad 	if (tbl == NULL)
    421       1.1      elad 		return (EEXIST);
    422       1.1      elad 
    423       1.1      elad 	hh = tbl->hash_tbl;
    424       1.1      elad 	for (i = 0; i < tbl->hash_size; i++) {
    425       1.1      elad 		struct fileassoc_hash_entry *mhe;
    426       1.1      elad 
    427       1.1      elad 		LIST_FOREACH(mhe, &hh[i], entries) {
    428  1.10.4.2      yamt 			file_cleanup(mhe, assoc);
    429  1.10.4.2      yamt 			file_setdata(mhe, assoc, NULL);
    430       1.1      elad 		}
    431       1.1      elad 	}
    432       1.1      elad 
    433  1.10.4.2      yamt 	table_cleanup(tbl, assoc);
    434  1.10.4.2      yamt 	table_setdata(tbl, assoc, NULL);
    435       1.1      elad 
    436       1.1      elad 	return (0);
    437       1.1      elad }
    438       1.1      elad 
    439       1.1      elad /*
    440       1.1      elad  * Add hook-specific data on a fileassoc table.
    441       1.1      elad  */
    442       1.1      elad int
    443  1.10.4.2      yamt fileassoc_tabledata_add(struct mount *mp, fileassoc_t assoc, void *data)
    444       1.1      elad {
    445       1.1      elad 	struct fileassoc_table *tbl;
    446       1.1      elad 
    447       1.1      elad 	tbl = fileassoc_table_lookup(mp);
    448       1.1      elad 	if (tbl == NULL)
    449       1.1      elad 		return (EFAULT);
    450       1.1      elad 
    451  1.10.4.2      yamt 	table_setdata(tbl, assoc, data);
    452       1.1      elad 
    453       1.1      elad 	return (0);
    454       1.1      elad }
    455       1.1      elad 
    456       1.1      elad /*
    457       1.1      elad  * Clear hook-specific data on a fileassoc table.
    458       1.1      elad  */
    459       1.1      elad int
    460  1.10.4.2      yamt fileassoc_tabledata_clear(struct mount *mp, fileassoc_t assoc)
    461       1.1      elad {
    462       1.1      elad 
    463  1.10.4.2      yamt 	return fileassoc_tabledata_add(mp, assoc, NULL);
    464       1.1      elad }
    465       1.1      elad 
    466       1.1      elad /*
    467       1.1      elad  * Retrieve hook-specific data from a fileassoc table.
    468       1.1      elad  */
    469       1.1      elad void *
    470  1.10.4.2      yamt fileassoc_tabledata_lookup(struct mount *mp, fileassoc_t assoc)
    471       1.1      elad {
    472       1.1      elad 	struct fileassoc_table *tbl;
    473       1.1      elad 
    474       1.1      elad 	tbl = fileassoc_table_lookup(mp);
    475       1.1      elad 	if (tbl == NULL)
    476       1.1      elad 		return (NULL);
    477       1.1      elad 
    478  1.10.4.2      yamt 	return table_getdata(tbl, assoc);
    479       1.1      elad }
    480       1.1      elad 
    481       1.1      elad /*
    482       1.1      elad  * Add a file entry to a table.
    483       1.1      elad  */
    484       1.3      elad static struct fileassoc_hash_entry *
    485       1.9     blymn fileassoc_file_add(struct vnode *vp, fhandle_t *hint)
    486       1.1      elad {
    487       1.1      elad 	struct fileassoc_table *tbl;
    488       1.1      elad 	struct fileassoc_hashhead *vhh;
    489       1.1      elad 	struct fileassoc_hash_entry *e;
    490       1.1      elad 	size_t indx;
    491       1.9     blymn 	fhandle_t *th;
    492       1.1      elad 	int error;
    493       1.1      elad 
    494  1.10.4.1      yamt 	if (hint == NULL) {
    495       1.9     blymn 		error = vfs_composefh_alloc(vp, &th);
    496       1.8     blymn 		if (error)
    497       1.8     blymn 			return (NULL);
    498       1.8     blymn 	} else
    499       1.9     blymn 		th = hint;
    500       1.1      elad 
    501       1.9     blymn 	e = fileassoc_file_lookup(vp, th);
    502      1.10     blymn 	if (e != NULL) {
    503      1.10     blymn 		if (hint == NULL)
    504      1.10     blymn 			vfs_composefh_free(th);
    505      1.10     blymn 
    506       1.1      elad 		return (e);
    507      1.10     blymn 	}
    508       1.1      elad 
    509       1.1      elad 	tbl = fileassoc_table_lookup(vp->v_mount);
    510      1.10     blymn 	if (tbl == NULL) {
    511      1.10     blymn 		if (hint == NULL)
    512      1.10     blymn 			vfs_composefh_free(th);
    513      1.10     blymn 
    514       1.1      elad 		return (NULL);
    515      1.10     blymn 	}
    516       1.1      elad 
    517       1.9     blymn 	indx = FILEASSOC_HASH(tbl, th);
    518       1.9     blymn 	vhh = &(tbl->hash_tbl[indx]);
    519       1.1      elad 
    520  1.10.4.2      yamt 	e = kmem_zalloc(sizeof(*e), KM_SLEEP);
    521       1.9     blymn 	e->handle = th;
    522  1.10.4.2      yamt 	specificdata_init(fileassoc_domain, &e->data);
    523       1.1      elad 	LIST_INSERT_HEAD(vhh, e, entries);
    524       1.1      elad 
    525       1.1      elad 	return (e);
    526       1.1      elad }
    527       1.1      elad 
    528       1.1      elad /*
    529       1.1      elad  * Delete a file entry from a table.
    530       1.1      elad  */
    531       1.1      elad int
    532       1.1      elad fileassoc_file_delete(struct vnode *vp)
    533       1.1      elad {
    534       1.1      elad 	struct fileassoc_hash_entry *mhe;
    535       1.1      elad 
    536       1.9     blymn 	mhe = fileassoc_file_lookup(vp, NULL);
    537       1.1      elad 	if (mhe == NULL)
    538       1.1      elad 		return (ENOENT);
    539       1.1      elad 
    540  1.10.4.2      yamt 	file_free(mhe);
    541       1.1      elad 
    542       1.1      elad 	return (0);
    543       1.1      elad }
    544       1.1      elad 
    545       1.1      elad /*
    546       1.9     blymn  * Add a hook to a vnode.
    547       1.1      elad  */
    548       1.1      elad int
    549  1.10.4.2      yamt fileassoc_add(struct vnode *vp, fileassoc_t assoc, void *data)
    550       1.1      elad {
    551       1.1      elad 	struct fileassoc_hash_entry *e;
    552  1.10.4.2      yamt 	void *olddata;
    553       1.1      elad 
    554       1.9     blymn 	e = fileassoc_file_lookup(vp, NULL);
    555       1.1      elad 	if (e == NULL) {
    556       1.9     blymn 		e = fileassoc_file_add(vp, NULL);
    557       1.1      elad 		if (e == NULL)
    558       1.1      elad 			return (ENOTDIR);
    559       1.1      elad 	}
    560       1.1      elad 
    561  1.10.4.2      yamt 	olddata = file_getdata(e, assoc);
    562  1.10.4.2      yamt 	if (olddata != NULL)
    563       1.1      elad 		return (EEXIST);
    564       1.1      elad 
    565  1.10.4.2      yamt 	file_setdata(e, assoc, data);
    566       1.1      elad 
    567       1.1      elad 	return (0);
    568       1.1      elad }
    569       1.1      elad 
    570       1.1      elad /*
    571       1.1      elad  * Clear a hook from a vnode.
    572       1.1      elad  */
    573       1.1      elad int
    574  1.10.4.2      yamt fileassoc_clear(struct vnode *vp, fileassoc_t assoc)
    575       1.1      elad {
    576       1.1      elad 	struct fileassoc_hash_entry *mhe;
    577       1.1      elad 
    578       1.9     blymn 	mhe = fileassoc_file_lookup(vp, NULL);
    579       1.1      elad 	if (mhe == NULL)
    580       1.1      elad 		return (ENOENT);
    581       1.1      elad 
    582  1.10.4.2      yamt 	file_cleanup(mhe, assoc);
    583  1.10.4.2      yamt 	file_setdata(mhe, assoc, NULL);
    584       1.1      elad 
    585       1.1      elad 	return (0);
    586       1.1      elad }
    587