2000-08-20 21:34:39 +00:00
|
|
|
/*
|
2004-10-22 09:59:37 +00:00
|
|
|
* Copyright (c) 2000-2004
|
2000-08-20 21:34:39 +00:00
|
|
|
* Poul-Henning Kamp. All rights reserved.
|
2004-10-22 09:59:37 +00:00
|
|
|
* Copyright (c) 1989, 1992-1993, 1995
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2000-08-20 21:34:39 +00:00
|
|
|
*
|
|
|
|
* This code is derived from software donated to Berkeley by
|
|
|
|
* Jan-Simon Pendry.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
|
|
|
|
* From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2001-05-23 17:48:20 +00:00
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* remove empty directories
|
2003-03-02 15:56:49 +00:00
|
|
|
* mknod: hunt down DE_DELETED, compare name, reinstantiate.
|
2001-05-23 17:48:20 +00:00
|
|
|
* mkdir: want it ?
|
|
|
|
*/
|
|
|
|
|
2001-05-13 20:52:40 +00:00
|
|
|
#include <opt_devfs.h>
|
2002-07-31 15:45:16 +00:00
|
|
|
#include <opt_mac.h>
|
2001-05-13 20:52:40 +00:00
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/dirent.h>
|
2004-10-22 09:59:37 +00:00
|
|
|
#include <sys/fcntl.h>
|
2004-11-08 10:46:47 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/filio.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
2002-07-31 15:45:16 +00:00
|
|
|
#include <sys/mac.h>
|
2000-08-20 21:34:39 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/namei.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/proc.h>
|
2004-10-22 09:59:37 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sx.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/time.h>
|
2004-11-08 10:46:47 +00:00
|
|
|
#include <sys/ttycom.h>
|
2001-11-25 21:00:38 +00:00
|
|
|
#include <sys/unistd.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/vnode.h>
|
2000-08-20 21:34:39 +00:00
|
|
|
|
|
|
|
#include <fs/devfs/devfs.h>
|
|
|
|
|
2004-11-08 10:46:47 +00:00
|
|
|
static fo_rdwr_t devfs_read_f;
|
|
|
|
static fo_rdwr_t devfs_write_f;
|
|
|
|
static fo_ioctl_t devfs_ioctl_f;
|
|
|
|
static fo_poll_t devfs_poll_f;
|
|
|
|
static fo_kqfilter_t devfs_kqfilter_f;
|
|
|
|
static fo_stat_t devfs_stat_f;
|
|
|
|
static fo_close_t devfs_close_f;
|
|
|
|
|
|
|
|
struct fileops devfs_ops_f = {
|
|
|
|
.fo_read = devfs_read_f,
|
|
|
|
.fo_write = devfs_write_f,
|
|
|
|
.fo_ioctl = devfs_ioctl_f,
|
|
|
|
.fo_poll = devfs_poll_f,
|
|
|
|
.fo_kqfilter = devfs_kqfilter_f,
|
|
|
|
.fo_stat = devfs_stat_f,
|
|
|
|
.fo_close = devfs_close_f,
|
|
|
|
.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
|
|
|
|
};
|
|
|
|
|
2004-12-01 12:24:41 +00:00
|
|
|
static vop_access_t devfs_access;
|
|
|
|
static vop_advlock_t devfs_advlock;
|
|
|
|
static vop_close_t devfs_close;
|
|
|
|
static vop_fsync_t devfs_fsync;
|
|
|
|
static vop_getattr_t devfs_getattr;
|
|
|
|
static vop_lookup_t devfs_lookupx;
|
|
|
|
static vop_mknod_t devfs_mknod;
|
|
|
|
static vop_open_t devfs_open;
|
|
|
|
static vop_pathconf_t devfs_pathconf;
|
|
|
|
static vop_print_t devfs_print;
|
|
|
|
static vop_readdir_t devfs_readdir;
|
|
|
|
static vop_readlink_t devfs_readlink;
|
|
|
|
static vop_reclaim_t devfs_reclaim;
|
|
|
|
static vop_remove_t devfs_remove;
|
|
|
|
static vop_revoke_t devfs_revoke;
|
|
|
|
static vop_ioctl_t devfs_rioctl;
|
|
|
|
static vop_read_t devfs_rread;
|
|
|
|
static vop_setattr_t devfs_setattr;
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
2004-12-01 12:24:41 +00:00
|
|
|
static vop_setlabel_t devfs_setlabel;
|
2002-07-31 15:45:16 +00:00
|
|
|
#endif
|
2004-12-01 12:24:41 +00:00
|
|
|
static vop_symlink_t devfs_symlink;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2004-12-01 23:16:38 +00:00
|
|
|
extern struct vop_vector devfs_vnodeops;
|
2004-12-20 21:34:29 +00:00
|
|
|
extern struct vop_vector devfs_specops;
|
2002-10-01 10:08:08 +00:00
|
|
|
|
2004-11-13 23:21:54 +00:00
|
|
|
static int
|
|
|
|
devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp)
|
|
|
|
{
|
|
|
|
|
|
|
|
*devp = fp->f_vnode->v_rdev;
|
|
|
|
if (*devp != fp->f_data)
|
|
|
|
return (ENXIO);
|
|
|
|
KASSERT((*devp)->si_refcount > 0,
|
|
|
|
("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
|
|
|
|
*dswp = dev_refthread(*devp);
|
|
|
|
if (*dswp == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-05-23 17:48:20 +00:00
|
|
|
/*
|
|
|
|
* Construct the fully qualified path name relative to the mountpoint
|
2003-03-02 15:56:49 +00:00
|
|
|
*/
|
2001-05-23 17:48:20 +00:00
|
|
|
static char *
|
|
|
|
devfs_fqpn(char *buf, struct vnode *dvp, struct componentname *cnp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct devfs_dirent *de, *dd;
|
|
|
|
struct devfs_mount *dmp;
|
|
|
|
|
|
|
|
dmp = VFSTODEVFS(dvp->v_mount);
|
|
|
|
dd = dvp->v_data;
|
|
|
|
i = SPECNAMELEN;
|
|
|
|
buf[i] = '\0';
|
|
|
|
i -= cnp->cn_namelen;
|
|
|
|
if (i < 0)
|
|
|
|
return (NULL);
|
|
|
|
bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
|
|
|
|
de = dd;
|
|
|
|
while (de != dmp->dm_basedir) {
|
|
|
|
i--;
|
|
|
|
if (i < 0)
|
|
|
|
return (NULL);
|
|
|
|
buf[i] = '/';
|
|
|
|
i -= de->de_dirent->d_namlen;
|
|
|
|
if (i < 0)
|
|
|
|
return (NULL);
|
|
|
|
bcopy(de->de_dirent->d_name, buf + i,
|
|
|
|
de->de_dirent->d_namlen);
|
|
|
|
de = TAILQ_FIRST(&de->de_dlist); /* "." */
|
|
|
|
de = TAILQ_NEXT(de, de_list); /* ".." */
|
|
|
|
de = de->de_dir;
|
|
|
|
}
|
|
|
|
return (buf + i);
|
|
|
|
}
|
|
|
|
|
2000-08-27 14:46:36 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
devfs_allocv(struct devfs_dirent *de, struct mount *mp, struct vnode **vpp, struct thread *td)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct vnode *vp;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2004-07-22 17:03:14 +00:00
|
|
|
KASSERT(td == curthread, ("devfs_allocv: td != curthread"));
|
2000-08-20 21:34:39 +00:00
|
|
|
loop:
|
|
|
|
vp = de->de_vnode;
|
|
|
|
if (vp != NULL) {
|
2004-07-22 17:03:14 +00:00
|
|
|
if (vget(vp, LK_EXCLUSIVE, td))
|
2000-08-20 21:34:39 +00:00
|
|
|
goto loop;
|
|
|
|
*vpp = vp;
|
|
|
|
return (0);
|
|
|
|
}
|
2000-09-06 11:26:43 +00:00
|
|
|
if (de->de_dirent->d_type == DT_CHR) {
|
|
|
|
dev = *devfs_itod(de->de_inode);
|
|
|
|
if (dev == NULL)
|
|
|
|
return (ENOENT);
|
|
|
|
} else {
|
2004-06-17 17:16:53 +00:00
|
|
|
dev = NULL;
|
2000-09-06 11:26:43 +00:00
|
|
|
}
|
2004-12-01 23:16:38 +00:00
|
|
|
error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
|
2000-08-20 21:34:39 +00:00
|
|
|
if (error != 0) {
|
|
|
|
printf("devfs_allocv: failed to allocate new vnode\n");
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (de->de_dirent->d_type == DT_CHR) {
|
|
|
|
vp->v_type = VCHR;
|
2004-12-07 08:15:41 +00:00
|
|
|
VI_LOCK(vp);
|
|
|
|
dev_lock();
|
|
|
|
dev->si_refcount++;
|
|
|
|
vp->v_rdev = dev;
|
|
|
|
SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
|
|
|
|
dev->si_usecount += vp->v_usecount;
|
|
|
|
dev_unlock();
|
|
|
|
VI_UNLOCK(vp);
|
2004-12-01 23:16:38 +00:00
|
|
|
vp->v_op = &devfs_specops;
|
2000-08-20 21:34:39 +00:00
|
|
|
} else if (de->de_dirent->d_type == DT_DIR) {
|
|
|
|
vp->v_type = VDIR;
|
|
|
|
} else if (de->de_dirent->d_type == DT_LNK) {
|
|
|
|
vp->v_type = VLNK;
|
|
|
|
} else {
|
|
|
|
vp->v_type = VBAD;
|
|
|
|
}
|
2000-08-24 15:36:55 +00:00
|
|
|
vp->v_data = de;
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_vnode = vp;
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
Slightly change the semantics of vnode labels for MAC: rather than
"refreshing" the label on the vnode before use, just get the label
right from inception. For single-label file systems, set the label
in the generic VFS getnewvnode() code; for multi-label file systems,
leave the labeling up to the file system. With UFS1/2, this means
reading the extended attribute during vfs_vget() as the inode is
pulled off disk, rather than hitting the extended attributes
frequently during operations later, improving performance. This
also corrects sematics for shared vnode locks, which were not
previously present in the system. This chances the cache
coherrency properties WRT out-of-band access to label data, but in
an acceptable form. With UFS1, there is a small race condition
during automatic extended attribute start -- this is not present
with UFS2, and occurs because EAs aren't available at vnode
inception. We'll introduce a work around for this shortly.
Approved by: re
Obtained from: TrustedBSD Project
Sponsored by: DARPA, Network Associates Laboratories
2002-10-26 14:38:24 +00:00
|
|
|
mac_associate_vnode_devfs(mp, de, vp);
|
2002-07-31 15:45:16 +00:00
|
|
|
#endif
|
2000-08-20 21:34:39 +00:00
|
|
|
*vpp = vp;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devfs_access(ap)
|
|
|
|
struct vop_access_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_mode;
|
|
|
|
struct ucred *a_cred;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *a_td;
|
2000-08-20 21:34:39 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
2000-08-21 14:45:19 +00:00
|
|
|
struct devfs_dirent *de;
|
Even if the permissions deny it, a process should be allowed to
access its controlling terminal.
In essense, history dictates that any process is allowed to open
/dev/tty for RW, irrespective of credential, because by definition
it is it's own controlling terminal.
Before DEVFS we relied on a hacky half-device thing (kern/tty_tty.c)
which did the magic deep down at device level, which at best was
disgusting from an architectural point of view.
My first shot at this was to use the cloning mechanism to simply
give people the right tty when they ask for /dev/tty, that's why
you get this, slightly counter intuitive result:
syv# ls -l /dev/tty `tty`
crw--w---- 1 u1 tty 5, 0 Jan 13 22:14 /dev/tty
crw--w---- 1 u1 tty 5, 0 Jan 13 22:14 /dev/ttyp0
Trouble is, when user u1 su(1)'s to user u2, he cannot open
/dev/ttyp0 anymore because he doesn't have permission to do so.
The above fix allows him to do that.
The interesting side effect is that one was previously only able
to access the controlling tty by indirection:
date > /dev/tty
but not by name:
date > `tty`
This is now possible, and that feels a lot more like DTRT.
PR: 46635
MFC candidate: could be.
2003-01-13 22:20:36 +00:00
|
|
|
int error;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
de = vp->v_data;
|
|
|
|
if (vp->v_type == VDIR)
|
|
|
|
de = de->de_dir;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
Even if the permissions deny it, a process should be allowed to
access its controlling terminal.
In essense, history dictates that any process is allowed to open
/dev/tty for RW, irrespective of credential, because by definition
it is it's own controlling terminal.
Before DEVFS we relied on a hacky half-device thing (kern/tty_tty.c)
which did the magic deep down at device level, which at best was
disgusting from an architectural point of view.
My first shot at this was to use the cloning mechanism to simply
give people the right tty when they ask for /dev/tty, that's why
you get this, slightly counter intuitive result:
syv# ls -l /dev/tty `tty`
crw--w---- 1 u1 tty 5, 0 Jan 13 22:14 /dev/tty
crw--w---- 1 u1 tty 5, 0 Jan 13 22:14 /dev/ttyp0
Trouble is, when user u1 su(1)'s to user u2, he cannot open
/dev/ttyp0 anymore because he doesn't have permission to do so.
The above fix allows him to do that.
The interesting side effect is that one was previously only able
to access the controlling tty by indirection:
date > /dev/tty
but not by name:
date > `tty`
This is now possible, and that feels a lot more like DTRT.
PR: 46635
MFC candidate: could be.
2003-01-13 22:20:36 +00:00
|
|
|
error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
|
|
|
|
ap->a_mode, ap->a_cred, NULL);
|
|
|
|
if (!error)
|
|
|
|
return (error);
|
|
|
|
if (error != EACCES)
|
|
|
|
return (error);
|
|
|
|
/* We do, however, allow access to the controlling terminal */
|
|
|
|
if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
|
|
|
|
return (error);
|
|
|
|
if (ap->a_td->td_proc->p_session->s_ttyvp == de->de_vnode)
|
|
|
|
return (0);
|
|
|
|
return (error);
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Special device advisory byte-level locks.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
devfs_advlock(ap)
|
|
|
|
struct vop_advlock_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
caddr_t a_id;
|
|
|
|
int a_op;
|
|
|
|
struct flock *a_fl;
|
|
|
|
int a_flags;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
|
|
|
|
return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Device close routine
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
devfs_close(ap)
|
|
|
|
struct vop_close_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_fflag;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
struct thread *a_td;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp, *oldvp;
|
|
|
|
struct thread *td = ap->a_td;
|
|
|
|
struct cdev *dev = vp->v_rdev;
|
|
|
|
struct cdevsw *dsw;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hack: a tty device that is a controlling terminal
|
|
|
|
* has a reference from the session structure.
|
|
|
|
* We cannot easily tell that a character device is
|
|
|
|
* a controlling terminal, unless it is the closing
|
|
|
|
* process' controlling terminal. In that case,
|
|
|
|
* if the reference count is 2 (this last descriptor
|
|
|
|
* plus the session), release the reference from the session.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This needs to be rewritten to take the vp interlock into
|
|
|
|
* consideration.
|
|
|
|
*/
|
|
|
|
|
|
|
|
oldvp = NULL;
|
|
|
|
sx_xlock(&proctree_lock);
|
|
|
|
if (td && vp == td->td_proc->p_session->s_ttyvp) {
|
|
|
|
SESS_LOCK(td->td_proc->p_session);
|
|
|
|
VI_LOCK(vp);
|
|
|
|
if (count_dev(dev) == 2 && (vp->v_iflag & VI_XLOCK) == 0) {
|
|
|
|
td->td_proc->p_session->s_ttyvp = NULL;
|
|
|
|
oldvp = vp;
|
|
|
|
}
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
SESS_UNLOCK(td->td_proc->p_session);
|
|
|
|
}
|
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
if (oldvp != NULL)
|
|
|
|
vrele(oldvp);
|
|
|
|
/*
|
|
|
|
* We do not want to really close the device if it
|
|
|
|
* is still in use unless we are trying to close it
|
|
|
|
* forcibly. Since every use (buffer, vnode, swap, cmap)
|
|
|
|
* holds a reference to the vnode, and because we mark
|
|
|
|
* any other vnodes that alias this device, when the
|
|
|
|
* sum of the reference counts on all the aliased
|
|
|
|
* vnodes descends to one, we are on last close.
|
|
|
|
*/
|
|
|
|
dsw = dev_refthread(dev);
|
|
|
|
if (dsw == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
VI_LOCK(vp);
|
|
|
|
if (vp->v_iflag & VI_XLOCK) {
|
|
|
|
/* Forced close. */
|
|
|
|
} else if (dsw->d_flags & D_TRACKCLOSE) {
|
|
|
|
/* Keep device updated on status. */
|
|
|
|
} else if (count_dev(dev) > 1) {
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
dev_relthread(dev);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
KASSERT(dev->si_refcount > 0,
|
|
|
|
("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
|
|
|
|
if (!(dsw->d_flags & D_NEEDGIANT)) {
|
|
|
|
DROP_GIANT();
|
|
|
|
error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
|
|
|
|
PICKUP_GIANT();
|
|
|
|
} else
|
|
|
|
error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
|
|
|
|
dev_relthread(dev);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_close_f(struct file *fp, struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (vnops.fo_close(fp, td));
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Synch buffers associated with a block device
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
devfs_fsync(ap)
|
|
|
|
struct vop_fsync_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
int a_waitfor;
|
|
|
|
struct thread *a_td;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
if (!vn_isdisk(ap->a_vp, NULL))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (vop_stdfsync(ap));
|
|
|
|
}
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
static int
|
|
|
|
devfs_getattr(ap)
|
|
|
|
struct vop_getattr_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct vattr *a_vap;
|
|
|
|
struct ucred *a_cred;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *a_td;
|
2000-08-20 21:34:39 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
struct vattr *vap = ap->a_vap;
|
|
|
|
int error = 0;
|
|
|
|
struct devfs_dirent *de;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
de = vp->v_data;
|
2004-12-20 21:12:11 +00:00
|
|
|
KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
|
|
|
|
if (vp->v_type == VDIR) {
|
2000-08-24 15:36:55 +00:00
|
|
|
de = de->de_dir;
|
2004-12-20 21:12:11 +00:00
|
|
|
KASSERT(de != NULL,
|
|
|
|
("Null dir dirent in devfs_getattr vp=%p", vp));
|
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
bzero((caddr_t) vap, sizeof(*vap));
|
|
|
|
vattr_null(vap);
|
|
|
|
vap->va_uid = de->de_uid;
|
|
|
|
vap->va_gid = de->de_gid;
|
|
|
|
vap->va_mode = de->de_mode;
|
2003-03-02 15:56:49 +00:00
|
|
|
if (vp->v_type == VLNK)
|
2004-02-19 19:09:52 +00:00
|
|
|
vap->va_size = strlen(de->de_symlink);
|
2001-11-25 21:00:38 +00:00
|
|
|
else if (vp->v_type == VDIR)
|
|
|
|
vap->va_size = vap->va_bytes = DEV_BSIZE;
|
2001-01-30 08:39:52 +00:00
|
|
|
else
|
|
|
|
vap->va_size = 0;
|
2001-11-25 21:00:38 +00:00
|
|
|
if (vp->v_type != VDIR)
|
|
|
|
vap->va_bytes = 0;
|
2000-08-20 21:34:39 +00:00
|
|
|
vap->va_blocksize = DEV_BSIZE;
|
2000-09-06 11:26:43 +00:00
|
|
|
vap->va_type = vp->v_type;
|
2001-02-02 22:54:41 +00:00
|
|
|
|
|
|
|
#define fix(aa) \
|
|
|
|
do { \
|
|
|
|
if ((aa).tv_sec == 0) { \
|
|
|
|
(aa).tv_sec = boottime.tv_sec; \
|
|
|
|
(aa).tv_nsec = boottime.tv_usec * 1000; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
if (vp->v_type != VCHR) {
|
2001-02-02 22:54:41 +00:00
|
|
|
fix(de->de_atime);
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_atime = de->de_atime;
|
2001-02-02 22:54:41 +00:00
|
|
|
fix(de->de_mtime);
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_mtime = de->de_mtime;
|
2001-02-02 22:54:41 +00:00
|
|
|
fix(de->de_ctime);
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_ctime = de->de_ctime;
|
|
|
|
} else {
|
|
|
|
dev = vp->v_rdev;
|
2001-02-02 22:54:41 +00:00
|
|
|
fix(dev->si_atime);
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_atime = dev->si_atime;
|
2001-02-02 22:54:41 +00:00
|
|
|
fix(dev->si_mtime);
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_mtime = dev->si_mtime;
|
2001-02-02 22:54:41 +00:00
|
|
|
fix(dev->si_ctime);
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_ctime = dev->si_ctime;
|
2000-09-06 11:26:43 +00:00
|
|
|
vap->va_rdev = dev->si_udev;
|
2000-08-24 15:36:55 +00:00
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
vap->va_gen = 0;
|
|
|
|
vap->va_flags = 0;
|
2000-08-24 15:36:55 +00:00
|
|
|
vap->va_nlink = de->de_links;
|
2000-08-20 21:34:39 +00:00
|
|
|
vap->va_fileid = de->de_inode;
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Device ioctl operation.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
|
|
|
|
{
|
|
|
|
struct cdev *dev;
|
|
|
|
struct cdevsw *dsw;
|
2004-11-13 23:21:54 +00:00
|
|
|
struct vnode *vp;
|
2004-11-08 10:46:47 +00:00
|
|
|
struct vnode *vpold;
|
|
|
|
int error;
|
|
|
|
|
2004-11-13 23:21:54 +00:00
|
|
|
error = devfs_fp_check(fp, &dev, &dsw);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2004-11-08 10:46:47 +00:00
|
|
|
|
|
|
|
if (com == FIODTYPE) {
|
|
|
|
*(int *)data = dsw->d_flags & D_TYPEMASK;
|
|
|
|
dev_relthread(dev);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
dev_relthread(dev);
|
|
|
|
if (error == ENOIOCTL)
|
|
|
|
error = ENOTTY;
|
|
|
|
if (error == 0 && com == TIOCSCTTY) {
|
2004-11-13 23:21:54 +00:00
|
|
|
vp = fp->f_vnode;
|
2004-11-08 10:46:47 +00:00
|
|
|
|
|
|
|
/* Do nothing if reassigning same control tty */
|
|
|
|
sx_slock(&proctree_lock);
|
|
|
|
if (td->td_proc->p_session->s_ttyvp == vp) {
|
|
|
|
sx_sunlock(&proctree_lock);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
|
|
|
|
vpold = td->td_proc->p_session->s_ttyvp;
|
|
|
|
VREF(vp);
|
|
|
|
SESS_LOCK(td->td_proc->p_session);
|
|
|
|
td->td_proc->p_session->s_ttyvp = vp;
|
|
|
|
SESS_UNLOCK(td->td_proc->p_session);
|
|
|
|
|
|
|
|
sx_sunlock(&proctree_lock);
|
|
|
|
|
|
|
|
/* Get rid of reference to old control tty */
|
|
|
|
if (vpold)
|
|
|
|
vrele(vpold);
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/* ARGSUSED */
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_kqfilter_f(struct file *fp, struct knote *kn)
|
|
|
|
{
|
|
|
|
struct cdev *dev;
|
|
|
|
struct cdevsw *dsw;
|
|
|
|
int error;
|
|
|
|
|
2004-11-13 23:21:54 +00:00
|
|
|
error = devfs_fp_check(fp, &dev, &dsw);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2004-11-08 10:46:47 +00:00
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
error = dsw->d_kqfilter(dev, kn);
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
dev_relthread(dev);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
static int
|
2000-09-06 11:26:43 +00:00
|
|
|
devfs_lookupx(ap)
|
2000-08-27 14:46:36 +00:00
|
|
|
struct vop_lookup_args /* {
|
|
|
|
struct vnode * a_dvp;
|
|
|
|
struct vnode ** a_vpp;
|
|
|
|
struct componentname * a_cnp;
|
2000-08-20 21:34:39 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
2000-08-27 14:46:36 +00:00
|
|
|
struct componentname *cnp;
|
|
|
|
struct vnode *dvp, **vpp;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
2000-08-27 14:46:36 +00:00
|
|
|
struct devfs_dirent *de, *dd;
|
2003-10-20 07:04:09 +00:00
|
|
|
struct devfs_dirent **dde;
|
2000-08-27 14:46:36 +00:00
|
|
|
struct devfs_mount *dmp;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *cdev;
|
2003-05-31 19:34:52 +00:00
|
|
|
int error, flags, nameiop;
|
2000-08-27 14:46:36 +00:00
|
|
|
char specname[SPECNAMELEN + 1], *pname;
|
|
|
|
|
|
|
|
cnp = ap->a_cnp;
|
|
|
|
vpp = ap->a_vpp;
|
|
|
|
dvp = ap->a_dvp;
|
|
|
|
pname = cnp->cn_nameptr;
|
2001-09-12 08:38:13 +00:00
|
|
|
td = cnp->cn_thread;
|
2000-08-27 14:46:36 +00:00
|
|
|
flags = cnp->cn_flags;
|
|
|
|
nameiop = cnp->cn_nameiop;
|
|
|
|
dmp = VFSTODEVFS(dvp->v_mount);
|
|
|
|
dd = dvp->v_data;
|
2003-03-02 15:56:49 +00:00
|
|
|
|
2000-08-27 14:46:36 +00:00
|
|
|
*vpp = NULLVP;
|
2002-06-01 09:17:43 +00:00
|
|
|
cnp->cn_flags &= ~PDIRUNLOCK;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2002-05-10 15:41:14 +00:00
|
|
|
if ((flags & ISLASTCN) && nameiop == RENAME)
|
2000-08-27 14:46:36 +00:00
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
if (dvp->v_type != VDIR)
|
|
|
|
return (ENOTDIR);
|
|
|
|
|
2002-08-04 10:29:36 +00:00
|
|
|
if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
|
2000-08-27 14:46:36 +00:00
|
|
|
return (EIO);
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
|
2000-08-27 14:46:36 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (cnp->cn_namelen == 1 && *pname == '.') {
|
2002-05-10 15:41:14 +00:00
|
|
|
if ((flags & ISLASTCN) && nameiop != LOOKUP)
|
2000-08-27 14:46:36 +00:00
|
|
|
return (EINVAL);
|
|
|
|
*vpp = dvp;
|
|
|
|
VREF(dvp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ISDOTDOT) {
|
2002-05-10 15:41:14 +00:00
|
|
|
if ((flags & ISLASTCN) && nameiop != LOOKUP)
|
2000-08-27 14:46:36 +00:00
|
|
|
return (EINVAL);
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(dvp, 0, td);
|
2002-06-01 09:17:43 +00:00
|
|
|
cnp->cn_flags |= PDIRUNLOCK;
|
2000-08-27 14:46:36 +00:00
|
|
|
de = TAILQ_FIRST(&dd->de_dlist); /* "." */
|
|
|
|
de = TAILQ_NEXT(de, de_list); /* ".." */
|
2000-08-24 15:36:55 +00:00
|
|
|
de = de->de_dir;
|
2001-09-12 08:38:13 +00:00
|
|
|
error = devfs_allocv(de, dvp->v_mount, vpp, td);
|
2002-06-01 09:17:43 +00:00
|
|
|
if (error || ((flags & LOCKPARENT) && (flags & ISLASTCN))) {
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
|
2002-06-01 09:17:43 +00:00
|
|
|
cnp->cn_flags &= ~PDIRUNLOCK;
|
2000-08-27 14:46:36 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2000-08-27 14:46:36 +00:00
|
|
|
devfs_populate(dmp);
|
|
|
|
dd = dvp->v_data;
|
|
|
|
TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
|
|
|
|
if (cnp->cn_namelen != de->de_dirent->d_namlen)
|
|
|
|
continue;
|
|
|
|
if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
|
|
|
|
de->de_dirent->d_namlen) != 0)
|
|
|
|
continue;
|
2001-05-23 17:48:20 +00:00
|
|
|
if (de->de_flags & DE_WHITEOUT)
|
|
|
|
goto notfound;
|
2000-08-27 14:46:36 +00:00
|
|
|
goto found;
|
2000-08-24 15:36:55 +00:00
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
|
2001-05-23 17:48:20 +00:00
|
|
|
if (nameiop == DELETE)
|
|
|
|
goto notfound;
|
|
|
|
|
2000-08-27 14:46:36 +00:00
|
|
|
/*
|
|
|
|
* OK, we didn't have an entry for the name we were asked for
|
|
|
|
* so we try to see if anybody can create it on demand.
|
|
|
|
*/
|
2001-05-23 17:48:20 +00:00
|
|
|
pname = devfs_fqpn(specname, dvp, cnp);
|
|
|
|
if (pname == NULL)
|
|
|
|
goto notfound;
|
2000-08-27 14:46:36 +00:00
|
|
|
|
2004-06-17 17:16:53 +00:00
|
|
|
cdev = NULL;
|
2001-05-23 17:48:20 +00:00
|
|
|
EVENTHANDLER_INVOKE(dev_clone, pname, strlen(pname), &cdev);
|
2004-06-17 17:16:53 +00:00
|
|
|
if (cdev == NULL)
|
2000-08-27 14:46:36 +00:00
|
|
|
goto notfound;
|
|
|
|
|
|
|
|
devfs_populate(dmp);
|
2001-05-14 08:20:46 +00:00
|
|
|
|
2003-10-20 07:04:09 +00:00
|
|
|
dde = devfs_itode(dmp, cdev->si_inode);
|
2003-10-20 15:08:10 +00:00
|
|
|
|
|
|
|
if (dde == NULL || *dde == NULL || *dde == DE_DELETED)
|
|
|
|
goto notfound;
|
|
|
|
|
|
|
|
if ((*dde)->de_flags & DE_WHITEOUT)
|
|
|
|
goto notfound;
|
|
|
|
|
|
|
|
de = *dde;
|
|
|
|
goto found;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2000-08-27 14:46:36 +00:00
|
|
|
notfound:
|
|
|
|
|
|
|
|
if ((nameiop == CREATE || nameiop == RENAME) &&
|
|
|
|
(flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
|
|
|
|
cnp->cn_flags |= SAVENAME;
|
2002-06-01 09:17:43 +00:00
|
|
|
if (!(flags & LOCKPARENT)) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(dvp, 0, td);
|
2002-06-01 09:17:43 +00:00
|
|
|
cnp->cn_flags |= PDIRUNLOCK;
|
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
return (EJUSTRETURN);
|
|
|
|
}
|
|
|
|
return (ENOENT);
|
|
|
|
|
|
|
|
|
|
|
|
found:
|
|
|
|
|
|
|
|
if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
|
2001-09-12 08:38:13 +00:00
|
|
|
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
|
2000-08-27 14:46:36 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (*vpp == dvp) {
|
|
|
|
VREF(dvp);
|
|
|
|
*vpp = dvp;
|
|
|
|
return (0);
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
error = devfs_allocv(de, dvp->v_mount, vpp, td);
|
2000-08-27 14:46:36 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2002-06-01 09:17:43 +00:00
|
|
|
if (!(flags & LOCKPARENT)) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(dvp, 0, td);
|
2002-06-01 09:17:43 +00:00
|
|
|
cnp->cn_flags |= PDIRUNLOCK;
|
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
error = devfs_allocv(de, dvp->v_mount, vpp, td);
|
2000-08-27 14:46:36 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2002-06-01 09:17:43 +00:00
|
|
|
if (!(flags & LOCKPARENT) || !(flags & ISLASTCN)) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(dvp, 0, td);
|
2002-06-01 09:17:43 +00:00
|
|
|
cnp->cn_flags |= PDIRUNLOCK;
|
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-09-06 11:26:43 +00:00
|
|
|
static int
|
|
|
|
devfs_lookup(struct vop_lookup_args *ap)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
struct devfs_mount *dmp;
|
|
|
|
|
|
|
|
dmp = VFSTODEVFS(ap->a_dvp->v_mount);
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread);
|
2000-09-06 11:26:43 +00:00
|
|
|
j = devfs_lookupx(ap);
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
|
2000-09-06 11:26:43 +00:00
|
|
|
return (j);
|
|
|
|
}
|
|
|
|
|
2001-05-23 17:48:20 +00:00
|
|
|
static int
|
|
|
|
devfs_mknod(struct vop_mknod_args *ap)
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
struct vop_mknod_args {
|
|
|
|
struct vnodeop_desc *a_desc;
|
|
|
|
struct vnode *a_dvp;
|
|
|
|
struct vnode **a_vpp;
|
|
|
|
struct componentname *a_cnp;
|
|
|
|
struct vattr *a_vap;
|
|
|
|
}; */
|
2001-05-23 17:48:20 +00:00
|
|
|
{
|
|
|
|
struct componentname *cnp;
|
|
|
|
struct vnode *dvp, **vpp;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
2001-05-23 17:48:20 +00:00
|
|
|
struct devfs_dirent *dd, *de;
|
|
|
|
struct devfs_mount *dmp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
dvp = ap->a_dvp;
|
|
|
|
dmp = VFSTODEVFS(dvp->v_mount);
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread);
|
2001-05-23 17:48:20 +00:00
|
|
|
|
|
|
|
cnp = ap->a_cnp;
|
|
|
|
vpp = ap->a_vpp;
|
2001-09-12 08:38:13 +00:00
|
|
|
td = cnp->cn_thread;
|
2001-05-23 17:48:20 +00:00
|
|
|
dd = dvp->v_data;
|
2003-03-02 15:56:49 +00:00
|
|
|
|
2001-05-23 17:48:20 +00:00
|
|
|
error = ENOENT;
|
|
|
|
TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
|
|
|
|
if (cnp->cn_namelen != de->de_dirent->d_namlen)
|
|
|
|
continue;
|
|
|
|
if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
|
|
|
|
de->de_dirent->d_namlen) != 0)
|
|
|
|
continue;
|
|
|
|
if (de->de_flags & DE_WHITEOUT)
|
|
|
|
break;
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
if (de == NULL)
|
|
|
|
goto notfound;
|
|
|
|
de->de_flags &= ~DE_WHITEOUT;
|
2001-09-12 08:38:13 +00:00
|
|
|
error = devfs_allocv(de, dvp->v_mount, vpp, td);
|
2001-05-23 17:48:20 +00:00
|
|
|
notfound:
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
|
2001-05-23 17:48:20 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Open a special file.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
devfs_open(ap)
|
|
|
|
struct vop_open_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_mode;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
struct thread *a_td;
|
2004-11-13 23:21:54 +00:00
|
|
|
int a_fdidx;
|
2004-10-22 09:59:37 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct thread *td = ap->a_td;
|
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
struct cdev *dev = vp->v_rdev;
|
2004-11-08 10:46:47 +00:00
|
|
|
struct file *fp;
|
2004-10-22 09:59:37 +00:00
|
|
|
int error;
|
|
|
|
struct cdevsw *dsw;
|
|
|
|
|
|
|
|
if (vp->v_type == VBLK)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if (dev == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/* Make this field valid before any I/O in d_open. */
|
|
|
|
if (dev->si_iosize_max == 0)
|
|
|
|
dev->si_iosize_max = DFLTPHYS;
|
|
|
|
|
2004-11-04 09:17:55 +00:00
|
|
|
if (vn_isdisk(vp, NULL) &&
|
|
|
|
ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
|
|
|
|
/*
|
|
|
|
* When running in very secure mode, do not allow
|
|
|
|
* opens for writing of any disks.
|
|
|
|
* XXX: should be in geom_dev.c, but we lack the cred there.
|
|
|
|
*/
|
|
|
|
error = securelevel_ge(td->td_ucred, 2);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
dsw = dev_refthread(dev);
|
|
|
|
if (dsw == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/* XXX: Special casing of ttys for deadfs. Probably redundant. */
|
|
|
|
if (dsw->d_flags & D_TTY)
|
|
|
|
vp->v_vflag |= VV_ISTTY;
|
|
|
|
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
|
|
|
|
|
|
|
if(!(dsw->d_flags & D_NEEDGIANT)) {
|
|
|
|
DROP_GIANT();
|
|
|
|
if (dsw->d_fdopen != NULL)
|
|
|
|
error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx);
|
|
|
|
else
|
|
|
|
error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
|
|
|
|
PICKUP_GIANT();
|
|
|
|
} else if (dsw->d_fdopen != NULL)
|
|
|
|
error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx);
|
|
|
|
else
|
|
|
|
error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
|
|
|
|
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
|
|
|
|
|
|
|
dev_relthread(dev);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2004-12-14 09:32:18 +00:00
|
|
|
#if 0 /* /dev/console */
|
|
|
|
KASSERT(ap->a_fdidx >= 0,
|
|
|
|
("Could not vnode bypass device on fd %d", ap->a_fdidx));
|
|
|
|
#else
|
|
|
|
if(ap->a_fdidx < 0)
|
|
|
|
return (error);
|
2004-11-08 10:46:47 +00:00
|
|
|
#endif
|
2004-12-14 09:32:18 +00:00
|
|
|
/*
|
|
|
|
* This is a pretty disgustingly long chain, but I am not
|
|
|
|
* sure there is any better way. Passing the fdidx into
|
|
|
|
* VOP_OPEN() offers us more information than just passing
|
|
|
|
* the file *.
|
|
|
|
*/
|
|
|
|
fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx];
|
|
|
|
KASSERT(fp->f_ops == &badfileops,
|
|
|
|
("Could not vnode bypass device on fdops %p", fp->f_ops));
|
|
|
|
fp->f_ops = &devfs_ops_f;
|
|
|
|
fp->f_data = dev;
|
2004-10-22 09:59:37 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-11-25 21:00:38 +00:00
|
|
|
static int
|
|
|
|
devfs_pathconf(ap)
|
|
|
|
struct vop_pathconf_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_name;
|
|
|
|
int *a_retval;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (ap->a_name) {
|
|
|
|
case _PC_NAME_MAX:
|
|
|
|
*ap->a_retval = NAME_MAX;
|
|
|
|
return (0);
|
|
|
|
case _PC_PATH_MAX:
|
|
|
|
*ap->a_retval = PATH_MAX;
|
|
|
|
return (0);
|
2002-10-20 22:50:43 +00:00
|
|
|
case _PC_MAC_PRESENT:
|
2002-08-02 03:12:40 +00:00
|
|
|
#ifdef MAC
|
|
|
|
/*
|
|
|
|
* If MAC is enabled, devfs automatically supports
|
|
|
|
* trivial non-persistant label storage.
|
|
|
|
*/
|
|
|
|
*ap->a_retval = 1;
|
|
|
|
#else
|
|
|
|
*ap->a_retval = 0;
|
2002-10-20 22:50:43 +00:00
|
|
|
#endif
|
2002-08-02 03:12:40 +00:00
|
|
|
return (0);
|
2001-11-25 21:00:38 +00:00
|
|
|
default:
|
|
|
|
return (vop_stdpathconf(ap));
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/* ARGSUSED */
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
|
|
|
|
{
|
|
|
|
struct cdev *dev;
|
|
|
|
struct cdevsw *dsw;
|
|
|
|
int error;
|
|
|
|
|
2004-11-13 23:21:54 +00:00
|
|
|
error = devfs_fp_check(fp, &dev, &dsw);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2004-11-08 10:46:47 +00:00
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
error = dsw->d_poll(dev, events, td);
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
dev_relthread(dev);
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Print out the contents of a special device vnode.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
devfs_print(ap)
|
|
|
|
struct vop_print_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Vnode op for read
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
|
|
|
|
{
|
|
|
|
struct cdev *dev;
|
|
|
|
int ioflag, error, resid;
|
|
|
|
struct cdevsw *dsw;
|
|
|
|
|
2004-11-13 23:21:54 +00:00
|
|
|
error = devfs_fp_check(fp, &dev, &dsw);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2004-11-08 10:46:47 +00:00
|
|
|
resid = uio->uio_resid;
|
2004-12-22 17:05:44 +00:00
|
|
|
ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
|
|
|
|
if (ioflag & O_DIRECT)
|
2004-11-08 10:46:47 +00:00
|
|
|
ioflag |= IO_DIRECT;
|
|
|
|
|
|
|
|
if ((flags & FOF_OFFSET) == 0)
|
|
|
|
uio->uio_offset = fp->f_offset;
|
|
|
|
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
error = dsw->d_read(dev, uio, ioflag);
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
dev_relthread(dev);
|
|
|
|
if (uio->uio_resid != resid || (error == 0 && resid != 0))
|
|
|
|
vfs_timestamp(&dev->si_atime);
|
|
|
|
|
|
|
|
if ((flags & FOF_OFFSET) == 0)
|
|
|
|
fp->f_offset = uio->uio_offset;
|
|
|
|
fp->f_nextoff = uio->uio_offset;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
static int
|
|
|
|
devfs_readdir(ap)
|
|
|
|
struct vop_readdir_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct uio *a_uio;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
int *a_eofflag;
|
|
|
|
int *a_ncookies;
|
|
|
|
u_long **a_cookies;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
2000-08-26 16:20:57 +00:00
|
|
|
int error;
|
|
|
|
struct uio *uio;
|
2000-08-20 21:34:39 +00:00
|
|
|
struct dirent *dp;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *dd;
|
2000-08-20 21:34:39 +00:00
|
|
|
struct devfs_dirent *de;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_mount *dmp;
|
2001-08-14 06:42:32 +00:00
|
|
|
off_t off, oldoff;
|
|
|
|
int ncookies = 0;
|
|
|
|
u_long *cookiebuf, *cookiep;
|
|
|
|
struct dirent *dps, *dpe;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
|
|
|
if (ap->a_vp->v_type != VDIR)
|
|
|
|
return (ENOTDIR);
|
|
|
|
|
2000-08-26 16:20:57 +00:00
|
|
|
uio = ap->a_uio;
|
|
|
|
if (uio->uio_offset < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
dmp = VFSTODEVFS(ap->a_vp->v_mount);
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread);
|
2000-08-24 15:36:55 +00:00
|
|
|
devfs_populate(dmp);
|
2000-08-20 21:34:39 +00:00
|
|
|
error = 0;
|
2000-08-24 15:36:55 +00:00
|
|
|
de = ap->a_vp->v_data;
|
2000-08-20 21:34:39 +00:00
|
|
|
off = 0;
|
2001-08-14 06:42:32 +00:00
|
|
|
oldoff = uio->uio_offset;
|
2001-05-23 17:48:20 +00:00
|
|
|
TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
|
2003-03-02 15:56:49 +00:00
|
|
|
if (dd->de_flags & DE_WHITEOUT)
|
2001-05-23 17:48:20 +00:00
|
|
|
continue;
|
2000-10-09 14:18:07 +00:00
|
|
|
if (dd->de_dirent->d_type == DT_DIR)
|
2000-08-24 15:36:55 +00:00
|
|
|
de = dd->de_dir;
|
|
|
|
else
|
|
|
|
de = dd;
|
|
|
|
dp = dd->de_dirent;
|
2000-08-26 16:20:57 +00:00
|
|
|
if (dp->d_reclen > uio->uio_resid)
|
|
|
|
break;
|
2000-08-20 21:34:39 +00:00
|
|
|
dp->d_fileno = de->de_inode;
|
2000-08-26 16:20:57 +00:00
|
|
|
if (off >= uio->uio_offset) {
|
2001-08-14 06:42:32 +00:00
|
|
|
ncookies++;
|
2003-03-02 15:50:23 +00:00
|
|
|
error = uiomove(dp, dp->d_reclen, uio);
|
2000-08-26 16:20:57 +00:00
|
|
|
if (error)
|
2000-08-20 21:34:39 +00:00
|
|
|
break;
|
2000-08-26 16:20:57 +00:00
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
off += dp->d_reclen;
|
|
|
|
}
|
2001-08-14 06:42:32 +00:00
|
|
|
if( !error && ap->a_ncookies != NULL && ap->a_cookies != NULL ) {
|
|
|
|
MALLOC(cookiebuf, u_long *, ncookies * sizeof(u_long),
|
2003-03-02 15:56:49 +00:00
|
|
|
M_TEMP, M_WAITOK);
|
2001-08-14 06:42:32 +00:00
|
|
|
cookiep = cookiebuf;
|
2002-10-11 14:58:34 +00:00
|
|
|
dps = (struct dirent *)((char *)uio->uio_iov->iov_base -
|
|
|
|
(uio->uio_offset - oldoff));
|
2001-08-14 06:42:32 +00:00
|
|
|
dpe = (struct dirent *) uio->uio_iov->iov_base;
|
2003-03-02 15:56:49 +00:00
|
|
|
for( dp = dps;
|
|
|
|
dp < dpe;
|
2001-08-14 06:42:32 +00:00
|
|
|
dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
|
|
|
|
oldoff += dp->d_reclen;
|
|
|
|
*cookiep++ = (u_long) oldoff;
|
|
|
|
}
|
|
|
|
*ap->a_ncookies = ncookies;
|
|
|
|
*ap->a_cookies = cookiebuf;
|
2002-09-28 17:37:55 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
|
2000-08-20 21:34:39 +00:00
|
|
|
uio->uio_offset = off;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devfs_readlink(ap)
|
|
|
|
struct vop_readlink_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct uio *a_uio;
|
|
|
|
struct ucred *a_cead;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct devfs_dirent *de;
|
|
|
|
|
|
|
|
de = ap->a_vp->v_data;
|
2001-05-26 20:07:57 +00:00
|
|
|
error = uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio);
|
2000-08-20 21:34:39 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devfs_reclaim(ap)
|
|
|
|
struct vop_reclaim_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *de;
|
2000-10-09 14:18:07 +00:00
|
|
|
int i;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
de = vp->v_data;
|
2000-10-09 14:18:07 +00:00
|
|
|
if (de != NULL)
|
|
|
|
de->de_vnode = NULL;
|
2000-08-20 21:34:39 +00:00
|
|
|
vp->v_data = NULL;
|
2004-11-13 23:21:54 +00:00
|
|
|
if (vp->v_rdev != NULL) {
|
2000-10-09 14:18:07 +00:00
|
|
|
i = vcount(vp);
|
2001-06-01 15:51:10 +00:00
|
|
|
if ((vp->v_rdev->si_flags & SI_CHEAPCLONE) && i == 0 &&
|
|
|
|
(vp->v_rdev->si_flags & SI_NAMED))
|
2000-10-09 14:18:07 +00:00
|
|
|
destroy_dev(vp->v_rdev);
|
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devfs_remove(ap)
|
|
|
|
struct vop_remove_args /* {
|
|
|
|
struct vnode *a_dvp;
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct componentname *a_cnp;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *dd;
|
2001-05-23 17:48:20 +00:00
|
|
|
struct devfs_dirent *de;
|
2000-09-06 11:26:43 +00:00
|
|
|
struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread);
|
2000-08-20 21:34:39 +00:00
|
|
|
dd = ap->a_dvp->v_data;
|
|
|
|
de = vp->v_data;
|
2001-09-30 08:43:33 +00:00
|
|
|
if (de->de_dirent->d_type == DT_LNK) {
|
|
|
|
TAILQ_REMOVE(&dd->de_dlist, de, de_list);
|
|
|
|
if (de->de_vnode)
|
|
|
|
de->de_vnode->v_data = NULL;
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
|
|
|
mac_destroy_devfsdirent(de);
|
|
|
|
#endif
|
2001-09-30 08:43:33 +00:00
|
|
|
FREE(de, M_DEVFS);
|
|
|
|
} else {
|
|
|
|
de->de_flags |= DE_WHITEOUT;
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
|
2000-08-20 21:34:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Revoke is called on a tty when a terminal session ends. The vnode
|
|
|
|
* is orphaned by setting v_op to deadfs so we need to let go of it
|
|
|
|
* as well so that we create a new one next time around.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
devfs_revoke(ap)
|
|
|
|
struct vop_revoke_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_flags;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
2004-11-13 23:37:29 +00:00
|
|
|
struct vnode *vq;
|
2000-08-20 21:34:39 +00:00
|
|
|
struct devfs_dirent *de;
|
2004-11-13 23:37:29 +00:00
|
|
|
struct cdev *dev;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2004-11-13 23:37:29 +00:00
|
|
|
KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
|
2000-08-20 21:34:39 +00:00
|
|
|
de = vp->v_data;
|
|
|
|
de->de_vnode = NULL;
|
2004-11-13 23:37:29 +00:00
|
|
|
|
|
|
|
VI_LOCK(vp);
|
|
|
|
/*
|
|
|
|
* If a vgone (or vclean) is already in progress,
|
|
|
|
* wait until it is done and return.
|
|
|
|
*/
|
|
|
|
if (vp->v_iflag & VI_XLOCK) {
|
|
|
|
vp->v_iflag |= VI_XWANT;
|
|
|
|
msleep(vp, VI_MTX(vp), PINOD | PDROP, "vop_revokeall", 0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
dev = vp->v_rdev;
|
|
|
|
for (;;) {
|
|
|
|
dev_lock();
|
|
|
|
vq = SLIST_FIRST(&dev->si_hlist);
|
|
|
|
dev_unlock();
|
|
|
|
if (vq == NULL)
|
|
|
|
break;
|
|
|
|
vgone(vq);
|
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
static int
|
|
|
|
devfs_rioctl(ap)
|
|
|
|
struct vop_ioctl_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
u_long a_command;
|
|
|
|
caddr_t a_data;
|
|
|
|
int a_fflag;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
struct thread *a_td;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct devfs_mount *dmp;
|
|
|
|
|
|
|
|
dmp = VFSTODEVFS(ap->a_vp->v_mount);
|
|
|
|
lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread);
|
|
|
|
devfs_populate(dmp);
|
|
|
|
lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
|
|
|
|
error = devfs_rules_ioctl(ap->a_vp->v_mount, ap->a_command, ap->a_data,
|
|
|
|
ap->a_td);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devfs_rread(ap)
|
|
|
|
struct vop_read_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct uio *a_uio;
|
|
|
|
int a_ioflag;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ap->a_vp->v_type != VDIR)
|
|
|
|
return (EINVAL);
|
|
|
|
return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
|
|
|
|
}
|
|
|
|
|
2000-08-27 14:46:36 +00:00
|
|
|
static int
|
|
|
|
devfs_setattr(ap)
|
|
|
|
struct vop_setattr_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct vattr *a_vap;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
struct proc *a_p;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct devfs_dirent *de;
|
2000-09-16 12:06:58 +00:00
|
|
|
struct vattr *vap;
|
2001-11-03 17:00:02 +00:00
|
|
|
struct vnode *vp;
|
2000-09-16 12:06:58 +00:00
|
|
|
int c, error;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
|
|
|
|
vap = ap->a_vap;
|
2001-11-03 17:00:02 +00:00
|
|
|
vp = ap->a_vp;
|
2000-09-16 12:06:58 +00:00
|
|
|
if ((vap->va_type != VNON) ||
|
|
|
|
(vap->va_nlink != VNOVAL) ||
|
|
|
|
(vap->va_fsid != VNOVAL) ||
|
|
|
|
(vap->va_fileid != VNOVAL) ||
|
|
|
|
(vap->va_blocksize != VNOVAL) ||
|
2000-09-18 09:40:01 +00:00
|
|
|
(vap->va_flags != VNOVAL && vap->va_flags != 0) ||
|
2000-09-16 12:06:58 +00:00
|
|
|
(vap->va_rdev != VNOVAL) ||
|
|
|
|
((int)vap->va_bytes != VNOVAL) ||
|
|
|
|
(vap->va_gen != VNOVAL)) {
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
|
2001-11-03 17:00:02 +00:00
|
|
|
de = vp->v_data;
|
|
|
|
if (vp->v_type == VDIR)
|
2000-08-27 14:46:36 +00:00
|
|
|
de = de->de_dir;
|
|
|
|
|
2000-09-16 12:06:58 +00:00
|
|
|
error = c = 0;
|
|
|
|
if (vap->va_uid == (uid_t)VNOVAL)
|
|
|
|
uid = de->de_uid;
|
|
|
|
else
|
|
|
|
uid = vap->va_uid;
|
|
|
|
if (vap->va_gid == (gid_t)VNOVAL)
|
|
|
|
gid = de->de_gid;
|
|
|
|
else
|
|
|
|
gid = vap->va_gid;
|
|
|
|
if (uid != de->de_uid || gid != de->de_gid) {
|
|
|
|
if (((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
|
|
|
|
(gid != de->de_gid && !groupmember(gid, ap->a_cred))) &&
|
2004-07-26 07:24:04 +00:00
|
|
|
(error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)) != 0)
|
2000-09-16 12:06:58 +00:00
|
|
|
return (error);
|
|
|
|
de->de_uid = uid;
|
|
|
|
de->de_gid = gid;
|
2000-08-27 14:46:36 +00:00
|
|
|
c = 1;
|
|
|
|
}
|
2001-11-03 17:00:02 +00:00
|
|
|
|
2000-09-16 12:06:58 +00:00
|
|
|
if (vap->va_mode != (mode_t)VNOVAL) {
|
|
|
|
if ((ap->a_cred->cr_uid != de->de_uid) &&
|
2004-07-26 07:24:04 +00:00
|
|
|
(error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)))
|
2000-09-16 12:06:58 +00:00
|
|
|
return (error);
|
|
|
|
de->de_mode = vap->va_mode;
|
2000-08-27 14:46:36 +00:00
|
|
|
c = 1;
|
|
|
|
}
|
2002-04-05 15:16:08 +00:00
|
|
|
|
|
|
|
if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
|
|
|
|
/* See the comment in ufs_vnops::ufs_setattr(). */
|
|
|
|
if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) &&
|
|
|
|
((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
|
|
|
|
(error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td))))
|
2000-09-16 12:06:58 +00:00
|
|
|
return (error);
|
2002-04-05 15:16:08 +00:00
|
|
|
if (vap->va_atime.tv_sec != VNOVAL) {
|
|
|
|
if (vp->v_type == VCHR)
|
|
|
|
vp->v_rdev->si_atime = vap->va_atime;
|
|
|
|
else
|
|
|
|
de->de_atime = vap->va_atime;
|
|
|
|
}
|
|
|
|
if (vap->va_mtime.tv_sec != VNOVAL) {
|
|
|
|
if (vp->v_type == VCHR)
|
|
|
|
vp->v_rdev->si_mtime = vap->va_mtime;
|
|
|
|
else
|
|
|
|
de->de_mtime = vap->va_mtime;
|
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
c = 1;
|
|
|
|
}
|
|
|
|
|
2002-04-05 15:16:08 +00:00
|
|
|
if (c) {
|
|
|
|
if (vp->v_type == VCHR)
|
|
|
|
vfs_timestamp(&vp->v_rdev->si_ctime);
|
|
|
|
else
|
|
|
|
vfs_timestamp(&de->de_mtime);
|
|
|
|
}
|
2000-08-27 14:46:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
|
|
|
static int
|
|
|
|
devfs_setlabel(ap)
|
|
|
|
struct vop_setlabel_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct mac *a_label;
|
|
|
|
struct ucred *a_cred;
|
|
|
|
struct thread *a_td;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp;
|
|
|
|
struct devfs_dirent *de;
|
|
|
|
|
|
|
|
vp = ap->a_vp;
|
|
|
|
de = vp->v_data;
|
|
|
|
|
|
|
|
mac_relabel_vnode(ap->a_cred, vp, ap->a_label);
|
2002-12-09 03:44:28 +00:00
|
|
|
mac_update_devfsdirent(vp->v_mount, de, vp);
|
2002-07-31 15:45:16 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (vnops.fo_stat(fp, sb, cred, td));
|
|
|
|
}
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
static int
|
|
|
|
devfs_symlink(ap)
|
|
|
|
struct vop_symlink_args /* {
|
|
|
|
struct vnode *a_dvp;
|
|
|
|
struct vnode **a_vpp;
|
|
|
|
struct componentname *a_cnp;
|
|
|
|
struct vattr *a_vap;
|
|
|
|
char *a_target;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
2001-02-02 18:35:29 +00:00
|
|
|
int i, error;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *dd;
|
2000-08-20 21:34:39 +00:00
|
|
|
struct devfs_dirent *de;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_mount *dmp;
|
2004-07-22 17:03:14 +00:00
|
|
|
struct thread *td;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2004-07-22 17:03:14 +00:00
|
|
|
td = ap->a_cnp->cn_thread;
|
|
|
|
KASSERT(td == curthread, ("devfs_symlink: td != curthread"));
|
|
|
|
error = suser(td);
|
2001-02-02 18:35:29 +00:00
|
|
|
if (error)
|
|
|
|
return(error);
|
2000-08-27 14:46:36 +00:00
|
|
|
dmp = VFSTODEVFS(ap->a_dvp->v_mount);
|
2000-08-20 21:34:39 +00:00
|
|
|
dd = ap->a_dvp->v_data;
|
|
|
|
de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
|
|
|
|
de->de_uid = 0;
|
|
|
|
de->de_gid = 0;
|
2001-02-02 18:35:29 +00:00
|
|
|
de->de_mode = 0755;
|
2000-08-24 15:36:55 +00:00
|
|
|
de->de_inode = dmp->dm_inode++;
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_dirent->d_type = DT_LNK;
|
|
|
|
i = strlen(ap->a_target) + 1;
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(de->de_symlink, char *, i, M_DEVFS, M_WAITOK);
|
2000-08-20 21:34:39 +00:00
|
|
|
bcopy(ap->a_target, de->de_symlink, i);
|
2004-07-22 17:03:14 +00:00
|
|
|
lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, td);
|
2002-10-05 18:40:10 +00:00
|
|
|
#ifdef MAC
|
2002-12-09 03:44:28 +00:00
|
|
|
mac_create_devfs_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
|
2002-10-05 18:40:10 +00:00
|
|
|
#endif
|
2000-08-24 15:36:55 +00:00
|
|
|
TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
|
2004-07-22 17:03:14 +00:00
|
|
|
devfs_allocv(de, ap->a_dvp->v_mount, ap->a_vpp, td);
|
|
|
|
lockmgr(&dmp->dm_lock, LK_RELEASE, 0, td);
|
2000-08-20 21:34:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-10-22 09:59:37 +00:00
|
|
|
/*
|
|
|
|
* Vnode op for write
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
2004-11-08 10:46:47 +00:00
|
|
|
static int
|
|
|
|
devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
|
|
|
|
{
|
|
|
|
struct cdev *dev;
|
|
|
|
struct vnode *vp;
|
|
|
|
int error, ioflag, resid;
|
|
|
|
struct cdevsw *dsw;
|
|
|
|
|
2004-11-13 23:21:54 +00:00
|
|
|
error = devfs_fp_check(fp, &dev, &dsw);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2004-11-08 10:46:47 +00:00
|
|
|
KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
|
|
|
|
vp = fp->f_vnode;
|
2004-12-22 17:05:44 +00:00
|
|
|
ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
|
|
|
|
if (ioflag & O_DIRECT)
|
2004-11-08 10:46:47 +00:00
|
|
|
ioflag |= IO_DIRECT;
|
|
|
|
if ((flags & FOF_OFFSET) == 0)
|
|
|
|
uio->uio_offset = fp->f_offset;
|
|
|
|
|
|
|
|
resid = uio->uio_resid;
|
|
|
|
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_lock(&Giant);
|
|
|
|
error = dsw->d_write(dev, uio, ioflag);
|
|
|
|
if (dsw->d_flags & D_NEEDGIANT)
|
|
|
|
mtx_unlock(&Giant);
|
|
|
|
dev_relthread(dev);
|
|
|
|
if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
|
|
|
|
vfs_timestamp(&dev->si_ctime);
|
|
|
|
dev->si_mtime = dev->si_ctime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & FOF_OFFSET) == 0)
|
|
|
|
fp->f_offset = uio->uio_offset;
|
|
|
|
fp->f_nextoff = uio->uio_offset;
|
|
|
|
return (error);
|
|
|
|
}
|
2004-10-22 09:59:37 +00:00
|
|
|
|
2004-12-01 23:16:38 +00:00
|
|
|
static struct vop_vector devfs_vnodeops = {
|
|
|
|
.vop_default = &default_vnodeops,
|
|
|
|
.vop_access = devfs_access,
|
|
|
|
.vop_getattr = devfs_getattr,
|
|
|
|
.vop_ioctl = devfs_rioctl,
|
|
|
|
.vop_lookup = devfs_lookup,
|
|
|
|
.vop_mknod = devfs_mknod,
|
|
|
|
.vop_pathconf = devfs_pathconf,
|
|
|
|
.vop_read = devfs_rread,
|
|
|
|
.vop_readdir = devfs_readdir,
|
|
|
|
.vop_readlink = devfs_readlink,
|
|
|
|
.vop_reclaim = devfs_reclaim,
|
|
|
|
.vop_remove = devfs_remove,
|
|
|
|
.vop_revoke = devfs_revoke,
|
|
|
|
.vop_setattr = devfs_setattr,
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_setlabel = devfs_setlabel,
|
2002-07-31 15:45:16 +00:00
|
|
|
#endif
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_symlink = devfs_symlink,
|
2000-08-20 21:34:39 +00:00
|
|
|
};
|
2004-10-22 09:59:37 +00:00
|
|
|
|
2004-12-20 21:34:29 +00:00
|
|
|
static struct vop_vector devfs_specops = {
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_default = &default_vnodeops,
|
|
|
|
.vop_access = devfs_access,
|
|
|
|
.vop_advlock = devfs_advlock,
|
|
|
|
.vop_bmap = VOP_PANIC,
|
|
|
|
.vop_close = devfs_close,
|
|
|
|
.vop_create = VOP_PANIC,
|
|
|
|
.vop_fsync = devfs_fsync,
|
|
|
|
.vop_getattr = devfs_getattr,
|
|
|
|
.vop_lease = VOP_NULL,
|
|
|
|
.vop_link = VOP_PANIC,
|
|
|
|
.vop_mkdir = VOP_PANIC,
|
|
|
|
.vop_mknod = VOP_PANIC,
|
|
|
|
.vop_open = devfs_open,
|
|
|
|
.vop_pathconf = devfs_pathconf,
|
|
|
|
.vop_print = devfs_print,
|
2004-12-13 07:13:21 +00:00
|
|
|
.vop_read = VOP_PANIC,
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_readdir = VOP_PANIC,
|
|
|
|
.vop_readlink = VOP_PANIC,
|
|
|
|
.vop_reallocblks = VOP_PANIC,
|
|
|
|
.vop_reclaim = devfs_reclaim,
|
|
|
|
.vop_remove = devfs_remove,
|
|
|
|
.vop_rename = VOP_PANIC,
|
|
|
|
.vop_revoke = devfs_revoke,
|
|
|
|
.vop_rmdir = VOP_PANIC,
|
|
|
|
.vop_setattr = devfs_setattr,
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_setlabel = devfs_setlabel,
|
2002-07-31 15:45:16 +00:00
|
|
|
#endif
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_strategy = VOP_PANIC,
|
|
|
|
.vop_symlink = VOP_PANIC,
|
2004-12-13 07:13:21 +00:00
|
|
|
.vop_write = VOP_PANIC,
|
2000-08-20 21:34:39 +00:00
|
|
|
};
|
2004-12-22 16:25:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Our calling convention to the device drivers used to be that we passed
|
|
|
|
* vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
|
|
|
|
* flags instead since that's what open(), close() and ioctl() takes and
|
|
|
|
* we don't really want vnode.h in device drivers.
|
|
|
|
* We solved the source compatibility by redefining some vnode flags to
|
|
|
|
* be the same as the fcntl ones and by sending down the bitwise OR of
|
|
|
|
* the respective fcntl/vnode flags. These CTASSERTS make sure nobody
|
|
|
|
* pulls the rug out under this.
|
|
|
|
*/
|
|
|
|
CTASSERT(O_NONBLOCK == IO_NDELAY);
|
|
|
|
CTASSERT(O_FSYNC == IO_SYNC);
|