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