autofs_vfsops.c revision 274234
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: stable/10/sys/fs/autofs/autofs_vfsops.c 274234 2014-11-07 15:44:03Z 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/sx.h>
43#include <sys/taskqueue.h>
44#include <sys/vnode.h>
45
46#include <fs/autofs/autofs.h>
47
48static const char *autofs_opts[] = {
49	"from", "master_options", "master_prefix", NULL
50};
51
52extern struct autofs_softc	*autofs_softc;
53
54static int
55autofs_mount(struct mount *mp)
56{
57	struct autofs_mount *amp;
58	char *from, *fspath, *options, *prefix;
59	int error;
60
61	if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
62		return (EINVAL);
63
64	if (mp->mnt_flag & MNT_UPDATE)
65		return (0);
66
67	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
68		return (EINVAL);
69
70	if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
71		return (EINVAL);
72
73	if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
74		return (EINVAL);
75
76	if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
77		return (EINVAL);
78
79	amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
80	mp->mnt_data = amp;
81	amp->am_mp = mp;
82	strlcpy(amp->am_from, from, sizeof(amp->am_from));
83	strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
84	strlcpy(amp->am_options, options, sizeof(amp->am_options));
85	strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
86	sx_init(&amp->am_lock, "autofslk");
87	amp->am_last_fileno = 1;
88
89	vfs_getnewfsid(mp);
90
91	AUTOFS_XLOCK(amp);
92	error = autofs_node_new(NULL, amp, ".", -1, &amp->am_root);
93	if (error != 0) {
94		AUTOFS_XUNLOCK(amp);
95		free(amp, M_AUTOFS);
96		return (error);
97	}
98	AUTOFS_XUNLOCK(amp);
99
100	vfs_mountedfrom(mp, from);
101
102	return (0);
103}
104
105static int
106autofs_unmount(struct mount *mp, int mntflags)
107{
108	struct autofs_mount *amp;
109	struct autofs_node *anp;
110	struct autofs_request *ar;
111	int error, flags;
112	bool found;
113
114	amp = VFSTOAUTOFS(mp);
115
116	flags = 0;
117	if (mntflags & MNT_FORCE)
118		flags |= FORCECLOSE;
119	error = vflush(mp, 0, flags, curthread);
120	if (error != 0) {
121		AUTOFS_WARN("vflush failed with error %d", error);
122		return (error);
123	}
124
125	/*
126	 * All vnodes are gone, and new one will not appear - so,
127	 * no new triggerings.  We can iterate over outstanding
128	 * autofs_requests and terminate them.
129	 */
130	for (;;) {
131		found = false;
132		sx_xlock(&autofs_softc->sc_lock);
133		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
134			if (ar->ar_mount != amp)
135				continue;
136			ar->ar_error = ENXIO;
137			ar->ar_done = true;
138			ar->ar_in_progress = false;
139			found = true;
140		}
141		sx_xunlock(&autofs_softc->sc_lock);
142		if (found == false)
143			break;
144
145		cv_broadcast(&autofs_softc->sc_cv);
146		pause("autofs_umount", 1);
147	}
148
149	AUTOFS_XLOCK(amp);
150
151	/*
152	 * Not terribly efficient, but at least not recursive.
153	 */
154	while (!TAILQ_EMPTY(&amp->am_root->an_children)) {
155		anp = TAILQ_FIRST(&amp->am_root->an_children);
156		while (!TAILQ_EMPTY(&anp->an_children))
157			anp = TAILQ_FIRST(&anp->an_children);
158		autofs_node_delete(anp);
159	}
160	autofs_node_delete(amp->am_root);
161
162	mp->mnt_data = NULL;
163	AUTOFS_XUNLOCK(amp);
164
165	sx_destroy(&amp->am_lock);
166
167	free(amp, M_AUTOFS);
168
169	return (0);
170}
171
172static int
173autofs_root(struct mount *mp, int flags, struct vnode **vpp)
174{
175	struct autofs_mount *amp;
176	int error;
177
178	amp = VFSTOAUTOFS(mp);
179
180	error = autofs_node_vn(amp->am_root, mp, vpp);
181
182	return (error);
183}
184
185static int
186autofs_statfs(struct mount *mp, struct statfs *sbp)
187{
188
189	sbp->f_bsize = 512;
190	sbp->f_iosize = 0;
191	sbp->f_blocks = 0;
192	sbp->f_bfree = 0;
193	sbp->f_bavail = 0;
194	sbp->f_files = 0;
195	sbp->f_ffree = 0;
196
197	return (0);
198}
199
200static struct vfsops autofs_vfsops = {
201	.vfs_fhtovp =		NULL, /* XXX */
202	.vfs_mount =		autofs_mount,
203	.vfs_unmount =		autofs_unmount,
204	.vfs_root =		autofs_root,
205	.vfs_statfs =		autofs_statfs,
206	.vfs_init =		autofs_init,
207	.vfs_uninit =		autofs_uninit,
208};
209
210VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
211MODULE_VERSION(autofs, 1);
212