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 write locked.
71 long cm_FindACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 *rightsp)
75 time_t now = time(NULL);
77 lock_AssertWrite(&scp->rw);
78 lock_ObtainWrite(&cm_aclLock);
79 *rightsp = 0; /* get a new acl from server if we don't find a
83 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
84 if (aclp->userp == userp) {
85 if (aclp->tgtLifetime && aclp->tgtLifetime <= now) {
87 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
90 /* move to the tail of the LRU queue */
91 osi_QAddT((osi_queue_t **) &cm_data.aclLRUp,
92 (osi_queue_t **) &cm_data.aclLRUEndp,
95 *rightsp = aclp->randomAccess;
96 if (cm_data.aclLRUp != aclp) {
97 /* move to the head of the LRU queue */
98 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
99 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
100 (osi_queue_t **) &cm_data.aclLRUEndp,
103 retval = 0; /* success */
109 lock_ReleaseWrite(&cm_aclLock);
114 * This function returns a free (not in the LRU queue) acl cache entry.
115 * It must be called with the cm_aclLock lock held
117 static cm_aclent_t *GetFreeACLEnt(cm_scache_t * scp)
120 cm_scache_t *ascp = 0;
122 if (cm_data.aclLRUp == NULL)
123 osi_panic("empty aclent LRU", __FILE__, __LINE__);
125 if (cm_data.aclLRUEndp == NULL)
126 osi_panic("inconsistent aclent LRUEndp == NULL", __FILE__, __LINE__);
128 aclp = cm_data.aclLRUEndp;
129 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
131 if (aclp->backp && scp != aclp->backp) {
133 lock_ReleaseWrite(&cm_aclLock);
134 lock_ObtainWrite(&ascp->rw);
135 lock_ObtainWrite(&cm_aclLock);
140 lock_ReleaseWrite(&ascp->rw);
144 time_t cm_TGTLifeTime(cm_user_t *userp, afs_uint32 cellID)
146 cm_cell_t *cellp = NULL;
147 cm_ucell_t * ucp = NULL;
148 time_t expirationTime = 0;
150 lock_ObtainMutex(&userp->mx);
151 cellp = cm_FindCellByID(cellID, CM_FLAG_NOPROBE);
152 ucp = cm_GetUCell(userp, cellp);
154 expirationTime = ucp->expirationTime;
155 lock_ReleaseMutex(&userp->mx);
157 return expirationTime;
161 cm_HaveToken(cm_user_t *userp, afs_uint32 cellID)
163 cm_cell_t *cellp = NULL;
164 cm_ucell_t * ucp = NULL;
168 lock_ObtainMutex(&userp->mx);
169 cellp = cm_FindCellByID(cellID, CM_FLAG_NOPROBE);
170 ucp = cm_GetUCell(userp, cellp);
173 if (ucp->expirationTime > now)
176 lock_ReleaseMutex(&userp->mx);
183 * Add rights to an acl cache entry. Do the right thing if not present,
184 * including digging up an entry from the LRU queue.
186 * The scp must be locked when this function is called.
188 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 rights)
190 struct cm_aclent *aclp;
193 tgtLifeTime = cm_TGTLifeTime(userp, scp->fid.cell);
195 lock_ObtainWrite(&cm_aclLock);
196 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
197 if (aclp->userp == userp) {
198 aclp->randomAccess = rights;
199 if (aclp->tgtLifetime < tgtLifeTime)
200 aclp->tgtLifetime = tgtLifeTime;
201 if (cm_data.aclLRUp != aclp) {
202 /* move to the head of the LRU queue */
203 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
204 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
205 (osi_queue_t **) &cm_data.aclLRUEndp,
208 lock_ReleaseWrite(&cm_aclLock);
214 * Didn't find the dude we're looking for, so take someone from the LRUQ
215 * and reuse. But first try the free list and see if there's already
218 aclp = GetFreeACLEnt(scp); /* can't fail, panics instead */
219 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
221 aclp->nextp = scp->randomACLp;
222 scp->randomACLp = aclp;
225 aclp->randomAccess = rights;
226 aclp->tgtLifetime = tgtLifeTime;
227 lock_ReleaseWrite(&cm_aclLock);
232 long cm_ShutdownACLCache(void)
237 long cm_ValidateACLCache(void)
239 long size = cm_data.stats * 2;
243 if ( cm_data.aclLRUp == NULL && cm_data.aclLRUEndp != NULL ||
244 cm_data.aclLRUp != NULL && cm_data.aclLRUEndp == NULL) {
245 afsi_log("cm_ValidateACLCache failure: inconsistent LRU pointers");
246 fprintf(stderr, "cm_ValidateACLCache failure: inconsistent LRU pointers\n");
250 for ( aclp = cm_data.aclLRUp, count = 0; aclp;
251 aclp = (cm_aclent_t *) osi_QNext(&aclp->q), count++ ) {
252 if (aclp->magic != CM_ACLENT_MAGIC) {
253 afsi_log("cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC");
254 fprintf(stderr, "cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC\n");
257 if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
258 afsi_log("cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC");
259 fprintf(stderr,"cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC\n");
262 if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
263 afsi_log("cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC");
264 fprintf(stderr,"cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC\n");
267 if (count != 0 && aclp == cm_data.aclLRUp || count > size) {
268 afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUp list");
269 fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUp list\n");
274 for ( aclp = cm_data.aclLRUEndp, count = 0; aclp;
275 aclp = (cm_aclent_t *) osi_QPrev(&aclp->q), count++ ) {
276 if (aclp->magic != CM_ACLENT_MAGIC) {
277 afsi_log("cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC");
278 fprintf(stderr, "cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC\n");
281 if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
282 afsi_log("cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC");
283 fprintf(stderr, "cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC\n");
286 if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
287 afsi_log("cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC");
288 fprintf(stderr, "cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC\n");
292 if (count != 0 && aclp == cm_data.aclLRUEndp || count > size) {
293 afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list");
294 fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list\n");
303 * Initialize the cache to have an entries. Called during system startup.
305 long cm_InitACLCache(int newFile, long size)
309 static osi_once_t once;
311 if (osi_Once(&once)) {
312 lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL);
316 lock_ObtainWrite(&cm_aclLock);
318 cm_data.aclLRUp = cm_data.aclLRUEndp = NULL;
319 aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
320 memset(aclp, 0, size * sizeof(cm_aclent_t));
323 * Put all of these guys on the LRU queue
325 for (i = 0; i < size; i++) {
326 aclp->magic = CM_ACLENT_MAGIC;
327 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
331 aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
332 for (i = 0; i < size; i++) {
334 aclp->tgtLifetime = 0;
338 lock_ReleaseWrite(&cm_aclLock);
344 * Free all associated acl entries. We actually just clear the back pointer
345 * since the acl entries are already in the free list. The scp must be locked
346 * or completely unreferenced (such as when called while recycling the scp).
348 void cm_FreeAllACLEnts(cm_scache_t *scp)
353 lock_ObtainWrite(&cm_aclLock);
354 for (aclp = scp->randomACLp; aclp; aclp = taclp) {
357 cm_ReleaseUser(aclp->userp);
360 aclp->backp = (struct cm_scache *) 0;
363 scp->randomACLp = (struct cm_aclent *) 0;
364 scp->anyAccess = 0; /* reset this, too */
365 lock_ReleaseWrite(&cm_aclLock);
370 * Invalidate all ACL entries for particular user on this particular vnode.
372 * The scp must not be locked.
374 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
377 cm_aclent_t **laclpp;
381 lock_ObtainWrite(&scp->rw);
382 lock_ObtainWrite(&cm_aclLock);
383 laclpp = &scp->randomACLp;
384 for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
385 if (userp == aclp->userp) { /* One for a given user/scache */
386 *laclpp = aclp->nextp;
387 cm_ReleaseUser(aclp->userp);
389 aclp->backp = (struct cm_scache *) 0;
394 lock_ReleaseWrite(&cm_aclLock);
396 callback = cm_HaveCallback(scp);
397 lock_ReleaseWrite(&scp->rw);
399 if (found && callback && RDR_Initialized)
400 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
401 scp->fid.hash, scp->fileType, AFS_INVALIDATE_CREDS);
405 * Invalidate ACL info for a user that has just obtained or lost tokens.
408 cm_ResetACLCache(cm_cell_t *cellp, cm_user_t *userp)
410 cm_volume_t *volp, *nextVolp;
411 cm_scache_t *scp, *nextScp;
414 lock_ObtainRead(&cm_scacheLock);
415 for (hash=0; hash < cm_data.scacheHashTableSize; hash++) {
416 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=nextScp) {
417 nextScp = scp->nextp;
419 scp->fid.cell == cellp->cellID) {
420 cm_HoldSCacheNoLock(scp);
421 lock_ReleaseRead(&cm_scacheLock);
422 cm_InvalidateACLUser(scp, userp);
423 lock_ObtainRead(&cm_scacheLock);
424 cm_ReleaseSCacheNoLock(scp);
428 lock_ReleaseRead(&cm_scacheLock);
430 cm_EAccesClearUserEntries(userp, cellp ? cellp->cellID : 0);
432 if (RDR_Initialized) {
433 lock_ObtainRead(&cm_volumeLock);
434 for (hash = 0; hash < cm_data.volumeHashTableSize; hash++) {
435 for ( volp = cm_data.volumeRWIDHashTablep[hash]; volp; volp = nextVolp) {
436 nextVolp = volp->vol[RWVOL].nextp;
437 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
438 volp->vol[RWVOL].ID) {
439 lock_ReleaseRead(&cm_volumeLock);
440 RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[RWVOL].ID, AFS_INVALIDATE_CREDS);
441 lock_ObtainRead(&cm_volumeLock);
444 for ( volp = cm_data.volumeROIDHashTablep[hash]; volp; volp = nextVolp) {
445 nextVolp = volp->vol[ROVOL].nextp;
446 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
447 volp->vol[ROVOL].ID) {
448 lock_ReleaseRead(&cm_volumeLock);
449 RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[ROVOL].ID, AFS_INVALIDATE_CREDS);
450 lock_ObtainRead(&cm_volumeLock);
453 for ( volp = cm_data.volumeBKIDHashTablep[hash]; volp; volp = nextVolp) {
454 nextVolp = volp->vol[BACKVOL].nextp;
455 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
456 volp->vol[BACKVOL].ID) {
457 lock_ReleaseRead(&cm_volumeLock);
458 RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[BACKVOL].ID, AFS_INVALIDATE_CREDS);
459 lock_ObtainRead(&cm_volumeLock);
463 lock_ReleaseRead(&cm_volumeLock);