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