overlay_vfsops.c revision 1.9 1 1.9 lukem /* $NetBSD: overlay_vfsops.c,v 1.9 2001/11/10 13:33:43 lukem Exp $ */
2 1.1 wrstuden
3 1.1 wrstuden /*
4 1.1 wrstuden * Copyright (c) 1999, 2000 National Aeronautics & Space Administration
5 1.1 wrstuden * All rights reserved.
6 1.1 wrstuden *
7 1.1 wrstuden * This software was written by William Studenmund of the
8 1.7 wiz * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9 1.1 wrstuden *
10 1.1 wrstuden * Redistribution and use in source and binary forms, with or without
11 1.1 wrstuden * modification, are permitted provided that the following conditions
12 1.1 wrstuden * are met:
13 1.1 wrstuden * 1. Redistributions of source code must retain the above copyright
14 1.1 wrstuden * notice, this list of conditions and the following disclaimer.
15 1.1 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 wrstuden * notice, this list of conditions and the following disclaimer in the
17 1.1 wrstuden * documentation and/or other materials provided with the distribution.
18 1.2 soren * 3. Neither the name of the National Aeronautics & Space Administration
19 1.1 wrstuden * nor the names of its contributors may be used to endorse or promote
20 1.1 wrstuden * products derived from this software without specific prior written
21 1.1 wrstuden * permission.
22 1.1 wrstuden *
23 1.1 wrstuden * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24 1.1 wrstuden * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 wrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 wrstuden * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27 1.1 wrstuden * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 1.1 wrstuden * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 wrstuden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 wrstuden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 wrstuden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 wrstuden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 wrstuden * POSSIBILITY OF SUCH DAMAGE.
34 1.1 wrstuden */
35 1.1 wrstuden /*
36 1.1 wrstuden * Copyright (c) 1992, 1993, 1995
37 1.1 wrstuden * The Regents of the University of California. All rights reserved.
38 1.1 wrstuden *
39 1.1 wrstuden * This code is derived from software donated to Berkeley by
40 1.1 wrstuden * Jan-Simon Pendry.
41 1.1 wrstuden *
42 1.1 wrstuden * Redistribution and use in source and binary forms, with or without
43 1.1 wrstuden * modification, are permitted provided that the following conditions
44 1.1 wrstuden * are met:
45 1.1 wrstuden * 1. Redistributions of source code must retain the above copyright
46 1.1 wrstuden * notice, this list of conditions and the following disclaimer.
47 1.1 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 wrstuden * notice, this list of conditions and the following disclaimer in the
49 1.1 wrstuden * documentation and/or other materials provided with the distribution.
50 1.1 wrstuden * 3. All advertising materials mentioning features or use of this software
51 1.1 wrstuden * must display the following acknowledgement:
52 1.1 wrstuden * This product includes software developed by the University of
53 1.1 wrstuden * California, Berkeley and its contributors.
54 1.1 wrstuden * 4. Neither the name of the University nor the names of its contributors
55 1.1 wrstuden * may be used to endorse or promote products derived from this software
56 1.1 wrstuden * without specific prior written permission.
57 1.1 wrstuden *
58 1.1 wrstuden * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 wrstuden * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 wrstuden * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 wrstuden * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 wrstuden * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 wrstuden * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 wrstuden * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 wrstuden * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 wrstuden * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 wrstuden * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 wrstuden * SUCH DAMAGE.
69 1.1 wrstuden *
70 1.1 wrstuden * from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
71 1.1 wrstuden * from: @(#)lofs_vfsops.c 1.2 (Berkeley) 6/18/92
72 1.1 wrstuden * @(#)null_vfsops.c 8.7 (Berkeley) 5/14/95
73 1.1 wrstuden */
74 1.1 wrstuden
75 1.1 wrstuden /*
76 1.7 wiz * Overlay Layer
77 1.7 wiz * (See overlay_vnops.c for a description of what this does.)
78 1.1 wrstuden */
79 1.9 lukem
80 1.9 lukem #include <sys/cdefs.h>
81 1.9 lukem __KERNEL_RCSID(0, "$NetBSD: overlay_vfsops.c,v 1.9 2001/11/10 13:33:43 lukem Exp $");
82 1.1 wrstuden
83 1.1 wrstuden #include <sys/param.h>
84 1.1 wrstuden #include <sys/systm.h>
85 1.1 wrstuden #include <sys/time.h>
86 1.1 wrstuden #include <sys/proc.h>
87 1.1 wrstuden #include <sys/types.h>
88 1.1 wrstuden #include <sys/vnode.h>
89 1.1 wrstuden #include <sys/mount.h>
90 1.1 wrstuden #include <sys/namei.h>
91 1.1 wrstuden #include <sys/malloc.h>
92 1.1 wrstuden #include <miscfs/overlay/overlay.h>
93 1.1 wrstuden #include <miscfs/genfs/layer_extern.h>
94 1.1 wrstuden
95 1.1 wrstuden int ov_mount __P((struct mount *, const char *, void *,
96 1.1 wrstuden struct nameidata *, struct proc *));
97 1.1 wrstuden int ov_unmount __P((struct mount *, int, struct proc *));
98 1.1 wrstuden
99 1.1 wrstuden #define NOVERLAYNODECACHE 16
100 1.1 wrstuden
101 1.1 wrstuden /*
102 1.1 wrstuden * Mount overlay layer
103 1.1 wrstuden */
104 1.1 wrstuden int
105 1.1 wrstuden ov_mount(mp, path, data, ndp, p)
106 1.1 wrstuden struct mount *mp;
107 1.1 wrstuden const char *path;
108 1.1 wrstuden void *data;
109 1.1 wrstuden struct nameidata *ndp;
110 1.1 wrstuden struct proc *p;
111 1.1 wrstuden {
112 1.1 wrstuden int error = 0;
113 1.1 wrstuden struct overlay_args args;
114 1.1 wrstuden struct vnode *lowerrootvp, *vp;
115 1.1 wrstuden struct overlay_mount *nmp;
116 1.1 wrstuden struct layer_mount *lmp;
117 1.1 wrstuden size_t size;
118 1.1 wrstuden
119 1.1 wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
120 1.1 wrstuden printf("ov_mount(mp = %p)\n", mp);
121 1.1 wrstuden #endif
122 1.1 wrstuden
123 1.1 wrstuden /*
124 1.1 wrstuden * Get argument
125 1.1 wrstuden */
126 1.1 wrstuden error = copyin(data, (caddr_t)&args, sizeof(struct overlay_args));
127 1.1 wrstuden if (error)
128 1.1 wrstuden return (error);
129 1.1 wrstuden
130 1.1 wrstuden /*
131 1.1 wrstuden * Update only does export updating.
132 1.1 wrstuden */
133 1.1 wrstuden if (mp->mnt_flag & MNT_UPDATE) {
134 1.1 wrstuden lmp = MOUNTTOLAYERMOUNT(mp);
135 1.1 wrstuden if (args.ova_target == 0)
136 1.1 wrstuden return (vfs_export(mp, &lmp->layerm_export,
137 1.1 wrstuden &args.la.export));
138 1.1 wrstuden else
139 1.1 wrstuden return (EOPNOTSUPP);
140 1.1 wrstuden }
141 1.1 wrstuden
142 1.1 wrstuden /*
143 1.1 wrstuden * Find lower node
144 1.1 wrstuden */
145 1.1 wrstuden lowerrootvp = mp->mnt_vnodecovered;
146 1.1 wrstuden if ((error = vget(lowerrootvp, LK_EXCLUSIVE | LK_RETRY)))
147 1.1 wrstuden return (error);
148 1.1 wrstuden
149 1.1 wrstuden /*
150 1.1 wrstuden * First cut at fixing up upper mount point
151 1.1 wrstuden */
152 1.1 wrstuden nmp = (struct overlay_mount *) malloc(sizeof(struct overlay_mount),
153 1.1 wrstuden M_UFSMNT, M_WAITOK); /* XXX */
154 1.1 wrstuden memset((caddr_t)nmp, 0, sizeof(struct overlay_mount));
155 1.1 wrstuden
156 1.1 wrstuden mp->mnt_data = (qaddr_t) nmp;
157 1.1 wrstuden nmp->ovm_vfs = lowerrootvp->v_mount;
158 1.1 wrstuden if (nmp->ovm_vfs->mnt_flag & MNT_LOCAL)
159 1.1 wrstuden mp->mnt_flag |= MNT_LOCAL;
160 1.1 wrstuden
161 1.1 wrstuden /*
162 1.1 wrstuden * Make sure that the mount point is sufficiently initialized
163 1.1 wrstuden * that the node create call will work.
164 1.1 wrstuden */
165 1.4 assar vfs_getnewfsid(mp);
166 1.1 wrstuden
167 1.1 wrstuden nmp->ovm_size = sizeof (struct overlay_node);
168 1.1 wrstuden nmp->ovm_tag = VT_OVERLAY;
169 1.1 wrstuden nmp->ovm_bypass = layer_bypass;
170 1.1 wrstuden nmp->ovm_alloc = layer_node_alloc; /* the default alloc is fine */
171 1.1 wrstuden nmp->ovm_vnodeop_p = overlay_vnodeop_p;
172 1.1 wrstuden simple_lock_init(&nmp->ovm_hashlock);
173 1.5 ad nmp->ovm_node_hashtbl = hashinit(NOVERLAYNODECACHE, HASH_LIST, M_CACHE,
174 1.5 ad M_WAITOK, &nmp->ovm_node_hash);
175 1.1 wrstuden
176 1.1 wrstuden /*
177 1.1 wrstuden * Fix up overlay node for root vnode
178 1.1 wrstuden */
179 1.1 wrstuden error = layer_node_create(mp, lowerrootvp, &vp);
180 1.1 wrstuden /*
181 1.1 wrstuden * Make sure the fixup worked
182 1.1 wrstuden */
183 1.1 wrstuden if (error) {
184 1.1 wrstuden vput(lowerrootvp);
185 1.1 wrstuden free(nmp, M_UFSMNT); /* XXX */
186 1.1 wrstuden return (error);
187 1.1 wrstuden }
188 1.1 wrstuden /*
189 1.1 wrstuden * Unlock the node
190 1.1 wrstuden */
191 1.1 wrstuden VOP_UNLOCK(vp, 0);
192 1.1 wrstuden
193 1.1 wrstuden /*
194 1.1 wrstuden * Keep a held reference to the root vnode.
195 1.1 wrstuden * It is vrele'd in ov_unmount.
196 1.1 wrstuden */
197 1.1 wrstuden vp->v_flag |= VROOT;
198 1.1 wrstuden nmp->ovm_rootvp = vp;
199 1.1 wrstuden
200 1.1 wrstuden (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
201 1.1 wrstuden memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
202 1.1 wrstuden (void) copyinstr(args.la.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
203 1.1 wrstuden &size);
204 1.1 wrstuden memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
205 1.1 wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
206 1.1 wrstuden printf("ov_mount: lower %s, alias at %s\n",
207 1.1 wrstuden mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
208 1.1 wrstuden #endif
209 1.1 wrstuden return (0);
210 1.1 wrstuden }
211 1.1 wrstuden
212 1.1 wrstuden /*
213 1.1 wrstuden * Free reference to overlay layer
214 1.1 wrstuden */
215 1.1 wrstuden int
216 1.1 wrstuden ov_unmount(mp, mntflags, p)
217 1.1 wrstuden struct mount *mp;
218 1.1 wrstuden int mntflags;
219 1.1 wrstuden struct proc *p;
220 1.1 wrstuden {
221 1.1 wrstuden struct vnode *overlay_rootvp = MOUNTTOOVERLAYMOUNT(mp)->ovm_rootvp;
222 1.1 wrstuden int error;
223 1.1 wrstuden int flags = 0;
224 1.1 wrstuden
225 1.1 wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
226 1.1 wrstuden printf("ov_unmount(mp = %p)\n", mp);
227 1.1 wrstuden #endif
228 1.1 wrstuden
229 1.1 wrstuden if (mntflags & MNT_FORCE)
230 1.1 wrstuden flags |= FORCECLOSE;
231 1.1 wrstuden
232 1.1 wrstuden /*
233 1.1 wrstuden * Clear out buffer cache. I don't think we
234 1.1 wrstuden * ever get anything cached at this level at the
235 1.1 wrstuden * moment, but who knows...
236 1.1 wrstuden */
237 1.1 wrstuden #if 0
238 1.1 wrstuden mntflushbuf(mp, 0);
239 1.1 wrstuden if (mntinvalbuf(mp, 1))
240 1.1 wrstuden return (EBUSY);
241 1.1 wrstuden #endif
242 1.1 wrstuden if (overlay_rootvp->v_usecount > 1)
243 1.1 wrstuden return (EBUSY);
244 1.1 wrstuden if ((error = vflush(mp, overlay_rootvp, flags)) != 0)
245 1.1 wrstuden return (error);
246 1.1 wrstuden
247 1.1 wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
248 1.1 wrstuden vprint("alias root of lower", overlay_rootvp);
249 1.1 wrstuden #endif
250 1.1 wrstuden /*
251 1.1 wrstuden * Release reference on underlying root vnode
252 1.1 wrstuden */
253 1.1 wrstuden vrele(overlay_rootvp);
254 1.1 wrstuden /*
255 1.1 wrstuden * And blow it away for future re-use
256 1.1 wrstuden */
257 1.1 wrstuden vgone(overlay_rootvp);
258 1.1 wrstuden /*
259 1.1 wrstuden * Finally, throw away the overlay_mount structure
260 1.1 wrstuden */
261 1.1 wrstuden free(mp->mnt_data, M_UFSMNT); /* XXX */
262 1.1 wrstuden mp->mnt_data = 0;
263 1.1 wrstuden return 0;
264 1.1 wrstuden }
265 1.1 wrstuden
266 1.6 jdolecek extern const struct vnodeopv_desc overlay_vnodeop_opv_desc;
267 1.1 wrstuden
268 1.6 jdolecek const struct vnodeopv_desc * const ov_vnodeopv_descs[] = {
269 1.1 wrstuden &overlay_vnodeop_opv_desc,
270 1.1 wrstuden NULL,
271 1.1 wrstuden };
272 1.1 wrstuden
273 1.1 wrstuden struct vfsops overlay_vfsops = {
274 1.1 wrstuden MOUNT_OVERLAY,
275 1.1 wrstuden ov_mount,
276 1.1 wrstuden layerfs_start,
277 1.1 wrstuden ov_unmount,
278 1.1 wrstuden layerfs_root,
279 1.1 wrstuden layerfs_quotactl,
280 1.1 wrstuden layerfs_statfs,
281 1.1 wrstuden layerfs_sync,
282 1.1 wrstuden layerfs_vget,
283 1.1 wrstuden layerfs_fhtovp,
284 1.1 wrstuden layerfs_vptofh,
285 1.1 wrstuden layerfs_init,
286 1.8 chs NULL,
287 1.3 jdolecek layerfs_done,
288 1.1 wrstuden layerfs_sysctl,
289 1.1 wrstuden NULL, /* vfs_mountroot */
290 1.1 wrstuden layerfs_checkexp,
291 1.1 wrstuden ov_vnodeopv_descs,
292 1.1 wrstuden };
293