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