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_AIX41_ENV)
54 lock_alloc((void*)&afs_global_lock, LOCK_ALLOC_PIN, 1, 1);
55 simple_lock_init((void *)&afs_global_lock);
57 #ifndef AFS_LINUX22_ENV
58 /* Linux initialization in osi directory. Should move the others. */
59 mutex_init(&afs_global_lock, "afs_global_lock", MUTEX_DEFAULT, NULL);
62 /* afs_rxglobal_lock is initialized in rx_Init. */
64 #endif /* AFS_HPUX_ENV */
66 if ( !afs_osicred_initialized )
68 bzero((char *)&afs_osi_cred, sizeof(struct AFS_UCRED));
69 crhold(&afs_osi_cred); /* don't let it evaporate */
70 afs_osicred_initialized = 1;
73 osi_flid.fl_pid = osi_flid.fl_sysid = 0;
78 register struct vcache *avc; {
79 AFS_STATCNT(osi_Active);
80 #if defined(AFS_SUN_ENV) || defined(AFS_AIX_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV) || (AFS_LINUX20_ENV)
81 if ((avc->opens > 0) || (avc->states & CMAPPED)) return 1; /* XXX: Warning, verify this XXX */
83 #if defined(AFS_MACH_ENV)
84 if (avc->opens > 0 || ((avc->v.v_flag & VTEXT) && !inode_uncache_try(avc))) return 1;
86 #if defined(AFS_SGI_ENV)
87 if ((avc->opens > 0) || AFS_VN_MAPPED((struct vnode *)avc))
90 if (avc->opens > 0 || (avc->v.v_flag & VTEXT)) return(1);
92 #endif /* AFS_MACH_ENV */
97 /* this call, unlike osi_FlushText, is supposed to discard caches that may
98 contain invalid information if a file is written remotely, but that may
99 contain valid information that needs to be written back if the file is
100 being written locally. It doesn't subsume osi_FlushText, since the latter
101 function may be needed to flush caches that are invalidated by local writes.
103 avc->pvnLock is already held, avc->lock is guaranteed not to be held (by
106 void osi_FlushPages(avc, credp)
107 register struct vcache *avc;
108 struct AFS_UCRED *credp;
111 ObtainReadLock(&avc->lock);
112 /* If we've already purged this version, or if we're the ones
113 writing this version, don't flush it (could lose the
114 data we're writing). */
115 if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0) ||
116 ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
117 ReleaseReadLock(&avc->lock);
120 ReleaseReadLock(&avc->lock);
121 ObtainWriteLock(&avc->lock,10);
123 if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0) ||
124 ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
125 ReleaseWriteLock(&avc->lock);
128 if (hiszero(avc->mapDV)) {
129 hset(avc->mapDV, avc->m.DataVersion);
130 ReleaseWriteLock(&avc->lock);
134 AFS_STATCNT(osi_FlushPages);
135 hset(origDV, avc->m.DataVersion);
136 afs_Trace3(afs_iclSetp, CM_TRACE_FLUSHPAGES, ICL_TYPE_POINTER, avc,
137 ICL_TYPE_INT32, origDV.low, ICL_TYPE_INT32, avc->m.Length);
139 ReleaseWriteLock(&avc->lock);
141 osi_VM_FlushPages(avc, credp);
143 ObtainWriteLock(&avc->lock,88);
145 /* do this last, and to original version, since stores may occur
146 while executing above PUTPAGE call */
147 hset(avc->mapDV, origDV);
148 ReleaseWriteLock(&avc->lock);
151 afs_lock_t afs_ftf; /* flush text lock */
155 /* This call is supposed to flush all caches that might be invalidated
156 * by either a local write operation or a write operation done on
157 * another client. This call may be called repeatedly on the same
158 * version of a file, even while a file is being written, so it
159 * shouldn't do anything that would discard newly written data before
160 * it is written to the file system. */
162 void osi_FlushText_really(vp)
163 register struct vcache *vp; {
164 afs_hyper_t fdv; /* version before which we'll flush */
166 AFS_STATCNT(osi_FlushText);
167 /* see if we've already flushed this data version */
168 if (hcmp(vp->m.DataVersion, vp->flushDV) <= 0) return;
172 void afs_gfs_FlushText();
173 afs_gfs_FlushText(vp);
178 MObtainWriteLock(&afs_ftf,317);
179 hset(fdv, vp->m.DataVersion);
181 /* why this disgusting code below?
182 * xuntext, called by xrele, doesn't notice when it is called
183 * with a freed text object. Sun continually calls xrele or xuntext
184 * without any locking, as long as VTEXT is set on the
185 * corresponding vnode.
186 * But, if the text object is locked when you check the VTEXT
187 * flag, several processes can wait in xuntext, waiting for the
188 * text lock; when the second one finally enters xuntext's
189 * critical region, the text object is already free, but the check
190 * was already done by xuntext's caller.
191 * Even worse, it turns out that xalloc locks the text object
192 * before reading or stating a file via the vnode layer. Thus, we
193 * could end up in getdcache, being asked to bring in a new
194 * version of a file, but the corresponding text object could be
195 * locked. We can't flush the text object without causing
196 * deadlock, so now we just don't try to lock the text object
197 * unless it is guaranteed to work. And we try to flush the text
198 * when we need to a bit more often at the vnode layer. Sun
199 * really blew the vm-cache flushing interface.
202 #if defined (AFS_HPUX_ENV)
203 if (vp->v.v_flag & VTEXT) {
206 if (vp->v.v_flag & VTEXT) { /* still has a text object? */
207 MReleaseWriteLock(&afs_ftf);
213 /* next do the stuff that need not check for deadlock problems */
216 /* finally, record that we've done it */
217 hset(vp->flushDV, fdv);
218 MReleaseWriteLock(&afs_ftf);
220 #endif /* AFS_DEC_ENV */
224 /* I don't really like using xinval() here, because it kills processes
225 * a bit aggressively. Previous incarnations of this functionality
226 * used to use xrele() instead of xinval, and didn't invoke
227 * cacheinval(). But they would panic. So it might be worth looking
228 * into some middle ground...
231 afs_gfs_FlushText(vp)
232 register struct vcache *vp; {
233 afs_hyper_t fdv; /* version before which we'll flush */
234 register struct text *xp;
237 MObtainWriteLock(&afs_ftf,318);
238 hset(fdv, vp->m.DataVersion);
242 /* this happens frequently after cores are created. */
243 MReleaseWriteLock(&afs_ftf);
247 if (gp->g_flag & GTEXT) {
249 xp = (struct text *) gp->g_textp ;
250 /* if text object is locked, give up */
251 if (xp && (xp->x_flag & XLOCK)) {
252 MReleaseWriteLock(&afs_ftf);
256 else xp = (struct text *) 0;
258 if (gp->g_flag & GTEXT) {/* still has a text object? */
263 /* next do the stuff that need not check for deadlock problems */
264 /* maybe xinval(gp); here instead of above */
267 /* finally, record that we've done it */
268 hset(vp->flushDV, fdv);
270 MReleaseWriteLock(&afs_ftf);
272 #endif /* AFS_DEC_ENV */
274 #endif /* AFS_TEXT_ENV */
276 /* procedure for making our processes as invisible as we can */
277 void afs_osi_Invisible() {
278 #ifndef AFS_AIX32_ENV
279 /* called once per "kernel" lwp to make it invisible */
281 u.u_procp->p_type |= SSYS;
283 #if defined(AFS_SUN5_ENV)
284 curproc->p_flag |= SSYS;
286 #if defined(AFS_SGI_ENV)
290 relvm(u.u_procp); /* release all the resources */
292 #if defined(AFS_HPUX101_ENV)
293 set_system_proc(u.u_procp);
295 #if !defined(AFS_SGI64_ENV) && !defined(AFS_LINUX20_ENV)
296 u.u_procp->p_flag |= SSYS;
297 #endif /* AFS_SGI64_ENV */
302 AFS_STATCNT(osi_Invisible);
306 #ifndef AFS_LINUX20_ENV /* Linux version in osi_misc.c */
307 /* set the real time */
309 register osi_timeval_t *atv; {
312 struct timestruc_t t;
314 t.tv_sec = atv->tv_sec;
315 t.tv_nsec = atv->tv_usec * 1000;
316 ksettimer(&t); /* Was -> settimer(TIMEOFDAY, &t); */
323 * To get more than second resolution we can use adjtime. The problem
324 * is that the usecs from the server are wrong (by now) so it isn't
325 * worth complicating the following code.
330 extern int stime(struct stimea *time, rval_t *rvp);
332 sta.time = atv->tv_sec;
336 #if defined(AFS_SGI_ENV)
340 extern int stime(struct stimea *time);
343 sta.time = atv->tv_sec;
347 /* stolen from kern_time.c */
349 boottime.tv_sec += atv->tv_sec - time.tv_sec;
355 t.tv_sec = atv->tv_sec;
356 t.tv_usec = atv->tv_usec;
357 s = spl7(); time = t; (void) splx(s);
363 s = splclock(); time = *atv; (void) splx(s);
368 logtchg(atv->tv_sec);
370 #endif /* AFS_SGI_ENV */
371 #endif /* AFS_SUN55_ENV */
372 #endif /* AFS_SUN5_ENV */
373 #endif /* AFS_AIX32_ENV */
374 AFS_STATCNT(osi_SetTime);
377 #endif /* AFS_LINUX20_ENV */
380 void *afs_osi_Alloc(size_t x)
382 register struct osimem *tm = NULL;
385 AFS_STATCNT(osi_Alloc);
386 /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
387 things so that NULL returned iff an error occurred */
388 if (x == 0) return &memZero;
390 AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
391 AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
392 #ifdef AFS_LINUX20_ENV
393 return osi_linux_alloc(x);
396 tm = (struct osimem *) AFS_KALLOC(size);
399 osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
406 #if defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
408 void *afs_osi_Alloc_NoSleep(size_t x)
410 register struct osimem *tm;
413 AFS_STATCNT(osi_Alloc);
414 /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
415 things so that NULL returned iff an error occurred */
416 if (x == 0) return &memZero;
419 AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
420 AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
421 tm = (struct osimem *) AFS_KALLOC_NOSLEEP(size);
425 #endif /* SUN || SGI */
427 void afs_osi_Free(void *x, size_t asize)
429 register struct osimem *tm, **lm, *um;
431 AFS_STATCNT(osi_Free);
432 if (x == &memZero) return; /* check for putting memZero back */
434 AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
435 AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
436 #ifdef AFS_LINUX20_ENV
439 AFS_KFREE((struct osimem *)x, asize);
444 /* ? is it moderately likely that there are dirty VM pages associated with
447 * Prereqs: avc must be write-locked
449 * System Dependencies: - *must* support each type of system for which
450 * memory mapped files are supported, even if all
451 * it does is return TRUE;
453 * NB: this routine should err on the side of caution for ProcessFS to work
454 * correctly (or at least, not to introduce worse bugs than already exist)
463 if (avc->execsOrWriters <= 0)
464 return 0; /* can't be many dirty pages here, I guess */
466 #if defined (AFS_AIX32_ENV)
468 /* because of the level of hardware involvment with VM and all the
469 * warnings about "This routine must be called at VMM interrupt
470 * level", I thought it would be safest to disable interrupts while
471 * looking at the software page fault table. */
473 /* convert vm handle into index into array: I think that stoinio is
474 * always zero... Look into this XXX */
475 #define VMHASH(handle) ( \
476 ( ((handle) & ~vmker.stoinio) \
477 ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
481 unsigned int pagef, pri, index, next;
482 extern struct vmkerdata vmker;
484 index = VMHASH(avc->vmh);
485 if (scb_valid(index)) { /* could almost be an ASSERT */
487 pri = disable_ints();
488 for (pagef = scb_sidlist(index); pagef >= 0; pagef = next) {
489 next = pft_sidfwd(pagef);
490 if (pft_modbit(pagef)) { /* has page frame been modified? */
500 #endif /* AFS_AIX32_ENV */
502 #if defined (AFS_SUN_ENV)
503 if (avc->states & CMAPPED) {
505 for (pg = avc->v.v_s.v_Pages ; pg ; pg = pg->p_vpnext) {
518 * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
519 * This leads to bad races when osi_ReleaseVM() is called from
520 * afs_InvalidateAllSegments().
522 * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
523 * vcache entry lock is held or not.
525 * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
526 * there may be cases where it does care. If so, it would be good to fix
527 * them so they don't care. Until then, we assume the worst.
529 * Locking: the vcache entry lock is held. It is dropped and re-obtained.
532 osi_ReleaseVM(avc, acred)
534 struct AFS_UCRED *acred;
538 osi_VM_Truncate(avc, 0, acred);
541 ReleaseWriteLock(&avc->lock);
543 osi_VM_Truncate(avc, 0, acred);
545 ObtainWriteLock(&avc->lock, 80);
552 extern int afs_cold_shutdown;
554 AFS_STATCNT(shutdown_osi);
555 if (afs_cold_shutdown) {
556 LOCK_INIT(&afs_ftf, "afs_ftf");
564 return afs_suser(credp);
572 /* afs_osi_TraverseProcTable() - Walk through the systems process
573 * table, calling afs_GCPAGs_perproc_func() for each process.
576 #if defined(AFS_SUN5_ENV)
577 void afs_osi_TraverseProcTable()
580 for (prp = practive; prp != NULL; prp = prp->p_next) {
581 afs_GCPAGs_perproc_func(prp);
586 #if defined(AFS_HPUX_ENV)
589 * NOTE: h/proc_private.h gives the process table locking rules
590 * It indicates that access to p_cred must be protected by
592 * mp_mtproc_unlock(p);
594 * The code in sys/pm_prot.c uses pcred_lock() to protect access to
595 * the process creds, and uses mp_mtproc_lock() only for audit-related
596 * changes. To be safe, we use both.
599 void afs_osi_TraverseProcTable()
604 MP_SPINLOCK(activeproc_lock);
605 MP_SPINLOCK(sched_lock);
609 * Instead of iterating through all of proc[], traverse only
610 * the list of active processes. As an example of this,
611 * see foreach_process() in sys/vm_sched.c.
613 * We hold the locks for the entire scan in order to get a
614 * consistent view of the current set of creds.
617 for(p = proc; endchain == 0; p = &proc[p->p_fandx]) {
618 if (p->p_fandx == 0) {
626 afs_GCPAGs_perproc_func(p);
631 MP_SPINUNLOCK(sched_lock);
632 MP_SPINUNLOCK(activeproc_lock);
636 #if defined(AFS_SGI_ENV)
639 /* TODO: Fix this later. */
640 static int SGI_ProcScanFunc(void *p, void *arg, int mode)
644 #else /* AFS_SGI65_ENV */
645 static int SGI_ProcScanFunc(proc_t *p, void *arg, int mode)
647 afs_int32 (*perproc_func)(struct proc *) = arg;
649 /* we pass in the function pointer for arg,
650 * mode ==0 for startup call, ==1 for each valid proc,
651 * and ==2 for terminate call.
654 code = perproc_func(p);
658 #endif /* AFS_SGI65_ENV */
660 void afs_osi_TraverseProcTable()
662 procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
664 #endif /* AFS_SGI_ENV */
666 #if defined(AFS_AIX_ENV)
667 void afs_osi_TraverseProcTable()
673 * For binary compatibility, on AIX we need to be careful to use the
674 * proper size of a struct proc, even if it is different from what
675 * we were compiled with.
677 if (!afs_gcpags_procsize)
680 simple_lock(&proc_tbl_lock);
681 for (p = (struct proc *)v.vb_proc, i = 0;
683 p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
685 if (p->p_stat == SNONE)
687 if (p->p_stat == SIDL)
689 if (p->p_stat == SEXIT)
694 if (PROCMASK(p->p_pid) != i) {
695 afs_gcpags = AFS_GCPAGS_EPIDCHECK;
701 if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
702 afs_gcpags = AFS_GCPAGS_ENICECHECK;
706 afs_GCPAGs_perproc_func(p);
708 simple_unlock(&proc_tbl_lock);
712 #if defined(AFS_OSF_ENV)
713 void afs_osi_TraverseProcTable()
715 struct pid_entry *pe;
717 extern struct pid_entry *pidtab;
719 #define pidNPID (pidtab + npid)
724 for (pe = pidtab; pe < pidNPID; ++pe) {
725 if (pe->pe_proc != PROC_NULL)
726 afs_GCPAGs_perproc_func(pe->pe_proc);
732 /* return a pointer (sometimes a static copy ) to the cred for a
734 * subsequent calls may overwrite the previously returned value.
737 #if defined(AFS_SGI65_ENV)
738 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
742 #elif defined(AFS_HPUX_ENV)
743 const struct AFS_UCRED *afs_osi_proc2cred(proc_t *p)
749 * Cannot use afs_warnuser() here, as the code path
750 * eventually wants to grab sched_lock, which is
756 #elif defined(AFS_AIX_ENV)
758 /* GLOBAL DECLARATIONS */
760 extern int xmattach(); /* fills out cross memory descriptor */
761 extern int xmdetach(); /* decrements reference count to segment */
764 * LOCKS: the caller must do
765 * simple_lock(&proc_tbl_lock);
766 * simple_unlock(&proc_tbl_lock);
767 * around calls to this function.
770 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pproc)
772 struct AFS_UCRED *pcred = 0;
775 * pointer to process user structure valid in *our*
778 * The user structure for a process is stored in the user
779 * address space (as distinct from the kernel address
780 * space), and so to refer to the user structure of a
781 * different process we must employ special measures.
783 * I followed the example used in the AIX getproc() system
784 * call in bos/kernel/proc/getproc.c
786 struct user *xmem_userp;
788 struct xmem dp; /* ptr to xmem descriptor */
789 int xm; /* xmem result */
796 * The process private segment in which the user
797 * area is located may disappear. We need to increment
798 * its use count. Therefore we
799 * - get the proc_tbl_lock to hold the segment.
800 * - get the p_lock to lockout vm_cleardata.
801 * - vm_att to load the segment register (no check)
802 * - xmattach to bump its use count.
803 * - release the p_lock.
804 * - release the proc_tbl_lock.
805 * - do whatever we need.
806 * - xmdetach to decrement the use count.
807 * - vm_det to free the segment register (no check)
812 /* simple_lock(&proc_tbl_lock); */
813 if (pproc->p_adspace != NULLSEGVAL) {
815 simple_lock(&pproc->p_lock);
817 if (pproc->p_threadcount &&
818 pproc->p_threadlist) {
821 * arbitrarily pick the first thread in pproc
823 struct thread *pproc_thread =
827 * location of 'struct user' in pproc's
830 struct user *pproc_userp =
831 pproc_thread->t_userp;
834 * create a pointer valid in my own address space
838 (struct user *)vm_att(pproc->p_adspace,
841 dp.aspace_id = XMEM_INVAL;
842 xm = xmattach(xmem_userp,
847 simple_unlock(&pproc->p_lock);
849 /* simple_unlock(&proc_tbl_lock); */
850 if (xm == XMEM_SUCC) {
852 static struct AFS_UCRED cred;
855 * What locking should we use to protect access to the user
856 * area? If needed also change the code in AIX/osi_groups.c.
859 /* copy cred to local address space */
860 cred = *xmem_userp->U_cred;
866 vm_det((void *)xmem_userp);
872 #elif defined(AFS_OSF_ENV)
873 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
875 struct AFS_UCRED *rv=NULL;
881 if((pr->p_stat == SSLEEP) ||
882 (pr->p_stat == SRUN) ||
883 (pr->p_stat == SSTOP))
889 const struct AFS_UCRED *afs_osi_proc2cred(AFS_PROC *pr)
891 struct AFS_UCRED *rv=NULL;
902 #endif /* AFS_GCPAGS */