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