venus: Remove dedebug
[openafs.git] / src / afs / SOLARIS / 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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* statistics */
17 #include "afs/nfsclient.h"
18
19 /* This file contains Solaris VM-related code for the cache manager. */
20
21 #include <sys/mman.h>
22 #include <vm/hat.h>
23 #include <vm/as.h>
24 #include <vm/page.h>
25 #include <vm/pvn.h>
26 #include <vm/seg.h>
27 #include <vm/seg_map.h>
28 #include <vm/seg_vn.h>
29 #include <vm/rm.h>
30 #include <sys/modctl.h>
31 #include <sys/syscall.h>
32 #include <sys/debug.h>
33 #include <sys/fs_subr.h>
34
35 /* Try to invalidate pages, in order to recycle a dcache entry.
36  *
37  * This function only exists for Solaris.  For other platforms, it's OK to
38  * recycle a dcache entry without invalidating pages, because the strategy
39  * function can call afs_GetDCache().
40  *
41  * Locking:  only the global lock is held on entry.
42  */
43 int
44 osi_VM_GetDownD(struct vcache *avc, struct dcache *adc)
45 {
46     int code;
47
48     AFS_GUNLOCK();
49     code =
50         afs_putpage(AFSTOV(avc), (offset_t) AFS_CHUNKTOBASE(adc->f.chunk),
51                     AFS_CHUNKTOSIZE(adc->f.chunk), B_INVAL, CRED());
52     AFS_GLOCK();
53
54     return code;
55 }
56
57 /* Does this dcache conflict with a multiPage request for this vcache?
58  *
59  * This function only exists for Solaris. This is used by afs_GetDownD to
60  * calculate if trying to evict the given dcache may deadlock with an
61  * in-progress afs_getpage call that is trying to get more than one page at
62  * once. See afs_getpage for details. We return 0 if we do NOT conflict,
63  * nonzero otherwise. If we return nonzero, we should NOT try to evict the
64  * given dcache entry from the cache.
65  *
66  * Locking: tvc->vlock is write-locked on entry (and GLOCK is held)
67  */
68 int
69 osi_VM_MultiPageConflict(struct vcache *avc, struct dcache *adc)
70 {
71     struct multiPage_range *range;
72     for (range = (struct multiPage_range *)avc->multiPage.next;
73          range != &avc->multiPage;
74          range = (struct multiPage_range *)QNext(&range->q)) {
75
76         if (adc->f.chunk >= AFS_CHUNK(range->off) &&
77             adc->f.chunk <= AFS_CHUNK(range->off + range->len - 1)) {
78             return 1;
79         }
80     }
81
82     return 0;
83 }
84
85 /* Try to discard pages, in order to recycle a vcache entry.
86  *
87  * We also make some sanity checks:  ref count, open count, held locks.
88  *
89  * We also do some non-VM-related chores, such as releasing the cred pointer
90  * (for AIX and Solaris) and releasing the gnode (for AIX).
91  *
92  * Locking:  afs_xvcache lock is held. It must not be dropped.
93  */
94 int
95 osi_VM_FlushVCache(struct vcache *avc)
96 {
97     if (avc->vrefCount != 0)
98         return EBUSY;
99
100     if (avc->opens)
101         return EBUSY;
102
103     /* if a lock is held, give up */
104     if (CheckLock(&avc->lock))
105         return EBUSY;
106
107     AFS_GUNLOCK();
108     pvn_vplist_dirty(AFSTOV(avc), 0, NULL, B_TRUNC | B_INVAL, CRED());
109     AFS_GLOCK();
110
111     /* Might as well make the obvious check */
112     if (AFSTOV(avc)->v_pages)
113         return EBUSY;           /* should be all gone still */
114
115     rw_destroy(&avc->rwlock);
116     if (avc->credp) {
117         crfree(avc->credp);
118         avc->credp = NULL;
119     }
120
121
122     return 0;
123 }
124
125 /* Try to store pages to cache, in order to store a file back to the server.
126  *
127  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
128  * re-obtained.
129  */
130 void
131 osi_VM_StoreAllSegments(struct vcache *avc)
132 {
133     AFS_GUNLOCK();
134     (void)pvn_vplist_dirty(AFSTOV(avc), (u_offset_t) 0, afs_putapage, 0,
135                            CRED());
136     AFS_GLOCK();
137 }
138
139 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
140  * try to free pages, when deleting a file.
141  *
142  * Locking:  the vcache entry's lock is held.  It may be dropped and
143  * re-obtained.
144  */
145 void
146 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
147 {
148     AFS_GUNLOCK();
149     (void)pvn_vplist_dirty(AFSTOV(avc), (u_offset_t) 0, afs_putapage,
150                            (sync ? B_INVAL : B_FREE), acred);
151     AFS_GLOCK();
152 }
153
154 /* Purge VM for a file when its callback is revoked.
155  *
156  * Locking:  No lock is held, not even the global lock.
157  */
158 void
159 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
160 {
161     extern int afs_pvn_vptrunc;
162
163     afs_pvn_vptrunc++;
164     (void)afs_putpage(AFSTOV(avc), (offset_t) 0, 0, B_TRUNC | B_INVAL, credp);
165 }
166
167 /* Zero no-longer-used part of last page, when truncating a file
168  *
169  * This function only exists for Solaris.  Other platforms do not support it.
170  *
171  * Locking:  the vcache entry lock is held.  It is released and re-obtained.
172  * The caller will raise activeV (to prevent pageins), but this function must
173  * be called first, since it causes a pagein.
174  */
175 void
176 osi_VM_PreTruncate(struct vcache *avc, int alen, afs_ucred_t *acred)
177 {
178     page_t *pp;
179     int pageOffset = (alen & PAGEOFFSET);
180
181     if (pageOffset == 0) {
182         return;
183     }
184
185     ReleaseWriteLock(&avc->lock);
186     AFS_GUNLOCK();
187     pp = page_lookup(AFSTOV(avc), alen - pageOffset, SE_EXCL);
188     if (pp) {
189         pagezero(pp, pageOffset, PAGESIZE - pageOffset);
190         page_unlock(pp);
191     }
192     AFS_GLOCK();
193     ObtainWriteLock(&avc->lock, 563);
194 }
195
196 /* Purge pages beyond end-of-file, when truncating a file.
197  *
198  * Locking:  no lock is held, not even the global lock.
199  * Pageins are blocked (activeV is raised).
200  */
201 void
202 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
203 {
204     /*
205      * It's OK to specify afs_putapage here, even though we aren't holding
206      * the vcache entry lock, because it isn't going to get called.
207      */
208     pvn_vplist_dirty(AFSTOV(avc), alen, afs_putapage, B_TRUNC | B_INVAL,
209                      acred);
210 }