xref: /freebsd/sys/fs/mntfs/mntfs_vnops.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Netflix, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
24  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/conf.h>
30 #include <sys/mount.h>
31 #include <sys/vnode.h>
32 
33 /*
34  * The "mntfs" VCHR vnodes implemented here provide a safe way for file systems
35  * to access their disk devices.  Using the normal devfs vnode has the problem
36  * that if the device disappears, the devfs vnode is vgone'd as part of
37  * removing it from the application-visible namespace, and some file systems
38  * (notably FFS with softdep) get very unhappy if their dirty buffers are
39  * invalidated out from under them.  By using a separate, private vnode,
40  * file systems are able to clean up their buffer state in a controlled fashion
41  * when the underlying device disappears.
42  */
43 
44 static int
45 mntfs_reclaim(struct vop_reclaim_args *ap)
46 {
47 	struct vnode *vp = ap->a_vp;
48 
49 	dev_rel(vp->v_rdev);
50 	return (0);
51 }
52 
53 struct vop_vector mntfs_vnodeops = {
54 	.vop_default =		&default_vnodeops,
55 
56 	.vop_fsync =		vop_stdfsync,
57 	.vop_strategy = 	VOP_PANIC,
58 	.vop_reclaim =		mntfs_reclaim,
59 };
60 VFS_VOP_VECTOR_REGISTER(mntfs_vnodeops);
61 
62 /*
63  * Allocate a private VCHR vnode for use by a mounted fs.
64  * The underlying device will be the same as for the given vnode.
65  * This mntfs vnode must be freed with mntfs_freevp() rather than just
66  * releasing the reference.
67  */
68 struct vnode *
69 mntfs_allocvp(struct mount *mp, struct vnode *ovp)
70 {
71 	struct vnode *vp;
72 	struct cdev *dev;
73 
74 	ASSERT_VOP_ELOCKED(ovp, __func__);
75 
76 	dev = ovp->v_rdev;
77 
78 	getnewvnode("mntfs", mp, &mntfs_vnodeops, &vp);
79 	vp->v_type = VCHR;
80 	vp->v_data = NULL;
81 	dev_ref(dev);
82 	vp->v_rdev = dev;
83 
84 	VOP_UNLOCK(ovp);
85 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
86 	vn_set_state(vp, VSTATE_CONSTRUCTED);
87 	return (vp);
88 }
89 
90 void
91 mntfs_freevp(struct vnode *vp)
92 {
93 	ASSERT_VOP_ELOCKED(vp, "mntfs_freevp");
94 	vgone(vp);
95 	vput(vp);
96 }
97