genfs_vnops.c revision 1.161 1 1.161 ad /* $NetBSD: genfs_vnops.c,v 1.161 2008/01/17 17:28:55 ad 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.81 agc * 3. Neither the name of the University nor the names of its contributors
16 1.6 fvdl * may be used to endorse or promote products derived from this software
17 1.6 fvdl * without specific prior written permission.
18 1.6 fvdl *
19 1.6 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.6 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.6 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.6 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.6 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.6 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.6 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.6 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.6 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.6 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.6 fvdl * SUCH DAMAGE.
30 1.6 fvdl *
31 1.6 fvdl */
32 1.40 lukem
33 1.40 lukem #include <sys/cdefs.h>
34 1.161 ad __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.161 2008/01/17 17:28:55 ad Exp $");
35 1.8 thorpej
36 1.1 mycroft #include <sys/param.h>
37 1.1 mycroft #include <sys/systm.h>
38 1.6 fvdl #include <sys/proc.h>
39 1.1 mycroft #include <sys/kernel.h>
40 1.1 mycroft #include <sys/mount.h>
41 1.1 mycroft #include <sys/namei.h>
42 1.1 mycroft #include <sys/vnode.h>
43 1.13 wrstuden #include <sys/fcntl.h>
44 1.135 yamt #include <sys/kmem.h>
45 1.3 mycroft #include <sys/poll.h>
46 1.37 chs #include <sys/mman.h>
47 1.66 jdolecek #include <sys/file.h>
48 1.125 elad #include <sys/kauth.h>
49 1.143 hannken #include <sys/fstrans.h>
50 1.1 mycroft
51 1.1 mycroft #include <miscfs/genfs/genfs.h>
52 1.37 chs #include <miscfs/genfs/genfs_node.h>
53 1.6 fvdl #include <miscfs/specfs/specdev.h>
54 1.1 mycroft
55 1.21 chs #include <uvm/uvm.h>
56 1.21 chs #include <uvm/uvm_pager.h>
57 1.21 chs
58 1.70 christos static void filt_genfsdetach(struct knote *);
59 1.70 christos static int filt_genfsread(struct knote *, long);
60 1.70 christos static int filt_genfsvnode(struct knote *, long);
61 1.70 christos
62 1.1 mycroft int
63 1.53 enami genfs_poll(void *v)
64 1.1 mycroft {
65 1.3 mycroft struct vop_poll_args /* {
66 1.1 mycroft struct vnode *a_vp;
67 1.3 mycroft int a_events;
68 1.116 christos struct lwp *a_l;
69 1.1 mycroft } */ *ap = v;
70 1.1 mycroft
71 1.3 mycroft return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
72 1.1 mycroft }
73 1.1 mycroft
74 1.1 mycroft int
75 1.53 enami genfs_seek(void *v)
76 1.4 kleink {
77 1.4 kleink struct vop_seek_args /* {
78 1.4 kleink struct vnode *a_vp;
79 1.4 kleink off_t a_oldoff;
80 1.4 kleink off_t a_newoff;
81 1.125 elad kauth_cred_t cred;
82 1.4 kleink } */ *ap = v;
83 1.4 kleink
84 1.4 kleink if (ap->a_newoff < 0)
85 1.4 kleink return (EINVAL);
86 1.4 kleink
87 1.4 kleink return (0);
88 1.4 kleink }
89 1.4 kleink
90 1.4 kleink int
91 1.53 enami genfs_abortop(void *v)
92 1.1 mycroft {
93 1.1 mycroft struct vop_abortop_args /* {
94 1.1 mycroft struct vnode *a_dvp;
95 1.1 mycroft struct componentname *a_cnp;
96 1.1 mycroft } */ *ap = v;
97 1.53 enami
98 1.1 mycroft if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
99 1.19 thorpej PNBUF_PUT(ap->a_cnp->cn_pnbuf);
100 1.1 mycroft return (0);
101 1.13 wrstuden }
102 1.13 wrstuden
103 1.13 wrstuden int
104 1.53 enami genfs_fcntl(void *v)
105 1.13 wrstuden {
106 1.13 wrstuden struct vop_fcntl_args /* {
107 1.13 wrstuden struct vnode *a_vp;
108 1.13 wrstuden u_int a_command;
109 1.150 christos void *a_data;
110 1.13 wrstuden int a_fflag;
111 1.125 elad kauth_cred_t a_cred;
112 1.116 christos struct lwp *a_l;
113 1.13 wrstuden } */ *ap = v;
114 1.13 wrstuden
115 1.13 wrstuden if (ap->a_command == F_SETFL)
116 1.13 wrstuden return (0);
117 1.13 wrstuden else
118 1.13 wrstuden return (EOPNOTSUPP);
119 1.1 mycroft }
120 1.1 mycroft
121 1.1 mycroft /*ARGSUSED*/
122 1.1 mycroft int
123 1.138 christos genfs_badop(void *v)
124 1.1 mycroft {
125 1.1 mycroft
126 1.1 mycroft panic("genfs: bad op");
127 1.1 mycroft }
128 1.1 mycroft
129 1.1 mycroft /*ARGSUSED*/
130 1.1 mycroft int
131 1.138 christos genfs_nullop(void *v)
132 1.1 mycroft {
133 1.1 mycroft
134 1.1 mycroft return (0);
135 1.10 kleink }
136 1.10 kleink
137 1.10 kleink /*ARGSUSED*/
138 1.10 kleink int
139 1.138 christos genfs_einval(void *v)
140 1.10 kleink {
141 1.10 kleink
142 1.10 kleink return (EINVAL);
143 1.1 mycroft }
144 1.1 mycroft
145 1.12 wrstuden /*
146 1.74 jdolecek * Called when an fs doesn't support a particular vop.
147 1.74 jdolecek * This takes care to vrele, vput, or vunlock passed in vnodes.
148 1.12 wrstuden */
149 1.12 wrstuden int
150 1.75 jdolecek genfs_eopnotsupp(void *v)
151 1.12 wrstuden {
152 1.12 wrstuden struct vop_generic_args /*
153 1.12 wrstuden struct vnodeop_desc *a_desc;
154 1.53 enami / * other random data follows, presumably * /
155 1.12 wrstuden } */ *ap = v;
156 1.12 wrstuden struct vnodeop_desc *desc = ap->a_desc;
157 1.74 jdolecek struct vnode *vp, *vp_last = NULL;
158 1.12 wrstuden int flags, i, j, offset;
159 1.12 wrstuden
160 1.12 wrstuden flags = desc->vdesc_flags;
161 1.12 wrstuden for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
162 1.12 wrstuden if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
163 1.12 wrstuden break; /* stop at end of list */
164 1.12 wrstuden if ((j = flags & VDESC_VP0_WILLPUT)) {
165 1.53 enami vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
166 1.74 jdolecek
167 1.74 jdolecek /* Skip if NULL */
168 1.74 jdolecek if (!vp)
169 1.74 jdolecek continue;
170 1.74 jdolecek
171 1.12 wrstuden switch (j) {
172 1.12 wrstuden case VDESC_VP0_WILLPUT:
173 1.74 jdolecek /* Check for dvp == vp cases */
174 1.74 jdolecek if (vp == vp_last)
175 1.74 jdolecek vrele(vp);
176 1.74 jdolecek else {
177 1.74 jdolecek vput(vp);
178 1.74 jdolecek vp_last = vp;
179 1.74 jdolecek }
180 1.12 wrstuden break;
181 1.12 wrstuden case VDESC_VP0_WILLUNLOCK:
182 1.12 wrstuden VOP_UNLOCK(vp, 0);
183 1.12 wrstuden break;
184 1.12 wrstuden case VDESC_VP0_WILLRELE:
185 1.12 wrstuden vrele(vp);
186 1.12 wrstuden break;
187 1.12 wrstuden }
188 1.12 wrstuden }
189 1.12 wrstuden }
190 1.12 wrstuden
191 1.12 wrstuden return (EOPNOTSUPP);
192 1.12 wrstuden }
193 1.12 wrstuden
194 1.1 mycroft /*ARGSUSED*/
195 1.1 mycroft int
196 1.138 christos genfs_ebadf(void *v)
197 1.1 mycroft {
198 1.1 mycroft
199 1.1 mycroft return (EBADF);
200 1.9 matthias }
201 1.9 matthias
202 1.9 matthias /* ARGSUSED */
203 1.9 matthias int
204 1.138 christos genfs_enoioctl(void *v)
205 1.9 matthias {
206 1.9 matthias
207 1.51 atatat return (EPASSTHROUGH);
208 1.6 fvdl }
209 1.6 fvdl
210 1.6 fvdl
211 1.6 fvdl /*
212 1.15 fvdl * Eliminate all activity associated with the requested vnode
213 1.6 fvdl * and with all vnodes aliased to the requested vnode.
214 1.6 fvdl */
215 1.6 fvdl int
216 1.53 enami genfs_revoke(void *v)
217 1.6 fvdl {
218 1.6 fvdl struct vop_revoke_args /* {
219 1.6 fvdl struct vnode *a_vp;
220 1.6 fvdl int a_flags;
221 1.6 fvdl } */ *ap = v;
222 1.6 fvdl
223 1.6 fvdl #ifdef DIAGNOSTIC
224 1.6 fvdl if ((ap->a_flags & REVOKEALL) == 0)
225 1.6 fvdl panic("genfs_revoke: not revokeall");
226 1.6 fvdl #endif
227 1.161 ad vrevoke(ap->a_vp);
228 1.6 fvdl return (0);
229 1.6 fvdl }
230 1.6 fvdl
231 1.6 fvdl /*
232 1.12 wrstuden * Lock the node.
233 1.6 fvdl */
234 1.6 fvdl int
235 1.53 enami genfs_lock(void *v)
236 1.6 fvdl {
237 1.6 fvdl struct vop_lock_args /* {
238 1.6 fvdl struct vnode *a_vp;
239 1.6 fvdl int a_flags;
240 1.6 fvdl } */ *ap = v;
241 1.6 fvdl struct vnode *vp = ap->a_vp;
242 1.6 fvdl
243 1.86 hannken return (lockmgr(vp->v_vnlock, ap->a_flags, &vp->v_interlock));
244 1.6 fvdl }
245 1.6 fvdl
246 1.6 fvdl /*
247 1.12 wrstuden * Unlock the node.
248 1.6 fvdl */
249 1.6 fvdl int
250 1.53 enami genfs_unlock(void *v)
251 1.6 fvdl {
252 1.6 fvdl struct vop_unlock_args /* {
253 1.6 fvdl struct vnode *a_vp;
254 1.6 fvdl int a_flags;
255 1.6 fvdl } */ *ap = v;
256 1.6 fvdl struct vnode *vp = ap->a_vp;
257 1.6 fvdl
258 1.86 hannken return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
259 1.53 enami &vp->v_interlock));
260 1.6 fvdl }
261 1.6 fvdl
262 1.6 fvdl /*
263 1.12 wrstuden * Return whether or not the node is locked.
264 1.6 fvdl */
265 1.6 fvdl int
266 1.53 enami genfs_islocked(void *v)
267 1.6 fvdl {
268 1.6 fvdl struct vop_islocked_args /* {
269 1.6 fvdl struct vnode *a_vp;
270 1.6 fvdl } */ *ap = v;
271 1.6 fvdl struct vnode *vp = ap->a_vp;
272 1.6 fvdl
273 1.86 hannken return (lockstatus(vp->v_vnlock));
274 1.12 wrstuden }
275 1.12 wrstuden
276 1.12 wrstuden /*
277 1.12 wrstuden * Stubs to use when there is no locking to be done on the underlying object.
278 1.12 wrstuden */
279 1.12 wrstuden int
280 1.53 enami genfs_nolock(void *v)
281 1.12 wrstuden {
282 1.12 wrstuden struct vop_lock_args /* {
283 1.12 wrstuden struct vnode *a_vp;
284 1.12 wrstuden int a_flags;
285 1.116 christos struct lwp *a_l;
286 1.12 wrstuden } */ *ap = v;
287 1.12 wrstuden
288 1.12 wrstuden /*
289 1.12 wrstuden * Since we are not using the lock manager, we must clear
290 1.12 wrstuden * the interlock here.
291 1.12 wrstuden */
292 1.12 wrstuden if (ap->a_flags & LK_INTERLOCK)
293 1.160 ad mutex_exit(&ap->a_vp->v_interlock);
294 1.12 wrstuden return (0);
295 1.12 wrstuden }
296 1.12 wrstuden
297 1.12 wrstuden int
298 1.138 christos genfs_nounlock(void *v)
299 1.12 wrstuden {
300 1.53 enami
301 1.12 wrstuden return (0);
302 1.12 wrstuden }
303 1.12 wrstuden
304 1.12 wrstuden int
305 1.138 christos genfs_noislocked(void *v)
306 1.12 wrstuden {
307 1.53 enami
308 1.12 wrstuden return (0);
309 1.8 thorpej }
310 1.8 thorpej
311 1.8 thorpej /*
312 1.142 yamt * Local lease check.
313 1.8 thorpej */
314 1.8 thorpej int
315 1.53 enami genfs_lease_check(void *v)
316 1.8 thorpej {
317 1.8 thorpej
318 1.8 thorpej return (0);
319 1.34 chs }
320 1.34 chs
321 1.34 chs int
322 1.138 christos genfs_mmap(void *v)
323 1.34 chs {
324 1.53 enami
325 1.53 enami return (0);
326 1.21 chs }
327 1.21 chs
328 1.37 chs void
329 1.98 yamt genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
330 1.37 chs {
331 1.37 chs struct genfs_node *gp = VTOG(vp);
332 1.37 chs
333 1.146 ad rw_init(&gp->g_glock);
334 1.37 chs gp->g_op = ops;
335 1.37 chs }
336 1.37 chs
337 1.37 chs void
338 1.147 ad genfs_node_destroy(struct vnode *vp)
339 1.147 ad {
340 1.147 ad struct genfs_node *gp = VTOG(vp);
341 1.147 ad
342 1.147 ad rw_destroy(&gp->g_glock);
343 1.147 ad }
344 1.147 ad
345 1.147 ad void
346 1.138 christos genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
347 1.21 chs {
348 1.21 chs int bsize;
349 1.21 chs
350 1.37 chs bsize = 1 << vp->v_mount->mnt_fs_bshift;
351 1.37 chs *eobp = (size + bsize - 1) & ~(bsize - 1);
352 1.43 chs }
353 1.43 chs
354 1.66 jdolecek static void
355 1.66 jdolecek filt_genfsdetach(struct knote *kn)
356 1.66 jdolecek {
357 1.66 jdolecek struct vnode *vp = (struct vnode *)kn->kn_hook;
358 1.66 jdolecek
359 1.66 jdolecek /* XXXLUKEM lock the struct? */
360 1.66 jdolecek SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
361 1.66 jdolecek }
362 1.66 jdolecek
363 1.66 jdolecek static int
364 1.66 jdolecek filt_genfsread(struct knote *kn, long hint)
365 1.66 jdolecek {
366 1.66 jdolecek struct vnode *vp = (struct vnode *)kn->kn_hook;
367 1.66 jdolecek
368 1.66 jdolecek /*
369 1.66 jdolecek * filesystem is gone, so set the EOF flag and schedule
370 1.66 jdolecek * the knote for deletion.
371 1.66 jdolecek */
372 1.66 jdolecek if (hint == NOTE_REVOKE) {
373 1.66 jdolecek kn->kn_flags |= (EV_EOF | EV_ONESHOT);
374 1.66 jdolecek return (1);
375 1.66 jdolecek }
376 1.66 jdolecek
377 1.66 jdolecek /* XXXLUKEM lock the struct? */
378 1.66 jdolecek kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
379 1.66 jdolecek return (kn->kn_data != 0);
380 1.66 jdolecek }
381 1.66 jdolecek
382 1.66 jdolecek static int
383 1.66 jdolecek filt_genfsvnode(struct knote *kn, long hint)
384 1.66 jdolecek {
385 1.66 jdolecek
386 1.66 jdolecek if (kn->kn_sfflags & hint)
387 1.66 jdolecek kn->kn_fflags |= hint;
388 1.66 jdolecek if (hint == NOTE_REVOKE) {
389 1.66 jdolecek kn->kn_flags |= EV_EOF;
390 1.66 jdolecek return (1);
391 1.66 jdolecek }
392 1.66 jdolecek return (kn->kn_fflags != 0);
393 1.66 jdolecek }
394 1.66 jdolecek
395 1.96 perry static const struct filterops genfsread_filtops =
396 1.66 jdolecek { 1, NULL, filt_genfsdetach, filt_genfsread };
397 1.96 perry static const struct filterops genfsvnode_filtops =
398 1.66 jdolecek { 1, NULL, filt_genfsdetach, filt_genfsvnode };
399 1.66 jdolecek
400 1.66 jdolecek int
401 1.66 jdolecek genfs_kqfilter(void *v)
402 1.66 jdolecek {
403 1.66 jdolecek struct vop_kqfilter_args /* {
404 1.66 jdolecek struct vnode *a_vp;
405 1.66 jdolecek struct knote *a_kn;
406 1.66 jdolecek } */ *ap = v;
407 1.66 jdolecek struct vnode *vp;
408 1.66 jdolecek struct knote *kn;
409 1.66 jdolecek
410 1.66 jdolecek vp = ap->a_vp;
411 1.66 jdolecek kn = ap->a_kn;
412 1.66 jdolecek switch (kn->kn_filter) {
413 1.66 jdolecek case EVFILT_READ:
414 1.66 jdolecek kn->kn_fop = &genfsread_filtops;
415 1.66 jdolecek break;
416 1.66 jdolecek case EVFILT_VNODE:
417 1.66 jdolecek kn->kn_fop = &genfsvnode_filtops;
418 1.66 jdolecek break;
419 1.66 jdolecek default:
420 1.159 pooka return (EINVAL);
421 1.66 jdolecek }
422 1.66 jdolecek
423 1.66 jdolecek kn->kn_hook = vp;
424 1.66 jdolecek
425 1.66 jdolecek /* XXXLUKEM lock the struct? */
426 1.66 jdolecek SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
427 1.66 jdolecek
428 1.66 jdolecek return (0);
429 1.1 mycroft }
430 1.136 yamt
431 1.136 yamt void
432 1.136 yamt genfs_node_wrlock(struct vnode *vp)
433 1.136 yamt {
434 1.136 yamt struct genfs_node *gp = VTOG(vp);
435 1.136 yamt
436 1.146 ad rw_enter(&gp->g_glock, RW_WRITER);
437 1.136 yamt }
438 1.136 yamt
439 1.136 yamt void
440 1.136 yamt genfs_node_rdlock(struct vnode *vp)
441 1.136 yamt {
442 1.136 yamt struct genfs_node *gp = VTOG(vp);
443 1.136 yamt
444 1.146 ad rw_enter(&gp->g_glock, RW_READER);
445 1.136 yamt }
446 1.136 yamt
447 1.136 yamt void
448 1.136 yamt genfs_node_unlock(struct vnode *vp)
449 1.136 yamt {
450 1.136 yamt struct genfs_node *gp = VTOG(vp);
451 1.136 yamt
452 1.146 ad rw_exit(&gp->g_glock);
453 1.136 yamt }
454