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