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