hpux-1122-initial-support-20030213
[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 #if !defined(AFS_HPUX1122_ENV)
389  /* drop the setting of the clock for now. spl7 is not
390   * known on hpux11.22
391   */
392         register ulong_t s;
393         struct timeval t;
394         t.tv_sec = atv->tv_sec;
395         t.tv_usec = atv->tv_usec;
396         s = spl7(); time = t; (void) splx(s);
397         resettodr(atv);
398 #endif
399     }
400 #else
401     {
402         register int s;
403         s = splclock(); time = *atv; (void) splx(s);
404     }
405     resettodr();
406 #endif
407 #ifdef  AFS_AUX_ENV
408     logtchg(atv->tv_sec);
409 #endif
410 #endif  /* AFS_DARWIN_ENV */
411 #endif  /* AFS_FBSD_ENV */
412 #endif  /* AFS_SGI_ENV */
413 #endif /* AFS_SUN55_ENV */
414 #endif /* AFS_SUN5_ENV */
415 #endif /* AFS_AIX32_ENV */
416     AFS_STATCNT(osi_SetTime);
417 }
418 #endif /* AFS_LINUX20_ENV */
419
420
421 void *afs_osi_Alloc(size_t x)
422 {
423     register struct osimem *tm = NULL;
424     register int size;
425
426     AFS_STATCNT(osi_Alloc);
427     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
428        things so that NULL returned iff an error occurred */
429     if (x == 0) return &memZero;
430
431     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
432     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
433 #ifdef AFS_LINUX20_ENV
434     return osi_linux_alloc(x, 1);
435 #else
436     size = x;
437     tm = (struct osimem *) AFS_KALLOC(size);
438 #ifdef  AFS_SUN_ENV
439     if (!tm)
440         osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
441                   size);
442 #endif
443     return (void *) tm;
444 #endif
445 }
446
447 #if     defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
448
449 void *afs_osi_Alloc_NoSleep(size_t x)
450 {
451     register struct osimem *tm;
452     register int size;
453
454     AFS_STATCNT(osi_Alloc);
455     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
456        things so that NULL returned iff an error occurred */
457     if (x == 0) return &memZero;
458
459     size = x;
460     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
461     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
462     tm = (struct osimem *) AFS_KALLOC_NOSLEEP(size);
463     return (void *) tm;
464 }
465
466 #endif  /* SUN || SGI */
467
468 void afs_osi_Free(void *x, size_t asize)
469 {
470     AFS_STATCNT(osi_Free);
471     if (x == &memZero) return;  /* check for putting memZero back */
472
473     AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
474     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
475 #if defined(AFS_LINUX20_ENV)
476     osi_linux_free(x);
477 #else
478     AFS_KFREE((struct osimem *)x, asize);
479 #endif
480 }
481
482 void afs_osi_FreeStr(char *x)
483 {
484     afs_osi_Free(x, strlen(x) + 1);
485 }
486
487 /* ? is it moderately likely that there are dirty VM pages associated with
488  * this vnode?
489  *
490  *  Prereqs:  avc must be write-locked
491  *
492  *  System Dependencies:  - *must* support each type of system for which
493  *                          memory mapped files are supported, even if all
494  *                          it does is return TRUE;
495  *
496  * NB:  this routine should err on the side of caution for ProcessFS to work
497  *      correctly (or at least, not to introduce worse bugs than already exist)
498  */
499 #ifdef  notdef
500 int osi_VMDirty_p(struct vcache *avc)
501 {
502     int dirtyPages;
503
504     if (avc->execsOrWriters <= 0)
505         return 0;         /* can't be many dirty pages here, I guess */
506
507 #if defined (AFS_AIX32_ENV)
508 #ifdef  notdef
509     /* because of the level of hardware involvment with VM and all the
510      * warnings about "This routine must be called at VMM interrupt
511      * level", I thought it would be safest to disable interrupts while
512      * looking at the software page fault table.  */
513
514     /* convert vm handle into index into array:  I think that stoinio is
515      * always zero...  Look into this XXX  */
516 #define VMHASH(handle) ( \
517                         ( ((handle) & ~vmker.stoinio)  \
518                          ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
519                          ) & 0x000fffff)
520
521     if (avc->vmh) {
522         unsigned int pagef, pri, index, next;
523
524         index = VMHASH(avc->vmh);
525         if (scb_valid(index)) {  /* could almost be an ASSERT */
526
527             pri = disable_ints();
528             for (pagef = scb_sidlist(index); pagef >= 0; pagef = next) {
529                 next = pft_sidfwd(pagef);
530                 if (pft_modbit(pagef)) {  /* has page frame been modified? */
531                     enable_ints(pri);
532                     return 1;
533                 }
534             }
535             enable_ints(pri);
536         }
537     }
538 #undef VMHASH
539 #endif
540 #endif /* AFS_AIX32_ENV */
541
542 #if defined (AFS_SUN_ENV)
543     if (avc->states & CMAPPED) {
544         struct page * pg;
545         for (pg = avc->v.v_s.v_Pages ; pg ; pg = pg->p_vpnext) {
546             if (pg->p_mod) {
547                 return 1;
548             }
549         }
550     }
551 #endif
552     return 0;
553 }
554 #endif /* notdef */
555
556
557 /*
558  * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
559  * This leads to bad races when osi_ReleaseVM() is called from
560  * afs_InvalidateAllSegments().
561
562  * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
563  * vcache entry lock is held or not.
564  *
565  * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
566  * there may be cases where it does care.  If so, it would be good to fix
567  * them so they don't care.  Until then, we assume the worst.
568  *
569  * Locking:  the vcache entry lock is held.  It is dropped and re-obtained.
570  */
571 void osi_ReleaseVM(struct vcache *avc, struct AFS_UCRED *acred)
572 {
573 #ifdef  AFS_SUN5_ENV
574     AFS_GUNLOCK();
575     osi_VM_Truncate(avc, 0, acred);
576     AFS_GLOCK();
577 #else
578     ReleaseWriteLock(&avc->lock);
579     AFS_GUNLOCK();
580     osi_VM_Truncate(avc, 0, acred);
581     AFS_GLOCK();
582     ObtainWriteLock(&avc->lock, 80);
583 #endif
584 }
585
586
587 void shutdown_osi(void)
588 {
589     AFS_STATCNT(shutdown_osi);
590     if (afs_cold_shutdown) {
591         LOCK_INIT(&afs_ftf, "afs_ftf");
592     }
593 }
594
595 #ifndef AFS_OBSD_ENV
596 int afs_osi_suser(void *credp)
597 {
598 #if defined(AFS_SUN5_ENV)
599     return afs_suser(credp);
600 #else
601     return afs_suser();
602 #endif
603 }
604 #endif
605
606 #if AFS_GCPAGS
607
608 /* afs_osi_TraverseProcTable() - Walk through the systems process
609  * table, calling afs_GCPAGs_perproc_func() for each process.
610  */
611
612 #if defined(AFS_SUN5_ENV)
613 void afs_osi_TraverseProcTable(void)
614 {
615     struct proc *prp;
616     for (prp = practive; prp != NULL; prp = prp->p_next) {
617         afs_GCPAGs_perproc_func(prp);
618     }
619 }
620 #endif
621
622 #if defined(AFS_HPUX_ENV)
623
624 /*
625  * NOTE: h/proc_private.h gives the process table locking rules
626  * It indicates that access to p_cred must be protected by
627  * mp_mtproc_lock(p);
628  * mp_mtproc_unlock(p);
629  *
630  * The code in sys/pm_prot.c uses pcred_lock() to protect access to
631  * the process creds, and uses mp_mtproc_lock() only for audit-related
632  * changes.  To be safe, we use both.
633  */
634
635 void afs_osi_TraverseProcTable(void)
636 {
637     register proc_t *p;
638     int endchain = 0;
639
640     MP_SPINLOCK(activeproc_lock);
641     MP_SPINLOCK(sched_lock);
642     pcred_lock();
643
644     /*
645      * Instead of iterating through all of proc[], traverse only
646      * the list of active processes.  As an example of this,
647      * see foreach_process() in sys/vm_sched.c.
648      *
649      * We hold the locks for the entire scan in order to get a
650      * consistent view of the current set of creds.
651      */
652
653     for(p = proc; endchain == 0; p = &proc[p->p_fandx]) {
654         if (p->p_fandx == 0) {
655             endchain = 1;
656         }
657
658         if (system_proc(p))
659             continue;
660
661         mp_mtproc_lock(p);
662         afs_GCPAGs_perproc_func(p);
663         mp_mtproc_unlock(p);
664     }
665
666     pcred_unlock();
667     MP_SPINUNLOCK(sched_lock);
668     MP_SPINUNLOCK(activeproc_lock);
669 }
670 #endif
671
672 #if defined(AFS_SGI_ENV)
673
674 #ifdef AFS_SGI65_ENV
675 /* TODO: Fix this later. */
676 static int SGI_ProcScanFunc(void *p, void *arg, int mode)
677 {
678     return 0;
679 }
680 #else   /* AFS_SGI65_ENV */
681 static int SGI_ProcScanFunc(proc_t *p, void *arg, int mode)
682 {
683     afs_int32 (*perproc_func)(struct proc *) = arg;
684     int code=0;
685     /* we pass in the function pointer for arg,
686      * mode ==0 for startup call, ==1 for each valid proc,
687      * and ==2 for terminate call.
688      */
689     if(mode == 1) {
690         code = perproc_func(p);
691     }
692     return code;
693 }
694 #endif  /* AFS_SGI65_ENV */
695
696 void afs_osi_TraverseProcTable(void)
697 {
698     procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
699 }
700 #endif  /* AFS_SGI_ENV */
701
702 #if defined(AFS_AIX_ENV)
703 #ifdef AFS_AIX51_ENV
704 #define max_proc v.ve_proc
705 #endif
706 void afs_osi_TraverseProcTable(void)
707 {
708     struct proc *p;
709     int i;
710
711     /*
712      * For binary compatibility, on AIX we need to be careful to use the
713      * proper size of a struct proc, even if it is different from what
714      * we were compiled with.
715      */
716     if (!afs_gcpags_procsize)
717         return;
718
719 #ifndef AFS_AIX51_ENV
720     simple_lock(&proc_tbl_lock);
721 #endif
722     for (p = (struct proc *)v.vb_proc, i = 0;
723          p < max_proc;
724          p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
725
726 #ifdef AFS_AIX51_ENV
727         if (p->p_pvprocp->pv_stat == SNONE)
728             continue;
729         if (p->p_pvprocp->pv_stat == SIDL)
730             continue;
731         if (p->p_pvprocp->pv_stat == SEXIT)
732             continue;
733 #else
734         if (p->p_stat == SNONE)
735             continue;
736         if (p->p_stat == SIDL)
737             continue;
738         if (p->p_stat == SEXIT)
739             continue;
740 #endif
741
742         /* sanity check */
743
744         if (PROCMASK(p->p_pid) != i) {
745             afs_gcpags = AFS_GCPAGS_EPIDCHECK;
746             break;
747         }
748
749         /* sanity check */
750
751         if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
752             afs_gcpags = AFS_GCPAGS_ENICECHECK;
753             break;
754         }
755
756         afs_GCPAGs_perproc_func(p);
757     }
758 #ifndef AFS_AIX51_ENV
759     simple_unlock(&proc_tbl_lock);
760 #endif
761 }
762 #endif
763
764 #if defined(AFS_OSF_ENV)
765 void afs_osi_TraverseProcTable(void)
766 {
767     struct pid_entry *pe;
768 #ifdef AFS_DUX50_ENV
769 #define pidNPID (pidtab + npid)
770 #define PID_LOCK()
771 #define PID_UNLOCK()
772 #endif
773     PID_LOCK();
774     for (pe = pidtab; pe < pidNPID; ++pe) {
775        if (pe->pe_proc != PROC_NULL)
776           afs_GCPAGs_perproc_func(pe->pe_proc);
777     }
778     PID_UNLOCK();
779 }
780 #endif
781
782 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
783 void afs_osi_TraverseProcTable(void)
784 {
785     struct proc *p;
786     LIST_FOREACH(p, &allproc, p_list) {
787         if (p->p_stat == SIDL)
788             continue;
789         if (p->p_stat == SZOMB)
790             continue;
791         if (p->p_flag & P_SYSTEM)
792             continue;
793           afs_GCPAGs_perproc_func(p);
794     }
795 }
796 #endif
797
798 #if defined(AFS_LINUX22_ENV)
799 void afs_osi_TraverseProcTable()
800 {
801     struct task_struct *p;
802
803 #ifdef EXPORTED_TASKLIST_LOCK
804     read_lock(&tasklist_lock);
805 #endif
806 #ifdef DEFINED_FOR_EACH_PROCESS
807     for_each_process(p) if (p->pid) {
808         if (p->state & TASK_ZOMBIE)
809             continue;
810         afs_GCPAGs_perproc_func(p);
811     }
812 #else
813     for_each_task(p) if (p->pid) {
814         if (p->state & TASK_ZOMBIE)
815             continue;
816         afs_GCPAGs_perproc_func(p);
817     }
818 #endif
819 #ifdef EXPORTED_TASKLIST_LOCK
820     read_unlock(&tasklist_lock);
821 #endif
822 }
823 #endif
824
825 /* return a pointer (sometimes a static copy ) to the cred for a
826  * given AFS_PROC.
827  * subsequent calls may overwrite the previously returned value.
828  */
829
830 #if defined(AFS_SGI65_ENV)
831 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *p)
832 {
833     return NULL;
834 }
835 #elif defined(AFS_HPUX_ENV)
836 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *p)
837 {
838     if (!p)
839         return;
840
841     /*
842      * Cannot use afs_warnuser() here, as the code path
843      * eventually wants to grab sched_lock, which is
844      * already held here
845      */
846
847     return p_cred(p);
848 }
849 #elif defined(AFS_AIX_ENV)
850
851 /* GLOBAL DECLARATIONS */
852
853 /*
854  * LOCKS: the caller must do
855  *  simple_lock(&proc_tbl_lock);
856  *  simple_unlock(&proc_tbl_lock);
857  * around calls to this function.
858  */
859
860 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pproc)
861 {
862     struct AFS_UCRED *pcred = 0;
863
864     /*
865      * pointer to process user structure valid in *our*
866      * address space
867      *
868      * The user structure for a process is stored in the user
869      * address space (as distinct from the kernel address
870      * space), and so to refer to the user structure of a
871      * different process we must employ special measures.
872      *
873      * I followed the example used in the AIX getproc() system
874      * call in bos/kernel/proc/getproc.c
875      */
876     struct user *xmem_userp;
877
878     struct xmem dp;             /* ptr to xmem descriptor */
879     int xm;                     /* xmem result */
880
881     if (!pproc) {
882         return pcred;
883     }
884
885     /*
886      * The process private segment in which the user
887      * area is located may disappear. We need to increment
888      * its use count. Therefore we
889      *    - get the proc_tbl_lock to hold the segment.
890      *    - get the p_lock to lockout vm_cleardata.
891      *    - vm_att to load the segment register (no check)
892      *    - xmattach to bump its use count.
893      *    - release the p_lock.
894      *    - release the proc_tbl_lock.
895      *    - do whatever we need.
896      *    - xmdetach to decrement the use count.
897      *    - vm_det to free the segment register (no check)
898      */
899
900     xmem_userp = NULL;
901     xm = XMEM_FAIL;
902     /* simple_lock(&proc_tbl_lock); */
903     if (pproc->p_adspace != NULLSEGVAL) {
904
905 #ifdef AFS_AIX51_ENV
906         simple_lock(&pproc->p_pvprocp->pv_lock);
907 #else
908         simple_lock(&pproc->p_lock);
909 #endif
910
911         if (pproc->p_threadcount &&
912 #ifdef AFS_AIX51_ENV
913             pproc->p_pvprocp->pv_threadlist) {
914 #else
915             pproc->p_threadlist) {
916 #endif
917
918             /*
919              * arbitrarily pick the first thread in pproc
920              */
921             struct thread *pproc_thread =
922 #ifdef AFS_AIX51_ENV
923                 pproc->p_pvprocp->pv_threadlist;
924 #else
925                 pproc->p_threadlist;
926 #endif
927
928             /*
929              * location of 'struct user' in pproc's
930              * address space
931              */
932             struct user *pproc_userp =
933                 pproc_thread->t_userp;
934
935             /*
936              * create a pointer valid in my own address space
937              */
938
939             xmem_userp =
940                 (struct user *)vm_att(pproc->p_adspace,
941                                       pproc_userp);
942
943             dp.aspace_id = XMEM_INVAL;
944             xm = xmattach(xmem_userp,
945                           sizeof(*xmem_userp),
946                           &dp, SYS_ADSPACE);
947         }
948
949 #ifdef AFS_AIX51_ENV
950         simple_unlock(&pproc->p_pvprocp->pv_lock);
951 #else
952         simple_unlock(&pproc->p_lock);
953 #endif
954     }
955     /* simple_unlock(&proc_tbl_lock); */
956     if (xm == XMEM_SUCC) {
957
958         static struct AFS_UCRED cred;
959
960         /*
961          * What locking should we use to protect access to the user
962          * area?  If needed also change the code in AIX/osi_groups.c.
963          */
964
965         /* copy cred to local address space */
966         cred = *xmem_userp->U_cred;
967         pcred = &cred;
968
969         xmdetach(&dp);
970     }
971     if (xmem_userp) {
972         vm_det((void *)xmem_userp);
973     }
974
975     return pcred;
976 }
977
978 #elif defined(AFS_OSF_ENV)
979 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
980 {
981     struct AFS_UCRED *rv=NULL;
982
983     if(pr == NULL) {
984        return NULL;
985     }
986
987     if((pr->p_stat == SSLEEP) ||
988        (pr->p_stat == SRUN) ||
989        (pr->p_stat == SSTOP))
990        rv = pr->p_rcred;
991
992     return rv;
993 }
994 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
995 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
996 {
997     struct AFS_UCRED *rv=NULL;
998     static struct AFS_UCRED cr;
999
1000     if(pr == NULL) {
1001        return NULL;
1002     }
1003
1004     if((pr->p_stat == SSLEEP) ||
1005        (pr->p_stat == SRUN) ||
1006        (pr->p_stat == SSTOP)) {
1007        pcred_readlock(pr);
1008        cr.cr_ref=1;
1009        cr.cr_uid=pr->p_cred->pc_ucred->cr_uid;
1010        cr.cr_ngroups=pr->p_cred->pc_ucred->cr_ngroups;
1011        memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups, NGROUPS *
1012              sizeof(gid_t));
1013        pcred_unlock(pr);
1014        rv = &cr;
1015     }
1016
1017     return rv;
1018 }
1019 #elif defined(AFS_LINUX22_ENV)
1020 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
1021 {
1022     struct AFS_UCRED *rv=NULL;
1023     static struct AFS_UCRED cr;
1024
1025     if(pr == NULL) {
1026        return NULL;
1027     }
1028
1029     if ((pr->state == TASK_RUNNING) ||
1030         (pr->state == TASK_INTERRUPTIBLE) ||
1031         (pr->state == TASK_UNINTERRUPTIBLE) ||
1032         (pr->state == TASK_STOPPED)) {
1033         cr.cr_ref=1;
1034         cr.cr_uid=pr->uid;
1035         cr.cr_ngroups=pr->ngroups;
1036         memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
1037         rv = &cr;
1038     }
1039
1040     return rv;
1041 }
1042 #else
1043 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
1044 {
1045     struct AFS_UCRED *rv=NULL;
1046
1047     if(pr == NULL) {
1048        return NULL;
1049     }
1050     rv = pr->p_cred;
1051
1052     return rv;
1053 }
1054 #endif
1055
1056 #endif  /* AFS_GCPAGS */