xref: /dflybsd-src/sys/vfs/devfs/devfs_vfsops.c (revision ca8d7677b7d28d7b85590e5ad8572db1f3d96bf3)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mount.h>
38 #include <sys/vnode.h>
39 #include <sys/jail.h>
40 #include <vfs/devfs/devfs.h>
41 
42 MALLOC_DECLARE(M_DEVFS);
43 
44 extern struct vop_ops devfs_vnode_norm_vops;
45 extern struct vop_ops devfs_vnode_dev_vops;
46 extern struct lock 	devfs_lock;
47 
48 static int	devfs_mount (struct mount *mp, char *path, caddr_t data,
49 				  struct ucred *cred);
50 static int	devfs_statfs (struct mount *mp, struct statfs *sbp,
51 				struct ucred *cred);
52 static int	devfs_unmount (struct mount *mp, int mntflags);
53 int			devfs_root(struct mount *mp, struct vnode **vpp);
54 
55 /*
56  * VFS Operations.
57  *
58  * mount system call
59  */
60 /* ARGSUSED */
61 static int
62 devfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
63 {
64 	struct devfs_mnt_data *mnt;
65 	size_t size;
66 
67 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n");
68 
69 	if (mp->mnt_flag & MNT_UPDATE)
70 		return (EOPNOTSUPP);
71 
72 
73 	mp->mnt_flag |= MNT_LOCAL;
74 	mp->mnt_kern_flag |= MNTK_NOSTKMNT;
75 	mp->mnt_data = NULL;
76 	vfs_getnewfsid(mp);
77 
78 	size = sizeof("devfs") - 1;
79 	bcopy("devfs", mp->mnt_stat.f_mntfromname, size);
80 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
81 	devfs_statfs(mp, &mp->mnt_stat, cred);
82 
83 	//XXX: save other mount info passed from userland or so.
84 	mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO);
85 
86 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
87 	mp->mnt_data = (qaddr_t)mnt;
88 	mnt->jailed = jailed(cred);
89 	mnt->leak_count = 0;
90 	mnt->mp = mp;
91 	TAILQ_INIT(&mnt->orphan_list);
92 	mnt->mntonnamelen = strlen(mp->mnt_stat.f_mntonname);
93 	mnt->root_node = devfs_allocp(Proot, "", NULL, mp, NULL);
94 	KKASSERT(mnt->root_node);
95 	lockmgr(&devfs_lock, LK_RELEASE);
96 
97 	vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops);
98 	vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops);
99 
100 	devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n");
101 	devfs_mount_add(mnt);
102 
103 	return (0);
104 }
105 
106 /*
107  * unmount system call
108  */
109 static int
110 devfs_unmount(struct mount *mp, int mntflags)
111 {
112 	int error = 0;
113 	int flags = 0;
114 
115 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n");
116 
117 	if (mntflags & MNT_FORCE)
118 		flags |= FORCECLOSE;
119 
120 	error = vflush(mp, 0, flags);
121 
122 	if (error)
123 		return (error);
124 
125 	devfs_debug(DEVFS_DEBUG_SHOW,
126 		    "There were %d devfs_node orphans left\n",
127 		    devfs_tracer_orphan_count(mp, 1));
128 	devfs_debug(DEVFS_DEBUG_SHOW,
129 		    "There are %d devfs_node orphans left\n",
130 		    devfs_tracer_orphan_count(mp, 0));
131 	devfs_mount_del(DEVFS_MNTDATA(mp));
132 	kfree(mp->mnt_data, M_DEVFS);
133 	mp->mnt_data = NULL;
134 
135 	return (0);
136 }
137 
138 /*
139  * Sets *vpp to the root procfs vnode, referenced and exclusively locked
140  */
141 int
142 devfs_root(struct mount *mp, struct vnode **vpp)
143 {
144 	int ret;
145 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n");
146 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
147 	ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node);
148 	lockmgr(&devfs_lock, LK_RELEASE);
149 	return ret;
150 }
151 
152 /*
153  * Get file system statistics.
154  */
155 static int
156 devfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
157 {
158 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n");
159 	sbp->f_bsize = DEV_BSIZE;
160 	sbp->f_iosize = DEV_BSIZE;
161 	sbp->f_blocks = 2;	/* avoid divide by zero in some df's */
162 	sbp->f_bfree = 0;
163 	sbp->f_bavail = 0;
164 	sbp->f_files = 0;
165 	sbp->f_ffree = 0;
166 
167 	if (sbp != &mp->mnt_stat) {
168 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
169 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
170 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
171 	}
172 
173 	return (0);
174 }
175 
176 static struct vfsops devfs_vfsops = {
177 	.vfs_mount =    	devfs_mount,
178 	.vfs_unmount =    	devfs_unmount,
179 	.vfs_root =    		devfs_root,
180 	.vfs_statfs =    	devfs_statfs,
181 	.vfs_sync =    		vfs_stdsync
182 };
183 
184 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
185 MODULE_VERSION(devfs, 1);
186