xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision 4f8f43b06ed07e96a250855488cc531799d5b78f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
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 	rsnum = 0;
85 	injail = jailed(td->td_ucred);
86 
87 	if (mp->mnt_optnew != NULL) {
88 		if (vfs_filteropt(mp->mnt_optnew, devfs_opts))
89 			return (EINVAL);
90 
91 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
92 			return (EOPNOTSUPP);
93 
94 		if (vfs_getopt(mp->mnt_optnew, "ruleset", NULL, NULL) == 0 &&
95 		    (vfs_scanopt(mp->mnt_optnew, "ruleset", "%d",
96 		    &rsnum) != 1 || rsnum < 0 || rsnum > 65535)) {
97 			vfs_mount_error(mp, "%s",
98 			    "invalid ruleset specification");
99 			return (EINVAL);
100 		}
101 
102 		if (injail && rsnum != 0 &&
103 		    rsnum != td->td_ucred->cr_prison->pr_devfs_rsnum)
104 			return (EPERM);
105 	}
106 
107 	/* jails enforce their ruleset */
108 	if (injail)
109 		rsnum = td->td_ucred->cr_prison->pr_devfs_rsnum;
110 
111 	if (mp->mnt_flag & MNT_UPDATE) {
112 		if (rsnum != 0) {
113 			fmp = mp->mnt_data;
114 			if (fmp != NULL) {
115 				sx_xlock(&fmp->dm_lock);
116 				devfs_ruleset_set((devfs_rsnum)rsnum, fmp);
117 				devfs_ruleset_apply(fmp);
118 				sx_xunlock(&fmp->dm_lock);
119 			}
120 		}
121 		return (0);
122 	}
123 
124 	fmp = malloc(sizeof *fmp, M_DEVFS, M_WAITOK | M_ZERO);
125 	fmp->dm_idx = alloc_unr(devfs_unr);
126 	sx_init(&fmp->dm_lock, "devfsmount");
127 	fmp->dm_holdcnt = 1;
128 
129 	MNT_ILOCK(mp);
130 	mp->mnt_flag |= MNT_LOCAL;
131 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
132 	    MNTK_NOMSYNC;
133 #ifdef MAC
134 	mp->mnt_flag |= MNT_MULTILABEL;
135 #endif
136 	MNT_IUNLOCK(mp);
137 	fmp->dm_mount = mp;
138 	mp->mnt_data = (void *) fmp;
139 	vfs_getnewfsid(mp);
140 
141 	fmp->dm_rootdir = devfs_vmkdir(fmp, NULL, 0, NULL, DEVFS_ROOTINO);
142 
143 	error = devfs_root(mp, LK_EXCLUSIVE, &rvp);
144 	if (error) {
145 		sx_destroy(&fmp->dm_lock);
146 		free_unr(devfs_unr, fmp->dm_idx);
147 		free(fmp, M_DEVFS);
148 		return (error);
149 	}
150 
151 	if (rsnum != 0) {
152 		sx_xlock(&fmp->dm_lock);
153 		devfs_ruleset_set((devfs_rsnum)rsnum, fmp);
154 		sx_xunlock(&fmp->dm_lock);
155 	}
156 
157 	VOP_UNLOCK(rvp);
158 	vfs_cache_root_set(mp, rvp);
159 
160 	vfs_mountedfrom(mp, "devfs");
161 
162 	return (0);
163 }
164 
165 void
166 devfs_unmount_final(struct devfs_mount *fmp)
167 {
168 	sx_destroy(&fmp->dm_lock);
169 	free(fmp, M_DEVFS);
170 }
171 
172 static int
173 devfs_unmount(struct mount *mp, int mntflags)
174 {
175 	int error;
176 	int flags = 0;
177 	struct devfs_mount *fmp;
178 	int hold;
179 	u_int idx;
180 
181 	fmp = VFSTODEVFS(mp);
182 	KASSERT(fmp->dm_mount != NULL,
183 		("devfs_unmount unmounted devfs_mount"));
184 	if (mntflags & MNT_FORCE)
185 		flags |= FORCECLOSE;
186 	/* There is 1 extra root vnode reference from devfs_mount(). */
187 	error = vflush(mp, 1, flags, curthread);
188 	if (error)
189 		return (error);
190 	sx_xlock(&fmp->dm_lock);
191 	devfs_cleanup(fmp);
192 	devfs_rules_cleanup(fmp);
193 	fmp->dm_mount = NULL;
194 	hold = --fmp->dm_holdcnt;
195 	mp->mnt_data = NULL;
196 	idx = fmp->dm_idx;
197 	sx_xunlock(&fmp->dm_lock);
198 	free_unr(devfs_unr, idx);
199 	if (hold == 0)
200 		devfs_unmount_final(fmp);
201 	return 0;
202 }
203 
204 /* Return locked reference to root.  */
205 
206 static int
207 devfs_root(struct mount *mp, int flags, struct vnode **vpp)
208 {
209 	int error;
210 	struct vnode *vp;
211 	struct devfs_mount *dmp;
212 
213 	dmp = VFSTODEVFS(mp);
214 	sx_xlock(&dmp->dm_lock);
215 	error = devfs_allocv(dmp->dm_rootdir, mp, LK_EXCLUSIVE, &vp);
216 	if (error)
217 		return (error);
218 	vp->v_vflag |= VV_ROOT;
219 	*vpp = vp;
220 	return (0);
221 }
222 
223 static int
224 devfs_statfs(struct mount *mp, struct statfs *sbp)
225 {
226 
227 	sbp->f_flags = mp->mnt_flag & MNT_IGNORE;
228 	sbp->f_bsize = DEV_BSIZE;
229 	sbp->f_iosize = DEV_BSIZE;
230 	sbp->f_blocks = 2;		/* 1K to keep df happy */
231 	sbp->f_bfree = 2;
232 	sbp->f_bavail = 2;
233 	sbp->f_files = 0;
234 	sbp->f_ffree = 0;
235 	return (0);
236 }
237 
238 static struct vfsops devfs_vfsops = {
239 	.vfs_mount =		devfs_mount,
240 	.vfs_root =		vfs_cache_root,
241 	.vfs_cachedroot =	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