xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision 93e779a26c651610ac6e7986d67ecc9ed2cadcbf)
1 /*-
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2000
5  *	Poul-Henning Kamp.  All rights reserved.
6  *
7  * This code is derived from software donated 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
32  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
33  *
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/proc.h>
44 #include <sys/sx.h>
45 #include <sys/vnode.h>
46 #include <sys/limits.h>
47 #include <sys/jail.h>
48 
49 #include <fs/devfs/devfs.h>
50 
51 static struct unrhdr	*devfs_unr;
52 
53 MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data");
54 
55 static vfs_mount_t	devfs_mount;
56 static vfs_unmount_t	devfs_unmount;
57 static vfs_root_t	devfs_root;
58 static vfs_statfs_t	devfs_statfs;
59 
60 static const char *devfs_opts[] = {
61 	"from", "export", "ruleset", NULL
62 };
63 
64 /*
65  * Mount the filesystem
66  */
67 static int
68 devfs_mount(struct mount *mp)
69 {
70 	int error;
71 	struct devfs_mount *fmp;
72 	struct vnode *rvp;
73 	struct thread *td = curthread;
74 	int injail, rsnum;
75 
76 	if (devfs_unr == NULL)
77 		devfs_unr = new_unrhdr(0, INT_MAX, NULL);
78 
79 	error = 0;
80 
81 	if (mp->mnt_flag & MNT_ROOTFS)
82 		return (EOPNOTSUPP);
83 
84 	if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_DEVFS))
85 		return (EPERM);
86 
87 	rsnum = 0;
88 	injail = jailed(td->td_ucred);
89 
90 	if (mp->mnt_optnew != NULL) {
91 		if (vfs_filteropt(mp->mnt_optnew, devfs_opts))
92 			return (EINVAL);
93 
94 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
95 			return (EOPNOTSUPP);
96 
97 		if (vfs_getopt(mp->mnt_optnew, "ruleset", NULL, NULL) == 0 &&
98 		    (vfs_scanopt(mp->mnt_optnew, "ruleset", "%d",
99 		    &rsnum) != 1 || rsnum < 0 || rsnum > 65535)) {
100 			vfs_mount_error(mp, "%s",
101 			    "invalid ruleset specification");
102 			return (EINVAL);
103 		}
104 
105 		if (injail && rsnum != 0 &&
106 		    rsnum != td->td_ucred->cr_prison->pr_devfs_rsnum)
107 			return (EPERM);
108 	}
109 
110 	/* jails enforce their ruleset */
111 	if (injail)
112 		rsnum = td->td_ucred->cr_prison->pr_devfs_rsnum;
113 
114 	if (mp->mnt_flag & MNT_UPDATE) {
115 		if (rsnum != 0) {
116 			fmp = mp->mnt_data;
117 			if (fmp != NULL) {
118 				sx_xlock(&fmp->dm_lock);
119 				devfs_ruleset_set((devfs_rsnum)rsnum, fmp);
120 				devfs_ruleset_apply(fmp);
121 				sx_xunlock(&fmp->dm_lock);
122 			}
123 		}
124 		return (0);
125 	}
126 
127 	fmp = malloc(sizeof *fmp, M_DEVFS, M_WAITOK | M_ZERO);
128 	fmp->dm_idx = alloc_unr(devfs_unr);
129 	sx_init(&fmp->dm_lock, "devfsmount");
130 	fmp->dm_holdcnt = 1;
131 
132 	MNT_ILOCK(mp);
133 	mp->mnt_flag |= MNT_LOCAL;
134 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
135 #ifdef MAC
136 	mp->mnt_flag |= MNT_MULTILABEL;
137 #endif
138 	MNT_IUNLOCK(mp);
139 	fmp->dm_mount = mp;
140 	mp->mnt_data = (void *) fmp;
141 	vfs_getnewfsid(mp);
142 
143 	fmp->dm_rootdir = devfs_vmkdir(fmp, NULL, 0, NULL, DEVFS_ROOTINO);
144 
145 	error = devfs_root(mp, LK_EXCLUSIVE, &rvp);
146 	if (error) {
147 		sx_destroy(&fmp->dm_lock);
148 		free_unr(devfs_unr, fmp->dm_idx);
149 		free(fmp, M_DEVFS);
150 		return (error);
151 	}
152 
153 	if (rsnum != 0) {
154 		sx_xlock(&fmp->dm_lock);
155 		devfs_ruleset_set((devfs_rsnum)rsnum, fmp);
156 		sx_xunlock(&fmp->dm_lock);
157 	}
158 
159 	VOP_UNLOCK(rvp, 0);
160 
161 	vfs_mountedfrom(mp, "devfs");
162 
163 	return (0);
164 }
165 
166 void
167 devfs_unmount_final(struct devfs_mount *fmp)
168 {
169 	sx_destroy(&fmp->dm_lock);
170 	free(fmp, M_DEVFS);
171 }
172 
173 static int
174 devfs_unmount(struct mount *mp, int mntflags)
175 {
176 	int error;
177 	int flags = 0;
178 	struct devfs_mount *fmp;
179 	int hold;
180 	u_int idx;
181 
182 	fmp = VFSTODEVFS(mp);
183 	KASSERT(fmp->dm_mount != NULL,
184 		("devfs_unmount unmounted devfs_mount"));
185 	if (mntflags & MNT_FORCE)
186 		flags |= FORCECLOSE;
187 	/* There is 1 extra root vnode reference from devfs_mount(). */
188 	error = vflush(mp, 1, flags, curthread);
189 	if (error)
190 		return (error);
191 	sx_xlock(&fmp->dm_lock);
192 	devfs_cleanup(fmp);
193 	devfs_rules_cleanup(fmp);
194 	fmp->dm_mount = NULL;
195 	hold = --fmp->dm_holdcnt;
196 	mp->mnt_data = NULL;
197 	idx = fmp->dm_idx;
198 	sx_xunlock(&fmp->dm_lock);
199 	free_unr(devfs_unr, idx);
200 	if (hold == 0)
201 		devfs_unmount_final(fmp);
202 	return 0;
203 }
204 
205 /* Return locked reference to root.  */
206 
207 static int
208 devfs_root(struct mount *mp, int flags, struct vnode **vpp)
209 {
210 	int error;
211 	struct vnode *vp;
212 	struct devfs_mount *dmp;
213 
214 	dmp = VFSTODEVFS(mp);
215 	sx_xlock(&dmp->dm_lock);
216 	error = devfs_allocv(dmp->dm_rootdir, mp, LK_EXCLUSIVE, &vp);
217 	if (error)
218 		return (error);
219 	vp->v_vflag |= VV_ROOT;
220 	*vpp = vp;
221 	return (0);
222 }
223 
224 static int
225 devfs_statfs(struct mount *mp, struct statfs *sbp)
226 {
227 
228 	sbp->f_flags = 0;
229 	sbp->f_bsize = DEV_BSIZE;
230 	sbp->f_iosize = DEV_BSIZE;
231 	sbp->f_blocks = 2;		/* 1K to keep df happy */
232 	sbp->f_bfree = 0;
233 	sbp->f_bavail = 0;
234 	sbp->f_files = 0;
235 	sbp->f_ffree = 0;
236 	return (0);
237 }
238 
239 static struct vfsops devfs_vfsops = {
240 	.vfs_mount =		devfs_mount,
241 	.vfs_root =		devfs_root,
242 	.vfs_statfs =		devfs_statfs,
243 	.vfs_unmount =		devfs_unmount,
244 };
245 
246 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC | VFCF_JAIL);
247