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