Windows: remove signed/unsigned mismatch cm_aclent.c
[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 <afsconfig.h>
11 #include <afs/param.h>
12 #include <roken.h>
13
14 #include <afs/stds.h>
15
16 #include <windows.h>
17 #include <stdlib.h>
18 #include <string.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_AssertWrite(&aclp->backp->rw);
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, afs_uint32 *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 <= time(NULL)) {
84                 /* ticket expired */
85                 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &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.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,
99                                &aclp->q);
100                 }
101                 retval = 0;     /* success */
102             }
103             break;
104         }
105     }
106
107     lock_ReleaseWrite(&cm_aclLock);
108     return retval;
109 }
110
111 /*
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
114  */
115 static cm_aclent_t *GetFreeACLEnt(cm_scache_t * scp)
116 {
117     cm_aclent_t *aclp;
118     cm_scache_t *ascp = 0;
119
120     if (cm_data.aclLRUp == NULL)
121         osi_panic("empty aclent LRU", __FILE__, __LINE__);
122
123     if (cm_data.aclLRUEndp == NULL)
124         osi_panic("inconsistent aclent LRUEndp == NULL", __FILE__, __LINE__);
125
126     aclp = cm_data.aclLRUEndp;
127     osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
128
129     if (aclp->backp && scp != aclp->backp) {
130         ascp = aclp->backp;
131         lock_ReleaseWrite(&cm_aclLock);
132         lock_ObtainWrite(&ascp->rw);
133         lock_ObtainWrite(&cm_aclLock);
134     }
135     CleanupACLEnt(aclp);
136
137     if (ascp)
138         lock_ReleaseWrite(&ascp->rw);
139     return aclp;
140 }
141
142
143 /*
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.
146  *
147  * The scp must be locked when this function is called.
148  */
149 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 rights)
150 {
151     struct cm_aclent *aclp;
152
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);
160             return 0;
161         }
162     }
163
164     /*
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
167      * someone there.
168      */
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);
171     aclp->backp = scp;
172     aclp->nextp = scp->randomACLp;
173     scp->randomACLp = aclp;
174     cm_HoldUser(userp);
175     aclp->userp = userp;
176     aclp->randomAccess = rights;
177     aclp->tgtLifetime = cm_TGTLifeTime(userp);
178     lock_ReleaseWrite(&cm_aclLock);
179
180     return 0;
181 }
182
183 long cm_ShutdownACLCache(void)
184 {
185     return 0;
186 }
187
188 long cm_ValidateACLCache(void)
189 {
190     long size = cm_data.stats * 2;
191     long count;
192     cm_aclent_t * aclp;
193
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");
198         return -9;
199     }
200
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");
206             return -1;
207         }
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");
211             return -2;
212         }
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");
216             return -3;
217         }
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");
221             return -4;
222         }
223     }
224
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");
230             return -5;
231         }
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");
235             return -6;
236         }
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");
240             return -7;
241         }
242
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");
246             return -8;
247         }
248     }
249
250     return 0;
251 }
252
253 /*
254  * Initialize the cache to have an entries.  Called during system startup.
255  */
256 long cm_InitACLCache(int newFile, long size)
257 {
258     cm_aclent_t *aclp;
259     long i;
260     static osi_once_t once;
261
262     if (osi_Once(&once)) {
263         lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL);
264         osi_EndOnce(&once);
265     }
266
267     lock_ObtainWrite(&cm_aclLock);
268     if ( newFile ) {
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));
272
273         /*
274          * Put all of these guys on the LRU queue
275          */
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);
279             aclp++;
280         }
281     } else {
282         aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
283         for (i = 0; i < size; i++) {
284             aclp->userp = NULL;
285             aclp->tgtLifetime = 0;
286             aclp++;
287         }
288     }
289     lock_ReleaseWrite(&cm_aclLock);
290     return 0;
291 }
292
293
294 /*
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).
298  */
299 void cm_FreeAllACLEnts(cm_scache_t *scp)
300 {
301     cm_aclent_t *aclp;
302     cm_aclent_t *taclp;
303
304     lock_ObtainWrite(&cm_aclLock);
305     for (aclp = scp->randomACLp; aclp; aclp = taclp) {
306         taclp = aclp->nextp;
307         if (aclp->userp) {
308             cm_ReleaseUser(aclp->userp);
309             aclp->userp = NULL;
310         }
311         aclp->backp = (struct cm_scache *) 0;
312     }
313
314     scp->randomACLp = (struct cm_aclent *) 0;
315     scp->anyAccess = 0;         /* reset this, too */
316     lock_ReleaseWrite(&cm_aclLock);
317 }
318
319
320 /*
321  * Invalidate all ACL entries for particular user on this particular vnode.
322  *
323  * The scp must be locked.
324  */
325 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
326 {
327     cm_aclent_t *aclp;
328     cm_aclent_t **laclpp;
329
330     lock_ObtainWrite(&cm_aclLock);
331     laclpp = &scp->randomACLp;
332     for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
333         if (userp == aclp->userp) {     /* One for a given user/scache */
334             *laclpp = aclp->nextp;
335             cm_ReleaseUser(aclp->userp);
336             aclp->userp = NULL;
337             aclp->backp = (struct cm_scache *) 0;
338             break;
339         }
340     }
341     lock_ReleaseWrite(&cm_aclLock);
342 }
343
344 /*
345  * Invalidate ACL info for a user that has just obtained or lost tokens.
346  */
347 void
348 cm_ResetACLCache(cm_cell_t *cellp, cm_user_t *userp)
349 {
350     cm_scache_t *scp;
351     afs_uint32 hash;
352
353     lock_ObtainWrite(&cm_scacheLock);
354     for (hash=0; hash < cm_data.scacheHashTableSize; hash++) {
355         for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
356             if (cellp == NULL ||
357                 scp->fid.cell == cellp->cellID) {
358                 cm_HoldSCacheNoLock(scp);
359                 lock_ReleaseWrite(&cm_scacheLock);
360                 lock_ObtainWrite(&scp->rw);
361                 cm_InvalidateACLUser(scp, userp);
362                 lock_ReleaseWrite(&scp->rw);
363                 lock_ObtainWrite(&cm_scacheLock);
364                 cm_ReleaseSCacheNoLock(scp);
365             }
366         }
367     }
368     lock_ReleaseWrite(&cm_scacheLock);
369 }
370
371