Remove the thread argument from all mount->vfs_* function vectors,
[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.20 2006/05/06 18:48: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 ucred *cred);
68 static int      umapfs_quotactl (struct mount *mp, int cmd, uid_t uid,
69                                      caddr_t arg, struct ucred *cred);
70 static int      umapfs_root (struct mount *mp, struct vnode **vpp);
71 static int      umapfs_statfs (struct mount *mp, struct statfs *sbp,
72                                 struct ucred *cred);
73 static int      umapfs_unmount (struct mount *mp, int mntflags);
74 static int      umapfs_vget (struct mount *mp, ino_t ino,
75                                  struct vnode **vpp);
76 static int      umapfs_vptofh (struct vnode *vp, struct fid *fhp);
77 static int      umapfs_extattrctl (struct mount *mp, int cmd,
78                                        const char *attrname, caddr_t arg,
79                                        struct ucred *cred);
80
81 /*
82  * Mount umap layer
83  */
84 static int
85 umapfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
86 {
87         struct umap_args args;
88         struct vnode *lowerrootvp, *vp;
89         struct vnode *umapm_rootvp;
90         struct umap_mount *amp;
91         struct nlookupdata nd;
92         u_int size;
93         int error;
94 #ifdef DEBUG
95         int     i;
96 #endif
97
98         /*
99          * Only for root
100          */
101         if ((error = suser_cred(cred, 0)) != 0)
102                 return (error);
103
104 #ifdef DEBUG
105         printf("umapfs_mount(mp = %p)\n", (void *)mp);
106 #endif
107
108         /*
109          * Update is a no-op
110          */
111         if (mp->mnt_flag & MNT_UPDATE) {
112                 return (EOPNOTSUPP);
113         }
114
115         /*
116          * Get argument
117          */
118         error = copyin(data, (caddr_t)&args, sizeof(struct umap_args));
119         if (error)
120                 return (error);
121
122         /*
123          * Find lower node
124          */
125         lowerrootvp = NULL;
126         error = nlookup_init(&nd, args.target, UIO_USERSPACE, NLC_FOLLOW);
127         if (error == 0)
128                 error = nlookup(&nd);
129         if (error == 0) {
130                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, 
131                                         &lowerrootvp);
132         }
133         nlookup_done(&nd);
134         if (error)
135                 return (error);
136
137         /*
138          * Sanity check on lower vnode
139          */
140 #ifdef DEBUG
141         printf("vp = %p, check for VDIR...\n", (void *)lowerrootvp);
142 #endif
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         if (args.nentries > MAPFILEENTRIES || args.gnentries >
164             GMAPFILEENTRIES) {
165                 vput(lowerrootvp);
166                 return (error);
167         }
168
169         amp->info_nentries = args.nentries;
170         amp->info_gnentries = args.gnentries;
171         error = copyin(args.mapdata, (caddr_t)amp->info_mapdata,
172             2*sizeof(u_long)*args.nentries);
173         if (error)
174                 return (error);
175
176 #ifdef DEBUG
177         printf("umap_mount:nentries %d\n",args.nentries);
178         for (i = 0; i < args.nentries; i++)
179                 printf("   %lu maps to %lu\n", amp->info_mapdata[i][0],
180                     amp->info_mapdata[i][1]);
181 #endif
182
183         error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
184             2*sizeof(u_long)*args.gnentries);
185         if (error)
186                 return (error);
187
188 #ifdef DEBUG
189         printf("umap_mount:gnentries %d\n",args.gnentries);
190         for (i = 0; i < args.gnentries; i++)
191                 printf("        group %lu maps to %lu\n",
192                     amp->info_gmapdata[i][0],
193                     amp->info_gmapdata[i][1]);
194 #endif
195
196         vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops,
197                          umap_vnodeop_entries, 0);
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, 0);
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(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
230             &size);
231         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
232         (void)umapfs_statfs(mp, &mp->mnt_stat, cred);
233         return (0);
234 }
235
236 /*
237  * Free reference to umap layer
238  */
239 static int
240 umapfs_unmount(struct mount *mp, int mntflags)
241 {
242         int error;
243         int flags = 0;
244
245 #ifdef DEBUG
246         printf("umapfs_unmount(mp = %p)\n", (void *)mp);
247 #endif
248
249         if (mntflags & MNT_FORCE)
250                 flags |= FORCECLOSE;
251
252         /*
253          * Clear out buffer cache.  I don't think we
254          * ever get anything cached at this level at the
255          * moment, but who knows...
256          */
257 #ifdef notyet
258         mntflushbuf(mp, 0);
259         if (mntinvalbuf(mp, 1))
260                 return (EBUSY);
261 #endif
262         /* There is 1 extra root vnode reference (umapm_rootvp). */
263         error = vflush(mp, 1, flags);
264         if (error)
265                 return (error);
266
267         /*
268          * Finally, throw away the umap_mount structure
269          */
270         free(mp->mnt_data, M_UMAPFSMNT);        /* XXX */
271         mp->mnt_data = 0;
272         return (0);
273 }
274
275 static int
276 umapfs_root(struct mount *mp, struct vnode **vpp)
277 {
278         struct vnode *vp;
279
280 #ifdef DEBUG
281         printf("umapfs_root(mp = %p, vp = %p->%p)\n",
282             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
283             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
284 #endif
285
286         /*
287          * Return locked reference to root.
288          */
289         vp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
290         vref(vp);
291         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
292         *vpp = vp;
293         return (0);
294 }
295
296 static int
297 umapfs_quotactl(struct mount *mp, int cmd, uid_t uid, 
298         caddr_t arg, struct ucred *cred)
299 {
300         return (VFS_QUOTACTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, uid, arg, cred));
301 }
302
303 static int
304 umapfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
305 {
306         int error;
307         struct statfs mstat;
308
309 #ifdef DEBUG
310         printf("umapfs_statfs(mp = %p, vp = %p->%p)\n",
311             (void *)mp, (void *)MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
312             (void *)UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
313 #endif
314
315         bzero(&mstat, sizeof(mstat));
316
317         error = VFS_STATFS(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, &mstat, cred);
318         if (error)
319                 return (error);
320
321         /* now copy across the "interesting" information and fake the rest */
322         sbp->f_type = mstat.f_type;
323         sbp->f_flags = mstat.f_flags;
324         sbp->f_bsize = mstat.f_bsize;
325         sbp->f_iosize = mstat.f_iosize;
326         sbp->f_blocks = mstat.f_blocks;
327         sbp->f_bfree = mstat.f_bfree;
328         sbp->f_bavail = mstat.f_bavail;
329         sbp->f_files = mstat.f_files;
330         sbp->f_ffree = mstat.f_ffree;
331         if (sbp != &mp->mnt_stat) {
332                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
333                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
334         }
335         return (0);
336 }
337
338 static int
339 umapfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
340 {
341
342         return (VFS_VGET(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, ino, vpp));
343 }
344
345 static int
346 umapfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
347 {
348         
349         return (VFS_FHTOVP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, fidp, vpp));
350 }
351
352 static int
353 umapfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
354         struct ucred **credanonp)
355 {
356
357         return (VFS_CHECKEXP(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, nam, 
358                 exflagsp, credanonp));
359 }
360
361 static int
362 umapfs_vptofh(struct vnode *vp, struct fid *fhp)
363 {
364         return (VFS_VPTOFH(UMAPVPTOLOWERVP(vp), fhp));
365 }
366
367 static int
368 umapfs_extattrctl(struct mount *mp, int cmd, const char *attrname,
369         caddr_t arg, struct ucred *cred)
370 {
371         return (VFS_EXTATTRCTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, attrname,
372             arg, cred));
373
374
375
376 static struct vfsops umap_vfsops = {
377         .vfs_mount =            umapfs_mount,
378         .vfs_unmount =          umapfs_unmount,
379         .vfs_root =             umapfs_root,
380         .vfs_quotactl =         umapfs_quotactl,
381         .vfs_statfs =           umapfs_statfs,
382         .vfs_sync =             vfs_stdsync,
383         .vfs_vget =             umapfs_vget,
384         .vfs_fhtovp =           umapfs_fhtovp,
385         .vfs_checkexp =         umapfs_checkexp,
386         .vfs_vptofh =           umapfs_vptofh,
387         .vfs_init =             umapfs_init,
388         .vfs_extattrctl =   umapfs_extattrctl
389 };
390
391 VFS_SET(umap_vfsops, umap, VFCF_LOOPBACK);