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