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