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