e9879254595d7bbbfdbce80f2f59a5360cef35e8
[openafs.git] / src / afs / afs_osi.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"       /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h"   /* afs statistics */
14 #ifdef AFS_AIX_ENV
15 #include <sys/adspace.h>        /* for vm_att(), vm_det() */
16 #endif
17
18 static char memZero;                    /* address of 0 bytes for kmem_alloc */
19 extern int afs_osicred_initialized;
20
21 struct osimem {
22     struct osimem *next;
23 };
24
25 /* osi_Init -- do once per kernel installation initialization.
26  *     -- On Solaris this is called from modload initialization.
27  *     -- On AIX called from afs_config.
28  *     -- On HP called from afsc_link.
29  *     -- On SGI called from afs_init. */
30
31 #ifdef AFS_SGI53_ENV
32 lock_t afs_event_lock;
33 #endif
34
35 #ifdef AFS_SGI64_ENV
36 flid_t osi_flid;
37 #endif
38
39 void osi_Init()
40 {
41     static int once = 0;
42     if (once++ > 0)                     /* just in case */
43         return;
44 #if     defined(AFS_HPUX_ENV)
45     osi_InitGlock();
46 #else   /* AFS_HPUX_ENV */
47 #if defined(AFS_GLOBAL_SUNLOCK)
48 #if defined(AFS_SGI62_ENV)
49     mutex_init(&afs_global_lock, MUTEX_DEFAULT, "afs_global_lock");
50 #elif defined(AFS_OSF_ENV)
51     usimple_lock_init(&afs_global_lock);
52     afs_global_owner = (thread_t)0;
53 #elif defined(AFS_DARWIN_ENV)
54     lockinit(&afs_global_lock, PLOCK, "afs global lock", 0, 0);
55     afs_global_owner = (thread_t)0;
56 #elif defined(AFS_AIX41_ENV)
57     lock_alloc((void*)&afs_global_lock, LOCK_ALLOC_PIN, 1, 1);
58     simple_lock_init((void *)&afs_global_lock);
59 #else
60 #ifndef AFS_LINUX22_ENV
61      /* Linux initialization in osi directory. Should move the others. */
62     mutex_init(&afs_global_lock, "afs_global_lock", MUTEX_DEFAULT, NULL);
63 #endif
64 #endif
65     /* afs_rxglobal_lock is initialized in rx_Init. */
66 #endif
67 #endif  /* AFS_HPUX_ENV */
68
69         if ( !afs_osicred_initialized )
70         {
71                 bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
72                 crhold(&afs_osi_cred);      /* don't let it evaporate */
73                 afs_osicred_initialized = 1;
74         }
75 #ifdef AFS_SGI64_ENV
76      osi_flid.fl_pid = osi_flid.fl_sysid = 0;
77 #endif
78 }
79
80 osi_Active(avc)
81 register struct vcache *avc; {
82     AFS_STATCNT(osi_Active);
83 #if defined(AFS_SUN_ENV) || defined(AFS_AIX_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV) || (AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV)
84     if ((avc->opens > 0) || (avc->states & CMAPPED))    return 1;   /* XXX: Warning, verify this XXX  */
85 #else
86 #if     defined(AFS_MACH_ENV)
87     if (avc->opens > 0 || ((avc->v.v_flag & VTEXT) && !inode_uncache_try(avc))) return 1;
88 #else
89 #if defined(AFS_SGI_ENV)
90     if ((avc->opens > 0) || AFS_VN_MAPPED((struct vnode *)avc))
91         return 1;
92 #else
93     if (avc->opens > 0 || (avc->v.v_flag & VTEXT)) return(1);
94 #endif
95 #endif /* AFS_MACH_ENV */
96 #endif
97     return 0;
98 }
99
100 /* this call, unlike osi_FlushText, is supposed to discard caches that may
101    contain invalid information if a file is written remotely, but that may
102    contain valid information that needs to be written back if the file is
103    being written locally.  It doesn't subsume osi_FlushText, since the latter
104    function may be needed to flush caches that are invalidated by local writes.
105
106    avc->pvnLock is already held, avc->lock is guaranteed not to be held (by
107    us, of course).
108 */
109 void osi_FlushPages(avc, credp)
110     register struct vcache *avc; 
111     struct AFS_UCRED *credp;    
112 {
113     afs_hyper_t origDV;
114     ObtainReadLock(&avc->lock);
115     /* If we've already purged this version, or if we're the ones
116        writing this version, don't flush it (could lose the
117        data we're writing). */
118     if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0) || 
119         ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
120         ReleaseReadLock(&avc->lock);
121         return;
122     }
123     ReleaseReadLock(&avc->lock);
124     ObtainWriteLock(&avc->lock,10);
125     /* Check again */
126     if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0) || 
127         ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
128         ReleaseWriteLock(&avc->lock);
129         return;
130     }
131     if (hiszero(avc->mapDV)) {
132         hset(avc->mapDV, avc->m.DataVersion);
133         ReleaseWriteLock(&avc->lock);
134         return;
135     }
136
137     AFS_STATCNT(osi_FlushPages);
138     hset(origDV, avc->m.DataVersion);
139     afs_Trace3(afs_iclSetp, CM_TRACE_FLUSHPAGES, ICL_TYPE_POINTER, avc,
140                    ICL_TYPE_INT32, origDV.low, ICL_TYPE_INT32, avc->m.Length);
141
142     ReleaseWriteLock(&avc->lock);
143     AFS_GUNLOCK();
144     osi_VM_FlushPages(avc, credp);
145     AFS_GLOCK();
146     ObtainWriteLock(&avc->lock,88);
147
148     /* do this last, and to original version, since stores may occur
149        while executing above PUTPAGE call */
150     hset(avc->mapDV, origDV);
151     ReleaseWriteLock(&avc->lock);
152 }
153
154 afs_lock_t afs_ftf;             /* flush text lock */
155
156 #ifdef  AFS_TEXT_ENV
157
158 /* This call is supposed to flush all caches that might be invalidated
159  * by either a local write operation or a write operation done on
160  * another client.  This call may be called repeatedly on the same
161  * version of a file, even while a file is being written, so it
162  * shouldn't do anything that would discard newly written data before
163  * it is written to the file system. */
164
165 void osi_FlushText_really(vp)
166     register struct vcache *vp; {
167     afs_hyper_t fdv;    /* version before which we'll flush */
168
169     AFS_STATCNT(osi_FlushText);
170     /* see if we've already flushed this data version */
171     if (hcmp(vp->m.DataVersion, vp->flushDV) <= 0) return;
172
173 #ifdef AFS_DEC_ENV 
174     {
175       void afs_gfs_FlushText();
176       afs_gfs_FlushText(vp);
177       return;
178     }
179 #else
180
181     MObtainWriteLock(&afs_ftf,317);
182     hset(fdv, vp->m.DataVersion);
183
184     /* why this disgusting code below?  
185      *    xuntext, called by xrele, doesn't notice when it is called 
186      * with a freed text object.  Sun continually calls xrele or xuntext
187      * without any locking, as long as VTEXT is set on the
188      * corresponding vnode.  
189      *    But, if the text object is locked when you check the VTEXT
190      * flag, several processes can wait in xuntext, waiting for the
191      * text lock; when the second one finally enters xuntext's
192      * critical region, the text object is already free, but the check
193      * was already done by xuntext's caller.
194      *    Even worse, it turns out that xalloc locks the text object
195      * before reading or stating a file via the vnode layer.  Thus, we
196      * could end up in getdcache, being asked to bring in a new
197      * version of a file, but the corresponding text object could be
198      * locked.  We can't flush the text object without causing
199      * deadlock, so now we just don't try to lock the text object
200      * unless it is guaranteed to work.  And we try to flush the text
201      * when we need to a bit more often at the vnode layer.  Sun
202      * really blew the vm-cache flushing interface.
203      */
204
205 #if defined (AFS_HPUX_ENV)
206     if (vp->v.v_flag & VTEXT) {
207         xrele(vp);  
208
209         if (vp->v.v_flag & VTEXT) {     /* still has a text object? */
210             MReleaseWriteLock(&afs_ftf);
211             return;
212         }
213     }
214 #endif
215
216     /* next do the stuff that need not check for deadlock problems */
217     mpurge(vp);
218
219     /* finally, record that we've done it */
220     hset(vp->flushDV, fdv);
221     MReleaseWriteLock(&afs_ftf);
222
223 #endif /* AFS_DEC_ENV */
224 }
225
226 #ifdef AFS_DEC_ENV
227 /* I don't really like using xinval() here, because it kills processes
228  * a bit aggressively.  Previous incarnations of this functionality
229  * used to use xrele() instead of xinval, and didn't invoke
230  * cacheinval().  But they would panic.  So it might be worth looking
231  * into some middle ground...
232  */
233 static void
234 afs_gfs_FlushText(vp)
235     register struct vcache *vp; {
236     afs_hyper_t fdv;    /* version before which we'll flush */
237     register struct text *xp;
238     struct gnode * gp;
239
240     MObtainWriteLock(&afs_ftf,318);
241     hset(fdv, vp->m.DataVersion);
242     gp = afs_vntogn(vp);
243
244     if (!gp) {
245       /* this happens frequently after cores are created. */
246       MReleaseWriteLock(&afs_ftf);
247       return;
248     }
249
250     if (gp->g_flag & GTEXT) {
251         if (gp->g_textp) {
252           xp = (struct text *) gp->g_textp ;
253           /* if text object is locked, give up */
254           if (xp && (xp->x_flag & XLOCK)) {
255             MReleaseWriteLock(&afs_ftf);
256             return;
257           }
258         }
259         else xp = (struct text *) 0;
260
261         if (gp->g_flag & GTEXT) {/* still has a text object? */
262           xinval(gp);
263           }
264     }
265
266     /* next do the stuff that need not check for deadlock problems */
267     /*    maybe xinval(gp); here instead of above */
268     binval(NODEV, gp);
269     cacheinval(gp);
270     /* finally, record that we've done it */
271     hset(vp->flushDV, fdv);
272
273     MReleaseWriteLock(&afs_ftf);
274 }
275 #endif /* AFS_DEC_ENV */
276
277 #endif /* AFS_TEXT_ENV */
278
279 /* procedure for making our processes as invisible as we can */
280 void afs_osi_Invisible() {
281 #ifndef AFS_AIX32_ENV
282     /* called once per "kernel" lwp to make it invisible */
283 #ifdef AFS_DEC_ENV
284     u.u_procp->p_type |= SSYS;
285 #else
286 #if     defined(AFS_SUN5_ENV)
287     curproc->p_flag |= SSYS;
288 #else
289 #if defined(AFS_SGI_ENV)
290     vrelvm();
291 #endif
292 #ifdef  AFS_SUN_ENV
293     relvm(u.u_procp);   /* release all the resources */
294 #endif
295 #if     defined(AFS_HPUX101_ENV)
296     set_system_proc(u.u_procp);
297 #else
298 #if defined(AFS_DARWIN_ENV)
299     current_proc()->p_flag |= P_SYSTEM;
300 #else
301 #if !defined(AFS_SGI64_ENV) && !defined(AFS_LINUX20_ENV)
302     u.u_procp->p_flag |= SSYS;
303 #endif /* AFS_SGI64_ENV */
304 #endif
305 #endif
306 #endif
307 #endif
308 #endif
309     AFS_STATCNT(osi_Invisible);
310 }
311
312
313 #ifndef AFS_LINUX20_ENV /* Linux version in osi_misc.c */
314 /* set the real time */
315 afs_osi_SetTime(atv)
316     register osi_timeval_t *atv; {
317
318 #ifdef  AFS_AIX32_ENV
319     struct timestruc_t t;
320
321     t.tv_sec  = atv->tv_sec;
322     t.tv_nsec = atv->tv_usec * 1000;
323     ksettimer(&t);              /*  Was -> settimer(TIMEOFDAY, &t); */
324 #else
325 #ifdef AFS_SUN55_ENV
326     stime(atv->tv_sec);
327 #else
328 #ifdef  AFS_SUN5_ENV
329     /*
330      * To get more than second resolution we can use adjtime. The problem
331      * is that the usecs from the server are wrong (by now) so it isn't
332      * worth complicating the following code.
333      */
334     struct stimea {
335         time_t time;
336     } sta;
337     extern int stime(struct stimea *time, rval_t *rvp);
338
339     sta.time = atv->tv_sec;
340
341     stime(&sta, NULL);
342 #else
343 #if defined(AFS_SGI_ENV)
344     struct stimea {
345         sysarg_t time;
346     } sta;
347     extern int stime(struct stimea *time);
348
349     AFS_GUNLOCK();
350     sta.time = atv->tv_sec;
351     stime(&sta);
352     AFS_GLOCK();
353 #else
354 #ifdef AFS_DARWIN_ENV
355     AFS_GUNLOCK();
356     setthetime(atv);
357     AFS_GLOCK();
358 #else
359     /* stolen from kern_time.c */
360 #ifndef AFS_AUX_ENV
361     boottime.tv_sec += atv->tv_sec - time.tv_sec;
362 #endif
363 #ifdef AFS_HPUX_ENV
364     {
365     register ulong_t s;
366     struct timeval t;
367     t.tv_sec = atv->tv_sec;
368     t.tv_usec = atv->tv_usec;
369     s = spl7(); time = t; (void) splx(s);
370     resettodr(atv);
371     }
372 #else
373     {
374     register int s;
375     s = splclock(); time = *atv; (void) splx(s);
376     }
377     resettodr();
378 #endif
379 #ifdef  AFS_AUX_ENV
380     logtchg(atv->tv_sec);
381 #endif
382 #endif  /* AFS_DARWIN_ENV */
383 #endif  /* AFS_SGI_ENV */
384 #endif /* AFS_SUN55_ENV */
385 #endif /* AFS_SUN5_ENV */
386 #endif /* AFS_AIX32_ENV */
387     AFS_STATCNT(osi_SetTime);
388     return 0;
389 }
390 #endif /* AFS_LINUX20_ENV */
391
392
393 void *afs_osi_Alloc(size_t x)
394 {
395     register struct osimem *tm = NULL;
396     register int size;
397
398     AFS_STATCNT(osi_Alloc);
399     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
400        things so that NULL returned iff an error occurred */
401     if (x == 0) return &memZero;
402
403     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
404     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
405 #ifdef AFS_LINUX20_ENV
406     return osi_linux_alloc(x);
407 #else
408     size = x;
409     tm = (struct osimem *) AFS_KALLOC(size);
410 #ifdef  AFS_SUN_ENV
411     if (!tm)
412         osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
413                   size);
414 #endif
415     return (char *) tm;
416 #endif
417 }
418
419 #if     defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
420
421 void *afs_osi_Alloc_NoSleep(size_t x)
422 {
423     register struct osimem *tm;
424     register int size;
425
426     AFS_STATCNT(osi_Alloc);
427     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
428        things so that NULL returned iff an error occurred */
429     if (x == 0) return &memZero;
430
431     size = x;
432     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
433     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
434     tm = (struct osimem *) AFS_KALLOC_NOSLEEP(size);
435     return (char *) tm;
436 }
437
438 #endif  /* SUN || SGI */
439
440 void afs_osi_Free(void *x, size_t asize)
441 {
442     register struct osimem *tm, **lm, *um;
443
444     AFS_STATCNT(osi_Free);
445     if (x == &memZero) return;  /* check for putting memZero back */
446
447     AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
448     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
449 #ifdef AFS_LINUX20_ENV
450     osi_linux_free(x);
451 #else
452     AFS_KFREE((struct osimem *)x, asize);
453 #endif
454 }
455
456
457 /* ? is it moderately likely that there are dirty VM pages associated with 
458  * this vnode?
459  *
460  *  Prereqs:  avc must be write-locked
461  *
462  *  System Dependencies:  - *must* support each type of system for which 
463  *                          memory mapped files are supported, even if all 
464  *                          it does is return TRUE;
465  *
466  * NB:  this routine should err on the side of caution for ProcessFS to work
467  *      correctly (or at least, not to introduce worse bugs than already exist)
468  */
469 #ifdef  notdef
470 int
471 osi_VMDirty_p(avc)
472      struct vcache *avc;
473 {
474     int dirtyPages;
475
476     if (avc->execsOrWriters <= 0)
477         return 0;         /* can't be many dirty pages here, I guess */
478     
479 #if defined (AFS_AIX32_ENV) 
480 #ifdef  notdef
481     /* because of the level of hardware involvment with VM and all the
482      * warnings about "This routine must be called at VMM interrupt
483      * level", I thought it would be safest to disable interrupts while
484      * looking at the software page fault table.  */
485
486     /* convert vm handle into index into array:  I think that stoinio is 
487      * always zero...  Look into this XXX  */
488 #define VMHASH(handle) ( \
489                         ( ((handle) & ~vmker.stoinio)  \
490                          ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
491                          ) & 0x000fffff) 
492
493     if (avc->vmh) {
494         unsigned int pagef, pri, index, next;
495         extern struct vmkerdata vmker;
496
497         index = VMHASH(avc->vmh);
498         if (scb_valid(index)) {  /* could almost be an ASSERT */
499
500             pri = disable_ints();
501             for (pagef = scb_sidlist(index); pagef >= 0; pagef = next) {
502                 next = pft_sidfwd(pagef);
503                 if (pft_modbit(pagef)) {  /* has page frame been modified? */
504                     enable_ints(pri);
505                     return 1;
506                 }
507             }
508             enable_ints(pri);
509         }
510     }
511 #undef VMHASH
512 #endif
513 #endif /* AFS_AIX32_ENV */
514
515 #if defined (AFS_SUN_ENV)
516     if (avc->states & CMAPPED) {
517         struct page * pg;
518         for (pg = avc->v.v_s.v_Pages ; pg ; pg = pg->p_vpnext) {
519             if (pg->p_mod) {
520                 return 1;
521             }
522         }
523     }
524 #endif
525 return 0;
526 }
527 #endif /* notdef */
528
529
530 /*
531  * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
532  * This leads to bad races when osi_ReleaseVM() is called from
533  * afs_InvalidateAllSegments().
534
535  * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
536  * vcache entry lock is held or not.
537  *
538  * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
539  * there may be cases where it does care.  If so, it would be good to fix
540  * them so they don't care.  Until then, we assume the worst.
541  *
542  * Locking:  the vcache entry lock is held.  It is dropped and re-obtained.
543  */
544 void
545 osi_ReleaseVM(avc, acred)
546     struct vcache *avc;
547     struct AFS_UCRED *acred;
548 {
549 #ifdef  AFS_SUN5_ENV
550     AFS_GUNLOCK();
551     osi_VM_Truncate(avc, 0, acred);
552     AFS_GLOCK();
553 #else
554     ReleaseWriteLock(&avc->lock);
555     AFS_GUNLOCK();
556     osi_VM_Truncate(avc, 0, acred);
557     AFS_GLOCK();
558     ObtainWriteLock(&avc->lock, 80);
559 #endif
560 }
561
562
563 void shutdown_osi()
564 {
565     extern int afs_cold_shutdown;
566
567     AFS_STATCNT(shutdown_osi);
568     if (afs_cold_shutdown) {
569         LOCK_INIT(&afs_ftf, "afs_ftf"); 
570       }
571 }
572
573 afs_osi_suser(credp) 
574   void * credp;
575 {
576 #ifdef AFS_SUN5_ENV
577   return afs_suser(credp);
578 #else
579   return afs_suser();
580 #endif
581 }
582
583 #if AFS_GCPAGS
584
585 /* afs_osi_TraverseProcTable() - Walk through the systems process
586  * table, calling afs_GCPAGs_perproc_func() for each process.
587  */
588
589 #if defined(AFS_SUN5_ENV)
590 void afs_osi_TraverseProcTable()
591 {
592     struct proc *prp;
593     for (prp = practive; prp != NULL; prp = prp->p_next) {
594         afs_GCPAGs_perproc_func(prp);
595     }
596 }
597 #endif
598
599 #if defined(AFS_HPUX_ENV)
600
601 /*
602  * NOTE: h/proc_private.h gives the process table locking rules
603  * It indicates that access to p_cred must be protected by
604  * mp_mtproc_lock(p);
605  * mp_mtproc_unlock(p);
606  *
607  * The code in sys/pm_prot.c uses pcred_lock() to protect access to
608  * the process creds, and uses mp_mtproc_lock() only for audit-related
609  * changes.  To be safe, we use both.
610  */
611
612 void afs_osi_TraverseProcTable()
613 {
614     register proc_t *p;
615     int endchain = 0;
616
617     MP_SPINLOCK(activeproc_lock);
618     MP_SPINLOCK(sched_lock);
619     pcred_lock();
620
621     /*
622      * Instead of iterating through all of proc[], traverse only
623      * the list of active processes.  As an example of this,
624      * see foreach_process() in sys/vm_sched.c.
625      *
626      * We hold the locks for the entire scan in order to get a
627      * consistent view of the current set of creds.
628      */
629
630     for(p = proc; endchain == 0; p = &proc[p->p_fandx]) {
631         if (p->p_fandx == 0) {
632             endchain = 1;
633         }
634
635         if (system_proc(p))
636             continue;
637
638         mp_mtproc_lock(p);
639         afs_GCPAGs_perproc_func(p);
640         mp_mtproc_unlock(p);
641     }
642
643     pcred_unlock();
644     MP_SPINUNLOCK(sched_lock);
645     MP_SPINUNLOCK(activeproc_lock);
646 }
647 #endif
648
649 #if defined(AFS_SGI_ENV)
650
651 #ifdef AFS_SGI65_ENV
652 /* TODO: Fix this later. */
653 static int SGI_ProcScanFunc(void *p, void *arg, int mode)
654 {
655     return 0;
656 }
657 #else   /* AFS_SGI65_ENV */
658 static int SGI_ProcScanFunc(proc_t *p, void *arg, int mode) 
659 {
660     afs_int32 (*perproc_func)(struct proc *) = arg;
661     int code=0;
662     /* we pass in the function pointer for arg,
663      * mode ==0 for startup call, ==1 for each valid proc, 
664      * and ==2 for terminate call.
665      */
666     if(mode == 1) {
667         code = perproc_func(p);
668     }
669     return code;
670 }
671 #endif  /* AFS_SGI65_ENV */
672
673 void afs_osi_TraverseProcTable()
674 {
675     procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
676 }
677 #endif  /* AFS_SGI_ENV */
678
679 #if defined(AFS_AIX_ENV)
680 void afs_osi_TraverseProcTable()
681 {
682     struct proc *p;
683     int i;
684
685     /*
686      * For binary compatibility, on AIX we need to be careful to use the
687      * proper size of a struct proc, even if it is different from what
688      * we were compiled with.
689      */
690     if (!afs_gcpags_procsize)
691         return;
692
693     simple_lock(&proc_tbl_lock);
694     for (p = (struct proc *)v.vb_proc, i = 0;
695          p < max_proc;
696          p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
697
698         if (p->p_stat == SNONE)
699             continue;
700         if (p->p_stat == SIDL)
701             continue;
702         if (p->p_stat == SEXIT)
703             continue;
704
705         /* sanity check */
706
707         if (PROCMASK(p->p_pid) != i) {
708             afs_gcpags = AFS_GCPAGS_EPIDCHECK;
709             break;
710         }
711
712         /* sanity check */
713
714         if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
715             afs_gcpags = AFS_GCPAGS_ENICECHECK;
716             break;
717         }
718
719         afs_GCPAGs_perproc_func(p);
720     }
721     simple_unlock(&proc_tbl_lock);
722 }
723 #endif
724
725 #if defined(AFS_OSF_ENV)
726 void afs_osi_TraverseProcTable()
727 {
728     struct pid_entry *pe;
729 #ifdef AFS_DUX50_ENV
730 extern struct pid_entry *pidtab;
731 extern int npid; 
732 #define pidNPID (pidtab + npid)
733 #define PID_LOCK()
734 #define PID_UNLOCK()
735 #endif
736     PID_LOCK();
737     for (pe = pidtab; pe < pidNPID; ++pe) {
738        if (pe->pe_proc != PROC_NULL)
739           afs_GCPAGs_perproc_func(pe->pe_proc);
740     }
741     PID_UNLOCK();
742 }
743 #endif
744
745 #if defined(AFS_DARWIN_ENV)
746 void afs_osi_TraverseProcTable()
747 {   
748     struct proc *p;
749     LIST_FOREACH(p, &allproc, p_list) {
750         if (p->p_stat == SIDL)
751             continue;
752         if (p->p_stat == SZOMB)
753             continue;
754         if (p->p_flag & P_SYSTEM)
755             continue;
756           afs_GCPAGs_perproc_func(p);
757     }
758 }   
759 #endif
760
761 /* return a pointer (sometimes a static copy ) to the cred for a
762  * given AFS_PROC.
763  * subsequent calls may overwrite the previously returned value.
764  */
765
766 #if defined(AFS_SGI65_ENV)
767 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
768 {
769     return NULL;
770 }
771 #elif defined(AFS_HPUX_ENV)
772 const struct AFS_UCRED *afs_osi_proc2cred(proc_t *p)
773 {
774     if (!p)
775         return;
776
777     /*
778      * Cannot use afs_warnuser() here, as the code path
779      * eventually wants to grab sched_lock, which is
780      * already held here
781      */
782
783     return p_cred(p);
784 }
785 #elif defined(AFS_AIX_ENV)
786
787 /* GLOBAL DECLARATIONS */
788
789 extern int      xmattach();        /* fills out cross memory descriptor */
790 extern int      xmdetach();        /* decrements reference count to segment */
791
792 /*
793  * LOCKS: the caller must do
794  *  simple_lock(&proc_tbl_lock);
795  *  simple_unlock(&proc_tbl_lock);
796  * around calls to this function.
797  */
798
799 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pproc)
800 {
801     struct AFS_UCRED *pcred = 0;
802
803     /*
804      * pointer to process user structure valid in *our*
805      * address space
806      *
807      * The user structure for a process is stored in the user
808      * address space (as distinct from the kernel address
809      * space), and so to refer to the user structure of a
810      * different process we must employ special measures.
811      *
812      * I followed the example used in the AIX getproc() system
813      * call in bos/kernel/proc/getproc.c
814      */
815     struct user *xmem_userp;
816
817     struct xmem dp;             /* ptr to xmem descriptor */
818     int xm;                     /* xmem result */
819
820     if (!pproc) {
821         return pcred;
822     }
823
824     /*
825      * The process private segment in which the user
826      * area is located may disappear. We need to increment
827      * its use count. Therefore we
828      *    - get the proc_tbl_lock to hold the segment.
829      *    - get the p_lock to lockout vm_cleardata.
830      *    - vm_att to load the segment register (no check)
831      *    - xmattach to bump its use count.
832      *    - release the p_lock.
833      *    - release the proc_tbl_lock.
834      *    - do whatever we need.
835      *    - xmdetach to decrement the use count.
836      *    - vm_det to free the segment register (no check)
837      */
838
839     xmem_userp = NULL;
840     xm = XMEM_FAIL;
841     /* simple_lock(&proc_tbl_lock); */
842     if (pproc->p_adspace != NULLSEGVAL) {
843
844         simple_lock(&pproc->p_lock);
845
846         if (pproc->p_threadcount &&
847             pproc->p_threadlist) {
848
849             /*
850              * arbitrarily pick the first thread in pproc
851              */
852             struct thread *pproc_thread =
853                 pproc->p_threadlist;
854
855             /*
856              * location of 'struct user' in pproc's
857              * address space
858              */
859             struct user *pproc_userp =
860                 pproc_thread->t_userp;
861
862             /*
863              * create a pointer valid in my own address space
864              */
865
866             xmem_userp =
867                 (struct user *)vm_att(pproc->p_adspace,
868                                       pproc_userp);
869
870             dp.aspace_id = XMEM_INVAL;
871             xm = xmattach(xmem_userp,
872                           sizeof(*xmem_userp),
873                           &dp, SYS_ADSPACE);
874         }
875
876         simple_unlock(&pproc->p_lock);
877     }
878     /* simple_unlock(&proc_tbl_lock); */
879     if (xm == XMEM_SUCC) {
880
881         static struct AFS_UCRED cred;
882
883         /*
884          * What locking should we use to protect access to the user
885          * area?  If needed also change the code in AIX/osi_groups.c.
886          */
887
888         /* copy cred to local address space */
889         cred = *xmem_userp->U_cred;
890         pcred = &cred;
891
892         xmdetach(&dp);
893     }
894     if (xmem_userp) {
895         vm_det((void *)xmem_userp);
896     }
897
898     return pcred;
899 }
900
901 #elif defined(AFS_OSF_ENV)
902 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
903 {
904     struct AFS_UCRED *rv=NULL;
905
906     if(pr == NULL) {
907        return NULL;
908     }
909
910     if((pr->p_stat == SSLEEP) ||
911        (pr->p_stat == SRUN) ||
912        (pr->p_stat == SSTOP)) 
913        rv = pr->p_rcred;
914
915     return rv;
916 }
917 #elif defined(AFS_DARWIN_ENV)
918 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
919 {   
920     struct AFS_UCRED *rv=NULL;
921     static struct AFS_UCRED cr;
922
923     if(pr == NULL) {
924        return NULL;
925     }
926    
927     if((pr->p_stat == SSLEEP) ||
928        (pr->p_stat == SRUN) ||
929        (pr->p_stat == SSTOP)) {
930        pcred_readlock(pr);
931        cr.cr_ref=1;
932        cr.cr_uid=pr->p_cred->pc_ucred->cr_uid;
933        cr.cr_ngroups=pr->p_cred->pc_ucred->cr_ngroups;
934        bcopy(pr->p_cred->pc_ucred->cr_groups, cr.cr_groups,NGROUPS *
935              sizeof(gid_t));
936        pcred_unlock(pr);
937        rv = &cr;
938     }
939     
940     return rv;
941 }  
942 #else
943 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
944 {
945     struct AFS_UCRED *rv=NULL;
946
947     if(pr == NULL) {
948        return NULL;
949     }
950     rv = pr->p_cred;          
951
952     return rv;
953 }
954 #endif
955
956 #endif  /* AFS_GCPAGS */