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