afsconfig-and-rcsid-all-around-20010705
[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 "../afs/param.h"       /* Should be always first */
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"  /* statistics */
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 int
34 osi_VM_FlushVCache(avc, slept)
35     struct vcache *avc;
36     int *slept;
37 {
38     if (avc->vrefCount != 0)
39         return EBUSY;
40
41     if (avc->opens)
42         return EBUSY;
43
44     /* Just in case someone is still referring to the vnode we
45      * give up trying to get rid of this guy */
46     if (avc->vrefCount || CheckLock(&avc->lock) || LockWaiters(&avc->lock))
47         return EBUSY;
48
49     if (avc->segid) {
50         AFS_GUNLOCK();
51         vms_delete(avc->segid);
52         AFS_GLOCK();
53         avc->segid = avc->vmh = NULL;
54     }
55
56     if (avc->credp) {
57         crfree(avc->credp);
58         avc->credp = NULL;
59     }
60
61     /* Free the alloced gnode that was accompanying the vcache's vnode */
62     aix_gnode_rele((struct vnode *)avc);
63
64     return 0;
65 }
66
67 /* Try to store pages to cache, in order to store a file back to the server.
68  *
69  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
70  * re-obtained.
71  */
72 void
73 osi_VM_StoreAllSegments(avc)
74     struct vcache *avc;
75 {
76     if (avc->vmh) {
77         /*
78          * The execsOrWriters test is done so that we don't thrash on
79          * the vm_writep call below. We only initiate a pageout of the
80          * dirty vm pages on the last store...
81          * this is strictly a pragmatic decision, and _does_ break the 
82          * advertised AFS consistency semantics.  Without this hack,
83          * AIX systems panic under heavy load.  I consider the current
84          * behavior a bug introduced to hack around a worse bug. XXX
85          *
86          * Removed do_writep governing sync'ing behavior.
87          */
88         ReleaseWriteLock(&avc->lock);           /* XXX */
89         AFS_GUNLOCK();
90         vm_writep(avc->vmh, 0, MAXFSIZE/PAGESIZE -1 );
91         vms_iowait(avc->vmh);
92         AFS_GLOCK();
93         ObtainWriteLock(&avc->lock,93);         /* XXX */
94         /*
95          * The following is necessary because of the following
96          * asynchronicity: We open a file, write to it and 
97          * close the file
98          * if CCore flag is set, we clear it and do the extra
99          * decrement ourselves now.
100          * If we're called by the CCore clearer, the CCore flag
101          * will already be clear, so we don't have to worry about
102          * clearing it twice.
103          * avc was "VN_HELD" and "crheld" when CCore was set in
104          * afs_FakeClose
105          */
106         if (avc->states & CCore) {
107             avc->states &= ~CCore;
108             avc->opens--;
109             avc->execsOrWriters--;
110             AFS_RELE((struct vnode *)avc);      
111             crfree((struct ucred *)avc->linkData);      
112             avc->linkData = (char *)0;
113         }
114     }
115 }
116
117 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
118  * try to free pages, when deleting a file.
119  *
120  * Locking:  the vcache entry's lock is held.  It may be dropped and 
121  * re-obtained.
122  *
123  * Since we drop and re-obtain the lock, we can't guarantee that there won't
124  * be some pages around when we return, newly created by concurrent activity.
125  */
126 void
127 osi_VM_TryToSmush(avc, acred, sync)
128     struct vcache *avc;
129     struct AFS_UCRED *acred;
130     int sync;
131 {
132     if (avc->segid) {
133         ReleaseWriteLock(&avc->lock);
134         AFS_GUNLOCK();
135         vm_flushp(avc->segid, 0, MAXFSIZE/PAGESIZE - 1);
136         vms_iowait(avc->vmh);           /* XXX Wait?? XXX */
137         AFS_GLOCK();
138         ObtainWriteLock(&avc->lock,60);
139     }
140 }
141
142 /* Purge VM for a file when its callback is revoked.
143  *
144  * Locking:  No lock is held, not even the global lock.
145  */
146 void
147 osi_VM_FlushPages(avc, credp)
148     struct vcache *avc;
149     struct AFS_UCRED *credp;
150 {
151     if (avc->segid) {
152         vm_flushp(avc->segid, 0, MAXFSIZE/PAGESIZE - 1);
153         /*
154          * XXX We probably don't need to wait but better be safe XXX
155          */
156         vms_iowait(avc->vmh);
157     }
158 }
159
160 /* Purge pages beyond end-of-file, when truncating a file.
161  *
162  * Locking:  no lock is held, not even the global lock.
163  * activeV is raised.  This is supposed to block pageins, but at present
164  * it only works on Solaris.
165  */
166 void
167 osi_VM_Truncate(avc, alen, acred)
168     struct vcache *avc;
169     int alen;
170     struct AFS_UCRED *acred;
171 {
172     if (avc->segid) {
173         int firstpage = (alen + PAGESIZE-1)/PAGESIZE;
174         vm_releasep(avc->segid, firstpage, MAXFSIZE/PAGESIZE - firstpage);
175         vms_iowait(avc->vmh);   /* Do we need this? */
176     }
177 }