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 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mount.h> 41 #include <sys/proc.h> 42 #include <sys/sx.h> 43 #include <sys/vnode.h> 44 #include <sys/limits.h> 45 #include <sys/jail.h> 46 47 #include <fs/devfs/devfs.h> 48 49 static struct unrhdr *devfs_unr; 50 51 MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data"); 52 53 static vfs_mount_t devfs_mount; 54 static vfs_unmount_t devfs_unmount; 55 static vfs_root_t devfs_root; 56 static vfs_statfs_t devfs_statfs; 57 58 static const char *devfs_opts[] = { 59 "from", "export", "ruleset", NULL 60 }; 61 62 /* 63 * Mount the filesystem 64 */ 65 static int 66 devfs_mount(struct mount *mp) 67 { 68 int error; 69 struct devfs_mount *fmp; 70 struct vnode *rvp; 71 struct thread *td = curthread; 72 int injail, rsnum; 73 74 if (devfs_unr == NULL) 75 devfs_unr = new_unrhdr(0, INT_MAX, NULL); 76 77 error = 0; 78 79 if (mp->mnt_flag & MNT_ROOTFS) 80 return (EOPNOTSUPP); 81 82 rsnum = 0; 83 injail = jailed(td->td_ucred); 84 85 if (mp->mnt_optnew != NULL) { 86 if (vfs_filteropt(mp->mnt_optnew, devfs_opts)) 87 return (EINVAL); 88 89 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) 90 return (EOPNOTSUPP); 91 92 if (vfs_getopt(mp->mnt_optnew, "ruleset", NULL, NULL) == 0 && 93 (vfs_scanopt(mp->mnt_optnew, "ruleset", "%d", 94 &rsnum) != 1 || rsnum < 0 || rsnum > 65535)) { 95 vfs_mount_error(mp, "%s", 96 "invalid ruleset specification"); 97 return (EINVAL); 98 } 99 100 if (injail && rsnum != 0 && 101 rsnum != td->td_ucred->cr_prison->pr_devfs_rsnum) 102 return (EPERM); 103 } 104 105 /* jails enforce their ruleset */ 106 if (injail) 107 rsnum = td->td_ucred->cr_prison->pr_devfs_rsnum; 108 109 if (mp->mnt_flag & MNT_UPDATE) { 110 if (rsnum != 0) { 111 fmp = mp->mnt_data; 112 if (fmp != NULL) { 113 sx_xlock(&fmp->dm_lock); 114 devfs_ruleset_set((devfs_rsnum)rsnum, fmp); 115 devfs_ruleset_apply(fmp); 116 sx_xunlock(&fmp->dm_lock); 117 } 118 } 119 return (0); 120 } 121 122 fmp = malloc(sizeof *fmp, M_DEVFS, M_WAITOK | M_ZERO); 123 fmp->dm_idx = alloc_unr(devfs_unr); 124 sx_init(&fmp->dm_lock, "devfsmount"); 125 fmp->dm_holdcnt = 1; 126 127 MNT_ILOCK(mp); 128 mp->mnt_flag |= MNT_LOCAL; 129 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED | 130 MNTK_NOMSYNC; 131 #ifdef MAC 132 mp->mnt_flag |= MNT_MULTILABEL; 133 #endif 134 MNT_IUNLOCK(mp); 135 fmp->dm_mount = mp; 136 mp->mnt_data = (void *) fmp; 137 vfs_getnewfsid(mp); 138 139 fmp->dm_rootdir = devfs_vmkdir(fmp, NULL, 0, NULL, DEVFS_ROOTINO); 140 141 error = devfs_root(mp, LK_EXCLUSIVE, &rvp); 142 if (error) { 143 sx_destroy(&fmp->dm_lock); 144 free_unr(devfs_unr, fmp->dm_idx); 145 free(fmp, M_DEVFS); 146 return (error); 147 } 148 149 if (rsnum != 0) { 150 sx_xlock(&fmp->dm_lock); 151 devfs_ruleset_set((devfs_rsnum)rsnum, fmp); 152 sx_xunlock(&fmp->dm_lock); 153 } 154 155 VOP_UNLOCK(rvp); 156 vfs_cache_root_set(mp, rvp); 157 158 vfs_mountedfrom(mp, "devfs"); 159 160 return (0); 161 } 162 163 void 164 devfs_unmount_final(struct devfs_mount *fmp) 165 { 166 sx_destroy(&fmp->dm_lock); 167 free(fmp, M_DEVFS); 168 } 169 170 static int 171 devfs_unmount(struct mount *mp, int mntflags) 172 { 173 int error; 174 int flags = 0; 175 struct devfs_mount *fmp; 176 int hold; 177 u_int idx; 178 179 fmp = VFSTODEVFS(mp); 180 KASSERT(fmp->dm_mount != NULL, 181 ("devfs_unmount unmounted devfs_mount")); 182 if (mntflags & MNT_FORCE) 183 flags |= FORCECLOSE; 184 /* There is 1 extra root vnode reference from devfs_mount(). */ 185 error = vflush(mp, 1, flags, curthread); 186 if (error) 187 return (error); 188 sx_xlock(&fmp->dm_lock); 189 devfs_cleanup(fmp); 190 devfs_rules_cleanup(fmp); 191 fmp->dm_mount = NULL; 192 hold = --fmp->dm_holdcnt; 193 mp->mnt_data = NULL; 194 idx = fmp->dm_idx; 195 sx_xunlock(&fmp->dm_lock); 196 free_unr(devfs_unr, idx); 197 if (hold == 0) 198 devfs_unmount_final(fmp); 199 return 0; 200 } 201 202 /* Return locked reference to root. */ 203 204 static int 205 devfs_root(struct mount *mp, int flags, struct vnode **vpp) 206 { 207 int error; 208 struct vnode *vp; 209 struct devfs_mount *dmp; 210 211 dmp = VFSTODEVFS(mp); 212 sx_xlock(&dmp->dm_lock); 213 error = devfs_allocv(dmp->dm_rootdir, mp, LK_EXCLUSIVE, &vp); 214 if (error) 215 return (error); 216 vp->v_vflag |= VV_ROOT; 217 *vpp = vp; 218 return (0); 219 } 220 221 static int 222 devfs_statfs(struct mount *mp, struct statfs *sbp) 223 { 224 225 sbp->f_flags = mp->mnt_flag & MNT_IGNORE; 226 sbp->f_bsize = DEV_BSIZE; 227 sbp->f_iosize = DEV_BSIZE; 228 sbp->f_blocks = 2; /* 1K to keep df happy */ 229 sbp->f_bfree = 2; 230 sbp->f_bavail = 2; 231 sbp->f_files = 0; 232 sbp->f_ffree = 0; 233 return (0); 234 } 235 236 static struct vfsops devfs_vfsops = { 237 .vfs_mount = devfs_mount, 238 .vfs_root = vfs_cache_root, 239 .vfs_cachedroot = devfs_root, 240 .vfs_statfs = devfs_statfs, 241 .vfs_unmount = devfs_unmount, 242 }; 243 244 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC | VFCF_JAIL); 245