2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
24 * This next lock controls access to all cm_aclent structures in the system,
25 * in either the free list or in the LRU queue. A read lock prevents someone
26 * from modifying the list(s), and a write lock is required for modifying
27 * the list. The actual data stored in the randomUid and randomAccess fields
28 * is actually maintained as up-to-date or not via the scache lock.
29 * An aclent structure is free if it has no back vnode pointer.
31 osi_rwlock_t cm_aclLock; /* lock for system's aclents */
33 /* This must be called with cm_aclLock and the aclp->back->mx held */
34 static void CleanupACLEnt(cm_aclent_t * aclp)
40 if (aclp->backp->randomACLp) {
42 * Remove the entry from the vnode's list
44 lock_AssertWrite(&aclp->backp->rw);
45 laclpp = &aclp->backp->randomACLp;
46 for (taclp = *laclpp; taclp; laclpp = &taclp->nextp, taclp = *laclpp) {
51 osi_panic("CleanupACLEnt race", __FILE__, __LINE__);
52 *laclpp = aclp->nextp; /* remove from vnode list */
57 /* release the old user */
59 cm_ReleaseUser(aclp->userp);
63 aclp->randomAccess = 0;
64 aclp->tgtLifetime = 0;
68 * Get an acl cache entry for a particular user and file, or return that it doesn't exist.
69 * Called with the scp locked.
71 long cm_FindACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 *rightsp)
76 lock_ObtainWrite(&cm_aclLock);
77 *rightsp = 0; /* get a new acl from server if we don't find a
81 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
82 if (aclp->userp == userp) {
83 if (aclp->tgtLifetime && aclp->tgtLifetime <= time(NULL)) {
85 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
88 /* move to the tail of the LRU queue */
89 osi_QAddT((osi_queue_t **) &cm_data.aclLRUp,
90 (osi_queue_t **) &cm_data.aclLRUEndp,
93 *rightsp = aclp->randomAccess;
94 if (cm_data.aclLRUp != aclp) {
95 /* move to the head of the LRU queue */
96 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
97 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
98 (osi_queue_t **) &cm_data.aclLRUEndp,
101 retval = 0; /* success */
107 lock_ReleaseWrite(&cm_aclLock);
112 * This function returns a free (not in the LRU queue) acl cache entry.
113 * It must be called with the cm_aclLock lock held
115 static cm_aclent_t *GetFreeACLEnt(cm_scache_t * scp)
118 cm_scache_t *ascp = 0;
120 if (cm_data.aclLRUp == NULL)
121 osi_panic("empty aclent LRU", __FILE__, __LINE__);
123 if (cm_data.aclLRUEndp == NULL)
124 osi_panic("inconsistent aclent LRUEndp == NULL", __FILE__, __LINE__);
126 aclp = cm_data.aclLRUEndp;
127 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
129 if (aclp->backp && scp != aclp->backp) {
131 lock_ReleaseWrite(&cm_aclLock);
132 lock_ObtainWrite(&ascp->rw);
133 lock_ObtainWrite(&cm_aclLock);
138 lock_ReleaseWrite(&ascp->rw);
144 * Add rights to an acl cache entry. Do the right thing if not present,
145 * including digging up an entry from the LRU queue.
147 * The scp must be locked when this function is called.
149 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 rights)
151 struct cm_aclent *aclp;
153 lock_ObtainWrite(&cm_aclLock);
154 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
155 if (aclp->userp == userp) {
156 aclp->randomAccess = rights;
157 if (aclp->tgtLifetime == 0)
158 aclp->tgtLifetime = cm_TGTLifeTime(pag);
159 lock_ReleaseWrite(&cm_aclLock);
165 * Didn't find the dude we're looking for, so take someone from the LRUQ
166 * and reuse. But first try the free list and see if there's already
169 aclp = GetFreeACLEnt(scp); /* can't fail, panics instead */
170 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
172 aclp->nextp = scp->randomACLp;
173 scp->randomACLp = aclp;
176 aclp->randomAccess = rights;
177 aclp->tgtLifetime = cm_TGTLifeTime(userp);
178 lock_ReleaseWrite(&cm_aclLock);
183 long cm_ShutdownACLCache(void)
188 long cm_ValidateACLCache(void)
190 long size = cm_data.stats * 2;
194 if ( cm_data.aclLRUp == NULL && cm_data.aclLRUEndp != NULL ||
195 cm_data.aclLRUp != NULL && cm_data.aclLRUEndp == NULL) {
196 afsi_log("cm_ValidateACLCache failure: inconsistent LRU pointers");
197 fprintf(stderr, "cm_ValidateACLCache failure: inconsistent LRU pointers\n");
201 for ( aclp = cm_data.aclLRUp, count = 0; aclp;
202 aclp = (cm_aclent_t *) osi_QNext(&aclp->q), count++ ) {
203 if (aclp->magic != CM_ACLENT_MAGIC) {
204 afsi_log("cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC");
205 fprintf(stderr, "cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC\n");
208 if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
209 afsi_log("cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC");
210 fprintf(stderr,"cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC\n");
213 if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
214 afsi_log("cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC");
215 fprintf(stderr,"cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC\n");
218 if (count != 0 && aclp == cm_data.aclLRUp || count > size) {
219 afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUp list");
220 fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUp list\n");
225 for ( aclp = cm_data.aclLRUEndp, count = 0; aclp;
226 aclp = (cm_aclent_t *) osi_QPrev(&aclp->q), count++ ) {
227 if (aclp->magic != CM_ACLENT_MAGIC) {
228 afsi_log("cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC");
229 fprintf(stderr, "cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC\n");
232 if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
233 afsi_log("cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC");
234 fprintf(stderr, "cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC\n");
237 if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
238 afsi_log("cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC");
239 fprintf(stderr, "cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC\n");
243 if (count != 0 && aclp == cm_data.aclLRUEndp || count > size) {
244 afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list");
245 fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list\n");
254 * Initialize the cache to have an entries. Called during system startup.
256 long cm_InitACLCache(int newFile, long size)
260 static osi_once_t once;
262 if (osi_Once(&once)) {
263 lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL);
267 lock_ObtainWrite(&cm_aclLock);
269 cm_data.aclLRUp = cm_data.aclLRUEndp = NULL;
270 aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
271 memset(aclp, 0, size * sizeof(cm_aclent_t));
274 * Put all of these guys on the LRU queue
276 for (i = 0; i < size; i++) {
277 aclp->magic = CM_ACLENT_MAGIC;
278 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
282 aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
283 for (i = 0; i < size; i++) {
285 aclp->tgtLifetime = 0;
289 lock_ReleaseWrite(&cm_aclLock);
295 * Free all associated acl entries. We actually just clear the back pointer
296 * since the acl entries are already in the free list. The scp must be locked
297 * or completely unreferenced (such as when called while recycling the scp).
299 void cm_FreeAllACLEnts(cm_scache_t *scp)
304 lock_ObtainWrite(&cm_aclLock);
305 for (aclp = scp->randomACLp; aclp; aclp = taclp) {
308 cm_ReleaseUser(aclp->userp);
311 aclp->backp = (struct cm_scache *) 0;
314 scp->randomACLp = (struct cm_aclent *) 0;
315 scp->anyAccess = 0; /* reset this, too */
316 lock_ReleaseWrite(&cm_aclLock);
321 * Invalidate all ACL entries for particular user on this particular vnode.
323 * The scp must not be locked.
325 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
328 cm_aclent_t **laclpp;
331 lock_ObtainWrite(&scp->rw);
332 lock_ObtainWrite(&cm_aclLock);
333 laclpp = &scp->randomACLp;
334 for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
335 if (userp == aclp->userp) { /* One for a given user/scache */
336 *laclpp = aclp->nextp;
337 cm_ReleaseUser(aclp->userp);
339 aclp->backp = (struct cm_scache *) 0;
344 lock_ReleaseWrite(&cm_aclLock);
345 lock_ReleaseWrite(&scp->rw);
349 * Invalidate ACL info for a user that has just obtained or lost tokens.
352 cm_ResetACLCache(cm_cell_t *cellp, cm_user_t *userp)
354 cm_scache_t *scp, *nextScp;
357 lock_ObtainRead(&cm_scacheLock);
358 for (hash=0; hash < cm_data.scacheHashTableSize; hash++) {
359 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=nextScp) {
360 nextScp = scp->nextp;
362 scp->fid.cell == cellp->cellID) {
363 cm_HoldSCacheNoLock(scp);
364 lock_ReleaseRead(&cm_scacheLock);
365 cm_InvalidateACLUser(scp, userp);
366 lock_ObtainRead(&cm_scacheLock);
367 cm_ReleaseSCacheNoLock(scp);
371 lock_ReleaseRead(&cm_scacheLock);