d53944be22bbe625cd17173b76e2c7f35619a7b3
[openafs.git] / src / afs / IRIX / 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 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 #include "sys/flock.h"          /* for IGN_PID */
19
20 extern struct vnodeops Afs_vnodeops;
21
22 /* Try to discard pages, in order to recycle a vcache entry.
23  *
24  * We also make some sanity checks:  ref count, open count, held locks.
25  *
26  * We also do some non-VM-related chores, such as releasing the cred pointer
27  * (for AIX and Solaris) and releasing the gnode (for AIX).
28  *
29  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
30  *   *slept should be set to warn the caller.
31  *
32  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
33  * is not dropped and re-acquired for any platform.  It may be that *slept is
34  * therefore obsolescent.
35  */
36 int
37 osi_VM_FlushVCache(avc, slept)
38     struct vcache *avc;
39     int *slept;
40 {
41     int s, code;
42     vnode_t *vp = &avc->v;
43
44     if (avc->vrefCount != 0)
45         return EBUSY;
46
47     if (avc->opens != 0)
48         return EBUSY;
49
50     /*
51      * Just in case someone is still referring to the vnode we give up
52      * trying to get rid of this guy.
53      */
54     if (CheckLock(&avc->lock) || LockWaiters(&avc->lock))
55         return EBUSY;
56
57     s = VN_LOCK(vp);
58
59     /*
60      * we just need to avoid the race
61      * in vn_rele between the ref count going to 0 and VOP_INACTIVE
62      * finishing up.
63      * Note that although we checked vcount above, we didn't have the lock
64      */
65     if (vp->v_count > 0 || (vp->v_flag & VINACT)) {
66         VN_UNLOCK(vp, s);
67         return EBUSY;
68     }
69     VN_UNLOCK(vp, s);
70
71     /*
72      * Since we store on last close and on VOP_INACTIVE
73      * there should be NO dirty pages
74      * Note that we hold the xvcache lock the entire time.
75      */
76     AFS_GUNLOCK();
77     PTOSSVP(vp, (off_t)0, (off_t)MAXLONG);
78     AFS_GLOCK();
79
80     /* afs_chkpgoob will drop and re-acquire the global lock. */
81     afs_chkpgoob(vp, 0);
82     osi_Assert(!VN_GET_PGCNT(vp));
83     osi_Assert(!AFS_VN_MAPPED(vp));
84     osi_Assert(!AFS_VN_DIRTY(&avc->v));
85
86 #if defined(AFS_SGI65_ENV)
87     if (vp->v_filocks)
88         cleanlocks(vp, IGN_PID, 0);
89     mutex_destroy(&vp->v_filocksem);
90 #else /* AFS_SGI65_ENV */
91     if (vp->v_filocksem) {
92         if (vp->v_filocks)
93 #ifdef AFS_SGI64_ENV
94                 cleanlocks(vp, &curprocp->p_flid);
95 #else
96                 cleanlocks(vp, IGN_PID, 0);
97 #endif
98         osi_Assert(vp->v_filocks == NULL);
99         mutex_destroy(vp->v_filocksem);
100         kmem_free(vp->v_filocksem, sizeof *vp->v_filocksem);
101         vp->v_filocksem = NULL;
102     }
103 #endif /* AFS_SGI65_ENV */
104
105     if (avc->vrefCount) osi_Panic("flushVcache: vm race");
106 #ifdef AFS_SGI64_ENV
107     AFS_GUNLOCK();
108     vnode_pcache_reclaim(vp); /* this can sleep */
109     vnode_pcache_free(vp);
110     if (vp->v_op != &Afs_vnodeops) {
111         VOP_RECLAIM(vp, FSYNC_WAIT, code);
112     }
113     AFS_GLOCK();
114 #ifdef AFS_SGI65_ENV
115 #ifdef VNODE_TRACING
116     ktrace_free(vp->v_trace);
117 #endif /* VNODE_TRACING */
118     vn_bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
119     vn_bhv_head_destroy(&(vp->v_bh));
120     destroy_bitlock(&vp->v_pcacheflag);
121     mutex_destroy(&vp->v_buf_lock);
122 #else
123     bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
124     bhv_head_destroy(&(vp->v_bh));
125 #endif
126     vp->v_flag = 0;     /* debug */
127 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
128     destroy_bitlock(&vp->v_flag);
129 #endif
130 #ifdef INTR_KTHREADS
131     AFS_VN_DESTROY_BUF_LOCK(vp);
132 #endif
133 #endif /* AFS_SGI64_ENV */
134
135     return 0;
136 }
137
138 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
139  * try to free pages, when deleting a file.
140  *
141  * Locking:  the vcache entry's lock is held.  It may be dropped and 
142  * re-obtained.
143  *
144  * Since we drop and re-obtain the lock, we can't guarantee that there won't
145  * be some pages around when we return, newly created by concurrent activity.
146  */
147 void
148 osi_VM_TryToSmush(avc, acred, sync)
149     struct vcache *avc;
150     struct AFS_UCRED *acred;
151     int sync;
152 {
153     ReleaseWriteLock(&avc->lock);
154     AFS_GUNLOCK();
155     /* current remapf restriction - cannot have VOP_RWLOCK */
156     osi_Assert(OSI_GET_LOCKID() != avc->vc_rwlockid);
157     if (((vnode_t *)avc)->v_type == VREG && AFS_VN_MAPPED(((vnode_t *)avc)))
158         remapf(((vnode_t *)avc), 0, 0);
159     PTOSSVP(AFSTOV(avc), (off_t)0, (off_t)MAXLONG);
160     AFS_GLOCK();
161     ObtainWriteLock(&avc->lock,62);
162 }
163
164 /* Flush and invalidate pages, for fsync() with INVAL flag
165  *
166  * Locking:  only the global lock is held.
167  */
168 void
169 osi_VM_FSyncInval(avc)
170     struct vcache *avc;
171 {
172     AFS_GUNLOCK();
173     PFLUSHINVALVP((vnode_t *)avc, (off_t)0, (off_t)avc->m.Length);
174     AFS_GLOCK();
175 }
176
177 /* Try to store pages to cache, in order to store a file back to the server.
178  *
179  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
180  * re-obtained.
181  */
182 void
183 osi_VM_StoreAllSegments(avc)
184     struct vcache *avc;
185 {
186     int error;
187     osi_Assert(valusema(&avc->vc_rwlock) <= 0);
188     osi_Assert(OSI_GET_LOCKID() == avc->vc_rwlockid);
189     osi_Assert(avc->vrefCount > 0);
190     ReleaseWriteLock(&avc->lock);
191     /* We may call back into AFS via:
192      * pflushvp->chunkpush->do_pdflush->mp_afs_bmap
193      */
194     AFS_GUNLOCK();
195
196     /* Write out dirty pages list to avoid B_DELWRI buffers. */
197     while (VN_GET_DPAGES((vnode_t*)avc)) {
198         pdflush(AFSTOV(avc), 0);
199     }
200
201     PFLUSHVP(AFSTOV(avc), (off_t)avc->m.Length, (off_t)0, error);
202     AFS_GLOCK();
203     if (error) {
204         /*
205          * If this fails (due to quota overage, etc.)
206          * what can we do?? we need to sure that
207          * that the VM cache is cleared of dirty pages
208          * We note that pinvalfree ignores write errors & otherwise
209          * does what we want (we don't use this normally since
210          * it also unhashes pages ..)
211          */
212         PINVALFREE((vnode_t *)avc, avc->m.Length);
213     }
214     ObtainWriteLock(&avc->lock,121);
215     if (error && avc->m.LinkCount)
216         cmn_err(CE_WARN, "AFS:Failed to push back pages for vnode 0x%x error %d (from afs_StoreOnLastReference)",
217                 avc, error);
218 }
219
220 /* Purge VM for a file when its callback is revoked.
221  *
222  * Locking:  No lock is held, not even the global lock.
223  */
224 void
225 osi_VM_FlushPages(avc, credp)
226     struct vcache *avc;
227     struct AFS_UCRED *credp;
228 {
229     vnode_t *vp = (vnode_t *)avc;
230
231     remapf(vp, /*avc->m.Length*/ 0, 0);
232
233     /* Used to grab locks and recheck avc->m.DataVersion and
234      * avc->execsOrWriters here, but we have to drop locks before calling
235      * ptossvp() anyway, so why bother.
236      */
237
238     /*
239      * ptossvp tosses all pages associated with this vnode
240      * All in-use pages are marked BAD
241      */
242     PTOSSVP(vp, (off_t)0, (off_t)MAXLONG);
243 }
244
245 /* Purge pages beyond end-of-file, when truncating a file.
246  *
247  * Locking:  no lock is held, not even the global lock.
248  * activeV is raised.  This is supposed to block pageins, but at present
249  * it only works on Solaris.
250  */
251 void
252 osi_VM_Truncate(avc, alen, acred)
253     struct vcache *avc;
254     int alen;
255     struct AFS_UCRED *acred;
256 {
257     PTOSSVP(&avc->v, (off_t)alen, (off_t)MAXLONG);
258 }