Home | History | Annotate | Line # | Download | only in procfs
procfs_vfsops.c revision 1.64
      1 /*	$NetBSD: procfs_vfsops.c,v 1.64 2006/05/14 21:31:53 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
     35  */
     36 
     37 /*
     38  * Copyright (c) 1993 Jan-Simon Pendry
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Jan-Simon Pendry.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
     72  */
     73 
     74 /*
     75  * procfs VFS interface
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.64 2006/05/14 21:31:53 elad Exp $");
     80 
     81 #if defined(_KERNEL_OPT)
     82 #include "opt_compat_netbsd.h"
     83 #endif
     84 
     85 #include <sys/param.h>
     86 #include <sys/time.h>
     87 #include <sys/kernel.h>
     88 #include <sys/systm.h>
     89 #include <sys/sysctl.h>
     90 #include <sys/proc.h>
     91 #include <sys/buf.h>
     92 #include <sys/syslog.h>
     93 #include <sys/mount.h>
     94 #include <sys/dirent.h>
     95 #include <sys/signalvar.h>
     96 #include <sys/vnode.h>
     97 #include <sys/malloc.h>
     98 #include <sys/kauth.h>
     99 
    100 #include <miscfs/procfs/procfs.h>
    101 
    102 #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
    103 
    104 void	procfs_init(void);
    105 void	procfs_reinit(void);
    106 void	procfs_done(void);
    107 int	procfs_mount(struct mount *, const char *, void *,
    108 			  struct nameidata *, struct lwp *);
    109 int	procfs_start(struct mount *, int, struct lwp *);
    110 int	procfs_unmount(struct mount *, int, struct lwp *);
    111 int	procfs_quotactl(struct mount *, int, uid_t, void *,
    112 			     struct lwp *);
    113 int	procfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
    114 int	procfs_sync(struct mount *, int, kauth_cred_t, struct lwp *);
    115 int	procfs_vget(struct mount *, ino_t, struct vnode **);
    116 
    117 /*
    118  * VFS Operations.
    119  *
    120  * mount system call
    121  */
    122 /* ARGSUSED */
    123 int
    124 procfs_mount(mp, path, data, ndp, l)
    125 	struct mount *mp;
    126 	const char *path;
    127 	void *data;
    128 	struct nameidata *ndp;
    129 	struct lwp *l;
    130 {
    131 	struct procfsmount *pmnt;
    132 	struct procfs_args args;
    133 	int error;
    134 
    135 	if (UIO_MX & (UIO_MX-1)) {
    136 		log(LOG_ERR, "procfs: invalid directory entry size");
    137 		return (EINVAL);
    138 	}
    139 
    140 	if (mp->mnt_flag & MNT_GETARGS) {
    141 		pmnt = VFSTOPROC(mp);
    142 		if (pmnt == NULL)
    143 			return EIO;
    144 		args.version = PROCFS_ARGSVERSION;
    145 		args.flags = pmnt->pmnt_flags;
    146 		return copyout(&args, data, sizeof(args));
    147 	}
    148 
    149 	if (mp->mnt_flag & MNT_UPDATE)
    150 		return (EOPNOTSUPP);
    151 
    152 	if (data != NULL) {
    153 		error = copyin(data, &args, sizeof args);
    154 		if (error != 0)
    155 			return error;
    156 
    157 		if (args.version != PROCFS_ARGSVERSION)
    158 			return EINVAL;
    159 	} else
    160 		args.flags = 0;
    161 
    162 	pmnt = (struct procfsmount *) malloc(sizeof(struct procfsmount),
    163 	    M_UFSMNT, M_WAITOK);   /* XXX need new malloc type */
    164 
    165 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    166 	mp->mnt_flag |= MNT_LOCAL;
    167 	mp->mnt_data = pmnt;
    168 	vfs_getnewfsid(mp);
    169 
    170 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
    171 	    mp, l);
    172 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
    173 	pmnt->pmnt_flags = args.flags;
    174 
    175 	return error;
    176 }
    177 
    178 /*
    179  * unmount system call
    180  */
    181 int
    182 procfs_unmount(mp, mntflags, l)
    183 	struct mount *mp;
    184 	int mntflags;
    185 	struct lwp *l;
    186 {
    187 	int error;
    188 	int flags = 0;
    189 
    190 	if (mntflags & MNT_FORCE)
    191 		flags |= FORCECLOSE;
    192 
    193 	if ((error = vflush(mp, 0, flags)) != 0)
    194 		return (error);
    195 
    196 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
    197 
    198 	free(mp->mnt_data, M_UFSMNT);
    199 	mp->mnt_data = 0;
    200 
    201 	return (0);
    202 }
    203 
    204 int
    205 procfs_root(mp, vpp)
    206 	struct mount *mp;
    207 	struct vnode **vpp;
    208 {
    209 
    210 	return (procfs_allocvp(mp, vpp, 0, PFSroot, -1));
    211 }
    212 
    213 /* ARGSUSED */
    214 int
    215 procfs_start(mp, flags, l)
    216 	struct mount *mp;
    217 	int flags;
    218 	struct lwp *l;
    219 {
    220 
    221 	return (0);
    222 }
    223 
    224 /*
    225  * Get file system statistics.
    226  */
    227 int
    228 procfs_statvfs(mp, sbp, l)
    229 	struct mount *mp;
    230 	struct statvfs *sbp;
    231 	struct lwp *l;
    232 {
    233 
    234 	sbp->f_bsize = PAGE_SIZE;
    235 	sbp->f_frsize = PAGE_SIZE;
    236 	sbp->f_iosize = PAGE_SIZE;
    237 	sbp->f_blocks = 1;	/* avoid divide by zero in some df's */
    238 	sbp->f_bfree = 0;
    239 	sbp->f_bavail = 0;
    240 	sbp->f_bresvd = 0;
    241 	sbp->f_files = maxproc;			/* approx */
    242 	sbp->f_ffree = maxproc - nprocs;	/* approx */
    243 	sbp->f_favail = maxproc - nprocs;	/* approx */
    244 	sbp->f_fresvd = 0;
    245 	copy_statvfs_info(sbp, mp);
    246 	return (0);
    247 }
    248 
    249 /*ARGSUSED*/
    250 int
    251 procfs_quotactl(mp, cmds, uid, arg, l)
    252 	struct mount *mp;
    253 	int cmds;
    254 	uid_t uid;
    255 	void *arg;
    256 	struct lwp *l;
    257 {
    258 
    259 	return (EOPNOTSUPP);
    260 }
    261 
    262 /*ARGSUSED*/
    263 int
    264 procfs_sync(mp, waitfor, uc, l)
    265 	struct mount *mp;
    266 	int waitfor;
    267 	kauth_cred_t uc;
    268 	struct lwp *l;
    269 {
    270 
    271 	return (0);
    272 }
    273 
    274 /*ARGSUSED*/
    275 int
    276 procfs_vget(mp, ino, vpp)
    277 	struct mount *mp;
    278 	ino_t ino;
    279 	struct vnode **vpp;
    280 {
    281 	return (EOPNOTSUPP);
    282 }
    283 
    284 void
    285 procfs_init()
    286 {
    287 	procfs_hashinit();
    288 }
    289 
    290 void
    291 procfs_reinit()
    292 {
    293 	procfs_hashreinit();
    294 }
    295 
    296 void
    297 procfs_done()
    298 {
    299 	procfs_hashdone();
    300 }
    301 
    302 SYSCTL_SETUP(sysctl_vfs_procfs_setup, "sysctl vfs.procfs subtree setup")
    303 {
    304 
    305 	sysctl_createv(clog, 0, NULL, NULL,
    306 		       CTLFLAG_PERMANENT,
    307 		       CTLTYPE_NODE, "vfs", NULL,
    308 		       NULL, 0, NULL, 0,
    309 		       CTL_VFS, CTL_EOL);
    310 	sysctl_createv(clog, 0, NULL, NULL,
    311 		       CTLFLAG_PERMANENT,
    312 		       CTLTYPE_NODE, "procfs",
    313 		       SYSCTL_DESCR("Process file system"),
    314 		       NULL, 0, NULL, 0,
    315 		       CTL_VFS, 12, CTL_EOL);
    316 	/*
    317 	 * XXX the "12" above could be dynamic, thereby eliminating
    318 	 * one more instance of the "number to vfs" mapping problem,
    319 	 * but "12" is the order as taken from sys/mount.h
    320 	 */
    321 }
    322 
    323 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
    324 
    325 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
    326 	&procfs_vnodeop_opv_desc,
    327 	NULL,
    328 };
    329 
    330 struct vfsops procfs_vfsops = {
    331 	MOUNT_PROCFS,
    332 	procfs_mount,
    333 	procfs_start,
    334 	procfs_unmount,
    335 	procfs_root,
    336 	procfs_quotactl,
    337 	procfs_statvfs,
    338 	procfs_sync,
    339 	procfs_vget,
    340 	NULL,				/* vfs_fhtovp */
    341 	NULL,				/* vfs_vptofh */
    342 	procfs_init,
    343 	procfs_reinit,
    344 	procfs_done,
    345 	NULL,				/* vfs_mountroot */
    346 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    347 	vfs_stdextattrctl,
    348 	procfs_vnodeopv_descs,
    349 };
    350 VFS_ATTACH(procfs_vfsops);
    351