/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020 Netflix, Inc.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,20* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS21* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND22* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR23* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE24* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/conf.h>29#include <sys/mount.h>30#include <sys/vnode.h>3132/*33* The "mntfs" VCHR vnodes implemented here provide a safe way for file systems34* to access their disk devices. Using the normal devfs vnode has the problem35* that if the device disappears, the devfs vnode is vgone'd as part of36* removing it from the application-visible namespace, and some file systems37* (notably FFS with softdep) get very unhappy if their dirty buffers are38* invalidated out from under them. By using a separate, private vnode,39* file systems are able to clean up their buffer state in a controlled fashion40* when the underlying device disappears.41*/4243static int44mntfs_reclaim(struct vop_reclaim_args *ap)45{46struct vnode *vp = ap->a_vp;4748dev_rel(vp->v_rdev);49return (0);50}5152struct vop_vector mntfs_vnodeops = {53.vop_default = &default_vnodeops,5455.vop_fsync = vop_stdfsync,56.vop_strategy = VOP_PANIC,57.vop_reclaim = mntfs_reclaim,58};59VFS_VOP_VECTOR_REGISTER(mntfs_vnodeops);6061/*62* Allocate a private VCHR vnode for use by a mounted fs.63* The underlying device will be the same as for the given vnode.64* This mntfs vnode must be freed with mntfs_freevp() rather than just65* releasing the reference.66*/67struct vnode *68mntfs_allocvp(struct mount *mp, struct vnode *ovp)69{70struct vnode *vp;71struct cdev *dev;7273ASSERT_VOP_ELOCKED(ovp, __func__);7475dev = ovp->v_rdev;7677getnewvnode("mntfs", mp, &mntfs_vnodeops, &vp);78vp->v_type = VCHR;79vp->v_data = NULL;80dev_ref(dev);81vp->v_rdev = dev;8283VOP_UNLOCK(ovp);84vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);85vn_set_state(vp, VSTATE_CONSTRUCTED);86return (vp);87}8889void90mntfs_freevp(struct vnode *vp)91{92ASSERT_VOP_ELOCKED(vp, "mntfs_freevp");93vgone(vp);94vput(vp);95}969798