1 /* Copyright (C) 1998 Transarc Corporation - All rights reserved. */
3 * (C) COPYRIGHT IBM CORPORATION 1987, 1988
4 * LICENSED MATERIALS - PROPERTY OF IBM
7 #include "../afs/param.h" /* Should be always first */
8 #include "../afs/sysincludes.h" /* Standard vendor system headers */
9 #include "../afs/afsincludes.h" /* Afs-based standard headers */
10 #include "../afs/afs_stats.h" /* statistics */
11 #include "sys/flock.h" /* for IGN_PID */
13 extern struct vnodeops Afs_vnodeops;
15 /* Try to discard pages, in order to recycle a vcache entry.
17 * We also make some sanity checks: ref count, open count, held locks.
19 * We also do some non-VM-related chores, such as releasing the cred pointer
20 * (for AIX and Solaris) and releasing the gnode (for AIX).
22 * Locking: afs_xvcache lock is held. If it is dropped and re-acquired,
23 * *slept should be set to warn the caller.
25 * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
26 * is not dropped and re-acquired for any platform. It may be that *slept is
27 * therefore obsolescent.
30 osi_VM_FlushVCache(avc, slept)
35 vnode_t *vp = &avc->v;
37 if (avc->vrefCount != 0)
44 * Just in case someone is still referring to the vnode we give up
45 * trying to get rid of this guy.
47 if (CheckLock(&avc->lock) || LockWaiters(&avc->lock))
53 * we just need to avoid the race
54 * in vn_rele between the ref count going to 0 and VOP_INACTIVE
56 * Note that although we checked vcount above, we didn't have the lock
58 if (vp->v_count > 0 || (vp->v_flag & VINACT)) {
65 * Since we store on last close and on VOP_INACTIVE
66 * there should be NO dirty pages
67 * Note that we hold the xvcache lock the entire time.
70 PTOSSVP(vp, (off_t)0, (off_t)MAXLONG);
73 /* afs_chkpgoob will drop and re-acquire the global lock. */
75 osi_Assert(!VN_GET_PGCNT(vp));
76 osi_Assert(!AFS_VN_MAPPED(vp));
77 osi_Assert(!AFS_VN_DIRTY(&avc->v));
79 #if defined(AFS_SGI65_ENV)
81 cleanlocks(vp, IGN_PID, 0);
82 mutex_destroy(&vp->v_filocksem);
83 #else /* AFS_SGI65_ENV */
84 if (vp->v_filocksem) {
87 cleanlocks(vp, &curprocp->p_flid);
89 cleanlocks(vp, IGN_PID, 0);
91 osi_Assert(vp->v_filocks == NULL);
92 mutex_destroy(vp->v_filocksem);
93 kmem_free(vp->v_filocksem, sizeof *vp->v_filocksem);
94 vp->v_filocksem = NULL;
96 #endif /* AFS_SGI65_ENV */
98 if (avc->vrefCount) osi_Panic("flushVcache: vm race");
101 vnode_pcache_reclaim(vp); /* this can sleep */
102 vnode_pcache_free(vp);
103 if (vp->v_op != &Afs_vnodeops) {
104 VOP_RECLAIM(vp, FSYNC_WAIT, code);
109 ktrace_free(vp->v_trace);
110 #endif /* VNODE_TRACING */
111 vn_bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
112 vn_bhv_head_destroy(&(vp->v_bh));
113 destroy_bitlock(&vp->v_pcacheflag);
114 mutex_destroy(&vp->v_buf_lock);
116 bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
117 bhv_head_destroy(&(vp->v_bh));
119 vp->v_flag = 0; /* debug */
120 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
121 destroy_bitlock(&vp->v_flag);
124 AFS_VN_DESTROY_BUF_LOCK(vp);
126 #endif /* AFS_SGI64_ENV */
131 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
132 * try to free pages, when deleting a file.
134 * Locking: the vcache entry's lock is held. It may be dropped and
137 * Since we drop and re-obtain the lock, we can't guarantee that there won't
138 * be some pages around when we return, newly created by concurrent activity.
141 osi_VM_TryToSmush(avc, acred, sync)
143 struct AFS_UCRED *acred;
146 ReleaseWriteLock(&avc->lock);
148 /* current remapf restriction - cannot have VOP_RWLOCK */
149 osi_Assert(OSI_GET_LOCKID() != avc->vc_rwlockid);
150 if (((vnode_t *)avc)->v_type == VREG && AFS_VN_MAPPED(((vnode_t *)avc)))
151 remapf(((vnode_t *)avc), 0, 0);
152 PTOSSVP((struct vnode *)avc, (off_t)0, (off_t)MAXLONG);
154 ObtainWriteLock(&avc->lock,62);
157 /* Flush and invalidate pages, for fsync() with INVAL flag
159 * Locking: only the global lock is held.
162 osi_VM_FSyncInval(avc)
166 PFLUSHINVALVP((vnode_t *)avc, (off_t)0, (off_t)avc->m.Length);
170 /* Try to store pages to cache, in order to store a file back to the server.
172 * Locking: the vcache entry's lock is held. It will usually be dropped and
176 osi_VM_StoreAllSegments(avc)
180 osi_Assert(valusema(&avc->vc_rwlock) <= 0);
181 osi_Assert(OSI_GET_LOCKID() == avc->vc_rwlockid);
182 osi_Assert(avc->vrefCount > 0);
183 ReleaseWriteLock(&avc->lock);
184 /* We may call back into AFS via:
185 * pflushvp->chunkpush->do_pdflush->mp_afs_bmap
189 /* Write out dirty pages list to avoid B_DELWRI buffers. */
190 while (VN_GET_DPAGES((vnode_t*)avc)) {
191 pdflush((struct vnode*)avc, 0);
194 PFLUSHVP((struct vnode *)avc, (off_t)avc->m.Length, (off_t)0, error);
198 * If this fails (due to quota overage, etc.)
199 * what can we do?? we need to sure that
200 * that the VM cache is cleared of dirty pages
201 * We note that pinvalfree ignores write errors & otherwise
202 * does what we want (we don't use this normally since
203 * it also unhashes pages ..)
205 PINVALFREE((vnode_t *)avc, avc->m.Length);
207 ObtainWriteLock(&avc->lock,121);
208 if (error && avc->m.LinkCount)
209 cmn_err(CE_WARN, "AFS:Failed to push back pages for vnode 0x%x error %d (from afs_StoreOnLastReference)",
213 /* Purge VM for a file when its callback is revoked.
215 * Locking: No lock is held, not even the global lock.
218 osi_VM_FlushPages(avc, credp)
220 struct AFS_UCRED *credp;
222 vnode_t *vp = (vnode_t *)avc;
224 remapf(vp, /*avc->m.Length*/ 0, 0);
226 /* Used to grab locks and recheck avc->m.DataVersion and
227 * avc->execsOrWriters here, but we have to drop locks before calling
228 * ptossvp() anyway, so why bother.
232 * ptossvp tosses all pages associated with this vnode
233 * All in-use pages are marked BAD
235 PTOSSVP(vp, (off_t)0, (off_t)MAXLONG);
238 /* Purge pages beyond end-of-file, when truncating a file.
240 * Locking: no lock is held, not even the global lock.
241 * activeV is raised. This is supposed to block pageins, but at present
242 * it only works on Solaris.
245 osi_VM_Truncate(avc, alen, acred)
248 struct AFS_UCRED *acred;
250 PTOSSVP(&avc->v, (off_t)alen, (off_t)MAXLONG);