1b57c767ef5b8888fdf00e945813c87506f15eae
[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
24 RCSID
25     ("$Header$");
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_FBSD50_ENV
52 #define lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curthread)
53 #define unlock_vnode(v) VOP_UNLOCK((v), 0, curthread)
54 #else
55 #define lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curproc)
56 #define unlock_vnode(v) VOP_UNLOCK((v), 0, curproc)
57 /* need splvm() protection? */
58 #define VM_OBJECT_LOCK(o)
59 #define VM_OBJECT_UNLOCK(o)
60 #endif
61
62 /* Try to discard pages, in order to recycle a vcache entry.
63  *
64  * We also make some sanity checks:  ref count, open count, held locks.
65  *
66  * We also do some non-VM-related chores, such as releasing the cred pointer
67  * (for AIX and Solaris) and releasing the gnode (for AIX).
68  *
69  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
70  *   *slept should be set to warn the caller.
71  *
72  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
73  * is not dropped and re-acquired for any platform.  It may be that *slept is
74  * therefore obsolescent.
75  *
76  * OSF/1 Locking:  VN_LOCK has been called.
77  * XXX - should FreeBSD have done this, too?  Certainly looks like it.
78  * Maybe better to just call vnode_pager_setsize()?
79  */
80 int
81 osi_VM_FlushVCache(struct vcache *avc, int *slept)
82 {
83     struct vm_object *obj;
84     struct vnode *vp;
85     if (VREFCOUNT(avc) > 1)
86         return EBUSY;
87
88     if (avc->opens)
89         return EBUSY;
90
91     /* if a lock is held, give up */
92     if (CheckLock(&avc->lock))
93         return EBUSY;
94
95     AFS_GUNLOCK();
96     vp = AFSTOV(avc);
97     lock_vnode(vp);
98     if (VOP_GETVOBJECT(vp, &obj) == 0) {
99         VM_OBJECT_LOCK(obj);
100         vm_object_page_remove(obj, 0, 0, FALSE);
101 #if 0
102         if (obj->ref_count == 0) {
103             vgonel(vp, curproc);
104             simple_lock(&vp->v_interlock);
105             vp->v_tag = VT_AFS;
106             SetAfsVnode(vp);
107         }
108 #endif
109         VM_OBJECT_UNLOCK(obj);
110     }
111     unlock_vnode(vp);
112     AFS_GLOCK();
113
114     return 0;
115 }
116
117 /* Try to store pages to cache, in order to store a file back to the server.
118  *
119  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
120  * re-obtained.
121  */
122 void
123 osi_VM_StoreAllSegments(struct vcache *avc)
124 {
125     struct vnode *vp;
126     struct vm_object *obj;
127     int anyio, tries;
128
129     ReleaseWriteLock(&avc->lock);
130     AFS_GUNLOCK();
131     tries = 5;
132     vp = AFSTOV(avc);
133
134     /*
135      * I don't understand this.  Why not just call vm_object_page_clean()
136      * and be done with it?  I particularly don't understand why we're calling
137      * vget() here.  Is there some reason to believe that the vnode might
138      * be being recycled at this point?  I don't think there's any need for
139      * this loop, either -- if we keep the vnode locked all the time,
140      * that and the object lock will prevent any new pages from appearing.
141      * The loop is what causes the race condition.  -GAW
142      */
143     do {
144         anyio = 0;
145         lock_vnode(vp);
146         if (VOP_GETVOBJECT(vp, &obj) == 0 && (obj->flags & OBJ_MIGHTBEDIRTY)) {
147             /* XXX - obj locking? */
148             unlock_vnode(vp);
149 #ifdef AFS_FBSD50_ENV
150             if (!vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread)) {
151 #else
152             if (!vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
153 #endif
154                 if (VOP_GETVOBJECT(vp, &obj) == 0) {
155                     VM_OBJECT_LOCK(obj);
156                     vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
157                     VM_OBJECT_UNLOCK(obj);
158                     anyio = 1;
159                 }
160                 vput(vp);
161             }
162         } else
163             unlock_vnode(vp);
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, struct AFS_UCRED *acred, int sync)
180 {
181     struct vnode *vp;
182     struct vm_object *obj;
183     int anyio, tries;
184
185     ReleaseWriteLock(&avc->lock);
186     AFS_GUNLOCK();
187     tries = 5;
188     vp = AFSTOV(avc);
189     do {
190         anyio = 0;
191         lock_vnode(vp);
192         /* See the comments above. */
193         if (VOP_GETVOBJECT(vp, &obj) == 0 && (obj->flags & OBJ_MIGHTBEDIRTY)) {
194             /* XXX - obj locking */
195             unlock_vnode(vp);
196 #ifdef AFS_FBSD50_ENV
197             if (!vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread)) {
198 #else
199             if (!vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
200 #endif
201                 if (VOP_GETVOBJECT(vp, &obj) == 0) {
202                     VM_OBJECT_LOCK(obj);
203                     /*
204                      * Do we really want OBJPC_SYNC?  OBJPC_INVAL would be
205                      * faster, if invalidation is really what we are being
206                      * asked to do.  (It would make more sense, too, since
207                      * otherwise this function is practically identical to
208                      * osi_VM_StoreAllSegments().)  -GAW
209                      */
210                     vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
211                     VM_OBJECT_UNLOCK(obj);
212                     anyio = 1;
213                 }
214                 vput(vp);
215             }
216         } else
217             unlock_vnode(vp);
218     } while (anyio && (--tries > 0));
219     lock_vnode(vp);
220     if (VOP_GETVOBJECT(vp, &obj) == 0) {
221         VM_OBJECT_LOCK(obj);
222         vm_object_page_remove(obj, 0, 0, FALSE);
223         VM_OBJECT_UNLOCK(obj);
224     }
225     unlock_vnode(vp);
226     /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0); */
227     AFS_GLOCK();
228     ObtainWriteLock(&avc->lock, 59);
229 }
230
231 /* Purge VM for a file when its callback is revoked.
232  *
233  * Locking:  No lock is held, not even the global lock.
234  */
235 void
236 osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
237 {
238     struct vnode *vp;
239     struct vm_object *obj;
240
241     vp = AFSTOV(avc);
242     ASSERT_VOP_LOCKED(vp, __func__);
243     if (VOP_GETVOBJECT(vp, &obj) == 0) {
244         VM_OBJECT_LOCK(obj);
245         vm_object_page_remove(obj, 0, 0, FALSE);
246         VM_OBJECT_UNLOCK(obj);
247     }
248     /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0); */
249 }
250
251 /* Purge pages beyond end-of-file, when truncating a file.
252  *
253  * Locking:  no lock is held, not even the global lock.
254  * activeV is raised.  This is supposed to block pageins, but at present
255  * it only works on Solaris.
256  */
257 void
258 osi_VM_Truncate(struct vcache *avc, int alen, struct AFS_UCRED *acred)
259 {
260     vnode_pager_setsize(AFSTOV(avc), alen);
261 }