]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/cddl/compat/opensolaris/kern/opensolaris_vfs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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         int locked;
49
50         if (!(locked = mtx_owned(MNT_MTX(vfsp))))
51                 MNT_ILOCK(vfsp);
52
53         if (vfsp->mnt_opt == NULL) {
54                 void *opts;
55
56                 MNT_IUNLOCK(vfsp);
57                 opts = malloc(sizeof(*vfsp->mnt_opt), M_MOUNT, M_WAITOK);
58                 MNT_ILOCK(vfsp);
59                 if (vfsp->mnt_opt == NULL) {
60                         vfsp->mnt_opt = opts;
61                         TAILQ_INIT(vfsp->mnt_opt);
62                 } else {
63                         free(opts, M_MOUNT);
64                 }
65         }
66
67         MNT_IUNLOCK(vfsp);
68
69         opt = malloc(sizeof(*opt), M_MOUNT, M_WAITOK);
70         namesize = strlen(name) + 1;
71         opt->name = malloc(namesize, M_MOUNT, M_WAITOK);
72         strlcpy(opt->name, name, namesize);
73         opt->pos = -1;
74         opt->seen = 1;
75         if (arg == NULL) {
76                 opt->value = NULL;
77                 opt->len = 0;
78         } else {
79                 opt->len = strlen(arg) + 1;
80                 opt->value = malloc(opt->len, M_MOUNT, M_WAITOK);
81                 bcopy(arg, opt->value, opt->len);
82         }
83
84         MNT_ILOCK(vfsp);
85         TAILQ_INSERT_TAIL(vfsp->mnt_opt, opt, link);
86         if (!locked)
87                 MNT_IUNLOCK(vfsp);
88 }
89
90 void
91 vfs_clearmntopt(vfs_t *vfsp, const char *name)
92 {
93         int locked;
94
95         if (!(locked = mtx_owned(MNT_MTX(vfsp))))
96                 MNT_ILOCK(vfsp);
97         vfs_deleteopt(vfsp->mnt_opt, name);
98         if (!locked)
99                 MNT_IUNLOCK(vfsp);
100 }
101
102 int
103 vfs_optionisset(const vfs_t *vfsp, const char *opt, char **argp)
104 {
105         struct vfsoptlist *opts = vfsp->mnt_optnew;
106         int error;
107
108         if (opts == NULL)
109                 return (0);
110         error = vfs_getopt(opts, opt, (void **)argp, NULL);
111         return (error != 0 ? 0 : 1);
112 }
113
114 int
115 mount_snapshot(kthread_t *td, vnode_t **vpp, const char *fstype, char *fspath,
116     char *fspec, int fsflags)
117 {
118         struct mount *mp;
119         struct vfsconf *vfsp;
120         struct ucred *cr;
121         vnode_t *vp;
122         int error;
123
124         /*
125          * Be ultra-paranoid about making sure the type and fspath
126          * variables will fit in our mp buffers, including the
127          * terminating NUL.
128          */
129         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
130                 return (ENAMETOOLONG);
131
132         vfsp = vfs_byname_kld(fstype, td, &error);
133         if (vfsp == NULL)
134                 return (ENODEV);
135
136         vp = *vpp;
137         if (vp->v_type != VDIR)
138                 return (ENOTDIR);
139         /*
140          * We need vnode lock to protect v_mountedhere and vnode interlock
141          * to protect v_iflag.
142          */
143         vn_lock(vp, LK_SHARED | LK_RETRY);
144         VI_LOCK(vp);
145         if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
146                 VI_UNLOCK(vp);
147                 VOP_UNLOCK(vp, 0);
148                 return (EBUSY);
149         }
150         vp->v_iflag |= VI_MOUNT;
151         VI_UNLOCK(vp);
152         VOP_UNLOCK(vp, 0);
153
154         /*
155          * Allocate and initialize the filesystem.
156          */
157         mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
158
159         mp->mnt_optnew = NULL;
160         vfs_setmntopt(mp, "from", fspec, 0);
161         mp->mnt_optnew = mp->mnt_opt;
162         mp->mnt_opt = NULL;
163
164         /*
165          * Set the mount level flags.
166          */
167         mp->mnt_flag &= ~MNT_UPDATEMASK;
168         mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
169         /*
170          * Snapshots are always read-only.
171          */
172         mp->mnt_flag |= MNT_RDONLY;
173         /*
174          * We don't want snapshots to be visible in regular
175          * mount(8) and df(1) output.
176          */
177         mp->mnt_flag |= MNT_IGNORE;
178         /*
179          * Unprivileged user can trigger mounting a snapshot, but we don't want
180          * him to unmount it, so we switch to privileged of original mount.
181          */
182         crfree(mp->mnt_cred);
183         mp->mnt_cred = crdup(vp->v_mount->mnt_cred);
184         mp->mnt_stat.f_owner = mp->mnt_cred->cr_uid;
185         /*
186          * XXX: This is evil, but we can't mount a snapshot as a regular user.
187          * XXX: Is is safe when snapshot is mounted from within a jail?
188          */
189         cr = td->td_ucred;
190         td->td_ucred = kcred;
191         error = VFS_MOUNT(mp);
192         td->td_ucred = cr;
193
194         if (error == 0) {
195                 if (mp->mnt_opt != NULL)
196                         vfs_freeopts(mp->mnt_opt);
197                 mp->mnt_opt = mp->mnt_optnew;
198                 (void)VFS_STATFS(mp, &mp->mnt_stat);
199         }
200         /*
201          * Prevent external consumers of mount options from reading
202          * mnt_optnew.
203         */
204         mp->mnt_optnew = NULL;
205         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
206 #ifdef FREEBSD_NAMECACHE
207         cache_purge(vp);
208 #endif
209         VI_LOCK(vp);
210         vp->v_iflag &= ~VI_MOUNT;
211         VI_UNLOCK(vp);
212         if (error == 0) {
213                 vnode_t *mvp;
214
215                 vp->v_mountedhere = mp;
216                 /*
217                  * Put the new filesystem on the mount list.
218                  */
219                 mtx_lock(&mountlist_mtx);
220                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
221                 mtx_unlock(&mountlist_mtx);
222                 vfs_event_signal(NULL, VQ_MOUNT, 0);
223                 if (VFS_ROOT(mp, LK_EXCLUSIVE, &mvp))
224                         panic("mount: lost mount");
225                 vput(vp);
226                 vfs_unbusy(mp);
227                 *vpp = mvp;
228         } else {
229                 vput(vp);
230                 vfs_unbusy(mp);
231                 vfs_mount_destroy(mp);
232                 *vpp = NULL;
233         }
234         return (error);
235 }