xref: /netbsd-src/sys/miscfs/umapfs/umap_vfsops.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: umap_vfsops.c,v 1.87 2012/03/13 18:40:58 elad 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.87 2012/03/13 18:40:58 elad Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysctl.h>
49 #include <sys/proc.h>
50 #include <sys/time.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/malloc.h>
55 #include <sys/kauth.h>
56 #include <sys/module.h>
57 
58 #include <miscfs/umapfs/umap.h>
59 #include <miscfs/genfs/layer_extern.h>
60 
61 MODULE(MODULE_CLASS_VFS, umap, "layerfs");
62 
63 VFS_PROTOS(umapfs);
64 
65 static struct sysctllog *umapfs_sysctl_log;
66 
67 /*
68  * Mount umap layer
69  */
70 int
71 umapfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
72 {
73 	struct lwp *l = curlwp;
74 	struct pathbuf *pb;
75 	struct nameidata nd;
76 	struct umap_args *args = data;
77 	struct vnode *lowerrootvp, *vp;
78 	struct umap_mount *amp;
79 	int error;
80 #ifdef UMAPFS_DIAGNOSTIC
81 	int i;
82 #endif
83 
84 	if (*data_len < sizeof *args)
85 		return EINVAL;
86 
87 	if (mp->mnt_flag & MNT_GETARGS) {
88 		amp = MOUNTTOUMAPMOUNT(mp);
89 		if (amp == NULL)
90 			return EIO;
91 		args->la.target = NULL;
92 		args->nentries = amp->info_nentries;
93 		args->gnentries = amp->info_gnentries;
94 		*data_len = sizeof *args;
95 		return 0;
96 	}
97 
98 	/* only for root */
99 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
100 	    KAUTH_REQ_SYSTEM_MOUNT_UMAP, NULL, NULL, NULL);
101 	if (error)
102 		return error;
103 
104 #ifdef UMAPFS_DIAGNOSTIC
105 	printf("umapfs_mount(mp = %p)\n", mp);
106 #endif
107 
108 	/*
109 	 * Update is not supported
110 	 */
111 	if (mp->mnt_flag & MNT_UPDATE)
112 		return EOPNOTSUPP;
113 
114 	/*
115 	 * Find lower node
116 	 */
117 	error = pathbuf_copyin(args->umap_target, &pb);
118 	if (error) {
119 		return error;
120 	}
121 	NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
122 	if ((error = namei(&nd)) != 0) {
123 		pathbuf_destroy(pb);
124 		return error;
125 	}
126 
127 	/*
128 	 * Sanity check on lower vnode
129 	 */
130 	lowerrootvp = nd.ni_vp;
131 	pathbuf_destroy(pb);
132 #ifdef UMAPFS_DIAGNOSTIC
133 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
134 #endif
135 
136 	if (lowerrootvp->v_type != VDIR) {
137 		vput(lowerrootvp);
138 		return (EINVAL);
139 	}
140 
141 #ifdef UMAPFS_DIAGNOSTIC
142 	printf("mp = %p\n", mp);
143 #endif
144 
145 	amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
146 				M_UFSMNT, M_WAITOK);	/* XXX */
147 	memset(amp, 0, sizeof(struct umap_mount));
148 
149 	mp->mnt_data = amp;
150 	amp->umapm_vfs = lowerrootvp->v_mount;
151 	if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
152 		mp->mnt_flag |= MNT_LOCAL;
153 
154 	/*
155 	 * Now copy in the number of entries and maps for umap mapping.
156 	 */
157 	if (args->nentries > MAPFILEENTRIES || args->gnentries > GMAPFILEENTRIES) {
158 		vput(lowerrootvp);
159 		return (error);
160 	}
161 
162 	amp->info_nentries = args->nentries;
163 	amp->info_gnentries = args->gnentries;
164 	error = copyin(args->mapdata, amp->info_mapdata,
165 	    2*sizeof(u_long)*args->nentries);
166 	if (error) {
167 		vput(lowerrootvp);
168 		return (error);
169 	}
170 
171 #ifdef UMAPFS_DIAGNOSTIC
172 	printf("umap_mount:nentries %d\n",args->nentries);
173 	for (i = 0; i < args->nentries; i++)
174 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
175 	 	    amp->info_mapdata[i][1]);
176 #endif
177 
178 	error = copyin(args->gmapdata, amp->info_gmapdata,
179 	    2*sizeof(u_long)*args->gnentries);
180 	if (error) {
181 		vput(lowerrootvp);
182 		return (error);
183 	}
184 
185 #ifdef UMAPFS_DIAGNOSTIC
186 	printf("umap_mount:gnentries %d\n",args->gnentries);
187 	for (i = 0; i < args->gnentries; i++)
188 		printf("\tgroup %ld maps to %ld\n",
189 		    amp->info_gmapdata[i][0],
190 	 	    amp->info_gmapdata[i][1]);
191 #endif
192 
193 	/*
194 	 * Make sure the mount point's sufficiently initialized
195 	 * that the node create call will work.
196 	 */
197 	vfs_getnewfsid(mp);
198 	amp->umapm_size = sizeof(struct umap_node);
199 	amp->umapm_tag = VT_UMAP;
200 	amp->umapm_bypass = umap_bypass;
201 	amp->umapm_alloc = layer_node_alloc;	/* the default alloc is fine */
202 	amp->umapm_vnodeop_p = umap_vnodeop_p;
203 	mutex_init(&amp->umapm_hashlock, MUTEX_DEFAULT, IPL_NONE);
204 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, true,
205 	    &amp->umapm_node_hash);
206 
207 
208 	/*
209 	 * fix up umap node for root vnode.
210 	 */
211 	error = layer_node_create(mp, lowerrootvp, &vp);
212 	/*
213 	 * Make sure the node alias worked
214 	 */
215 	if (error) {
216 		vput(lowerrootvp);
217 		hashdone(amp->umapm_node_hashtbl, HASH_LIST,
218 		    amp->umapm_node_hash);
219 		free(amp, M_UFSMNT);	/* XXX */
220 		return (error);
221 	}
222 	/*
223 	 * Unlock the node (either the lower or the alias)
224 	 */
225 	vp->v_vflag |= VV_ROOT;
226 	VOP_UNLOCK(vp);
227 
228 	/*
229 	 * Keep a held reference to the root vnode.
230 	 * It is vrele'd in umapfs_unmount.
231 	 */
232 	amp->umapm_rootvp = vp;
233 
234 	error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target,
235 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
236 #ifdef UMAPFS_DIAGNOSTIC
237 	printf("umapfs_mount: lower %s, alias at %s\n",
238 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
239 #endif
240 	return error;
241 }
242 
243 /*
244  * Free reference to umap layer
245  */
246 int
247 umapfs_unmount(struct mount *mp, int mntflags)
248 {
249 	struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp);
250 	struct vnode *rtvp = amp->umapm_rootvp;
251 	int error;
252 	int flags = 0;
253 
254 #ifdef UMAPFS_DIAGNOSTIC
255 	printf("umapfs_unmount(mp = %p)\n", mp);
256 #endif
257 
258 	if (mntflags & MNT_FORCE)
259 		flags |= FORCECLOSE;
260 
261 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
262 		return (EBUSY);
263 	if ((error = vflush(mp, rtvp, flags)) != 0)
264 		return (error);
265 
266 #ifdef UMAPFS_DIAGNOSTIC
267 	vprint("alias root of lower", rtvp);
268 #endif
269 	/*
270 	 * Blow it away for future re-use
271 	 */
272 	vgone(rtvp);
273 	/*
274 	 * Finally, throw away the umap_mount structure
275 	 */
276 	mutex_destroy(&amp->umapm_hashlock);
277 	hashdone(amp->umapm_node_hashtbl, HASH_LIST, amp->umapm_node_hash);
278 	free(amp, M_UFSMNT);	/* XXX */
279 	mp->mnt_data = NULL;
280 	return (0);
281 }
282 
283 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
284 
285 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
286 	&umapfs_vnodeop_opv_desc,
287 	NULL,
288 };
289 
290 struct vfsops umapfs_vfsops = {
291 	MOUNT_UMAP,
292 	sizeof (struct umap_args),
293 	umapfs_mount,
294 	layerfs_start,
295 	umapfs_unmount,
296 	layerfs_root,
297 	layerfs_quotactl,
298 	layerfs_statvfs,
299 	layerfs_sync,
300 	layerfs_vget,
301 	layerfs_fhtovp,
302 	layerfs_vptofh,
303 	layerfs_init,
304 	NULL,
305 	layerfs_done,
306 	NULL,				/* vfs_mountroot */
307 	layerfs_snapshot,
308 	vfs_stdextattrctl,
309 	(void *)eopnotsupp,		/* vfs_suspendctl */
310 	layerfs_renamelock_enter,
311 	layerfs_renamelock_exit,
312 	(void *)eopnotsupp,
313 	umapfs_vnodeopv_descs,
314 	0,				/* vfs_refcount */
315 	{ NULL, NULL },
316 };
317 
318 static int
319 umap_modcmd(modcmd_t cmd, void *arg)
320 {
321 	int error;
322 
323 	switch (cmd) {
324 	case MODULE_CMD_INIT:
325 		error = vfs_attach(&umapfs_vfsops);
326 		if (error != 0)
327 			break;
328 		sysctl_createv(&umapfs_sysctl_log, 0, NULL, NULL,
329 			       CTLFLAG_PERMANENT,
330 			       CTLTYPE_NODE, "vfs", NULL,
331 			       NULL, 0, NULL, 0,
332 			       CTL_VFS, CTL_EOL);
333 		sysctl_createv(&umapfs_sysctl_log, 0, NULL, NULL,
334 			       CTLFLAG_PERMANENT,
335 			       CTLTYPE_NODE, "umap",
336 			       SYSCTL_DESCR("UID/GID remapping file system"),
337 			       NULL, 0, NULL, 0,
338 			       CTL_VFS, 10, CTL_EOL);
339 		/*
340 		 * XXX the "10" above could be dynamic, thereby eliminating
341 		 * one more instance of the "number to vfs" mapping problem,
342 		 * but "10" is the order as taken from sys/mount.h
343 		 */
344 		break;
345 	case MODULE_CMD_FINI:
346 		error = vfs_detach(&umapfs_vfsops);
347 		if (error != 0)
348 			break;
349 		sysctl_teardown(&umapfs_sysctl_log);
350 		break;
351 	default:
352 		error = ENOTTY;
353 		break;
354 	}
355 
356 	return (error);
357 }
358