Make typedefs of AFS_UCRED and AFS_PROC with renaming
[openafs.git] / src / afs / DARWIN / 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 <sys/ubc.h>
18
19 /* Try to discard pages, in order to recycle a vcache entry.
20  *
21  * We also make some sanity checks:  ref count, open count, held locks.
22  *
23  * We also do some non-VM-related chores, such as releasing the cred pointer
24  * (for AIX and Solaris) and releasing the gnode (for AIX).
25  *
26  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
27  *   *slept should be set to warn the caller.
28  *
29  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
30  * is not dropped and re-acquired for any platform.  It may be that *slept is
31  * therefore obsolescent.
32  *
33  * OSF/1 Locking:  VN_LOCK has been called.
34  */
35 int
36 osi_VM_FlushVCache(struct vcache *avc, int *slept)
37 {
38     struct vnode *vp = AFSTOV(avc);
39     kern_return_t kret;
40     off_t size;
41
42     if (!vp)
43         return 0;
44     AFS_GUNLOCK();
45 #if 0
46     if (!(UBCINFOMISSING(vp) || UBCINFORECLAIMED(vp))) {
47       size=ubc_getsize(vp);
48       kret=ubc_invalidate(vp,0,size); 
49     }
50 #endif
51     cache_purge(vp);
52     AFS_GLOCK();
53
54     return 0;
55 }
56
57
58 /* Try to store pages to cache, in order to store a file back to the server.
59  *
60  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
61  * re-obtained.
62  */
63 void
64 osi_VM_StoreAllSegments(struct vcache *avc)
65 {
66     struct vnode *vp = AFSTOV(avc);
67     ReleaseWriteLock(&avc->lock);
68     AFS_GUNLOCK();
69 #ifdef AFS_DARWIN80_ENV
70     ubc_msync_range(vp, 0, ubc_getsize(vp), UBC_SYNC|UBC_PUSHDIRTY);
71 #else
72     if (UBCINFOEXISTS(vp)) {
73         ubc_pushdirty(vp);
74     }
75 #endif
76     AFS_GLOCK();
77     ObtainWriteLock(&avc->lock, 94);
78 }
79
80 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
81  * try to free pages, when deleting a file.
82  *
83  * Locking:  the vcache entry's lock is held.  It may be dropped and 
84  * re-obtained.
85  *
86  * Since we drop and re-obtain the lock, we can't guarantee that there won't
87  * be some pages around when we return, newly created by concurrent activity.
88  */
89 void
90 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
91 {
92     struct vnode *vp = AFSTOV(avc);
93     void *object;
94     kern_return_t kret;
95     off_t size, lastpg;
96
97     ReleaseWriteLock(&avc->lock);
98     AFS_GUNLOCK();
99 #ifdef AFS_DARWIN80_ENV
100     ubc_msync_range(vp, 0, ubc_getsize(vp), UBC_INVALIDATE);
101 #else
102     if (UBCINFOEXISTS(vp)) {
103         size = ubc_getsize(vp);
104         kret = ubc_invalidate(vp, 0, size);
105         if (kret != 1)          /* should be KERN_SUCCESS */
106             printf("TryToSmush: invalidate failed (error = %d)\n", kret);
107     }
108 #endif
109     AFS_GLOCK();
110     ObtainWriteLock(&avc->lock, 59);
111 }
112
113 /* Purge VM for a file when its callback is revoked.
114  *
115  * Locking:  No lock is held, not even the global lock.
116  */
117 /* XXX this seems to not be referenced anywhere. *somebody* ought to be calling
118    this, and also making sure that ubc's idea of the filesize is right more
119    often */
120 void
121 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
122 {
123     struct vnode *vp = AFSTOV(avc);
124     void *object;
125     kern_return_t kret;
126     off_t size;
127 #ifdef AFS_DARWIN80_ENV
128     size = ubc_getsize(vp);
129     ubc_msync_range(vp, 0, size, UBC_INVALIDATE);
130         /* XXX what about when not CStatd */
131     if (avc->f.states & CStatd && size != avc->f.m.Length)
132        ubc_setsize(vp, avc->f.m.Length);
133 #else
134     if (UBCINFOEXISTS(vp)) {
135         size = ubc_getsize(vp);
136         kret = ubc_invalidate(vp, 0, size);
137         if (kret != 1)          /* Should be KERN_SUCCESS */
138             printf("VMFlushPages: invalidate failed (error = %d)\n", kret);
139         /* XXX what about when not CStatd */
140         if (avc->f.states & CStatd && size != avc->f.m.Length)
141           if (UBCISVALID(vp))
142             ubc_setsize(vp, avc->f.m.Length);
143     }
144 #endif
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, int alen, afs_ucred_t *acred)
155 {
156     struct vnode *vp = AFSTOV(avc);
157 #ifdef AFS_DARWIN80_ENV
158     ubc_setsize(vp, alen);
159 #else
160     if (UBCINFOEXISTS(vp) && UBCISVALID(vp)) {
161         ubc_setsize(vp, alen);
162     }
163 #endif
164 }
165
166 void
167 osi_VM_NukePages(struct vnode *vp, off_t offset, off_t size)
168 {
169 #if 0
170     void *object;
171     struct vcache *avc = VTOAFS(vp);
172
173     offset = trunc_page(offset);
174     size = round_page(size + 1);
175     while (size) {
176         ubc_page_op(vp, (vm_offset_t) offset,
177                     UPL_POP_SET | UPL_POP_BUSY | UPL_POP_DUMP, 0, 0);
178         size -= PAGE_SIZE;
179         offset += PAGE_SIZE;
180     }
181 #endif
182 }
183
184 int
185 osi_VM_Setup(struct vcache *avc, int force)
186 {
187     int error;
188     struct vnode *vp = AFSTOV(avc);
189
190 #ifndef AFS_DARWIN80_ENV
191     if (UBCISVALID(vp) && ((avc->f.states & CStatd) || force)) {
192         if (!UBCINFOEXISTS(vp)) {
193             osi_vnhold(avc, 0);
194             avc->f.states |= CUBCinit;
195             AFS_GUNLOCK();
196             if ((error = ubc_info_init(vp))) {
197                 AFS_GLOCK();
198                 avc->f.states &= ~CUBCinit;
199                 AFS_RELE(vp);
200                 return error;
201             }
202             AFS_GLOCK();
203             avc->f.states &= ~CUBCinit;
204             AFS_RELE(vp);
205         }
206         if (UBCINFOEXISTS(vp) && UBCISVALID(vp)) {
207             ubc_setsize(vp, avc->f.m.Length);
208         }
209     }
210 #endif
211     return 0;
212 }