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