umap_vfsops.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1992, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * the UCLA Ficus project.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)umap_vfsops.c 8.8 (Berkeley) 5/14/95
37 *
38 * @(#)null_vfsops.c 1.5 (Berkeley) 7/10/92
39 */
40
41 /*
42 * Umap Layer
43 * (See mount_umap(8) for a description of this layer.)
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/time.h>
50 #include <sys/types.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
57 /*
58 * Mount umap layer
59 */
60 int
61 umapfs_mount(mp, path, data, ndp, p)
62 struct mount *mp;
63 char *path;
64 caddr_t data;
65 struct nameidata *ndp;
66 struct proc *p;
67 {
68 struct umap_args args;
69 struct vnode *lowerrootvp, *vp;
70 struct vnode *umapm_rootvp;
71 struct umap_mount *amp;
72 u_int size;
73 int error;
74
75 #ifdef UMAPFS_DIAGNOSTIC
76 printf("umapfs_mount(mp = %x)\n", mp);
77 #endif
78
79 /*
80 * Update is a no-op
81 */
82 if (mp->mnt_flag & MNT_UPDATE) {
83 return (EOPNOTSUPP);
84 /* return (VFS_MOUNT(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, path, data, ndp, p));*/
85 }
86
87 /*
88 * Get argument
89 */
90 if (error = copyin(data, (caddr_t)&args, sizeof(struct umap_args)))
91 return (error);
92
93 /*
94 * Find lower node
95 */
96 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
97 UIO_USERSPACE, args.target, p);
98 if (error = namei(ndp))
99 return (error);
100
101 /*
102 * Sanity check on lower vnode
103 */
104 lowerrootvp = ndp->ni_vp;
105 #ifdef UMAPFS_DIAGNOSTIC
106 printf("vp = %x, check for VDIR...\n", lowerrootvp);
107 #endif
108 vrele(ndp->ni_dvp);
109 ndp->ni_dvp = 0;
110
111 if (lowerrootvp->v_type != VDIR) {
112 vput(lowerrootvp);
113 return (EINVAL);
114 }
115
116 #ifdef UMAPFS_DIAGNOSTIC
117 printf("mp = %x\n", mp);
118 #endif
119
120 amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
121 M_UFSMNT, M_WAITOK); /* XXX */
122
123 /*
124 * Save reference to underlying FS
125 */
126 amp->umapm_vfs = lowerrootvp->v_mount;
127
128 /*
129 * Now copy in the number of entries and maps for umap mapping.
130 */
131 amp->info_nentries = args.nentries;
132 amp->info_gnentries = args.gnentries;
133 error = copyin(args.mapdata, (caddr_t)amp->info_mapdata,
134 2*sizeof(u_long)*args.nentries);
135 if (error)
136 return (error);
137
138 #ifdef UMAP_DIAGNOSTIC
139 printf("umap_mount:nentries %d\n",args.nentries);
140 for (i = 0; i < args.nentries; i++)
141 printf(" %d maps to %d\n", amp->info_mapdata[i][0],
142 amp->info_mapdata[i][1]);
143 #endif
144
145 error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
146 2*sizeof(u_long)*args.nentries);
147 if (error)
148 return (error);
149
150 #ifdef UMAP_DIAGNOSTIC
151 printf("umap_mount:gnentries %d\n",args.gnentries);
152 for (i = 0; i < args.gnentries; i++)
153 printf(" group %d maps to %d\n",
154 amp->info_gmapdata[i][0],
155 amp->info_gmapdata[i][1]);
156 #endif
157
158
159 /*
160 * Save reference. Each mount also holds
161 * a reference on the root vnode.
162 */
163 error = umap_node_create(mp, lowerrootvp, &vp);
164 /*
165 * Unlock the node (either the lower or the alias)
166 */
167 VOP_UNLOCK(vp, 0, p);
168 /*
169 * Make sure the node alias worked
170 */
171 if (error) {
172 vrele(lowerrootvp);
173 free(amp, M_UFSMNT); /* XXX */
174 return (error);
175 }
176
177 /*
178 * Keep a held reference to the root vnode.
179 * It is vrele'd in umapfs_unmount.
180 */
181 umapm_rootvp = vp;
182 umapm_rootvp->v_flag |= VROOT;
183 amp->umapm_rootvp = umapm_rootvp;
184 if (UMAPVPTOLOWERVP(umapm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
185 mp->mnt_flag |= MNT_LOCAL;
186 mp->mnt_data = (qaddr_t) amp;
187 vfs_getnewfsid(mp);
188
189 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
190 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
191 (void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
192 &size);
193 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
194 #ifdef UMAPFS_DIAGNOSTIC
195 printf("umapfs_mount: lower %s, alias at %s\n",
196 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
197 #endif
198 return (0);
199 }
200
201 /*
202 * VFS start. Nothing needed here - the start routine
203 * on the underlying filesystem will have been called
204 * when that filesystem was mounted.
205 */
206 int
207 umapfs_start(mp, flags, p)
208 struct mount *mp;
209 int flags;
210 struct proc *p;
211 {
212 return (0);
213 /* return (VFS_START(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, flags, p)); */
214 }
215
216 /*
217 * Free reference to umap layer
218 */
219 int
220 umapfs_unmount(mp, mntflags, p)
221 struct mount *mp;
222 int mntflags;
223 struct proc *p;
224 {
225 struct vnode *umapm_rootvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
226 int error;
227 int flags = 0;
228
229 #ifdef UMAPFS_DIAGNOSTIC
230 printf("umapfs_unmount(mp = %x)\n", mp);
231 #endif
232
233 if (mntflags & MNT_FORCE)
234 flags |= FORCECLOSE;
235
236 /*
237 * Clear out buffer cache. I don't think we
238 * ever get anything cached at this level at the
239 * moment, but who knows...
240 */
241 #ifdef notyet
242 mntflushbuf(mp, 0);
243 if (mntinvalbuf(mp, 1))
244 return (EBUSY);
245 #endif
246 if (umapm_rootvp->v_usecount > 1)
247 return (EBUSY);
248 if (error = vflush(mp, umapm_rootvp, flags))
249 return (error);
250
251 #ifdef UMAPFS_DIAGNOSTIC
252 vprint("alias root of lower", umapm_rootvp);
253 #endif
254 /*
255 * Release reference on underlying root vnode
256 */
257 vrele(umapm_rootvp);
258 /*
259 * And blow it away for future re-use
260 */
261 vgone(umapm_rootvp);
262 /*
263 * Finally, throw away the umap_mount structure
264 */
265 free(mp->mnt_data, M_UFSMNT); /* XXX */
266 mp->mnt_data = 0;
267 return (0);
268 }
269
270 int
271 umapfs_root(mp, vpp)
272 struct mount *mp;
273 struct vnode **vpp;
274 {
275 struct proc *p = curproc; /* XXX */
276 struct vnode *vp;
277
278 #ifdef UMAPFS_DIAGNOSTIC
279 printf("umapfs_root(mp = %x, vp = %x->%x)\n", mp,
280 MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
281 UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp)
282 );
283 #endif
284
285 /*
286 * Return locked reference to root.
287 */
288 vp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
289 VREF(vp);
290 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
291 *vpp = vp;
292 return (0);
293 }
294
295 int
296 umapfs_quotactl(mp, cmd, uid, arg, p)
297 struct mount *mp;
298 int cmd;
299 uid_t uid;
300 caddr_t arg;
301 struct proc *p;
302 {
303 return (VFS_QUOTACTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, uid, arg, p));
304 }
305
306 int
307 umapfs_statfs(mp, sbp, p)
308 struct mount *mp;
309 struct statfs *sbp;
310 struct proc *p;
311 {
312 int error;
313 struct statfs mstat;
314
315 #ifdef UMAPFS_DIAGNOSTIC
316 printf("umapfs_statfs(mp = %x, vp = %x->%x)\n", mp,
317 MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
318 UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp)
319 );
320 #endif
321
322 bzero(&mstat, sizeof(mstat));
323
324 error = VFS_STATFS(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, &mstat, p);
325 if (error)
326 return (error);
327
328 /* now copy across the "interesting" information and fake the rest */
329 sbp->f_type = mstat.f_type;
330 sbp->f_flags = mstat.f_flags;
331 sbp->f_bsize = mstat.f_bsize;
332 sbp->f_iosize = mstat.f_iosize;
333 sbp->f_blocks = mstat.f_blocks;
334 sbp->f_bfree = mstat.f_bfree;
335 sbp->f_bavail = mstat.f_bavail;
336 sbp->f_files = mstat.f_files;
337 sbp->f_ffree = mstat.f_ffree;
338 if (sbp != &mp->mnt_stat) {
339 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
340 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
341 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
342 }
343 return (0);
344 }
345
346 int
347 umapfs_sync(mp, waitfor, cred, p)
348 struct mount *mp;
349 int waitfor;
350 struct ucred *cred;
351 struct proc *p;
352 {
353 /*
354 * XXX - Assumes no data cached at umap layer.
355 */
356 return (0);
357 }
358
359 int
360 umapfs_vget(mp, ino, vpp)
361 struct mount *mp;
362 ino_t ino;
363 struct vnode **vpp;
364 {
365
366 return (VFS_VGET(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, ino, vpp));
367 }
368
369 int
370 umapfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
371 struct mount *mp;
372 struct fid *fidp;
373 struct mbuf *nam;
374 struct vnode **vpp;
375 int *exflagsp;
376 struct ucred**credanonp;
377 {
378
379 return (VFS_FHTOVP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, fidp, nam, vpp, exflagsp,credanonp));
380 }
381
382 int
383 umapfs_vptofh(vp, fhp)
384 struct vnode *vp;
385 struct fid *fhp;
386 {
387 return (VFS_VPTOFH(UMAPVPTOLOWERVP(vp), fhp));
388 }
389
390 int umapfs_init __P((struct vfsconf *));
391 #define umapfs_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
392 size_t, struct proc *)))eopnotsupp)
393
394 struct vfsops umap_vfsops = {
395 umapfs_mount,
396 umapfs_start,
397 umapfs_unmount,
398 umapfs_root,
399 umapfs_quotactl,
400 umapfs_statfs,
401 umapfs_sync,
402 umapfs_vget,
403 umapfs_fhtovp,
404 umapfs_vptofh,
405 umapfs_init,
406 umapfs_sysctl,
407 };
408