77bef23fe4de31f938d8ad95f0ddbfe1789c436b
[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 "../afs/param.h"       /* Should be always first */
11 #include <afsconfig.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 = (thread_t)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                 bzero((char *)&afs_osi_cred, 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((struct vnode *)avc))
95         return 1;
96 #else
97     if (avc->opens > 0 || (avc->v.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 /* procedure for making our processes as invisible as we can */
284 void afs_osi_Invisible() {
285 #ifndef AFS_AIX32_ENV
286     /* called once per "kernel" lwp to make it invisible */
287 #ifdef AFS_DEC_ENV
288     u.u_procp->p_type |= SSYS;
289 #else
290 #if     defined(AFS_SUN5_ENV)
291     curproc->p_flag |= SSYS;
292 #else
293 #if defined(AFS_SGI_ENV)
294     vrelvm();
295 #endif
296 #ifdef  AFS_SUN_ENV
297     relvm(u.u_procp);   /* release all the resources */
298 #endif
299 #if     defined(AFS_HPUX101_ENV)
300     set_system_proc(u.u_procp);
301 #else
302 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
303     current_proc()->p_flag |= P_SYSTEM;
304 #else
305 #if !defined(AFS_SGI64_ENV) && !defined(AFS_LINUX20_ENV)
306     u.u_procp->p_flag |= SSYS;
307 #endif /* AFS_SGI64_ENV */
308 #endif
309 #endif
310 #endif
311 #endif
312 #endif
313     AFS_STATCNT(osi_Invisible);
314 }
315
316
317 #ifndef AFS_LINUX20_ENV /* Linux version in osi_misc.c */
318 /* set the real time */
319 afs_osi_SetTime(atv)
320     register osi_timeval_t *atv; {
321
322 #ifdef  AFS_AIX32_ENV
323     struct timestruc_t t;
324
325     t.tv_sec  = atv->tv_sec;
326     t.tv_nsec = atv->tv_usec * 1000;
327     ksettimer(&t);              /*  Was -> settimer(TIMEOFDAY, &t); */
328 #else
329 #ifdef AFS_SUN55_ENV
330     stime(atv->tv_sec);
331 #else
332 #ifdef  AFS_SUN5_ENV
333     /*
334      * To get more than second resolution we can use adjtime. The problem
335      * is that the usecs from the server are wrong (by now) so it isn't
336      * worth complicating the following code.
337      */
338     struct stimea {
339         time_t time;
340     } sta;
341     extern int stime(struct stimea *time, rval_t *rvp);
342
343     sta.time = atv->tv_sec;
344
345     stime(&sta, NULL);
346 #else
347 #if defined(AFS_SGI_ENV)
348     struct stimea {
349         sysarg_t time;
350     } sta;
351     extern int stime(struct stimea *time);
352
353     AFS_GUNLOCK();
354     sta.time = atv->tv_sec;
355     stime(&sta);
356     AFS_GLOCK();
357 #else
358 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
359     AFS_GUNLOCK();
360     setthetime(atv);
361     AFS_GLOCK();
362 #else
363     /* stolen from kern_time.c */
364 #ifndef AFS_AUX_ENV
365     boottime.tv_sec += atv->tv_sec - time.tv_sec;
366 #endif
367 #ifdef AFS_HPUX_ENV
368     {
369     register ulong_t s;
370     struct timeval t;
371     t.tv_sec = atv->tv_sec;
372     t.tv_usec = atv->tv_usec;
373     s = spl7(); time = t; (void) splx(s);
374     resettodr(atv);
375     }
376 #else
377     {
378     register int s;
379     s = splclock(); time = *atv; (void) splx(s);
380     }
381     resettodr();
382 #endif
383 #ifdef  AFS_AUX_ENV
384     logtchg(atv->tv_sec);
385 #endif
386 #endif  /* AFS_DARWIN_ENV || AFS_FBSD_ENV */
387 #endif  /* AFS_SGI_ENV */
388 #endif /* AFS_SUN55_ENV */
389 #endif /* AFS_SUN5_ENV */
390 #endif /* AFS_AIX32_ENV */
391     AFS_STATCNT(osi_SetTime);
392     return 0;
393 }
394 #endif /* AFS_LINUX20_ENV */
395
396
397 void *afs_osi_Alloc(size_t x)
398 {
399     register struct osimem *tm = NULL;
400     register int size;
401
402     AFS_STATCNT(osi_Alloc);
403     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
404        things so that NULL returned iff an error occurred */
405     if (x == 0) return &memZero;
406
407     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
408     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
409 #ifdef AFS_LINUX20_ENV
410     return osi_linux_alloc(x);
411 #else
412     size = x;
413     tm = (struct osimem *) AFS_KALLOC(size);
414 #ifdef  AFS_SUN_ENV
415     if (!tm)
416         osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
417                   size);
418 #endif
419     return (char *) tm;
420 #endif
421 }
422
423 #if     defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
424
425 void *afs_osi_Alloc_NoSleep(size_t x)
426 {
427     register struct osimem *tm;
428     register int size;
429
430     AFS_STATCNT(osi_Alloc);
431     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
432        things so that NULL returned iff an error occurred */
433     if (x == 0) return &memZero;
434
435     size = x;
436     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
437     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
438     tm = (struct osimem *) AFS_KALLOC_NOSLEEP(size);
439     return (char *) tm;
440 }
441
442 #endif  /* SUN || SGI */
443
444 void afs_osi_Free(void *x, size_t asize)
445 {
446     register struct osimem *tm, **lm, *um;
447
448     AFS_STATCNT(osi_Free);
449     if (x == &memZero) return;  /* check for putting memZero back */
450
451     AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
452     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
453 #ifdef AFS_LINUX20_ENV
454     osi_linux_free(x);
455 #else
456     AFS_KFREE((struct osimem *)x, asize);
457 #endif
458 }
459
460
461 /* ? is it moderately likely that there are dirty VM pages associated with 
462  * this vnode?
463  *
464  *  Prereqs:  avc must be write-locked
465  *
466  *  System Dependencies:  - *must* support each type of system for which 
467  *                          memory mapped files are supported, even if all 
468  *                          it does is return TRUE;
469  *
470  * NB:  this routine should err on the side of caution for ProcessFS to work
471  *      correctly (or at least, not to introduce worse bugs than already exist)
472  */
473 #ifdef  notdef
474 int
475 osi_VMDirty_p(avc)
476      struct vcache *avc;
477 {
478     int dirtyPages;
479
480     if (avc->execsOrWriters <= 0)
481         return 0;         /* can't be many dirty pages here, I guess */
482     
483 #if defined (AFS_AIX32_ENV) 
484 #ifdef  notdef
485     /* because of the level of hardware involvment with VM and all the
486      * warnings about "This routine must be called at VMM interrupt
487      * level", I thought it would be safest to disable interrupts while
488      * looking at the software page fault table.  */
489
490     /* convert vm handle into index into array:  I think that stoinio is 
491      * always zero...  Look into this XXX  */
492 #define VMHASH(handle) ( \
493                         ( ((handle) & ~vmker.stoinio)  \
494                          ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
495                          ) & 0x000fffff) 
496
497     if (avc->vmh) {
498         unsigned int pagef, pri, index, next;
499         extern struct vmkerdata vmker;
500
501         index = VMHASH(avc->vmh);
502         if (scb_valid(index)) {  /* could almost be an ASSERT */
503
504             pri = disable_ints();
505             for (pagef = scb_sidlist(index); pagef >= 0; pagef = next) {
506                 next = pft_sidfwd(pagef);
507                 if (pft_modbit(pagef)) {  /* has page frame been modified? */
508                     enable_ints(pri);
509                     return 1;
510                 }
511             }
512             enable_ints(pri);
513         }
514     }
515 #undef VMHASH
516 #endif
517 #endif /* AFS_AIX32_ENV */
518
519 #if defined (AFS_SUN_ENV)
520     if (avc->states & CMAPPED) {
521         struct page * pg;
522         for (pg = avc->v.v_s.v_Pages ; pg ; pg = pg->p_vpnext) {
523             if (pg->p_mod) {
524                 return 1;
525             }
526         }
527     }
528 #endif
529 return 0;
530 }
531 #endif /* notdef */
532
533
534 /*
535  * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
536  * This leads to bad races when osi_ReleaseVM() is called from
537  * afs_InvalidateAllSegments().
538
539  * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
540  * vcache entry lock is held or not.
541  *
542  * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
543  * there may be cases where it does care.  If so, it would be good to fix
544  * them so they don't care.  Until then, we assume the worst.
545  *
546  * Locking:  the vcache entry lock is held.  It is dropped and re-obtained.
547  */
548 void
549 osi_ReleaseVM(avc, acred)
550     struct vcache *avc;
551     struct AFS_UCRED *acred;
552 {
553 #ifdef  AFS_SUN5_ENV
554     AFS_GUNLOCK();
555     osi_VM_Truncate(avc, 0, acred);
556     AFS_GLOCK();
557 #else
558     ReleaseWriteLock(&avc->lock);
559     AFS_GUNLOCK();
560     osi_VM_Truncate(avc, 0, acred);
561     AFS_GLOCK();
562     ObtainWriteLock(&avc->lock, 80);
563 #endif
564 }
565
566
567 void shutdown_osi()
568 {
569     extern int afs_cold_shutdown;
570
571     AFS_STATCNT(shutdown_osi);
572     if (afs_cold_shutdown) {
573         LOCK_INIT(&afs_ftf, "afs_ftf"); 
574       }
575 }
576
577 afs_osi_suser(credp) 
578   void * credp;
579 {
580 #ifdef AFS_SUN5_ENV
581   return afs_suser(credp);
582 #else
583   return afs_suser();
584 #endif
585 }
586
587 #if AFS_GCPAGS
588
589 /* afs_osi_TraverseProcTable() - Walk through the systems process
590  * table, calling afs_GCPAGs_perproc_func() for each process.
591  */
592
593 #if defined(AFS_SUN5_ENV)
594 void afs_osi_TraverseProcTable()
595 {
596     struct proc *prp;
597     for (prp = practive; prp != NULL; prp = prp->p_next) {
598         afs_GCPAGs_perproc_func(prp);
599     }
600 }
601 #endif
602
603 #if defined(AFS_HPUX_ENV)
604
605 /*
606  * NOTE: h/proc_private.h gives the process table locking rules
607  * It indicates that access to p_cred must be protected by
608  * mp_mtproc_lock(p);
609  * mp_mtproc_unlock(p);
610  *
611  * The code in sys/pm_prot.c uses pcred_lock() to protect access to
612  * the process creds, and uses mp_mtproc_lock() only for audit-related
613  * changes.  To be safe, we use both.
614  */
615
616 void afs_osi_TraverseProcTable()
617 {
618     register proc_t *p;
619     int endchain = 0;
620
621     MP_SPINLOCK(activeproc_lock);
622     MP_SPINLOCK(sched_lock);
623     pcred_lock();
624
625     /*
626      * Instead of iterating through all of proc[], traverse only
627      * the list of active processes.  As an example of this,
628      * see foreach_process() in sys/vm_sched.c.
629      *
630      * We hold the locks for the entire scan in order to get a
631      * consistent view of the current set of creds.
632      */
633
634     for(p = proc; endchain == 0; p = &proc[p->p_fandx]) {
635         if (p->p_fandx == 0) {
636             endchain = 1;
637         }
638
639         if (system_proc(p))
640             continue;
641
642         mp_mtproc_lock(p);
643         afs_GCPAGs_perproc_func(p);
644         mp_mtproc_unlock(p);
645     }
646
647     pcred_unlock();
648     MP_SPINUNLOCK(sched_lock);
649     MP_SPINUNLOCK(activeproc_lock);
650 }
651 #endif
652
653 #if defined(AFS_SGI_ENV)
654
655 #ifdef AFS_SGI65_ENV
656 /* TODO: Fix this later. */
657 static int SGI_ProcScanFunc(void *p, void *arg, int mode)
658 {
659     return 0;
660 }
661 #else   /* AFS_SGI65_ENV */
662 static int SGI_ProcScanFunc(proc_t *p, void *arg, int mode) 
663 {
664     afs_int32 (*perproc_func)(struct proc *) = arg;
665     int code=0;
666     /* we pass in the function pointer for arg,
667      * mode ==0 for startup call, ==1 for each valid proc, 
668      * and ==2 for terminate call.
669      */
670     if(mode == 1) {
671         code = perproc_func(p);
672     }
673     return code;
674 }
675 #endif  /* AFS_SGI65_ENV */
676
677 void afs_osi_TraverseProcTable()
678 {
679     procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
680 }
681 #endif  /* AFS_SGI_ENV */
682
683 #if defined(AFS_AIX_ENV)
684 void afs_osi_TraverseProcTable()
685 {
686     struct proc *p;
687     int i;
688
689     /*
690      * For binary compatibility, on AIX we need to be careful to use the
691      * proper size of a struct proc, even if it is different from what
692      * we were compiled with.
693      */
694     if (!afs_gcpags_procsize)
695         return;
696
697     simple_lock(&proc_tbl_lock);
698     for (p = (struct proc *)v.vb_proc, i = 0;
699          p < max_proc;
700          p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
701
702         if (p->p_stat == SNONE)
703             continue;
704         if (p->p_stat == SIDL)
705             continue;
706         if (p->p_stat == SEXIT)
707             continue;
708
709         /* sanity check */
710
711         if (PROCMASK(p->p_pid) != i) {
712             afs_gcpags = AFS_GCPAGS_EPIDCHECK;
713             break;
714         }
715
716         /* sanity check */
717
718         if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
719             afs_gcpags = AFS_GCPAGS_ENICECHECK;
720             break;
721         }
722
723         afs_GCPAGs_perproc_func(p);
724     }
725     simple_unlock(&proc_tbl_lock);
726 }
727 #endif
728
729 #if defined(AFS_OSF_ENV)
730 void afs_osi_TraverseProcTable()
731 {
732     struct pid_entry *pe;
733 #ifdef AFS_DUX50_ENV
734 extern struct pid_entry *pidtab;
735 extern int npid; 
736 #define pidNPID (pidtab + npid)
737 #define PID_LOCK()
738 #define PID_UNLOCK()
739 #endif
740     PID_LOCK();
741     for (pe = pidtab; pe < pidNPID; ++pe) {
742        if (pe->pe_proc != PROC_NULL)
743           afs_GCPAGs_perproc_func(pe->pe_proc);
744     }
745     PID_UNLOCK();
746 }
747 #endif
748
749 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
750 void afs_osi_TraverseProcTable()
751 {   
752     struct proc *p;
753     LIST_FOREACH(p, &allproc, p_list) {
754         if (p->p_stat == SIDL)
755             continue;
756         if (p->p_stat == SZOMB)
757             continue;
758         if (p->p_flag & P_SYSTEM)
759             continue;
760           afs_GCPAGs_perproc_func(p);
761     }
762 }   
763 #endif
764
765 /* return a pointer (sometimes a static copy ) to the cred for a
766  * given AFS_PROC.
767  * subsequent calls may overwrite the previously returned value.
768  */
769
770 #if defined(AFS_SGI65_ENV)
771 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
772 {
773     return NULL;
774 }
775 #elif defined(AFS_HPUX_ENV)
776 const struct AFS_UCRED *afs_osi_proc2cred(proc_t *p)
777 {
778     if (!p)
779         return;
780
781     /*
782      * Cannot use afs_warnuser() here, as the code path
783      * eventually wants to grab sched_lock, which is
784      * already held here
785      */
786
787     return p_cred(p);
788 }
789 #elif defined(AFS_AIX_ENV)
790
791 /* GLOBAL DECLARATIONS */
792
793 extern int      xmattach();        /* fills out cross memory descriptor */
794 extern int      xmdetach();        /* decrements reference count to segment */
795
796 /*
797  * LOCKS: the caller must do
798  *  simple_lock(&proc_tbl_lock);
799  *  simple_unlock(&proc_tbl_lock);
800  * around calls to this function.
801  */
802
803 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pproc)
804 {
805     struct AFS_UCRED *pcred = 0;
806
807     /*
808      * pointer to process user structure valid in *our*
809      * address space
810      *
811      * The user structure for a process is stored in the user
812      * address space (as distinct from the kernel address
813      * space), and so to refer to the user structure of a
814      * different process we must employ special measures.
815      *
816      * I followed the example used in the AIX getproc() system
817      * call in bos/kernel/proc/getproc.c
818      */
819     struct user *xmem_userp;
820
821     struct xmem dp;             /* ptr to xmem descriptor */
822     int xm;                     /* xmem result */
823
824     if (!pproc) {
825         return pcred;
826     }
827
828     /*
829      * The process private segment in which the user
830      * area is located may disappear. We need to increment
831      * its use count. Therefore we
832      *    - get the proc_tbl_lock to hold the segment.
833      *    - get the p_lock to lockout vm_cleardata.
834      *    - vm_att to load the segment register (no check)
835      *    - xmattach to bump its use count.
836      *    - release the p_lock.
837      *    - release the proc_tbl_lock.
838      *    - do whatever we need.
839      *    - xmdetach to decrement the use count.
840      *    - vm_det to free the segment register (no check)
841      */
842
843     xmem_userp = NULL;
844     xm = XMEM_FAIL;
845     /* simple_lock(&proc_tbl_lock); */
846     if (pproc->p_adspace != NULLSEGVAL) {
847
848         simple_lock(&pproc->p_lock);
849
850         if (pproc->p_threadcount &&
851             pproc->p_threadlist) {
852
853             /*
854              * arbitrarily pick the first thread in pproc
855              */
856             struct thread *pproc_thread =
857                 pproc->p_threadlist;
858
859             /*
860              * location of 'struct user' in pproc's
861              * address space
862              */
863             struct user *pproc_userp =
864                 pproc_thread->t_userp;
865
866             /*
867              * create a pointer valid in my own address space
868              */
869
870             xmem_userp =
871                 (struct user *)vm_att(pproc->p_adspace,
872                                       pproc_userp);
873
874             dp.aspace_id = XMEM_INVAL;
875             xm = xmattach(xmem_userp,
876                           sizeof(*xmem_userp),
877                           &dp, SYS_ADSPACE);
878         }
879
880         simple_unlock(&pproc->p_lock);
881     }
882     /* simple_unlock(&proc_tbl_lock); */
883     if (xm == XMEM_SUCC) {
884
885         static struct AFS_UCRED cred;
886
887         /*
888          * What locking should we use to protect access to the user
889          * area?  If needed also change the code in AIX/osi_groups.c.
890          */
891
892         /* copy cred to local address space */
893         cred = *xmem_userp->U_cred;
894         pcred = &cred;
895
896         xmdetach(&dp);
897     }
898     if (xmem_userp) {
899         vm_det((void *)xmem_userp);
900     }
901
902     return pcred;
903 }
904
905 #elif defined(AFS_OSF_ENV)
906 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
907 {
908     struct AFS_UCRED *rv=NULL;
909
910     if(pr == NULL) {
911        return NULL;
912     }
913
914     if((pr->p_stat == SSLEEP) ||
915        (pr->p_stat == SRUN) ||
916        (pr->p_stat == SSTOP)) 
917        rv = pr->p_rcred;
918
919     return rv;
920 }
921 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
922 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
923 {   
924     struct AFS_UCRED *rv=NULL;
925     static struct AFS_UCRED cr;
926
927     if(pr == NULL) {
928        return NULL;
929     }
930    
931     if((pr->p_stat == SSLEEP) ||
932        (pr->p_stat == SRUN) ||
933        (pr->p_stat == SSTOP)) {
934        pcred_readlock(pr);
935        cr.cr_ref=1;
936        cr.cr_uid=pr->p_cred->pc_ucred->cr_uid;
937        cr.cr_ngroups=pr->p_cred->pc_ucred->cr_ngroups;
938        bcopy(pr->p_cred->pc_ucred->cr_groups, cr.cr_groups,NGROUPS *
939              sizeof(gid_t));
940        pcred_unlock(pr);
941        rv = &cr;
942     }
943     
944     return rv;
945 }  
946 #else
947 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
948 {
949     struct AFS_UCRED *rv=NULL;
950
951     if(pr == NULL) {
952        return NULL;
953     }
954     rv = pr->p_cred;          
955
956     return rv;
957 }
958 #endif
959
960 #endif  /* AFS_GCPAGS */