NetBSD/sys/miscfs/kernfs/kernfs_vnops.c

806 lines
20 KiB
C
Raw Normal View History

/* $NetBSD: kernfs_vnops.c,v 1.65 1999/08/03 20:19:19 wrstuden Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software donated to Berkeley by
* Jan-Simon Pendry.
*
changed copyright notice thanks to following statement: Return-Path: jsp@compnews.co.uk Received: from ben.uknet.ac.uk by postgres.Berkeley.EDU (5.61/1.29) id AA25983; Thu, 25 Mar 93 05:37:37 -0800 Received: from fennel.compnews.co.uk by ben.uknet.ac.uk via UKIP with SMTP (PP) id <g.05640-0@ben.uknet.ac.uk>; Thu, 25 Mar 1993 13:37:19 +0000 Received: from sage.compnews.co.uk by fennel.compnews.co.uk; Thu, 25 Mar 93 13:37:08 GMT Message-Id: <28109.9303251337@sage.compnews.co.uk> From: jsp@compnews.co.uk (Jan-Simon Pendry) Date: Thu, 25 Mar 1993 13:37:05 +0100 In-Reply-To: cgd@postgres.berkeley.edu's message as of Mar 25, 5:32am. Phone-Number-1: +44 430 432450 Phone-Number-2: +44 430 432480 x20 Fax-Number: +44 430 432022 X-Mailer: Mail User's Shell (7.2.5 10/14/92) To: cgd@postgres.berkeley.edu Subject: Re: fdesc/kernfs/etc code... You may put this copyright message on the source code: /* * Copyright (c) 1990, 1992 Jan-Simon Pendry * All rights reserved. * * This code is derived from software contributed 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. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. 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. * */
1993-03-25 09:00:17 +03:00
* 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. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
changed copyright notice thanks to following statement: Return-Path: jsp@compnews.co.uk Received: from ben.uknet.ac.uk by postgres.Berkeley.EDU (5.61/1.29) id AA25983; Thu, 25 Mar 93 05:37:37 -0800 Received: from fennel.compnews.co.uk by ben.uknet.ac.uk via UKIP with SMTP (PP) id <g.05640-0@ben.uknet.ac.uk>; Thu, 25 Mar 1993 13:37:19 +0000 Received: from sage.compnews.co.uk by fennel.compnews.co.uk; Thu, 25 Mar 93 13:37:08 GMT Message-Id: <28109.9303251337@sage.compnews.co.uk> From: jsp@compnews.co.uk (Jan-Simon Pendry) Date: Thu, 25 Mar 1993 13:37:05 +0100 In-Reply-To: cgd@postgres.berkeley.edu's message as of Mar 25, 5:32am. Phone-Number-1: +44 430 432450 Phone-Number-2: +44 430 432480 x20 Fax-Number: +44 430 432022 X-Mailer: Mail User's Shell (7.2.5 10/14/92) To: cgd@postgres.berkeley.edu Subject: Re: fdesc/kernfs/etc code... You may put this copyright message on the source code: /* * Copyright (c) 1990, 1992 Jan-Simon Pendry * All rights reserved. * * This code is derived from software contributed 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. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. 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. * */
1993-03-25 09:00:17 +03:00
* 4. 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.
*
changed copyright notice thanks to following statement: Return-Path: jsp@compnews.co.uk Received: from ben.uknet.ac.uk by postgres.Berkeley.EDU (5.61/1.29) id AA25983; Thu, 25 Mar 93 05:37:37 -0800 Received: from fennel.compnews.co.uk by ben.uknet.ac.uk via UKIP with SMTP (PP) id <g.05640-0@ben.uknet.ac.uk>; Thu, 25 Mar 1993 13:37:19 +0000 Received: from sage.compnews.co.uk by fennel.compnews.co.uk; Thu, 25 Mar 93 13:37:08 GMT Message-Id: <28109.9303251337@sage.compnews.co.uk> From: jsp@compnews.co.uk (Jan-Simon Pendry) Date: Thu, 25 Mar 1993 13:37:05 +0100 In-Reply-To: cgd@postgres.berkeley.edu's message as of Mar 25, 5:32am. Phone-Number-1: +44 430 432450 Phone-Number-2: +44 430 432480 x20 Fax-Number: +44 430 432022 X-Mailer: Mail User's Shell (7.2.5 10/14/92) To: cgd@postgres.berkeley.edu Subject: Re: fdesc/kernfs/etc code... You may put this copyright message on the source code: /* * Copyright (c) 1990, 1992 Jan-Simon Pendry * All rights reserved. * * This code is derived from software contributed 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. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. 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. * */
1993-03-25 09:00:17 +03:00
* 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.
*
1998-03-01 05:20:01 +03:00
* @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
*/
/*
* Kernel parameter filesystem (/kern)
*/
1993-12-18 06:48:27 +03:00
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/vmmeter.h>
1993-12-18 06:48:27 +03:00
#include <sys/types.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/file.h>
1993-12-18 06:48:27 +03:00
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/buf.h>
#include <sys/dirent.h>
1994-07-21 14:22:20 +04:00
#include <sys/msgbuf.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/kernfs/kernfs.h>
#include <vm/vm.h>
#include <uvm/uvm_extern.h>
#define KSTRING 256 /* Largest I/O available via this filesystem */
#define UIO_MX 32
#define READ_MODE (S_IRUSR|S_IRGRP|S_IROTH)
#define WRITE_MODE (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
#define DIR_MODE (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
struct kern_target kern_targets[] = {
/* NOTE: The name must be less than UIO_MX-16 chars in length */
#define N(s) sizeof(s)-1, s
/* name data tag type ro/rw */
{ DT_DIR, N("."), 0, KTT_NULL, VDIR, DIR_MODE },
{ DT_DIR, N(".."), 0, KTT_NULL, VDIR, DIR_MODE },
{ DT_REG, N("boottime"), &boottime.tv_sec, KTT_INT, VREG, READ_MODE },
{ DT_REG, N("copyright"), copyright, KTT_STRING, VREG, READ_MODE },
{ DT_REG, N("hostname"), 0, KTT_HOSTNAME, VREG, WRITE_MODE },
{ DT_REG, N("hz"), &hz, KTT_INT, VREG, READ_MODE },
{ DT_REG, N("loadavg"), 0, KTT_AVENRUN, VREG, READ_MODE },
1994-07-21 14:22:20 +04:00
{ DT_REG, N("msgbuf"), 0, KTT_MSGBUF, VREG, READ_MODE },
{ DT_REG, N("pagesize"), &uvmexp.pagesize, KTT_INT, VREG, READ_MODE },
{ DT_REG, N("physmem"), &physmem, KTT_INT, VREG, READ_MODE },
#if 0
{ DT_DIR, N("root"), 0, KTT_NULL, VDIR, DIR_MODE },
#endif
{ DT_BLK, N("rootdev"), &rootdev, KTT_DEVICE, VBLK, READ_MODE },
{ DT_CHR, N("rrootdev"), &rrootdev, KTT_DEVICE, VCHR, READ_MODE },
{ DT_REG, N("time"), 0, KTT_TIME, VREG, READ_MODE },
{ DT_REG, N("version"), version, KTT_STRING, VREG, READ_MODE },
#undef N
};
static int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
1996-02-10 01:39:56 +03:00
int kernfs_lookup __P((void *));
#define kernfs_create genfs_eopnotsupp_rele
#define kernfs_mknod genfs_eopnotsupp_rele
#define kernfs_open genfs_nullop
#define kernfs_close genfs_nullop
1996-02-10 01:39:56 +03:00
int kernfs_access __P((void *));
int kernfs_getattr __P((void *));
int kernfs_setattr __P((void *));
int kernfs_read __P((void *));
int kernfs_write __P((void *));
#define kernfs_fcntl genfs_fcntl
#define kernfs_ioctl genfs_enoioctl
1996-09-07 16:40:22 +04:00
#define kernfs_poll genfs_poll
1998-03-01 05:20:01 +03:00
#define kernfs_revoke genfs_revoke
#define kernfs_mmap genfs_eopnotsupp
#define kernfs_fsync genfs_nullop
#define kernfs_seek genfs_nullop
#define kernfs_remove genfs_eopnotsupp_rele
1996-02-10 01:39:56 +03:00
int kernfs_link __P((void *));
#define kernfs_rename genfs_eopnotsupp_rele
#define kernfs_mkdir genfs_eopnotsupp_rele
#define kernfs_rmdir genfs_eopnotsupp_rele
1996-02-10 01:39:56 +03:00
int kernfs_symlink __P((void *));
int kernfs_readdir __P((void *));
#define kernfs_readlink genfs_eopnotsupp
#define kernfs_abortop genfs_abortop
1996-02-10 01:39:56 +03:00
int kernfs_inactive __P((void *));
int kernfs_reclaim __P((void *));
#define kernfs_lock genfs_lock
#define kernfs_unlock genfs_unlock
#define kernfs_bmap genfs_badop
#define kernfs_strategy genfs_badop
1996-02-10 01:39:56 +03:00
int kernfs_print __P((void *));
#define kernfs_islocked genfs_islocked
1996-02-10 01:39:56 +03:00
int kernfs_pathconf __P((void *));
#define kernfs_advlock genfs_einval
#define kernfs_blkatoff genfs_eopnotsupp
#define kernfs_valloc genfs_eopnotsupp
#define kernfs_vfree genfs_nullop
#define kernfs_truncate genfs_eopnotsupp
#define kernfs_update genfs_nullop
#define kernfs_bwrite genfs_eopnotsupp
1996-02-10 01:39:56 +03:00
int kernfs_xread __P((struct kern_target *, int, char **, int));
int kernfs_xwrite __P((struct kern_target *, char *, int));
int (**kernfs_vnodeop_p) __P((void *));
struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
{ &vop_default_desc, vn_default_error },
{ &vop_lookup_desc, kernfs_lookup }, /* lookup */
{ &vop_create_desc, kernfs_create }, /* create */
{ &vop_mknod_desc, kernfs_mknod }, /* mknod */
{ &vop_open_desc, kernfs_open }, /* open */
{ &vop_close_desc, kernfs_close }, /* close */
{ &vop_access_desc, kernfs_access }, /* access */
{ &vop_getattr_desc, kernfs_getattr }, /* getattr */
{ &vop_setattr_desc, kernfs_setattr }, /* setattr */
{ &vop_read_desc, kernfs_read }, /* read */
{ &vop_write_desc, kernfs_write }, /* write */
{ &vop_fcntl_desc, kernfs_fcntl }, /* fcntl */
{ &vop_ioctl_desc, kernfs_ioctl }, /* ioctl */
1996-09-07 16:40:22 +04:00
{ &vop_poll_desc, kernfs_poll }, /* poll */
1998-03-01 05:20:01 +03:00
{ &vop_revoke_desc, kernfs_revoke }, /* revoke */
{ &vop_mmap_desc, kernfs_mmap }, /* mmap */
{ &vop_fsync_desc, kernfs_fsync }, /* fsync */
{ &vop_seek_desc, kernfs_seek }, /* seek */
{ &vop_remove_desc, kernfs_remove }, /* remove */
{ &vop_link_desc, kernfs_link }, /* link */
{ &vop_rename_desc, kernfs_rename }, /* rename */
{ &vop_mkdir_desc, kernfs_mkdir }, /* mkdir */
{ &vop_rmdir_desc, kernfs_rmdir }, /* rmdir */
{ &vop_symlink_desc, kernfs_symlink }, /* symlink */
{ &vop_readdir_desc, kernfs_readdir }, /* readdir */
{ &vop_readlink_desc, kernfs_readlink }, /* readlink */
{ &vop_abortop_desc, kernfs_abortop }, /* abortop */
{ &vop_inactive_desc, kernfs_inactive }, /* inactive */
{ &vop_reclaim_desc, kernfs_reclaim }, /* reclaim */
{ &vop_lock_desc, kernfs_lock }, /* lock */
{ &vop_unlock_desc, kernfs_unlock }, /* unlock */
{ &vop_bmap_desc, kernfs_bmap }, /* bmap */
{ &vop_strategy_desc, kernfs_strategy }, /* strategy */
{ &vop_print_desc, kernfs_print }, /* print */
{ &vop_islocked_desc, kernfs_islocked }, /* islocked */
{ &vop_pathconf_desc, kernfs_pathconf }, /* pathconf */
{ &vop_advlock_desc, kernfs_advlock }, /* advlock */
{ &vop_blkatoff_desc, kernfs_blkatoff }, /* blkatoff */
{ &vop_valloc_desc, kernfs_valloc }, /* valloc */
{ &vop_vfree_desc, kernfs_vfree }, /* vfree */
{ &vop_truncate_desc, kernfs_truncate }, /* truncate */
{ &vop_update_desc, kernfs_update }, /* update */
{ &vop_bwrite_desc, kernfs_bwrite }, /* bwrite */
1996-02-10 01:39:56 +03:00
{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
};
struct vnodeopv_desc kernfs_vnodeop_opv_desc =
{ &kernfs_vnodeop_p, kernfs_vnodeop_entries };
1994-07-21 14:22:20 +04:00
int
kernfs_xread(kt, off, bufp, len)
struct kern_target *kt;
1994-07-21 14:22:20 +04:00
int off;
char **bufp;
int len;
{
switch (kt->kt_tag) {
case KTT_TIME: {
struct timeval tv;
1994-07-21 14:22:20 +04:00
microtime(&tv);
1996-10-13 06:21:25 +04:00
sprintf(*bufp, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
break;
}
case KTT_INT: {
int *ip = kt->kt_data;
1994-07-21 14:22:20 +04:00
1996-10-13 06:21:25 +04:00
sprintf(*bufp, "%d\n", *ip);
break;
}
case KTT_STRING: {
char *cp = kt->kt_data;
1994-07-21 14:22:20 +04:00
*bufp = cp;
break;
}
1994-07-21 14:22:20 +04:00
case KTT_MSGBUF: {
long n;
/*
* deal with cases where the message buffer has
* become corrupted.
*/
if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
msgbufenabled = 0;
return (ENXIO);
}
/*
* Note that reads of /kern/msgbuf won't necessarily yield
* consistent results, if the message buffer is modified
* while the read is in progress. The worst that can happen
* is that incorrect data will be read. There's no way
* that this can crash the system unless the values in the
* message buffer header are corrupted, but that'll cause
* the system to die anyway.
*/
if (off >= msgbufp->msg_bufs)
1994-07-21 14:22:20 +04:00
return (0);
n = msgbufp->msg_bufx + off;
if (n >= msgbufp->msg_bufs)
n -= msgbufp->msg_bufs;
len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
1994-07-21 14:22:20 +04:00
*bufp = msgbufp->msg_bufc + n;
return (len);
}
case KTT_HOSTNAME: {
char *cp = hostname;
int xlen = hostnamelen;
if (xlen >= (len-2))
return (EINVAL);
memcpy(*bufp, cp, xlen);
1994-07-21 14:22:20 +04:00
(*bufp)[xlen] = '\n';
(*bufp)[xlen+1] = '\0';
break;
}
case KTT_AVENRUN:
averunnable.fscale = FSCALE;
1996-10-13 06:21:25 +04:00
sprintf(*bufp, "%d %d %d %ld\n",
averunnable.ldavg[0], averunnable.ldavg[1],
averunnable.ldavg[2], averunnable.fscale);
break;
default:
1994-07-21 14:22:20 +04:00
return (0);
}
1994-07-21 14:22:20 +04:00
len = strlen(*bufp);
if (len <= off)
return (0);
*bufp += off;
return (len - off);
}
1994-07-21 14:22:20 +04:00
int
kernfs_xwrite(kt, buf, len)
struct kern_target *kt;
char *buf;
int len;
{
switch (kt->kt_tag) {
case KTT_HOSTNAME:
if (buf[len-1] == '\n')
--len;
memcpy(hostname, buf, len);
hostname[len] = '\0';
hostnamelen = len;
return (0);
default:
return (EIO);
}
}
/*
* vp is the current namei directory
* ndp is the name to locate in that directory...
*/
1996-02-10 01:39:56 +03:00
int
kernfs_lookup(v)
void *v;
{
struct vop_lookup_args /* {
struct vnode * a_dvp;
struct vnode ** a_vpp;
struct componentname * a_cnp;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct componentname *cnp = ap->a_cnp;
struct vnode **vpp = ap->a_vpp;
struct vnode *dvp = ap->a_dvp;
const char *pname = cnp->cn_nameptr;
struct kern_target *kt;
struct vnode *fvp;
int error, i, wantpunlock;
#ifdef KERNFS_DIAGNOSTIC
printf("kernfs_lookup(%p)\n", ap);
printf("kernfs_lookup(dp = %p, vpp = %p, cnp = %p)\n", dvp, vpp, ap->a_cnp);
1996-10-13 06:21:25 +04:00
printf("kernfs_lookup(%s)\n", pname);
#endif
*vpp = NULLVP;
cnp->cn_flags &= ~PDIRUNLOCK;
if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
return (EROFS);
if (cnp->cn_namelen == 1 && *pname == '.') {
*vpp = dvp;
VREF(dvp);
return (0);
}
/*
* This code only supports a flat directory, so we don't
* need to worry about ..
*/
#if 0
if (cnp->cn_namelen == 4 && memcmp(pname, "root", 4) == 0) {
*vpp = rootdir;
VREF(rootdir);
1998-03-01 05:20:01 +03:00
vn_lock(rootdir, LK_SHARED | LK_RETRY);
return (0);
}
#endif
1993-03-27 03:37:08 +03:00
wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
for (kt = kern_targets, i = 0; i < nkern_targets; kt++, i++) {
if (cnp->cn_namelen == kt->kt_namlen &&
memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
goto found;
}
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kernfs_lookup: i = %d, failed", i);
#endif
return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
found:
if (kt->kt_tag == KTT_DEVICE) {
dev_t *dp = kt->kt_data;
1994-06-15 07:30:44 +04:00
loop:
1998-03-01 05:20:01 +03:00
if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp)) {
1994-06-15 07:30:44 +04:00
return (ENOENT);
1998-03-01 05:20:01 +03:00
}
*vpp = fvp;
1998-03-01 05:20:01 +03:00
if (vget(fvp, LK_EXCLUSIVE))
1994-06-15 07:30:44 +04:00
goto loop;
if (wantpunlock) {
VOP_UNLOCK(dvp, 0);
cnp->cn_flags |= PDIRUNLOCK;
}
return (0);
}
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kernfs_lookup: allocate new vnode\n");
#endif
1996-02-10 01:39:56 +03:00
error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p, &fvp);
1998-03-01 05:20:01 +03:00
if (error) {
return (error);
1998-03-01 05:20:01 +03:00
}
MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP,
M_WAITOK);
VTOKERN(fvp)->kf_kt = kt;
fvp->v_type = kt->kt_vtype;
vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
*vpp = fvp;
#ifdef KERNFS_DIAGNOSTIC
printf("kernfs_lookup: newvp = %p\n", fvp);
#endif
if (wantpunlock) {
VOP_UNLOCK(dvp, 0);
cnp->cn_flags |= PDIRUNLOCK;
}
return (0);
}
1994-07-21 14:22:20 +04:00
int
1996-02-10 01:39:56 +03:00
kernfs_access(v)
void *v;
{
struct vop_access_args /* {
struct vnode *a_vp;
1994-12-27 22:05:12 +03:00
int a_mode;
struct ucred *a_cred;
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct vnode *vp = ap->a_vp;
mode_t mode;
if (vp->v_flag & VROOT) {
mode = DIR_MODE;
} else {
struct kern_target *kt = VTOKERN(vp)->kf_kt;
mode = kt->kt_mode;
}
return (vaccess(vp->v_type, mode, (uid_t)0, (gid_t)0, ap->a_mode,
ap->a_cred));
}
1996-02-10 01:39:56 +03:00
int
kernfs_getattr(v)
void *v;
{
struct vop_getattr_args /* {
struct vnode *a_vp;
struct vattr *a_vap;
struct ucred *a_cred;
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct vattr *vap = ap->a_vap;
1995-04-15 05:56:43 +04:00
struct timeval tv;
int error = 0;
1994-07-21 14:22:20 +04:00
char strbuf[KSTRING], *buf;
memset((caddr_t) vap, 0, sizeof(*vap));
vattr_null(vap);
vap->va_uid = 0;
vap->va_gid = 0;
vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
vap->va_size = 0;
vap->va_blocksize = DEV_BSIZE;
1995-04-15 05:56:43 +04:00
microtime(&tv);
TIMEVAL_TO_TIMESPEC(&tv, &vap->va_atime);
vap->va_mtime = vap->va_atime;
vap->va_ctime = vap->va_ctime;
vap->va_gen = 0;
vap->va_flags = 0;
vap->va_rdev = 0;
vap->va_bytes = 0;
if (vp->v_flag & VROOT) {
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kernfs_getattr: stat rootdir\n");
#endif
vap->va_type = VDIR;
vap->va_mode = DIR_MODE;
vap->va_nlink = 2;
vap->va_fileid = 2;
vap->va_size = DEV_BSIZE;
} else {
struct kern_target *kt = VTOKERN(vp)->kf_kt;
1994-07-21 14:22:20 +04:00
int nbytes, total;
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kernfs_getattr: stat target %s\n", kt->kt_name);
#endif
vap->va_type = kt->kt_vtype;
vap->va_mode = kt->kt_mode;
vap->va_nlink = 1;
1998-03-01 05:20:01 +03:00
vap->va_fileid = 1 + (kt - kern_targets);
1994-07-21 14:22:20 +04:00
total = 0;
while (buf = strbuf,
nbytes = kernfs_xread(kt, total, &buf, sizeof(strbuf)))
total += nbytes;
vap->va_size = total;
}
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kernfs_getattr: return error %d\n", error);
#endif
return (error);
}
1996-02-10 01:39:56 +03:00
/*ARGSUSED*/
int
kernfs_setattr(v)
void *v;
{
/*
* 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);
}
1994-07-21 14:22:20 +04:00
int
1996-02-10 01:39:56 +03:00
kernfs_read(v)
void *v;
{
struct vop_read_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
struct kern_target *kt;
1994-07-21 14:22:20 +04:00
char strbuf[KSTRING], *buf;
int off, len;
int error;
if (vp->v_type == VDIR)
return (EOPNOTSUPP);
kt = VTOKERN(vp)->kf_kt;
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kern_read %s\n", kt->kt_name);
#endif
1994-07-21 14:22:20 +04:00
off = uio->uio_offset;
#if 0
while (buf = strbuf,
#else
if (buf = strbuf,
#endif
len = kernfs_xread(kt, off, &buf, sizeof(strbuf))) {
1996-02-10 01:39:56 +03:00
if ((error = uiomove(buf, len, uio)) != 0)
1994-07-21 14:22:20 +04:00
return (error);
off += len;
}
return (0);
}
1994-07-21 14:22:20 +04:00
int
1996-02-10 01:39:56 +03:00
kernfs_write(v)
void *v;
{
struct vop_write_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
struct kern_target *kt;
int error, xlen;
char strbuf[KSTRING];
if (vp->v_type == VDIR)
return (EOPNOTSUPP);
kt = VTOKERN(vp)->kf_kt;
if (uio->uio_offset != 0)
return (EINVAL);
xlen = min(uio->uio_resid, KSTRING-1);
1996-02-10 01:39:56 +03:00
if ((error = uiomove(strbuf, xlen, uio)) != 0)
return (error);
if (uio->uio_resid != 0)
return (EIO);
strbuf[xlen] = '\0';
xlen = strlen(strbuf);
return (kernfs_xwrite(kt, strbuf, xlen));
}
1996-02-10 01:39:56 +03:00
int
kernfs_readdir(v)
void *v;
{
struct vop_readdir_args /* {
struct vnode *a_vp;
struct uio *a_uio;
struct ucred *a_cred;
int *a_eofflag;
1998-03-01 05:20:01 +03:00
off_t **a_cookies;
int a_*ncookies;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct uio *uio = ap->a_uio;
struct dirent d;
struct kern_target *kt;
int i;
int error;
1998-03-01 05:20:01 +03:00
off_t *cookies = NULL;
int ncookies = 0, nc = 0;
if (ap->a_vp->v_type != VDIR)
return (ENOTDIR);
if (uio->uio_resid < UIO_MX)
return (EINVAL);
if (uio->uio_offset < 0)
return (EINVAL);
error = 0;
i = uio->uio_offset;
memset((caddr_t)&d, 0, UIO_MX);
d.d_reclen = UIO_MX;
1998-03-01 05:20:01 +03:00
if (ap->a_ncookies) {
nc = uio->uio_resid / UIO_MX;
nc = min(nc, (nkern_targets - i));
MALLOC(cookies, off_t *, nc * sizeof(off_t), M_TEMP,
M_WAITOK);
*ap->a_cookies = cookies;
}
for (kt = &kern_targets[i];
uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) {
#ifdef KERNFS_DIAGNOSTIC
1996-10-13 06:21:25 +04:00
printf("kernfs_readdir: i = %d\n", i);
#endif
if (kt->kt_tag == KTT_DEVICE) {
dev_t *dp = kt->kt_data;
struct vnode *fvp;
if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
continue;
}
d.d_fileno = i + 3;
d.d_namlen = kt->kt_namlen;
memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
d.d_type = kt->kt_type;
1996-02-10 01:39:56 +03:00
if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
break;
1998-03-01 05:20:01 +03:00
if (cookies) {
*cookies++ = i + 1;
1998-03-01 05:20:01 +03:00
ncookies++;
}
}
if (ap->a_ncookies) {
if (error) {
FREE(*ap->a_cookies, M_TEMP);
*ap->a_ncookies = 0;
*ap->a_cookies = NULL;
} else
*ap->a_ncookies = ncookies;
}
uio->uio_offset = i;
return (error);
}
1996-02-10 01:39:56 +03:00
int
kernfs_inactive(v)
void *v;
{
struct vop_inactive_args /* {
struct vnode *a_vp;
1998-03-01 05:20:01 +03:00
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct vnode *vp = ap->a_vp;
#ifdef KERNFS_DIAGNOSTIC
printf("kernfs_inactive(%p)\n", vp);
#endif
/*
* Clear out the v_type field to avoid
* nasty things happening in vgone().
*/
1998-03-01 05:20:01 +03:00
VOP_UNLOCK(vp, 0);
vp->v_type = VNON;
return (0);
}
1996-02-10 01:39:56 +03:00
int
kernfs_reclaim(v)
void *v;
{
struct vop_reclaim_args /* {
struct vnode *a_vp;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct vnode *vp = ap->a_vp;
#ifdef KERNFS_DIAGNOSTIC
printf("kernfs_reclaim(%p)\n", vp);
#endif
if (vp->v_data) {
FREE(vp->v_data, M_TEMP);
vp->v_data = 0;
}
return (0);
}
/*
* Return POSIX pathconf information applicable to special devices.
*/
1996-02-10 01:39:56 +03:00
int
kernfs_pathconf(v)
void *v;
{
struct vop_pathconf_args /* {
struct vnode *a_vp;
int a_name;
register_t *a_retval;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = LINK_MAX;
return (0);
case _PC_MAX_CANON:
*ap->a_retval = MAX_CANON;
return (0);
case _PC_MAX_INPUT:
*ap->a_retval = MAX_INPUT;
return (0);
case _PC_PIPE_BUF:
*ap->a_retval = PIPE_BUF;
return (0);
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return (0);
case _PC_VDISABLE:
*ap->a_retval = _POSIX_VDISABLE;
return (0);
1998-08-03 18:19:57 +04:00
case _PC_SYNC_IO:
*ap->a_retval = 1;
return (0);
default:
return (EINVAL);
}
/* NOTREACHED */
}
/*
* Print out the contents of a /dev/fd vnode.
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
kernfs_print(v)
void *v;
{
1996-10-13 06:21:25 +04:00
printf("tag VT_KERNFS, kernfs vnode\n");
return (0);
}
int
1996-02-10 01:39:56 +03:00
kernfs_link(v)
void *v;
{
struct vop_link_args /* {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
vput(ap->a_dvp);
return (EROFS);
}
int
1996-02-10 01:39:56 +03:00
kernfs_symlink(v)
void *v;
{
struct vop_symlink_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
char *a_target;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
vput(ap->a_dvp);
return (EROFS);
}