umap_vfsops.c revision 1.56.10.2 1 /* $NetBSD: umap_vfsops.c,v 1.56.10.2 2006/03/10 13:57:49 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.56.10.2 2006/03/10 13:57:49 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 <miscfs/umapfs/umap.h>
56 #include <miscfs/genfs/layer_extern.h>
57
58 int umapfs_mount(struct mount *, const char *, void *,
59 struct nameidata *, struct lwp *);
60 int umapfs_unmount(struct mount *, int, struct lwp *);
61
62 /*
63 * Mount umap layer
64 */
65 int
66 umapfs_mount(mp, path, data, ndp, l)
67 struct mount *mp;
68 const char *path;
69 void *data;
70 struct nameidata *ndp;
71 struct lwp *l;
72 {
73 struct umap_args args;
74 struct vnode *lowerrootvp, *vp;
75 struct umap_mount *amp;
76 struct proc *p = l->l_proc;
77 int error;
78 #ifdef UMAPFS_DIAGNOSTIC
79 int i;
80 #endif
81
82 if (mp->mnt_flag & MNT_GETARGS) {
83 amp = MOUNTTOUMAPMOUNT(mp);
84 if (amp == NULL)
85 return EIO;
86 args.la.target = NULL;
87 args.nentries = amp->info_nentries;
88 args.gnentries = amp->info_gnentries;
89 return copyout(&args, data, sizeof(args));
90 }
91
92 /* only for root */
93 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
94 &p->p_acflag)) != 0)
95 return error;
96
97 #ifdef UMAPFS_DIAGNOSTIC
98 printf("umapfs_mount(mp = %p)\n", mp);
99 #endif
100
101 /*
102 * Get argument
103 */
104 error = copyin(data, &args, sizeof(struct umap_args));
105 if (error)
106 return (error);
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(ndp, LOOKUP, FOLLOW|LOCKLEAF,
118 UIO_USERSPACE, args.umap_target, l);
119 if ((error = namei(ndp)) != 0)
120 return (error);
121
122 /*
123 * Sanity check on lower vnode
124 */
125 lowerrootvp = ndp->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 mp->mnt_leaf = lowerrootvp->v_mount->mnt_leaf;
145 amp->umapm_vfs = lowerrootvp->v_mount;
146 if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
147 mp->mnt_flag |= MNT_LOCAL;
148
149 /*
150 * Now copy in the number of entries and maps for umap mapping.
151 */
152 if (args.nentries > MAPFILEENTRIES || args.gnentries > GMAPFILEENTRIES) {
153 vput(lowerrootvp);
154 return (error);
155 }
156
157 amp->info_nentries = args.nentries;
158 amp->info_gnentries = args.gnentries;
159 error = copyin(args.mapdata, amp->info_mapdata,
160 2*sizeof(u_long)*args.nentries);
161 if (error) {
162 vput(lowerrootvp);
163 return (error);
164 }
165
166 #ifdef UMAPFS_DIAGNOSTIC
167 printf("umap_mount:nentries %d\n",args.nentries);
168 for (i = 0; i < args.nentries; i++)
169 printf(" %ld maps to %ld\n", amp->info_mapdata[i][0],
170 amp->info_mapdata[i][1]);
171 #endif
172
173 error = copyin(args.gmapdata, amp->info_gmapdata,
174 2*sizeof(u_long)*args.gnentries);
175 if (error) {
176 vput(lowerrootvp);
177 return (error);
178 }
179
180 #ifdef UMAPFS_DIAGNOSTIC
181 printf("umap_mount:gnentries %d\n",args.gnentries);
182 for (i = 0; i < args.gnentries; i++)
183 printf("\tgroup %ld maps to %ld\n",
184 amp->info_gmapdata[i][0],
185 amp->info_gmapdata[i][1]);
186 #endif
187
188 /*
189 * Make sure the mount point's sufficiently initialized
190 * that the node create call will work.
191 */
192 vfs_getnewfsid(mp);
193 amp->umapm_size = sizeof(struct umap_node);
194 amp->umapm_tag = VT_UMAP;
195 amp->umapm_bypass = umap_bypass;
196 amp->umapm_alloc = layer_node_alloc; /* the default alloc is fine */
197 amp->umapm_vnodeop_p = umap_vnodeop_p;
198 simple_lock_init(&->umapm_hashlock);
199 amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE,
200 M_WAITOK, &->umapm_node_hash);
201
202
203 /*
204 * fix up umap node for root vnode.
205 */
206 error = layer_node_create(mp, lowerrootvp, &vp);
207 /*
208 * Make sure the node alias worked
209 */
210 if (error) {
211 vput(lowerrootvp);
212 free(amp, M_UFSMNT); /* XXX */
213 return (error);
214 }
215 /*
216 * Unlock the node (either the lower or the alias)
217 */
218 VOP_UNLOCK(vp, 0);
219
220 /*
221 * Keep a held reference to the root vnode.
222 * It is vrele'd in umapfs_unmount.
223 */
224 vp->v_flag |= VROOT;
225 amp->umapm_rootvp = vp;
226
227 error = set_statvfs_info(path, UIO_USERSPACE, args.umap_target,
228 UIO_USERSPACE, mp, l);
229 #ifdef UMAPFS_DIAGNOSTIC
230 printf("umapfs_mount: lower %s, alias at %s\n",
231 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
232 #endif
233 return error;
234 }
235
236 /*
237 * Free reference to umap layer
238 */
239 int
240 umapfs_unmount(mp, mntflags, l)
241 struct mount *mp;
242 int mntflags;
243 struct lwp *l;
244 {
245 struct vnode *rtvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
246 int error;
247 int flags = 0;
248
249 #ifdef UMAPFS_DIAGNOSTIC
250 printf("umapfs_unmount(mp = %p)\n", mp);
251 #endif
252
253 if (mntflags & MNT_FORCE)
254 flags |= FORCECLOSE;
255
256 /*
257 * Clear out buffer cache. I don't think we
258 * ever get anything cached at this level at the
259 * moment, but who knows...
260 */
261 #ifdef notyet
262 mntflushbuf(mp, 0);
263 if (mntinvalbuf(mp, 1))
264 return (EBUSY);
265 #endif
266 if (rtvp->v_usecount > 1)
267 return (EBUSY);
268 if ((error = vflush(mp, rtvp, flags)) != 0)
269 return (error);
270
271 #ifdef UMAPFS_DIAGNOSTIC
272 vprint("alias root of lower", rtvp);
273 #endif
274 /*
275 * Release reference on underlying root vnode
276 */
277 vrele(rtvp);
278 /*
279 * And blow it away for future re-use
280 */
281 vgone(rtvp);
282 /*
283 * Finally, throw away the umap_mount structure
284 */
285 free(mp->mnt_data, M_UFSMNT); /* XXX */
286 mp->mnt_data = 0;
287 return (0);
288 }
289
290 SYSCTL_SETUP(sysctl_vfs_umap_setup, "sysctl vfs.umap subtree setup")
291 {
292
293 sysctl_createv(clog, 0, NULL, NULL,
294 CTLFLAG_PERMANENT,
295 CTLTYPE_NODE, "vfs", NULL,
296 NULL, 0, NULL, 0,
297 CTL_VFS, CTL_EOL);
298 sysctl_createv(clog, 0, NULL, NULL,
299 CTLFLAG_PERMANENT,
300 CTLTYPE_NODE, "umap",
301 SYSCTL_DESCR("UID/GID remapping file system"),
302 NULL, 0, NULL, 0,
303 CTL_VFS, 10, CTL_EOL);
304 /*
305 * XXX the "10" above could be dynamic, thereby eliminating
306 * one more instance of the "number to vfs" mapping problem,
307 * but "10" is the order as taken from sys/mount.h
308 */
309 }
310
311 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
312
313 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
314 &umapfs_vnodeop_opv_desc,
315 NULL,
316 };
317
318 struct vfsops umapfs_vfsops = {
319 MOUNT_UMAP,
320 umapfs_mount,
321 layerfs_start,
322 umapfs_unmount,
323 layerfs_root,
324 layerfs_quotactl,
325 layerfs_statvfs,
326 layerfs_sync,
327 layerfs_vget,
328 layerfs_fhtovp,
329 layerfs_vptofh,
330 layerfs_init,
331 NULL,
332 layerfs_done,
333 NULL, /* vfs_mountroot */
334 layerfs_snapshot,
335 vfs_stdextattrctl,
336 umapfs_vnodeopv_descs,
337 };
338 VFS_ATTACH(umapfs_vfsops);
339