xref: /dflybsd-src/sys/vfs/devfs/devfs_vfsops.c (revision 51a529db74b2b342d44ffd3feab524929341896f)
121864bc5SMatthew Dillon /*
29f889dc4SMatthew Dillon  * (MPSAFE)
39f889dc4SMatthew Dillon  *
421864bc5SMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
521864bc5SMatthew Dillon  *
621864bc5SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
721864bc5SMatthew Dillon  * by Alex Hornung <ahornung@gmail.com>
821864bc5SMatthew Dillon  *
921864bc5SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
1021864bc5SMatthew Dillon  * modification, are permitted provided that the following conditions
1121864bc5SMatthew Dillon  * are met:
1221864bc5SMatthew Dillon  *
1321864bc5SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
1421864bc5SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1521864bc5SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
1621864bc5SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
1721864bc5SMatthew Dillon  *    the documentation and/or other materials provided with the
1821864bc5SMatthew Dillon  *    distribution.
1921864bc5SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
2021864bc5SMatthew Dillon  *    contributors may be used to endorse or promote products derived
2121864bc5SMatthew Dillon  *    from this software without specific, prior written permission.
2221864bc5SMatthew Dillon  *
2321864bc5SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2421864bc5SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2521864bc5SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2621864bc5SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2721864bc5SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2821864bc5SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2921864bc5SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3021864bc5SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
3121864bc5SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3221864bc5SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3321864bc5SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3421864bc5SMatthew Dillon  * SUCH DAMAGE.
3521864bc5SMatthew Dillon  */
3621864bc5SMatthew Dillon #include <sys/param.h>
3721864bc5SMatthew Dillon #include <sys/systm.h>
3821864bc5SMatthew Dillon #include <sys/kernel.h>
3921864bc5SMatthew Dillon #include <sys/mount.h>
4075779c3cSAlex Hornung #include <sys/namecache.h>
4121864bc5SMatthew Dillon #include <sys/vnode.h>
4221864bc5SMatthew Dillon #include <sys/jail.h>
43fa7e6f37SAlex Hornung #include <sys/lock.h>
442c1e28ddSAlex Hornung #include <sys/devfs.h>
4521864bc5SMatthew Dillon 
4621864bc5SMatthew Dillon MALLOC_DECLARE(M_DEVFS);
4721864bc5SMatthew Dillon 
4821864bc5SMatthew Dillon extern struct vop_ops devfs_vnode_norm_vops;
4921864bc5SMatthew Dillon extern struct vop_ops devfs_vnode_dev_vops;
5021864bc5SMatthew Dillon extern struct lock 	devfs_lock;
5121864bc5SMatthew Dillon 
529f889dc4SMatthew Dillon static int	devfs_vfs_mount (struct mount *mp, char *path, caddr_t data,
5321864bc5SMatthew Dillon 				  struct ucred *cred);
549f889dc4SMatthew Dillon static int	devfs_vfs_statfs (struct mount *mp, struct statfs *sbp,
5521864bc5SMatthew Dillon 				struct ucred *cred);
569f889dc4SMatthew Dillon static int	devfs_vfs_unmount (struct mount *mp, int mntflags);
579f889dc4SMatthew Dillon int		devfs_vfs_root(struct mount *mp, struct vnode **vpp);
589f889dc4SMatthew Dillon static int	devfs_vfs_vget(struct mount *mp, struct vnode *dvp,
59fa7e6f37SAlex Hornung 				ino_t ino, struct vnode **vpp);
609f889dc4SMatthew Dillon static int	devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
61fa7e6f37SAlex Hornung 				struct fid *fhp, struct vnode **vpp);
629f889dc4SMatthew Dillon static int	devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp);
63fa7e6f37SAlex Hornung 
6421864bc5SMatthew Dillon 
6521864bc5SMatthew Dillon /*
6621864bc5SMatthew Dillon  * VFS Operations.
6721864bc5SMatthew Dillon  *
6821864bc5SMatthew Dillon  * mount system call
6921864bc5SMatthew Dillon  */
7021864bc5SMatthew Dillon static int
719f889dc4SMatthew Dillon devfs_vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
7221864bc5SMatthew Dillon {
739cf39e57SAlex Hornung 	struct devfs_mount_info info;
74ca8d7677SMatthew Dillon 	struct devfs_mnt_data *mnt;
7521864bc5SMatthew Dillon 	size_t size;
769cf39e57SAlex Hornung 	int error;
7721864bc5SMatthew Dillon 
7821864bc5SMatthew Dillon 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n");
7921864bc5SMatthew Dillon 
8021864bc5SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE)
8121864bc5SMatthew Dillon 		return (EOPNOTSUPP);
8221864bc5SMatthew Dillon 
839cf39e57SAlex Hornung 	if (data == NULL) {
849cf39e57SAlex Hornung 		bzero(&info, sizeof(info));
859cf39e57SAlex Hornung 	} else {
869cf39e57SAlex Hornung 		if ((error = copyin(data, &info, sizeof(info))) != 0)
879cf39e57SAlex Hornung 			return (error);
889cf39e57SAlex Hornung 	}
8921864bc5SMatthew Dillon 
9021864bc5SMatthew Dillon 	mp->mnt_flag |= MNT_LOCAL;
919f889dc4SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_NOSTKMNT | MNTK_ALL_MPSAFE;
92*51a529dbSMatthew Dillon 	mp->mnt_kern_flag |= MNTK_QUICKHALT;   /* no teardown needed on halt */
93ca8d7677SMatthew Dillon 	mp->mnt_data = NULL;
9421864bc5SMatthew Dillon 	vfs_getnewfsid(mp);
9521864bc5SMatthew Dillon 
9621864bc5SMatthew Dillon 	size = sizeof("devfs") - 1;
9721864bc5SMatthew Dillon 	bcopy("devfs", mp->mnt_stat.f_mntfromname, size);
9821864bc5SMatthew Dillon 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
9981c25373SAlex Hornung 	copyinstr(path, mp->mnt_stat.f_mntonname,
10081c25373SAlex Hornung 	    sizeof(mp->mnt_stat.f_mntonname) -1, &size);
1019f889dc4SMatthew Dillon 	devfs_vfs_statfs(mp, &mp->mnt_stat, cred);
10221864bc5SMatthew Dillon 
103bc185c5aSAlex Hornung 	/*
104bc185c5aSAlex Hornung 	 * XXX: save other mount info passed from userland or so.
105bc185c5aSAlex Hornung 	 */
106ca8d7677SMatthew Dillon 	mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO);
10721864bc5SMatthew Dillon 
10821864bc5SMatthew Dillon 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
109ca8d7677SMatthew Dillon 	mp->mnt_data = (qaddr_t)mnt;
1109cf39e57SAlex Hornung 
1119cf39e57SAlex Hornung 	if (info.flags & DEVFS_MNT_JAIL)
1129cf39e57SAlex Hornung 		mnt->jailed = 1;
1139cf39e57SAlex Hornung 	else
114ca8d7677SMatthew Dillon 		mnt->jailed = jailed(cred);
1159cf39e57SAlex Hornung 
116ca8d7677SMatthew Dillon 	mnt->leak_count = 0;
117aee6fa68SAlex Hornung 	mnt->file_count = 0;
118ca8d7677SMatthew Dillon 	mnt->mp = mp;
119ca8d7677SMatthew Dillon 	TAILQ_INIT(&mnt->orphan_list);
1208e78a293SSascha Wildner 	mnt->root_node = devfs_allocp(Nroot, "", NULL, mp, NULL);
121ca8d7677SMatthew Dillon 	KKASSERT(mnt->root_node);
12221864bc5SMatthew Dillon 	lockmgr(&devfs_lock, LK_RELEASE);
12321864bc5SMatthew Dillon 
12421864bc5SMatthew Dillon 	vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops);
12521864bc5SMatthew Dillon 	vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops);
12621864bc5SMatthew Dillon 
12721864bc5SMatthew Dillon 	devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n");
128ca8d7677SMatthew Dillon 	devfs_mount_add(mnt);
12921864bc5SMatthew Dillon 
13021864bc5SMatthew Dillon 	return (0);
13121864bc5SMatthew Dillon }
13221864bc5SMatthew Dillon 
13321864bc5SMatthew Dillon /*
13421864bc5SMatthew Dillon  * unmount system call
13521864bc5SMatthew Dillon  */
13621864bc5SMatthew Dillon static int
1379f889dc4SMatthew Dillon devfs_vfs_unmount(struct mount *mp, int mntflags)
13821864bc5SMatthew Dillon {
13921864bc5SMatthew Dillon 	int error = 0;
14021864bc5SMatthew Dillon 	int flags = 0;
14121864bc5SMatthew Dillon 
14221864bc5SMatthew Dillon 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n");
14321864bc5SMatthew Dillon 
14421864bc5SMatthew Dillon 	if (mntflags & MNT_FORCE)
14521864bc5SMatthew Dillon 		flags |= FORCECLOSE;
14621864bc5SMatthew Dillon 
14721864bc5SMatthew Dillon 	error = vflush(mp, 0, flags);
14821864bc5SMatthew Dillon 
14921864bc5SMatthew Dillon 	if (error)
15021864bc5SMatthew Dillon 		return (error);
1519f889dc4SMatthew Dillon 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
15289bf2026SMatthew Dillon 	devfs_tracer_orphan_count(mp, 1);
1539f889dc4SMatthew Dillon 	lockmgr(&devfs_lock, LK_RELEASE);
15421864bc5SMatthew Dillon 	devfs_mount_del(DEVFS_MNTDATA(mp));
15521864bc5SMatthew Dillon 	kfree(mp->mnt_data, M_DEVFS);
156ca8d7677SMatthew Dillon 	mp->mnt_data = NULL;
15721864bc5SMatthew Dillon 
15821864bc5SMatthew Dillon 	return (0);
15921864bc5SMatthew Dillon }
16021864bc5SMatthew Dillon 
16121864bc5SMatthew Dillon /*
16221864bc5SMatthew Dillon  * Sets *vpp to the root procfs vnode, referenced and exclusively locked
16321864bc5SMatthew Dillon  */
16421864bc5SMatthew Dillon int
1659f889dc4SMatthew Dillon devfs_vfs_root(struct mount *mp, struct vnode **vpp)
16621864bc5SMatthew Dillon {
16721864bc5SMatthew Dillon 	int ret;
1689f889dc4SMatthew Dillon 
16921864bc5SMatthew Dillon 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n");
17021864bc5SMatthew Dillon 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
17121864bc5SMatthew Dillon 	ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node);
17221864bc5SMatthew Dillon 	lockmgr(&devfs_lock, LK_RELEASE);
1739f889dc4SMatthew Dillon 
17421864bc5SMatthew Dillon 	return ret;
17521864bc5SMatthew Dillon }
17621864bc5SMatthew Dillon 
17721864bc5SMatthew Dillon /*
17821864bc5SMatthew Dillon  * Get file system statistics.
17921864bc5SMatthew Dillon  */
18021864bc5SMatthew Dillon static int
1819f889dc4SMatthew Dillon devfs_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
18221864bc5SMatthew Dillon {
18321864bc5SMatthew Dillon 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n");
18421864bc5SMatthew Dillon 	sbp->f_bsize = DEV_BSIZE;
18521864bc5SMatthew Dillon 	sbp->f_iosize = DEV_BSIZE;
18621864bc5SMatthew Dillon 	sbp->f_blocks = 2;	/* avoid divide by zero in some df's */
18721864bc5SMatthew Dillon 	sbp->f_bfree = 0;
18821864bc5SMatthew Dillon 	sbp->f_bavail = 0;
189aee6fa68SAlex Hornung 	sbp->f_files = (DEVFS_MNTDATA(mp))?(DEVFS_MNTDATA(mp)->file_count):0;
19021864bc5SMatthew Dillon 	sbp->f_ffree = 0;
19121864bc5SMatthew Dillon 
19221864bc5SMatthew Dillon 	if (sbp != &mp->mnt_stat) {
19321864bc5SMatthew Dillon 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
19421864bc5SMatthew Dillon 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
19521864bc5SMatthew Dillon 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
19621864bc5SMatthew Dillon 	}
19721864bc5SMatthew Dillon 
19821864bc5SMatthew Dillon 	return (0);
19921864bc5SMatthew Dillon }
20021864bc5SMatthew Dillon 
201fa7e6f37SAlex Hornung static int
2029f889dc4SMatthew Dillon devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
203fa7e6f37SAlex Hornung 		 struct fid *fhp, struct vnode **vpp)
204fa7e6f37SAlex Hornung {
205fa7e6f37SAlex Hornung 	struct vnode		*vp;
206fa7e6f37SAlex Hornung 	struct devfs_fid	*dfhp;
207fa7e6f37SAlex Hornung 
208fa7e6f37SAlex Hornung 	dfhp = (struct devfs_fid *)fhp;
209fa7e6f37SAlex Hornung 
210fa7e6f37SAlex Hornung 	if (dfhp->fid_gen != boottime.tv_sec)
211fa7e6f37SAlex Hornung 		return EINVAL;
212fa7e6f37SAlex Hornung 
213fa7e6f37SAlex Hornung 	vp = devfs_inode_to_vnode(mp, dfhp->fid_ino);
214fa7e6f37SAlex Hornung 
215bc185c5aSAlex Hornung 	if (vp == NULL)
216fa7e6f37SAlex Hornung 		return ENOENT;
217fa7e6f37SAlex Hornung 
218bc185c5aSAlex Hornung 	*vpp = vp;
219fa7e6f37SAlex Hornung 	return 0;
220fa7e6f37SAlex Hornung }
221fa7e6f37SAlex Hornung 
222fa7e6f37SAlex Hornung /*
223fa7e6f37SAlex Hornung  * Vnode pointer to File handle
224fa7e6f37SAlex Hornung  */
225fa7e6f37SAlex Hornung static int
2269f889dc4SMatthew Dillon devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp)
227fa7e6f37SAlex Hornung {
228fa7e6f37SAlex Hornung 	struct devfs_node	*node;
229fa7e6f37SAlex Hornung 	struct devfs_fid	*dfhp;
230fa7e6f37SAlex Hornung 
231fa7e6f37SAlex Hornung 	if ((node = DEVFS_NODE(vp)) != NULL) {
232fa7e6f37SAlex Hornung 		dfhp = (struct devfs_fid *)fhp;
233fa7e6f37SAlex Hornung 		dfhp->fid_len = sizeof(struct devfs_fid);
234fa7e6f37SAlex Hornung 		dfhp->fid_ino = node->d_dir.d_ino;
235fa7e6f37SAlex Hornung 		dfhp->fid_gen = boottime.tv_sec;
236fa7e6f37SAlex Hornung 	} else {
237fa7e6f37SAlex Hornung 		return ENOENT;
238fa7e6f37SAlex Hornung 	}
239fa7e6f37SAlex Hornung 
240fa7e6f37SAlex Hornung 	return (0);
241fa7e6f37SAlex Hornung }
242fa7e6f37SAlex Hornung 
243fa7e6f37SAlex Hornung static int
2449f889dc4SMatthew Dillon devfs_vfs_vget(struct mount *mp, struct vnode *dvp,
2459f889dc4SMatthew Dillon 	       ino_t ino, struct vnode **vpp)
246fa7e6f37SAlex Hornung {
247fa7e6f37SAlex Hornung 	struct vnode *vp;
248fa7e6f37SAlex Hornung 	vp = devfs_inode_to_vnode(mp, ino);
249fa7e6f37SAlex Hornung 
250bc185c5aSAlex Hornung 	if (vp == NULL)
251fa7e6f37SAlex Hornung 		return ENOENT;
252fa7e6f37SAlex Hornung 
253bc185c5aSAlex Hornung 	*vpp = vp;
254fa7e6f37SAlex Hornung 	return 0;
255fa7e6f37SAlex Hornung }
256fa7e6f37SAlex Hornung 
25775779c3cSAlex Hornung static void
25875779c3cSAlex Hornung devfs_vfs_ncpgen_set(struct mount *mp, struct namecache *ncp)
25975779c3cSAlex Hornung {
26075779c3cSAlex Hornung 	ncp->nc_namecache_gen = mp->mnt_namecache_gen;
26175779c3cSAlex Hornung }
26275779c3cSAlex Hornung 
26375779c3cSAlex Hornung static int
26475779c3cSAlex Hornung devfs_vfs_ncpgen_test(struct mount *mp, struct namecache *ncp)
26575779c3cSAlex Hornung {
26675779c3cSAlex Hornung 	return (ncp->nc_namecache_gen != mp->mnt_namecache_gen);
26775779c3cSAlex Hornung }
268fa7e6f37SAlex Hornung 
26921864bc5SMatthew Dillon static struct vfsops devfs_vfsops = {
2709f889dc4SMatthew Dillon 	.vfs_mount 	= devfs_vfs_mount,
2719f889dc4SMatthew Dillon 	.vfs_unmount	= devfs_vfs_unmount,
2729f889dc4SMatthew Dillon 	.vfs_root 	= devfs_vfs_root,
2739f889dc4SMatthew Dillon 	.vfs_statfs	= devfs_vfs_statfs,
2749f889dc4SMatthew Dillon 	.vfs_vget	= devfs_vfs_vget,
2759f889dc4SMatthew Dillon 	.vfs_vptofh	= devfs_vfs_vptofh,
27675779c3cSAlex Hornung 	.vfs_fhtovp	= devfs_vfs_fhtovp,
27775779c3cSAlex Hornung 	.vfs_ncpgen_set	= devfs_vfs_ncpgen_set,
27875779c3cSAlex Hornung 	.vfs_ncpgen_test	= devfs_vfs_ncpgen_test
27921864bc5SMatthew Dillon };
28021864bc5SMatthew Dillon 
28187f62b1cSMatthew Dillon VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC | VFCF_MPSAFE);
28221864bc5SMatthew Dillon MODULE_VERSION(devfs, 1);
283