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 <afs/param.h>
23 * This next lock controls access to all cm_aclent structures in the system,
24 * in either the free list or in the LRU queue. A read lock prevents someone
25 * from modifying the list(s), and a write lock is required for modifying
26 * the list. The actual data stored in the randomUid and randomAccess fields
27 * is actually maintained as up-to-date or not via the scache llock.
28 * An aclent structure is free if it has no back vnode pointer.
30 osi_rwlock_t cm_aclLock; /* lock for system's aclents */
31 cm_aclent_t *cm_aclLRUp; /* LRUQ for dudes in vnode's lists */
32 cm_aclent_t *cm_aclLRUEndp; /* ditto */
34 * Get an acl cache entry for a particular user and file, or return that it doesn't exist.
35 * Called with the scp locked.
37 long cm_FindACLCache(cm_scache_t *scp, cm_user_t *userp, long *rightsp)
42 lock_ObtainWrite(&cm_aclLock);
43 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
44 if (aclp->userp == userp) {
45 if (aclp->tgtLifetime && aclp->tgtLifetime <= (long) osi_Time()) {
47 aclp->tgtLifetime = 0;
48 *rightsp = 0; /* get a new acl from server */
50 /* Shouldn't we remove this entry from the scp?
51 * 2005-01-25 - jaltman@secure-endpoints.com
54 *rightsp = aclp->randomAccess;
55 if (cm_aclLRUEndp == aclp)
56 cm_aclLRUEndp = (cm_aclent_t *) osi_QPrev(&aclp->q);
58 /* move to the head of the LRU queue */
59 osi_QRemove((osi_queue_t **) &cm_aclLRUp, &aclp->q);
60 osi_QAddH((osi_queue_t **) &cm_aclLRUp,
61 (osi_queue_t **) &cm_aclLRUEndp,
63 retval = 0; /* success */
69 lock_ReleaseWrite(&cm_aclLock);
75 * This function returns a free (not in the LRU queue) acl cache entry.
76 * It must be called with the cm_aclLock lock held.
78 static cm_aclent_t *GetFreeACLEnt(void)
84 if (cm_aclLRUp == NULL)
85 osi_panic("empty aclent LRU", __FILE__, __LINE__);
88 if (aclp == cm_aclLRUEndp)
89 cm_aclLRUEndp = (cm_aclent_t *) osi_QPrev(&aclp->q);
90 osi_QRemove((osi_queue_t **) &cm_aclLRUp, &aclp->q);
93 * Remove the entry from the vnode's list
95 laclpp = &aclp->backp->randomACLp;
96 for (taclp = *laclpp; taclp; laclpp = &taclp->nextp, taclp = *laclpp) {
101 osi_panic("GetFreeACLEnt race", __FILE__, __LINE__);
102 *laclpp = aclp->nextp; /* remove from vnode list */
106 /* release the old user */
108 cm_ReleaseUser(aclp->userp);
116 * Add rights to an acl cache entry. Do the right thing if not present,
117 * including digging up an entry from the LRU queue.
119 * The scp must be locked when this function is called.
121 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, long rights)
123 register struct cm_aclent *aclp;
125 lock_ObtainWrite(&cm_aclLock);
126 for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
127 if (aclp->userp == userp) {
128 aclp->randomAccess = rights;
129 if (aclp->tgtLifetime == 0)
130 aclp->tgtLifetime = cm_TGTLifeTime(pag);
131 lock_ReleaseWrite(&cm_aclLock);
137 * Didn't find the dude we're looking for, so take someone from the LRUQ
138 * and reuse. But first try the free list and see if there's already
141 aclp = GetFreeACLEnt(); /* can't fail, panics instead */
142 osi_QAddH((osi_queue_t **) &cm_aclLRUp, (osi_queue_t **) &cm_aclLRUEndp, &aclp->q);
144 aclp->nextp = scp->randomACLp;
145 scp->randomACLp = aclp;
148 aclp->randomAccess = rights;
149 aclp->tgtLifetime = cm_TGTLifeTime(userp);
150 lock_ReleaseWrite(&cm_aclLock);
156 * Initialize the cache to have an entries. Called during system startup.
158 long cm_InitACLCache(long size)
162 static osi_once_t once;
164 if (osi_Once(&once)) {
165 lock_InitializeRWLock(&cm_aclLock, "cm_aclLock");
169 lock_ObtainWrite(&cm_aclLock);
170 cm_aclLRUp = cm_aclLRUEndp = NULL;
171 aclp = (cm_aclent_t *) malloc(size * sizeof(cm_aclent_t));
172 memset(aclp, 0, size * sizeof(cm_aclent_t));
175 * Put all of these guys on the LRU queue
177 for (i = 0; i < size; i++) {
178 osi_QAddH((osi_queue_t **) &cm_aclLRUp, (osi_queue_t **) &cm_aclLRUEndp, &aclp->q);
181 lock_ReleaseWrite(&cm_aclLock);
187 * Free all associated acl entries. We actually just clear the back pointer
188 * since the acl entries are already in the free list. The scp must be locked
189 * or completely unreferenced (such as when called while recycling the scp).
191 void cm_FreeAllACLEnts(cm_scache_t *scp)
196 lock_ObtainWrite(&cm_aclLock);
197 for (aclp = scp->randomACLp; aclp; aclp = taclp) {
200 cm_ReleaseUser(aclp->userp);
203 aclp->backp = (struct cm_scache *) 0;
206 scp->randomACLp = (struct cm_aclent *) 0;
207 scp->anyAccess = 0; /* reset this, too */
208 lock_ReleaseWrite(&cm_aclLock);
213 * Invalidate all ACL entries for particular user on this particular vnode.
215 * The scp must be locked.
217 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
220 cm_aclent_t **laclpp;
222 lock_ObtainWrite(&cm_aclLock);
223 laclpp = &scp->randomACLp;
224 for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
225 if (userp == aclp->userp) { /* One for a given user/scache */
226 *laclpp = aclp->nextp;
227 cm_ReleaseUser(aclp->userp);
229 aclp->backp = (struct cm_scache *) 0;
233 lock_ReleaseWrite(&cm_aclLock);