opensolaris_vfs.c revision 302735
1131554Stjr/*-
2131554Stjr * Copyright (c) 2006-2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3131554Stjr * All rights reserved.
4131554Stjr *
5131554Stjr * Redistribution and use in source and binary forms, with or without
6131554Stjr * modification, are permitted provided that the following conditions
7131554Stjr * are met:
8131554Stjr * 1. Redistributions of source code must retain the above copyright
9131554Stjr *    notice, this list of conditions and the following disclaimer.
10131554Stjr * 2. Redistributions in binary form must reproduce the above copyright
11131554Stjr *    notice, this list of conditions and the following disclaimer in the
12131554Stjr *    documentation and/or other materials provided with the distribution.
13131554Stjr *
14131554Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15131554Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16131554Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17131554Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18131554Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19131554Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20131554Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21131554Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22131554Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23131554Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24131554Stjr * SUCH DAMAGE.
25131554Stjr */
26131554Stjr
27131554Stjr#include <sys/cdefs.h>
28131554Stjr__FBSDID("$FreeBSD: stable/10/sys/cddl/compat/opensolaris/kern/opensolaris_vfs.c 302735 2016-07-13 09:40:53Z avg $");
29131554Stjr
30131554Stjr#include <sys/param.h>
31131554Stjr#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
40MALLOC_DECLARE(M_MOUNT);
41
42void
43vfs_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
90void
91vfs_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
102int
103vfs_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
114int
115mount_snapshot(kthread_t *td, vnode_t **vpp, const char *fstype, char *fspath,
116    char *fspec, int fsflags)
117{
118	struct vfsconf *vfsp;
119	struct mount *mp;
120	vnode_t *vp, *mvp;
121	struct ucred *cr;
122	int error;
123
124	ASSERT_VOP_ELOCKED(*vpp, "mount_snapshot");
125
126	vp = *vpp;
127	*vpp = NULL;
128	error = 0;
129
130	/*
131	 * Be ultra-paranoid about making sure the type and fspath
132	 * variables will fit in our mp buffers, including the
133	 * terminating NUL.
134	 */
135	if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
136		error = ENAMETOOLONG;
137	if (error == 0 && (vfsp = vfs_byname_kld(fstype, td, &error)) == NULL)
138		error = ENODEV;
139	if (error == 0 && vp->v_type != VDIR)
140		error = ENOTDIR;
141	/*
142	 * We need vnode lock to protect v_mountedhere and vnode interlock
143	 * to protect v_iflag.
144	 */
145	if (error == 0) {
146		VI_LOCK(vp);
147		if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
148			vp->v_iflag |= VI_MOUNT;
149		else
150			error = EBUSY;
151		VI_UNLOCK(vp);
152	}
153	if (error != 0) {
154		vput(vp);
155		return (error);
156	}
157	VOP_UNLOCK(vp, 0);
158
159	/*
160	 * Allocate and initialize the filesystem.
161	 * We don't want regular user that triggered snapshot mount to be able
162	 * to unmount it, so pass credentials of the parent mount.
163	 */
164	mp = vfs_mount_alloc(vp, vfsp, fspath, vp->v_mount->mnt_cred);
165
166	mp->mnt_optnew = NULL;
167	vfs_setmntopt(mp, "from", fspec, 0);
168	mp->mnt_optnew = mp->mnt_opt;
169	mp->mnt_opt = NULL;
170
171	/*
172	 * Set the mount level flags.
173	 */
174	mp->mnt_flag = fsflags & MNT_UPDATEMASK;
175	/*
176	 * Snapshots are always read-only.
177	 */
178	mp->mnt_flag |= MNT_RDONLY;
179	/*
180	 * We don't want snapshots to allow access to vulnerable setuid
181	 * programs, so we turn off setuid when mounting snapshots.
182	 */
183	mp->mnt_flag |= MNT_NOSUID;
184	/*
185	 * We don't want snapshots to be visible in regular
186	 * mount(8) and df(1) output.
187	 */
188	mp->mnt_flag |= MNT_IGNORE;
189	/*
190	 * XXX: This is evil, but we can't mount a snapshot as a regular user.
191	 * XXX: Is is safe when snapshot is mounted from within a jail?
192	 */
193	cr = td->td_ucred;
194	td->td_ucred = kcred;
195	error = VFS_MOUNT(mp);
196	td->td_ucred = cr;
197
198	if (error != 0) {
199		VI_LOCK(vp);
200		vp->v_iflag &= ~VI_MOUNT;
201		VI_UNLOCK(vp);
202		vrele(vp);
203		vfs_unbusy(mp);
204		vfs_freeopts(mp->mnt_optnew);
205		vfs_mount_destroy(mp);
206		return (error);
207	}
208
209	if (mp->mnt_opt != NULL)
210		vfs_freeopts(mp->mnt_opt);
211	mp->mnt_opt = mp->mnt_optnew;
212	(void)VFS_STATFS(mp, &mp->mnt_stat);
213
214	/*
215	 * Prevent external consumers of mount options from reading
216	 * mnt_optnew.
217	*/
218	mp->mnt_optnew = NULL;
219
220	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
221#ifdef FREEBSD_NAMECACHE
222	cache_purge(vp);
223#endif
224	VI_LOCK(vp);
225	vp->v_iflag &= ~VI_MOUNT;
226	VI_UNLOCK(vp);
227
228	vp->v_mountedhere = mp;
229	/* Put the new filesystem on the mount list. */
230	mtx_lock(&mountlist_mtx);
231	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
232	mtx_unlock(&mountlist_mtx);
233	vfs_event_signal(NULL, VQ_MOUNT, 0);
234	if (VFS_ROOT(mp, LK_EXCLUSIVE, &mvp))
235		panic("mount: lost mount");
236	VOP_UNLOCK(vp, 0);
237	vfs_unbusy(mp);
238	*vpp = mvp;
239	return (0);
240}
241