xref: /dragonfly/sys/vfs/autofs/autofs_vfsops.c (revision 5062ee70)
1 /*-
2  * Copyright (c) 2016 The DragonFly Project
3  * Copyright (c) 2014 The FreeBSD Foundation
4  * All rights reserved.
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 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/stat.h>
35 
36 #include "autofs.h"
37 #include "autofs_mount.h"
38 
39 static int	autofs_statfs(struct mount *mp, struct statfs *sbp,
40 		    struct ucred *cred);
41 
42 extern struct autofs_softc	*autofs_softc;
43 
44 static int
45 autofs_mount(struct mount *mp, char *mntpt, caddr_t data, struct ucred *cred)
46 {
47 	struct autofs_mount_info info;
48 	struct autofs_mount *amp;
49 	struct statfs *sbp = &mp->mnt_stat;
50 	int error;
51 
52 	if (mp->mnt_flag & MNT_UPDATE) {
53 		autofs_flush(VFSTOAUTOFS(mp));
54 		return (0);
55 	}
56 
57 	error = copyin(data, &info, sizeof(info));
58 	if (error)
59 		return (error);
60 
61 	/*
62 	 * Copy-in ->f_mntfromname string.
63 	 */
64 	memset(sbp->f_mntfromname, 0, sizeof(sbp->f_mntfromname));
65 	error = copyinstr(info.from, sbp->f_mntfromname,
66 	    sizeof(sbp->f_mntfromname), NULL);
67 	if (error)
68 		return (error);
69 	/*
70 	 * Copy-in ->f_mntonname string.
71 	 */
72 	memset(sbp->f_mntonname, 0, sizeof(sbp->f_mntonname));
73 	error = copyinstr(mntpt, sbp->f_mntonname,
74 	    sizeof(sbp->f_mntonname), NULL);
75 	if (error)
76 		return (error);
77 
78 	/*
79 	 * Allocate the autofs mount.
80 	 */
81 	amp = kmalloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
82 	mp->mnt_data = (qaddr_t)amp;
83 	amp->am_mp = mp;
84 	strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
85 	strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
86 
87 	/*
88 	 * Copy-in master_options string.
89 	 */
90 	error = copyinstr(info.master_options, amp->am_options,
91 	    sizeof(amp->am_options), NULL);
92 	if (error)
93 		goto fail;
94 	/*
95 	 * Copy-in master_prefix string.
96 	 */
97 	error = copyinstr(info.master_prefix, amp->am_prefix,
98 	    sizeof(amp->am_prefix), NULL);
99 	if (error)
100 		goto fail;
101 
102 	/*
103 	 * Initialize the autofs mount.
104 	 */
105 	mtx_init(&amp->am_lock, "autofsmnlk");
106 	amp->am_last_ino = AUTOFS_ROOTINO;
107 
108 	vfs_getnewfsid(mp);
109 	vfs_add_vnodeops(mp, &autofs_vnode_vops, &mp->mnt_vn_norm_ops);
110 
111 	mtx_lock_ex_quick(&amp->am_lock);
112 	error = autofs_node_new(NULL, amp, ".", -1, &amp->am_root);
113 	mtx_unlock_ex(&amp->am_lock);
114 	KKASSERT(error == 0);
115 	KKASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
116 
117 	autofs_statfs(mp, sbp, cred);
118 
119 	return (0);
120 
121 fail:
122 	kfree(amp, M_AUTOFS);
123 	return (error);
124 }
125 
126 static int
127 autofs_unmount(struct mount *mp, int mntflags)
128 {
129 	struct autofs_mount *amp = VFSTOAUTOFS(mp);
130 	int error, flags;
131 
132 	flags = 0;
133 	if (mntflags & MNT_FORCE)
134 		flags |= FORCECLOSE;
135 	error = vflush(mp, 0, flags);
136 	if (error) {
137 		AUTOFS_WARN("vflush failed with error %d", error);
138 		return (error);
139 	}
140 
141 	/*
142 	 * All vnodes are gone, and new one will not appear - so,
143 	 * no new triggerings.
144 	 */
145 	for (;;) {
146 		struct autofs_request *ar;
147 		int dummy;
148 		bool found;
149 
150 		found = false;
151 		lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
152 		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
153 			if (ar->ar_mount != amp)
154 				continue;
155 			ar->ar_error = ENXIO;
156 			ar->ar_done = true;
157 			ar->ar_in_progress = false;
158 			found = true;
159 		}
160 		if (found == false) {
161 			lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
162 			break;
163 		}
164 
165 		cv_broadcast(&autofs_softc->sc_cv);
166 		lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
167 
168 		tsleep(&dummy, 0, "autofs_umount", hz);
169 	}
170 
171 	mtx_lock_ex_quick(&amp->am_lock);
172 	while (!RB_EMPTY(&amp->am_root->an_children)) {
173 		struct autofs_node *anp;
174 		anp = RB_MIN(autofs_node_tree, &amp->am_root->an_children);
175 		if (!RB_EMPTY(&anp->an_children)) {
176 			AUTOFS_DEBUG("%s has children", anp->an_name);
177 			mtx_unlock_ex(&amp->am_lock);
178 			return EBUSY;
179 		}
180 		autofs_node_delete(anp);
181 	}
182 	autofs_node_delete(amp->am_root);
183 	mp->mnt_data = NULL;
184 	mtx_unlock_ex(&amp->am_lock);
185 
186 	mtx_uninit(&amp->am_lock);
187 
188 	kfree(amp, M_AUTOFS);
189 
190 	return (0);
191 }
192 
193 static int
194 autofs_root(struct mount *mp, struct vnode **vpp)
195 {
196 	struct autofs_mount *amp = VFSTOAUTOFS(mp);
197 	int error;
198 
199 	if (amp->am_root == NULL) {
200 		AUTOFS_FATAL("called without root node %p", mp);
201 		print_backtrace(-1);
202 		*vpp = NULL;
203 		error = EINVAL;
204 	} else {
205 		error = autofs_node_vn(amp->am_root, mp, LK_EXCLUSIVE, vpp);
206 		(*vpp)->v_flag |= VROOT;
207 		KKASSERT((*vpp)->v_type == VDIR);
208 	}
209 
210 	return (error);
211 }
212 
213 static int
214 autofs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
215 {
216 	sbp->f_bsize = S_BLKSIZE;
217 	sbp->f_iosize = 0;
218 	sbp->f_blocks = 0;
219 	sbp->f_bfree = 0;
220 	sbp->f_bavail = 0;
221 	sbp->f_files = 0;
222 	sbp->f_ffree = 0;
223 
224 	return (0);
225 }
226 
227 static int
228 autofs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
229 {
230 	sbp->f_bsize = S_BLKSIZE;
231 	sbp->f_frsize = 0;
232 	sbp->f_blocks = 0;
233 	sbp->f_bfree = 0;
234 	sbp->f_bavail = 0;
235 	sbp->f_files = 0;
236 	sbp->f_ffree = 0;
237 
238 	return (0);
239 }
240 
241 static struct vfsops autofs_vfsops = {
242 	.vfs_mount =		autofs_mount,
243 	.vfs_unmount =		autofs_unmount,
244 	.vfs_root =		autofs_root,
245 	.vfs_statfs =		autofs_statfs,
246 	.vfs_statvfs =		autofs_statvfs,
247 	.vfs_init =		autofs_init,
248 	.vfs_uninit =		autofs_uninit,
249 };
250 
251 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
252 MODULE_VERSION(autofs, 1);
253