genfs_vnops.c revision 1.8 1 1.8 thorpej /* $NetBSD: genfs_vnops.c,v 1.8 1998/06/25 22:15:30 thorpej Exp $ */
2 1.6 fvdl
3 1.6 fvdl /*
4 1.6 fvdl * Copyright (c) 1982, 1986, 1989, 1993
5 1.6 fvdl * The Regents of the University of California. All rights reserved.
6 1.6 fvdl *
7 1.6 fvdl * Redistribution and use in source and binary forms, with or without
8 1.6 fvdl * modification, are permitted provided that the following conditions
9 1.6 fvdl * are met:
10 1.6 fvdl * 1. Redistributions of source code must retain the above copyright
11 1.6 fvdl * notice, this list of conditions and the following disclaimer.
12 1.6 fvdl * 2. Redistributions in binary form must reproduce the above copyright
13 1.6 fvdl * notice, this list of conditions and the following disclaimer in the
14 1.6 fvdl * documentation and/or other materials provided with the distribution.
15 1.6 fvdl * 3. All advertising materials mentioning features or use of this software
16 1.6 fvdl * must display the following acknowledgement:
17 1.6 fvdl * This product includes software developed by the University of
18 1.6 fvdl * California, Berkeley and its contributors.
19 1.6 fvdl * 4. Neither the name of the University nor the names of its contributors
20 1.6 fvdl * may be used to endorse or promote products derived from this software
21 1.6 fvdl * without specific prior written permission.
22 1.6 fvdl *
23 1.6 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.6 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.6 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.6 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.6 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.6 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.6 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.6 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.6 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.6 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.6 fvdl * SUCH DAMAGE.
34 1.6 fvdl *
35 1.6 fvdl */
36 1.5 perry
37 1.8 thorpej #include "opt_nfsserver.h"
38 1.8 thorpej
39 1.1 mycroft #include <sys/param.h>
40 1.1 mycroft #include <sys/systm.h>
41 1.6 fvdl #include <sys/proc.h>
42 1.1 mycroft #include <sys/kernel.h>
43 1.1 mycroft #include <sys/mount.h>
44 1.1 mycroft #include <sys/namei.h>
45 1.1 mycroft #include <sys/vnode.h>
46 1.1 mycroft #include <sys/malloc.h>
47 1.3 mycroft #include <sys/poll.h>
48 1.1 mycroft
49 1.1 mycroft #include <miscfs/genfs/genfs.h>
50 1.6 fvdl #include <miscfs/specfs/specdev.h>
51 1.1 mycroft
52 1.8 thorpej #ifdef NFSSERVER
53 1.8 thorpej #include <nfs/rpcv2.h>
54 1.8 thorpej #include <nfs/nfsproto.h>
55 1.8 thorpej #include <nfs/nfs.h>
56 1.8 thorpej #include <nfs/nqnfs.h>
57 1.8 thorpej #include <nfs/nfs_var.h>
58 1.8 thorpej #endif
59 1.8 thorpej
60 1.1 mycroft int
61 1.3 mycroft genfs_poll(v)
62 1.1 mycroft void *v;
63 1.1 mycroft {
64 1.3 mycroft struct vop_poll_args /* {
65 1.1 mycroft struct vnode *a_vp;
66 1.3 mycroft int a_events;
67 1.1 mycroft struct proc *a_p;
68 1.1 mycroft } */ *ap = v;
69 1.1 mycroft
70 1.3 mycroft return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
71 1.1 mycroft }
72 1.1 mycroft
73 1.1 mycroft int
74 1.1 mycroft genfs_fsync(v)
75 1.1 mycroft void *v;
76 1.1 mycroft {
77 1.1 mycroft struct vop_fsync_args /* {
78 1.1 mycroft struct vnode *a_vp;
79 1.1 mycroft struct ucred *a_cred;
80 1.7 kleink int a_flags;
81 1.1 mycroft struct proc *a_p;
82 1.1 mycroft } */ *ap = v;
83 1.1 mycroft register struct vnode *vp = ap->a_vp;
84 1.1 mycroft struct timespec ts;
85 1.1 mycroft
86 1.7 kleink vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
87 1.7 kleink if ((ap->a_flags & FSYNC_DATAONLY) != 0) {
88 1.7 kleink return (0);
89 1.7 kleink } else {
90 1.7 kleink TIMEVAL_TO_TIMESPEC(&time, &ts);
91 1.7 kleink return (VOP_UPDATE(ap->a_vp, &ts, &ts,
92 1.7 kleink (ap->a_flags & FSYNC_WAIT) != 0));
93 1.7 kleink }
94 1.1 mycroft }
95 1.1 mycroft
96 1.1 mycroft int
97 1.4 kleink genfs_seek(v)
98 1.4 kleink void *v;
99 1.4 kleink {
100 1.4 kleink struct vop_seek_args /* {
101 1.4 kleink struct vnode *a_vp;
102 1.4 kleink off_t a_oldoff;
103 1.4 kleink off_t a_newoff;
104 1.4 kleink struct ucred *a_ucred;
105 1.4 kleink } */ *ap = v;
106 1.4 kleink
107 1.4 kleink if (ap->a_newoff < 0)
108 1.4 kleink return (EINVAL);
109 1.4 kleink
110 1.4 kleink return (0);
111 1.4 kleink }
112 1.4 kleink
113 1.4 kleink int
114 1.1 mycroft genfs_abortop(v)
115 1.1 mycroft void *v;
116 1.1 mycroft {
117 1.1 mycroft struct vop_abortop_args /* {
118 1.1 mycroft struct vnode *a_dvp;
119 1.1 mycroft struct componentname *a_cnp;
120 1.1 mycroft } */ *ap = v;
121 1.1 mycroft
122 1.1 mycroft if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
123 1.1 mycroft FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
124 1.1 mycroft return (0);
125 1.1 mycroft }
126 1.1 mycroft
127 1.1 mycroft /*ARGSUSED*/
128 1.1 mycroft int
129 1.1 mycroft genfs_badop(v)
130 1.1 mycroft void *v;
131 1.1 mycroft {
132 1.1 mycroft
133 1.1 mycroft panic("genfs: bad op");
134 1.1 mycroft }
135 1.1 mycroft
136 1.1 mycroft /*ARGSUSED*/
137 1.1 mycroft int
138 1.1 mycroft genfs_nullop(v)
139 1.1 mycroft void *v;
140 1.1 mycroft {
141 1.1 mycroft
142 1.1 mycroft return (0);
143 1.1 mycroft }
144 1.1 mycroft
145 1.1 mycroft /*ARGSUSED*/
146 1.1 mycroft int
147 1.1 mycroft genfs_eopnotsupp(v)
148 1.1 mycroft void *v;
149 1.1 mycroft {
150 1.1 mycroft
151 1.1 mycroft return (EOPNOTSUPP);
152 1.1 mycroft }
153 1.1 mycroft
154 1.1 mycroft /*ARGSUSED*/
155 1.1 mycroft int
156 1.1 mycroft genfs_ebadf(v)
157 1.1 mycroft void *v;
158 1.1 mycroft {
159 1.1 mycroft
160 1.1 mycroft return (EBADF);
161 1.6 fvdl }
162 1.6 fvdl
163 1.6 fvdl
164 1.6 fvdl /*
165 1.6 fvdl * Eliminate all activity associated with the requested vnode
166 1.6 fvdl * and with all vnodes aliased to the requested vnode.
167 1.6 fvdl */
168 1.6 fvdl int
169 1.6 fvdl genfs_revoke(v)
170 1.6 fvdl void *v;
171 1.6 fvdl {
172 1.6 fvdl struct vop_revoke_args /* {
173 1.6 fvdl struct vnode *a_vp;
174 1.6 fvdl int a_flags;
175 1.6 fvdl } */ *ap = v;
176 1.6 fvdl struct vnode *vp, *vq;
177 1.6 fvdl struct proc *p = curproc; /* XXX */
178 1.6 fvdl
179 1.6 fvdl #ifdef DIAGNOSTIC
180 1.6 fvdl if ((ap->a_flags & REVOKEALL) == 0)
181 1.6 fvdl panic("genfs_revoke: not revokeall");
182 1.6 fvdl #endif
183 1.6 fvdl
184 1.6 fvdl vp = ap->a_vp;
185 1.6 fvdl simple_lock(&vp->v_interlock);
186 1.6 fvdl
187 1.6 fvdl if (vp->v_flag & VALIASED) {
188 1.6 fvdl /*
189 1.6 fvdl * If a vgone (or vclean) is already in progress,
190 1.6 fvdl * wait until it is done and return.
191 1.6 fvdl */
192 1.6 fvdl if (vp->v_flag & VXLOCK) {
193 1.6 fvdl vp->v_flag |= VXWANT;
194 1.6 fvdl simple_unlock(&vp->v_interlock);
195 1.6 fvdl tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
196 1.6 fvdl return (0);
197 1.6 fvdl }
198 1.6 fvdl /*
199 1.6 fvdl * Ensure that vp will not be vgone'd while we
200 1.6 fvdl * are eliminating its aliases.
201 1.6 fvdl */
202 1.6 fvdl vp->v_flag |= VXLOCK;
203 1.6 fvdl simple_unlock(&vp->v_interlock);
204 1.6 fvdl while (vp->v_flag & VALIASED) {
205 1.6 fvdl simple_lock(&spechash_slock);
206 1.6 fvdl for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
207 1.6 fvdl if (vq->v_rdev != vp->v_rdev ||
208 1.6 fvdl vq->v_type != vp->v_type || vp == vq)
209 1.6 fvdl continue;
210 1.6 fvdl simple_unlock(&spechash_slock);
211 1.6 fvdl vgone(vq);
212 1.6 fvdl break;
213 1.6 fvdl }
214 1.6 fvdl if (vq == NULLVP)
215 1.6 fvdl simple_unlock(&spechash_slock);
216 1.6 fvdl }
217 1.6 fvdl /*
218 1.6 fvdl * Remove the lock so that vgone below will
219 1.6 fvdl * really eliminate the vnode after which time
220 1.6 fvdl * vgone will awaken any sleepers.
221 1.6 fvdl */
222 1.6 fvdl simple_lock(&vp->v_interlock);
223 1.6 fvdl vp->v_flag &= ~VXLOCK;
224 1.6 fvdl }
225 1.6 fvdl vgonel(vp, p);
226 1.6 fvdl return (0);
227 1.6 fvdl }
228 1.6 fvdl
229 1.6 fvdl
230 1.6 fvdl /*
231 1.6 fvdl * Stubs to use when there is no locking to be done on the underlying object.
232 1.6 fvdl * A minimal shared lock is necessary to ensure that the underlying object
233 1.6 fvdl * is not revoked while an operation is in progress. So, an active shared
234 1.6 fvdl * count is maintained in an auxillary vnode lock structure.
235 1.6 fvdl */
236 1.6 fvdl int
237 1.6 fvdl genfs_nolock(v)
238 1.6 fvdl void *v;
239 1.6 fvdl {
240 1.6 fvdl struct vop_lock_args /* {
241 1.6 fvdl struct vnode *a_vp;
242 1.6 fvdl int a_flags;
243 1.6 fvdl struct proc *a_p;
244 1.6 fvdl } */ *ap = v;
245 1.6 fvdl
246 1.6 fvdl #ifdef notyet
247 1.6 fvdl /*
248 1.6 fvdl * This code cannot be used until all the non-locking filesystems
249 1.6 fvdl * (notably NFS) are converted to properly lock and release nodes.
250 1.6 fvdl * Also, certain vnode operations change the locking state within
251 1.6 fvdl * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
252 1.6 fvdl * and symlink). Ideally these operations should not change the
253 1.6 fvdl * lock state, but should be changed to let the caller of the
254 1.6 fvdl * function unlock them. Otherwise all intermediate vnode layers
255 1.6 fvdl * (such as union, umapfs, etc) must catch these functions to do
256 1.6 fvdl * the necessary locking at their layer. Note that the inactive
257 1.6 fvdl * and lookup operations also change their lock state, but this
258 1.6 fvdl * cannot be avoided, so these two operations will always need
259 1.6 fvdl * to be handled in intermediate layers.
260 1.6 fvdl */
261 1.6 fvdl struct vnode *vp = ap->a_vp;
262 1.6 fvdl int vnflags, flags = ap->a_flags;
263 1.6 fvdl
264 1.6 fvdl if (vp->v_vnlock == NULL) {
265 1.6 fvdl if ((flags & LK_TYPE_MASK) == LK_DRAIN)
266 1.6 fvdl return (0);
267 1.6 fvdl MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
268 1.6 fvdl M_VNODE, M_WAITOK);
269 1.6 fvdl lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
270 1.6 fvdl }
271 1.6 fvdl switch (flags & LK_TYPE_MASK) {
272 1.6 fvdl case LK_DRAIN:
273 1.6 fvdl vnflags = LK_DRAIN;
274 1.6 fvdl break;
275 1.6 fvdl case LK_EXCLUSIVE:
276 1.6 fvdl case LK_SHARED:
277 1.6 fvdl vnflags = LK_SHARED;
278 1.6 fvdl break;
279 1.6 fvdl case LK_UPGRADE:
280 1.6 fvdl case LK_EXCLUPGRADE:
281 1.6 fvdl case LK_DOWNGRADE:
282 1.6 fvdl return (0);
283 1.6 fvdl case LK_RELEASE:
284 1.6 fvdl default:
285 1.6 fvdl panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
286 1.6 fvdl }
287 1.6 fvdl if (flags & LK_INTERLOCK)
288 1.6 fvdl vnflags |= LK_INTERLOCK;
289 1.6 fvdl return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock));
290 1.6 fvdl #else /* for now */
291 1.6 fvdl /*
292 1.6 fvdl * Since we are not using the lock manager, we must clear
293 1.6 fvdl * the interlock here.
294 1.6 fvdl */
295 1.6 fvdl if (ap->a_flags & LK_INTERLOCK)
296 1.6 fvdl simple_unlock(&ap->a_vp->v_interlock);
297 1.6 fvdl return (0);
298 1.6 fvdl #endif
299 1.6 fvdl }
300 1.6 fvdl
301 1.6 fvdl /*
302 1.6 fvdl * Decrement the active use count.
303 1.6 fvdl */
304 1.6 fvdl int
305 1.6 fvdl genfs_nounlock(v)
306 1.6 fvdl void *v;
307 1.6 fvdl {
308 1.6 fvdl struct vop_unlock_args /* {
309 1.6 fvdl struct vnode *a_vp;
310 1.6 fvdl int a_flags;
311 1.6 fvdl struct proc *a_p;
312 1.6 fvdl } */ *ap = v;
313 1.6 fvdl struct vnode *vp = ap->a_vp;
314 1.6 fvdl
315 1.6 fvdl if (vp->v_vnlock == NULL)
316 1.6 fvdl return (0);
317 1.6 fvdl return (lockmgr(vp->v_vnlock, LK_RELEASE, NULL));
318 1.6 fvdl }
319 1.6 fvdl
320 1.6 fvdl /*
321 1.6 fvdl * Return whether or not the node is in use.
322 1.6 fvdl */
323 1.6 fvdl int
324 1.6 fvdl genfs_noislocked(v)
325 1.6 fvdl void *v;
326 1.6 fvdl {
327 1.6 fvdl struct vop_islocked_args /* {
328 1.6 fvdl struct vnode *a_vp;
329 1.6 fvdl } */ *ap = v;
330 1.6 fvdl struct vnode *vp = ap->a_vp;
331 1.6 fvdl
332 1.6 fvdl if (vp->v_vnlock == NULL)
333 1.6 fvdl return (0);
334 1.6 fvdl return (lockstatus(vp->v_vnlock));
335 1.8 thorpej }
336 1.8 thorpej
337 1.8 thorpej /*
338 1.8 thorpej * Local lease check for NFS servers. Just set up args and let
339 1.8 thorpej * nqsrv_getlease() do the rest. If NFSSERVER is not in the kernel,
340 1.8 thorpej * this is a null operation.
341 1.8 thorpej */
342 1.8 thorpej int
343 1.8 thorpej genfs_lease_check(v)
344 1.8 thorpej void *v;
345 1.8 thorpej {
346 1.8 thorpej #ifdef NFSSERVER
347 1.8 thorpej struct vop_lease_args /* {
348 1.8 thorpej struct vnode *a_vp;
349 1.8 thorpej struct proc *a_p;
350 1.8 thorpej struct ucred *a_cred;
351 1.8 thorpej int a_flag;
352 1.8 thorpej } */ *ap = v;
353 1.8 thorpej u_int32_t duration = 0;
354 1.8 thorpej int cache;
355 1.8 thorpej u_quad_t frev;
356 1.8 thorpej
357 1.8 thorpej (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
358 1.8 thorpej NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
359 1.8 thorpej return (0);
360 1.8 thorpej #else
361 1.8 thorpej return (0);
362 1.8 thorpej #endif /* NFSSERVER */
363 1.1 mycroft }
364