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