1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014 The FreeBSD Foundation
5 *
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32#include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/conf.h>
38#include <sys/condvar.h>
39#include <sys/ioccom.h>
40#include <sys/kernel.h>
41#include <sys/module.h>
42#include <sys/mount.h>
43#include <sys/stat.h>
44#include <sys/sx.h>
45#include <sys/taskqueue.h>
46#include <sys/tree.h>
47#include <sys/vnode.h>
48
49#include <fs/autofs/autofs.h>
50
51static const char *autofs_opts[] = {
52	"from", "master_options", "master_prefix", NULL
53};
54
55extern struct autofs_softc	*autofs_softc;
56
57static int
58autofs_mount(struct mount *mp)
59{
60	struct autofs_mount *amp;
61	char *from, *fspath, *options, *prefix;
62	int error;
63
64	if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
65		return (EINVAL);
66
67	if (mp->mnt_flag & MNT_UPDATE) {
68		autofs_flush(VFSTOAUTOFS(mp));
69		return (0);
70	}
71
72	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
73		return (EINVAL);
74
75	if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
76		return (EINVAL);
77
78	if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
79		return (EINVAL);
80
81	if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
82		return (EINVAL);
83
84	amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
85	mp->mnt_data = amp;
86	amp->am_mp = mp;
87	strlcpy(amp->am_from, from, sizeof(amp->am_from));
88	strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
89	strlcpy(amp->am_options, options, sizeof(amp->am_options));
90	strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
91	sx_init(&amp->am_lock, "autofslk");
92	amp->am_last_fileno = 1;
93
94	vfs_getnewfsid(mp);
95
96	MNT_ILOCK(mp);
97	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
98	MNT_IUNLOCK(mp);
99
100	AUTOFS_XLOCK(amp);
101	error = autofs_node_new(NULL, amp, ".", -1, &amp->am_root);
102	if (error != 0) {
103		AUTOFS_XUNLOCK(amp);
104		free(amp, M_AUTOFS);
105		return (error);
106	}
107	AUTOFS_XUNLOCK(amp);
108
109	vfs_mountedfrom(mp, from);
110
111	return (0);
112}
113
114static int
115autofs_unmount(struct mount *mp, int mntflags)
116{
117	struct autofs_mount *amp;
118	struct autofs_node *anp;
119	struct autofs_request *ar;
120	int error, flags;
121	bool found;
122
123	amp = VFSTOAUTOFS(mp);
124
125	flags = 0;
126	if (mntflags & MNT_FORCE)
127		flags |= FORCECLOSE;
128	error = vflush(mp, 0, flags, curthread);
129	if (error != 0) {
130		AUTOFS_DEBUG("vflush failed with error %d", error);
131		return (error);
132	}
133
134	/*
135	 * All vnodes are gone, and new one will not appear - so,
136	 * no new triggerings.  We can iterate over outstanding
137	 * autofs_requests and terminate them.
138	 */
139	for (;;) {
140		found = false;
141		sx_xlock(&autofs_softc->sc_lock);
142		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
143			if (ar->ar_mount != amp)
144				continue;
145			ar->ar_error = ENXIO;
146			ar->ar_done = true;
147			ar->ar_in_progress = false;
148			found = true;
149		}
150		sx_xunlock(&autofs_softc->sc_lock);
151		if (found == false)
152			break;
153
154		cv_broadcast(&autofs_softc->sc_cv);
155		pause("autofs_umount", 1);
156	}
157
158	AUTOFS_XLOCK(amp);
159
160	/*
161	 * Not terribly efficient, but at least not recursive.
162	 */
163	while (!RB_EMPTY(&amp->am_root->an_children)) {
164		anp = RB_MIN(autofs_node_tree, &amp->am_root->an_children);
165		while (!RB_EMPTY(&anp->an_children))
166			anp = RB_MIN(autofs_node_tree, &anp->an_children);
167		autofs_node_delete(anp);
168	}
169	autofs_node_delete(amp->am_root);
170
171	mp->mnt_data = NULL;
172	AUTOFS_XUNLOCK(amp);
173
174	sx_destroy(&amp->am_lock);
175
176	free(amp, M_AUTOFS);
177
178	return (0);
179}
180
181static int
182autofs_root(struct mount *mp, int flags, struct vnode **vpp)
183{
184	struct autofs_mount *amp;
185	int error;
186
187	amp = VFSTOAUTOFS(mp);
188
189	error = autofs_node_vn(amp->am_root, mp, flags, vpp);
190
191	return (error);
192}
193
194static int
195autofs_statfs(struct mount *mp, struct statfs *sbp)
196{
197
198	sbp->f_bsize = S_BLKSIZE;
199	sbp->f_iosize = 0;
200	sbp->f_blocks = 0;
201	sbp->f_bfree = 0;
202	sbp->f_bavail = 0;
203	sbp->f_files = 0;
204	sbp->f_ffree = 0;
205
206	return (0);
207}
208
209static struct vfsops autofs_vfsops = {
210	.vfs_fhtovp =		NULL, /* XXX */
211	.vfs_mount =		autofs_mount,
212	.vfs_unmount =		autofs_unmount,
213	.vfs_root =		autofs_root,
214	.vfs_statfs =		autofs_statfs,
215	.vfs_init =		autofs_init,
216	.vfs_uninit =		autofs_uninit,
217};
218
219VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
220MODULE_VERSION(autofs, 1);
221