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