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