cedcb97a768c67b3ade10cdb750267606e8a4e6a
[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("$Header$");
25
26 #include "../afs/sysincludes.h" /* Standard vendor system headers */
27 #include "../afs/afsincludes.h" /* Afs-based standard headers */
28 #include "../afs/afs_stats.h"  /* statistics */
29 #include <vm/vm_object.h>
30 #include <vm/vm_map.h>
31 #include <limits.h>
32 #include <float.h>
33
34 /* Try to discard pages, in order to recycle a vcache entry.
35  *
36  * We also make some sanity checks:  ref count, open count, held locks.
37  *
38  * We also do some non-VM-related chores, such as releasing the cred pointer
39  * (for AIX and Solaris) and releasing the gnode (for AIX).
40  *
41  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
42  *   *slept should be set to warn the caller.
43  *
44  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
45  * is not dropped and re-acquired for any platform.  It may be that *slept is
46  * therefore obsolescent.
47  *
48  * OSF/1 Locking:  VN_LOCK has been called.
49  */
50 int
51 osi_VM_FlushVCache(avc, slept)
52     struct vcache *avc;
53     int *slept;
54 {
55     struct vm_object *obj;
56     struct vnode *vp;
57     if (VREFCOUNT(avc) > 1)
58         return EBUSY;
59
60     if (avc->opens)
61         return EBUSY;
62
63     /* if a lock is held, give up */
64     if (CheckLock(&avc->lock) || afs_CheckBozonLock(&avc->pvnLock))
65         return EBUSY;
66
67     AFS_GUNLOCK();
68     vp=avc;
69     simple_lock(&vp->v_interlock);
70     if (VOP_GETVOBJECT(vp, &obj) == 0) {
71        vm_object_page_remove(obj, 0, 0, FALSE);
72 #if 0
73         if (obj->ref_count == 0) {
74            vgonel(vp,curproc);
75            simple_lock(&vp->v_interlock);
76            vp->v_tag=VT_AFS;
77            SetAfsVnode(vp);
78         }
79 #endif
80     } 
81     simple_unlock(&vp->v_interlock);
82     AFS_GLOCK();
83
84     return 0;
85 }
86
87 /* Try to store pages to cache, in order to store a file back to the server.
88  *
89  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
90  * re-obtained.
91  */
92 void
93 osi_VM_StoreAllSegments(avc)
94     struct vcache *avc;
95 {
96     struct vnode *vp;
97     struct vm_object *obj;
98     int anyio,tries;
99     ReleaseWriteLock(&avc->lock);
100     AFS_GUNLOCK();
101     tries=5;
102     vp=avc;
103     do {
104     anyio=0;
105     simple_lock(&vp->v_interlock);
106     if (VOP_GETVOBJECT(vp, &obj) == 0 &&
107        (obj->flags & OBJ_MIGHTBEDIRTY)) {
108         if (!vget(vp,
109             LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
110             if (VOP_GETVOBJECT(vp, &obj) == 0) {
111                vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
112                anyio = 1;
113             }
114             vput(vp);
115         }
116     } else {
117         simple_unlock(&vp->v_interlock);
118     }
119     } while (anyio && (--tries > 0));
120     AFS_GLOCK();
121     ObtainWriteLock(&avc->lock,94);
122 }
123
124 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
125  * try to free pages, when deleting a file.
126  *
127  * Locking:  the vcache entry's lock is held.  It may be dropped and 
128  * re-obtained.
129  *
130  * Since we drop and re-obtain the lock, we can't guarantee that there won't
131  * be some pages around when we return, newly created by concurrent activity.
132  */
133 void
134 osi_VM_TryToSmush(avc, acred, sync)
135     struct vcache *avc;
136     struct AFS_UCRED *acred;
137     int sync;
138 {
139     struct vnode *vp;
140     struct vm_object *obj;
141     int anyio, tries;
142     ReleaseWriteLock(&avc->lock);
143     AFS_GUNLOCK();
144     tries=5;
145     vp=avc;
146     do {
147        anyio=0;
148        simple_lock(&vp->v_interlock);
149        if (VOP_GETVOBJECT(vp, &obj) == 0 &&
150           (obj->flags & OBJ_MIGHTBEDIRTY)) {
151            if (!vget(vp,
152                LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
153                if (VOP_GETVOBJECT(vp, &obj) == 0) {
154                   vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
155                   anyio = 1;
156                }
157                vput(vp);
158            }
159        } else {
160            simple_unlock(&vp->v_interlock);
161        }
162     } while (anyio && (--tries > 0));
163     simple_lock(&vp->v_interlock);
164     if (VOP_GETVOBJECT(vp, &obj) == 0) {
165        vm_object_page_remove(obj, 0, 0, FALSE);
166     }
167     simple_unlock(&vp->v_interlock);
168     /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0);*/
169     AFS_GLOCK();
170     ObtainWriteLock(&avc->lock,59);
171 }
172
173 /* Purge VM for a file when its callback is revoked.
174  *
175  * Locking:  No lock is held, not even the global lock.
176  */
177 void
178 osi_VM_FlushPages(avc, credp)
179     struct vcache *avc;
180     struct AFS_UCRED *credp;
181 {
182     struct vnode *vp;
183     struct vm_object *obj;
184     vp=avc;
185     simple_lock(&vp->v_interlock);
186     if (VOP_GETVOBJECT(vp, &obj) == 0) {
187        vm_object_page_remove(obj, 0, 0, FALSE);
188     }
189     simple_unlock(&vp->v_interlock);
190     /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0);*/
191 }
192
193 /* Purge pages beyond end-of-file, when truncating a file.
194  *
195  * Locking:  no lock is held, not even the global lock.
196  * activeV is raised.  This is supposed to block pageins, but at present
197  * it only works on Solaris.
198  */
199 void
200 osi_VM_Truncate(avc, alen, acred)
201     struct vcache *avc;
202     int alen;
203     struct AFS_UCRED *acred;
204 {
205     vnode_pager_setsize(AFSTOV(avc), alen);
206 }