genfs_vnops.c revision 1.10 1 1.10 kleink /* $NetBSD: genfs_vnops.c,v 1.10 1998/08/13 09:59:53 kleink 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.10 kleink }
144 1.10 kleink
145 1.10 kleink /*ARGSUSED*/
146 1.10 kleink int
147 1.10 kleink genfs_einval(v)
148 1.10 kleink void *v;
149 1.10 kleink {
150 1.10 kleink
151 1.10 kleink return (EINVAL);
152 1.1 mycroft }
153 1.1 mycroft
154 1.1 mycroft /*ARGSUSED*/
155 1.1 mycroft int
156 1.1 mycroft genfs_eopnotsupp(v)
157 1.1 mycroft void *v;
158 1.1 mycroft {
159 1.1 mycroft
160 1.1 mycroft return (EOPNOTSUPP);
161 1.1 mycroft }
162 1.1 mycroft
163 1.1 mycroft /*ARGSUSED*/
164 1.1 mycroft int
165 1.1 mycroft genfs_ebadf(v)
166 1.1 mycroft void *v;
167 1.1 mycroft {
168 1.1 mycroft
169 1.1 mycroft return (EBADF);
170 1.9 matthias }
171 1.9 matthias
172 1.9 matthias /* ARGSUSED */
173 1.9 matthias int
174 1.9 matthias genfs_enoioctl(v)
175 1.9 matthias void *v;
176 1.9 matthias {
177 1.9 matthias
178 1.9 matthias return (ENOTTY);
179 1.6 fvdl }
180 1.6 fvdl
181 1.6 fvdl
182 1.6 fvdl /*
183 1.6 fvdl * Eliminate all activity associated with the requested vnode
184 1.6 fvdl * and with all vnodes aliased to the requested vnode.
185 1.6 fvdl */
186 1.6 fvdl int
187 1.6 fvdl genfs_revoke(v)
188 1.6 fvdl void *v;
189 1.6 fvdl {
190 1.6 fvdl struct vop_revoke_args /* {
191 1.6 fvdl struct vnode *a_vp;
192 1.6 fvdl int a_flags;
193 1.6 fvdl } */ *ap = v;
194 1.6 fvdl struct vnode *vp, *vq;
195 1.6 fvdl struct proc *p = curproc; /* XXX */
196 1.6 fvdl
197 1.6 fvdl #ifdef DIAGNOSTIC
198 1.6 fvdl if ((ap->a_flags & REVOKEALL) == 0)
199 1.6 fvdl panic("genfs_revoke: not revokeall");
200 1.6 fvdl #endif
201 1.6 fvdl
202 1.6 fvdl vp = ap->a_vp;
203 1.6 fvdl simple_lock(&vp->v_interlock);
204 1.6 fvdl
205 1.6 fvdl if (vp->v_flag & VALIASED) {
206 1.6 fvdl /*
207 1.6 fvdl * If a vgone (or vclean) is already in progress,
208 1.6 fvdl * wait until it is done and return.
209 1.6 fvdl */
210 1.6 fvdl if (vp->v_flag & VXLOCK) {
211 1.6 fvdl vp->v_flag |= VXWANT;
212 1.6 fvdl simple_unlock(&vp->v_interlock);
213 1.6 fvdl tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
214 1.6 fvdl return (0);
215 1.6 fvdl }
216 1.6 fvdl /*
217 1.6 fvdl * Ensure that vp will not be vgone'd while we
218 1.6 fvdl * are eliminating its aliases.
219 1.6 fvdl */
220 1.6 fvdl vp->v_flag |= VXLOCK;
221 1.6 fvdl simple_unlock(&vp->v_interlock);
222 1.6 fvdl while (vp->v_flag & VALIASED) {
223 1.6 fvdl simple_lock(&spechash_slock);
224 1.6 fvdl for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
225 1.6 fvdl if (vq->v_rdev != vp->v_rdev ||
226 1.6 fvdl vq->v_type != vp->v_type || vp == vq)
227 1.6 fvdl continue;
228 1.6 fvdl simple_unlock(&spechash_slock);
229 1.6 fvdl vgone(vq);
230 1.6 fvdl break;
231 1.6 fvdl }
232 1.6 fvdl if (vq == NULLVP)
233 1.6 fvdl simple_unlock(&spechash_slock);
234 1.6 fvdl }
235 1.6 fvdl /*
236 1.6 fvdl * Remove the lock so that vgone below will
237 1.6 fvdl * really eliminate the vnode after which time
238 1.6 fvdl * vgone will awaken any sleepers.
239 1.6 fvdl */
240 1.6 fvdl simple_lock(&vp->v_interlock);
241 1.6 fvdl vp->v_flag &= ~VXLOCK;
242 1.6 fvdl }
243 1.6 fvdl vgonel(vp, p);
244 1.6 fvdl return (0);
245 1.6 fvdl }
246 1.6 fvdl
247 1.6 fvdl
248 1.6 fvdl /*
249 1.6 fvdl * Stubs to use when there is no locking to be done on the underlying object.
250 1.6 fvdl * A minimal shared lock is necessary to ensure that the underlying object
251 1.6 fvdl * is not revoked while an operation is in progress. So, an active shared
252 1.6 fvdl * count is maintained in an auxillary vnode lock structure.
253 1.6 fvdl */
254 1.6 fvdl int
255 1.6 fvdl genfs_nolock(v)
256 1.6 fvdl void *v;
257 1.6 fvdl {
258 1.6 fvdl struct vop_lock_args /* {
259 1.6 fvdl struct vnode *a_vp;
260 1.6 fvdl int a_flags;
261 1.6 fvdl struct proc *a_p;
262 1.6 fvdl } */ *ap = v;
263 1.6 fvdl
264 1.6 fvdl #ifdef notyet
265 1.6 fvdl /*
266 1.6 fvdl * This code cannot be used until all the non-locking filesystems
267 1.6 fvdl * (notably NFS) are converted to properly lock and release nodes.
268 1.6 fvdl * Also, certain vnode operations change the locking state within
269 1.6 fvdl * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
270 1.6 fvdl * and symlink). Ideally these operations should not change the
271 1.6 fvdl * lock state, but should be changed to let the caller of the
272 1.6 fvdl * function unlock them. Otherwise all intermediate vnode layers
273 1.6 fvdl * (such as union, umapfs, etc) must catch these functions to do
274 1.6 fvdl * the necessary locking at their layer. Note that the inactive
275 1.6 fvdl * and lookup operations also change their lock state, but this
276 1.6 fvdl * cannot be avoided, so these two operations will always need
277 1.6 fvdl * to be handled in intermediate layers.
278 1.6 fvdl */
279 1.6 fvdl struct vnode *vp = ap->a_vp;
280 1.6 fvdl int vnflags, flags = ap->a_flags;
281 1.6 fvdl
282 1.6 fvdl if (vp->v_vnlock == NULL) {
283 1.6 fvdl if ((flags & LK_TYPE_MASK) == LK_DRAIN)
284 1.6 fvdl return (0);
285 1.6 fvdl MALLOC(vp->v_vnlock, struct lock *, sizeof(struct lock),
286 1.6 fvdl M_VNODE, M_WAITOK);
287 1.6 fvdl lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0);
288 1.6 fvdl }
289 1.6 fvdl switch (flags & LK_TYPE_MASK) {
290 1.6 fvdl case LK_DRAIN:
291 1.6 fvdl vnflags = LK_DRAIN;
292 1.6 fvdl break;
293 1.6 fvdl case LK_EXCLUSIVE:
294 1.6 fvdl case LK_SHARED:
295 1.6 fvdl vnflags = LK_SHARED;
296 1.6 fvdl break;
297 1.6 fvdl case LK_UPGRADE:
298 1.6 fvdl case LK_EXCLUPGRADE:
299 1.6 fvdl case LK_DOWNGRADE:
300 1.6 fvdl return (0);
301 1.6 fvdl case LK_RELEASE:
302 1.6 fvdl default:
303 1.6 fvdl panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK);
304 1.6 fvdl }
305 1.6 fvdl if (flags & LK_INTERLOCK)
306 1.6 fvdl vnflags |= LK_INTERLOCK;
307 1.6 fvdl return(lockmgr(vp->v_vnlock, vnflags, &vp->v_interlock));
308 1.6 fvdl #else /* for now */
309 1.6 fvdl /*
310 1.6 fvdl * Since we are not using the lock manager, we must clear
311 1.6 fvdl * the interlock here.
312 1.6 fvdl */
313 1.6 fvdl if (ap->a_flags & LK_INTERLOCK)
314 1.6 fvdl simple_unlock(&ap->a_vp->v_interlock);
315 1.6 fvdl return (0);
316 1.6 fvdl #endif
317 1.6 fvdl }
318 1.6 fvdl
319 1.6 fvdl /*
320 1.6 fvdl * Decrement the active use count.
321 1.6 fvdl */
322 1.6 fvdl int
323 1.6 fvdl genfs_nounlock(v)
324 1.6 fvdl void *v;
325 1.6 fvdl {
326 1.6 fvdl struct vop_unlock_args /* {
327 1.6 fvdl struct vnode *a_vp;
328 1.6 fvdl int a_flags;
329 1.6 fvdl struct proc *a_p;
330 1.6 fvdl } */ *ap = v;
331 1.6 fvdl struct vnode *vp = ap->a_vp;
332 1.6 fvdl
333 1.6 fvdl if (vp->v_vnlock == NULL)
334 1.6 fvdl return (0);
335 1.6 fvdl return (lockmgr(vp->v_vnlock, LK_RELEASE, NULL));
336 1.6 fvdl }
337 1.6 fvdl
338 1.6 fvdl /*
339 1.6 fvdl * Return whether or not the node is in use.
340 1.6 fvdl */
341 1.6 fvdl int
342 1.6 fvdl genfs_noislocked(v)
343 1.6 fvdl void *v;
344 1.6 fvdl {
345 1.6 fvdl struct vop_islocked_args /* {
346 1.6 fvdl struct vnode *a_vp;
347 1.6 fvdl } */ *ap = v;
348 1.6 fvdl struct vnode *vp = ap->a_vp;
349 1.6 fvdl
350 1.6 fvdl if (vp->v_vnlock == NULL)
351 1.6 fvdl return (0);
352 1.6 fvdl return (lockstatus(vp->v_vnlock));
353 1.8 thorpej }
354 1.8 thorpej
355 1.8 thorpej /*
356 1.8 thorpej * Local lease check for NFS servers. Just set up args and let
357 1.8 thorpej * nqsrv_getlease() do the rest. If NFSSERVER is not in the kernel,
358 1.8 thorpej * this is a null operation.
359 1.8 thorpej */
360 1.8 thorpej int
361 1.8 thorpej genfs_lease_check(v)
362 1.8 thorpej void *v;
363 1.8 thorpej {
364 1.8 thorpej #ifdef NFSSERVER
365 1.8 thorpej struct vop_lease_args /* {
366 1.8 thorpej struct vnode *a_vp;
367 1.8 thorpej struct proc *a_p;
368 1.8 thorpej struct ucred *a_cred;
369 1.8 thorpej int a_flag;
370 1.8 thorpej } */ *ap = v;
371 1.8 thorpej u_int32_t duration = 0;
372 1.8 thorpej int cache;
373 1.8 thorpej u_quad_t frev;
374 1.8 thorpej
375 1.8 thorpej (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
376 1.8 thorpej NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
377 1.8 thorpej return (0);
378 1.8 thorpej #else
379 1.8 thorpej return (0);
380 1.8 thorpej #endif /* NFSSERVER */
381 1.1 mycroft }
382