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