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