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 <afsconfig.h>
11 #include "afs/param.h"
14 #include "afs/sysincludes.h" /* Standard vendor system headers */
15 #include "afsincludes.h" /* Afs-based standard headers */
16 #include "afs/afs_stats.h" /* statistics */
17 #include "sys/flock.h" /* for IGN_PID */
19 extern struct vnodeops Afs_vnodeops;
21 /* Try to discard pages, in order to recycle a vcache entry.
23 * We also make some sanity checks: ref count, open count, held locks.
25 * We also do some non-VM-related chores, such as releasing the cred pointer
26 * (for AIX and Solaris) and releasing the gnode (for AIX).
28 * Locking: afs_xvcache lock is held. If it is dropped and re-acquired,
29 * *slept should be set to warn the caller.
31 * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
32 * is not dropped and re-acquired for any platform. It may be that *slept is
33 * therefore obsolescent.
36 osi_VM_FlushVCache(struct vcache *avc, int *slept)
39 vnode_t *vp = &avc->v;
41 if (avc->vrefCount != 0)
48 * Just in case someone is still referring to the vnode we give up
49 * trying to get rid of this guy.
51 if (CheckLock(&avc->lock) || LockWaiters(&avc->lock))
57 * we just need to avoid the race
58 * in vn_rele between the ref count going to 0 and VOP_INACTIVE
60 * Note that although we checked vcount above, we didn't have the lock
62 if (vp->v_count > 0 || (vp->v_flag & VINACT)) {
69 * Since we store on last close and on VOP_INACTIVE
70 * there should be NO dirty pages
71 * Note that we hold the xvcache lock the entire time.
74 PTOSSVP(vp, (off_t) 0, (off_t) MAXLONG);
77 /* afs_chkpgoob will drop and re-acquire the global lock. */
79 osi_Assert(!VN_GET_PGCNT(vp));
80 osi_Assert(!AFS_VN_MAPPED(vp));
81 osi_Assert(!AFS_VN_DIRTY(&avc->v));
83 #if defined(AFS_SGI65_ENV)
85 cleanlocks(vp, IGN_PID, 0);
86 mutex_destroy(&vp->v_filocksem);
87 #else /* AFS_SGI65_ENV */
88 if (vp->v_filocksem) {
91 cleanlocks(vp, &curprocp->p_flid);
93 cleanlocks(vp, IGN_PID, 0);
95 osi_Assert(vp->v_filocks == NULL);
96 mutex_destroy(vp->v_filocksem);
97 kmem_free(vp->v_filocksem, sizeof *vp->v_filocksem);
98 vp->v_filocksem = NULL;
100 #endif /* AFS_SGI65_ENV */
103 osi_Panic("flushVcache: vm race");
106 vnode_pcache_reclaim(vp); /* this can sleep */
107 vnode_pcache_free(vp);
108 if (vp->v_op != &Afs_vnodeops) {
109 VOP_RECLAIM(vp, FSYNC_WAIT, code);
114 ktrace_free(vp->v_trace);
115 #endif /* VNODE_TRACING */
116 vn_bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
117 vn_bhv_head_destroy(&(vp->v_bh));
118 destroy_bitlock(&vp->v_pcacheflag);
119 mutex_destroy(&vp->v_buf_lock);
121 bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
122 bhv_head_destroy(&(vp->v_bh));
124 vp->v_flag = 0; /* debug */
125 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
126 destroy_bitlock(&vp->v_flag);
129 AFS_VN_DESTROY_BUF_LOCK(vp);
131 #endif /* AFS_SGI64_ENV */
136 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
137 * try to free pages, when deleting a file.
139 * Locking: the vcache entry's lock is held. It may be dropped and
142 * Since we drop and re-obtain the lock, we can't guarantee that there won't
143 * be some pages around when we return, newly created by concurrent activity.
146 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
148 ReleaseWriteLock(&avc->lock);
150 /* current remapf restriction - cannot have VOP_RWLOCK */
151 osi_Assert(OSI_GET_LOCKID() != avc->vc_rwlockid);
152 if (((vnode_t *) avc)->v_type == VREG && AFS_VN_MAPPED(((vnode_t *) avc)))
153 remapf(((vnode_t *) avc), 0, 0);
154 PTOSSVP(AFSTOV(avc), (off_t) 0, (off_t) MAXLONG);
156 ObtainWriteLock(&avc->lock, 62);
159 /* Flush and invalidate pages, for fsync() with INVAL flag
161 * Locking: only the global lock is held.
164 osi_VM_FSyncInval(struct vcache *avc)
167 PFLUSHINVALVP((vnode_t *) avc, (off_t) 0, (off_t) avc->f.m.Length);
171 /* Try to store pages to cache, in order to store a file back to the server.
173 * Locking: the vcache entry's lock is held. It will usually be dropped and
177 osi_VM_StoreAllSegments(struct vcache *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(AFSTOV(avc), 0);
194 PFLUSHVP(AFSTOV(avc), (off_t) avc->f.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->f.m.Length);
207 ObtainWriteLock(&avc->lock, 121);
208 if (error && avc->f.m.LinkCount)
210 "AFS:Failed to push back pages for vnode 0x%x error %d (from afs_StoreOnLastReference)",
214 /* Purge VM for a file when its callback is revoked.
216 * Locking: No lock is held, not even the global lock.
219 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
221 vnode_t *vp = (vnode_t *) avc;
223 remapf(vp, /*avc->f.m.Length */ 0, 0);
225 /* Used to grab locks and recheck avc->f.m.DataVersion and
226 * avc->execsOrWriters here, but we have to drop locks before calling
227 * ptossvp() anyway, so why bother.
231 * ptossvp tosses all pages associated with this vnode
232 * All in-use pages are marked BAD
234 PTOSSVP(vp, (off_t) 0, (off_t) MAXLONG);
237 /* Purge pages beyond end-of-file, when truncating a file.
239 * Locking: no lock is held, not even the global lock.
240 * activeV is raised. This is supposed to block pageins, but at present
241 * it only works on Solaris.
244 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
246 PTOSSVP(&avc->v, (off_t) alen, (off_t) MAXLONG);