1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992, 1993, 1995 5 * The Regents of the University of California. 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. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Null Layer 37 * (See null_vnops.c for a description of what this does.) 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/fcntl.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mount.h> 47 #include <sys/namei.h> 48 #include <sys/proc.h> 49 #include <sys/sysctl.h> 50 #include <sys/vnode.h> 51 #include <sys/jail.h> 52 53 #include <fs/nullfs/null.h> 54 55 static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure"); 56 57 static vfs_fhtovp_t nullfs_fhtovp; 58 static vfs_mount_t nullfs_mount; 59 static vfs_quotactl_t nullfs_quotactl; 60 static vfs_root_t nullfs_root; 61 static vfs_sync_t nullfs_sync; 62 static vfs_statfs_t nullfs_statfs; 63 static vfs_unmount_t nullfs_unmount; 64 static vfs_vget_t nullfs_vget; 65 static vfs_extattrctl_t nullfs_extattrctl; 66 67 SYSCTL_NODE(_vfs, OID_AUTO, nullfs, CTLFLAG_RW, 0, "nullfs"); 68 69 static bool null_cache_vnodes = true; 70 SYSCTL_BOOL(_vfs_nullfs, OID_AUTO, cache_vnodes, CTLFLAG_RWTUN, 71 &null_cache_vnodes, 0, 72 "cache free nullfs vnodes"); 73 74 /* 75 * Mount null layer 76 */ 77 static int 78 nullfs_mount(struct mount *mp) 79 { 80 struct vnode *lowerrootvp; 81 struct vnode *nullm_rootvp; 82 struct null_mount *xmp; 83 struct null_node *nn; 84 struct nameidata nd, *ndp; 85 char *target; 86 int error, len; 87 bool isvnunlocked; 88 static const char cache_opt_name[] = "cache"; 89 static const char nocache_opt_name[] = "nocache"; 90 static const char unixbypass_opt_name[] = "unixbypass"; 91 static const char nounixbypass_opt_name[] = "nounixbypass"; 92 93 NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp); 94 95 if (mp->mnt_flag & MNT_ROOTFS) 96 return (EOPNOTSUPP); 97 98 /* 99 * Update is a no-op 100 */ 101 if (mp->mnt_flag & MNT_UPDATE) { 102 /* 103 * Only support update mounts for NFS export. 104 */ 105 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) 106 return (0); 107 else 108 return (EOPNOTSUPP); 109 } 110 111 /* 112 * Get argument 113 */ 114 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target, &len); 115 if (error != 0) 116 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len); 117 if (error || target[len - 1] != '\0') 118 return (EINVAL); 119 120 /* 121 * Unlock lower node to avoid possible deadlock. 122 */ 123 if (null_is_nullfs_vnode(mp->mnt_vnodecovered) && 124 VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) { 125 VOP_UNLOCK(mp->mnt_vnodecovered); 126 isvnunlocked = true; 127 } else { 128 isvnunlocked = false; 129 } 130 131 /* 132 * Find lower node 133 */ 134 ndp = &nd; 135 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target); 136 error = namei(ndp); 137 138 /* 139 * Re-lock vnode. 140 * XXXKIB This is deadlock-prone as well. 141 */ 142 if (isvnunlocked) 143 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY); 144 145 if (error) 146 return (error); 147 NDFREE_PNBUF(ndp); 148 149 /* 150 * Sanity check on lower vnode 151 */ 152 lowerrootvp = ndp->ni_vp; 153 154 /* 155 * Do not allow to mount a vnode over itself. 156 */ 157 if (mp->mnt_vnodecovered == lowerrootvp) { 158 vput(lowerrootvp); 159 return (EDEADLK); 160 } 161 162 /* 163 * Check multi null mount to avoid `lock against myself' panic. 164 */ 165 if (null_is_nullfs_vnode(mp->mnt_vnodecovered)) { 166 nn = VTONULL(mp->mnt_vnodecovered); 167 if (nn == NULL || lowerrootvp == nn->null_lowervp) { 168 NULLFSDEBUG("nullfs_mount: multi null mount?\n"); 169 vput(lowerrootvp); 170 return (EDEADLK); 171 } 172 } 173 174 /* 175 * Lower vnode must be the same type as the covered vnode - we 176 * don't allow mounting directories to files or vice versa. 177 */ 178 if ((lowerrootvp->v_type != VDIR && lowerrootvp->v_type != VREG && 179 lowerrootvp->v_type != VSOCK) || 180 lowerrootvp->v_type != mp->mnt_vnodecovered->v_type) { 181 NULLFSDEBUG("nullfs_mount: target must be same type as fspath"); 182 vput(lowerrootvp); 183 return (EINVAL); 184 } 185 186 xmp = malloc(sizeof(struct null_mount), M_NULLFSMNT, 187 M_WAITOK | M_ZERO); 188 189 /* 190 * Save pointer to underlying FS and the reference to the 191 * lower root vnode. 192 */ 193 xmp->nullm_vfs = vfs_register_upper_from_vp(lowerrootvp, mp, 194 &xmp->upper_node); 195 if (xmp->nullm_vfs == NULL) { 196 vput(lowerrootvp); 197 free(xmp, M_NULLFSMNT); 198 return (ENOENT); 199 } 200 vref(lowerrootvp); 201 xmp->nullm_lowerrootvp = lowerrootvp; 202 mp->mnt_data = xmp; 203 204 /* 205 * Make sure the node alias worked. 206 */ 207 error = null_nodeget(mp, lowerrootvp, &nullm_rootvp); 208 if (error != 0) { 209 vfs_unregister_upper(xmp->nullm_vfs, &xmp->upper_node); 210 vrele(lowerrootvp); 211 free(xmp, M_NULLFSMNT); 212 return (error); 213 } 214 215 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) { 216 MNT_ILOCK(mp); 217 mp->mnt_flag |= MNT_LOCAL; 218 MNT_IUNLOCK(mp); 219 } 220 221 if (vfs_getopt(mp->mnt_optnew, cache_opt_name, NULL, NULL) == 0) { 222 xmp->nullm_flags |= NULLM_CACHE; 223 } else if (vfs_getopt(mp->mnt_optnew, nocache_opt_name, NULL, 224 NULL) == 0) { 225 ; 226 } else if (null_cache_vnodes && 227 (xmp->nullm_vfs->mnt_kern_flag & MNTK_NULL_NOCACHE) == 0) { 228 xmp->nullm_flags |= NULLM_CACHE; 229 } 230 231 if ((xmp->nullm_flags & NULLM_CACHE) != 0) { 232 vfs_register_for_notification(xmp->nullm_vfs, mp, 233 &xmp->notify_node); 234 } 235 236 if (vfs_getopt(mp->mnt_optnew, unixbypass_opt_name, NULL, NULL) == 0) { 237 ; 238 } else if (vfs_getopt(mp->mnt_optnew, nounixbypass_opt_name, NULL, 239 NULL) == 0) { 240 xmp->nullm_flags |= NULLM_NOUNPBYPASS; 241 } 242 243 if (lowerrootvp == mp->mnt_vnodecovered) { 244 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE); 245 lowerrootvp->v_vflag |= VV_CROSSLOCK; 246 VOP_UNLOCK(lowerrootvp); 247 } 248 249 MNT_ILOCK(mp); 250 if ((xmp->nullm_flags & NULLM_CACHE) != 0) { 251 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & 252 (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED | 253 MNTK_EXTENDED_SHARED); 254 } 255 mp->mnt_kern_flag |= MNTK_NOMSYNC | MNTK_UNLOCKED_INSMNTQUE; 256 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & 257 (MNTK_USES_BCACHE | MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS); 258 MNT_IUNLOCK(mp); 259 vfs_getnewfsid(mp); 260 vfs_mountedfrom(mp, target); 261 vput(nullm_rootvp); 262 263 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n", 264 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 265 return (0); 266 } 267 268 /* 269 * Free reference to null layer 270 */ 271 static int 272 nullfs_unmount(struct mount *mp, int mntflags) 273 { 274 struct null_mount *mntdata; 275 int error, flags; 276 277 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp); 278 279 if (mntflags & MNT_FORCE) 280 flags = FORCECLOSE; 281 else 282 flags = 0; 283 284 for (;;) { 285 /* There is 1 extra root vnode reference (nullm_rootvp). */ 286 error = vflush(mp, 0, flags, curthread); 287 if (error) 288 return (error); 289 MNT_ILOCK(mp); 290 if (mp->mnt_nvnodelistsize == 0) { 291 MNT_IUNLOCK(mp); 292 break; 293 } 294 MNT_IUNLOCK(mp); 295 if ((mntflags & MNT_FORCE) == 0) 296 return (EBUSY); 297 } 298 299 /* 300 * Finally, throw away the null_mount structure 301 */ 302 mntdata = mp->mnt_data; 303 if ((mntdata->nullm_flags & NULLM_CACHE) != 0) { 304 vfs_unregister_for_notification(mntdata->nullm_vfs, 305 &mntdata->notify_node); 306 } 307 if (mntdata->nullm_lowerrootvp == mp->mnt_vnodecovered) { 308 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE); 309 mp->mnt_vnodecovered->v_vflag &= ~VV_CROSSLOCK; 310 VOP_UNLOCK(mp->mnt_vnodecovered); 311 } 312 vfs_unregister_upper(mntdata->nullm_vfs, &mntdata->upper_node); 313 vrele(mntdata->nullm_lowerrootvp); 314 mp->mnt_data = NULL; 315 free(mntdata, M_NULLFSMNT); 316 return (0); 317 } 318 319 static int 320 nullfs_root(struct mount *mp, int flags, struct vnode **vpp) 321 { 322 struct vnode *vp; 323 struct null_mount *mntdata; 324 int error; 325 326 mntdata = MOUNTTONULLMOUNT(mp); 327 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p)\n", mp, 328 mntdata->nullm_lowerrootvp); 329 330 error = vget(mntdata->nullm_lowerrootvp, flags); 331 if (error == 0) { 332 error = null_nodeget(mp, mntdata->nullm_lowerrootvp, &vp); 333 if (error == 0) { 334 *vpp = vp; 335 } 336 } 337 return (error); 338 } 339 340 static int 341 nullfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, bool *mp_busy) 342 { 343 struct mount *lowermp; 344 struct null_mount *mntdata; 345 int error; 346 bool unbusy; 347 348 mntdata = MOUNTTONULLMOUNT(mp); 349 lowermp = atomic_load_ptr(&mntdata->nullm_vfs); 350 KASSERT(*mp_busy == true, ("upper mount not busy")); 351 /* 352 * See comment in sys_quotactl() for an explanation of why the 353 * lower mount needs to be busied by the caller of VFS_QUOTACTL() 354 * but may be unbusied by the implementation. We must unbusy 355 * the upper mount for the same reason; otherwise a namei lookup 356 * issued by the VFS_QUOTACTL() implementation could traverse the 357 * upper mount and deadlock. 358 */ 359 vfs_unbusy(mp); 360 *mp_busy = false; 361 unbusy = true; 362 error = vfs_busy(lowermp, 0); 363 if (error == 0) 364 error = VFS_QUOTACTL(lowermp, cmd, uid, arg, &unbusy); 365 if (unbusy) 366 vfs_unbusy(lowermp); 367 368 return (error); 369 } 370 371 static int 372 nullfs_statfs(struct mount *mp, struct statfs *sbp) 373 { 374 int error; 375 struct statfs *mstat; 376 377 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp, 378 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 379 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 380 381 mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO); 382 383 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, mstat); 384 if (error) { 385 free(mstat, M_STATFS); 386 return (error); 387 } 388 389 sbp->f_type = mstat->f_type; 390 sbp->f_bsize = mstat->f_bsize; 391 sbp->f_iosize = mstat->f_iosize; 392 sbp->f_blocks = mstat->f_blocks; 393 sbp->f_bfree = mstat->f_bfree; 394 sbp->f_bavail = mstat->f_bavail; 395 sbp->f_files = mstat->f_files; 396 sbp->f_ffree = mstat->f_ffree; 397 398 free(mstat, M_STATFS); 399 return (0); 400 } 401 402 static int 403 nullfs_sync(struct mount *mp, int waitfor) 404 { 405 /* 406 * XXX - Assumes no data cached at null layer. 407 */ 408 return (0); 409 } 410 411 static int 412 nullfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 413 { 414 int error; 415 416 KASSERT((flags & LK_TYPE_MASK) != 0, 417 ("nullfs_vget: no lock requested")); 418 419 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp); 420 if (error != 0) 421 return (error); 422 return (null_nodeget(mp, *vpp, vpp)); 423 } 424 425 static int 426 nullfs_fhtovp(struct mount *mp, struct fid *fidp, int flags, struct vnode **vpp) 427 { 428 int error; 429 430 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags, 431 vpp); 432 if (error != 0) 433 return (error); 434 return (null_nodeget(mp, *vpp, vpp)); 435 } 436 437 static int 438 nullfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 439 int namespace, const char *attrname) 440 { 441 442 return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, 443 filename_vp, namespace, attrname)); 444 } 445 446 static void 447 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp) 448 { 449 struct vnode *vp; 450 451 vp = null_hashget(mp, lowervp); 452 if (vp == NULL) 453 return; 454 VTONULL(vp)->null_flags |= NULLV_NOUNLOCK; 455 vgone(vp); 456 vput(vp); 457 } 458 459 static void 460 nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp) 461 { 462 struct vnode *vp; 463 struct null_node *xp; 464 465 vp = null_hashget(mp, lowervp); 466 if (vp == NULL) 467 return; 468 xp = VTONULL(vp); 469 xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK; 470 vhold(vp); 471 vunref(vp); 472 473 if (VN_IS_DOOMED(vp)) { 474 /* 475 * If the vnode is doomed, its lock was split from the lower 476 * vnode lock. Therefore we need to do an extra unlock before 477 * allowing the final vdrop() to free the vnode. 478 */ 479 VOP_UNLOCK(vp); 480 } else { 481 /* 482 * Otherwise, the nullfs vnode still shares the lock 483 * with the lower vnode, and must not be unlocked. 484 * Also clear the NULLV_NOUNLOCK, the flag is not 485 * relevant for future reclamations. 486 */ 487 ASSERT_VOP_ELOCKED(vp, "unlink_lowervp"); 488 xp->null_flags &= ~NULLV_NOUNLOCK; 489 } 490 vdrop(vp); 491 } 492 493 static struct vfsops null_vfsops = { 494 .vfs_extattrctl = nullfs_extattrctl, 495 .vfs_fhtovp = nullfs_fhtovp, 496 .vfs_init = nullfs_init, 497 .vfs_mount = nullfs_mount, 498 .vfs_quotactl = nullfs_quotactl, 499 .vfs_root = nullfs_root, 500 .vfs_statfs = nullfs_statfs, 501 .vfs_sync = nullfs_sync, 502 .vfs_uninit = nullfs_uninit, 503 .vfs_unmount = nullfs_unmount, 504 .vfs_vget = nullfs_vget, 505 .vfs_reclaim_lowervp = nullfs_reclaim_lowervp, 506 .vfs_unlink_lowervp = nullfs_unlink_lowervp, 507 }; 508 509 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL | VFCF_FILEMOUNT); 510