null_vfsops.c revision 1.77.12.1 1 1.77.12.1 sborrill /* $NetBSD: null_vfsops.c,v 1.77.12.1 2014/04/28 16:03:15 sborrill Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.24 wrstuden * Copyright (c) 1999 National Aeronautics & Space Administration
5 1.24 wrstuden * All rights reserved.
6 1.24 wrstuden *
7 1.24 wrstuden * This software was written by William Studenmund of the
8 1.30 wiz * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9 1.24 wrstuden *
10 1.24 wrstuden * Redistribution and use in source and binary forms, with or without
11 1.24 wrstuden * modification, are permitted provided that the following conditions
12 1.24 wrstuden * are met:
13 1.24 wrstuden * 1. Redistributions of source code must retain the above copyright
14 1.24 wrstuden * notice, this list of conditions and the following disclaimer.
15 1.24 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
16 1.24 wrstuden * notice, this list of conditions and the following disclaimer in the
17 1.24 wrstuden * documentation and/or other materials provided with the distribution.
18 1.25 soren * 3. Neither the name of the National Aeronautics & Space Administration
19 1.24 wrstuden * nor the names of its contributors may be used to endorse or promote
20 1.24 wrstuden * products derived from this software without specific prior written
21 1.24 wrstuden * permission.
22 1.24 wrstuden *
23 1.24 wrstuden * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24 1.24 wrstuden * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.24 wrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.24 wrstuden * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27 1.24 wrstuden * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 1.24 wrstuden * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.24 wrstuden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.24 wrstuden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.24 wrstuden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.24 wrstuden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.24 wrstuden * POSSIBILITY OF SUCH DAMAGE.
34 1.24 wrstuden */
35 1.24 wrstuden /*
36 1.19 fvdl * Copyright (c) 1992, 1993, 1995
37 1.1 mycroft * The Regents of the University of California. All rights reserved.
38 1.1 mycroft *
39 1.1 mycroft * This code is derived from software donated to Berkeley by
40 1.1 mycroft * Jan-Simon Pendry.
41 1.1 mycroft *
42 1.1 mycroft * Redistribution and use in source and binary forms, with or without
43 1.1 mycroft * modification, are permitted provided that the following conditions
44 1.1 mycroft * are met:
45 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
46 1.1 mycroft * notice, this list of conditions and the following disclaimer.
47 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
49 1.1 mycroft * documentation and/or other materials provided with the distribution.
50 1.43 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 mycroft * may be used to endorse or promote products derived from this software
52 1.1 mycroft * without specific prior written permission.
53 1.1 mycroft *
54 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 mycroft * SUCH DAMAGE.
65 1.1 mycroft *
66 1.1 mycroft * from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
67 1.1 mycroft * from: @(#)lofs_vfsops.c 1.2 (Berkeley) 6/18/92
68 1.19 fvdl * @(#)null_vfsops.c 8.7 (Berkeley) 5/14/95
69 1.1 mycroft */
70 1.1 mycroft
71 1.1 mycroft /*
72 1.1 mycroft * Null Layer
73 1.1 mycroft * (See null_vnops.c for a description of what this does.)
74 1.1 mycroft */
75 1.36 lukem
76 1.36 lukem #include <sys/cdefs.h>
77 1.77.12.1 sborrill __KERNEL_RCSID(0, "$NetBSD: null_vfsops.c,v 1.77.12.1 2014/04/28 16:03:15 sborrill Exp $");
78 1.1 mycroft
79 1.1 mycroft #include <sys/param.h>
80 1.1 mycroft #include <sys/systm.h>
81 1.44 atatat #include <sys/sysctl.h>
82 1.1 mycroft #include <sys/time.h>
83 1.19 fvdl #include <sys/proc.h>
84 1.1 mycroft #include <sys/vnode.h>
85 1.1 mycroft #include <sys/mount.h>
86 1.1 mycroft #include <sys/namei.h>
87 1.1 mycroft #include <sys/malloc.h>
88 1.76 rumble #include <sys/module.h>
89 1.33 enami
90 1.1 mycroft #include <miscfs/nullfs/null.h>
91 1.24 wrstuden #include <miscfs/genfs/layer_extern.h>
92 1.1 mycroft
93 1.76 rumble MODULE(MODULE_CLASS_VFS, nullfs, NULL);
94 1.76 rumble
95 1.68 pooka VFS_PROTOS(nullfs);
96 1.24 wrstuden
97 1.77 ad static struct sysctllog *nullfs_sysctl_log;
98 1.77 ad
99 1.1 mycroft /*
100 1.1 mycroft * Mount null layer
101 1.1 mycroft */
102 1.1 mycroft int
103 1.70 pooka nullfs_mount(mp, path, data, data_len)
104 1.1 mycroft struct mount *mp;
105 1.14 cgd const char *path;
106 1.14 cgd void *data;
107 1.65 dsl size_t *data_len;
108 1.1 mycroft {
109 1.70 pooka struct lwp *l = curlwp;
110 1.68 pooka struct nameidata nd;
111 1.65 dsl struct null_args *args = data;
112 1.1 mycroft struct vnode *lowerrootvp, *vp;
113 1.24 wrstuden struct null_mount *nmp;
114 1.24 wrstuden struct layer_mount *lmp;
115 1.33 enami int error = 0;
116 1.1 mycroft
117 1.1 mycroft #ifdef NULLFS_DIAGNOSTIC
118 1.16 christos printf("nullfs_mount(mp = %p)\n", mp);
119 1.1 mycroft #endif
120 1.1 mycroft
121 1.77.12.1 sborrill if (args == NULL)
122 1.77.12.1 sborrill return EINVAL;
123 1.65 dsl if (*data_len < sizeof *args)
124 1.65 dsl return EINVAL;
125 1.65 dsl
126 1.38 christos if (mp->mnt_flag & MNT_GETARGS) {
127 1.38 christos lmp = MOUNTTOLAYERMOUNT(mp);
128 1.38 christos if (lmp == NULL)
129 1.38 christos return EIO;
130 1.65 dsl args->la.target = NULL;
131 1.65 dsl *data_len = sizeof *args;
132 1.65 dsl return 0;
133 1.38 christos }
134 1.1 mycroft
135 1.1 mycroft /*
136 1.57 jmmv * Update is not supported
137 1.24 wrstuden */
138 1.57 jmmv if (mp->mnt_flag & MNT_UPDATE)
139 1.57 jmmv return EOPNOTSUPP;
140 1.24 wrstuden
141 1.24 wrstuden /*
142 1.1 mycroft * Find lower node
143 1.1 mycroft */
144 1.71 pooka NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, UIO_USERSPACE, args->la.target);
145 1.68 pooka if ((error = namei(&nd)) != 0)
146 1.1 mycroft return (error);
147 1.1 mycroft
148 1.1 mycroft /*
149 1.1 mycroft * Sanity check on lower vnode
150 1.1 mycroft */
151 1.68 pooka lowerrootvp = nd.ni_vp;
152 1.1 mycroft
153 1.24 wrstuden /*
154 1.24 wrstuden * First cut at fixing up upper mount point
155 1.24 wrstuden */
156 1.24 wrstuden nmp = (struct null_mount *) malloc(sizeof(struct null_mount),
157 1.33 enami M_UFSMNT, M_WAITOK); /* XXX */
158 1.48 jrf memset(nmp, 0, sizeof(struct null_mount));
159 1.24 wrstuden
160 1.37 soren mp->mnt_data = nmp;
161 1.24 wrstuden nmp->nullm_vfs = lowerrootvp->v_mount;
162 1.24 wrstuden if (nmp->nullm_vfs->mnt_flag & MNT_LOCAL)
163 1.24 wrstuden mp->mnt_flag |= MNT_LOCAL;
164 1.1 mycroft
165 1.1 mycroft /*
166 1.24 wrstuden * Make sure that the mount point is sufficiently initialized
167 1.24 wrstuden * that the node create call will work.
168 1.1 mycroft */
169 1.27 assar vfs_getnewfsid(mp);
170 1.24 wrstuden
171 1.33 enami nmp->nullm_size = sizeof(struct null_node);
172 1.24 wrstuden nmp->nullm_tag = VT_NULL;
173 1.24 wrstuden nmp->nullm_bypass = layer_bypass;
174 1.24 wrstuden nmp->nullm_alloc = layer_node_alloc; /* the default alloc is fine */
175 1.24 wrstuden nmp->nullm_vnodeop_p = null_vnodeop_p;
176 1.69 ad mutex_init(&nmp->nullm_hashlock, MUTEX_DEFAULT, IPL_NONE);
177 1.75 ad nmp->nullm_node_hashtbl = hashinit(desiredvnodes, HASH_LIST, true,
178 1.75 ad &nmp->nullm_node_hash);
179 1.1 mycroft
180 1.1 mycroft /*
181 1.24 wrstuden * Fix up null node for root vnode
182 1.1 mycroft */
183 1.24 wrstuden error = layer_node_create(mp, lowerrootvp, &vp);
184 1.1 mycroft /*
185 1.24 wrstuden * Make sure the fixup worked
186 1.1 mycroft */
187 1.1 mycroft if (error) {
188 1.24 wrstuden vput(lowerrootvp);
189 1.75 ad hashdone(nmp->nullm_node_hashtbl, HASH_LIST,
190 1.75 ad nmp->nullm_node_hash);
191 1.24 wrstuden free(nmp, M_UFSMNT); /* XXX */
192 1.1 mycroft return (error);
193 1.1 mycroft }
194 1.21 wrstuden /*
195 1.72 ad * Keep a held reference to the root vnode.
196 1.72 ad * It is vrele'd in nullfs_unmount.
197 1.21 wrstuden */
198 1.69 ad vp->v_vflag |= VV_ROOT;
199 1.72 ad nmp->nullm_rootvp = vp;
200 1.72 ad
201 1.72 ad /* We don't need kernel_lock. */
202 1.72 ad mp->mnt_iflag |= IMNT_MPSAFE;
203 1.1 mycroft
204 1.1 mycroft /*
205 1.72 ad * Unlock the node
206 1.1 mycroft */
207 1.72 ad VOP_UNLOCK(vp, 0);
208 1.1 mycroft
209 1.65 dsl error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
210 1.66 pooka UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
211 1.1 mycroft #ifdef NULLFS_DIAGNOSTIC
212 1.13 christos printf("nullfs_mount: lower %s, alias at %s\n",
213 1.12 christos mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
214 1.1 mycroft #endif
215 1.54 perry return error;
216 1.1 mycroft }
217 1.1 mycroft
218 1.1 mycroft /*
219 1.1 mycroft * Free reference to null layer
220 1.1 mycroft */
221 1.1 mycroft int
222 1.70 pooka nullfs_unmount(struct mount *mp, int mntflags)
223 1.1 mycroft {
224 1.34 enami struct null_mount *nmp = MOUNTTONULLMOUNT(mp);
225 1.34 enami struct vnode *null_rootvp = nmp->nullm_rootvp;
226 1.1 mycroft int error;
227 1.1 mycroft int flags = 0;
228 1.1 mycroft
229 1.1 mycroft #ifdef NULLFS_DIAGNOSTIC
230 1.16 christos printf("nullfs_unmount(mp = %p)\n", mp);
231 1.1 mycroft #endif
232 1.1 mycroft
233 1.19 fvdl if (mntflags & MNT_FORCE)
234 1.1 mycroft flags |= FORCECLOSE;
235 1.1 mycroft
236 1.64 pooka if (null_rootvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
237 1.1 mycroft return (EBUSY);
238 1.24 wrstuden if ((error = vflush(mp, null_rootvp, flags)) != 0)
239 1.1 mycroft return (error);
240 1.1 mycroft
241 1.1 mycroft #ifdef NULLFS_DIAGNOSTIC
242 1.24 wrstuden vprint("alias root of lower", null_rootvp);
243 1.33 enami #endif
244 1.1 mycroft /*
245 1.72 ad * Blow it away for future re-use
246 1.1 mycroft */
247 1.24 wrstuden vgone(null_rootvp);
248 1.33 enami
249 1.1 mycroft /*
250 1.1 mycroft * Finally, throw away the null_mount structure
251 1.1 mycroft */
252 1.75 ad hashdone(nmp->nullm_node_hashtbl, HASH_LIST, nmp->nullm_node_hash);
253 1.69 ad mutex_destroy(&nmp->nullm_hashlock);
254 1.1 mycroft free(mp->mnt_data, M_UFSMNT); /* XXX */
255 1.33 enami mp->mnt_data = NULL;
256 1.33 enami return (0);
257 1.1 mycroft }
258 1.1 mycroft
259 1.29 jdolecek extern const struct vnodeopv_desc null_vnodeop_opv_desc;
260 1.18 thorpej
261 1.29 jdolecek const struct vnodeopv_desc * const nullfs_vnodeopv_descs[] = {
262 1.19 fvdl &null_vnodeop_opv_desc,
263 1.18 thorpej NULL,
264 1.18 thorpej };
265 1.18 thorpej
266 1.17 thorpej struct vfsops nullfs_vfsops = {
267 1.1 mycroft MOUNT_NULL,
268 1.65 dsl sizeof (struct null_args),
269 1.1 mycroft nullfs_mount,
270 1.24 wrstuden layerfs_start,
271 1.1 mycroft nullfs_unmount,
272 1.24 wrstuden layerfs_root,
273 1.24 wrstuden layerfs_quotactl,
274 1.47 christos layerfs_statvfs,
275 1.24 wrstuden layerfs_sync,
276 1.24 wrstuden layerfs_vget,
277 1.24 wrstuden layerfs_fhtovp,
278 1.24 wrstuden layerfs_vptofh,
279 1.24 wrstuden layerfs_init,
280 1.32 chs NULL,
281 1.26 jdolecek layerfs_done,
282 1.18 thorpej NULL, /* vfs_mountroot */
283 1.51 wrstuden layerfs_snapshot,
284 1.53 thorpej vfs_stdextattrctl,
285 1.67 pooka (void *)eopnotsupp, /* vfs_suspendctl */
286 1.73 dholland layerfs_renamelock_enter,
287 1.73 dholland layerfs_renamelock_exit,
288 1.74 ad (void *)eopnotsupp,
289 1.18 thorpej nullfs_vnodeopv_descs,
290 1.59 christos 0,
291 1.59 christos { NULL, NULL },
292 1.1 mycroft };
293 1.76 rumble
294 1.76 rumble static int
295 1.76 rumble nullfs_modcmd(modcmd_t cmd, void *arg)
296 1.76 rumble {
297 1.77 ad int error;
298 1.76 rumble
299 1.76 rumble switch (cmd) {
300 1.76 rumble case MODULE_CMD_INIT:
301 1.77 ad error = vfs_attach(&nullfs_vfsops);
302 1.77 ad if (error != 0)
303 1.77 ad break;
304 1.77 ad sysctl_createv(&nullfs_sysctl_log, 0, NULL, NULL,
305 1.77 ad CTLFLAG_PERMANENT,
306 1.77 ad CTLTYPE_NODE, "vfs", NULL,
307 1.77 ad NULL, 0, NULL, 0,
308 1.77 ad CTL_VFS, CTL_EOL);
309 1.77 ad sysctl_createv(&nullfs_sysctl_log, 0, NULL, NULL,
310 1.77 ad CTLFLAG_PERMANENT,
311 1.77 ad CTLTYPE_NODE, "null",
312 1.77 ad SYSCTL_DESCR("Loopback file system"),
313 1.77 ad NULL, 0, NULL, 0,
314 1.77 ad CTL_VFS, 9, CTL_EOL);
315 1.77 ad /*
316 1.77 ad * XXX the "9" above could be dynamic, thereby eliminating
317 1.77 ad * one more instance of the "number to vfs" mapping problem,
318 1.77 ad * but "9" is the order as taken from sys/mount.h
319 1.77 ad */
320 1.77 ad break;
321 1.76 rumble case MODULE_CMD_FINI:
322 1.77 ad error = vfs_detach(&nullfs_vfsops);
323 1.77 ad if (error != 0)
324 1.77 ad break;
325 1.77 ad sysctl_teardown(&nullfs_sysctl_log);
326 1.77 ad break;
327 1.76 rumble default:
328 1.77 ad error = ENOTTY;
329 1.77 ad break;
330 1.76 rumble }
331 1.77 ad
332 1.77 ad return error;
333 1.76 rumble }
334