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