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