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