freebsd: properly track vcache references
[openafs.git] / src / afs / FBSD / osi_vm.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10
11 /* osi_vm.c implements:
12  *
13  * osi_VM_FlushVCache(avc, slept)
14  * osi_ubc_flush_dirty_and_wait(vp, flags)
15  * osi_VM_StoreAllSegments(avc)
16  * osi_VM_TryToSmush(avc, acred, sync)
17  * osi_VM_FlushPages(avc, credp)
18  * osi_VM_Truncate(avc, alen, acred)
19  */
20
21 #include <afsconfig.h>
22 #include "afs/param.h"
23 #include <sys/param.h>
24 #include <sys/vnode.h>
25
26
27 #include "afs/sysincludes.h"    /* Standard vendor system headers */
28 #include "afsincludes.h"        /* Afs-based standard headers */
29 #include "afs/afs_stats.h"      /* statistics */
30 #include <vm/vm_object.h>
31 #include <vm/vm_map.h>
32 #include <limits.h>
33 #include <float.h>
34
35 /*
36  * FreeBSD implementation notes:
37  * Most of these operations require us to frob vm_objects.  Most
38  * functions require that the object be locked (with VM_OBJECT_LOCK)
39  * on entry and leave it locked on exit.  In order to get the
40  * vm_object itself we call VOP_GETVOBJECT on the vnode; the
41  * locking protocol requires that we do so with the heavy vnode lock
42  * held and the vnode interlock unlocked, and it returns the same
43  * way.
44  *
45  * The locking protocol for vnodes is defined in
46  * kern/vnode_if.src and sys/vnode.h; the locking is still a work in 
47  * progress, so some fields are (as of 5.1) still protected by Giant
48  * rather than an explicit lock.
49  */
50
51 #ifdef AFS_FBSD60_ENV
52 #define VOP_GETVOBJECT(vp, objp) (*(objp) = (vp)->v_object)
53 #endif
54
55 #if defined(AFS_FBSD80_ENV)
56 #define lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY)
57 #define ilock_vnode(v)  vn_lock((v), LK_INTERLOCK|LK_EXCLUSIVE|LK_RETRY);
58 #define unlock_vnode(v) VOP_UNLOCK((v), 0)
59 #else
60 #define lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curthread)
61 #define ilock_vnode(v)  vn_lock((v), LK_INTERLOCK|LK_EXCLUSIVE|LK_RETRY, curthread);
62 #define unlock_vnode(v) VOP_UNLOCK((v), 0, curthread)
63 #endif
64
65 /* Try to discard pages, in order to recycle a vcache entry.
66  *
67  * We also make some sanity checks:  ref count, open count, held locks.
68  *
69  * We also do some non-VM-related chores, such as releasing the cred pointer
70  * (for AIX and Solaris) and releasing the gnode (for AIX).
71  *
72  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
73  *   *slept should be set to warn the caller.
74  *
75  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
76  * is not dropped and re-acquired for any platform.  It may be that *slept is
77  * therefore obsolescent.
78  *
79  */
80 int
81 osi_VM_FlushVCache(struct vcache *avc, int *slept)
82 {
83     struct vnode *vp = AFSTOV(avc);
84
85     if (!VI_TRYLOCK(vp)) /* need interlock to check usecount */
86         return EBUSY;
87
88     if (vp->v_usecount > 0) {
89         VI_UNLOCK(vp);
90         return EBUSY;
91     }
92
93     /* XXX
94      * The value of avc->opens here came to be, at some point,
95      * typically -1.  This was caused by incorrectly performing afs_close
96      * processing on vnodes being recycled */
97     if (avc->opens) {
98         VI_UNLOCK(vp);
99         return EBUSY;
100     }
101
102     /* if a lock is held, give up */
103     if (CheckLock(&avc->lock)) {
104         VI_UNLOCK(vp);
105         return EBUSY;
106     }
107
108     if ((vp->v_iflag & VI_DOOMED) != 0) {
109         VI_UNLOCK(vp);
110         return (0);
111     }
112
113     /* must hold the vnode before calling vgone()
114      * This code largely copied from vfs_subr.c:vlrureclaim() */
115     vholdl(vp);
116     AFS_GUNLOCK();
117     *slept = 1;
118     /* use the interlock while locking, so no one else can DOOM this */
119     ilock_vnode(vp);
120     vgone(vp);
121     unlock_vnode(vp);
122     vdrop(vp);
123
124     AFS_GLOCK();
125     return 0;
126 }
127
128 /* Try to store pages to cache, in order to store a file back to the server.
129  *
130  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
131  * re-obtained.
132  */
133 void
134 osi_VM_StoreAllSegments(struct vcache *avc)
135 {
136     struct vnode *vp;
137     struct vm_object *obj;
138     int anyio, tries;
139
140     ReleaseWriteLock(&avc->lock);
141     AFS_GUNLOCK();
142     tries = 5;
143     vp = AFSTOV(avc);
144
145     /*
146      * I don't understand this.  Why not just call vm_object_page_clean()
147      * and be done with it?  I particularly don't understand why we're calling
148      * vget() here.  Is there some reason to believe that the vnode might
149      * be being recycled at this point?  I don't think there's any need for
150      * this loop, either -- if we keep the vnode locked all the time,
151      * that and the object lock will prevent any new pages from appearing.
152      * The loop is what causes the race condition.  -GAW
153      */
154     do {
155         anyio = 0;
156         if (VOP_GETVOBJECT(vp, &obj) == 0 && (obj->flags & OBJ_MIGHTBEDIRTY)) {
157             if (!vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread)) {
158                     if (VOP_GETVOBJECT(vp, &obj) == 0) {
159                         VM_OBJECT_LOCK(obj);
160                         vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
161                         VM_OBJECT_UNLOCK(obj);
162                         anyio = 1;
163                     }
164                     vput(vp);
165                 }
166             }
167     } while (anyio && (--tries > 0));
168     AFS_GLOCK();
169     ObtainWriteLock(&avc->lock, 94);
170 }
171
172 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
173  * try to free pages, when deleting a file.
174  *
175  * Locking:  the vcache entry's lock is held.  It may be dropped and 
176  * re-obtained.
177  *
178  * Since we drop and re-obtain the lock, we can't guarantee that there won't
179  * be some pages around when we return, newly created by concurrent activity.
180  */
181 void
182 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
183 {
184     struct vnode *vp;
185     int tries, code;
186     int islocked;
187
188     vp = AFSTOV(avc);
189
190     VI_LOCK(vp);
191     if (vp->v_iflag & VI_DOOMED) {
192         VI_UNLOCK(vp);
193         return;
194     }
195     VI_UNLOCK(vp);
196
197     islocked = VOP_ISLOCKED(vp);
198     if (islocked == LK_EXCLOTHER)
199         panic("Trying to Smush over someone else's lock");
200     else if (islocked == LK_SHARED) {
201         afs_warn("Trying to Smush with a shared lock");
202         vn_lock(vp, LK_UPGRADE);
203     } else if (!islocked)
204         vn_lock(vp, LK_EXCLUSIVE);
205
206     if (vp->v_bufobj.bo_object != NULL) {
207         VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
208         /*
209          * Do we really want OBJPC_SYNC?  OBJPC_INVAL would be
210          * faster, if invalidation is really what we are being
211          * asked to do.  (It would make more sense, too, since
212          * otherwise this function is practically identical to
213          * osi_VM_StoreAllSegments().)  -GAW
214          */
215
216         /*
217          * Dunno.  We no longer resemble osi_VM_StoreAllSegments,
218          * though maybe that's wrong, now.  And OBJPC_SYNC is the
219          * common thing in 70 file systems, it seems.  Matt.
220          */
221
222         vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
223         VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
224     }
225
226     tries = 5;
227     code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
228     while (code && (tries > 0)) {
229         afs_warn("TryToSmush retrying vinvalbuf");
230         code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
231         --tries;
232     }
233     if (islocked == LK_SHARED)
234         vn_lock(vp, LK_DOWNGRADE);
235     else if (!islocked)
236         VOP_UNLOCK(vp, 0);
237 }
238
239 /* Purge VM for a file when its callback is revoked.
240  *
241  * Locking:  No lock is held, not even the global lock.
242  */
243 void
244 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
245 {
246     struct vnode *vp;
247     struct vm_object *obj;
248
249     vp = AFSTOV(avc);
250     ASSERT_VOP_LOCKED(vp, __func__);
251     if (VOP_GETVOBJECT(vp, &obj) == 0) {
252         VM_OBJECT_LOCK(obj);
253         vm_object_page_remove(obj, 0, 0, FALSE);
254         VM_OBJECT_UNLOCK(obj);
255     }
256     osi_vinvalbuf(vp, 0, 0, 0);
257 }
258
259 /* Purge pages beyond end-of-file, when truncating a file.
260  *
261  * Locking:  no lock is held, not even the global lock.
262  * activeV is raised.  This is supposed to block pageins, but at present
263  * it only works on Solaris.
264  */
265 void
266 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
267 {
268     vnode_pager_setsize(AFSTOV(avc), alen);
269 }