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