kern_fileassoc.c revision 1.37 1 1.37 riastrad /* $NetBSD: kern_fileassoc.c,v 1.37 2023/08/02 07:11:31 riastradh 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.37 riastrad __KERNEL_RCSID(0, "$NetBSD: kern_fileassoc.c,v 1.37 2023/08/02 07:11:31 riastradh 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.14 yamt return 0;
223 1.1 elad }
224 1.1 elad
225 1.1 elad /*
226 1.33 elad * Register a new assoc.
227 1.1 elad */
228 1.14 yamt int
229 1.14 yamt fileassoc_register(const char *name, fileassoc_cleanup_cb_t cleanup_cb,
230 1.14 yamt fileassoc_t *result)
231 1.1 elad {
232 1.14 yamt int error;
233 1.14 yamt specificdata_key_t key;
234 1.14 yamt struct fileassoc *assoc;
235 1.14 yamt
236 1.16 yamt error = RUN_ONCE(&control, fileassoc_init);
237 1.16 yamt if (error) {
238 1.16 yamt return error;
239 1.16 yamt }
240 1.14 yamt error = specificdata_key_create(fileassoc_domain, &key, NULL);
241 1.14 yamt if (error) {
242 1.14 yamt return error;
243 1.14 yamt }
244 1.14 yamt assoc = kmem_alloc(sizeof(*assoc), KM_SLEEP);
245 1.33 elad assoc->assoc_name = name;
246 1.33 elad assoc->assoc_cleanup_cb = cleanup_cb;
247 1.33 elad assoc->assoc_key = key;
248 1.33 elad
249 1.33 elad LIST_INSERT_HEAD(&fileassoc_list, assoc, assoc_list);
250 1.33 elad
251 1.14 yamt *result = assoc;
252 1.1 elad
253 1.14 yamt return 0;
254 1.1 elad }
255 1.1 elad
256 1.1 elad /*
257 1.33 elad * Deregister an assoc.
258 1.1 elad */
259 1.1 elad int
260 1.14 yamt fileassoc_deregister(fileassoc_t assoc)
261 1.1 elad {
262 1.1 elad
263 1.33 elad LIST_REMOVE(assoc, assoc_list);
264 1.33 elad specificdata_key_delete(fileassoc_domain, assoc->assoc_key);
265 1.14 yamt kmem_free(assoc, sizeof(*assoc));
266 1.1 elad
267 1.14 yamt return 0;
268 1.1 elad }
269 1.1 elad
270 1.1 elad /*
271 1.1 elad * Get the hash table for the specified device.
272 1.1 elad */
273 1.3 elad static struct fileassoc_table *
274 1.1 elad fileassoc_table_lookup(struct mount *mp)
275 1.1 elad {
276 1.25 yamt int error;
277 1.1 elad
278 1.37 riastrad if (!fileassoc_inuse())
279 1.37 riastrad return NULL;
280 1.37 riastrad
281 1.25 yamt error = RUN_ONCE(&control, fileassoc_init);
282 1.25 yamt if (error) {
283 1.25 yamt return NULL;
284 1.25 yamt }
285 1.16 yamt return mount_getspecific(mp, fileassoc_mountspecific_key);
286 1.1 elad }
287 1.1 elad
288 1.1 elad /*
289 1.8 blymn * Perform a lookup on a hash table. If hint is non-zero then use the value
290 1.8 blymn * of the hint as the identifier instead of performing a lookup for the
291 1.8 blymn * fileid.
292 1.1 elad */
293 1.33 elad static struct fileassoc_file *
294 1.9 blymn fileassoc_file_lookup(struct vnode *vp, fhandle_t *hint)
295 1.1 elad {
296 1.1 elad struct fileassoc_table *tbl;
297 1.33 elad struct fileassoc_hash_entry *hash_entry;
298 1.33 elad struct fileassoc_file *faf;
299 1.1 elad size_t indx;
300 1.9 blymn fhandle_t *th;
301 1.1 elad int error;
302 1.1 elad
303 1.16 yamt tbl = fileassoc_table_lookup(vp->v_mount);
304 1.16 yamt if (tbl == NULL) {
305 1.16 yamt return NULL;
306 1.16 yamt }
307 1.16 yamt
308 1.9 blymn if (hint == NULL) {
309 1.9 blymn error = vfs_composefh_alloc(vp, &th);
310 1.8 blymn if (error)
311 1.8 blymn return (NULL);
312 1.16 yamt } else {
313 1.8 blymn th = hint;
314 1.10 blymn }
315 1.1 elad
316 1.8 blymn indx = FILEASSOC_HASH(tbl, th);
317 1.33 elad hash_entry = &(tbl->tbl_hash[indx]);
318 1.1 elad
319 1.33 elad LIST_FOREACH(faf, hash_entry, faf_list) {
320 1.33 elad if (((FHANDLE_FILEID(faf->faf_handle)->fid_len ==
321 1.10 blymn FHANDLE_FILEID(th)->fid_len)) &&
322 1.33 elad (memcmp(FHANDLE_FILEID(faf->faf_handle), FHANDLE_FILEID(th),
323 1.11 elad (FHANDLE_FILEID(th))->fid_len) == 0)) {
324 1.16 yamt break;
325 1.11 elad }
326 1.1 elad }
327 1.1 elad
328 1.10 blymn if (hint == NULL)
329 1.10 blymn vfs_composefh_free(th);
330 1.10 blymn
331 1.33 elad return faf;
332 1.1 elad }
333 1.1 elad
334 1.1 elad /*
335 1.33 elad * Return assoc data associated with a vnode.
336 1.1 elad */
337 1.1 elad void *
338 1.14 yamt fileassoc_lookup(struct vnode *vp, fileassoc_t assoc)
339 1.1 elad {
340 1.33 elad struct fileassoc_file *faf;
341 1.8 blymn
342 1.33 elad faf = fileassoc_file_lookup(vp, NULL);
343 1.33 elad if (faf == NULL)
344 1.33 elad return (NULL);
345 1.1 elad
346 1.33 elad return file_getdata(faf, assoc);
347 1.1 elad }
348 1.1 elad
349 1.22 elad static struct fileassoc_table *
350 1.22 elad fileassoc_table_resize(struct fileassoc_table *tbl)
351 1.22 elad {
352 1.22 elad struct fileassoc_table *newtbl;
353 1.22 elad u_long i;
354 1.22 elad
355 1.22 elad /*
356 1.22 elad * Allocate a new table. Like the condition in fileassoc_file_add(),
357 1.22 elad * this is also temporary -- just double the number of slots.
358 1.22 elad */
359 1.22 elad newtbl = kmem_zalloc(sizeof(*newtbl), KM_SLEEP);
360 1.33 elad newtbl->tbl_nslots = (tbl->tbl_nslots * 2);
361 1.33 elad if (newtbl->tbl_nslots < tbl->tbl_nslots)
362 1.33 elad newtbl->tbl_nslots = tbl->tbl_nslots;
363 1.33 elad newtbl->tbl_hash = hashinit(newtbl->tbl_nslots, HASH_LIST,
364 1.33 elad true, &newtbl->tbl_mask);
365 1.33 elad newtbl->tbl_nused = 0;
366 1.33 elad specificdata_init(fileassoc_domain, &newtbl->tbl_data);
367 1.22 elad
368 1.22 elad /* XXX we need to make sure nothing uses fileassoc here! */
369 1.22 elad
370 1.33 elad for (i = 0; i < tbl->tbl_nslots; i++) {
371 1.33 elad struct fileassoc_file *faf;
372 1.22 elad
373 1.33 elad while ((faf = LIST_FIRST(&tbl->tbl_hash[i])) != NULL) {
374 1.33 elad struct fileassoc_hash_entry *hash_entry;
375 1.22 elad size_t indx;
376 1.22 elad
377 1.33 elad LIST_REMOVE(faf, faf_list);
378 1.22 elad
379 1.33 elad indx = FILEASSOC_HASH(newtbl, faf->faf_handle);
380 1.33 elad hash_entry = &(newtbl->tbl_hash[indx]);
381 1.22 elad
382 1.33 elad LIST_INSERT_HEAD(hash_entry, faf, faf_list);
383 1.22 elad
384 1.33 elad newtbl->tbl_nused++;
385 1.22 elad }
386 1.22 elad }
387 1.22 elad
388 1.33 elad if (tbl->tbl_nused != newtbl->tbl_nused)
389 1.22 elad panic("fileassoc_table_resize: inconsistency detected! "
390 1.33 elad "needed %zu entries, got %zu", tbl->tbl_nused,
391 1.33 elad newtbl->tbl_nused);
392 1.22 elad
393 1.33 elad hashdone(tbl->tbl_hash, HASH_LIST, tbl->tbl_mask);
394 1.33 elad specificdata_fini(fileassoc_domain, &tbl->tbl_data);
395 1.22 elad kmem_free(tbl, sizeof(*tbl));
396 1.22 elad
397 1.22 elad return (newtbl);
398 1.22 elad }
399 1.22 elad
400 1.1 elad /*
401 1.1 elad * Create a new fileassoc table.
402 1.1 elad */
403 1.23 elad static struct fileassoc_table *
404 1.22 elad fileassoc_table_add(struct mount *mp)
405 1.1 elad {
406 1.1 elad struct fileassoc_table *tbl;
407 1.1 elad
408 1.1 elad /* Check for existing table for device. */
409 1.23 elad tbl = fileassoc_table_lookup(mp);
410 1.23 elad if (tbl != NULL)
411 1.23 elad return (tbl);
412 1.1 elad
413 1.18 elad /* Allocate and initialize a table. */
414 1.14 yamt tbl = kmem_zalloc(sizeof(*tbl), KM_SLEEP);
415 1.33 elad tbl->tbl_nslots = FILEASSOC_INITIAL_TABLESIZE;
416 1.33 elad tbl->tbl_hash = hashinit(tbl->tbl_nslots, HASH_LIST, true,
417 1.33 elad &tbl->tbl_mask);
418 1.33 elad tbl->tbl_nused = 0;
419 1.33 elad specificdata_init(fileassoc_domain, &tbl->tbl_data);
420 1.1 elad
421 1.16 yamt mount_setspecific(mp, fileassoc_mountspecific_key, tbl);
422 1.1 elad
423 1.23 elad return (tbl);
424 1.1 elad }
425 1.1 elad
426 1.1 elad /*
427 1.1 elad * Delete a table.
428 1.1 elad */
429 1.1 elad int
430 1.1 elad fileassoc_table_delete(struct mount *mp)
431 1.1 elad {
432 1.1 elad struct fileassoc_table *tbl;
433 1.1 elad
434 1.1 elad tbl = fileassoc_table_lookup(mp);
435 1.1 elad if (tbl == NULL)
436 1.1 elad return (EEXIST);
437 1.1 elad
438 1.16 yamt mount_setspecific(mp, fileassoc_mountspecific_key, NULL);
439 1.16 yamt table_dtor(tbl);
440 1.1 elad
441 1.1 elad return (0);
442 1.1 elad }
443 1.1 elad
444 1.1 elad /*
445 1.33 elad * Run a callback for each assoc in a table.
446 1.6 christos */
447 1.6 christos int
448 1.29 elad fileassoc_table_run(struct mount *mp, fileassoc_t assoc, fileassoc_cb_t cb,
449 1.29 elad void *cookie)
450 1.6 christos {
451 1.6 christos struct fileassoc_table *tbl;
452 1.6 christos u_long i;
453 1.6 christos
454 1.6 christos tbl = fileassoc_table_lookup(mp);
455 1.6 christos if (tbl == NULL)
456 1.6 christos return (EEXIST);
457 1.6 christos
458 1.33 elad for (i = 0; i < tbl->tbl_nslots; i++) {
459 1.33 elad struct fileassoc_file *faf;
460 1.6 christos
461 1.33 elad LIST_FOREACH(faf, &tbl->tbl_hash[i], faf_list) {
462 1.14 yamt void *data;
463 1.14 yamt
464 1.33 elad data = file_getdata(faf, assoc);
465 1.14 yamt if (data != NULL)
466 1.29 elad cb(data, cookie);
467 1.6 christos }
468 1.6 christos }
469 1.6 christos
470 1.6 christos return (0);
471 1.6 christos }
472 1.6 christos
473 1.6 christos /*
474 1.33 elad * Clear a table for a given assoc.
475 1.1 elad */
476 1.1 elad int
477 1.14 yamt fileassoc_table_clear(struct mount *mp, fileassoc_t assoc)
478 1.1 elad {
479 1.1 elad struct fileassoc_table *tbl;
480 1.1 elad u_long i;
481 1.1 elad
482 1.1 elad tbl = fileassoc_table_lookup(mp);
483 1.1 elad if (tbl == NULL)
484 1.1 elad return (EEXIST);
485 1.1 elad
486 1.33 elad for (i = 0; i < tbl->tbl_nslots; i++) {
487 1.33 elad struct fileassoc_file *faf;
488 1.33 elad
489 1.33 elad LIST_FOREACH(faf, &tbl->tbl_hash[i], faf_list) {
490 1.33 elad file_cleanup(faf, assoc);
491 1.33 elad file_setdata(faf, assoc, NULL);
492 1.37 riastrad /* XXX missing faf->faf_nassocs--? */
493 1.37 riastrad fileassoc_decuse();
494 1.1 elad }
495 1.1 elad }
496 1.1 elad
497 1.1 elad return (0);
498 1.1 elad }
499 1.1 elad
500 1.1 elad /*
501 1.1 elad * Add a file entry to a table.
502 1.1 elad */
503 1.33 elad static struct fileassoc_file *
504 1.9 blymn fileassoc_file_add(struct vnode *vp, fhandle_t *hint)
505 1.1 elad {
506 1.1 elad struct fileassoc_table *tbl;
507 1.33 elad struct fileassoc_hash_entry *hash_entry;
508 1.33 elad struct fileassoc_file *faf;
509 1.1 elad size_t indx;
510 1.9 blymn fhandle_t *th;
511 1.1 elad int error;
512 1.1 elad
513 1.12 elad if (hint == NULL) {
514 1.9 blymn error = vfs_composefh_alloc(vp, &th);
515 1.8 blymn if (error)
516 1.8 blymn return (NULL);
517 1.8 blymn } else
518 1.9 blymn th = hint;
519 1.1 elad
520 1.33 elad faf = fileassoc_file_lookup(vp, th);
521 1.33 elad if (faf != NULL) {
522 1.10 blymn if (hint == NULL)
523 1.10 blymn vfs_composefh_free(th);
524 1.10 blymn
525 1.33 elad return (faf);
526 1.10 blymn }
527 1.1 elad
528 1.1 elad tbl = fileassoc_table_lookup(vp->v_mount);
529 1.10 blymn if (tbl == NULL) {
530 1.23 elad tbl = fileassoc_table_add(vp->v_mount);
531 1.10 blymn }
532 1.1 elad
533 1.9 blymn indx = FILEASSOC_HASH(tbl, th);
534 1.33 elad hash_entry = &(tbl->tbl_hash[indx]);
535 1.1 elad
536 1.33 elad faf = kmem_zalloc(sizeof(*faf), KM_SLEEP);
537 1.33 elad faf->faf_handle = th;
538 1.33 elad specificdata_init(fileassoc_domain, &faf->faf_data);
539 1.33 elad LIST_INSERT_HEAD(hash_entry, faf, faf_list);
540 1.1 elad
541 1.22 elad /*
542 1.22 elad * This decides when we need to resize the table. For now,
543 1.22 elad * resize it whenever we "filled" up the number of slots it
544 1.22 elad * has. That's not really true unless of course we had zero
545 1.22 elad * collisions. Think positive! :)
546 1.22 elad */
547 1.33 elad if (++(tbl->tbl_nused) == tbl->tbl_nslots) {
548 1.22 elad struct fileassoc_table *newtbl;
549 1.22 elad
550 1.22 elad newtbl = fileassoc_table_resize(tbl);
551 1.22 elad mount_setspecific(vp->v_mount, fileassoc_mountspecific_key,
552 1.22 elad newtbl);
553 1.22 elad }
554 1.22 elad
555 1.33 elad return (faf);
556 1.1 elad }
557 1.1 elad
558 1.1 elad /*
559 1.1 elad * Delete a file entry from a table.
560 1.1 elad */
561 1.1 elad int
562 1.1 elad fileassoc_file_delete(struct vnode *vp)
563 1.1 elad {
564 1.23 elad struct fileassoc_table *tbl;
565 1.33 elad struct fileassoc_file *faf;
566 1.1 elad
567 1.37 riastrad if (!fileassoc_inuse())
568 1.35 rmind return ENOENT;
569 1.37 riastrad
570 1.30 ad KERNEL_LOCK(1, NULL);
571 1.30 ad
572 1.33 elad faf = fileassoc_file_lookup(vp, NULL);
573 1.33 elad if (faf == NULL) {
574 1.30 ad KERNEL_UNLOCK_ONE(NULL);
575 1.1 elad return (ENOENT);
576 1.30 ad }
577 1.1 elad
578 1.33 elad file_free(faf);
579 1.1 elad
580 1.23 elad tbl = fileassoc_table_lookup(vp->v_mount);
581 1.36 christos KASSERT(tbl != NULL);
582 1.33 elad --(tbl->tbl_nused); /* XXX gc? */
583 1.23 elad
584 1.30 ad KERNEL_UNLOCK_ONE(NULL);
585 1.30 ad
586 1.1 elad return (0);
587 1.1 elad }
588 1.1 elad
589 1.1 elad /*
590 1.33 elad * Add an assoc to a vnode.
591 1.1 elad */
592 1.1 elad int
593 1.14 yamt fileassoc_add(struct vnode *vp, fileassoc_t assoc, void *data)
594 1.1 elad {
595 1.33 elad struct fileassoc_file *faf;
596 1.14 yamt void *olddata;
597 1.1 elad
598 1.33 elad faf = fileassoc_file_lookup(vp, NULL);
599 1.33 elad if (faf == NULL) {
600 1.33 elad faf = fileassoc_file_add(vp, NULL);
601 1.33 elad if (faf == NULL)
602 1.1 elad return (ENOTDIR);
603 1.1 elad }
604 1.1 elad
605 1.33 elad olddata = file_getdata(faf, assoc);
606 1.14 yamt if (olddata != NULL)
607 1.1 elad return (EEXIST);
608 1.1 elad
609 1.37 riastrad fileassoc_incuse();
610 1.37 riastrad
611 1.33 elad file_setdata(faf, assoc, data);
612 1.1 elad
613 1.33 elad faf->faf_nassocs++;
614 1.23 elad
615 1.1 elad return (0);
616 1.1 elad }
617 1.1 elad
618 1.1 elad /*
619 1.33 elad * Clear an assoc from a vnode.
620 1.1 elad */
621 1.1 elad int
622 1.14 yamt fileassoc_clear(struct vnode *vp, fileassoc_t assoc)
623 1.1 elad {
624 1.33 elad struct fileassoc_file *faf;
625 1.1 elad
626 1.33 elad faf = fileassoc_file_lookup(vp, NULL);
627 1.33 elad if (faf == NULL)
628 1.1 elad return (ENOENT);
629 1.1 elad
630 1.33 elad file_cleanup(faf, assoc);
631 1.33 elad file_setdata(faf, assoc, NULL);
632 1.1 elad
633 1.33 elad --(faf->faf_nassocs); /* XXX gc? */
634 1.23 elad
635 1.37 riastrad fileassoc_decuse();
636 1.37 riastrad
637 1.1 elad return (0);
638 1.1 elad }
639