Merge from vendor branch CVS:
[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.18 2005/09/17 07:43:12 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_statfs (struct mount *mp, struct statfs *sbp,
72                                    struct thread *td);
73 static int      umapfs_unmount (struct mount *mp, int mntflags,
74                                     struct thread *td);
75 static int      umapfs_vget (struct mount *mp, ino_t ino,
76                                  struct vnode **vpp);
77 static int      umapfs_vptofh (struct vnode *vp, struct fid *fhp);
78 static int      umapfs_extattrctl (struct mount *mp, int cmd,
79                                        const char *attrname, caddr_t arg,
80                                        struct thread *td);
81
82 /*
83  * Mount umap layer
84  */
85 static int
86 umapfs_mount(struct mount *mp, char *path, caddr_t data, struct thread *td)
87 {
88         struct umap_args args;
89         struct vnode *lowerrootvp, *vp;
90         struct vnode *umapm_rootvp;
91         struct umap_mount *amp;
92         struct nlookupdata nd;
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(td)) != 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         }
115
116         /*
117          * Get argument
118          */
119         error = copyin(data, (caddr_t)&args, sizeof(struct umap_args));
120         if (error)
121                 return (error);
122
123         /*
124          * Find lower node
125          */
126         lowerrootvp = NULL;
127         error = nlookup_init(&nd, args.target, UIO_USERSPACE, NLC_FOLLOW);
128         if (error == 0)
129                 error = nlookup(&nd);
130         if (error == 0) {
131                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, 
132                                         &lowerrootvp);
133         }
134         nlookup_done(&nd);
135         if (error)
136                 return (error);
137
138         /*
139          * Sanity check on lower vnode
140          */
141 #ifdef DEBUG
142         printf("vp = %p, check for VDIR...\n", (void *)lowerrootvp);
143 #endif
144         if (lowerrootvp->v_type != VDIR) {
145                 vput(lowerrootvp);
146                 return (EINVAL);
147         }
148
149 #ifdef DEBUG
150         printf("mp = %p\n", (void *)mp);
151 #endif
152
153         amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
154                                 M_UMAPFSMNT, M_WAITOK); /* XXX */
155
156         /*
157          * Save reference to underlying FS
158          */
159         amp->umapm_vfs = lowerrootvp->v_mount;
160
161         /*
162          * Now copy in the number of entries and maps for umap mapping.
163          */
164         if (args.nentries > MAPFILEENTRIES || args.gnentries >
165             GMAPFILEENTRIES) {
166                 vput(lowerrootvp);
167                 return (error);
168         }
169
170         amp->info_nentries = args.nentries;
171         amp->info_gnentries = args.gnentries;
172         error = copyin(args.mapdata, (caddr_t)amp->info_mapdata,
173             2*sizeof(u_long)*args.nentries);
174         if (error)
175                 return (error);
176
177 #ifdef DEBUG
178         printf("umap_mount:nentries %d\n",args.nentries);
179         for (i = 0; i < args.nentries; i++)
180                 printf("   %lu maps to %lu\n", amp->info_mapdata[i][0],
181                     amp->info_mapdata[i][1]);
182 #endif
183
184         error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
185             2*sizeof(u_long)*args.gnentries);
186         if (error)
187                 return (error);
188
189 #ifdef DEBUG
190         printf("umap_mount:gnentries %d\n",args.gnentries);
191         for (i = 0; i < args.gnentries; i++)
192                 printf("        group %lu maps to %lu\n",
193                     amp->info_gmapdata[i][0],
194                     amp->info_gmapdata[i][1]);
195 #endif
196
197         vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops,
198                          umap_vnodeop_entries, 0);
199
200         /*
201          * Save reference.  Each mount also holds
202          * a reference on the root vnode.
203          */
204         error = umap_node_create(mp, lowerrootvp, &vp);
205         /*
206          * Unlock the node (either the lower or the alias)
207          */
208         VOP_UNLOCK(vp, 0, td);
209         /*
210          * Make sure the node alias worked
211          */
212         if (error) {
213                 vrele(lowerrootvp);
214                 free(amp, M_UMAPFSMNT); /* XXX */
215                 return (error);
216         }
217
218         /*
219          * Keep a held reference to the root vnode.
220          * It is vrele'd in umapfs_unmount.
221          */
222         umapm_rootvp = vp;
223         umapm_rootvp->v_flag |= VROOT;
224         amp->umapm_rootvp = umapm_rootvp;
225         if (UMAPVPTOLOWERVP(umapm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
226                 mp->mnt_flag |= MNT_LOCAL;
227         mp->mnt_data = (qaddr_t) amp;
228         vfs_getnewfsid(mp);
229
230         (void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
231             &size);
232         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
233         (void)umapfs_statfs(mp, &mp->mnt_stat, td);
234         return (0);
235 }
236
237 /*
238  * Free reference to umap layer
239  */
240 static int
241 umapfs_unmount(struct mount *mp, int mntflags, struct thread *td)
242 {
243         int error;
244         int flags = 0;
245
246 #ifdef DEBUG
247         printf("umapfs_unmount(mp = %p)\n", (void *)mp);
248 #endif
249
250         if (mntflags & MNT_FORCE)
251                 flags |= FORCECLOSE;
252
253         /*
254          * Clear out buffer cache.  I don't think we
255          * ever get anything cached at this level at the
256          * moment, but who knows...
257          */
258 #ifdef notyet
259         mntflushbuf(mp, 0);
260         if (mntinvalbuf(mp, 1))
261                 return (EBUSY);
262 #endif
263         /* There is 1 extra root vnode reference (umapm_rootvp). */
264         error = vflush(mp, 1, flags);
265         if (error)
266                 return (error);
267
268         /*
269          * Finally, throw away the umap_mount structure
270          */
271         free(mp->mnt_data, M_UMAPFSMNT);        /* XXX */
272         mp->mnt_data = 0;
273         return (0);
274 }
275
276 static int
277 umapfs_root(struct mount *mp, struct vnode **vpp)
278 {
279         struct thread *td = curthread;  /* XXX */
280         struct vnode *vp;
281
282 #ifdef DEBUG
283         printf("umapfs_root(mp = %p, vp = %p->%p)\n",
284             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
285             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
286 #endif
287
288         /*
289          * Return locked reference to root.
290          */
291         vp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
292         vref(vp);
293         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
294         *vpp = vp;
295         return (0);
296 }
297
298 static int
299 umapfs_quotactl(struct mount *mp, int cmd, uid_t uid, 
300         caddr_t arg, struct thread *td)
301 {
302         return (VFS_QUOTACTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, uid, arg, td));
303 }
304
305 static int
306 umapfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
307 {
308         int error;
309         struct statfs mstat;
310
311 #ifdef DEBUG
312         printf("umapfs_statfs(mp = %p, vp = %p->%p)\n",
313             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
314             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
315 #endif
316
317         bzero(&mstat, sizeof(mstat));
318
319         error = VFS_STATFS(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, &mstat, td);
320         if (error)
321                 return (error);
322
323         /* now copy across the "interesting" information and fake the rest */
324         sbp->f_type = mstat.f_type;
325         sbp->f_flags = mstat.f_flags;
326         sbp->f_bsize = mstat.f_bsize;
327         sbp->f_iosize = mstat.f_iosize;
328         sbp->f_blocks = mstat.f_blocks;
329         sbp->f_bfree = mstat.f_bfree;
330         sbp->f_bavail = mstat.f_bavail;
331         sbp->f_files = mstat.f_files;
332         sbp->f_ffree = mstat.f_ffree;
333         if (sbp != &mp->mnt_stat) {
334                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
335                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
336         }
337         return (0);
338 }
339
340 static int
341 umapfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
342 {
343
344         return (VFS_VGET(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, ino, vpp));
345 }
346
347 static int
348 umapfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
349 {
350         
351         return (VFS_FHTOVP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, fidp, vpp));
352 }
353
354 static int
355 umapfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
356         struct ucred **credanonp)
357 {
358
359         return (VFS_CHECKEXP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, nam, 
360                 exflagsp, credanonp));
361 }
362
363 static int
364 umapfs_vptofh(struct vnode *vp, struct fid *fhp)
365 {
366         return (VFS_VPTOFH(UMAPVPTOLOWERVP(vp), fhp));
367 }
368
369 static int
370 umapfs_extattrctl(struct mount *mp, int cmd, const char *attrname,
371         caddr_t arg, struct thread *td)
372 {
373         return (VFS_EXTATTRCTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, attrname,
374             arg, td));
375
376
377
378 static struct vfsops umap_vfsops = {
379         .vfs_mount =            umapfs_mount,
380         .vfs_unmount =          umapfs_unmount,
381         .vfs_root =             umapfs_root,
382         .vfs_quotactl =         umapfs_quotactl,
383         .vfs_statfs =           umapfs_statfs,
384         .vfs_sync =             vfs_stdsync,
385         .vfs_vget =             umapfs_vget,
386         .vfs_fhtovp =           umapfs_fhtovp,
387         .vfs_checkexp =         umapfs_checkexp,
388         .vfs_vptofh =           umapfs_vptofh,
389         .vfs_init =             umapfs_init,
390         .vfs_extattrctl =   umapfs_extattrctl
391 };
392
393 VFS_SET(umap_vfsops, umap, VFCF_LOOPBACK);