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