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