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