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