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. It must not be dropped.
31 osi_VM_FlushVCache(struct vcache *avc)
34 vnode_t *vp = &avc->v;
36 if (avc->vrefCount != 0)
43 * Just in case someone is still referring to the vnode we give up
44 * trying to get rid of this guy.
46 if (CheckLock(&avc->lock) || LockWaiters(&avc->lock))
52 * we just need to avoid the race
53 * in vn_rele between the ref count going to 0 and VOP_INACTIVE
55 * Note that although we checked vcount above, we didn't have the lock
57 if (vp->v_count > 0 || (vp->v_flag & VINACT)) {
64 * Since we store on last close and on VOP_INACTIVE
65 * there should be NO dirty pages
66 * Note that we hold the xvcache lock the entire time.
69 PTOSSVP(vp, (off_t) 0, (off_t) MAXLONG);
72 /* afs_chkpgoob will drop and re-acquire the global lock. */
74 osi_Assert(!VN_GET_PGCNT(vp));
75 osi_Assert(!AFS_VN_MAPPED(vp));
76 osi_Assert(!AFS_VN_DIRTY(&avc->v));
78 #if defined(AFS_SGI65_ENV)
80 cleanlocks(vp, IGN_PID, 0);
81 mutex_destroy(&vp->v_filocksem);
82 #else /* AFS_SGI65_ENV */
83 if (vp->v_filocksem) {
86 cleanlocks(vp, &curprocp->p_flid);
88 cleanlocks(vp, IGN_PID, 0);
90 osi_Assert(vp->v_filocks == NULL);
91 mutex_destroy(vp->v_filocksem);
92 kmem_free(vp->v_filocksem, sizeof *vp->v_filocksem);
93 vp->v_filocksem = NULL;
95 #endif /* AFS_SGI65_ENV */
98 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(struct vcache *avc, afs_ucred_t *acred, int sync)
143 ReleaseWriteLock(&avc->lock);
145 /* current remapf restriction - cannot have VOP_RWLOCK */
146 osi_Assert(OSI_GET_LOCKID() != avc->vc_rwlockid);
147 if (((vnode_t *) avc)->v_type == VREG && AFS_VN_MAPPED(((vnode_t *) avc)))
148 remapf(((vnode_t *) avc), 0, 0);
149 PTOSSVP(AFSTOV(avc), (off_t) 0, (off_t) MAXLONG);
151 ObtainWriteLock(&avc->lock, 62);
154 /* Flush and invalidate pages, for fsync() with INVAL flag
156 * Locking: only the global lock is held.
159 osi_VM_FSyncInval(struct vcache *avc)
162 PFLUSHINVALVP((vnode_t *) avc, (off_t) 0, (off_t) avc->f.m.Length);
166 /* Try to store pages to cache, in order to store a file back to the server.
168 * Locking: the vcache entry's lock is held. It will usually be dropped and
172 osi_VM_StoreAllSegments(struct vcache *avc)
175 osi_Assert(valusema(&avc->vc_rwlock) <= 0);
176 osi_Assert(OSI_GET_LOCKID() == avc->vc_rwlockid);
177 osi_Assert(avc->vrefCount > 0);
178 ReleaseWriteLock(&avc->lock);
179 /* We may call back into AFS via:
180 * pflushvp->chunkpush->do_pdflush->mp_afs_bmap
184 /* Write out dirty pages list to avoid B_DELWRI buffers. */
185 while (VN_GET_DPAGES((vnode_t *) avc)) {
186 pdflush(AFSTOV(avc), 0);
189 PFLUSHVP(AFSTOV(avc), (off_t) avc->f.m.Length, (off_t) 0, error);
193 * If this fails (due to quota overage, etc.)
194 * what can we do?? we need to sure that
195 * that the VM cache is cleared of dirty pages
196 * We note that pinvalfree ignores write errors & otherwise
197 * does what we want (we don't use this normally since
198 * it also unhashes pages ..)
200 PINVALFREE((vnode_t *) avc, avc->f.m.Length);
202 ObtainWriteLock(&avc->lock, 121);
203 if (error && avc->f.m.LinkCount)
205 "AFS:Failed to push back pages for vnode 0x%x error %d (from afs_StoreOnLastReference)",
209 /* Purge VM for a file when its callback is revoked.
211 * Locking: No lock is held, not even the global lock.
214 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
216 vnode_t *vp = (vnode_t *) avc;
218 remapf(vp, /*avc->f.m.Length */ 0, 0);
220 /* Used to grab locks and recheck avc->f.m.DataVersion and
221 * avc->execsOrWriters here, but we have to drop locks before calling
222 * ptossvp() anyway, so why bother.
226 * ptossvp tosses all pages associated with this vnode
227 * All in-use pages are marked BAD
229 PTOSSVP(vp, (off_t) 0, (off_t) MAXLONG);
232 /* Purge pages beyond end-of-file, when truncating a file.
234 * Locking: no lock is held, not even the global lock.
235 * activeV is raised. This is supposed to block pageins, but at present
236 * it only works on Solaris.
239 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
241 PTOSSVP(&avc->v, (off_t) alen, (off_t) MAXLONG);