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