Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / vfs / umapfs / umap_vfsops.c
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  * $FreeBSD: src/sys/miscfs/umapfs/umap_vfsops.c,v 1.31.2.2 2001/09/11 09:49:53 kris Exp $
39  * $DragonFly: src/sys/vfs/umapfs/Attic/umap_vfsops.c,v 1.2 2003/06/17 04:28:43 dillon Exp $
40  */
41
42 /*
43  * Umap Layer
44  * (See mount_umap(8) for a description of this layer.)
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.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 <vm/vm_zone.h>
57
58 static MALLOC_DEFINE(M_UMAPFSMNT, "UMAP mount", "UMAP mount structure");
59
60 static int      umapfs_fhtovp __P((struct mount *mp, struct fid *fidp,
61                                    struct vnode **vpp));
62 static int      umapfs_checkexp __P((struct mount *mp, struct sockaddr *nam,
63                                     int *extflagsp, struct ucred **credanonp));
64 static int      umapfs_mount __P((struct mount *mp, char *path, caddr_t data,
65                                   struct nameidata *ndp, struct proc *p));
66 static int      umapfs_quotactl __P((struct mount *mp, int cmd, uid_t uid,
67                                      caddr_t arg, struct proc *p));
68 static int      umapfs_root __P((struct mount *mp, struct vnode **vpp));
69 static int      umapfs_start __P((struct mount *mp, int flags, struct proc *p));
70 static int      umapfs_statfs __P((struct mount *mp, struct statfs *sbp,
71                                    struct proc *p));
72 static int      umapfs_sync __P((struct mount *mp, int waitfor,
73                                  struct ucred *cred, struct proc *p));
74 static int      umapfs_unmount __P((struct mount *mp, int mntflags,
75                                     struct proc *p));
76 static int      umapfs_vget __P((struct mount *mp, ino_t ino,
77                                  struct vnode **vpp));
78 static int      umapfs_vptofh __P((struct vnode *vp, struct fid *fhp));
79 static int      umapfs_extattrctl __P((struct mount *mp, int cmd,
80                                        const char *attrname, caddr_t arg,
81                                        struct proc *p));
82
83 /*
84  * Mount umap layer
85  */
86 static int
87 umapfs_mount(mp, path, data, ndp, p)
88         struct mount *mp;
89         char *path;
90         caddr_t data;
91         struct nameidata *ndp;
92         struct proc *p;
93 {
94         struct umap_args args;
95         struct vnode *lowerrootvp, *vp;
96         struct vnode *umapm_rootvp;
97         struct umap_mount *amp;
98         u_int size;
99         int error;
100 #ifdef DEBUG
101         int     i;
102 #endif
103
104         /*
105          * Only for root
106          */
107         if ((error = suser(p)) != 0)
108                 return (error);
109
110 #ifdef DEBUG
111         printf("umapfs_mount(mp = %p)\n", (void *)mp);
112 #endif
113
114         /*
115          * Update is a no-op
116          */
117         if (mp->mnt_flag & MNT_UPDATE) {
118                 return (EOPNOTSUPP);
119                 /* return (VFS_MOUNT(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, path, data, ndp, p));*/
120         }
121
122         /*
123          * Get argument
124          */
125         error = copyin(data, (caddr_t)&args, sizeof(struct umap_args));
126         if (error)
127                 return (error);
128
129         /*
130          * Find lower node
131          */
132         NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
133                 UIO_USERSPACE, args.target, p);
134         error = namei(ndp);
135         if (error)
136                 return (error);
137         NDFREE(ndp, NDF_ONLY_PNBUF);
138
139         /*
140          * Sanity check on lower vnode
141          */
142         lowerrootvp = ndp->ni_vp;
143 #ifdef DEBUG
144         printf("vp = %p, check for VDIR...\n", (void *)lowerrootvp);
145 #endif
146         vrele(ndp->ni_dvp);
147         ndp->ni_dvp = 0;
148
149         if (lowerrootvp->v_type != VDIR) {
150                 vput(lowerrootvp);
151                 return (EINVAL);
152         }
153
154 #ifdef DEBUG
155         printf("mp = %p\n", (void *)mp);
156 #endif
157
158         amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
159                                 M_UMAPFSMNT, M_WAITOK); /* XXX */
160
161         /*
162          * Save reference to underlying FS
163          */
164         amp->umapm_vfs = lowerrootvp->v_mount;
165
166         /*
167          * Now copy in the number of entries and maps for umap mapping.
168          */
169         if (args.nentries > MAPFILEENTRIES || args.gnentries >
170             GMAPFILEENTRIES) {
171                 vput(lowerrootvp);
172                 return (error);
173         }
174
175         amp->info_nentries = args.nentries;
176         amp->info_gnentries = args.gnentries;
177         error = copyin(args.mapdata, (caddr_t)amp->info_mapdata,
178             2*sizeof(u_long)*args.nentries);
179         if (error)
180                 return (error);
181
182 #ifdef DEBUG
183         printf("umap_mount:nentries %d\n",args.nentries);
184         for (i = 0; i < args.nentries; i++)
185                 printf("   %lu maps to %lu\n", amp->info_mapdata[i][0],
186                     amp->info_mapdata[i][1]);
187 #endif
188
189         error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
190             2*sizeof(u_long)*args.gnentries);
191         if (error)
192                 return (error);
193
194 #ifdef DEBUG
195         printf("umap_mount:gnentries %d\n",args.gnentries);
196         for (i = 0; i < args.gnentries; i++)
197                 printf("        group %lu maps to %lu\n",
198                     amp->info_gmapdata[i][0],
199                     amp->info_gmapdata[i][1]);
200 #endif
201
202
203         /*
204          * Save reference.  Each mount also holds
205          * a reference on the root vnode.
206          */
207         error = umap_node_create(mp, lowerrootvp, &vp);
208         /*
209          * Unlock the node (either the lower or the alias)
210          */
211         VOP_UNLOCK(vp, 0, p);
212         /*
213          * Make sure the node alias worked
214          */
215         if (error) {
216                 vrele(lowerrootvp);
217                 free(amp, M_UMAPFSMNT); /* XXX */
218                 return (error);
219         }
220
221         /*
222          * Keep a held reference to the root vnode.
223          * It is vrele'd in umapfs_unmount.
224          */
225         umapm_rootvp = vp;
226         umapm_rootvp->v_flag |= VROOT;
227         amp->umapm_rootvp = umapm_rootvp;
228         if (UMAPVPTOLOWERVP(umapm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
229                 mp->mnt_flag |= MNT_LOCAL;
230         mp->mnt_data = (qaddr_t) amp;
231         vfs_getnewfsid(mp);
232
233         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
234         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
235         (void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
236             &size);
237         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
238         (void)umapfs_statfs(mp, &mp->mnt_stat, p);
239 #ifdef DEBUG
240         printf("umapfs_mount: lower %s, alias at %s\n",
241                 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
242 #endif
243         return (0);
244 }
245
246 /*
247  * VFS start.  Nothing needed here - the start routine
248  * on the underlying filesystem will have been called
249  * when that filesystem was mounted.
250  */
251 static int
252 umapfs_start(mp, flags, p)
253         struct mount *mp;
254         int flags;
255         struct proc *p;
256 {
257         return (0);
258         /* return (VFS_START(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, flags, p)); */
259 }
260
261 /*
262  * Free reference to umap layer
263  */
264 static int
265 umapfs_unmount(mp, mntflags, p)
266         struct mount *mp;
267         int mntflags;
268         struct proc *p;
269 {
270         int error;
271         int flags = 0;
272
273 #ifdef DEBUG
274         printf("umapfs_unmount(mp = %p)\n", (void *)mp);
275 #endif
276
277         if (mntflags & MNT_FORCE)
278                 flags |= FORCECLOSE;
279
280         /*
281          * Clear out buffer cache.  I don't think we
282          * ever get anything cached at this level at the
283          * moment, but who knows...
284          */
285 #ifdef notyet
286         mntflushbuf(mp, 0);
287         if (mntinvalbuf(mp, 1))
288                 return (EBUSY);
289 #endif
290         /* There is 1 extra root vnode reference (umapm_rootvp). */
291         error = vflush(mp, 1, flags);
292         if (error)
293                 return (error);
294
295         /*
296          * Finally, throw away the umap_mount structure
297          */
298         free(mp->mnt_data, M_UMAPFSMNT);        /* XXX */
299         mp->mnt_data = 0;
300         return (0);
301 }
302
303 static int
304 umapfs_root(mp, vpp)
305         struct mount *mp;
306         struct vnode **vpp;
307 {
308         struct proc *p = curproc;       /* XXX */
309         struct vnode *vp;
310
311 #ifdef DEBUG
312         printf("umapfs_root(mp = %p, vp = %p->%p)\n",
313             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
314             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
315 #endif
316
317         /*
318          * Return locked reference to root.
319          */
320         vp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
321         VREF(vp);
322         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
323         *vpp = vp;
324         return (0);
325 }
326
327 static int
328 umapfs_quotactl(mp, cmd, uid, arg, p)
329         struct mount *mp;
330         int cmd;
331         uid_t uid;
332         caddr_t arg;
333         struct proc *p;
334 {
335         return (VFS_QUOTACTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, uid, arg, p));
336 }
337
338 static int
339 umapfs_statfs(mp, sbp, p)
340         struct mount *mp;
341         struct statfs *sbp;
342         struct proc *p;
343 {
344         int error;
345         struct statfs mstat;
346
347 #ifdef DEBUG
348         printf("umapfs_statfs(mp = %p, vp = %p->%p)\n",
349             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
350             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
351 #endif
352
353         bzero(&mstat, sizeof(mstat));
354
355         error = VFS_STATFS(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, &mstat, p);
356         if (error)
357                 return (error);
358
359         /* now copy across the "interesting" information and fake the rest */
360         sbp->f_type = mstat.f_type;
361         sbp->f_flags = mstat.f_flags;
362         sbp->f_bsize = mstat.f_bsize;
363         sbp->f_iosize = mstat.f_iosize;
364         sbp->f_blocks = mstat.f_blocks;
365         sbp->f_bfree = mstat.f_bfree;
366         sbp->f_bavail = mstat.f_bavail;
367         sbp->f_files = mstat.f_files;
368         sbp->f_ffree = mstat.f_ffree;
369         if (sbp != &mp->mnt_stat) {
370                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
371                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
372                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
373         }
374         return (0);
375 }
376
377 static int
378 umapfs_sync(mp, waitfor, cred, p)
379         struct mount *mp;
380         int waitfor;
381         struct ucred *cred;
382         struct proc *p;
383 {
384         /*
385          * XXX - Assumes no data cached at umap layer.
386          */
387         return (0);
388 }
389
390 static int
391 umapfs_vget(mp, ino, vpp)
392         struct mount *mp;
393         ino_t ino;
394         struct vnode **vpp;
395 {
396
397         return (VFS_VGET(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, ino, vpp));
398 }
399
400 static int
401 umapfs_fhtovp(mp, fidp, vpp)
402         struct mount *mp;
403         struct fid *fidp;
404         struct vnode **vpp;
405 {
406         
407         return (VFS_FHTOVP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, fidp, vpp));
408 }
409
410 static int
411 umapfs_checkexp(mp, nam, exflagsp, credanonp)
412         struct mount *mp;
413         struct sockaddr *nam;
414         int *exflagsp;
415         struct ucred **credanonp;
416 {
417
418         return (VFS_CHECKEXP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, nam, 
419                 exflagsp, credanonp));
420 }
421
422 static int
423 umapfs_vptofh(vp, fhp)
424         struct vnode *vp;
425         struct fid *fhp;
426 {
427         return (VFS_VPTOFH(UMAPVPTOLOWERVP(vp), fhp));
428 }
429
430 static int
431 umapfs_extattrctl(mp, cmd, attrname, arg, p)
432         struct mount *mp;
433         int cmd;
434         const char *attrname;
435         caddr_t arg;
436         struct proc *p;
437 {
438         return (VFS_EXTATTRCTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, attrname,
439             arg, p));
440
441
442
443 static struct vfsops umap_vfsops = {
444         umapfs_mount,
445         umapfs_start,
446         umapfs_unmount,
447         umapfs_root,
448         umapfs_quotactl,
449         umapfs_statfs,
450         umapfs_sync,
451         umapfs_vget,
452         umapfs_fhtovp,
453         umapfs_checkexp,
454         umapfs_vptofh,
455         umapfs_init,
456         vfs_stduninit,
457         umapfs_extattrctl,
458 };
459
460 VFS_SET(umap_vfsops, umap, VFCF_LOOPBACK);