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