5cc2beb3fa3772a25788205d1deb208a9113ef4e
[openafs.git] / src / afs / NBSD / 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 #include "afs/sysincludes.h"    /* Standard vendor system headers */
25 #include "afs/afsincludes.h"    /* Afs-based standard headers */
26 #include "afs/afs_stats.h"      /* statistics */
27
28 /* Try to discard pages, in order to recycle a vcache entry.
29  *
30  * We also make some sanity checks:  ref count, open count, held locks.
31  *
32  * We also do some non-VM-related chores, such as releasing the cred pointer
33  * (for AIX and Solaris) and releasing the gnode (for AIX).
34  *
35  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
36  *   *slept should be set to warn the caller.
37  *
38  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
39  * is not dropped and re-acquired for any platform.  It may be that *slept is
40  * therefore obsolescent.
41  *
42  * OSF/1 Locking:  VN_LOCK has been called.
43  */
44 int
45 osi_VM_FlushVCache(struct vcache *avc, int *slept)
46 {
47     struct vnode *vp = AFSTOV(avc);
48
49     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
50         printf("%s enter\n", __func__);
51     }
52
53     if (vp == NULL) {
54         printf("%s NULL vp\n", __func__);
55         return 0;
56     }
57
58     AFS_GUNLOCK();
59     cache_purge(vp);
60     vflushbuf(vp, 1);
61     AFS_GLOCK();
62
63     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
64         printf("%s exit\n", __func__);
65     }
66
67     return 0;
68 }
69
70 /* Try to store pages to cache, in order to store a file back to the server.
71  *
72  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
73  * re-obtained.
74  */
75 void
76 osi_VM_StoreAllSegments(struct vcache *avc)
77 {
78     struct vnode *vp;
79
80     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
81         printf("%s enter\n", __func__);
82     }
83
84     ReleaseWriteLock(&avc->lock);
85     AFS_GUNLOCK();
86     vp = AFSTOV(avc);
87     mutex_enter(&vp->v_interlock);
88     VOP_PUTPAGES(vp, 0, 0, PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO);
89     AFS_GLOCK();
90     ObtainWriteLock(&avc->lock, 94);
91     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
92         printf("%s exit\n", __func__);
93     }
94 }
95
96 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
97  * try to free pages, when deleting a file.
98  *
99  * Locking:  the vcache entry's lock is held.  It may be dropped and
100  * re-obtained.
101  *
102  * Since we drop and re-obtain the lock, we can't guarantee that there won't
103  * be some pages around when we return, newly created by concurrent activity.
104  */
105 void
106 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
107 {
108     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
109         printf("%s enter\n", __func__);
110     }
111
112     ReleaseWriteLock(&avc->lock);
113     osi_VM_FlushVCache(avc, NULL);
114     ObtainWriteLock(&avc->lock, 59);
115
116     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
117         printf("%s exit\n", __func__);
118     }
119 }
120
121 /* Purge VM for a file when its callback is revoked.
122  *
123  * Locking:  No lock is held, not even the global lock.
124  */
125 void
126 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
127 {
128     struct vnode *vp = AFSTOV(avc);
129
130     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
131         printf("%s enter\n", __func__);
132     }
133
134     if (!vp) {
135         printf("%s NULL vp\n", __func__);
136         return;
137     }
138
139     cache_purge(vp);
140     vinvalbuf(vp, 0, credp, curlwp, false, 1);
141
142     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
143         printf("%s exit\n", __func__);
144     }
145 }
146
147 /* Purge pages beyond end-of-file, when truncating a file.
148  *
149  * Locking:  no lock is held, not even the global lock.
150  * activeV is raised.  This is supposed to block pageins, but at present
151  * it only works on Solaris.
152  */
153 void
154 osi_VM_Truncate(struct vcache *avc, voff_t alen, afs_ucred_t *acred)
155 {
156     struct vnode *vp = AFSTOV(avc);
157
158     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
159         printf("%s enter\n", __func__);
160     }
161
162     vtruncbuf(vp, alen, false, 0);
163
164     if ((afs_debug & AFSDEB_VNLAYER) != 0) {
165         printf("%s exit\n", __func__);
166     }
167 }