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