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