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