Merge from vendor branch NTPD:
[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.14 2004/11/12 00:09:53 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/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->mnt_vn_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(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, td);
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(struct mount *mp, int flags, struct thread *td)
252 {
253         return (0);
254         /* return (VFS_START(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, flags, p)); */
255 }
256
257 /*
258  * Free reference to umap layer
259  */
260 static int
261 umapfs_unmount(struct mount *mp, int mntflags, struct thread *td)
262 {
263         int error;
264         int flags = 0;
265
266 #ifdef DEBUG
267         printf("umapfs_unmount(mp = %p)\n", (void *)mp);
268 #endif
269
270         if (mntflags & MNT_FORCE)
271                 flags |= FORCECLOSE;
272
273         /*
274          * Clear out buffer cache.  I don't think we
275          * ever get anything cached at this level at the
276          * moment, but who knows...
277          */
278 #ifdef notyet
279         mntflushbuf(mp, 0);
280         if (mntinvalbuf(mp, 1))
281                 return (EBUSY);
282 #endif
283         /* There is 1 extra root vnode reference (umapm_rootvp). */
284         error = vflush(mp, 1, flags);
285         if (error)
286                 return (error);
287
288         /*
289          * Finally, throw away the umap_mount structure
290          */
291         free(mp->mnt_data, M_UMAPFSMNT);        /* XXX */
292         mp->mnt_data = 0;
293         return (0);
294 }
295
296 static int
297 umapfs_root(struct mount *mp, struct vnode **vpp)
298 {
299         struct thread *td = curthread;  /* XXX */
300         struct vnode *vp;
301
302 #ifdef DEBUG
303         printf("umapfs_root(mp = %p, vp = %p->%p)\n",
304             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
305             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
306 #endif
307
308         /*
309          * Return locked reference to root.
310          */
311         vp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
312         vref(vp);
313         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
314         *vpp = vp;
315         return (0);
316 }
317
318 static int
319 umapfs_quotactl(struct mount *mp, int cmd, uid_t uid, 
320         caddr_t arg, struct thread *td)
321 {
322         return (VFS_QUOTACTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, uid, arg, td));
323 }
324
325 static int
326 umapfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
327 {
328         int error;
329         struct statfs mstat;
330
331 #ifdef DEBUG
332         printf("umapfs_statfs(mp = %p, vp = %p->%p)\n",
333             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
334             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
335 #endif
336
337         bzero(&mstat, sizeof(mstat));
338
339         error = VFS_STATFS(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, &mstat, td);
340         if (error)
341                 return (error);
342
343         /* now copy across the "interesting" information and fake the rest */
344         sbp->f_type = mstat.f_type;
345         sbp->f_flags = mstat.f_flags;
346         sbp->f_bsize = mstat.f_bsize;
347         sbp->f_iosize = mstat.f_iosize;
348         sbp->f_blocks = mstat.f_blocks;
349         sbp->f_bfree = mstat.f_bfree;
350         sbp->f_bavail = mstat.f_bavail;
351         sbp->f_files = mstat.f_files;
352         sbp->f_ffree = mstat.f_ffree;
353         if (sbp != &mp->mnt_stat) {
354                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
355                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
356                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
357         }
358         return (0);
359 }
360
361 static int
362 umapfs_sync(struct mount *mp, int waitfor, struct thread *td)
363 {
364         /*
365          * XXX - Assumes no data cached at umap layer.
366          */
367         return (0);
368 }
369
370 static int
371 umapfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
372 {
373
374         return (VFS_VGET(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, ino, vpp));
375 }
376
377 static int
378 umapfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
379 {
380         
381         return (VFS_FHTOVP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, fidp, vpp));
382 }
383
384 static int
385 umapfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
386         struct ucred **credanonp)
387 {
388
389         return (VFS_CHECKEXP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, nam, 
390                 exflagsp, credanonp));
391 }
392
393 static int
394 umapfs_vptofh(struct vnode *vp, struct fid *fhp)
395 {
396         return (VFS_VPTOFH(UMAPVPTOLOWERVP(vp), fhp));
397 }
398
399 static int
400 umapfs_extattrctl(struct mount *mp, int cmd, const char *attrname,
401         caddr_t arg, struct thread *td)
402 {
403         return (VFS_EXTATTRCTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, attrname,
404             arg, td));
405
406
407
408 static struct vfsops umap_vfsops = {
409         umapfs_mount,
410         umapfs_start,
411         umapfs_unmount,
412         umapfs_root,
413         umapfs_quotactl,
414         umapfs_statfs,
415         umapfs_sync,
416         umapfs_vget,
417         umapfs_fhtovp,
418         umapfs_checkexp,
419         umapfs_vptofh,
420         umapfs_init,
421         vfs_stduninit,
422         umapfs_extattrctl,
423 };
424
425 VFS_SET(umap_vfsops, umap, VFCF_LOOPBACK);