coda_namecache.c revision 1.1 1 1.1 rvb /*
2 1.1 rvb
3 1.1 rvb Coda: an Experimental Distributed File System
4 1.1 rvb Release 3.1
5 1.1 rvb
6 1.1 rvb Copyright (c) 1987-1998 Carnegie Mellon University
7 1.1 rvb All Rights Reserved
8 1.1 rvb
9 1.1 rvb Permission to use, copy, modify and distribute this software and its
10 1.1 rvb documentation is hereby granted, provided that both the copyright
11 1.1 rvb notice and this permission notice appear in all copies of the
12 1.1 rvb software, derivative works or modified versions, and any portions
13 1.1 rvb thereof, and that both notices appear in supporting documentation, and
14 1.1 rvb that credit is given to Carnegie Mellon University in all documents
15 1.1 rvb and publicity pertaining to direct or indirect use of this code or its
16 1.1 rvb derivatives.
17 1.1 rvb
18 1.1 rvb CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
19 1.1 rvb SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
20 1.1 rvb FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
21 1.1 rvb DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
22 1.1 rvb RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
23 1.1 rvb ANY DERIVATIVE WORK.
24 1.1 rvb
25 1.1 rvb Carnegie Mellon encourages users of this software to return any
26 1.1 rvb improvements or extensions that they make, and to grant Carnegie
27 1.1 rvb Mellon the rights to redistribute these changes without encumbrance.
28 1.1 rvb */
29 1.1 rvb
30 1.1 rvb /* $Header: /tank/opengrok/rsync2/NetBSD/src/sys/coda/coda_namecache.c,v 1.1 1998/08/29 21:26:45 rvb Exp $ */
31 1.1 rvb
32 1.1 rvb /*
33 1.1 rvb * Mach Operating System
34 1.1 rvb * Copyright (c) 1990 Carnegie-Mellon University
35 1.1 rvb * Copyright (c) 1989 Carnegie-Mellon University
36 1.1 rvb * All rights reserved. The CMU software License Agreement specifies
37 1.1 rvb * the terms and conditions for use and redistribution.
38 1.1 rvb */
39 1.1 rvb
40 1.1 rvb /*
41 1.1 rvb * This code was written for the Coda file system at Carnegie Mellon University.
42 1.1 rvb * Contributers include David Steere, James Kistler, and M. Satyanarayanan.
43 1.1 rvb */
44 1.1 rvb
45 1.1 rvb /*
46 1.1 rvb * HISTORY
47 1.1 rvb * $Log: coda_namecache.c,v $
48 1.1 rvb * Revision 1.1 1998/08/29 21:26:45 rvb
49 1.1 rvb * Initial revision
50 1.1 rvb *
51 1.1 rvb * Revision 1.11 1998/08/28 18:12:16 rvb
52 1.1 rvb * Now it also works on FreeBSD -current. This code will be
53 1.1 rvb * committed to the FreeBSD -current and NetBSD -current
54 1.1 rvb * trees. It will then be tailored to the particular platform
55 1.1 rvb * by flushing conditional code.
56 1.1 rvb *
57 1.1 rvb * Revision 1.10 1998/08/18 17:05:14 rvb
58 1.1 rvb * Don't use __RCSID now
59 1.1 rvb *
60 1.1 rvb * Revision 1.9 1998/08/18 16:31:39 rvb
61 1.1 rvb * Sync the code for NetBSD -current; test on 1.3 later
62 1.1 rvb *
63 1.1 rvb * Revision 1.8 98/01/31 20:53:10 rvb
64 1.1 rvb * First version that works on FreeBSD 2.2.5
65 1.1 rvb *
66 1.1 rvb * Revision 1.7 98/01/23 11:53:39 rvb
67 1.1 rvb * Bring RVB_CFS1_1 to HEAD
68 1.1 rvb *
69 1.1 rvb * Revision 1.6.2.4 98/01/23 11:21:02 rvb
70 1.1 rvb * Sync with 2.2.5
71 1.1 rvb *
72 1.1 rvb * Revision 1.6.2.3 97/12/16 12:40:03 rvb
73 1.1 rvb * Sync with 1.3
74 1.1 rvb *
75 1.1 rvb * Revision 1.6.2.2 97/12/09 16:07:10 rvb
76 1.1 rvb * Sync with vfs/include/coda.h
77 1.1 rvb *
78 1.1 rvb * Revision 1.6.2.1 97/12/06 17:41:18 rvb
79 1.1 rvb * Sync with peters coda.h
80 1.1 rvb *
81 1.1 rvb * Revision 1.6 97/12/05 10:39:13 rvb
82 1.1 rvb * Read CHANGES
83 1.1 rvb *
84 1.1 rvb * Revision 1.5.4.7 97/11/25 08:08:43 rvb
85 1.1 rvb * cfs_venus ... done; until cred/vattr change
86 1.1 rvb *
87 1.1 rvb * Revision 1.5.4.6 97/11/24 15:44:43 rvb
88 1.1 rvb * Final cfs_venus.c w/o macros, but one locking bug
89 1.1 rvb *
90 1.1 rvb * Revision 1.5.4.5 97/11/20 11:46:38 rvb
91 1.1 rvb * Capture current cfs_venus
92 1.1 rvb *
93 1.1 rvb * Revision 1.5.4.4 97/11/18 10:27:13 rvb
94 1.1 rvb * cfs_nbsd.c is DEAD!!!; integrated into cfs_vf/vnops.c
95 1.1 rvb * cfs_nb_foo and cfs_foo are joined
96 1.1 rvb *
97 1.1 rvb * Revision 1.5.4.3 97/11/13 22:02:57 rvb
98 1.1 rvb * pass2 cfs_NetBSD.h mt
99 1.1 rvb *
100 1.1 rvb * Revision 1.5.4.2 97/11/12 12:09:35 rvb
101 1.1 rvb * reorg pass1
102 1.1 rvb *
103 1.1 rvb * Revision 1.5.4.1 97/10/28 23:10:12 rvb
104 1.1 rvb * >64Meg; venus can be killed!
105 1.1 rvb *
106 1.1 rvb * Revision 1.5 97/08/05 11:08:01 lily
107 1.1 rvb * Removed cfsnc_replace, replaced it with a cfs_find, unhash, and
108 1.1 rvb * rehash. This fixes a cnode leak and a bug in which the fid is
109 1.1 rvb * not actually replaced. (cfs_namecache.c, cfsnc.h, cfs_subr.c)
110 1.1 rvb *
111 1.1 rvb * Revision 1.4 96/12/12 22:10:57 bnoble
112 1.1 rvb * Fixed the "downcall invokes venus operation" deadlock in all known cases.
113 1.1 rvb * There may be more
114 1.1 rvb *
115 1.1 rvb * Revision 1.3 1996/11/08 18:06:09 bnoble
116 1.1 rvb * Minor changes in vnode operation signature, VOP_UPDATE signature, and
117 1.1 rvb * some newly defined bits in the include files.
118 1.1 rvb *
119 1.1 rvb * Revision 1.2 1996/01/02 16:56:50 bnoble
120 1.1 rvb * Added support for Coda MiniCache and raw inode calls (final commit)
121 1.1 rvb *
122 1.1 rvb * Revision 1.1.2.1 1995/12/20 01:57:15 bnoble
123 1.1 rvb * Added CFS-specific files
124 1.1 rvb *
125 1.1 rvb * Revision 3.1.1.1 1995/03/04 19:07:57 bnoble
126 1.1 rvb * Branch for NetBSD port revisions
127 1.1 rvb *
128 1.1 rvb * Revision 3.1 1995/03/04 19:07:56 bnoble
129 1.1 rvb * Bump to major revision 3 to prepare for NetBSD port
130 1.1 rvb *
131 1.1 rvb * Revision 2.3 1994/10/14 09:57:54 dcs
132 1.1 rvb * Made changes 'cause sun4s have braindead compilers
133 1.1 rvb *
134 1.1 rvb * Revision 2.2 94/08/28 19:37:35 luqi
135 1.1 rvb * Add a new CFS_REPLACE call to allow venus to replace a ViceFid in the
136 1.1 rvb * mini-cache.
137 1.1 rvb *
138 1.1 rvb * In "cfs.h":
139 1.1 rvb * Add CFS_REPLACE decl.
140 1.1 rvb *
141 1.1 rvb * In "cfs_namecache.c":
142 1.1 rvb * Add routine cfsnc_replace.
143 1.1 rvb *
144 1.1 rvb * In "cfs_subr.c":
145 1.1 rvb * Add case-statement to process CFS_REPLACE.
146 1.1 rvb *
147 1.1 rvb * In "cfsnc.h":
148 1.1 rvb * Add decl for CFSNC_REPLACE.
149 1.1 rvb *
150 1.1 rvb *
151 1.1 rvb * Revision 2.1 94/07/21 16:25:15 satya
152 1.1 rvb * Conversion to C++ 3.0; start of Coda Release 2.0
153 1.1 rvb *
154 1.1 rvb * Revision 1.2 92/10/27 17:58:21 lily
155 1.1 rvb * merge kernel/latest and alpha/src/cfs
156 1.1 rvb *
157 1.1 rvb * Revision 2.3 92/09/30 14:16:20 mja
158 1.1 rvb * call cfs_flush instead of calling inode_uncache_try directly
159 1.1 rvb * (from dcs). Also...
160 1.1 rvb *
161 1.1 rvb * Substituted rvb's history blurb so that we agree with Mach 2.5 sources.
162 1.1 rvb * [91/02/09 jjk]
163 1.1 rvb *
164 1.1 rvb * Added contributors blurb.
165 1.1 rvb * [90/12/13 jjk]
166 1.1 rvb *
167 1.1 rvb * Revision 2.2 90/07/05 11:26:30 mrt
168 1.1 rvb * Created for the Coda File System.
169 1.1 rvb * [90/05/23 dcs]
170 1.1 rvb *
171 1.1 rvb * Revision 1.3 90/05/31 17:01:24 dcs
172 1.1 rvb * Prepare for merge with facilities kernel.
173 1.1 rvb *
174 1.1 rvb *
175 1.1 rvb */
176 1.1 rvb
177 1.1 rvb /*
178 1.1 rvb * This module contains the routines to implement the CFS name cache. The
179 1.1 rvb * purpose of this cache is to reduce the cost of translating pathnames
180 1.1 rvb * into Vice FIDs. Each entry in the cache contains the name of the file,
181 1.1 rvb * the vnode (FID) of the parent directory, and the cred structure of the
182 1.1 rvb * user accessing the file.
183 1.1 rvb *
184 1.1 rvb * The first time a file is accessed, it is looked up by the local Venus
185 1.1 rvb * which first insures that the user has access to the file. In addition
186 1.1 rvb * we are guaranteed that Venus will invalidate any name cache entries in
187 1.1 rvb * case the user no longer should be able to access the file. For these
188 1.1 rvb * reasons we do not need to keep access list information as well as a
189 1.1 rvb * cred structure for each entry.
190 1.1 rvb *
191 1.1 rvb * The table can be accessed through the routines cnc_init(), cnc_enter(),
192 1.1 rvb * cnc_lookup(), cnc_rmfidcred(), cnc_rmfid(), cnc_rmcred(), and cnc_purge().
193 1.1 rvb * There are several other routines which aid in the implementation of the
194 1.1 rvb * hash table.
195 1.1 rvb */
196 1.1 rvb
197 1.1 rvb /*
198 1.1 rvb * NOTES: rvb@cs
199 1.1 rvb * 1. The name cache holds a reference to every vnode in it. Hence files can not be
200 1.1 rvb * closed or made inactive until they are released.
201 1.1 rvb * 2. cfsnc_name(cp) was added to get a name for a cnode pointer for debugging.
202 1.1 rvb * 3. cfsnc_find() has debug code to detect when entries are stored with different
203 1.1 rvb * credentials. We don't understand yet, if/how entries are NOT EQ but still
204 1.1 rvb * EQUAL
205 1.1 rvb * 4. I wonder if this name cache could be replace by the vnode name cache.
206 1.1 rvb * The latter has no zapping functions, so probably not.
207 1.1 rvb */
208 1.1 rvb
209 1.1 rvb #include <sys/param.h>
210 1.1 rvb #include <sys/errno.h>
211 1.1 rvb #include <sys/malloc.h>
212 1.1 rvb #include <sys/select.h>
213 1.1 rvb
214 1.1 rvb #include <cfs/coda.h>
215 1.1 rvb #include <cfs/cnode.h>
216 1.1 rvb #include <cfs/cfsnc.h>
217 1.1 rvb
218 1.1 rvb #if defined(__NetBSD__) || defined(__FreeBSD__)
219 1.1 rvb #ifndef insque
220 1.1 rvb #include <sys/systm.h>
221 1.1 rvb #endif /* insque */
222 1.1 rvb #endif /* __NetBSD__ || defined(__FreeBSD__) */
223 1.1 rvb
224 1.1 rvb #ifdef __FreeBSD__
225 1.1 rvb #include <vm/vm.h>
226 1.1 rvb #include <vm/vm_object.h>
227 1.1 rvb #ifdef __FreeBSD_version
228 1.1 rvb #include <sys/ucred.h>
229 1.1 rvb #endif
230 1.1 rvb #endif
231 1.1 rvb
232 1.1 rvb /*
233 1.1 rvb * Declaration of the name cache data structure.
234 1.1 rvb */
235 1.1 rvb
236 1.1 rvb int cfsnc_use = 1; /* Indicate use of CFS Name Cache */
237 1.1 rvb
238 1.1 rvb int cfsnc_size = CFSNC_CACHESIZE; /* size of the cache */
239 1.1 rvb int cfsnc_hashsize = CFSNC_HASHSIZE; /* size of the primary hash */
240 1.1 rvb
241 1.1 rvb struct cfscache *cfsncheap; /* pointer to the cache entries */
242 1.1 rvb struct cfshash *cfsnchash; /* hash table of cfscache pointers */
243 1.1 rvb struct cfslru cfsnc_lru; /* head of lru chain */
244 1.1 rvb
245 1.1 rvb struct cfsnc_statistics cfsnc_stat; /* Keep various stats */
246 1.1 rvb
247 1.1 rvb /*
248 1.1 rvb * for testing purposes
249 1.1 rvb */
250 1.1 rvb int cfsnc_debug = 0;
251 1.1 rvb
252 1.1 rvb
253 1.1 rvb /*
254 1.1 rvb * Entry points for the CFS Name Cache
255 1.1 rvb */
256 1.1 rvb static struct cfscache *
257 1.1 rvb cfsnc_find(struct cnode *dcp, const char *name, int namelen,
258 1.1 rvb struct ucred *cred, int hash);
259 1.1 rvb static void
260 1.1 rvb cfsnc_remove(struct cfscache *cncp, enum dc_status dcstat);
261 1.1 rvb
262 1.1 rvb
263 1.1 rvb /*
264 1.1 rvb * Initialize the cache, the LRU structure and the Hash structure(s)
265 1.1 rvb */
266 1.1 rvb
267 1.1 rvb #define TOTAL_CACHE_SIZE (sizeof(struct cfscache) * cfsnc_size)
268 1.1 rvb #define TOTAL_HASH_SIZE (sizeof(struct cfshash) * cfsnc_hashsize)
269 1.1 rvb
270 1.1 rvb int cfsnc_initialized = 0; /* Initially the cache has not been initialized */
271 1.1 rvb
272 1.1 rvb void
273 1.1 rvb cfsnc_init(void)
274 1.1 rvb {
275 1.1 rvb int i;
276 1.1 rvb
277 1.1 rvb /* zero the statistics structure */
278 1.1 rvb
279 1.1 rvb bzero(&cfsnc_stat, (sizeof(struct cfsnc_statistics)));
280 1.1 rvb
281 1.1 rvb printf("CFS NAME CACHE: CACHE %d, HASH TBL %d\n", CFSNC_CACHESIZE, CFSNC_HASHSIZE);
282 1.1 rvb CFS_ALLOC(cfsncheap, struct cfscache *, TOTAL_CACHE_SIZE);
283 1.1 rvb CFS_ALLOC(cfsnchash, struct cfshash *, TOTAL_HASH_SIZE);
284 1.1 rvb
285 1.1 rvb cfsnc_lru.lru_next =
286 1.1 rvb cfsnc_lru.lru_prev = (struct cfscache *)LRU_PART(&cfsnc_lru);
287 1.1 rvb
288 1.1 rvb
289 1.1 rvb for (i=0; i < cfsnc_size; i++) { /* initialize the heap */
290 1.1 rvb CFSNC_LRUINS(&cfsncheap[i], &cfsnc_lru);
291 1.1 rvb CFSNC_HSHNUL(&cfsncheap[i]);
292 1.1 rvb cfsncheap[i].cp = cfsncheap[i].dcp = (struct cnode *)0;
293 1.1 rvb }
294 1.1 rvb
295 1.1 rvb for (i=0; i < cfsnc_hashsize; i++) { /* initialize the hashtable */
296 1.1 rvb CFSNC_HSHNUL((struct cfscache *)&cfsnchash[i]);
297 1.1 rvb }
298 1.1 rvb
299 1.1 rvb cfsnc_initialized++;
300 1.1 rvb }
301 1.1 rvb
302 1.1 rvb /*
303 1.1 rvb * Auxillary routines -- shouldn't be entry points
304 1.1 rvb */
305 1.1 rvb
306 1.1 rvb static struct cfscache *
307 1.1 rvb cfsnc_find(dcp, name, namelen, cred, hash)
308 1.1 rvb struct cnode *dcp;
309 1.1 rvb const char *name;
310 1.1 rvb int namelen;
311 1.1 rvb struct ucred *cred;
312 1.1 rvb int hash;
313 1.1 rvb {
314 1.1 rvb /*
315 1.1 rvb * hash to find the appropriate bucket, look through the chain
316 1.1 rvb * for the right entry (especially right cred, unless cred == 0)
317 1.1 rvb */
318 1.1 rvb struct cfscache *cncp;
319 1.1 rvb int count = 1;
320 1.1 rvb
321 1.1 rvb CFSNC_DEBUG(CFSNC_FIND,
322 1.1 rvb myprintf(("cfsnc_find(dcp %p, name %s, len %d, cred %p, hash %d\n",
323 1.1 rvb dcp, name, namelen, cred, hash));)
324 1.1 rvb
325 1.1 rvb for (cncp = cfsnchash[hash].hash_next;
326 1.1 rvb cncp != (struct cfscache *)&cfsnchash[hash];
327 1.1 rvb cncp = cncp->hash_next, count++)
328 1.1 rvb {
329 1.1 rvb
330 1.1 rvb if ((CFS_NAMEMATCH(cncp, name, namelen, dcp)) &&
331 1.1 rvb ((cred == 0) || (cncp->cred == cred)))
332 1.1 rvb {
333 1.1 rvb /* compare cr_uid instead */
334 1.1 rvb cfsnc_stat.Search_len += count;
335 1.1 rvb return(cncp);
336 1.1 rvb }
337 1.1 rvb #ifdef DEBUG
338 1.1 rvb else if (CFS_NAMEMATCH(cncp, name, namelen, dcp)) {
339 1.1 rvb printf("cfsnc_find: name %s, new cred = %p, cred = %p\n",
340 1.1 rvb name, cred, cncp->cred);
341 1.1 rvb printf("nref %d, nuid %d, ngid %d // oref %d, ocred %d, ogid %d\n",
342 1.1 rvb cred->cr_ref, cred->cr_uid, cred->cr_gid,
343 1.1 rvb cncp->cred->cr_ref, cncp->cred->cr_uid, cncp->cred->cr_gid);
344 1.1 rvb print_cred(cred);
345 1.1 rvb print_cred(cncp->cred);
346 1.1 rvb }
347 1.1 rvb #endif
348 1.1 rvb }
349 1.1 rvb
350 1.1 rvb return((struct cfscache *)0);
351 1.1 rvb }
352 1.1 rvb
353 1.1 rvb /*
354 1.1 rvb * Enter a new (dir cnode, name) pair into the cache, updating the
355 1.1 rvb * LRU and Hash as needed.
356 1.1 rvb */
357 1.1 rvb void
358 1.1 rvb cfsnc_enter(dcp, name, namelen, cred, cp)
359 1.1 rvb struct cnode *dcp;
360 1.1 rvb const char *name;
361 1.1 rvb int namelen;
362 1.1 rvb struct ucred *cred;
363 1.1 rvb struct cnode *cp;
364 1.1 rvb {
365 1.1 rvb struct cfscache *cncp;
366 1.1 rvb int hash;
367 1.1 rvb
368 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
369 1.1 rvb return;
370 1.1 rvb
371 1.1 rvb CFSNC_DEBUG(CFSNC_ENTER,
372 1.1 rvb myprintf(("Enter: dcp %p cp %p name %s cred %p \n",
373 1.1 rvb dcp, cp, name, cred)); )
374 1.1 rvb
375 1.1 rvb if (namelen > CFSNC_NAMELEN) {
376 1.1 rvb CFSNC_DEBUG(CFSNC_ENTER,
377 1.1 rvb myprintf(("long name enter %s\n",name));)
378 1.1 rvb cfsnc_stat.long_name_enters++; /* record stats */
379 1.1 rvb return;
380 1.1 rvb }
381 1.1 rvb
382 1.1 rvb hash = CFSNC_HASH(name, namelen, dcp);
383 1.1 rvb cncp = cfsnc_find(dcp, name, namelen, cred, hash);
384 1.1 rvb if (cncp != (struct cfscache *) 0) {
385 1.1 rvb cfsnc_stat.dbl_enters++; /* duplicate entry */
386 1.1 rvb return;
387 1.1 rvb }
388 1.1 rvb
389 1.1 rvb cfsnc_stat.enters++; /* record the enters statistic */
390 1.1 rvb
391 1.1 rvb /* Grab the next element in the lru chain */
392 1.1 rvb cncp = CFSNC_LRUGET(cfsnc_lru);
393 1.1 rvb
394 1.1 rvb CFSNC_LRUREM(cncp); /* remove it from the lists */
395 1.1 rvb
396 1.1 rvb if (CFSNC_VALID(cncp)) {
397 1.1 rvb /* Seems really ugly, but we have to decrement the appropriate
398 1.1 rvb hash bucket length here, so we have to find the hash bucket
399 1.1 rvb */
400 1.1 rvb cfsnchash[CFSNC_HASH(cncp->name, cncp->namelen, cncp->dcp)].length--;
401 1.1 rvb
402 1.1 rvb cfsnc_stat.lru_rm++; /* zapped a valid entry */
403 1.1 rvb CFSNC_HSHREM(cncp);
404 1.1 rvb vrele(CTOV(cncp->dcp));
405 1.1 rvb vrele(CTOV(cncp->cp));
406 1.1 rvb crfree(cncp->cred);
407 1.1 rvb }
408 1.1 rvb
409 1.1 rvb /*
410 1.1 rvb * Put a hold on the current vnodes and fill in the cache entry.
411 1.1 rvb */
412 1.1 rvb vref(CTOV(cp));
413 1.1 rvb vref(CTOV(dcp));
414 1.1 rvb crhold(cred);
415 1.1 rvb cncp->dcp = dcp;
416 1.1 rvb cncp->cp = cp;
417 1.1 rvb cncp->namelen = namelen;
418 1.1 rvb cncp->cred = cred;
419 1.1 rvb
420 1.1 rvb bcopy(name, cncp->name, (unsigned)namelen);
421 1.1 rvb
422 1.1 rvb /* Insert into the lru and hash chains. */
423 1.1 rvb
424 1.1 rvb CFSNC_LRUINS(cncp, &cfsnc_lru);
425 1.1 rvb CFSNC_HSHINS(cncp, &cfsnchash[hash]);
426 1.1 rvb cfsnchash[hash].length++; /* Used for tuning */
427 1.1 rvb
428 1.1 rvb CFSNC_DEBUG(CFSNC_PRINTCFSNC, print_cfsnc(); )
429 1.1 rvb }
430 1.1 rvb
431 1.1 rvb /*
432 1.1 rvb * Find the (dir cnode, name) pair in the cache, if it's cred
433 1.1 rvb * matches the input, return it, otherwise return 0
434 1.1 rvb */
435 1.1 rvb struct cnode *
436 1.1 rvb cfsnc_lookup(dcp, name, namelen, cred)
437 1.1 rvb struct cnode *dcp;
438 1.1 rvb const char *name;
439 1.1 rvb int namelen;
440 1.1 rvb struct ucred *cred;
441 1.1 rvb {
442 1.1 rvb int hash;
443 1.1 rvb struct cfscache *cncp;
444 1.1 rvb
445 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
446 1.1 rvb return((struct cnode *) 0);
447 1.1 rvb
448 1.1 rvb if (namelen > CFSNC_NAMELEN) {
449 1.1 rvb CFSNC_DEBUG(CFSNC_LOOKUP,
450 1.1 rvb myprintf(("long name lookup %s\n",name));)
451 1.1 rvb cfsnc_stat.long_name_lookups++; /* record stats */
452 1.1 rvb return((struct cnode *) 0);
453 1.1 rvb }
454 1.1 rvb
455 1.1 rvb /* Use the hash function to locate the starting point,
456 1.1 rvb then the search routine to go down the list looking for
457 1.1 rvb the correct cred.
458 1.1 rvb */
459 1.1 rvb
460 1.1 rvb hash = CFSNC_HASH(name, namelen, dcp);
461 1.1 rvb cncp = cfsnc_find(dcp, name, namelen, cred, hash);
462 1.1 rvb if (cncp == (struct cfscache *) 0) {
463 1.1 rvb cfsnc_stat.misses++; /* record miss */
464 1.1 rvb return((struct cnode *) 0);
465 1.1 rvb }
466 1.1 rvb
467 1.1 rvb cfsnc_stat.hits++;
468 1.1 rvb
469 1.1 rvb /* put this entry at the end of the LRU */
470 1.1 rvb CFSNC_LRUREM(cncp);
471 1.1 rvb CFSNC_LRUINS(cncp, &cfsnc_lru);
472 1.1 rvb
473 1.1 rvb /* move it to the front of the hash chain */
474 1.1 rvb /* don't need to change the hash bucket length */
475 1.1 rvb CFSNC_HSHREM(cncp);
476 1.1 rvb CFSNC_HSHINS(cncp, &cfsnchash[hash]);
477 1.1 rvb
478 1.1 rvb CFSNC_DEBUG(CFSNC_LOOKUP,
479 1.1 rvb printf("lookup: dcp %p, name %s, cred %p = cp %p\n",
480 1.1 rvb dcp, name, cred, cncp->cp); )
481 1.1 rvb
482 1.1 rvb return(cncp->cp);
483 1.1 rvb }
484 1.1 rvb
485 1.1 rvb static void
486 1.1 rvb cfsnc_remove(cncp, dcstat)
487 1.1 rvb struct cfscache *cncp;
488 1.1 rvb enum dc_status dcstat;
489 1.1 rvb {
490 1.1 rvb /*
491 1.1 rvb * remove an entry -- vrele(cncp->dcp, cp), crfree(cred),
492 1.1 rvb * remove it from it's hash chain, and
493 1.1 rvb * place it at the head of the lru list.
494 1.1 rvb */
495 1.1 rvb CFSNC_DEBUG(CFSNC_REMOVE,
496 1.1 rvb myprintf(("cfsnc_remove %s from parent %lx.%lx.%lx\n",
497 1.1 rvb cncp->name, (cncp->dcp)->c_fid.Volume,
498 1.1 rvb (cncp->dcp)->c_fid.Vnode, (cncp->dcp)->c_fid.Unique));)
499 1.1 rvb
500 1.1 rvb CFSNC_HSHREM(cncp);
501 1.1 rvb
502 1.1 rvb CFSNC_HSHNUL(cncp); /* have it be a null chain */
503 1.1 rvb if ((dcstat == IS_DOWNCALL) && (CTOV(cncp->dcp)->v_usecount == 1)) {
504 1.1 rvb cncp->dcp->c_flags |= C_PURGING;
505 1.1 rvb }
506 1.1 rvb vrele(CTOV(cncp->dcp));
507 1.1 rvb
508 1.1 rvb if ((dcstat == IS_DOWNCALL) && (CTOV(cncp->cp)->v_usecount == 1)) {
509 1.1 rvb cncp->cp->c_flags |= C_PURGING;
510 1.1 rvb }
511 1.1 rvb vrele(CTOV(cncp->cp));
512 1.1 rvb
513 1.1 rvb crfree(cncp->cred);
514 1.1 rvb bzero(DATA_PART(cncp),DATA_SIZE);
515 1.1 rvb
516 1.1 rvb /* Put the null entry just after the least-recently-used entry */
517 1.1 rvb /* LRU_TOP adjusts the pointer to point to the top of the structure. */
518 1.1 rvb CFSNC_LRUREM(cncp);
519 1.1 rvb CFSNC_LRUINS(cncp, LRU_TOP(cfsnc_lru.lru_prev));
520 1.1 rvb }
521 1.1 rvb
522 1.1 rvb /*
523 1.1 rvb * Remove all entries with a parent which has the input fid.
524 1.1 rvb */
525 1.1 rvb void
526 1.1 rvb cfsnc_zapParentfid(fid, dcstat)
527 1.1 rvb ViceFid *fid;
528 1.1 rvb enum dc_status dcstat;
529 1.1 rvb {
530 1.1 rvb /* To get to a specific fid, we might either have another hashing
531 1.1 rvb function or do a sequential search through the cache for the
532 1.1 rvb appropriate entries. The later may be acceptable since I don't
533 1.1 rvb think callbacks or whatever Case 1 covers are frequent occurences.
534 1.1 rvb */
535 1.1 rvb struct cfscache *cncp, *ncncp;
536 1.1 rvb int i;
537 1.1 rvb
538 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
539 1.1 rvb return;
540 1.1 rvb
541 1.1 rvb CFSNC_DEBUG(CFSNC_ZAPPFID,
542 1.1 rvb myprintf(("ZapParent: fid 0x%lx, 0x%lx, 0x%lx \n",
543 1.1 rvb fid->Volume, fid->Vnode, fid->Unique)); )
544 1.1 rvb
545 1.1 rvb cfsnc_stat.zapPfids++;
546 1.1 rvb
547 1.1 rvb for (i = 0; i < cfsnc_hashsize; i++) {
548 1.1 rvb
549 1.1 rvb /*
550 1.1 rvb * Need to save the hash_next pointer in case we remove the
551 1.1 rvb * entry. remove causes hash_next to point to itself.
552 1.1 rvb */
553 1.1 rvb
554 1.1 rvb for (cncp = cfsnchash[i].hash_next;
555 1.1 rvb cncp != (struct cfscache *)&cfsnchash[i];
556 1.1 rvb cncp = ncncp) {
557 1.1 rvb ncncp = cncp->hash_next;
558 1.1 rvb if ((cncp->dcp->c_fid.Volume == fid->Volume) &&
559 1.1 rvb (cncp->dcp->c_fid.Vnode == fid->Vnode) &&
560 1.1 rvb (cncp->dcp->c_fid.Unique == fid->Unique)) {
561 1.1 rvb cfsnchash[i].length--; /* Used for tuning */
562 1.1 rvb cfsnc_remove(cncp, dcstat);
563 1.1 rvb }
564 1.1 rvb }
565 1.1 rvb }
566 1.1 rvb }
567 1.1 rvb
568 1.1 rvb
569 1.1 rvb /*
570 1.1 rvb * Remove all entries which have the same fid as the input
571 1.1 rvb */
572 1.1 rvb void
573 1.1 rvb cfsnc_zapfid(fid, dcstat)
574 1.1 rvb ViceFid *fid;
575 1.1 rvb enum dc_status dcstat;
576 1.1 rvb {
577 1.1 rvb /* See comment for zapParentfid. This routine will be used
578 1.1 rvb if attributes are being cached.
579 1.1 rvb */
580 1.1 rvb struct cfscache *cncp, *ncncp;
581 1.1 rvb int i;
582 1.1 rvb
583 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
584 1.1 rvb return;
585 1.1 rvb
586 1.1 rvb CFSNC_DEBUG(CFSNC_ZAPFID,
587 1.1 rvb myprintf(("Zapfid: fid 0x%lx, 0x%lx, 0x%lx \n",
588 1.1 rvb fid->Volume, fid->Vnode, fid->Unique)); )
589 1.1 rvb
590 1.1 rvb cfsnc_stat.zapFids++;
591 1.1 rvb
592 1.1 rvb for (i = 0; i < cfsnc_hashsize; i++) {
593 1.1 rvb for (cncp = cfsnchash[i].hash_next;
594 1.1 rvb cncp != (struct cfscache *)&cfsnchash[i];
595 1.1 rvb cncp = ncncp) {
596 1.1 rvb ncncp = cncp->hash_next;
597 1.1 rvb if ((cncp->cp->c_fid.Volume == fid->Volume) &&
598 1.1 rvb (cncp->cp->c_fid.Vnode == fid->Vnode) &&
599 1.1 rvb (cncp->cp->c_fid.Unique == fid->Unique)) {
600 1.1 rvb cfsnchash[i].length--; /* Used for tuning */
601 1.1 rvb cfsnc_remove(cncp, dcstat);
602 1.1 rvb }
603 1.1 rvb }
604 1.1 rvb }
605 1.1 rvb }
606 1.1 rvb
607 1.1 rvb /*
608 1.1 rvb * Remove all entries which match the fid and the cred
609 1.1 rvb */
610 1.1 rvb void
611 1.1 rvb cfsnc_zapvnode(fid, cred, dcstat)
612 1.1 rvb ViceFid *fid;
613 1.1 rvb struct ucred *cred;
614 1.1 rvb enum dc_status dcstat;
615 1.1 rvb {
616 1.1 rvb /* See comment for zapfid. I don't think that one would ever
617 1.1 rvb want to zap a file with a specific cred from the kernel.
618 1.1 rvb We'll leave this one unimplemented.
619 1.1 rvb */
620 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
621 1.1 rvb return;
622 1.1 rvb
623 1.1 rvb CFSNC_DEBUG(CFSNC_ZAPVNODE,
624 1.1 rvb myprintf(("Zapvnode: fid 0x%lx, 0x%lx, 0x%lx cred %p\n",
625 1.1 rvb fid->Volume, fid->Vnode, fid->Unique, cred)); )
626 1.1 rvb
627 1.1 rvb }
628 1.1 rvb
629 1.1 rvb /*
630 1.1 rvb * Remove all entries which have the (dir vnode, name) pair
631 1.1 rvb */
632 1.1 rvb void
633 1.1 rvb cfsnc_zapfile(dcp, name, namelen)
634 1.1 rvb struct cnode *dcp;
635 1.1 rvb const char *name;
636 1.1 rvb int namelen;
637 1.1 rvb {
638 1.1 rvb /* use the hash function to locate the file, then zap all
639 1.1 rvb entries of it regardless of the cred.
640 1.1 rvb */
641 1.1 rvb struct cfscache *cncp;
642 1.1 rvb int hash;
643 1.1 rvb
644 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
645 1.1 rvb return;
646 1.1 rvb
647 1.1 rvb CFSNC_DEBUG(CFSNC_ZAPFILE,
648 1.1 rvb myprintf(("Zapfile: dcp %p name %s \n",
649 1.1 rvb dcp, name)); )
650 1.1 rvb
651 1.1 rvb if (namelen > CFSNC_NAMELEN) {
652 1.1 rvb cfsnc_stat.long_remove++; /* record stats */
653 1.1 rvb return;
654 1.1 rvb }
655 1.1 rvb
656 1.1 rvb cfsnc_stat.zapFile++;
657 1.1 rvb
658 1.1 rvb hash = CFSNC_HASH(name, namelen, dcp);
659 1.1 rvb cncp = cfsnc_find(dcp, name, namelen, 0, hash);
660 1.1 rvb
661 1.1 rvb while (cncp) {
662 1.1 rvb cfsnchash[hash].length--; /* Used for tuning */
663 1.1 rvb /* 1.3 */
664 1.1 rvb cfsnc_remove(cncp, NOT_DOWNCALL);
665 1.1 rvb cncp = cfsnc_find(dcp, name, namelen, 0, hash);
666 1.1 rvb }
667 1.1 rvb }
668 1.1 rvb
669 1.1 rvb /*
670 1.1 rvb * Remove all the entries for a particular user. Used when tokens expire.
671 1.1 rvb * A user is determined by his/her effective user id (id_uid).
672 1.1 rvb */
673 1.1 rvb void
674 1.1 rvb cfsnc_purge_user(uid, dcstat)
675 1.1 rvb vuid_t uid;
676 1.1 rvb enum dc_status dcstat;
677 1.1 rvb {
678 1.1 rvb /*
679 1.1 rvb * I think the best approach is to go through the entire cache
680 1.1 rvb * via HASH or whatever and zap all entries which match the
681 1.1 rvb * input cred. Or just flush the whole cache. It might be
682 1.1 rvb * best to go through on basis of LRU since cache will almost
683 1.1 rvb * always be full and LRU is more straightforward.
684 1.1 rvb */
685 1.1 rvb
686 1.1 rvb struct cfscache *cncp, *ncncp;
687 1.1 rvb int hash;
688 1.1 rvb
689 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
690 1.1 rvb return;
691 1.1 rvb
692 1.1 rvb CFSNC_DEBUG(CFSNC_PURGEUSER,
693 1.1 rvb myprintf(("ZapDude: uid %lx\n", uid)); )
694 1.1 rvb cfsnc_stat.zapUsers++;
695 1.1 rvb
696 1.1 rvb for (cncp = CFSNC_LRUGET(cfsnc_lru);
697 1.1 rvb cncp != (struct cfscache *)(&cfsnc_lru);
698 1.1 rvb cncp = ncncp) {
699 1.1 rvb ncncp = CFSNC_LRUGET(*cncp);
700 1.1 rvb
701 1.1 rvb if ((CFSNC_VALID(cncp)) &&
702 1.1 rvb ((cncp->cred)->cr_uid == uid)) {
703 1.1 rvb /* Seems really ugly, but we have to decrement the appropriate
704 1.1 rvb hash bucket length here, so we have to find the hash bucket
705 1.1 rvb */
706 1.1 rvb hash = CFSNC_HASH(cncp->name, cncp->namelen, cncp->dcp);
707 1.1 rvb cfsnchash[hash].length--; /* For performance tuning */
708 1.1 rvb
709 1.1 rvb cfsnc_remove(cncp, dcstat);
710 1.1 rvb }
711 1.1 rvb }
712 1.1 rvb }
713 1.1 rvb
714 1.1 rvb /*
715 1.1 rvb * Flush the entire name cache. In response to a flush of the Venus cache.
716 1.1 rvb */
717 1.1 rvb void
718 1.1 rvb cfsnc_flush(dcstat)
719 1.1 rvb enum dc_status dcstat;
720 1.1 rvb {
721 1.1 rvb /* One option is to deallocate the current name cache and
722 1.1 rvb call init to start again. Or just deallocate, then rebuild.
723 1.1 rvb Or again, we could just go through the array and zero the
724 1.1 rvb appropriate fields.
725 1.1 rvb */
726 1.1 rvb
727 1.1 rvb /*
728 1.1 rvb * Go through the whole lru chain and kill everything as we go.
729 1.1 rvb * I don't use remove since that would rebuild the lru chain
730 1.1 rvb * as it went and that seemed unneccesary.
731 1.1 rvb */
732 1.1 rvb struct cfscache *cncp;
733 1.1 rvb int i;
734 1.1 rvb
735 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
736 1.1 rvb return;
737 1.1 rvb
738 1.1 rvb cfsnc_stat.Flushes++;
739 1.1 rvb
740 1.1 rvb for (cncp = CFSNC_LRUGET(cfsnc_lru);
741 1.1 rvb cncp != (struct cfscache *)&cfsnc_lru;
742 1.1 rvb cncp = CFSNC_LRUGET(*cncp)) {
743 1.1 rvb if (CFSNC_VALID(cncp)) {
744 1.1 rvb
745 1.1 rvb CFSNC_HSHREM(cncp); /* only zero valid nodes */
746 1.1 rvb CFSNC_HSHNUL(cncp);
747 1.1 rvb if ((dcstat == IS_DOWNCALL)
748 1.1 rvb && (CTOV(cncp->dcp)->v_usecount == 1))
749 1.1 rvb {
750 1.1 rvb cncp->dcp->c_flags |= C_PURGING;
751 1.1 rvb }
752 1.1 rvb vrele(CTOV(cncp->dcp));
753 1.1 rvb
754 1.1 rvb if (CTOV(cncp->cp)->v_flag & VTEXT) {
755 1.1 rvb if (cfs_vmflush(cncp->cp))
756 1.1 rvb CFSDEBUG(CFS_FLUSH,
757 1.1 rvb myprintf(("cfsnc_flush: (%lx.%lx.%lx) busy\n", cncp->cp->c_fid.Volume, cncp->cp->c_fid.Vnode, cncp->cp->c_fid.Unique)); )
758 1.1 rvb }
759 1.1 rvb
760 1.1 rvb if ((dcstat == IS_DOWNCALL)
761 1.1 rvb && (CTOV(cncp->cp)->v_usecount == 1))
762 1.1 rvb {
763 1.1 rvb cncp->cp->c_flags |= C_PURGING;
764 1.1 rvb }
765 1.1 rvb vrele(CTOV(cncp->cp));
766 1.1 rvb
767 1.1 rvb crfree(cncp->cred);
768 1.1 rvb bzero(DATA_PART(cncp),DATA_SIZE);
769 1.1 rvb }
770 1.1 rvb }
771 1.1 rvb
772 1.1 rvb for (i = 0; i < cfsnc_hashsize; i++)
773 1.1 rvb cfsnchash[i].length = 0;
774 1.1 rvb }
775 1.1 rvb
776 1.1 rvb /*
777 1.1 rvb * Debugging routines
778 1.1 rvb */
779 1.1 rvb
780 1.1 rvb /*
781 1.1 rvb * This routine should print out all the hash chains to the console.
782 1.1 rvb */
783 1.1 rvb void
784 1.1 rvb print_cfsnc(void)
785 1.1 rvb {
786 1.1 rvb int hash;
787 1.1 rvb struct cfscache *cncp;
788 1.1 rvb
789 1.1 rvb for (hash = 0; hash < cfsnc_hashsize; hash++) {
790 1.1 rvb myprintf(("\nhash %d\n",hash));
791 1.1 rvb
792 1.1 rvb for (cncp = cfsnchash[hash].hash_next;
793 1.1 rvb cncp != (struct cfscache *)&cfsnchash[hash];
794 1.1 rvb cncp = cncp->hash_next) {
795 1.1 rvb myprintf(("cp %p dcp %p cred %p name %s\n",
796 1.1 rvb cncp->cp, cncp->dcp,
797 1.1 rvb cncp->cred, cncp->name));
798 1.1 rvb }
799 1.1 rvb }
800 1.1 rvb }
801 1.1 rvb
802 1.1 rvb void
803 1.1 rvb cfsnc_gather_stats(void)
804 1.1 rvb {
805 1.1 rvb int i, max = 0, sum = 0, temp, zeros = 0, ave, n;
806 1.1 rvb
807 1.1 rvb for (i = 0; i < cfsnc_hashsize; i++) {
808 1.1 rvb if (cfsnchash[i].length) {
809 1.1 rvb sum += cfsnchash[i].length;
810 1.1 rvb } else {
811 1.1 rvb zeros++;
812 1.1 rvb }
813 1.1 rvb
814 1.1 rvb if (cfsnchash[i].length > max)
815 1.1 rvb max = cfsnchash[i].length;
816 1.1 rvb }
817 1.1 rvb
818 1.1 rvb /*
819 1.1 rvb * When computing the Arithmetic mean, only count slots which
820 1.1 rvb * are not empty in the distribution.
821 1.1 rvb */
822 1.1 rvb cfsnc_stat.Sum_bucket_len = sum;
823 1.1 rvb cfsnc_stat.Num_zero_len = zeros;
824 1.1 rvb cfsnc_stat.Max_bucket_len = max;
825 1.1 rvb
826 1.1 rvb if ((n = cfsnc_hashsize - zeros) > 0)
827 1.1 rvb ave = sum / n;
828 1.1 rvb else
829 1.1 rvb ave = 0;
830 1.1 rvb
831 1.1 rvb sum = 0;
832 1.1 rvb for (i = 0; i < cfsnc_hashsize; i++) {
833 1.1 rvb if (cfsnchash[i].length) {
834 1.1 rvb temp = cfsnchash[i].length - ave;
835 1.1 rvb sum += temp * temp;
836 1.1 rvb }
837 1.1 rvb }
838 1.1 rvb cfsnc_stat.Sum2_bucket_len = sum;
839 1.1 rvb }
840 1.1 rvb
841 1.1 rvb /*
842 1.1 rvb * The purpose of this routine is to allow the hash and cache sizes to be
843 1.1 rvb * changed dynamically. This should only be used in controlled environments,
844 1.1 rvb * it makes no effort to lock other users from accessing the cache while it
845 1.1 rvb * is in an improper state (except by turning the cache off).
846 1.1 rvb */
847 1.1 rvb int
848 1.1 rvb cfsnc_resize(hashsize, heapsize, dcstat)
849 1.1 rvb int hashsize, heapsize;
850 1.1 rvb enum dc_status dcstat;
851 1.1 rvb {
852 1.1 rvb if ((hashsize % 2) || (heapsize % 2)) { /* Illegal hash or cache sizes */
853 1.1 rvb return(EINVAL);
854 1.1 rvb }
855 1.1 rvb
856 1.1 rvb cfsnc_use = 0; /* Turn the cache off */
857 1.1 rvb
858 1.1 rvb cfsnc_flush(dcstat); /* free any cnodes in the cache */
859 1.1 rvb
860 1.1 rvb /* WARNING: free must happen *before* size is reset */
861 1.1 rvb CFS_FREE(cfsncheap,TOTAL_CACHE_SIZE);
862 1.1 rvb CFS_FREE(cfsnchash,TOTAL_HASH_SIZE);
863 1.1 rvb
864 1.1 rvb cfsnc_hashsize = hashsize;
865 1.1 rvb cfsnc_size = heapsize;
866 1.1 rvb
867 1.1 rvb cfsnc_init(); /* Set up a cache with the new size */
868 1.1 rvb
869 1.1 rvb cfsnc_use = 1; /* Turn the cache back on */
870 1.1 rvb return(0);
871 1.1 rvb }
872 1.1 rvb
873 1.1 rvb #define DEBUG
874 1.1 rvb #ifdef DEBUG
875 1.1 rvb char cfsnc_name_buf[CFS_MAXNAMLEN+1];
876 1.1 rvb
877 1.1 rvb void
878 1.1 rvb cfsnc_name(struct cnode *cp)
879 1.1 rvb {
880 1.1 rvb struct cfscache *cncp, *ncncp;
881 1.1 rvb int i;
882 1.1 rvb
883 1.1 rvb if (cfsnc_use == 0) /* Cache is off */
884 1.1 rvb return;
885 1.1 rvb
886 1.1 rvb for (i = 0; i < cfsnc_hashsize; i++) {
887 1.1 rvb for (cncp = cfsnchash[i].hash_next;
888 1.1 rvb cncp != (struct cfscache *)&cfsnchash[i];
889 1.1 rvb cncp = ncncp) {
890 1.1 rvb ncncp = cncp->hash_next;
891 1.1 rvb if (cncp->cp == cp) {
892 1.1 rvb bcopy(cncp->name, cfsnc_name_buf, cncp->namelen);
893 1.1 rvb cfsnc_name_buf[cncp->namelen] = 0;
894 1.1 rvb printf(" is %s (%p,%p)@%p",
895 1.1 rvb cfsnc_name_buf, cncp->cp, cncp->dcp, cncp);
896 1.1 rvb }
897 1.1 rvb
898 1.1 rvb }
899 }
900 }
901 #endif
902