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