xref: /netbsd/sys/miscfs/umapfs/umap_vfsops.c (revision bf9ec67e)
1 /*	$NetBSD: umap_vfsops.c,v 1.33 2001/11/15 09:48:23 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * the UCLA Ficus project.
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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: @(#)null_vfsops.c       1.5 (Berkeley) 7/10/92
39  *	@(#)umap_vfsops.c	8.8 (Berkeley) 5/14/95
40  */
41 
42 /*
43  * Umap Layer
44  * (See mount_umap(8) for a description of this layer.)
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.33 2001/11/15 09:48:23 lukem Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/time.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/malloc.h>
58 #include <miscfs/umapfs/umap.h>
59 #include <miscfs/genfs/layer_extern.h>
60 
61 int	umapfs_mount __P((struct mount *, const char *, void *,
62 			  struct nameidata *, struct proc *));
63 int	umapfs_unmount __P((struct mount *, int, struct proc *));
64 
65 /*
66  * Mount umap layer
67  */
68 int
69 umapfs_mount(mp, path, data, ndp, p)
70 	struct mount *mp;
71 	const char *path;
72 	void *data;
73 	struct nameidata *ndp;
74 	struct proc *p;
75 {
76 	struct umap_args args;
77 	struct vnode *lowerrootvp, *vp;
78 	struct umap_mount *amp;
79 	size_t size;
80 	int error;
81 #ifdef UMAPFS_DIAGNOSTIC
82 	int i;
83 #endif
84 
85 	/* only for root */
86 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
87 		return error;
88 
89 #ifdef UMAPFS_DIAGNOSTIC
90 	printf("umapfs_mount(mp = %p)\n", mp);
91 #endif
92 
93 	/*
94 	 * Get argument
95 	 */
96 	error = copyin(data, (caddr_t)&args, sizeof(struct umap_args));
97 	if (error)
98 		return (error);
99 
100 	/*
101 	 * Update only does export updating.
102 	 */
103 	if (mp->mnt_flag & MNT_UPDATE) {
104 		amp = MOUNTTOUMAPMOUNT(mp);
105 		if (args.umap_target == 0)
106 			return (vfs_export(mp, &amp->umapm_export,
107 					&args.umap_export));
108 		else
109 			return (EOPNOTSUPP);
110 	}
111 
112 	/*
113 	 * Find lower node
114 	 */
115 	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF,
116 		UIO_USERSPACE, args.umap_target, p);
117 	if ((error = namei(ndp)) != 0)
118 		return (error);
119 
120 	/*
121 	 * Sanity check on lower vnode
122 	 */
123 	lowerrootvp = ndp->ni_vp;
124 #ifdef UMAPFS_DIAGNOSTIC
125 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
126 #endif
127 
128 	if (lowerrootvp->v_type != VDIR) {
129 		vput(lowerrootvp);
130 		return (EINVAL);
131 	}
132 
133 #ifdef UMAPFS_DIAGNOSTIC
134 	printf("mp = %p\n", mp);
135 #endif
136 
137 	amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
138 				M_UFSMNT, M_WAITOK);	/* XXX */
139 	memset((caddr_t)amp, 0, sizeof(struct umap_mount));
140 
141 	mp->mnt_data = (qaddr_t) amp;
142 	amp->umapm_vfs = lowerrootvp->v_mount;
143 	if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
144 		mp->mnt_flag |= MNT_LOCAL;
145 
146 	/*
147 	 * Now copy in the number of entries and maps for umap mapping.
148 	 */
149 	if (args.nentries > MAPFILEENTRIES || args.gnentries > GMAPFILEENTRIES) {
150 		vput(lowerrootvp);
151 		return (error);
152 	}
153 
154 	amp->info_nentries = args.nentries;
155 	amp->info_gnentries = args.gnentries;
156 	error = copyin(args.mapdata, (caddr_t)amp->info_mapdata,
157 	    2*sizeof(u_long)*args.nentries);
158 	if (error) {
159 		vput(lowerrootvp);
160 		return (error);
161 	}
162 
163 #ifdef UMAPFS_DIAGNOSTIC
164 	printf("umap_mount:nentries %d\n",args.nentries);
165 	for (i = 0; i < args.nentries; i++)
166 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
167 	 	    amp->info_mapdata[i][1]);
168 #endif
169 
170 	error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
171 	    2*sizeof(u_long)*args.gnentries);
172 	if (error) {
173 		vput(lowerrootvp);
174 		return (error);
175 	}
176 
177 #ifdef UMAPFS_DIAGNOSTIC
178 	printf("umap_mount:gnentries %d\n",args.gnentries);
179 	for (i = 0; i < args.gnentries; i++)
180 		printf("\tgroup %ld maps to %ld\n",
181 		    amp->info_gmapdata[i][0],
182 	 	    amp->info_gmapdata[i][1]);
183 #endif
184 
185 	/*
186 	 * Make sure the mount point's sufficiently initialized
187 	 * that the node create call will work.
188 	 */
189 	vfs_getnewfsid(mp);
190 	amp->umapm_size = sizeof(struct umap_node);
191 	amp->umapm_tag = VT_UMAP;
192 	amp->umapm_bypass = umap_bypass;
193 	amp->umapm_alloc = layer_node_alloc;	/* the default alloc is fine */
194 	amp->umapm_vnodeop_p = umap_vnodeop_p;
195 	simple_lock_init(&amp->umapm_hashlock);
196 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE,
197 	    M_WAITOK, &amp->umapm_node_hash);
198 
199 
200 	/*
201 	 * fix up umap node for root vnode.
202 	 */
203 	error = layer_node_create(mp, lowerrootvp, &vp);
204 	/*
205 	 * Make sure the node alias worked
206 	 */
207 	if (error) {
208 		vput(lowerrootvp);
209 		free(amp, M_UFSMNT);	/* XXX */
210 		return (error);
211 	}
212 	/*
213 	 * Unlock the node (either the lower or the alias)
214 	 */
215 	VOP_UNLOCK(vp, 0);
216 
217 	/*
218 	 * Keep a held reference to the root vnode.
219 	 * It is vrele'd in umapfs_unmount.
220 	 */
221 	vp->v_flag |= VROOT;
222 	amp->umapm_rootvp = vp;
223 
224 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
225 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
226 	(void) copyinstr(args.umap_target, mp->mnt_stat.f_mntfromname,
227 		MNAMELEN - 1, &size);
228 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
229 #ifdef UMAPFS_DIAGNOSTIC
230 	printf("umapfs_mount: lower %s, alias at %s\n",
231 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
232 #endif
233 	return (0);
234 }
235 
236 /*
237  * Free reference to umap layer
238  */
239 int
240 umapfs_unmount(mp, mntflags, p)
241 	struct mount *mp;
242 	int mntflags;
243 	struct proc *p;
244 {
245 	struct vnode *rootvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
246 	int error;
247 	int flags = 0;
248 
249 #ifdef UMAPFS_DIAGNOSTIC
250 	printf("umapfs_unmount(mp = %p)\n", mp);
251 #endif
252 
253 	if (mntflags & MNT_FORCE)
254 		flags |= FORCECLOSE;
255 
256 	/*
257 	 * Clear out buffer cache.  I don't think we
258 	 * ever get anything cached at this level at the
259 	 * moment, but who knows...
260 	 */
261 #ifdef notyet
262 	mntflushbuf(mp, 0);
263 	if (mntinvalbuf(mp, 1))
264 		return (EBUSY);
265 #endif
266 	if (rootvp->v_usecount > 1)
267 		return (EBUSY);
268 	if ((error = vflush(mp, rootvp, flags)) != 0)
269 		return (error);
270 
271 #ifdef UMAPFS_DIAGNOSTIC
272 	vprint("alias root of lower", rootvp);
273 #endif
274 	/*
275 	 * Release reference on underlying root vnode
276 	 */
277 	vrele(rootvp);
278 	/*
279 	 * And blow it away for future re-use
280 	 */
281 	vgone(rootvp);
282 	/*
283 	 * Finally, throw away the umap_mount structure
284 	 */
285 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
286 	mp->mnt_data = 0;
287 	return (0);
288 }
289 
290 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
291 
292 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
293 	&umapfs_vnodeop_opv_desc,
294 	NULL,
295 };
296 
297 struct vfsops umapfs_vfsops = {
298 	MOUNT_UMAP,
299 	umapfs_mount,
300 	layerfs_start,
301 	umapfs_unmount,
302 	layerfs_root,
303 	layerfs_quotactl,
304 	layerfs_statfs,
305 	layerfs_sync,
306 	layerfs_vget,
307 	layerfs_fhtovp,
308 	layerfs_vptofh,
309 	layerfs_init,
310 	NULL,
311 	layerfs_done,
312 	layerfs_sysctl,
313 	NULL,				/* vfs_mountroot */
314 	layerfs_checkexp,
315 	umapfs_vnodeopv_descs,
316 };
317