Home | History | Annotate | Line # | Download | only in procfs
procfs_vfsops.c revision 1.91
      1  1.91      maxv /*	$NetBSD: procfs_vfsops.c,v 1.91 2014/04/16 18:55:19 maxv Exp $	*/
      2  1.18       cgd 
      3   1.1        pk /*
      4  1.16   mycroft  * Copyright (c) 1993
      5  1.16   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.2        pk  *
      7   1.9       cgd  * This code is derived from software contributed to Berkeley by
      8   1.9       cgd  * Jan-Simon Pendry.
      9   1.9       cgd  *
     10   1.2        pk  * Redistribution and use in source and binary forms, with or without
     11   1.2        pk  * modification, are permitted provided that the following conditions
     12   1.2        pk  * are met:
     13   1.2        pk  * 1. Redistributions of source code must retain the above copyright
     14   1.2        pk  *    notice, this list of conditions and the following disclaimer.
     15   1.2        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2        pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.2        pk  *    documentation and/or other materials provided with the distribution.
     18  1.49       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.49       agc  *    may be used to endorse or promote products derived from this software
     20  1.49       agc  *    without specific prior written permission.
     21  1.49       agc  *
     22  1.49       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.49       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.49       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.49       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.49       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.49       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.49       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.49       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.49       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.49       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.49       agc  * SUCH DAMAGE.
     33  1.49       agc  *
     34  1.49       agc  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
     35  1.49       agc  */
     36  1.49       agc 
     37  1.49       agc /*
     38  1.49       agc  * Copyright (c) 1993 Jan-Simon Pendry
     39  1.49       agc  *
     40  1.49       agc  * This code is derived from software contributed to Berkeley by
     41  1.49       agc  * Jan-Simon Pendry.
     42  1.49       agc  *
     43  1.49       agc  * Redistribution and use in source and binary forms, with or without
     44  1.49       agc  * modification, are permitted provided that the following conditions
     45  1.49       agc  * are met:
     46  1.49       agc  * 1. Redistributions of source code must retain the above copyright
     47  1.49       agc  *    notice, this list of conditions and the following disclaimer.
     48  1.49       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.49       agc  *    notice, this list of conditions and the following disclaimer in the
     50  1.49       agc  *    documentation and/or other materials provided with the distribution.
     51   1.2        pk  * 3. All advertising materials mentioning features or use of this software
     52   1.2        pk  *    must display the following acknowledgement:
     53   1.9       cgd  *	This product includes software developed by the University of
     54   1.9       cgd  *	California, Berkeley and its contributors.
     55   1.9       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.9       cgd  *    may be used to endorse or promote products derived from this software
     57   1.9       cgd  *    without specific prior written permission.
     58   1.9       cgd  *
     59   1.9       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.9       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.9       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.9       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.9       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.9       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.9       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.9       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.9       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.9       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.9       cgd  * SUCH DAMAGE.
     70   1.9       cgd  *
     71  1.28      fvdl  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
     72   1.2        pk  */
     73   1.2        pk 
     74   1.2        pk /*
     75   1.9       cgd  * procfs VFS interface
     76   1.1        pk  */
     77  1.41     lukem 
     78  1.41     lukem #include <sys/cdefs.h>
     79  1.91      maxv __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.91 2014/04/16 18:55:19 maxv Exp $");
     80  1.29  jonathan 
     81  1.39       mrg #if defined(_KERNEL_OPT)
     82  1.29  jonathan #include "opt_compat_netbsd.h"
     83  1.29  jonathan #endif
     84   1.1        pk 
     85   1.8   mycroft #include <sys/param.h>
     86   1.8   mycroft #include <sys/time.h>
     87   1.8   mycroft #include <sys/kernel.h>
     88  1.25  christos #include <sys/systm.h>
     89  1.51    atatat #include <sys/sysctl.h>
     90   1.8   mycroft #include <sys/proc.h>
     91   1.8   mycroft #include <sys/buf.h>
     92   1.9       cgd #include <sys/syslog.h>
     93   1.8   mycroft #include <sys/mount.h>
     94  1.54  christos #include <sys/dirent.h>
     95   1.8   mycroft #include <sys/signalvar.h>
     96   1.8   mycroft #include <sys/vnode.h>
     97  1.32      fvdl #include <sys/malloc.h>
     98  1.64      elad #include <sys/kauth.h>
     99  1.79    rumble #include <sys/module.h>
    100  1.35       mrg 
    101  1.77  dholland #include <miscfs/genfs/genfs.h>
    102  1.77  dholland 
    103   1.9       cgd #include <miscfs/procfs/procfs.h>
    104  1.35       mrg 
    105  1.35       mrg #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
    106   1.1        pk 
    107  1.79    rumble MODULE(MODULE_CLASS_VFS, procfs, NULL);
    108  1.79    rumble 
    109  1.74     pooka VFS_PROTOS(procfs);
    110  1.52    atatat 
    111  1.81    rumble static struct sysctllog *procfs_sysctl_log;
    112  1.81    rumble 
    113  1.84      elad static kauth_listener_t procfs_listener;
    114  1.84      elad 
    115   1.1        pk /*
    116   1.1        pk  * VFS Operations.
    117   1.1        pk  *
    118   1.1        pk  * mount system call
    119   1.1        pk  */
    120   1.1        pk /* ARGSUSED */
    121  1.25  christos int
    122  1.66  christos procfs_mount(
    123  1.66  christos     struct mount *mp,
    124  1.66  christos     const char *path,
    125  1.66  christos     void *data,
    126  1.75     pooka     size_t *data_len)
    127   1.1        pk {
    128  1.75     pooka 	struct lwp *l = curlwp;
    129  1.32      fvdl 	struct procfsmount *pmnt;
    130  1.71       dsl 	struct procfs_args *args = data;
    131  1.36      fvdl 	int error;
    132   1.1        pk 
    133  1.91      maxv 	if (args == NULL)
    134  1.91      maxv 		return EINVAL;
    135  1.91      maxv 
    136   1.9       cgd 	if (UIO_MX & (UIO_MX-1)) {
    137   1.9       cgd 		log(LOG_ERR, "procfs: invalid directory entry size");
    138   1.9       cgd 		return (EINVAL);
    139  1.43  christos 	}
    140  1.43  christos 
    141  1.43  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    142  1.71       dsl 		if (*data_len < sizeof *args)
    143  1.71       dsl 			return EINVAL;
    144  1.71       dsl 
    145  1.43  christos 		pmnt = VFSTOPROC(mp);
    146  1.43  christos 		if (pmnt == NULL)
    147  1.43  christos 			return EIO;
    148  1.71       dsl 		args->version = PROCFS_ARGSVERSION;
    149  1.71       dsl 		args->flags = pmnt->pmnt_flags;
    150  1.71       dsl 		*data_len = sizeof *args;
    151  1.71       dsl 		return 0;
    152   1.1        pk 	}
    153   1.1        pk 
    154   1.9       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    155   1.9       cgd 		return (EOPNOTSUPP);
    156   1.9       cgd 
    157  1.71       dsl 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
    158  1.71       dsl 		return EINVAL;
    159  1.36      fvdl 
    160  1.87     rmind 	pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
    161  1.32      fvdl 
    162  1.86  christos 	mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
    163  1.58  jdolecek 	mp->mnt_flag |= MNT_LOCAL;
    164  1.42     soren 	mp->mnt_data = pmnt;
    165  1.34     assar 	vfs_getnewfsid(mp);
    166   1.9       cgd 
    167  1.53  christos 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
    168  1.72     pooka 	    mp->mnt_op->vfs_name, mp, l);
    169  1.32      fvdl 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
    170  1.71       dsl 	if (*data_len >= sizeof *args)
    171  1.71       dsl 		pmnt->pmnt_flags = args->flags;
    172  1.71       dsl 	else
    173  1.71       dsl 		pmnt->pmnt_flags = 0;
    174  1.32      fvdl 
    175  1.76        ad 	mp->mnt_iflag |= IMNT_MPSAFE;
    176  1.45  christos 	return error;
    177   1.1        pk }
    178   1.1        pk 
    179   1.1        pk /*
    180   1.1        pk  * unmount system call
    181   1.1        pk  */
    182  1.25  christos int
    183  1.75     pooka procfs_unmount(struct mount *mp, int mntflags)
    184   1.1        pk {
    185   1.9       cgd 	int error;
    186   1.9       cgd 	int flags = 0;
    187   1.9       cgd 
    188  1.28      fvdl 	if (mntflags & MNT_FORCE)
    189   1.9       cgd 		flags |= FORCECLOSE;
    190   1.9       cgd 
    191  1.25  christos 	if ((error = vflush(mp, 0, flags)) != 0)
    192   1.9       cgd 		return (error);
    193   1.9       cgd 
    194  1.32      fvdl 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
    195  1.32      fvdl 
    196  1.87     rmind 	kmem_free(mp->mnt_data, sizeof(struct procfsmount));
    197  1.80    simonb 	mp->mnt_data = NULL;
    198  1.32      fvdl 
    199  1.87     rmind 	return 0;
    200   1.1        pk }
    201   1.1        pk 
    202  1.25  christos int
    203  1.82       dsl procfs_root(struct mount *mp, struct vnode **vpp)
    204   1.1        pk {
    205  1.88   hannken 	int error;
    206  1.88   hannken 
    207  1.88   hannken 	error = procfs_allocvp(mp, vpp, 0, PFSroot, -1, NULL);
    208  1.88   hannken 	if (error == 0) {
    209  1.88   hannken 		error = vn_lock(*vpp, LK_EXCLUSIVE);
    210  1.88   hannken 		if (error != 0) {
    211  1.88   hannken 			vrele(*vpp);
    212  1.88   hannken 			*vpp = NULL;
    213  1.88   hannken 		}
    214  1.88   hannken 	}
    215   1.1        pk 
    216  1.88   hannken 	return error;
    217   1.1        pk }
    218   1.1        pk 
    219   1.1        pk /* ARGSUSED */
    220  1.25  christos int
    221  1.75     pooka procfs_start(struct mount *mp, int flags)
    222   1.1        pk {
    223   1.9       cgd 
    224   1.9       cgd 	return (0);
    225   1.1        pk }
    226   1.1        pk 
    227   1.1        pk /*
    228   1.1        pk  * Get file system statistics.
    229   1.1        pk  */
    230  1.25  christos int
    231  1.75     pooka procfs_statvfs(struct mount *mp, struct statvfs *sbp)
    232   1.1        pk {
    233  1.16   mycroft 
    234  1.85     pooka 	genfs_statvfs(mp, sbp);
    235  1.85     pooka 
    236  1.16   mycroft 	sbp->f_bsize = PAGE_SIZE;
    237  1.53  christos 	sbp->f_frsize = PAGE_SIZE;
    238  1.14       cgd 	sbp->f_iosize = PAGE_SIZE;
    239  1.85     pooka 	sbp->f_blocks = 1;
    240   1.9       cgd 	sbp->f_files = maxproc;			/* approx */
    241   1.9       cgd 	sbp->f_ffree = maxproc - nprocs;	/* approx */
    242  1.53  christos 	sbp->f_favail = maxproc - nprocs;	/* approx */
    243  1.85     pooka 
    244   1.9       cgd 	return (0);
    245   1.1        pk }
    246   1.1        pk 
    247  1.25  christos /*ARGSUSED*/
    248  1.25  christos int
    249  1.66  christos procfs_sync(
    250  1.67  christos     struct mount *mp,
    251  1.67  christos     int waitfor,
    252  1.75     pooka     kauth_cred_t uc)
    253   1.1        pk {
    254   1.9       cgd 
    255   1.9       cgd 	return (0);
    256   1.1        pk }
    257   1.1        pk 
    258  1.25  christos /*ARGSUSED*/
    259  1.25  christos int
    260  1.67  christos procfs_vget(struct mount *mp, ino_t ino,
    261  1.67  christos     struct vnode **vpp)
    262  1.16   mycroft {
    263  1.16   mycroft 	return (EOPNOTSUPP);
    264  1.16   mycroft }
    265  1.16   mycroft 
    266  1.25  christos void
    267  1.83    cegger procfs_init(void)
    268   1.1        pk {
    269  1.32      fvdl 	procfs_hashinit();
    270   1.1        pk }
    271   1.9       cgd 
    272  1.33  jdolecek void
    273  1.83    cegger procfs_reinit(void)
    274  1.40       chs {
    275  1.40       chs 	procfs_hashreinit();
    276  1.40       chs }
    277  1.40       chs 
    278  1.40       chs void
    279  1.83    cegger procfs_done(void)
    280  1.33  jdolecek {
    281  1.33  jdolecek 	procfs_hashdone();
    282  1.33  jdolecek }
    283  1.33  jdolecek 
    284  1.37  jdolecek extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
    285  1.27   thorpej 
    286  1.37  jdolecek const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
    287  1.27   thorpej 	&procfs_vnodeop_opv_desc,
    288  1.27   thorpej 	NULL,
    289  1.27   thorpej };
    290  1.27   thorpej 
    291   1.9       cgd struct vfsops procfs_vfsops = {
    292  1.90   hannken 	.vfs_name = MOUNT_PROCFS,
    293  1.90   hannken 	.vfs_min_mount_data = sizeof (struct procfs_args),
    294  1.90   hannken 	.vfs_mount = procfs_mount,
    295  1.90   hannken 	.vfs_start = procfs_start,
    296  1.90   hannken 	.vfs_unmount = procfs_unmount,
    297  1.90   hannken 	.vfs_root = procfs_root,
    298  1.90   hannken 	.vfs_quotactl = (void *)eopnotsupp,
    299  1.90   hannken 	.vfs_statvfs = procfs_statvfs,
    300  1.90   hannken 	.vfs_sync = procfs_sync,
    301  1.90   hannken 	.vfs_vget = procfs_vget,
    302  1.90   hannken 	.vfs_fhtovp = (void *)eopnotsupp,
    303  1.90   hannken 	.vfs_vptofh = (void *)eopnotsupp,
    304  1.90   hannken 	.vfs_init = procfs_init,
    305  1.90   hannken 	.vfs_reinit = procfs_reinit,
    306  1.90   hannken 	.vfs_done = procfs_done,
    307  1.90   hannken 	.vfs_snapshot = (void *)eopnotsupp,
    308  1.90   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    309  1.90   hannken 	.vfs_suspendctl = (void *)eopnotsupp,
    310  1.90   hannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
    311  1.90   hannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
    312  1.90   hannken 	.vfs_fsync = (void *)eopnotsupp,
    313  1.90   hannken 	.vfs_opv_descs = procfs_vnodeopv_descs
    314   1.9       cgd };
    315  1.79    rumble 
    316  1.79    rumble static int
    317  1.84      elad procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    318  1.84      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    319  1.84      elad {
    320  1.84      elad 	struct proc *p;
    321  1.84      elad 	struct pfsnode *pfs;
    322  1.84      elad 	enum kauth_process_req req;
    323  1.84      elad 	int result;
    324  1.84      elad 
    325  1.84      elad 	result = KAUTH_RESULT_DEFER;
    326  1.84      elad 	p = arg0;
    327  1.84      elad 	pfs = arg1;
    328  1.84      elad 	req = (enum kauth_process_req)(unsigned long)arg2;
    329  1.84      elad 
    330  1.84      elad 	if (action != KAUTH_PROCESS_PROCFS)
    331  1.84      elad 		return result;
    332  1.84      elad 
    333  1.84      elad 	/* Privileged; let secmodel handle that. */
    334  1.84      elad 	if (req == KAUTH_REQ_PROCESS_PROCFS_CTL)
    335  1.84      elad 		return result;
    336  1.84      elad 
    337  1.84      elad 	switch (pfs->pfs_type) {
    338  1.84      elad 	case PFSregs:
    339  1.84      elad 	case PFSfpregs:
    340  1.84      elad 	case PFSmem:
    341  1.84      elad 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    342  1.84      elad 		    ISSET(p->p_flag, PK_SUGID))
    343  1.84      elad 			break;
    344  1.84      elad 
    345  1.84      elad 		/*FALLTHROUGH*/
    346  1.84      elad 	default:
    347  1.84      elad 		result = KAUTH_RESULT_ALLOW;
    348  1.84      elad 		break;
    349  1.84      elad 	}
    350  1.84      elad 
    351  1.84      elad 	return result;
    352  1.84      elad }
    353  1.84      elad 
    354  1.84      elad 
    355  1.84      elad static int
    356  1.79    rumble procfs_modcmd(modcmd_t cmd, void *arg)
    357  1.79    rumble {
    358  1.81    rumble 	int error;
    359  1.79    rumble 
    360  1.79    rumble 	switch (cmd) {
    361  1.79    rumble 	case MODULE_CMD_INIT:
    362  1.81    rumble 		error = vfs_attach(&procfs_vfsops);
    363  1.81    rumble 		if (error != 0)
    364  1.81    rumble 			break;
    365  1.81    rumble 		sysctl_createv(&procfs_sysctl_log, 0, NULL, NULL,
    366  1.81    rumble 			       CTLFLAG_PERMANENT,
    367  1.81    rumble 			       CTLTYPE_NODE, "procfs",
    368  1.81    rumble 			       SYSCTL_DESCR("Process file system"),
    369  1.81    rumble 			       NULL, 0, NULL, 0,
    370  1.81    rumble 			       CTL_VFS, 12, CTL_EOL);
    371  1.81    rumble 		/*
    372  1.81    rumble 		 * XXX the "12" above could be dynamic, thereby eliminating
    373  1.81    rumble 		 * one more instance of the "number to vfs" mapping problem,
    374  1.81    rumble 		 * but "12" is the order as taken from sys/mount.h
    375  1.81    rumble 		 */
    376  1.84      elad 
    377  1.84      elad 		procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    378  1.84      elad 		    procfs_listener_cb, NULL);
    379  1.84      elad 
    380  1.81    rumble 		break;
    381  1.79    rumble 	case MODULE_CMD_FINI:
    382  1.81    rumble 		error = vfs_detach(&procfs_vfsops);
    383  1.81    rumble 		if (error != 0)
    384  1.81    rumble 			break;
    385  1.81    rumble 		sysctl_teardown(&procfs_sysctl_log);
    386  1.84      elad 		kauth_unlisten_scope(procfs_listener);
    387  1.81    rumble 		break;
    388  1.79    rumble 	default:
    389  1.81    rumble 		error = ENOTTY;
    390  1.81    rumble 		break;
    391  1.79    rumble 	}
    392  1.81    rumble 
    393  1.81    rumble 	return (error);
    394  1.79    rumble }
    395