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