494eab81e47f74bce491ae70f481e1d78c2ee222
[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 write 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     time_t now = time(NULL);
76
77     lock_AssertWrite(&scp->rw);
78     lock_ObtainWrite(&cm_aclLock);
79     *rightsp = 0;   /* get a new acl from server if we don't find a
80                      * current entry
81                      */
82
83     for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
84         if (aclp->userp == userp) {
85             if (aclp->tgtLifetime && aclp->tgtLifetime <= now) {
86                 /* ticket expired */
87                 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
88                 CleanupACLEnt(aclp);
89
90                 /* move to the tail of the LRU queue */
91                 osi_QAddT((osi_queue_t **) &cm_data.aclLRUp,
92                            (osi_queue_t **) &cm_data.aclLRUEndp,
93                            &aclp->q);
94             } else {
95                 *rightsp = aclp->randomAccess;
96                 if (cm_data.aclLRUp != aclp) {
97                     /* move to the head of the LRU queue */
98                     osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
99                     osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
100                               (osi_queue_t **) &cm_data.aclLRUEndp,
101                               &aclp->q);
102                 }
103                 retval = 0;     /* success */
104             }
105             break;
106         }
107     }
108
109     lock_ReleaseWrite(&cm_aclLock);
110     return retval;
111 }
112
113 /*
114  * This function returns a free (not in the LRU queue) acl cache entry.
115  * It must be called with the cm_aclLock lock held
116  */
117 static cm_aclent_t *GetFreeACLEnt(cm_scache_t * scp)
118 {
119     cm_aclent_t *aclp;
120     cm_scache_t *ascp = 0;
121
122     if (cm_data.aclLRUp == NULL)
123         osi_panic("empty aclent LRU", __FILE__, __LINE__);
124
125     if (cm_data.aclLRUEndp == NULL)
126         osi_panic("inconsistent aclent LRUEndp == NULL", __FILE__, __LINE__);
127
128     aclp = cm_data.aclLRUEndp;
129     osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
130
131     if (aclp->backp && scp != aclp->backp) {
132         ascp = aclp->backp;
133         lock_ReleaseWrite(&cm_aclLock);
134         lock_ObtainWrite(&ascp->rw);
135         lock_ObtainWrite(&cm_aclLock);
136     }
137     CleanupACLEnt(aclp);
138
139     if (ascp)
140         lock_ReleaseWrite(&ascp->rw);
141     return aclp;
142 }
143
144 time_t cm_TGTLifeTime(cm_user_t *userp, afs_uint32 cellID)
145 {
146     cm_cell_t *cellp = NULL;
147     cm_ucell_t * ucp = NULL;
148     time_t      expirationTime = 0;
149
150     lock_ObtainMutex(&userp->mx);
151     cellp = cm_FindCellByID(cellID, CM_FLAG_NOPROBE);
152     ucp = cm_GetUCell(userp, cellp);
153     if (ucp->ticketp)
154         expirationTime = ucp->expirationTime;
155     lock_ReleaseMutex(&userp->mx);
156
157     return expirationTime;
158 }
159
160
161 /*
162  * Add rights to an acl cache entry.  Do the right thing if not present,
163  * including digging up an entry from the LRU queue.
164  *
165  * The scp must be locked when this function is called.
166  */
167 long cm_AddACLCache(cm_scache_t *scp, cm_user_t *userp, afs_uint32 rights)
168 {
169     struct cm_aclent *aclp;
170     time_t tgtLifeTime;
171
172     tgtLifeTime = cm_TGTLifeTime(userp, scp->fid.cell);
173
174     lock_ObtainWrite(&cm_aclLock);
175     for (aclp = scp->randomACLp; aclp; aclp = aclp->nextp) {
176         if (aclp->userp == userp) {
177             aclp->randomAccess = rights;
178             if (aclp->tgtLifetime < tgtLifeTime)
179                 aclp->tgtLifetime = tgtLifeTime;
180             if (cm_data.aclLRUp != aclp) {
181                 /* move to the head of the LRU queue */
182                 osi_QRemoveHT((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
183                 osi_QAddH((osi_queue_t **) &cm_data.aclLRUp,
184                            (osi_queue_t **) &cm_data.aclLRUEndp,
185                            &aclp->q);
186             }
187             lock_ReleaseWrite(&cm_aclLock);
188             return 0;
189         }
190     }
191
192     /*
193      * Didn't find the dude we're looking for, so take someone from the LRUQ
194      * and  reuse. But first try the free list and see if there's already
195      * someone there.
196      */
197     aclp = GetFreeACLEnt(scp);           /* can't fail, panics instead */
198     osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
199     aclp->backp = scp;
200     aclp->nextp = scp->randomACLp;
201     scp->randomACLp = aclp;
202     cm_HoldUser(userp);
203     aclp->userp = userp;
204     aclp->randomAccess = rights;
205     aclp->tgtLifetime = tgtLifeTime;
206     lock_ReleaseWrite(&cm_aclLock);
207
208     return 0;
209 }
210
211 long cm_ShutdownACLCache(void)
212 {
213     return 0;
214 }
215
216 long cm_ValidateACLCache(void)
217 {
218     long size = cm_data.stats * 2;
219     long count;
220     cm_aclent_t * aclp;
221
222     if ( cm_data.aclLRUp == NULL && cm_data.aclLRUEndp != NULL ||
223          cm_data.aclLRUp != NULL && cm_data.aclLRUEndp == NULL) {
224         afsi_log("cm_ValidateACLCache failure: inconsistent LRU pointers");
225         fprintf(stderr, "cm_ValidateACLCache failure: inconsistent LRU pointers\n");
226         return -9;
227     }
228
229     for ( aclp = cm_data.aclLRUp, count = 0; aclp;
230           aclp = (cm_aclent_t *) osi_QNext(&aclp->q), count++ ) {
231         if (aclp->magic != CM_ACLENT_MAGIC) {
232             afsi_log("cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC");
233             fprintf(stderr, "cm_ValidateACLCache failure: acpl->magic != CM_ACLENT_MAGIC\n");
234             return -1;
235         }
236         if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
237             afsi_log("cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC");
238             fprintf(stderr,"cm_ValidateACLCache failure: acpl->nextp->magic != CM_ACLENT_MAGIC\n");
239             return -2;
240         }
241         if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
242             afsi_log("cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC");
243             fprintf(stderr,"cm_ValidateACLCache failure: acpl->backp->magic != CM_SCACHE_MAGIC\n");
244             return -3;
245         }
246         if (count != 0 && aclp == cm_data.aclLRUp || count > size) {
247             afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUp list");
248             fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUp list\n");
249             return -4;
250         }
251     }
252
253     for ( aclp = cm_data.aclLRUEndp, count = 0; aclp;
254           aclp = (cm_aclent_t *) osi_QPrev(&aclp->q), count++ ) {
255         if (aclp->magic != CM_ACLENT_MAGIC) {
256             afsi_log("cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC");
257             fprintf(stderr, "cm_ValidateACLCache failure: aclp->magic != CM_ACLENT_MAGIC\n");
258             return -5;
259         }
260         if (aclp->nextp && aclp->nextp->magic != CM_ACLENT_MAGIC) {
261             afsi_log("cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC");
262             fprintf(stderr, "cm_ValidateACLCache failure: aclp->nextp->magic != CM_ACLENT_MAGIC\n");
263             return -6;
264         }
265         if (aclp->backp && aclp->backp->magic != CM_SCACHE_MAGIC) {
266             afsi_log("cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC");
267             fprintf(stderr, "cm_ValidateACLCache failure: aclp->backp->magic != CM_SCACHE_MAGIC\n");
268             return -7;
269         }
270
271         if (count != 0 && aclp == cm_data.aclLRUEndp || count > size) {
272             afsi_log("cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list");
273             fprintf(stderr, "cm_ValidateACLCache failure: loop in cm_data.aclLRUEndp list\n");
274             return -8;
275         }
276     }
277
278     return 0;
279 }
280
281 /*
282  * Initialize the cache to have an entries.  Called during system startup.
283  */
284 long cm_InitACLCache(int newFile, long size)
285 {
286     cm_aclent_t *aclp;
287     long i;
288     static osi_once_t once;
289
290     if (osi_Once(&once)) {
291         lock_InitializeRWLock(&cm_aclLock, "cm_aclLock", LOCK_HIERARCHY_ACL_GLOBAL);
292         osi_EndOnce(&once);
293     }
294
295     lock_ObtainWrite(&cm_aclLock);
296     if ( newFile ) {
297         cm_data.aclLRUp = cm_data.aclLRUEndp = NULL;
298         aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
299         memset(aclp, 0, size * sizeof(cm_aclent_t));
300
301         /*
302          * Put all of these guys on the LRU queue
303          */
304         for (i = 0; i < size; i++) {
305             aclp->magic = CM_ACLENT_MAGIC;
306             osi_QAddH((osi_queue_t **) &cm_data.aclLRUp, (osi_queue_t **) &cm_data.aclLRUEndp, &aclp->q);
307             aclp++;
308         }
309     } else {
310         aclp = (cm_aclent_t *) cm_data.aclBaseAddress;
311         for (i = 0; i < size; i++) {
312             aclp->userp = NULL;
313             aclp->tgtLifetime = 0;
314             aclp++;
315         }
316     }
317     lock_ReleaseWrite(&cm_aclLock);
318     return 0;
319 }
320
321
322 /*
323  * Free all associated acl entries.  We actually just clear the back pointer
324  * since the acl entries are already in the free list.  The scp must be locked
325  * or completely unreferenced (such as when called while recycling the scp).
326  */
327 void cm_FreeAllACLEnts(cm_scache_t *scp)
328 {
329     cm_aclent_t *aclp;
330     cm_aclent_t *taclp;
331
332     lock_ObtainWrite(&cm_aclLock);
333     for (aclp = scp->randomACLp; aclp; aclp = taclp) {
334         taclp = aclp->nextp;
335         if (aclp->userp) {
336             cm_ReleaseUser(aclp->userp);
337             aclp->userp = NULL;
338         }
339         aclp->backp = (struct cm_scache *) 0;
340     }
341
342     scp->randomACLp = (struct cm_aclent *) 0;
343     scp->anyAccess = 0;         /* reset this, too */
344     lock_ReleaseWrite(&cm_aclLock);
345 }
346
347
348 /*
349  * Invalidate all ACL entries for particular user on this particular vnode.
350  *
351  * The scp must not be locked.
352  */
353 void cm_InvalidateACLUser(cm_scache_t *scp, cm_user_t *userp)
354 {
355     cm_aclent_t *aclp;
356     cm_aclent_t **laclpp;
357     int found = 0;
358     int callback = 0;
359
360     lock_ObtainWrite(&scp->rw);
361     lock_ObtainWrite(&cm_aclLock);
362     laclpp = &scp->randomACLp;
363     for (aclp = *laclpp; aclp; laclpp = &aclp->nextp, aclp = *laclpp) {
364         if (userp == aclp->userp) {     /* One for a given user/scache */
365             *laclpp = aclp->nextp;
366             cm_ReleaseUser(aclp->userp);
367             aclp->userp = NULL;
368             aclp->backp = (struct cm_scache *) 0;
369             found = 1;
370             break;
371         }
372     }
373     lock_ReleaseWrite(&cm_aclLock);
374     if (found)
375         callback = cm_HaveCallback(scp);
376     lock_ReleaseWrite(&scp->rw);
377
378     if (found && callback && RDR_Initialized)
379         RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
380                              scp->fid.hash, scp->fileType, AFS_INVALIDATE_CREDS);
381 }
382
383 /*
384  * Invalidate ACL info for a user that has just obtained or lost tokens.
385  */
386 void
387 cm_ResetACLCache(cm_cell_t *cellp, cm_user_t *userp)
388 {
389     cm_volume_t *volp, *nextVolp;
390     cm_scache_t *scp, *nextScp;
391     afs_uint32 hash;
392
393     lock_ObtainRead(&cm_scacheLock);
394     for (hash=0; hash < cm_data.scacheHashTableSize; hash++) {
395         for (scp=cm_data.scacheHashTablep[hash]; scp; scp=nextScp) {
396             nextScp = scp->nextp;
397             if (cellp == NULL ||
398                 scp->fid.cell == cellp->cellID) {
399                 cm_HoldSCacheNoLock(scp);
400                 lock_ReleaseRead(&cm_scacheLock);
401                 cm_InvalidateACLUser(scp, userp);
402                 lock_ObtainRead(&cm_scacheLock);
403                 cm_ReleaseSCacheNoLock(scp);
404             }
405         }
406     }
407     lock_ReleaseRead(&cm_scacheLock);
408
409     cm_EAccesClearUserEntries(userp, cellp->cellID);
410
411     if (RDR_Initialized) {
412         lock_ObtainRead(&cm_volumeLock);
413         for (hash = 0; hash < cm_data.volumeHashTableSize; hash++) {
414             for ( volp = cm_data.volumeRWIDHashTablep[hash]; volp; volp = nextVolp) {
415                 nextVolp = volp->vol[RWVOL].nextp;
416                 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
417                     volp->vol[RWVOL].ID) {
418                     lock_ReleaseRead(&cm_volumeLock);
419                     RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[RWVOL].ID, AFS_INVALIDATE_CREDS);
420                     lock_ObtainRead(&cm_volumeLock);
421                 }
422             }
423             for ( volp = cm_data.volumeROIDHashTablep[hash]; volp; volp = nextVolp) {
424                 nextVolp = volp->vol[ROVOL].nextp;
425                 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
426                     volp->vol[ROVOL].ID) {
427                     lock_ReleaseRead(&cm_volumeLock);
428                     RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[ROVOL].ID, AFS_INVALIDATE_CREDS);
429                     lock_ObtainRead(&cm_volumeLock);
430                 }
431             }
432             for ( volp = cm_data.volumeBKIDHashTablep[hash]; volp; volp = nextVolp) {
433                 nextVolp = volp->vol[BACKVOL].nextp;
434                 if ((cellp == NULL || cellp->cellID == volp->cellp->cellID) &&
435                     volp->vol[BACKVOL].ID) {
436                     lock_ReleaseRead(&cm_volumeLock);
437                     RDR_InvalidateVolume(volp->cellp->cellID, volp->vol[BACKVOL].ID, AFS_INVALIDATE_CREDS);
438                     lock_ObtainRead(&cm_volumeLock);
439                 }
440             }
441         }
442         lock_ReleaseRead(&cm_volumeLock);
443     }
444 }
445
446