1 /*        $NetBSD: null_vfsops.c,v 1.101 2023/02/06 10:32:58 hannken Exp $      */
2 
3 /*
4  * Copyright (c) 1999 National Aeronautics & Space Administration
5  * All rights reserved.
6  *
7  * This software was written by William Studenmund of the
8  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
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 National Aeronautics & Space Administration
19  *    nor the names of its contributors may be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1992, 1993, 1995
38  *        The Regents of the University of California.  All rights reserved.
39  *
40  * This code is derived from software donated to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *        from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
68  *        from: @(#)lofs_vfsops.c       1.2 (Berkeley) 6/18/92
69  *        @(#)null_vfsops.c   8.7 (Berkeley) 5/14/95
70  */
71 
72 /*
73  * Null file-system: VFS operations.
74  *
75  * See null_vnops.c for a description.
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: null_vfsops.c,v 1.101 2023/02/06 10:32:58 hannken Exp $");
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/sysctl.h>
84 #include <sys/vnode.h>
85 #include <sys/mount.h>
86 #include <sys/namei.h>
87 #include <sys/module.h>
88 
89 #include <miscfs/nullfs/null.h>
90 #include <miscfs/genfs/layer_extern.h>
91 
92 MODULE(MODULE_CLASS_VFS, null, "layerfs");
93 
94 VFS_PROTOS(nullfs);
95 
96 int
nullfs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)97 nullfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
98 {
99           struct vnode *lowerrootvp, *vp;
100           struct null_args *args = data;
101           struct null_mount *nmp;
102           struct layer_mount *lmp;
103           struct pathbuf *pb;
104           struct nameidata nd;
105           int error;
106 
107           if (args == NULL)
108                     return EINVAL;
109           if (*data_len < sizeof(*args))
110                     return EINVAL;
111 
112           if (mp->mnt_flag & MNT_GETARGS) {
113                     lmp = MOUNTTOLAYERMOUNT(mp);
114                     if (lmp == NULL)
115                               return EIO;
116                     args->la.target = NULL;
117                     *data_len = sizeof(*args);
118                     return 0;
119           }
120 
121           /* Update is not supported. */
122           if (mp->mnt_flag & MNT_UPDATE)
123                     return EOPNOTSUPP;
124 
125           /* Find the lower vnode and lock it. */
126           error = pathbuf_copyin(args->la.target, &pb);
127           if (error) {
128                     return error;
129           }
130           NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
131           if ((error = namei(&nd)) != 0) {
132                     pathbuf_destroy(pb);
133                     return error;
134           }
135           lowerrootvp = nd.ni_vp;
136           pathbuf_destroy(pb);
137 
138           /* Create the mount point. */
139           nmp = kmem_zalloc(sizeof(struct null_mount), KM_SLEEP);
140           mp->mnt_data = nmp;
141           mp->mnt_iflag |= lowerrootvp->v_mount->mnt_iflag & IMNT_MPSAFE;
142           mp->mnt_iflag |= lowerrootvp->v_mount->mnt_iflag & IMNT_SHRLOOKUP;
143 
144           /*
145            * Make sure that the mount point is sufficiently initialized
146            * that the node create call will work.
147            */
148           vfs_getnewfsid(mp);
149           error = vfs_set_lowermount(mp, lowerrootvp->v_mount);
150           if (error) {
151                     vput(lowerrootvp);
152                     kmem_free(nmp, sizeof(struct null_mount));
153                     return error;
154           }
155 
156           nmp->nullm_size = sizeof(struct null_node);
157           nmp->nullm_tag = VT_NULL;
158           nmp->nullm_bypass = layer_bypass;
159           nmp->nullm_vnodeop_p = null_vnodeop_p;
160 
161           /* Setup a null node for root vnode. */
162           VOP_UNLOCK(lowerrootvp);
163           error = layer_node_create(mp, lowerrootvp, &vp);
164           if (error) {
165                     vrele(lowerrootvp);
166                     kmem_free(nmp, sizeof(struct null_mount));
167                     return error;
168           }
169           /*
170            * Keep a held reference to the root vnode.  It will be released on
171            * umount.  Note: nullfs is MP-safe.
172            */
173           vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
174           vp->v_vflag |= VV_ROOT;
175           nmp->nullm_rootvp = vp;
176           VOP_UNLOCK(vp);
177 
178           error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
179               UIO_USERSPACE, mp->mnt_op->vfs_name, mp, curlwp);
180           if (error)
181                     return error;
182 
183           if (mp->mnt_lower->mnt_flag & MNT_LOCAL)
184                     mp->mnt_flag |= MNT_LOCAL;
185           return 0;
186 }
187 
188 int
nullfs_unmount(struct mount * mp,int mntflags)189 nullfs_unmount(struct mount *mp, int mntflags)
190 {
191           struct null_mount *nmp = MOUNTTONULLMOUNT(mp);
192           struct vnode *null_rootvp = nmp->nullm_rootvp;
193           int error, flags = 0;
194 
195           if (mntflags & MNT_FORCE)
196                     flags |= FORCECLOSE;
197 
198           if (vrefcnt(null_rootvp) > 1 && (mntflags & MNT_FORCE) == 0)
199                     return EBUSY;
200 
201           if ((error = vflush(mp, null_rootvp, flags)) != 0)
202                     return error;
203 
204           /* Eliminate all activity and release the vnode. */
205           vgone(null_rootvp);
206 
207           /* Finally, destroy the mount point structures. */
208           kmem_free(mp->mnt_data, sizeof(struct null_mount));
209           mp->mnt_data = NULL;
210           return 0;
211 }
212 
213 extern const struct vnodeopv_desc null_vnodeop_opv_desc;
214 
215 const struct vnodeopv_desc * const nullfs_vnodeopv_descs[] = {
216           &null_vnodeop_opv_desc,
217           NULL,
218 };
219 
220 struct vfsops nullfs_vfsops = {
221           .vfs_name = MOUNT_NULL,
222           .vfs_min_mount_data = sizeof (struct null_args),
223           .vfs_mount = nullfs_mount,
224           .vfs_start = layerfs_start,
225           .vfs_unmount = nullfs_unmount,
226           .vfs_root = layerfs_root,
227           .vfs_quotactl = layerfs_quotactl,
228           .vfs_statvfs = layerfs_statvfs,
229           .vfs_sync = layerfs_sync,
230           .vfs_loadvnode = layerfs_loadvnode,
231           .vfs_vget = layerfs_vget,
232           .vfs_fhtovp = layerfs_fhtovp,
233           .vfs_vptofh = layerfs_vptofh,
234           .vfs_init = layerfs_init,
235           .vfs_done = layerfs_done,
236           .vfs_snapshot = layerfs_snapshot,
237           .vfs_extattrctl = vfs_stdextattrctl,
238           .vfs_suspendctl = layerfs_suspendctl,
239           .vfs_renamelock_enter = layerfs_renamelock_enter,
240           .vfs_renamelock_exit = layerfs_renamelock_exit,
241           .vfs_fsync = (void *)eopnotsupp,
242           .vfs_opv_descs = nullfs_vnodeopv_descs
243 };
244 
245 SYSCTL_SETUP(nullfs_sysctl_setup, "nullfs sysctl")
246 {
247 
248           sysctl_createv(clog, 0, NULL, NULL,
249               CTLFLAG_PERMANENT,
250               CTLTYPE_NODE, "null",
251               SYSCTL_DESCR("Loopback file system"),
252               NULL, 0, NULL, 0,
253               CTL_VFS, 9, CTL_EOL);
254           /*
255            * XXX the "9" above could be dynamic, thereby eliminating
256            * one more instance of the "number to vfs" mapping problem,
257            * but "9" is the order as taken from sys/mount.h
258            */
259 }
260 
261 static int
null_modcmd(modcmd_t cmd,void * arg)262 null_modcmd(modcmd_t cmd, void *arg)
263 {
264           int error;
265 
266           switch (cmd) {
267           case MODULE_CMD_INIT:
268                     error = vfs_attach(&nullfs_vfsops);
269                     if (error != 0)
270                               break;
271                     break;
272           case MODULE_CMD_FINI:
273                     error = vfs_detach(&nullfs_vfsops);
274                     if (error != 0)
275                               break;
276                     break;
277           default:
278                     error = ENOTTY;
279                     break;
280           }
281           return error;
282 }
283