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