afs: afs_osi_Alloc_NoSleep() cleanup
[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
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/flock.h"          /* for IGN_PID */
18
19 extern struct vnodeops Afs_vnodeops;
20
21 /* Try to discard pages, in order to recycle a vcache entry.
22  *
23  * We also make some sanity checks:  ref count, open count, held locks.
24  *
25  * We also do some non-VM-related chores, such as releasing the cred pointer
26  * (for AIX and Solaris) and releasing the gnode (for AIX).
27  *
28  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
29  *   *slept should be set to warn the caller.
30  *
31  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
32  * is not dropped and re-acquired for any platform.  It may be that *slept is
33  * therefore obsolescent.
34  */
35 int
36 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)
103         osi_Panic("flushVcache: vm race");
104 #ifdef AFS_SGI64_ENV
105     AFS_GUNLOCK();
106     vnode_pcache_reclaim(vp);   /* this can sleep */
107     vnode_pcache_free(vp);
108     if (vp->v_op != &Afs_vnodeops) {
109         VOP_RECLAIM(vp, FSYNC_WAIT, code);
110     }
111     AFS_GLOCK();
112 #ifdef AFS_SGI65_ENV
113 #ifdef VNODE_TRACING
114     ktrace_free(vp->v_trace);
115 #endif /* VNODE_TRACING */
116     vn_bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
117     vn_bhv_head_destroy(&(vp->v_bh));
118     destroy_bitlock(&vp->v_pcacheflag);
119     mutex_destroy(&vp->v_buf_lock);
120 #else
121     bhv_remove(VN_BHV_HEAD(vp), &(avc->vc_bhv_desc));
122     bhv_head_destroy(&(vp->v_bh));
123 #endif
124     vp->v_flag = 0;             /* debug */
125 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
126     destroy_bitlock(&vp->v_flag);
127 #endif
128 #ifdef INTR_KTHREADS
129     AFS_VN_DESTROY_BUF_LOCK(vp);
130 #endif
131 #endif /* AFS_SGI64_ENV */
132
133     return 0;
134 }
135
136 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
137  * try to free pages, when deleting a file.
138  *
139  * Locking:  the vcache entry's lock is held.  It may be dropped and 
140  * re-obtained.
141  *
142  * Since we drop and re-obtain the lock, we can't guarantee that there won't
143  * be some pages around when we return, newly created by concurrent activity.
144  */
145 void
146 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
147 {
148     ReleaseWriteLock(&avc->lock);
149     AFS_GUNLOCK();
150     /* current remapf restriction - cannot have VOP_RWLOCK */
151     osi_Assert(OSI_GET_LOCKID() != avc->vc_rwlockid);
152     if (((vnode_t *) avc)->v_type == VREG && AFS_VN_MAPPED(((vnode_t *) avc)))
153         remapf(((vnode_t *) avc), 0, 0);
154     PTOSSVP(AFSTOV(avc), (off_t) 0, (off_t) MAXLONG);
155     AFS_GLOCK();
156     ObtainWriteLock(&avc->lock, 62);
157 }
158
159 /* Flush and invalidate pages, for fsync() with INVAL flag
160  *
161  * Locking:  only the global lock is held.
162  */
163 void
164 osi_VM_FSyncInval(struct vcache *avc)
165 {
166     AFS_GUNLOCK();
167     PFLUSHINVALVP((vnode_t *) avc, (off_t) 0, (off_t) avc->f.m.Length);
168     AFS_GLOCK();
169 }
170
171 /* Try to store pages to cache, in order to store a file back to the server.
172  *
173  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
174  * re-obtained.
175  */
176 void
177 osi_VM_StoreAllSegments(struct vcache *avc)
178 {
179     int error;
180     osi_Assert(valusema(&avc->vc_rwlock) <= 0);
181     osi_Assert(OSI_GET_LOCKID() == avc->vc_rwlockid);
182     osi_Assert(avc->vrefCount > 0);
183     ReleaseWriteLock(&avc->lock);
184     /* We may call back into AFS via:
185      * pflushvp->chunkpush->do_pdflush->mp_afs_bmap
186      */
187     AFS_GUNLOCK();
188
189     /* Write out dirty pages list to avoid B_DELWRI buffers. */
190     while (VN_GET_DPAGES((vnode_t *) avc)) {
191         pdflush(AFSTOV(avc), 0);
192     }
193
194     PFLUSHVP(AFSTOV(avc), (off_t) avc->f.m.Length, (off_t) 0, error);
195     AFS_GLOCK();
196     if (error) {
197         /*
198          * If this fails (due to quota overage, etc.)
199          * what can we do?? we need to sure that
200          * that the VM cache is cleared of dirty pages
201          * We note that pinvalfree ignores write errors & otherwise
202          * does what we want (we don't use this normally since
203          * it also unhashes pages ..)
204          */
205         PINVALFREE((vnode_t *) avc, avc->f.m.Length);
206     }
207     ObtainWriteLock(&avc->lock, 121);
208     if (error && avc->f.m.LinkCount)
209         cmn_err(CE_WARN,
210                 "AFS:Failed to push back pages for vnode 0x%x error %d (from afs_StoreOnLastReference)",
211                 avc, error);
212 }
213
214 /* Purge VM for a file when its callback is revoked.
215  *
216  * Locking:  No lock is held, not even the global lock.
217  */
218 void
219 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
220 {
221     vnode_t *vp = (vnode_t *) avc;
222
223     remapf(vp, /*avc->f.m.Length */ 0, 0);
224
225     /* Used to grab locks and recheck avc->f.m.DataVersion and
226      * avc->execsOrWriters here, but we have to drop locks before calling
227      * ptossvp() anyway, so why bother.
228      */
229
230     /*
231      * ptossvp tosses all pages associated with this vnode
232      * All in-use pages are marked BAD
233      */
234     PTOSSVP(vp, (off_t) 0, (off_t) MAXLONG);
235 }
236
237 /* Purge pages beyond end-of-file, when truncating a file.
238  *
239  * Locking:  no lock is held, not even the global lock.
240  * activeV is raised.  This is supposed to block pageins, but at present
241  * it only works on Solaris.
242  */
243 void
244 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
245 {
246     PTOSSVP(&avc->v, (off_t) alen, (off_t) MAXLONG);
247 }