autofs_vfsops.c revision 1.2 1 /*-
2 * Copyright (c) 2017 The NetBSD Foundation, Inc.
3 * Copyright (c) 2016 The DragonFly Project
4 * Copyright (c) 2014 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tomohiro Kusumi <kusumi.tomohiro (at) gmail.com>.
9 *
10 * This software was developed by Edward Tomasz Napierala under sponsorship
11 * from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: autofs_vfsops.c,v 1.2 2018/01/09 16:19:39 christos Exp $");
37
38
39 #include "autofs.h"
40 #include "autofs_mount.h"
41
42 #include <sys/stat.h>
43 #include <sys/sysctl.h>
44 #include <miscfs/genfs/genfs.h>
45
46 MODULE(MODULE_CLASS_VFS, autofs, NULL);
47
48 static int autofs_statvfs(struct mount *, struct statvfs *);
49 static int autofs_sysctl_create(void);
50
51 static void
52 autofs_init(void)
53 {
54
55 KASSERT(!autofs_softc);
56
57 autofs_softc = kmem_zalloc(sizeof(*autofs_softc), KM_SLEEP);
58
59 pool_init(&autofs_request_pool, sizeof(struct autofs_request), 0, 0, 0,
60 "autofs_request", &pool_allocator_nointr, IPL_NONE);
61 pool_init(&autofs_node_pool, sizeof(struct autofs_node), 0, 0, 0,
62 "autofs_node", &pool_allocator_nointr, IPL_NONE);
63
64 TAILQ_INIT(&autofs_softc->sc_requests);
65 cv_init(&autofs_softc->sc_cv, "autofscv");
66 mutex_init(&autofs_softc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
67 autofs_softc->sc_dev_opened = false;
68
69 autofs_sysctl_create();
70 workqueue_create(&autofs_tmo_wq, "autofstmo",
71 autofs_timeout_wq, NULL, 0, 0, WQ_MPSAFE);
72 }
73
74 static void
75 autofs_done(void)
76 {
77 KASSERT(autofs_softc);
78 KASSERT(!autofs_softc->sc_dev_opened);
79
80 workqueue_destroy(autofs_tmo_wq);
81
82 struct autofs_softc *sc = autofs_softc;
83 autofs_softc = NULL;
84
85 cv_destroy(&sc->sc_cv);
86 mutex_destroy(&sc->sc_lock);
87
88 pool_destroy(&autofs_request_pool);
89 pool_destroy(&autofs_node_pool);
90
91 kmem_free(sc, sizeof(*sc));
92 }
93
94 static int
95 autofs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
96 {
97 struct autofs_args *args = data;
98 struct autofs_mount *amp;
99 struct statvfs *sbp = &mp->mnt_stat;
100 int error;
101
102 if (!args)
103 return EINVAL;
104
105 /*
106 * MNT_GETARGS is unsupported. Autofs is mounted via automount(8) by
107 * parsing /etc/auto_master instead of regular mount(8) variants with
108 * -o getargs support, thus not really needed either.
109 */
110 if (mp->mnt_flag & MNT_GETARGS)
111 return EOPNOTSUPP;
112
113 if (mp->mnt_flag & MNT_UPDATE) {
114 autofs_flush(VFSTOAUTOFS(mp));
115 return 0;
116 }
117
118 /*
119 * Allocate the autofs mount.
120 */
121 amp = kmem_zalloc(sizeof(*amp), KM_SLEEP);
122 mp->mnt_data = amp;
123 amp->am_mp = mp;
124
125 /*
126 * Copy-in master_options string.
127 */
128 error = copyinstr(args->master_options, amp->am_options,
129 sizeof(amp->am_options), NULL);
130 if (error)
131 goto fail;
132
133 /*
134 * Copy-in master_prefix string.
135 */
136 error = copyinstr(args->master_prefix, amp->am_prefix,
137 sizeof(amp->am_prefix), NULL);
138 if (error)
139 goto fail;
140
141 /*
142 * Initialize the autofs mount.
143 */
144 mutex_init(&->am_lock, MUTEX_DEFAULT, IPL_NONE);
145 amp->am_last_ino = AUTOFS_ROOTINO;
146
147 mutex_enter(&->am_lock);
148 error = autofs_node_new(NULL, amp, ".", -1, &->am_root);
149 if (error) {
150 mutex_exit(&->am_lock);
151 goto fail;
152 }
153 mutex_exit(&->am_lock);
154 KASSERT(amp->am_root->an_ino == AUTOFS_ROOTINO);
155
156 autofs_statvfs(mp, sbp);
157 vfs_getnewfsid(mp);
158
159 error = set_statvfs_info(path, UIO_USERSPACE, args->from, UIO_USERSPACE,
160 mp->mnt_op->vfs_name, mp, curlwp);
161 if (error)
162 goto fail;
163 strlcpy(amp->am_from, sbp->f_mntfromname, sizeof(amp->am_from));
164 strlcpy(amp->am_on, sbp->f_mntonname, sizeof(amp->am_on));
165
166 return 0;
167
168 fail:
169 kmem_free(amp, sizeof(*amp));
170 return error;
171 }
172
173 static int
174 autofs_unmount(struct mount *mp, int mntflags)
175 {
176 struct autofs_mount *amp = VFSTOAUTOFS(mp);
177 int error, flags;
178
179 flags = 0;
180 if (mntflags & MNT_FORCE)
181 flags |= FORCECLOSE;
182 error = vflush(mp, NULL, flags);
183 if (error) {
184 AUTOFS_WARN("vflush failed with error %d", error);
185 return error;
186 }
187
188 /*
189 * All vnodes are gone, and new one will not appear - so,
190 * no new triggerings.
191 */
192 for (;;) {
193 struct autofs_request *ar;
194 int dummy;
195 bool found;
196
197 found = false;
198 mutex_enter(&autofs_softc->sc_lock);
199 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
200 if (ar->ar_mount != amp)
201 continue;
202 ar->ar_error = ENXIO;
203 ar->ar_done = true;
204 ar->ar_in_progress = false;
205 found = true;
206 }
207 if (found == false) {
208 mutex_exit(&autofs_softc->sc_lock);
209 break;
210 }
211
212 cv_broadcast(&autofs_softc->sc_cv);
213 mutex_exit(&autofs_softc->sc_lock);
214
215 tsleep(&dummy, 0, "autofs_umount", hz);
216 }
217
218 mutex_enter(&->am_lock);
219 while (!RB_EMPTY(&->am_root->an_children)) {
220 struct autofs_node *anp;
221 anp = RB_MIN(autofs_node_tree, &->am_root->an_children);
222 autofs_node_delete(anp);
223 }
224 autofs_node_delete(amp->am_root);
225 mp->mnt_data = NULL;
226 mutex_exit(&->am_lock);
227
228 mutex_destroy(&->am_lock);
229
230 kmem_free(amp, sizeof(*amp));
231
232 return 0;
233 }
234
235 static int
236 autofs_start(struct mount *mp, int flags)
237 {
238
239 return 0;
240 }
241
242 static int
243 autofs_root(struct mount *mp, struct vnode **vpp)
244 {
245 struct autofs_node *anp = VFSTOAUTOFS(mp)->am_root;
246 int error;
247
248 error = vcache_get(mp, &anp, sizeof(anp), vpp);
249 if (error)
250 return error;
251 error = vn_lock(*vpp, LK_EXCLUSIVE);
252 if (error) {
253 vrele(*vpp);
254 *vpp = NULL;
255 return error;
256 }
257
258 return 0;
259 }
260
261 static int
262 autofs_statvfs(struct mount *mp, struct statvfs *sbp)
263 {
264
265 sbp->f_bsize = S_BLKSIZE;
266 sbp->f_frsize = S_BLKSIZE;
267 sbp->f_iosize = 0;
268 sbp->f_blocks = 0;
269 sbp->f_bfree = 0;
270 sbp->f_bavail = 0;
271 sbp->f_bresvd = 0;
272 sbp->f_files = 0;
273 sbp->f_ffree = 0;
274 sbp->f_favail = 0;
275 sbp->f_fresvd = 0;
276
277 copy_statvfs_info(sbp, mp);
278
279 return 0;
280 }
281
282 static int
283 autofs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
284 {
285
286 return 0;
287 }
288
289 static int
290 autofs_loadvnode(struct mount *mp, struct vnode *vp,
291 const void *key, size_t key_len, const void **new_key)
292 {
293 struct autofs_node *anp;
294
295 KASSERT(key_len == sizeof(anp));
296 memcpy(&anp, key, key_len);
297 KASSERT(!anp->an_vnode);
298
299 vp->v_tag = VT_AUTOFS;
300 vp->v_type = VDIR;
301 vp->v_op = autofs_vnodeop_p;
302 vp->v_data = anp;
303
304 if (anp->an_ino == AUTOFS_ROOTINO)
305 vp->v_vflag |= VV_ROOT;
306
307 anp->an_vnode = vp;
308 uvm_vnp_setsize(vp, 0);
309
310 *new_key = &vp->v_data;
311
312 return 0;
313 }
314
315 static const struct vnodeopv_desc * const autofs_vnodeopv_descs[] = {
316 &autofs_vnodeop_opv_desc,
317 NULL,
318 };
319
320 static struct vfsops autofs_vfsops = {
321 .vfs_name = MOUNT_AUTOFS,
322 .vfs_min_mount_data = sizeof(struct autofs_args),
323 .vfs_mount = autofs_mount,
324 .vfs_start = autofs_start,
325 .vfs_unmount = autofs_unmount,
326 .vfs_root = autofs_root,
327 .vfs_quotactl = (void *)eopnotsupp,
328 .vfs_statvfs = autofs_statvfs,
329 .vfs_sync = autofs_sync,
330 .vfs_vget = (void *)eopnotsupp,
331 .vfs_loadvnode = (void *)autofs_loadvnode,
332 .vfs_newvnode = (void *)eopnotsupp,
333 .vfs_fhtovp = (void *)eopnotsupp,
334 .vfs_vptofh = (void *)eopnotsupp,
335 .vfs_init = autofs_init,
336 .vfs_reinit = (void *)eopnotsupp,
337 .vfs_done = autofs_done,
338 .vfs_mountroot = (void *)eopnotsupp,
339 .vfs_snapshot = (void *)eopnotsupp,
340 .vfs_extattrctl = (void *)eopnotsupp,
341 .vfs_suspendctl = (void *)genfs_suspendctl,
342 .vfs_renamelock_enter = (void *)eopnotsupp,
343 .vfs_renamelock_exit = (void *)eopnotsupp,
344 .vfs_fsync = (void *)eopnotsupp,
345 .vfs_opv_descs = autofs_vnodeopv_descs
346 };
347
348 #define AUTOFS_SYSCTL_DEBUG 1
349 #define AUTOFS_SYSCTL_MOUNT_ON_STAT 2
350 #define AUTOFS_SYSCTL_TIMEOUT 3
351 #define AUTOFS_SYSCTL_CACHE 4
352 #define AUTOFS_SYSCTL_RETRY_ATTEMPTS 5
353 #define AUTOFS_SYSCTL_RETRY_DELAY 6
354 #define AUTOFS_SYSCTL_INTERRUPTIBLE 7
355
356 static struct sysctllog *autofs_sysctl_log;
357
358 static int
359 autofs_sysctl_create(void)
360 {
361 int error;
362
363 /*
364 * XXX the "33" below could be dynamic, thereby eliminating one
365 * more instance of the "number to vfs" mapping problem, but
366 * "33" is the order as taken from sys/mount.h
367 */
368 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
369 CTLFLAG_PERMANENT,
370 CTLTYPE_NODE, "autofs",
371 SYSCTL_DESCR("Automounter filesystem"),
372 NULL, 0, NULL, 0,
373 CTL_VFS, 33, CTL_EOL);
374 if (error)
375 goto fail;
376
377 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
378 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
379 CTLTYPE_INT, "autofs_debug",
380 SYSCTL_DESCR("Enable debug messages"),
381 NULL, 0, &autofs_debug, 0,
382 CTL_VFS, 33, AUTOFS_SYSCTL_DEBUG, CTL_EOL);
383 if (error)
384 goto fail;
385
386 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
387 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
388 CTLTYPE_INT, "autofs_mount_on_stat",
389 SYSCTL_DESCR("Trigger mount on stat(2) on mountpoint"),
390 NULL, 0, &autofs_mount_on_stat, 0,
391 CTL_VFS, 33, AUTOFS_SYSCTL_MOUNT_ON_STAT, CTL_EOL);
392 if (error)
393 goto fail;
394
395 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
396 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
397 CTLTYPE_INT, "autofs_timeout",
398 SYSCTL_DESCR("Number of seconds to wait for automountd(8)"),
399 NULL, 0, &autofs_timeout, 0,
400 CTL_VFS, 33, AUTOFS_SYSCTL_TIMEOUT, CTL_EOL);
401 if (error)
402 goto fail;
403
404 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
405 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
406 CTLTYPE_INT, "autofs_cache",
407 SYSCTL_DESCR("Number of seconds to wait before reinvoking"),
408 NULL, 0, &autofs_cache, 0,
409 CTL_VFS, 33, AUTOFS_SYSCTL_CACHE, CTL_EOL);
410 if (error)
411 goto fail;
412
413 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
414 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
415 CTLTYPE_INT, "autofs_retry_attempts",
416 SYSCTL_DESCR("Number of attempts before failing mount"),
417 NULL, 0, &autofs_retry_attempts, 0,
418 CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_ATTEMPTS, CTL_EOL);
419 if (error)
420 goto fail;
421
422 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
423 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
424 CTLTYPE_INT, "autofs_retry_delay",
425 SYSCTL_DESCR("Number of seconds before retrying"),
426 NULL, 0, &autofs_retry_delay, 0,
427 CTL_VFS, 33, AUTOFS_SYSCTL_RETRY_DELAY, CTL_EOL);
428 if (error)
429 goto fail;
430
431 error = sysctl_createv(&autofs_sysctl_log, 0, NULL, NULL,
432 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
433 CTLTYPE_INT, "autofs_interruptible",
434 SYSCTL_DESCR("Allow requests to be interrupted by signal"),
435 NULL, 0, &autofs_interruptible, 0,
436 CTL_VFS, 33, AUTOFS_SYSCTL_INTERRUPTIBLE, CTL_EOL);
437 if (error)
438 goto fail;
439
440 return 0;
441 fail:
442 AUTOFS_WARN("sysctl_createv failed with error %d", error);
443
444 return error;
445 }
446
447 extern const struct cdevsw autofs_cdevsw;
448
449 static int
450 autofs_modcmd(modcmd_t cmd, void *arg)
451 {
452 #ifdef _MODULE
453 devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
454 #endif
455 int error = 0;
456
457 switch (cmd) {
458 case MODULE_CMD_INIT:
459 error = vfs_attach(&autofs_vfsops);
460 if (error)
461 break;
462 #ifdef _MODULE
463 error = devsw_attach("autofs", NULL, &bmajor, &autofs_cdevsw,
464 &cmajor);
465 if (error) {
466 vfs_detach(&autofs_vfsops);
467 break;
468 }
469 #endif
470 break;
471 case MODULE_CMD_FINI:
472 #ifdef _MODULE
473 KASSERT(autofs_softc);
474 mutex_enter(&autofs_softc->sc_lock);
475 if (autofs_softc->sc_dev_opened) {
476 mutex_exit(&autofs_softc->sc_lock);
477 error = EBUSY;
478 break;
479 }
480 mutex_exit(&autofs_softc->sc_lock);
481
482 error = devsw_detach(NULL, &autofs_cdevsw);
483 if (error)
484 break;
485 #endif
486 error = vfs_detach(&autofs_vfsops);
487 if (error)
488 break;
489 break;
490 default:
491 error = ENOTTY;
492 break;
493 }
494
495 return error;
496 }
497