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