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 * @(#)null_vfsops.c 8.2 (Berkeley) 1/21/94 35 * 36 * @(#)lofs_vfsops.c 1.2 (Berkeley) 6/18/92 37 */ 38 39 /* 40 * Null Layer 41 * (See null_vnops.c for a description of what this does.) 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/fcntl.h> 47 #include <sys/kernel.h> 48 #include <sys/lock.h> 49 #include <sys/malloc.h> 50 #include <sys/mount.h> 51 #include <sys/namei.h> 52 #include <sys/proc.h> 53 #include <sys/sysctl.h> 54 #include <sys/vnode.h> 55 #include <sys/jail.h> 56 57 #include <fs/nullfs/null.h> 58 59 static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure"); 60 61 static vfs_fhtovp_t nullfs_fhtovp; 62 static vfs_mount_t nullfs_mount; 63 static vfs_quotactl_t nullfs_quotactl; 64 static vfs_root_t nullfs_root; 65 static vfs_sync_t nullfs_sync; 66 static vfs_statfs_t nullfs_statfs; 67 static vfs_unmount_t nullfs_unmount; 68 static vfs_vget_t nullfs_vget; 69 static vfs_extattrctl_t nullfs_extattrctl; 70 71 SYSCTL_NODE(_vfs, OID_AUTO, nullfs, CTLFLAG_RW, 0, "nullfs"); 72 73 static bool null_cache_vnodes = true; 74 SYSCTL_BOOL(_vfs_nullfs, OID_AUTO, cache_vnodes, CTLFLAG_RWTUN, 75 &null_cache_vnodes, 0, 76 "cache free nullfs vnodes"); 77 78 /* 79 * Mount null layer 80 */ 81 static int 82 nullfs_mount(struct mount *mp) 83 { 84 struct vnode *lowerrootvp; 85 struct vnode *nullm_rootvp; 86 struct null_mount *xmp; 87 struct null_node *nn; 88 struct nameidata nd, *ndp; 89 char *target; 90 int error, len; 91 bool isvnunlocked; 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 (mp->mnt_vnodecovered->v_op == &null_vnodeops && 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 * Check multi null mount to avoid `lock against myself' panic. 156 */ 157 if (mp->mnt_vnodecovered->v_op == &null_vnodeops) { 158 nn = VTONULL(mp->mnt_vnodecovered); 159 if (nn == NULL || lowerrootvp == nn->null_lowervp) { 160 NULLFSDEBUG("nullfs_mount: multi null mount?\n"); 161 vput(lowerrootvp); 162 return (EDEADLK); 163 } 164 } 165 166 /* 167 * Lower vnode must be the same type as the covered vnode - we 168 * don't allow mounting directories to files or vice versa. 169 */ 170 if ((lowerrootvp->v_type != VDIR && lowerrootvp->v_type != VREG) || 171 lowerrootvp->v_type != mp->mnt_vnodecovered->v_type) { 172 NULLFSDEBUG("nullfs_mount: target must be same type as fspath"); 173 vput(lowerrootvp); 174 return (EINVAL); 175 } 176 177 xmp = malloc(sizeof(struct null_mount), M_NULLFSMNT, 178 M_WAITOK | M_ZERO); 179 180 /* 181 * Save pointer to underlying FS and the reference to the 182 * lower root vnode. 183 */ 184 xmp->nullm_vfs = vfs_register_upper_from_vp(lowerrootvp, mp, 185 &xmp->upper_node); 186 if (xmp->nullm_vfs == NULL) { 187 vput(lowerrootvp); 188 free(xmp, M_NULLFSMNT); 189 return (ENOENT); 190 } 191 vref(lowerrootvp); 192 xmp->nullm_lowerrootvp = lowerrootvp; 193 mp->mnt_data = xmp; 194 195 /* 196 * Make sure the node alias worked. 197 */ 198 error = null_nodeget(mp, lowerrootvp, &nullm_rootvp); 199 if (error != 0) { 200 vfs_unregister_upper(xmp->nullm_vfs, &xmp->upper_node); 201 vrele(lowerrootvp); 202 free(xmp, M_NULLFSMNT); 203 return (error); 204 } 205 206 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) { 207 MNT_ILOCK(mp); 208 mp->mnt_flag |= MNT_LOCAL; 209 MNT_IUNLOCK(mp); 210 } 211 212 if (vfs_getopt(mp->mnt_optnew, "cache", NULL, NULL) == 0) { 213 xmp->nullm_flags |= NULLM_CACHE; 214 } else if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0) { 215 ; 216 } else if (null_cache_vnodes && 217 (xmp->nullm_vfs->mnt_kern_flag & MNTK_NULL_NOCACHE) == 0) { 218 xmp->nullm_flags |= NULLM_CACHE; 219 } 220 221 if ((xmp->nullm_flags & NULLM_CACHE) != 0) { 222 vfs_register_for_notification(xmp->nullm_vfs, mp, 223 &xmp->notify_node); 224 } 225 226 if (lowerrootvp == mp->mnt_vnodecovered) { 227 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE); 228 lowerrootvp->v_vflag |= VV_CROSSLOCK; 229 VOP_UNLOCK(lowerrootvp); 230 } 231 232 MNT_ILOCK(mp); 233 if ((xmp->nullm_flags & NULLM_CACHE) != 0) { 234 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & 235 (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED | 236 MNTK_EXTENDED_SHARED); 237 } 238 mp->mnt_kern_flag |= MNTK_NOMSYNC | MNTK_UNLOCKED_INSMNTQUE; 239 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & 240 (MNTK_USES_BCACHE | MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS); 241 MNT_IUNLOCK(mp); 242 vfs_getnewfsid(mp); 243 vfs_mountedfrom(mp, target); 244 vput(nullm_rootvp); 245 246 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n", 247 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 248 return (0); 249 } 250 251 /* 252 * Free reference to null layer 253 */ 254 static int 255 nullfs_unmount(struct mount *mp, int mntflags) 256 { 257 struct null_mount *mntdata; 258 int error, flags; 259 260 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp); 261 262 if (mntflags & MNT_FORCE) 263 flags = FORCECLOSE; 264 else 265 flags = 0; 266 267 for (;;) { 268 /* There is 1 extra root vnode reference (nullm_rootvp). */ 269 error = vflush(mp, 0, flags, curthread); 270 if (error) 271 return (error); 272 MNT_ILOCK(mp); 273 if (mp->mnt_nvnodelistsize == 0) { 274 MNT_IUNLOCK(mp); 275 break; 276 } 277 MNT_IUNLOCK(mp); 278 if ((mntflags & MNT_FORCE) == 0) 279 return (EBUSY); 280 } 281 282 /* 283 * Finally, throw away the null_mount structure 284 */ 285 mntdata = mp->mnt_data; 286 if ((mntdata->nullm_flags & NULLM_CACHE) != 0) { 287 vfs_unregister_for_notification(mntdata->nullm_vfs, 288 &mntdata->notify_node); 289 } 290 if (mntdata->nullm_lowerrootvp == mp->mnt_vnodecovered) { 291 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE); 292 mp->mnt_vnodecovered->v_vflag &= ~VV_CROSSLOCK; 293 VOP_UNLOCK(mp->mnt_vnodecovered); 294 } 295 vfs_unregister_upper(mntdata->nullm_vfs, &mntdata->upper_node); 296 vrele(mntdata->nullm_lowerrootvp); 297 mp->mnt_data = NULL; 298 free(mntdata, M_NULLFSMNT); 299 return (0); 300 } 301 302 static int 303 nullfs_root(struct mount *mp, int flags, struct vnode **vpp) 304 { 305 struct vnode *vp; 306 struct null_mount *mntdata; 307 int error; 308 309 mntdata = MOUNTTONULLMOUNT(mp); 310 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p)\n", mp, 311 mntdata->nullm_lowerrootvp); 312 313 error = vget(mntdata->nullm_lowerrootvp, flags); 314 if (error == 0) { 315 error = null_nodeget(mp, mntdata->nullm_lowerrootvp, &vp); 316 if (error == 0) { 317 *vpp = vp; 318 } 319 } 320 return (error); 321 } 322 323 static int 324 nullfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, bool *mp_busy) 325 { 326 struct mount *lowermp; 327 struct null_mount *mntdata; 328 int error; 329 bool unbusy; 330 331 mntdata = MOUNTTONULLMOUNT(mp); 332 lowermp = atomic_load_ptr(&mntdata->nullm_vfs); 333 KASSERT(*mp_busy == true, ("upper mount not busy")); 334 /* 335 * See comment in sys_quotactl() for an explanation of why the 336 * lower mount needs to be busied by the caller of VFS_QUOTACTL() 337 * but may be unbusied by the implementation. We must unbusy 338 * the upper mount for the same reason; otherwise a namei lookup 339 * issued by the VFS_QUOTACTL() implementation could traverse the 340 * upper mount and deadlock. 341 */ 342 vfs_unbusy(mp); 343 *mp_busy = false; 344 unbusy = true; 345 error = vfs_busy(lowermp, 0); 346 if (error == 0) 347 error = VFS_QUOTACTL(lowermp, cmd, uid, arg, &unbusy); 348 if (unbusy) 349 vfs_unbusy(lowermp); 350 351 return (error); 352 } 353 354 static int 355 nullfs_statfs(struct mount *mp, struct statfs *sbp) 356 { 357 int error; 358 struct statfs *mstat; 359 360 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp, 361 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 362 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 363 364 mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO); 365 366 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, mstat); 367 if (error) { 368 free(mstat, M_STATFS); 369 return (error); 370 } 371 372 /* now copy across the "interesting" information and fake the rest */ 373 sbp->f_type = mstat->f_type; 374 sbp->f_flags = (sbp->f_flags & (MNT_RDONLY | MNT_NOEXEC | MNT_NOSUID | 375 MNT_UNION | MNT_NOSYMFOLLOW | MNT_AUTOMOUNTED)) | 376 (mstat->f_flags & ~(MNT_ROOTFS | MNT_AUTOMOUNTED)); 377 sbp->f_bsize = mstat->f_bsize; 378 sbp->f_iosize = mstat->f_iosize; 379 sbp->f_blocks = mstat->f_blocks; 380 sbp->f_bfree = mstat->f_bfree; 381 sbp->f_bavail = mstat->f_bavail; 382 sbp->f_files = mstat->f_files; 383 sbp->f_ffree = mstat->f_ffree; 384 385 free(mstat, M_STATFS); 386 return (0); 387 } 388 389 static int 390 nullfs_sync(struct mount *mp, int waitfor) 391 { 392 /* 393 * XXX - Assumes no data cached at null layer. 394 */ 395 return (0); 396 } 397 398 static int 399 nullfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 400 { 401 int error; 402 403 KASSERT((flags & LK_TYPE_MASK) != 0, 404 ("nullfs_vget: no lock requested")); 405 406 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp); 407 if (error != 0) 408 return (error); 409 return (null_nodeget(mp, *vpp, vpp)); 410 } 411 412 static int 413 nullfs_fhtovp(struct mount *mp, struct fid *fidp, int flags, struct vnode **vpp) 414 { 415 int error; 416 417 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags, 418 vpp); 419 if (error != 0) 420 return (error); 421 return (null_nodeget(mp, *vpp, vpp)); 422 } 423 424 static int 425 nullfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 426 int namespace, const char *attrname) 427 { 428 429 return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, 430 filename_vp, namespace, attrname)); 431 } 432 433 static void 434 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp) 435 { 436 struct vnode *vp; 437 438 vp = null_hashget(mp, lowervp); 439 if (vp == NULL) 440 return; 441 VTONULL(vp)->null_flags |= NULLV_NOUNLOCK; 442 vgone(vp); 443 vput(vp); 444 } 445 446 static void 447 nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp) 448 { 449 struct vnode *vp; 450 struct null_node *xp; 451 452 vp = null_hashget(mp, lowervp); 453 if (vp == NULL) 454 return; 455 xp = VTONULL(vp); 456 xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK; 457 vhold(vp); 458 vunref(vp); 459 460 if (vp->v_usecount == 0) { 461 /* 462 * If vunref() dropped the last use reference on the 463 * nullfs vnode, it must be reclaimed, and its lock 464 * was split from the lower vnode lock. Need to do 465 * extra unlock before allowing the final vdrop() to 466 * free the vnode. 467 */ 468 KASSERT(VN_IS_DOOMED(vp), 469 ("not reclaimed nullfs vnode %p", vp)); 470 VOP_UNLOCK(vp); 471 } else { 472 /* 473 * Otherwise, the nullfs vnode still shares the lock 474 * with the lower vnode, and must not be unlocked. 475 * Also clear the NULLV_NOUNLOCK, the flag is not 476 * relevant for future reclamations. 477 */ 478 ASSERT_VOP_ELOCKED(vp, "unlink_lowervp"); 479 KASSERT(!VN_IS_DOOMED(vp), 480 ("reclaimed nullfs vnode %p", vp)); 481 xp->null_flags &= ~NULLV_NOUNLOCK; 482 } 483 vdrop(vp); 484 } 485 486 static struct vfsops null_vfsops = { 487 .vfs_extattrctl = nullfs_extattrctl, 488 .vfs_fhtovp = nullfs_fhtovp, 489 .vfs_init = nullfs_init, 490 .vfs_mount = nullfs_mount, 491 .vfs_quotactl = nullfs_quotactl, 492 .vfs_root = nullfs_root, 493 .vfs_statfs = nullfs_statfs, 494 .vfs_sync = nullfs_sync, 495 .vfs_uninit = nullfs_uninit, 496 .vfs_unmount = nullfs_unmount, 497 .vfs_vget = nullfs_vget, 498 .vfs_reclaim_lowervp = nullfs_reclaim_lowervp, 499 .vfs_unlink_lowervp = nullfs_unlink_lowervp, 500 }; 501 502 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL | VFCF_FILEMOUNT); 503