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