2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include "../afs/param.h" /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h" /* afs statistics */
15 #include <sys/adspace.h> /* for vm_att(), vm_det() */
18 static char memZero; /* address of 0 bytes for kmem_alloc */
19 extern int afs_osicred_initialized;
25 /* osi_Init -- do once per kernel installation initialization.
26 * -- On Solaris this is called from modload initialization.
27 * -- On AIX called from afs_config.
28 * -- On HP called from afsc_link.
29 * -- On SGI called from afs_init. */
32 lock_t afs_event_lock;
42 if (once++ > 0) /* just in case */
44 #if defined(AFS_HPUX_ENV)
46 #else /* AFS_HPUX_ENV */
47 #if defined(AFS_GLOBAL_SUNLOCK)
48 #if defined(AFS_SGI62_ENV)
49 mutex_init(&afs_global_lock, MUTEX_DEFAULT, "afs_global_lock");
50 #elif defined(AFS_OSF_ENV)
51 usimple_lock_init(&afs_global_lock);
52 afs_global_owner = (thread_t)0;
53 #elif defined(AFS_DARWIN_ENV)
54 lockinit(&afs_global_lock, PLOCK, "afs global lock", 0, 0);
55 afs_global_owner = (thread_t)0;
56 #elif defined(AFS_AIX41_ENV)
57 lock_alloc((void*)&afs_global_lock, LOCK_ALLOC_PIN, 1, 1);
58 simple_lock_init((void *)&afs_global_lock);
60 #ifndef AFS_LINUX22_ENV
61 /* Linux initialization in osi directory. Should move the others. */
62 mutex_init(&afs_global_lock, "afs_global_lock", MUTEX_DEFAULT, NULL);
65 /* afs_rxglobal_lock is initialized in rx_Init. */
67 #endif /* AFS_HPUX_ENV */
69 if ( !afs_osicred_initialized )
71 bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
72 crhold(&afs_osi_cred); /* don't let it evaporate */
73 afs_osicred_initialized = 1;
76 osi_flid.fl_pid = osi_flid.fl_sysid = 0;
81 register struct vcache *avc; {
82 AFS_STATCNT(osi_Active);
83 #if defined(AFS_SUN_ENV) || defined(AFS_AIX_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV) || (AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV)
84 if ((avc->opens > 0) || (avc->states & CMAPPED)) return 1; /* XXX: Warning, verify this XXX */
86 #if defined(AFS_MACH_ENV)
87 if (avc->opens > 0 || ((avc->v.v_flag & VTEXT) && !inode_uncache_try(avc))) return 1;
89 #if defined(AFS_SGI_ENV)
90 if ((avc->opens > 0) || AFS_VN_MAPPED((struct vnode *)avc))
93 if (avc->opens > 0 || (avc->v.v_flag & VTEXT)) return(1);
95 #endif /* AFS_MACH_ENV */
100 /* this call, unlike osi_FlushText, is supposed to discard caches that may
101 contain invalid information if a file is written remotely, but that may
102 contain valid information that needs to be written back if the file is
103 being written locally. It doesn't subsume osi_FlushText, since the latter
104 function may be needed to flush caches that are invalidated by local writes.
106 avc->pvnLock is already held, avc->lock is guaranteed not to be held (by
109 void osi_FlushPages(avc, credp)
110 register struct vcache *avc;
111 struct AFS_UCRED *credp;
114 ObtainReadLock(&avc->lock);
115 /* If we've already purged this version, or if we're the ones
116 writing this version, don't flush it (could lose the
117 data we're writing). */
118 if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0) ||
119 ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
120 ReleaseReadLock(&avc->lock);
123 ReleaseReadLock(&avc->lock);
124 ObtainWriteLock(&avc->lock,10);
126 if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0) ||
127 ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
128 ReleaseWriteLock(&avc->lock);
131 if (hiszero(avc->mapDV)) {
132 hset(avc->mapDV, avc->m.DataVersion);
133 ReleaseWriteLock(&avc->lock);
137 AFS_STATCNT(osi_FlushPages);
138 hset(origDV, avc->m.DataVersion);
139 afs_Trace3(afs_iclSetp, CM_TRACE_FLUSHPAGES, ICL_TYPE_POINTER, avc,
140 ICL_TYPE_INT32, origDV.low, ICL_TYPE_INT32, avc->m.Length);
142 ReleaseWriteLock(&avc->lock);
144 osi_VM_FlushPages(avc, credp);
146 ObtainWriteLock(&avc->lock,88);
148 /* do this last, and to original version, since stores may occur
149 while executing above PUTPAGE call */
150 hset(avc->mapDV, origDV);
151 ReleaseWriteLock(&avc->lock);
154 afs_lock_t afs_ftf; /* flush text lock */
158 /* This call is supposed to flush all caches that might be invalidated
159 * by either a local write operation or a write operation done on
160 * another client. This call may be called repeatedly on the same
161 * version of a file, even while a file is being written, so it
162 * shouldn't do anything that would discard newly written data before
163 * it is written to the file system. */
165 void osi_FlushText_really(vp)
166 register struct vcache *vp; {
167 afs_hyper_t fdv; /* version before which we'll flush */
169 AFS_STATCNT(osi_FlushText);
170 /* see if we've already flushed this data version */
171 if (hcmp(vp->m.DataVersion, vp->flushDV) <= 0) return;
175 void afs_gfs_FlushText();
176 afs_gfs_FlushText(vp);
181 MObtainWriteLock(&afs_ftf,317);
182 hset(fdv, vp->m.DataVersion);
184 /* why this disgusting code below?
185 * xuntext, called by xrele, doesn't notice when it is called
186 * with a freed text object. Sun continually calls xrele or xuntext
187 * without any locking, as long as VTEXT is set on the
188 * corresponding vnode.
189 * But, if the text object is locked when you check the VTEXT
190 * flag, several processes can wait in xuntext, waiting for the
191 * text lock; when the second one finally enters xuntext's
192 * critical region, the text object is already free, but the check
193 * was already done by xuntext's caller.
194 * Even worse, it turns out that xalloc locks the text object
195 * before reading or stating a file via the vnode layer. Thus, we
196 * could end up in getdcache, being asked to bring in a new
197 * version of a file, but the corresponding text object could be
198 * locked. We can't flush the text object without causing
199 * deadlock, so now we just don't try to lock the text object
200 * unless it is guaranteed to work. And we try to flush the text
201 * when we need to a bit more often at the vnode layer. Sun
202 * really blew the vm-cache flushing interface.
205 #if defined (AFS_HPUX_ENV)
206 if (vp->v.v_flag & VTEXT) {
209 if (vp->v.v_flag & VTEXT) { /* still has a text object? */
210 MReleaseWriteLock(&afs_ftf);
216 /* next do the stuff that need not check for deadlock problems */
219 /* finally, record that we've done it */
220 hset(vp->flushDV, fdv);
221 MReleaseWriteLock(&afs_ftf);
223 #endif /* AFS_DEC_ENV */
227 /* I don't really like using xinval() here, because it kills processes
228 * a bit aggressively. Previous incarnations of this functionality
229 * used to use xrele() instead of xinval, and didn't invoke
230 * cacheinval(). But they would panic. So it might be worth looking
231 * into some middle ground...
234 afs_gfs_FlushText(vp)
235 register struct vcache *vp; {
236 afs_hyper_t fdv; /* version before which we'll flush */
237 register struct text *xp;
240 MObtainWriteLock(&afs_ftf,318);
241 hset(fdv, vp->m.DataVersion);
245 /* this happens frequently after cores are created. */
246 MReleaseWriteLock(&afs_ftf);
250 if (gp->g_flag & GTEXT) {
252 xp = (struct text *) gp->g_textp ;
253 /* if text object is locked, give up */
254 if (xp && (xp->x_flag & XLOCK)) {
255 MReleaseWriteLock(&afs_ftf);
259 else xp = (struct text *) 0;
261 if (gp->g_flag & GTEXT) {/* still has a text object? */
266 /* next do the stuff that need not check for deadlock problems */
267 /* maybe xinval(gp); here instead of above */
270 /* finally, record that we've done it */
271 hset(vp->flushDV, fdv);
273 MReleaseWriteLock(&afs_ftf);
275 #endif /* AFS_DEC_ENV */
277 #endif /* AFS_TEXT_ENV */
279 /* procedure for making our processes as invisible as we can */
280 void afs_osi_Invisible() {
281 #ifndef AFS_AIX32_ENV
282 /* called once per "kernel" lwp to make it invisible */
284 u.u_procp->p_type |= SSYS;
286 #if defined(AFS_SUN5_ENV)
287 curproc->p_flag |= SSYS;
289 #if defined(AFS_SGI_ENV)
293 relvm(u.u_procp); /* release all the resources */
295 #if defined(AFS_HPUX101_ENV)
296 set_system_proc(u.u_procp);
298 #if defined(AFS_DARWIN_ENV)
299 current_proc()->p_flag |= P_SYSTEM;
301 #if !defined(AFS_SGI64_ENV) && !defined(AFS_LINUX20_ENV)
302 u.u_procp->p_flag |= SSYS;
303 #endif /* AFS_SGI64_ENV */
309 AFS_STATCNT(osi_Invisible);
313 #ifndef AFS_LINUX20_ENV /* Linux version in osi_misc.c */
314 /* set the real time */
316 register osi_timeval_t *atv; {
319 struct timestruc_t t;
321 t.tv_sec = atv->tv_sec;
322 t.tv_nsec = atv->tv_usec * 1000;
323 ksettimer(&t); /* Was -> settimer(TIMEOFDAY, &t); */
330 * To get more than second resolution we can use adjtime. The problem
331 * is that the usecs from the server are wrong (by now) so it isn't
332 * worth complicating the following code.
337 extern int stime(struct stimea *time, rval_t *rvp);
339 sta.time = atv->tv_sec;
343 #if defined(AFS_SGI_ENV)
347 extern int stime(struct stimea *time);
350 sta.time = atv->tv_sec;
354 #ifdef AFS_DARWIN_ENV
359 /* stolen from kern_time.c */
361 boottime.tv_sec += atv->tv_sec - time.tv_sec;
367 t.tv_sec = atv->tv_sec;
368 t.tv_usec = atv->tv_usec;
369 s = spl7(); time = t; (void) splx(s);
375 s = splclock(); time = *atv; (void) splx(s);
380 logtchg(atv->tv_sec);
382 #endif /* AFS_DARWIN_ENV */
383 #endif /* AFS_SGI_ENV */
384 #endif /* AFS_SUN55_ENV */
385 #endif /* AFS_SUN5_ENV */
386 #endif /* AFS_AIX32_ENV */
387 AFS_STATCNT(osi_SetTime);
390 #endif /* AFS_LINUX20_ENV */
393 void *afs_osi_Alloc(size_t x)
395 register struct osimem *tm = NULL;
398 AFS_STATCNT(osi_Alloc);
399 /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
400 things so that NULL returned iff an error occurred */
401 if (x == 0) return &memZero;
403 AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
404 AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
405 #ifdef AFS_LINUX20_ENV
406 return osi_linux_alloc(x);
409 tm = (struct osimem *) AFS_KALLOC(size);
412 osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
419 #if defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
421 void *afs_osi_Alloc_NoSleep(size_t x)
423 register struct osimem *tm;
426 AFS_STATCNT(osi_Alloc);
427 /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
428 things so that NULL returned iff an error occurred */
429 if (x == 0) return &memZero;
432 AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
433 AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
434 tm = (struct osimem *) AFS_KALLOC_NOSLEEP(size);
438 #endif /* SUN || SGI */
440 void afs_osi_Free(void *x, size_t asize)
442 register struct osimem *tm, **lm, *um;
444 AFS_STATCNT(osi_Free);
445 if (x == &memZero) return; /* check for putting memZero back */
447 AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
448 AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
449 #ifdef AFS_LINUX20_ENV
452 AFS_KFREE((struct osimem *)x, asize);
457 /* ? is it moderately likely that there are dirty VM pages associated with
460 * Prereqs: avc must be write-locked
462 * System Dependencies: - *must* support each type of system for which
463 * memory mapped files are supported, even if all
464 * it does is return TRUE;
466 * NB: this routine should err on the side of caution for ProcessFS to work
467 * correctly (or at least, not to introduce worse bugs than already exist)
476 if (avc->execsOrWriters <= 0)
477 return 0; /* can't be many dirty pages here, I guess */
479 #if defined (AFS_AIX32_ENV)
481 /* because of the level of hardware involvment with VM and all the
482 * warnings about "This routine must be called at VMM interrupt
483 * level", I thought it would be safest to disable interrupts while
484 * looking at the software page fault table. */
486 /* convert vm handle into index into array: I think that stoinio is
487 * always zero... Look into this XXX */
488 #define VMHASH(handle) ( \
489 ( ((handle) & ~vmker.stoinio) \
490 ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
494 unsigned int pagef, pri, index, next;
495 extern struct vmkerdata vmker;
497 index = VMHASH(avc->vmh);
498 if (scb_valid(index)) { /* could almost be an ASSERT */
500 pri = disable_ints();
501 for (pagef = scb_sidlist(index); pagef >= 0; pagef = next) {
502 next = pft_sidfwd(pagef);
503 if (pft_modbit(pagef)) { /* has page frame been modified? */
513 #endif /* AFS_AIX32_ENV */
515 #if defined (AFS_SUN_ENV)
516 if (avc->states & CMAPPED) {
518 for (pg = avc->v.v_s.v_Pages ; pg ; pg = pg->p_vpnext) {
531 * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
532 * This leads to bad races when osi_ReleaseVM() is called from
533 * afs_InvalidateAllSegments().
535 * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
536 * vcache entry lock is held or not.
538 * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
539 * there may be cases where it does care. If so, it would be good to fix
540 * them so they don't care. Until then, we assume the worst.
542 * Locking: the vcache entry lock is held. It is dropped and re-obtained.
545 osi_ReleaseVM(avc, acred)
547 struct AFS_UCRED *acred;
551 osi_VM_Truncate(avc, 0, acred);
554 ReleaseWriteLock(&avc->lock);
556 osi_VM_Truncate(avc, 0, acred);
558 ObtainWriteLock(&avc->lock, 80);
565 extern int afs_cold_shutdown;
567 AFS_STATCNT(shutdown_osi);
568 if (afs_cold_shutdown) {
569 LOCK_INIT(&afs_ftf, "afs_ftf");
577 return afs_suser(credp);
585 /* afs_osi_TraverseProcTable() - Walk through the systems process
586 * table, calling afs_GCPAGs_perproc_func() for each process.
589 #if defined(AFS_SUN5_ENV)
590 void afs_osi_TraverseProcTable()
593 for (prp = practive; prp != NULL; prp = prp->p_next) {
594 afs_GCPAGs_perproc_func(prp);
599 #if defined(AFS_HPUX_ENV)
602 * NOTE: h/proc_private.h gives the process table locking rules
603 * It indicates that access to p_cred must be protected by
605 * mp_mtproc_unlock(p);
607 * The code in sys/pm_prot.c uses pcred_lock() to protect access to
608 * the process creds, and uses mp_mtproc_lock() only for audit-related
609 * changes. To be safe, we use both.
612 void afs_osi_TraverseProcTable()
617 MP_SPINLOCK(activeproc_lock);
618 MP_SPINLOCK(sched_lock);
622 * Instead of iterating through all of proc[], traverse only
623 * the list of active processes. As an example of this,
624 * see foreach_process() in sys/vm_sched.c.
626 * We hold the locks for the entire scan in order to get a
627 * consistent view of the current set of creds.
630 for(p = proc; endchain == 0; p = &proc[p->p_fandx]) {
631 if (p->p_fandx == 0) {
639 afs_GCPAGs_perproc_func(p);
644 MP_SPINUNLOCK(sched_lock);
645 MP_SPINUNLOCK(activeproc_lock);
649 #if defined(AFS_SGI_ENV)
652 /* TODO: Fix this later. */
653 static int SGI_ProcScanFunc(void *p, void *arg, int mode)
657 #else /* AFS_SGI65_ENV */
658 static int SGI_ProcScanFunc(proc_t *p, void *arg, int mode)
660 afs_int32 (*perproc_func)(struct proc *) = arg;
662 /* we pass in the function pointer for arg,
663 * mode ==0 for startup call, ==1 for each valid proc,
664 * and ==2 for terminate call.
667 code = perproc_func(p);
671 #endif /* AFS_SGI65_ENV */
673 void afs_osi_TraverseProcTable()
675 procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
677 #endif /* AFS_SGI_ENV */
679 #if defined(AFS_AIX_ENV)
680 void afs_osi_TraverseProcTable()
686 * For binary compatibility, on AIX we need to be careful to use the
687 * proper size of a struct proc, even if it is different from what
688 * we were compiled with.
690 if (!afs_gcpags_procsize)
693 simple_lock(&proc_tbl_lock);
694 for (p = (struct proc *)v.vb_proc, i = 0;
696 p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
698 if (p->p_stat == SNONE)
700 if (p->p_stat == SIDL)
702 if (p->p_stat == SEXIT)
707 if (PROCMASK(p->p_pid) != i) {
708 afs_gcpags = AFS_GCPAGS_EPIDCHECK;
714 if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
715 afs_gcpags = AFS_GCPAGS_ENICECHECK;
719 afs_GCPAGs_perproc_func(p);
721 simple_unlock(&proc_tbl_lock);
725 #if defined(AFS_OSF_ENV)
726 void afs_osi_TraverseProcTable()
728 struct pid_entry *pe;
730 extern struct pid_entry *pidtab;
732 #define pidNPID (pidtab + npid)
737 for (pe = pidtab; pe < pidNPID; ++pe) {
738 if (pe->pe_proc != PROC_NULL)
739 afs_GCPAGs_perproc_func(pe->pe_proc);
745 #if defined(AFS_DARWIN_ENV)
746 void afs_osi_TraverseProcTable()
749 LIST_FOREACH(p, &allproc, p_list) {
750 if (p->p_stat == SIDL)
752 if (p->p_stat == SZOMB)
754 if (p->p_flag & P_SYSTEM)
756 afs_GCPAGs_perproc_func(p);
761 /* return a pointer (sometimes a static copy ) to the cred for a
763 * subsequent calls may overwrite the previously returned value.
766 #if defined(AFS_SGI65_ENV)
767 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
771 #elif defined(AFS_HPUX_ENV)
772 const struct AFS_UCRED *afs_osi_proc2cred(proc_t *p)
778 * Cannot use afs_warnuser() here, as the code path
779 * eventually wants to grab sched_lock, which is
785 #elif defined(AFS_AIX_ENV)
787 /* GLOBAL DECLARATIONS */
789 extern int xmattach(); /* fills out cross memory descriptor */
790 extern int xmdetach(); /* decrements reference count to segment */
793 * LOCKS: the caller must do
794 * simple_lock(&proc_tbl_lock);
795 * simple_unlock(&proc_tbl_lock);
796 * around calls to this function.
799 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pproc)
801 struct AFS_UCRED *pcred = 0;
804 * pointer to process user structure valid in *our*
807 * The user structure for a process is stored in the user
808 * address space (as distinct from the kernel address
809 * space), and so to refer to the user structure of a
810 * different process we must employ special measures.
812 * I followed the example used in the AIX getproc() system
813 * call in bos/kernel/proc/getproc.c
815 struct user *xmem_userp;
817 struct xmem dp; /* ptr to xmem descriptor */
818 int xm; /* xmem result */
825 * The process private segment in which the user
826 * area is located may disappear. We need to increment
827 * its use count. Therefore we
828 * - get the proc_tbl_lock to hold the segment.
829 * - get the p_lock to lockout vm_cleardata.
830 * - vm_att to load the segment register (no check)
831 * - xmattach to bump its use count.
832 * - release the p_lock.
833 * - release the proc_tbl_lock.
834 * - do whatever we need.
835 * - xmdetach to decrement the use count.
836 * - vm_det to free the segment register (no check)
841 /* simple_lock(&proc_tbl_lock); */
842 if (pproc->p_adspace != NULLSEGVAL) {
844 simple_lock(&pproc->p_lock);
846 if (pproc->p_threadcount &&
847 pproc->p_threadlist) {
850 * arbitrarily pick the first thread in pproc
852 struct thread *pproc_thread =
856 * location of 'struct user' in pproc's
859 struct user *pproc_userp =
860 pproc_thread->t_userp;
863 * create a pointer valid in my own address space
867 (struct user *)vm_att(pproc->p_adspace,
870 dp.aspace_id = XMEM_INVAL;
871 xm = xmattach(xmem_userp,
876 simple_unlock(&pproc->p_lock);
878 /* simple_unlock(&proc_tbl_lock); */
879 if (xm == XMEM_SUCC) {
881 static struct AFS_UCRED cred;
884 * What locking should we use to protect access to the user
885 * area? If needed also change the code in AIX/osi_groups.c.
888 /* copy cred to local address space */
889 cred = *xmem_userp->U_cred;
895 vm_det((void *)xmem_userp);
901 #elif defined(AFS_OSF_ENV)
902 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
904 struct AFS_UCRED *rv=NULL;
910 if((pr->p_stat == SSLEEP) ||
911 (pr->p_stat == SRUN) ||
912 (pr->p_stat == SSTOP))
917 #elif defined(AFS_DARWIN_ENV)
918 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
920 struct AFS_UCRED *rv=NULL;
921 static struct AFS_UCRED cr;
927 if((pr->p_stat == SSLEEP) ||
928 (pr->p_stat == SRUN) ||
929 (pr->p_stat == SSTOP)) {
932 cr.cr_uid=pr->p_cred->pc_ucred->cr_uid;
933 cr.cr_ngroups=pr->p_cred->pc_ucred->cr_ngroups;
934 bcopy(pr->p_cred->pc_ucred->cr_groups, cr.cr_groups,NGROUPS *
943 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
945 struct AFS_UCRED *rv=NULL;
956 #endif /* AFS_GCPAGS */