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 * $FreeBSD$ 38 */ 39 40 /* 41 * Null Layer 42 * (See null_vnops.c for a description of what this does.) 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/fcntl.h> 48 #include <sys/kernel.h> 49 #include <sys/lock.h> 50 #include <sys/malloc.h> 51 #include <sys/mount.h> 52 #include <sys/namei.h> 53 #include <sys/proc.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 /* 72 * Mount null layer 73 */ 74 static int 75 nullfs_mount(struct mount *mp) 76 { 77 struct vnode *lowerrootvp; 78 struct vnode *nullm_rootvp; 79 struct null_mount *xmp; 80 struct null_node *nn; 81 struct nameidata nd, *ndp; 82 char *target; 83 int error, len; 84 bool isvnunlocked; 85 86 NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp); 87 88 if (mp->mnt_flag & MNT_ROOTFS) 89 return (EOPNOTSUPP); 90 91 /* 92 * Update is a no-op 93 */ 94 if (mp->mnt_flag & MNT_UPDATE) { 95 /* 96 * Only support update mounts for NFS export. 97 */ 98 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) 99 return (0); 100 else 101 return (EOPNOTSUPP); 102 } 103 104 /* 105 * Get argument 106 */ 107 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target, &len); 108 if (error != 0) 109 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len); 110 if (error || target[len - 1] != '\0') 111 return (EINVAL); 112 113 /* 114 * Unlock lower node to avoid possible deadlock. 115 */ 116 if (mp->mnt_vnodecovered->v_op == &null_vnodeops && 117 VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) { 118 VOP_UNLOCK(mp->mnt_vnodecovered); 119 isvnunlocked = true; 120 } else { 121 isvnunlocked = false; 122 } 123 124 /* 125 * Find lower node 126 */ 127 ndp = &nd; 128 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread); 129 error = namei(ndp); 130 131 /* 132 * Re-lock vnode. 133 * XXXKIB This is deadlock-prone as well. 134 */ 135 if (isvnunlocked) 136 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY); 137 138 if (error) 139 return (error); 140 NDFREE(ndp, NDF_ONLY_PNBUF); 141 142 /* 143 * Sanity check on lower vnode 144 */ 145 lowerrootvp = ndp->ni_vp; 146 147 /* 148 * Check multi null mount to avoid `lock against myself' panic. 149 */ 150 if (mp->mnt_vnodecovered->v_op == &null_vnodeops) { 151 nn = VTONULL(mp->mnt_vnodecovered); 152 if (nn == NULL || lowerrootvp == nn->null_lowervp) { 153 NULLFSDEBUG("nullfs_mount: multi null mount?\n"); 154 vput(lowerrootvp); 155 return (EDEADLK); 156 } 157 } 158 159 xmp = (struct null_mount *) malloc(sizeof(struct null_mount), 160 M_NULLFSMNT, M_WAITOK | M_ZERO); 161 162 /* 163 * Save pointer to underlying FS and the reference to the 164 * lower root vnode. 165 */ 166 xmp->nullm_vfs = lowerrootvp->v_mount; 167 vref(lowerrootvp); 168 xmp->nullm_lowerrootvp = lowerrootvp; 169 mp->mnt_data = xmp; 170 171 /* 172 * Make sure the node alias worked. 173 */ 174 error = null_nodeget(mp, lowerrootvp, &nullm_rootvp); 175 if (error != 0) { 176 vrele(lowerrootvp); 177 free(xmp, M_NULLFSMNT); 178 return (error); 179 } 180 181 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) { 182 MNT_ILOCK(mp); 183 mp->mnt_flag |= MNT_LOCAL; 184 MNT_IUNLOCK(mp); 185 } 186 187 xmp->nullm_flags |= NULLM_CACHE; 188 if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0 || 189 (xmp->nullm_vfs->mnt_kern_flag & MNTK_NULL_NOCACHE) != 0) 190 xmp->nullm_flags &= ~NULLM_CACHE; 191 192 MNT_ILOCK(mp); 193 if ((xmp->nullm_flags & NULLM_CACHE) != 0) { 194 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & 195 (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED | 196 MNTK_EXTENDED_SHARED); 197 } 198 mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT | MNTK_NOMSYNC; 199 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & 200 (MNTK_USES_BCACHE | MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS); 201 MNT_IUNLOCK(mp); 202 vfs_getnewfsid(mp); 203 if ((xmp->nullm_flags & NULLM_CACHE) != 0) { 204 MNT_ILOCK(xmp->nullm_vfs); 205 TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp, 206 mnt_upper_link); 207 MNT_IUNLOCK(xmp->nullm_vfs); 208 } 209 210 vfs_mountedfrom(mp, target); 211 vput(nullm_rootvp); 212 213 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n", 214 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 215 return (0); 216 } 217 218 /* 219 * Free reference to null layer 220 */ 221 static int 222 nullfs_unmount(mp, mntflags) 223 struct mount *mp; 224 int mntflags; 225 { 226 struct null_mount *mntdata; 227 struct mount *ump; 228 int error, flags; 229 230 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp); 231 232 if (mntflags & MNT_FORCE) 233 flags = FORCECLOSE; 234 else 235 flags = 0; 236 237 for (;;) { 238 /* There is 1 extra root vnode reference (nullm_rootvp). */ 239 error = vflush(mp, 0, flags, curthread); 240 if (error) 241 return (error); 242 MNT_ILOCK(mp); 243 if (mp->mnt_nvnodelistsize == 0) { 244 MNT_IUNLOCK(mp); 245 break; 246 } 247 MNT_IUNLOCK(mp); 248 if ((mntflags & MNT_FORCE) == 0) 249 return (EBUSY); 250 } 251 252 /* 253 * Finally, throw away the null_mount structure 254 */ 255 mntdata = mp->mnt_data; 256 ump = mntdata->nullm_vfs; 257 if ((mntdata->nullm_flags & NULLM_CACHE) != 0) { 258 MNT_ILOCK(ump); 259 while ((ump->mnt_kern_flag & MNTK_VGONE_UPPER) != 0) { 260 ump->mnt_kern_flag |= MNTK_VGONE_WAITER; 261 msleep(&ump->mnt_uppers, &ump->mnt_mtx, 0, "vgnupw", 0); 262 } 263 TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link); 264 MNT_IUNLOCK(ump); 265 } 266 vrele(mntdata->nullm_lowerrootvp); 267 mp->mnt_data = NULL; 268 free(mntdata, M_NULLFSMNT); 269 return (0); 270 } 271 272 static int 273 nullfs_root(mp, flags, vpp) 274 struct mount *mp; 275 int flags; 276 struct vnode **vpp; 277 { 278 struct vnode *vp; 279 struct null_mount *mntdata; 280 int error; 281 282 mntdata = MOUNTTONULLMOUNT(mp); 283 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p)\n", mp, 284 mntdata->nullm_lowerrootvp); 285 286 error = vget(mntdata->nullm_lowerrootvp, flags); 287 if (error == 0) { 288 error = null_nodeget(mp, mntdata->nullm_lowerrootvp, &vp); 289 if (error == 0) { 290 *vpp = vp; 291 } 292 } 293 return (error); 294 } 295 296 static int 297 nullfs_quotactl(mp, cmd, uid, arg, mp_busy) 298 struct mount *mp; 299 int cmd; 300 uid_t uid; 301 void *arg; 302 bool *mp_busy; 303 { 304 struct mount *lowermp; 305 struct null_mount *mntdata; 306 int error; 307 bool unbusy; 308 309 mntdata = MOUNTTONULLMOUNT(mp); 310 lowermp = atomic_load_ptr(&mntdata->nullm_vfs); 311 KASSERT(*mp_busy == true, ("upper mount not busy")); 312 /* 313 * See comment in sys_quotactl() for an explanation of why the 314 * lower mount needs to be busied by the caller of VFS_QUOTACTL() 315 * but may be unbusied by the implementation. We must unbusy 316 * the upper mount for the same reason; otherwise a namei lookup 317 * issued by the VFS_QUOTACTL() implementation could traverse the 318 * upper mount and deadlock. 319 */ 320 vfs_unbusy(mp); 321 *mp_busy = false; 322 unbusy = true; 323 error = vfs_busy(lowermp, 0); 324 if (error == 0) 325 error = VFS_QUOTACTL(lowermp, cmd, uid, arg, &unbusy); 326 if (unbusy) 327 vfs_unbusy(lowermp); 328 329 return (error); 330 } 331 332 static int 333 nullfs_statfs(mp, sbp) 334 struct mount *mp; 335 struct statfs *sbp; 336 { 337 int error; 338 struct statfs *mstat; 339 340 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp, 341 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 342 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 343 344 mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO); 345 346 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, mstat); 347 if (error) { 348 free(mstat, M_STATFS); 349 return (error); 350 } 351 352 /* now copy across the "interesting" information and fake the rest */ 353 sbp->f_type = mstat->f_type; 354 sbp->f_flags = (sbp->f_flags & (MNT_RDONLY | MNT_NOEXEC | MNT_NOSUID | 355 MNT_UNION | MNT_NOSYMFOLLOW | MNT_AUTOMOUNTED)) | 356 (mstat->f_flags & ~(MNT_ROOTFS | MNT_AUTOMOUNTED)); 357 sbp->f_bsize = mstat->f_bsize; 358 sbp->f_iosize = mstat->f_iosize; 359 sbp->f_blocks = mstat->f_blocks; 360 sbp->f_bfree = mstat->f_bfree; 361 sbp->f_bavail = mstat->f_bavail; 362 sbp->f_files = mstat->f_files; 363 sbp->f_ffree = mstat->f_ffree; 364 365 free(mstat, M_STATFS); 366 return (0); 367 } 368 369 static int 370 nullfs_sync(mp, waitfor) 371 struct mount *mp; 372 int waitfor; 373 { 374 /* 375 * XXX - Assumes no data cached at null layer. 376 */ 377 return (0); 378 } 379 380 static int 381 nullfs_vget(mp, ino, flags, vpp) 382 struct mount *mp; 383 ino_t ino; 384 int flags; 385 struct vnode **vpp; 386 { 387 int error; 388 389 KASSERT((flags & LK_TYPE_MASK) != 0, 390 ("nullfs_vget: no lock requested")); 391 392 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp); 393 if (error != 0) 394 return (error); 395 return (null_nodeget(mp, *vpp, vpp)); 396 } 397 398 static int 399 nullfs_fhtovp(mp, fidp, flags, vpp) 400 struct mount *mp; 401 struct fid *fidp; 402 int flags; 403 struct vnode **vpp; 404 { 405 int error; 406 407 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags, 408 vpp); 409 if (error != 0) 410 return (error); 411 return (null_nodeget(mp, *vpp, vpp)); 412 } 413 414 static int 415 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname) 416 struct mount *mp; 417 int cmd; 418 struct vnode *filename_vp; 419 int namespace; 420 const char *attrname; 421 { 422 423 return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, 424 filename_vp, namespace, attrname)); 425 } 426 427 static void 428 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp) 429 { 430 struct vnode *vp; 431 432 vp = null_hashget(mp, lowervp); 433 if (vp == NULL) 434 return; 435 VTONULL(vp)->null_flags |= NULLV_NOUNLOCK; 436 vgone(vp); 437 vput(vp); 438 } 439 440 static void 441 nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp) 442 { 443 struct vnode *vp; 444 struct null_node *xp; 445 446 vp = null_hashget(mp, lowervp); 447 if (vp == NULL) 448 return; 449 xp = VTONULL(vp); 450 xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK; 451 vhold(vp); 452 vunref(vp); 453 454 if (vp->v_usecount == 0) { 455 /* 456 * If vunref() dropped the last use reference on the 457 * nullfs vnode, it must be reclaimed, and its lock 458 * was split from the lower vnode lock. Need to do 459 * extra unlock before allowing the final vdrop() to 460 * free the vnode. 461 */ 462 KASSERT(VN_IS_DOOMED(vp), 463 ("not reclaimed nullfs vnode %p", vp)); 464 VOP_UNLOCK(vp); 465 } else { 466 /* 467 * Otherwise, the nullfs vnode still shares the lock 468 * with the lower vnode, and must not be unlocked. 469 * Also clear the NULLV_NOUNLOCK, the flag is not 470 * relevant for future reclamations. 471 */ 472 ASSERT_VOP_ELOCKED(vp, "unlink_lowervp"); 473 KASSERT(!VN_IS_DOOMED(vp), 474 ("reclaimed nullfs vnode %p", vp)); 475 xp->null_flags &= ~NULLV_NOUNLOCK; 476 } 477 vdrop(vp); 478 } 479 480 static struct vfsops null_vfsops = { 481 .vfs_extattrctl = nullfs_extattrctl, 482 .vfs_fhtovp = nullfs_fhtovp, 483 .vfs_init = nullfs_init, 484 .vfs_mount = nullfs_mount, 485 .vfs_quotactl = nullfs_quotactl, 486 .vfs_root = nullfs_root, 487 .vfs_statfs = nullfs_statfs, 488 .vfs_sync = nullfs_sync, 489 .vfs_uninit = nullfs_uninit, 490 .vfs_unmount = nullfs_unmount, 491 .vfs_vget = nullfs_vget, 492 .vfs_reclaim_lowervp = nullfs_reclaim_lowervp, 493 .vfs_unlink_lowervp = nullfs_unlink_lowervp, 494 }; 495 496 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL); 497