diff options
author | phk <phk@FreeBSD.org> | 2000-08-20 21:34:39 +0000 |
---|---|---|
committer | phk <phk@FreeBSD.org> | 2000-08-20 21:34:39 +0000 |
commit | b648921accec69a7e5c83e915ded3037cbca7f3d (patch) | |
tree | fa2e43c05e3c1d31732408f806d72db091c03d14 /sys/fs | |
parent | 1c624ac57c791b6df4b51eb86e04dc404052c700 (diff) | |
download | FreeBSD-src-b648921accec69a7e5c83e915ded3037cbca7f3d.zip FreeBSD-src-b648921accec69a7e5c83e915ded3037cbca7f3d.tar.gz |
Remove all traces of Julians DEVFS (incl from kern/subr_diskslice.c)
Remove old DEVFS support fields from dev_t.
Make uid, gid & mode members of dev_t and set them in make_dev().
Use correct uid, gid & mode in make_dev in disk minilayer.
Add support for registering alias names for a dev_t using the
new function make_dev_alias(). These will show up as symlinks
in DEVFS.
Use makedev() rather than make_dev() for MFSs magic devices to prevent
DEVFS from noticing this abuse.
Add a field for DEVFS inode number in dev_t.
Add new DEVFS in fs/devfs.
Add devfs cloning to:
disk minilayer (ie: ad(4), sd(4), cd(4) etc etc)
md(4), tun(4), bpf(4), fd(4)
If DEVFS add -d flag to /sbin/inits args to make it mount devfs.
Add commented out DEVFS to GENERIC
Diffstat (limited to 'sys/fs')
-rw-r--r-- | sys/fs/devfs/devfs.h | 97 | ||||
-rw-r--r-- | sys/fs/devfs/devfs_devs.c | 230 | ||||
-rw-r--r-- | sys/fs/devfs/devfs_vfsops.c | 243 | ||||
-rw-r--r-- | sys/fs/devfs/devfs_vnops.c | 569 |
4 files changed, 1139 insertions, 0 deletions
diff --git a/sys/fs/devfs/devfs.h b/sys/fs/devfs/devfs.h new file mode 100644 index 0000000..00a34b1 --- /dev/null +++ b/sys/fs/devfs/devfs.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * Copyright (c) 2000 + * Poul-Henning Kamp. All rights reserved. + * + * 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.h 8.6 (Berkeley) 3/29/95 + * From: FreeBSD: src/sys/miscfs/kernfs/kernfs.h 1.14 + * + * $FreeBSD$ + */ + +#ifdef _KERNEL + +#ifdef DEVFS_INTERN + +#define NDEVINO 1024 + +MALLOC_DECLARE(M_DEVFS); + +struct devfs_dir { + TAILQ_HEAD(, devfs_dirent) dd_list; +}; + +struct devfs_dirent { + int de_inode; + struct dirent *de_dirent; + TAILQ_ENTRY(devfs_dirent) de_list; + struct devfs_dir *de_dir; + mode_t de_mode; + uid_t de_uid; + gid_t de_gid; + struct timespec de_atime; + struct timespec de_mtime; + struct timespec de_ctime; + struct vnode *de_vnode; + char * de_symlink; +}; + +struct devfs_node { + struct kern_target *kf_kt; +}; + +struct devfs_mount { + struct vnode *dm_root; /* Root node */ + struct devfs_dir *dm_rootdir; + struct devfs_dir *dm_basedir; + unsigned dm_generation; + struct devfs_dirent *dm_dirent[NDEVINO]; + int dm_inode; +}; + + +extern dev_t devfs_inot[NDEVINO]; +extern int devfs_nino; +extern unsigned devfs_generation; + +#define VFSTODEVFS(mp) ((struct devfs_mount *)((mp)->mnt_data)) + +extern vop_t **devfs_vnodeop_p; +extern vop_t **devfs_specop_p; +int devfs_populate __P((struct devfs_mount *dm)); +struct devfs_dir * devfs_vmkdir __P((void)); +struct devfs_dirent * devfs_newdirent __P((char *name, int namelen)); +void devfs_purge __P((struct devfs_dir *dd)); +void devfs_delete __P((struct devfs_dir *dd, struct devfs_dirent *de)); +#endif /* DEVFS_INTERN */ + +typedef void (*devfs_clone_fn) __P((void *arg, char *name, int namelen, dev_t *result)); + +int devfs_stdclone __P((char *name, char **namep, char *stem, int *unit)); +EVENTHANDLER_DECLARE(devfs_clone, devfs_clone_fn); +#endif /* _KERNEL */ diff --git a/sys/fs/devfs/devfs_devs.c b/sys/fs/devfs/devfs_devs.c new file mode 100644 index 0000000..4bcfc03 --- /dev/null +++ b/sys/fs/devfs/devfs_devs.c @@ -0,0 +1,230 @@ +#define DEBUG 1 +/* + * Copyright (c) 2000 + * Poul-Henning Kamp. All rights reserved. + * + * 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. + * + * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36 + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/dirent.h> +#include <sys/conf.h> +#include <sys/proc.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/malloc.h> +#include <sys/eventhandler.h> +#include <sys/ctype.h> + +#define DEVFS_INTERN +#include <fs/devfs/devfs.h> + +struct devfs_dirent * +devfs_newdirent(char *name, int namelen) +{ + int i; + struct devfs_dirent *de; + struct dirent d; + + d.d_namlen = namelen; + i = sizeof (*de) + GENERIC_DIRSIZ(&d); + MALLOC(de, struct devfs_dirent *, i, M_DEVFS, M_WAITOK); + bzero(de, i); + de->de_dirent = (struct dirent *)(de + 1); + de->de_dirent->d_namlen = namelen; + de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d); + bcopy(name, de->de_dirent->d_name, namelen + 1); + nanotime(&de->de_ctime); + return (de); +} + +struct devfs_dir * +devfs_vmkdir(void) +{ + struct devfs_dir *dd; + struct devfs_dirent *de; + + MALLOC(dd, struct devfs_dir *, sizeof(*dd), M_DEVFS, M_WAITOK); + bzero(dd, sizeof(*dd)); + TAILQ_INIT(&dd->dd_list); + + de = devfs_newdirent(".", 1); + de->de_dirent->d_type = DT_DIR; + TAILQ_INSERT_TAIL(&dd->dd_list, de, de_list); + de = TAILQ_FIRST(&dd->dd_list); + de->de_mode = 0755; + return (dd); +} + +void +devfs_delete(struct devfs_dir *dd, struct devfs_dirent *de) +{ + + if (de->de_symlink) { + FREE(de->de_symlink, M_DEVFS); + de->de_symlink = NULL; + } + if (de->de_vnode) { + de->de_vnode->v_data = NULL; + vdrop(de->de_vnode); + } + TAILQ_REMOVE(&dd->dd_list, de, de_list); + FREE(de, M_DEVFS); +} + +void +devfs_purge(struct devfs_dir *dd) +{ + struct devfs_dirent *de; + + for (;;) { + de = TAILQ_FIRST(&dd->dd_list); + if (de == NULL) + break; + devfs_delete(dd, de); + } + FREE(dd, M_DEVFS); +} + + +int +devfs_populate(struct devfs_mount *dm) +{ + int i, j; + dev_t dev, pdev; + struct devfs_dir *dd; + struct devfs_dirent *de; + char *q, *s; + + while (dm->dm_generation != devfs_generation) { + dm->dm_generation = devfs_generation; + for (i = 0; i < NDEVINO; i++) { + dev = devfs_inot[i]; + de = dm->dm_dirent[i]; + if (dev == NULL && de != NULL) { +#if 0 + printf("Del ino%d %s\n", i, de->de_dirent->d_name); +#endif + dd = de->de_dir; + dm->dm_dirent[i] = NULL; + TAILQ_REMOVE(&dd->dd_list, de, de_list); + if (de->de_vnode) { + de->de_vnode->v_data = NULL; + vdrop(de->de_vnode); + } + FREE(de, M_DEVFS); + continue; + } + if (dev == NULL) + continue; + if (de != NULL) + continue; + dd = dm->dm_basedir; + s = dev->si_name; + for (q = s; *q != '/' && *q != '\0'; q++) + continue; + if (*q == '/') { + continue; + } + de = devfs_newdirent(s, q - s); + if (dev->si_flags & SI_ALIAS) { + de->de_inode = dm->dm_inode++; + de->de_uid = 0; + de->de_gid = 0; + de->de_mode = 0642; + de->de_dirent->d_type = DT_LNK; + pdev = dev->si_drv1; + j = strlen(pdev->si_name) + 1; + MALLOC(de->de_symlink, char *, j, M_DEVFS, M_WAITOK); + bcopy(pdev->si_name, de->de_symlink, j); + } else { + de->de_inode = i; + de->de_uid = dev->si_uid; + de->de_gid = dev->si_gid; + de->de_mode = dev->si_mode; + de->de_dirent->d_type = DT_CHR; + dm->dm_dirent[i] = de; + } + TAILQ_INSERT_TAIL(&dd->dd_list, de, de_list); +#if 0 + printf("Add ino%d %s\n", i, dev->si_name); +#endif + } + } + return (0); +} + +dev_t devfs_inot[NDEVINO]; +int devfs_nino = 3; +unsigned devfs_generation; + +static void +devfs_create(dev_t dev) +{ + if (dev->si_inode == 0 && devfs_nino < NDEVINO) + dev->si_inode = devfs_nino++; + if (dev->si_inode == 0) { + printf("NDEVINO too small\n"); + return; + } + devfs_inot[dev->si_inode] = dev; + devfs_generation++; +} + +static void +devfs_remove(dev_t dev) +{ + devfs_inot[dev->si_inode] = NULL; + devfs_generation++; +} + +devfs_create_t *devfs_create_hook = devfs_create; +devfs_remove_t *devfs_remove_hook = devfs_remove; + +int +devfs_stdclone(char *name, char **namep, char *stem, int *unit) +{ + int u, i; + + if (bcmp(stem, name, strlen(stem)) != 0) + return (0); + i = strlen(stem); + if (!isdigit(name[i])) + return (0); + u = 0; + while (isdigit(name[i])) { + u *= 10; + u += name[i++] - '0'; + } + *unit = u; + if (namep) + *namep = &name[i]; + if (name[i]) + return (2); + return (1); +} diff --git a/sys/fs/devfs/devfs_vfsops.c b/sys/fs/devfs/devfs_vfsops.c new file mode 100644 index 0000000..2cd7611 --- /dev/null +++ b/sys/fs/devfs/devfs_vfsops.c @@ -0,0 +1,243 @@ +/* + * Copyright (c) 1992, 1993, 1995 + * The Regents of the University of California. All rights reserved. + * Copyright (c) 2000 + * Poul-Henning Kamp. All rights reserved. + * + * 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_vfsops.c 8.10 (Berkeley) 5/14/95 + * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36 + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/dirent.h> +#include <sys/conf.h> +#include <sys/proc.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/malloc.h> +#include <sys/eventhandler.h> + +#define DEVFS_INTERN +#include <fs/devfs/devfs.h> + +MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data"); + +static int devfs_mount __P((struct mount *mp, char *path, caddr_t data, + struct nameidata *ndp, struct proc *p)); +static int devfs_unmount __P((struct mount *mp, int mntflags, + struct proc *p)); +static int devfs_root __P((struct mount *mp, struct vnode **vpp)); +static int devfs_statfs __P((struct mount *mp, struct statfs *sbp, + struct proc *p)); + +/* + * Mount the filesystem + */ +static int +devfs_mount(mp, path, data, ndp, p) + struct mount *mp; + char *path; + caddr_t data; + struct nameidata *ndp; + struct proc *p; +{ + int error = 0; + u_int size; + struct devfs_mount *fmp; + struct vnode *rvp; + + /* + * Update is a no-op + */ + if (mp->mnt_flag & MNT_UPDATE) + return (EOPNOTSUPP); + + MALLOC(fmp, struct devfs_mount *, sizeof(struct devfs_mount), M_DEVFS, M_WAITOK); + + bzero(fmp, sizeof(*fmp)); + + error = getnewvnode(VT_DEVFS, mp, devfs_vnodeop_p, &rvp); + if (error) { + FREE(fmp, M_DEVFS); + return (error); + } + + vhold(rvp); + rvp->v_type = VDIR; + rvp->v_flag |= VROOT; + mp->mnt_flag |= MNT_LOCAL; + mp->mnt_data = (qaddr_t) fmp; + vfs_getnewfsid(mp); + + fmp->dm_inode = NDEVINO; + fmp->dm_root = rvp; + fmp->dm_rootdir = devfs_vmkdir(); + rvp->v_data = fmp->dm_rootdir; + TAILQ_FIRST(&fmp->dm_rootdir->dd_list)->de_vnode = rvp; + TAILQ_FIRST(&fmp->dm_rootdir->dd_list)->de_inode = 2; + +#ifdef DEVFS_DEVBASE + { + struct devfs_dirent *de; + + fmp->dm_basedir = devfs_vmkdir(); + de = devfs_newdirent("dev", 3); + de->de_inode = fmp->dm_inode++; + de->de_dir = fmp->dm_basedir; + TAILQ_FIRST(&de->de_dir->dd_list)->de_inode = de->de_inode; + de->de_uid = 0; + de->de_gid = 0; + de->de_mode = 0755; + de->de_dirent->d_type = DT_DIR; + TAILQ_INSERT_TAIL(&fmp->dm_rootdir->dd_list, de, de_list); + } +#else + fmp->dm_basedir = fmp->dm_rootdir; +#endif + + if (path != NULL) { + (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); + } else { + strcpy(mp->mnt_stat.f_mntonname, "/"); + size = 1; + } + bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); + bzero(mp->mnt_stat.f_mntfromname, MNAMELEN); + bcopy("devfs", mp->mnt_stat.f_mntfromname, sizeof("devfs")); + (void)devfs_statfs(mp, &mp->mnt_stat, p); + devfs_populate(fmp); + + return (0); +} + +static int +devfs_unmount(mp, mntflags, p) + struct mount *mp; + int mntflags; + struct proc *p; +{ + int error; + int flags = 0; + struct vnode *rootvp = VFSTODEVFS(mp)->dm_root; + struct devfs_mount *fmp; + + fmp = (struct devfs_mount*) mp->mnt_data; + if (mntflags & MNT_FORCE) + flags |= FORCECLOSE; + + /* + * Clear out buffer cache. I don't think we + * ever get anything cached at this level at the + * moment, but who knows... + */ + if (rootvp->v_usecount > 2) + return (EBUSY); + devfs_purge(fmp->dm_rootdir); + error = vflush(mp, rootvp, flags); + if (error) + return (error); + + /* + * Release reference on underlying root vnode + */ + vrele(rootvp); + /* + * And blow it away for future re-use + */ + vgone(rootvp); + /* + * Finally, throw away the devfs_mount structure + */ + free(mp->mnt_data, M_DEVFS); + mp->mnt_data = 0; + return 0; +} + +static int +devfs_root(mp, vpp) + struct mount *mp; + struct vnode **vpp; +{ + struct proc *p = curproc; /* XXX */ + struct vnode *vp; + + /* + * Return locked reference to root. + */ + vp = VFSTODEVFS(mp)->dm_root; + VREF(vp); + vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); + *vpp = vp; + return (0); +} + +static int +devfs_statfs(mp, sbp, p) + struct mount *mp; + struct statfs *sbp; + struct proc *p; +{ + + sbp->f_flags = 0; + sbp->f_bsize = DEV_BSIZE; + sbp->f_iosize = DEV_BSIZE; + sbp->f_blocks = 2; /* 1K to keep df happy */ + sbp->f_bfree = 0; + sbp->f_bavail = 0; + sbp->f_files = 0; + sbp->f_ffree = 0; + if (sbp != &mp->mnt_stat) { + sbp->f_type = mp->mnt_vfc->vfc_typenum; + bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); + bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); + bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); + } + return (0); +} + +static struct vfsops devfs_vfsops = { + devfs_mount, + vfs_stdstart, + devfs_unmount, + devfs_root, + vfs_stdquotactl, + devfs_statfs, + vfs_stdsync, + vfs_stdvget, + vfs_stdfhtovp, + vfs_stdcheckexp, + vfs_stdvptofh, + vfs_stdinit, + vfs_stduninit, + vfs_stdextattrctl, +}; + +VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC); diff --git a/sys/fs/devfs/devfs_vnops.c b/sys/fs/devfs/devfs_vnops.c new file mode 100644 index 0000000..fb14abf --- /dev/null +++ b/sys/fs/devfs/devfs_vnops.c @@ -0,0 +1,569 @@ +#define DEBUG 1 +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * Copyright (c) 2000 + * Poul-Henning Kamp. All rights reserved. + * + * 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$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/vmmeter.h> +#include <sys/time.h> +#include <sys/conf.h> +#include <sys/vnode.h> +#include <sys/malloc.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/dirent.h> +#include <sys/resource.h> +#include <sys/eventhandler.h> + +#define DEVFS_INTERN +#include <fs/devfs/devfs.h> + +#define KSTRING 256 /* Largest I/O available via this filesystem */ +#define UIO_MX 32 + +static int devfs_access __P((struct vop_access_args *ap)); +static int devfs_badop __P((void)); +static int devfs_getattr __P((struct vop_getattr_args *ap)); +static int devfs_lookup __P((struct vop_lookup_args *ap)); +static int devfs_print __P((struct vop_print_args *ap)); +static int devfs_readdir __P((struct vop_readdir_args *ap)); +static int devfs_readlink __P((struct vop_readlink_args *ap)); +static int devfs_reclaim __P((struct vop_reclaim_args *ap)); +static int devfs_remove __P((struct vop_remove_args *ap)); +static int devfs_revoke __P((struct vop_revoke_args *ap)); +static int devfs_setattr __P((struct vop_setattr_args *ap)); +static int devfs_symlink __P((struct vop_symlink_args *ap)); + + +static int +devfs_allocv(struct devfs_dirent *de, struct mount *mp, struct vnode **vpp, struct proc *p) +{ + int error; + struct vnode *vp; + +loop: + vp = de->de_vnode; + if (vp != NULL) { + if (vget(vp, 0, p ? p : curproc)) + goto loop; + *vpp = vp; + return (0); + } + error = getnewvnode(VT_DEVFS, mp, devfs_vnodeop_p, &vp); + 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; + vp = addaliasu(vp, devfs_inot[de->de_inode]->si_udev); + vp->v_op = devfs_specop_p; + vp->v_data = de; + } else if (de->de_dirent->d_type == DT_DIR) { + vp->v_type = VDIR; + vp->v_data = de->de_dir; + TAILQ_FIRST(&de->de_dir->dd_list)->de_vnode = vp; + } else if (de->de_dirent->d_type == DT_LNK) { + vp->v_type = VLNK; + vp->v_data = de; + } else { + vp->v_type = VBAD; + vp->v_data = de; + } + de->de_vnode = vp; + vhold(vp); + *vpp = vp; + return (0); +} +/* + * vp is the current namei directory + * ndp is the name to locate in that directory... + */ +static int +devfs_lookup(ap) + struct vop_lookup_args /* { + struct vnode * a_dvp; + struct vnode ** a_vpp; + struct componentname * a_cnp; + } */ *ap; +{ + struct componentname *cnp = ap->a_cnp; + struct vnode **vpp = ap->a_vpp; + struct vnode *dvp = ap->a_dvp; + char *pname = cnp->cn_nameptr; + struct proc *p = cnp->cn_proc; + struct devfs_dir *dd; + struct devfs_dirent *de; + struct devfs_mount *fmp; + dev_t cdev; + int error, cloned; + + *vpp = NULLVP; + + VOP_UNLOCK(dvp, 0, p); + if (cnp->cn_namelen == 1 && *pname == '.') { + *vpp = dvp; + VREF(dvp); + vn_lock(dvp, LK_SHARED | LK_RETRY, p); + return (0); + } + + cloned = 0; + + fmp = (struct devfs_mount *)dvp->v_mount->mnt_data; +again: + + devfs_populate(fmp); + dd = dvp->v_data; + TAILQ_FOREACH(de, &dd->dd_list, 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; + goto found; + } + + if (!cloned) { + /* OK, we didn't have that one, so lets try to create it on the fly... */ + cdev = NODEV; + EVENTHANDLER_INVOKE(devfs_clone, cnp->cn_nameptr, cnp->cn_namelen, &cdev); +#if 0 + printf("cloned %s -> %p %s\n", cnp->cn_nameptr, cdev, + cdev == NODEV ? "NODEV" : cdev->si_name); +#endif + if (cdev != NODEV) { + cloned = 1; + goto again; + } + } + + /* No luck, too bad. */ + + if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) && + (cnp->cn_flags & ISLASTCN)) { + cnp->cn_flags |= SAVENAME; + if (!(cnp->cn_flags & LOCKPARENT)) + VOP_UNLOCK(dvp, 0, p); + return (EJUSTRETURN); + } else { + vn_lock(dvp, LK_SHARED | LK_RETRY, p); + return (ENOENT); + } + + +found: + + error = devfs_allocv(de, dvp->v_mount, vpp, p); + if (error != 0) { + vn_lock(dvp, LK_SHARED | LK_RETRY, p); + return (error); + } + if ((cnp->cn_nameiop == DELETE) && (cnp->cn_flags & ISLASTCN)) { + if (*vpp == dvp) { + VREF(dvp); + *vpp = dvp; + return (0); + } + VREF(*vpp); + if (!(cnp->cn_flags & LOCKPARENT)) + VOP_UNLOCK(dvp, 0, p); + return (0); + } + vn_lock(*vpp, LK_SHARED | LK_RETRY, p); + if (!(cnp->cn_flags & LOCKPARENT)) + VOP_UNLOCK(dvp, 0, p); + return (0); +} + +static int +devfs_access(ap) + struct vop_access_args /* { + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + mode_t amode = ap->a_mode; + struct devfs_dirent *de = vp->v_data; + mode_t fmode = de->de_mode; + + /* Some files are simply not modifiable. */ + if ((amode & VWRITE) && (fmode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0) + return (EPERM); + + return (vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid, + ap->a_mode, ap->a_cred)); +} + +static int +devfs_getattr(ap) + struct vop_getattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + struct vattr *vap = ap->a_vap; + int error = 0; + struct devfs_dirent *de; + struct devfs_dir *dd; + + if (vp->v_type == VDIR) { + dd = vp->v_data; + de = TAILQ_FIRST(&dd->dd_list); + } else { + de = vp->v_data; + } + 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; + vap->va_size = 0; + vap->va_blocksize = DEV_BSIZE; + vap->va_atime = de->de_atime; + vap->va_mtime = de->de_mtime; + vap->va_ctime = de->de_ctime; + vap->va_gen = 0; + vap->va_flags = 0; + vap->va_rdev = 0; + vap->va_bytes = 0; + vap->va_nlink = 1; + vap->va_fileid = de->de_inode; + +#if 0 + if (vp->v_flag & VROOT) { +#ifdef DEBUG + printf("devfs_getattr: stat rootdir\n"); +#endif + vap->va_type = VDIR; + vap->va_nlink = 2; + vap->va_fileid = 2; + vap->va_size = DEV_BSIZE; + } else +#endif + + if (de->de_dirent->d_type == DT_DIR) { + vap->va_type = VDIR; + } else if (de->de_dirent->d_type == DT_LNK) { + vap->va_type = VLNK; + } else if (de->de_dirent->d_type == DT_CHR) { + vap->va_type = VCHR; + vap->va_rdev = devfs_inot[de->de_inode]->si_udev; + } + +#ifdef DEBUG + if (error) + printf("devfs_getattr: return error %d\n", error); +#endif + return (error); +} + +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_dir *dd; + struct devfs_dirent *de; + + if (ap->a_vp->v_type == VDIR) { + dd = ap->a_vp->v_data; + de = TAILQ_FIRST(&dd->dd_list); + } else { + de = ap->a_vp->v_data; + } + + if (ap->a_vap->va_flags != VNOVAL) + return (EOPNOTSUPP); + if (ap->a_vap->va_uid != (uid_t)VNOVAL) + de->de_uid = ap->a_vap->va_uid; + if (ap->a_vap->va_gid != (gid_t)VNOVAL) + de->de_gid = ap->a_vap->va_gid; + if (ap->a_vap->va_mode != (mode_t)VNOVAL) + de->de_mode = ap->a_vap->va_mode; + if (ap->a_vap->va_atime.tv_sec != VNOVAL) + de->de_atime = ap->a_vap->va_atime; + if (ap->a_vap->va_mtime.tv_sec != VNOVAL) + de->de_mtime = ap->a_vap->va_mtime; + + /* + * Silently ignore attribute changes. + * This allows for open with truncate to have no + * effect until some data is written. I want to + * do it this way because all writes are atomic. + */ + return (0); +} + +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; +{ + int error, i; + struct uio *uio = ap->a_uio; + struct dirent *dp; + struct devfs_dir *dd; + struct devfs_dirent *de; + off_t off; + + if (ap->a_vp->v_type != VDIR) + return (ENOTDIR); + + i = (u_int)off / UIO_MX; + error = 0; + dd = ap->a_vp->v_data; + de = TAILQ_FIRST(&dd->dd_list); + off = 0; + while (uio->uio_resid >= UIO_MX && de != NULL) { + dp = de->de_dirent; + dp->d_fileno = de->de_inode; + if (off >= uio->uio_offset) + if ((error = uiomove((caddr_t)dp, dp->d_reclen, uio)) != 0) + break; + off += dp->d_reclen; + de = TAILQ_NEXT(de, de_list); + } + + 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; + error = uiomove(de->de_symlink, strlen(de->de_symlink) + 1, ap->a_uio); + return (error); +} + +static int +devfs_reclaim(ap) + struct vop_reclaim_args /* { + struct vnode *a_vp; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + + vp->v_data = NULL; + 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; + struct devfs_dir *dd; + struct devfs_dirent *de; + + dd = ap->a_dvp->v_data; + de = vp->v_data; + devfs_delete(dd, de); + 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; + struct devfs_dirent *de; + + de = vp->v_data; + vdrop(de->de_vnode); + de->de_vnode = NULL; + vop_revoke(ap); + return (0); +} + +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; +{ + int i; + struct devfs_dir *dd; + struct devfs_dirent *de; + struct devfs_mount *fmp; + + fmp = (struct devfs_mount *)ap->a_dvp->v_mount->mnt_data; + 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; + de->de_mode = 0642; + de->de_inode = fmp->dm_inode++; + de->de_dirent->d_type = DT_LNK; + i = strlen(ap->a_target) + 1; + MALLOC(de->de_symlink, char *, i, M_DEVFS, M_WAITOK); + bcopy(ap->a_target, de->de_symlink, i); + TAILQ_INSERT_TAIL(&dd->dd_list, de, de_list); + devfs_allocv(de, ap->a_dvp->v_mount, ap->a_vpp, 0); + VREF(*(ap->a_vpp)); + return (0); +} + +/* + * Print out the contents of a devfs vnode. + */ +/* ARGSUSED */ +static int +devfs_print(ap) + struct vop_print_args /* { + struct vnode *a_vp; + } */ *ap; +{ + + printf("tag VT_DEVFS, devfs vnode\n"); + return (0); +} + +/* + * Kernfs "should never get here" operation + */ +static int +devfs_badop() +{ + return (EIO); +} + +vop_t **devfs_vnodeop_p; +static struct vnodeopv_entry_desc devfs_vnodeop_entries[] = { + { &vop_default_desc, (vop_t *) vop_defaultop }, + { &vop_access_desc, (vop_t *) devfs_access }, + { &vop_bmap_desc, (vop_t *) devfs_badop }, + { &vop_getattr_desc, (vop_t *) devfs_getattr }, + { &vop_lookup_desc, (vop_t *) devfs_lookup }, + { &vop_pathconf_desc, (vop_t *) vop_stdpathconf }, + { &vop_print_desc, (vop_t *) devfs_print }, + { &vop_readdir_desc, (vop_t *) devfs_readdir }, + { &vop_readlink_desc, (vop_t *) devfs_readlink }, + { &vop_reclaim_desc, (vop_t *) devfs_reclaim }, + { &vop_remove_desc, (vop_t *) devfs_remove }, + { &vop_revoke_desc, (vop_t *) devfs_revoke }, + { &vop_setattr_desc, (vop_t *) devfs_setattr }, + { &vop_symlink_desc, (vop_t *) devfs_symlink }, + { NULL, NULL } +}; +static struct vnodeopv_desc devfs_vnodeop_opv_desc = + { &devfs_vnodeop_p, devfs_vnodeop_entries }; + +VNODEOP_SET(devfs_vnodeop_opv_desc); + +#if 0 +int +foo(ap) + struct vop_generic_args *ap; +{ + int i; + + i = spec_vnoperate(ap); + printf("foo(%s) = %d\n", ap->a_desc->vdesc_name, i); + return (i); +} +#endif + +vop_t **devfs_specop_p; +static struct vnodeopv_entry_desc devfs_specop_entries[] = { +#if 1 + { &vop_default_desc, (vop_t *) spec_vnoperate }, +#else + { &vop_default_desc, (vop_t *) foo }, + { &vop_lock_desc, (vop_t *) spec_vnoperate }, + { &vop_unlock_desc, (vop_t *) spec_vnoperate }, + { &vop_lease_desc, (vop_t *) spec_vnoperate }, + { &vop_strategy_desc, (vop_t *) spec_vnoperate }, + { &vop_bmap_desc, (vop_t *) spec_vnoperate }, +#endif + { &vop_access_desc, (vop_t *) devfs_access }, + { &vop_getattr_desc, (vop_t *) devfs_getattr }, + { &vop_print_desc, (vop_t *) devfs_print }, + { &vop_reclaim_desc, (vop_t *) devfs_reclaim }, + { &vop_remove_desc, (vop_t *) devfs_remove }, + { &vop_revoke_desc, (vop_t *) devfs_revoke }, + { &vop_setattr_desc, (vop_t *) devfs_setattr }, + { NULL, NULL } +}; +static struct vnodeopv_desc devfs_specop_opv_desc = + { &devfs_specop_p, devfs_specop_entries }; + +VNODEOP_SET(devfs_specop_opv_desc); |