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