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