]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/compat/opensolaris/kern/opensolaris_vfs.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / cddl / compat / opensolaris / kern / opensolaris_vfs.c
1 /*-
2  * Copyright (c) 2006-2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/mount.h>
35 #include <sys/cred.h>
36 #include <sys/vfs.h>
37 #include <sys/priv.h>
38 #include <sys/libkern.h>
39
40 MALLOC_DECLARE(M_MOUNT);
41
42 void
43 vfs_setmntopt(vfs_t *vfsp, const char *name, const char *arg,
44     int flags __unused)
45 {
46         struct vfsopt *opt;
47         size_t namesize;
48
49         if (vfsp->mnt_opt == NULL) {
50                 vfsp->mnt_opt = malloc(sizeof(*vfsp->mnt_opt), M_MOUNT, M_WAITOK);
51                 TAILQ_INIT(vfsp->mnt_opt);
52         }
53
54         opt = malloc(sizeof(*opt), M_MOUNT, M_WAITOK);
55
56         namesize = strlen(name) + 1;
57         opt->name = malloc(namesize, M_MOUNT, M_WAITOK);
58         strlcpy(opt->name, name, namesize);
59         opt->pos = -1;
60         opt->seen = 1;
61
62         if (arg == NULL) {
63                 opt->value = NULL;
64                 opt->len = 0;
65         } else {
66                 opt->len = strlen(arg) + 1;
67                 opt->value = malloc(opt->len, M_MOUNT, M_WAITOK);
68                 bcopy(arg, opt->value, opt->len);
69         }
70         /* TODO: Locking. */
71         TAILQ_INSERT_TAIL(vfsp->mnt_opt, opt, link);
72 }
73
74 void
75 vfs_clearmntopt(vfs_t *vfsp, const char *name)
76 {
77
78         /* TODO: Locking. */
79         vfs_deleteopt(vfsp->mnt_opt, name);
80 }
81
82 int
83 vfs_optionisset(const vfs_t *vfsp, const char *opt, char **argp)
84 {
85         struct vfsoptlist *opts = vfsp->mnt_optnew;
86         int error;
87
88         if (opts == NULL)
89                 return (0);
90         error = vfs_getopt(opts, opt, (void **)argp, NULL);
91         return (error != 0 ? 0 : 1);
92 }
93
94 int
95 domount(kthread_t *td, vnode_t *vp, const char *fstype, char *fspath,
96     char *fspec, int fsflags)
97 {
98         struct mount *mp;
99         struct vfsconf *vfsp;
100         struct ucred *cr;
101         int error;
102
103         /*
104          * Be ultra-paranoid about making sure the type and fspath
105          * variables will fit in our mp buffers, including the
106          * terminating NUL.
107          */
108         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
109                 return (ENAMETOOLONG);
110
111         vfsp = vfs_byname_kld(fstype, td, &error);
112         if (vfsp == NULL)
113                 return (ENODEV);
114
115         if (vp->v_type != VDIR)
116                 return (ENOTDIR);
117         VI_LOCK(vp);
118         if ((vp->v_iflag & VI_MOUNT) != 0 ||
119             vp->v_mountedhere != NULL) {
120                 VI_UNLOCK(vp);
121                 return (EBUSY);
122         }
123         vp->v_iflag |= VI_MOUNT;
124         VI_UNLOCK(vp);
125
126         /*
127          * Allocate and initialize the filesystem.
128          */
129         vn_lock(vp, LK_SHARED | LK_RETRY);
130         mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
131         VOP_UNLOCK(vp, 0);
132
133         mp->mnt_optnew = NULL;
134         vfs_setmntopt(mp, "from", fspec, 0);
135         mp->mnt_optnew = mp->mnt_opt;
136         mp->mnt_opt = NULL;
137
138         /*
139          * Set the mount level flags.
140          */
141         if (fsflags & MNT_RDONLY)
142                 mp->mnt_flag |= MNT_RDONLY;
143         mp->mnt_flag &=~ MNT_UPDATEMASK;
144         mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
145         /*
146          * Unprivileged user can trigger mounting a snapshot, but we don't want
147          * him to unmount it, so we switch to privileged of original mount.
148          */
149         crfree(mp->mnt_cred);
150         mp->mnt_cred = crdup(vp->v_mount->mnt_cred);
151         mp->mnt_stat.f_owner = mp->mnt_cred->cr_uid;
152         /*
153          * Mount the filesystem.
154          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
155          * get.  No freeing of cn_pnbuf.
156          */
157         /*
158          * XXX: This is evil, but we can't mount a snapshot as a regular user.
159          * XXX: Is is safe when snapshot is mounted from within a jail?
160          */
161         cr = td->td_ucred;
162         td->td_ucred = kcred;
163         error = VFS_MOUNT(mp);
164         td->td_ucred = cr;
165
166         if (!error) {
167                 if (mp->mnt_opt != NULL)
168                         vfs_freeopts(mp->mnt_opt);
169                 mp->mnt_opt = mp->mnt_optnew;
170                 (void)VFS_STATFS(mp, &mp->mnt_stat);
171         }
172         /*
173          * Prevent external consumers of mount options from reading
174          * mnt_optnew.
175         */
176         mp->mnt_optnew = NULL;
177         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
178         /*
179          * Put the new filesystem on the mount list after root.
180          */
181 #ifdef FREEBSD_NAMECACHE
182         cache_purge(vp);
183 #endif
184         if (!error) {
185                 vnode_t *mvp;
186
187                 VI_LOCK(vp);
188                 vp->v_iflag &= ~VI_MOUNT;
189                 VI_UNLOCK(vp);
190                 vp->v_mountedhere = mp;
191                 mtx_lock(&mountlist_mtx);
192                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
193                 mtx_unlock(&mountlist_mtx);
194                 vfs_event_signal(NULL, VQ_MOUNT, 0);
195                 if (VFS_ROOT(mp, LK_EXCLUSIVE, &mvp))
196                         panic("mount: lost mount");
197                 mountcheckdirs(vp, mvp);
198                 vput(mvp);
199                 VOP_UNLOCK(vp, 0);
200                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
201                         error = vfs_allocate_syncvnode(mp);
202                 vfs_unbusy(mp);
203                 if (error)
204                         vrele(vp);
205                 else
206                         vfs_mountedfrom(mp, fspec);
207         } else {
208                 VI_LOCK(vp);
209                 vp->v_iflag &= ~VI_MOUNT;
210                 VI_UNLOCK(vp);
211                 VOP_UNLOCK(vp, 0);
212                 vfs_unbusy(mp);
213                 vfs_mount_destroy(mp);
214         }
215         return (error);
216 }