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