null_vfsops.c revision 1.89 1 /* $NetBSD: null_vfsops.c,v 1.89 2014/05/25 13:51:25 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.89 2014/05/25 13:51:25 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/malloc.h>
88 #include <sys/module.h>
89
90 #include <miscfs/nullfs/null.h>
91 #include <miscfs/genfs/layer_extern.h>
92
93 MODULE(MODULE_CLASS_VFS, null, "layerfs");
94
95 VFS_PROTOS(nullfs);
96
97 static struct sysctllog *nullfs_sysctl_log;
98
99 int
100 nullfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
101 {
102 struct vnode *lowerrootvp, *vp;
103 struct null_args *args = data;
104 struct null_mount *nmp;
105 struct layer_mount *lmp;
106 struct pathbuf *pb;
107 struct nameidata nd;
108 int error;
109
110 if (args == NULL)
111 return EINVAL;
112 if (*data_len < sizeof(*args))
113 return EINVAL;
114
115 if (mp->mnt_flag & MNT_GETARGS) {
116 lmp = MOUNTTOLAYERMOUNT(mp);
117 if (lmp == NULL)
118 return EIO;
119 args->la.target = NULL;
120 *data_len = sizeof(*args);
121 return 0;
122 }
123
124 /* Update is not supported. */
125 if (mp->mnt_flag & MNT_UPDATE)
126 return EOPNOTSUPP;
127
128 /* Find the lower vnode and lock it. */
129 error = pathbuf_copyin(args->la.target, &pb);
130 if (error) {
131 return error;
132 }
133 NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
134 if ((error = namei(&nd)) != 0) {
135 pathbuf_destroy(pb);
136 return error;
137 }
138 lowerrootvp = nd.ni_vp;
139 pathbuf_destroy(pb);
140
141 /* Create the mount point. */
142 nmp = kmem_zalloc(sizeof(struct null_mount), KM_SLEEP);
143 mp->mnt_data = nmp;
144 nmp->nullm_vfs = lowerrootvp->v_mount;
145 if (nmp->nullm_vfs->mnt_flag & MNT_LOCAL)
146 mp->mnt_flag |= MNT_LOCAL;
147
148 /*
149 * Make sure that the mount point is sufficiently initialized
150 * that the node create call will work.
151 */
152 vfs_getnewfsid(mp);
153
154 nmp->nullm_size = sizeof(struct null_node);
155 nmp->nullm_tag = VT_NULL;
156 nmp->nullm_bypass = layer_bypass;
157 nmp->nullm_vnodeop_p = null_vnodeop_p;
158
159 /* Setup a null node for root vnode. */
160 VOP_UNLOCK(lowerrootvp);
161 error = layer_node_create(mp, lowerrootvp, &vp);
162 if (error) {
163 vrele(lowerrootvp);
164 kmem_free(nmp, sizeof(struct null_mount));
165 return error;
166 }
167 /*
168 * Keep a held reference to the root vnode. It will be released on
169 * umount. Note: nullfs is MP-safe.
170 */
171 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
172 vp->v_vflag |= VV_ROOT;
173 nmp->nullm_rootvp = vp;
174 mp->mnt_iflag |= IMNT_MPSAFE;
175 VOP_UNLOCK(vp);
176
177 error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
178 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, curlwp);
179 return error;
180 }
181
182 int
183 nullfs_unmount(struct mount *mp, int mntflags)
184 {
185 struct null_mount *nmp = MOUNTTONULLMOUNT(mp);
186 struct vnode *null_rootvp = nmp->nullm_rootvp;
187 int error, flags = 0;
188
189 if (mntflags & MNT_FORCE)
190 flags |= FORCECLOSE;
191
192 if (null_rootvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
193 return EBUSY;
194
195 if ((error = vflush(mp, null_rootvp, flags)) != 0)
196 return error;
197
198 /* Eliminate all activity and release the vnode. */
199 vgone(null_rootvp);
200
201 /* Finally, destroy the mount point structures. */
202 kmem_free(mp->mnt_data, sizeof(struct null_mount));
203 mp->mnt_data = NULL;
204 return 0;
205 }
206
207 extern const struct vnodeopv_desc null_vnodeop_opv_desc;
208
209 const struct vnodeopv_desc * const nullfs_vnodeopv_descs[] = {
210 &null_vnodeop_opv_desc,
211 NULL,
212 };
213
214 struct vfsops nullfs_vfsops = {
215 .vfs_name = MOUNT_NULL,
216 .vfs_min_mount_data = sizeof (struct null_args),
217 .vfs_mount = nullfs_mount,
218 .vfs_start = layerfs_start,
219 .vfs_unmount = nullfs_unmount,
220 .vfs_root = layerfs_root,
221 .vfs_quotactl = layerfs_quotactl,
222 .vfs_statvfs = layerfs_statvfs,
223 .vfs_sync = layerfs_sync,
224 .vfs_loadvnode = layerfs_loadvnode,
225 .vfs_vget = layerfs_vget,
226 .vfs_fhtovp = layerfs_fhtovp,
227 .vfs_vptofh = layerfs_vptofh,
228 .vfs_init = layerfs_init,
229 .vfs_done = layerfs_done,
230 .vfs_snapshot = layerfs_snapshot,
231 .vfs_extattrctl = vfs_stdextattrctl,
232 .vfs_suspendctl = (void *)eopnotsupp,
233 .vfs_renamelock_enter = layerfs_renamelock_enter,
234 .vfs_renamelock_exit = layerfs_renamelock_exit,
235 .vfs_fsync = (void *)eopnotsupp,
236 .vfs_opv_descs = nullfs_vnodeopv_descs
237 };
238
239 static int
240 null_modcmd(modcmd_t cmd, void *arg)
241 {
242 int error;
243
244 switch (cmd) {
245 case MODULE_CMD_INIT:
246 error = vfs_attach(&nullfs_vfsops);
247 if (error != 0)
248 break;
249 sysctl_createv(&nullfs_sysctl_log, 0, NULL, NULL,
250 CTLFLAG_PERMANENT,
251 CTLTYPE_NODE, "null",
252 SYSCTL_DESCR("Loopback file system"),
253 NULL, 0, NULL, 0,
254 CTL_VFS, 9, CTL_EOL);
255 /*
256 * XXX the "9" above could be dynamic, thereby eliminating
257 * one more instance of the "number to vfs" mapping problem,
258 * but "9" is the order as taken from sys/mount.h
259 */
260 break;
261 case MODULE_CMD_FINI:
262 error = vfs_detach(&nullfs_vfsops);
263 if (error != 0)
264 break;
265 sysctl_teardown(&nullfs_sysctl_log);
266 break;
267 default:
268 error = ENOTTY;
269 break;
270 }
271 return error;
272 }
273