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