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