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