1 /* $NetBSD: genfs_vnops.c,v 1.173 2009/11/20 13:19:46 pooka Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.173 2009/11/20 13:19:46 pooka Exp $");
62 #include <sys/param.h>
63 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/vnode.h>
69 #include <sys/fcntl.h>
74 #include <sys/kauth.h>
77 #include <miscfs/genfs/genfs.h>
78 #include <miscfs/genfs/genfs_node.h>
79 #include <miscfs/specfs/specdev.h>
82 #include <uvm/uvm_pager.h>
84 static void filt_genfsdetach(struct knote
*);
85 static int filt_genfsread(struct knote
*, long);
86 static int filt_genfsvnode(struct knote
*, long);
91 struct vop_poll_args
/* {
97 return (ap
->a_events
& (POLLIN
| POLLOUT
| POLLRDNORM
| POLLWRNORM
));
103 struct vop_seek_args
/* {
110 if (ap
->a_newoff
< 0)
117 genfs_abortop(void *v
)
119 struct vop_abortop_args
/* {
121 struct componentname *a_cnp;
124 if ((ap
->a_cnp
->cn_flags
& (HASBUF
| SAVESTART
)) == HASBUF
)
125 PNBUF_PUT(ap
->a_cnp
->cn_pnbuf
);
132 struct vop_fcntl_args
/* {
141 if (ap
->a_command
== F_SETFL
)
152 panic("genfs: bad op");
157 genfs_nullop(void *v
)
165 genfs_einval(void *v
)
172 * Called when an fs doesn't support a particular vop.
173 * This takes care to vrele, vput, or vunlock passed in vnodes.
176 genfs_eopnotsupp(void *v
)
178 struct vop_generic_args
/*
179 struct vnodeop_desc *a_desc;
180 / * other random data follows, presumably * /
182 struct vnodeop_desc
*desc
= ap
->a_desc
;
183 struct vnode
*vp
, *vp_last
= NULL
;
184 int flags
, i
, j
, offset
;
186 flags
= desc
->vdesc_flags
;
187 for (i
= 0; i
< VDESC_MAX_VPS
; flags
>>=1, i
++) {
188 if ((offset
= desc
->vdesc_vp_offsets
[i
]) == VDESC_NO_OFFSET
)
189 break; /* stop at end of list */
190 if ((j
= flags
& VDESC_VP0_WILLPUT
)) {
191 vp
= *VOPARG_OFFSETTO(struct vnode
**, offset
, ap
);
198 case VDESC_VP0_WILLPUT
:
199 /* Check for dvp == vp cases */
207 case VDESC_VP0_WILLUNLOCK
:
210 case VDESC_VP0_WILLRELE
:
230 genfs_enoioctl(void *v
)
233 return (EPASSTHROUGH
);
238 * Eliminate all activity associated with the requested vnode
239 * and with all vnodes aliased to the requested vnode.
242 genfs_revoke(void *v
)
244 struct vop_revoke_args
/* {
250 if ((ap
->a_flags
& REVOKEALL
) == 0)
251 panic("genfs_revoke: not revokeall");
263 struct vop_lock_args
/* {
267 struct vnode
*vp
= ap
->a_vp
;
268 int flags
= ap
->a_flags
;
270 if ((flags
& LK_INTERLOCK
) != 0) {
271 flags
&= ~LK_INTERLOCK
;
272 mutex_exit(&vp
->v_interlock
);
275 return (vlockmgr(vp
->v_vnlock
, flags
));
282 genfs_unlock(void *v
)
284 struct vop_unlock_args
/* {
288 struct vnode
*vp
= ap
->a_vp
;
290 KASSERT(ap
->a_flags
== 0);
292 return (vlockmgr(vp
->v_vnlock
, LK_RELEASE
));
296 * Return whether or not the node is locked.
299 genfs_islocked(void *v
)
301 struct vop_islocked_args
/* {
304 struct vnode
*vp
= ap
->a_vp
;
306 return (vlockstatus(vp
->v_vnlock
));
310 * Stubs to use when there is no locking to be done on the underlying object.
313 genfs_nolock(void *v
)
315 struct vop_lock_args
/* {
322 * Since we are not using the lock manager, we must clear
323 * the interlock here.
325 if (ap
->a_flags
& LK_INTERLOCK
)
326 mutex_exit(&ap
->a_vp
->v_interlock
);
331 genfs_nounlock(void *v
)
338 genfs_noislocked(void *v
)
352 * VOP_PUTPAGES() for vnodes which never have pages.
356 genfs_null_putpages(void *v
)
358 struct vop_putpages_args
/* {
364 struct vnode
*vp
= ap
->a_vp
;
366 KASSERT(vp
->v_uobj
.uo_npages
== 0);
367 mutex_exit(&vp
->v_interlock
);
372 genfs_node_init(struct vnode
*vp
, const struct genfs_ops
*ops
)
374 struct genfs_node
*gp
= VTOG(vp
);
376 rw_init(&gp
->g_glock
);
381 genfs_node_destroy(struct vnode
*vp
)
383 struct genfs_node
*gp
= VTOG(vp
);
385 rw_destroy(&gp
->g_glock
);
389 genfs_size(struct vnode
*vp
, off_t size
, off_t
*eobp
, int flags
)
393 bsize
= 1 << vp
->v_mount
->mnt_fs_bshift
;
394 *eobp
= (size
+ bsize
- 1) & ~(bsize
- 1);
398 filt_genfsdetach(struct knote
*kn
)
400 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
402 mutex_enter(&vp
->v_interlock
);
403 SLIST_REMOVE(&vp
->v_klist
, kn
, knote
, kn_selnext
);
404 mutex_exit(&vp
->v_interlock
);
408 filt_genfsread(struct knote
*kn
, long hint
)
410 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
414 * filesystem is gone, so set the EOF flag and schedule
415 * the knote for deletion.
419 KASSERT(mutex_owned(&vp
->v_interlock
));
420 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
423 mutex_enter(&vp
->v_interlock
);
424 kn
->kn_data
= vp
->v_size
- ((file_t
*)kn
->kn_obj
)->f_offset
;
425 rv
= (kn
->kn_data
!= 0);
426 mutex_exit(&vp
->v_interlock
);
429 KASSERT(mutex_owned(&vp
->v_interlock
));
430 kn
->kn_data
= vp
->v_size
- ((file_t
*)kn
->kn_obj
)->f_offset
;
431 return (kn
->kn_data
!= 0);
436 filt_genfsvnode(struct knote
*kn
, long hint
)
438 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
443 KASSERT(mutex_owned(&vp
->v_interlock
));
444 kn
->kn_flags
|= EV_EOF
;
445 if ((kn
->kn_sfflags
& hint
) != 0)
446 kn
->kn_fflags
|= hint
;
449 mutex_enter(&vp
->v_interlock
);
450 fflags
= kn
->kn_fflags
;
451 mutex_exit(&vp
->v_interlock
);
454 KASSERT(mutex_owned(&vp
->v_interlock
));
455 if ((kn
->kn_sfflags
& hint
) != 0)
456 kn
->kn_fflags
|= hint
;
457 fflags
= kn
->kn_fflags
;
461 return (fflags
!= 0);
464 static const struct filterops genfsread_filtops
=
465 { 1, NULL
, filt_genfsdetach
, filt_genfsread
};
466 static const struct filterops genfsvnode_filtops
=
467 { 1, NULL
, filt_genfsdetach
, filt_genfsvnode
};
470 genfs_kqfilter(void *v
)
472 struct vop_kqfilter_args
/* {
481 switch (kn
->kn_filter
) {
483 kn
->kn_fop
= &genfsread_filtops
;
486 kn
->kn_fop
= &genfsvnode_filtops
;
494 mutex_enter(&vp
->v_interlock
);
495 SLIST_INSERT_HEAD(&vp
->v_klist
, kn
, kn_selnext
);
496 mutex_exit(&vp
->v_interlock
);
502 genfs_node_wrlock(struct vnode
*vp
)
504 struct genfs_node
*gp
= VTOG(vp
);
506 rw_enter(&gp
->g_glock
, RW_WRITER
);
510 genfs_node_rdlock(struct vnode
*vp
)
512 struct genfs_node
*gp
= VTOG(vp
);
514 rw_enter(&gp
->g_glock
, RW_READER
);
518 genfs_node_unlock(struct vnode
*vp
)
520 struct genfs_node
*gp
= VTOG(vp
);
522 rw_exit(&gp
->g_glock
);
526 * Do the usual access checking.
527 * file_mode, uid and gid are from the vnode in question,
528 * while acc_mode and cred are from the VOP_ACCESS parameter list
531 genfs_can_access(enum vtype type
, mode_t file_mode
, uid_t uid
, gid_t gid
,
532 mode_t acc_mode
, kauth_cred_t cred
)
538 * Super-user always gets read/write access, but execute access depends
539 * on at least one execute bit being set.
541 if (kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
, NULL
) == 0) {
542 if ((acc_mode
& VEXEC
) && type
!= VDIR
&&
543 (file_mode
& (S_IXUSR
|S_IXGRP
|S_IXOTH
)) == 0)
550 /* Otherwise, check the owner. */
551 if (kauth_cred_geteuid(cred
) == uid
) {
552 if (acc_mode
& VEXEC
)
554 if (acc_mode
& VREAD
)
556 if (acc_mode
& VWRITE
)
558 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
561 /* Otherwise, check the groups. */
562 error
= kauth_cred_ismember_gid(cred
, gid
, &ismember
);
565 if (kauth_cred_getegid(cred
) == gid
|| ismember
) {
566 if (acc_mode
& VEXEC
)
568 if (acc_mode
& VREAD
)
570 if (acc_mode
& VWRITE
)
572 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
575 /* Otherwise, check everyone else. */
576 if (acc_mode
& VEXEC
)
578 if (acc_mode
& VREAD
)
580 if (acc_mode
& VWRITE
)
582 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
586 * Common routine to check if chmod() is allowed.
589 * - You must be root, or
590 * - You must own the file, and
591 * - You must not set the "sticky" bit (meaningless, see chmod(2))
592 * - You must be a member of the group if you're trying to set the
595 * cred - credentials of the invoker
596 * vp - vnode of the file-system object
597 * cur_uid, cur_gid - current uid/gid of the file-system object
598 * new_mode - new mode for the file-system object
600 * Returns 0 if the change is allowed, or an error value otherwise.
603 genfs_can_chmod(vnode_t
*vp
, kauth_cred_t cred
, uid_t cur_uid
,
604 gid_t cur_gid
, mode_t new_mode
)
608 /* Superuser can always change mode. */
609 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
,
614 /* Otherwise, user must own the file. */
615 if (kauth_cred_geteuid(cred
) != cur_uid
)
619 * Non-root users can't set the sticky bit on files.
621 if ((vp
->v_type
!= VDIR
) && (new_mode
& S_ISTXT
))
625 * If the invoker is trying to set the SGID bit on the file,
626 * check group membership.
628 if (new_mode
& S_ISGID
) {
631 error
= kauth_cred_ismember_gid(cred
, cur_gid
,
633 if (error
|| !ismember
)
641 * Common routine to check if chown() is allowed.
644 * - You must be root, or
645 * - You must own the file, and
646 * - You must not try to change ownership, and
647 * - You must be member of the new group
649 * cred - credentials of the invoker
650 * cur_uid, cur_gid - current uid/gid of the file-system object
651 * new_uid, new_gid - target uid/gid of the file-system object
653 * Returns 0 if the change is allowed, or an error value otherwise.
656 genfs_can_chown(vnode_t
*vp
, kauth_cred_t cred
, uid_t cur_uid
,
657 gid_t cur_gid
, uid_t new_uid
, gid_t new_gid
)
662 * You can only change ownership of a file if:
663 * You are the superuser, or...
665 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
,
671 * You own the file and...
673 if (kauth_cred_geteuid(cred
) == cur_uid
) {
675 * You don't try to change ownership, and...
677 if (new_uid
!= cur_uid
)
681 * You don't try to change group (no-op), or...
683 if (new_gid
== cur_gid
)
687 * Your effective gid is the new gid, or...
689 if (kauth_cred_getegid(cred
) == new_gid
)
693 * The new gid is one you're a member of.
696 error
= kauth_cred_ismember_gid(cred
, new_gid
,
698 if (!error
&& ismember
)
706 * Common routine to check if the device can be mounted.
708 * devvp - the locked vnode of the device
709 * cred - credentials of the invoker
710 * accessmode - the accessmode (VREAD, VWRITE)
712 * Returns 0 if the mount is allowed, or an error value otherwise.
715 genfs_can_mount(vnode_t
*devvp
, mode_t accessmode
, kauth_cred_t cred
)
719 /* Always allow for root. */
720 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
, NULL
);
724 error
= VOP_ACCESS(devvp
, accessmode
, cred
);
730 genfs_can_chtimes(vnode_t
*vp
, u_int vaflags
, uid_t owner_uid
,
735 /* Must be root, or... */
736 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
, NULL
);
740 /* must be owner, or... */
741 if (kauth_cred_geteuid(cred
) == owner_uid
)
744 /* set the times to the current time, and... */
745 if ((vaflags
& VA_UTIMES_NULL
) == 0)
748 /* have write access. */
749 error
= VOP_ACCESS(vp
, VWRITE
, cred
);