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