windows-locking-freeacls-20050415
[openafs.git] / src / WINNT / afsd / cm_aclent.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #ifndef DJGPP
14 #include <windows.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 #include <malloc.h>
19
20 #include "afsd.h"
21 #include <osisleep.h>
22
23 /* 
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.
30  */
31 osi_rwlock_t cm_aclLock;                /* lock for system's aclents */
32
33 /* This must be called with cm_aclLock and the aclp->back->mx held */
34 static void CleanupACLEnt(cm_aclent_t * aclp)
35 {
36     cm_aclent_t *taclp;
37     cm_aclent_t **laclpp;
38         
39     if (aclp->backp) {
40         if (aclp->backp->randomACLp) {
41             /* 
42              * Remove the entry from the vnode's list 
43              */
44             lock_AssertMutex(&aclp->backp->mx);
45             laclpp = &aclp->backp->randomACLp;
46             for (taclp = *laclpp; taclp; laclpp = &taclp->nextp, taclp = *laclpp) {
47                 if (taclp == aclp) 
48                     break;
49             }
50             if (!taclp) 
51                 osi_panic("CleanupACLEnt race", __FILE__, __LINE__);
52             *laclpp = aclp->nextp;                      /* remove from vnode list */
53         }
54         aclp->backp = NULL;
55     }
56
57     /* release the old user */
58     if (aclp->userp) {
59         cm_ReleaseUser(aclp->userp);
60         aclp->userp = NULL;
61     }
62
63     aclp->randomAccess = 0;
64     aclp->tgtLifetime = 0;
65 }
66
67 /* 
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.
70  */
71 long cm_FindACLCache(cm_scache_t *scp, cm_user_t *userp, long *rightsp)
72 {
73     cm_aclent_t *aclp;
74     long retval = -1;
75
76     lock_ObtainWrite(&cm_aclLock);
77     *rightsp = 0;   /* get a new acl from server if we don't find a
78                      * current entry 
79                      */
80
81     for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
82         if (aclp->userp == userp) {
83             if (aclp->tgtLifetime && aclp->tgtLifetime <= osi_Time()) {
84                 /* ticket expired */
85                 osi_QRemove((osi_queue_t **) &cm_data.aclLRUp, &aclp->q);
86                 CleanupACLEnt(aclp);
87
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,
91                            &aclp->q);
92             } else {
93                 *rightsp = aclp->randomAccess;
94                 if (cm_data.aclLRUEndp == aclp)
95                     cm_data.aclLRUEndp = (cm_aclent_t *) osi_QPrev(&aclp->q);
96
97                 /* move to the head of the LRU queue */
98                 osi_QRemove((osi_queue_t **) &cm_data.aclLRUp, &aclp->q);
99                 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
100                            (osi_queue_t **) &cm_data.aclLRUEndp,
101                            &aclp->q);
102                 retval = 0;     /* success */
103             }               
104             break;
105         }
106     }
107
108     lock_ReleaseWrite(&cm_aclLock);
109     return retval;
110 }       
111
112 /* 
113  * This function returns a free (not in the LRU queue) acl cache entry.
114  * It must be called with the cm_aclLock lock held
115  */
116 static cm_aclent_t *GetFreeACLEnt(cm_scache_t * scp)
117 {
118     cm_aclent_t *aclp;
119     cm_scache_t *ascp = 0;
120         
121     if (cm_data.aclLRUp == NULL)
122         osi_panic("empty aclent LRU", __FILE__, __LINE__);
123
124     aclp = cm_data.aclLRUEndp;
125     cm_data.aclLRUEndp = (cm_aclent_t *) osi_QPrev(&aclp->q);
126     osi_QRemove((osi_queue_t **) &cm_data.aclLRUp, &aclp->q);
127
128     if (aclp->backp && scp != aclp->backp) {
129         ascp = aclp->backp;
130         lock_ObtainMutex(&ascp->mx);
131     }
132     CleanupACLEnt(aclp);
133
134     if (ascp)
135         lock_ReleaseMutex(&ascp->mx);
136     return aclp;
137 }
138
139
140 /* 
141  * Add rights to an acl cache entry.  Do the right thing if not present, 
142  * including digging up an entry from the LRU queue.
143  *
144  * The scp must be locked when this function is called.
145  */
146 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, long rights)
147 {
148     register struct cm_aclent *aclp;
149
150     lock_ObtainWrite(&cm_aclLock);
151     for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
152         if (aclp->userp == userp) {
153             aclp->randomAccess = rights;
154             if (aclp->tgtLifetime == 0) 
155                 aclp->tgtLifetime = cm_TGTLifeTime(pag);
156             lock_ReleaseWrite(&cm_aclLock);
157             return 0;
158         }
159     }
160
161     /* 
162      * Didn't find the dude we're looking for, so take someone from the LRUQ 
163      * and  reuse. But first try the free list and see if there's already 
164      * someone there.
165      */
166     aclp = GetFreeACLEnt(scp);           /* can't fail, panics instead */
167     osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
168     aclp->backp = scp;
169     aclp->nextp = scp->randomACLp;
170     scp->randomACLp = aclp;
171     cm_HoldUser(userp);
172     aclp->userp = userp;
173     aclp->randomAccess = rights;
174     aclp->tgtLifetime = cm_TGTLifeTime(userp);
175     lock_ReleaseWrite(&cm_aclLock);
176
177     return 0;
178 }
179
180 long cm_ShutdownACLCache(void)
181 {
182     return 0;
183 }
184
185 long cm_ValidateACLCache(void)
186 {
187     long size = cm_data.stats * 2;
188     long count;
189     cm_aclent_t * aclp;
190
191     for ( aclp = cm_data.aclLRUp, count = 0; aclp;
192           aclp = (cm_aclent_t *) osi_QNext(&aclp->q), count++ ) {
193         if (aclp->magic != CM_ACLENT_MAGIC) {
194             afsi_log("cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC");
195             fprintf(stderr, "cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC\n");
196             return -1;
197         }
198         if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
199             afsi_log("cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC");
200             fprintf(stderr,"cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC\n");
201             return -2;
202         }
203         if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
204             afsi_log("cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC");
205             fprintf(stderr,"cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC\n");
206             return -3;
207         }
208         if (count != 0 && aclp == cm_data.aclLRUp || count > size) {
209             afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUp list");
210             fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUp list\n");
211             return -4;
212         }
213     }
214
215     for ( aclp = cm_data.aclLRUEndp, count = 0; aclp;
216           aclp = (cm_aclent_t *) osi_QPrev(&aclp->q), count++ ) {
217         if (aclp->magic != CM_ACLENT_MAGIC) {
218             afsi_log("cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC");
219             fprintf(stderr, "cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC\n");
220             return -5;
221         }
222         if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
223             afsi_log("cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC");
224             fprintf(stderr, "cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC\n");
225             return -6;
226         }
227         if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
228             afsi_log("cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC");
229             fprintf(stderr, "cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC\n");
230             return -7;
231         }
232
233         if (count != 0 && aclp == cm_data.aclLRUEndp || count > size) {
234             afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list");
235             fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list\n");
236             return -8;
237         }
238     }
239
240     return 0;
241 }
242
243 /* 
244  * Initialize the cache to have an entries.  Called during system startup.
245  */
246 long cm_InitACLCache(int newFile, long size)
247 {
248     cm_aclent_t *aclp;
249     long i;
250     static osi_once_t once;
251
252     if (osi_Once(&once)) {
253         lock_InitializeRWLock(&cm_aclLock, "cm_aclLock");
254         osi_EndOnce(&once);
255     }
256
257     lock_ObtainWrite(&cm_aclLock);
258     if ( newFile ) {
259         cm_data.aclLRUp = cm_data.aclLRUEndp = NULL;
260         aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
261         memset(aclp, 0, size * sizeof(cm_aclent_t));
262
263         /* 
264          * Put all of these guys on the LRU queue 
265          */
266         for (i = 0; i < size; i++) {
267             aclp->magic = CM_ACLENT_MAGIC;
268             osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
269             aclp++;
270         }
271     } else {
272         aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
273         for (i = 0; i < size; i++) {
274             aclp->userp = NULL;
275             aclp->tgtLifetime = 0;
276             aclp++;
277         }
278     }
279     lock_ReleaseWrite(&cm_aclLock);
280     return 0;
281 }
282
283
284 /* 
285  * Free all associated acl entries.  We actually just clear the back pointer
286  * since the acl entries are already in the free list.  The scp must be locked
287  * or completely unreferenced (such as when called while recycling the scp).
288  */
289 void cm_FreeAllACLEnts(cm_scache_t *scp)
290 {
291     cm_aclent_t *aclp;
292     cm_aclent_t *taclp;
293
294     lock_ObtainWrite(&cm_aclLock);
295     for (aclp = scp->randomACLp; aclp; aclp = taclp) {
296         taclp = aclp->nextp;
297         if (aclp->userp) {
298             cm_ReleaseUser(aclp->userp);
299             aclp->userp = NULL;
300         }
301         aclp->backp = (struct cm_scache *) 0;
302     }
303
304     scp->randomACLp = (struct cm_aclent *) 0;
305     scp->anyAccess = 0;         /* reset this, too */
306     lock_ReleaseWrite(&cm_aclLock);
307 }
308
309
310 /* 
311  * Invalidate all ACL entries for particular user on this particular vnode.
312  *
313  * The scp must be locked.
314  */
315 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
316 {
317     cm_aclent_t *aclp;
318     cm_aclent_t **laclpp;
319
320     lock_ObtainWrite(&cm_aclLock);
321     laclpp = &scp->randomACLp;
322     for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
323         if (userp == aclp->userp) {     /* One for a given user/scache */
324             *laclpp = aclp->nextp;
325             cm_ReleaseUser(aclp->userp);
326             aclp->userp = NULL;
327             aclp->backp = (struct cm_scache *) 0;
328             break;
329         }
330     }
331     lock_ReleaseWrite(&cm_aclLock);
332 }