xref: /netbsd-src/sys/miscfs/overlay/overlay_vfsops.c (revision 6dffe8d42bd46273f674d7ab834e7be9b1af990e)
1 /*	$NetBSD: overlay_vfsops.c,v 1.54 2008/12/05 13:05:38 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2000 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  * Copyright (c) 1992, 1993, 1995
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software donated to Berkeley by
40  * Jan-Simon Pendry.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
67  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
68  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
69  */
70 
71 /*
72  * Overlay Layer
73  * (See overlay_vnops.c for a description of what this does.)
74  */
75 
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: overlay_vfsops.c,v 1.54 2008/12/05 13:05:38 ad Exp $");
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/sysctl.h>
82 #include <sys/time.h>
83 #include <sys/proc.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 #include <miscfs/overlay/overlay.h>
90 #include <miscfs/genfs/layer_extern.h>
91 
92 MODULE(MODULE_CLASS_VFS, overlay, "layerfs");
93 
94 VFS_PROTOS(ov);
95 
96 static struct sysctllog *overlay_sysctl_log;
97 
98 #define	NOVERLAYNODECACHE	16
99 
100 /*
101  * Mount overlay layer
102  */
103 int
104 ov_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
105 {
106 	struct lwp *l = curlwp;
107 	int error = 0;
108 	struct overlay_args *args = data;
109 	struct vnode *lowerrootvp, *vp;
110 	struct overlay_mount *nmp;
111 	struct layer_mount *lmp;
112 
113 #ifdef OVERLAYFS_DIAGNOSTIC
114 	printf("ov_mount(mp = %p)\n", mp);
115 #endif
116 
117 	if (*data_len < sizeof *args)
118 		return EINVAL;
119 
120 	if (mp->mnt_flag & MNT_GETARGS) {
121 		lmp = MOUNTTOLAYERMOUNT(mp);
122 		if (lmp == NULL)
123 			return EIO;
124 		args->la.target = NULL;
125 		*data_len = sizeof *args;
126 		return 0;
127 	}
128 
129 	/*
130 	 * Update is not supported
131 	 */
132 	if (mp->mnt_flag & MNT_UPDATE)
133 		return EOPNOTSUPP;
134 
135 	/*
136 	 * Find lower node
137 	 */
138 	lowerrootvp = mp->mnt_vnodecovered;
139 	if ((error = vget(lowerrootvp, LK_EXCLUSIVE | LK_RETRY)))
140 		return (error);
141 
142 	/*
143 	 * First cut at fixing up upper mount point
144 	 */
145 	nmp = (struct overlay_mount *) malloc(sizeof(struct overlay_mount),
146 				M_UFSMNT, M_WAITOK);	/* XXX */
147 	memset(nmp, 0, sizeof(struct overlay_mount));
148 
149 	mp->mnt_data = nmp;
150 	nmp->ovm_vfs = lowerrootvp->v_mount;
151 	if (nmp->ovm_vfs->mnt_flag & MNT_LOCAL)
152 		mp->mnt_flag |= MNT_LOCAL;
153 
154 	/*
155 	 * Make sure that the mount point is sufficiently initialized
156 	 * that the node create call will work.
157 	 */
158 	vfs_getnewfsid(mp);
159 
160 	nmp->ovm_size = sizeof (struct overlay_node);
161 	nmp->ovm_tag = VT_OVERLAY;
162 	nmp->ovm_bypass = layer_bypass;
163 	nmp->ovm_alloc = layer_node_alloc;	/* the default alloc is fine */
164 	nmp->ovm_vnodeop_p = overlay_vnodeop_p;
165 	mutex_init(&nmp->ovm_hashlock, MUTEX_DEFAULT, IPL_NONE);
166 	nmp->ovm_node_hashtbl = hashinit(NOVERLAYNODECACHE, HASH_LIST, true,
167 	     &nmp->ovm_node_hash);
168 
169 	/*
170 	 * Fix up overlay node for root vnode
171 	 */
172 	error = layer_node_create(mp, lowerrootvp, &vp);
173 	/*
174 	 * Make sure the fixup worked
175 	 */
176 	if (error) {
177 		vput(lowerrootvp);
178 		hashdone(nmp->ovm_node_hashtbl, HASH_LIST, nmp->ovm_node_hash);
179 		free(nmp, M_UFSMNT);	/* XXX */
180 		return (error);
181 	}
182 	/*
183 	 * Unlock the node
184 	 */
185 	VOP_UNLOCK(vp, 0);
186 
187 	/*
188 	 * Keep a held reference to the root vnode.
189 	 * It is vrele'd in ov_unmount.
190 	 */
191 	vp->v_vflag |= VV_ROOT;
192 	nmp->ovm_rootvp = vp;
193 
194 	error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
195 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
196 #ifdef OVERLAYFS_DIAGNOSTIC
197 	printf("ov_mount: lower %s, alias at %s\n",
198 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
199 #endif
200 	return error;
201 }
202 
203 /*
204  * Free reference to overlay layer
205  */
206 int
207 ov_unmount(struct mount *mp, int mntflags)
208 {
209 	struct vnode *overlay_rootvp = MOUNTTOOVERLAYMOUNT(mp)->ovm_rootvp;
210 	struct overlay_mount *omp;
211 	int error;
212 	int flags = 0;
213 
214 #ifdef OVERLAYFS_DIAGNOSTIC
215 	printf("ov_unmount(mp = %p)\n", mp);
216 #endif
217 
218 	if (mntflags & MNT_FORCE)
219 		flags |= FORCECLOSE;
220 
221 	if (overlay_rootvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
222 		return (EBUSY);
223 	if ((error = vflush(mp, overlay_rootvp, flags)) != 0)
224 		return (error);
225 
226 #ifdef OVERLAYFS_DIAGNOSTIC
227 	vprint("alias root of lower", overlay_rootvp);
228 #endif
229 	/*
230 	 * Blow it away for future re-use
231 	 */
232 	vgone(overlay_rootvp);
233 	/*
234 	 * Finally, throw away the overlay_mount structure
235 	 */
236 	omp = mp->mnt_data;
237 	mutex_destroy(&omp->ovm_hashlock);
238 	hashdone(omp->ovm_node_hashtbl, HASH_LIST, omp->ovm_node_hash);
239 	free(omp, M_UFSMNT);	/* XXX */
240 	mp->mnt_data = NULL;
241 	return 0;
242 }
243 
244 extern const struct vnodeopv_desc overlay_vnodeop_opv_desc;
245 
246 const struct vnodeopv_desc * const ov_vnodeopv_descs[] = {
247 	&overlay_vnodeop_opv_desc,
248 	NULL,
249 };
250 
251 struct vfsops overlay_vfsops = {
252 	MOUNT_OVERLAY,
253 	sizeof (struct overlay_args),
254 	ov_mount,
255 	layerfs_start,
256 	ov_unmount,
257 	layerfs_root,
258 	layerfs_quotactl,
259 	layerfs_statvfs,
260 	layerfs_sync,
261 	layerfs_vget,
262 	layerfs_fhtovp,
263 	layerfs_vptofh,
264 	layerfs_init,
265 	NULL,
266 	layerfs_done,
267 	NULL,				/* vfs_mountroot */
268 	layerfs_snapshot,
269 	vfs_stdextattrctl,
270 	(void *)eopnotsupp,		/* vfs_suspendctl */
271 	layerfs_renamelock_enter,
272 	layerfs_renamelock_exit,
273 	(void *)eopnotsupp,
274 	ov_vnodeopv_descs,
275 	0,
276 	{ NULL, NULL },
277 };
278 
279 static int
280 overlay_modcmd(modcmd_t cmd, void *arg)
281 {
282 	int error;
283 
284 	switch (cmd) {
285 	case MODULE_CMD_INIT:
286 		error = vfs_attach(&overlay_vfsops);
287 		if (error != 0)
288 			break;
289 		sysctl_createv(&overlay_sysctl_log, 0, NULL, NULL,
290 			       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
291 			       NULL, 0, NULL, 0,
292 			       CTL_VFS, CTL_EOL);
293 		sysctl_createv(&overlay_sysctl_log, 0, NULL, NULL,
294 			       CTLFLAG_PERMANENT, CTLTYPE_NODE, "overlay",
295 			       SYSCTL_DESCR("Overlay file system"),
296 			       NULL, 0, NULL, 0,
297 			       CTL_VFS, CTL_CREATE, CTL_EOL);
298 		break;
299 	case MODULE_CMD_FINI:
300 		error = vfs_detach(&overlay_vfsops);
301 		if (error != 0)
302 			break;
303 		sysctl_teardown(&overlay_sysctl_log);
304 		break;
305 	default:
306 		error = ENOTTY;
307 		break;
308 	}
309 
310 	return (error);
311 }
312