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