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);
142 time_t cm_TGTLifeTime(cm_user_t *userp, afs_uint32 cellID)
144 cm_cell_t *cellp = NULL;
145 cm_ucell_t * ucp = NULL;
146 time_t expirationTime = 0;
148 cellp = cm_FindCellByID(cellID, CM_FLAG_NOPROBE);
149 lock_ObtainMutex(&userp->mx);
150 ucp = cm_GetUCell(userp, cellp);
152 expirationTime = ucp->expirationTime;
153 lock_ReleaseMutex(&userp->mx);
155 return expirationTime;
160 * Add rights to an acl cache entry. Do the right thing if not present,
161 * including digging up an entry from the LRU queue.
163 * The scp must be locked when this function is called.
165 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 rights)
167 struct cm_aclent *aclp;
169 lock_ObtainWrite(&cm_aclLock);
170 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
171 if (aclp->userp == userp) {
172 aclp->randomAccess = rights;
173 if (aclp->tgtLifetime == 0)
174 aclp->tgtLifetime = cm_TGTLifeTime(userp, scp->fid.cell);
175 if (cm_data.aclLRUp != aclp) {
176 /* move to the head of the LRU queue */
177 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
178 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
179 (osi_queue_t **) &cm_data.aclLRUEndp,
182 lock_ReleaseWrite(&cm_aclLock);
188 * Didn't find the dude we're looking for, so take someone from the LRUQ
189 * and reuse. But first try the free list and see if there's already
192 aclp = GetFreeACLEnt(scp); /* can't fail, panics instead */
193 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
195 aclp->nextp = scp->randomACLp;
196 scp->randomACLp = aclp;
199 aclp->randomAccess = rights;
200 aclp->tgtLifetime = cm_TGTLifeTime(userp, scp->fid.cell);
201 lock_ReleaseWrite(&cm_aclLock);
206 long cm_ShutdownACLCache(void)
211 long cm_ValidateACLCache(void)
213 long size = cm_data.stats * 2;
217 if ( cm_data.aclLRUp == NULL && cm_data.aclLRUEndp != NULL ||
218 cm_data.aclLRUp != NULL && cm_data.aclLRUEndp == NULL) {
219 afsi_log("cm_ValidateACLCache failure: inconsistent LRU pointers");
220 fprintf(stderr, "cm_ValidateACLCache failure: inconsistent LRU pointers\n");
224 for ( aclp = cm_data.aclLRUp, count = 0; aclp;
225 aclp = (cm_aclent_t *) osi_QNext(&aclp->q), count++ ) {
226 if (aclp->magic != CM_ACLENT_MAGIC) {
227 afsi_log("cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC");
228 fprintf(stderr, "cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC\n");
231 if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
232 afsi_log("cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC");
233 fprintf(stderr,"cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC\n");
236 if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
237 afsi_log("cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC");
238 fprintf(stderr,"cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC\n");
241 if (count != 0 && aclp == cm_data.aclLRUp || count > size) {
242 afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUp list");
243 fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUp list\n");
248 for ( aclp = cm_data.aclLRUEndp, count = 0; aclp;
249 aclp = (cm_aclent_t *) osi_QPrev(&aclp->q), count++ ) {
250 if (aclp->magic != CM_ACLENT_MAGIC) {
251 afsi_log("cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC");
252 fprintf(stderr, "cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC\n");
255 if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
256 afsi_log("cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC");
257 fprintf(stderr, "cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC\n");
260 if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
261 afsi_log("cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC");
262 fprintf(stderr, "cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC\n");
266 if (count != 0 && aclp == cm_data.aclLRUEndp || count > size) {
267 afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list");
268 fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list\n");
277 * Initialize the cache to have an entries. Called during system startup.
279 long cm_InitACLCache(int newFile, long size)
283 static osi_once_t once;
285 if (osi_Once(&once)) {
286 lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL);
290 lock_ObtainWrite(&cm_aclLock);
292 cm_data.aclLRUp = cm_data.aclLRUEndp = NULL;
293 aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
294 memset(aclp, 0, size * sizeof(cm_aclent_t));
297 * Put all of these guys on the LRU queue
299 for (i = 0; i < size; i++) {
300 aclp->magic = CM_ACLENT_MAGIC;
301 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
305 aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
306 for (i = 0; i < size; i++) {
308 aclp->tgtLifetime = 0;
312 lock_ReleaseWrite(&cm_aclLock);
318 * Free all associated acl entries. We actually just clear the back pointer
319 * since the acl entries are already in the free list. The scp must be locked
320 * or completely unreferenced (such as when called while recycling the scp).
322 void cm_FreeAllACLEnts(cm_scache_t *scp)
327 lock_ObtainWrite(&cm_aclLock);
328 for (aclp = scp->randomACLp; aclp; aclp = taclp) {
331 cm_ReleaseUser(aclp->userp);
334 aclp->backp = (struct cm_scache *) 0;
337 scp->randomACLp = (struct cm_aclent *) 0;
338 scp->anyAccess = 0; /* reset this, too */
339 lock_ReleaseWrite(&cm_aclLock);
344 * Invalidate all ACL entries for particular user on this particular vnode.
346 * The scp must not be locked.
348 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
351 cm_aclent_t **laclpp;
355 lock_ObtainWrite(&scp->rw);
356 lock_ObtainWrite(&cm_aclLock);
357 laclpp = &scp->randomACLp;
358 for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
359 if (userp == aclp->userp) { /* One for a given user/scache */
360 *laclpp = aclp->nextp;
361 cm_ReleaseUser(aclp->userp);
363 aclp->backp = (struct cm_scache *) 0;
368 lock_ReleaseWrite(&cm_aclLock);
370 callback = cm_HaveCallback(scp);
371 lock_ReleaseWrite(&scp->rw);
373 if (found && callback && RDR_Initialized)
374 RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
375 scp->fid.hash, scp->fileType, AFS_INVALIDATE_CREDS);
379 * Invalidate ACL info for a user that has just obtained or lost tokens.
382 cm_ResetACLCache(cm_cell_t *cellp, cm_user_t *userp)
384 cm_volume_t *volp, *nextVolp;
385 cm_scache_t *scp, *nextScp;
388 lock_ObtainRead(&cm_scacheLock);
389 for (hash=0; hash < cm_data.scacheHashTableSize; hash++) {
390 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=nextScp) {
391 nextScp = scp->nextp;
393 scp->fid.cell == cellp->cellID) {
394 cm_HoldSCacheNoLock(scp);
395 lock_ReleaseRead(&cm_scacheLock);
396 cm_InvalidateACLUser(scp, userp);
397 lock_ObtainRead(&cm_scacheLock);
398 cm_ReleaseSCacheNoLock(scp);
402 lock_ReleaseRead(&cm_scacheLock);
404 if (RDR_Initialized) {
405 lock_ObtainRead(&cm_volumeLock);
406 for (hash = 0; hash < cm_data.volumeHashTableSize; hash++) {
407 for ( volp = cm_data.volumeRWIDHashTablep[hash]; volp; volp = nextVolp) {
408 nextVolp = volp->vol[RWVOL].nextp;
409 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
410 volp->vol[RWVOL].ID) {
411 lock_ReleaseRead(&cm_volumeLock);
412 RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[RWVOL].ID, AFS_INVALIDATE_CREDS);
413 lock_ObtainRead(&cm_volumeLock);
416 for ( volp = cm_data.volumeROIDHashTablep[hash]; volp; volp = nextVolp) {
417 nextVolp = volp->vol[ROVOL].nextp;
418 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
419 volp->vol[ROVOL].ID) {
420 lock_ReleaseRead(&cm_volumeLock);
421 RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[ROVOL].ID, AFS_INVALIDATE_CREDS);
422 lock_ObtainRead(&cm_volumeLock);
425 for ( volp = cm_data.volumeBKIDHashTablep[hash]; volp; volp = nextVolp) {
426 nextVolp = volp->vol[BACKVOL].nextp;
427 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
428 volp->vol[BACKVOL].ID) {
429 lock_ReleaseRead(&cm_volumeLock);
430 RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[BACKVOL].ID, AFS_INVALIDATE_CREDS);
431 lock_ObtainRead(&cm_volumeLock);
435 lock_ReleaseRead(&cm_volumeLock);