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