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