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