macos-104-not-yet-20050508
[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_DARWIN_ENV)
290     /* maybe call init_process instead? */
291     current_proc()->p_flag |= P_SYSTEM;
292 #elif defined(AFS_XBSD_ENV)
293     curproc->p_flag |= P_SYSTEM;
294 #elif defined(AFS_SGI_ENV)
295     vrelvm();
296 #endif
297
298     AFS_STATCNT(osi_Invisible);
299 }
300
301
302 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV)
303 /* set the real time */
304 void
305 afs_osi_SetTime(osi_timeval_t * atv)
306 {
307 #if defined(AFS_AIX32_ENV)
308     struct timestruc_t t;
309
310     t.tv_sec = atv->tv_sec;
311     t.tv_nsec = atv->tv_usec * 1000;
312     ksettimer(&t);              /*  Was -> settimer(TIMEOFDAY, &t); */
313 #elif defined(AFS_SUN55_ENV)
314     stime(atv->tv_sec);
315 #elif defined(AFS_SUN5_ENV)
316     /*
317      * To get more than second resolution we can use adjtime. The problem
318      * is that the usecs from the server are wrong (by now) so it isn't
319      * worth complicating the following code.
320      */
321     struct stimea {
322         time_t time;
323     } sta;
324
325     sta.time = atv->tv_sec;
326
327     stime(&sta, NULL);
328 #elif defined(AFS_SGI_ENV)
329     struct stimea {
330         sysarg_t time;
331     } sta;
332
333     AFS_GUNLOCK();
334     sta.time = atv->tv_sec;
335     stime(&sta);
336     AFS_GLOCK();
337 #elif defined(AFS_DARWIN_ENV)
338     AFS_GUNLOCK();
339     setthetime(atv);
340     AFS_GLOCK();
341 #else
342     /* stolen from kern_time.c */
343 #ifndef AFS_AUX_ENV
344     boottime.tv_sec += atv->tv_sec - time.tv_sec;
345 #endif
346 #ifdef AFS_HPUX_ENV
347     {
348 #if !defined(AFS_HPUX1122_ENV)
349         /* drop the setting of the clock for now. spl7 is not
350          * known on hpux11.22
351          */
352         register ulong_t s;
353         struct timeval t;
354         t.tv_sec = atv->tv_sec;
355         t.tv_usec = atv->tv_usec;
356         s = spl7();
357         time = t;
358         (void)splx(s);
359         resettodr(atv);
360 #endif
361     }
362 #else
363     {
364         register int s;
365         s = splclock();
366         time = *atv;
367         (void)splx(s);
368     }
369     resettodr();
370 #endif
371 #ifdef  AFS_AUX_ENV
372     logtchg(atv->tv_sec);
373 #endif
374 #endif /* AFS_DARWIN_ENV */
375     AFS_STATCNT(osi_SetTime);
376 }
377 #endif /* AFS_LINUX20_ENV */
378
379
380 void *
381 afs_osi_Alloc(size_t x)
382 {
383 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV)
384     register struct osimem *tm = NULL;
385     register int size;
386 #endif
387
388     AFS_STATCNT(osi_Alloc);
389     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
390      * things so that NULL returned iff an error occurred */
391     if (x == 0)
392         return &memZero;
393
394     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
395     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
396 #ifdef AFS_LINUX20_ENV
397     return osi_linux_alloc(x, 1);
398 #elif defined(AFS_FBSD_ENV)
399     return osi_fbsd_alloc(x, 1);
400 #else
401     size = x;
402     tm = (struct osimem *)AFS_KALLOC(size);
403 #ifdef  AFS_SUN5_ENV
404     if (!tm)
405         osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
406                   size);
407 #endif
408     return (void *)tm;
409 #endif
410 }
411
412 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
413
414 void *
415 afs_osi_Alloc_NoSleep(size_t x)
416 {
417     register struct osimem *tm;
418     register int size;
419
420     AFS_STATCNT(osi_Alloc);
421     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
422      * things so that NULL returned iff an error occurred */
423     if (x == 0)
424         return &memZero;
425
426     size = x;
427     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
428     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
429     tm = (struct osimem *)AFS_KALLOC_NOSLEEP(size);
430     return (void *)tm;
431 }
432
433 #endif /* SUN || SGI */
434
435 void
436 afs_osi_Free(void *x, size_t asize)
437 {
438     AFS_STATCNT(osi_Free);
439     if (x == &memZero)
440         return;                 /* check for putting memZero back */
441
442     AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
443     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
444 #if defined(AFS_LINUX20_ENV)
445     osi_linux_free(x);
446 #elif defined(AFS_FBSD_ENV)
447     osi_fbsd_free(x);
448 #else
449     AFS_KFREE((struct osimem *)x, asize);
450 #endif
451 }
452
453 void
454 afs_osi_FreeStr(char *x)
455 {
456     afs_osi_Free(x, strlen(x) + 1);
457 }
458
459 /* ? is it moderately likely that there are dirty VM pages associated with
460  * this vnode?
461  *
462  *  Prereqs:  avc must be write-locked
463  *
464  *  System Dependencies:  - *must* support each type of system for which
465  *                          memory mapped files are supported, even if all
466  *                          it does is return TRUE;
467  *
468  * NB:  this routine should err on the side of caution for ProcessFS to work
469  *      correctly (or at least, not to introduce worse bugs than already exist)
470  */
471 #ifdef  notdef
472 int
473 osi_VMDirty_p(struct vcache *avc)
474 {
475     int dirtyPages;
476
477     if (avc->execsOrWriters <= 0)
478         return 0;               /* can't be many dirty pages here, I guess */
479
480 #if defined (AFS_AIX32_ENV)
481 #ifdef  notdef
482     /* because of the level of hardware involvment with VM and all the
483      * warnings about "This routine must be called at VMM interrupt
484      * level", I thought it would be safest to disable interrupts while
485      * looking at the software page fault table.  */
486
487     /* convert vm handle into index into array:  I think that stoinio is
488      * always zero...  Look into this XXX  */
489 #define VMHASH(handle) ( \
490                         ( ((handle) & ~vmker.stoinio)  \
491                          ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
492                          ) & 0x000fffff)
493
494     if (avc->segid) {
495         unsigned int pagef, pri, index, next;
496
497         index = VMHASH(avc->segid);
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_SUN5_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(struct vcache *avc, struct AFS_UCRED *acred)
546 {
547 #ifdef  AFS_SUN5_ENV
548     AFS_GUNLOCK();
549     osi_VM_Truncate(avc, 0, acred);
550     AFS_GLOCK();
551 #else
552     ReleaseWriteLock(&avc->lock);
553     AFS_GUNLOCK();
554     osi_VM_Truncate(avc, 0, acred);
555     AFS_GLOCK();
556     ObtainWriteLock(&avc->lock, 80);
557 #endif
558 }
559
560
561 void
562 shutdown_osi(void)
563 {
564     AFS_STATCNT(shutdown_osi);
565     if (afs_cold_shutdown) {
566         LOCK_INIT(&afs_ftf, "afs_ftf");
567     }
568 }
569
570 #ifndef AFS_OBSD_ENV
571 int
572 afs_osi_suser(void *credp)
573 {
574 #if defined(AFS_SUN5_ENV)
575     return afs_suser(credp);
576 #else
577     return afs_suser(NULL);
578 #endif
579 }
580 #endif
581
582 #if AFS_GCPAGS
583
584 /* afs_osi_TraverseProcTable() - Walk through the systems process
585  * table, calling afs_GCPAGs_perproc_func() for each process.
586  */
587
588 #if defined(AFS_SUN5_ENV)
589 void
590 afs_osi_TraverseProcTable(void)
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
613 afs_osi_TraverseProcTable(void)
614 {
615     register proc_t *p;
616     int endchain = 0;
617
618     MP_SPINLOCK(activeproc_lock);
619     MP_SPINLOCK(sched_lock);
620     pcred_lock();
621
622     /*
623      * Instead of iterating through all of proc[], traverse only
624      * the list of active processes.  As an example of this,
625      * see foreach_process() in sys/vm_sched.c.
626      *
627      * We hold the locks for the entire scan in order to get a
628      * consistent view of the current set of creds.
629      */
630
631     for (p = proc; endchain == 0; p = &proc[p->p_fandx]) {
632         if (p->p_fandx == 0) {
633             endchain = 1;
634         }
635
636         if (system_proc(p))
637             continue;
638
639         mp_mtproc_lock(p);
640         afs_GCPAGs_perproc_func(p);
641         mp_mtproc_unlock(p);
642     }
643
644     pcred_unlock();
645     MP_SPINUNLOCK(sched_lock);
646     MP_SPINUNLOCK(activeproc_lock);
647 }
648 #endif
649
650 #if defined(AFS_SGI_ENV)
651
652 #ifdef AFS_SGI65_ENV
653 /* TODO: Fix this later. */
654 static int
655 SGI_ProcScanFunc(void *p, void *arg, int mode)
656 {
657     return 0;
658 }
659 #else /* AFS_SGI65_ENV */
660 static int
661 SGI_ProcScanFunc(proc_t * p, void *arg, int mode)
662 {
663     afs_int32(*perproc_func) (struct proc *) = arg;
664     int code = 0;
665     /* we pass in the function pointer for arg,
666      * mode ==0 for startup call, ==1 for each valid proc,
667      * and ==2 for terminate call.
668      */
669     if (mode == 1) {
670         code = perproc_func(p);
671     }
672     return code;
673 }
674 #endif /* AFS_SGI65_ENV */
675
676 void
677 afs_osi_TraverseProcTable(void)
678 {
679     procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
680 }
681 #endif /* AFS_SGI_ENV */
682
683 #if defined(AFS_AIX_ENV)
684 #ifdef AFS_AIX51_ENV
685 #define max_proc v.ve_proc
686 #endif
687 void
688 afs_osi_TraverseProcTable(void)
689 {
690     struct proc *p;
691     int i;
692
693     /*
694      * For binary compatibility, on AIX we need to be careful to use the
695      * proper size of a struct proc, even if it is different from what
696      * we were compiled with.
697      */
698     if (!afs_gcpags_procsize)
699         return;
700
701 #ifndef AFS_AIX51_ENV
702     simple_lock(&proc_tbl_lock);
703 #endif
704     for (p = (struct proc *)v.vb_proc, i = 0; p < max_proc;
705          p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
706
707 #ifdef AFS_AIX51_ENV
708         if (p->p_pvprocp->pv_stat == SNONE)
709             continue;
710         if (p->p_pvprocp->pv_stat == SIDL)
711             continue;
712         if (p->p_pvprocp->pv_stat == SEXIT)
713             continue;
714 #else
715         if (p->p_stat == SNONE)
716             continue;
717         if (p->p_stat == SIDL)
718             continue;
719         if (p->p_stat == SEXIT)
720             continue;
721 #endif
722
723         /* sanity check */
724
725         if (PROCMASK(p->p_pid) != i) {
726             afs_gcpags = AFS_GCPAGS_EPIDCHECK;
727             break;
728         }
729
730         /* sanity check */
731
732         if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
733             afs_gcpags = AFS_GCPAGS_ENICECHECK;
734             break;
735         }
736
737         afs_GCPAGs_perproc_func(p);
738     }
739 #ifndef AFS_AIX51_ENV
740     simple_unlock(&proc_tbl_lock);
741 #endif
742 }
743 #endif
744
745 #if defined(AFS_OSF_ENV)
746
747 #ifdef AFS_DUX50_ENV
748 extern struct pid_entry *pidtab;
749 extern int npid; 
750 #endif
751
752 void
753 afs_osi_TraverseProcTable(void)
754 {
755     struct pid_entry *pe;
756 #ifdef AFS_DUX50_ENV
757 #define pidNPID (pidtab + npid)
758 #define PID_LOCK()
759 #define PID_UNLOCK()
760 #endif
761     PID_LOCK();
762     for (pe = pidtab; pe < pidNPID; ++pe) {
763         if (pe->pe_proc != PROC_NULL)
764             afs_GCPAGs_perproc_func(pe->pe_proc);
765     }
766     PID_UNLOCK();
767 }
768 #endif
769
770 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
771 void
772 afs_osi_TraverseProcTable(void)
773 {
774     struct proc *p;
775     LIST_FOREACH(p, &allproc, p_list) {
776         if (p->p_stat == SIDL)
777             continue;
778         if (p->p_stat == SZOMB)
779             continue;
780         if (p->p_flag & P_SYSTEM)
781             continue;
782         afs_GCPAGs_perproc_func(p);
783     }
784 }
785 #endif
786
787 #if defined(AFS_LINUX22_ENV)
788 extern rwlock_t tasklist_lock __attribute__((weak));
789 void
790 afs_osi_TraverseProcTable()
791 {
792     struct task_struct *p;
793     if (&tasklist_lock)
794        read_lock(&tasklist_lock);
795 #ifdef DEFINED_FOR_EACH_PROCESS
796     for_each_process(p) if (p->pid) {
797 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
798         if (p->exit_state)
799             continue;
800 #else
801         if (p->state & TASK_ZOMBIE)
802             continue;
803 #endif
804         afs_GCPAGs_perproc_func(p);
805     }
806 #else
807     for_each_task(p) if (p->pid) {
808 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
809         if (p->exit_state)
810             continue;
811 #else
812         if (p->state & TASK_ZOMBIE)
813             continue;
814 #endif
815         afs_GCPAGs_perproc_func(p);
816     }
817 #endif
818     if (&tasklist_lock)
819        read_unlock(&tasklist_lock);
820 }
821 #endif
822
823 /* return a pointer (sometimes a static copy ) to the cred for a
824  * given AFS_PROC.
825  * subsequent calls may overwrite the previously returned value.
826  */
827
828 #if defined(AFS_SGI65_ENV)
829 const struct AFS_UCRED *
830 afs_osi_proc2cred(AFS_PROC * p)
831 {
832     return NULL;
833 }
834 #elif defined(AFS_HPUX_ENV)
835 const struct AFS_UCRED *
836 afs_osi_proc2cred(AFS_PROC * p)
837 {
838     if (!p)
839         return;
840
841     /*
842      * Cannot use afs_warnuser() here, as the code path
843      * eventually wants to grab sched_lock, which is
844      * already held here
845      */
846
847     return p_cred(p);
848 }
849 #elif defined(AFS_AIX_ENV)
850
851 /* GLOBAL DECLARATIONS */
852
853 /*
854  * LOCKS: the caller must do
855  *  simple_lock(&proc_tbl_lock);
856  *  simple_unlock(&proc_tbl_lock);
857  * around calls to this function.
858  */
859
860 const struct AFS_UCRED *
861 afs_osi_proc2cred(AFS_PROC * pproc)
862 {
863     struct AFS_UCRED *pcred = 0;
864
865     /*
866      * pointer to process user structure valid in *our*
867      * address space
868      *
869      * The user structure for a process is stored in the user
870      * address space (as distinct from the kernel address
871      * space), and so to refer to the user structure of a
872      * different process we must employ special measures.
873      *
874      * I followed the example used in the AIX getproc() system
875      * call in bos/kernel/proc/getproc.c
876      */
877     struct user *xmem_userp;
878
879     struct xmem dp;             /* ptr to xmem descriptor */
880     int xm;                     /* xmem result */
881
882     if (!pproc) {
883         return pcred;
884     }
885
886     /*
887      * The process private segment in which the user
888      * area is located may disappear. We need to increment
889      * its use count. Therefore we
890      *    - get the proc_tbl_lock to hold the segment.
891      *    - get the p_lock to lockout vm_cleardata.
892      *    - vm_att to load the segment register (no check)
893      *    - xmattach to bump its use count.
894      *    - release the p_lock.
895      *    - release the proc_tbl_lock.
896      *    - do whatever we need.
897      *    - xmdetach to decrement the use count.
898      *    - vm_det to free the segment register (no check)
899      */
900
901     xmem_userp = NULL;
902     xm = XMEM_FAIL;
903     /* simple_lock(&proc_tbl_lock); */
904 #ifdef __64BIT__
905     if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
906 #else
907     if (pproc->p_adspace != NULLSEGVAL) {
908 #endif
909
910 #ifdef AFS_AIX51_ENV
911         simple_lock(&pproc->p_pvprocp->pv_lock);
912 #else
913         simple_lock(&pproc->p_lock);
914 #endif
915
916         if (pproc->p_threadcount &&
917 #ifdef AFS_AIX51_ENV
918             pproc->p_pvprocp->pv_threadlist) {
919 #else
920             pproc->p_threadlist) {
921 #endif
922
923             /*
924              * arbitrarily pick the first thread in pproc
925              */
926             struct thread *pproc_thread =
927 #ifdef AFS_AIX51_ENV
928                 pproc->p_pvprocp->pv_threadlist;
929 #else
930                 pproc->p_threadlist;
931 #endif
932
933             /*
934              * location of 'struct user' in pproc's
935              * address space
936              */
937             struct user *pproc_userp = pproc_thread->t_userp;
938
939             /*
940              * create a pointer valid in my own address space
941              */
942
943             xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
944
945             dp.aspace_id = XMEM_INVAL;
946             xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
947         }
948
949 #ifdef AFS_AIX51_ENV
950         simple_unlock(&pproc->p_pvprocp->pv_lock);
951 #else
952         simple_unlock(&pproc->p_lock);
953 #endif
954     }
955     /* simple_unlock(&proc_tbl_lock); */
956     if (xm == XMEM_SUCC) {
957
958         static struct AFS_UCRED cred;
959
960         /*
961          * What locking should we use to protect access to the user
962          * area?  If needed also change the code in AIX/osi_groups.c.
963          */
964
965         /* copy cred to local address space */
966         cred = *xmem_userp->U_cred;
967         pcred = &cred;
968
969         xmdetach(&dp);
970     }
971     if (xmem_userp) {
972         vm_det((void *)xmem_userp);
973     }
974
975     return pcred;
976 }
977
978 #elif defined(AFS_OSF_ENV)
979 const struct AFS_UCRED *
980 afs_osi_proc2cred(AFS_PROC * pr)
981 {
982     struct AFS_UCRED *rv = NULL;
983
984     if (pr == NULL) {
985         return NULL;
986     }
987
988     if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
989         || (pr->p_stat == SSTOP))
990         rv = pr->p_rcred;
991
992     return rv;
993 }
994 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
995 const struct AFS_UCRED *
996 afs_osi_proc2cred(AFS_PROC * pr)
997 {
998     struct AFS_UCRED *rv = NULL;
999     static struct AFS_UCRED cr;
1000
1001     if (pr == NULL) {
1002         return NULL;
1003     }
1004
1005     if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
1006         || (pr->p_stat == SSTOP)) {
1007         pcred_readlock(pr);
1008         cr.cr_ref = 1;
1009         cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
1010         cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
1011         memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
1012                NGROUPS * sizeof(gid_t));
1013         pcred_unlock(pr);
1014         rv = &cr;
1015     }
1016
1017     return rv;
1018 }
1019 #elif defined(AFS_LINUX22_ENV)
1020 const struct AFS_UCRED *
1021 afs_osi_proc2cred(AFS_PROC * pr)
1022 {
1023     struct AFS_UCRED *rv = NULL;
1024     static struct AFS_UCRED cr;
1025
1026     if (pr == NULL) {
1027         return NULL;
1028     }
1029
1030     if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
1031         || (pr->state == TASK_UNINTERRUPTIBLE)
1032         || (pr->state == TASK_STOPPED)) {
1033         cr.cr_ref = 1;
1034         cr.cr_uid = pr->uid;
1035 #if defined(AFS_LINUX26_ENV)
1036         get_group_info(pr->group_info);
1037         cr.cr_group_info = pr->group_info;
1038 #else
1039         cr.cr_ngroups = pr->ngroups;
1040         memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
1041 #endif
1042         rv = &cr;
1043     }
1044
1045     return rv;
1046 }
1047 #else
1048 const struct AFS_UCRED *
1049 afs_osi_proc2cred(AFS_PROC * pr)
1050 {
1051     struct AFS_UCRED *rv = NULL;
1052
1053     if (pr == NULL) {
1054         return NULL;
1055     }
1056     rv = pr->p_cred;
1057
1058     return rv;
1059 }
1060 #endif
1061
1062 #endif /* AFS_GCPAGS */