Fix an endless recursion and double fault which could occur when accessing
[dragonfly.git] / sys / vfs / nullfs / null_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  * Jan-Simon Pendry.
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  *      @(#)null_vfsops.c       8.2 (Berkeley) 1/21/94
37  *
38  * @(#)lofs_vfsops.c    1.2 (Berkeley) 6/18/92
39  * $FreeBSD: src/sys/miscfs/nullfs/null_vfsops.c,v 1.35.2.3 2001/07/26 20:37:11 iedowse Exp $
40  * $DragonFly: src/sys/vfs/nullfs/null_vfsops.c,v 1.30 2008/09/05 23:27:12 dillon Exp $
41  */
42
43 /*
44  * Null Layer
45  * (See null_vnops.c for a description of what this does.)
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/nlookup.h>
56 #include "null.h"
57
58 extern struct vop_ops null_vnode_vops;
59
60 static MALLOC_DEFINE(M_NULLFSMNT, "NULLFS mount", "NULLFS mount structure");
61
62 static int      nullfs_root(struct mount *mp, struct vnode **vpp);
63 static int      nullfs_statfs(struct mount *mp, struct statfs *sbp,
64                                 struct ucred *cred);
65
66 /*
67  * Mount null layer
68  */
69 static int
70 nullfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
71 {
72         int error = 0;
73         struct null_args args;
74         struct vnode *rootvp;
75         struct null_mount *xmp;
76         u_int size;
77         struct nlookupdata nd;
78
79         NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
80
81         /*
82          * Update is a no-op
83          */
84         if (mp->mnt_flag & MNT_UPDATE) {
85                 return (EOPNOTSUPP);
86         }
87
88         /*
89          * Get argument
90          */
91         error = copyin(data, (caddr_t)&args, sizeof(struct null_args));
92         if (error)
93                 return (error);
94
95         /*
96          * Find lower node
97          */
98         rootvp = NULL;
99         error = nlookup_init(&nd, args.target, UIO_USERSPACE, NLC_FOLLOW);
100         if (error)
101                 goto fail1;
102         error = nlookup(&nd);
103         if (error)
104                 goto fail2;
105         error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &rootvp);
106         if (error)
107                 goto fail2;
108
109         xmp = (struct null_mount *) kmalloc(sizeof(struct null_mount),
110                                 M_NULLFSMNT, M_WAITOK); /* XXX */
111
112         /*
113          * Save reference to underlying FS
114          *
115          * As lite stacking enters the scene, the old way of doing this
116          * -- via the vnode -- is not good enough anymore.  Use the
117          * underlying filesystem's namecache handle as our mount point
118          * root, adjusting the mount to point to us. 
119          *
120          * NCF_ISMOUNTPT is normally set on the mount point, but we also
121          * want to set it on the base directory being mounted to prevent
122          * that directory from being destroyed out from under the nullfs
123          * mount. 
124          *
125          * The forwarding mount pointer (xmp->nullm_vfs) must be set to
126          * the actual target filesystem.  If the target filesystem was
127          * resolved via a nullfs mount nd.nl_nch.mount will be pointing
128          * to the nullfs mount structure instead of the target filesystem,
129          * which would otherwise cause the mount VOPS and VFSOPS to recurse
130          * endlessly.  If we are mounting via a nullfs mount we inherit
131          * its read-only state, if set.
132          */
133         xmp->nullm_vfs = nd.nl_nch.mount;
134         if (xmp->nullm_vfs != rootvp->v_mount) {
135                 if (xmp->nullm_vfs->mnt_flag & MNT_RDONLY)
136                         mp->mnt_flag |= MNT_RDONLY;
137                 xmp->nullm_vfs = rootvp->v_mount;
138         }
139
140         /*
141          * ncmountpt is the parent glue.  When mounting a nullfs via a nullfs
142          * we retain the parent nullfs to create a unique chain tuple.
143          */
144         mp->mnt_ncmountpt = nd.nl_nch;
145         cache_changemount(&mp->mnt_ncmountpt, mp);
146         mp->mnt_ncmountpt.ncp->nc_flag |= NCF_ISMOUNTPT;
147         cache_unlock(&mp->mnt_ncmountpt);
148         cache_zero(&nd.nl_nch);
149         nlookup_done(&nd);
150
151         vfs_add_vnodeops(mp, &null_vnode_vops, &mp->mnt_vn_norm_ops);
152
153         vn_unlock(rootvp);              /* leave reference intact */
154
155         /*
156          * Keep a held reference to the root vnode.
157          * It is vrele'd in nullfs_unmount.
158          */
159         xmp->nullm_rootvp = rootvp;
160         /*
161          * XXX What's the proper safety condition for querying
162          * the underlying mount? Is this flag tuning necessary
163          * at all?
164          */
165         if (xmp->nullm_vfs->mnt_flag & MNT_LOCAL)
166                 mp->mnt_flag |= MNT_LOCAL;
167         mp->mnt_data = (qaddr_t) xmp;
168         vfs_getnewfsid(mp);
169
170         (void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
171             &size);
172         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
173         (void)nullfs_statfs(mp, &mp->mnt_stat, cred);
174         NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
175                 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntfromname);
176
177         /*
178          * So unmount won't complain about namecache refs still existing
179          */
180         mp->mnt_kern_flag |= MNTK_NCALIASED;
181         return (0);
182 fail2:
183         nlookup_done(&nd);
184 fail1:
185         return (error);
186 }
187
188 /*
189  * Free reference to null layer
190  */
191 static int
192 nullfs_unmount(struct mount *mp, int mntflags)
193 {
194         struct null_mount *xmp;
195         int flags = 0;
196
197         NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
198
199         if (mntflags & MNT_FORCE)
200                 flags |= FORCECLOSE;
201
202         /*
203          * Finally, throw away the null_mount structure
204          */
205         xmp = (void *)mp->mnt_data;
206         mp->mnt_data = 0;
207         if (xmp->nullm_rootvp) {
208                 vrele(xmp->nullm_rootvp);
209                 xmp->nullm_rootvp = NULL;
210         }
211         kfree(xmp, M_NULLFSMNT);
212         return 0;
213 }
214
215 static int
216 nullfs_root(struct mount *mp, struct vnode **vpp)
217 {
218         struct vnode *vp;
219         int error;
220
221         NULLFSDEBUG("nullfs_root(mp = %p, vp = %p)\n", (void *)mp,
222             (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp);
223
224         /*
225          * Return locked reference to root.
226          */
227         vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
228         error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
229         if (error == 0)
230                 *vpp = vp;
231         return (error);
232 }
233
234 static int
235 nullfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg,
236                 struct ucred *cred)
237 {
238         return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, cred);
239 }
240
241 static int
242 nullfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
243 {
244         int error;
245         struct statfs mstat;
246
247         NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p)\n", (void *)mp,
248             (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp);
249
250         bzero(&mstat, sizeof(mstat));
251
252         error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, cred);
253         if (error)
254                 return (error);
255
256         /* now copy across the "interesting" information and fake the rest */
257         sbp->f_type = mstat.f_type;
258         sbp->f_flags = mstat.f_flags;
259         sbp->f_bsize = mstat.f_bsize;
260         sbp->f_iosize = mstat.f_iosize;
261         sbp->f_blocks = mstat.f_blocks;
262         sbp->f_bfree = mstat.f_bfree;
263         sbp->f_bavail = mstat.f_bavail;
264         sbp->f_files = mstat.f_files;
265         sbp->f_ffree = mstat.f_ffree;
266         if (sbp != &mp->mnt_stat) {
267                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
268                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
269         }
270         return (0);
271 }
272
273 static int
274 nullfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
275                 struct ucred **credanonp)
276 {
277
278         return VFS_CHECKEXP(MOUNTTONULLMOUNT(mp)->nullm_vfs, nam, 
279                 extflagsp, credanonp);
280 }
281
282 static int                        
283 nullfs_extattrctl(struct mount *mp, int cmd, const char *attrname, caddr_t arg,
284                   struct ucred *cred)
285 {
286         return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, attrname,
287             arg, cred);
288 }
289
290
291 static struct vfsops null_vfsops = {
292         .vfs_mount =            nullfs_mount,
293         .vfs_unmount =          nullfs_unmount,
294         .vfs_root =             nullfs_root,
295         .vfs_quotactl =         nullfs_quotactl,
296         .vfs_statfs =           nullfs_statfs,
297         .vfs_sync =             vfs_stdsync,
298         .vfs_checkexp =         nullfs_checkexp,
299         .vfs_extattrctl =       nullfs_extattrctl
300 };
301
302 VFS_SET(null_vfsops, null, VFCF_LOOPBACK);