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