Windows: fix indentation
[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 time_t cm_TGTLifeTime(cm_user_t *userp, afs_uint32 cellID)
143 {
144     cm_cell_t *cellp = NULL;
145     cm_ucell_t * ucp = NULL;
146     time_t      expirationTime = 0;
147
148     cellp = cm_FindCellByID(cellID, CM_FLAG_NOPROBE);
149     lock_ObtainMutex(&userp->mx);
150     ucp = cm_GetUCell(userp, cellp);
151     if (ucp->ticketp)
152         expirationTime = ucp->expirationTime;
153     lock_ReleaseMutex(&userp->mx);
154
155     return expirationTime;
156 }
157
158
159 /*
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.
162  *
163  * The scp must be locked when this function is called.
164  */
165 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 rights)
166 {
167     struct cm_aclent *aclp;
168
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,
180                            &aclp->q);
181             }
182             lock_ReleaseWrite(&cm_aclLock);
183             return 0;
184         }
185     }
186
187     /*
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
190      * someone there.
191      */
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);
194     aclp->backp = scp;
195     aclp->nextp = scp->randomACLp;
196     scp->randomACLp = aclp;
197     cm_HoldUser(userp);
198     aclp->userp = userp;
199     aclp->randomAccess = rights;
200     aclp->tgtLifetime = cm_TGTLifeTime(userp, scp->fid.cell);
201     lock_ReleaseWrite(&cm_aclLock);
202
203     return 0;
204 }
205
206 long cm_ShutdownACLCache(void)
207 {
208     return 0;
209 }
210
211 long cm_ValidateACLCache(void)
212 {
213     long size = cm_data.stats * 2;
214     long count;
215     cm_aclent_t * aclp;
216
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");
221         return -9;
222     }
223
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");
229             return -1;
230         }
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");
234             return -2;
235         }
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");
239             return -3;
240         }
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");
244             return -4;
245         }
246     }
247
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");
253             return -5;
254         }
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");
258             return -6;
259         }
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");
263             return -7;
264         }
265
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");
269             return -8;
270         }
271     }
272
273     return 0;
274 }
275
276 /*
277  * Initialize the cache to have an entries.  Called during system startup.
278  */
279 long cm_InitACLCache(int newFile, long size)
280 {
281     cm_aclent_t *aclp;
282     long i;
283     static osi_once_t once;
284
285     if (osi_Once(&once)) {
286         lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL);
287         osi_EndOnce(&once);
288     }
289
290     lock_ObtainWrite(&cm_aclLock);
291     if ( newFile ) {
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));
295
296         /*
297          * Put all of these guys on the LRU queue
298          */
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);
302             aclp++;
303         }
304     } else {
305         aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
306         for (i = 0; i < size; i++) {
307             aclp->userp = NULL;
308             aclp->tgtLifetime = 0;
309             aclp++;
310         }
311     }
312     lock_ReleaseWrite(&cm_aclLock);
313     return 0;
314 }
315
316
317 /*
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).
321  */
322 void cm_FreeAllACLEnts(cm_scache_t *scp)
323 {
324     cm_aclent_t *aclp;
325     cm_aclent_t *taclp;
326
327     lock_ObtainWrite(&cm_aclLock);
328     for (aclp = scp->randomACLp; aclp; aclp = taclp) {
329         taclp = aclp->nextp;
330         if (aclp->userp) {
331             cm_ReleaseUser(aclp->userp);
332             aclp->userp = NULL;
333         }
334         aclp->backp = (struct cm_scache *) 0;
335     }
336
337     scp->randomACLp = (struct cm_aclent *) 0;
338     scp->anyAccess = 0;         /* reset this, too */
339     lock_ReleaseWrite(&cm_aclLock);
340 }
341
342
343 /*
344  * Invalidate all ACL entries for particular user on this particular vnode.
345  *
346  * The scp must not be locked.
347  */
348 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
349 {
350     cm_aclent_t *aclp;
351     cm_aclent_t **laclpp;
352     int found = 0;
353     int callback = 0;
354
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);
362             aclp->userp = NULL;
363             aclp->backp = (struct cm_scache *) 0;
364             found = 1;
365             break;
366         }
367     }
368     lock_ReleaseWrite(&cm_aclLock);
369     if (found)
370         callback = cm_HaveCallback(scp);
371     lock_ReleaseWrite(&scp->rw);
372
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);
376 }
377
378 /*
379  * Invalidate ACL info for a user that has just obtained or lost tokens.
380  */
381 void
382 cm_ResetACLCache(cm_cell_t *cellp, cm_user_t *userp)
383 {
384     cm_volume_t *volp, *nextVolp;
385     cm_scache_t *scp, *nextScp;
386     afs_uint32 hash;
387
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;
392             if (cellp == NULL ||
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);
399             }
400         }
401     }
402     lock_ReleaseRead(&cm_scacheLock);
403
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);
414                 }
415             }
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);
423                 }
424             }
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);
432                 }
433             }
434         }
435         lock_ReleaseRead(&cm_volumeLock);
436     }
437 }
438
439