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