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