Home | History | Annotate | Line # | Download | only in kern
kern_fileassoc.c revision 1.1
      1 /* $NetBSD: kern_fileassoc.c,v 1.1 2006/07/14 18:41:40 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. 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/param.h>
     34 #include <sys/mount.h>
     35 #include <sys/queue.h>
     36 #include <sys/malloc.h>
     37 #include <sys/vnode.h>
     38 #include <sys/namei.h>
     39 #include <sys/exec.h>
     40 #include <sys/proc.h>
     41 #include <sys/inttypes.h>
     42 #include <sys/errno.h>
     43 #include <sys/fileassoc.h>
     44 
     45 struct fileassoc_hook fileassoc_hooks[FILEASSOC_NHOOKS];
     46 int fileassoc_nhooks;
     47 
     48 
     49 /* Global list of hash tables, one per device. */
     50 LIST_HEAD(, fileassoc_table) fileassoc_tables;
     51 
     52 /*
     53  * Hashing function: Takes a number modulus the mask to give back
     54  * an index into the hash table.
     55  */
     56 #define FILEASSOC_HASH(tbl, fileid)	\
     57 	(hash32_buf(&(fileid), sizeof((fileid)), HASH32_BUF_INIT) \
     58 	 & ((tbl)->hash_mask))
     59 
     60 /*
     61  * Initialize the fileassoc subsystem.
     62  */
     63 void
     64 fileassoc_init(void)
     65 {
     66 	memset(fileassoc_hooks, 0, sizeof(fileassoc_hooks));
     67 	fileassoc_nhooks = 0;
     68 }
     69 
     70 /*
     71  * Register a new hook.
     72  */
     73 fileassoc_t
     74 fileassoc_register(const char *name, fileassoc_cleanup_cb_t cleanup_cb)
     75 {
     76 	int i;
     77 
     78 	if (fileassoc_nhooks >= FILEASSOC_NHOOKS)
     79 		return (-1);
     80 
     81 	for (i = 0; i < FILEASSOC_NHOOKS; i++)
     82 		if (fileassoc_hooks[i].hook_name == NULL)
     83 			break;
     84 
     85 	fileassoc_hooks[i].hook_name = name;
     86 	fileassoc_hooks[i].hook_cleanup_cb = cleanup_cb;
     87 
     88 	fileassoc_nhooks++;
     89 
     90 	return (i);
     91 }
     92 
     93 /*
     94  * Deregister a hook.
     95  */
     96 int
     97 fileassoc_deregister(fileassoc_t id)
     98 {
     99 	if (id < 0 || id >= FILEASSOC_NHOOKS)
    100 		return (EINVAL);
    101 
    102 	fileassoc_hooks[id].hook_name = NULL;
    103 	fileassoc_hooks[id].hook_cleanup_cb = NULL;
    104 
    105 	fileassoc_nhooks--;
    106 
    107 	return (0);
    108 }
    109 
    110 /*
    111  * Get the hash table for the specified device.
    112  */
    113 struct fileassoc_table *
    114 fileassoc_table_lookup(struct mount *mp)
    115 {
    116 	struct fileassoc_table *tbl;
    117 
    118 	LIST_FOREACH(tbl, &fileassoc_tables, hash_list) {
    119 		if (tbl->tbl_mntpt == mp)
    120 			return (tbl);
    121 	}
    122 
    123 	return (NULL);
    124 }
    125 
    126 /*
    127  * Perform a lookup on a hash table.
    128  */
    129 struct fileassoc_hash_entry *
    130 fileassoc_file_lookup(struct vnode *vp)
    131 {
    132 	struct fileassoc_table *tbl;
    133 	struct fileassoc_hashhead *tble;
    134 	struct fileassoc_hash_entry *e;
    135 	struct vattr va;
    136 	size_t indx;
    137 	int error;
    138 
    139 	error = VOP_GETATTR(vp, &va, curlwp->l_proc->p_cred, curlwp);
    140 	if (error)
    141 		return (NULL);
    142 
    143 	tbl = fileassoc_table_lookup(vp->v_mount);
    144 	if (tbl == NULL)
    145 		return (NULL);
    146 
    147 	/*
    148 	 * XXX: We should NOT rely on fileid here!
    149 	 */
    150 	indx = FILEASSOC_HASH(tbl, va.va_fileid);
    151 	tble = &(tbl->hash_tbl[indx & ((tbl)->hash_mask)]);
    152 
    153 	LIST_FOREACH(e, tble, entries) {
    154 		if ((e != NULL) && (e->fileid == va.va_fileid))
    155 			return (e);
    156 	}
    157 
    158 	return (NULL);
    159 }
    160 
    161 /*
    162  * Return hook data associated with a vnode.
    163  */
    164 void *
    165 fileassoc_lookup(struct vnode *vp, fileassoc_t id)
    166 {
    167 	struct fileassoc_hash_entry *mhe;
    168 
    169 	mhe = fileassoc_file_lookup(vp);
    170 	if (mhe == NULL)
    171 		return (NULL);
    172 
    173 	return (mhe->hooks[id]);
    174 }
    175 
    176 /*
    177  * Create a new fileassoc table.
    178  */
    179 int
    180 fileassoc_table_add(struct mount *mp, size_t size)
    181 {
    182 	struct fileassoc_table *tbl;
    183 
    184 	/* Check for existing table for device. */
    185 	if (fileassoc_table_lookup(mp) != NULL)
    186 		return (EEXIST);
    187 
    188 	/* Allocate and initialize a Veriexec hash table. */
    189 	tbl = malloc(sizeof(*tbl), M_TEMP, M_WAITOK | M_ZERO);
    190 	tbl->hash_size = size;
    191 	tbl->tbl_mntpt = mp;
    192 	tbl->hash_tbl = hashinit(size, HASH_LIST, M_TEMP,
    193 				 M_WAITOK | M_ZERO, &tbl->hash_mask);
    194 
    195 	LIST_INSERT_HEAD(&fileassoc_tables, tbl, hash_list);
    196 
    197 	return (0);
    198 }
    199 
    200 /*
    201  * Delete a table.
    202  */
    203 int
    204 fileassoc_table_delete(struct mount *mp)
    205 {
    206 	struct fileassoc_table *tbl;
    207 	struct fileassoc_hashhead *hh;
    208 	u_long i;
    209 	int j;
    210 
    211 	tbl = fileassoc_table_lookup(mp);
    212 	if (tbl == NULL)
    213 		return (EEXIST);
    214 
    215 	/* Remove all entries from the table and lists */
    216 	hh = tbl->hash_tbl;
    217 	for (i = 0; i < tbl->hash_size; i++) {
    218 		struct fileassoc_hash_entry *mhe;
    219 
    220 		while (LIST_FIRST(&hh[i]) != NULL) {
    221 			mhe = LIST_FIRST(&hh[i]);
    222 			LIST_REMOVE(mhe, entries);
    223 
    224 			for (j = 0; j < fileassoc_nhooks; j++)
    225 				if (fileassoc_hooks[j].hook_cleanup_cb != NULL)
    226 					(fileassoc_hooks[j].hook_cleanup_cb)(mhe->hooks[j], 1);
    227 
    228 			free(mhe, M_TEMP);
    229 		}
    230 	}
    231 
    232 	for (j = 0; j < fileassoc_nhooks; j++)
    233 		if (fileassoc_hooks[j].hook_cleanup_cb != NULL)
    234 			(fileassoc_hooks[j].hook_cleanup_cb)(tbl->tables[j], 0);
    235 
    236 	/* Remove hash table and sysctl node */
    237 	hashdone(tbl->hash_tbl, M_TEMP);
    238 	LIST_REMOVE(tbl, hash_list);
    239 
    240 	return (0);
    241 }
    242 
    243 /*
    244  * Clear a table for a given hook.
    245  */
    246 int
    247 fileassoc_table_clear(struct mount *mp, fileassoc_t id)
    248 {
    249 	struct fileassoc_table *tbl;
    250 	struct fileassoc_hashhead *hh;
    251 	fileassoc_cleanup_cb_t cleanup_cb;
    252 	u_long i;
    253 
    254 	tbl = fileassoc_table_lookup(mp);
    255 	if (tbl == NULL)
    256 		return (EEXIST);
    257 
    258 	cleanup_cb = fileassoc_hooks[id].hook_cleanup_cb;
    259 
    260 	hh = tbl->hash_tbl;
    261 	for (i = 0; i < tbl->hash_size; i++) {
    262 		struct fileassoc_hash_entry *mhe;
    263 
    264 		LIST_FOREACH(mhe, &hh[i], entries) {
    265 			if ((mhe->hooks[id] != NULL) && cleanup_cb != NULL)
    266 				cleanup_cb(mhe->hooks[id], FILEASSOC_CLEANUP_FILE);
    267 
    268 			mhe->hooks[id] = NULL;
    269 		}
    270 	}
    271 
    272 	if ((tbl->tables[id] != NULL) && cleanup_cb != NULL)
    273 		cleanup_cb(tbl->tables[id], FILEASSOC_CLEANUP_TABLE);
    274 
    275 	tbl->tables[id] = NULL;
    276 
    277 	return (0);
    278 }
    279 
    280 /*
    281  * Add hook-specific data on a fileassoc table.
    282  */
    283 int
    284 fileassoc_tabledata_add(struct mount *mp, fileassoc_t id, void *data)
    285 {
    286 	struct fileassoc_table *tbl;
    287 
    288 	tbl = fileassoc_table_lookup(mp);
    289 	if (tbl == NULL)
    290 		return (EFAULT);
    291 
    292 	tbl->tables[id] = data;
    293 
    294 	return (0);
    295 }
    296 
    297 /*
    298  * Clear hook-specific data on a fileassoc table.
    299  */
    300 int
    301 fileassoc_tabledata_clear(struct mount *mp, fileassoc_t id)
    302 {
    303 	struct fileassoc_table *tbl;
    304 
    305 	tbl = fileassoc_table_lookup(mp);
    306 	if (tbl == NULL)
    307 		return (EFAULT);
    308 
    309 	tbl->tables[id] = NULL;
    310 
    311 	return (0);
    312 }
    313 
    314 /*
    315  * Retrieve hook-specific data from a fileassoc table.
    316  */
    317 void *
    318 fileassoc_tabledata_lookup(struct mount *mp, fileassoc_t id)
    319 {
    320 	struct fileassoc_table *tbl;
    321 
    322 	tbl = fileassoc_table_lookup(mp);
    323 	if (tbl == NULL)
    324 		return (NULL);
    325 
    326 	return (tbl->tables[id]);
    327 }
    328 
    329 /*
    330  * Add a file entry to a table.
    331  */
    332 struct fileassoc_hash_entry *
    333 fileassoc_file_add(struct vnode *vp)
    334 {
    335 	struct fileassoc_table *tbl;
    336 	struct fileassoc_hashhead *vhh;
    337 	struct fileassoc_hash_entry *e;
    338 	struct vattr va;
    339 	size_t indx;
    340 	int error;
    341 
    342 	error = VOP_GETATTR(vp, &va, curlwp->l_proc->p_cred, curlwp);
    343 	if (error)
    344 		return (NULL);
    345 
    346 	e = fileassoc_file_lookup(vp);
    347 	if (e != NULL)
    348 		return (e);
    349 
    350 	tbl = fileassoc_table_lookup(vp->v_mount);
    351 	if (tbl == NULL)
    352 		return (NULL);
    353 
    354 	/*
    355 	 * XXX: We should NOT rely on fileid here!
    356 	 */
    357 	indx = FILEASSOC_HASH(tbl, va.va_fileid);
    358 	vhh = &(tbl->hash_tbl[indx & ((tbl)->hash_mask)]);
    359 
    360 	e = malloc(sizeof(*e), M_TEMP, M_WAITOK | M_ZERO);
    361 	e->fileid = va.va_fileid;
    362 	LIST_INSERT_HEAD(vhh, e, entries);
    363 
    364 	return (e);
    365 }
    366 
    367 /*
    368  * Delete a file entry from a table.
    369  */
    370 int
    371 fileassoc_file_delete(struct vnode *vp)
    372 {
    373 	struct fileassoc_hash_entry *mhe;
    374 	int i;
    375 
    376 	mhe = fileassoc_file_lookup(vp);
    377 	if (mhe == NULL)
    378 		return (ENOENT);
    379 
    380 	LIST_REMOVE(mhe, entries);
    381 
    382 	for (i = 0; i < fileassoc_nhooks; i++)
    383 		if (fileassoc_hooks[i].hook_cleanup_cb != NULL)
    384 			(fileassoc_hooks[i].hook_cleanup_cb)(mhe->hooks[i],
    385 			    FILEASSOC_CLEANUP_FILE);
    386 
    387 	free(mhe, M_TEMP);
    388 
    389 	return (0);
    390 }
    391 
    392 /*
    393  * Add a hook to a vnode.
    394  */
    395 int
    396 fileassoc_add(struct vnode *vp, fileassoc_t id, void *data)
    397 {
    398 	struct fileassoc_hash_entry *e;
    399 
    400 	e = fileassoc_file_lookup(vp);
    401 	if (e == NULL) {
    402 		e = fileassoc_file_add(vp);
    403 		if (e == NULL)
    404 			return (ENOTDIR);
    405 	}
    406 
    407 	if (e->hooks[id] != NULL)
    408 		return (EEXIST);
    409 
    410 	e->hooks[id] = data;
    411 
    412 	return (0);
    413 }
    414 
    415 /*
    416  * Clear a hook from a vnode.
    417  */
    418 int
    419 fileassoc_clear(struct vnode *vp, fileassoc_t id)
    420 {
    421 	struct fileassoc_hash_entry *mhe;
    422 	fileassoc_cleanup_cb_t cleanup_cb;
    423 
    424 	mhe = fileassoc_file_lookup(vp);
    425 	if (mhe == NULL)
    426 		return (ENOENT);
    427 
    428 	cleanup_cb = fileassoc_hooks[id].hook_cleanup_cb;
    429 	if ((mhe->hooks[id] != NULL) && cleanup_cb != NULL)
    430 		cleanup_cb(mhe->hooks[id], FILEASSOC_CLEANUP_FILE);
    431 
    432 	mhe->hooks[id] = NULL;
    433 
    434 	return (0);
    435 }
    436