Make typedefs of AFS_UCRED and AFS_PROC with renaming
[openafs.git] / src / afs / AIX / 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
18 /* Try to discard pages, in order to recycle a vcache entry.
19  *
20  * We also make some sanity checks:  ref count, open count, held locks.
21  *
22  * We also do some non-VM-related chores, such as releasing the cred pointer
23  * (for AIX and Solaris) and releasing the gnode (for AIX).
24  *
25  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
26  *   *slept should be set to warn the caller.
27  *
28  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
29  * is not dropped and re-acquired for any platform.  It may be that *slept is
30  * therefore obsolescent.
31  */
32 int
33 osi_VM_FlushVCache(struct vcache *avc, int *slept)
34 {
35     if (avc->vrefCount != 0)
36         return EBUSY;
37
38     if (avc->opens)
39         return EBUSY;
40
41     /* Just in case someone is still referring to the vnode we
42      * give up trying to get rid of this guy */
43     if (avc->vrefCount || CheckLock(&avc->lock) || LockWaiters(&avc->lock))
44         return EBUSY;
45
46     if (avc->segid) {
47         AFS_GUNLOCK();
48         vms_delete(avc->segid);
49         AFS_GLOCK();
50         avc->segid = avc->vmh = NULL;
51     }
52
53     if (avc->credp) {
54         crfree(avc->credp);
55         avc->credp = NULL;
56     }
57
58     /* Free the alloced gnode that was accompanying the vcache's vnode */
59     aix_gnode_rele(AFSTOV(avc));
60
61     return 0;
62 }
63
64 /* Try to store pages to cache, in order to store a file back to the server.
65  *
66  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
67  * re-obtained.
68  */
69 void
70 osi_VM_StoreAllSegments(struct vcache *avc)
71 {
72     if (avc->segid) {
73         /*
74          * The execsOrWriters test is done so that we don't thrash on
75          * the vm_writep call below. We only initiate a pageout of the
76          * dirty vm pages on the last store...
77          * this is strictly a pragmatic decision, and _does_ break the 
78          * advertised AFS consistency semantics.  Without this hack,
79          * AIX systems panic under heavy load.  I consider the current
80          * behavior a bug introduced to hack around a worse bug. XXX
81          *
82          * Removed do_writep governing sync'ing behavior.
83          */
84         ReleaseWriteLock(&avc->lock);   /* XXX */
85         AFS_GUNLOCK();
86         vm_writep(avc->segid, 0, MAXFSIZE / PAGESIZE - 1);
87         vms_iowait(avc->segid);
88         AFS_GLOCK();
89         ObtainWriteLock(&avc->lock, 93);        /* XXX */
90         /*
91          * The following is necessary because of the following
92          * asynchronicity: We open a file, write to it and 
93          * close the file
94          * if CCore flag is set, we clear it and do the extra
95          * decrement ourselves now.
96          * If we're called by the CCore clearer, the CCore flag
97          * will already be clear, so we don't have to worry about
98          * clearing it twice.
99          * avc was "VN_HELD" and "crheld" when CCore was set in
100          * afs_FakeClose
101          */
102         if (avc->f.states & CCore) {
103             avc->f.states &= ~CCore;
104             avc->opens--;
105             avc->execsOrWriters--;
106             AFS_RELE(AFSTOV(avc));
107             crfree((struct ucred *)avc->linkData);
108             avc->linkData = NULL;
109         }
110     }
111 }
112
113 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
114  * try to free pages, when deleting a file.
115  *
116  * Locking:  the vcache entry's lock is held.  It may be dropped and 
117  * re-obtained.
118  *
119  * Since we drop and re-obtain the lock, we can't guarantee that there won't
120  * be some pages around when we return, newly created by concurrent activity.
121  */
122 void
123 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
124 {
125     if (avc->segid) {
126         ReleaseWriteLock(&avc->lock);
127         AFS_GUNLOCK();
128         vm_flushp(avc->segid, 0, MAXFSIZE / PAGESIZE - 1);
129         vms_iowait(avc->segid); /* XXX Wait?? XXX */
130         AFS_GLOCK();
131         ObtainWriteLock(&avc->lock, 60);
132     }
133 }
134
135 /* Purge VM for a file when its callback is revoked.
136  *
137  * Locking:  No lock is held, not even the global lock.
138  */
139 void
140 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
141 {
142     if (avc->segid) {
143         vm_flushp(avc->segid, 0, MAXFSIZE / PAGESIZE - 1);
144         /*
145          * XXX We probably don't need to wait but better be safe XXX
146          */
147         vms_iowait(avc->segid);
148     }
149 }
150
151 /* Purge pages beyond end-of-file, when truncating a file.
152  *
153  * Locking:  no lock is held, not even the global lock.
154  * activeV is raised.  This is supposed to block pageins, but at present
155  * it only works on Solaris.
156  */
157 void
158 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
159 {
160     if (avc->segid) {
161         int firstpage = (alen + PAGESIZE - 1) / PAGESIZE;
162         vm_releasep(avc->segid, firstpage, MAXFSIZE / PAGESIZE - firstpage);
163         vms_iowait(avc->segid); /* Do we need this? */
164     }
165 }